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;
102 redraw(void *data, struct wl_callback *callback, uint32_t time);
104 static struct buffer *
105 alloc_buffer(struct window *window, int width, int height)
107 struct buffer *buffer = static_cast<struct buffer *>(calloc(1, sizeof(*buffer)));
109 buffer->width = width;
110 buffer->height = height;
111 wl_list_insert(&window->buffer_list, &buffer->buffer_link);
117 destroy_buffer(struct buffer *buffer)
120 wl_buffer_destroy(buffer->buffer);
122 munmap(buffer->shm_data, buffer->size);
123 wl_list_remove(&buffer->buffer_link);
127 static struct buffer *
128 pick_free_buffer(struct window *window)
131 struct buffer *buffer = NULL;
133 wl_list_for_each(b, &window->buffer_list, buffer_link) {
144 prune_old_released_buffers(struct window *window)
146 struct buffer *b, *b_next;
148 wl_list_for_each_safe(b, b_next,
149 &window->buffer_list, buffer_link) {
150 if (!b->busy && (b->width != window->width ||
151 b->height != window->height))
157 paint_pixels(void *image, int padding, int width, int height, uint32_t time)
159 memset(image, 0x00, width * height * 4);
163 buffer_release(void *data, struct wl_buffer *buffer)
165 struct buffer *mybuf = static_cast<struct buffer *>(data);
169 static const struct wl_buffer_listener buffer_listener = {
175 create_shm_buffer(struct display *display, struct buffer *buffer,
176 int width, int height, uint32_t format)
178 struct wl_shm_pool *pool;
179 int fd, size, stride;
183 size = stride * height;
185 fd = os_create_anonymous_file(size);
187 fprintf(stderr, "creating a buffer file for %d B failed: %s\n",
188 size, strerror(errno));
192 data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
193 if (data == MAP_FAILED) {
194 fprintf(stderr, "mmap failed: %s\n", strerror(errno));
199 pool = wl_shm_create_pool(display->shm, fd, size);
200 buffer->buffer = wl_shm_pool_create_buffer(pool, 0, width,
201 height, stride, format);
202 wl_buffer_add_listener(buffer->buffer, &buffer_listener, buffer);
203 wl_shm_pool_destroy(pool);
206 buffer->shm_data = data;
208 buffer->width = width;
209 buffer->height = height;
211 fprintf(stdout, "Created shm buffer with width %d, height %d\n", width, height);
215 static struct buffer *
216 get_next_buffer(struct window *window)
218 struct buffer *buffer = NULL;
221 if (window->needs_update_buffer) {
224 for (i = 0; i < MAX_BUFFER_ALLOC; i++)
225 alloc_buffer(window, window->width, window->height);
227 window->needs_update_buffer = false;
230 buffer = pick_free_buffer(window);
235 if (!buffer->buffer) {
236 ret = create_shm_buffer(window->display, buffer, window->width,
237 window->height, WL_SHM_FORMAT_XRGB8888);
242 /* paint the padding */
243 memset(buffer->shm_data, 0x00, window->width * window->height * 4);
250 static const struct wl_callback_listener frame_listener = {
255 redraw(void *data, struct wl_callback *callback, uint32_t time)
257 struct window *window = static_cast<struct window *>(data);
258 struct buffer *buffer;
260 prune_old_released_buffers(window);
262 buffer = get_next_buffer(window);
265 !callback ? "Failed to create the first buffer.\n" :
266 "Both buffers busy at redraw(). Server bug?\n");
270 // do the actual painting
271 paint_pixels(buffer->shm_data, 0x0, window->width, window->height, time);
273 wl_surface_attach(window->surface, buffer->buffer, 0, 0);
274 wl_surface_damage(window->surface, 0, 0, window->width, window->height);
277 wl_callback_destroy(callback);
279 window->callback = wl_surface_frame(window->surface);
280 wl_callback_add_listener(window->callback, &frame_listener, window);
281 wl_surface_commit(window->surface);
287 shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
289 struct display *d = static_cast<struct display *>(data);
291 if (format == WL_SHM_FORMAT_XRGB8888)
295 static const struct wl_shm_listener shm_listener = {
300 xdg_wm_base_ping(void *data, struct xdg_wm_base *shell, uint32_t serial)
302 xdg_wm_base_pong(shell, serial);
305 static const struct xdg_wm_base_listener xdg_wm_base_listener = {
310 display_handle_geometry(void *data, struct wl_output *wl_output,
311 int x, int y, int physical_width, int physical_height,
312 int subpixel, const char *make, const char *model, int transform)
318 (void) physical_width;
319 (void) physical_height;
327 display_handle_mode(void *data, struct wl_output *wl_output, uint32_t flags,
328 int width, int height, int refresh)
330 struct display *d = static_cast<struct display *>(data);
332 if (wl_output == d->wl_output && (flags & WL_OUTPUT_MODE_CURRENT)) {
333 d->output_data.width = width;
334 d->output_data.height = height;
336 fprintf(stdout, "Found output with width %d and height %d\n",
337 d->output_data.width, d->output_data.height);
342 display_handle_scale(void *data, struct wl_output *wl_output, int scale)
350 display_handle_done(void *data, struct wl_output *wl_output)
356 static const struct wl_output_listener output_listener = {
357 display_handle_geometry,
364 application_id(void *data, struct agl_shell_desktop *agl_shell_desktop,
368 (void) agl_shell_desktop;
373 application_id_state(void *data, struct agl_shell_desktop *agl_shell_desktop,
374 const char *app_id, const char *app_data,
375 uint32_t app_state, uint32_t app_role)
379 (void) agl_shell_desktop;
385 static const struct agl_shell_desktop_listener agl_shell_desktop_listener = {
387 application_id_state,
391 registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
392 const char *interface, uint32_t version)
394 struct display *d = static_cast<struct display *>(data);
396 if (strcmp(interface, "wl_compositor") == 0) {
398 static_cast<struct wl_compositor *>(wl_registry_bind(registry, id,
399 &wl_compositor_interface, 1));
400 } else if (strcmp(interface, "xdg_wm_base") == 0) {
401 d->wm_base = static_cast<struct xdg_wm_base *>(wl_registry_bind(registry,
402 id, &xdg_wm_base_interface, 1));
403 xdg_wm_base_add_listener(d->wm_base, &xdg_wm_base_listener, d);
404 } else if (strcmp(interface, "wl_shm") == 0) {
405 d->shm = static_cast<struct wl_shm *>(wl_registry_bind(registry,
406 id, &wl_shm_interface, 1));
407 wl_shm_add_listener(d->shm, &shm_listener, d);
408 } else if (strcmp(interface, "agl_shell_desktop") == 0) {
409 d->agl_shell_desktop = static_cast<struct agl_shell_desktop *>(wl_registry_bind(registry, id,
410 &agl_shell_desktop_interface, 1));
411 /* as an example, show how to register for events from the compositor */
412 agl_shell_desktop_add_listener(d->agl_shell_desktop,
413 &agl_shell_desktop_listener, d);
414 } else if (strcmp(interface, "wl_output") == 0) {
415 d->wl_output = static_cast<struct wl_output *>(wl_registry_bind(registry, id,
416 &wl_output_interface, 1));
417 wl_output_add_listener(d->wl_output, &output_listener, d);
422 registry_handle_global_remove(void *data, struct wl_registry *reg, uint32_t id)
429 static const struct wl_registry_listener registry_listener = {
430 registry_handle_global,
431 registry_handle_global_remove,
436 error_cb(GstBus *bus, GstMessage *msg, gpointer user_data)
438 struct receiver_data *d =
439 static_cast<struct receiver_data *>(user_data);
444 gst_message_parse_error(msg, &err, &debug);
446 g_print("Error: %s\n", err->message);
450 g_print("Debug details: %s\n", debug);
454 gst_element_set_state(d->pipeline, GST_STATE_NULL);
457 static GstBusSyncReply
458 bus_sync_handler(GstBus *bus, GstMessage *message, gpointer user_data)
460 struct receiver_data *d =
461 static_cast<struct receiver_data *>(user_data);
463 if (gst_is_wayland_display_handle_need_context_message(message)) {
465 struct wl_display *display_handle = d->window->display->wl_display;
467 context = gst_wayland_display_handle_context_new(display_handle);
468 d->wl_video = GST_WAYLAND_VIDEO(GST_MESSAGE_SRC(message));
469 gst_element_set_context(GST_ELEMENT(GST_MESSAGE_SRC(message)), context);
472 } else if (gst_is_video_overlay_prepare_window_handle_message(message)) {
473 struct wl_surface *window_handle = d->window->surface;
475 /* GST_MESSAGE_SRC(message) will be the overlay object that we
476 * have to use. This may be waylandsink, but it may also be
477 * playbin. In the latter case, we must make sure to use
478 * playbin instead of waylandsink, because playbin resets the
479 * window handle and render_rectangle after restarting playback
480 * and the actual window size is lost */
481 d->overlay = GST_VIDEO_OVERLAY(GST_MESSAGE_SRC(message));
483 g_print("setting window handle and size (%d x %d) w %d, h %d\n",
484 d->window->x, d->window->y,
485 d->window->width, d->window->height);
487 gst_video_overlay_set_window_handle(d->overlay, (guintptr) window_handle);
488 gst_video_overlay_set_render_rectangle(d->overlay,
489 d->window->x, d->window->y,
490 d->window->width, d->window->height);
498 gst_message_unref(message);
503 handle_xdg_surface_configure(void *data, struct xdg_surface *surface, uint32_t serial)
505 struct window *window = static_cast<struct window *>(data);
507 xdg_surface_ack_configure(surface, serial);
509 if (window->wait_for_configure) {
510 redraw(window, NULL, 0);
511 window->wait_for_configure = false;
515 static const struct xdg_surface_listener xdg_surface_listener = {
516 handle_xdg_surface_configure,
520 handle_xdg_toplevel_configure(void *data, struct xdg_toplevel *xdg_toplevel,
521 int32_t width, int32_t height,
522 struct wl_array *states)
524 struct window *window = static_cast<struct window *>(data);
527 window->fullscreen = 0;
528 window->maximized = 0;
530 // use our own macro as C++ can't typecast from (void *) directly
531 WL_ARRAY_FOR_EACH(p, states, uint32_t *) {
534 case XDG_TOPLEVEL_STATE_FULLSCREEN:
535 window->fullscreen = 1;
537 case XDG_TOPLEVEL_STATE_MAXIMIZED:
538 window->maximized = 1;
543 if (width > 0 && height > 0) {
544 if (!window->fullscreen && !window->maximized) {
545 window->init_width = width;
546 window->init_height = height;
548 window->width = width;
549 window->height = height;
550 } else if (!window->fullscreen && !window->maximized) {
551 window->width = window->init_width;
552 window->height = window->init_height;
555 window->needs_update_buffer = true;
560 handle_xdg_toplevel_close(void *data, struct xdg_toplevel *xdg_toplevel)
565 static const struct xdg_toplevel_listener xdg_toplevel_listener = {
566 handle_xdg_toplevel_configure,
567 handle_xdg_toplevel_close,
570 static struct window *
571 create_window(struct display *display, int width, int height, const char *app_id)
573 struct window *window;
576 assert(display->wm_base != NULL);
578 window = static_cast<struct window *>(calloc(1, sizeof(*window)));
582 wl_list_init(&window->buffer_list);
583 window->callback = NULL;
584 window->display = display;
585 window->width = width;
586 window->height = height;
587 window->surface = wl_compositor_create_surface(display->wl_compositor);
589 if (display->wm_base) {
590 window->xdg_surface =
591 xdg_wm_base_get_xdg_surface(display->wm_base, window->surface);
592 assert(window->xdg_surface);
594 xdg_surface_add_listener(window->xdg_surface,
595 &xdg_surface_listener, window);
596 window->xdg_toplevel = xdg_surface_get_toplevel(window->xdg_surface);
597 assert(window->xdg_toplevel);
599 xdg_toplevel_add_listener(window->xdg_toplevel,
600 &xdg_toplevel_listener, window);
602 xdg_toplevel_set_app_id(window->xdg_toplevel, app_id);
604 wl_surface_commit(window->surface);
605 window->wait_for_configure = true;
608 for (i = 0; i < MAX_BUFFER_ALLOC; i++)
609 alloc_buffer(window, window->width, window->height);
616 destroy_window(struct window *window)
618 struct buffer *buffer, *buffer_next;
620 if (window->callback)
621 wl_callback_destroy(window->callback);
623 wl_list_for_each_safe(buffer, buffer_next,
624 &window->buffer_list, buffer_link)
625 destroy_buffer(buffer);
627 if (window->xdg_toplevel)
628 xdg_toplevel_destroy(window->xdg_toplevel);
630 if (window->xdg_surface)
631 xdg_surface_destroy(window->xdg_surface);
633 wl_surface_destroy(window->surface);
638 signal_int(int sig, siginfo_t *si, void *_unused)
643 static struct display *
644 create_display(int argc, char *argv[])
646 struct display *display;
648 display = static_cast<struct display *>(calloc(1, sizeof(*display)));
649 if (display == NULL) {
650 fprintf(stderr, "out of memory\n");
653 display->wl_display = wl_display_connect(NULL);
654 assert(display->wl_display);
656 display->has_xrgb = false;
657 display->wl_registry = wl_display_get_registry(display->wl_display);
659 wl_registry_add_listener(display->wl_registry, ®istry_listener, display);
660 wl_display_roundtrip(display->wl_display);
662 if (display->shm == NULL) {
663 fprintf(stderr, "No wl_shm global\n");
667 if (display->agl_shell_desktop == NULL) {
668 fprintf(stderr, "No agl_shell extension present\n");
672 wl_display_roundtrip(display->wl_display);
674 if (!display->has_xrgb) {
675 fprintf(stderr, "WL_SHM_FORMAT_XRGB32 not available\n");
683 destroy_display(struct display *display)
686 wl_shm_destroy(display->shm);
688 if (display->wm_base)
689 xdg_wm_base_destroy(display->wm_base);
691 if (display->agl_shell_desktop)
692 agl_shell_desktop_destroy(display->agl_shell_desktop);
694 if (display->wl_compositor)
695 wl_compositor_destroy(display->wl_compositor);
697 wl_registry_destroy(display->wl_registry);
698 wl_display_flush(display->wl_display);
699 wl_display_disconnect(display->wl_display);
703 int main(int argc, char *argv[])
707 struct receiver_data receiver_data = {};
708 struct display *display;
709 struct window *window;
710 const char *camera_device = NULL;
711 const char *width_str = NULL;
712 const char *height_str = NULL;
716 // pipewire is default.
717 char *v4l2_path = getenv("ENABLE_V4L2_PATH");
720 char pipeline_str[1024];
721 GError *error = NULL;
722 const char *app_id = "camera-gstreamer";
724 sa.sa_sigaction = signal_int;
725 sigemptyset(&sa.sa_mask);
726 sa.sa_flags = SA_RESETHAND | SA_SIGINFO;
727 sigaction(SIGINT, &sa, NULL);
730 char **gargv = static_cast<char **>(calloc(2, sizeof(char *)));
732 gargv[0] = strdup(argv[0]);
733 gargv[1] = strdup("--gst-debug-level=2");
735 camera_device = getenv("DEFAULT_V4L2_DEVICE");
737 camera_device = get_first_camera_device();
739 width_str = getenv("DEFAULT_DEVICE_WIDTH");
741 width = WINDOW_WIDTH_SIZE;
743 width = atoi(width_str);
745 height_str = getenv("DEFAULT_DEVICE_HEIGHT");
747 height = WINDOW_HEIGHT_SIZE;
749 height = atoi(height_str);
751 if (v4l2_path == NULL)
753 else if (g_str_equal(v4l2_path, "yes") || g_str_equal(v4l2_path, "true"))
756 memset(pipeline_str, 0, sizeof(pipeline_str));
759 snprintf(pipeline_str, sizeof(pipeline_str), "v4l2src device=%s ! video/x-raw,width=%d,height=%d ! waylandsink",
760 camera_device, width, height);
762 snprintf(pipeline_str, sizeof(pipeline_str), "pipewiresrc ! video/x-raw,width=%d,height=%d ! waylandsink",
765 gst_init(&gargc, &gargv);
767 setbuf(stdout, NULL);
769 fprintf(stdout, "Using pipeline: %s\n", pipeline_str);
771 GstElement *pipeline = gst_parse_launch(pipeline_str, &error);
772 if (error || !pipeline) {
773 fprintf(stderr, "gstreamer pipeline construction failed!\n");
778 receiver_data.pipeline = pipeline;
780 display = create_display(argc, argv);
784 // if you'd want to place the video in a pop-up/dialog type of window:
785 // agl_shell_desktop_set_app_property(display->agl_shell_desktop, app_id,
786 // AGL_SHELL_DESKTOP_APP_ROLE_POPUP,
787 // WINDOW_WIDTH_POS_X, WINDOW_WIDTH_POS_Y,
788 // 0, 0, WINDOW_WIDTH_SIZE, WINDOW_HEIGHT_SIZE,
789 // display->wl_output);
791 // we use the role to set a correspondence between the top level
792 // surface and our application, with the previous call letting the
793 // compositor know that we're one and the same
794 window = create_window(display, WINDOW_WIDTH_SIZE, WINDOW_HEIGHT_SIZE, app_id);
801 window->display = display;
802 receiver_data.window = window;
804 /* Initialise damage to full surface, so the padding gets painted */
805 wl_surface_damage(window->surface, 0, 0,
806 window->width, window->height);
808 if (!window->wait_for_configure) {
809 redraw(window, NULL, 0);
812 GstBus *bus = gst_element_get_bus(pipeline);
813 gst_bus_add_signal_watch(bus);
815 g_signal_connect(bus, "message::error", G_CALLBACK(error_cb), &receiver_data);
816 gst_bus_set_sync_handler(bus, bus_sync_handler, &receiver_data, NULL);
817 gst_object_unref(bus);
819 gst_element_set_state(pipeline, GST_STATE_PLAYING);
820 fprintf(stdout, "gstreamer pipeline running\n");
822 // run the application
823 while (running && ret != -1)
824 ret = wl_display_dispatch(display->wl_display);
826 gst_element_set_state(pipeline, GST_STATE_NULL);
827 gst_object_unref(pipeline);
829 destroy_window(window);
830 destroy_display(display);