OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/bind.h" | 5 #include "base/bind.h" |
6 #include "base/bind_helpers.h" | 6 #include "base/bind_helpers.h" |
7 #include "base/synchronization/condition_variable.h" | 7 #include "base/synchronization/condition_variable.h" |
8 #include "base/synchronization/lock.h" | 8 #include "base/synchronization/lock.h" |
9 #include "base/sys_byteorder.h" | 9 #include "base/sys_byteorder.h" |
10 #include "base/time/time.h" | 10 #include "base/time/time.h" |
11 #include "media/cast/cast_config.h" | 11 #include "media/cast/cast_config.h" |
12 #include "media/cast/receiver/audio_decoder.h" | 12 #include "media/cast/receiver/audio_decoder.h" |
13 #include "media/cast/test/utility/audio_utility.h" | 13 #include "media/cast/test/utility/audio_utility.h" |
14 #include "media/cast/test/utility/standalone_cast_environment.h" | 14 #include "media/cast/test/utility/standalone_cast_environment.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
16 #include "third_party/opus/src/include/opus.h" | 16 #include "third_party/opus/src/include/opus.h" |
17 | 17 |
18 namespace media { | 18 namespace media { |
19 namespace cast { | 19 namespace cast { |
20 | 20 |
21 namespace { | 21 namespace { |
22 struct TestScenario { | 22 struct TestScenario { |
23 transport::Codec codec; | 23 Codec codec; |
24 int num_channels; | 24 int num_channels; |
25 int sampling_rate; | 25 int sampling_rate; |
26 | 26 |
27 TestScenario(transport::Codec c, int n, int s) | 27 TestScenario(Codec c, int n, int s) |
28 : codec(c), num_channels(n), sampling_rate(s) {} | 28 : codec(c), num_channels(n), sampling_rate(s) {} |
29 }; | 29 }; |
30 } // namespace | 30 } // namespace |
31 | 31 |
32 class AudioDecoderTest : public ::testing::TestWithParam<TestScenario> { | 32 class AudioDecoderTest : public ::testing::TestWithParam<TestScenario> { |
33 public: | 33 public: |
34 AudioDecoderTest() | 34 AudioDecoderTest() |
35 : cast_environment_(new StandaloneCastEnvironment()), | 35 : cast_environment_(new StandaloneCastEnvironment()), |
36 cond_(&lock_) {} | 36 cond_(&lock_) {} |
37 | 37 |
38 protected: | 38 protected: |
39 virtual void SetUp() OVERRIDE { | 39 virtual void SetUp() OVERRIDE { |
40 audio_decoder_.reset(new AudioDecoder(cast_environment_, | 40 audio_decoder_.reset(new AudioDecoder(cast_environment_, |
41 GetParam().num_channels, | 41 GetParam().num_channels, |
42 GetParam().sampling_rate, | 42 GetParam().sampling_rate, |
43 GetParam().codec)); | 43 GetParam().codec)); |
44 CHECK_EQ(STATUS_AUDIO_INITIALIZED, audio_decoder_->InitializationResult()); | 44 CHECK_EQ(STATUS_AUDIO_INITIALIZED, audio_decoder_->InitializationResult()); |
45 | 45 |
46 audio_bus_factory_.reset( | 46 audio_bus_factory_.reset( |
47 new TestAudioBusFactory(GetParam().num_channels, | 47 new TestAudioBusFactory(GetParam().num_channels, |
48 GetParam().sampling_rate, | 48 GetParam().sampling_rate, |
49 TestAudioBusFactory::kMiddleANoteFreq, | 49 TestAudioBusFactory::kMiddleANoteFreq, |
50 0.5f)); | 50 0.5f)); |
51 last_frame_id_ = 0; | 51 last_frame_id_ = 0; |
52 seen_a_decoded_frame_ = false; | 52 seen_a_decoded_frame_ = false; |
53 | 53 |
54 if (GetParam().codec == transport::CODEC_AUDIO_OPUS) { | 54 if (GetParam().codec == CODEC_AUDIO_OPUS) { |
55 opus_encoder_memory_.reset( | 55 opus_encoder_memory_.reset( |
56 new uint8[opus_encoder_get_size(GetParam().num_channels)]); | 56 new uint8[opus_encoder_get_size(GetParam().num_channels)]); |
57 OpusEncoder* const opus_encoder = | 57 OpusEncoder* const opus_encoder = |
58 reinterpret_cast<OpusEncoder*>(opus_encoder_memory_.get()); | 58 reinterpret_cast<OpusEncoder*>(opus_encoder_memory_.get()); |
59 CHECK_EQ(OPUS_OK, opus_encoder_init(opus_encoder, | 59 CHECK_EQ(OPUS_OK, opus_encoder_init(opus_encoder, |
60 GetParam().sampling_rate, | 60 GetParam().sampling_rate, |
61 GetParam().num_channels, | 61 GetParam().num_channels, |
62 OPUS_APPLICATION_AUDIO)); | 62 OPUS_APPLICATION_AUDIO)); |
63 CHECK_EQ(OPUS_OK, | 63 CHECK_EQ(OPUS_OK, |
64 opus_encoder_ctl(opus_encoder, OPUS_SET_BITRATE(OPUS_AUTO))); | 64 opus_encoder_ctl(opus_encoder, OPUS_SET_BITRATE(OPUS_AUTO))); |
65 } | 65 } |
66 | 66 |
67 total_audio_feed_in_ = base::TimeDelta(); | 67 total_audio_feed_in_ = base::TimeDelta(); |
68 total_audio_decoded_ = base::TimeDelta(); | 68 total_audio_decoded_ = base::TimeDelta(); |
69 } | 69 } |
70 | 70 |
71 // Called from the unit test thread to create another EncodedFrame and push it | 71 // Called from the unit test thread to create another EncodedFrame and push it |
72 // into the decoding pipeline. | 72 // into the decoding pipeline. |
73 void FeedMoreAudio(const base::TimeDelta& duration, | 73 void FeedMoreAudio(const base::TimeDelta& duration, |
74 int num_dropped_frames) { | 74 int num_dropped_frames) { |
75 // Prepare a simulated EncodedFrame to feed into the AudioDecoder. | 75 // Prepare a simulated EncodedFrame to feed into the AudioDecoder. |
76 scoped_ptr<transport::EncodedFrame> encoded_frame( | 76 scoped_ptr<EncodedFrame> encoded_frame( |
77 new transport::EncodedFrame()); | 77 new EncodedFrame()); |
78 encoded_frame->dependency = transport::EncodedFrame::KEY; | 78 encoded_frame->dependency = EncodedFrame::KEY; |
79 encoded_frame->frame_id = last_frame_id_ + 1 + num_dropped_frames; | 79 encoded_frame->frame_id = last_frame_id_ + 1 + num_dropped_frames; |
80 encoded_frame->referenced_frame_id = encoded_frame->frame_id; | 80 encoded_frame->referenced_frame_id = encoded_frame->frame_id; |
81 last_frame_id_ = encoded_frame->frame_id; | 81 last_frame_id_ = encoded_frame->frame_id; |
82 | 82 |
83 const scoped_ptr<AudioBus> audio_bus( | 83 const scoped_ptr<AudioBus> audio_bus( |
84 audio_bus_factory_->NextAudioBus(duration).Pass()); | 84 audio_bus_factory_->NextAudioBus(duration).Pass()); |
85 | 85 |
86 // Encode |audio_bus| into |encoded_frame->data|. | 86 // Encode |audio_bus| into |encoded_frame->data|. |
87 const int num_elements = audio_bus->channels() * audio_bus->frames(); | 87 const int num_elements = audio_bus->channels() * audio_bus->frames(); |
88 std::vector<int16> interleaved(num_elements); | 88 std::vector<int16> interleaved(num_elements); |
89 audio_bus->ToInterleaved( | 89 audio_bus->ToInterleaved( |
90 audio_bus->frames(), sizeof(int16), &interleaved.front()); | 90 audio_bus->frames(), sizeof(int16), &interleaved.front()); |
91 if (GetParam().codec == transport::CODEC_AUDIO_PCM16) { | 91 if (GetParam().codec == CODEC_AUDIO_PCM16) { |
92 encoded_frame->data.resize(num_elements * sizeof(int16)); | 92 encoded_frame->data.resize(num_elements * sizeof(int16)); |
93 int16* const pcm_data = | 93 int16* const pcm_data = |
94 reinterpret_cast<int16*>(encoded_frame->mutable_bytes()); | 94 reinterpret_cast<int16*>(encoded_frame->mutable_bytes()); |
95 for (size_t i = 0; i < interleaved.size(); ++i) | 95 for (size_t i = 0; i < interleaved.size(); ++i) |
96 pcm_data[i] = static_cast<int16>(base::HostToNet16(interleaved[i])); | 96 pcm_data[i] = static_cast<int16>(base::HostToNet16(interleaved[i])); |
97 } else if (GetParam().codec == transport::CODEC_AUDIO_OPUS) { | 97 } else if (GetParam().codec == CODEC_AUDIO_OPUS) { |
98 OpusEncoder* const opus_encoder = | 98 OpusEncoder* const opus_encoder = |
99 reinterpret_cast<OpusEncoder*>(opus_encoder_memory_.get()); | 99 reinterpret_cast<OpusEncoder*>(opus_encoder_memory_.get()); |
100 const int kOpusEncodeBufferSize = 4000; | 100 const int kOpusEncodeBufferSize = 4000; |
101 encoded_frame->data.resize(kOpusEncodeBufferSize); | 101 encoded_frame->data.resize(kOpusEncodeBufferSize); |
102 const int payload_size = | 102 const int payload_size = |
103 opus_encode(opus_encoder, | 103 opus_encode(opus_encoder, |
104 &interleaved.front(), | 104 &interleaved.front(), |
105 audio_bus->frames(), | 105 audio_bus->frames(), |
106 encoded_frame->mutable_bytes(), | 106 encoded_frame->mutable_bytes(), |
107 encoded_frame->data.size()); | 107 encoded_frame->data.size()); |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
147 // A NULL |audio_bus| indicates a decode error, which we don't expect. | 147 // A NULL |audio_bus| indicates a decode error, which we don't expect. |
148 ASSERT_FALSE(!audio_bus); | 148 ASSERT_FALSE(!audio_bus); |
149 | 149 |
150 // Did the decoder detect whether frames were dropped? | 150 // Did the decoder detect whether frames were dropped? |
151 EXPECT_EQ(should_be_continuous, is_continuous); | 151 EXPECT_EQ(should_be_continuous, is_continuous); |
152 | 152 |
153 // Does the audio data seem to be intact? For Opus, we have to ignore the | 153 // Does the audio data seem to be intact? For Opus, we have to ignore the |
154 // first frame seen at the start (and immediately after dropped packet | 154 // first frame seen at the start (and immediately after dropped packet |
155 // recovery) because it introduces a tiny, significant delay. | 155 // recovery) because it introduces a tiny, significant delay. |
156 bool examine_signal = true; | 156 bool examine_signal = true; |
157 if (GetParam().codec == transport::CODEC_AUDIO_OPUS) { | 157 if (GetParam().codec == CODEC_AUDIO_OPUS) { |
158 examine_signal = seen_a_decoded_frame_ && should_be_continuous; | 158 examine_signal = seen_a_decoded_frame_ && should_be_continuous; |
159 seen_a_decoded_frame_ = true; | 159 seen_a_decoded_frame_ = true; |
160 } | 160 } |
161 if (examine_signal) { | 161 if (examine_signal) { |
162 for (int ch = 0; ch < audio_bus->channels(); ++ch) { | 162 for (int ch = 0; ch < audio_bus->channels(); ++ch) { |
163 EXPECT_NEAR( | 163 EXPECT_NEAR( |
164 TestAudioBusFactory::kMiddleANoteFreq * 2 * audio_bus->frames() / | 164 TestAudioBusFactory::kMiddleANoteFreq * 2 * audio_bus->frames() / |
165 GetParam().sampling_rate, | 165 GetParam().sampling_rate, |
166 CountZeroCrossings(audio_bus->channel(ch), audio_bus->frames()), | 166 CountZeroCrossings(audio_bus->channel(ch), audio_bus->frames()), |
167 1); | 167 1); |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
226 FeedMoreAudio(kTenMilliseconds, 0); | 226 FeedMoreAudio(kTenMilliseconds, 0); |
227 } | 227 } |
228 } | 228 } |
229 WaitForAllAudioToBeDecoded(); | 229 WaitForAllAudioToBeDecoded(); |
230 } | 230 } |
231 | 231 |
232 INSTANTIATE_TEST_CASE_P( | 232 INSTANTIATE_TEST_CASE_P( |
233 AudioDecoderTestScenarios, | 233 AudioDecoderTestScenarios, |
234 AudioDecoderTest, | 234 AudioDecoderTest, |
235 ::testing::Values( | 235 ::testing::Values( |
236 TestScenario(transport::CODEC_AUDIO_PCM16, 1, 8000), | 236 TestScenario(CODEC_AUDIO_PCM16, 1, 8000), |
237 TestScenario(transport::CODEC_AUDIO_PCM16, 2, 48000), | 237 TestScenario(CODEC_AUDIO_PCM16, 2, 48000), |
238 TestScenario(transport::CODEC_AUDIO_OPUS, 1, 8000), | 238 TestScenario(CODEC_AUDIO_OPUS, 1, 8000), |
239 TestScenario(transport::CODEC_AUDIO_OPUS, 2, 48000))); | 239 TestScenario(CODEC_AUDIO_OPUS, 2, 48000))); |
240 | 240 |
241 } // namespace cast | 241 } // namespace cast |
242 } // namespace media | 242 } // namespace media |
OLD | NEW |