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