1 #define GST_USE_UNSTABLE_API
2 #define HAVE_MEMFD_CREATE
11 #include <wayland-client.h>
19 #include "xdg-shell-client-protocol.h"
20 #include "AglShellGrpcClient.h"
24 #include <gst/video/videooverlay.h>
25 #include <gst/wayland/wayland.h>
27 #if !GST_CHECK_VERSION(1, 22, 0)
28 #define gst_is_wl_display_handle_need_context_message gst_is_wayland_display_handle_need_context_message
29 #define gst_wl_display_handle_context_new gst_wayland_display_handle_context_new
33 #define APP_DATA_PATH /usr/share/applications/data
36 // these only applies if the window is a dialog/pop-up one
37 // by default the compositor make the window maximized
38 #define WINDOW_WIDTH_SIZE 640
39 #define WINDOW_HEIGHT_SIZE 720
41 #define WINDOW_WIDTH_POS_X 640
42 #define WINDOW_WIDTH_POS_Y 180
44 #define MAX_BUFFER_ALLOC 2
46 const char* my_app_id = "camera-gstreamer";
48 // C++ requires a cast and we in wayland we do the cast implictly
49 #define WL_ARRAY_FOR_EACH(pos, array, type) \
50 for (pos = (type)(array)->data; \
51 (const char *) pos < ((const char *) (array)->data + (array)->size); \
55 struct wl_display *wl_display;
56 struct wl_registry *wl_registry;
57 struct wl_compositor *wl_compositor;
58 struct wl_output *wl_output;
66 struct xdg_wm_base *wm_base;
71 struct wl_buffer *buffer;
75 size_t size; /* width * 4 * height */
76 struct wl_list buffer_link; /** window::buffer_list */
80 struct display *display;
87 struct wl_surface *surface;
89 struct xdg_surface *xdg_surface;
90 struct xdg_toplevel *xdg_toplevel;
91 bool wait_for_configure;
93 int fullscreen, maximized;
95 struct wl_list buffer_list;
96 struct wl_callback *callback;
97 bool needs_update_buffer;
101 struct receiver_data {
102 struct window *window;
104 GstElement *pipeline;
105 GstVideoOverlay *overlay;
108 static int running = 1;
109 static bool gst_pipeline_failed = FALSE;
110 static bool fallback_gst_pipeline_tried = FALSE;
113 redraw(void *data, struct wl_callback *callback, uint32_t time);
115 static struct buffer *
116 alloc_buffer(struct window *window, int width, int height)
118 struct buffer *buffer = static_cast<struct buffer *>(calloc(1, sizeof(*buffer)));
120 buffer->width = width;
121 buffer->height = height;
122 wl_list_insert(&window->buffer_list, &buffer->buffer_link);
128 destroy_buffer(struct buffer *buffer)
131 wl_buffer_destroy(buffer->buffer);
133 munmap(buffer->shm_data, buffer->size);
134 wl_list_remove(&buffer->buffer_link);
138 static struct buffer *
139 pick_free_buffer(struct window *window)
142 struct buffer *buffer = NULL;
144 wl_list_for_each(b, &window->buffer_list, buffer_link) {
155 prune_old_released_buffers(struct window *window)
157 struct buffer *b, *b_next;
159 wl_list_for_each_safe(b, b_next,
160 &window->buffer_list, buffer_link) {
161 if (!b->busy && (b->width != window->width ||
162 b->height != window->height))
168 paint_pixels(void *image, int padding, int width, int height, uint32_t time)
170 memset(image, 0x00, width * height * 4);
174 buffer_release(void *data, struct wl_buffer *buffer)
176 struct buffer *mybuf = static_cast<struct buffer *>(data);
180 static const struct wl_buffer_listener buffer_listener = {
186 create_shm_buffer(struct display *display, struct buffer *buffer,
187 int width, int height, uint32_t format)
189 struct wl_shm_pool *pool;
190 int fd, size, stride;
194 size = stride * height;
196 fd = os_create_anonymous_file(size);
198 fprintf(stderr, "creating a buffer file for %d B failed: %s\n",
199 size, strerror(errno));
203 data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
204 if (data == MAP_FAILED) {
205 fprintf(stderr, "mmap failed: %s\n", strerror(errno));
210 pool = wl_shm_create_pool(display->shm, fd, size);
211 buffer->buffer = wl_shm_pool_create_buffer(pool, 0, width,
212 height, stride, format);
213 wl_buffer_add_listener(buffer->buffer, &buffer_listener, buffer);
214 wl_shm_pool_destroy(pool);
217 buffer->shm_data = data;
219 buffer->width = width;
220 buffer->height = height;
222 fprintf(stdout, "Created shm buffer with width %d, height %d\n", width, height);
227 static struct buffer *
228 get_next_buffer(struct window *window)
230 struct buffer *buffer = NULL;
233 if (window->needs_update_buffer) {
236 for (i = 0; i < MAX_BUFFER_ALLOC; i++)
237 alloc_buffer(window, window->width, window->height);
239 window->needs_update_buffer = false;
242 buffer = pick_free_buffer(window);
247 if (!buffer->buffer) {
248 ret = create_shm_buffer(window->display, buffer, window->width,
249 window->height, WL_SHM_FORMAT_XRGB8888);
254 /* paint the padding */
255 memset(buffer->shm_data, 0x00, window->width * window->height * 4);
262 static const struct wl_callback_listener frame_listener = {
267 redraw(void *data, struct wl_callback *callback, uint32_t time)
269 struct window *window = static_cast<struct window *>(data);
270 struct buffer *buffer;
272 prune_old_released_buffers(window);
274 buffer = get_next_buffer(window);
277 !callback ? "Failed to create the first buffer.\n" :
278 "Both buffers busy at redraw(). Server bug?\n");
282 // do the actual painting
283 paint_pixels(buffer->shm_data, 0x0, window->width, window->height, time);
285 wl_surface_attach(window->surface, buffer->buffer, 0, 0);
286 wl_surface_damage(window->surface, 0, 0, window->width, window->height);
289 wl_callback_destroy(callback);
291 window->callback = wl_surface_frame(window->surface);
292 wl_callback_add_listener(window->callback, &frame_listener, window);
293 wl_surface_commit(window->surface);
299 shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
301 struct display *d = static_cast<struct display *>(data);
303 if (format == WL_SHM_FORMAT_XRGB8888)
307 static const struct wl_shm_listener shm_listener = {
312 xdg_wm_base_ping(void *data, struct xdg_wm_base *shell, uint32_t serial)
314 xdg_wm_base_pong(shell, serial);
317 static const struct xdg_wm_base_listener xdg_wm_base_listener = {
322 display_handle_geometry(void *data, struct wl_output *wl_output,
323 int x, int y, int physical_width, int physical_height,
324 int subpixel, const char *make, const char *model, int transform)
330 (void) physical_width;
331 (void) physical_height;
339 display_handle_mode(void *data, struct wl_output *wl_output, uint32_t flags,
340 int width, int height, int refresh)
342 struct display *d = static_cast<struct display *>(data);
344 if (wl_output == d->wl_output && (flags & WL_OUTPUT_MODE_CURRENT)) {
345 d->output_data.width = width;
346 d->output_data.height = height;
348 fprintf(stdout, "Found output with width %d and height %d\n",
349 d->output_data.width, d->output_data.height);
354 display_handle_scale(void *data, struct wl_output *wl_output, int scale)
362 display_handle_done(void *data, struct wl_output *wl_output)
368 static const struct wl_output_listener output_listener = {
369 display_handle_geometry,
377 registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
378 const char *interface, uint32_t version)
380 struct display *d = static_cast<struct display *>(data);
382 if (strcmp(interface, "wl_compositor") == 0) {
384 static_cast<struct wl_compositor *>(wl_registry_bind(registry, id,
385 &wl_compositor_interface, 1));
386 } else if (strcmp(interface, "xdg_wm_base") == 0) {
387 d->wm_base = static_cast<struct xdg_wm_base *>(wl_registry_bind(registry,
388 id, &xdg_wm_base_interface, 1));
389 xdg_wm_base_add_listener(d->wm_base, &xdg_wm_base_listener, d);
390 } else if (strcmp(interface, "wl_shm") == 0) {
391 d->shm = static_cast<struct wl_shm *>(wl_registry_bind(registry,
392 id, &wl_shm_interface, 1));
393 wl_shm_add_listener(d->shm, &shm_listener, d);
394 } else if (strcmp(interface, "wl_output") == 0) {
395 d->wl_output = static_cast<struct wl_output *>(wl_registry_bind(registry, id,
396 &wl_output_interface, 1));
397 wl_output_add_listener(d->wl_output, &output_listener, d);
402 registry_handle_global_remove(void *data, struct wl_registry *reg, uint32_t id)
409 static const struct wl_registry_listener registry_listener = {
410 registry_handle_global,
411 registry_handle_global_remove,
416 error_cb(GstBus *bus, GstMessage *msg, gpointer user_data)
418 struct receiver_data *d =
419 static_cast<struct receiver_data *>(user_data);
424 gst_message_parse_error(msg, &err, &debug);
426 g_print("Error: %s\n", err->message);
430 g_print("Debug details: %s\n", debug);
434 gst_element_set_state(d->pipeline, GST_STATE_NULL);
437 static GstBusSyncReply
438 bus_sync_handler(GstBus *bus, GstMessage *message, gpointer user_data)
440 struct receiver_data *d =
441 static_cast<struct receiver_data *>(user_data);
443 if (gst_is_wl_display_handle_need_context_message(message)) {
445 struct wl_display *display_handle = d->window->display->wl_display;
447 context = gst_wl_display_handle_context_new(display_handle);
448 gst_element_set_context(GST_ELEMENT(GST_MESSAGE_SRC(message)), context);
451 } else if (gst_is_video_overlay_prepare_window_handle_message(message)) {
452 struct wl_surface *window_handle = d->window->surface;
454 /* GST_MESSAGE_SRC(message) will be the overlay object that we
455 * have to use. This may be waylandsink, but it may also be
456 * playbin. In the latter case, we must make sure to use
457 * playbin instead of waylandsink, because playbin resets the
458 * window handle and render_rectangle after restarting playback
459 * and the actual window size is lost */
460 d->overlay = GST_VIDEO_OVERLAY(GST_MESSAGE_SRC(message));
462 g_print("setting window handle and size (%d x %d) w %d, h %d\n",
463 d->window->x, d->window->y,
464 d->window->width, d->window->height);
466 gst_video_overlay_set_window_handle(d->overlay, (guintptr) window_handle);
467 gst_video_overlay_set_render_rectangle(d->overlay,
468 d->window->x, d->window->y,
469 d->window->width, d->window->height);
473 else if (GST_MESSAGE_TYPE(message) == GST_MESSAGE_ERROR) {
475 gchar* dbg_info = NULL;
477 gst_message_parse_error(message, &err, &dbg_info);
478 g_printerr("ERROR from element %s: %s code %d\n",
479 GST_OBJECT_NAME(message->src), err->message, err->code);
480 g_printerr("Debugging info: %s\n", (dbg_info) ? dbg_info : "none");
481 gst_pipeline_failed = TRUE;
490 gst_message_unref(message);
495 handle_xdg_surface_configure(void *data, struct xdg_surface *surface, uint32_t serial)
497 struct window *window = static_cast<struct window *>(data);
499 xdg_surface_ack_configure(surface, serial);
501 if (window->wait_for_configure) {
502 redraw(window, NULL, 0);
503 window->wait_for_configure = false;
507 static const struct xdg_surface_listener xdg_surface_listener = {
508 handle_xdg_surface_configure,
512 handle_xdg_toplevel_configure(void *data, struct xdg_toplevel *xdg_toplevel,
513 int32_t width, int32_t height,
514 struct wl_array *states)
516 struct window *window = static_cast<struct window *>(data);
519 window->fullscreen = 0;
520 window->maximized = 0;
522 // use our own macro as C++ can't typecast from (void *) directly
523 WL_ARRAY_FOR_EACH(p, states, uint32_t *) {
526 case XDG_TOPLEVEL_STATE_FULLSCREEN:
527 window->fullscreen = 1;
529 case XDG_TOPLEVEL_STATE_MAXIMIZED:
530 window->maximized = 1;
535 if (width > 0 && height > 0) {
536 if (!window->fullscreen && !window->maximized) {
537 window->init_width = width;
538 window->init_height = height;
540 window->width = width;
541 window->height = height;
542 } else if (!window->fullscreen && !window->maximized) {
543 window->width = window->init_width;
544 window->height = window->init_height;
547 window->needs_update_buffer = true;
552 handle_xdg_toplevel_close(void *data, struct xdg_toplevel *xdg_toplevel)
557 static const struct xdg_toplevel_listener xdg_toplevel_listener = {
558 handle_xdg_toplevel_configure,
559 handle_xdg_toplevel_close,
562 static struct window *
563 create_window(struct display *display, int width, int height, const char *app_id)
565 struct window *window;
568 assert(display->wm_base != NULL);
570 window = static_cast<struct window *>(calloc(1, sizeof(*window)));
574 wl_list_init(&window->buffer_list);
575 window->callback = NULL;
576 window->display = display;
577 window->width = width;
578 window->height = height;
579 window->init_width = width;
580 window->init_height = height;
581 window->surface = wl_compositor_create_surface(display->wl_compositor);
583 if (display->wm_base) {
584 window->xdg_surface =
585 xdg_wm_base_get_xdg_surface(display->wm_base, window->surface);
586 assert(window->xdg_surface);
588 xdg_surface_add_listener(window->xdg_surface,
589 &xdg_surface_listener, window);
590 window->xdg_toplevel = xdg_surface_get_toplevel(window->xdg_surface);
591 assert(window->xdg_toplevel);
593 xdg_toplevel_add_listener(window->xdg_toplevel,
594 &xdg_toplevel_listener, window);
596 xdg_toplevel_set_app_id(window->xdg_toplevel, app_id);
598 wl_surface_commit(window->surface);
599 window->wait_for_configure = true;
602 for (i = 0; i < MAX_BUFFER_ALLOC; i++)
603 alloc_buffer(window, window->width, window->height);
610 destroy_window(struct window *window)
612 struct buffer *buffer, *buffer_next;
614 if (window->callback)
615 wl_callback_destroy(window->callback);
617 wl_list_for_each_safe(buffer, buffer_next,
618 &window->buffer_list, buffer_link)
619 destroy_buffer(buffer);
621 if (window->xdg_toplevel)
622 xdg_toplevel_destroy(window->xdg_toplevel);
624 if (window->xdg_surface)
625 xdg_surface_destroy(window->xdg_surface);
627 wl_surface_destroy(window->surface);
632 signal_int(int sig, siginfo_t *si, void *_unused)
637 static struct display *
638 create_display(int argc, char *argv[])
640 struct display *display;
642 display = static_cast<struct display *>(calloc(1, sizeof(*display)));
643 if (display == NULL) {
644 fprintf(stderr, "out of memory\n");
647 display->wl_display = wl_display_connect(NULL);
648 assert(display->wl_display);
650 display->has_xrgb = false;
651 display->wl_registry = wl_display_get_registry(display->wl_display);
653 wl_registry_add_listener(display->wl_registry, ®istry_listener, display);
654 wl_display_roundtrip(display->wl_display);
656 if (display->shm == NULL) {
657 fprintf(stderr, "No wl_shm global\n");
661 wl_display_roundtrip(display->wl_display);
663 if (!display->has_xrgb) {
664 fprintf(stderr, "WL_SHM_FORMAT_XRGB32 not available\n");
672 destroy_display(struct display *display)
675 wl_shm_destroy(display->shm);
677 if (display->wm_base)
678 xdg_wm_base_destroy(display->wm_base);
680 if (display->wl_compositor)
681 wl_compositor_destroy(display->wl_compositor);
683 wl_registry_destroy(display->wl_registry);
684 wl_display_flush(display->wl_display);
685 wl_display_disconnect(display->wl_display);
689 // stringify the un-quoted string to quoted C string
690 #define xstr(a) str(a)
693 GstElement* create_pipeline(int* argc, char** argv[])
695 GError *error = NULL;
696 const char *camera_device = NULL;
697 const char *width_str = NULL;
698 const char *height_str = NULL;
702 // pipewire is default.
703 char *v4l2_path = getenv("ENABLE_V4L2_PATH");
706 char pipeline_str[1024];
708 camera_device = getenv("DEFAULT_V4L2_DEVICE");
710 camera_device = get_first_camera_device();
712 width_str = getenv("DEFAULT_DEVICE_WIDTH");
714 width = WINDOW_WIDTH_SIZE;
716 width = atoi(width_str);
718 height_str = getenv("DEFAULT_DEVICE_HEIGHT");
720 height = WINDOW_HEIGHT_SIZE;
722 height = atoi(height_str);
724 if (v4l2_path == NULL)
726 else if (g_str_equal(v4l2_path, "yes") || g_str_equal(v4l2_path, "true"))
729 memset(pipeline_str, 0, sizeof(pipeline_str));
732 snprintf(pipeline_str, sizeof(pipeline_str), "v4l2src device=%s ! video/x-raw,width=%d,height=%d ! waylandsink",
733 camera_device, width, height);
734 else if (gst_pipeline_failed == TRUE) {
735 snprintf(pipeline_str, sizeof(pipeline_str), "filesrc location=%s/still-image.jpg ! decodebin ! videoconvert ! imagefreeze ! waylandsink",
736 xstr(APP_DATA_PATH));
737 fallback_gst_pipeline_tried = TRUE;
740 snprintf(pipeline_str, sizeof(pipeline_str), "pipewiresrc ! waylandsink");
743 fprintf(stdout, "Using pipeline: %s\n", pipeline_str);
745 GstElement *pipeline = gst_parse_launch(pipeline_str, &error);
747 if (error || !pipeline) {
748 fprintf(stderr, "gstreamer pipeline construction failed!\n");
757 run_in_thread(GrpcClient *client)
759 grpc::Status status = client->Wait();
763 app_status_callback(::agl_shell_ipc::AppStateResponse app_response, void *data)
767 std::cout << " >> AppStateResponse app_id " <<
768 app_response.app_id() << ", with state " <<
769 app_response.state() << std::endl;
771 // there's no implicit deactivation for an activation of another app so
772 // assume that another application got activate we are also deactived
773 if (app_response.state() == 2 && app_response.app_id() != my_app_id) {
775 std::cout << "Suspending gstreamer pipeline" << std::endl;
780 int main(int argc, char* argv[])
784 struct receiver_data receiver_data = {};
785 struct display* display;
786 struct window* window;
788 // always start the grpc client
789 GrpcClient *client = new GrpcClient();
791 // subscribe to events to be able to suspend/pause the video
793 std::thread th = std::thread(run_in_thread, client);
794 client->AppStatusState(app_status_callback, &receiver_data);
796 // for starting the application from the beginning, with a diffrent
797 // role we need to handle that creating the main window
798 if (argc >= 2 && strcmp(argv[1], "float") == 0) {
799 client->SetAppFloat(std::string(my_app_id), 30, 400);
802 sa.sa_sigaction = signal_int;
803 sigemptyset(&sa.sa_mask);
804 sa.sa_flags = SA_RESETHAND | SA_SIGINFO;
805 sigaction(SIGINT, &sa, NULL);
808 char** gargv = static_cast<char**>(calloc(2, sizeof(char*)));
810 gargv[0] = strdup(argv[0]);
811 gargv[1] = strdup("--gst-debug-level=2");
813 setbuf(stdout, NULL);
815 gst_init(&gargc, &gargv);
817 receiver_data.pipeline = create_pipeline(&gargc, &gargv);
819 if (!receiver_data.pipeline)
822 display = create_display(argc, argv);
826 // we use the role to set a correspondence between the top level
827 // surface and our application, with the previous call letting the
828 // compositor know that we're one and the same
829 window = create_window(display, WINDOW_WIDTH_SIZE, WINDOW_HEIGHT_SIZE, my_app_id);
836 window->display = display;
837 receiver_data.window = window;
839 /* Initialise damage to full surface, so the padding gets painted */
840 wl_surface_damage(window->surface, 0, 0,
841 window->width, window->height);
843 if (!window->wait_for_configure) {
844 redraw(window, NULL, 0);
847 GstBus *bus = gst_element_get_bus(receiver_data.pipeline);
848 gst_bus_add_signal_watch(bus);
850 g_signal_connect(bus, "message::error", G_CALLBACK(error_cb), &receiver_data);
851 gst_bus_set_sync_handler(bus, bus_sync_handler, &receiver_data, NULL);
852 gst_object_unref(bus);
854 gst_element_set_state(receiver_data.pipeline, GST_STATE_PLAYING);
855 fprintf(stdout, "gstreamer pipeline running\n");
857 // run the application
858 while (running && ret != -1) {
859 ret = wl_display_dispatch(display->wl_display);
860 if (gst_pipeline_failed && fallback_gst_pipeline_tried == FALSE) {
861 gst_element_set_state(receiver_data.pipeline, GST_STATE_NULL);
862 gst_object_unref(receiver_data.pipeline);
863 /* retry with fallback pipeline */
864 receiver_data.pipeline = create_pipeline(&gargc, &gargv);
865 GstBus *bus = gst_element_get_bus(receiver_data.pipeline);
866 gst_bus_add_signal_watch(bus);
867 g_signal_connect(bus, "message::error", G_CALLBACK(error_cb), &receiver_data);
868 gst_bus_set_sync_handler(bus, bus_sync_handler, &receiver_data, NULL);
869 gst_object_unref(bus);
870 gst_element_set_state(receiver_data.pipeline, GST_STATE_PLAYING);
873 gst_element_set_state(receiver_data.pipeline, GST_STATE_NULL);
874 gst_object_unref(receiver_data.pipeline);
876 destroy_window(window);
877 destroy_display(display);