henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 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. |
| 9 | */ |
| 10 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame^] | 11 | #include "modules/audio_coding/neteq/dtmf_buffer.h" |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 12 | |
| 13 | #ifdef WIN32 |
| 14 | #include <winsock2.h> // ntohl() |
| 15 | #else |
| 16 | #include <arpa/inet.h> // ntohl() |
| 17 | #endif |
| 18 | |
| 19 | #include <iostream> |
| 20 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame^] | 21 | #include "test/gtest.h" |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 22 | |
| 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 |
| 27 | |
| 28 | namespace webrtc { |
| 29 | |
| 30 | static int sample_rate_hz = 8000; |
| 31 | |
| 32 | static uint32_t MakeDtmfPayload(int event, bool end, int volume, int duration) { |
| 33 | uint32_t payload = 0; |
| 34 | // 0 1 2 3 |
| 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); |
| 44 | return payload; |
| 45 | } |
| 46 | |
| 47 | static bool EqualEvents(const DtmfEvent& a, |
| 48 | const DtmfEvent& b) { |
| 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); |
| 54 | } |
| 55 | |
| 56 | TEST(DtmfBuffer, CreateAndDestroy) { |
| 57 | DtmfBuffer* buffer = new DtmfBuffer(sample_rate_hz); |
| 58 | delete buffer; |
| 59 | } |
| 60 | |
| 61 | // Test the event parser. |
| 62 | TEST(DtmfBuffer, ParseEvent) { |
| 63 | int event_no = 7; |
| 64 | bool end_bit = true; |
| 65 | int volume = 17; |
| 66 | int duration = 4711; |
| 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); |
| 70 | DtmfEvent event; |
| 71 | EXPECT_EQ(DtmfBuffer::kOK, |
| 72 | DtmfBuffer::ParseEvent(timestamp, payload_ptr, sizeof(payload), |
| 73 | &event)); |
| 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); |
| 79 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 80 | EXPECT_EQ(DtmfBuffer::kPayloadTooShort, |
| 81 | DtmfBuffer::ParseEvent(timestamp, payload_ptr, 3, &event)); |
| 82 | } |
| 83 | |
| 84 | TEST(DtmfBuffer, SimpleInsertAndGet) { |
| 85 | int event_no = 7; |
| 86 | bool end_bit = true; |
| 87 | int volume = 17; |
| 88 | int duration = 4711; |
| 89 | uint32_t timestamp = 0x12345678; |
| 90 | DtmfEvent event(timestamp, event_no, volume, duration, end_bit); |
| 91 | DtmfBuffer buffer(sample_rate_hz); |
| 92 | EXPECT_EQ(DtmfBuffer::kOK, buffer.InsertEvent(event)); |
| 93 | EXPECT_EQ(1u, buffer.Length()); |
| 94 | EXPECT_FALSE(buffer.Empty()); |
| 95 | DtmfEvent out_event; |
| 96 | // Too early to get event. |
| 97 | EXPECT_FALSE(buffer.GetEvent(timestamp - 10, &out_event)); |
| 98 | EXPECT_EQ(1u, buffer.Length()); |
| 99 | EXPECT_FALSE(buffer.Empty()); |
| 100 | // Get the event at its starting timestamp. |
| 101 | EXPECT_TRUE(buffer.GetEvent(timestamp, &out_event)); |
| 102 | EXPECT_TRUE(EqualEvents(event, out_event)); |
| 103 | EXPECT_EQ(1u, buffer.Length()); |
| 104 | EXPECT_FALSE(buffer.Empty()); |
| 105 | // Get the event some time into the event. |
| 106 | EXPECT_TRUE(buffer.GetEvent(timestamp + duration / 2, &out_event)); |
| 107 | EXPECT_TRUE(EqualEvents(event, out_event)); |
| 108 | EXPECT_EQ(1u, buffer.Length()); |
| 109 | EXPECT_FALSE(buffer.Empty()); |
| 110 | // Give a "current" timestamp after the event has ended. |
| 111 | #ifdef LEGACY_BITEXACT |
| 112 | EXPECT_TRUE(buffer.GetEvent(timestamp + duration + 10, &out_event)); |
| 113 | #endif |
| 114 | EXPECT_FALSE(buffer.GetEvent(timestamp + duration + 10, &out_event)); |
| 115 | EXPECT_EQ(0u, buffer.Length()); |
| 116 | EXPECT_TRUE(buffer.Empty()); |
| 117 | } |
| 118 | |
| 119 | TEST(DtmfBuffer, MergingPackets) { |
| 120 | int event_no = 0; |
| 121 | bool end_bit = false; |
| 122 | int volume = 17; |
| 123 | int duration = 80; |
| 124 | uint32_t timestamp = 0x12345678; |
| 125 | DtmfEvent event(timestamp, event_no, volume, duration, end_bit); |
| 126 | DtmfBuffer buffer(sample_rate_hz); |
| 127 | EXPECT_EQ(DtmfBuffer::kOK, buffer.InsertEvent(event)); |
| 128 | |
| 129 | event.duration += 80; |
| 130 | EXPECT_EQ(DtmfBuffer::kOK, buffer.InsertEvent(event)); |
| 131 | |
| 132 | event.duration += 80; |
| 133 | event.end_bit = true; |
| 134 | EXPECT_EQ(DtmfBuffer::kOK, buffer.InsertEvent(event)); |
| 135 | |
| 136 | EXPECT_EQ(1u, buffer.Length()); |
| 137 | |
| 138 | DtmfEvent out_event; |
| 139 | EXPECT_TRUE(buffer.GetEvent(timestamp, &out_event)); |
| 140 | EXPECT_TRUE(EqualEvents(event, out_event)); |
| 141 | } |
| 142 | |
| 143 | // This test case inserts one shorter event completely overlapped by one longer |
| 144 | // event. The expected outcome is that only the longer event is played. |
| 145 | TEST(DtmfBuffer, OverlappingEvents) { |
| 146 | int event_no = 0; |
| 147 | bool end_bit = true; |
| 148 | int volume = 1; |
| 149 | int duration = 80; |
| 150 | uint32_t timestamp = 0x12345678 + 80; |
| 151 | DtmfEvent short_event(timestamp, event_no, volume, duration, end_bit); |
| 152 | DtmfBuffer buffer(sample_rate_hz); |
| 153 | EXPECT_EQ(DtmfBuffer::kOK, buffer.InsertEvent(short_event)); |
| 154 | |
| 155 | event_no = 10; |
| 156 | end_bit = false; |
| 157 | timestamp = 0x12345678; |
| 158 | DtmfEvent long_event(timestamp, event_no, volume, duration, end_bit); |
| 159 | EXPECT_EQ(DtmfBuffer::kOK, buffer.InsertEvent(long_event)); |
| 160 | |
| 161 | long_event.duration += 80; |
| 162 | EXPECT_EQ(DtmfBuffer::kOK, buffer.InsertEvent(long_event)); |
| 163 | |
| 164 | long_event.duration += 80; |
| 165 | long_event.end_bit = true; |
| 166 | EXPECT_EQ(DtmfBuffer::kOK, buffer.InsertEvent(long_event)); |
| 167 | |
| 168 | EXPECT_EQ(2u, buffer.Length()); |
| 169 | |
| 170 | DtmfEvent out_event; |
| 171 | // Expect to get the long event. |
| 172 | EXPECT_TRUE(buffer.GetEvent(timestamp, &out_event)); |
| 173 | EXPECT_TRUE(EqualEvents(long_event, out_event)); |
| 174 | // Expect no more events. |
| 175 | #ifdef LEGACY_BITEXACT |
| 176 | EXPECT_TRUE(buffer.GetEvent(timestamp + long_event.duration + 10, |
| 177 | &out_event)); |
| 178 | EXPECT_TRUE(EqualEvents(long_event, out_event)); |
| 179 | EXPECT_TRUE(buffer.GetEvent(timestamp + long_event.duration + 10, |
| 180 | &out_event)); |
| 181 | EXPECT_TRUE(EqualEvents(short_event, out_event)); |
| 182 | #else |
| 183 | EXPECT_FALSE(buffer.GetEvent(timestamp + long_event.duration + 10, |
| 184 | &out_event)); |
| 185 | #endif |
| 186 | EXPECT_TRUE(buffer.Empty()); |
| 187 | } |
| 188 | |
| 189 | TEST(DtmfBuffer, ExtrapolationTime) { |
| 190 | int event_no = 0; |
| 191 | bool end_bit = false; |
| 192 | int volume = 1; |
| 193 | int duration = 80; |
| 194 | uint32_t timestamp = 0x12345678; |
| 195 | DtmfEvent event1(timestamp, event_no, volume, duration, end_bit); |
| 196 | DtmfBuffer buffer(sample_rate_hz); |
| 197 | EXPECT_EQ(DtmfBuffer::kOK, buffer.InsertEvent(event1)); |
| 198 | EXPECT_EQ(1u, buffer.Length()); |
| 199 | |
| 200 | DtmfEvent out_event; |
| 201 | // Get the event at the start. |
| 202 | EXPECT_TRUE(buffer.GetEvent(timestamp, &out_event)); |
| 203 | EXPECT_TRUE(EqualEvents(event1, out_event)); |
| 204 | // Also get the event 100 samples after the end of the event (since we're |
| 205 | // missing the end bit). |
| 206 | uint32_t timestamp_now = timestamp + duration + 100; |
| 207 | EXPECT_TRUE(buffer.GetEvent(timestamp_now, &out_event)); |
| 208 | EXPECT_TRUE(EqualEvents(event1, out_event)); |
| 209 | // Insert another event starting back-to-back with the previous event. |
| 210 | timestamp += duration; |
| 211 | event_no = 1; |
| 212 | DtmfEvent event2(timestamp, event_no, volume, duration, end_bit); |
| 213 | EXPECT_EQ(DtmfBuffer::kOK, buffer.InsertEvent(event2)); |
| 214 | EXPECT_EQ(2u, buffer.Length()); |
| 215 | // Now we expect to get the new event when supplying |timestamp_now|. |
| 216 | EXPECT_TRUE(buffer.GetEvent(timestamp_now, &out_event)); |
| 217 | EXPECT_TRUE(EqualEvents(event2, out_event)); |
| 218 | // Expect the the first event to be erased now. |
| 219 | EXPECT_EQ(1u, buffer.Length()); |
| 220 | // Move |timestamp_now| to more than 560 samples after the end of the second |
| 221 | // event. Expect that event to be erased. |
| 222 | timestamp_now = timestamp + duration + 600; |
| 223 | #ifdef LEGACY_BITEXACT |
| 224 | EXPECT_TRUE(buffer.GetEvent(timestamp_now, &out_event)); |
| 225 | #endif |
| 226 | EXPECT_FALSE(buffer.GetEvent(timestamp_now, &out_event)); |
| 227 | EXPECT_TRUE(buffer.Empty()); |
| 228 | } |
| 229 | |
| 230 | TEST(DtmfBuffer, TimestampWraparound) { |
| 231 | int event_no = 0; |
| 232 | bool end_bit = true; |
| 233 | int volume = 1; |
| 234 | int duration = 80; |
| 235 | uint32_t timestamp1 = 0xFFFFFFFF - duration; |
| 236 | DtmfEvent event1(timestamp1, event_no, volume, duration, end_bit); |
| 237 | uint32_t timestamp2 = 0; |
| 238 | DtmfEvent event2(timestamp2, event_no, volume, duration, end_bit); |
| 239 | DtmfBuffer buffer(sample_rate_hz); |
| 240 | EXPECT_EQ(DtmfBuffer::kOK, buffer.InsertEvent(event1)); |
| 241 | EXPECT_EQ(DtmfBuffer::kOK, buffer.InsertEvent(event2)); |
| 242 | EXPECT_EQ(2u, buffer.Length()); |
| 243 | DtmfEvent out_event; |
| 244 | EXPECT_TRUE(buffer.GetEvent(timestamp1, &out_event)); |
| 245 | EXPECT_TRUE(EqualEvents(event1, out_event)); |
| 246 | #ifdef LEGACY_BITEXACT |
| 247 | EXPECT_EQ(1u, buffer.Length()); |
| 248 | #else |
| 249 | EXPECT_EQ(2u, buffer.Length()); |
| 250 | #endif |
| 251 | |
| 252 | buffer.Flush(); |
| 253 | // Reverse the insert order. Expect same results. |
| 254 | EXPECT_EQ(DtmfBuffer::kOK, buffer.InsertEvent(event2)); |
| 255 | EXPECT_EQ(DtmfBuffer::kOK, buffer.InsertEvent(event1)); |
| 256 | EXPECT_EQ(2u, buffer.Length()); |
| 257 | EXPECT_TRUE(buffer.GetEvent(timestamp1, &out_event)); |
| 258 | EXPECT_TRUE(EqualEvents(event1, out_event)); |
| 259 | #ifdef LEGACY_BITEXACT |
| 260 | EXPECT_EQ(1u, buffer.Length()); |
| 261 | #else |
| 262 | EXPECT_EQ(2u, buffer.Length()); |
| 263 | #endif |
| 264 | } |
| 265 | |
| 266 | TEST(DtmfBuffer, InvalidEvents) { |
| 267 | int event_no = 0; |
| 268 | bool end_bit = true; |
| 269 | int volume = 1; |
| 270 | int duration = 80; |
| 271 | uint32_t timestamp = 0x12345678; |
| 272 | DtmfEvent event(timestamp, event_no, volume, duration, end_bit); |
| 273 | DtmfBuffer buffer(sample_rate_hz); |
| 274 | |
| 275 | // Invalid event number. |
| 276 | event.event_no = -1; |
| 277 | EXPECT_EQ(DtmfBuffer::kInvalidEventParameters, buffer.InsertEvent(event)); |
| 278 | event.event_no = 16; |
| 279 | EXPECT_EQ(DtmfBuffer::kInvalidEventParameters, buffer.InsertEvent(event)); |
| 280 | event.event_no = 0; // Valid value; |
| 281 | |
| 282 | // Invalid volume. |
| 283 | event.volume = -1; |
| 284 | EXPECT_EQ(DtmfBuffer::kInvalidEventParameters, buffer.InsertEvent(event)); |
solenberg | 99df6c0 | 2016-10-11 04:35:34 -0700 | [diff] [blame] | 285 | event.volume = 64; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 286 | EXPECT_EQ(DtmfBuffer::kInvalidEventParameters, buffer.InsertEvent(event)); |
| 287 | event.volume = 0; // Valid value; |
| 288 | |
| 289 | // Invalid duration. |
| 290 | event.duration = -1; |
| 291 | EXPECT_EQ(DtmfBuffer::kInvalidEventParameters, buffer.InsertEvent(event)); |
| 292 | event.duration = 0; |
| 293 | EXPECT_EQ(DtmfBuffer::kInvalidEventParameters, buffer.InsertEvent(event)); |
| 294 | event.duration = 0xFFFF + 1; |
| 295 | EXPECT_EQ(DtmfBuffer::kInvalidEventParameters, buffer.InsertEvent(event)); |
| 296 | event.duration = 1; // Valid value; |
| 297 | |
| 298 | // Finish with a valid event, just to verify that all is ok. |
| 299 | EXPECT_EQ(DtmfBuffer::kOK, buffer.InsertEvent(event)); |
| 300 | } |
| 301 | } // namespace webrtc |