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