app: Listen for activation events
[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 #include <thread>
17
18 #include "utils.h"
19 #include "xdg-shell-client-protocol.h"
20 #include "AglShellGrpcClient.h"
21
22 #include <gst/gst.h>
23
24 #include <gst/video/videooverlay.h>
25 #include <gst/wayland/wayland.h>
26
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
30 #endif
31
32 #ifndef APP_DATA_PATH
33 #define APP_DATA_PATH /usr/share/applications/data
34 #endif
35
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
40
41 #define WINDOW_WIDTH_POS_X      640
42 #define WINDOW_WIDTH_POS_Y      180
43
44 #define MAX_BUFFER_ALLOC        2
45
46 const char* my_app_id = "camera-gstreamer";
47
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); \
52              (pos)++)
53
54 struct display {
55         struct wl_display *wl_display;
56         struct wl_registry *wl_registry;
57         struct wl_compositor *wl_compositor;
58         struct wl_output *wl_output;
59         struct wl_shm *shm;
60
61         struct {
62                 int width;
63                 int height;
64         } output_data;
65
66         struct xdg_wm_base *wm_base;
67         int has_xrgb;
68 };
69
70 struct buffer {
71         struct wl_buffer *buffer;
72         void *shm_data;
73         int busy;
74         int width, height;
75         size_t size;    /* width * 4 * height */
76         struct wl_list buffer_link; /** window::buffer_list */
77 };
78
79 struct window {
80         struct display *display;
81
82         int x, y;
83         int width, height;
84         int init_width;
85         int init_height;
86
87         struct wl_surface *surface;
88
89         struct xdg_surface *xdg_surface;
90         struct xdg_toplevel *xdg_toplevel;
91         bool wait_for_configure;
92
93         int fullscreen, maximized;
94
95         struct wl_list buffer_list;
96         struct wl_callback *callback;
97         bool needs_update_buffer;
98 };
99
100
101 struct receiver_data {
102         struct window *window;
103
104         GstElement *pipeline;
105         GstVideoOverlay *overlay;
106 };
107
108 static int running = 1;
109 static bool gst_pipeline_failed = FALSE;
110 static bool fallback_gst_pipeline_tried = FALSE;
111
112 static void
113 redraw(void *data, struct wl_callback *callback, uint32_t time);
114
115 static struct buffer *
116 alloc_buffer(struct window *window, int width, int height)
117 {
118         struct buffer *buffer = static_cast<struct buffer *>(calloc(1, sizeof(*buffer)));
119
120         buffer->width = width;
121         buffer->height = height;
122         wl_list_insert(&window->buffer_list, &buffer->buffer_link);
123
124         return buffer;
125 }
126
127 static void
128 destroy_buffer(struct buffer *buffer)
129 {
130         if (buffer->buffer)
131                 wl_buffer_destroy(buffer->buffer);
132
133         munmap(buffer->shm_data, buffer->size);
134         wl_list_remove(&buffer->buffer_link);
135         free(buffer);
136 }
137
138 static struct buffer *
139 pick_free_buffer(struct window *window)
140 {
141         struct buffer *b;
142         struct buffer *buffer = NULL;
143
144         wl_list_for_each(b, &window->buffer_list, buffer_link) {
145                 if (!b->busy) {
146                         buffer = b;
147                         break;
148                 }
149         }
150
151         return buffer;
152 }
153
154 static void
155 prune_old_released_buffers(struct window *window)
156 {
157         struct buffer *b, *b_next;
158
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))
163                         destroy_buffer(b);
164         }
165 }
166
167 static void
168 paint_pixels(void *image, int padding, int width, int height, uint32_t time)
169 {
170         memset(image, 0x00, width * height * 4);
171 }
172
173 static void
174 buffer_release(void *data, struct wl_buffer *buffer)
175 {
176         struct buffer *mybuf = static_cast<struct buffer *>(data);
177         mybuf->busy = 0;
178 }
179
180 static const struct wl_buffer_listener buffer_listener = {
181         buffer_release
182 };
183
184
185 static int
186 create_shm_buffer(struct display *display, struct buffer *buffer,
187                   int width, int height, uint32_t format)
188 {
189         struct wl_shm_pool *pool;
190         int fd, size, stride;
191         void *data;
192
193         stride = width * 4;
194         size = stride * height;
195
196         fd = os_create_anonymous_file(size);
197         if (fd < 0) {
198                 fprintf(stderr, "creating a buffer file for %d B failed: %s\n",
199                                 size, strerror(errno));
200                 return -1;
201         }
202
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));
206                 close(fd);
207                 return -1;
208         }
209
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);
215         close(fd);
216
217         buffer->shm_data = data;
218         buffer->size = size;
219         buffer->width = width;
220         buffer->height = height;
221
222         fprintf(stdout, "Created shm buffer with width %d, height %d\n", width, height);
223
224         return 0;
225 }
226
227 static struct buffer *
228 get_next_buffer(struct window *window)
229 {
230         struct buffer *buffer = NULL;
231         int ret = 0;
232
233         if (window->needs_update_buffer) {
234                 int i;
235
236                 for (i = 0; i < MAX_BUFFER_ALLOC; i++)
237                         alloc_buffer(window, window->width, window->height);
238
239                 window->needs_update_buffer = false;
240         }
241
242         buffer = pick_free_buffer(window);
243         if (!buffer)
244                 return NULL;
245
246
247         if (!buffer->buffer) {
248                 ret = create_shm_buffer(window->display, buffer, window->width,
249                                         window->height, WL_SHM_FORMAT_XRGB8888);
250
251                 if (ret < 0)
252                         return NULL;
253
254                 /* paint the padding */
255                 memset(buffer->shm_data, 0x00, window->width * window->height * 4);
256         }
257
258         return buffer;
259 }
260
261
262 static const struct wl_callback_listener frame_listener = {
263         redraw
264 };
265
266 static void
267 redraw(void *data, struct wl_callback *callback, uint32_t time)
268 {
269         struct window *window = static_cast<struct window *>(data);
270         struct buffer *buffer;
271
272         prune_old_released_buffers(window);
273
274         buffer = get_next_buffer(window);
275         if (!buffer) {
276                 fprintf(stderr,
277                                 !callback ? "Failed to create the first buffer.\n" :
278                                 "Both buffers busy at redraw(). Server bug?\n");
279                 abort();
280         }
281
282         // do the actual painting
283         paint_pixels(buffer->shm_data, 0x0, window->width, window->height, time);
284
285         wl_surface_attach(window->surface, buffer->buffer, 0, 0);
286         wl_surface_damage(window->surface, 0, 0, window->width, window->height);
287
288         if (callback)
289                 wl_callback_destroy(callback);
290
291         window->callback = wl_surface_frame(window->surface);
292         wl_callback_add_listener(window->callback, &frame_listener, window);
293         wl_surface_commit(window->surface);
294
295         buffer->busy = 1;
296 }
297
298 static void
299 shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
300 {
301         struct display *d = static_cast<struct display *>(data);
302
303         if (format == WL_SHM_FORMAT_XRGB8888)
304                 d->has_xrgb = true;
305 }
306
307 static const struct wl_shm_listener shm_listener = {
308         shm_format
309 };
310
311 static void
312 xdg_wm_base_ping(void *data, struct xdg_wm_base *shell, uint32_t serial)
313 {
314         xdg_wm_base_pong(shell, serial);
315 }
316
317 static const struct xdg_wm_base_listener xdg_wm_base_listener = {
318         xdg_wm_base_ping,
319 };
320
321 static void
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)
325 {
326         (void) data;
327         (void) wl_output;
328         (void) x;
329         (void) y;
330         (void) physical_width;
331         (void) physical_height;
332         (void) subpixel;
333         (void) make;
334         (void) model;
335         (void) transform;
336 }
337
338 static void
339 display_handle_mode(void *data, struct wl_output *wl_output, uint32_t flags,
340                     int width, int height, int refresh)
341 {
342         struct display *d = static_cast<struct display *>(data);
343
344         if (wl_output == d->wl_output && (flags & WL_OUTPUT_MODE_CURRENT)) {
345                 d->output_data.width = width;
346                 d->output_data.height = height;
347
348                 fprintf(stdout, "Found output with width %d and height %d\n",
349                                 d->output_data.width, d->output_data.height);
350         }
351 }
352
353 static void
354 display_handle_scale(void *data, struct wl_output *wl_output, int scale)
355 {
356         (void) data;
357         (void) wl_output;
358         (void) scale;
359 }
360
361 static void
362 display_handle_done(void *data, struct wl_output *wl_output)
363 {
364         (void) data;
365         (void) wl_output;
366 }
367
368 static const struct wl_output_listener output_listener = {
369         display_handle_geometry,
370         display_handle_mode,
371         display_handle_done,
372         display_handle_scale
373 };
374
375
376 static void
377 registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
378                        const char *interface, uint32_t version)
379 {
380         struct display *d = static_cast<struct display *>(data);
381
382         if (strcmp(interface, "wl_compositor") == 0) {
383                 d->wl_compositor =
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);
398         }
399 }
400
401 static void
402 registry_handle_global_remove(void *data, struct wl_registry *reg, uint32_t id)
403 {
404         (void) data;
405         (void) reg;
406         (void) id;
407 }
408
409 static const struct wl_registry_listener registry_listener = {
410         registry_handle_global,
411         registry_handle_global_remove,
412 };
413
414
415 static void
416 error_cb(GstBus *bus, GstMessage *msg, gpointer user_data)
417 {
418         struct receiver_data *d =
419                 static_cast<struct receiver_data *>(user_data);
420
421         gchar *debug = NULL;
422         GError *err = NULL;
423
424         gst_message_parse_error(msg, &err, &debug);
425
426         g_print("Error: %s\n", err->message);
427         g_error_free(err);
428
429         if (debug) {
430                 g_print("Debug details: %s\n", debug);
431                 g_free(debug);
432         }
433
434         gst_element_set_state(d->pipeline, GST_STATE_NULL);
435 }
436
437 static GstBusSyncReply
438 bus_sync_handler(GstBus *bus, GstMessage *message, gpointer user_data)
439 {
440         struct receiver_data *d =
441                 static_cast<struct receiver_data *>(user_data);
442
443         if (gst_is_wl_display_handle_need_context_message(message)) {
444                 GstContext *context;
445                 struct wl_display *display_handle = d->window->display->wl_display;
446
447                 context = gst_wl_display_handle_context_new(display_handle);
448                 gst_element_set_context(GST_ELEMENT(GST_MESSAGE_SRC(message)), context);
449
450                 goto drop;
451         } else if (gst_is_video_overlay_prepare_window_handle_message(message)) {
452                 struct wl_surface *window_handle = d->window->surface;
453
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));
461
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);
465
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);
470
471                 goto drop;
472         }
473         else if (GST_MESSAGE_TYPE(message) == GST_MESSAGE_ERROR) {
474                 GError* err = NULL;
475                 gchar* dbg_info = NULL;
476
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;
482                 g_error_free(err);
483                 g_free(dbg_info);
484                 goto drop;
485         }
486
487         return GST_BUS_PASS;
488
489 drop:
490         gst_message_unref(message);
491         return GST_BUS_DROP;
492 }
493
494 static void
495 handle_xdg_surface_configure(void *data, struct xdg_surface *surface, uint32_t serial)
496 {
497         struct window *window = static_cast<struct window *>(data);
498
499         xdg_surface_ack_configure(surface, serial);
500
501         if (window->wait_for_configure) {
502                 redraw(window, NULL, 0);
503                 window->wait_for_configure = false;
504         }
505 }
506
507 static const struct xdg_surface_listener xdg_surface_listener = {
508         handle_xdg_surface_configure,
509 };
510
511 static void
512 handle_xdg_toplevel_configure(void *data, struct xdg_toplevel *xdg_toplevel,
513                               int32_t width, int32_t height,
514                               struct wl_array *states)
515 {
516         struct window *window = static_cast<struct window *>(data);
517         uint32_t *p;
518
519         window->fullscreen = 0;
520         window->maximized = 0;
521
522         // use our own macro as C++ can't typecast from (void *) directly
523         WL_ARRAY_FOR_EACH(p, states, uint32_t *) {
524                 uint32_t state = *p;
525                 switch (state) {
526                 case XDG_TOPLEVEL_STATE_FULLSCREEN:
527                         window->fullscreen = 1;
528                         break;
529                 case XDG_TOPLEVEL_STATE_MAXIMIZED:
530                         window->maximized = 1;
531                         break;
532                 }
533         }
534
535         if (width > 0 && height > 0) {
536                 if (!window->fullscreen && !window->maximized) {
537                         window->init_width = width;
538                         window->init_height = height;
539                 }
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;
545         }
546
547         window->needs_update_buffer = true;
548
549 }
550
551 static void
552 handle_xdg_toplevel_close(void *data, struct xdg_toplevel *xdg_toplevel)
553 {
554         running = 0;
555 }
556
557 static const struct xdg_toplevel_listener xdg_toplevel_listener = {
558         handle_xdg_toplevel_configure,
559         handle_xdg_toplevel_close,
560 };
561
562 static struct window *
563 create_window(struct display *display, int width, int height, const char *app_id)
564 {
565         struct window *window;
566         int i;
567
568         assert(display->wm_base != NULL);
569
570         window = static_cast<struct window *>(calloc(1, sizeof(*window)));
571         if (!window)
572                 return NULL;
573
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);
582
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);
587
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);
592
593                 xdg_toplevel_add_listener(window->xdg_toplevel,
594                                           &xdg_toplevel_listener, window);
595
596                 xdg_toplevel_set_app_id(window->xdg_toplevel, app_id);
597
598                 wl_surface_commit(window->surface);
599                 window->wait_for_configure = true;
600         }
601
602         for (i = 0; i < MAX_BUFFER_ALLOC; i++)
603                 alloc_buffer(window, window->width, window->height);
604
605         return window;
606 }
607
608
609 static void
610 destroy_window(struct window *window)
611 {
612         struct buffer *buffer, *buffer_next;
613
614         if (window->callback)
615                 wl_callback_destroy(window->callback);
616
617         wl_list_for_each_safe(buffer, buffer_next,
618                               &window->buffer_list, buffer_link)
619                 destroy_buffer(buffer);
620
621         if (window->xdg_toplevel)
622                 xdg_toplevel_destroy(window->xdg_toplevel);
623
624         if (window->xdg_surface)
625                 xdg_surface_destroy(window->xdg_surface);
626
627         wl_surface_destroy(window->surface);
628         free(window);
629 }
630
631 static void
632 signal_int(int sig, siginfo_t *si, void *_unused)
633 {
634         running = 0;
635 }
636
637 static struct display *
638 create_display(int argc, char *argv[])
639 {
640         struct display *display;
641
642         display = static_cast<struct display *>(calloc(1, sizeof(*display)));
643         if (display == NULL) {
644                 fprintf(stderr, "out of memory\n");
645                 exit(1);
646         }
647         display->wl_display = wl_display_connect(NULL);
648         assert(display->wl_display);
649
650         display->has_xrgb = false;
651         display->wl_registry = wl_display_get_registry(display->wl_display);
652
653         wl_registry_add_listener(display->wl_registry, &registry_listener, display);
654         wl_display_roundtrip(display->wl_display);
655
656         if (display->shm == NULL) {
657                 fprintf(stderr, "No wl_shm global\n");
658                 return NULL;
659         }
660
661         wl_display_roundtrip(display->wl_display);
662
663         if (!display->has_xrgb) {
664                 fprintf(stderr, "WL_SHM_FORMAT_XRGB32 not available\n");
665                 return NULL;
666         }
667
668         return display;
669 }
670
671 static void
672 destroy_display(struct display *display)
673 {
674         if (display->shm)
675                 wl_shm_destroy(display->shm);
676
677         if (display->wm_base)
678                 xdg_wm_base_destroy(display->wm_base);
679
680         if (display->wl_compositor)
681                 wl_compositor_destroy(display->wl_compositor);
682
683         wl_registry_destroy(display->wl_registry);
684         wl_display_flush(display->wl_display);
685         wl_display_disconnect(display->wl_display);
686         free(display);
687 }
688
689 // stringify the un-quoted string to quoted C string
690 #define xstr(a) str(a)
691 #define str(a) #a
692
693 GstElement* create_pipeline(int* argc, char** argv[])
694 {
695         GError *error = NULL;
696         const char *camera_device = NULL;
697         const char *width_str = NULL;
698         const char *height_str = NULL;
699         int width;
700         int height;
701
702         // pipewire is default.
703         char *v4l2_path = getenv("ENABLE_V4L2_PATH");
704         bool v4l2 = false;
705
706         char pipeline_str[1024];
707
708         camera_device = getenv("DEFAULT_V4L2_DEVICE");
709         if (!camera_device)
710                 camera_device = get_first_camera_device();
711
712         width_str = getenv("DEFAULT_DEVICE_WIDTH");
713         if (!width_str)
714                 width = WINDOW_WIDTH_SIZE;
715         else
716                 width = atoi(width_str);
717
718         height_str = getenv("DEFAULT_DEVICE_HEIGHT");
719         if (!height_str)
720                 height = WINDOW_HEIGHT_SIZE;
721         else
722                 height = atoi(height_str);
723
724         if (v4l2_path == NULL)
725                 v4l2 = false;
726         else if (g_str_equal(v4l2_path, "yes") || g_str_equal(v4l2_path, "true"))
727                 v4l2 = true;
728
729         memset(pipeline_str, 0, sizeof(pipeline_str));
730
731         if (v4l2)
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;
738         }
739         else {
740                 snprintf(pipeline_str, sizeof(pipeline_str), "pipewiresrc ! waylandsink");
741         }
742
743         fprintf(stdout, "Using pipeline: %s\n", pipeline_str);
744
745         GstElement *pipeline = gst_parse_launch(pipeline_str, &error);
746
747         if (error || !pipeline) {
748                 fprintf(stderr, "gstreamer pipeline construction failed!\n");
749                 free(argv);
750                 return NULL;
751         }
752
753         return pipeline;
754 }
755
756 static void
757 run_in_thread(GrpcClient *client)
758 {
759         grpc::Status status = client->Wait();
760 }
761
762 static void
763 app_status_callback(::agl_shell_ipc::AppStateResponse app_response, void *data)
764 {
765         (void) data;
766
767         std::cout << " >> AppStateResponse app_id " <<
768                 app_response.app_id() << ", with state " <<
769                 app_response.state() << std::endl;
770
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) {
774                 // suspend pipeline
775                 std::cout << "Suspending gstreamer pipeline" << std::endl;
776         }
777 }
778
779
780 int main(int argc, char* argv[])
781 {
782         int ret = 0;
783         struct sigaction sa;
784         struct receiver_data receiver_data = {};
785         struct display* display;
786         struct window* window;
787
788         // always start the grpc client
789         GrpcClient *client = new GrpcClient();
790
791         // subscribe to events to be able to suspend/pause the video
792         // feed
793         std::thread th = std::thread(run_in_thread, client);
794         client->AppStatusState(app_status_callback, &receiver_data);
795
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);
800         }
801
802         sa.sa_sigaction = signal_int;
803         sigemptyset(&sa.sa_mask);
804         sa.sa_flags = SA_RESETHAND | SA_SIGINFO;
805         sigaction(SIGINT, &sa, NULL);
806
807         int gargc = 2;
808         char** gargv = static_cast<char**>(calloc(2, sizeof(char*)));
809
810         gargv[0] = strdup(argv[0]);
811         gargv[1] = strdup("--gst-debug-level=2");
812
813         setbuf(stdout, NULL);
814
815         gst_init(&gargc, &gargv);
816
817         receiver_data.pipeline = create_pipeline(&gargc, &gargv);
818
819         if (!receiver_data.pipeline)
820                 return EXIT_FAILURE;
821
822         display = create_display(argc, argv);
823         if (!display)
824                 return -1;
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, my_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 }