Chromium Code Reviews| OLD | NEW |
|---|---|
| 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(CastLoggingConfig())), |
| 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 GetDefaultCastReceiverLoggingConfig()); | 49 audio_bus_factory_.reset( |
| 39 } | 50 new TestAudioBusFactory(GetParam().num_channels, |
| 40 virtual ~AudioDecoderTest() {} | 51 GetParam().sampling_rate, |
| 41 | 52 TestAudioBusFactory::kMiddleANoteFreq, |
| 42 void Configure(const AudioReceiverConfig& audio_config) { | 53 0.5f)); |
| 43 audio_decoder_.reset( | 54 last_frame_id_ = 0; |
| 44 new AudioDecoder(cast_environment_, audio_config, &cast_feedback_)); | 55 seen_a_decoded_frame_ = false; |
| 45 } | 56 |
| 46 | 57 if (GetParam().codec == transport::kOpus) { |
| 47 TestRtpPayloadFeedback cast_feedback_; | 58 opus_encoder_memory_.reset( |
| 48 // Owned by CastEnvironment. | 59 new uint8[opus_encoder_get_size(GetParam().num_channels)]); |
| 49 base::SimpleTestTickClock* testing_clock_; | 60 OpusEncoder* const opus_encoder = |
| 50 scoped_refptr<test::FakeSingleThreadTaskRunner> task_runner_; | 61 reinterpret_cast<OpusEncoder*>(opus_encoder_memory_.get()); |
| 51 scoped_refptr<CastEnvironment> cast_environment_; | 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_packets) { | |
| 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_packets; | |
| 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_packets == 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 packets 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_; | |
| 52 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_; | |
| 53 | 192 |
| 54 DISALLOW_COPY_AND_ASSIGN(AudioDecoderTest); | 193 DISALLOW_COPY_AND_ASSIGN(AudioDecoderTest); |
| 55 }; | 194 }; |
| 56 | 195 |
| 57 TEST_F(AudioDecoderTest, Pcm16MonoNoResampleOnePacket) { | 196 TEST_P(AudioDecoderTest, DecodesFramesWithSameDuration) { |
| 58 AudioReceiverConfig audio_config; | 197 const base::TimeDelta kTenMilliseconds = |
| 59 audio_config.rtp_payload_type = 127; | 198 base::TimeDelta::FromMilliseconds(10); |
| 60 audio_config.frequency = 16000; | 199 const int kNumPackets = 10; |
| 61 audio_config.channels = 1; | 200 for (int i = 0; i < kNumPackets; ++i) |
| 62 audio_config.codec = transport::kPcm16; | 201 FeedMoreAudio(kTenMilliseconds, 0); |
| 63 audio_config.use_external_decoder = false; | 202 WaitForAllAudioToBeDecoded(); |
| 64 Configure(audio_config); | |
| 65 | |
| 66 RtpCastHeader rtp_header; | |
| 67 rtp_header.webrtc.header.payloadType = 127; | |
| 68 rtp_header.webrtc.header.sequenceNumber = 1234; | |
| 69 rtp_header.webrtc.header.timestamp = 0x87654321; | |
| 70 rtp_header.webrtc.header.ssrc = 0x12345678; | |
| 71 rtp_header.webrtc.header.paddingLength = 0; | |
| 72 rtp_header.webrtc.header.headerLength = 12; | |
| 73 rtp_header.webrtc.type.Audio.channel = 1; | |
| 74 rtp_header.webrtc.type.Audio.isCNG = false; | |
| 75 | |
| 76 std::vector<int16> payload(640, 0x1234); | |
| 77 int number_of_10ms_blocks = 4; | |
| 78 int desired_frequency = 16000; | |
| 79 PcmAudioFrame audio_frame; | |
| 80 uint32 rtp_timestamp; | |
| 81 | |
| 82 EXPECT_FALSE(audio_decoder_->GetRawAudioFrame( | |
| 83 number_of_10ms_blocks, desired_frequency, &audio_frame, &rtp_timestamp)); | |
| 84 | |
| 85 uint8* payload_data = reinterpret_cast<uint8*>(&payload[0]); | |
| 86 size_t payload_size = payload.size() * sizeof(int16); | |
| 87 | |
| 88 audio_decoder_->IncomingParsedRtpPacket( | |
| 89 payload_data, payload_size, rtp_header); | |
| 90 | |
| 91 EXPECT_TRUE(audio_decoder_->GetRawAudioFrame( | |
| 92 number_of_10ms_blocks, desired_frequency, &audio_frame, &rtp_timestamp)); | |
| 93 EXPECT_EQ(1, audio_frame.channels); | |
| 94 EXPECT_EQ(16000, audio_frame.frequency); | |
| 95 EXPECT_EQ(640ul, audio_frame.samples.size()); | |
| 96 // First 10 samples per channel are 0 from NetEq. | |
| 97 for (size_t i = 10; i < audio_frame.samples.size(); ++i) { | |
| 98 EXPECT_EQ(0x3412, audio_frame.samples[i]); | |
| 99 } | |
| 100 } | 203 } |
| 101 | 204 |
| 102 TEST_F(AudioDecoderTest, Pcm16StereoNoResampleTwoPackets) { | 205 TEST_P(AudioDecoderTest, DecodesFramesWithVaryingDuration) { |
| 103 AudioReceiverConfig audio_config; | 206 // These are the set of frame durations supported by the Opus encoder. |
| 104 audio_config.rtp_payload_type = 127; | 207 const int kFrameDurationMs[] = { 5, 10, 20, 40, 60 }; |
| 105 audio_config.frequency = 16000; | 208 |
| 106 audio_config.channels = 2; | 209 const int kNumPackets = 10; |
| 107 audio_config.codec = transport::kPcm16; | 210 for (size_t i = 0; i < arraysize(kFrameDurationMs); ++i) |
| 108 audio_config.use_external_decoder = false; | 211 for (int j = 0; j < kNumPackets; ++j) |
| 109 Configure(audio_config); | 212 FeedMoreAudio(base::TimeDelta::FromMilliseconds(kFrameDurationMs[i]), 0); |
| 110 | 213 WaitForAllAudioToBeDecoded(); |
| 111 RtpCastHeader rtp_header; | |
| 112 rtp_header.frame_id = 0; | |
| 113 rtp_header.webrtc.header.payloadType = 127; | |
| 114 rtp_header.webrtc.header.sequenceNumber = 1234; | |
| 115 rtp_header.webrtc.header.timestamp = 0x87654321; | |
| 116 rtp_header.webrtc.header.ssrc = 0x12345678; | |
| 117 rtp_header.webrtc.header.paddingLength = 0; | |
| 118 rtp_header.webrtc.header.headerLength = 12; | |
| 119 | |
| 120 rtp_header.webrtc.type.Audio.isCNG = false; | |
| 121 rtp_header.webrtc.type.Audio.channel = 2; | |
| 122 | |
| 123 std::vector<int16> payload(640, 0x1234); | |
| 124 | |
| 125 uint8* payload_data = reinterpret_cast<uint8*>(&payload[0]); | |
| 126 size_t payload_size = payload.size() * sizeof(int16); | |
| 127 | |
| 128 audio_decoder_->IncomingParsedRtpPacket( | |
| 129 payload_data, payload_size, rtp_header); | |
| 130 | |
| 131 int number_of_10ms_blocks = 2; | |
| 132 int desired_frequency = 16000; | |
| 133 PcmAudioFrame audio_frame; | |
| 134 uint32 rtp_timestamp; | |
| 135 | |
| 136 EXPECT_TRUE(audio_decoder_->GetRawAudioFrame( | |
| 137 number_of_10ms_blocks, desired_frequency, &audio_frame, &rtp_timestamp)); | |
| 138 EXPECT_EQ(2, audio_frame.channels); | |
| 139 EXPECT_EQ(16000, audio_frame.frequency); | |
| 140 EXPECT_EQ(640ul, audio_frame.samples.size()); | |
| 141 // First 10 samples per channel are 0 from NetEq. | |
| 142 for (size_t i = 10 * audio_config.channels; i < audio_frame.samples.size(); | |
| 143 ++i) { | |
| 144 EXPECT_EQ(0x3412, audio_frame.samples[i]); | |
| 145 } | |
| 146 | |
| 147 rtp_header.frame_id++; | |
| 148 rtp_header.webrtc.header.sequenceNumber++; | |
| 149 rtp_header.webrtc.header.timestamp += (audio_config.frequency / 100) * 2 * 2; | |
| 150 | |
| 151 audio_decoder_->IncomingParsedRtpPacket( | |
| 152 payload_data, payload_size, rtp_header); | |
| 153 | |
| 154 EXPECT_TRUE(audio_decoder_->GetRawAudioFrame( | |
| 155 number_of_10ms_blocks, desired_frequency, &audio_frame, &rtp_timestamp)); | |
| 156 EXPECT_EQ(2, audio_frame.channels); | |
| 157 EXPECT_EQ(16000, audio_frame.frequency); | |
| 158 EXPECT_EQ(640ul, audio_frame.samples.size()); | |
| 159 for (size_t i = 0; i < audio_frame.samples.size(); ++i) { | |
| 160 EXPECT_NEAR(0x3412, audio_frame.samples[i], 1000); | |
| 161 } | |
| 162 // Test cast callback. | |
| 163 audio_decoder_->SendCastMessage(); | |
| 164 testing_clock_->Advance(base::TimeDelta::FromMilliseconds(33)); | |
| 165 audio_decoder_->SendCastMessage(); | |
| 166 } | 214 } |
| 167 | 215 |
| 168 TEST_F(AudioDecoderTest, Pcm16Resample) { | 216 TEST_P(AudioDecoderTest, RecoversFromDroppedPackets) { |
|
miu
2014/03/27 05:36:32
TODO for me: Should be called RecoversFromDroppedF
miu
2014/03/28 23:51:58
Done.
| |
| 169 AudioReceiverConfig audio_config; | 217 const base::TimeDelta kTenMilliseconds = |
| 170 audio_config.rtp_payload_type = 127; | 218 base::TimeDelta::FromMilliseconds(10); |
| 171 audio_config.frequency = 16000; | 219 const int kNumPackets = 100; |
| 172 audio_config.channels = 2; | 220 int next_drop_at = 3; |
| 173 audio_config.codec = transport::kPcm16; | 221 int next_num_dropped = 1; |
| 174 audio_config.use_external_decoder = false; | 222 for (int i = 0; i < kNumPackets; ++i) { |
| 175 Configure(audio_config); | 223 if (i == next_drop_at) { |
| 176 | 224 const int num_dropped = next_num_dropped++; |
| 177 RtpCastHeader rtp_header; | 225 next_drop_at *= 2; |
| 178 rtp_header.webrtc.header.payloadType = 127; | 226 i += num_dropped; |
| 179 rtp_header.webrtc.header.sequenceNumber = 1234; | 227 FeedMoreAudio(kTenMilliseconds, num_dropped); |
| 180 rtp_header.webrtc.header.timestamp = 0x87654321; | 228 } else { |
| 181 rtp_header.webrtc.header.ssrc = 0x12345678; | 229 FeedMoreAudio(kTenMilliseconds, 0); |
| 182 rtp_header.webrtc.header.paddingLength = 0; | 230 } |
| 183 rtp_header.webrtc.header.headerLength = 12; | 231 } |
| 184 | 232 WaitForAllAudioToBeDecoded(); |
| 185 rtp_header.webrtc.type.Audio.isCNG = false; | |
| 186 rtp_header.webrtc.type.Audio.channel = 2; | |
| 187 | |
| 188 std::vector<int16> payload(640, 0x1234); | |
| 189 | |
| 190 uint8* payload_data = reinterpret_cast<uint8*>(&payload[0]); | |
| 191 size_t payload_size = payload.size() * sizeof(int16); | |
| 192 | |
| 193 audio_decoder_->IncomingParsedRtpPacket( | |
| 194 payload_data, payload_size, rtp_header); | |
| 195 | |
| 196 int number_of_10ms_blocks = 2; | |
| 197 int desired_frequency = 48000; | |
| 198 PcmAudioFrame audio_frame; | |
| 199 uint32 rtp_timestamp; | |
| 200 | |
| 201 EXPECT_TRUE(audio_decoder_->GetRawAudioFrame( | |
| 202 number_of_10ms_blocks, desired_frequency, &audio_frame, &rtp_timestamp)); | |
| 203 | |
| 204 EXPECT_EQ(2, audio_frame.channels); | |
| 205 EXPECT_EQ(48000, audio_frame.frequency); | |
| 206 EXPECT_EQ(1920ul, audio_frame.samples.size()); // Upsampled to 48 KHz. | |
| 207 int count = 0; | |
| 208 // Resampling makes the variance worse. | |
| 209 for (size_t i = 100 * audio_config.channels; i < audio_frame.samples.size(); | |
| 210 ++i) { | |
| 211 EXPECT_NEAR(0x3412, audio_frame.samples[i], 400); | |
| 212 if (0x3412 == audio_frame.samples[i]) | |
| 213 count++; | |
| 214 } | |
| 215 } | 233 } |
| 216 | 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 | |
| 217 } // namespace cast | 243 } // namespace cast |
| 218 } // namespace media | 244 } // namespace media |
| OLD | NEW |