1 #define GST_USE_UNSTABLE_API
2 #define HAVE_MEMFD_CREATE
11 #include <wayland-client.h>
17 #include "xdg-shell-client-protocol.h"
18 #include "agl-shell-desktop-client-protocol.h"
23 #include <gst/video/videooverlay.h>
24 #include <gst/wayland/wayland.h>
26 #if !GST_CHECK_VERSION(1, 22, 0)
27 #define gst_is_wl_display_handle_need_context_message gst_is_wayland_display_handle_need_context_message
28 #define gst_wl_display_handle_context_new gst_wayland_display_handle_context_new
31 // these only applies if the window is a dialog/pop-up one
32 // by default the compositor make the window maximized
33 #define WINDOW_WIDTH_SIZE 640
34 #define WINDOW_HEIGHT_SIZE 720
36 #define WINDOW_WIDTH_POS_X 640
37 #define WINDOW_WIDTH_POS_Y 180
39 #define MAX_BUFFER_ALLOC 2
41 // C++ requires a cast and we in wayland we do the cast implictly
42 #define WL_ARRAY_FOR_EACH(pos, array, type) \
43 for (pos = (type)(array)->data; \
44 (const char *) pos < ((const char *) (array)->data + (array)->size); \
48 struct wl_display *wl_display;
49 struct wl_registry *wl_registry;
50 struct wl_compositor *wl_compositor;
51 struct wl_output *wl_output;
59 struct xdg_wm_base *wm_base;
60 struct agl_shell_desktop *agl_shell_desktop;
66 struct wl_buffer *buffer;
70 size_t size; /* width * 4 * height */
71 struct wl_list buffer_link; /** window::buffer_list */
75 struct display *display;
82 struct wl_surface *surface;
84 struct xdg_surface *xdg_surface;
85 struct xdg_toplevel *xdg_toplevel;
86 bool wait_for_configure;
88 int fullscreen, maximized;
90 struct wl_list buffer_list;
91 struct wl_callback *callback;
92 bool needs_update_buffer;
96 struct receiver_data {
97 struct window *window;
100 GstVideoOverlay *overlay;
103 static int running = 1;
104 static bool gst_pipeline_failed = FALSE;
105 static bool fallback_gst_pipeline_tried = FALSE;
108 redraw(void *data, struct wl_callback *callback, uint32_t time);
110 static struct buffer *
111 alloc_buffer(struct window *window, int width, int height)
113 struct buffer *buffer = static_cast<struct buffer *>(calloc(1, sizeof(*buffer)));
115 buffer->width = width;
116 buffer->height = height;
117 wl_list_insert(&window->buffer_list, &buffer->buffer_link);
123 destroy_buffer(struct buffer *buffer)
126 wl_buffer_destroy(buffer->buffer);
128 munmap(buffer->shm_data, buffer->size);
129 wl_list_remove(&buffer->buffer_link);
133 static struct buffer *
134 pick_free_buffer(struct window *window)
137 struct buffer *buffer = NULL;
139 wl_list_for_each(b, &window->buffer_list, buffer_link) {
150 prune_old_released_buffers(struct window *window)
152 struct buffer *b, *b_next;
154 wl_list_for_each_safe(b, b_next,
155 &window->buffer_list, buffer_link) {
156 if (!b->busy && (b->width != window->width ||
157 b->height != window->height))
163 paint_pixels(void *image, int padding, int width, int height, uint32_t time)
165 memset(image, 0x00, width * height * 4);
169 buffer_release(void *data, struct wl_buffer *buffer)
171 struct buffer *mybuf = static_cast<struct buffer *>(data);
175 static const struct wl_buffer_listener buffer_listener = {
181 create_shm_buffer(struct display *display, struct buffer *buffer,
182 int width, int height, uint32_t format)
184 struct wl_shm_pool *pool;
185 int fd, size, stride;
189 size = stride * height;
191 fd = os_create_anonymous_file(size);
193 fprintf(stderr, "creating a buffer file for %d B failed: %s\n",
194 size, strerror(errno));
198 data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
199 if (data == MAP_FAILED) {
200 fprintf(stderr, "mmap failed: %s\n", strerror(errno));
205 pool = wl_shm_create_pool(display->shm, fd, size);
206 buffer->buffer = wl_shm_pool_create_buffer(pool, 0, width,
207 height, stride, format);
208 wl_buffer_add_listener(buffer->buffer, &buffer_listener, buffer);
209 wl_shm_pool_destroy(pool);
212 buffer->shm_data = data;
214 buffer->width = width;
215 buffer->height = height;
217 fprintf(stdout, "Created shm buffer with width %d, height %d\n", width, height);
221 static struct buffer *
222 get_next_buffer(struct window *window)
224 struct buffer *buffer = NULL;
227 if (window->needs_update_buffer) {
230 for (i = 0; i < MAX_BUFFER_ALLOC; i++)
231 alloc_buffer(window, window->width, window->height);
233 window->needs_update_buffer = false;
236 buffer = pick_free_buffer(window);
241 if (!buffer->buffer) {
242 ret = create_shm_buffer(window->display, buffer, window->width,
243 window->height, WL_SHM_FORMAT_XRGB8888);
248 /* paint the padding */
249 memset(buffer->shm_data, 0x00, window->width * window->height * 4);
256 static const struct wl_callback_listener frame_listener = {
261 redraw(void *data, struct wl_callback *callback, uint32_t time)
263 struct window *window = static_cast<struct window *>(data);
264 struct buffer *buffer;
266 prune_old_released_buffers(window);
268 buffer = get_next_buffer(window);
271 !callback ? "Failed to create the first buffer.\n" :
272 "Both buffers busy at redraw(). Server bug?\n");
276 // do the actual painting
277 paint_pixels(buffer->shm_data, 0x0, window->width, window->height, time);
279 wl_surface_attach(window->surface, buffer->buffer, 0, 0);
280 wl_surface_damage(window->surface, 0, 0, window->width, window->height);
283 wl_callback_destroy(callback);
285 window->callback = wl_surface_frame(window->surface);
286 wl_callback_add_listener(window->callback, &frame_listener, window);
287 wl_surface_commit(window->surface);
293 shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
295 struct display *d = static_cast<struct display *>(data);
297 if (format == WL_SHM_FORMAT_XRGB8888)
301 static const struct wl_shm_listener shm_listener = {
306 xdg_wm_base_ping(void *data, struct xdg_wm_base *shell, uint32_t serial)
308 xdg_wm_base_pong(shell, serial);
311 static const struct xdg_wm_base_listener xdg_wm_base_listener = {
316 display_handle_geometry(void *data, struct wl_output *wl_output,
317 int x, int y, int physical_width, int physical_height,
318 int subpixel, const char *make, const char *model, int transform)
324 (void) physical_width;
325 (void) physical_height;
333 display_handle_mode(void *data, struct wl_output *wl_output, uint32_t flags,
334 int width, int height, int refresh)
336 struct display *d = static_cast<struct display *>(data);
338 if (wl_output == d->wl_output && (flags & WL_OUTPUT_MODE_CURRENT)) {
339 d->output_data.width = width;
340 d->output_data.height = height;
342 fprintf(stdout, "Found output with width %d and height %d\n",
343 d->output_data.width, d->output_data.height);
348 display_handle_scale(void *data, struct wl_output *wl_output, int scale)
356 display_handle_done(void *data, struct wl_output *wl_output)
362 static const struct wl_output_listener output_listener = {
363 display_handle_geometry,
370 application_id(void *data, struct agl_shell_desktop *agl_shell_desktop,
374 (void) agl_shell_desktop;
379 application_id_state(void *data, struct agl_shell_desktop *agl_shell_desktop,
380 const char *app_id, const char *app_data,
381 uint32_t app_state, uint32_t app_role)
385 (void) agl_shell_desktop;
391 static const struct agl_shell_desktop_listener agl_shell_desktop_listener = {
393 application_id_state,
397 registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
398 const char *interface, uint32_t version)
400 struct display *d = static_cast<struct display *>(data);
402 if (strcmp(interface, "wl_compositor") == 0) {
404 static_cast<struct wl_compositor *>(wl_registry_bind(registry, id,
405 &wl_compositor_interface, 1));
406 } else if (strcmp(interface, "xdg_wm_base") == 0) {
407 d->wm_base = static_cast<struct xdg_wm_base *>(wl_registry_bind(registry,
408 id, &xdg_wm_base_interface, 1));
409 xdg_wm_base_add_listener(d->wm_base, &xdg_wm_base_listener, d);
410 } else if (strcmp(interface, "wl_shm") == 0) {
411 d->shm = static_cast<struct wl_shm *>(wl_registry_bind(registry,
412 id, &wl_shm_interface, 1));
413 wl_shm_add_listener(d->shm, &shm_listener, d);
414 } else if (strcmp(interface, "agl_shell_desktop") == 0) {
415 d->agl_shell_desktop = static_cast<struct agl_shell_desktop *>(wl_registry_bind(registry, id,
416 &agl_shell_desktop_interface, 1));
417 /* as an example, show how to register for events from the compositor */
418 agl_shell_desktop_add_listener(d->agl_shell_desktop,
419 &agl_shell_desktop_listener, d);
420 } else if (strcmp(interface, "wl_output") == 0) {
421 d->wl_output = static_cast<struct wl_output *>(wl_registry_bind(registry, id,
422 &wl_output_interface, 1));
423 wl_output_add_listener(d->wl_output, &output_listener, d);
428 registry_handle_global_remove(void *data, struct wl_registry *reg, uint32_t id)
435 static const struct wl_registry_listener registry_listener = {
436 registry_handle_global,
437 registry_handle_global_remove,
442 error_cb(GstBus *bus, GstMessage *msg, gpointer user_data)
444 struct receiver_data *d =
445 static_cast<struct receiver_data *>(user_data);
450 gst_message_parse_error(msg, &err, &debug);
452 g_print("Error: %s\n", err->message);
456 g_print("Debug details: %s\n", debug);
460 gst_element_set_state(d->pipeline, GST_STATE_NULL);
463 static GstBusSyncReply
464 bus_sync_handler(GstBus *bus, GstMessage *message, gpointer user_data)
466 struct receiver_data *d =
467 static_cast<struct receiver_data *>(user_data);
469 if (gst_is_wl_display_handle_need_context_message(message)) {
471 struct wl_display *display_handle = d->window->display->wl_display;
473 context = gst_wl_display_handle_context_new(display_handle);
474 gst_element_set_context(GST_ELEMENT(GST_MESSAGE_SRC(message)), context);
477 } else if (gst_is_video_overlay_prepare_window_handle_message(message)) {
478 struct wl_surface *window_handle = d->window->surface;
480 /* GST_MESSAGE_SRC(message) will be the overlay object that we
481 * have to use. This may be waylandsink, but it may also be
482 * playbin. In the latter case, we must make sure to use
483 * playbin instead of waylandsink, because playbin resets the
484 * window handle and render_rectangle after restarting playback
485 * and the actual window size is lost */
486 d->overlay = GST_VIDEO_OVERLAY(GST_MESSAGE_SRC(message));
488 g_print("setting window handle and size (%d x %d) w %d, h %d\n",
489 d->window->x, d->window->y,
490 d->window->width, d->window->height);
492 gst_video_overlay_set_window_handle(d->overlay, (guintptr) window_handle);
493 gst_video_overlay_set_render_rectangle(d->overlay,
494 d->window->x, d->window->y,
495 d->window->width, d->window->height);
499 else if (GST_MESSAGE_TYPE(message) == GST_MESSAGE_ERROR) {
501 gchar* dbg_info = NULL;
503 gst_message_parse_error(message, &err, &dbg_info);
504 g_printerr("ERROR from element %s: %s code %d\n",
505 GST_OBJECT_NAME(message->src), err->message, err->code);
506 g_printerr("Debugging info: %s\n", (dbg_info) ? dbg_info : "none");
507 gst_pipeline_failed = TRUE;
516 gst_message_unref(message);
521 handle_xdg_surface_configure(void *data, struct xdg_surface *surface, uint32_t serial)
523 struct window *window = static_cast<struct window *>(data);
525 xdg_surface_ack_configure(surface, serial);
527 if (window->wait_for_configure) {
528 redraw(window, NULL, 0);
529 window->wait_for_configure = false;
533 static const struct xdg_surface_listener xdg_surface_listener = {
534 handle_xdg_surface_configure,
538 handle_xdg_toplevel_configure(void *data, struct xdg_toplevel *xdg_toplevel,
539 int32_t width, int32_t height,
540 struct wl_array *states)
542 struct window *window = static_cast<struct window *>(data);
545 window->fullscreen = 0;
546 window->maximized = 0;
548 // use our own macro as C++ can't typecast from (void *) directly
549 WL_ARRAY_FOR_EACH(p, states, uint32_t *) {
552 case XDG_TOPLEVEL_STATE_FULLSCREEN:
553 window->fullscreen = 1;
555 case XDG_TOPLEVEL_STATE_MAXIMIZED:
556 window->maximized = 1;
561 if (width > 0 && height > 0) {
562 if (!window->fullscreen && !window->maximized) {
563 window->init_width = width;
564 window->init_height = height;
566 window->width = width;
567 window->height = height;
568 } else if (!window->fullscreen && !window->maximized) {
569 window->width = window->init_width;
570 window->height = window->init_height;
573 window->needs_update_buffer = true;
578 handle_xdg_toplevel_close(void *data, struct xdg_toplevel *xdg_toplevel)
583 static const struct xdg_toplevel_listener xdg_toplevel_listener = {
584 handle_xdg_toplevel_configure,
585 handle_xdg_toplevel_close,
588 static struct window *
589 create_window(struct display *display, int width, int height, const char *app_id)
591 struct window *window;
594 assert(display->wm_base != NULL);
596 window = static_cast<struct window *>(calloc(1, sizeof(*window)));
600 wl_list_init(&window->buffer_list);
601 window->callback = NULL;
602 window->display = display;
603 window->width = width;
604 window->height = height;
605 window->surface = wl_compositor_create_surface(display->wl_compositor);
607 if (display->wm_base) {
608 window->xdg_surface =
609 xdg_wm_base_get_xdg_surface(display->wm_base, window->surface);
610 assert(window->xdg_surface);
612 xdg_surface_add_listener(window->xdg_surface,
613 &xdg_surface_listener, window);
614 window->xdg_toplevel = xdg_surface_get_toplevel(window->xdg_surface);
615 assert(window->xdg_toplevel);
617 xdg_toplevel_add_listener(window->xdg_toplevel,
618 &xdg_toplevel_listener, window);
620 xdg_toplevel_set_app_id(window->xdg_toplevel, app_id);
622 wl_surface_commit(window->surface);
623 window->wait_for_configure = true;
626 for (i = 0; i < MAX_BUFFER_ALLOC; i++)
627 alloc_buffer(window, window->width, window->height);
634 destroy_window(struct window *window)
636 struct buffer *buffer, *buffer_next;
638 if (window->callback)
639 wl_callback_destroy(window->callback);
641 wl_list_for_each_safe(buffer, buffer_next,
642 &window->buffer_list, buffer_link)
643 destroy_buffer(buffer);
645 if (window->xdg_toplevel)
646 xdg_toplevel_destroy(window->xdg_toplevel);
648 if (window->xdg_surface)
649 xdg_surface_destroy(window->xdg_surface);
651 wl_surface_destroy(window->surface);
656 signal_int(int sig, siginfo_t *si, void *_unused)
661 static struct display *
662 create_display(int argc, char *argv[])
664 struct display *display;
666 display = static_cast<struct display *>(calloc(1, sizeof(*display)));
667 if (display == NULL) {
668 fprintf(stderr, "out of memory\n");
671 display->wl_display = wl_display_connect(NULL);
672 assert(display->wl_display);
674 display->has_xrgb = false;
675 display->wl_registry = wl_display_get_registry(display->wl_display);
677 wl_registry_add_listener(display->wl_registry, ®istry_listener, display);
678 wl_display_roundtrip(display->wl_display);
680 if (display->shm == NULL) {
681 fprintf(stderr, "No wl_shm global\n");
685 if (display->agl_shell_desktop == NULL) {
686 fprintf(stderr, "No agl_shell extension present\n");
690 wl_display_roundtrip(display->wl_display);
692 if (!display->has_xrgb) {
693 fprintf(stderr, "WL_SHM_FORMAT_XRGB32 not available\n");
701 destroy_display(struct display *display)
704 wl_shm_destroy(display->shm);
706 if (display->wm_base)
707 xdg_wm_base_destroy(display->wm_base);
709 if (display->agl_shell_desktop)
710 agl_shell_desktop_destroy(display->agl_shell_desktop);
712 if (display->wl_compositor)
713 wl_compositor_destroy(display->wl_compositor);
715 wl_registry_destroy(display->wl_registry);
716 wl_display_flush(display->wl_display);
717 wl_display_disconnect(display->wl_display);
721 // stringify the un-quoted string to quoted C string
722 #define xstr(a) str(a)
725 GstElement* create_pipeline(int* argc, char** argv[])
727 GError *error = NULL;
728 const char *camera_device = NULL;
729 const char *width_str = NULL;
730 const char *height_str = NULL;
734 // pipewire is default.
735 char *v4l2_path = getenv("ENABLE_V4L2_PATH");
738 char pipeline_str[1024];
740 camera_device = getenv("DEFAULT_V4L2_DEVICE");
742 camera_device = get_first_camera_device();
744 width_str = getenv("DEFAULT_DEVICE_WIDTH");
746 width = WINDOW_WIDTH_SIZE;
748 width = atoi(width_str);
750 height_str = getenv("DEFAULT_DEVICE_HEIGHT");
752 height = WINDOW_HEIGHT_SIZE;
754 height = atoi(height_str);
756 if (v4l2_path == NULL)
758 else if (g_str_equal(v4l2_path, "yes") || g_str_equal(v4l2_path, "true"))
761 memset(pipeline_str, 0, sizeof(pipeline_str));
764 snprintf(pipeline_str, sizeof(pipeline_str), "v4l2src device=%s ! video/x-raw,width=%d,height=%d ! waylandsink",
765 camera_device, width, height);
766 else if (gst_pipeline_failed == TRUE) {
767 snprintf(pipeline_str, sizeof(pipeline_str), "filesrc location=%s/still-image.jpg ! decodebin ! videoconvert ! imagefreeze ! waylandsink fullscreen=true",
768 xstr(APP_DATA_PATH), width, height);
769 fallback_gst_pipeline_tried = TRUE;
772 snprintf(pipeline_str, sizeof(pipeline_str), "pipewiresrc ! video/x-raw,width=%d,height=%d ! waylandsink", width,
776 fprintf(stdout, "Using pipeline: %s\n", pipeline_str);
778 GstElement *pipeline = gst_parse_launch(pipeline_str, &error);
780 if (error || !pipeline) {
781 fprintf(stderr, "gstreamer pipeline construction failed!\n");
789 int main(int argc, char* argv[])
793 struct receiver_data receiver_data = {};
794 struct display* display;
795 struct window* window;
796 const char* app_id = "camera-gstreamer";
798 sa.sa_sigaction = signal_int;
799 sigemptyset(&sa.sa_mask);
800 sa.sa_flags = SA_RESETHAND | SA_SIGINFO;
801 sigaction(SIGINT, &sa, NULL);
804 char** gargv = static_cast<char**>(calloc(2, sizeof(char*)));
806 gargv[0] = strdup(argv[0]);
807 gargv[1] = strdup("--gst-debug-level=2");
809 setbuf(stdout, NULL);
811 gst_init(&gargc, &gargv);
813 receiver_data.pipeline = create_pipeline(&gargc, &gargv);
815 if (!receiver_data.pipeline)
818 display = create_display(argc, argv);
822 // if you'd want to place the video in a pop-up/dialog type of window:
823 // agl_shell_desktop_set_app_property(display->agl_shell_desktop, app_id,
824 // AGL_SHELL_DESKTOP_APP_ROLE_POPUP,
825 // WINDOW_WIDTH_POS_X, WINDOW_WIDTH_POS_Y,
826 // 0, 0, WINDOW_WIDTH_SIZE, WINDOW_HEIGHT_SIZE,
827 // display->wl_output);
829 // we use the role to set a correspondence between the top level
830 // surface and our application, with the previous call letting the
831 // compositor know that we're one and the same
832 window = create_window(display, WINDOW_WIDTH_SIZE, WINDOW_HEIGHT_SIZE, app_id);
839 window->display = display;
840 receiver_data.window = window;
842 /* Initialise damage to full surface, so the padding gets painted */
843 wl_surface_damage(window->surface, 0, 0,
844 window->width, window->height);
846 if (!window->wait_for_configure) {
847 redraw(window, NULL, 0);
850 GstBus *bus = gst_element_get_bus(receiver_data.pipeline);
851 gst_bus_add_signal_watch(bus);
853 g_signal_connect(bus, "message::error", G_CALLBACK(error_cb), &receiver_data);
854 gst_bus_set_sync_handler(bus, bus_sync_handler, &receiver_data, NULL);
855 gst_object_unref(bus);
857 gst_element_set_state(receiver_data.pipeline, GST_STATE_PLAYING);
858 fprintf(stdout, "gstreamer pipeline running\n");
860 // run the application
861 while (running && ret != -1) {
862 ret = wl_display_dispatch(display->wl_display);
863 if (gst_pipeline_failed && fallback_gst_pipeline_tried == FALSE) {
864 gst_element_set_state(receiver_data.pipeline, GST_STATE_NULL);
865 gst_object_unref(receiver_data.pipeline);
866 /* retry with fallback pipeline */
867 receiver_data.pipeline = create_pipeline(&gargc, &gargv);
868 GstBus *bus = gst_element_get_bus(receiver_data.pipeline);
869 gst_bus_add_signal_watch(bus);
870 g_signal_connect(bus, "message::error", G_CALLBACK(error_cb), &receiver_data);
871 gst_bus_set_sync_handler(bus, bus_sync_handler, &receiver_data, NULL);
872 gst_object_unref(bus);
873 gst_element_set_state(receiver_data.pipeline, GST_STATE_PLAYING);
876 gst_element_set_state(receiver_data.pipeline, GST_STATE_NULL);
877 gst_object_unref(receiver_data.pipeline);
879 destroy_window(window);
880 destroy_display(display);