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 // Unit tests for DelayPeakDetector class. 12 13 #include "webrtc/modules/audio_coding/neteq/delay_peak_detector.h" 14 15 #include "testing/gtest/include/gtest/gtest.h" 16 17 namespace webrtc { 18 19 TEST(DelayPeakDetector, CreateAndDestroy) { 20 DelayPeakDetector* detector = new DelayPeakDetector(); 21 EXPECT_FALSE(detector->peak_found()); 22 delete detector; 23 } 24 25 TEST(DelayPeakDetector, EmptyHistory) { 26 DelayPeakDetector detector; 27 EXPECT_EQ(-1, detector.MaxPeakHeight()); 28 EXPECT_EQ(-1, detector.MaxPeakPeriod()); 29 } 30 31 // Inject a series of packet arrivals into the detector. Three of the packets 32 // have suffered delays. After the third delay peak, peak-mode is expected to 33 // start. This should then continue until it is disengaged due to lack of peaks. 34 TEST(DelayPeakDetector, TriggerPeakMode) { 35 DelayPeakDetector detector; 36 const int kPacketSizeMs = 30; 37 detector.SetPacketAudioLength(kPacketSizeMs); 38 39 // Load up normal arrival times; 0 ms, 30 ms, 60 ms, 90 ms, ... 40 const int kNumPackets = 1000; 41 int arrival_times_ms[kNumPackets]; 42 for (int i = 0; i < kNumPackets; ++i) { 43 arrival_times_ms[i] = i * kPacketSizeMs; 44 } 45 46 // Delay three packets. 47 const int kPeakDelayMs = 100; 48 // First delay peak. 49 arrival_times_ms[100] += kPeakDelayMs; 50 // Second delay peak. 51 arrival_times_ms[200] += kPeakDelayMs; 52 // Third delay peak. Trigger peak-mode after this packet. 53 arrival_times_ms[400] += kPeakDelayMs; 54 // The second peak period is the longest, 200 packets. 55 const int kWorstPeakPeriod = 200 * kPacketSizeMs; 56 int peak_mode_start_ms = arrival_times_ms[400]; 57 // Expect to disengage after no peaks are observed for two period times. 58 int peak_mode_end_ms = peak_mode_start_ms + 2 * kWorstPeakPeriod; 59 60 // Load into detector. 61 int time = 0; 62 int next = 1; // Start with the second packet to get a proper IAT. 63 while (next < kNumPackets) { 64 while (next < kNumPackets && arrival_times_ms[next] <= time) { 65 int iat_packets = (arrival_times_ms[next] - arrival_times_ms[next - 1]) / 66 kPacketSizeMs; 67 const int kTargetBufferLevel = 1; // Define peaks to be iat > 2. 68 if (time < peak_mode_start_ms || time > peak_mode_end_ms) { 69 EXPECT_FALSE(detector.Update(iat_packets, kTargetBufferLevel)); 70 } else { 71 EXPECT_TRUE(detector.Update(iat_packets, kTargetBufferLevel)); 72 EXPECT_EQ(kWorstPeakPeriod, detector.MaxPeakPeriod()); 73 EXPECT_EQ(kPeakDelayMs / kPacketSizeMs + 1, detector.MaxPeakHeight()); 74 } 75 ++next; 76 } 77 detector.IncrementCounter(10); 78 time += 10; // Increase time 10 ms. 79 } 80 } 81 82 // Same test as TriggerPeakMode, but with base target buffer level increased to 83 // 2, in order to raise the bar for delay peaks to inter-arrival times > 4. 84 // The delay pattern has peaks with delay = 3, thus should not trigger. 85 TEST(DelayPeakDetector, DoNotTriggerPeakMode) { 86 DelayPeakDetector detector; 87 const int kPacketSizeMs = 30; 88 detector.SetPacketAudioLength(kPacketSizeMs); 89 90 // Load up normal arrival times; 0 ms, 30 ms, 60 ms, 90 ms, ... 91 const int kNumPackets = 1000; 92 int arrival_times_ms[kNumPackets]; 93 for (int i = 0; i < kNumPackets; ++i) { 94 arrival_times_ms[i] = i * kPacketSizeMs; 95 } 96 97 // Delay three packets. 98 const int kPeakDelayMs = 100; 99 // First delay peak. 100 arrival_times_ms[100] += kPeakDelayMs; 101 // Second delay peak. 102 arrival_times_ms[200] += kPeakDelayMs; 103 // Third delay peak. 104 arrival_times_ms[400] += kPeakDelayMs; 105 106 // Load into detector. 107 int time = 0; 108 int next = 1; // Start with the second packet to get a proper IAT. 109 while (next < kNumPackets) { 110 while (next < kNumPackets && arrival_times_ms[next] <= time) { 111 int iat_packets = (arrival_times_ms[next] - arrival_times_ms[next - 1]) / 112 kPacketSizeMs; 113 const int kTargetBufferLevel = 2; // Define peaks to be iat > 4. 114 EXPECT_FALSE(detector.Update(iat_packets, kTargetBufferLevel)); 115 ++next; 116 } 117 detector.IncrementCounter(10); 118 time += 10; // Increase time 10 ms. 119 } 120 } 121 } // namespace webrtc 122