1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
|
/*
* Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include <cstddef>
#include <cstdint>
#include <cstdlib>
#include <map>
#include <memory>
#include "api/array_view.h"
#include "api/audio/audio_frame.h"
#include "api/audio_codecs/audio_format.h"
#include "api/audio_codecs/builtin_audio_decoder_factory.h"
#include "api/environment/environment_factory.h"
#include "api/neteq/default_neteq_factory.h"
#include "api/neteq/neteq.h"
#include "api/rtp_headers.h"
#include "api/units/timestamp.h"
#include "modules/audio_coding/codecs/pcm16b/pcm16b.h"
#include "modules/audio_coding/include/audio_coding_module.h"
#include "test/gtest.h"
#include "test/testsupport/file_utils.h"
namespace webrtc {
class TargetDelayTest : public ::testing::Test {
protected:
TargetDelayTest()
: neteq_(
DefaultNetEqFactory().Create(CreateEnvironment(),
NetEq::Config(),
CreateBuiltinAudioDecoderFactory())) {}
~TargetDelayTest() {}
void SetUp() {
constexpr int pltype = 108;
std::map<int, SdpAudioFormat> receive_codecs = {
{pltype, {"L16", kSampleRateHz, 1}}};
neteq_->SetCodecs(receive_codecs);
rtp_header_.payloadType = pltype;
rtp_header_.timestamp = 0;
rtp_header_.ssrc = 0x12345678;
rtp_header_.markerBit = false;
rtp_header_.sequenceNumber = 0;
int16_t audio[kFrameSizeSamples];
const int kRange = 0x7FF; // 2047, easy for masking.
for (size_t n = 0; n < kFrameSizeSamples; ++n)
audio[n] = (rand() & kRange) - kRange / 2;
WebRtcPcm16b_Encode(audio, kFrameSizeSamples, payload_);
}
void OutOfRangeInput() {
EXPECT_FALSE(SetMinimumDelay(-1));
EXPECT_FALSE(SetMinimumDelay(10001));
}
void TargetDelayBufferMinMax() {
const int kTargetMinDelayMs = kNum10msPerFrame * 10;
ASSERT_TRUE(SetMinimumDelay(kTargetMinDelayMs));
for (int m = 0; m < 30; ++m) // Run enough iterations to fill the buffer.
Run(true);
int clean_optimal_delay = GetCurrentOptimalDelayMs();
EXPECT_EQ(kTargetMinDelayMs, clean_optimal_delay);
const int kTargetMaxDelayMs = 2 * (kNum10msPerFrame * 10);
ASSERT_TRUE(SetMaximumDelay(kTargetMaxDelayMs));
for (int n = 0; n < 30; ++n) // Run enough iterations to fill the buffer.
Run(false);
int capped_optimal_delay = GetCurrentOptimalDelayMs();
EXPECT_EQ(kTargetMaxDelayMs, capped_optimal_delay);
}
private:
static const int kSampleRateHz = 16000;
static const int kNum10msPerFrame = 2;
static const size_t kFrameSizeSamples = 320; // 20 ms @ 16 kHz.
// payload-len = frame-samples * 2 bytes/sample.
static const int kPayloadLenBytes = 320 * 2;
// Inter-arrival time in number of packets in a jittery channel. One is no
// jitter.
static const int kInterarrivalJitterPacket = 2;
void Push() {
rtp_header_.timestamp += kFrameSizeSamples;
rtp_header_.sequenceNumber++;
ASSERT_EQ(0, neteq_->InsertPacket(
rtp_header_,
ArrayView<const uint8_t>(payload_, kFrameSizeSamples * 2),
Timestamp::MinusInfinity()));
}
// Pull audio equivalent to the amount of audio in one RTP packet.
void Pull() {
AudioFrame frame;
bool muted;
for (int k = 0; k < kNum10msPerFrame; ++k) { // Pull one frame.
ASSERT_EQ(NetEq::kOK, neteq_->GetAudio(&frame, &muted));
ASSERT_FALSE(muted);
// Had to use ASSERT_TRUE, ASSERT_EQ generated error.
ASSERT_TRUE(kSampleRateHz == frame.sample_rate_hz_);
ASSERT_EQ(1u, frame.num_channels_);
ASSERT_TRUE(kSampleRateHz / 100 == frame.samples_per_channel_);
}
}
void Run(bool clean) {
for (int n = 0; n < 10; ++n) {
for (int m = 0; m < 5; ++m) {
Push();
Pull();
}
if (!clean) {
for (int o = 0; o < 10; ++o) { // Long enough to trigger delay change.
Push();
for (int p = 0; p < kInterarrivalJitterPacket; ++p)
Pull();
}
}
}
}
int SetMinimumDelay(int delay_ms) {
return neteq_->SetMinimumDelay(delay_ms);
}
int SetMaximumDelay(int delay_ms) {
return neteq_->SetMaximumDelay(delay_ms);
}
int GetCurrentOptimalDelayMs() {
NetEqNetworkStatistics neteq_stats;
neteq_->NetworkStatistics(&neteq_stats);
return neteq_stats.preferred_buffer_size_ms;
}
std::unique_ptr<NetEq> neteq_;
RTPHeader rtp_header_;
uint8_t payload_[kPayloadLenBytes];
};
// Flaky on iOS: webrtc:7057.
#if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS)
#define MAYBE_OutOfRangeInput DISABLED_OutOfRangeInput
#else
#define MAYBE_OutOfRangeInput OutOfRangeInput
#endif
TEST_F(TargetDelayTest, MAYBE_OutOfRangeInput) {
OutOfRangeInput();
}
// Flaky on iOS: webrtc:7057.
#if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS)
#define MAYBE_TargetDelayBufferMinMax DISABLED_TargetDelayBufferMinMax
#else
#define MAYBE_TargetDelayBufferMinMax TargetDelayBufferMinMax
#endif
TEST_F(TargetDelayTest, MAYBE_TargetDelayBufferMinMax) {
TargetDelayBufferMinMax();
}
} // namespace webrtc
|