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