make %u in all request pad templates
[platform/upstream/gst-plugins-good.git] / tests / check / elements / avimux.c
1 /* GStreamer
2  *
3  * unit test for avimux
4  *
5  * Copyright (C) <2006> Mark Nauwelaerts <manauw@skynet.be>
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 #include <unistd.h>
24
25 #include <gst/check/gstcheck.h>
26
27 /* For ease of programming we use globals to keep refs for our floating
28  * src and sink pads we create; otherwise we always have to do get_pad,
29  * get_peer, and then remove references in every test function */
30 static GstPad *mysrcpad, *mysinkpad;
31
32 #define AUDIO_CAPS_STRING "audio/x-ac3, " \
33                         "channels = (int) 1, " \
34                         "rate = (int) 8000"
35 #define VIDEO_CAPS_STRING "video/x-xvid, " \
36                            "width = (int) 384, " \
37                            "height = (int) 288, " \
38                            "framerate = (fraction) 25/1"
39
40 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
41     GST_PAD_SINK,
42     GST_PAD_ALWAYS,
43     GST_STATIC_CAPS ("video/x-msvideo"));
44 static GstStaticPadTemplate srcvideotemplate = GST_STATIC_PAD_TEMPLATE ("src",
45     GST_PAD_SRC,
46     GST_PAD_ALWAYS,
47     GST_STATIC_CAPS (VIDEO_CAPS_STRING));
48
49 static GstStaticPadTemplate srcaudiotemplate = GST_STATIC_PAD_TEMPLATE ("src",
50     GST_PAD_SRC,
51     GST_PAD_ALWAYS,
52     GST_STATIC_CAPS (AUDIO_CAPS_STRING));
53
54
55 /* setup and teardown needs some special handling for muxer */
56 static GstPad *
57 setup_src_pad (GstElement * element,
58     GstStaticPadTemplate * template, GstCaps * caps, const gchar * sinkname)
59 {
60   GstPad *srcpad, *sinkpad;
61
62   GST_DEBUG_OBJECT (element, "setting up sending pad");
63   /* sending pad */
64   srcpad = gst_pad_new_from_static_template (template, "src");
65   fail_if (srcpad == NULL, "Could not create a srcpad");
66   ASSERT_OBJECT_REFCOUNT (srcpad, "srcpad", 1);
67
68   if (!(sinkpad = gst_element_get_static_pad (element, sinkname)))
69     sinkpad = gst_element_get_request_pad (element, sinkname);
70   fail_if (sinkpad == NULL, "Could not get sink pad from %s",
71       GST_ELEMENT_NAME (element));
72   /* references are owned by: 1) us, 2) avimux, 3) collect pads */
73   ASSERT_OBJECT_REFCOUNT (sinkpad, "sinkpad", 3);
74   if (caps)
75     fail_unless (gst_pad_set_caps (srcpad, caps));
76   fail_unless (gst_pad_link (srcpad, sinkpad) == GST_PAD_LINK_OK,
77       "Could not link source and %s sink pads", GST_ELEMENT_NAME (element));
78   gst_object_unref (sinkpad);   /* because we got it higher up */
79
80   /* references are owned by: 1) avimux, 2) collect pads */
81   ASSERT_OBJECT_REFCOUNT (sinkpad, "sinkpad", 2);
82
83   return srcpad;
84 }
85
86 static void
87 teardown_src_pad (GstElement * element, const gchar * sinkname)
88 {
89   GstPad *srcpad, *sinkpad;
90   gchar *padname;
91
92   /* clean up floating src pad */
93   /* hm, avimux uses _00 as suffixes for padnames */
94   padname = g_strdup (sinkname);
95   memcpy (strchr (padname, '%'), "00", 2);
96   if (!(sinkpad = gst_element_get_static_pad (element, padname)))
97     sinkpad = gst_element_get_request_pad (element, padname);
98   g_free (padname);
99   /* pad refs held by 1) avimux 2) collectpads and 3) us (through _get) */
100   ASSERT_OBJECT_REFCOUNT (sinkpad, "sinkpad", 3);
101   srcpad = gst_pad_get_peer (sinkpad);
102
103   gst_pad_unlink (srcpad, sinkpad);
104
105   /* after unlinking, pad refs still held by
106    * 1) avimux and 2) collectpads and 3) us (through _get) */
107   ASSERT_OBJECT_REFCOUNT (sinkpad, "sinkpad", 3);
108   gst_object_unref (sinkpad);
109   /* one more ref is held by element itself */
110
111   /* pad refs held by both creator and this function (through _get_peer) */
112   ASSERT_OBJECT_REFCOUNT (srcpad, "srcpad", 2);
113   gst_object_unref (srcpad);
114   gst_object_unref (srcpad);
115
116 }
117
118 static GstElement *
119 setup_avimux (GstStaticPadTemplate * srctemplate, const gchar * sinkname)
120 {
121   GstElement *avimux;
122
123   GST_DEBUG ("setup_avimux");
124   avimux = gst_check_setup_element ("avimux");
125   mysrcpad = setup_src_pad (avimux, srctemplate, NULL, sinkname);
126   mysinkpad = gst_check_setup_sink_pad (avimux, &sinktemplate, NULL);
127   gst_pad_set_active (mysrcpad, TRUE);
128   gst_pad_set_active (mysinkpad, TRUE);
129
130   return avimux;
131 }
132
133 static void
134 cleanup_avimux (GstElement * avimux, const gchar * sinkname)
135 {
136   GST_DEBUG ("cleanup_avimux");
137   gst_element_set_state (avimux, GST_STATE_NULL);
138
139   gst_pad_set_active (mysrcpad, FALSE);
140   gst_pad_set_active (mysinkpad, FALSE);
141   teardown_src_pad (avimux, sinkname);
142   gst_check_teardown_sink_pad (avimux);
143   gst_check_teardown_element (avimux);
144 }
145
146 static void
147 check_avimux_pad (GstStaticPadTemplate * srctemplate,
148     const gchar * src_caps_string, const gchar * chunk_id,
149     const gchar * sinkname)
150 {
151   GstElement *avimux;
152   GstBuffer *inbuffer, *outbuffer;
153   GstCaps *caps;
154   int num_buffers;
155   int i;
156   guint8 data0[4] = "RIFF";
157   guint8 data1[8] = "AVI LIST";
158   guint8 data2[8] = "hdrlavih";
159   guint8 data3[4] = "LIST";
160   guint8 data4[8] = "strlstrh";
161   guint8 data5[4] = "strf";
162   guint8 data6[4] = "LIST";
163   guint8 data7[4] = "movi";
164
165   avimux = setup_avimux (srctemplate, sinkname);
166   fail_unless (gst_element_set_state (avimux,
167           GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
168       "could not set to playing");
169
170   inbuffer = gst_buffer_new_and_alloc (1);
171   caps = gst_caps_from_string (src_caps_string);
172   gst_buffer_set_caps (inbuffer, caps);
173   gst_caps_unref (caps);
174   GST_BUFFER_TIMESTAMP (inbuffer) = 0;
175   ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
176   fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
177   num_buffers = g_list_length (buffers);
178   /* at least expect avi header, chunk header, chunk and padding */
179   fail_unless (num_buffers >= 4);
180
181   for (i = 0; i < num_buffers; ++i) {
182     outbuffer = GST_BUFFER (buffers->data);
183     fail_if (outbuffer == NULL);
184     buffers = g_list_remove (buffers, outbuffer);
185
186     switch (i) {
187       case 0:{                 /* check riff header */
188         /* avi header */
189         guint8 *data = GST_BUFFER_DATA (outbuffer);
190
191         fail_unless (memcmp (data, data0, sizeof (data0)) == 0);
192         fail_unless (memcmp (data + 8, data1, sizeof (data1)) == 0);
193         fail_unless (memcmp (data + 20, data2, sizeof (data2)) == 0);
194         /* video or audio header */
195         data += 32 + 56;
196         fail_unless (memcmp (data, data3, sizeof (data3)) == 0);
197         fail_unless (memcmp (data + 8, data4, sizeof (data4)) == 0);
198         fail_unless (memcmp (data + 76, data5, sizeof (data5)) == 0);
199         /* avi data header */
200         data = GST_BUFFER_DATA (outbuffer);
201         data += GST_BUFFER_SIZE (outbuffer) - 12;
202         fail_unless (memcmp (data, data6, sizeof (data6)) == 0);
203         data += 8;
204         fail_unless (memcmp (data, data7, sizeof (data7)) == 0);
205         break;
206       }
207       case 1:                  /* chunk header */
208         fail_unless (GST_BUFFER_SIZE (outbuffer) == 8);
209         fail_unless (memcmp (GST_BUFFER_DATA (outbuffer), chunk_id, 4) == 0);
210         break;
211       case 2:
212         fail_unless (GST_BUFFER_SIZE (outbuffer) == 1);
213         break;
214       case 3:                  /* buffer we put in, must be padded to even size */
215         fail_unless (GST_BUFFER_SIZE (outbuffer) == 1);
216         break;
217       default:
218         break;
219     }
220
221     ASSERT_BUFFER_REFCOUNT (outbuffer, "outbuffer", 1);
222     gst_buffer_unref (outbuffer);
223     outbuffer = NULL;
224   }
225
226   cleanup_avimux (avimux, sinkname);
227   g_list_free (buffers);
228   buffers = NULL;
229 }
230
231
232 GST_START_TEST (test_video_pad)
233 {
234   check_avimux_pad (&srcvideotemplate, VIDEO_CAPS_STRING, "00db", "video_%u");
235 }
236
237 GST_END_TEST;
238
239
240 GST_START_TEST (test_audio_pad)
241 {
242   check_avimux_pad (&srcaudiotemplate, AUDIO_CAPS_STRING, "00wb", "audio_%u");
243 }
244
245 GST_END_TEST;
246
247
248 static Suite *
249 avimux_suite (void)
250 {
251   Suite *s = suite_create ("avimux");
252   TCase *tc_chain = tcase_create ("general");
253
254   suite_add_tcase (s, tc_chain);
255   tcase_add_test (tc_chain, test_video_pad);
256   tcase_add_test (tc_chain, test_audio_pad);
257
258   return s;
259 }
260
261 int
262 main (int argc, char **argv)
263 {
264   int nf;
265
266   Suite *s = avimux_suite ();
267   SRunner *sr = srunner_create (s);
268
269   gst_check_init (&argc, &argv);
270
271   srunner_run_all (sr, CK_NORMAL);
272   nf = srunner_ntests_failed (sr);
273   srunner_free (sr);
274
275   return nf;
276 }