Merging gst-plugins-ugly
[platform/upstream/gstreamer.git] / subprojects / gst-plugins-ugly / 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., 51 Franklin St, Fifth Floor,
21  * Boston, MA 02110-1301, USA.
22  */
23
24 #include <gst/check/gstcheck.h>
25 #include <gst/audio/audio.h>
26
27 #define SRC_CAPS "audio/x-raw, format = (string)" GST_AUDIO_NE (S16) ", " \
28     "layout = (string) interleaved, channels = (int) 1, rate = (int) 8000"
29 #define SINK_CAPS "audio/AMR"
30
31 static GstPad *srcpad, *sinkpad;
32
33 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
34     GST_PAD_SINK,
35     GST_PAD_ALWAYS,
36     GST_STATIC_CAPS (SINK_CAPS)
37     );
38
39 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
40     GST_PAD_SRC,
41     GST_PAD_ALWAYS,
42     GST_STATIC_CAPS (SRC_CAPS)
43     );
44
45 static void
46 buffer_unref (void *buffer, void *user_data)
47 {
48   gst_buffer_unref (GST_BUFFER (buffer));
49 }
50
51 static GstElement *
52 setup_amrnbenc (void)
53 {
54   GstElement *amrnbenc;
55   GstCaps *caps;
56   GstBus *bus;
57
58   GST_DEBUG ("setup_amrnbenc");
59
60   amrnbenc = gst_check_setup_element ("amrnbenc");
61   srcpad = gst_check_setup_src_pad (amrnbenc, &srctemplate);
62   sinkpad = gst_check_setup_sink_pad (amrnbenc, &sinktemplate);
63   gst_pad_set_active (srcpad, TRUE);
64   gst_pad_set_active (sinkpad, TRUE);
65
66   bus = gst_bus_new ();
67   gst_element_set_bus (amrnbenc, bus);
68
69   fail_unless (gst_element_set_state (amrnbenc,
70           GST_STATE_PLAYING) != GST_STATE_CHANGE_FAILURE,
71       "could not set to playing");
72
73   caps = gst_caps_from_string (SRC_CAPS);
74   gst_check_setup_events (srcpad, amrnbenc, caps, GST_FORMAT_TIME);
75   gst_caps_unref (caps);
76
77   buffers = NULL;
78   return amrnbenc;
79 }
80
81 static void
82 cleanup_amrnbenc (GstElement * amrnbenc)
83 {
84   GstBus *bus;
85
86   /* free encoded buffers */
87   g_list_foreach (buffers, buffer_unref, NULL);
88   g_list_free (buffers);
89   buffers = NULL;
90
91   bus = GST_ELEMENT_BUS (amrnbenc);
92   gst_bus_set_flushing (bus, TRUE);
93   gst_object_unref (bus);
94
95   GST_DEBUG ("cleanup_amrnbenc");
96   gst_pad_set_active (srcpad, FALSE);
97   gst_pad_set_active (sinkpad, FALSE);
98   gst_check_teardown_src_pad (amrnbenc);
99   gst_check_teardown_sink_pad (amrnbenc);
100   gst_check_teardown_element (amrnbenc);
101 }
102
103 /* push a random block of audio of the given size */
104 static void
105 push_data (gint size, GstFlowReturn expected_return)
106 {
107   GstBuffer *buffer;
108   GstFlowReturn res;
109
110   buffer = gst_buffer_new_and_alloc (size);
111   /* make valgrind happier */
112   gst_buffer_memset (buffer, 0, 0, size);
113
114   res = gst_pad_push (srcpad, buffer);
115   fail_unless (res == expected_return,
116       "pushing audio returned %d (%s) not %d (%s)", res,
117       gst_flow_get_name (res), expected_return,
118       gst_flow_get_name (expected_return));
119 }
120
121 GST_START_TEST (test_enc)
122 {
123   GstElement *amrnbenc;
124
125   amrnbenc = setup_amrnbenc ();
126   push_data (1000, GST_FLOW_OK);
127
128   cleanup_amrnbenc (amrnbenc);
129 }
130
131 GST_END_TEST;
132
133 static Suite *
134 amrnbenc_suite ()
135 {
136   Suite *s = suite_create ("amrnbenc");
137   TCase *tc_chain = tcase_create ("general");
138
139   suite_add_tcase (s, tc_chain);
140   tcase_add_test (tc_chain, test_enc);
141   return s;
142 }
143
144 GST_CHECK_MAIN (amrnbenc);