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 // C++ requires a cast and we in wayland we do the cast implictly
35 #define WL_ARRAY_FOR_EACH(pos, array, type) \
36 for (pos = (type)(array)->data; \
37 (const char *) pos < ((const char *) (array)->data + (array)->size); \
41 struct wl_display *wl_display;
42 struct wl_registry *wl_registry;
43 struct wl_compositor *wl_compositor;
44 struct wl_output *wl_output;
52 struct xdg_wm_base *wm_base;
53 struct agl_shell_desktop *agl_shell_desktop;
59 struct wl_buffer *buffer;
65 struct display *display;
70 struct wl_surface *surface;
72 struct xdg_surface *xdg_surface;
73 struct xdg_toplevel *xdg_toplevel;
74 bool wait_for_configure;
76 int fullscreen, maximized;
78 struct buffer buffers[2];
79 struct wl_callback *callback;
83 struct receiver_data {
84 struct window *window;
87 GstWaylandVideo *wl_video;
88 GstVideoOverlay *overlay;
91 static int running = 1;
94 redraw(void *data, struct wl_callback *callback, uint32_t time);
97 paint_pixels(void *image, int padding, int width, int height, uint32_t time)
99 memset(image, 0x00, width * height * 4);
103 buffer_release(void *data, struct wl_buffer *buffer)
105 struct buffer *mybuf = static_cast<struct buffer *>(data);
109 static const struct wl_buffer_listener buffer_listener = {
115 create_shm_buffer(struct display *display, struct buffer *buffer,
116 int width, int height, uint32_t format)
118 struct wl_shm_pool *pool;
119 int fd, size, stride;
123 size = stride * height;
125 fd = os_create_anonymous_file(size);
127 fprintf(stderr, "creating a buffer file for %d B failed: %s\n",
128 size, strerror(errno));
132 data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
133 if (data == MAP_FAILED) {
134 fprintf(stderr, "mmap failed: %s\n", strerror(errno));
139 pool = wl_shm_create_pool(display->shm, fd, size);
140 buffer->buffer = wl_shm_pool_create_buffer(pool, 0, width,
141 height, stride, format);
142 wl_buffer_add_listener(buffer->buffer, &buffer_listener, buffer);
143 wl_shm_pool_destroy(pool);
146 buffer->shm_data = data;
147 fprintf(stdout, "Created shm buffer with width %d, height %d\n", width, height);
151 static struct buffer *
152 get_next_buffer(struct window *window)
154 struct buffer *buffer = NULL;
157 /* we need to create new buffers for the resized value so discard
158 * the 'old' one and force creation of the buffer with the newer
160 if (window->wait_for_configure && window->maximized) {
161 if (!window->buffers[0].busy && window->buffers[0].buffer) {
162 wl_buffer_destroy(window->buffers[0].buffer);
163 window->buffers[0].buffer = NULL;
164 window->wait_for_configure = false;
168 if (!window->buffers[0].busy) {
169 buffer = &window->buffers[0];
170 } else if (!window->buffers[1].busy) {
171 buffer = &window->buffers[1];
176 if (!buffer->buffer) {
177 ret = create_shm_buffer(window->display, buffer, window->width,
178 window->height, WL_SHM_FORMAT_XRGB8888);
183 /* paint the padding */
184 memset(buffer->shm_data, 0x00, window->width * window->height * 4);
191 static const struct wl_callback_listener frame_listener = {
196 redraw(void *data, struct wl_callback *callback, uint32_t time)
198 struct window *window = static_cast<struct window *>(data);
199 struct buffer *buffer;
201 buffer = get_next_buffer(window);
204 !callback ? "Failed to create the first buffer.\n" :
205 "Both buffers busy at redraw(). Server bug?\n");
209 // do the actual painting
210 paint_pixels(buffer->shm_data, 0x0, window->width, window->height, time);
212 wl_surface_attach(window->surface, buffer->buffer, 0, 0);
213 wl_surface_damage(window->surface, 0, 0, window->width, window->height);
216 wl_callback_destroy(callback);
218 window->callback = wl_surface_frame(window->surface);
219 wl_callback_add_listener(window->callback, &frame_listener, window);
220 wl_surface_commit(window->surface);
226 shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
228 struct display *d = static_cast<struct display *>(data);
230 if (format == WL_SHM_FORMAT_XRGB8888)
234 static const struct wl_shm_listener shm_listener = {
239 xdg_wm_base_ping(void *data, struct xdg_wm_base *shell, uint32_t serial)
241 xdg_wm_base_pong(shell, serial);
244 static const struct xdg_wm_base_listener xdg_wm_base_listener = {
249 display_handle_geometry(void *data, struct wl_output *wl_output,
250 int x, int y, int physical_width, int physical_height,
251 int subpixel, const char *make, const char *model, int transform)
257 (void) physical_width;
258 (void) physical_height;
266 display_handle_mode(void *data, struct wl_output *wl_output, uint32_t flags,
267 int width, int height, int refresh)
269 struct display *d = static_cast<struct display *>(data);
271 if (wl_output == d->wl_output && (flags & WL_OUTPUT_MODE_CURRENT)) {
272 d->output_data.width = width;
273 d->output_data.height = height;
275 fprintf(stdout, "Found output with width %d and height %d\n",
276 d->output_data.width, d->output_data.height);
281 display_handle_scale(void *data, struct wl_output *wl_output, int scale)
289 display_handle_done(void *data, struct wl_output *wl_output)
295 static const struct wl_output_listener output_listener = {
296 display_handle_geometry,
303 application_id(void *data, struct agl_shell_desktop *agl_shell_desktop,
307 (void) agl_shell_desktop;
312 application_id_state(void *data, struct agl_shell_desktop *agl_shell_desktop,
313 const char *app_id, const char *app_data,
314 uint32_t app_state, uint32_t app_role)
318 (void) agl_shell_desktop;
324 static const struct agl_shell_desktop_listener agl_shell_desktop_listener = {
326 application_id_state,
330 registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
331 const char *interface, uint32_t version)
333 struct display *d = static_cast<struct display *>(data);
335 if (strcmp(interface, "wl_compositor") == 0) {
337 static_cast<struct wl_compositor *>(wl_registry_bind(registry, id,
338 &wl_compositor_interface, 1));
339 } else if (strcmp(interface, "xdg_wm_base") == 0) {
340 d->wm_base = static_cast<struct xdg_wm_base *>(wl_registry_bind(registry,
341 id, &xdg_wm_base_interface, 1));
342 xdg_wm_base_add_listener(d->wm_base, &xdg_wm_base_listener, d);
343 } else if (strcmp(interface, "wl_shm") == 0) {
344 d->shm = static_cast<struct wl_shm *>(wl_registry_bind(registry,
345 id, &wl_shm_interface, 1));
346 wl_shm_add_listener(d->shm, &shm_listener, d);
347 } else if (strcmp(interface, "agl_shell_desktop") == 0) {
348 d->agl_shell_desktop = static_cast<struct agl_shell_desktop *>(wl_registry_bind(registry, id,
349 &agl_shell_desktop_interface, 1));
350 /* as an example, show how to register for events from the compositor */
351 agl_shell_desktop_add_listener(d->agl_shell_desktop,
352 &agl_shell_desktop_listener, d);
353 } else if (strcmp(interface, "wl_output") == 0) {
354 d->wl_output = static_cast<struct wl_output *>(wl_registry_bind(registry, id,
355 &wl_output_interface, 1));
356 wl_output_add_listener(d->wl_output, &output_listener, d);
361 registry_handle_global_remove(void *data, struct wl_registry *reg, uint32_t id)
368 static const struct wl_registry_listener registry_listener = {
369 registry_handle_global,
370 registry_handle_global_remove,
375 error_cb(GstBus *bus, GstMessage *msg, gpointer user_data)
377 struct receiver_data *d =
378 static_cast<struct receiver_data *>(user_data);
383 gst_message_parse_error(msg, &err, &debug);
385 g_print("Error: %s\n", err->message);
389 g_print("Debug details: %s\n", debug);
393 gst_element_set_state(d->pipeline, GST_STATE_NULL);
396 static GstBusSyncReply
397 bus_sync_handler(GstBus *bus, GstMessage *message, gpointer user_data)
399 struct receiver_data *d =
400 static_cast<struct receiver_data *>(user_data);
402 if (gst_is_wayland_display_handle_need_context_message(message)) {
404 struct wl_display *display_handle = d->window->display->wl_display;
406 context = gst_wayland_display_handle_context_new(display_handle);
407 d->wl_video = GST_WAYLAND_VIDEO(GST_MESSAGE_SRC(message));
408 gst_element_set_context(GST_ELEMENT(GST_MESSAGE_SRC(message)), context);
411 } else if (gst_is_video_overlay_prepare_window_handle_message(message)) {
412 struct wl_surface *window_handle = d->window->surface;
414 /* GST_MESSAGE_SRC(message) will be the overlay object that we
415 * have to use. This may be waylandsink, but it may also be
416 * playbin. In the latter case, we must make sure to use
417 * playbin instead of waylandsink, because playbin resets the
418 * window handle and render_rectangle after restarting playback
419 * and the actual window size is lost */
420 d->overlay = GST_VIDEO_OVERLAY(GST_MESSAGE_SRC(message));
422 g_print("setting window handle and size (%d x %d) w %d, h %d\n",
423 d->window->x, d->window->y,
424 d->window->width, d->window->height);
426 gst_video_overlay_set_window_handle(d->overlay, (guintptr) window_handle);
427 gst_video_overlay_set_render_rectangle(d->overlay,
428 d->window->x, d->window->y,
429 d->window->width, d->window->height);
437 gst_message_unref(message);
442 handle_xdg_surface_configure(void *data, struct xdg_surface *surface, uint32_t serial)
444 struct window *window = static_cast<struct window *>(data);
446 xdg_surface_ack_configure(surface, serial);
448 if (window->wait_for_configure) {
449 redraw(window, NULL, 0);
453 static const struct xdg_surface_listener xdg_surface_listener = {
454 handle_xdg_surface_configure,
458 handle_xdg_toplevel_configure(void *data, struct xdg_toplevel *xdg_toplevel,
459 int32_t width, int32_t height,
460 struct wl_array *states)
462 struct window *window = static_cast<struct window *>(data);
465 window->fullscreen = 0;
466 window->maximized = 0;
468 // use our own macro as C++ can't typecast from (void *) directly
469 WL_ARRAY_FOR_EACH(p, states, uint32_t *) {
472 case XDG_TOPLEVEL_STATE_FULLSCREEN:
473 window->fullscreen = 1;
475 case XDG_TOPLEVEL_STATE_MAXIMIZED:
476 window->maximized = 1;
481 if (width > 0 && height > 0) {
482 if (!window->fullscreen && !window->maximized) {
483 window->width = width;
484 window->height = height;
486 window->width = width;
487 window->height = height;
488 } else if (!window->fullscreen && !window->maximized) {
490 window->width = WINDOW_WIDTH_SIZE;
492 window->width = width;
495 window->height = WINDOW_HEIGHT_SIZE;
497 window->height = height;
500 /* if we've been resized set wait_for_configure to adjust the fb size
501 * in the frame callback handler, which will also clear this up */
502 if ((window->width > 0 && window->width != WINDOW_WIDTH_SIZE) &&
503 (window->height > 0 && window->height != WINDOW_HEIGHT_SIZE)) {
504 window->wait_for_configure = true;
509 handle_xdg_toplevel_close(void *data, struct xdg_toplevel *xdg_toplevel)
514 static const struct xdg_toplevel_listener xdg_toplevel_listener = {
515 handle_xdg_toplevel_configure,
516 handle_xdg_toplevel_close,
519 static struct window *
520 create_window(struct display *display, int width, int height, const char *app_id)
522 struct window *window;
524 assert(display->wm_base != NULL);
526 window = static_cast<struct window *>(calloc(1, sizeof(*window)));
530 window->callback = NULL;
531 window->display = display;
532 window->width = width;
533 window->height = height;
534 window->surface = wl_compositor_create_surface(display->wl_compositor);
536 if (display->wm_base) {
537 window->xdg_surface =
538 xdg_wm_base_get_xdg_surface(display->wm_base, window->surface);
539 assert(window->xdg_surface);
541 xdg_surface_add_listener(window->xdg_surface,
542 &xdg_surface_listener, window);
543 window->xdg_toplevel = xdg_surface_get_toplevel(window->xdg_surface);
544 assert(window->xdg_toplevel);
546 xdg_toplevel_add_listener(window->xdg_toplevel,
547 &xdg_toplevel_listener, window);
549 xdg_toplevel_set_app_id(window->xdg_toplevel, app_id);
551 wl_surface_commit(window->surface);
552 window->wait_for_configure = true;
560 destroy_window(struct window *window)
562 if (window->callback)
563 wl_callback_destroy(window->callback);
565 if (window->buffers[0].buffer)
566 wl_buffer_destroy(window->buffers[0].buffer);
567 if (window->buffers[1].buffer)
568 wl_buffer_destroy(window->buffers[1].buffer);
570 if (window->xdg_toplevel)
571 xdg_toplevel_destroy(window->xdg_toplevel);
573 if (window->xdg_surface)
574 xdg_surface_destroy(window->xdg_surface);
576 wl_surface_destroy(window->surface);
581 signal_int(int sig, siginfo_t *si, void *_unused)
586 static struct display *
587 create_display(int argc, char *argv[])
589 struct display *display;
591 display = static_cast<struct display *>(calloc(1, sizeof(*display)));
592 if (display == NULL) {
593 fprintf(stderr, "out of memory\n");
596 display->wl_display = wl_display_connect(NULL);
597 assert(display->wl_display);
599 display->has_xrgb = false;
600 display->wl_registry = wl_display_get_registry(display->wl_display);
602 wl_registry_add_listener(display->wl_registry, ®istry_listener, display);
603 wl_display_roundtrip(display->wl_display);
605 if (display->shm == NULL) {
606 fprintf(stderr, "No wl_shm global\n");
610 if (display->agl_shell_desktop == NULL) {
611 fprintf(stderr, "No agl_shell extension present\n");
615 wl_display_roundtrip(display->wl_display);
617 if (!display->has_xrgb) {
618 fprintf(stderr, "WL_SHM_FORMAT_XRGB32 not available\n");
626 destroy_display(struct display *display)
629 wl_shm_destroy(display->shm);
631 if (display->wm_base)
632 xdg_wm_base_destroy(display->wm_base);
634 if (display->agl_shell_desktop)
635 agl_shell_desktop_destroy(display->agl_shell_desktop);
637 if (display->wl_compositor)
638 wl_compositor_destroy(display->wl_compositor);
640 wl_registry_destroy(display->wl_registry);
641 wl_display_flush(display->wl_display);
642 wl_display_disconnect(display->wl_display);
646 int main(int argc, char *argv[])
650 struct receiver_data receiver_data = {};
651 struct display *display;
652 struct window *window;
653 char pipeline_str[1024];
654 GError *error = NULL;
655 const char *app_id = "camera-gstreamer";
657 sa.sa_sigaction = signal_int;
658 sigemptyset(&sa.sa_mask);
659 sa.sa_flags = SA_RESETHAND | SA_SIGINFO;
660 sigaction(SIGINT, &sa, NULL);
663 char **gargv = static_cast<char **>(calloc(2, sizeof(char *)));
665 gargv[0] = strdup(argv[0]);
666 gargv[1] = strdup("--gst-debug-level=2");
668 memset(pipeline_str, 0, sizeof(pipeline_str));
669 snprintf(pipeline_str, sizeof(pipeline_str), "v4l2src device=%s ! video/x-raw,width=%d,height=%d ! waylandsink",
670 get_camera_device(), WINDOW_WIDTH_SIZE, WINDOW_HEIGHT_SIZE);
671 gst_init(&gargc, &gargv);
673 setbuf(stdout, NULL);
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);