tests: playback: include stdlib.h unconditionally
[platform/upstream/gstreamer.git] / tests / icles / playback / test6.c
1 /* GStreamer
2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19 #ifdef HAVE_CONFIG_H
20 #include "config.h"
21 #endif
22
23 #include <stdlib.h>             /* exit */
24
25 #include <gst/gst.h>
26
27 static void
28 pad_added_cb (GstElement * decodebin, GstPad * new_pad, GstElement * pipeline)
29 {
30   GstElement *fakesink;
31   GstPad *sinkpad;
32
33   fakesink = gst_element_factory_make ("fakesink", NULL);
34   gst_bin_add (GST_BIN (pipeline), fakesink);
35
36   sinkpad = gst_element_get_static_pad (fakesink, "sink");
37   if (GST_PAD_LINK_FAILED (gst_pad_link (new_pad, sinkpad))) {
38     g_warning ("Failed to link %s:%s to %s:%s", GST_DEBUG_PAD_NAME (new_pad),
39         GST_DEBUG_PAD_NAME (sinkpad));
40     gst_bin_remove (GST_BIN (pipeline), fakesink);
41   } else {
42     gst_element_set_state (fakesink, GST_STATE_PAUSED);
43   }
44 }
45
46 static void
47 show_error (const gchar * errmsg, GstBus * bus)
48 {
49   GstMessage *msg;
50   GError *err = NULL;
51   gchar *dbg = NULL;
52
53   msg = gst_bus_poll (bus, GST_MESSAGE_ERROR, 0);
54   if (msg) {
55     g_assert (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_ERROR);
56
57     gst_message_parse_error (msg, &err, &dbg);
58   }
59
60   g_print ("ERROR: %s\n", errmsg);
61   g_print ("       %s\n", (err) ? err->message : "");
62   if (dbg) {
63     g_print ("\ndebug: %s\n\n", dbg);
64     g_free (dbg);
65   }
66
67   if (err)
68     g_error_free (err);
69 }
70
71 gint
72 main (gint argc, gchar * argv[])
73 {
74   GstElement *pipeline, *filesrc, *decodebin;
75   GstStateChangeReturn res;
76   GstIterator *it;
77   GstBus *bus;
78   GValue data = { 0, };
79
80   gst_init (&argc, &argv);
81
82   pipeline = gst_pipeline_new ("pipeline");
83
84   filesrc = gst_element_factory_make ("filesrc", "filesrc");
85   g_assert (filesrc);
86
87   decodebin = gst_element_factory_make ("decodebin", "decodebin");
88   g_assert (decodebin);
89
90   gst_bin_add_many (GST_BIN (pipeline), filesrc, decodebin, NULL);
91   gst_element_link (filesrc, decodebin);
92
93   if (argc < 2) {
94     g_print ("usage: %s <filenames>\n", argv[0]);
95     exit (-1);
96   }
97
98   if (!g_str_has_prefix (argv[1], "file://")) {
99     g_object_set (G_OBJECT (filesrc), "location", argv[1], NULL);
100   } else {
101     g_object_set (G_OBJECT (filesrc), "location", argv[1] + 7, NULL);
102   }
103
104   /* we've got to connect fakesinks to newly decoded pads to make sure
105    * buffers have actually been flowing over those pads and caps have
106    * been set on them. decodebin might insert internal queues and
107    * without fakesinks it's pot-luck what caps we get from the pad, because
108    * it depends on whether the queues have started pushing buffers yet or not.
109    * With fakesinks we make sure that the pipeline doesn't go to PAUSED state
110    * before each fakesink has a buffer queued. */
111   g_signal_connect (decodebin, "pad-added",
112       G_CALLBACK (pad_added_cb), pipeline);
113
114   bus = gst_element_get_bus (pipeline);
115
116   g_print ("pause..\n");
117   res = gst_element_set_state (pipeline, GST_STATE_PAUSED);
118   if (res == GST_STATE_CHANGE_FAILURE) {
119     show_error ("Could not go to PAUSED state", bus);
120     exit (-1);
121   }
122   g_print ("waiting..\n");
123   res = gst_element_get_state (pipeline, NULL, NULL, GST_CLOCK_TIME_NONE);
124   if (res != GST_STATE_CHANGE_SUCCESS) {
125     show_error ("Failed to complete state change to PAUSED", bus);
126     exit (-1);
127   }
128   g_print ("stats..\n");
129
130   it = gst_element_iterate_src_pads (decodebin);
131   while (gst_iterator_next (it, &data) == GST_ITERATOR_OK) {
132     GstPad *pad = g_value_get_object (&data);
133     GstCaps *caps;
134     gchar *str;
135     GstQuery *query;
136
137     g_print ("stream %s:\n", GST_OBJECT_NAME (pad));
138
139     caps = gst_pad_query_caps (pad, NULL);
140     str = gst_caps_to_string (caps);
141     g_print (" caps: %s\n", str);
142     g_free (str);
143     gst_caps_unref (caps);
144
145     query = gst_query_new_duration (GST_FORMAT_TIME);
146     if (gst_pad_query (pad, query)) {
147       gint64 duration;
148
149       gst_query_parse_duration (query, NULL, &duration);
150
151       g_print (" duration: %" GST_TIME_FORMAT "\n", GST_TIME_ARGS (duration));
152     }
153     gst_query_unref (query);
154
155     g_value_reset (&data);
156   }
157   g_value_unset (&data);
158   gst_iterator_free (it);
159
160   return 0;
161 }