tizen 2.3 release
[framework/multimedia/gst-plugins-base0.10.git] / tests / check / pipelines / oggmux.c
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., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, 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
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
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 *
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
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
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 gboolean
161 eos_buffer_probe (GstPad * pad, GstBuffer * buffer, gpointer unused)
162 {
163   gint ret;
164   gint size;
165   guint8 *data;
166   gchar *oggbuffer;
167   ChainState *state = NULL;
168   gboolean has_video = FALSE;
169
170   size = GST_BUFFER_SIZE (buffer);
171   data = GST_BUFFER_DATA (buffer);
172
173   oggbuffer = ogg_sync_buffer (&oggsync, size);
174   memcpy (oggbuffer, data, size);
175   ogg_sync_wrote (&oggsync, size);
176
177   do {
178     ogg_page page;
179
180     ret = ogg_sync_pageout (&oggsync, &page);
181     if (ret > 0)
182       state = validate_ogg_page (&page);
183   }
184   while (ret != 0);
185
186   if (state) {
187     /* Now, we can do buffer-level checks...
188      * If we have video somewhere, then we should have DELTA_UNIT set on all
189      * non-header (not IN_CAPS), non-video buffers
190      */
191     g_hash_table_foreach (eos_chain_states, (GHFunc) is_video, &has_video);
192     if (has_video && state->codec != CODEC_THEORA) {
193       if (!GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_IN_CAPS))
194         fail_unless (GST_BUFFER_FLAG_IS_SET (buffer,
195                 GST_BUFFER_FLAG_DELTA_UNIT),
196             "Non-video buffer doesn't have DELTA_UNIT in stream with video");
197     }
198   }
199
200   return TRUE;
201 }
202
203 static void
204 start_pipeline (GstElement * bin, GstPad * pad)
205 {
206   GstStateChangeReturn ret;
207
208   ogg_sync_init (&oggsync);
209
210   eos_chain_states =
211       g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, g_free);
212   probe_id =
213       gst_pad_add_buffer_probe (pad, G_CALLBACK (eos_buffer_probe), 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
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_buffer_probe (pad, (guint) 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
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
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_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",
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
316 GST_START_TEST (test_vorbis)
317 {
318   test_pipeline
319       ("audiotestsrc num-buffers=5 ! audioconvert ! vorbisenc ! oggmux");
320 }
321
322 GST_END_TEST;
323
324 GST_START_TEST (test_vorbis_oggmux_unlinked)
325 {
326   GstElement *pipe;
327   GstMessage *msg;
328
329   pipe = gst_parse_launch ("audiotestsrc ! vorbisenc ! 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
350 GST_START_TEST (test_theora)
351 {
352   test_pipeline
353       ("videotestsrc num-buffers=5 ! ffmpegcolorspace ! theoraenc ! oggmux");
354 }
355
356 GST_END_TEST;
357 #endif
358
359 #if (defined (HAVE_THEORA) && defined (HAVE_VORBIS))
360 GST_START_TEST (test_theora_vorbis)
361 {
362   test_pipeline
363       ("videotestsrc num-buffers=10 ! ffmpegcolorspace ! theoraenc ! queue ! oggmux name=mux "
364       "audiotestsrc num-buffers=2 ! audioconvert ! vorbisenc ! queue ! mux.");
365 }
366
367 GST_END_TEST;
368
369 GST_START_TEST (test_vorbis_theora)
370 {
371   test_pipeline
372       ("videotestsrc num-buffers=2 ! ffmpegcolorspace ! theoraenc ! queue ! oggmux name=mux "
373       "audiotestsrc num-buffers=10 ! audioconvert ! vorbisenc ! queue ! mux.");
374 }
375
376 GST_END_TEST;
377 #endif
378
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
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, "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);
402 }
403
404 GST_END_TEST;
405
406 static Suite *
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);