Tizen 2.1 base
[profile/ivi/gst-plugins-ugly0.10.git] / tests / check / elements / amrnbenc.c
1 /*
2  * GStreamer
3  *
4  * unit test for amrnbenc
5  *
6  * Copyright (C) 2006 Thomas Vander Stichele <thomas at apestaart dot org>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23
24 #include <gst/check/gstcheck.h>
25
26 #define SRC_CAPS "audio/x-raw-int,width=16,depth=16,channels=1,rate=8000,signed=true,endianness=BYTE_ORDER"
27 #define SINK_CAPS "audio/AMR"
28
29 GList *buffers;
30 GList *current_buf = NULL;
31
32 GstPad *srcpad, *sinkpad;
33
34 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
35     GST_PAD_SINK,
36     GST_PAD_ALWAYS,
37     GST_STATIC_CAPS (SINK_CAPS)
38     );
39
40 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
41     GST_PAD_SRC,
42     GST_PAD_ALWAYS,
43     GST_STATIC_CAPS (SRC_CAPS)
44     );
45
46 /* takes a copy of the passed buffer data */
47 GstBuffer *
48 buffer_new (const gchar * buffer_data, guint size)
49 {
50   GstBuffer *buffer;
51   GstCaps *caps;
52
53   buffer = gst_buffer_new_and_alloc (size);
54   memcpy (GST_BUFFER_DATA (buffer), buffer_data, size);
55   caps = gst_caps_from_string (SRC_CAPS);
56   gst_buffer_set_caps (buffer, caps);
57   gst_caps_unref (caps);
58
59   return buffer;
60 }
61
62 static void
63 buffer_unref (void *buffer, void *user_data)
64 {
65   gst_buffer_unref (GST_BUFFER (buffer));
66 }
67
68 GstElement *
69 setup_amrnbenc ()
70 {
71   GstElement *amrnbenc;
72   GstBus *bus;
73   guint64 granulerate_n, granulerate_d;
74
75   GST_DEBUG ("setup_amrnbenc");
76
77   amrnbenc = gst_check_setup_element ("amrnbenc");
78   srcpad = gst_check_setup_src_pad (amrnbenc, &srctemplate, NULL);
79   sinkpad = gst_check_setup_sink_pad (amrnbenc, &sinktemplate, NULL);
80   gst_pad_set_active (srcpad, TRUE);
81   gst_pad_set_active (sinkpad, TRUE);
82
83   bus = gst_bus_new ();
84   gst_element_set_bus (amrnbenc, bus);
85
86   fail_unless (gst_element_set_state (amrnbenc,
87           GST_STATE_PLAYING) != GST_STATE_CHANGE_FAILURE,
88       "could not set to playing");
89
90   buffers = NULL;
91   return amrnbenc;
92 }
93
94 static void
95 cleanup_amrnbenc (GstElement * amrnbenc)
96 {
97   GstBus *bus;
98
99   /* free encoded buffers */
100   g_list_foreach (buffers, buffer_unref, NULL);
101   g_list_free (buffers);
102   buffers = NULL;
103
104   bus = GST_ELEMENT_BUS (amrnbenc);
105   gst_bus_set_flushing (bus, TRUE);
106   gst_object_unref (bus);
107
108   GST_DEBUG ("cleanup_amrnbenc");
109   gst_pad_set_active (srcpad, FALSE);
110   gst_pad_set_active (sinkpad, FALSE);
111   gst_check_teardown_src_pad (amrnbenc);
112   gst_check_teardown_sink_pad (amrnbenc);
113   gst_check_teardown_element (amrnbenc);
114 }
115
116 /* push a random block of audio of the given size */
117 static void
118 push_data (gint size, GstFlowReturn expected_return)
119 {
120   GstBuffer *buffer;
121   GstFlowReturn res;
122   gchar *data = g_malloc0 (size);
123
124   buffer = buffer_new (data, size);
125   g_free (data);
126   res = gst_pad_push (srcpad, buffer);
127   fail_unless (res == expected_return,
128       "pushing audio returned %d not %d", res, expected_return);
129 }
130
131 GST_START_TEST (test_enc)
132 {
133   GstElement *amrnbenc;
134
135   amrnbenc = setup_amrnbenc ();
136   push_data (1000, GST_FLOW_OK);
137
138   cleanup_amrnbenc (amrnbenc);
139 }
140
141 GST_END_TEST;
142
143 static Suite *
144 amrnbenc_suite ()
145 {
146   Suite *s = suite_create ("amrnbenc");
147   TCase *tc_chain = tcase_create ("general");
148
149   suite_add_tcase (s, tc_chain);
150   tcase_add_test (tc_chain, test_enc);
151   return s;
152 }
153
154 int
155 main (int argc, char **argv)
156 {
157   int nf;
158
159   Suite *s = amrnbenc_suite ();
160   SRunner *sr = srunner_create (s);
161
162   gst_check_init (&argc, &argv);
163
164   srunner_run_all (sr, CK_NORMAL);
165   nf = srunner_ntests_failed (sr);
166   srunner_free (sr);
167
168   return nf;
169 }