Tizen 2.0 Release
[framework/multimedia/gst-plugins-good0.10.git] / tests / check / elements / flvmux.c
1 /* GStreamer unit tests for flvmux
2  *
3  * Copyright (C) 2009 Tim-Philipp Müller  <tim centricular net>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 #include <gst/check/gstcheck.h>
22
23 #include <gst/gst.h>
24
25 static GstBusSyncReply
26 error_cb (GstBus * bus, GstMessage * msg, gpointer user_data)
27 {
28   if (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_ERROR) {
29     GError *err = NULL;
30     gchar *dbg = NULL;
31
32     gst_message_parse_error (msg, &err, &dbg);
33     g_error ("ERROR: %s\n%s\n", err->message, dbg);
34   }
35
36   return GST_BUS_PASS;
37 }
38
39 static void
40 handoff_cb (GstElement * element, GstBuffer * buf, GstPad * pad,
41     gint * p_counter)
42 {
43   *p_counter += 1;
44   GST_LOG ("counter = %d", *p_counter);
45
46   fail_unless (GST_BUFFER_CAPS (buf) != NULL);
47 }
48
49 static void
50 mux_pcm_audio (guint num_buffers, guint repeat)
51 {
52   GstElement *src, *sink, *flvmux, *conv, *pipeline;
53   GstPad *sinkpad, *srcpad;
54   gint counter;
55
56   GST_LOG ("num_buffers = %u", num_buffers);
57
58   pipeline = gst_pipeline_new ("pipeline");
59   fail_unless (pipeline != NULL, "Failed to create pipeline!");
60
61   /* kids, don't use a sync handler for this at home, really; we do because
62    * we just want to abort and nothing else */
63   gst_bus_set_sync_handler (GST_ELEMENT_BUS (pipeline), error_cb, NULL);
64
65   src = gst_element_factory_make ("audiotestsrc", "audiotestsrc");
66   fail_unless (src != NULL, "Failed to create 'audiotestsrc' element!");
67
68   g_object_set (src, "num-buffers", num_buffers, NULL);
69
70   conv = gst_element_factory_make ("audioconvert", "audioconvert");
71   fail_unless (conv != NULL, "Failed to create 'audioconvert' element!");
72
73   flvmux = gst_element_factory_make ("flvmux", "flvmux");
74   fail_unless (flvmux != NULL, "Failed to create 'flvmux' element!");
75
76   sink = gst_element_factory_make ("fakesink", "fakesink");
77   fail_unless (sink != NULL, "Failed to create 'fakesink' element!");
78
79   g_object_set (sink, "signal-handoffs", TRUE, NULL);
80   g_signal_connect (sink, "handoff", G_CALLBACK (handoff_cb), &counter);
81
82   gst_bin_add_many (GST_BIN (pipeline), src, conv, flvmux, sink, NULL);
83
84   fail_unless (gst_element_link (src, conv));
85   fail_unless (gst_element_link (flvmux, sink));
86
87   /* now link the elements */
88   sinkpad = gst_element_get_request_pad (flvmux, "audio");
89   fail_unless (sinkpad != NULL, "Could not get audio request pad");
90
91   srcpad = gst_element_get_static_pad (conv, "src");
92   fail_unless (srcpad != NULL, "Could not get audioconvert's source pad");
93
94   fail_unless_equals_int (gst_pad_link (srcpad, sinkpad), GST_PAD_LINK_OK);
95
96   gst_object_unref (srcpad);
97   gst_object_unref (sinkpad);
98
99   do {
100     GstStateChangeReturn state_ret;
101     GstMessage *msg;
102
103     GST_LOG ("repeat=%d", repeat);
104
105     counter = 0;
106
107     state_ret = gst_element_set_state (pipeline, GST_STATE_PAUSED);
108     fail_unless (state_ret != GST_STATE_CHANGE_FAILURE);
109
110     if (state_ret == GST_STATE_CHANGE_ASYNC) {
111       GST_LOG ("waiting for pipeline to reach PAUSED state");
112       state_ret = gst_element_get_state (pipeline, NULL, NULL, -1);
113       fail_unless_equals_int (state_ret, GST_STATE_CHANGE_SUCCESS);
114     }
115
116     GST_LOG ("PAUSED, let's do the rest of it");
117
118     state_ret = gst_element_set_state (pipeline, GST_STATE_PLAYING);
119     fail_unless (state_ret != GST_STATE_CHANGE_FAILURE);
120
121     msg = gst_bus_poll (GST_ELEMENT_BUS (pipeline), GST_MESSAGE_EOS, -1);
122     fail_unless (msg != NULL, "Expected EOS message on bus!");
123
124     GST_LOG ("EOS");
125     gst_message_unref (msg);
126
127     /* should have some output */
128     fail_unless (counter > 2);
129
130     fail_unless_equals_int (gst_element_set_state (pipeline, GST_STATE_NULL),
131         GST_STATE_CHANGE_SUCCESS);
132
133     /* repeat = test re-usability */
134     --repeat;
135   } while (repeat > 0);
136
137   gst_object_unref (pipeline);
138 }
139
140 GST_START_TEST (test_index_writing)
141 {
142   /* note: there's a magic 128 value in flvmux when doing index writing */
143   if ((__i__ % 33) == 1)
144     mux_pcm_audio (__i__, 2);
145 }
146
147 GST_END_TEST;
148
149 static Suite *
150 flvmux_suite (void)
151 {
152   Suite *s = suite_create ("flvmux");
153   TCase *tc_chain = tcase_create ("general");
154
155   suite_add_tcase (s, tc_chain);
156   tcase_add_loop_test (tc_chain, test_index_writing, 1, 499);
157
158   return s;
159 }
160
161 GST_CHECK_MAIN (flvmux)