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 gst_context_unref (context);
99
100 goto drop;
101 } else if (gst_is_video_overlay_prepare_window_handle_message (message)) {
102 GtkAllocation allocation;
103 GdkWindow *window;
104 struct wl_surface *window_handle;
105
106 /* GST_MESSAGE_SRC (message) will be the overlay object that we have to
107 * use. This may be waylandsink, but it may also be playbin. In the latter
108 * case, we must make sure to use playbin instead of waylandsink, because
109 * playbin resets the window handle and render_rectangle after restarting
110 * playback and the actual window size is lost */
111 d->overlay = GST_VIDEO_OVERLAY (GST_MESSAGE_SRC (message));
112
113 gtk_widget_get_allocation (d->video_widget, &allocation);
114 window = gtk_widget_get_window (d->video_widget);
115 window_handle = gdk_wayland_window_get_wl_surface (window);
116
117 g_print ("setting window handle and size (%d x %d)\n",
118 allocation.width, allocation.height);
119
120 gst_video_overlay_set_window_handle (d->overlay, (guintptr) window_handle);
121 gst_video_overlay_set_render_rectangle (d->overlay, allocation.x,
122 allocation.y, allocation.width, allocation.height);
123
124 goto drop;
125 }
126
127 return GST_BUS_PASS;
128
129 drop:
130 gst_message_unref (message);
131 return GST_BUS_DROP;
132 }
133
134 /* We use the "draw" callback to change the size of the sink
135 * because the "configure-event" is only sent to top-level widgets. */
136 static gboolean
video_widget_draw_cb(GtkWidget * widget,cairo_t * cr,gpointer user_data)137 video_widget_draw_cb (GtkWidget * widget, cairo_t * cr, gpointer user_data)
138 {
139 DemoApp *d = user_data;
140 GtkAllocation allocation;
141
142 gtk_widget_get_allocation (widget, &allocation);
143
144 g_print ("draw_cb x %d, y %d, w %d, h %d\n",
145 allocation.x, allocation.y, allocation.width, allocation.height);
146
147 if (d->overlay) {
148 gst_video_overlay_set_render_rectangle (d->overlay, allocation.x,
149 allocation.y, allocation.width, allocation.height);
150 }
151
152 /* There is no need to call gst_video_overlay_expose().
153 * The wayland compositor can always re-draw the window
154 * based on its last contents if necessary */
155
156 return FALSE;
157 }
158
159 static void
playing_clicked_cb(GtkButton * button,DemoApp * d)160 playing_clicked_cb (GtkButton * button, DemoApp * d)
161 {
162 gst_element_set_state (d->pipeline, GST_STATE_PLAYING);
163 }
164
165 static void
paused_clicked_cb(GtkButton * button,DemoApp * d)166 paused_clicked_cb (GtkButton * button, DemoApp * d)
167 {
168 gst_element_set_state (d->pipeline, GST_STATE_PAUSED);
169 }
170
171 static void
ready_clicked_cb(GtkButton * button,DemoApp * d)172 ready_clicked_cb (GtkButton * button, DemoApp * d)
173 {
174 gst_element_set_state (d->pipeline, GST_STATE_READY);
175 }
176
177 static void
null_clicked_cb(GtkButton * button,DemoApp * d)178 null_clicked_cb (GtkButton * button, DemoApp * d)
179 {
180 gst_element_set_state (d->pipeline, GST_STATE_NULL);
181 }
182
183 static void
build_window(DemoApp * d)184 build_window (DemoApp * d)
185 {
186 GtkBuilder *builder;
187 GtkWidget *button;
188 GError *error = NULL;
189
190 builder = gtk_builder_new ();
191 if (!gtk_builder_add_from_file (builder, "window.ui", &error)) {
192 g_error ("Failed to load window.ui: %s", error->message);
193 g_error_free (error);
194 goto exit;
195 }
196
197 d->app_widget = GTK_WIDGET (gtk_builder_get_object (builder, "window"));
198 g_object_ref (d->app_widget);
199 g_signal_connect (d->app_widget, "destroy", G_CALLBACK (gtk_main_quit), NULL);
200
201 d->video_widget = GTK_WIDGET (gtk_builder_get_object (builder, "videoarea"));
202 g_signal_connect (d->video_widget, "draw",
203 G_CALLBACK (video_widget_draw_cb), d);
204
205 button = GTK_WIDGET (gtk_builder_get_object (builder, "button_playing"));
206 g_signal_connect (button, "clicked", G_CALLBACK (playing_clicked_cb), d);
207
208 button = GTK_WIDGET (gtk_builder_get_object (builder, "button_paused"));
209 g_signal_connect (button, "clicked", G_CALLBACK (paused_clicked_cb), d);
210
211 button = GTK_WIDGET (gtk_builder_get_object (builder, "button_ready"));
212 g_signal_connect (button, "clicked", G_CALLBACK (ready_clicked_cb), d);
213
214 button = GTK_WIDGET (gtk_builder_get_object (builder, "button_null"));
215 g_signal_connect (button, "clicked", G_CALLBACK (null_clicked_cb), d);
216
217 gtk_widget_show_all (d->app_widget);
218
219 exit:
220 g_object_unref (builder);
221 }
222
223 int
main(int argc,char ** argv)224 main (int argc, char **argv)
225 {
226 DemoApp *d;
227 GOptionContext *context;
228 GstBus *bus;
229 GError *error = NULL;
230
231 gtk_init (&argc, &argv);
232 gst_init (&argc, &argv);
233
234 context = g_option_context_new ("- waylandsink gtk demo");
235 g_option_context_add_main_entries (context, entries, NULL);
236 if (!g_option_context_parse (context, &argc, &argv, &error)) {
237 g_printerr ("option parsing failed: %s\n", error->message);
238 return 1;
239 }
240
241 d = g_slice_new0 (DemoApp);
242 build_window (d);
243
244 if (argc > 1) {
245 d->argv = argv;
246 d->current_uri = 1;
247
248 d->pipeline = gst_parse_launch ("playbin video-sink=waylandsink", NULL);
249 g_object_set (d->pipeline, "uri", argv[d->current_uri], NULL);
250
251 /* enable looping */
252 g_signal_connect (d->pipeline, "about-to-finish",
253 G_CALLBACK (on_about_to_finish), d);
254 } else {
255 if (live) {
256 d->pipeline = gst_parse_launch ("videotestsrc pattern=18 "
257 "background-color=0xFF0062FF is-live=true ! waylandsink", NULL);
258 } else {
259 d->pipeline = gst_parse_launch ("videotestsrc pattern=18 "
260 "background-color=0xFF0062FF ! waylandsink", NULL);
261 }
262 }
263
264 bus = gst_pipeline_get_bus (GST_PIPELINE (d->pipeline));
265 gst_bus_add_signal_watch (bus);
266 g_signal_connect (bus, "message::error", G_CALLBACK (error_cb), d);
267 gst_bus_set_sync_handler (bus, bus_sync_handler, d, NULL);
268 gst_object_unref (bus);
269
270 gst_element_set_state (d->pipeline, GST_STATE_PLAYING);
271
272 gtk_main ();
273
274 gst_element_set_state (d->pipeline, GST_STATE_NULL);
275 gst_object_unref (d->pipeline);
276 g_object_unref (d->app_widget);
277 g_slice_free (DemoApp, d);
278
279 return 0;
280 }
281