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