2d286ba5b4a348873908581207cc0cd12c897de9
[apps/camera-gstreamer.git] / app / main.cpp
1 #define GST_USE_UNSTABLE_API
2 #define HAVE_MEMFD_CREATE
3
4 #include <string>
5 #include <iostream>
6 #include <cstring>
7 #include <cstdio>
8 #include <cstdlib>
9
10 #include <signal.h>
11 #include <wayland-client.h>
12 #include <sys/mman.h>
13 #include <unistd.h>
14 #include <fcntl.h>
15 #include <assert.h>
16
17 #include "xdg-shell-client-protocol.h"
18 #include "agl-shell-desktop-client-protocol.h"
19 #include "utils.h"
20
21 #include <gst/gst.h>
22
23 #include <gst/video/videooverlay.h>
24 #include <gst/wayland/wayland.h>
25
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
30
31 #define WINDOW_WIDTH_POS_X      640
32 #define WINDOW_WIDTH_POS_Y      180
33
34 #define MAX_BUFFER_ALLOC        2
35
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); \
40              (pos)++)
41
42 struct display {
43         struct wl_display *wl_display;
44         struct wl_registry *wl_registry;
45         struct wl_compositor *wl_compositor;
46         struct wl_output *wl_output;
47         struct wl_shm *shm;
48
49         struct {
50                 int width;
51                 int height;
52         } output_data;
53
54         struct xdg_wm_base *wm_base;
55         struct agl_shell_desktop *agl_shell_desktop;
56
57         int has_xrgb;
58 };
59
60 struct buffer {
61         struct wl_buffer *buffer;
62         void *shm_data;
63         int busy;
64         int width, height;
65         size_t size;    /* width * 4 * height */
66         struct wl_list buffer_link; /** window::buffer_list */
67 };
68
69 struct window {
70         struct display *display;
71
72         int x, y;
73         int width, height;
74         int init_width;
75         int init_height;
76
77         struct wl_surface *surface;
78
79         struct xdg_surface *xdg_surface;
80         struct xdg_toplevel *xdg_toplevel;
81         bool wait_for_configure;
82
83         int fullscreen, maximized;
84
85         struct wl_list buffer_list;
86         struct wl_callback *callback;
87         bool needs_update_buffer;
88 };
89
90
91 struct receiver_data {
92         struct window *window;
93
94         GstElement *pipeline;
95         GstWaylandVideo *wl_video;
96         GstVideoOverlay *overlay;
97 };
98
99 static int running = 1;
100 static bool gst_pipeline_failed = FALSE;
101 static bool fallback_gst_pipeline_tried = FALSE;
102
103 static void
104 redraw(void *data, struct wl_callback *callback, uint32_t time);
105
106 static struct buffer *
107 alloc_buffer(struct window *window, int width, int height)
108 {
109         struct buffer *buffer = static_cast<struct buffer *>(calloc(1, sizeof(*buffer)));
110
111         buffer->width = width;
112         buffer->height = height;
113         wl_list_insert(&window->buffer_list, &buffer->buffer_link);
114
115         return buffer;
116 }
117
118 static void
119 destroy_buffer(struct buffer *buffer)
120 {
121         if (buffer->buffer)
122                 wl_buffer_destroy(buffer->buffer);
123
124         munmap(buffer->shm_data, buffer->size);
125         wl_list_remove(&buffer->buffer_link);
126         free(buffer);
127 }
128
129 static struct buffer *
130 pick_free_buffer(struct window *window)
131 {
132         struct buffer *b;
133         struct buffer *buffer = NULL;
134
135         wl_list_for_each(b, &window->buffer_list, buffer_link) {
136                 if (!b->busy) {
137                         buffer = b;
138                         break;
139                 }
140         }
141
142         return buffer;
143 }
144
145 static void
146 prune_old_released_buffers(struct window *window)
147 {
148         struct buffer *b, *b_next;
149
150         wl_list_for_each_safe(b, b_next,
151                               &window->buffer_list, buffer_link) {
152                 if (!b->busy && (b->width != window->width ||
153                                  b->height != window->height))
154                         destroy_buffer(b);
155         }
156 }
157
158 static void
159 paint_pixels(void *image, int padding, int width, int height, uint32_t time)
160 {
161         memset(image, 0x00, width * height * 4);
162 }
163
164 static void
165 buffer_release(void *data, struct wl_buffer *buffer)
166 {
167         struct buffer *mybuf = static_cast<struct buffer *>(data);
168         mybuf->busy = 0;
169 }
170
171 static const struct wl_buffer_listener buffer_listener = {
172         buffer_release
173 };
174
175
176 static int
177 create_shm_buffer(struct display *display, struct buffer *buffer,
178                   int width, int height, uint32_t format)
179 {
180         struct wl_shm_pool *pool;
181         int fd, size, stride;
182         void *data;
183
184         stride = width * 4;
185         size = stride * height;
186
187         fd = os_create_anonymous_file(size);
188         if (fd < 0) {
189                 fprintf(stderr, "creating a buffer file for %d B failed: %s\n",
190                                 size, strerror(errno));
191                 return -1;
192         }
193
194         data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
195         if (data == MAP_FAILED) {
196                 fprintf(stderr, "mmap failed: %s\n", strerror(errno));
197                 close(fd);
198                 return -1;
199         }
200
201         pool = wl_shm_create_pool(display->shm, fd, size);
202         buffer->buffer = wl_shm_pool_create_buffer(pool, 0, width,
203                                                    height, stride, format);
204         wl_buffer_add_listener(buffer->buffer, &buffer_listener, buffer);
205         wl_shm_pool_destroy(pool);
206         close(fd);
207
208         buffer->shm_data = data;
209         buffer->size = size;
210         buffer->width = width;
211         buffer->height = height;
212
213         fprintf(stdout, "Created shm buffer with width %d, height %d\n", width, height);
214         return 0;
215 }
216
217 static struct buffer *
218 get_next_buffer(struct window *window)
219 {
220         struct buffer *buffer = NULL;
221         int ret = 0;
222
223         if (window->needs_update_buffer) {
224                 int i;
225
226                 for (i = 0; i < MAX_BUFFER_ALLOC; i++)
227                         alloc_buffer(window, window->width, window->height);
228
229                 window->needs_update_buffer = false;
230         }
231
232         buffer = pick_free_buffer(window);
233         if (!buffer)
234                 return NULL;
235
236
237         if (!buffer->buffer) {
238                 ret = create_shm_buffer(window->display, buffer, window->width,
239                                         window->height, WL_SHM_FORMAT_XRGB8888);
240
241                 if (ret < 0)
242                         return NULL;
243
244                 /* paint the padding */
245                 memset(buffer->shm_data, 0x00, window->width * window->height * 4);
246         }
247
248         return buffer;
249 }
250
251
252 static const struct wl_callback_listener frame_listener = {
253         redraw
254 };
255
256 static void
257 redraw(void *data, struct wl_callback *callback, uint32_t time)
258 {
259         struct window *window = static_cast<struct window *>(data);
260         struct buffer *buffer;
261
262         prune_old_released_buffers(window);
263
264         buffer = get_next_buffer(window);
265         if (!buffer) {
266                 fprintf(stderr,
267                                 !callback ? "Failed to create the first buffer.\n" :
268                                 "Both buffers busy at redraw(). Server bug?\n");
269                 abort();
270         }
271
272         // do the actual painting
273         paint_pixels(buffer->shm_data, 0x0, window->width, window->height, time);
274
275         wl_surface_attach(window->surface, buffer->buffer, 0, 0);
276         wl_surface_damage(window->surface, 0, 0, window->width, window->height);
277
278         if (callback)
279                 wl_callback_destroy(callback);
280
281         window->callback = wl_surface_frame(window->surface);
282         wl_callback_add_listener(window->callback, &frame_listener, window);
283         wl_surface_commit(window->surface);
284
285         buffer->busy = 1;
286 }
287
288 static void
289 shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
290 {
291         struct display *d = static_cast<struct display *>(data);
292
293         if (format == WL_SHM_FORMAT_XRGB8888)
294                 d->has_xrgb = true;
295 }
296
297 static const struct wl_shm_listener shm_listener = {
298         shm_format
299 };
300
301 static void
302 xdg_wm_base_ping(void *data, struct xdg_wm_base *shell, uint32_t serial)
303 {
304         xdg_wm_base_pong(shell, serial);
305 }
306
307 static const struct xdg_wm_base_listener xdg_wm_base_listener = {
308         xdg_wm_base_ping,
309 };
310
311 static void
312 display_handle_geometry(void *data, struct wl_output *wl_output,
313                         int x, int y, int physical_width, int physical_height,
314                         int subpixel, const char *make, const char *model, int transform)
315 {
316         (void) data;
317         (void) wl_output;
318         (void) x;
319         (void) y;
320         (void) physical_width;
321         (void) physical_height;
322         (void) subpixel;
323         (void) make;
324         (void) model;
325         (void) transform;
326 }
327
328 static void
329 display_handle_mode(void *data, struct wl_output *wl_output, uint32_t flags,
330                     int width, int height, int refresh)
331 {
332         struct display *d = static_cast<struct display *>(data);
333
334         if (wl_output == d->wl_output && (flags & WL_OUTPUT_MODE_CURRENT)) {
335                 d->output_data.width = width;
336                 d->output_data.height = height;
337
338                 fprintf(stdout, "Found output with width %d and height %d\n",
339                                 d->output_data.width, d->output_data.height);
340         }
341 }
342
343 static void
344 display_handle_scale(void *data, struct wl_output *wl_output, int scale)
345 {
346         (void) data;
347         (void) wl_output;
348         (void) scale;
349 }
350
351 static void
352 display_handle_done(void *data, struct wl_output *wl_output)
353 {
354         (void) data;
355         (void) wl_output;
356 }
357
358 static const struct wl_output_listener output_listener = {
359         display_handle_geometry,
360         display_handle_mode,
361         display_handle_done,
362         display_handle_scale
363 };
364
365 static void
366 application_id(void *data, struct agl_shell_desktop *agl_shell_desktop,
367                 const char *app_id)
368 {
369         (void) data;
370         (void) agl_shell_desktop;
371         (void) app_id;
372 }
373
374 static void
375 application_id_state(void *data, struct agl_shell_desktop *agl_shell_desktop,
376                      const char *app_id, const char *app_data,
377                      uint32_t app_state, uint32_t app_role)
378 {
379         (void) data;
380         (void) app_data;
381         (void) agl_shell_desktop;
382         (void) app_id;
383         (void) app_state;
384         (void) app_role;
385 }
386
387 static const struct agl_shell_desktop_listener agl_shell_desktop_listener = {
388         application_id,
389         application_id_state,
390 };
391
392 static void
393 registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
394                        const char *interface, uint32_t version)
395 {
396         struct display *d = static_cast<struct display *>(data);
397
398         if (strcmp(interface, "wl_compositor") == 0) {
399                 d->wl_compositor =
400                         static_cast<struct wl_compositor *>(wl_registry_bind(registry, id,
401                                                     &wl_compositor_interface, 1));
402         } else if (strcmp(interface, "xdg_wm_base") == 0) {
403                 d->wm_base = static_cast<struct xdg_wm_base *>(wl_registry_bind(registry,
404                                 id, &xdg_wm_base_interface, 1));
405                 xdg_wm_base_add_listener(d->wm_base, &xdg_wm_base_listener, d);
406         } else if (strcmp(interface, "wl_shm") == 0) {
407                 d->shm = static_cast<struct wl_shm *>(wl_registry_bind(registry,
408                                 id, &wl_shm_interface, 1));
409                 wl_shm_add_listener(d->shm, &shm_listener, d);
410         } else if (strcmp(interface, "agl_shell_desktop") == 0) {
411                 d->agl_shell_desktop = static_cast<struct agl_shell_desktop *>(wl_registry_bind(registry, id,
412                                                         &agl_shell_desktop_interface, 1));
413                 /* as an example, show how to register for events from the compositor */
414                 agl_shell_desktop_add_listener(d->agl_shell_desktop,
415                                                &agl_shell_desktop_listener, d);
416         } else if (strcmp(interface, "wl_output") == 0) {
417                 d->wl_output = static_cast<struct wl_output *>(wl_registry_bind(registry, id,
418                                              &wl_output_interface, 1));
419                 wl_output_add_listener(d->wl_output, &output_listener, d);
420         }
421 }
422
423 static void
424 registry_handle_global_remove(void *data, struct wl_registry *reg, uint32_t id)
425 {
426         (void) data;
427         (void) reg;
428         (void) id;
429 }
430
431 static const struct wl_registry_listener registry_listener = {
432         registry_handle_global,
433         registry_handle_global_remove,
434 };
435
436
437 static void
438 error_cb(GstBus *bus, GstMessage *msg, gpointer user_data)
439 {
440         struct receiver_data *d =
441                 static_cast<struct receiver_data *>(user_data);
442
443         gchar *debug = NULL;
444         GError *err = NULL;
445
446         gst_message_parse_error(msg, &err, &debug);
447
448         g_print("Error: %s\n", err->message);
449         g_error_free(err);
450
451         if (debug) {
452                 g_print("Debug details: %s\n", debug);
453                 g_free(debug);
454         }
455
456         gst_element_set_state(d->pipeline, GST_STATE_NULL);
457 }
458
459 static GstBusSyncReply
460 bus_sync_handler(GstBus *bus, GstMessage *message, gpointer user_data)
461 {
462         struct receiver_data *d =
463                 static_cast<struct receiver_data *>(user_data);
464
465         if (gst_is_wayland_display_handle_need_context_message(message)) {
466                 GstContext *context;
467                 struct wl_display *display_handle = d->window->display->wl_display;
468
469                 context = gst_wayland_display_handle_context_new(display_handle);
470                 d->wl_video = GST_WAYLAND_VIDEO(GST_MESSAGE_SRC(message));
471                 gst_element_set_context(GST_ELEMENT(GST_MESSAGE_SRC(message)), context);
472
473                 goto drop;
474         } else if (gst_is_video_overlay_prepare_window_handle_message(message)) {
475                 struct wl_surface *window_handle = d->window->surface;
476
477                 /* GST_MESSAGE_SRC(message) will be the overlay object that we
478                  * have to use. This may be waylandsink, but it may also be
479                  * playbin. In the latter case, we must make sure to use
480                  * playbin instead of waylandsink, because playbin resets the
481                  * window handle and render_rectangle after restarting playback
482                  * and the actual window size is lost */
483                 d->overlay = GST_VIDEO_OVERLAY(GST_MESSAGE_SRC(message));
484
485                 g_print("setting window handle and size (%d x %d) w %d, h %d\n",
486                                 d->window->x, d->window->y,
487                                 d->window->width, d->window->height);
488
489                 gst_video_overlay_set_window_handle(d->overlay, (guintptr) window_handle);
490                 gst_video_overlay_set_render_rectangle(d->overlay,
491                                                        d->window->x, d->window->y,
492                                                        d->window->width, d->window->height);
493
494                 goto drop;
495         }
496         else if (GST_MESSAGE_TYPE(message) == GST_MESSAGE_ERROR) {
497                 GError* err = NULL;
498                 gchar* dbg_info = NULL;
499
500                 gst_message_parse_error(message, &err, &dbg_info);
501                 g_printerr("ERROR from element %s: %s code %d\n",
502                         GST_OBJECT_NAME(message->src), err->message, err->code);
503                 g_printerr("Debugging info: %s\n", (dbg_info) ? dbg_info : "none");
504                 gst_pipeline_failed = TRUE;
505                 g_error_free(err);
506                 g_free(dbg_info);
507                 goto drop;
508         }
509
510         return GST_BUS_PASS;
511
512 drop:
513         gst_message_unref(message);
514         return GST_BUS_DROP;
515 }
516
517 static void
518 handle_xdg_surface_configure(void *data, struct xdg_surface *surface, uint32_t serial)
519 {
520         struct window *window = static_cast<struct window *>(data);
521
522         xdg_surface_ack_configure(surface, serial);
523
524         if (window->wait_for_configure) {
525                 redraw(window, NULL, 0);
526                 window->wait_for_configure = false;
527         }
528 }
529
530 static const struct xdg_surface_listener xdg_surface_listener = {
531         handle_xdg_surface_configure,
532 };
533
534 static void
535 handle_xdg_toplevel_configure(void *data, struct xdg_toplevel *xdg_toplevel,
536                               int32_t width, int32_t height,
537                               struct wl_array *states)
538 {
539         struct window *window = static_cast<struct window *>(data);
540         uint32_t *p;
541
542         window->fullscreen = 0;
543         window->maximized = 0;
544
545         // use our own macro as C++ can't typecast from (void *) directly
546         WL_ARRAY_FOR_EACH(p, states, uint32_t *) {
547                 uint32_t state = *p;
548                 switch (state) {
549                 case XDG_TOPLEVEL_STATE_FULLSCREEN:
550                         window->fullscreen = 1;
551                         break;
552                 case XDG_TOPLEVEL_STATE_MAXIMIZED:
553                         window->maximized = 1;
554                         break;
555                 }
556         }
557
558         if (width > 0 && height > 0) {
559                 if (!window->fullscreen && !window->maximized) {
560                         window->init_width = width;
561                         window->init_height = height;
562                 }
563                 window->width = width;
564                 window->height = height;
565         } else if (!window->fullscreen && !window->maximized) {
566                 window->width = window->init_width;
567                 window->height = window->init_height;
568         }
569
570         window->needs_update_buffer = true;
571
572 }
573
574 static void
575 handle_xdg_toplevel_close(void *data, struct xdg_toplevel *xdg_toplevel)
576 {
577         running = 0;
578 }
579
580 static const struct xdg_toplevel_listener xdg_toplevel_listener = {
581         handle_xdg_toplevel_configure,
582         handle_xdg_toplevel_close,
583 };
584
585 static struct window *
586 create_window(struct display *display, int width, int height, const char *app_id)
587 {
588         struct window *window;
589         int i;
590
591         assert(display->wm_base != NULL);
592
593         window = static_cast<struct window *>(calloc(1, sizeof(*window)));
594         if (!window)
595                 return NULL;
596
597         wl_list_init(&window->buffer_list);
598         window->callback = NULL;
599         window->display = display;
600         window->width = width;
601         window->height = height;
602         window->surface = wl_compositor_create_surface(display->wl_compositor);
603
604         if (display->wm_base) {
605                 window->xdg_surface =
606                         xdg_wm_base_get_xdg_surface(display->wm_base, window->surface);
607                 assert(window->xdg_surface);
608
609                 xdg_surface_add_listener(window->xdg_surface,
610                                          &xdg_surface_listener, window);
611                 window->xdg_toplevel = xdg_surface_get_toplevel(window->xdg_surface);
612                 assert(window->xdg_toplevel);
613
614                 xdg_toplevel_add_listener(window->xdg_toplevel,
615                                           &xdg_toplevel_listener, window);
616
617                 xdg_toplevel_set_app_id(window->xdg_toplevel, app_id);
618
619                 wl_surface_commit(window->surface);
620                 window->wait_for_configure = true;
621         }
622
623         for (i = 0; i < MAX_BUFFER_ALLOC; i++)
624                 alloc_buffer(window, window->width, window->height);
625
626         return window;
627 }
628
629
630 static void
631 destroy_window(struct window *window)
632 {
633         struct buffer *buffer, *buffer_next;
634
635         if (window->callback)
636                 wl_callback_destroy(window->callback);
637
638         wl_list_for_each_safe(buffer, buffer_next,
639                               &window->buffer_list, buffer_link)
640                 destroy_buffer(buffer);
641
642         if (window->xdg_toplevel)
643                 xdg_toplevel_destroy(window->xdg_toplevel);
644
645         if (window->xdg_surface)
646                 xdg_surface_destroy(window->xdg_surface);
647
648         wl_surface_destroy(window->surface);
649         free(window);
650 }
651
652 static void
653 signal_int(int sig, siginfo_t *si, void *_unused)
654 {
655         running = 0;
656 }
657
658 static struct display *
659 create_display(int argc, char *argv[])
660 {
661         struct display *display;
662
663         display = static_cast<struct display *>(calloc(1, sizeof(*display)));
664         if (display == NULL) {
665                 fprintf(stderr, "out of memory\n");
666                 exit(1);
667         }
668         display->wl_display = wl_display_connect(NULL);
669         assert(display->wl_display);
670
671         display->has_xrgb = false;
672         display->wl_registry = wl_display_get_registry(display->wl_display);
673
674         wl_registry_add_listener(display->wl_registry, &registry_listener, display);
675         wl_display_roundtrip(display->wl_display);
676
677         if (display->shm == NULL) {
678                 fprintf(stderr, "No wl_shm global\n");
679                 return NULL;
680         }
681
682         if (display->agl_shell_desktop == NULL) {
683                 fprintf(stderr, "No agl_shell extension present\n");
684                 return NULL;
685         }
686
687         wl_display_roundtrip(display->wl_display);
688
689         if (!display->has_xrgb) {
690                 fprintf(stderr, "WL_SHM_FORMAT_XRGB32 not available\n");
691                 return NULL;
692         }
693
694         return display;
695 }
696
697 static void
698 destroy_display(struct display *display)
699 {
700         if (display->shm)
701                 wl_shm_destroy(display->shm);
702
703         if (display->wm_base)
704                 xdg_wm_base_destroy(display->wm_base);
705
706         if (display->agl_shell_desktop)
707                 agl_shell_desktop_destroy(display->agl_shell_desktop);
708
709         if (display->wl_compositor)
710                 wl_compositor_destroy(display->wl_compositor);
711
712         wl_registry_destroy(display->wl_registry);
713         wl_display_flush(display->wl_display);
714         wl_display_disconnect(display->wl_display);
715         free(display);
716 }
717
718 // stringify the un-quoted string to quoted C string
719 #define xstr(a) str(a)
720 #define str(a) #a
721
722 GstElement* create_pipeline(int* argc, char** argv[])
723 {
724         GError *error = NULL;
725         const char *camera_device = NULL;
726         const char *width_str = NULL;
727         const char *height_str = NULL;
728         int width;
729         int height;
730
731         // pipewire is default.
732         char *v4l2_path = getenv("ENABLE_V4L2_PATH");
733         bool v4l2 = false;
734
735         char pipeline_str[1024];
736
737         camera_device = getenv("DEFAULT_V4L2_DEVICE");
738         if (!camera_device)
739                 camera_device = get_first_camera_device();
740
741         width_str = getenv("DEFAULT_DEVICE_WIDTH");
742         if (!width_str)
743                 width = WINDOW_WIDTH_SIZE;
744         else
745                 width = atoi(width_str);
746
747         height_str = getenv("DEFAULT_DEVICE_HEIGHT");
748         if (!height_str)
749                 height = WINDOW_HEIGHT_SIZE;
750         else
751                 height = atoi(height_str);
752
753         if (v4l2_path == NULL)
754                 v4l2 = false;
755         else if (g_str_equal(v4l2_path, "yes") || g_str_equal(v4l2_path, "true"))
756                 v4l2 = true;
757
758         memset(pipeline_str, 0, sizeof(pipeline_str));
759
760         if (v4l2)
761                 snprintf(pipeline_str, sizeof(pipeline_str), "v4l2src device=%s ! video/x-raw,width=%d,height=%d ! waylandsink",
762                         camera_device, width, height);
763         else if (gst_pipeline_failed == TRUE) {
764                 snprintf(pipeline_str, sizeof(pipeline_str), "filesrc location=%s/still-image.jpg ! decodebin ! videoconvert ! imagefreeze ! waylandsink fullscreen=true",
765                         xstr(APP_DATA_PATH), width, height);
766                 fallback_gst_pipeline_tried = TRUE;
767         }
768         else {
769                 snprintf(pipeline_str, sizeof(pipeline_str), "pipewiresrc ! video/x-raw,width=%d,height=%d ! waylandsink", width,
770                         height);
771         }
772
773         fprintf(stdout, "Using pipeline: %s\n", pipeline_str);
774
775         GstElement *pipeline = gst_parse_launch(pipeline_str, &error);
776
777         if (error || !pipeline) {
778                 fprintf(stderr, "gstreamer pipeline construction failed!\n");
779                 free(argv);
780                 return NULL;
781         }
782
783         return pipeline;
784 }
785
786 int main(int argc, char* argv[])
787 {
788         int ret = 0;
789         struct sigaction sa;
790         struct receiver_data receiver_data = {};
791         struct display* display;
792         struct window* window;
793         const char* app_id = "camera-gstreamer";
794
795         sa.sa_sigaction = signal_int;
796         sigemptyset(&sa.sa_mask);
797         sa.sa_flags = SA_RESETHAND | SA_SIGINFO;
798         sigaction(SIGINT, &sa, NULL);
799
800         int gargc = 2;
801         char** gargv = static_cast<char**>(calloc(2, sizeof(char*)));
802
803         gargv[0] = strdup(argv[0]);
804         gargv[1] = strdup("--gst-debug-level=2");
805
806         setbuf(stdout, NULL);
807
808         gst_init(&gargc, &gargv);
809
810         receiver_data.pipeline = create_pipeline(&gargc, &gargv);
811
812         if (!receiver_data.pipeline)
813                 return EXIT_FAILURE;
814
815         display = create_display(argc, argv);
816         if (!display)
817                 return -1;
818
819         // if you'd want to place the video in a pop-up/dialog type of window:
820         // agl_shell_desktop_set_app_property(display->agl_shell_desktop, app_id,
821         //                                 AGL_SHELL_DESKTOP_APP_ROLE_POPUP,
822         //                                 WINDOW_WIDTH_POS_X, WINDOW_WIDTH_POS_Y,
823         //                                 0, 0, WINDOW_WIDTH_SIZE, WINDOW_HEIGHT_SIZE,
824         //                                 display->wl_output);
825
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, app_id);
830
831         if (!window) {
832                 free(gargv);
833                 return EXIT_FAILURE;
834         }
835
836         window->display = display;
837         receiver_data.window = window;
838
839         /* Initialise damage to full surface, so the padding gets painted */
840         wl_surface_damage(window->surface, 0, 0,
841                           window->width, window->height);
842
843         if (!window->wait_for_configure) {
844                 redraw(window, NULL, 0);
845         }
846
847         GstBus *bus = gst_element_get_bus(receiver_data.pipeline);
848         gst_bus_add_signal_watch(bus);
849
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);
853
854         gst_element_set_state(receiver_data.pipeline, GST_STATE_PLAYING);
855         fprintf(stdout, "gstreamer pipeline running\n");
856
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);
871                 }
872         }
873         gst_element_set_state(receiver_data.pipeline, GST_STATE_NULL);
874         gst_object_unref(receiver_data.pipeline);
875
876         destroy_window(window);
877         destroy_display(display);
878         free(gargv);
879
880         return ret;
881 }