1 #define GST_USE_UNSTABLE_API
2 #define HAVE_MEMFD_CREATE
11 #include <wayland-client.h>
18 #include "xdg-shell-client-protocol.h"
19 #include "AglShellGrpcClient.h"
23 #include <gst/video/videooverlay.h>
24 #include <gst/wayland/wayland.h>
26 #if !GST_CHECK_VERSION(1, 22, 0)
27 #define gst_is_wl_display_handle_need_context_message gst_is_wayland_display_handle_need_context_message
28 #define gst_wl_display_handle_context_new gst_wayland_display_handle_context_new
32 #define APP_DATA_PATH /usr/share/applications/data
35 // these only applies if the window is a dialog/pop-up one
36 // by default the compositor make the window maximized
37 #define WINDOW_WIDTH_SIZE 640
38 #define WINDOW_HEIGHT_SIZE 720
40 #define WINDOW_WIDTH_POS_X 640
41 #define WINDOW_WIDTH_POS_Y 180
43 #define MAX_BUFFER_ALLOC 2
45 // C++ requires a cast and we in wayland we do the cast implictly
46 #define WL_ARRAY_FOR_EACH(pos, array, type) \
47 for (pos = (type)(array)->data; \
48 (const char *) pos < ((const char *) (array)->data + (array)->size); \
52 struct wl_display *wl_display;
53 struct wl_registry *wl_registry;
54 struct wl_compositor *wl_compositor;
55 struct wl_output *wl_output;
63 struct xdg_wm_base *wm_base;
68 struct wl_buffer *buffer;
72 size_t size; /* width * 4 * height */
73 struct wl_list buffer_link; /** window::buffer_list */
77 struct display *display;
84 struct wl_surface *surface;
86 struct xdg_surface *xdg_surface;
87 struct xdg_toplevel *xdg_toplevel;
88 bool wait_for_configure;
90 int fullscreen, maximized;
92 struct wl_list buffer_list;
93 struct wl_callback *callback;
94 bool needs_update_buffer;
98 struct receiver_data {
99 struct window *window;
101 GstElement *pipeline;
102 GstVideoOverlay *overlay;
105 static int running = 1;
106 static bool gst_pipeline_failed = FALSE;
107 static bool fallback_gst_pipeline_tried = FALSE;
110 redraw(void *data, struct wl_callback *callback, uint32_t time);
112 static struct buffer *
113 alloc_buffer(struct window *window, int width, int height)
115 struct buffer *buffer = static_cast<struct buffer *>(calloc(1, sizeof(*buffer)));
117 buffer->width = width;
118 buffer->height = height;
119 wl_list_insert(&window->buffer_list, &buffer->buffer_link);
125 destroy_buffer(struct buffer *buffer)
128 wl_buffer_destroy(buffer->buffer);
130 munmap(buffer->shm_data, buffer->size);
131 wl_list_remove(&buffer->buffer_link);
135 static struct buffer *
136 pick_free_buffer(struct window *window)
139 struct buffer *buffer = NULL;
141 wl_list_for_each(b, &window->buffer_list, buffer_link) {
152 prune_old_released_buffers(struct window *window)
154 struct buffer *b, *b_next;
156 wl_list_for_each_safe(b, b_next,
157 &window->buffer_list, buffer_link) {
158 if (!b->busy && (b->width != window->width ||
159 b->height != window->height))
165 paint_pixels(void *image, int padding, int width, int height, uint32_t time)
167 memset(image, 0x00, width * height * 4);
171 buffer_release(void *data, struct wl_buffer *buffer)
173 struct buffer *mybuf = static_cast<struct buffer *>(data);
177 static const struct wl_buffer_listener buffer_listener = {
183 create_shm_buffer(struct display *display, struct buffer *buffer,
184 int width, int height, uint32_t format)
186 struct wl_shm_pool *pool;
187 int fd, size, stride;
191 size = stride * height;
193 fd = os_create_anonymous_file(size);
195 fprintf(stderr, "creating a buffer file for %d B failed: %s\n",
196 size, strerror(errno));
200 data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
201 if (data == MAP_FAILED) {
202 fprintf(stderr, "mmap failed: %s\n", strerror(errno));
207 pool = wl_shm_create_pool(display->shm, fd, size);
208 buffer->buffer = wl_shm_pool_create_buffer(pool, 0, width,
209 height, stride, format);
210 wl_buffer_add_listener(buffer->buffer, &buffer_listener, buffer);
211 wl_shm_pool_destroy(pool);
214 buffer->shm_data = data;
216 buffer->width = width;
217 buffer->height = height;
219 fprintf(stdout, "Created shm buffer with width %d, height %d\n", width, height);
224 static struct buffer *
225 get_next_buffer(struct window *window)
227 struct buffer *buffer = NULL;
230 if (window->needs_update_buffer) {
233 for (i = 0; i < MAX_BUFFER_ALLOC; i++)
234 alloc_buffer(window, window->width, window->height);
236 window->needs_update_buffer = false;
239 buffer = pick_free_buffer(window);
244 if (!buffer->buffer) {
245 ret = create_shm_buffer(window->display, buffer, window->width,
246 window->height, WL_SHM_FORMAT_XRGB8888);
251 /* paint the padding */
252 memset(buffer->shm_data, 0x00, window->width * window->height * 4);
259 static const struct wl_callback_listener frame_listener = {
264 redraw(void *data, struct wl_callback *callback, uint32_t time)
266 struct window *window = static_cast<struct window *>(data);
267 struct buffer *buffer;
269 prune_old_released_buffers(window);
271 buffer = get_next_buffer(window);
274 !callback ? "Failed to create the first buffer.\n" :
275 "Both buffers busy at redraw(). Server bug?\n");
279 // do the actual painting
280 paint_pixels(buffer->shm_data, 0x0, window->width, window->height, time);
282 wl_surface_attach(window->surface, buffer->buffer, 0, 0);
283 wl_surface_damage(window->surface, 0, 0, window->width, window->height);
286 wl_callback_destroy(callback);
288 window->callback = wl_surface_frame(window->surface);
289 wl_callback_add_listener(window->callback, &frame_listener, window);
290 wl_surface_commit(window->surface);
296 shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
298 struct display *d = static_cast<struct display *>(data);
300 if (format == WL_SHM_FORMAT_XRGB8888)
304 static const struct wl_shm_listener shm_listener = {
309 xdg_wm_base_ping(void *data, struct xdg_wm_base *shell, uint32_t serial)
311 xdg_wm_base_pong(shell, serial);
314 static const struct xdg_wm_base_listener xdg_wm_base_listener = {
319 display_handle_geometry(void *data, struct wl_output *wl_output,
320 int x, int y, int physical_width, int physical_height,
321 int subpixel, const char *make, const char *model, int transform)
327 (void) physical_width;
328 (void) physical_height;
336 display_handle_mode(void *data, struct wl_output *wl_output, uint32_t flags,
337 int width, int height, int refresh)
339 struct display *d = static_cast<struct display *>(data);
341 if (wl_output == d->wl_output && (flags & WL_OUTPUT_MODE_CURRENT)) {
342 d->output_data.width = width;
343 d->output_data.height = height;
345 fprintf(stdout, "Found output with width %d and height %d\n",
346 d->output_data.width, d->output_data.height);
351 display_handle_scale(void *data, struct wl_output *wl_output, int scale)
359 display_handle_done(void *data, struct wl_output *wl_output)
365 static const struct wl_output_listener output_listener = {
366 display_handle_geometry,
374 registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
375 const char *interface, uint32_t version)
377 struct display *d = static_cast<struct display *>(data);
379 if (strcmp(interface, "wl_compositor") == 0) {
381 static_cast<struct wl_compositor *>(wl_registry_bind(registry, id,
382 &wl_compositor_interface, 1));
383 } else if (strcmp(interface, "xdg_wm_base") == 0) {
384 d->wm_base = static_cast<struct xdg_wm_base *>(wl_registry_bind(registry,
385 id, &xdg_wm_base_interface, 1));
386 xdg_wm_base_add_listener(d->wm_base, &xdg_wm_base_listener, d);
387 } else if (strcmp(interface, "wl_shm") == 0) {
388 d->shm = static_cast<struct wl_shm *>(wl_registry_bind(registry,
389 id, &wl_shm_interface, 1));
390 wl_shm_add_listener(d->shm, &shm_listener, d);
391 } else if (strcmp(interface, "wl_output") == 0) {
392 d->wl_output = static_cast<struct wl_output *>(wl_registry_bind(registry, id,
393 &wl_output_interface, 1));
394 wl_output_add_listener(d->wl_output, &output_listener, d);
399 registry_handle_global_remove(void *data, struct wl_registry *reg, uint32_t id)
406 static const struct wl_registry_listener registry_listener = {
407 registry_handle_global,
408 registry_handle_global_remove,
413 error_cb(GstBus *bus, GstMessage *msg, gpointer user_data)
415 struct receiver_data *d =
416 static_cast<struct receiver_data *>(user_data);
421 gst_message_parse_error(msg, &err, &debug);
423 g_print("Error: %s\n", err->message);
427 g_print("Debug details: %s\n", debug);
431 gst_element_set_state(d->pipeline, GST_STATE_NULL);
434 static GstBusSyncReply
435 bus_sync_handler(GstBus *bus, GstMessage *message, gpointer user_data)
437 struct receiver_data *d =
438 static_cast<struct receiver_data *>(user_data);
440 if (gst_is_wl_display_handle_need_context_message(message)) {
442 struct wl_display *display_handle = d->window->display->wl_display;
444 context = gst_wl_display_handle_context_new(display_handle);
445 gst_element_set_context(GST_ELEMENT(GST_MESSAGE_SRC(message)), context);
448 } else if (gst_is_video_overlay_prepare_window_handle_message(message)) {
449 struct wl_surface *window_handle = d->window->surface;
451 /* GST_MESSAGE_SRC(message) will be the overlay object that we
452 * have to use. This may be waylandsink, but it may also be
453 * playbin. In the latter case, we must make sure to use
454 * playbin instead of waylandsink, because playbin resets the
455 * window handle and render_rectangle after restarting playback
456 * and the actual window size is lost */
457 d->overlay = GST_VIDEO_OVERLAY(GST_MESSAGE_SRC(message));
459 g_print("setting window handle and size (%d x %d) w %d, h %d\n",
460 d->window->x, d->window->y,
461 d->window->width, d->window->height);
463 gst_video_overlay_set_window_handle(d->overlay, (guintptr) window_handle);
464 gst_video_overlay_set_render_rectangle(d->overlay,
465 d->window->x, d->window->y,
466 d->window->width, d->window->height);
470 else if (GST_MESSAGE_TYPE(message) == GST_MESSAGE_ERROR) {
472 gchar* dbg_info = NULL;
474 gst_message_parse_error(message, &err, &dbg_info);
475 g_printerr("ERROR from element %s: %s code %d\n",
476 GST_OBJECT_NAME(message->src), err->message, err->code);
477 g_printerr("Debugging info: %s\n", (dbg_info) ? dbg_info : "none");
478 gst_pipeline_failed = TRUE;
487 gst_message_unref(message);
492 handle_xdg_surface_configure(void *data, struct xdg_surface *surface, uint32_t serial)
494 struct window *window = static_cast<struct window *>(data);
496 xdg_surface_ack_configure(surface, serial);
498 if (window->wait_for_configure) {
499 redraw(window, NULL, 0);
500 window->wait_for_configure = false;
504 static const struct xdg_surface_listener xdg_surface_listener = {
505 handle_xdg_surface_configure,
509 handle_xdg_toplevel_configure(void *data, struct xdg_toplevel *xdg_toplevel,
510 int32_t width, int32_t height,
511 struct wl_array *states)
513 struct window *window = static_cast<struct window *>(data);
516 window->fullscreen = 0;
517 window->maximized = 0;
519 // use our own macro as C++ can't typecast from (void *) directly
520 WL_ARRAY_FOR_EACH(p, states, uint32_t *) {
523 case XDG_TOPLEVEL_STATE_FULLSCREEN:
524 window->fullscreen = 1;
526 case XDG_TOPLEVEL_STATE_MAXIMIZED:
527 window->maximized = 1;
532 if (width > 0 && height > 0) {
533 if (!window->fullscreen && !window->maximized) {
534 window->init_width = width;
535 window->init_height = height;
537 window->width = width;
538 window->height = height;
539 } else if (!window->fullscreen && !window->maximized) {
540 window->width = window->init_width;
541 window->height = window->init_height;
544 window->needs_update_buffer = true;
549 handle_xdg_toplevel_close(void *data, struct xdg_toplevel *xdg_toplevel)
554 static const struct xdg_toplevel_listener xdg_toplevel_listener = {
555 handle_xdg_toplevel_configure,
556 handle_xdg_toplevel_close,
559 static struct window *
560 create_window(struct display *display, int width, int height, const char *app_id)
562 struct window *window;
565 assert(display->wm_base != NULL);
567 window = static_cast<struct window *>(calloc(1, sizeof(*window)));
571 wl_list_init(&window->buffer_list);
572 window->callback = NULL;
573 window->display = display;
574 window->width = width;
575 window->height = height;
576 window->init_width = width;
577 window->init_height = height;
578 window->surface = wl_compositor_create_surface(display->wl_compositor);
580 if (display->wm_base) {
581 window->xdg_surface =
582 xdg_wm_base_get_xdg_surface(display->wm_base, window->surface);
583 assert(window->xdg_surface);
585 xdg_surface_add_listener(window->xdg_surface,
586 &xdg_surface_listener, window);
587 window->xdg_toplevel = xdg_surface_get_toplevel(window->xdg_surface);
588 assert(window->xdg_toplevel);
590 xdg_toplevel_add_listener(window->xdg_toplevel,
591 &xdg_toplevel_listener, window);
593 xdg_toplevel_set_app_id(window->xdg_toplevel, app_id);
595 wl_surface_commit(window->surface);
596 window->wait_for_configure = true;
599 for (i = 0; i < MAX_BUFFER_ALLOC; i++)
600 alloc_buffer(window, window->width, window->height);
607 destroy_window(struct window *window)
609 struct buffer *buffer, *buffer_next;
611 if (window->callback)
612 wl_callback_destroy(window->callback);
614 wl_list_for_each_safe(buffer, buffer_next,
615 &window->buffer_list, buffer_link)
616 destroy_buffer(buffer);
618 if (window->xdg_toplevel)
619 xdg_toplevel_destroy(window->xdg_toplevel);
621 if (window->xdg_surface)
622 xdg_surface_destroy(window->xdg_surface);
624 wl_surface_destroy(window->surface);
629 signal_int(int sig, siginfo_t *si, void *_unused)
634 static struct display *
635 create_display(int argc, char *argv[])
637 struct display *display;
639 display = static_cast<struct display *>(calloc(1, sizeof(*display)));
640 if (display == NULL) {
641 fprintf(stderr, "out of memory\n");
644 display->wl_display = wl_display_connect(NULL);
645 assert(display->wl_display);
647 display->has_xrgb = false;
648 display->wl_registry = wl_display_get_registry(display->wl_display);
650 wl_registry_add_listener(display->wl_registry, ®istry_listener, display);
651 wl_display_roundtrip(display->wl_display);
653 if (display->shm == NULL) {
654 fprintf(stderr, "No wl_shm global\n");
658 wl_display_roundtrip(display->wl_display);
660 if (!display->has_xrgb) {
661 fprintf(stderr, "WL_SHM_FORMAT_XRGB32 not available\n");
669 destroy_display(struct display *display)
672 wl_shm_destroy(display->shm);
674 if (display->wm_base)
675 xdg_wm_base_destroy(display->wm_base);
677 if (display->wl_compositor)
678 wl_compositor_destroy(display->wl_compositor);
680 wl_registry_destroy(display->wl_registry);
681 wl_display_flush(display->wl_display);
682 wl_display_disconnect(display->wl_display);
686 // stringify the un-quoted string to quoted C string
687 #define xstr(a) str(a)
690 GstElement* create_pipeline(int* argc, char** argv[])
692 GError *error = NULL;
693 const char *camera_device = NULL;
694 const char *width_str = NULL;
695 const char *height_str = NULL;
699 // pipewire is default.
700 char *v4l2_path = getenv("ENABLE_V4L2_PATH");
703 char pipeline_str[1024];
705 camera_device = getenv("DEFAULT_V4L2_DEVICE");
707 camera_device = get_first_camera_device();
709 width_str = getenv("DEFAULT_DEVICE_WIDTH");
711 width = WINDOW_WIDTH_SIZE;
713 width = atoi(width_str);
715 height_str = getenv("DEFAULT_DEVICE_HEIGHT");
717 height = WINDOW_HEIGHT_SIZE;
719 height = atoi(height_str);
721 if (v4l2_path == NULL)
723 else if (g_str_equal(v4l2_path, "yes") || g_str_equal(v4l2_path, "true"))
726 memset(pipeline_str, 0, sizeof(pipeline_str));
729 snprintf(pipeline_str, sizeof(pipeline_str), "v4l2src device=%s ! video/x-raw,width=%d,height=%d ! waylandsink",
730 camera_device, width, height);
731 else if (gst_pipeline_failed == TRUE) {
732 snprintf(pipeline_str, sizeof(pipeline_str), "filesrc location=%s/still-image.jpg ! decodebin ! videoconvert ! imagefreeze ! waylandsink fullscreen=true",
733 xstr(APP_DATA_PATH));
734 fallback_gst_pipeline_tried = TRUE;
737 snprintf(pipeline_str, sizeof(pipeline_str), "pipewiresrc ! waylandsink");
740 fprintf(stdout, "Using pipeline: %s\n", pipeline_str);
742 GstElement *pipeline = gst_parse_launch(pipeline_str, &error);
744 if (error || !pipeline) {
745 fprintf(stderr, "gstreamer pipeline construction failed!\n");
753 int main(int argc, char* argv[])
757 struct receiver_data receiver_data = {};
758 struct display* display;
759 struct window* window;
760 const char* app_id = "camera-gstreamer";
762 // for starting the application from the beginning, with a diffrent
763 // role we need to handle that creating the main window
764 if (argc >= 2 && strcmp(argv[1], "float") == 0) {
765 GrpcClient *client = new GrpcClient();
766 client->SetAppFloat(std::string(app_id), 30, 400);
769 sa.sa_sigaction = signal_int;
770 sigemptyset(&sa.sa_mask);
771 sa.sa_flags = SA_RESETHAND | SA_SIGINFO;
772 sigaction(SIGINT, &sa, NULL);
775 char** gargv = static_cast<char**>(calloc(2, sizeof(char*)));
777 gargv[0] = strdup(argv[0]);
778 gargv[1] = strdup("--gst-debug-level=2");
780 setbuf(stdout, NULL);
782 gst_init(&gargc, &gargv);
784 receiver_data.pipeline = create_pipeline(&gargc, &gargv);
786 if (!receiver_data.pipeline)
789 display = create_display(argc, argv);
793 // we use the role to set a correspondence between the top level
794 // surface and our application, with the previous call letting the
795 // compositor know that we're one and the same
796 window = create_window(display, WINDOW_WIDTH_SIZE, WINDOW_HEIGHT_SIZE, app_id);
803 window->display = display;
804 receiver_data.window = window;
806 /* Initialise damage to full surface, so the padding gets painted */
807 wl_surface_damage(window->surface, 0, 0,
808 window->width, window->height);
810 if (!window->wait_for_configure) {
811 redraw(window, NULL, 0);
814 GstBus *bus = gst_element_get_bus(receiver_data.pipeline);
815 gst_bus_add_signal_watch(bus);
817 g_signal_connect(bus, "message::error", G_CALLBACK(error_cb), &receiver_data);
818 gst_bus_set_sync_handler(bus, bus_sync_handler, &receiver_data, NULL);
819 gst_object_unref(bus);
821 gst_element_set_state(receiver_data.pipeline, GST_STATE_PLAYING);
822 fprintf(stdout, "gstreamer pipeline running\n");
824 // run the application
825 while (running && ret != -1) {
826 ret = wl_display_dispatch(display->wl_display);
827 if (gst_pipeline_failed && fallback_gst_pipeline_tried == FALSE) {
828 gst_element_set_state(receiver_data.pipeline, GST_STATE_NULL);
829 gst_object_unref(receiver_data.pipeline);
830 /* retry with fallback pipeline */
831 receiver_data.pipeline = create_pipeline(&gargc, &gargv);
832 GstBus *bus = gst_element_get_bus(receiver_data.pipeline);
833 gst_bus_add_signal_watch(bus);
834 g_signal_connect(bus, "message::error", G_CALLBACK(error_cb), &receiver_data);
835 gst_bus_set_sync_handler(bus, bus_sync_handler, &receiver_data, NULL);
836 gst_object_unref(bus);
837 gst_element_set_state(receiver_data.pipeline, GST_STATE_PLAYING);
840 gst_element_set_state(receiver_data.pipeline, GST_STATE_NULL);
841 gst_object_unref(receiver_data.pipeline);
843 destroy_window(window);
844 destroy_display(display);