1 /*
2  * Copyright (C) 2014-2015 Collabora Ltd.
3  *   @author George Kiagiadakis <george.kiagiadakis@collabora.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20 
21 #include <gst/gst.h>
22 #include <gtk/gtk.h>
23 #include <gdk/gdk.h>
24 
25 #ifdef GDK_WINDOWING_WAYLAND
26 #include <gdk/gdkwayland.h>
27 #else
28 #error "Wayland is not supported in GTK+"
29 #endif
30 
31 #include <gst/video/videooverlay.h>
32 #include <gst/wayland/wayland.h>
33 
34 
35 static gboolean live = FALSE;
36 
37 static GOptionEntry entries[] = {
38   {"live", 'l', 0, G_OPTION_ARG_NONE, &live, "Use a live source", NULL},
39   {NULL}
40 };
41 
42 typedef struct
43 {
44   GtkWidget *app_widget;
45   GtkWidget *video_widget;
46 
47   GstElement *pipeline;
48   GstVideoOverlay *overlay;
49 
50   gchar **argv;
51   gint current_uri;             /* index for argv */
52 } DemoApp;
53 
54 static void
on_about_to_finish(GstElement * playbin,DemoApp * d)55 on_about_to_finish (GstElement * playbin, DemoApp * d)
56 {
57   if (d->argv[++d->current_uri] == NULL)
58     d->current_uri = 1;
59 
60   g_print ("Now playing %s\n", d->argv[d->current_uri]);
61   g_object_set (playbin, "uri", d->argv[d->current_uri], NULL);
62 }
63 
64 static void
error_cb(GstBus * bus,GstMessage * msg,gpointer user_data)65 error_cb (GstBus * bus, GstMessage * msg, gpointer user_data)
66 {
67   DemoApp *d = user_data;
68   gchar *debug = NULL;
69   GError *err = NULL;
70 
71   gst_message_parse_error (msg, &err, &debug);
72 
73   g_print ("Error: %s\n", err->message);
74   g_error_free (err);
75 
76   if (debug) {
77     g_print ("Debug details: %s\n", debug);
78     g_free (debug);
79   }
80 
81   gst_element_set_state (d->pipeline, GST_STATE_NULL);
82 }
83 
84 static GstBusSyncReply
bus_sync_handler(GstBus * bus,GstMessage * message,gpointer user_data)85 bus_sync_handler (GstBus * bus, GstMessage * message, gpointer user_data)
86 {
87   DemoApp *d = user_data;
88 
89   if (gst_is_wayland_display_handle_need_context_message (message)) {
90     GstContext *context;
91     GdkDisplay *display;
92     struct wl_display *display_handle;
93 
94     display = gtk_widget_get_display (d->video_widget);
95     display_handle = gdk_wayland_display_get_wl_display (display);
96     context = gst_wayland_display_handle_context_new (display_handle);
97     gst_element_set_context (GST_ELEMENT (GST_MESSAGE_SRC (message)), context);
98 
99     goto drop;
100   } else if (gst_is_video_overlay_prepare_window_handle_message (message)) {
101     GtkAllocation allocation;
102     GdkWindow *window;
103     struct wl_surface *window_handle;
104 
105     /* GST_MESSAGE_SRC (message) will be the overlay object that we have to
106      * use. This may be waylandsink, but it may also be playbin. In the latter
107      * case, we must make sure to use playbin instead of waylandsink, because
108      * playbin resets the window handle and render_rectangle after restarting
109      * playback and the actual window size is lost */
110     d->overlay = GST_VIDEO_OVERLAY (GST_MESSAGE_SRC (message));
111 
112     gtk_widget_get_allocation (d->video_widget, &allocation);
113     window = gtk_widget_get_window (d->video_widget);
114     window_handle = gdk_wayland_window_get_wl_surface (window);
115 
116     g_print ("setting window handle and size (%d x %d)\n",
117         allocation.width, allocation.height);
118 
119     gst_video_overlay_set_window_handle (d->overlay, (guintptr) window_handle);
120     gst_video_overlay_set_render_rectangle (d->overlay, allocation.x,
121         allocation.y, allocation.width, allocation.height);
122 
123     goto drop;
124   }
125 
126   return GST_BUS_PASS;
127 
128 drop:
129   gst_message_unref (message);
130   return GST_BUS_DROP;
131 }
132 
133 /* We use the "draw" callback to change the size of the sink
134  * because the "configure-event" is only sent to top-level widgets. */
135 static gboolean
video_widget_draw_cb(GtkWidget * widget,cairo_t * cr,gpointer user_data)136 video_widget_draw_cb (GtkWidget * widget, cairo_t * cr, gpointer user_data)
137 {
138   DemoApp *d = user_data;
139   GtkAllocation allocation;
140 
141   gtk_widget_get_allocation (widget, &allocation);
142 
143   g_print ("draw_cb x %d, y %d, w %d, h %d\n",
144       allocation.x, allocation.y, allocation.width, allocation.height);
145 
146   if (d->overlay) {
147     gst_video_overlay_set_render_rectangle (d->overlay, allocation.x,
148         allocation.y, allocation.width, allocation.height);
149   }
150 
151   /* There is no need to call gst_video_overlay_expose().
152    * The wayland compositor can always re-draw the window
153    * based on its last contents if necessary */
154 
155   return FALSE;
156 }
157 
158 static void
playing_clicked_cb(GtkButton * button,DemoApp * d)159 playing_clicked_cb (GtkButton * button, DemoApp * d)
160 {
161   gst_element_set_state (d->pipeline, GST_STATE_PLAYING);
162 }
163 
164 static void
paused_clicked_cb(GtkButton * button,DemoApp * d)165 paused_clicked_cb (GtkButton * button, DemoApp * d)
166 {
167   gst_element_set_state (d->pipeline, GST_STATE_PAUSED);
168 }
169 
170 static void
ready_clicked_cb(GtkButton * button,DemoApp * d)171 ready_clicked_cb (GtkButton * button, DemoApp * d)
172 {
173   gst_element_set_state (d->pipeline, GST_STATE_READY);
174 }
175 
176 static void
null_clicked_cb(GtkButton * button,DemoApp * d)177 null_clicked_cb (GtkButton * button, DemoApp * d)
178 {
179   gst_element_set_state (d->pipeline, GST_STATE_NULL);
180 }
181 
182 static void
build_window(DemoApp * d)183 build_window (DemoApp * d)
184 {
185   GtkBuilder *builder;
186   GtkWidget *button;
187   GError *error = NULL;
188 
189   builder = gtk_builder_new ();
190   if (!gtk_builder_add_from_file (builder, "window.ui", &error)) {
191     g_error ("Failed to load window.ui: %s", error->message);
192     g_error_free (error);
193     goto exit;
194   }
195 
196   d->app_widget = GTK_WIDGET (gtk_builder_get_object (builder, "window"));
197   g_object_ref (d->app_widget);
198   g_signal_connect (d->app_widget, "destroy", G_CALLBACK (gtk_main_quit), NULL);
199 
200   d->video_widget = GTK_WIDGET (gtk_builder_get_object (builder, "videoarea"));
201   g_signal_connect (d->video_widget, "draw",
202       G_CALLBACK (video_widget_draw_cb), d);
203 
204   button = GTK_WIDGET (gtk_builder_get_object (builder, "button_playing"));
205   g_signal_connect (button, "clicked", G_CALLBACK (playing_clicked_cb), d);
206 
207   button = GTK_WIDGET (gtk_builder_get_object (builder, "button_paused"));
208   g_signal_connect (button, "clicked", G_CALLBACK (paused_clicked_cb), d);
209 
210   button = GTK_WIDGET (gtk_builder_get_object (builder, "button_ready"));
211   g_signal_connect (button, "clicked", G_CALLBACK (ready_clicked_cb), d);
212 
213   button = GTK_WIDGET (gtk_builder_get_object (builder, "button_null"));
214   g_signal_connect (button, "clicked", G_CALLBACK (null_clicked_cb), d);
215 
216   gtk_widget_show_all (d->app_widget);
217 
218 exit:
219   g_object_unref (builder);
220 }
221 
222 int
main(int argc,char ** argv)223 main (int argc, char **argv)
224 {
225   DemoApp *d;
226   GOptionContext *context;
227   GstBus *bus;
228   GError *error = NULL;
229 
230   gtk_init (&argc, &argv);
231   gst_init (&argc, &argv);
232 
233   context = g_option_context_new ("- waylandsink gtk demo");
234   g_option_context_add_main_entries (context, entries, NULL);
235   if (!g_option_context_parse (context, &argc, &argv, &error)) {
236     g_printerr ("option parsing failed: %s\n", error->message);
237     return 1;
238   }
239 
240   d = g_slice_new0 (DemoApp);
241   build_window (d);
242 
243   if (argc > 1) {
244     d->argv = argv;
245     d->current_uri = 1;
246 
247     d->pipeline = gst_parse_launch ("playbin video-sink=waylandsink", NULL);
248     g_object_set (d->pipeline, "uri", argv[d->current_uri], NULL);
249 
250     /* enable looping */
251     g_signal_connect (d->pipeline, "about-to-finish",
252         G_CALLBACK (on_about_to_finish), d);
253   } else {
254     if (live) {
255       d->pipeline = gst_parse_launch ("videotestsrc pattern=18 "
256           "background-color=0x000062FF is-live=true ! waylandsink", NULL);
257     } else {
258       d->pipeline = gst_parse_launch ("videotestsrc pattern=18 "
259           "background-color=0x000062FF ! waylandsink", NULL);
260     }
261   }
262 
263   bus = gst_pipeline_get_bus (GST_PIPELINE (d->pipeline));
264   gst_bus_add_signal_watch (bus);
265   g_signal_connect (bus, "message::error", G_CALLBACK (error_cb), d);
266   gst_bus_set_sync_handler (bus, bus_sync_handler, d, NULL);
267   gst_object_unref (bus);
268 
269   gst_element_set_state (d->pipeline, GST_STATE_PLAYING);
270 
271   gtk_main ();
272 
273   gst_element_set_state (d->pipeline, GST_STATE_NULL);
274   gst_object_unref (d->pipeline);
275   g_object_unref (d->app_widget);
276   g_slice_free (DemoApp, d);
277 
278   return 0;
279 }
280