1 /* GStreamer MulawEnc unit tests
2  *
3  * This library is free software; you can redistribute it and/or
4  * modify it under the terms of the GNU Library General Public
5  * License as published by the Free Software Foundation; either
6  * version 2 of the License, or (at your option) any later version.
7  *
8  * This library is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * Library General Public License for more details.
12  *
13  * You should have received a copy of the GNU Library General Public
14  * License along with this library; if not, write to the
15  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
16  * Boston, MA 02110-1301, USA.
17  */
18 #ifdef HAVE_CONFIG_H
19 #include "config.h"
20 #endif
21 
22 #include <gst/check/gstcheck.h>
23 #include <gst/audio/audio.h>
24 #include <string.h>
25 
26 static GstPad *mysrcpad, *mysinkpad;
27 static GstElement *mulawenc = NULL;
28 
29 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
30     GST_PAD_SINK,
31     GST_PAD_ALWAYS,
32     GST_STATIC_CAPS ("audio/x-mulaw," "rate = (int) 8000," "channels = (int) 1")
33     );
34 
35 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
36     GST_PAD_SRC,
37     GST_PAD_ALWAYS,
38     GST_STATIC_CAPS ("audio/x-raw,"
39         "format = (string) " GST_AUDIO_NE (S16) ", "
40         "rate = (int) 8000, "
41         "channels = (int) 1, " "layout = (string)interleaved")
42     );
43 
44 static void
mulawenc_setup(void)45 mulawenc_setup (void)
46 {
47   GstCaps *src_caps;
48 
49   src_caps = gst_caps_from_string ("audio/x-raw,"
50       "format = (string) " GST_AUDIO_NE (S16) ", "
51       "rate = (int) 8000, "
52       "channels = (int) 1, " "layout = (string)interleaved");
53 
54   GST_DEBUG ("%s", __FUNCTION__);
55 
56   mulawenc = gst_check_setup_element ("mulawenc");
57 
58   mysrcpad = gst_check_setup_src_pad (mulawenc, &srctemplate);
59   mysinkpad = gst_check_setup_sink_pad (mulawenc, &sinktemplate);
60 
61   gst_pad_set_active (mysrcpad, TRUE);
62   gst_pad_set_active (mysinkpad, TRUE);
63 
64   gst_check_setup_events (mysrcpad, mulawenc, src_caps, GST_FORMAT_TIME);
65   gst_caps_unref (src_caps);
66 }
67 
68 static void
buffer_unref(void * buffer,void * user_data)69 buffer_unref (void *buffer, void *user_data)
70 {
71   gst_buffer_unref (GST_BUFFER (buffer));
72 }
73 
74 static void
mulawenc_teardown(void)75 mulawenc_teardown (void)
76 {
77   /* free encoded buffers */
78   g_list_foreach (buffers, buffer_unref, NULL);
79   g_list_free (buffers);
80   buffers = NULL;
81 
82   gst_pad_set_active (mysrcpad, FALSE);
83   gst_pad_set_active (mysinkpad, FALSE);
84   gst_check_teardown_src_pad (mulawenc);
85   gst_check_teardown_sink_pad (mulawenc);
86   gst_check_teardown_element (mulawenc);
87   mulawenc = NULL;
88 }
89 
90 static gboolean
check_for_maximum_bitrate(GstPad * pad,GstEvent ** eventp,gpointer user_data)91 check_for_maximum_bitrate (GstPad * pad, GstEvent ** eventp, gpointer user_data)
92 {
93   gboolean *found_maximum_bitrate = (gboolean *) user_data;
94   GstEvent *event = *eventp;
95 
96   if (event->type == GST_EVENT_TAG) {
97     GstTagList *taglist = NULL;
98     guint value = 0;
99     gst_event_parse_tag (event, &taglist);
100 
101     fail_unless (taglist != NULL);
102 
103     fail_unless (gst_tag_list_get_uint (taglist, GST_TAG_MAXIMUM_BITRATE,
104             &value));
105 
106     /* bitrate needs to be exactly sample rate * channels * 8 */
107     fail_unless (value == 8000 * 1 * 8);
108 
109     *found_maximum_bitrate = TRUE;
110   }
111 
112   return TRUE;
113 }
114 
GST_START_TEST(test_one_buffer)115 GST_START_TEST (test_one_buffer)
116 {
117   GstBuffer *buffer;
118   gint buf_size = 4096;
119   guint8 *dp;
120 
121   fail_unless (gst_element_set_state (mulawenc, GST_STATE_PLAYING) ==
122       GST_STATE_CHANGE_SUCCESS, "could not change state to playing");
123 
124   buffer = gst_buffer_new ();
125   dp = g_malloc0 (buf_size);
126   gst_buffer_append_memory (buffer,
127       gst_memory_new_wrapped (0, dp, buf_size, 0, buf_size, dp, g_free));
128   ASSERT_BUFFER_REFCOUNT (buffer, "buffer", 1);
129 
130   fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
131 
132   fail_unless (g_list_length (buffers) == 1);
133   fail_unless (gst_buffer_get_size (GST_BUFFER (g_list_first (buffers)->data)));
134 }
135 
136 GST_END_TEST;
137 
GST_START_TEST(test_tags)138 GST_START_TEST (test_tags)
139 {
140   GstBuffer *buffer;
141   gint buf_size = 4096;
142   guint8 *dp;
143   gboolean found_maximum_bitrate = FALSE;
144 
145   fail_unless (gst_element_set_state (mulawenc, GST_STATE_PLAYING) ==
146       GST_STATE_CHANGE_SUCCESS, "could not change state to playing");
147 
148   buffer = gst_buffer_new ();
149   dp = g_malloc0 (buf_size);
150   gst_buffer_append_memory (buffer,
151       gst_memory_new_wrapped (0, dp, buf_size, 0, buf_size, dp, g_free));
152   ASSERT_BUFFER_REFCOUNT (buffer, "buffer", 1);
153 
154   fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
155   gst_pad_sticky_events_foreach (mysinkpad, check_for_maximum_bitrate,
156       &found_maximum_bitrate);
157   fail_unless (found_maximum_bitrate);
158 }
159 
160 GST_END_TEST;
161 
162 static Suite *
mulawenc_suite(void)163 mulawenc_suite (void)
164 {
165   Suite *s = suite_create ("mulawenc");
166   TCase *tc_chain = tcase_create ("mulawenc");
167 
168   tcase_add_checked_fixture (tc_chain, mulawenc_setup, mulawenc_teardown);
169 
170   suite_add_tcase (s, tc_chain);
171   tcase_add_test (tc_chain, test_one_buffer);
172   tcase_add_test (tc_chain, test_tags);
173   return s;
174 }
175 
176 GST_CHECK_MAIN (mulawenc)
177