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