tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

dtmf_buffer_unittest.cc (10380B)


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