1 /* GStreamer
2 *
3 * unit test for mplex
4 *
5 * Copyright (C) <2008> Mark Nauwelaerts <mnauw@users.sourceforge.net>
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., 51 Franklin St, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
21 */
22
23 #include <unistd.h>
24
25 #include <gst/check/gstcheck.h>
26
27 /* For ease of programming we use globals to keep refs for our floating
28 * src and sink pads we create; otherwise we always have to do get_pad,
29 * get_peer, and then remove references in every test function */
30 static GstPad *mysrcpad, *mysinkpad;
31
32 #define AUDIO_CAPS_STRING "audio/mpeg, " \
33 "mpegversion = (int) 1, " \
34 "layer = (int) 2, " \
35 "rate = (int) 48000, " \
36 "channels = (int) 1, " \
37 "framerate = (fraction) 25/1"
38
39 #define MPEG_CAPS_STRING "video/mpeg, " \
40 "systemstream = (bool) true"
41
42 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
43 GST_PAD_SINK,
44 GST_PAD_ALWAYS,
45 GST_STATIC_CAPS (MPEG_CAPS_STRING));
46
47 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
48 GST_PAD_SRC,
49 GST_PAD_ALWAYS,
50 GST_STATIC_CAPS (AUDIO_CAPS_STRING));
51
52
53 /* some global vars, makes it easy as for the ones above */
54 static GMutex mplex_mutex;
55 static GCond mplex_cond;
56 static gboolean arrived_eos;
57
58 /* another easy hack, some mp2 audio data that should please mplex
59 * perhaps less would also do, but anyway ...
60 */
61 /* begin binary data: */
62 guint8 mp2_data[] = /*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x9A, 0x99, 0x58,
79 0x71, 0x26, 0x20, 0x33, 0x76, 0xEE, 0x96, 0x70, 0xF2, 0xBC, 0xB3, 0x7D,
80 0x6B, 0x35, 0x48, 0x37, 0x59, 0x21, 0xC4, 0x87, 0x8A, 0xD8, 0x05, 0x36,
81 0xA5, 0x1A, 0x5C, 0x0A, 0x4F, 0x4B, 0x39, 0x40, 0x39, 0x9A, 0x17, 0xD9,
82 0xAD, 0x21, 0xBE, 0x64, 0xB4, 0x6B, 0x13, 0x03, 0x20, 0x95, 0xDA, 0x18,
83 0x89, 0x88, 0xB5, 0x44, 0xE2, 0x5D, 0x4F, 0x12, 0x19, 0xC4, 0x1A, 0x1A,
84 0x07, 0x07, 0x91, 0xA8, 0x4C, 0x66, 0xB4, 0x81, 0x33, 0xDE, 0xDB, 0xD6,
85 0x24, 0x17, 0xD2, 0x9A, 0x4E, 0xC9, 0x88, 0xAB, 0x44, 0xAA, 0x25, 0x4A,
86 0x79, 0xA9, 0x39, 0x39, 0x0D, 0x2D, 0x20, 0x76, 0x68, 0x5F, 0x65, 0x25,
87 0xCF, 0x29, 0x27, 0x67, 0xB3, 0x68, 0x6C, 0xE5, 0xDC, 0xA5, 0x79, 0xC9,
88 0xAB, 0x46, 0x9D, 0x21, 0x35, 0x82, 0x98, 0xBA, 0x0E, 0x26, 0x39, 0x20,
89 0xAE, 0x1B, 0x92, 0x3D, 0xF7, 0x9F, 0x29, 0xB5, 0xF3, 0xB6, 0x38, 0x68,
90 0x65, 0x99, 0xAD, 0xD8, 0x98, 0x56, 0x5A, 0x61, 0x8D, 0xCB, 0x4A, 0x29,
91 0x43, 0x0E, 0x2D, 0x33, 0x40, 0x6A, 0xB7, 0x5F, 0x49, 0xC9, 0x81, 0xE4,
92 0x0D, 0x6F, 0x15, 0x58, 0x1B, 0x9E, 0x74, 0x20, 0x5D, 0x97, 0x5B, 0x5A,
93 0xDF, 0x92, 0x2D, 0x5A, 0x98, 0xCE, 0x50, 0x20, 0x1A, 0x33, 0x6A, 0x67,
94 0xE2, 0x18, 0x94, 0xA4, 0x70, 0x8F, 0x5F, 0x11, 0x85, 0xB0, 0xE5, 0xD8,
95 0xD4, 0xAA, 0x86, 0xAE, 0x1C, 0x0D, 0xA1, 0x6B, 0x21, 0xB9, 0xC2, 0x17
96 };
97
98 /* end binary data. size = 384 bytes */
99
100 static gboolean
test_sink_event(GstPad * pad,GstObject * parent,GstEvent * event)101 test_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
102 {
103
104 switch (GST_EVENT_TYPE (event)) {
105 case GST_EVENT_EOS:
106 g_mutex_lock (&mplex_mutex);
107 arrived_eos = TRUE;
108 g_cond_signal (&mplex_cond);
109 g_mutex_unlock (&mplex_mutex);
110 break;
111 default:
112 break;
113 }
114
115 return gst_pad_event_default (pad, parent, event);
116 }
117
118 /* setup and teardown needs some special handling for muxer */
119 static GstPad *
setup_src_pad(GstElement * element,GstStaticPadTemplate * template,GstCaps * caps,const gchar * sinkname)120 setup_src_pad (GstElement * element,
121 GstStaticPadTemplate * template, GstCaps * caps, const gchar * sinkname)
122 {
123 GstPad *srcpad, *sinkpad;
124
125 GST_DEBUG_OBJECT (element, "setting up sending pad");
126 /* sending pad */
127 srcpad = gst_pad_new_from_static_template (template, "src");
128 fail_if (srcpad == NULL, "Could not create a srcpad");
129 ASSERT_OBJECT_REFCOUNT (srcpad, "srcpad", 1);
130
131 sinkpad = gst_element_request_pad_simple (element, sinkname);
132 fail_if (sinkpad == NULL, "Could not get sink pad from %s",
133 GST_ELEMENT_NAME (element));
134 /* references are owned by: 1) us, 2) mplex, 3) mplex list */
135 ASSERT_OBJECT_REFCOUNT (sinkpad, "sinkpad", 3);
136 if (caps)
137 fail_unless (gst_pad_set_caps (srcpad, caps));
138 fail_unless (gst_pad_link (srcpad, sinkpad) == GST_PAD_LINK_OK,
139 "Could not link source and %s sink pads", GST_ELEMENT_NAME (element));
140 gst_object_unref (sinkpad); /* because we got it higher up */
141
142 /* references are owned by: 1) mplex, 2) mplex list */
143 ASSERT_OBJECT_REFCOUNT (sinkpad, "sinkpad", 2);
144
145 return srcpad;
146 }
147
148 static void
teardown_src_pad(GstElement * element,const gchar * sinkname)149 teardown_src_pad (GstElement * element, const gchar * sinkname)
150 {
151 GstPad *srcpad, *sinkpad;
152 gchar *padname;
153
154 /* clean up floating src pad */
155 padname = g_strdup (sinkname);
156 memcpy (strchr (padname, '%'), "0", 2);
157 sinkpad = gst_element_get_static_pad (element, padname);
158 g_free (padname);
159 /* pad refs held by 1) mplex 2) mplex list and 3) us (through _get) */
160 ASSERT_OBJECT_REFCOUNT (sinkpad, "sinkpad", 3);
161 srcpad = gst_pad_get_peer (sinkpad);
162
163 gst_pad_unlink (srcpad, sinkpad);
164
165 /* after unlinking, pad refs still held by
166 * 1) mplex and 2) mplex list and 3) us (through _get) */
167 ASSERT_OBJECT_REFCOUNT (sinkpad, "sinkpad", 3);
168 gst_object_unref (sinkpad);
169 /* one more ref is held by element itself */
170
171 /* pad refs held by both creator and this function (through _get_peer) */
172 ASSERT_OBJECT_REFCOUNT (srcpad, "srcpad", 2);
173 gst_object_unref (srcpad);
174 gst_object_unref (srcpad);
175
176 }
177
178 static GstElement *
setup_mplex(void)179 setup_mplex (void)
180 {
181 GstElement *mplex;
182
183 GST_DEBUG ("setup_mplex");
184 mplex = gst_check_setup_element ("mplex");
185 mysrcpad = setup_src_pad (mplex, &srctemplate, NULL, "audio_%u");
186 mysinkpad = gst_check_setup_sink_pad (mplex, &sinktemplate);
187 gst_pad_set_active (mysrcpad, TRUE);
188 gst_pad_set_active (mysinkpad, TRUE);
189
190 /* need to know when we are eos */
191 gst_pad_set_event_function (mysinkpad, test_sink_event);
192
193 /* and notify the test run */
194 g_mutex_init (&mplex_mutex);
195 g_cond_init (&mplex_cond);
196
197 return mplex;
198 }
199
200 static void
cleanup_mplex(GstElement * mplex)201 cleanup_mplex (GstElement * mplex)
202 {
203 GST_DEBUG ("cleanup_mplex");
204 gst_element_set_state (mplex, GST_STATE_NULL);
205
206 gst_pad_set_active (mysrcpad, FALSE);
207 gst_pad_set_active (mysinkpad, FALSE);
208 teardown_src_pad (mplex, "audio_%u");
209 gst_check_teardown_sink_pad (mplex);
210 gst_check_teardown_element (mplex);
211
212 g_mutex_clear (&mplex_mutex);
213 g_cond_clear (&mplex_cond);
214
215 gst_deinit ();
216 }
217
GST_START_TEST(test_audio_pad)218 GST_START_TEST (test_audio_pad)
219 {
220 GstElement *mplex;
221 GstBuffer *inbuffer, *outbuffer;
222 GstCaps *caps;
223 int i, num_buffers;
224
225 /* PES pack_start_code */
226 guint8 data0[] = { 0x00, 0x00, 0x01, 0xba };
227 /* MPEG_program_end_code */
228 guint8 data1[] = { 0x00, 0x00, 0x01, 0xb9 };
229
230 mplex = setup_mplex ();
231 fail_unless (gst_element_set_state (mplex,
232 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
233 "could not set to playing");
234
235 caps = gst_caps_from_string (AUDIO_CAPS_STRING);
236 gst_check_setup_events_with_stream_id (mysrcpad, mplex, caps,
237 GST_FORMAT_TIME, "mplex-test");
238 gst_caps_unref (caps);
239
240 /* corresponds to I420 buffer for the size mentioned in the caps */
241 inbuffer = gst_buffer_new_and_alloc (sizeof (mp2_data));
242 gst_buffer_fill (inbuffer, 0, mp2_data, sizeof (mp2_data));
243 GST_BUFFER_TIMESTAMP (inbuffer) = 0;
244 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
245 fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
246
247 /* need to force eos and state change to make sure the encoding task ends */
248 fail_unless (gst_pad_push_event (mysrcpad, gst_event_new_eos ()) == TRUE);
249 /* need to wait a bit to make sure mplex task digested all this */
250 g_mutex_lock (&mplex_mutex);
251 while (!arrived_eos)
252 g_cond_wait (&mplex_cond, &mplex_mutex);
253 g_mutex_unlock (&mplex_mutex);
254
255 num_buffers = g_list_length (buffers);
256 /* well, we do not really know much with mplex, but at least something ... */
257 fail_unless (num_buffers >= 1);
258
259 /* clean up buffers */
260 for (i = 0; i < num_buffers; ++i) {
261 outbuffer = GST_BUFFER (buffers->data);
262 fail_if (outbuffer == NULL);
263
264 if (i == 0) {
265 fail_unless (gst_buffer_get_size (outbuffer) >= sizeof (data0));
266 fail_unless (gst_buffer_memcmp (outbuffer, 0, data0,
267 sizeof (data0)) == 0);
268 }
269 if (i == num_buffers - 1) {
270 fail_unless (gst_buffer_get_size (outbuffer) >= sizeof (data1));
271 fail_unless (gst_buffer_memcmp (outbuffer,
272 gst_buffer_get_size (outbuffer) - sizeof (data1), data1,
273 sizeof (data1)) == 0);
274 }
275 buffers = g_list_remove (buffers, outbuffer);
276
277 ASSERT_BUFFER_REFCOUNT (outbuffer, "outbuffer", 1);
278 gst_buffer_unref (outbuffer);
279 outbuffer = NULL;
280 }
281
282 cleanup_mplex (mplex);
283 g_list_free (buffers);
284 buffers = NULL;
285 }
286
287 GST_END_TEST;
288
289 static Suite *
mplex_suite(void)290 mplex_suite (void)
291 {
292 Suite *s = suite_create ("mplex");
293 TCase *tc_chain = tcase_create ("general");
294
295 suite_add_tcase (s, tc_chain);
296 tcase_add_test (tc_chain, test_audio_pad);
297
298 return s;
299 }
300
301 GST_CHECK_MAIN (mplex);
302