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 static ogg_sync_state oggsync;
50 static GHashTable *eos_chain_states;
51 static gulong probe_id;
52
53 static ChainCodec
get_page_codec(ogg_page * page)54 get_page_codec (ogg_page * page)
55 {
56 ChainCodec codec = CODEC_UNKNOWN;
57 ogg_stream_state state;
58 ogg_packet packet;
59
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)
65 codec = CODEC_VORBIS;
66 else if (strncmp ((char *) &packet.packet[1], "theora",
67 strlen ("theora")) == 0)
68 codec = CODEC_THEORA;
69 else if (strncmp ((char *) &packet.packet[0], "Speex ",
70 strlen ("Speex ")) == 0)
71 codec = CODEC_SPEEX;
72 }
73 }
74 ogg_stream_clear (&state);
75
76 return codec;
77 }
78
79 static void
fail_if_audio(gpointer key,ChainState * state,gpointer data)80 fail_if_audio (gpointer key, ChainState * state, gpointer data)
81 {
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");
85 }
86
87 static ChainState *
validate_ogg_page(ogg_page * page)88 validate_ogg_page (ogg_page * page)
89 {
90 gulong serialno;
91 gint64 granule;
92 ChainState *state;
93
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));
97
98 fail_if (ogg_page_packets (page) == 0 && granule != -1,
99 "Must have granulepos -1 when page has no packets, has %" G_GINT64_FORMAT,
100 granule);
101
102 if (ogg_page_bos (page)) {
103 fail_unless (state == NULL, "Extraneous BOS flag on chain %u", serialno);
104
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);
110
111 /* check for things like BOS ordering, etc */
112 switch (state->codec) {
113 case CODEC_THEORA:
114 /* check we have no vorbis/speex chains yet */
115 g_hash_table_foreach (eos_chain_states, (GHFunc) fail_if_audio, NULL);
116 break;
117 case CODEC_VORBIS:
118 case CODEC_SPEEX:
119 /* no checks (yet) */
120 break;
121 case CODEC_UNKNOWN:
122 default:
123 break;
124 }
125 } else if (ogg_page_eos (page)) {
126 fail_unless (state != NULL, "Missing BOS flag on chain %u", serialno);
127 state->eos = TRUE;
128 } else {
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);
131 }
132
133 if (granule != -1) {
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;
138 }
139
140 return state;
141 }
142
143 static void
is_video(gpointer key,ChainState * state,gpointer data)144 is_video (gpointer key, ChainState * state, gpointer data)
145 {
146 if (state->codec == CODEC_THEORA)
147 *((gboolean *) data) = TRUE;
148 }
149
150 static gboolean
check_chain_final_state(gpointer key,ChainState * state,gpointer data)151 check_chain_final_state (gpointer key, ChainState * state, gpointer data)
152 {
153 fail_unless (state->eos, "missing EOS flag on chain %u", state->serialno);
154
155 /* return TRUE to empty the chain table */
156 return TRUE;
157 }
158
159 static GstPadProbeReturn
eos_buffer_probe(GstPad * pad,GstPadProbeInfo * info,gpointer unused)160 eos_buffer_probe (GstPad * pad, GstPadProbeInfo * info, gpointer unused)
161 {
162 GstBuffer *buffer = GST_PAD_PROBE_INFO_BUFFER (info);
163 gint ret;
164 gint size;
165 gchar *oggbuffer;
166 ChainState *state = NULL;
167 gboolean has_video = FALSE;
168
169 size = gst_buffer_get_size (buffer);
170
171 oggbuffer = ogg_sync_buffer (&oggsync, size);
172 gst_buffer_extract (buffer, 0, oggbuffer, size);
173 ogg_sync_wrote (&oggsync, size);
174
175 do {
176 ogg_page page;
177
178 ret = ogg_sync_pageout (&oggsync, &page);
179 if (ret > 0)
180 state = validate_ogg_page (&page);
181 }
182 while (ret != 0);
183
184 if (state) {
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
188 */
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");
195 }
196 }
197
198 return GST_PAD_PROBE_OK;
199 }
200
201 static void
start_pipeline(GstElement * bin,GstPad * pad)202 start_pipeline (GstElement * bin, GstPad * pad)
203 {
204 GstStateChangeReturn ret;
205
206 ogg_sync_init (&oggsync);
207
208 eos_chain_states =
209 g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, g_free);
210 probe_id =
211 gst_pad_add_probe (pad, GST_PAD_PROBE_TYPE_BUFFER,
212 (GstPadProbeCallback) eos_buffer_probe, NULL, NULL);
213
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");
219 }
220 }
221
222 static void
stop_pipeline(GstElement * bin,GstPad * pad)223 stop_pipeline (GstElement * bin, GstPad * pad)
224 {
225 GstStateChangeReturn ret;
226
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");
232 }
233
234 gst_pad_remove_probe (pad, probe_id);
235 ogg_sync_clear (&oggsync);
236
237 /* check end conditions, such as EOS flags */
238 g_hash_table_foreach_remove (eos_chain_states,
239 (GHRFunc) check_chain_final_state, NULL);
240 }
241
242 static gboolean
eos_watch(GstBus * bus,GstMessage * message,GMainLoop * loop)243 eos_watch (GstBus * bus, GstMessage * message, GMainLoop * loop)
244 {
245 if (GST_MESSAGE_TYPE (message) == GST_MESSAGE_EOS) {
246 g_main_loop_quit (loop);
247 }
248 return TRUE;
249 }
250
251 static void
test_pipeline(const char * pipeline)252 test_pipeline (const char *pipeline)
253 {
254 GstElement *bin, *sink;
255 GstPad *pad, *sinkpad;
256 GstBus *bus;
257 GError *error = NULL;
258 GMainLoop *loop;
259 GstPadLinkReturn linkret;
260 guint bus_watch = 0;
261
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");
267
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");
274
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);
279
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);
285
286 start_pipeline (bin, pad);
287 g_main_loop_run (loop);
288
289 /* we're EOS now; make sure oggmux out caps have stream headers on them */
290 {
291 GstStructure *s;
292 GstCaps *muxcaps;
293
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",
300 GST_TYPE_ARRAY));
301 gst_caps_unref (muxcaps);
302 }
303
304 stop_pipeline (bin, pad);
305
306 /* clean up */
307 g_main_loop_unref (loop);
308 g_source_remove (bus_watch);
309 gst_object_unref (pad);
310 gst_object_unref (bin);
311 }
312
GST_START_TEST(test_vorbis)313 GST_START_TEST (test_vorbis)
314 {
315 test_pipeline
316 ("audiotestsrc num-buffers=5 ! audioconvert ! vorbisenc ! .audio_%u oggmux");
317 }
318
319 GST_END_TEST;
320
GST_START_TEST(test_vorbis_oggmux_unlinked)321 GST_START_TEST (test_vorbis_oggmux_unlinked)
322 {
323 GstElement *pipe;
324 GstMessage *msg;
325
326 pipe = gst_parse_launch ("audiotestsrc ! vorbisenc ! .audio_%u oggmux", NULL);
327 if (pipe == NULL) {
328 g_printerr ("Skipping test 'test_vorbis_oggmux_unlinked'");
329 return;
330 }
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,
336 GST_MESSAGE_ERROR);
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);
341 }
342
343 GST_END_TEST;
344
GST_START_TEST(test_theora)345 GST_START_TEST (test_theora)
346 {
347 test_pipeline
348 ("videotestsrc num-buffers=5 ! videoconvert ! theoraenc ! .video_%u oggmux");
349 }
350
351 GST_END_TEST;
352
GST_START_TEST(test_theora_vorbis)353 GST_START_TEST (test_theora_vorbis)
354 {
355 test_pipeline
356 ("videotestsrc num-buffers=10 ! videoconvert ! theoraenc ! queue ! .video_%u oggmux name=mux "
357 "audiotestsrc num-buffers=2 ! audioconvert ! vorbisenc ! queue ! mux.audio_%u");
358 }
359
360 GST_END_TEST;
361
GST_START_TEST(test_vorbis_theora)362 GST_START_TEST (test_vorbis_theora)
363 {
364 test_pipeline
365 ("videotestsrc num-buffers=2 ! videoconvert ! theoraenc ! queue ! .video_%u oggmux name=mux "
366 "audiotestsrc num-buffers=10 ! audioconvert ! vorbisenc ! queue ! mux.audio_%u");
367 }
368
369 GST_END_TEST;
370
GST_START_TEST(test_simple_cleanup)371 GST_START_TEST (test_simple_cleanup)
372 {
373 GstElement *oggmux;
374
375 oggmux = gst_element_factory_make ("oggmux", NULL);
376 gst_object_unref (oggmux);
377 }
378
379 GST_END_TEST;
380
GST_START_TEST(test_request_pad_cleanup)381 GST_START_TEST (test_request_pad_cleanup)
382 {
383 GstElement *oggmux;
384 GstPad *pad;
385
386 oggmux = gst_element_factory_make ("oggmux", NULL);
387 pad = gst_element_request_pad_simple (oggmux, "video_%u");
388 fail_unless (pad != NULL);
389 gst_object_unref (pad);
390 pad = gst_element_request_pad_simple (oggmux, "audio_%u");
391 fail_unless (pad != NULL);
392 gst_object_unref (pad);
393 gst_object_unref (oggmux);
394 }
395
396 GST_END_TEST;
397
398 static Suite *
oggmux_suite(void)399 oggmux_suite (void)
400 {
401 gboolean have_vorbisenc;
402 gboolean have_theoraenc;
403
404 Suite *s = suite_create ("oggmux");
405 TCase *tc_chain = tcase_create ("general");
406
407 have_vorbisenc =
408 gst_registry_check_feature_version (gst_registry_get (), "vorbisenc",
409 GST_VERSION_MAJOR, GST_VERSION_MINOR, 0);
410
411 have_theoraenc =
412 gst_registry_check_feature_version (gst_registry_get (), "theoraenc",
413 GST_VERSION_MAJOR, GST_VERSION_MINOR, 0);
414
415 suite_add_tcase (s, tc_chain);
416
417 if (have_vorbisenc) {
418 tcase_add_test (tc_chain, test_vorbis);
419 tcase_add_test (tc_chain, test_vorbis_oggmux_unlinked);
420 }
421
422 if (have_theoraenc) {
423 tcase_add_test (tc_chain, test_theora);
424 }
425
426 if (have_vorbisenc && have_theoraenc) {
427 tcase_add_test (tc_chain, test_vorbis_theora);
428 tcase_add_test (tc_chain, test_theora_vorbis);
429 }
430
431 tcase_add_test (tc_chain, test_simple_cleanup);
432 tcase_add_test (tc_chain, test_request_pad_cleanup);
433 return s;
434 }
435
436 GST_CHECK_MAIN (oggmux);
437