1 /* sample application for testing decodebin3
3 * Copyright (C) 2015 Centricular Ltd
4 * @author: Edward Hervey <edward@centricular.com>
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.
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.
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.
27 #include <glib-object.h>
28 #include <glib/gprintf.h>
31 /* Global structure */
33 typedef struct _MyDataStruct
39 GstElement *decodebin;
45 /* Current collection */
46 GstStreamCollection *collection;
57 print_tag_foreach (const GstTagList * tags, const gchar * tag,
62 gint depth = GPOINTER_TO_INT (user_data);
64 if (!gst_tag_list_copy_value (&val, tags, tag))
67 if (G_VALUE_HOLDS_STRING (&val))
68 str = g_value_dup_string (&val);
70 str = gst_value_serialize (&val);
72 g_print ("%*s%s: %s\n", 2 * depth, " ", gst_tag_get_nick (tag), str);
79 dump_collection (GstStreamCollection * collection)
82 const GstTagList *tags;
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));
92 caps = gst_stream_get_caps (stream);
94 gchar *caps_str = gst_caps_to_string (caps);
95 g_print (" caps: %s\n", caps_str);
99 tags = gst_stream_get_tags (stream);
101 g_print (" tags:\n");
102 gst_tag_list_foreach (tags, print_tag_foreach, GUINT_TO_POINTER (3));
108 switch_streams (MyDataStruct * data)
111 gint nb_video = 0, nb_audio = 0, nb_text = 0;
112 GstStream *videos[256], *audios[256], *texts[256];
113 GList *streams = NULL;
116 g_print ("Switching Streams...\n");
118 /* Calculate the number of streams of each type */
119 nb_streams = gst_stream_collection_get_size (data->collection);
120 for (i = 0; i < nb_streams; i++) {
121 GstStream *stream = gst_stream_collection_get_stream (data->collection, i);
122 GstStreamType stype = gst_stream_get_stream_type (stream);
123 if (stype == GST_STREAM_TYPE_VIDEO) {
124 videos[nb_video] = stream;
126 } else if (stype == GST_STREAM_TYPE_AUDIO) {
127 audios[nb_audio] = stream;
129 } else if (stype == GST_STREAM_TYPE_TEXT) {
130 texts[nb_text] = stream;
136 data->current_video = (data->current_video + 1) % nb_video;
138 g_list_append (streams,
139 (gchar *) gst_stream_get_stream_id (videos[data->current_video]));
140 g_print (" Selecting video channel #%d : %s\n", data->current_video,
141 gst_stream_get_stream_id (videos[data->current_video]));
144 data->current_audio = (data->current_audio + 1) % nb_audio;
146 g_list_append (streams,
147 (gchar *) gst_stream_get_stream_id (audios[data->current_audio]));
148 g_print (" Selecting audio channel #%d : %s\n", data->current_audio,
149 gst_stream_get_stream_id (audios[data->current_audio]));
152 data->current_text = (data->current_text + 1) % nb_text;
154 g_list_append (streams,
155 (gchar *) gst_stream_get_stream_id (texts[data->current_text]));
156 g_print (" Selecting text channel #%d : %s\n", data->current_text,
157 gst_stream_get_stream_id (texts[data->current_text]));
160 ev = gst_event_new_select_streams (streams);
161 gst_element_send_event (data->pipeline, ev);
163 return G_SOURCE_CONTINUE;
167 stream_notify_cb (GstStreamCollection * collection, GstStream * stream,
168 GParamSpec * pspec, guint * val)
170 g_print ("Got stream-notify from stream %s for %s (collection %p)\n",
171 stream->stream_id, pspec->name, collection);
172 if (g_str_equal (pspec->name, "caps")) {
173 GstCaps *caps = gst_stream_get_caps (stream);
174 gchar *caps_str = gst_caps_to_string (caps);
175 g_print (" New caps: %s\n", caps_str);
177 gst_caps_unref (caps);
181 static GstBusSyncReply
182 _on_bus_message (GstBus * bus, GstMessage * message, MyDataStruct * data)
184 GstObject *src = GST_MESSAGE_SRC (message);
185 switch (GST_MESSAGE_TYPE (message)) {
186 case GST_MESSAGE_ERROR:{
188 gchar *name = gst_object_get_path_string (GST_MESSAGE_SRC (message));
189 gst_message_parse_error (message, &err, NULL);
191 g_printerr ("ERROR: from element %s: %s\n", name, err->message);
195 g_printf ("Stopping\n");
196 g_main_loop_quit (data->mainloop);
199 case GST_MESSAGE_EOS:
200 g_printf ("EOS ! Stopping \n");
201 g_main_loop_quit (data->mainloop);
203 case GST_MESSAGE_STREAM_COLLECTION:
205 GstStreamCollection *collection = NULL;
206 gst_message_parse_stream_collection (message, &collection);
208 g_printf ("Got a collection from %s:\n",
209 src ? GST_OBJECT_NAME (src) : "Unknown");
210 dump_collection (collection);
211 if (data->collection && data->notify_id) {
212 g_signal_handler_disconnect (data->collection, data->notify_id);
215 gst_object_replace ((GstObject **) & data->collection,
216 (GstObject *) collection);
217 if (data->collection) {
219 g_signal_connect (data->collection, "stream-notify",
220 (GCallback) stream_notify_cb, data);
222 if (data->timeout_id == 0)
223 /* In 5s try to change streams */
225 g_timeout_add_seconds (5, (GSourceFunc) switch_streams, data);
226 gst_object_unref (collection);
238 decodebin_pad_added_cb (GstElement * dbin, GstPad * pad, MyDataStruct * data)
240 gchar *pad_name = gst_pad_get_name (pad);
241 const gchar *sink_pad = NULL;
243 GST_DEBUG_OBJECT (pad, "New pad ! Link to playsink !");
244 if (!g_ascii_strncasecmp (pad_name, "video_", 6))
245 sink_pad = "video_sink";
246 else if (!g_ascii_strncasecmp (pad_name, "audio_", 6))
247 sink_pad = "audio_sink";
248 else if (!g_ascii_strncasecmp (pad_name, "text_", 5))
249 sink_pad = "text_sink";
251 GST_WARNING_OBJECT (pad, "non audio/video/text pad");
256 GstPad *playsink_pad;
258 playsink_pad = gst_element_get_request_pad (data->playsink, sink_pad);
260 gst_pad_link (pad, playsink_pad);
265 main (int argc, gchar ** argv)
267 GError *error = NULL;
272 gst_init (&argc, &argv);
274 data = g_new0 (MyDataStruct, 1);
277 g_print ("Usage: decodebin3 URI\n");
281 data->pipeline = gst_pipeline_new ("pipeline");
282 data->decodebin = gst_element_factory_make ("decodebin3", NULL);
285 gst_element_make_from_uri (GST_URI_SRC, argv[1], "source", &error);
287 g_printf ("pipeline could not be constructed: %s\n", error->message);
288 g_error_free (error);
291 data->playsink = gst_element_factory_make ("playsink", NULL);
295 GstElement *sink = gst_element_factory_make ("fakesink", NULL);
296 g_object_set (sink, "sync", FALSE, NULL);
297 g_object_set (data->playsink, "video-sink", sink, NULL);
299 sink = gst_element_factory_make ("fakesink", NULL);
300 g_object_set (sink, "sync", FALSE, NULL);
301 g_object_set (data->playsink, "audio-sink", sink, NULL);
305 gst_bin_add_many ((GstBin *) data->pipeline, data->src, data->decodebin,
306 data->playsink, NULL);
307 if (!gst_element_link (data->src, (GstElement *) data->decodebin)) {
308 g_printf ("Could not link source to demuxer\n");
312 /* Handle other inputs if specified */
314 for (i = 2; i < argc; i++) {
315 GstElement *new_src =
316 gst_element_make_from_uri (GST_URI_SRC, argv[i], NULL, &error);
317 GstPad *src_pad, *sink_pad;
319 g_printf ("pipeline could not be constructed: %s\n", error->message);
320 g_error_free (error);
323 data->other_src = g_list_append (data->other_src, new_src);
324 gst_bin_add ((GstBin *) data->pipeline, new_src);
325 src_pad = gst_element_get_static_pad (new_src, "src");
326 sink_pad = gst_element_get_request_pad (data->decodebin, "sink_%u");
327 if (gst_pad_link (src_pad, sink_pad) != GST_PAD_LINK_OK) {
328 g_printf ("Could not link new source to decodebin : %s\n", argv[i]);
335 g_signal_connect (data->decodebin, "pad-added",
336 (GCallback) decodebin_pad_added_cb, data);
337 data->mainloop = g_main_loop_new (NULL, FALSE);
339 /* Put a bus handler */
340 bus = gst_pipeline_get_bus (GST_PIPELINE (data->pipeline));
341 gst_bus_set_sync_handler (bus, (GstBusSyncHandler) _on_bus_message, data,
345 gst_element_set_state (data->pipeline, GST_STATE_PLAYING);
346 g_main_loop_run (data->mainloop);
348 gst_element_set_state (data->pipeline, GST_STATE_NULL);
350 gst_object_unref (data->pipeline);
351 gst_object_unref (bus);