playbin3: fix leaks of collection returned by message parse API
[platform/upstream/gstreamer.git] / tests / examples / decodebin_next / playbin-test.c
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
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
73 dump_collection (GstStreamCollection * collection)
74 {
75   guint i;
76   const GstTagList *tags;
77   const 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     }
92
93     tags = gst_stream_get_tags (stream);
94     if (tags) {
95       g_print ("  tags:\n");
96       gst_tag_list_foreach (tags, print_tag_foreach, GUINT_TO_POINTER (3));
97     }
98   }
99 }
100
101 static gboolean
102 switch_streams (MyDataStruct * data)
103 {
104   guint i, nb_streams;
105   gint nb_video = 0, nb_audio = 0, nb_text = 0;
106   GstStream *videos[256], *audios[256], *texts[256];
107   GList *streams = NULL;
108   GstEvent *ev;
109
110   g_print ("Switching Streams...\n");
111
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;
119       nb_video += 1;
120     } else if (stype == GST_STREAM_TYPE_AUDIO) {
121       audios[nb_audio] = stream;
122       nb_audio += 1;
123     } else if (stype == GST_STREAM_TYPE_TEXT) {
124       texts[nb_text] = stream;
125       nb_text += 1;
126     }
127   }
128
129   if (nb_video) {
130     data->current_video = (data->current_video + 1) % nb_video;
131     streams =
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]));
136   }
137   if (nb_audio) {
138     data->current_audio = (data->current_audio + 1) % nb_audio;
139     streams =
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]));
144   }
145   if (nb_text) {
146     data->current_text = (data->current_text + 1) % nb_text;
147     streams =
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]));
152   }
153
154   ev = gst_event_new_select_streams (streams);
155   gst_element_send_event (data->pipeline, ev);
156
157   return G_SOURCE_CONTINUE;
158 }
159
160 static void
161 stream_notify_cb (GstStreamCollection * collection, GstStream * stream,
162     GParamSpec * pspec, guint * val)
163 {
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);
170     g_free (caps_str);
171     gst_caps_unref (caps);
172   }
173 }
174
175 static GstBusSyncReply
176 _on_bus_message (GstBus * bus, GstMessage * message, MyDataStruct * data)
177 {
178   GstObject *src = GST_MESSAGE_SRC (message);
179   switch (GST_MESSAGE_TYPE (message)) {
180     case GST_MESSAGE_ERROR:{
181       GError *err = NULL;
182       gchar *name = gst_object_get_path_string (GST_MESSAGE_SRC (message));
183       gst_message_parse_error (message, &err, NULL);
184
185       g_printerr ("ERROR: from element %s: %s\n", name, err->message);
186       g_error_free (err);
187       g_free (name);
188
189       g_printf ("Stopping\n");
190       g_main_loop_quit (data->mainloop);
191       break;
192     }
193     case GST_MESSAGE_EOS:
194       g_printf ("EOS ! Stopping \n");
195       g_main_loop_quit (data->mainloop);
196       break;
197     case GST_MESSAGE_STREAM_COLLECTION:
198     {
199       GstStreamCollection *collection = NULL;
200       gst_message_parse_stream_collection (message, &collection);
201       if (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);
207           data->notify_id = 0;
208         }
209         gst_object_replace ((GstObject **) & data->collection,
210             (GstObject *) collection);
211         if (data->collection) {
212           data->notify_id =
213               g_signal_connect (data->collection, "stream-notify",
214               (GCallback) stream_notify_cb, data);
215         }
216         if (data->timeout_id == 0)
217           /* In 5s try to change streams */
218           data->timeout_id =
219               g_timeout_add_seconds (5, (GSourceFunc) switch_streams, data);
220         gst_object_unref (collection);
221       }
222       break;
223     }
224     case GST_MESSAGE_STREAMS_SELECTED:
225     {
226       GstStreamCollection *collection = NULL;
227       gst_message_parse_streams_selected (message, &collection);
228       if (collection) {
229         guint i, len;
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++) {
235           GstStream *stream =
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);
240         }
241         gst_object_unref (collection);
242       }
243     }
244     default:
245       break;
246   }
247
248   return GST_BUS_PASS;
249 }
250
251 static gchar *
252 cmdline_to_uri (const gchar * arg)
253 {
254   if (gst_uri_is_valid (arg))
255     return g_strdup (arg);
256
257   return gst_filename_to_uri (arg, NULL);
258 }
259
260 int
261 main (int argc, gchar ** argv)
262 {
263   GstBus *bus;
264   MyDataStruct *data;
265   gchar *uri;
266
267   gst_init (&argc, &argv);
268
269   data = g_new0 (MyDataStruct, 1);
270
271   uri = cmdline_to_uri (argv[1]);
272
273   if (argc < 2 || uri == NULL) {
274     g_print ("Usage: %s URI\n", argv[0]);
275     return 1;
276   }
277
278   data->pipeline = gst_element_factory_make ("playbin3", NULL);
279   if (data->pipeline == NULL) {
280     g_printerr ("Failed to create playbin element. Aborting");
281     return 1;
282   }
283
284   g_object_set (data->pipeline, "uri", uri, "auto-select-streams", FALSE, NULL);
285   g_free (uri);
286
287 #if 0
288   {
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);
292
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);
296   }
297 #endif
298
299   /* Handle other input if specified */
300   if (argc > 2) {
301     uri = cmdline_to_uri (argv[2]);
302     if (uri != NULL) {
303       g_object_set (data->pipeline, "suburi", uri, NULL);
304       g_free (uri);
305     } else {
306       g_warning ("Could not parse auxilliary file argument. Ignoring");
307     }
308   }
309
310   data->mainloop = g_main_loop_new (NULL, FALSE);
311
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,
315       NULL);
316
317   /* Start pipeline */
318   gst_element_set_state (data->pipeline, GST_STATE_PLAYING);
319   g_main_loop_run (data->mainloop);
320
321   gst_element_set_state (data->pipeline, GST_STATE_NULL);
322
323   gst_object_unref (data->pipeline);
324   gst_object_unref (bus);
325
326   return 0;
327 }