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., 51 Franklin St, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
21 */
22
23 #include <gst/check/gstcheck.h>
24
25 /* For ease of programming we use globals to keep refs for our floating
26 * src and sink pads we create; otherwise we always have to do get_pad,
27 * get_peer, and then remove references in every test function */
28 static GstPad *mysrcpad, *mysinkpad;
29
30 #define AUDIO_CAPS_STRING "audio/x-wma, " \
31 "channels = (int) 2, " \
32 "rate = (int) 8000, " \
33 "wmaversion = (int) 2, " \
34 "block-align = (int) 14, " \
35 "bitrate = (int) 64000"
36
37 #define VIDEO_CAPS_STRING "video/x-wmv, " \
38 "width = (int) 384, " \
39 "height = (int) 288, " \
40 "framerate = (fraction) 25/1, " \
41 "wmvversion = (int) 2"
42
43 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
44 GST_PAD_SINK,
45 GST_PAD_ALWAYS,
46 GST_STATIC_CAPS ("video/x-ms-asf"));
47 static GstStaticPadTemplate srcvideotemplate = GST_STATIC_PAD_TEMPLATE ("src",
48 GST_PAD_SRC,
49 GST_PAD_ALWAYS,
50 GST_STATIC_CAPS (VIDEO_CAPS_STRING));
51 static GstStaticPadTemplate srcaudiotemplate = GST_STATIC_PAD_TEMPLATE ("src",
52 GST_PAD_SRC,
53 GST_PAD_ALWAYS,
54 GST_STATIC_CAPS (AUDIO_CAPS_STRING));
55
56 static GstPad *
setup_src_pad(GstElement * element,GstStaticPadTemplate * template,const gchar * sinkname)57 setup_src_pad (GstElement * element,
58 GstStaticPadTemplate * template, 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) asfmux, 3) collect pads */
73 ASSERT_OBJECT_REFCOUNT (sinkpad, "sinkpad", 3);
74 fail_unless (gst_pad_link (srcpad, sinkpad) == GST_PAD_LINK_OK,
75 "Could not link source and %s sink pads", GST_ELEMENT_NAME (element));
76 gst_object_unref (sinkpad); /* because we got it higher up */
77
78 /* references are owned by: 1) asfmux, 2) collect pads */
79 ASSERT_OBJECT_REFCOUNT (sinkpad, "sinkpad", 2);
80
81 return srcpad;
82 }
83
84 static void
teardown_src_pad(GstElement * element,const gchar * sinkname)85 teardown_src_pad (GstElement * element, const gchar * sinkname)
86 {
87 GstPad *srcpad, *sinkpad;
88 gchar *padname;
89
90 /* clean up floating src pad */
91 padname = g_strdup_printf (sinkname, 1);
92 if (!(sinkpad = gst_element_get_static_pad (element, padname)))
93 sinkpad = gst_element_get_request_pad (element, padname);
94 g_free (padname);
95
96 fail_if (sinkpad == NULL, "sinkpad is null");
97
98 /* pad refs held by 1) asfmux 2) collectpads and 3) us (through _get) */
99 ASSERT_OBJECT_REFCOUNT (sinkpad, "sinkpad", 3);
100 fail_unless (gst_pad_is_linked (sinkpad));
101 srcpad = gst_pad_get_peer (sinkpad);
102
103 fail_if (srcpad == NULL, "Couldn't get srcpad");
104 gst_pad_unlink (srcpad, sinkpad);
105
106 /* after unlinking, pad refs still held by
107 * 1) asfmux and 2) collectpads and 3) us (through _get) */
108 ASSERT_OBJECT_REFCOUNT (sinkpad, "sinkpad", 3);
109 gst_object_unref (sinkpad);
110 /* one more ref is held by element itself */
111
112 /* pad refs held by both creator and this function (through _get_peer) */
113 ASSERT_OBJECT_REFCOUNT (srcpad, "srcpad", 2);
114 gst_object_unref (srcpad);
115 gst_object_unref (srcpad);
116 }
117
118 static GstElement *
setup_asfmux(GstStaticPadTemplate * srctemplate,const gchar * sinkname)119 setup_asfmux (GstStaticPadTemplate * srctemplate, const gchar * sinkname)
120 {
121 GstElement *asfmux;
122
123 GST_DEBUG ("setup_asfmux");
124 asfmux = gst_check_setup_element ("asfmux");
125
126 mysrcpad = setup_src_pad (asfmux, srctemplate, sinkname);
127 mysinkpad = gst_check_setup_sink_pad (asfmux, &sinktemplate);
128 gst_pad_set_active (mysrcpad, TRUE);
129 gst_pad_set_active (mysinkpad, TRUE);
130 return asfmux;
131 }
132
133 static void
cleanup_asfmux(GstElement * asfmux,const gchar * sinkname)134 cleanup_asfmux (GstElement * asfmux, const gchar * sinkname)
135 {
136 GST_DEBUG ("cleanup_asfmux");
137 gst_element_set_state (asfmux, GST_STATE_NULL);
138 gst_pad_set_active (mysrcpad, FALSE);
139 gst_pad_set_active (mysinkpad, FALSE);
140 teardown_src_pad (asfmux, sinkname);
141 gst_check_teardown_sink_pad (asfmux);
142 gst_check_teardown_element (asfmux);
143 }
144
145 static void
check_asfmux_pad(GstStaticPadTemplate * srctemplate,const gchar * src_caps_string,const gchar * sinkname)146 check_asfmux_pad (GstStaticPadTemplate * srctemplate,
147 const gchar * src_caps_string, const gchar * sinkname)
148 {
149 GstElement *asfmux;
150 GstBuffer *inbuffer;
151 GstCaps *caps;
152 GstFlowReturn ret;
153 GList *l;
154
155 asfmux = setup_asfmux (srctemplate, sinkname);
156 fail_unless (gst_element_set_state (asfmux,
157 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
158 "could not set to playing");
159
160 inbuffer = gst_buffer_new_and_alloc (1);
161 caps = gst_caps_from_string (src_caps_string);
162 gst_check_setup_events (mysrcpad, asfmux, caps, GST_FORMAT_TIME);
163 gst_caps_unref (caps);
164 GST_BUFFER_TIMESTAMP (inbuffer) = 0;
165 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
166 ret = gst_pad_push (mysrcpad, inbuffer);
167 fail_unless (ret == GST_FLOW_OK, "Pad push returned: %d", ret);
168
169 cleanup_asfmux (asfmux, sinkname);
170 for (l = buffers; l; l = l->next)
171 gst_buffer_unref (l->data);
172 g_list_free (buffers);
173 buffers = NULL;
174 }
175
GST_START_TEST(test_video_pad)176 GST_START_TEST (test_video_pad)
177 {
178 check_asfmux_pad (&srcvideotemplate, VIDEO_CAPS_STRING, "video_%u");
179 }
180
181 GST_END_TEST;
182
GST_START_TEST(test_audio_pad)183 GST_START_TEST (test_audio_pad)
184 {
185 check_asfmux_pad (&srcaudiotemplate, AUDIO_CAPS_STRING, "audio_%u");
186 }
187
188 GST_END_TEST;
189
190 static Suite *
asfmux_suite(void)191 asfmux_suite (void)
192 {
193 Suite *s = suite_create ("asfmux");
194 TCase *tc_chain = tcase_create ("general");
195 tcase_add_test (tc_chain, test_video_pad);
196 tcase_add_test (tc_chain, test_audio_pad);
197
198 suite_add_tcase (s, tc_chain);
199
200 return s;
201 }
202
203 GST_CHECK_MAIN (asfmux);
204