Tizen 2.1 base
[profile/ivi/gst-plugins-ugly0.10.git] / tests / check / elements / xingmux.c
1 /* GStreamer
2  *
3  * Copyright (C) 2008 Sebastian Dröge <slomo@circular-chaos.org>
4  *
5  * xingmux.c: Unit test for the xingmux element
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public License
9  * as published by the Free Software Foundation; either version 2.1 of
10  * the License, or (at your option) any later version.
11  * 
12  * This library is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  * 
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20  * 02110-1301 USA
21  */
22
23 #include <gst/gst.h>
24 #include <gst/base/gstbasetransform.h>
25 #include <gst/check/gstcheck.h>
26
27 #include <math.h>
28
29 #include "xingmux_testdata.h"
30
31 /* For ease of programming we use globals to keep refs for our floating
32  * src and sink pads we create; otherwise we always have to do get_pad,
33  * get_peer, and then remove references in every test function */
34 GstPad *mysrcpad, *mysinkpad;
35
36 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
37     GST_PAD_SINK,
38     GST_PAD_ALWAYS,
39     GST_STATIC_CAPS ("audio/mpeg, " "mpegversion = (int) 1," "layer = (int) 3")
40     );
41 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
42     GST_PAD_SRC,
43     GST_PAD_ALWAYS,
44     GST_STATIC_CAPS ("audio/mpeg, " "mpegversion = (int) 1," "layer = (int) 3")
45     );
46
47 GstElement *
48 setup_xingmux ()
49 {
50   GstElement *xingmux;
51
52   GST_DEBUG ("setup_xingmux");
53   xingmux = gst_check_setup_element ("xingmux");
54   mysrcpad = gst_check_setup_src_pad (xingmux, &srctemplate, NULL);
55   mysinkpad = gst_check_setup_sink_pad (xingmux, &sinktemplate, NULL);
56   gst_pad_set_active (mysrcpad, TRUE);
57   gst_pad_set_active (mysinkpad, TRUE);
58
59   return xingmux;
60 }
61
62 void
63 cleanup_xingmux (GstElement * xingmux)
64 {
65   GST_DEBUG ("cleanup_xingmux");
66
67   g_list_foreach (buffers, (GFunc) gst_mini_object_unref, NULL);
68   g_list_free (buffers);
69   buffers = NULL;
70
71   gst_pad_set_active (mysrcpad, FALSE);
72   gst_pad_set_active (mysinkpad, FALSE);
73   gst_check_teardown_src_pad (xingmux);
74   gst_check_teardown_sink_pad (xingmux);
75   gst_check_teardown_element (xingmux);
76 }
77
78 GST_START_TEST (test_xing_remux)
79 {
80   GstElement *xingmux;
81   GstBuffer *inbuffer;
82   GList *it;
83   const guint8 *verify_data;
84
85   xingmux = setup_xingmux ();
86
87   fail_unless (gst_element_set_state (xingmux,
88           GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
89       "could not set to playing");
90
91   inbuffer = gst_buffer_new_and_alloc (sizeof (test_xing));
92   memcpy (GST_BUFFER_DATA (inbuffer), test_xing, sizeof (test_xing));
93
94   gst_buffer_set_caps (inbuffer, GST_PAD_CAPS (mysrcpad));
95   ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
96
97   /* pushing gives away my reference ... */
98   fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
99   fail_unless (gst_pad_push_event (mysrcpad, gst_event_new_eos ()));
100   /* ... and puts a new buffer on the global list */
101   fail_unless_equals_int (g_list_length (buffers), 93);
102
103   verify_data = test_xing;
104   for (it = buffers; it != NULL; it = it->next) {
105     GstBuffer *outbuffer = (GstBuffer *) it->data;
106
107     if (it == buffers) {
108       gint j;
109
110       /* Empty Xing header, should be the same as input data until the "Xing" marker
111        * and zeroes afterwards. */
112       fail_unless (memcmp (test_xing, GST_BUFFER_DATA (outbuffer), 25) == 0);
113       for (j = 26; j < GST_BUFFER_SIZE (outbuffer); j++)
114         fail_unless (GST_BUFFER_DATA (outbuffer)[j] == 0);
115       verify_data += GST_BUFFER_SIZE (outbuffer);
116     } else if (it->next != NULL) {
117       /* Should contain the raw MP3 data without changes */
118       fail_unless (memcmp (verify_data, GST_BUFFER_DATA (outbuffer),
119               GST_BUFFER_SIZE (outbuffer)) == 0);
120       verify_data += GST_BUFFER_SIZE (outbuffer);
121     } else {
122       /* Last buffer is the rewrite of the first buffer and should be exactly the same
123        * as the old Xing header we had */
124       fail_unless (memcmp (test_xing, GST_BUFFER_DATA (outbuffer),
125               GST_BUFFER_SIZE (outbuffer)) == 0);
126     }
127   }
128
129   /* cleanup */
130   cleanup_xingmux (xingmux);
131 }
132
133 GST_END_TEST;
134
135 Suite *
136 xingmux_suite (void)
137 {
138   Suite *s = suite_create ("xingmux");
139   TCase *tc_chain = tcase_create ("general");
140
141   suite_add_tcase (s, tc_chain);
142   tcase_add_test (tc_chain, test_xing_remux);
143
144   return s;
145 }
146
147 int
148 main (int argc, char **argv)
149 {
150   int nf;
151
152   Suite *s = xingmux_suite ();
153   SRunner *sr = srunner_create (s);
154
155   gst_check_init (&argc, &argv);
156
157   srunner_run_all (sr, CK_NORMAL);
158   nf = srunner_ntests_failed (sr);
159   srunner_free (sr);
160
161   return nf;
162 }