1 /* sample application for testing decodebin3
2 *
3 * Copyright (C) 2015 Centricular Ltd
4 * @author: Edward Hervey <edward@centricular.com>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
15 *
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20 */
21
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25
26 #include <glib.h>
27 #include <glib-object.h>
28 #include <glib/gprintf.h>
29 #include <gst/gst.h>
30
31 /* Global structure */
32
33 typedef struct _MyDataStruct
34 {
35 GMainLoop *mainloop;
36 GstElement *pipeline;
37 GstBus *demux_bus;
38
39 GstElement *decodebin;
40
41 GstElement *src;
42 GList *other_src;
43 GstElement *playsink;
44
45 /* Current collection */
46 GstStreamCollection *collection;
47 guint notify_id;
48
49 guint current_audio;
50 guint current_video;
51 guint current_text;
52
53 glong timeout_id;
54 } MyDataStruct;
55
56 static void
print_tag_foreach(const GstTagList * tags,const gchar * tag,gpointer user_data)57 print_tag_foreach (const GstTagList * tags, const gchar * tag,
58 gpointer user_data)
59 {
60 GValue val = { 0, };
61 gchar *str;
62 gint depth = GPOINTER_TO_INT (user_data);
63
64 if (!gst_tag_list_copy_value (&val, tags, tag))
65 return;
66
67 if (G_VALUE_HOLDS_STRING (&val))
68 str = g_value_dup_string (&val);
69 else
70 str = gst_value_serialize (&val);
71
72 g_print ("%*s%s: %s\n", 2 * depth, " ", gst_tag_get_nick (tag), str);
73 g_free (str);
74
75 g_value_unset (&val);
76 }
77
78 static void
dump_collection(GstStreamCollection * collection)79 dump_collection (GstStreamCollection * collection)
80 {
81 guint i;
82 GstTagList *tags;
83 GstCaps *caps;
84
85 for (i = 0; i < gst_stream_collection_get_size (collection); i++) {
86 GstStream *stream = gst_stream_collection_get_stream (collection, i);
87 g_print (" Stream %u type %s flags 0x%x\n", i,
88 gst_stream_type_get_name (gst_stream_get_stream_type (stream)),
89 gst_stream_get_stream_flags (stream));
90 g_print (" ID: %s\n", gst_stream_get_stream_id (stream));
91
92 caps = gst_stream_get_caps (stream);
93 if (caps) {
94 gchar *caps_str = gst_caps_to_string (caps);
95 g_print (" caps: %s\n", caps_str);
96 g_free (caps_str);
97 gst_caps_unref (caps);
98 }
99
100 tags = gst_stream_get_tags (stream);
101 if (tags) {
102 g_print (" tags:\n");
103 gst_tag_list_foreach (tags, print_tag_foreach, GUINT_TO_POINTER (3));
104 gst_tag_list_unref (tags);
105 }
106 }
107 }
108
109 static gboolean
switch_streams(MyDataStruct * data)110 switch_streams (MyDataStruct * data)
111 {
112 guint i, nb_streams;
113 gint nb_video = 0, nb_audio = 0, nb_text = 0;
114 GstStream *videos[256], *audios[256], *texts[256];
115 GList *streams = NULL;
116 GstEvent *ev;
117
118 g_print ("Switching Streams...\n");
119
120 /* Calculate the number of streams of each type */
121 nb_streams = gst_stream_collection_get_size (data->collection);
122 for (i = 0; i < nb_streams; i++) {
123 GstStream *stream = gst_stream_collection_get_stream (data->collection, i);
124 GstStreamType stype = gst_stream_get_stream_type (stream);
125 if (stype == GST_STREAM_TYPE_VIDEO) {
126 videos[nb_video] = stream;
127 nb_video += 1;
128 } else if (stype == GST_STREAM_TYPE_AUDIO) {
129 audios[nb_audio] = stream;
130 nb_audio += 1;
131 } else if (stype == GST_STREAM_TYPE_TEXT) {
132 texts[nb_text] = stream;
133 nb_text += 1;
134 }
135 }
136
137 if (nb_video) {
138 data->current_video = (data->current_video + 1) % nb_video;
139 streams =
140 g_list_append (streams,
141 (gchar *) gst_stream_get_stream_id (videos[data->current_video]));
142 g_print (" Selecting video channel #%d : %s\n", data->current_video,
143 gst_stream_get_stream_id (videos[data->current_video]));
144 }
145 if (nb_audio) {
146 data->current_audio = (data->current_audio + 1) % nb_audio;
147 streams =
148 g_list_append (streams,
149 (gchar *) gst_stream_get_stream_id (audios[data->current_audio]));
150 g_print (" Selecting audio channel #%d : %s\n", data->current_audio,
151 gst_stream_get_stream_id (audios[data->current_audio]));
152 }
153 if (nb_text) {
154 data->current_text = (data->current_text + 1) % nb_text;
155 streams =
156 g_list_append (streams,
157 (gchar *) gst_stream_get_stream_id (texts[data->current_text]));
158 g_print (" Selecting text channel #%d : %s\n", data->current_text,
159 gst_stream_get_stream_id (texts[data->current_text]));
160 }
161
162 ev = gst_event_new_select_streams (streams);
163 gst_element_send_event (data->pipeline, ev);
164
165 return G_SOURCE_CONTINUE;
166 }
167
168 static void
stream_notify_cb(GstStreamCollection * collection,GstStream * stream,GParamSpec * pspec,guint * val)169 stream_notify_cb (GstStreamCollection * collection, GstStream * stream,
170 GParamSpec * pspec, guint * val)
171 {
172 g_print ("Got stream-notify from stream %s for %s (collection %p)\n",
173 stream->stream_id, pspec->name, collection);
174 if (g_str_equal (pspec->name, "caps")) {
175 GstCaps *caps = gst_stream_get_caps (stream);
176 gchar *caps_str = gst_caps_to_string (caps);
177 g_print (" New caps: %s\n", caps_str);
178 g_free (caps_str);
179 gst_caps_unref (caps);
180 }
181 }
182
183 static GstBusSyncReply
_on_bus_message(GstBus * bus,GstMessage * message,MyDataStruct * data)184 _on_bus_message (GstBus * bus, GstMessage * message, MyDataStruct * data)
185 {
186 GstObject *src = GST_MESSAGE_SRC (message);
187 switch (GST_MESSAGE_TYPE (message)) {
188 case GST_MESSAGE_ERROR:{
189 GError *err = NULL;
190 gchar *name = gst_object_get_path_string (GST_MESSAGE_SRC (message));
191 gst_message_parse_error (message, &err, NULL);
192
193 g_printerr ("ERROR: from element %s: %s\n", name, err->message);
194 g_error_free (err);
195 g_free (name);
196
197 g_printf ("Stopping\n");
198 g_main_loop_quit (data->mainloop);
199 break;
200 }
201 case GST_MESSAGE_EOS:
202 g_printf ("EOS ! Stopping \n");
203 g_main_loop_quit (data->mainloop);
204 break;
205 case GST_MESSAGE_STREAM_COLLECTION:
206 {
207 GstStreamCollection *collection = NULL;
208 gst_message_parse_stream_collection (message, &collection);
209 if (collection) {
210 g_printf ("Got a collection from %s:\n",
211 src ? GST_OBJECT_NAME (src) : "Unknown");
212 dump_collection (collection);
213 if (data->collection && data->notify_id) {
214 g_signal_handler_disconnect (data->collection, data->notify_id);
215 data->notify_id = 0;
216 }
217 gst_object_replace ((GstObject **) & data->collection,
218 (GstObject *) collection);
219 if (data->collection) {
220 data->notify_id =
221 g_signal_connect (data->collection, "stream-notify",
222 (GCallback) stream_notify_cb, data);
223 }
224 if (data->timeout_id == 0)
225 /* In 5s try to change streams */
226 data->timeout_id =
227 g_timeout_add_seconds (5, (GSourceFunc) switch_streams, data);
228 gst_object_unref (collection);
229 }
230 break;
231 }
232 default:
233 break;
234 }
235
236 return GST_BUS_PASS;
237 }
238
239 static void
decodebin_pad_added_cb(GstElement * dbin,GstPad * pad,MyDataStruct * data)240 decodebin_pad_added_cb (GstElement * dbin, GstPad * pad, MyDataStruct * data)
241 {
242 gchar *pad_name = gst_pad_get_name (pad);
243 const gchar *sink_pad = NULL;
244
245 GST_DEBUG_OBJECT (pad, "New pad ! Link to playsink !");
246 if (!g_ascii_strncasecmp (pad_name, "video_", 6))
247 sink_pad = "video_sink";
248 else if (!g_ascii_strncasecmp (pad_name, "audio_", 6))
249 sink_pad = "audio_sink";
250 else if (!g_ascii_strncasecmp (pad_name, "text_", 5))
251 sink_pad = "text_sink";
252 else
253 GST_WARNING_OBJECT (pad, "non audio/video/text pad");
254
255 g_free (pad_name);
256
257 if (sink_pad) {
258 GstPad *playsink_pad;
259
260 playsink_pad = gst_element_request_pad_simple (data->playsink, sink_pad);
261 if (playsink_pad)
262 gst_pad_link (pad, playsink_pad);
263 }
264 }
265
266 int
main(int argc,gchar ** argv)267 main (int argc, gchar ** argv)
268 {
269 GError *error = NULL;
270 GstBus *bus;
271 MyDataStruct *data;
272 int i;
273
274 gst_init (&argc, &argv);
275
276 data = g_new0 (MyDataStruct, 1);
277
278 if (argc < 2) {
279 g_print ("Usage: decodebin3 URI\n");
280 return 1;
281 }
282
283 data->pipeline = gst_pipeline_new ("pipeline");
284 data->decodebin = gst_element_factory_make ("decodebin3", NULL);
285
286 data->src =
287 gst_element_make_from_uri (GST_URI_SRC, argv[1], "source", &error);
288 if (error) {
289 g_printf ("pipeline could not be constructed: %s\n", error->message);
290 g_error_free (error);
291 return 1;
292 }
293 data->playsink = gst_element_factory_make ("playsink", NULL);
294
295 #if 0
296 {
297 GstElement *sink = gst_element_factory_make ("fakesink", NULL);
298 g_object_set (sink, "sync", FALSE, NULL);
299 g_object_set (data->playsink, "video-sink", sink, NULL);
300
301 sink = gst_element_factory_make ("fakesink", NULL);
302 g_object_set (sink, "sync", FALSE, NULL);
303 g_object_set (data->playsink, "audio-sink", sink, NULL);
304 }
305 #endif
306
307 gst_bin_add_many ((GstBin *) data->pipeline, data->src, data->decodebin,
308 data->playsink, NULL);
309 if (!gst_element_link (data->src, (GstElement *) data->decodebin)) {
310 g_printf ("Could not link source to demuxer\n");
311 return 1;
312 }
313
314 /* Handle other inputs if specified */
315 if (argc > 2) {
316 for (i = 2; i < argc; i++) {
317 GstElement *new_src =
318 gst_element_make_from_uri (GST_URI_SRC, argv[i], NULL, &error);
319 GstPad *src_pad, *sink_pad;
320 if (error) {
321 g_printf ("pipeline could not be constructed: %s\n", error->message);
322 g_error_free (error);
323 return 1;
324 }
325 data->other_src = g_list_append (data->other_src, new_src);
326 gst_bin_add ((GstBin *) data->pipeline, new_src);
327 src_pad = gst_element_get_static_pad (new_src, "src");
328 sink_pad = gst_element_request_pad_simple (data->decodebin, "sink_%u");
329 if (gst_pad_link (src_pad, sink_pad) != GST_PAD_LINK_OK) {
330 g_printf ("Could not link new source to decodebin : %s\n", argv[i]);
331 return 1;
332 }
333 }
334 }
335
336
337 g_signal_connect (data->decodebin, "pad-added",
338 (GCallback) decodebin_pad_added_cb, data);
339 data->mainloop = g_main_loop_new (NULL, FALSE);
340
341 /* Put a bus handler */
342 bus = gst_pipeline_get_bus (GST_PIPELINE (data->pipeline));
343 gst_bus_set_sync_handler (bus, (GstBusSyncHandler) _on_bus_message, data,
344 NULL);
345
346 /* Start pipeline */
347 gst_element_set_state (data->pipeline, GST_STATE_PLAYING);
348 g_main_loop_run (data->mainloop);
349
350 gst_element_set_state (data->pipeline, GST_STATE_NULL);
351
352 gst_object_unref (data->pipeline);
353 gst_object_unref (bus);
354
355 return 0;
356 }
357