Tizen 2.0 Release
[framework/multimedia/gst-plugins-good0.10.git] / tests / check / elements / flvdemux.c
1 /* GStreamer unit tests for flvdemux
2  *
3  * Copyright (C) 2009 Tim-Philipp Müller  <tim centricular net>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 #include <gst/check/gstcheck.h>
22
23 #include <gst/gst.h>
24
25 static void
26 pad_added_cb (GstElement * flvdemux, GstPad * pad, GstBin * pipeline)
27 {
28   GstElement *sink;
29
30   sink = gst_bin_get_by_name (pipeline, "fakesink");
31   fail_unless (gst_element_link (flvdemux, sink));
32   gst_object_unref (sink);
33
34   gst_element_set_state (sink, GST_STATE_PAUSED);
35 }
36
37 static GstBusSyncReply
38 error_cb (GstBus * bus, GstMessage * msg, gpointer user_data)
39 {
40   if (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_ERROR) {
41     const gchar *file = (const gchar *) user_data;
42     GError *err = NULL;
43     gchar *dbg = NULL;
44
45     gst_message_parse_error (msg, &err, &dbg);
46     g_error ("ERROR for %s: %s\n%s\n", file, err->message, dbg);
47   }
48
49   return GST_BUS_PASS;
50 }
51
52 static void
53 handoff_cb (GstElement * element, GstBuffer * buf, GstPad * pad,
54     gint * p_counter)
55 {
56   *p_counter += 1;
57   GST_LOG ("counter = %d", *p_counter);
58
59   fail_unless (GST_BUFFER_CAPS (buf) != NULL);
60 }
61
62 static void
63 process_file (const gchar * file, gboolean push_mode, gint repeat,
64     gint num_buffers)
65 {
66   GstElement *src, *sep, *sink, *flvdemux, *pipeline;
67   GstBus *bus;
68   gchar *path;
69   gint counter;
70
71   pipeline = gst_pipeline_new ("pipeline");
72   fail_unless (pipeline != NULL, "Failed to create pipeline!");
73
74   bus = gst_element_get_bus (pipeline);
75
76   /* kids, don't use a sync handler for this at home, really; we do because
77    * we just want to abort and nothing else */
78   gst_bus_set_sync_handler (bus, error_cb, (gpointer) file);
79
80   src = gst_element_factory_make ("filesrc", "filesrc");
81   fail_unless (src != NULL, "Failed to create 'filesrc' element!");
82
83   if (push_mode) {
84     sep = gst_element_factory_make ("queue", "queue");
85     fail_unless (sep != NULL, "Failed to create 'queue' element");
86   } else {
87     sep = gst_element_factory_make ("identity", "identity");
88     fail_unless (sep != NULL, "Failed to create 'identity' element");
89   }
90
91   flvdemux = gst_element_factory_make ("flvdemux", "flvdemux");
92   fail_unless (flvdemux != NULL, "Failed to create 'flvdemux' element!");
93
94   sink = gst_element_factory_make ("fakesink", "fakesink");
95   fail_unless (sink != NULL, "Failed to create 'fakesink' element!");
96
97   g_object_set (sink, "signal-handoffs", TRUE, NULL);
98   g_signal_connect (sink, "handoff", G_CALLBACK (handoff_cb), &counter);
99
100   gst_bin_add_many (GST_BIN (pipeline), src, sep, flvdemux, sink, NULL);
101
102   fail_unless (gst_element_link (src, sep));
103   fail_unless (gst_element_link (sep, flvdemux));
104
105   /* can't link flvdemux and sink yet, do that later */
106   g_signal_connect (flvdemux, "pad-added", G_CALLBACK (pad_added_cb), pipeline);
107
108   path = g_build_filename (GST_TEST_FILES_PATH, file, NULL);
109   GST_LOG ("processing file '%s'", path);
110   g_object_set (src, "location", path, NULL);
111
112   do {
113     GstStateChangeReturn state_ret;
114     GstMessage *msg;
115
116     GST_LOG ("repeat=%d", repeat);
117
118     counter = 0;
119
120     state_ret = gst_element_set_state (pipeline, GST_STATE_PAUSED);
121     fail_unless (state_ret != GST_STATE_CHANGE_FAILURE);
122
123     if (state_ret == GST_STATE_CHANGE_ASYNC) {
124       GST_LOG ("waiting for pipeline to reach PAUSED state");
125       state_ret = gst_element_get_state (pipeline, NULL, NULL, -1);
126       fail_unless_equals_int (state_ret, GST_STATE_CHANGE_SUCCESS);
127     }
128
129     GST_LOG ("PAUSED, let's read all of it");
130
131     state_ret = gst_element_set_state (pipeline, GST_STATE_PLAYING);
132     fail_unless (state_ret != GST_STATE_CHANGE_FAILURE);
133
134     msg = gst_bus_poll (bus, GST_MESSAGE_EOS, -1);
135     fail_unless (msg != NULL, "Expected EOS message on bus! (%s)", file);
136
137     gst_message_unref (msg);
138
139     if (num_buffers >= 0) {
140       fail_unless_equals_int (counter, num_buffers);
141     }
142
143     fail_unless_equals_int (gst_element_set_state (pipeline, GST_STATE_NULL),
144         GST_STATE_CHANGE_SUCCESS);
145
146     --repeat;
147   } while (repeat > 0);
148
149   gst_object_unref (bus);
150   gst_object_unref (pipeline);
151
152   g_free (path);
153 }
154
155 GST_START_TEST (test_reuse_pull)
156 {
157   process_file ("pcm16sine.flv", FALSE, 3, 129);
158   gst_task_cleanup_all ();
159 }
160
161 GST_END_TEST;
162
163 GST_START_TEST (test_reuse_push)
164 {
165   process_file ("pcm16sine.flv", TRUE, 3, 129);
166   gst_task_cleanup_all ();
167 }
168
169 GST_END_TEST;
170
171 static Suite *
172 flvdemux_suite (void)
173 {
174   Suite *s = suite_create ("flvdemux");
175   TCase *tc_chain = tcase_create ("general");
176
177   suite_add_tcase (s, tc_chain);
178   tcase_add_test (tc_chain, test_reuse_push);
179   tcase_add_test (tc_chain, test_reuse_pull);
180
181   return s;
182 }
183
184 GST_CHECK_MAIN (flvdemux)