Tizen 2.0 Release
[framework/multimedia/gst-plugins-bad0.10.git] / tests / check / elements / asfmux.c
1 /* GStreamer
2  *
3  * unit test for asfmux
4  *
5  * Copyright (C) <2008> Thiago Santos <thiagoss@embedded.ufcg.edu.br>
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-wma, " \
33                         "channels = (int) 2, " \
34                         "rate = (int) 8000, " \
35                         "wmaversion = (int) 2, " \
36                         "block-align = (int) 14, " \
37                         "bitrate = (int) 64000"
38
39 #define VIDEO_CAPS_STRING "video/x-wmv, " \
40                            "width = (int) 384, " \
41                            "height = (int) 288, " \
42                            "framerate = (fraction) 25/1, " \
43                            "wmvversion = (int) 2"
44
45 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
46     GST_PAD_SINK,
47     GST_PAD_ALWAYS,
48     GST_STATIC_CAPS ("video/x-ms-asf"));
49 static GstStaticPadTemplate srcvideotemplate = GST_STATIC_PAD_TEMPLATE ("src",
50     GST_PAD_SRC,
51     GST_PAD_ALWAYS,
52     GST_STATIC_CAPS (VIDEO_CAPS_STRING));
53 static GstStaticPadTemplate srcaudiotemplate = GST_STATIC_PAD_TEMPLATE ("src",
54     GST_PAD_SRC,
55     GST_PAD_ALWAYS,
56     GST_STATIC_CAPS (AUDIO_CAPS_STRING));
57
58 static GstPad *
59 setup_src_pad (GstElement * element,
60     GstStaticPadTemplate * template, GstCaps * caps, const gchar * sinkname)
61 {
62   GstPad *srcpad, *sinkpad;
63
64   GST_DEBUG_OBJECT (element, "setting up sending pad");
65   /* sending pad */
66   srcpad = gst_pad_new_from_static_template (template, "src");
67   fail_if (srcpad == NULL, "Could not create a srcpad");
68   ASSERT_OBJECT_REFCOUNT (srcpad, "srcpad", 1);
69
70   if (!(sinkpad = gst_element_get_static_pad (element, sinkname)))
71     sinkpad = gst_element_get_request_pad (element, sinkname);
72   fail_if (sinkpad == NULL, "Could not get sink pad from %s",
73       GST_ELEMENT_NAME (element));
74   /* references are owned by: 1) us, 2) asfmux, 3) collect pads */
75   ASSERT_OBJECT_REFCOUNT (sinkpad, "sinkpad", 3);
76   if (caps)
77     fail_unless (gst_pad_set_caps (srcpad, caps));
78   fail_unless (gst_pad_link (srcpad, sinkpad) == GST_PAD_LINK_OK,
79       "Could not link source and %s sink pads", GST_ELEMENT_NAME (element));
80   gst_object_unref (sinkpad);   /* because we got it higher up */
81
82   /* references are owned by: 1) asfmux, 2) collect pads */
83   ASSERT_OBJECT_REFCOUNT (sinkpad, "sinkpad", 2);
84
85   return srcpad;
86 }
87
88 static void
89 teardown_src_pad (GstElement * element, const gchar * sinkname)
90 {
91   GstPad *srcpad, *sinkpad;
92   gchar *padname;
93
94   /* clean up floating src pad */
95   /* hm, asfmux uses _01 as suffixes for padnames */
96   padname = g_strdup (sinkname);
97   memcpy (strchr (padname, '%'), "01", 2);
98   if (!(sinkpad = gst_element_get_static_pad (element, padname)))
99     sinkpad = gst_element_get_request_pad (element, padname);
100   g_free (padname);
101
102   fail_if (sinkpad == NULL, "sinkpad is null");
103
104   /* pad refs held by 1) asfmux 2) collectpads and 3) us (through _get) */
105   ASSERT_OBJECT_REFCOUNT (sinkpad, "sinkpad", 3);
106   fail_unless (gst_pad_is_linked (sinkpad));
107   srcpad = gst_pad_get_peer (sinkpad);
108
109   fail_if (srcpad == NULL, "Couldn't get srcpad");
110   gst_pad_unlink (srcpad, sinkpad);
111
112   /* after unlinking, pad refs still held by
113    * 1) asfmux and 2) collectpads and 3) us (through _get) */
114   ASSERT_OBJECT_REFCOUNT (sinkpad, "sinkpad", 3);
115   gst_object_unref (sinkpad);
116   /* one more ref is held by element itself */
117
118   /* pad refs held by both creator and this function (through _get_peer) */
119   ASSERT_OBJECT_REFCOUNT (srcpad, "srcpad", 2);
120   gst_object_unref (srcpad);
121   gst_object_unref (srcpad);
122 }
123
124 static GstElement *
125 setup_asfmux (GstStaticPadTemplate * srctemplate, const gchar * sinkname)
126 {
127   GstElement *asfmux;
128
129   GST_DEBUG ("setup_asfmux");
130   asfmux = gst_check_setup_element ("asfmux");
131
132   mysrcpad = setup_src_pad (asfmux, srctemplate, NULL, sinkname);
133   mysinkpad = gst_check_setup_sink_pad (asfmux, &sinktemplate, NULL);
134   gst_pad_set_active (mysrcpad, TRUE);
135   gst_pad_set_active (mysinkpad, TRUE);
136   return asfmux;
137 }
138
139 static void
140 cleanup_asfmux (GstElement * asfmux, const gchar * sinkname)
141 {
142   GST_DEBUG ("cleanup_asfmux");
143   gst_element_set_state (asfmux, GST_STATE_NULL);
144   gst_pad_set_active (mysrcpad, FALSE);
145   gst_pad_set_active (mysinkpad, FALSE);
146   teardown_src_pad (asfmux, sinkname);
147   gst_check_teardown_sink_pad (asfmux);
148   gst_check_teardown_element (asfmux);
149 }
150
151 static void
152 check_asfmux_pad (GstStaticPadTemplate * srctemplate,
153     const gchar * src_caps_string, const gchar * sinkname)
154 {
155   GstElement *asfmux;
156   GstBuffer *inbuffer;
157   GstCaps *caps;
158   GstFlowReturn ret;
159   GList *l;
160
161   asfmux = setup_asfmux (srctemplate, sinkname);
162   fail_unless (gst_element_set_state (asfmux,
163           GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
164       "could not set to playing");
165
166   inbuffer = gst_buffer_new_and_alloc (1);
167   caps = gst_caps_from_string (src_caps_string);
168   gst_buffer_set_caps (inbuffer, caps);
169   gst_caps_unref (caps);
170   GST_BUFFER_TIMESTAMP (inbuffer) = 0;
171   ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
172   ret = gst_pad_push (mysrcpad, inbuffer);
173   fail_unless (ret == GST_FLOW_OK, "Pad push returned: %d", ret);
174
175   cleanup_asfmux (asfmux, sinkname);
176   for (l = buffers; l; l = l->next)
177     gst_buffer_unref (l->data);
178   g_list_free (buffers);
179   buffers = NULL;
180 }
181
182 GST_START_TEST (test_video_pad)
183 {
184   check_asfmux_pad (&srcvideotemplate, VIDEO_CAPS_STRING, "video_%d");
185 }
186
187 GST_END_TEST;
188
189 GST_START_TEST (test_audio_pad)
190 {
191   check_asfmux_pad (&srcaudiotemplate, AUDIO_CAPS_STRING, "audio_%d");
192 }
193
194 GST_END_TEST;
195
196 static Suite *
197 asfmux_suite (void)
198 {
199   Suite *s = suite_create ("asfmux");
200   TCase *tc_chain = tcase_create ("general");
201   tcase_add_test (tc_chain, test_video_pad);
202   tcase_add_test (tc_chain, test_audio_pad);
203
204   suite_add_tcase (s, tc_chain);
205
206   return s;
207 }
208
209 int
210 main (int argc, char **argv)
211 {
212   int nf;
213
214   Suite *s = asfmux_suite ();
215   SRunner *sr = srunner_create (s);
216
217   gst_check_init (&argc, &argv);
218
219   srunner_run_all (sr, CK_NORMAL);
220   nf = srunner_ntests_failed (sr);
221   srunner_free (sr);
222
223   return nf;
224 }