1 /* sample application for testing decodebin3 w/ playbin
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 *bus;
38
39 /* Current collection */
40 GstStreamCollection *collection;
41 guint notify_id;
42
43 guint current_audio;
44 guint current_video;
45 guint current_text;
46
47 glong timeout_id;
48 } MyDataStruct;
49
50 static void
print_tag_foreach(const GstTagList * tags,const gchar * tag,gpointer user_data)51 print_tag_foreach (const GstTagList * tags, const gchar * tag,
52 gpointer user_data)
53 {
54 GValue val = { 0, };
55 gchar *str;
56 gint depth = GPOINTER_TO_INT (user_data);
57
58 if (!gst_tag_list_copy_value (&val, tags, tag))
59 return;
60
61 if (G_VALUE_HOLDS_STRING (&val))
62 str = g_value_dup_string (&val);
63 else
64 str = gst_value_serialize (&val);
65
66 g_print ("%*s%s: %s\n", 2 * depth, " ", gst_tag_get_nick (tag), str);
67 g_free (str);
68
69 g_value_unset (&val);
70 }
71
72 static void
dump_collection(GstStreamCollection * collection)73 dump_collection (GstStreamCollection * collection)
74 {
75 guint i;
76 GstTagList *tags;
77 GstCaps *caps;
78
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));
85
86 caps = gst_stream_get_caps (stream);
87 if (caps) {
88 gchar *caps_str = gst_caps_to_string (caps);
89 g_print (" caps: %s\n", caps_str);
90 g_free (caps_str);
91 gst_caps_unref (caps);
92 }
93
94 tags = gst_stream_get_tags (stream);
95 if (tags) {
96 g_print (" tags:\n");
97 gst_tag_list_foreach (tags, print_tag_foreach, GUINT_TO_POINTER (3));
98 gst_tag_list_unref (tags);
99 }
100 }
101 }
102
103 static gboolean
switch_streams(MyDataStruct * data)104 switch_streams (MyDataStruct * data)
105 {
106 guint i, nb_streams;
107 gint nb_video = 0, nb_audio = 0, nb_text = 0;
108 GstStream *videos[256], *audios[256], *texts[256];
109 GList *streams = NULL;
110 GstEvent *ev;
111
112 g_print ("Switching Streams...\n");
113
114 /* Calculate the number of streams of each type */
115 nb_streams = gst_stream_collection_get_size (data->collection);
116 for (i = 0; i < nb_streams; i++) {
117 GstStream *stream = gst_stream_collection_get_stream (data->collection, i);
118 GstStreamType stype = gst_stream_get_stream_type (stream);
119 if (stype == GST_STREAM_TYPE_VIDEO) {
120 videos[nb_video] = stream;
121 nb_video += 1;
122 } else if (stype == GST_STREAM_TYPE_AUDIO) {
123 audios[nb_audio] = stream;
124 nb_audio += 1;
125 } else if (stype == GST_STREAM_TYPE_TEXT) {
126 texts[nb_text] = stream;
127 nb_text += 1;
128 }
129 }
130
131 if (nb_video) {
132 data->current_video = (data->current_video + 1) % nb_video;
133 streams =
134 g_list_append (streams,
135 (gchar *) gst_stream_get_stream_id (videos[data->current_video]));
136 g_print (" Selecting video channel #%d : %s\n", data->current_video,
137 gst_stream_get_stream_id (videos[data->current_video]));
138 }
139 if (nb_audio) {
140 data->current_audio = (data->current_audio + 1) % nb_audio;
141 streams =
142 g_list_append (streams,
143 (gchar *) gst_stream_get_stream_id (audios[data->current_audio]));
144 g_print (" Selecting audio channel #%d : %s\n", data->current_audio,
145 gst_stream_get_stream_id (audios[data->current_audio]));
146 }
147 if (nb_text) {
148 data->current_text = (data->current_text + 1) % nb_text;
149 streams =
150 g_list_append (streams,
151 (gchar *) gst_stream_get_stream_id (texts[data->current_text]));
152 g_print (" Selecting text channel #%d : %s\n", data->current_text,
153 gst_stream_get_stream_id (texts[data->current_text]));
154 }
155
156 ev = gst_event_new_select_streams (streams);
157 gst_element_send_event (data->pipeline, ev);
158 g_list_free (streams);
159
160 return G_SOURCE_CONTINUE;
161 }
162
163 static void
stream_notify_cb(GstStreamCollection * collection,GstStream * stream,GParamSpec * pspec,guint * val)164 stream_notify_cb (GstStreamCollection * collection, GstStream * stream,
165 GParamSpec * pspec, guint * val)
166 {
167 g_print ("Got stream-notify from stream %s for %s (collection %p)\n",
168 stream->stream_id, pspec->name, collection);
169 if (g_str_equal (pspec->name, "caps")) {
170 GstCaps *caps = gst_stream_get_caps (stream);
171 gchar *caps_str = gst_caps_to_string (caps);
172 g_print (" New caps: %s\n", caps_str);
173 g_free (caps_str);
174 gst_caps_unref (caps);
175 }
176 }
177
178 static GstBusSyncReply
_on_bus_message(GstBus * bus,GstMessage * message,MyDataStruct * data)179 _on_bus_message (GstBus * bus, GstMessage * message, MyDataStruct * data)
180 {
181 GstObject *src = GST_MESSAGE_SRC (message);
182 switch (GST_MESSAGE_TYPE (message)) {
183 case GST_MESSAGE_ERROR:{
184 GError *err = NULL;
185 gchar *name = gst_object_get_path_string (GST_MESSAGE_SRC (message));
186 gst_message_parse_error (message, &err, NULL);
187
188 g_printerr ("ERROR: from element %s: %s\n", name, err->message);
189 g_error_free (err);
190 g_free (name);
191
192 g_printf ("Stopping\n");
193 g_main_loop_quit (data->mainloop);
194 break;
195 }
196 case GST_MESSAGE_EOS:
197 g_printf ("EOS ! Stopping \n");
198 g_main_loop_quit (data->mainloop);
199 break;
200 case GST_MESSAGE_STREAM_COLLECTION:
201 {
202 GstStreamCollection *collection = NULL;
203 gst_message_parse_stream_collection (message, &collection);
204 if (collection) {
205 g_printf ("Got a collection from %s:\n",
206 src ? GST_OBJECT_NAME (src) : "Unknown");
207 dump_collection (collection);
208 if (data->collection && data->notify_id) {
209 g_signal_handler_disconnect (data->collection, data->notify_id);
210 data->notify_id = 0;
211 }
212 gst_object_replace ((GstObject **) & data->collection,
213 (GstObject *) collection);
214 if (data->collection) {
215 data->notify_id =
216 g_signal_connect (data->collection, "stream-notify",
217 (GCallback) stream_notify_cb, data);
218 }
219 if (data->timeout_id == 0)
220 /* In 5s try to change streams */
221 data->timeout_id =
222 g_timeout_add_seconds (5, (GSourceFunc) switch_streams, data);
223 gst_object_unref (collection);
224 }
225 break;
226 }
227 case GST_MESSAGE_STREAMS_SELECTED:
228 {
229 GstStreamCollection *collection = NULL;
230 gst_message_parse_streams_selected (message, &collection);
231 if (collection) {
232 guint i, len;
233 g_printf ("Got a STREAMS_SELECTED message from %s (seqnum:%"
234 G_GUINT32_FORMAT "):\n", src ? GST_OBJECT_NAME (src) : "unknown",
235 GST_MESSAGE_SEQNUM (message));
236 len = gst_message_streams_selected_get_size (message);
237 for (i = 0; i < len; i++) {
238 GstStream *stream =
239 gst_message_streams_selected_get_stream (message, i);
240 g_printf (" Stream #%d : %s\n", i,
241 gst_stream_get_stream_id (stream));
242 gst_object_unref (stream);
243 }
244 gst_object_unref (collection);
245 }
246 }
247 default:
248 break;
249 }
250
251 return GST_BUS_PASS;
252 }
253
254 static gchar *
cmdline_to_uri(const gchar * arg)255 cmdline_to_uri (const gchar * arg)
256 {
257 if (gst_uri_is_valid (arg))
258 return g_strdup (arg);
259
260 return gst_filename_to_uri (arg, NULL);
261 }
262
263 int
main(int argc,gchar ** argv)264 main (int argc, gchar ** argv)
265 {
266 GstBus *bus;
267 MyDataStruct *data;
268 gchar *uri;
269
270 gst_init (&argc, &argv);
271
272 data = g_new0 (MyDataStruct, 1);
273
274 uri = cmdline_to_uri (argv[1]);
275
276 if (argc < 2 || uri == NULL) {
277 g_print ("Usage: %s URI\n", argv[0]);
278 return 1;
279 }
280
281 data->pipeline = gst_element_factory_make ("playbin3", NULL);
282 if (data->pipeline == NULL) {
283 g_printerr ("Failed to create playbin element. Aborting");
284 return 1;
285 }
286
287 g_object_set (data->pipeline, "uri", uri, NULL);
288 g_free (uri);
289
290 #if 0
291 {
292 GstElement *sink = gst_element_factory_make ("fakesink", NULL);
293 g_object_set (sink, "sync", FALSE, NULL);
294 g_object_set (data->pipeline, "video-sink", sink, NULL);
295
296 sink = gst_element_factory_make ("fakesink", NULL);
297 g_object_set (sink, "sync", FALSE, NULL);
298 g_object_set (data->pipeline, "audio-sink", sink, NULL);
299 }
300 #endif
301
302 /* Handle other input if specified */
303 if (argc > 2) {
304 uri = cmdline_to_uri (argv[2]);
305 if (uri != NULL) {
306 g_object_set (data->pipeline, "suburi", uri, NULL);
307 g_free (uri);
308 } else {
309 g_warning ("Could not parse auxiliary file argument. Ignoring");
310 }
311 }
312
313 data->mainloop = g_main_loop_new (NULL, FALSE);
314
315 /* Put a bus handler */
316 bus = gst_pipeline_get_bus (GST_PIPELINE (data->pipeline));
317 gst_bus_set_sync_handler (bus, (GstBusSyncHandler) _on_bus_message, data,
318 NULL);
319
320 /* Start pipeline */
321 gst_element_set_state (data->pipeline, GST_STATE_PLAYING);
322 g_main_loop_run (data->mainloop);
323
324 gst_element_set_state (data->pipeline, GST_STATE_NULL);
325
326 gst_object_unref (data->pipeline);
327 gst_object_unref (bus);
328
329 return 0;
330 }
331