1 /* GStreamer
2 *
3 * Copyright (C) 2020 Seungha Yang <seungha@centricular.com>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 */
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include <gst/check/gstcheck.h>
26 #include <gst/check/gstharness.h>
27 #include <gst/audio/audio.h>
28
GST_START_TEST(test_audioenc_drain)29 GST_START_TEST (test_audioenc_drain)
30 {
31 GstHarness *h;
32 GstAudioInfo info;
33 GstBuffer *in_buf;
34 gint i = 0;
35 gint num_output = 0;
36 GstFlowReturn ret;
37 GstSegment segment;
38 GstCaps *caps;
39 gint samples_per_buffer = 1024;
40 gint rate = 44100;
41 gint size;
42 GstClockTime duration;
43
44 h = gst_harness_new ("avenc_aac");
45 fail_unless (h != NULL);
46
47 gst_audio_info_set_format (&info, GST_AUDIO_FORMAT_F32, rate, 1, NULL);
48
49 caps = gst_audio_info_to_caps (&info);
50 gst_harness_set_src_caps (h, gst_caps_copy (caps));
51
52 duration = gst_util_uint64_scale_int (samples_per_buffer, GST_SECOND, rate);
53 size = samples_per_buffer * GST_AUDIO_INFO_BPF (&info);
54
55 for (i = 0; i < 2; i++) {
56 in_buf = gst_buffer_new_and_alloc (size);
57
58 gst_buffer_memset (in_buf, 0, 0, size);
59
60 /* small rounding error would be expected, but should be fine */
61 GST_BUFFER_PTS (in_buf) = i * duration;
62 GST_BUFFER_DURATION (in_buf) = duration;
63
64 ret = gst_harness_push (h, in_buf);
65
66 fail_unless (ret == GST_FLOW_OK, "GstFlowReturn was %s",
67 gst_flow_get_name (ret));
68 }
69
70 gst_segment_init (&segment, GST_FORMAT_TIME);
71 fail_unless (gst_segment_set_running_time (&segment, GST_FORMAT_TIME,
72 2 * duration));
73
74 /* Push new eos event to drain encoder */
75 fail_unless (gst_harness_push_event (h, gst_event_new_eos ()));
76
77 /* And start new stream */
78 fail_unless (gst_harness_push_event (h,
79 gst_event_new_stream_start ("new-stream-id")));
80 gst_harness_set_src_caps (h, caps);
81 fail_unless (gst_harness_push_event (h, gst_event_new_segment (&segment)));
82
83 in_buf = gst_buffer_new_and_alloc (size);
84
85 GST_BUFFER_PTS (in_buf) = 2 * duration;
86 GST_BUFFER_DURATION (in_buf) = duration;
87
88 ret = gst_harness_push (h, in_buf);
89 fail_unless (ret == GST_FLOW_OK, "GstFlowReturn was %s",
90 gst_flow_get_name (ret));
91
92 /* Finish encoding and drain again */
93 fail_unless (gst_harness_push_event (h, gst_event_new_eos ()));
94 do {
95 GstBuffer *out_buf = NULL;
96
97 out_buf = gst_harness_try_pull (h);
98 if (out_buf) {
99 num_output++;
100 gst_buffer_unref (out_buf);
101 continue;
102 }
103
104 break;
105 } while (1);
106
107 fail_unless (num_output >= 3);
108
109 gst_harness_teardown (h);
110 }
111
112 GST_END_TEST;
113
114 static Suite *
avaudenc_suite(void)115 avaudenc_suite (void)
116 {
117 Suite *s = suite_create ("avaudenc");
118 TCase *tc_chain = tcase_create ("general");
119
120 suite_add_tcase (s, tc_chain);
121 tcase_add_test (tc_chain, test_audioenc_drain);
122
123 return s;
124 }
125
126 GST_CHECK_MAIN (avaudenc)
127