1 /* GStreamer
2 *
3 * Copyright (C) 2014 Samsung Electronics. All rights reserved.
4 * Author: Thiago Santos <ts.santos@sisa.samsung.com>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
15 *
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20 */
21
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25 #include <gst/gst.h>
26 #include <gst/check/gstcheck.h>
27 #include <gst/check/gstharness.h>
28 #include <gst/audio/audio.h>
29 #include <gst/app/app.h>
30
31 #define TEST_AUDIO_RATE 44100
32 #define TEST_AUDIO_CHANNELS 2
33 #define TEST_AUDIO_FORMAT "S16LE"
34
35 #define GST_AUDIO_ENCODER_TESTER_TYPE gst_audio_encoder_tester_get_type()
36 static GType gst_audio_encoder_tester_get_type (void);
37
38 typedef struct _GstAudioEncoderTester GstAudioEncoderTester;
39 typedef struct _GstAudioEncoderTesterClass GstAudioEncoderTesterClass;
40
41 struct _GstAudioEncoderTester
42 {
43 GstAudioEncoder parent;
44 };
45
46 struct _GstAudioEncoderTesterClass
47 {
48 GstAudioEncoderClass parent_class;
49 };
50
51 G_DEFINE_TYPE (GstAudioEncoderTester, gst_audio_encoder_tester,
52 GST_TYPE_AUDIO_ENCODER);
53
54 static gboolean
gst_audio_encoder_tester_start(GstAudioEncoder * enc)55 gst_audio_encoder_tester_start (GstAudioEncoder * enc)
56 {
57 return TRUE;
58 }
59
60 static gboolean
gst_audio_encoder_tester_stop(GstAudioEncoder * enc)61 gst_audio_encoder_tester_stop (GstAudioEncoder * enc)
62 {
63 return TRUE;
64 }
65
66 static gboolean
gst_audio_encoder_tester_set_format(GstAudioEncoder * enc,GstAudioInfo * info)67 gst_audio_encoder_tester_set_format (GstAudioEncoder * enc, GstAudioInfo * info)
68 {
69 GstCaps *caps;
70
71 caps = gst_caps_new_simple ("audio/x-test-custom", "rate", G_TYPE_INT,
72 TEST_AUDIO_RATE, "channels", G_TYPE_INT, TEST_AUDIO_CHANNELS, NULL);
73 gst_audio_encoder_set_output_format (enc, caps);
74 gst_caps_unref (caps);
75
76 return TRUE;
77 }
78
79 static GstFlowReturn
gst_audio_encoder_tester_handle_frame(GstAudioEncoder * enc,GstBuffer * buffer)80 gst_audio_encoder_tester_handle_frame (GstAudioEncoder * enc,
81 GstBuffer * buffer)
82 {
83 guint8 *data;
84 GstMapInfo map;
85 guint64 input_num;
86 GstBuffer *output_buffer;
87
88 if (buffer == NULL)
89 return GST_FLOW_OK;
90
91 gst_buffer_map (buffer, &map, GST_MAP_READ);
92 input_num = *((guint64 *) map.data);
93 gst_buffer_unmap (buffer, &map);
94
95 data = g_malloc (sizeof (guint64));
96 *(guint64 *) data = input_num;
97
98 output_buffer = gst_buffer_new_wrapped (data, sizeof (guint64));
99 GST_BUFFER_PTS (output_buffer) = GST_BUFFER_PTS (buffer);
100 GST_BUFFER_DURATION (output_buffer) = GST_BUFFER_DURATION (buffer);
101
102 return gst_audio_encoder_finish_frame (enc, output_buffer, TEST_AUDIO_RATE);
103 }
104
105 static void
gst_audio_encoder_tester_class_init(GstAudioEncoderTesterClass * klass)106 gst_audio_encoder_tester_class_init (GstAudioEncoderTesterClass * klass)
107 {
108 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
109 GstAudioEncoderClass *audioencoder_class = GST_AUDIO_ENCODER_CLASS (klass);
110
111 static GstStaticPadTemplate sink_templ = GST_STATIC_PAD_TEMPLATE ("sink",
112 GST_PAD_SINK, GST_PAD_ALWAYS,
113 GST_STATIC_CAPS ("audio/x-raw"));
114
115 static GstStaticPadTemplate src_templ = GST_STATIC_PAD_TEMPLATE ("src",
116 GST_PAD_SRC, GST_PAD_ALWAYS,
117 GST_STATIC_CAPS ("audio/x-test-custom"));
118
119 gst_element_class_add_static_pad_template (element_class, &sink_templ);
120 gst_element_class_add_static_pad_template (element_class, &src_templ);
121
122 gst_element_class_set_metadata (element_class,
123 "AudioEncoderTester", "Encoder/Audio", "yep", "me");
124
125 audioencoder_class->start = gst_audio_encoder_tester_start;
126 audioencoder_class->stop = gst_audio_encoder_tester_stop;
127 audioencoder_class->handle_frame = gst_audio_encoder_tester_handle_frame;
128 audioencoder_class->set_format = gst_audio_encoder_tester_set_format;
129 }
130
131 static void
gst_audio_encoder_tester_init(GstAudioEncoderTester * tester)132 gst_audio_encoder_tester_init (GstAudioEncoderTester * tester)
133 {
134 }
135
136 static GstHarness *
setup_audioencodertester(void)137 setup_audioencodertester (void)
138 {
139 GstHarness *h;
140 GstElement *enc;
141
142 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
143 GST_PAD_SINK,
144 GST_PAD_ALWAYS,
145 GST_STATIC_CAPS ("audio/x-test-custom")
146 );
147 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
148 GST_PAD_SRC,
149 GST_PAD_ALWAYS,
150 GST_STATIC_CAPS ("audio/x-raw")
151 );
152
153 enc = g_object_new (GST_AUDIO_ENCODER_TESTER_TYPE, NULL);
154 h = gst_harness_new_full (enc, &srctemplate, "sink", &sinktemplate, "src");
155
156 gst_harness_set_src_caps (h,
157 gst_caps_new_simple ("audio/x-raw",
158 "rate", G_TYPE_INT, TEST_AUDIO_RATE,
159 "channels", G_TYPE_INT, TEST_AUDIO_CHANNELS,
160 "format", G_TYPE_STRING, TEST_AUDIO_FORMAT,
161 "layout", G_TYPE_STRING, "interleaved", NULL));
162
163 gst_object_unref (enc);
164 return h;
165 }
166
167 static GstBuffer *
create_test_buffer(guint64 num)168 create_test_buffer (guint64 num)
169 {
170 GstBuffer *buffer;
171 guint64 *data;
172 gsize size;
173 guint64 samples;
174
175 samples = TEST_AUDIO_RATE;
176 size = 2 * 2 * samples;
177
178 data = g_malloc0 (size);
179 *data = num;
180
181 buffer = gst_buffer_new_wrapped (data, size);
182
183 GST_BUFFER_PTS (buffer) = num * GST_SECOND;
184 GST_BUFFER_DURATION (buffer) = GST_SECOND;
185
186 return buffer;
187 }
188
189 #define NUM_BUFFERS 100
GST_START_TEST(audioencoder_playback)190 GST_START_TEST (audioencoder_playback)
191 {
192 GstBuffer *buffer;
193 guint64 i;
194 guint buffers_available;
195
196 GstHarness *h = setup_audioencodertester ();
197
198 /* push buffers, the data is actually a number so we can track them */
199 for (i = 0; i < NUM_BUFFERS; i++) {
200 fail_unless (gst_harness_push (h, create_test_buffer (i)) == GST_FLOW_OK);
201 }
202
203 fail_unless (gst_harness_push_event (h, gst_event_new_eos ()));
204
205 /* check that all buffers were received by our source pad */
206 buffers_available = gst_harness_buffers_in_queue (h);
207 fail_unless_equals_int (NUM_BUFFERS, buffers_available);
208
209 for (i = 0; i < buffers_available; i++) {
210 GstMapInfo map;
211 guint64 num;
212
213 buffer = gst_harness_pull (h);
214
215 gst_buffer_map (buffer, &map, GST_MAP_READ);
216
217 num = *(guint64 *) map.data;
218 fail_unless (i == num);
219 fail_unless (GST_BUFFER_PTS (buffer) == i * GST_SECOND);
220 fail_unless (GST_BUFFER_DURATION (buffer) == GST_SECOND);
221
222 gst_buffer_unmap (buffer, &map);
223 gst_buffer_unref (buffer);
224 }
225
226 gst_harness_teardown (h);
227 }
228
229 GST_END_TEST;
230
231
GST_START_TEST(audioencoder_flush_events)232 GST_START_TEST (audioencoder_flush_events)
233 {
234 guint i;
235
236 GstHarness *h = setup_audioencodertester ();
237
238 /* push buffers, the data is actually a number so we can track them */
239 for (i = 0; i < NUM_BUFFERS; i++) {
240 if (i % 10 == 0) {
241 GstTagList *tags;
242
243 tags = gst_tag_list_new (GST_TAG_TRACK_NUMBER, i, NULL);
244 fail_unless (gst_harness_push_event (h, gst_event_new_tag (tags)));
245 } else {
246 fail_unless (gst_harness_push (h, create_test_buffer (i)) == GST_FLOW_OK);
247 }
248 }
249
250 fail_unless (gst_harness_push_event (h, gst_event_new_eos ()));
251
252 /* make sure the usual events have been received */
253 {
254 GstEvent *sstart = gst_harness_pull_event (h);
255 fail_unless (GST_EVENT_TYPE (sstart) == GST_EVENT_STREAM_START);
256 gst_event_unref (sstart);
257 }
258 {
259 GstEvent *caps_event = gst_harness_pull_event (h);
260 fail_unless (GST_EVENT_TYPE (caps_event) == GST_EVENT_CAPS);
261 gst_event_unref (caps_event);
262 }
263 {
264 GstEvent *segment_event = gst_harness_pull_event (h);
265 fail_unless (GST_EVENT_TYPE (segment_event) == GST_EVENT_SEGMENT);
266 gst_event_unref (segment_event);
267 }
268
269 /* check that EOS was received */
270 fail_unless (GST_PAD_IS_EOS (h->srcpad));
271 fail_unless (gst_harness_push_event (h, gst_event_new_flush_start ()));
272 fail_unless (GST_PAD_IS_EOS (h->srcpad));
273
274 /* Check that we have tags */
275 {
276 GstEvent *tags = gst_pad_get_sticky_event (h->srcpad, GST_EVENT_TAG, 0);
277
278 fail_unless (tags != NULL);
279 gst_event_unref (tags);
280 }
281
282 /* Check that we still have a segment set */
283 {
284 GstEvent *segment =
285 gst_pad_get_sticky_event (h->srcpad, GST_EVENT_SEGMENT, 0);
286
287 fail_unless (segment != NULL);
288 gst_event_unref (segment);
289 }
290
291 fail_unless (gst_harness_push_event (h, gst_event_new_flush_stop (TRUE)));
292 fail_if (GST_PAD_IS_EOS (h->srcpad));
293
294 /* Check that the segment was flushed on FLUSH_STOP */
295 {
296 GstEvent *segment =
297 gst_pad_get_sticky_event (h->srcpad, GST_EVENT_SEGMENT, 0);
298
299 fail_unless (segment == NULL);
300 }
301
302 /* Check the tags were not lost on FLUSH_STOP */
303 {
304 GstEvent *tags = gst_pad_get_sticky_event (h->srcpad, GST_EVENT_TAG, 0);
305
306 fail_unless (tags != NULL);
307 gst_event_unref (tags);
308
309 }
310
311 gst_harness_teardown (h);
312 }
313
314 GST_END_TEST;
315
316 /* make sure tags sent right before eos are pushed */
GST_START_TEST(audioencoder_tags_before_eos)317 GST_START_TEST (audioencoder_tags_before_eos)
318 {
319 GstTagList *tags;
320 GstEvent *event;
321
322 GstHarness *h = setup_audioencodertester ();
323
324 /* push buffer */
325 fail_unless (gst_harness_push (h, create_test_buffer (0)) == GST_FLOW_OK);
326
327 /* clean received events list */
328 while ((event = gst_harness_try_pull_event (h)))
329 gst_event_unref (event);
330
331 /* push a tag event */
332 tags = gst_tag_list_new (GST_TAG_COMMENT, "test-comment", NULL);
333 fail_unless (gst_harness_push_event (h, gst_event_new_tag (tags)));
334
335 fail_unless (gst_harness_push_event (h, gst_event_new_eos ()));
336
337 /* check that the tag was received */
338 {
339 GstEvent *tag_event = gst_harness_pull_event (h);
340 gchar *str;
341
342 fail_unless (GST_EVENT_TYPE (tag_event) == GST_EVENT_TAG);
343 gst_event_parse_tag (tag_event, &tags);
344 fail_unless (gst_tag_list_get_string (tags, GST_TAG_COMMENT, &str));
345 fail_unless (strcmp (str, "test-comment") == 0);
346 g_free (str);
347 gst_event_unref (tag_event);
348 }
349
350 gst_harness_teardown (h);
351 }
352
353 GST_END_TEST;
354
355 /* make sure events sent right before eos are pushed */
GST_START_TEST(audioencoder_events_before_eos)356 GST_START_TEST (audioencoder_events_before_eos)
357 {
358 GstMessage *msg;
359 GstEvent *event;
360
361 GstHarness *h = setup_audioencodertester ();
362
363 /* push buffer */
364 fail_unless (gst_harness_push (h, create_test_buffer (0)) == GST_FLOW_OK);
365
366 /* clean received events list */
367 while ((event = gst_harness_try_pull_event (h)))
368 gst_event_unref (event);
369
370 /* push a serialized event */
371 msg = gst_message_new_element (GST_OBJECT (h->element),
372 gst_structure_new_empty ("test"));
373 fail_unless (gst_harness_push_event (h,
374 gst_event_new_sink_message ("sink-test", msg)));
375 gst_message_unref (msg);
376
377 fail_unless (gst_harness_push_event (h, gst_event_new_eos ()));
378
379 /* check that the tag was received */
380 {
381 GstEvent *msg_event = gst_harness_pull_event (h);
382 const GstStructure *structure;
383
384 fail_unless (GST_EVENT_TYPE (msg_event) == GST_EVENT_SINK_MESSAGE);
385 fail_unless (gst_event_has_name (msg_event, "sink-test"));
386 gst_event_parse_sink_message (msg_event, &msg);
387 structure = gst_message_get_structure (msg);
388 fail_unless (gst_structure_has_name (structure, "test"));
389 gst_message_unref (msg);
390 gst_event_unref (msg_event);
391 }
392
393 gst_harness_teardown (h);
394 }
395
396 GST_END_TEST;
397
398 static Suite *
gst_audioencoder_suite(void)399 gst_audioencoder_suite (void)
400 {
401 Suite *s = suite_create ("GstAudioEncoder");
402 TCase *tc = tcase_create ("general");
403
404 suite_add_tcase (s, tc);
405 tcase_add_test (tc, audioencoder_playback);
406
407 tcase_add_test (tc, audioencoder_tags_before_eos);
408 tcase_add_test (tc, audioencoder_events_before_eos);
409 tcase_add_test (tc, audioencoder_flush_events);
410
411 return s;
412 }
413
414 GST_CHECK_MAIN (gst_audioencoder);
415