1 #define GST_USE_UNSTABLE_API
2 #define HAVE_MEMFD_CREATE
11 #include <wayland-client.h>
18 #include "xdg-shell-client-protocol.h"
22 #include <gst/video/videooverlay.h>
23 #include <gst/wayland/wayland.h>
25 #if !GST_CHECK_VERSION(1, 22, 0)
26 #define gst_is_wl_display_handle_need_context_message gst_is_wayland_display_handle_need_context_message
27 #define gst_wl_display_handle_context_new gst_wayland_display_handle_context_new
31 #define APP_DATA_PATH /usr/share/applications/data
34 // these only applies if the window is a dialog/pop-up one
35 // by default the compositor make the window maximized
36 #define WINDOW_WIDTH_SIZE 640
37 #define WINDOW_HEIGHT_SIZE 720
39 #define WINDOW_WIDTH_POS_X 640
40 #define WINDOW_WIDTH_POS_Y 180
42 #define MAX_BUFFER_ALLOC 2
44 // C++ requires a cast and we in wayland we do the cast implictly
45 #define WL_ARRAY_FOR_EACH(pos, array, type) \
46 for (pos = (type)(array)->data; \
47 (const char *) pos < ((const char *) (array)->data + (array)->size); \
51 struct wl_display *wl_display;
52 struct wl_registry *wl_registry;
53 struct wl_compositor *wl_compositor;
54 struct wl_output *wl_output;
62 struct xdg_wm_base *wm_base;
67 struct wl_buffer *buffer;
71 size_t size; /* width * 4 * height */
72 struct wl_list buffer_link; /** window::buffer_list */
76 struct display *display;
83 struct wl_surface *surface;
85 struct xdg_surface *xdg_surface;
86 struct xdg_toplevel *xdg_toplevel;
87 bool wait_for_configure;
89 int fullscreen, maximized;
91 struct wl_list buffer_list;
92 struct wl_callback *callback;
93 bool needs_update_buffer;
97 struct receiver_data {
98 struct window *window;
100 GstElement *pipeline;
101 GstVideoOverlay *overlay;
104 static int running = 1;
105 static bool gst_pipeline_failed = FALSE;
106 static bool fallback_gst_pipeline_tried = FALSE;
109 redraw(void *data, struct wl_callback *callback, uint32_t time);
111 static struct buffer *
112 alloc_buffer(struct window *window, int width, int height)
114 struct buffer *buffer = static_cast<struct buffer *>(calloc(1, sizeof(*buffer)));
116 buffer->width = width;
117 buffer->height = height;
118 wl_list_insert(&window->buffer_list, &buffer->buffer_link);
124 destroy_buffer(struct buffer *buffer)
127 wl_buffer_destroy(buffer->buffer);
129 munmap(buffer->shm_data, buffer->size);
130 wl_list_remove(&buffer->buffer_link);
134 static struct buffer *
135 pick_free_buffer(struct window *window)
138 struct buffer *buffer = NULL;
140 wl_list_for_each(b, &window->buffer_list, buffer_link) {
151 prune_old_released_buffers(struct window *window)
153 struct buffer *b, *b_next;
155 wl_list_for_each_safe(b, b_next,
156 &window->buffer_list, buffer_link) {
157 if (!b->busy && (b->width != window->width ||
158 b->height != window->height))
164 paint_pixels(void *image, int padding, int width, int height, uint32_t time)
166 memset(image, 0x00, width * height * 4);
170 buffer_release(void *data, struct wl_buffer *buffer)
172 struct buffer *mybuf = static_cast<struct buffer *>(data);
176 static const struct wl_buffer_listener buffer_listener = {
182 create_shm_buffer(struct display *display, struct buffer *buffer,
183 int width, int height, uint32_t format)
185 struct wl_shm_pool *pool;
186 int fd, size, stride;
190 size = stride * height;
192 fd = os_create_anonymous_file(size);
194 fprintf(stderr, "creating a buffer file for %d B failed: %s\n",
195 size, strerror(errno));
199 data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
200 if (data == MAP_FAILED) {
201 fprintf(stderr, "mmap failed: %s\n", strerror(errno));
206 pool = wl_shm_create_pool(display->shm, fd, size);
207 buffer->buffer = wl_shm_pool_create_buffer(pool, 0, width,
208 height, stride, format);
209 wl_buffer_add_listener(buffer->buffer, &buffer_listener, buffer);
210 wl_shm_pool_destroy(pool);
213 buffer->shm_data = data;
215 buffer->width = width;
216 buffer->height = height;
218 fprintf(stdout, "Created shm buffer with width %d, height %d\n", width, height);
222 static struct buffer *
223 get_next_buffer(struct window *window)
225 struct buffer *buffer = NULL;
228 if (window->needs_update_buffer) {
231 for (i = 0; i < MAX_BUFFER_ALLOC; i++)
232 alloc_buffer(window, window->width, window->height);
234 window->needs_update_buffer = false;
237 buffer = pick_free_buffer(window);
242 if (!buffer->buffer) {
243 ret = create_shm_buffer(window->display, buffer, window->width,
244 window->height, WL_SHM_FORMAT_XRGB8888);
249 /* paint the padding */
250 memset(buffer->shm_data, 0x00, window->width * window->height * 4);
257 static const struct wl_callback_listener frame_listener = {
262 redraw(void *data, struct wl_callback *callback, uint32_t time)
264 struct window *window = static_cast<struct window *>(data);
265 struct buffer *buffer;
267 prune_old_released_buffers(window);
269 buffer = get_next_buffer(window);
272 !callback ? "Failed to create the first buffer.\n" :
273 "Both buffers busy at redraw(). Server bug?\n");
277 // do the actual painting
278 paint_pixels(buffer->shm_data, 0x0, window->width, window->height, time);
280 wl_surface_attach(window->surface, buffer->buffer, 0, 0);
281 wl_surface_damage(window->surface, 0, 0, window->width, window->height);
284 wl_callback_destroy(callback);
286 window->callback = wl_surface_frame(window->surface);
287 wl_callback_add_listener(window->callback, &frame_listener, window);
288 wl_surface_commit(window->surface);
294 shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
296 struct display *d = static_cast<struct display *>(data);
298 if (format == WL_SHM_FORMAT_XRGB8888)
302 static const struct wl_shm_listener shm_listener = {
307 xdg_wm_base_ping(void *data, struct xdg_wm_base *shell, uint32_t serial)
309 xdg_wm_base_pong(shell, serial);
312 static const struct xdg_wm_base_listener xdg_wm_base_listener = {
317 display_handle_geometry(void *data, struct wl_output *wl_output,
318 int x, int y, int physical_width, int physical_height,
319 int subpixel, const char *make, const char *model, int transform)
325 (void) physical_width;
326 (void) physical_height;
334 display_handle_mode(void *data, struct wl_output *wl_output, uint32_t flags,
335 int width, int height, int refresh)
337 struct display *d = static_cast<struct display *>(data);
339 if (wl_output == d->wl_output && (flags & WL_OUTPUT_MODE_CURRENT)) {
340 d->output_data.width = width;
341 d->output_data.height = height;
343 fprintf(stdout, "Found output with width %d and height %d\n",
344 d->output_data.width, d->output_data.height);
349 display_handle_scale(void *data, struct wl_output *wl_output, int scale)
357 display_handle_done(void *data, struct wl_output *wl_output)
363 static const struct wl_output_listener output_listener = {
364 display_handle_geometry,
372 registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
373 const char *interface, uint32_t version)
375 struct display *d = static_cast<struct display *>(data);
377 if (strcmp(interface, "wl_compositor") == 0) {
379 static_cast<struct wl_compositor *>(wl_registry_bind(registry, id,
380 &wl_compositor_interface, 1));
381 } else if (strcmp(interface, "xdg_wm_base") == 0) {
382 d->wm_base = static_cast<struct xdg_wm_base *>(wl_registry_bind(registry,
383 id, &xdg_wm_base_interface, 1));
384 xdg_wm_base_add_listener(d->wm_base, &xdg_wm_base_listener, d);
385 } else if (strcmp(interface, "wl_shm") == 0) {
386 d->shm = static_cast<struct wl_shm *>(wl_registry_bind(registry,
387 id, &wl_shm_interface, 1));
388 wl_shm_add_listener(d->shm, &shm_listener, d);
389 } else if (strcmp(interface, "wl_output") == 0) {
390 d->wl_output = static_cast<struct wl_output *>(wl_registry_bind(registry, id,
391 &wl_output_interface, 1));
392 wl_output_add_listener(d->wl_output, &output_listener, d);
397 registry_handle_global_remove(void *data, struct wl_registry *reg, uint32_t id)
404 static const struct wl_registry_listener registry_listener = {
405 registry_handle_global,
406 registry_handle_global_remove,
411 error_cb(GstBus *bus, GstMessage *msg, gpointer user_data)
413 struct receiver_data *d =
414 static_cast<struct receiver_data *>(user_data);
419 gst_message_parse_error(msg, &err, &debug);
421 g_print("Error: %s\n", err->message);
425 g_print("Debug details: %s\n", debug);
429 gst_element_set_state(d->pipeline, GST_STATE_NULL);
432 static GstBusSyncReply
433 bus_sync_handler(GstBus *bus, GstMessage *message, gpointer user_data)
435 struct receiver_data *d =
436 static_cast<struct receiver_data *>(user_data);
438 if (gst_is_wl_display_handle_need_context_message(message)) {
440 struct wl_display *display_handle = d->window->display->wl_display;
442 context = gst_wl_display_handle_context_new(display_handle);
443 gst_element_set_context(GST_ELEMENT(GST_MESSAGE_SRC(message)), context);
446 } else if (gst_is_video_overlay_prepare_window_handle_message(message)) {
447 struct wl_surface *window_handle = d->window->surface;
449 /* GST_MESSAGE_SRC(message) will be the overlay object that we
450 * have to use. This may be waylandsink, but it may also be
451 * playbin. In the latter case, we must make sure to use
452 * playbin instead of waylandsink, because playbin resets the
453 * window handle and render_rectangle after restarting playback
454 * and the actual window size is lost */
455 d->overlay = GST_VIDEO_OVERLAY(GST_MESSAGE_SRC(message));
457 g_print("setting window handle and size (%d x %d) w %d, h %d\n",
458 d->window->x, d->window->y,
459 d->window->width, d->window->height);
461 gst_video_overlay_set_window_handle(d->overlay, (guintptr) window_handle);
462 gst_video_overlay_set_render_rectangle(d->overlay,
463 d->window->x, d->window->y,
464 d->window->width, d->window->height);
468 else if (GST_MESSAGE_TYPE(message) == GST_MESSAGE_ERROR) {
470 gchar* dbg_info = NULL;
472 gst_message_parse_error(message, &err, &dbg_info);
473 g_printerr("ERROR from element %s: %s code %d\n",
474 GST_OBJECT_NAME(message->src), err->message, err->code);
475 g_printerr("Debugging info: %s\n", (dbg_info) ? dbg_info : "none");
476 gst_pipeline_failed = TRUE;
485 gst_message_unref(message);
490 handle_xdg_surface_configure(void *data, struct xdg_surface *surface, uint32_t serial)
492 struct window *window = static_cast<struct window *>(data);
494 xdg_surface_ack_configure(surface, serial);
496 if (window->wait_for_configure) {
497 redraw(window, NULL, 0);
498 window->wait_for_configure = false;
502 static const struct xdg_surface_listener xdg_surface_listener = {
503 handle_xdg_surface_configure,
507 handle_xdg_toplevel_configure(void *data, struct xdg_toplevel *xdg_toplevel,
508 int32_t width, int32_t height,
509 struct wl_array *states)
511 struct window *window = static_cast<struct window *>(data);
514 window->fullscreen = 0;
515 window->maximized = 0;
517 // use our own macro as C++ can't typecast from (void *) directly
518 WL_ARRAY_FOR_EACH(p, states, uint32_t *) {
521 case XDG_TOPLEVEL_STATE_FULLSCREEN:
522 window->fullscreen = 1;
524 case XDG_TOPLEVEL_STATE_MAXIMIZED:
525 window->maximized = 1;
530 if (width > 0 && height > 0) {
531 if (!window->fullscreen && !window->maximized) {
532 window->init_width = width;
533 window->init_height = height;
535 window->width = width;
536 window->height = height;
537 } else if (!window->fullscreen && !window->maximized) {
538 window->width = window->init_width;
539 window->height = window->init_height;
542 window->needs_update_buffer = true;
547 handle_xdg_toplevel_close(void *data, struct xdg_toplevel *xdg_toplevel)
552 static const struct xdg_toplevel_listener xdg_toplevel_listener = {
553 handle_xdg_toplevel_configure,
554 handle_xdg_toplevel_close,
557 static struct window *
558 create_window(struct display *display, int width, int height, const char *app_id)
560 struct window *window;
563 assert(display->wm_base != NULL);
565 window = static_cast<struct window *>(calloc(1, sizeof(*window)));
569 wl_list_init(&window->buffer_list);
570 window->callback = NULL;
571 window->display = display;
572 window->width = width;
573 window->height = height;
574 window->surface = wl_compositor_create_surface(display->wl_compositor);
576 if (display->wm_base) {
577 window->xdg_surface =
578 xdg_wm_base_get_xdg_surface(display->wm_base, window->surface);
579 assert(window->xdg_surface);
581 xdg_surface_add_listener(window->xdg_surface,
582 &xdg_surface_listener, window);
583 window->xdg_toplevel = xdg_surface_get_toplevel(window->xdg_surface);
584 assert(window->xdg_toplevel);
586 xdg_toplevel_add_listener(window->xdg_toplevel,
587 &xdg_toplevel_listener, window);
589 xdg_toplevel_set_app_id(window->xdg_toplevel, app_id);
591 wl_surface_commit(window->surface);
592 window->wait_for_configure = true;
595 for (i = 0; i < MAX_BUFFER_ALLOC; i++)
596 alloc_buffer(window, window->width, window->height);
603 destroy_window(struct window *window)
605 struct buffer *buffer, *buffer_next;
607 if (window->callback)
608 wl_callback_destroy(window->callback);
610 wl_list_for_each_safe(buffer, buffer_next,
611 &window->buffer_list, buffer_link)
612 destroy_buffer(buffer);
614 if (window->xdg_toplevel)
615 xdg_toplevel_destroy(window->xdg_toplevel);
617 if (window->xdg_surface)
618 xdg_surface_destroy(window->xdg_surface);
620 wl_surface_destroy(window->surface);
625 signal_int(int sig, siginfo_t *si, void *_unused)
630 static struct display *
631 create_display(int argc, char *argv[])
633 struct display *display;
635 display = static_cast<struct display *>(calloc(1, sizeof(*display)));
636 if (display == NULL) {
637 fprintf(stderr, "out of memory\n");
640 display->wl_display = wl_display_connect(NULL);
641 assert(display->wl_display);
643 display->has_xrgb = false;
644 display->wl_registry = wl_display_get_registry(display->wl_display);
646 wl_registry_add_listener(display->wl_registry, ®istry_listener, display);
647 wl_display_roundtrip(display->wl_display);
649 if (display->shm == NULL) {
650 fprintf(stderr, "No wl_shm global\n");
654 wl_display_roundtrip(display->wl_display);
656 if (!display->has_xrgb) {
657 fprintf(stderr, "WL_SHM_FORMAT_XRGB32 not available\n");
665 destroy_display(struct display *display)
668 wl_shm_destroy(display->shm);
670 if (display->wm_base)
671 xdg_wm_base_destroy(display->wm_base);
673 if (display->wl_compositor)
674 wl_compositor_destroy(display->wl_compositor);
676 wl_registry_destroy(display->wl_registry);
677 wl_display_flush(display->wl_display);
678 wl_display_disconnect(display->wl_display);
682 // stringify the un-quoted string to quoted C string
683 #define xstr(a) str(a)
686 GstElement* create_pipeline(int* argc, char** argv[])
688 GError *error = NULL;
689 const char *camera_device = NULL;
690 const char *width_str = NULL;
691 const char *height_str = NULL;
695 // pipewire is default.
696 char *v4l2_path = getenv("ENABLE_V4L2_PATH");
699 char pipeline_str[1024];
701 camera_device = getenv("DEFAULT_V4L2_DEVICE");
703 camera_device = get_first_camera_device();
705 width_str = getenv("DEFAULT_DEVICE_WIDTH");
707 width = WINDOW_WIDTH_SIZE;
709 width = atoi(width_str);
711 height_str = getenv("DEFAULT_DEVICE_HEIGHT");
713 height = WINDOW_HEIGHT_SIZE;
715 height = atoi(height_str);
717 if (v4l2_path == NULL)
719 else if (g_str_equal(v4l2_path, "yes") || g_str_equal(v4l2_path, "true"))
722 memset(pipeline_str, 0, sizeof(pipeline_str));
725 snprintf(pipeline_str, sizeof(pipeline_str), "v4l2src device=%s ! video/x-raw,width=%d,height=%d ! waylandsink",
726 camera_device, width, height);
727 else if (gst_pipeline_failed == TRUE) {
728 snprintf(pipeline_str, sizeof(pipeline_str), "filesrc location=%s/still-image.jpg ! decodebin ! videoconvert ! imagefreeze ! waylandsink fullscreen=true",
729 xstr(APP_DATA_PATH));
730 fallback_gst_pipeline_tried = TRUE;
733 snprintf(pipeline_str, sizeof(pipeline_str), "pipewiresrc ! video/x-raw,width=%d,height=%d ! waylandsink", width,
737 fprintf(stdout, "Using pipeline: %s\n", pipeline_str);
739 GstElement *pipeline = gst_parse_launch(pipeline_str, &error);
741 if (error || !pipeline) {
742 fprintf(stderr, "gstreamer pipeline construction failed!\n");
750 int main(int argc, char* argv[])
754 struct receiver_data receiver_data = {};
755 struct display* display;
756 struct window* window;
757 const char* app_id = "camera-gstreamer";
759 sa.sa_sigaction = signal_int;
760 sigemptyset(&sa.sa_mask);
761 sa.sa_flags = SA_RESETHAND | SA_SIGINFO;
762 sigaction(SIGINT, &sa, NULL);
765 char** gargv = static_cast<char**>(calloc(2, sizeof(char*)));
767 gargv[0] = strdup(argv[0]);
768 gargv[1] = strdup("--gst-debug-level=2");
770 setbuf(stdout, NULL);
772 gst_init(&gargc, &gargv);
774 receiver_data.pipeline = create_pipeline(&gargc, &gargv);
776 if (!receiver_data.pipeline)
779 display = create_display(argc, argv);
783 // we use the role to set a correspondence between the top level
784 // surface and our application, with the previous call letting the
785 // compositor know that we're one and the same
786 window = create_window(display, WINDOW_WIDTH_SIZE, WINDOW_HEIGHT_SIZE, app_id);
793 window->display = display;
794 receiver_data.window = window;
796 /* Initialise damage to full surface, so the padding gets painted */
797 wl_surface_damage(window->surface, 0, 0,
798 window->width, window->height);
800 if (!window->wait_for_configure) {
801 redraw(window, NULL, 0);
804 GstBus *bus = gst_element_get_bus(receiver_data.pipeline);
805 gst_bus_add_signal_watch(bus);
807 g_signal_connect(bus, "message::error", G_CALLBACK(error_cb), &receiver_data);
808 gst_bus_set_sync_handler(bus, bus_sync_handler, &receiver_data, NULL);
809 gst_object_unref(bus);
811 gst_element_set_state(receiver_data.pipeline, GST_STATE_PLAYING);
812 fprintf(stdout, "gstreamer pipeline running\n");
814 // run the application
815 while (running && ret != -1) {
816 ret = wl_display_dispatch(display->wl_display);
817 if (gst_pipeline_failed && fallback_gst_pipeline_tried == FALSE) {
818 gst_element_set_state(receiver_data.pipeline, GST_STATE_NULL);
819 gst_object_unref(receiver_data.pipeline);
820 /* retry with fallback pipeline */
821 receiver_data.pipeline = create_pipeline(&gargc, &gargv);
822 GstBus *bus = gst_element_get_bus(receiver_data.pipeline);
823 gst_bus_add_signal_watch(bus);
824 g_signal_connect(bus, "message::error", G_CALLBACK(error_cb), &receiver_data);
825 gst_bus_set_sync_handler(bus, bus_sync_handler, &receiver_data, NULL);
826 gst_object_unref(bus);
827 gst_element_set_state(receiver_data.pipeline, GST_STATE_PLAYING);
830 gst_element_set_state(receiver_data.pipeline, GST_STATE_NULL);
831 gst_object_unref(receiver_data.pipeline);
833 destroy_window(window);
834 destroy_display(display);