2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
11 #include "webrtc/modules/audio_coding/neteq/dtmf_buffer.h"
14 #include <winsock2.h> // ntohl()
16 #include <arpa/inet.h> // ntohl()
21 #include "gtest/gtest.h"
23 // Modify the tests so that they pass with the modifications done to DtmfBuffer
24 // for backwards bit-exactness. Once bit-exactness is no longer required, this
25 // #define should be removed (and the code that it enables).
26 #define LEGACY_BITEXACT
30 static int sample_rate_hz = 8000;
32 static uint32_t MakeDtmfPayload(int event, bool end, int volume, int duration) {
35 // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
36 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
37 // | event |E|R| volume | duration |
38 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
39 payload |= (event & 0x00FF) << 24;
40 payload |= (end ? 0x00800000 : 0x00000000);
41 payload |= (volume & 0x003F) << 16;
42 payload |= (duration & 0xFFFF);
43 payload = ntohl(payload);
47 static bool EqualEvents(const DtmfEvent& a,
49 return (a.duration == b.duration
50 && a.end_bit == b.end_bit
51 && a.event_no == b.event_no
52 && a.timestamp == b.timestamp
53 && a.volume == b.volume);
56 TEST(DtmfBuffer, CreateAndDestroy) {
57 DtmfBuffer* buffer = new DtmfBuffer(sample_rate_hz);
61 // Test the event parser.
62 TEST(DtmfBuffer, ParseEvent) {
67 uint32_t timestamp = 0x12345678;
68 uint32_t payload = MakeDtmfPayload(event_no, end_bit, volume, duration);
69 uint8_t* payload_ptr = reinterpret_cast<uint8_t*>(&payload);
71 EXPECT_EQ(DtmfBuffer::kOK,
72 DtmfBuffer::ParseEvent(timestamp, payload_ptr, sizeof(payload),
74 EXPECT_EQ(duration, event.duration);
75 EXPECT_EQ(end_bit, event.end_bit);
76 EXPECT_EQ(event_no, event.event_no);
77 EXPECT_EQ(timestamp, event.timestamp);
78 EXPECT_EQ(volume, event.volume);
80 EXPECT_EQ(DtmfBuffer::kInvalidPointer,
81 DtmfBuffer::ParseEvent(timestamp, NULL, 4, &event));
83 EXPECT_EQ(DtmfBuffer::kInvalidPointer,
84 DtmfBuffer::ParseEvent(timestamp, payload_ptr, 4, NULL));
86 EXPECT_EQ(DtmfBuffer::kPayloadTooShort,
87 DtmfBuffer::ParseEvent(timestamp, payload_ptr, 3, &event));
90 TEST(DtmfBuffer, SimpleInsertAndGet) {
95 uint32_t timestamp = 0x12345678;
96 DtmfEvent event(timestamp, event_no, volume, duration, end_bit);
97 DtmfBuffer buffer(sample_rate_hz);
98 EXPECT_EQ(DtmfBuffer::kOK, buffer.InsertEvent(event));
99 EXPECT_EQ(1u, buffer.Length());
100 EXPECT_FALSE(buffer.Empty());
102 // Too early to get event.
103 EXPECT_FALSE(buffer.GetEvent(timestamp - 10, &out_event));
104 EXPECT_EQ(1u, buffer.Length());
105 EXPECT_FALSE(buffer.Empty());
106 // Get the event at its starting timestamp.
107 EXPECT_TRUE(buffer.GetEvent(timestamp, &out_event));
108 EXPECT_TRUE(EqualEvents(event, out_event));
109 EXPECT_EQ(1u, buffer.Length());
110 EXPECT_FALSE(buffer.Empty());
111 // Get the event some time into the event.
112 EXPECT_TRUE(buffer.GetEvent(timestamp + duration / 2, &out_event));
113 EXPECT_TRUE(EqualEvents(event, out_event));
114 EXPECT_EQ(1u, buffer.Length());
115 EXPECT_FALSE(buffer.Empty());
116 // Give a "current" timestamp after the event has ended.
117 #ifdef LEGACY_BITEXACT
118 EXPECT_TRUE(buffer.GetEvent(timestamp + duration + 10, &out_event));
120 EXPECT_FALSE(buffer.GetEvent(timestamp + duration + 10, &out_event));
121 EXPECT_EQ(0u, buffer.Length());
122 EXPECT_TRUE(buffer.Empty());
125 TEST(DtmfBuffer, MergingPackets) {
127 bool end_bit = false;
130 uint32_t timestamp = 0x12345678;
131 DtmfEvent event(timestamp, event_no, volume, duration, end_bit);
132 DtmfBuffer buffer(sample_rate_hz);
133 EXPECT_EQ(DtmfBuffer::kOK, buffer.InsertEvent(event));
135 event.duration += 80;
136 EXPECT_EQ(DtmfBuffer::kOK, buffer.InsertEvent(event));
138 event.duration += 80;
139 event.end_bit = true;
140 EXPECT_EQ(DtmfBuffer::kOK, buffer.InsertEvent(event));
142 EXPECT_EQ(1u, buffer.Length());
145 EXPECT_TRUE(buffer.GetEvent(timestamp, &out_event));
146 EXPECT_TRUE(EqualEvents(event, out_event));
149 // This test case inserts one shorter event completely overlapped by one longer
150 // event. The expected outcome is that only the longer event is played.
151 TEST(DtmfBuffer, OverlappingEvents) {
156 uint32_t timestamp = 0x12345678 + 80;
157 DtmfEvent short_event(timestamp, event_no, volume, duration, end_bit);
158 DtmfBuffer buffer(sample_rate_hz);
159 EXPECT_EQ(DtmfBuffer::kOK, buffer.InsertEvent(short_event));
163 timestamp = 0x12345678;
164 DtmfEvent long_event(timestamp, event_no, volume, duration, end_bit);
165 EXPECT_EQ(DtmfBuffer::kOK, buffer.InsertEvent(long_event));
167 long_event.duration += 80;
168 EXPECT_EQ(DtmfBuffer::kOK, buffer.InsertEvent(long_event));
170 long_event.duration += 80;
171 long_event.end_bit = true;
172 EXPECT_EQ(DtmfBuffer::kOK, buffer.InsertEvent(long_event));
174 EXPECT_EQ(2u, buffer.Length());
177 // Expect to get the long event.
178 EXPECT_TRUE(buffer.GetEvent(timestamp, &out_event));
179 EXPECT_TRUE(EqualEvents(long_event, out_event));
180 // Expect no more events.
181 #ifdef LEGACY_BITEXACT
182 EXPECT_TRUE(buffer.GetEvent(timestamp + long_event.duration + 10,
184 EXPECT_TRUE(EqualEvents(long_event, out_event));
185 EXPECT_TRUE(buffer.GetEvent(timestamp + long_event.duration + 10,
187 EXPECT_TRUE(EqualEvents(short_event, out_event));
189 EXPECT_FALSE(buffer.GetEvent(timestamp + long_event.duration + 10,
192 EXPECT_TRUE(buffer.Empty());
195 TEST(DtmfBuffer, ExtrapolationTime) {
197 bool end_bit = false;
200 uint32_t timestamp = 0x12345678;
201 DtmfEvent event1(timestamp, event_no, volume, duration, end_bit);
202 DtmfBuffer buffer(sample_rate_hz);
203 EXPECT_EQ(DtmfBuffer::kOK, buffer.InsertEvent(event1));
204 EXPECT_EQ(1u, buffer.Length());
207 // Get the event at the start.
208 EXPECT_TRUE(buffer.GetEvent(timestamp, &out_event));
209 EXPECT_TRUE(EqualEvents(event1, out_event));
210 // Also get the event 100 samples after the end of the event (since we're
211 // missing the end bit).
212 uint32_t timestamp_now = timestamp + duration + 100;
213 EXPECT_TRUE(buffer.GetEvent(timestamp_now, &out_event));
214 EXPECT_TRUE(EqualEvents(event1, out_event));
215 // Insert another event starting back-to-back with the previous event.
216 timestamp += duration;
218 DtmfEvent event2(timestamp, event_no, volume, duration, end_bit);
219 EXPECT_EQ(DtmfBuffer::kOK, buffer.InsertEvent(event2));
220 EXPECT_EQ(2u, buffer.Length());
221 // Now we expect to get the new event when supplying |timestamp_now|.
222 EXPECT_TRUE(buffer.GetEvent(timestamp_now, &out_event));
223 EXPECT_TRUE(EqualEvents(event2, out_event));
224 // Expect the the first event to be erased now.
225 EXPECT_EQ(1u, buffer.Length());
226 // Move |timestamp_now| to more than 560 samples after the end of the second
227 // event. Expect that event to be erased.
228 timestamp_now = timestamp + duration + 600;
229 #ifdef LEGACY_BITEXACT
230 EXPECT_TRUE(buffer.GetEvent(timestamp_now, &out_event));
232 EXPECT_FALSE(buffer.GetEvent(timestamp_now, &out_event));
233 EXPECT_TRUE(buffer.Empty());
236 TEST(DtmfBuffer, TimestampWraparound) {
241 uint32_t timestamp1 = 0xFFFFFFFF - duration;
242 DtmfEvent event1(timestamp1, event_no, volume, duration, end_bit);
243 uint32_t timestamp2 = 0;
244 DtmfEvent event2(timestamp2, event_no, volume, duration, end_bit);
245 DtmfBuffer buffer(sample_rate_hz);
246 EXPECT_EQ(DtmfBuffer::kOK, buffer.InsertEvent(event1));
247 EXPECT_EQ(DtmfBuffer::kOK, buffer.InsertEvent(event2));
248 EXPECT_EQ(2u, buffer.Length());
250 EXPECT_TRUE(buffer.GetEvent(timestamp1, &out_event));
251 EXPECT_TRUE(EqualEvents(event1, out_event));
252 #ifdef LEGACY_BITEXACT
253 EXPECT_EQ(1u, buffer.Length());
255 EXPECT_EQ(2u, buffer.Length());
259 // Reverse the insert order. Expect same results.
260 EXPECT_EQ(DtmfBuffer::kOK, buffer.InsertEvent(event2));
261 EXPECT_EQ(DtmfBuffer::kOK, buffer.InsertEvent(event1));
262 EXPECT_EQ(2u, buffer.Length());
263 EXPECT_TRUE(buffer.GetEvent(timestamp1, &out_event));
264 EXPECT_TRUE(EqualEvents(event1, out_event));
265 #ifdef LEGACY_BITEXACT
266 EXPECT_EQ(1u, buffer.Length());
268 EXPECT_EQ(2u, buffer.Length());
272 TEST(DtmfBuffer, InvalidEvents) {
277 uint32_t timestamp = 0x12345678;
278 DtmfEvent event(timestamp, event_no, volume, duration, end_bit);
279 DtmfBuffer buffer(sample_rate_hz);
281 // Invalid event number.
283 EXPECT_EQ(DtmfBuffer::kInvalidEventParameters, buffer.InsertEvent(event));
285 EXPECT_EQ(DtmfBuffer::kInvalidEventParameters, buffer.InsertEvent(event));
286 event.event_no = 0; // Valid value;
290 EXPECT_EQ(DtmfBuffer::kInvalidEventParameters, buffer.InsertEvent(event));
292 EXPECT_EQ(DtmfBuffer::kInvalidEventParameters, buffer.InsertEvent(event));
293 event.volume = 0; // Valid value;
297 EXPECT_EQ(DtmfBuffer::kInvalidEventParameters, buffer.InsertEvent(event));
299 EXPECT_EQ(DtmfBuffer::kInvalidEventParameters, buffer.InsertEvent(event));
300 event.duration = 0xFFFF + 1;
301 EXPECT_EQ(DtmfBuffer::kInvalidEventParameters, buffer.InsertEvent(event));
302 event.duration = 1; // Valid value;
304 // Finish with a valid event, just to verify that all is ok.
305 EXPECT_EQ(DtmfBuffer::kOK, buffer.InsertEvent(event));
307 } // namespace webrtc