Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(542)

Side by Side Diff: media/cast/audio_receiver/audio_decoder_unittest.cc

Issue 214273003: [Cast] Remove AudioDecoder's dependency on WebRTC, and refactor/clean-up AudioReceiver. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: One moar Windows compile fix. Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « media/cast/audio_receiver/audio_decoder.cc ('k') | media/cast/audio_receiver/audio_receiver.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/test/simple_test_tick_clock.h" 5 #include "base/bind.h"
6 #include "base/bind_helpers.h"
7 #include "base/stl_util.h"
8 #include "base/synchronization/condition_variable.h"
9 #include "base/synchronization/lock.h"
10 #include "base/sys_byteorder.h"
11 #include "base/time/time.h"
6 #include "media/cast/audio_receiver/audio_decoder.h" 12 #include "media/cast/audio_receiver/audio_decoder.h"
7 #include "media/cast/cast_environment.h" 13 #include "media/cast/cast_config.h"
8 #include "media/cast/test/fake_single_thread_task_runner.h" 14 #include "media/cast/test/utility/audio_utility.h"
9 #include "testing/gmock/include/gmock/gmock.h" 15 #include "media/cast/test/utility/standalone_cast_environment.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17 #include "third_party/opus/src/include/opus.h"
10 18
11 namespace media { 19 namespace media {
12 namespace cast { 20 namespace cast {
13 21
14 namespace { 22 namespace {
15 class TestRtpPayloadFeedback : public RtpPayloadFeedback { 23 struct TestScenario {
24 transport::AudioCodec codec;
25 int num_channels;
26 int sampling_rate;
27
28 TestScenario(transport::AudioCodec c, int n, int s)
29 : codec(c), num_channels(n), sampling_rate(s) {}
30 };
31 } // namespace
32
33 class AudioDecoderTest : public ::testing::TestWithParam<TestScenario> {
16 public: 34 public:
17 TestRtpPayloadFeedback() {} 35 AudioDecoderTest()
18 virtual ~TestRtpPayloadFeedback() {} 36 : cast_environment_(new StandaloneCastEnvironment()),
19 37 cond_(&lock_) {}
20 virtual void CastFeedback(const RtcpCastMessage& cast_feedback) OVERRIDE { 38
21 EXPECT_EQ(1u, cast_feedback.ack_frame_id_);
22 EXPECT_EQ(0u, cast_feedback.missing_frames_and_packets_.size());
23 }
24 };
25 } // namespace.
26
27 class AudioDecoderTest : public ::testing::Test {
28 protected: 39 protected:
29 AudioDecoderTest() { 40 virtual void SetUp() OVERRIDE {
30 testing_clock_ = new base::SimpleTestTickClock(); 41 AudioReceiverConfig decoder_config;
31 testing_clock_->Advance(base::TimeDelta::FromMilliseconds(1234)); 42 decoder_config.use_external_decoder = false;
32 task_runner_ = new test::FakeSingleThreadTaskRunner(testing_clock_); 43 decoder_config.frequency = GetParam().sampling_rate;
33 cast_environment_ = 44 decoder_config.channels = GetParam().num_channels;
34 new CastEnvironment(scoped_ptr<base::TickClock>(testing_clock_).Pass(), 45 decoder_config.codec = GetParam().codec;
35 task_runner_, 46 audio_decoder_.reset(new AudioDecoder(cast_environment_, decoder_config));
36 task_runner_, 47 CHECK_EQ(STATUS_AUDIO_INITIALIZED, audio_decoder_->InitializationResult());
37 task_runner_); 48
38 } 49 audio_bus_factory_.reset(
39 virtual ~AudioDecoderTest() {} 50 new TestAudioBusFactory(GetParam().num_channels,
40 51 GetParam().sampling_rate,
41 void Configure(const AudioReceiverConfig& audio_config) { 52 TestAudioBusFactory::kMiddleANoteFreq,
42 audio_decoder_.reset( 53 0.5f));
43 new AudioDecoder(cast_environment_, audio_config, &cast_feedback_)); 54 last_frame_id_ = 0;
44 } 55 seen_a_decoded_frame_ = false;
45 56
46 TestRtpPayloadFeedback cast_feedback_; 57 if (GetParam().codec == transport::kOpus) {
47 // Owned by CastEnvironment. 58 opus_encoder_memory_.reset(
48 base::SimpleTestTickClock* testing_clock_; 59 new uint8[opus_encoder_get_size(GetParam().num_channels)]);
49 scoped_refptr<test::FakeSingleThreadTaskRunner> task_runner_; 60 OpusEncoder* const opus_encoder =
50 scoped_refptr<CastEnvironment> cast_environment_; 61 reinterpret_cast<OpusEncoder*>(opus_encoder_memory_.get());
62 CHECK_EQ(OPUS_OK, opus_encoder_init(opus_encoder,
63 GetParam().sampling_rate,
64 GetParam().num_channels,
65 OPUS_APPLICATION_AUDIO));
66 CHECK_EQ(OPUS_OK,
67 opus_encoder_ctl(opus_encoder, OPUS_SET_BITRATE(OPUS_AUTO)));
68 }
69
70 total_audio_feed_in_ = base::TimeDelta();
71 total_audio_decoded_ = base::TimeDelta();
72 }
73
74 // Called from the unit test thread to create another EncodedAudioFrame and
75 // push it into the decoding pipeline.
76 void FeedMoreAudio(const base::TimeDelta& duration,
77 int num_dropped_frames) {
78 // Prepare a simulated EncodedAudioFrame to feed into the AudioDecoder.
79 scoped_ptr<transport::EncodedAudioFrame> encoded_frame(
80 new transport::EncodedAudioFrame());
81 encoded_frame->codec = GetParam().codec;
82 encoded_frame->frame_id = last_frame_id_ + 1 + num_dropped_frames;
83 last_frame_id_ = encoded_frame->frame_id;
84
85 const scoped_ptr<AudioBus> audio_bus(
86 audio_bus_factory_->NextAudioBus(duration).Pass());
87
88 // Encode |audio_bus| into |encoded_frame->data|.
89 const int num_elements = audio_bus->channels() * audio_bus->frames();
90 std::vector<int16> interleaved(num_elements);
91 audio_bus->ToInterleaved(
92 audio_bus->frames(), sizeof(int16), &interleaved.front());
93 if (GetParam().codec == transport::kPcm16) {
94 encoded_frame->data.resize(num_elements * sizeof(int16));
95 int16* const pcm_data =
96 reinterpret_cast<int16*>(string_as_array(&encoded_frame->data));
97 for (size_t i = 0; i < interleaved.size(); ++i)
98 pcm_data[i] = static_cast<int16>(base::HostToNet16(interleaved[i]));
99 } else if (GetParam().codec == transport::kOpus) {
100 OpusEncoder* const opus_encoder =
101 reinterpret_cast<OpusEncoder*>(opus_encoder_memory_.get());
102 const int kOpusEncodeBufferSize = 4000;
103 encoded_frame->data.resize(kOpusEncodeBufferSize);
104 const int payload_size =
105 opus_encode(opus_encoder,
106 &interleaved.front(),
107 audio_bus->frames(),
108 reinterpret_cast<unsigned char*>(
109 string_as_array(&encoded_frame->data)),
110 encoded_frame->data.size());
111 CHECK_GT(payload_size, 1);
112 encoded_frame->data.resize(payload_size);
113 } else {
114 ASSERT_TRUE(false); // Not reached.
115 }
116
117 {
118 base::AutoLock auto_lock(lock_);
119 total_audio_feed_in_ += duration;
120 }
121
122 cast_environment_->PostTask(
123 CastEnvironment::MAIN,
124 FROM_HERE,
125 base::Bind(&AudioDecoder::DecodeFrame,
126 base::Unretained(audio_decoder_.get()),
127 base::Passed(&encoded_frame),
128 base::Bind(&AudioDecoderTest::OnDecodedFrame,
129 base::Unretained(this),
130 num_dropped_frames == 0)));
131 }
132
133 // Blocks the caller until all audio that has been feed in has been decoded.
134 void WaitForAllAudioToBeDecoded() {
135 DCHECK(!cast_environment_->CurrentlyOn(CastEnvironment::MAIN));
136 base::AutoLock auto_lock(lock_);
137 while (total_audio_decoded_ < total_audio_feed_in_)
138 cond_.Wait();
139 EXPECT_EQ(total_audio_feed_in_.InMicroseconds(),
140 total_audio_decoded_.InMicroseconds());
141 }
142
143 private:
144 // Called by |audio_decoder_| to deliver each frame of decoded audio.
145 void OnDecodedFrame(bool should_be_continuous,
146 scoped_ptr<AudioBus> audio_bus,
147 bool is_continuous) {
148 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN));
149
150 // A NULL |audio_bus| indicates a decode error, which we don't expect.
151 ASSERT_FALSE(!audio_bus);
152
153 // Did the decoder detect whether frames were dropped?
154 EXPECT_EQ(should_be_continuous, is_continuous);
155
156 // Does the audio data seem to be intact? For Opus, we have to ignore the
157 // first frame seen at the start (and immediately after dropped packet
158 // recovery) because it introduces a tiny, significant delay.
159 bool examine_signal = true;
160 if (GetParam().codec == transport::kOpus) {
161 examine_signal = seen_a_decoded_frame_ && should_be_continuous;
162 seen_a_decoded_frame_ = true;
163 }
164 if (examine_signal) {
165 for (int ch = 0; ch < audio_bus->channels(); ++ch) {
166 EXPECT_NEAR(
167 TestAudioBusFactory::kMiddleANoteFreq * 2 * audio_bus->frames() /
168 GetParam().sampling_rate,
169 CountZeroCrossings(audio_bus->channel(ch), audio_bus->frames()),
170 1);
171 }
172 }
173
174 // Signal the main test thread that more audio was decoded.
175 base::AutoLock auto_lock(lock_);
176 total_audio_decoded_ += base::TimeDelta::FromSeconds(1) *
177 audio_bus->frames() / GetParam().sampling_rate;
178 cond_.Signal();
179 }
180
181 const scoped_refptr<StandaloneCastEnvironment> cast_environment_;
51 scoped_ptr<AudioDecoder> audio_decoder_; 182 scoped_ptr<AudioDecoder> audio_decoder_;
183 scoped_ptr<TestAudioBusFactory> audio_bus_factory_;
184 uint32 last_frame_id_;
185 bool seen_a_decoded_frame_;
186 scoped_ptr<uint8[]> opus_encoder_memory_;
187
188 base::Lock lock_;
189 base::ConditionVariable cond_;
190 base::TimeDelta total_audio_feed_in_;
191 base::TimeDelta total_audio_decoded_;
52 192
53 DISALLOW_COPY_AND_ASSIGN(AudioDecoderTest); 193 DISALLOW_COPY_AND_ASSIGN(AudioDecoderTest);
54 }; 194 };
55 195
56 TEST_F(AudioDecoderTest, Pcm16MonoNoResampleOnePacket) { 196 TEST_P(AudioDecoderTest, DecodesFramesWithSameDuration) {
57 AudioReceiverConfig audio_config; 197 const base::TimeDelta kTenMilliseconds =
58 audio_config.rtp_payload_type = 127; 198 base::TimeDelta::FromMilliseconds(10);
59 audio_config.frequency = 16000; 199 const int kNumFrames = 10;
60 audio_config.channels = 1; 200 for (int i = 0; i < kNumFrames; ++i)
61 audio_config.codec = transport::kPcm16; 201 FeedMoreAudio(kTenMilliseconds, 0);
62 audio_config.use_external_decoder = false; 202 WaitForAllAudioToBeDecoded();
63 Configure(audio_config);
64
65 RtpCastHeader rtp_header;
66 rtp_header.webrtc.header.payloadType = 127;
67 rtp_header.webrtc.header.sequenceNumber = 1234;
68 rtp_header.webrtc.header.timestamp = 0x87654321;
69 rtp_header.webrtc.header.ssrc = 0x12345678;
70 rtp_header.webrtc.header.paddingLength = 0;
71 rtp_header.webrtc.header.headerLength = 12;
72 rtp_header.webrtc.type.Audio.channel = 1;
73 rtp_header.webrtc.type.Audio.isCNG = false;
74
75 std::vector<int16> payload(640, 0x1234);
76 int number_of_10ms_blocks = 4;
77 int desired_frequency = 16000;
78 PcmAudioFrame audio_frame;
79 uint32 rtp_timestamp;
80
81 EXPECT_FALSE(audio_decoder_->GetRawAudioFrame(
82 number_of_10ms_blocks, desired_frequency, &audio_frame, &rtp_timestamp));
83
84 uint8* payload_data = reinterpret_cast<uint8*>(&payload[0]);
85 size_t payload_size = payload.size() * sizeof(int16);
86
87 audio_decoder_->IncomingParsedRtpPacket(
88 payload_data, payload_size, rtp_header);
89
90 EXPECT_TRUE(audio_decoder_->GetRawAudioFrame(
91 number_of_10ms_blocks, desired_frequency, &audio_frame, &rtp_timestamp));
92 EXPECT_EQ(1, audio_frame.channels);
93 EXPECT_EQ(16000, audio_frame.frequency);
94 EXPECT_EQ(640ul, audio_frame.samples.size());
95 // First 10 samples per channel are 0 from NetEq.
96 for (size_t i = 10; i < audio_frame.samples.size(); ++i) {
97 EXPECT_EQ(0x3412, audio_frame.samples[i]);
98 }
99 } 203 }
100 204
101 TEST_F(AudioDecoderTest, Pcm16StereoNoResampleTwoPackets) { 205 TEST_P(AudioDecoderTest, DecodesFramesWithVaryingDuration) {
102 AudioReceiverConfig audio_config; 206 // These are the set of frame durations supported by the Opus encoder.
103 audio_config.rtp_payload_type = 127; 207 const int kFrameDurationMs[] = { 5, 10, 20, 40, 60 };
104 audio_config.frequency = 16000; 208
105 audio_config.channels = 2; 209 const int kNumFrames = 10;
106 audio_config.codec = transport::kPcm16; 210 for (size_t i = 0; i < arraysize(kFrameDurationMs); ++i)
107 audio_config.use_external_decoder = false; 211 for (int j = 0; j < kNumFrames; ++j)
108 Configure(audio_config); 212 FeedMoreAudio(base::TimeDelta::FromMilliseconds(kFrameDurationMs[i]), 0);
109 213 WaitForAllAudioToBeDecoded();
110 RtpCastHeader rtp_header;
111 rtp_header.frame_id = 0;
112 rtp_header.webrtc.header.payloadType = 127;
113 rtp_header.webrtc.header.sequenceNumber = 1234;
114 rtp_header.webrtc.header.timestamp = 0x87654321;
115 rtp_header.webrtc.header.ssrc = 0x12345678;
116 rtp_header.webrtc.header.paddingLength = 0;
117 rtp_header.webrtc.header.headerLength = 12;
118
119 rtp_header.webrtc.type.Audio.isCNG = false;
120 rtp_header.webrtc.type.Audio.channel = 2;
121
122 std::vector<int16> payload(640, 0x1234);
123
124 uint8* payload_data = reinterpret_cast<uint8*>(&payload[0]);
125 size_t payload_size = payload.size() * sizeof(int16);
126
127 audio_decoder_->IncomingParsedRtpPacket(
128 payload_data, payload_size, rtp_header);
129
130 int number_of_10ms_blocks = 2;
131 int desired_frequency = 16000;
132 PcmAudioFrame audio_frame;
133 uint32 rtp_timestamp;
134
135 EXPECT_TRUE(audio_decoder_->GetRawAudioFrame(
136 number_of_10ms_blocks, desired_frequency, &audio_frame, &rtp_timestamp));
137 EXPECT_EQ(2, audio_frame.channels);
138 EXPECT_EQ(16000, audio_frame.frequency);
139 EXPECT_EQ(640ul, audio_frame.samples.size());
140 // First 10 samples per channel are 0 from NetEq.
141 for (size_t i = 10 * audio_config.channels; i < audio_frame.samples.size();
142 ++i) {
143 EXPECT_EQ(0x3412, audio_frame.samples[i]);
144 }
145
146 rtp_header.frame_id++;
147 rtp_header.webrtc.header.sequenceNumber++;
148 rtp_header.webrtc.header.timestamp += (audio_config.frequency / 100) * 2 * 2;
149
150 audio_decoder_->IncomingParsedRtpPacket(
151 payload_data, payload_size, rtp_header);
152
153 EXPECT_TRUE(audio_decoder_->GetRawAudioFrame(
154 number_of_10ms_blocks, desired_frequency, &audio_frame, &rtp_timestamp));
155 EXPECT_EQ(2, audio_frame.channels);
156 EXPECT_EQ(16000, audio_frame.frequency);
157 EXPECT_EQ(640ul, audio_frame.samples.size());
158 for (size_t i = 0; i < audio_frame.samples.size(); ++i) {
159 EXPECT_NEAR(0x3412, audio_frame.samples[i], 1000);
160 }
161 // Test cast callback.
162 audio_decoder_->SendCastMessage();
163 testing_clock_->Advance(base::TimeDelta::FromMilliseconds(33));
164 audio_decoder_->SendCastMessage();
165 } 214 }
166 215
167 TEST_F(AudioDecoderTest, Pcm16Resample) { 216 TEST_P(AudioDecoderTest, RecoversFromDroppedFrames) {
168 AudioReceiverConfig audio_config; 217 const base::TimeDelta kTenMilliseconds =
169 audio_config.rtp_payload_type = 127; 218 base::TimeDelta::FromMilliseconds(10);
170 audio_config.frequency = 16000; 219 const int kNumFrames = 100;
171 audio_config.channels = 2; 220 int next_drop_at = 3;
172 audio_config.codec = transport::kPcm16; 221 int next_num_dropped = 1;
173 audio_config.use_external_decoder = false; 222 for (int i = 0; i < kNumFrames; ++i) {
174 Configure(audio_config); 223 if (i == next_drop_at) {
175 224 const int num_dropped = next_num_dropped++;
176 RtpCastHeader rtp_header; 225 next_drop_at *= 2;
177 rtp_header.webrtc.header.payloadType = 127; 226 i += num_dropped;
178 rtp_header.webrtc.header.sequenceNumber = 1234; 227 FeedMoreAudio(kTenMilliseconds, num_dropped);
179 rtp_header.webrtc.header.timestamp = 0x87654321; 228 } else {
180 rtp_header.webrtc.header.ssrc = 0x12345678; 229 FeedMoreAudio(kTenMilliseconds, 0);
181 rtp_header.webrtc.header.paddingLength = 0; 230 }
182 rtp_header.webrtc.header.headerLength = 12; 231 }
183 232 WaitForAllAudioToBeDecoded();
184 rtp_header.webrtc.type.Audio.isCNG = false;
185 rtp_header.webrtc.type.Audio.channel = 2;
186
187 std::vector<int16> payload(640, 0x1234);
188
189 uint8* payload_data = reinterpret_cast<uint8*>(&payload[0]);
190 size_t payload_size = payload.size() * sizeof(int16);
191
192 audio_decoder_->IncomingParsedRtpPacket(
193 payload_data, payload_size, rtp_header);
194
195 int number_of_10ms_blocks = 2;
196 int desired_frequency = 48000;
197 PcmAudioFrame audio_frame;
198 uint32 rtp_timestamp;
199
200 EXPECT_TRUE(audio_decoder_->GetRawAudioFrame(
201 number_of_10ms_blocks, desired_frequency, &audio_frame, &rtp_timestamp));
202
203 EXPECT_EQ(2, audio_frame.channels);
204 EXPECT_EQ(48000, audio_frame.frequency);
205 EXPECT_EQ(1920ul, audio_frame.samples.size()); // Upsampled to 48 KHz.
206 int count = 0;
207 // Resampling makes the variance worse.
208 for (size_t i = 100 * audio_config.channels; i < audio_frame.samples.size();
209 ++i) {
210 EXPECT_NEAR(0x3412, audio_frame.samples[i], 400);
211 if (0x3412 == audio_frame.samples[i])
212 count++;
213 }
214 } 233 }
215 234
235 INSTANTIATE_TEST_CASE_P(AudioDecoderTestScenarios,
236 AudioDecoderTest,
237 ::testing::Values(
238 TestScenario(transport::kPcm16, 1, 8000),
239 TestScenario(transport::kPcm16, 2, 48000),
240 TestScenario(transport::kOpus, 1, 8000),
241 TestScenario(transport::kOpus, 2, 48000)));
242
216 } // namespace cast 243 } // namespace cast
217 } // namespace media 244 } // namespace media
OLDNEW
« no previous file with comments | « media/cast/audio_receiver/audio_decoder.cc ('k') | media/cast/audio_receiver/audio_receiver.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698