1 /* GStreamer
2 *
3 * unit tests for oggmux
4 *
5 * Copyright (C) 2006 James Livingston <doclivingston at gmail.com>
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 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include <string.h>
28
29 #include <gst/check/gstcheck.h>
30 #include <ogg/ogg.h>
31
32
33 typedef enum
34 {
35 CODEC_UNKNOWN,
36 CODEC_VORBIS,
37 CODEC_THEORA,
38 CODEC_SPEEX,
39 } ChainCodec;
40
41 typedef struct
42 {
43 gboolean eos;
44 gulong serialno;
45 gint64 last_granule;
46 ChainCodec codec;
47 } ChainState;
48
49 #if (defined (HAVE_THEORA) || defined (HAVE_VORBIS))
50 static ogg_sync_state oggsync;
51 static GHashTable *eos_chain_states;
52 static gulong probe_id;
53
54 static ChainCodec
get_page_codec(ogg_page * page)55 get_page_codec (ogg_page * page)
56 {
57 ChainCodec codec = CODEC_UNKNOWN;
58 ogg_stream_state state;
59 ogg_packet packet;
60
61 ogg_stream_init (&state, ogg_page_serialno (page));
62 if (ogg_stream_pagein (&state, page) == 0) {
63 if (ogg_stream_packetpeek (&state, &packet) > 0) {
64 if (strncmp ((char *) &packet.packet[1], "vorbis",
65 strlen ("vorbis")) == 0)
66 codec = CODEC_VORBIS;
67 else if (strncmp ((char *) &packet.packet[1], "theora",
68 strlen ("theora")) == 0)
69 codec = CODEC_THEORA;
70 else if (strncmp ((char *) &packet.packet[0], "Speex ",
71 strlen ("Speex ")) == 0)
72 codec = CODEC_SPEEX;
73 }
74 }
75 ogg_stream_clear (&state);
76
77 return codec;
78 }
79
80 static void
fail_if_audio(gpointer key,ChainState * state,gpointer data)81 fail_if_audio (gpointer key, ChainState * state, gpointer data)
82 {
83 fail_if (state->codec == CODEC_VORBIS,
84 "vorbis BOS occurred before theora BOS");
85 fail_if (state->codec == CODEC_SPEEX, "speex BOS occurred before theora BOS");
86 }
87
88 static ChainState *
validate_ogg_page(ogg_page * page)89 validate_ogg_page (ogg_page * page)
90 {
91 gulong serialno;
92 gint64 granule;
93 ChainState *state;
94
95 serialno = ogg_page_serialno (page);
96 granule = ogg_page_granulepos (page);
97 state = g_hash_table_lookup (eos_chain_states, GINT_TO_POINTER (serialno));
98
99 fail_if (ogg_page_packets (page) == 0 && granule != -1,
100 "Must have granulepos -1 when page has no packets, has %" G_GINT64_FORMAT,
101 granule);
102
103 if (ogg_page_bos (page)) {
104 fail_unless (state == NULL, "Extraneous BOS flag on chain %u", serialno);
105
106 state = g_new0 (ChainState, 1);
107 g_hash_table_insert (eos_chain_states, GINT_TO_POINTER (serialno), state);
108 state->serialno = serialno;
109 state->last_granule = granule;
110 state->codec = get_page_codec (page);
111
112 /* check for things like BOS ordering, etc */
113 switch (state->codec) {
114 case CODEC_THEORA:
115 /* check we have no vorbis/speex chains yet */
116 g_hash_table_foreach (eos_chain_states, (GHFunc) fail_if_audio, NULL);
117 break;
118 case CODEC_VORBIS:
119 case CODEC_SPEEX:
120 /* no checks (yet) */
121 break;
122 case CODEC_UNKNOWN:
123 default:
124 break;
125 }
126 } else if (ogg_page_eos (page)) {
127 fail_unless (state != NULL, "Missing BOS flag on chain %u", serialno);
128 state->eos = TRUE;
129 } else {
130 fail_unless (state != NULL, "Missing BOS flag on chain %u", serialno);
131 fail_unless (!state->eos, "Data after EOS flag on chain %u", serialno);
132 }
133
134 if (granule != -1) {
135 fail_unless (granule >= state->last_granule,
136 "Granulepos out-of-order for chain %u: old=%" G_GINT64_FORMAT ", new="
137 G_GINT64_FORMAT, serialno, state->last_granule, granule);
138 state->last_granule = granule;
139 }
140
141 return state;
142 }
143
144 static void
is_video(gpointer key,ChainState * state,gpointer data)145 is_video (gpointer key, ChainState * state, gpointer data)
146 {
147 if (state->codec == CODEC_THEORA)
148 *((gboolean *) data) = TRUE;
149 }
150
151 static gboolean
check_chain_final_state(gpointer key,ChainState * state,gpointer data)152 check_chain_final_state (gpointer key, ChainState * state, gpointer data)
153 {
154 fail_unless (state->eos, "missing EOS flag on chain %u", state->serialno);
155
156 /* return TRUE to empty the chain table */
157 return TRUE;
158 }
159
160 static GstPadProbeReturn
eos_buffer_probe(GstPad * pad,GstPadProbeInfo * info,gpointer unused)161 eos_buffer_probe (GstPad * pad, GstPadProbeInfo * info, gpointer unused)
162 {
163 GstBuffer *buffer = GST_PAD_PROBE_INFO_BUFFER (info);
164 gint ret;
165 gint size;
166 gchar *oggbuffer;
167 ChainState *state = NULL;
168 gboolean has_video = FALSE;
169
170 size = gst_buffer_get_size (buffer);
171
172 oggbuffer = ogg_sync_buffer (&oggsync, size);
173 gst_buffer_extract (buffer, 0, oggbuffer, size);
174 ogg_sync_wrote (&oggsync, size);
175
176 do {
177 ogg_page page;
178
179 ret = ogg_sync_pageout (&oggsync, &page);
180 if (ret > 0)
181 state = validate_ogg_page (&page);
182 }
183 while (ret != 0);
184
185 if (state) {
186 /* Now, we can do buffer-level checks...
187 * If we have video somewhere, then we should have DELTA_UNIT set on all
188 * non-header (not HEADER), non-video buffers
189 */
190 g_hash_table_foreach (eos_chain_states, (GHFunc) is_video, &has_video);
191 if (has_video && state->codec != CODEC_THEORA) {
192 if (!GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_HEADER))
193 fail_unless (GST_BUFFER_FLAG_IS_SET (buffer,
194 GST_BUFFER_FLAG_DELTA_UNIT),
195 "Non-video buffer doesn't have DELTA_UNIT in stream with video");
196 }
197 }
198
199 return GST_PAD_PROBE_OK;
200 }
201
202 static void
start_pipeline(GstElement * bin,GstPad * pad)203 start_pipeline (GstElement * bin, GstPad * pad)
204 {
205 GstStateChangeReturn ret;
206
207 ogg_sync_init (&oggsync);
208
209 eos_chain_states =
210 g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, g_free);
211 probe_id =
212 gst_pad_add_probe (pad, GST_PAD_PROBE_TYPE_BUFFER,
213 (GstPadProbeCallback) eos_buffer_probe, NULL, NULL);
214
215 ret = gst_element_set_state (bin, GST_STATE_PLAYING);
216 fail_if (ret == GST_STATE_CHANGE_FAILURE, "Could not start test pipeline");
217 if (ret == GST_STATE_CHANGE_ASYNC) {
218 ret = gst_element_get_state (bin, NULL, NULL, GST_CLOCK_TIME_NONE);
219 fail_if (ret != GST_STATE_CHANGE_SUCCESS, "Could not start test pipeline");
220 }
221 }
222
223 static void
stop_pipeline(GstElement * bin,GstPad * pad)224 stop_pipeline (GstElement * bin, GstPad * pad)
225 {
226 GstStateChangeReturn ret;
227
228 ret = gst_element_set_state (bin, GST_STATE_NULL);
229 fail_if (ret == GST_STATE_CHANGE_FAILURE, "Could not stop test pipeline");
230 if (ret == GST_STATE_CHANGE_ASYNC) {
231 ret = gst_element_get_state (bin, NULL, NULL, GST_CLOCK_TIME_NONE);
232 fail_if (ret != GST_STATE_CHANGE_SUCCESS, "Could not stop test pipeline");
233 }
234
235 gst_pad_remove_probe (pad, probe_id);
236 ogg_sync_clear (&oggsync);
237
238 /* check end conditions, such as EOS flags */
239 g_hash_table_foreach_remove (eos_chain_states,
240 (GHRFunc) check_chain_final_state, NULL);
241 }
242
243 static gboolean
eos_watch(GstBus * bus,GstMessage * message,GMainLoop * loop)244 eos_watch (GstBus * bus, GstMessage * message, GMainLoop * loop)
245 {
246 if (GST_MESSAGE_TYPE (message) == GST_MESSAGE_EOS) {
247 g_main_loop_quit (loop);
248 }
249 return TRUE;
250 }
251
252 static void
test_pipeline(const char * pipeline)253 test_pipeline (const char *pipeline)
254 {
255 GstElement *bin, *sink;
256 GstPad *pad, *sinkpad;
257 GstBus *bus;
258 GError *error = NULL;
259 GMainLoop *loop;
260 GstPadLinkReturn linkret;
261 guint bus_watch = 0;
262
263 bin = gst_parse_launch (pipeline, &error);
264 fail_unless (bin != NULL, "Error parsing pipeline: %s",
265 error ? error->message : "(invalid error)");
266 pad = gst_bin_find_unlinked_pad (GST_BIN (bin), GST_PAD_SRC);
267 fail_unless (pad != NULL, "Could not locate free src pad");
268
269 /* connect the fake sink */
270 sink = gst_element_factory_make ("fakesink", "fake_sink");
271 fail_unless (sink != NULL, "Could create fakesink");
272 fail_unless (gst_bin_add (GST_BIN (bin), sink), "Could not insert fakesink");
273 sinkpad = gst_element_get_static_pad (sink, "sink");
274 fail_unless (sinkpad != NULL, "Could not get fakesink src pad");
275
276 linkret = gst_pad_link (pad, sinkpad);
277 fail_unless (GST_PAD_LINK_SUCCESSFUL (linkret),
278 "Could not link to fake sink");
279 gst_object_unref (sinkpad);
280
281 /* run until we receive EOS */
282 loop = g_main_loop_new (NULL, FALSE);
283 bus = gst_element_get_bus (bin);
284 bus_watch = gst_bus_add_watch (bus, (GstBusFunc) eos_watch, loop);
285 gst_object_unref (bus);
286
287 start_pipeline (bin, pad);
288 g_main_loop_run (loop);
289
290 /* we're EOS now; make sure oggmux out caps have stream headers on them */
291 {
292 GstStructure *s;
293 GstCaps *muxcaps;
294
295 muxcaps = gst_pad_get_current_caps (sinkpad);
296 fail_unless (muxcaps != NULL);
297 s = gst_caps_get_structure (muxcaps, 0);
298 fail_unless (gst_structure_has_name (s, "application/ogg"));
299 fail_unless (gst_structure_has_field (s, "streamheader"));
300 fail_unless (gst_structure_has_field_typed (s, "streamheader",
301 GST_TYPE_ARRAY));
302 gst_caps_unref (muxcaps);
303 }
304
305 stop_pipeline (bin, pad);
306
307 /* clean up */
308 g_main_loop_unref (loop);
309 g_source_remove (bus_watch);
310 gst_object_unref (pad);
311 gst_object_unref (bin);
312 }
313 #endif
314
315 #ifdef HAVE_VORBIS
GST_START_TEST(test_vorbis)316 GST_START_TEST (test_vorbis)
317 {
318 test_pipeline
319 ("audiotestsrc num-buffers=5 ! audioconvert ! vorbisenc ! .audio_%u oggmux");
320 }
321
322 GST_END_TEST;
323
GST_START_TEST(test_vorbis_oggmux_unlinked)324 GST_START_TEST (test_vorbis_oggmux_unlinked)
325 {
326 GstElement *pipe;
327 GstMessage *msg;
328
329 pipe = gst_parse_launch ("audiotestsrc ! vorbisenc ! .audio_%u oggmux", NULL);
330 if (pipe == NULL) {
331 g_printerr ("Skipping test 'test_vorbis_oggmux_unlinked'");
332 return;
333 }
334 /* no sink, no async state change */
335 fail_unless_equals_int (gst_element_set_state (pipe, GST_STATE_PAUSED),
336 GST_STATE_CHANGE_SUCCESS);
337 /* we expect an error (without any criticals/warnings) */
338 msg = gst_bus_timed_pop_filtered (GST_ELEMENT_BUS (pipe), -1,
339 GST_MESSAGE_ERROR);
340 gst_message_unref (msg);
341 fail_unless_equals_int (gst_element_set_state (pipe, GST_STATE_NULL),
342 GST_STATE_CHANGE_SUCCESS);
343 gst_object_unref (pipe);
344 }
345
346 GST_END_TEST;
347 #endif
348
349 #ifdef HAVE_THEORA
GST_START_TEST(test_theora)350 GST_START_TEST (test_theora)
351 {
352 test_pipeline
353 ("videotestsrc num-buffers=5 ! videoconvert ! theoraenc ! .video_%u oggmux");
354 }
355
356 GST_END_TEST;
357 #endif
358
359 #if (defined (HAVE_THEORA) && defined (HAVE_VORBIS))
GST_START_TEST(test_theora_vorbis)360 GST_START_TEST (test_theora_vorbis)
361 {
362 test_pipeline
363 ("videotestsrc num-buffers=10 ! videoconvert ! theoraenc ! queue ! .video_%u oggmux name=mux "
364 "audiotestsrc num-buffers=2 ! audioconvert ! vorbisenc ! queue ! mux.audio_%u");
365 }
366
367 GST_END_TEST;
368
GST_START_TEST(test_vorbis_theora)369 GST_START_TEST (test_vorbis_theora)
370 {
371 test_pipeline
372 ("videotestsrc num-buffers=2 ! videoconvert ! theoraenc ! queue ! .video_%u oggmux name=mux "
373 "audiotestsrc num-buffers=10 ! audioconvert ! vorbisenc ! queue ! mux.audio_%u");
374 }
375
376 GST_END_TEST;
377 #endif
378
GST_START_TEST(test_simple_cleanup)379 GST_START_TEST (test_simple_cleanup)
380 {
381 GstElement *oggmux;
382
383 oggmux = gst_element_factory_make ("oggmux", NULL);
384 gst_object_unref (oggmux);
385 }
386
387 GST_END_TEST;
388
GST_START_TEST(test_request_pad_cleanup)389 GST_START_TEST (test_request_pad_cleanup)
390 {
391 GstElement *oggmux;
392 GstPad *pad;
393
394 oggmux = gst_element_factory_make ("oggmux", NULL);
395 pad = gst_element_get_request_pad (oggmux, "video_%u");
396 fail_unless (pad != NULL);
397 gst_object_unref (pad);
398 pad = gst_element_get_request_pad (oggmux, "audio_%u");
399 fail_unless (pad != NULL);
400 gst_object_unref (pad);
401 gst_object_unref (oggmux);
402 }
403
404 GST_END_TEST;
405
406 static Suite *
oggmux_suite(void)407 oggmux_suite (void)
408 {
409 Suite *s = suite_create ("oggmux");
410 TCase *tc_chain = tcase_create ("general");
411
412 suite_add_tcase (s, tc_chain);
413 #ifdef HAVE_VORBIS
414 tcase_add_test (tc_chain, test_vorbis);
415 tcase_add_test (tc_chain, test_vorbis_oggmux_unlinked);
416 #endif
417
418 #ifdef HAVE_THEORA
419 tcase_add_test (tc_chain, test_theora);
420 #endif
421
422 #if (defined (HAVE_THEORA) && defined (HAVE_VORBIS))
423 tcase_add_test (tc_chain, test_vorbis_theora);
424 tcase_add_test (tc_chain, test_theora_vorbis);
425 #endif
426
427 tcase_add_test (tc_chain, test_simple_cleanup);
428 tcase_add_test (tc_chain, test_request_pad_cleanup);
429 return s;
430 }
431
432 GST_CHECK_MAIN (oggmux);
433