xingmux: move from gst-plugins-ugly to gst-plugins-good
[platform/upstream/gstreamer.git] / subprojects / gst-plugins-good / 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 static GstElement *
48 setup_xingmux (void)
49 {
50   GstElement *xingmux;
51   GstCaps *caps;
52
53   GST_DEBUG ("setup_xingmux");
54   xingmux = gst_check_setup_element ("xingmux");
55   mysrcpad = gst_check_setup_src_pad (xingmux, &srctemplate);
56   mysinkpad = gst_check_setup_sink_pad (xingmux, &sinktemplate);
57   gst_pad_set_active (mysrcpad, TRUE);
58   gst_pad_set_active (mysinkpad, TRUE);
59
60   caps = gst_caps_new_simple ("audio/mpeg",
61       "mpegversion", G_TYPE_INT, 1, "layer", G_TYPE_INT, 3, NULL);
62   gst_check_setup_events (mysrcpad, xingmux, caps, GST_FORMAT_TIME);
63   gst_caps_unref (caps);
64
65   return xingmux;
66 }
67
68 static void
69 cleanup_xingmux (GstElement * xingmux)
70 {
71   GST_DEBUG ("cleanup_xingmux");
72
73   g_list_foreach (buffers, (GFunc) gst_mini_object_unref, NULL);
74   g_list_free (buffers);
75   buffers = NULL;
76
77   gst_pad_set_active (mysrcpad, FALSE);
78   gst_pad_set_active (mysinkpad, FALSE);
79   gst_check_teardown_src_pad (xingmux);
80   gst_check_teardown_sink_pad (xingmux);
81   gst_check_teardown_element (xingmux);
82 }
83
84 GST_START_TEST (test_xing_remux)
85 {
86   GstElement *xingmux;
87   GstBuffer *inbuffer;
88   GList *it;
89   const guint8 *verify_data;
90
91   xingmux = setup_xingmux ();
92
93   fail_unless (gst_element_set_state (xingmux,
94           GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
95       "could not set to playing");
96
97   inbuffer = gst_buffer_new_and_alloc (sizeof (test_xing));
98   gst_buffer_fill (inbuffer, 0, test_xing, sizeof (test_xing));
99
100   ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
101
102   /* pushing gives away my reference ... */
103   fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
104   fail_unless (gst_pad_push_event (mysrcpad, gst_event_new_eos ()));
105   /* ... and puts a new buffer on the global list */
106   fail_unless_equals_int (g_list_length (buffers), 93);
107
108   verify_data = test_xing;
109   for (it = buffers; it != NULL; it = it->next) {
110     GstBuffer *outbuffer = (GstBuffer *) it->data;
111     GstMapInfo map;
112
113     gst_buffer_map (outbuffer, &map, GST_MAP_READ);
114
115     if (it == buffers) {
116       gint j;
117
118       /* Empty Xing header, should be the same as input data until the "Xing" marker
119        * and zeroes afterwards. */
120       fail_unless (memcmp (map.data, test_xing, 25) == 0);
121       for (j = 26; j < map.size; j++)
122         fail_unless (map.data[j] == 0);
123       verify_data += map.size;
124     } else if (it->next != NULL) {
125       /* Should contain the raw MP3 data without changes */
126       fail_unless (memcmp (map.data, verify_data, map.size) == 0);
127       verify_data += map.size;
128     } else {
129       /* Last buffer is the rewrite of the first buffer and should be exactly the same
130        * as the old Xing header we had */
131       fail_unless (memcmp (test_xing, map.data, map.size) == 0);
132     }
133     gst_buffer_unmap (outbuffer, &map);
134   }
135
136   /* cleanup */
137   cleanup_xingmux (xingmux);
138 }
139
140 GST_END_TEST;
141
142 static Suite *
143 xingmux_suite (void)
144 {
145   Suite *s = suite_create ("xingmux");
146   TCase *tc_chain = tcase_create ("general");
147
148   suite_add_tcase (s, tc_chain);
149   tcase_add_test (tc_chain, test_xing_remux);
150
151   return s;
152 }
153
154 GST_CHECK_MAIN (xingmux);