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