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 #define DEFAULT_VIDEO_DEVICE "/dev/video0"
28 // these only applies if the window is a dialog/pop-up one
29 // by default the compositor make the window maximized
30 #define WINDOW_WIDTH_SIZE 640
31 #define WINDOW_HEIGHT_SIZE 720
33 #define WINDOW_WIDTH_POS_X 640
34 #define WINDOW_WIDTH_POS_Y 180
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;
67 struct display *display;
72 struct wl_surface *surface;
74 struct xdg_surface *xdg_surface;
75 struct xdg_toplevel *xdg_toplevel;
76 bool wait_for_configure;
78 int fullscreen, maximized;
80 struct buffer buffers[2];
81 struct wl_callback *callback;
85 struct receiver_data {
86 struct window *window;
89 GstWaylandVideo *wl_video;
90 GstVideoOverlay *overlay;
93 static int running = 1;
96 redraw(void *data, struct wl_callback *callback, uint32_t time);
99 paint_pixels(void *image, int padding, int width, int height, uint32_t time)
101 memset(image, 0x00, width * height * 4);
105 buffer_release(void *data, struct wl_buffer *buffer)
107 struct buffer *mybuf = static_cast<struct buffer *>(data);
111 static const struct wl_buffer_listener buffer_listener = {
117 create_shm_buffer(struct display *display, struct buffer *buffer,
118 int width, int height, uint32_t format)
120 struct wl_shm_pool *pool;
121 int fd, size, stride;
125 size = stride * height;
127 fd = os_create_anonymous_file(size);
129 fprintf(stderr, "creating a buffer file for %d B failed: %s\n",
130 size, strerror(errno));
134 data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
135 if (data == MAP_FAILED) {
136 fprintf(stderr, "mmap failed: %s\n", strerror(errno));
141 pool = wl_shm_create_pool(display->shm, fd, size);
142 buffer->buffer = wl_shm_pool_create_buffer(pool, 0, width,
143 height, stride, format);
144 wl_buffer_add_listener(buffer->buffer, &buffer_listener, buffer);
145 wl_shm_pool_destroy(pool);
148 buffer->shm_data = data;
149 fprintf(stdout, "Created shm buffer with width %d, height %d\n", width, height);
153 static struct buffer *
154 get_next_buffer(struct window *window)
156 struct buffer *buffer = NULL;
159 /* we need to create new buffers for the resized value so discard
160 * the 'old' one and force creation of the buffer with the newer
162 if (window->wait_for_configure && window->maximized) {
163 if (!window->buffers[0].busy) {
164 wl_buffer_destroy(window->buffers[0].buffer);
165 window->buffers[0].buffer = NULL;
166 window->wait_for_configure = false;
170 if (!window->buffers[0].busy) {
171 buffer = &window->buffers[0];
172 } else if (!window->buffers[1].busy) {
173 buffer = &window->buffers[1];
178 if (!buffer->buffer) {
179 ret = create_shm_buffer(window->display, buffer, window->width,
180 window->height, WL_SHM_FORMAT_XRGB8888);
185 /* paint the padding */
186 memset(buffer->shm_data, 0x00, window->width * window->height * 4);
193 static const struct wl_callback_listener frame_listener = {
198 redraw(void *data, struct wl_callback *callback, uint32_t time)
200 struct window *window = static_cast<struct window *>(data);
201 struct buffer *buffer;
203 buffer = get_next_buffer(window);
206 !callback ? "Failed to create the first buffer.\n" :
207 "Both buffers busy at redraw(). Server bug?\n");
211 // do the actual painting
212 paint_pixels(buffer->shm_data, 0x0, window->width, window->height, time);
214 wl_surface_attach(window->surface, buffer->buffer, 0, 0);
215 wl_surface_damage(window->surface, 0, 0, window->width, window->height);
218 wl_callback_destroy(callback);
220 window->callback = wl_surface_frame(window->surface);
221 wl_callback_add_listener(window->callback, &frame_listener, window);
222 wl_surface_commit(window->surface);
228 shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
230 struct display *d = static_cast<struct display *>(data);
232 if (format == WL_SHM_FORMAT_XRGB8888)
236 static const struct wl_shm_listener shm_listener = {
241 xdg_wm_base_ping(void *data, struct xdg_wm_base *shell, uint32_t serial)
243 xdg_wm_base_pong(shell, serial);
246 static const struct xdg_wm_base_listener xdg_wm_base_listener = {
251 display_handle_geometry(void *data, struct wl_output *wl_output,
252 int x, int y, int physical_width, int physical_height,
253 int subpixel, const char *make, const char *model, int transform)
259 (void) physical_width;
260 (void) physical_height;
268 display_handle_mode(void *data, struct wl_output *wl_output, uint32_t flags,
269 int width, int height, int refresh)
271 struct display *d = static_cast<struct display *>(data);
273 if (wl_output == d->wl_output && (flags & WL_OUTPUT_MODE_CURRENT)) {
274 d->output_data.width = width;
275 d->output_data.height = height;
277 fprintf(stdout, "Found output with width %d and height %d\n",
278 d->output_data.width, d->output_data.height);
283 display_handle_scale(void *data, struct wl_output *wl_output, int scale)
291 display_handle_done(void *data, struct wl_output *wl_output)
297 static const struct wl_output_listener output_listener = {
298 display_handle_geometry,
305 application_id(void *data, struct agl_shell_desktop *agl_shell_desktop,
309 (void) agl_shell_desktop;
314 application_id_state(void *data, struct agl_shell_desktop *agl_shell_desktop,
315 const char *app_id, const char *app_data,
316 uint32_t app_state, uint32_t app_role)
320 (void) agl_shell_desktop;
326 static const struct agl_shell_desktop_listener agl_shell_desktop_listener = {
328 application_id_state,
332 registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
333 const char *interface, uint32_t version)
335 struct display *d = static_cast<struct display *>(data);
337 if (strcmp(interface, "wl_compositor") == 0) {
339 static_cast<struct wl_compositor *>(wl_registry_bind(registry, id,
340 &wl_compositor_interface, 1));
341 } else if (strcmp(interface, "xdg_wm_base") == 0) {
342 d->wm_base = static_cast<struct xdg_wm_base *>(wl_registry_bind(registry,
343 id, &xdg_wm_base_interface, 1));
344 xdg_wm_base_add_listener(d->wm_base, &xdg_wm_base_listener, d);
345 } else if (strcmp(interface, "wl_shm") == 0) {
346 d->shm = static_cast<struct wl_shm *>(wl_registry_bind(registry,
347 id, &wl_shm_interface, 1));
348 wl_shm_add_listener(d->shm, &shm_listener, d);
349 } else if (strcmp(interface, "agl_shell_desktop") == 0) {
350 d->agl_shell_desktop = static_cast<struct agl_shell_desktop *>(wl_registry_bind(registry, id,
351 &agl_shell_desktop_interface, 1));
352 /* as an example, show how to register for events from the compositor */
353 agl_shell_desktop_add_listener(d->agl_shell_desktop,
354 &agl_shell_desktop_listener, d);
355 } else if (strcmp(interface, "wl_output") == 0) {
356 d->wl_output = static_cast<struct wl_output *>(wl_registry_bind(registry, id,
357 &wl_output_interface, 1));
358 wl_output_add_listener(d->wl_output, &output_listener, d);
363 registry_handle_global_remove(void *data, struct wl_registry *reg, uint32_t id)
370 static const struct wl_registry_listener registry_listener = {
371 registry_handle_global,
372 registry_handle_global_remove,
377 error_cb(GstBus *bus, GstMessage *msg, gpointer user_data)
379 struct receiver_data *d =
380 static_cast<struct receiver_data *>(user_data);
385 gst_message_parse_error(msg, &err, &debug);
387 g_print("Error: %s\n", err->message);
391 g_print("Debug details: %s\n", debug);
395 gst_element_set_state(d->pipeline, GST_STATE_NULL);
398 static GstBusSyncReply
399 bus_sync_handler(GstBus *bus, GstMessage *message, gpointer user_data)
401 struct receiver_data *d =
402 static_cast<struct receiver_data *>(user_data);
404 if (gst_is_wayland_display_handle_need_context_message(message)) {
406 struct wl_display *display_handle = d->window->display->wl_display;
408 context = gst_wayland_display_handle_context_new(display_handle);
409 d->wl_video = GST_WAYLAND_VIDEO(GST_MESSAGE_SRC(message));
410 gst_element_set_context(GST_ELEMENT(GST_MESSAGE_SRC(message)), context);
413 } else if (gst_is_video_overlay_prepare_window_handle_message(message)) {
414 struct wl_surface *window_handle = d->window->surface;
416 /* GST_MESSAGE_SRC(message) will be the overlay object that we
417 * have to use. This may be waylandsink, but it may also be
418 * playbin. In the latter case, we must make sure to use
419 * playbin instead of waylandsink, because playbin resets the
420 * window handle and render_rectangle after restarting playback
421 * and the actual window size is lost */
422 d->overlay = GST_VIDEO_OVERLAY(GST_MESSAGE_SRC(message));
424 g_print("setting window handle and size (%d x %d) w %d, h %d\n",
425 d->window->x, d->window->y,
426 d->window->width, d->window->height);
428 gst_video_overlay_set_window_handle(d->overlay, (guintptr) window_handle);
429 gst_video_overlay_set_render_rectangle(d->overlay,
430 d->window->x, d->window->y,
431 d->window->width, d->window->height);
439 gst_message_unref(message);
444 handle_xdg_surface_configure(void *data, struct xdg_surface *surface, uint32_t serial)
446 struct window *window = static_cast<struct window *>(data);
448 xdg_surface_ack_configure(surface, serial);
450 if (window->wait_for_configure) {
451 redraw(window, NULL, 0);
455 static const struct xdg_surface_listener xdg_surface_listener = {
456 handle_xdg_surface_configure,
460 handle_xdg_toplevel_configure(void *data, struct xdg_toplevel *xdg_toplevel,
461 int32_t width, int32_t height,
462 struct wl_array *states)
464 struct window *window = static_cast<struct window *>(data);
467 window->fullscreen = 0;
468 window->maximized = 0;
470 // use our own macro as C++ can't typecast from (void *) directly
471 WL_ARRAY_FOR_EACH(p, states, uint32_t *) {
474 case XDG_TOPLEVEL_STATE_FULLSCREEN:
475 window->fullscreen = 1;
477 case XDG_TOPLEVEL_STATE_MAXIMIZED:
478 window->maximized = 1;
483 if (width > 0 && height > 0) {
484 if (!window->fullscreen && !window->maximized) {
485 window->width = width;
486 window->height = height;
488 window->width = width;
489 window->height = height;
490 } else if (!window->fullscreen && !window->maximized) {
492 window->width = WINDOW_WIDTH_SIZE;
494 window->width = width;
497 window->height = WINDOW_HEIGHT_SIZE;
499 window->height = height;
502 /* if we've been resized set wait_for_configure to adjust the fb size
503 * in the frame callback handler, which will also clear this up */
504 if ((window->width > 0 && window->width != WINDOW_WIDTH_SIZE) &&
505 (window->height > 0 && window->height != WINDOW_HEIGHT_SIZE)) {
506 window->wait_for_configure = true;
511 handle_xdg_toplevel_close(void *data, struct xdg_toplevel *xdg_toplevel)
516 static const struct xdg_toplevel_listener xdg_toplevel_listener = {
517 handle_xdg_toplevel_configure,
518 handle_xdg_toplevel_close,
521 static struct window *
522 create_window(struct display *display, int width, int height, const char *app_id)
524 struct window *window;
526 assert(display->wm_base != NULL);
528 window = static_cast<struct window *>(calloc(1, sizeof(*window)));
532 window->callback = NULL;
533 window->display = display;
534 window->width = width;
535 window->height = height;
536 window->surface = wl_compositor_create_surface(display->wl_compositor);
538 if (display->wm_base) {
539 window->xdg_surface =
540 xdg_wm_base_get_xdg_surface(display->wm_base, window->surface);
541 assert(window->xdg_surface);
543 xdg_surface_add_listener(window->xdg_surface,
544 &xdg_surface_listener, window);
545 window->xdg_toplevel = xdg_surface_get_toplevel(window->xdg_surface);
546 assert(window->xdg_toplevel);
548 xdg_toplevel_add_listener(window->xdg_toplevel,
549 &xdg_toplevel_listener, window);
551 xdg_toplevel_set_app_id(window->xdg_toplevel, app_id);
553 wl_surface_commit(window->surface);
554 window->wait_for_configure = true;
562 destroy_window(struct window *window)
564 if (window->callback)
565 wl_callback_destroy(window->callback);
567 if (window->buffers[0].buffer)
568 wl_buffer_destroy(window->buffers[0].buffer);
569 if (window->buffers[1].buffer)
570 wl_buffer_destroy(window->buffers[1].buffer);
572 if (window->xdg_toplevel)
573 xdg_toplevel_destroy(window->xdg_toplevel);
575 if (window->xdg_surface)
576 xdg_surface_destroy(window->xdg_surface);
578 wl_surface_destroy(window->surface);
583 signal_int(int sig, siginfo_t *si, void *_unused)
588 static struct display *
589 create_display(int argc, char *argv[])
591 struct display *display;
593 display = static_cast<struct display *>(calloc(1, sizeof(*display)));
594 if (display == NULL) {
595 fprintf(stderr, "out of memory\n");
598 display->wl_display = wl_display_connect(NULL);
599 assert(display->wl_display);
601 display->has_xrgb = false;
602 display->wl_registry = wl_display_get_registry(display->wl_display);
604 wl_registry_add_listener(display->wl_registry, ®istry_listener, display);
605 wl_display_roundtrip(display->wl_display);
607 if (display->shm == NULL) {
608 fprintf(stderr, "No wl_shm global\n");
612 if (display->agl_shell_desktop == NULL) {
613 fprintf(stderr, "No agl_shell extension present\n");
617 wl_display_roundtrip(display->wl_display);
619 if (!display->has_xrgb) {
620 fprintf(stderr, "WL_SHM_FORMAT_XRGB32 not available\n");
628 destroy_display(struct display *display)
631 wl_shm_destroy(display->shm);
633 if (display->wm_base)
634 xdg_wm_base_destroy(display->wm_base);
636 if (display->agl_shell_desktop)
637 agl_shell_desktop_destroy(display->agl_shell_desktop);
639 if (display->wl_compositor)
640 wl_compositor_destroy(display->wl_compositor);
642 wl_registry_destroy(display->wl_registry);
643 wl_display_flush(display->wl_display);
644 wl_display_disconnect(display->wl_display);
648 int main(int argc, char *argv[])
652 struct receiver_data receiver_data = {};
653 struct display *display;
654 struct window *window;
655 char pipeline_str[1024];
656 GError *error = NULL;
657 const char *app_id = "camera-gstreamer";
659 sa.sa_sigaction = signal_int;
660 sigemptyset(&sa.sa_mask);
661 sa.sa_flags = SA_RESETHAND | SA_SIGINFO;
662 sigaction(SIGINT, &sa, NULL);
665 char **gargv = static_cast<char **>(calloc(2, sizeof(char *)));
667 gargv[0] = strdup(argv[0]);
668 gargv[1] = strdup("--gst-debug-level=2");
670 memset(pipeline_str, 0, sizeof(pipeline_str));
671 snprintf(pipeline_str, sizeof(pipeline_str), "v4l2src device=%s ! video/x-raw,width=%d,height=%d ! waylandsink",
672 DEFAULT_VIDEO_DEVICE, WINDOW_WIDTH_SIZE, WINDOW_HEIGHT_SIZE);
673 gst_init(&gargc, &gargv);
675 fprintf(stdout, "Using pipeline: %s\n", pipeline_str);
677 GstElement *pipeline = gst_parse_launch(pipeline_str, &error);
678 if (error || !pipeline) {
679 fprintf(stderr, "gstreamer pipeline construction failed!\n");
684 receiver_data.pipeline = pipeline;
686 display = create_display(argc, argv);
690 // if you'd want to place the video in a pop-up/dialog type of window:
691 // agl_shell_desktop_set_app_property(display->agl_shell_desktop, app_id,
692 // AGL_SHELL_DESKTOP_APP_ROLE_POPUP,
693 // WINDOW_WIDTH_POS_X, WINDOW_WIDTH_POS_Y,
694 // 0, 0, WINDOW_WIDTH_SIZE, WINDOW_HEIGHT_SIZE,
695 // display->wl_output);
697 // we use the role to set a correspondence between the top level
698 // surface and our application, with the previous call letting the
699 // compositor know that we're one and the same
700 window = create_window(display, WINDOW_WIDTH_SIZE, WINDOW_HEIGHT_SIZE, app_id);
707 window->display = display;
708 receiver_data.window = window;
710 /* Initialise damage to full surface, so the padding gets painted */
711 wl_surface_damage(window->surface, 0, 0,
712 window->width, window->height);
714 if (!window->wait_for_configure) {
715 redraw(window, NULL, 0);
718 GstBus *bus = gst_element_get_bus(pipeline);
719 gst_bus_add_signal_watch(bus);
721 g_signal_connect(bus, "message::error", G_CALLBACK(error_cb), &receiver_data);
722 gst_bus_set_sync_handler(bus, bus_sync_handler, &receiver_data, NULL);
723 gst_object_unref(bus);
725 gst_element_set_state(pipeline, GST_STATE_PLAYING);
726 fprintf(stdout, "gstreamer pipeline running\n");
728 // run the application
729 while (running && ret != -1)
730 ret = wl_display_dispatch(display->wl_display);
732 destroy_window(window);
733 destroy_display(display);
736 gst_element_set_state(pipeline, GST_STATE_NULL);
737 gst_object_unref(pipeline);