3 * unit tests for oggmux
5 * Copyright (C) 2006 James Livingston <doclivingston at gmail.com>
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.
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.
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.
29 #include <gst/check/gstcheck.h>
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;
55 get_page_codec (ogg_page * page)
57 ChainCodec codec = CODEC_UNKNOWN;
58 ogg_stream_state state;
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)
67 else if (strncmp ((char *) &packet.packet[1], "theora",
68 strlen ("theora")) == 0)
70 else if (strncmp ((char *) &packet.packet[0], "Speex ",
71 strlen ("Speex ")) == 0)
75 ogg_stream_clear (&state);
81 fail_if_audio (gpointer key, ChainState * state, gpointer data)
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");
89 validate_ogg_page (ogg_page * page)
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));
99 fail_if (ogg_page_packets (page) == 0 && granule != -1,
100 "Must have granulepos -1 when page has no packets, has %" G_GINT64_FORMAT,
103 if (ogg_page_bos (page)) {
104 fail_unless (state == NULL, "Extraneous BOS flag on chain %u", serialno);
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);
112 /* check for things like BOS ordering, etc */
113 switch (state->codec) {
115 /* check we have no vorbis/speex chains yet */
116 g_hash_table_foreach (eos_chain_states, (GHFunc) fail_if_audio, NULL);
120 /* no checks (yet) */
126 } else if (ogg_page_eos (page)) {
127 fail_unless (state != NULL, "Missing BOS flag on chain %u", serialno);
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);
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;
145 is_video (gpointer key, ChainState * state, gpointer data)
147 if (state->codec == CODEC_THEORA)
148 *((gboolean *) data) = TRUE;
152 check_chain_final_state (gpointer key, ChainState * state, gpointer data)
154 fail_unless (state->eos, "missing EOS flag on chain %u", state->serialno);
156 /* return TRUE to empty the chain table */
160 static GstProbeReturn
161 eos_buffer_probe (GstPad * pad, GstProbeType type, GstBuffer * buffer,
167 ChainState *state = NULL;
168 gboolean has_video = FALSE;
170 size = gst_buffer_get_size (buffer);
172 oggbuffer = ogg_sync_buffer (&oggsync, size);
173 gst_buffer_extract (buffer, 0, oggbuffer, size);
174 ogg_sync_wrote (&oggsync, size);
179 ret = ogg_sync_pageout (&oggsync, &page);
181 state = validate_ogg_page (&page);
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 IN_CAPS), non-video buffers
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_IN_CAPS))
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");
203 start_pipeline (GstElement * bin, GstPad * pad)
205 GstStateChangeReturn ret;
207 ogg_sync_init (&oggsync);
210 g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, g_free);
212 gst_pad_add_probe (pad, GST_PROBE_TYPE_BUFFER,
213 (GstPadProbeCallback) eos_buffer_probe, NULL, NULL);
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");
224 stop_pipeline (GstElement * bin, GstPad * pad)
226 GstStateChangeReturn ret;
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");
235 gst_pad_remove_probe (pad, probe_id);
236 ogg_sync_clear (&oggsync);
238 /* check end conditions, such as EOS flags */
239 g_hash_table_foreach_remove (eos_chain_states,
240 (GHRFunc) check_chain_final_state, NULL);
244 eos_watch (GstBus * bus, GstMessage * message, GMainLoop * loop)
246 if (GST_MESSAGE_TYPE (message) == GST_MESSAGE_EOS) {
247 g_main_loop_quit (loop);
253 test_pipeline (const char *pipeline)
255 GstElement *bin, *sink;
256 GstPad *pad, *sinkpad;
258 GError *error = NULL;
260 GstPadLinkReturn linkret;
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");
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");
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);
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);
287 start_pipeline (bin, pad);
288 g_main_loop_run (loop);
290 /* we're EOS now; make sure oggmux out caps have stream headers on them */
295 muxcaps = gst_pad_get_negotiated_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",
302 gst_caps_unref (muxcaps);
305 stop_pipeline (bin, pad);
308 g_main_loop_unref (loop);
309 g_source_remove (bus_watch);
310 gst_object_unref (pad);
311 gst_object_unref (bin);
316 GST_START_TEST (test_vorbis)
319 ("audiotestsrc num-buffers=5 ! audioconvert ! vorbisenc ! oggmux");
324 GST_START_TEST (test_vorbis_oggmux_unlinked)
329 pipe = gst_parse_launch ("audiotestsrc ! vorbisenc ! oggmux", NULL);
331 g_printerr ("Skipping test 'test_vorbis_oggmux_unlinked'");
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,
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);
350 GST_START_TEST (test_theora)
353 ("videotestsrc num-buffers=5 ! videoconvert ! theoraenc ! oggmux");
359 #if (defined (HAVE_THEORA) && defined (HAVE_VORBIS))
360 GST_START_TEST (test_theora_vorbis)
363 ("videotestsrc num-buffers=10 ! videoconvert ! theoraenc ! queue ! oggmux name=mux "
364 "audiotestsrc num-buffers=2 ! audioconvert ! vorbisenc ! queue ! mux.");
369 GST_START_TEST (test_vorbis_theora)
372 ("videotestsrc num-buffers=2 ! videoconvert ! theoraenc ! queue ! oggmux name=mux "
373 "audiotestsrc num-buffers=10 ! audioconvert ! vorbisenc ! queue ! mux.");
379 GST_START_TEST (test_simple_cleanup)
383 oggmux = gst_element_factory_make ("oggmux", NULL);
384 gst_object_unref (oggmux);
389 GST_START_TEST (test_request_pad_cleanup)
394 oggmux = gst_element_factory_make ("oggmux", NULL);
395 pad = gst_element_get_request_pad (oggmux, "sink_%d");
396 fail_unless (pad != NULL);
397 gst_object_unref (pad);
398 pad = gst_element_get_request_pad (oggmux, "sink_%d");
399 fail_unless (pad != NULL);
400 gst_object_unref (pad);
401 gst_object_unref (oggmux);
409 Suite *s = suite_create ("oggmux");
410 TCase *tc_chain = tcase_create ("general");
412 suite_add_tcase (s, tc_chain);
414 tcase_add_test (tc_chain, test_vorbis);
415 tcase_add_test (tc_chain, test_vorbis_oggmux_unlinked);
419 tcase_add_test (tc_chain, test_theora);
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);
427 tcase_add_test (tc_chain, test_simple_cleanup);
428 tcase_add_test (tc_chain, test_request_pad_cleanup);
432 GST_CHECK_MAIN (oggmux);