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., 51 Franklin St, Fifth Floor,
20  * Boston, MA 02110-1301, USA.
21  */
22 
23 
24 #include <gst/check/gstcheck.h>
25 
26 /* For ease of programming we use globals to keep refs for our floating
27  * src and sink pads we create; otherwise we always have to do get_pad,
28  * get_peer, and then remove references in every test function */
29 static GstPad *mysrcpad, *mysinkpad;
30 
31 #define AUDIO_CAPS_STRING "audio/x-ac3, " \
32                         "channels = (int) 1, " \
33                         "rate = (int) 8000"
34 #define VIDEO_CAPS_STRING "video/mpeg, mpegversion = (int) 4, " \
35                            "systemstream = (bool) false, " \
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 *
setup_src_pad(GstElement * element,GstStaticPadTemplate * template,GstCaps * caps,const gchar * sinkname)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
teardown_src_pad(GstElement * element,const gchar * sinkname)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   padname = g_strdup (sinkname);
94   memcpy (strchr (padname, '%'), "0", 2);
95   if (!(sinkpad = gst_element_get_static_pad (element, padname)))
96     sinkpad = gst_element_get_request_pad (element, padname);
97   g_free (padname);
98   /* pad refs held by 1) avimux 2) collectpads and 3) us (through _get) */
99   ASSERT_OBJECT_REFCOUNT (sinkpad, "sinkpad", 3);
100   srcpad = gst_pad_get_peer (sinkpad);
101 
102   gst_pad_unlink (srcpad, sinkpad);
103 
104   /* after unlinking, pad refs still held by
105    * 1) avimux and 2) collectpads and 3) us (through _get) */
106   ASSERT_OBJECT_REFCOUNT (sinkpad, "sinkpad", 3);
107   gst_object_unref (sinkpad);
108   /* one more ref is held by element itself */
109 
110   /* pad refs held by both creator and this function (through _get_peer) */
111   ASSERT_OBJECT_REFCOUNT (srcpad, "srcpad", 2);
112   gst_object_unref (srcpad);
113   gst_object_unref (srcpad);
114 
115 }
116 
117 static GstElement *
setup_avimux(GstStaticPadTemplate * srctemplate,const gchar * sinkname)118 setup_avimux (GstStaticPadTemplate * srctemplate, const gchar * sinkname)
119 {
120   GstElement *avimux;
121 
122   GST_DEBUG ("setup_avimux");
123   avimux = gst_check_setup_element ("avimux");
124   mysrcpad = setup_src_pad (avimux, srctemplate, NULL, sinkname);
125   mysinkpad = gst_check_setup_sink_pad (avimux, &sinktemplate);
126   gst_pad_set_active (mysrcpad, TRUE);
127   gst_pad_set_active (mysinkpad, TRUE);
128 
129   return avimux;
130 }
131 
132 static void
cleanup_avimux(GstElement * avimux,const gchar * sinkname)133 cleanup_avimux (GstElement * avimux, const gchar * sinkname)
134 {
135   GST_DEBUG ("cleanup_avimux");
136   gst_element_set_state (avimux, GST_STATE_NULL);
137 
138   gst_pad_set_active (mysrcpad, FALSE);
139   gst_pad_set_active (mysinkpad, FALSE);
140   teardown_src_pad (avimux, sinkname);
141   gst_check_teardown_sink_pad (avimux);
142   gst_check_teardown_element (avimux);
143 }
144 
145 static void
check_avimux_pad(GstStaticPadTemplate * srctemplate,const gchar * src_caps_string,const gchar * chunk_id,const gchar * sinkname)146 check_avimux_pad (GstStaticPadTemplate * srctemplate,
147     const gchar * src_caps_string, const gchar * chunk_id,
148     const gchar * sinkname)
149 {
150   GstElement *avimux;
151   GstBuffer *inbuffer, *outbuffer;
152   GstCaps *caps;
153   int num_buffers;
154   int i;
155   guint8 data0[4] = "RIFF";
156   guint8 data1[8] = "AVI LIST";
157   guint8 data2[8] = "hdrlavih";
158   guint8 data3[4] = "LIST";
159   guint8 data4[8] = "strlstrh";
160   guint8 data5[4] = "strf";
161   guint8 data6[4] = "LIST";
162   guint8 data7[4] = "movi";
163 
164   avimux = setup_avimux (srctemplate, sinkname);
165   fail_unless (gst_element_set_state (avimux,
166           GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
167       "could not set to playing");
168 
169   inbuffer = gst_buffer_new_and_alloc (1);
170   caps = gst_caps_from_string (src_caps_string);
171   gst_check_setup_events (mysrcpad, avimux, caps, GST_FORMAT_TIME);
172   gst_caps_unref (caps);
173 
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         GstMapInfo map;
190         gsize size;
191         guint8 *data;
192 
193         gst_buffer_map (outbuffer, &map, GST_MAP_READ);
194         data = map.data;
195         size = map.size;
196 
197         fail_unless (memcmp (data, data0, sizeof (data0)) == 0);
198         fail_unless (memcmp (data + 8, data1, sizeof (data1)) == 0);
199         fail_unless (memcmp (data + 20, data2, sizeof (data2)) == 0);
200         /* video or audio header */
201         data += 32 + 56;
202         fail_unless (memcmp (data, data3, sizeof (data3)) == 0);
203         fail_unless (memcmp (data + 8, data4, sizeof (data4)) == 0);
204         fail_unless (memcmp (data + 76, data5, sizeof (data5)) == 0);
205         /* avi data header */
206         data = map.data;
207         data += size - 12;
208         fail_unless (memcmp (data, data6, sizeof (data6)) == 0);
209         data += 8;
210         fail_unless (memcmp (data, data7, sizeof (data7)) == 0);
211         gst_buffer_unmap (outbuffer, &map);
212         break;
213       }
214       case 1:                  /* chunk header */
215         fail_unless (gst_buffer_get_size (outbuffer) == 8);
216         fail_unless (gst_buffer_memcmp (outbuffer, 0, chunk_id, 4) == 0);
217         break;
218       case 2:
219         fail_unless (gst_buffer_get_size (outbuffer) == 1);
220         break;
221       case 3:                  /* buffer we put in, must be padded to even size */
222         fail_unless (gst_buffer_get_size (outbuffer) == 1);
223         break;
224       default:
225         break;
226     }
227 
228     ASSERT_BUFFER_REFCOUNT (outbuffer, "outbuffer", 1);
229     gst_buffer_unref (outbuffer);
230     outbuffer = NULL;
231   }
232 
233   cleanup_avimux (avimux, sinkname);
234   g_list_free (buffers);
235   buffers = NULL;
236 }
237 
238 
GST_START_TEST(test_video_pad)239 GST_START_TEST (test_video_pad)
240 {
241   check_avimux_pad (&srcvideotemplate, VIDEO_CAPS_STRING, "00db", "video_%u");
242 }
243 
244 GST_END_TEST;
245 
246 
GST_START_TEST(test_audio_pad)247 GST_START_TEST (test_audio_pad)
248 {
249   check_avimux_pad (&srcaudiotemplate, AUDIO_CAPS_STRING, "00wb", "audio_%u");
250 }
251 
252 GST_END_TEST;
253 
254 
255 static Suite *
avimux_suite(void)256 avimux_suite (void)
257 {
258   Suite *s = suite_create ("avimux");
259   TCase *tc_chain = tcase_create ("general");
260 
261   suite_add_tcase (s, tc_chain);
262   tcase_add_test (tc_chain, test_video_pad);
263   tcase_add_test (tc_chain, test_audio_pad);
264 
265   return s;
266 }
267 
268 GST_CHECK_MAIN (avimux);
269