Imported Upstream version 0.10.23
[profile/ivi/gst-plugins-bad.git] / tests / check / elements / mxfdemux.c
1 /* GStreamer
2  *
3  * unit test for mxfdemux
4  *
5  * Copyright (C) <2008> Sebastian Dröge <sebastian.droege@collabora.co.uk>
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., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 #include <gst/check/gstcheck.h>
24 #include <string.h>
25 #include "mxfdemux.h"
26
27 static GstPad *mysrcpad, *mysinkpad;
28 static GMainLoop *loop = NULL;
29 static gboolean have_eos = FALSE;
30 static gboolean have_data = FALSE;
31
32 static GstStaticPadTemplate mysrctemplate =
33 GST_STATIC_PAD_TEMPLATE ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
34     GST_STATIC_CAPS ("application/mxf"));
35
36 static GstStaticPadTemplate mysinktemplate =
37 GST_STATIC_PAD_TEMPLATE ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
38     GST_STATIC_CAPS_ANY);
39
40 static void
41 _pad_added (GstElement * element, GstPad * pad, gpointer user_data)
42 {
43   gchar *name = gst_pad_get_name (pad);
44
45   fail_unless_equals_string (name, "track_2");
46   fail_unless (gst_pad_link (pad, mysinkpad) == GST_PAD_LINK_OK);
47
48   g_free (name);
49 }
50
51 static GstFlowReturn
52 _sink_chain (GstPad * pad, GstBuffer * buffer)
53 {
54   GstCaps *caps = gst_caps_new_simple ("audio/x-raw-int",
55       "rate", G_TYPE_INT, 11025,
56       "channels", G_TYPE_INT, 1,
57       "signed", G_TYPE_BOOLEAN, FALSE,
58       "endianness", G_TYPE_INT, G_LITTLE_ENDIAN,
59       "depth", G_TYPE_INT, 8,
60       "width", G_TYPE_INT, 8,
61       NULL);
62
63   fail_unless (GST_BUFFER_CAPS (buffer) != NULL);
64   fail_unless (gst_caps_is_always_compatible (GST_BUFFER_CAPS (buffer), caps));
65
66   fail_unless_equals_int (GST_BUFFER_SIZE (buffer), sizeof (mxf_essence));
67   fail_unless (memcmp (GST_BUFFER_DATA (buffer), mxf_essence,
68           sizeof (mxf_essence)) == 0);
69
70   fail_unless (GST_BUFFER_TIMESTAMP (buffer) == 0);
71   fail_unless (GST_BUFFER_DURATION (buffer) == 200 * GST_MSECOND);
72
73   gst_buffer_unref (buffer);
74   gst_caps_unref (caps);
75
76   have_data = TRUE;
77   return GST_FLOW_OK;
78 }
79
80 static gboolean
81 _sink_event (GstPad * pad, GstEvent * event)
82 {
83   if (GST_EVENT_TYPE (event) == GST_EVENT_EOS) {
84     if (loop) {
85       while (!g_main_loop_is_running (loop));
86     }
87
88     have_eos = TRUE;
89     if (loop)
90       g_main_loop_quit (loop);
91   }
92
93   gst_event_unref (event);
94
95   return TRUE;
96 }
97
98 static GstPad *
99 _create_sink_pad (void)
100 {
101   mysinkpad = gst_pad_new_from_static_template (&mysinktemplate, "sink");
102
103   gst_pad_set_chain_function (mysinkpad, _sink_chain);
104   gst_pad_set_event_function (mysinkpad, _sink_event);
105
106   return mysinkpad;
107 }
108
109 static GstPad *
110 _create_src_pad_push (void)
111 {
112   mysrcpad = gst_pad_new_from_static_template (&mysrctemplate, "src");
113
114   return mysrcpad;
115 }
116
117 static GstFlowReturn
118 _src_getrange (GstPad * pad, guint64 offset, guint length, GstBuffer ** buffer)
119 {
120   GstCaps *caps;
121
122   if (offset + length > sizeof (mxf_file))
123     return GST_FLOW_UNEXPECTED;
124
125   caps = gst_caps_new_simple ("application/mxf", NULL);
126
127   *buffer = gst_buffer_new ();
128   GST_BUFFER_DATA (*buffer) = (guint8 *) (mxf_file + offset);
129   GST_BUFFER_SIZE (*buffer) = length;
130   gst_buffer_set_caps (*buffer, caps);
131   gst_caps_unref (caps);
132
133   return GST_FLOW_OK;
134 }
135
136 static gboolean
137 _src_query (GstPad * pad, GstQuery * query)
138 {
139   GstFormat fmt;
140
141   if (GST_QUERY_TYPE (query) != GST_QUERY_DURATION)
142     return FALSE;
143
144   gst_query_parse_duration (query, &fmt, NULL);
145
146   if (fmt != GST_FORMAT_BYTES)
147     return FALSE;
148
149   gst_query_set_duration (query, fmt, sizeof (mxf_file));
150
151   return TRUE;
152 }
153
154 static GstPad *
155 _create_src_pad_pull (void)
156 {
157   mysrcpad = gst_pad_new_from_static_template (&mysrctemplate, "src");
158   gst_pad_set_getrange_function (mysrcpad, _src_getrange);
159   gst_pad_set_query_function (mysrcpad, _src_query);
160
161   return mysrcpad;
162 }
163
164 GST_START_TEST (test_pull)
165 {
166   GstElement *mxfdemux;
167   GstPad *sinkpad;
168
169   have_eos = FALSE;
170   have_data = FALSE;
171   loop = g_main_loop_new (NULL, FALSE);
172
173   mxfdemux = gst_element_factory_make ("mxfdemux", NULL);
174   fail_unless (mxfdemux != NULL);
175   g_signal_connect (mxfdemux, "pad-added", G_CALLBACK (_pad_added), NULL);
176   sinkpad = gst_element_get_static_pad (mxfdemux, "sink");
177   fail_unless (sinkpad != NULL);
178
179   mysinkpad = _create_sink_pad ();
180   fail_unless (mysinkpad != NULL);
181   mysrcpad = _create_src_pad_pull ();
182   fail_unless (mysrcpad != NULL);
183
184   fail_unless (gst_pad_link (mysrcpad, sinkpad) == GST_PAD_LINK_OK);
185   gst_object_unref (sinkpad);
186
187   gst_pad_set_active (mysinkpad, TRUE);
188   gst_pad_set_active (mysrcpad, TRUE);
189
190   gst_element_set_state (mxfdemux, GST_STATE_PLAYING);
191
192   g_main_loop_run (loop);
193   fail_unless (have_eos == TRUE);
194   fail_unless (have_data == TRUE);
195
196   gst_element_set_state (mxfdemux, GST_STATE_NULL);
197   gst_pad_set_active (mysinkpad, FALSE);
198   gst_pad_set_active (mysrcpad, FALSE);
199
200   gst_object_unref (mxfdemux);
201   gst_object_unref (mysinkpad);
202   gst_object_unref (mysrcpad);
203   g_main_loop_unref (loop);
204   loop = NULL;
205 }
206
207 GST_END_TEST;
208
209 GST_START_TEST (test_push)
210 {
211   GstElement *mxfdemux;
212   GstBuffer *buffer;
213   GstPad *sinkpad;
214
215   have_data = FALSE;
216   have_eos = FALSE;
217
218   mxfdemux = gst_element_factory_make ("mxfdemux", NULL);
219   fail_unless (mxfdemux != NULL);
220   g_signal_connect (mxfdemux, "pad-added", G_CALLBACK (_pad_added), NULL);
221   sinkpad = gst_element_get_static_pad (mxfdemux, "sink");
222   fail_unless (sinkpad != NULL);
223
224   buffer = gst_buffer_new ();
225   GST_BUFFER_DATA (buffer) = (guint8 *) mxf_file;
226   GST_BUFFER_SIZE (buffer) = sizeof (mxf_file);
227   GST_BUFFER_OFFSET (buffer) = 0;
228
229   mysinkpad = _create_sink_pad ();
230   fail_unless (mysinkpad != NULL);
231   mysrcpad = _create_src_pad_push ();
232   fail_unless (mysrcpad != NULL);
233
234   fail_unless (gst_pad_link (mysrcpad, sinkpad) == GST_PAD_LINK_OK);
235   gst_object_unref (sinkpad);
236
237   gst_pad_set_active (mysinkpad, TRUE);
238   gst_pad_set_active (mysrcpad, TRUE);
239
240   gst_element_set_state (mxfdemux, GST_STATE_PLAYING);
241
242   fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
243   fail_unless (gst_pad_push_event (mysrcpad, gst_event_new_eos ()));
244
245   fail_unless (have_eos == TRUE);
246   fail_unless (have_data == TRUE);
247
248   gst_element_set_state (mxfdemux, GST_STATE_NULL);
249   gst_pad_set_active (mysinkpad, FALSE);
250   gst_pad_set_active (mysrcpad, FALSE);
251
252   gst_object_unref (mxfdemux);
253   gst_object_unref (mysinkpad);
254   gst_object_unref (mysrcpad);
255 }
256
257 GST_END_TEST;
258
259 static Suite *
260 mxfdemux_suite (void)
261 {
262   Suite *s = suite_create ("mxfdemux");
263   TCase *tc_chain = tcase_create ("general");
264
265   suite_add_tcase (s, tc_chain);
266   tcase_set_timeout (tc_chain, 180);
267   tcase_add_test (tc_chain, test_pull);
268   tcase_add_test (tc_chain, test_push);
269
270   return s;
271 }
272
273 GST_CHECK_MAIN (mxfdemux);