playbin3: fix leaks of collection returned by message parse API
[platform/upstream/gstreamer.git] / tests / examples / decodebin_next / decodebin3.c
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
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
79 dump_collection (GstStreamCollection * collection)
80 {
81   guint i;
82   const GstTagList *tags;
83   const 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     }
98
99     tags = gst_stream_get_tags (stream);
100     if (tags) {
101       g_print ("  tags:\n");
102       gst_tag_list_foreach (tags, print_tag_foreach, GUINT_TO_POINTER (3));
103     }
104   }
105 }
106
107 static gboolean
108 switch_streams (MyDataStruct * data)
109 {
110   guint i, nb_streams;
111   gint nb_video = 0, nb_audio = 0, nb_text = 0;
112   GstStream *videos[256], *audios[256], *texts[256];
113   GList *streams = NULL;
114   GstEvent *ev;
115
116   g_print ("Switching Streams...\n");
117
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;
125       nb_video += 1;
126     } else if (stype == GST_STREAM_TYPE_AUDIO) {
127       audios[nb_audio] = stream;
128       nb_audio += 1;
129     } else if (stype == GST_STREAM_TYPE_TEXT) {
130       texts[nb_text] = stream;
131       nb_text += 1;
132     }
133   }
134
135   if (nb_video) {
136     data->current_video = (data->current_video + 1) % nb_video;
137     streams =
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]));
142   }
143   if (nb_audio) {
144     data->current_audio = (data->current_audio + 1) % nb_audio;
145     streams =
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]));
150   }
151   if (nb_text) {
152     data->current_text = (data->current_text + 1) % nb_text;
153     streams =
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]));
158   }
159
160   ev = gst_event_new_select_streams (streams);
161   gst_element_send_event (data->pipeline, ev);
162
163   return G_SOURCE_CONTINUE;
164 }
165
166 static void
167 stream_notify_cb (GstStreamCollection * collection, GstStream * stream,
168     GParamSpec * pspec, guint * val)
169 {
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);
176     g_free (caps_str);
177     gst_caps_unref (caps);
178   }
179 }
180
181 static GstBusSyncReply
182 _on_bus_message (GstBus * bus, GstMessage * message, MyDataStruct * data)
183 {
184   GstObject *src = GST_MESSAGE_SRC (message);
185   switch (GST_MESSAGE_TYPE (message)) {
186     case GST_MESSAGE_ERROR:{
187       GError *err = NULL;
188       gchar *name = gst_object_get_path_string (GST_MESSAGE_SRC (message));
189       gst_message_parse_error (message, &err, NULL);
190
191       g_printerr ("ERROR: from element %s: %s\n", name, err->message);
192       g_error_free (err);
193       g_free (name);
194
195       g_printf ("Stopping\n");
196       g_main_loop_quit (data->mainloop);
197       break;
198     }
199     case GST_MESSAGE_EOS:
200       g_printf ("EOS ! Stopping \n");
201       g_main_loop_quit (data->mainloop);
202       break;
203     case GST_MESSAGE_STREAM_COLLECTION:
204     {
205       GstStreamCollection *collection = NULL;
206       gst_message_parse_stream_collection (message, &collection);
207       if (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);
213           data->notify_id = 0;
214         }
215         gst_object_replace ((GstObject **) & data->collection,
216             (GstObject *) collection);
217         if (data->collection) {
218           data->notify_id =
219               g_signal_connect (data->collection, "stream-notify",
220               (GCallback) stream_notify_cb, data);
221         }
222         if (data->timeout_id == 0)
223           /* In 5s try to change streams */
224           data->timeout_id =
225               g_timeout_add_seconds (5, (GSourceFunc) switch_streams, data);
226         gst_object_unref (collection);
227       }
228       break;
229     }
230     default:
231       break;
232   }
233
234   return GST_BUS_PASS;
235 }
236
237 static void
238 decodebin_pad_added_cb (GstElement * dbin, GstPad * pad, MyDataStruct * data)
239 {
240   gchar *pad_name = gst_pad_get_name (pad);
241   const gchar *sink_pad = NULL;
242
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";
250   else
251     GST_WARNING_OBJECT (pad, "non audio/video/text pad");
252
253   g_free (pad_name);
254
255   if (sink_pad) {
256     GstPad *playsink_pad;
257
258     playsink_pad = gst_element_get_request_pad (data->playsink, sink_pad);
259     if (playsink_pad)
260       gst_pad_link (pad, playsink_pad);
261   }
262 }
263
264 int
265 main (int argc, gchar ** argv)
266 {
267   GError *error = NULL;
268   GstBus *bus;
269   MyDataStruct *data;
270   int i;
271
272   gst_init (&argc, &argv);
273
274   data = g_new0 (MyDataStruct, 1);
275
276   if (argc < 2) {
277     g_print ("Usage: decodebin3 URI\n");
278     return 1;
279   }
280
281   data->pipeline = gst_pipeline_new ("pipeline");
282   data->decodebin = gst_element_factory_make ("decodebin3", NULL);
283
284   data->src =
285       gst_element_make_from_uri (GST_URI_SRC, argv[1], "source", &error);
286   if (error) {
287     g_printf ("pipeline could not be constructed: %s\n", error->message);
288     g_error_free (error);
289     return 1;
290   }
291   data->playsink = gst_element_factory_make ("playsink", NULL);
292
293 #if 0
294   {
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);
298
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);
302   }
303 #endif
304
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");
309     return 1;
310   }
311
312   /* Handle other inputs if specified */
313   if (argc > 2) {
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;
318       if (error) {
319         g_printf ("pipeline could not be constructed: %s\n", error->message);
320         g_error_free (error);
321         return 1;
322       }
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]);
329         return 1;
330       }
331     }
332   }
333
334
335   g_signal_connect (data->decodebin, "pad-added",
336       (GCallback) decodebin_pad_added_cb, data);
337   data->mainloop = g_main_loop_new (NULL, FALSE);
338
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,
342       NULL);
343
344   /* Start pipeline */
345   gst_element_set_state (data->pipeline, GST_STATE_PLAYING);
346   g_main_loop_run (data->mainloop);
347
348   gst_element_set_state (data->pipeline, GST_STATE_NULL);
349
350   gst_object_unref (data->pipeline);
351   gst_object_unref (bus);
352
353   return 0;
354 }