2 * Copyright (c) 2013 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 // Test to verify correct operation for externally created decoders.
16 #include "gmock/gmock.h"
17 #include "gtest/gtest.h"
18 #include "webrtc/modules/audio_coding/neteq4/interface/neteq.h"
19 #include "webrtc/modules/audio_coding/neteq4/mock/mock_external_decoder_pcm16b.h"
20 #include "webrtc/modules/audio_coding/neteq4/tools/input_audio_file.h"
21 #include "webrtc/modules/audio_coding/neteq4/tools/rtp_generator.h"
22 #include "webrtc/system_wrappers/interface/scoped_ptr.h"
23 #include "webrtc/test/testsupport/fileutils.h"
24 #include "webrtc/test/testsupport/gtest_disable.h"
30 // This test encodes a few packets of PCM16b 32 kHz data and inserts it into two
31 // different NetEq instances. The first instance uses the internal version of
32 // the decoder object, while the second one uses an externally created decoder
33 // object (ExternalPcm16B wrapped in MockExternalPcm16B, both defined above).
34 // The test verifies that the output from both instances match.
35 class NetEqExternalDecoderTest : public ::testing::Test {
37 static const int kTimeStepMs = 10;
38 static const int kMaxBlockSize = 480; // 10 ms @ 48 kHz.
39 static const uint8_t kPayloadType = 95;
40 static const int kSampleRateHz = 32000;
42 NetEqExternalDecoderTest()
43 : sample_rate_hz_(kSampleRateHz),
44 samples_per_ms_(sample_rate_hz_ / 1000),
46 frame_size_samples_(frame_size_ms_ * samples_per_ms_),
47 output_size_samples_(frame_size_ms_ * samples_per_ms_),
48 external_decoder_(new MockExternalPcm16B(kDecoderPCM16Bswb32kHz)),
49 rtp_generator_(samples_per_ms_),
50 payload_size_bytes_(0),
52 last_arrival_time_(0) {
54 config.sample_rate_hz = sample_rate_hz_;
55 neteq_external_ = NetEq::Create(config);
56 neteq_ = NetEq::Create(config);
57 input_ = new int16_t[frame_size_samples_];
58 encoded_ = new uint8_t[2 * frame_size_samples_];
61 ~NetEqExternalDecoderTest() {
62 delete neteq_external_;
64 // We will now delete the decoder ourselves, so expecting Die to be called.
65 EXPECT_CALL(*external_decoder_, Die()).Times(1);
66 delete external_decoder_;
71 virtual void SetUp() {
72 const std::string file_name =
73 webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm");
74 input_file_.reset(new test::InputAudioFile(file_name));
75 assert(sample_rate_hz_ == 32000);
76 NetEqDecoder decoder = kDecoderPCM16Bswb32kHz;
77 EXPECT_CALL(*external_decoder_, Init());
78 // NetEq is not allowed to delete the external decoder (hence Times(0)).
79 EXPECT_CALL(*external_decoder_, Die()).Times(0);
81 neteq_external_->RegisterExternalDecoder(external_decoder_,
85 neteq_->RegisterPayloadType(decoder, kPayloadType));
88 virtual void TearDown() {}
91 if (!input_file_->Read(frame_size_samples_, input_)) {
94 payload_size_bytes_ = WebRtcPcm16b_Encode(input_, frame_size_samples_,
96 if (frame_size_samples_ * 2 != payload_size_bytes_) {
99 int next_send_time = rtp_generator_.GetRtpHeader(kPayloadType,
102 return next_send_time;
105 void VerifyOutput(size_t num_samples) {
106 for (size_t i = 0; i < num_samples; ++i) {
107 ASSERT_EQ(output_[i], output_external_[i]) <<
108 "Diff in sample " << i << ".";
112 virtual int GetArrivalTime(int send_time) {
113 int arrival_time = last_arrival_time_ + (send_time - last_send_time_);
114 last_send_time_ = send_time;
115 last_arrival_time_ = arrival_time;
119 virtual bool Lost() { return false; }
121 void RunTest(int num_loops) {
122 // Get next input packets (mono and multi-channel).
124 int next_arrival_time;
126 next_send_time = GetNewPackets();
127 ASSERT_NE(-1, next_send_time);
128 next_arrival_time = GetArrivalTime(next_send_time);
129 } while (Lost()); // If lost, immediately read the next packet.
131 EXPECT_CALL(*external_decoder_, Decode(_, payload_size_bytes_, _, _))
135 for (int k = 0; k < num_loops; ++k) {
136 while (time_now >= next_arrival_time) {
137 // Insert packet in regular instance.
138 ASSERT_EQ(NetEq::kOK,
139 neteq_->InsertPacket(rtp_header_, encoded_,
142 // Insert packet in external decoder instance.
143 EXPECT_CALL(*external_decoder_,
144 IncomingPacket(_, payload_size_bytes_,
145 rtp_header_.header.sequenceNumber,
146 rtp_header_.header.timestamp,
148 ASSERT_EQ(NetEq::kOK,
149 neteq_external_->InsertPacket(rtp_header_, encoded_,
152 // Get next input packet.
154 next_send_time = GetNewPackets();
155 ASSERT_NE(-1, next_send_time);
156 next_arrival_time = GetArrivalTime(next_send_time);
157 } while (Lost()); // If lost, immediately read the next packet.
159 NetEqOutputType output_type;
160 // Get audio from regular instance.
161 int samples_per_channel;
163 EXPECT_EQ(NetEq::kOK,
164 neteq_->GetAudio(kMaxBlockSize, output_,
165 &samples_per_channel, &num_channels,
167 EXPECT_EQ(1, num_channels);
168 EXPECT_EQ(output_size_samples_, samples_per_channel);
169 // Get audio from external decoder instance.
170 ASSERT_EQ(NetEq::kOK,
171 neteq_external_->GetAudio(kMaxBlockSize, output_external_,
172 &samples_per_channel, &num_channels,
174 EXPECT_EQ(1, num_channels);
175 EXPECT_EQ(output_size_samples_, samples_per_channel);
176 std::ostringstream ss;
177 ss << "Lap number " << k << ".";
178 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
179 // Compare mono and multi-channel.
180 ASSERT_NO_FATAL_FAILURE(VerifyOutput(output_size_samples_));
182 time_now += kTimeStepMs;
186 const int sample_rate_hz_;
187 const int samples_per_ms_;
188 const int frame_size_ms_;
189 const int frame_size_samples_;
190 const int output_size_samples_;
191 NetEq* neteq_external_;
193 MockExternalPcm16B* external_decoder_;
194 test::RtpGenerator rtp_generator_;
197 int16_t output_[kMaxBlockSize];
198 int16_t output_external_[kMaxBlockSize];
199 WebRtcRTPHeader rtp_header_;
200 int payload_size_bytes_;
202 int last_arrival_time_;
203 scoped_ptr<test::InputAudioFile> input_file_;
206 TEST_F(NetEqExternalDecoderTest, DISABLED_ON_ANDROID(RunTest)) {
207 RunTest(100); // Run 100 laps @ 10 ms each in the test loop.
210 } // namespace webrtc