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