1 /* sample application for testing decodebin3 w/ playbin
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 /* Current collection */
40 GstStreamCollection *collection;
51 print_tag_foreach (const GstTagList * tags, const gchar * tag,
56 gint depth = GPOINTER_TO_INT (user_data);
58 if (!gst_tag_list_copy_value (&val, tags, tag))
61 if (G_VALUE_HOLDS_STRING (&val))
62 str = g_value_dup_string (&val);
64 str = gst_value_serialize (&val);
66 g_print ("%*s%s: %s\n", 2 * depth, " ", gst_tag_get_nick (tag), str);
73 dump_collection (GstStreamCollection * collection)
76 const GstTagList *tags;
79 for (i = 0; i < gst_stream_collection_get_size (collection); i++) {
80 GstStream *stream = gst_stream_collection_get_stream (collection, i);
81 g_print (" Stream %u type %s flags 0x%x\n", i,
82 gst_stream_type_get_name (gst_stream_get_stream_type (stream)),
83 gst_stream_get_stream_flags (stream));
84 g_print (" ID: %s\n", gst_stream_get_stream_id (stream));
86 caps = gst_stream_get_caps (stream);
88 gchar *caps_str = gst_caps_to_string (caps);
89 g_print (" caps: %s\n", caps_str);
93 tags = gst_stream_get_tags (stream);
96 gst_tag_list_foreach (tags, print_tag_foreach, GUINT_TO_POINTER (3));
102 switch_streams (MyDataStruct * data)
105 gint nb_video = 0, nb_audio = 0, nb_text = 0;
106 GstStream *videos[256], *audios[256], *texts[256];
107 GList *streams = NULL;
110 g_print ("Switching Streams...\n");
112 /* Calculate the number of streams of each type */
113 nb_streams = gst_stream_collection_get_size (data->collection);
114 for (i = 0; i < nb_streams; i++) {
115 GstStream *stream = gst_stream_collection_get_stream (data->collection, i);
116 GstStreamType stype = gst_stream_get_stream_type (stream);
117 if (stype == GST_STREAM_TYPE_VIDEO) {
118 videos[nb_video] = stream;
120 } else if (stype == GST_STREAM_TYPE_AUDIO) {
121 audios[nb_audio] = stream;
123 } else if (stype == GST_STREAM_TYPE_TEXT) {
124 texts[nb_text] = stream;
130 data->current_video = (data->current_video + 1) % nb_video;
132 g_list_append (streams,
133 (gchar *) gst_stream_get_stream_id (videos[data->current_video]));
134 g_print (" Selecting video channel #%d : %s\n", data->current_video,
135 gst_stream_get_stream_id (videos[data->current_video]));
138 data->current_audio = (data->current_audio + 1) % nb_audio;
140 g_list_append (streams,
141 (gchar *) gst_stream_get_stream_id (audios[data->current_audio]));
142 g_print (" Selecting audio channel #%d : %s\n", data->current_audio,
143 gst_stream_get_stream_id (audios[data->current_audio]));
146 data->current_text = (data->current_text + 1) % nb_text;
148 g_list_append (streams,
149 (gchar *) gst_stream_get_stream_id (texts[data->current_text]));
150 g_print (" Selecting text channel #%d : %s\n", data->current_text,
151 gst_stream_get_stream_id (texts[data->current_text]));
154 ev = gst_event_new_select_streams (streams);
155 gst_element_send_event (data->pipeline, ev);
157 return G_SOURCE_CONTINUE;
161 stream_notify_cb (GstStreamCollection * collection, GstStream * stream,
162 GParamSpec * pspec, guint * val)
164 g_print ("Got stream-notify from stream %s for %s (collection %p)\n",
165 stream->stream_id, pspec->name, collection);
166 if (g_str_equal (pspec->name, "caps")) {
167 GstCaps *caps = gst_stream_get_caps (stream);
168 gchar *caps_str = gst_caps_to_string (caps);
169 g_print (" New caps: %s\n", caps_str);
171 gst_caps_unref (caps);
175 static GstBusSyncReply
176 _on_bus_message (GstBus * bus, GstMessage * message, MyDataStruct * data)
178 GstObject *src = GST_MESSAGE_SRC (message);
179 switch (GST_MESSAGE_TYPE (message)) {
180 case GST_MESSAGE_ERROR:{
182 gchar *name = gst_object_get_path_string (GST_MESSAGE_SRC (message));
183 gst_message_parse_error (message, &err, NULL);
185 g_printerr ("ERROR: from element %s: %s\n", name, err->message);
189 g_printf ("Stopping\n");
190 g_main_loop_quit (data->mainloop);
193 case GST_MESSAGE_EOS:
194 g_printf ("EOS ! Stopping \n");
195 g_main_loop_quit (data->mainloop);
197 case GST_MESSAGE_STREAM_COLLECTION:
199 GstStreamCollection *collection = NULL;
200 gst_message_parse_stream_collection (message, &collection);
202 g_printf ("Got a collection from %s:\n",
203 src ? GST_OBJECT_NAME (src) : "Unknown");
204 dump_collection (collection);
205 if (data->collection && data->notify_id) {
206 g_signal_handler_disconnect (data->collection, data->notify_id);
209 gst_object_replace ((GstObject **) & data->collection,
210 (GstObject *) collection);
211 if (data->collection) {
213 g_signal_connect (data->collection, "stream-notify",
214 (GCallback) stream_notify_cb, data);
216 if (data->timeout_id == 0)
217 /* In 5s try to change streams */
219 g_timeout_add_seconds (5, (GSourceFunc) switch_streams, data);
220 gst_object_unref (collection);
224 case GST_MESSAGE_STREAMS_SELECTED:
226 GstStreamCollection *collection = NULL;
227 gst_message_parse_streams_selected (message, &collection);
230 g_printf ("Got a STREAMS_SELECTED message from %s (seqnum:%"
231 G_GUINT32_FORMAT "):\n", src ? GST_OBJECT_NAME (src) : "unknown",
232 GST_MESSAGE_SEQNUM (message));
233 len = gst_message_streams_selected_get_size (message);
234 for (i = 0; i < len; i++) {
236 gst_message_streams_selected_get_stream (message, i);
237 g_printf (" Stream #%d : %s\n", i,
238 gst_stream_get_stream_id (stream));
239 gst_object_unref (stream);
241 gst_object_unref (collection);
252 cmdline_to_uri (const gchar * arg)
254 if (gst_uri_is_valid (arg))
255 return g_strdup (arg);
257 return gst_filename_to_uri (arg, NULL);
261 main (int argc, gchar ** argv)
267 gst_init (&argc, &argv);
269 data = g_new0 (MyDataStruct, 1);
271 uri = cmdline_to_uri (argv[1]);
273 if (argc < 2 || uri == NULL) {
274 g_print ("Usage: %s URI\n", argv[0]);
278 data->pipeline = gst_element_factory_make ("playbin3", NULL);
279 if (data->pipeline == NULL) {
280 g_printerr ("Failed to create playbin element. Aborting");
284 g_object_set (data->pipeline, "uri", uri, "auto-select-streams", FALSE, NULL);
289 GstElement *sink = gst_element_factory_make ("fakesink", NULL);
290 g_object_set (sink, "sync", FALSE, NULL);
291 g_object_set (data->pipeline, "video-sink", sink, NULL);
293 sink = gst_element_factory_make ("fakesink", NULL);
294 g_object_set (sink, "sync", FALSE, NULL);
295 g_object_set (data->pipeline, "audio-sink", sink, NULL);
299 /* Handle other input if specified */
301 uri = cmdline_to_uri (argv[2]);
303 g_object_set (data->pipeline, "suburi", uri, NULL);
306 g_warning ("Could not parse auxilliary file argument. Ignoring");
310 data->mainloop = g_main_loop_new (NULL, FALSE);
312 /* Put a bus handler */
313 bus = gst_pipeline_get_bus (GST_PIPELINE (data->pipeline));
314 gst_bus_set_sync_handler (bus, (GstBusSyncHandler) _on_bus_message, data,
318 gst_element_set_state (data->pipeline, GST_STATE_PLAYING);
319 g_main_loop_run (data->mainloop);
321 gst_element_set_state (data->pipeline, GST_STATE_NULL);
323 gst_object_unref (data->pipeline);
324 gst_object_unref (bus);