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 // these only applies if the window is a dialog/pop-up one
27 // by default the compositor make the window maximized
28 #define WINDOW_WIDTH_SIZE 640
29 #define WINDOW_HEIGHT_SIZE 720
31 #define WINDOW_WIDTH_POS_X 640
32 #define WINDOW_WIDTH_POS_Y 180
34 #define MAX_BUFFER_ALLOC 2
36 // C++ requires a cast and we in wayland we do the cast implictly
37 #define WL_ARRAY_FOR_EACH(pos, array, type) \
38 for (pos = (type)(array)->data; \
39 (const char *) pos < ((const char *) (array)->data + (array)->size); \
43 struct wl_display *wl_display;
44 struct wl_registry *wl_registry;
45 struct wl_compositor *wl_compositor;
46 struct wl_output *wl_output;
54 struct xdg_wm_base *wm_base;
55 struct agl_shell_desktop *agl_shell_desktop;
61 struct wl_buffer *buffer;
65 size_t size; /* width * 4 * height */
66 struct wl_list buffer_link; /** window::buffer_list */
70 struct display *display;
77 struct wl_surface *surface;
79 struct xdg_surface *xdg_surface;
80 struct xdg_toplevel *xdg_toplevel;
81 bool wait_for_configure;
83 int fullscreen, maximized;
85 struct wl_list buffer_list;
86 struct wl_callback *callback;
87 bool needs_update_buffer;
91 struct receiver_data {
92 struct window *window;
95 GstWaylandVideo *wl_video;
96 GstVideoOverlay *overlay;
99 static int running = 1;
100 static bool gst_pipeline_failed = FALSE;
101 static bool fallback_gst_pipeline_tried = FALSE;
104 redraw(void *data, struct wl_callback *callback, uint32_t time);
106 static struct buffer *
107 alloc_buffer(struct window *window, int width, int height)
109 struct buffer *buffer = static_cast<struct buffer *>(calloc(1, sizeof(*buffer)));
111 buffer->width = width;
112 buffer->height = height;
113 wl_list_insert(&window->buffer_list, &buffer->buffer_link);
119 destroy_buffer(struct buffer *buffer)
122 wl_buffer_destroy(buffer->buffer);
124 munmap(buffer->shm_data, buffer->size);
125 wl_list_remove(&buffer->buffer_link);
129 static struct buffer *
130 pick_free_buffer(struct window *window)
133 struct buffer *buffer = NULL;
135 wl_list_for_each(b, &window->buffer_list, buffer_link) {
146 prune_old_released_buffers(struct window *window)
148 struct buffer *b, *b_next;
150 wl_list_for_each_safe(b, b_next,
151 &window->buffer_list, buffer_link) {
152 if (!b->busy && (b->width != window->width ||
153 b->height != window->height))
159 paint_pixels(void *image, int padding, int width, int height, uint32_t time)
161 memset(image, 0x00, width * height * 4);
165 buffer_release(void *data, struct wl_buffer *buffer)
167 struct buffer *mybuf = static_cast<struct buffer *>(data);
171 static const struct wl_buffer_listener buffer_listener = {
177 create_shm_buffer(struct display *display, struct buffer *buffer,
178 int width, int height, uint32_t format)
180 struct wl_shm_pool *pool;
181 int fd, size, stride;
185 size = stride * height;
187 fd = os_create_anonymous_file(size);
189 fprintf(stderr, "creating a buffer file for %d B failed: %s\n",
190 size, strerror(errno));
194 data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
195 if (data == MAP_FAILED) {
196 fprintf(stderr, "mmap failed: %s\n", strerror(errno));
201 pool = wl_shm_create_pool(display->shm, fd, size);
202 buffer->buffer = wl_shm_pool_create_buffer(pool, 0, width,
203 height, stride, format);
204 wl_buffer_add_listener(buffer->buffer, &buffer_listener, buffer);
205 wl_shm_pool_destroy(pool);
208 buffer->shm_data = data;
210 buffer->width = width;
211 buffer->height = height;
213 fprintf(stdout, "Created shm buffer with width %d, height %d\n", width, height);
217 static struct buffer *
218 get_next_buffer(struct window *window)
220 struct buffer *buffer = NULL;
223 if (window->needs_update_buffer) {
226 for (i = 0; i < MAX_BUFFER_ALLOC; i++)
227 alloc_buffer(window, window->width, window->height);
229 window->needs_update_buffer = false;
232 buffer = pick_free_buffer(window);
237 if (!buffer->buffer) {
238 ret = create_shm_buffer(window->display, buffer, window->width,
239 window->height, WL_SHM_FORMAT_XRGB8888);
244 /* paint the padding */
245 memset(buffer->shm_data, 0x00, window->width * window->height * 4);
252 static const struct wl_callback_listener frame_listener = {
257 redraw(void *data, struct wl_callback *callback, uint32_t time)
259 struct window *window = static_cast<struct window *>(data);
260 struct buffer *buffer;
262 prune_old_released_buffers(window);
264 buffer = get_next_buffer(window);
267 !callback ? "Failed to create the first buffer.\n" :
268 "Both buffers busy at redraw(). Server bug?\n");
272 // do the actual painting
273 paint_pixels(buffer->shm_data, 0x0, window->width, window->height, time);
275 wl_surface_attach(window->surface, buffer->buffer, 0, 0);
276 wl_surface_damage(window->surface, 0, 0, window->width, window->height);
279 wl_callback_destroy(callback);
281 window->callback = wl_surface_frame(window->surface);
282 wl_callback_add_listener(window->callback, &frame_listener, window);
283 wl_surface_commit(window->surface);
289 shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
291 struct display *d = static_cast<struct display *>(data);
293 if (format == WL_SHM_FORMAT_XRGB8888)
297 static const struct wl_shm_listener shm_listener = {
302 xdg_wm_base_ping(void *data, struct xdg_wm_base *shell, uint32_t serial)
304 xdg_wm_base_pong(shell, serial);
307 static const struct xdg_wm_base_listener xdg_wm_base_listener = {
312 display_handle_geometry(void *data, struct wl_output *wl_output,
313 int x, int y, int physical_width, int physical_height,
314 int subpixel, const char *make, const char *model, int transform)
320 (void) physical_width;
321 (void) physical_height;
329 display_handle_mode(void *data, struct wl_output *wl_output, uint32_t flags,
330 int width, int height, int refresh)
332 struct display *d = static_cast<struct display *>(data);
334 if (wl_output == d->wl_output && (flags & WL_OUTPUT_MODE_CURRENT)) {
335 d->output_data.width = width;
336 d->output_data.height = height;
338 fprintf(stdout, "Found output with width %d and height %d\n",
339 d->output_data.width, d->output_data.height);
344 display_handle_scale(void *data, struct wl_output *wl_output, int scale)
352 display_handle_done(void *data, struct wl_output *wl_output)
358 static const struct wl_output_listener output_listener = {
359 display_handle_geometry,
366 application_id(void *data, struct agl_shell_desktop *agl_shell_desktop,
370 (void) agl_shell_desktop;
375 application_id_state(void *data, struct agl_shell_desktop *agl_shell_desktop,
376 const char *app_id, const char *app_data,
377 uint32_t app_state, uint32_t app_role)
381 (void) agl_shell_desktop;
387 static const struct agl_shell_desktop_listener agl_shell_desktop_listener = {
389 application_id_state,
393 registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
394 const char *interface, uint32_t version)
396 struct display *d = static_cast<struct display *>(data);
398 if (strcmp(interface, "wl_compositor") == 0) {
400 static_cast<struct wl_compositor *>(wl_registry_bind(registry, id,
401 &wl_compositor_interface, 1));
402 } else if (strcmp(interface, "xdg_wm_base") == 0) {
403 d->wm_base = static_cast<struct xdg_wm_base *>(wl_registry_bind(registry,
404 id, &xdg_wm_base_interface, 1));
405 xdg_wm_base_add_listener(d->wm_base, &xdg_wm_base_listener, d);
406 } else if (strcmp(interface, "wl_shm") == 0) {
407 d->shm = static_cast<struct wl_shm *>(wl_registry_bind(registry,
408 id, &wl_shm_interface, 1));
409 wl_shm_add_listener(d->shm, &shm_listener, d);
410 } else if (strcmp(interface, "agl_shell_desktop") == 0) {
411 d->agl_shell_desktop = static_cast<struct agl_shell_desktop *>(wl_registry_bind(registry, id,
412 &agl_shell_desktop_interface, 1));
413 /* as an example, show how to register for events from the compositor */
414 agl_shell_desktop_add_listener(d->agl_shell_desktop,
415 &agl_shell_desktop_listener, d);
416 } else if (strcmp(interface, "wl_output") == 0) {
417 d->wl_output = static_cast<struct wl_output *>(wl_registry_bind(registry, id,
418 &wl_output_interface, 1));
419 wl_output_add_listener(d->wl_output, &output_listener, d);
424 registry_handle_global_remove(void *data, struct wl_registry *reg, uint32_t id)
431 static const struct wl_registry_listener registry_listener = {
432 registry_handle_global,
433 registry_handle_global_remove,
438 error_cb(GstBus *bus, GstMessage *msg, gpointer user_data)
440 struct receiver_data *d =
441 static_cast<struct receiver_data *>(user_data);
446 gst_message_parse_error(msg, &err, &debug);
448 g_print("Error: %s\n", err->message);
452 g_print("Debug details: %s\n", debug);
456 gst_element_set_state(d->pipeline, GST_STATE_NULL);
459 static GstBusSyncReply
460 bus_sync_handler(GstBus *bus, GstMessage *message, gpointer user_data)
462 struct receiver_data *d =
463 static_cast<struct receiver_data *>(user_data);
465 if (gst_is_wayland_display_handle_need_context_message(message)) {
467 struct wl_display *display_handle = d->window->display->wl_display;
469 context = gst_wayland_display_handle_context_new(display_handle);
470 d->wl_video = GST_WAYLAND_VIDEO(GST_MESSAGE_SRC(message));
471 gst_element_set_context(GST_ELEMENT(GST_MESSAGE_SRC(message)), context);
474 } else if (gst_is_video_overlay_prepare_window_handle_message(message)) {
475 struct wl_surface *window_handle = d->window->surface;
477 /* GST_MESSAGE_SRC(message) will be the overlay object that we
478 * have to use. This may be waylandsink, but it may also be
479 * playbin. In the latter case, we must make sure to use
480 * playbin instead of waylandsink, because playbin resets the
481 * window handle and render_rectangle after restarting playback
482 * and the actual window size is lost */
483 d->overlay = GST_VIDEO_OVERLAY(GST_MESSAGE_SRC(message));
485 g_print("setting window handle and size (%d x %d) w %d, h %d\n",
486 d->window->x, d->window->y,
487 d->window->width, d->window->height);
489 gst_video_overlay_set_window_handle(d->overlay, (guintptr) window_handle);
490 gst_video_overlay_set_render_rectangle(d->overlay,
491 d->window->x, d->window->y,
492 d->window->width, d->window->height);
496 else if (GST_MESSAGE_TYPE(message) == GST_MESSAGE_ERROR) {
498 gchar* dbg_info = NULL;
500 gst_message_parse_error(message, &err, &dbg_info);
501 g_printerr("ERROR from element %s: %s code %d\n",
502 GST_OBJECT_NAME(message->src), err->message, err->code);
503 g_printerr("Debugging info: %s\n", (dbg_info) ? dbg_info : "none");
504 gst_pipeline_failed = TRUE;
513 gst_message_unref(message);
518 handle_xdg_surface_configure(void *data, struct xdg_surface *surface, uint32_t serial)
520 struct window *window = static_cast<struct window *>(data);
522 xdg_surface_ack_configure(surface, serial);
524 if (window->wait_for_configure) {
525 redraw(window, NULL, 0);
526 window->wait_for_configure = false;
530 static const struct xdg_surface_listener xdg_surface_listener = {
531 handle_xdg_surface_configure,
535 handle_xdg_toplevel_configure(void *data, struct xdg_toplevel *xdg_toplevel,
536 int32_t width, int32_t height,
537 struct wl_array *states)
539 struct window *window = static_cast<struct window *>(data);
542 window->fullscreen = 0;
543 window->maximized = 0;
545 // use our own macro as C++ can't typecast from (void *) directly
546 WL_ARRAY_FOR_EACH(p, states, uint32_t *) {
549 case XDG_TOPLEVEL_STATE_FULLSCREEN:
550 window->fullscreen = 1;
552 case XDG_TOPLEVEL_STATE_MAXIMIZED:
553 window->maximized = 1;
558 if (width > 0 && height > 0) {
559 if (!window->fullscreen && !window->maximized) {
560 window->init_width = width;
561 window->init_height = height;
563 window->width = width;
564 window->height = height;
565 } else if (!window->fullscreen && !window->maximized) {
566 window->width = window->init_width;
567 window->height = window->init_height;
570 window->needs_update_buffer = true;
575 handle_xdg_toplevel_close(void *data, struct xdg_toplevel *xdg_toplevel)
580 static const struct xdg_toplevel_listener xdg_toplevel_listener = {
581 handle_xdg_toplevel_configure,
582 handle_xdg_toplevel_close,
585 static struct window *
586 create_window(struct display *display, int width, int height, const char *app_id)
588 struct window *window;
591 assert(display->wm_base != NULL);
593 window = static_cast<struct window *>(calloc(1, sizeof(*window)));
597 wl_list_init(&window->buffer_list);
598 window->callback = NULL;
599 window->display = display;
600 window->width = width;
601 window->height = height;
602 window->surface = wl_compositor_create_surface(display->wl_compositor);
604 if (display->wm_base) {
605 window->xdg_surface =
606 xdg_wm_base_get_xdg_surface(display->wm_base, window->surface);
607 assert(window->xdg_surface);
609 xdg_surface_add_listener(window->xdg_surface,
610 &xdg_surface_listener, window);
611 window->xdg_toplevel = xdg_surface_get_toplevel(window->xdg_surface);
612 assert(window->xdg_toplevel);
614 xdg_toplevel_add_listener(window->xdg_toplevel,
615 &xdg_toplevel_listener, window);
617 xdg_toplevel_set_app_id(window->xdg_toplevel, app_id);
619 wl_surface_commit(window->surface);
620 window->wait_for_configure = true;
623 for (i = 0; i < MAX_BUFFER_ALLOC; i++)
624 alloc_buffer(window, window->width, window->height);
631 destroy_window(struct window *window)
633 struct buffer *buffer, *buffer_next;
635 if (window->callback)
636 wl_callback_destroy(window->callback);
638 wl_list_for_each_safe(buffer, buffer_next,
639 &window->buffer_list, buffer_link)
640 destroy_buffer(buffer);
642 if (window->xdg_toplevel)
643 xdg_toplevel_destroy(window->xdg_toplevel);
645 if (window->xdg_surface)
646 xdg_surface_destroy(window->xdg_surface);
648 wl_surface_destroy(window->surface);
653 signal_int(int sig, siginfo_t *si, void *_unused)
658 static struct display *
659 create_display(int argc, char *argv[])
661 struct display *display;
663 display = static_cast<struct display *>(calloc(1, sizeof(*display)));
664 if (display == NULL) {
665 fprintf(stderr, "out of memory\n");
668 display->wl_display = wl_display_connect(NULL);
669 assert(display->wl_display);
671 display->has_xrgb = false;
672 display->wl_registry = wl_display_get_registry(display->wl_display);
674 wl_registry_add_listener(display->wl_registry, ®istry_listener, display);
675 wl_display_roundtrip(display->wl_display);
677 if (display->shm == NULL) {
678 fprintf(stderr, "No wl_shm global\n");
682 if (display->agl_shell_desktop == NULL) {
683 fprintf(stderr, "No agl_shell extension present\n");
687 wl_display_roundtrip(display->wl_display);
689 if (!display->has_xrgb) {
690 fprintf(stderr, "WL_SHM_FORMAT_XRGB32 not available\n");
698 destroy_display(struct display *display)
701 wl_shm_destroy(display->shm);
703 if (display->wm_base)
704 xdg_wm_base_destroy(display->wm_base);
706 if (display->agl_shell_desktop)
707 agl_shell_desktop_destroy(display->agl_shell_desktop);
709 if (display->wl_compositor)
710 wl_compositor_destroy(display->wl_compositor);
712 wl_registry_destroy(display->wl_registry);
713 wl_display_flush(display->wl_display);
714 wl_display_disconnect(display->wl_display);
718 // stringify the un-quoted string to quoted C string
719 #define xstr(a) str(a)
722 GstElement* create_pipeline(int* argc, char** argv[])
724 GError *error = NULL;
725 const char *camera_device = NULL;
726 const char *width_str = NULL;
727 const char *height_str = NULL;
731 // pipewire is default.
732 char *v4l2_path = getenv("ENABLE_V4L2_PATH");
735 char pipeline_str[1024];
737 camera_device = getenv("DEFAULT_V4L2_DEVICE");
739 camera_device = get_first_camera_device();
741 width_str = getenv("DEFAULT_DEVICE_WIDTH");
743 width = WINDOW_WIDTH_SIZE;
745 width = atoi(width_str);
747 height_str = getenv("DEFAULT_DEVICE_HEIGHT");
749 height = WINDOW_HEIGHT_SIZE;
751 height = atoi(height_str);
753 if (v4l2_path == NULL)
755 else if (g_str_equal(v4l2_path, "yes") || g_str_equal(v4l2_path, "true"))
758 memset(pipeline_str, 0, sizeof(pipeline_str));
761 snprintf(pipeline_str, sizeof(pipeline_str), "v4l2src device=%s ! video/x-raw,width=%d,height=%d ! waylandsink",
762 camera_device, width, height);
763 else if (gst_pipeline_failed == TRUE) {
764 snprintf(pipeline_str, sizeof(pipeline_str), "filesrc location=%s/still-image.jpg ! decodebin ! videoconvert ! imagefreeze ! waylandsink fullscreen=true",
765 xstr(APP_DATA_PATH), width, height);
766 fallback_gst_pipeline_tried = TRUE;
769 snprintf(pipeline_str, sizeof(pipeline_str), "pipewiresrc ! video/x-raw,width=%d,height=%d ! waylandsink", width,
773 fprintf(stdout, "Using pipeline: %s\n", pipeline_str);
775 GstElement *pipeline = gst_parse_launch(pipeline_str, &error);
777 if (error || !pipeline) {
778 fprintf(stderr, "gstreamer pipeline construction failed!\n");
786 int main(int argc, char* argv[])
790 struct receiver_data receiver_data = {};
791 struct display* display;
792 struct window* window;
793 const char* app_id = "camera-gstreamer";
795 sa.sa_sigaction = signal_int;
796 sigemptyset(&sa.sa_mask);
797 sa.sa_flags = SA_RESETHAND | SA_SIGINFO;
798 sigaction(SIGINT, &sa, NULL);
801 char** gargv = static_cast<char**>(calloc(2, sizeof(char*)));
803 gargv[0] = strdup(argv[0]);
804 gargv[1] = strdup("--gst-debug-level=2");
806 setbuf(stdout, NULL);
808 gst_init(&gargc, &gargv);
810 receiver_data.pipeline = create_pipeline(&gargc, &gargv);
812 if (!receiver_data.pipeline)
815 display = create_display(argc, argv);
819 // if you'd want to place the video in a pop-up/dialog type of window:
820 // agl_shell_desktop_set_app_property(display->agl_shell_desktop, app_id,
821 // AGL_SHELL_DESKTOP_APP_ROLE_POPUP,
822 // WINDOW_WIDTH_POS_X, WINDOW_WIDTH_POS_Y,
823 // 0, 0, WINDOW_WIDTH_SIZE, WINDOW_HEIGHT_SIZE,
824 // display->wl_output);
826 // we use the role to set a correspondence between the top level
827 // surface and our application, with the previous call letting the
828 // compositor know that we're one and the same
829 window = create_window(display, WINDOW_WIDTH_SIZE, WINDOW_HEIGHT_SIZE, app_id);
836 window->display = display;
837 receiver_data.window = window;
839 /* Initialise damage to full surface, so the padding gets painted */
840 wl_surface_damage(window->surface, 0, 0,
841 window->width, window->height);
843 if (!window->wait_for_configure) {
844 redraw(window, NULL, 0);
847 GstBus *bus = gst_element_get_bus(receiver_data.pipeline);
848 gst_bus_add_signal_watch(bus);
850 g_signal_connect(bus, "message::error", G_CALLBACK(error_cb), &receiver_data);
851 gst_bus_set_sync_handler(bus, bus_sync_handler, &receiver_data, NULL);
852 gst_object_unref(bus);
854 gst_element_set_state(receiver_data.pipeline, GST_STATE_PLAYING);
855 fprintf(stdout, "gstreamer pipeline running\n");
857 // run the application
858 while (running && ret != -1) {
859 ret = wl_display_dispatch(display->wl_display);
860 if (gst_pipeline_failed && fallback_gst_pipeline_tried == FALSE) {
861 gst_element_set_state(receiver_data.pipeline, GST_STATE_NULL);
862 gst_object_unref(receiver_data.pipeline);
863 /* retry with fallback pipeline */
864 receiver_data.pipeline = create_pipeline(&gargc, &gargv);
865 GstBus *bus = gst_element_get_bus(receiver_data.pipeline);
866 gst_bus_add_signal_watch(bus);
867 g_signal_connect(bus, "message::error", G_CALLBACK(error_cb), &receiver_data);
868 gst_bus_set_sync_handler(bus, bus_sync_handler, &receiver_data, NULL);
869 gst_object_unref(bus);
870 gst_element_set_state(receiver_data.pipeline, GST_STATE_PLAYING);
873 gst_element_set_state(receiver_data.pipeline, GST_STATE_NULL);
874 gst_object_unref(receiver_data.pipeline);
876 destroy_window(window);
877 destroy_display(display);