blob: fec25e985e15c5325337b5c7fadf8ca287645bc4 [file] [log] [blame]
henrik.lundin@webrtc.org9a400812013-01-29 12:09:21 +00001/*
2 * Copyright (c) 2013 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// Test to verify correct operation for externally created decoders.
12
13#include <string>
14#include <list>
15
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"
henrike@webrtc.org7537dde2013-07-08 18:53:54 +000024#include "webrtc/test/testsupport/gtest_disable.h"
henrik.lundin@webrtc.org9a400812013-01-29 12:09:21 +000025
26namespace webrtc {
27
28using ::testing::_;
29
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.
35class NetEqExternalDecoderTest : public ::testing::Test {
36 protected:
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;
41
42 NetEqExternalDecoderTest()
43 : sample_rate_hz_(kSampleRateHz),
44 samples_per_ms_(sample_rate_hz_ / 1000),
45 frame_size_ms_(10),
46 frame_size_samples_(frame_size_ms_ * samples_per_ms_),
47 output_size_samples_(frame_size_ms_ * samples_per_ms_),
48 neteq_external_(NetEq::Create(sample_rate_hz_)),
49 neteq_(NetEq::Create(sample_rate_hz_)),
50 external_decoder_(new MockExternalPcm16B(kDecoderPCM16Bswb32kHz)),
51 rtp_generator_(samples_per_ms_),
52 payload_size_bytes_(0),
53 last_send_time_(0),
54 last_arrival_time_(0) {
55 input_ = new int16_t[frame_size_samples_];
56 encoded_ = new uint8_t[2 * frame_size_samples_];
57 }
58
59 ~NetEqExternalDecoderTest() {
60 delete neteq_external_;
61 delete neteq_;
62 // We will now delete the decoder ourselves, so expecting Die to be called.
63 EXPECT_CALL(*external_decoder_, Die()).Times(1);
64 delete external_decoder_;
65 delete [] input_;
66 delete [] encoded_;
67 }
68
69 virtual void SetUp() {
70 const std::string file_name =
71 webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm");
72 input_file_.reset(new test::InputAudioFile(file_name));
73 assert(sample_rate_hz_ == 32000);
74 NetEqDecoder decoder = kDecoderPCM16Bswb32kHz;
75 EXPECT_CALL(*external_decoder_, Init());
76 // NetEq is not allowed to delete the external decoder (hence Times(0)).
77 EXPECT_CALL(*external_decoder_, Die()).Times(0);
78 ASSERT_EQ(NetEq::kOK,
79 neteq_external_->RegisterExternalDecoder(external_decoder_,
80 decoder,
81 sample_rate_hz_,
82 kPayloadType));
83 ASSERT_EQ(NetEq::kOK,
84 neteq_->RegisterPayloadType(decoder, kPayloadType));
85 }
86
87 virtual void TearDown() {}
88
89 int GetNewPackets() {
90 if (!input_file_->Read(frame_size_samples_, input_)) {
91 return -1;
92 }
93 payload_size_bytes_ = WebRtcPcm16b_Encode(input_, frame_size_samples_,
94 encoded_);
95 if (frame_size_samples_ * 2 != payload_size_bytes_) {
96 return -1;
97 }
98 int next_send_time = rtp_generator_.GetRtpHeader(kPayloadType,
99 frame_size_samples_,
100 &rtp_header_);
101 return next_send_time;
102 }
103
104 void VerifyOutput(size_t num_samples) {
105 for (size_t i = 0; i < num_samples; ++i) {
106 ASSERT_EQ(output_[i], output_external_[i]) <<
107 "Diff in sample " << i << ".";
108 }
109 }
110
111 virtual int GetArrivalTime(int send_time) {
112 int arrival_time = last_arrival_time_ + (send_time - last_send_time_);
113 last_send_time_ = send_time;
114 last_arrival_time_ = arrival_time;
115 return arrival_time;
116 }
117
118 virtual bool Lost() { return false; }
119
120 void RunTest(int num_loops) {
121 // Get next input packets (mono and multi-channel).
122 int next_send_time;
123 int next_arrival_time;
124 do {
125 next_send_time = GetNewPackets();
126 ASSERT_NE(-1, next_send_time);
127 next_arrival_time = GetArrivalTime(next_send_time);
128 } while (Lost()); // If lost, immediately read the next packet.
129
130 EXPECT_CALL(*external_decoder_, Decode(_, payload_size_bytes_, _, _))
131 .Times(num_loops);
132
133 int time_now = 0;
134 for (int k = 0; k < num_loops; ++k) {
135 while (time_now >= next_arrival_time) {
136 // Insert packet in regular instance.
137 ASSERT_EQ(NetEq::kOK,
138 neteq_->InsertPacket(rtp_header_, encoded_,
139 payload_size_bytes_,
140 next_arrival_time));
141 // Insert packet in external decoder instance.
142 EXPECT_CALL(*external_decoder_,
143 IncomingPacket(_, payload_size_bytes_,
144 rtp_header_.header.sequenceNumber,
145 rtp_header_.header.timestamp,
146 next_arrival_time));
147 ASSERT_EQ(NetEq::kOK,
148 neteq_external_->InsertPacket(rtp_header_, encoded_,
149 payload_size_bytes_,
150 next_arrival_time));
151 // Get next input packet.
152 do {
153 next_send_time = GetNewPackets();
154 ASSERT_NE(-1, next_send_time);
155 next_arrival_time = GetArrivalTime(next_send_time);
156 } while (Lost()); // If lost, immediately read the next packet.
157 }
158 NetEqOutputType output_type;
159 // Get audio from regular instance.
160 int samples_per_channel;
161 int num_channels;
162 EXPECT_EQ(NetEq::kOK,
163 neteq_->GetAudio(kMaxBlockSize, output_,
164 &samples_per_channel, &num_channels,
165 &output_type));
166 EXPECT_EQ(1, num_channels);
167 EXPECT_EQ(output_size_samples_, samples_per_channel);
168 // Get audio from external decoder instance.
169 ASSERT_EQ(NetEq::kOK,
170 neteq_external_->GetAudio(kMaxBlockSize, output_external_,
171 &samples_per_channel, &num_channels,
172 &output_type));
173 EXPECT_EQ(1, num_channels);
174 EXPECT_EQ(output_size_samples_, samples_per_channel);
175 std::ostringstream ss;
176 ss << "Lap number " << k << ".";
177 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
178 // Compare mono and multi-channel.
179 ASSERT_NO_FATAL_FAILURE(VerifyOutput(output_size_samples_));
180
181 time_now += kTimeStepMs;
182 }
183 }
184
185 const int sample_rate_hz_;
186 const int samples_per_ms_;
187 const int frame_size_ms_;
188 const int frame_size_samples_;
189 const int output_size_samples_;
190 NetEq* neteq_external_;
191 NetEq* neteq_;
192 MockExternalPcm16B* external_decoder_;
193 test::RtpGenerator rtp_generator_;
194 int16_t* input_;
195 uint8_t* encoded_;
196 int16_t output_[kMaxBlockSize];
197 int16_t output_external_[kMaxBlockSize];
198 WebRtcRTPHeader rtp_header_;
199 int payload_size_bytes_;
200 int last_send_time_;
201 int last_arrival_time_;
202 scoped_ptr<test::InputAudioFile> input_file_;
203};
204
henrike@webrtc.org7537dde2013-07-08 18:53:54 +0000205TEST_F(NetEqExternalDecoderTest, DISABLED_ON_ANDROID(RunTest)) {
henrik.lundin@webrtc.org9a400812013-01-29 12:09:21 +0000206 RunTest(100); // Run 100 laps @ 10 ms each in the test loop.
207}
208
209} // namespace webrtc