Debian Codegraph @

tests/examples/gtk/gtkwaylandsink.c

raw
1 /*
2 * Copyright (C) 2014-2021 Collabora Ltd.
3 * @author George Kiagiadakis <[email protected]>
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
24 static gboolean live = FALSE;
25 static gboolean scrollable = FALSE;
26
27 static GOptionEntry entries[] = {
28 {"live", 'l', 0, G_OPTION_ARG_NONE, &live, "Use a live source", NULL},
29 {"scrollable", 's', 0, G_OPTION_ARG_NONE, &scrollable,
30 "Put the GtkWaylandSink into a GtkScrolledWindow ", NULL},
31 {NULL}
32 };
33
34 typedef struct
35 {
36 GtkWidget *app_widget;
37
38 GstElement *pipeline;
39
40 gchar **argv;
41 gint current_uri; /* index for argv */
42
43 gboolean is_fullscreen;
44 } DemoApp;
45
46 static void
47 on_about_to_finish (GstElement * playbin, DemoApp * d)
48 {
49 if (d->argv[++d->current_uri] == NULL)
50 d->current_uri = 1;
51
52 g_print ("Now playing %s\n", d->argv[d->current_uri]);
53 g_object_set (playbin, "uri", d->argv[d->current_uri], NULL);
54 }
55
56 static void
57 error_cb (GstBus * bus, GstMessage * msg, gpointer user_data)
58 {
59 DemoApp *d = user_data;
60 gchar *debug = NULL;
61 GError *err = NULL;
62
63 gst_message_parse_error (msg, &err, &debug);
64
65 g_print ("Error: %s\n", err->message);
66 g_error_free (err);
67
68 if (debug) {
69 g_print ("Debug details: %s\n", debug);
70 g_free (debug);
71 }
72
73 gst_element_set_state (d->pipeline, GST_STATE_NULL);
74 }
75
76 static gboolean
77 video_widget_button_pressed_cb (GtkWidget * widget,
78 GdkEventButton * eventButton, DemoApp * d)
79 {
80 if (eventButton->type == GDK_2BUTTON_PRESS) {
81 if (d->is_fullscreen) {
82 gtk_window_unfullscreen (GTK_WINDOW (d->app_widget));
83 d->is_fullscreen = FALSE;
84 } else {
85 gtk_window_fullscreen (GTK_WINDOW (d->app_widget));
86 d->is_fullscreen = TRUE;
87 }
88 }
89
90 return FALSE;
91 }
92
93 static void
94 playing_clicked_cb (GtkButton * button, DemoApp * d)
95 {
96 gst_element_set_state (d->pipeline, GST_STATE_PLAYING);
97 }
98
99 static void
100 paused_clicked_cb (GtkButton * button, DemoApp * d)
101 {
102 gst_element_set_state (d->pipeline, GST_STATE_PAUSED);
103 }
104
105 static void
106 ready_clicked_cb (GtkButton * button, DemoApp * d)
107 {
108 gst_element_set_state (d->pipeline, GST_STATE_READY);
109 }
110
111 static void
112 null_clicked_cb (GtkButton * button, DemoApp * d)
113 {
114 gst_element_set_state (d->pipeline, GST_STATE_NULL);
115 }
116
117 static void
118 build_window (DemoApp * d)
119 {
120 GtkBuilder *builder;
121 GstElement *sink;
122 GtkWidget *box;
123 GtkWidget *widget;
124 GError *error = NULL;
125
126 builder = gtk_builder_new ();
127 if (!gtk_builder_add_from_file (builder, "window.ui", &error)) {
128 g_error ("Failed to load window.ui: %s", error->message);
129 g_error_free (error);
130 goto exit;
131 }
132
133 d->app_widget = GTK_WIDGET (gtk_builder_get_object (builder, "window"));
134 g_object_ref (d->app_widget);
135 g_signal_connect (d->app_widget, "destroy", G_CALLBACK (gtk_main_quit), NULL);
136
137 box = GTK_WIDGET (gtk_builder_get_object (builder, "box"));
138 sink = gst_bin_get_by_name (GST_BIN (d->pipeline), "vsink");
139 if (!sink && !g_strcmp0 (G_OBJECT_TYPE_NAME (d->pipeline), "GstPlayBin")) {
140 g_object_get (d->pipeline, "video-sink", &sink, NULL);
141 if (sink && g_strcmp0 (G_OBJECT_TYPE_NAME (sink), "GstGtkWaylandSink") != 0
142 && GST_IS_BIN (sink)) {
143 GstBin *sinkbin = GST_BIN (sink);
144 sink = gst_bin_get_by_name (sinkbin, "vsink");
145 gst_object_unref (sinkbin);
146 }
147 }
148 g_assert (sink);
149
150 g_object_get (sink, "widget", &widget, NULL);
151 if (scrollable) {
152 GtkWidget *scrollable;
153 scrollable = gtk_scrolled_window_new (NULL, NULL);
154
155 gtk_container_add (GTK_CONTAINER (scrollable), widget);
156 g_object_unref (widget);
157 widget = scrollable;
158 }
159
160 gtk_box_pack_start (GTK_BOX (box), widget, TRUE, TRUE, 0);
161 gtk_box_reorder_child (GTK_BOX (box), widget, 0);
162
163 g_signal_connect (widget, "button-press-event",
164 G_CALLBACK (video_widget_button_pressed_cb), d);
165 if (!scrollable)
166 g_object_unref (widget);
167 g_object_unref (sink);
168
169 widget = GTK_WIDGET (gtk_builder_get_object (builder, "button_playing"));
170 g_signal_connect (widget, "clicked", G_CALLBACK (playing_clicked_cb), d);
171
172 widget = GTK_WIDGET (gtk_builder_get_object (builder, "button_paused"));
173 g_signal_connect (widget, "clicked", G_CALLBACK (paused_clicked_cb), d);
174
175 widget = GTK_WIDGET (gtk_builder_get_object (builder, "button_ready"));
176 g_signal_connect (widget, "clicked", G_CALLBACK (ready_clicked_cb), d);
177
178 widget = GTK_WIDGET (gtk_builder_get_object (builder, "button_null"));
179 g_signal_connect (widget, "clicked", G_CALLBACK (null_clicked_cb), d);
180
181 gtk_widget_show_all (d->app_widget);
182
183 exit:
184 g_object_unref (builder);
185 }
186
187 int
188 main (int argc, char **argv)
189 {
190 DemoApp *d;
191 GOptionContext *context;
192 GstBus *bus;
193 GError *error = NULL;
194
195 gtk_init (&argc, &argv);
196 gst_init (&argc, &argv);
197
198 context = g_option_context_new ("- gtkwaylandsink demo");
199 g_option_context_add_main_entries (context, entries, NULL);
200 if (!g_option_context_parse (context, &argc, &argv, &error)) {
201 g_printerr ("option parsing failed: %s\n", error->message);
202 return 1;
203 }
204
205 d = g_slice_new0 (DemoApp);
206
207 if (argc > 1) {
208 d->argv = argv;
209 d->current_uri = 1;
210
211 d->pipeline =
212 gst_parse_launch ("playbin video-sink=\"gtkwaylandsink name=vsink\"",
213 NULL);
214 g_object_set (d->pipeline, "uri", argv[d->current_uri], NULL);
215
216 /* enable looping */
217 g_signal_connect (d->pipeline, "about-to-finish",
218 G_CALLBACK (on_about_to_finish), d);
219 } else {
220 if (live) {
221 d->pipeline = gst_parse_launch ("videotestsrc pattern=18 "
222 "background-color=0xFF0062FF is-live=true ! navigationtest ! "
223 "videoconvert ! gtkwaylandsink name=vsink", NULL);
224 } else {
225 d->pipeline = gst_parse_launch ("videotestsrc pattern=18 "
226 "background-color=0xFF0062FF ! navigationtest ! videoconvert ! "
227 "gtkwaylandsink name=vsink", NULL);
228 }
229 }
230
231 build_window (d);
232
233 bus = gst_pipeline_get_bus (GST_PIPELINE (d->pipeline));
234 gst_bus_add_signal_watch (bus);
235 g_signal_connect (bus, "message::error", G_CALLBACK (error_cb), d);
236 gst_object_unref (bus);
237
238 gst_element_set_state (d->pipeline, GST_STATE_PLAYING);
239
240 gtk_main ();
241
242 gst_element_set_state (d->pipeline, GST_STATE_NULL);
243 gst_object_unref (d->pipeline);
244 g_object_unref (d->app_widget);
245 g_slice_free (DemoApp, d);
246
247 return 0;
248 }