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., 51 Franklin St, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
29 #include <gst/check/gstcheck.h>
49 static ogg_sync_state oggsync;
50 static GHashTable *eos_chain_states;
51 static gulong probe_id;
54 get_page_codec (ogg_page * page)
56 ChainCodec codec = CODEC_UNKNOWN;
57 ogg_stream_state state;
60 ogg_stream_init (&state, ogg_page_serialno (page));
61 if (ogg_stream_pagein (&state, page) == 0) {
62 if (ogg_stream_packetpeek (&state, &packet) > 0) {
63 if (strncmp ((char *) &packet.packet[1], "vorbis",
64 strlen ("vorbis")) == 0)
66 else if (strncmp ((char *) &packet.packet[1], "theora",
67 strlen ("theora")) == 0)
69 else if (strncmp ((char *) &packet.packet[0], "Speex ",
70 strlen ("Speex ")) == 0)
74 ogg_stream_clear (&state);
80 fail_if_audio (gpointer key, ChainState * state, gpointer data)
82 fail_if (state->codec == CODEC_VORBIS,
83 "vorbis BOS occurred before theora BOS");
84 fail_if (state->codec == CODEC_SPEEX, "speex BOS occurred before theora BOS");
88 validate_ogg_page (ogg_page * page)
94 serialno = ogg_page_serialno (page);
95 granule = ogg_page_granulepos (page);
96 state = g_hash_table_lookup (eos_chain_states, GINT_TO_POINTER (serialno));
98 fail_if (ogg_page_packets (page) == 0 && granule != -1,
99 "Must have granulepos -1 when page has no packets, has %" G_GINT64_FORMAT,
102 if (ogg_page_bos (page)) {
103 fail_unless (state == NULL, "Extraneous BOS flag on chain %u", serialno);
105 state = g_new0 (ChainState, 1);
106 g_hash_table_insert (eos_chain_states, GINT_TO_POINTER (serialno), state);
107 state->serialno = serialno;
108 state->last_granule = granule;
109 state->codec = get_page_codec (page);
111 /* check for things like BOS ordering, etc */
112 switch (state->codec) {
114 /* check we have no vorbis/speex chains yet */
115 g_hash_table_foreach (eos_chain_states, (GHFunc) fail_if_audio, NULL);
119 /* no checks (yet) */
125 } else if (ogg_page_eos (page)) {
126 fail_unless (state != NULL, "Missing BOS flag on chain %u", serialno);
129 fail_unless (state != NULL, "Missing BOS flag on chain %u", serialno);
130 fail_unless (!state->eos, "Data after EOS flag on chain %u", serialno);
134 fail_unless (granule >= state->last_granule,
135 "Granulepos out-of-order for chain %u: old=%" G_GINT64_FORMAT ", new="
136 G_GINT64_FORMAT, serialno, state->last_granule, granule);
137 state->last_granule = granule;
144 is_video (gpointer key, ChainState * state, gpointer data)
146 if (state->codec == CODEC_THEORA)
147 *((gboolean *) data) = TRUE;
151 check_chain_final_state (gpointer key, ChainState * state, gpointer data)
153 fail_unless (state->eos, "missing EOS flag on chain %u", state->serialno);
155 /* return TRUE to empty the chain table */
159 static GstPadProbeReturn
160 eos_buffer_probe (GstPad * pad, GstPadProbeInfo * info, gpointer unused)
162 GstBuffer *buffer = GST_PAD_PROBE_INFO_BUFFER (info);
166 ChainState *state = NULL;
167 gboolean has_video = FALSE;
169 size = gst_buffer_get_size (buffer);
171 oggbuffer = ogg_sync_buffer (&oggsync, size);
172 gst_buffer_extract (buffer, 0, oggbuffer, size);
173 ogg_sync_wrote (&oggsync, size);
178 ret = ogg_sync_pageout (&oggsync, &page);
180 state = validate_ogg_page (&page);
185 /* Now, we can do buffer-level checks...
186 * If we have video somewhere, then we should have DELTA_UNIT set on all
187 * non-header (not HEADER), non-video buffers
189 g_hash_table_foreach (eos_chain_states, (GHFunc) is_video, &has_video);
190 if (has_video && state->codec != CODEC_THEORA) {
191 if (!GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_HEADER))
192 fail_unless (GST_BUFFER_FLAG_IS_SET (buffer,
193 GST_BUFFER_FLAG_DELTA_UNIT),
194 "Non-video buffer doesn't have DELTA_UNIT in stream with video");
198 return GST_PAD_PROBE_OK;
202 start_pipeline (GstElement * bin, GstPad * pad)
204 GstStateChangeReturn ret;
206 ogg_sync_init (&oggsync);
209 g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, g_free);
211 gst_pad_add_probe (pad, GST_PAD_PROBE_TYPE_BUFFER,
212 (GstPadProbeCallback) eos_buffer_probe, NULL, NULL);
214 ret = gst_element_set_state (bin, GST_STATE_PLAYING);
215 fail_if (ret == GST_STATE_CHANGE_FAILURE, "Could not start test pipeline");
216 if (ret == GST_STATE_CHANGE_ASYNC) {
217 ret = gst_element_get_state (bin, NULL, NULL, GST_CLOCK_TIME_NONE);
218 fail_if (ret != GST_STATE_CHANGE_SUCCESS, "Could not start test pipeline");
223 stop_pipeline (GstElement * bin, GstPad * pad)
225 GstStateChangeReturn ret;
227 ret = gst_element_set_state (bin, GST_STATE_NULL);
228 fail_if (ret == GST_STATE_CHANGE_FAILURE, "Could not stop test pipeline");
229 if (ret == GST_STATE_CHANGE_ASYNC) {
230 ret = gst_element_get_state (bin, NULL, NULL, GST_CLOCK_TIME_NONE);
231 fail_if (ret != GST_STATE_CHANGE_SUCCESS, "Could not stop test pipeline");
234 gst_pad_remove_probe (pad, probe_id);
235 ogg_sync_clear (&oggsync);
237 /* check end conditions, such as EOS flags */
238 g_hash_table_foreach_remove (eos_chain_states,
239 (GHRFunc) check_chain_final_state, NULL);
243 eos_watch (GstBus * bus, GstMessage * message, GMainLoop * loop)
245 if (GST_MESSAGE_TYPE (message) == GST_MESSAGE_EOS) {
246 g_main_loop_quit (loop);
252 test_pipeline (const char *pipeline)
254 GstElement *bin, *sink;
255 GstPad *pad, *sinkpad;
257 GError *error = NULL;
259 GstPadLinkReturn linkret;
262 bin = gst_parse_launch (pipeline, &error);
263 fail_unless (bin != NULL, "Error parsing pipeline: %s",
264 error ? error->message : "(invalid error)");
265 pad = gst_bin_find_unlinked_pad (GST_BIN (bin), GST_PAD_SRC);
266 fail_unless (pad != NULL, "Could not locate free src pad");
268 /* connect the fake sink */
269 sink = gst_element_factory_make ("fakesink", "fake_sink");
270 fail_unless (sink != NULL, "Could create fakesink");
271 fail_unless (gst_bin_add (GST_BIN (bin), sink), "Could not insert fakesink");
272 sinkpad = gst_element_get_static_pad (sink, "sink");
273 fail_unless (sinkpad != NULL, "Could not get fakesink src pad");
275 linkret = gst_pad_link (pad, sinkpad);
276 fail_unless (GST_PAD_LINK_SUCCESSFUL (linkret),
277 "Could not link to fake sink");
278 gst_object_unref (sinkpad);
280 /* run until we receive EOS */
281 loop = g_main_loop_new (NULL, FALSE);
282 bus = gst_element_get_bus (bin);
283 bus_watch = gst_bus_add_watch (bus, (GstBusFunc) eos_watch, loop);
284 gst_object_unref (bus);
286 start_pipeline (bin, pad);
287 g_main_loop_run (loop);
289 /* we're EOS now; make sure oggmux out caps have stream headers on them */
294 muxcaps = gst_pad_get_current_caps (sinkpad);
295 fail_unless (muxcaps != NULL);
296 s = gst_caps_get_structure (muxcaps, 0);
297 fail_unless (gst_structure_has_name (s, "application/ogg"));
298 fail_unless (gst_structure_has_field (s, "streamheader"));
299 fail_unless (gst_structure_has_field_typed (s, "streamheader",
301 gst_caps_unref (muxcaps);
304 stop_pipeline (bin, pad);
307 g_main_loop_unref (loop);
308 g_source_remove (bus_watch);
309 gst_object_unref (pad);
310 gst_object_unref (bin);
313 GST_START_TEST (test_vorbis)
316 ("audiotestsrc num-buffers=5 ! audioconvert ! vorbisenc ! .audio_%u oggmux");
321 GST_START_TEST (test_vorbis_oggmux_unlinked)
326 pipe = gst_parse_launch ("audiotestsrc ! vorbisenc ! .audio_%u oggmux", NULL);
328 g_printerr ("Skipping test 'test_vorbis_oggmux_unlinked'");
331 /* no sink, no async state change */
332 fail_unless_equals_int (gst_element_set_state (pipe, GST_STATE_PAUSED),
333 GST_STATE_CHANGE_SUCCESS);
334 /* we expect an error (without any criticals/warnings) */
335 msg = gst_bus_timed_pop_filtered (GST_ELEMENT_BUS (pipe), -1,
337 gst_message_unref (msg);
338 fail_unless_equals_int (gst_element_set_state (pipe, GST_STATE_NULL),
339 GST_STATE_CHANGE_SUCCESS);
340 gst_object_unref (pipe);
345 GST_START_TEST (test_theora)
348 ("videotestsrc num-buffers=5 ! videoconvert ! theoraenc ! .video_%u oggmux");
353 GST_START_TEST (test_theora_vorbis)
356 ("videotestsrc num-buffers=10 ! videoconvert ! theoraenc ! queue ! .video_%u oggmux name=mux "
357 "audiotestsrc num-buffers=2 ! audioconvert ! vorbisenc ! queue ! mux.audio_%u");
362 GST_START_TEST (test_vorbis_theora)
365 ("videotestsrc num-buffers=2 ! videoconvert ! theoraenc ! queue ! .video_%u oggmux name=mux "
366 "audiotestsrc num-buffers=10 ! audioconvert ! vorbisenc ! queue ! mux.audio_%u");
371 GST_START_TEST (test_simple_cleanup)
375 oggmux = gst_element_factory_make ("oggmux", NULL);
376 gst_object_unref (oggmux);
381 GST_START_TEST (test_request_pad_cleanup)
386 oggmux = gst_element_factory_make ("oggmux", NULL);
387 pad = gst_element_get_request_pad (oggmux, "video_%u");
388 fail_unless (pad != NULL);
389 gst_object_unref (pad);
390 pad = gst_element_get_request_pad (oggmux, "audio_%u");
391 fail_unless (pad != NULL);
392 gst_object_unref (pad);
393 gst_object_unref (oggmux);
401 gboolean have_vorbisenc;
402 gboolean have_theoraenc;
404 Suite *s = suite_create ("oggmux");
405 TCase *tc_chain = tcase_create ("general");
408 gst_registry_check_feature_version (gst_registry_get (), "vorbisenc",
409 GST_VERSION_MAJOR, GST_VERSION_MINOR, 0);
412 gst_registry_check_feature_version (gst_registry_get (), "theoraenc",
413 GST_VERSION_MAJOR, GST_VERSION_MINOR, 0);
415 suite_add_tcase (s, tc_chain);
417 if (have_vorbisenc) {
418 tcase_add_test (tc_chain, test_vorbis);
419 tcase_add_test (tc_chain, test_vorbis_oggmux_unlinked);
422 if (have_theoraenc) {
423 tcase_add_test (tc_chain, test_theora);
426 if (have_vorbisenc && have_theoraenc) {
427 tcase_add_test (tc_chain, test_vorbis_theora);
428 tcase_add_test (tc_chain, test_theora_vorbis);
431 tcase_add_test (tc_chain, test_simple_cleanup);
432 tcase_add_test (tc_chain, test_request_pad_cleanup);
436 GST_CHECK_MAIN (oggmux);