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 <sstream> | 5 #include <sstream> |
| 6 #include <string> | 6 #include <string> |
| 7 | 7 |
| 8 #include "base/bind.h" | 8 #include "base/bind.h" |
| 9 #include "base/bind_helpers.h" | 9 #include "base/bind_helpers.h" |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "media/base/audio_bus.h" | 11 #include "media/base/audio_bus.h" |
| 12 #include "media/base/media.h" | 12 #include "media/base/media.h" |
| 13 #include "media/cast/audio_sender/audio_encoder.h" | 13 #include "media/cast/audio_sender/audio_encoder.h" |
| 14 #include "media/cast/cast_config.h" | 14 #include "media/cast/cast_config.h" |
| 15 #include "media/cast/cast_environment.h" | 15 #include "media/cast/cast_environment.h" |
| 16 #include "media/cast/test/audio_utility.h" | 16 #include "media/cast/test/audio_utility.h" |
| 17 #include "media/cast/test/fake_task_runner.h" | 17 #include "media/cast/test/fake_task_runner.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 19 | 19 |
| 20 namespace media { | 20 namespace media { |
| 21 namespace cast { | 21 namespace cast { |
| 22 | 22 |
| 23 static const int64 kStartMillisecond = GG_INT64_C(12345678900000); | 23 static const int64 kStartMillisecond = GG_INT64_C(12345678900000); |
| 24 | 24 |
| 25 namespace { | 25 namespace { |
| 26 | 26 |
| 27 class TestEncodedAudioFrameReceiver { | 27 class TestEncodedAudioFrameReceiver { |
| 28 public: | 28 public: |
| 29 explicit TestEncodedAudioFrameReceiver(transport::AudioCodec codec) : | 29 explicit TestEncodedAudioFrameReceiver(transport::AudioCodec codec) |
| 30 codec_(codec), frames_received_(0) {} | 30 : codec_(codec), frames_received_(0) {} |
| 31 virtual ~TestEncodedAudioFrameReceiver() {} | 31 virtual ~TestEncodedAudioFrameReceiver() {} |
| 32 | 32 |
| 33 int frames_received() const { | 33 int frames_received() const { return frames_received_; } |
| 34 return frames_received_; | |
| 35 } | |
| 36 | 34 |
| 37 void SetRecordedTimeLowerBound(const base::TimeTicks& t) { | 35 void SetRecordedTimeLowerBound(const base::TimeTicks& t) { lower_bound_ = t; } |
| 38 lower_bound_ = t; | |
| 39 } | |
| 40 | 36 |
| 41 void SetRecordedTimeUpperBound(const base::TimeTicks& t) { | 37 void SetRecordedTimeUpperBound(const base::TimeTicks& t) { upper_bound_ = t; } |
| 42 upper_bound_ = t; | |
| 43 } | |
| 44 | 38 |
| 45 void FrameEncoded(scoped_ptr<transport::EncodedAudioFrame> encoded_frame, | 39 void FrameEncoded(scoped_ptr<transport::EncodedAudioFrame> encoded_frame, |
| 46 const base::TimeTicks& recorded_time) { | 40 const base::TimeTicks& recorded_time) { |
| 47 EXPECT_EQ(codec_, encoded_frame->codec); | 41 EXPECT_EQ(codec_, encoded_frame->codec); |
| 48 EXPECT_EQ(static_cast<uint8>(frames_received_ & 0xff), | 42 EXPECT_EQ(static_cast<uint8>(frames_received_ & 0xff), |
| 49 encoded_frame->frame_id); | 43 encoded_frame->frame_id); |
| 50 EXPECT_LT(0, encoded_frame->samples); | 44 EXPECT_LT(0, encoded_frame->samples); |
| 51 EXPECT_TRUE(!encoded_frame->data.empty()); | 45 EXPECT_TRUE(!encoded_frame->data.empty()); |
| 52 | 46 |
| 53 EXPECT_LE(lower_bound_, recorded_time); | 47 EXPECT_LE(lower_bound_, recorded_time); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 90 public: | 84 public: |
| 91 AudioEncoderTest() { | 85 AudioEncoderTest() { |
| 92 InitializeMediaLibraryForTesting(); | 86 InitializeMediaLibraryForTesting(); |
| 93 testing_clock_ = new base::SimpleTestTickClock(); | 87 testing_clock_ = new base::SimpleTestTickClock(); |
| 94 testing_clock_->Advance( | 88 testing_clock_->Advance( |
| 95 base::TimeDelta::FromMilliseconds(kStartMillisecond)); | 89 base::TimeDelta::FromMilliseconds(kStartMillisecond)); |
| 96 } | 90 } |
| 97 | 91 |
| 98 virtual void SetUp() { | 92 virtual void SetUp() { |
| 99 task_runner_ = new test::FakeTaskRunner(testing_clock_); | 93 task_runner_ = new test::FakeTaskRunner(testing_clock_); |
| 100 cast_environment_ = new CastEnvironment( | 94 cast_environment_ = |
| 101 scoped_ptr<base::TickClock>(testing_clock_).Pass(), task_runner_, | 95 new CastEnvironment(scoped_ptr<base::TickClock>(testing_clock_).Pass(), |
| 102 task_runner_, task_runner_, task_runner_, task_runner_, task_runner_, | 96 task_runner_, |
| 103 GetDefaultCastSenderLoggingConfig()); | 97 task_runner_, |
| 98 task_runner_, | |
| 99 task_runner_, | |
| 100 task_runner_, | |
| 101 task_runner_, | |
| 102 GetDefaultCastSenderLoggingConfig()); | |
| 104 } | 103 } |
| 105 | 104 |
| 106 virtual ~AudioEncoderTest() {} | 105 virtual ~AudioEncoderTest() {} |
| 107 | 106 |
| 108 void RunTestForCodec(transport::AudioCodec codec) { | 107 void RunTestForCodec(transport::AudioCodec codec) { |
| 109 const TestScenario& scenario = GetParam(); | 108 const TestScenario& scenario = GetParam(); |
| 110 SCOPED_TRACE(::testing::Message() | 109 SCOPED_TRACE(::testing::Message() << "Durations: " << scenario.ToString()); |
| 111 << "Durations: " << scenario.ToString()); | |
| 112 | 110 |
| 113 CreateObjectsForCodec(codec); | 111 CreateObjectsForCodec(codec); |
| 114 | 112 |
| 115 receiver_->SetRecordedTimeLowerBound(testing_clock_->NowTicks()); | 113 receiver_->SetRecordedTimeLowerBound(testing_clock_->NowTicks()); |
| 116 for (size_t i = 0; i < scenario.num_durations; ++i) { | 114 for (size_t i = 0; i < scenario.num_durations; ++i) { |
| 117 const base::TimeDelta duration = | 115 const base::TimeDelta duration = |
| 118 base::TimeDelta::FromMilliseconds(scenario.durations_in_ms[i]); | 116 base::TimeDelta::FromMilliseconds(scenario.durations_in_ms[i]); |
| 119 receiver_->SetRecordedTimeUpperBound( | 117 receiver_->SetRecordedTimeUpperBound(testing_clock_->NowTicks() + |
| 120 testing_clock_->NowTicks() + duration); | 118 duration); |
| 121 | 119 |
| 122 const scoped_ptr<AudioBus> bus( | 120 const scoped_ptr<AudioBus> bus( |
| 123 audio_bus_factory_->NextAudioBus(duration)); | 121 audio_bus_factory_->NextAudioBus(duration)); |
| 124 | 122 |
| 125 const int last_count = release_callback_count_; | 123 const int last_count = release_callback_count_; |
| 126 audio_encoder_->InsertAudio( | 124 audio_encoder_->InsertAudio( |
| 127 bus.get(), testing_clock_->NowTicks(), | 125 bus.get(), |
| 126 testing_clock_->NowTicks(), | |
| 128 base::Bind(&AudioEncoderTest::IncrementReleaseCallbackCounter, | 127 base::Bind(&AudioEncoderTest::IncrementReleaseCallbackCounter, |
| 129 base::Unretained(this))); | 128 base::Unretained(this))); |
| 130 task_runner_->RunTasks(); | 129 task_runner_->RunTasks(); |
| 131 EXPECT_EQ(1, release_callback_count_ - last_count) | 130 EXPECT_EQ(1, release_callback_count_ - last_count) |
| 132 << "Release callback was not invoked once."; | 131 << "Release callback was not invoked once."; |
| 133 | 132 |
| 134 testing_clock_->Advance(duration); | 133 testing_clock_->Advance(duration); |
| 135 } | 134 } |
| 136 | 135 |
| 137 DVLOG(1) << "Received " << receiver_->frames_received() | 136 DVLOG(1) << "Received " << receiver_->frames_received() |
| 138 << " frames for this test run: " << scenario.ToString(); | 137 << " frames for this test run: " << scenario.ToString(); |
| 139 } | 138 } |
| 140 | 139 |
| 141 private: | 140 private: |
| 142 void CreateObjectsForCodec(transport::AudioCodec codec) { | 141 void CreateObjectsForCodec(transport::AudioCodec codec) { |
| 143 AudioSenderConfig audio_config; | 142 AudioSenderConfig audio_config; |
| 144 audio_config.codec = codec; | 143 audio_config.codec = codec; |
| 145 audio_config.use_external_encoder = false; | 144 audio_config.use_external_encoder = false; |
| 146 audio_config.frequency = kDefaultAudioSamplingRate; | 145 audio_config.frequency = kDefaultAudioSamplingRate; |
| 147 audio_config.channels = 2; | 146 audio_config.channels = 2; |
| 148 audio_config.bitrate = kDefaultAudioEncoderBitrate; | 147 audio_config.bitrate = kDefaultAudioEncoderBitrate; |
| 149 audio_config.rtp_payload_type = 127; | 148 audio_config.rtp_payload_type = 127; |
| 150 | 149 |
| 151 audio_bus_factory_.reset(new TestAudioBusFactory( | 150 audio_bus_factory_.reset( |
| 152 audio_config.channels, audio_config.frequency, | 151 new TestAudioBusFactory(audio_config.channels, |
| 153 TestAudioBusFactory::kMiddleANoteFreq, 0.5f)); | 152 audio_config.frequency, |
| 153 TestAudioBusFactory::kMiddleANoteFreq, | |
| 154 0.5f)); | |
| 154 | 155 |
| 155 receiver_.reset(new TestEncodedAudioFrameReceiver(codec)); | 156 receiver_.reset(new TestEncodedAudioFrameReceiver(codec)); |
| 156 | 157 |
| 157 audio_encoder_ = new AudioEncoder( | 158 audio_encoder_ = new AudioEncoder( |
| 158 cast_environment_, audio_config, | 159 cast_environment_, |
| 160 audio_config, | |
| 159 base::Bind(&TestEncodedAudioFrameReceiver::FrameEncoded, | 161 base::Bind(&TestEncodedAudioFrameReceiver::FrameEncoded, |
| 160 base::Unretained(receiver_.get()))); | 162 base::Unretained(receiver_.get()))); |
| 161 release_callback_count_ = 0; | 163 release_callback_count_ = 0; |
| 162 } | 164 } |
| 163 | 165 |
| 164 void IncrementReleaseCallbackCounter() { | 166 void IncrementReleaseCallbackCounter() { ++release_callback_count_; } |
| 165 ++release_callback_count_; | |
| 166 } | |
| 167 | 167 |
| 168 base::SimpleTestTickClock* testing_clock_; // Owned by CastEnvironment. | 168 base::SimpleTestTickClock* testing_clock_; // Owned by CastEnvironment. |
| 169 scoped_refptr<test::FakeTaskRunner> task_runner_; | 169 scoped_refptr<test::FakeTaskRunner> task_runner_; |
| 170 scoped_ptr<TestAudioBusFactory> audio_bus_factory_; | 170 scoped_ptr<TestAudioBusFactory> audio_bus_factory_; |
| 171 scoped_ptr<TestEncodedAudioFrameReceiver> receiver_; | 171 scoped_ptr<TestEncodedAudioFrameReceiver> receiver_; |
| 172 scoped_refptr<AudioEncoder> audio_encoder_; | 172 scoped_refptr<AudioEncoder> audio_encoder_; |
| 173 scoped_refptr<CastEnvironment> cast_environment_; | 173 scoped_refptr<CastEnvironment> cast_environment_; |
| 174 int release_callback_count_; | 174 int release_callback_count_; |
| 175 | 175 |
| 176 DISALLOW_COPY_AND_ASSIGN(AudioEncoderTest); | 176 DISALLOW_COPY_AND_ASSIGN(AudioEncoderTest); |
| 177 }; | 177 }; |
| 178 | 178 |
| 179 TEST_P(AudioEncoderTest, EncodeOpus) { | 179 TEST_P(AudioEncoderTest, EncodeOpus) { RunTestForCodec(transport::kOpus); } |
| 180 RunTestForCodec(transport::kOpus); | |
| 181 } | |
| 182 | 180 |
| 183 TEST_P(AudioEncoderTest, EncodePcm16) { | 181 TEST_P(AudioEncoderTest, EncodePcm16) { RunTestForCodec(transport::kPcm16); } |
| 184 RunTestForCodec(transport::kPcm16); | |
| 185 } | |
| 186 | 182 |
| 187 static const int64 kOneCall_3Millis[] = { 3 }; | 183 static const int64 kOneCall_3Millis[] = {3}; |
| 188 static const int64 kOneCall_10Millis[] = { 10 }; | 184 static const int64 kOneCall_10Millis[] = {10}; |
| 189 static const int64 kOneCall_13Millis[] = { 13 }; | 185 static const int64 kOneCall_13Millis[] = {13}; |
| 190 static const int64 kOneCall_20Millis[] = { 20 }; | 186 static const int64 kOneCall_20Millis[] = {20}; |
| 191 | 187 |
| 192 static const int64 kTwoCalls_3Millis[] = { 3, 3 }; | 188 static const int64 kTwoCalls_3Millis[] = {3, 3}; |
| 193 static const int64 kTwoCalls_10Millis[] = { 10, 10 }; | 189 static const int64 kTwoCalls_10Millis[] = {10, 10}; |
| 194 static const int64 kTwoCalls_Mixed1[] = { 3, 10 }; | 190 static const int64 kTwoCalls_Mixed1[] = {3, 10}; |
| 195 static const int64 kTwoCalls_Mixed2[] = { 10, 3 }; | 191 static const int64 kTwoCalls_Mixed2[] = {10, 3}; |
| 196 static const int64 kTwoCalls_Mixed3[] = { 3, 17 }; | 192 static const int64 kTwoCalls_Mixed3[] = {3, 17}; |
| 197 static const int64 kTwoCalls_Mixed4[] = { 17, 3 }; | 193 static const int64 kTwoCalls_Mixed4[] = {17, 3}; |
| 198 | 194 |
| 199 static const int64 kManyCalls_3Millis[] = | 195 static const int64 kManyCalls_3Millis[] = {3, 3, 3, 3, 3, 3, 3, 3, |
|
hubbe
2014/01/29 20:12:20
This block looked better before I think.
mikhal1
2014/01/29 20:53:43
I'd rather keep as is, so we won't get many change
| |
| 200 { 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3 }; | 196 3, 3, 3, 3, 3, 3, 3}; |
| 201 static const int64 kManyCalls_10Millis[] = | 197 static const int64 kManyCalls_10Millis[] = {10, 10, 10, 10, 10, 10, 10, 10, |
| 202 { 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10 }; | 198 10, 10, 10, 10, 10, 10, 10}; |
| 203 static const int64 kManyCalls_Mixed1[] = | 199 static const int64 kManyCalls_Mixed1[] = {3, 10, 3, 10, 3, 10, 3, 10, 3, |
| 204 { 3, 10, 3, 10, 3, 10, 3, 10, 3, 10, 3, 10, 3, 10, 3, 10, 3, 10 }; | 200 10, 3, 10, 3, 10, 3, 10, 3, 10}; |
| 205 static const int64 kManyCalls_Mixed2[] = | 201 static const int64 kManyCalls_Mixed2[] = {10, 3, 10, 3, 10, 3, 10, 3, 10, 3, |
| 206 { 10, 3, 10, 3, 10, 3, 10, 3, 10, 3, 10, 3, 10, 3, 10, 3, 10, 3, 10, 3 }; | 202 10, 3, 10, 3, 10, 3, 10, 3, 10, 3}; |
| 207 static const int64 kManyCalls_Mixed3[] = | 203 static const int64 kManyCalls_Mixed3[] = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, |
| 208 { 3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9, 3, 2, 3, 8, 4, 6, 2, 6, 4 }; | 204 9, 7, 9, 3, 2, 3, 8, 4, 6, 2, 6, 4}; |
| 209 static const int64 kManyCalls_Mixed4[] = | 205 static const int64 kManyCalls_Mixed4[] = {31, 4, 15, 9, 26, 53, 5, 8, 9, |
| 210 { 31, 4, 15, 9, 26, 53, 5, 8, 9, 7, 9, 32, 38, 4, 62, 64, 3 }; | 206 7, 9, 32, 38, 4, 62, 64, 3}; |
| 211 static const int64 kManyCalls_Mixed5[] = | 207 static const int64 kManyCalls_Mixed5[] = {3, 14, 15, 9, 26, 53, 58, 9, 7, |
| 212 { 3, 14, 15, 9, 26, 53, 58, 9, 7, 9, 3, 23, 8, 4, 6, 2, 6, 43 }; | 208 9, 3, 23, 8, 4, 6, 2, 6, 43}; |
| 213 | 209 |
| 214 INSTANTIATE_TEST_CASE_P( | 210 INSTANTIATE_TEST_CASE_P( |
| 215 AudioEncoderTestScenarios, AudioEncoderTest, | 211 AudioEncoderTestScenarios, |
| 212 AudioEncoderTest, | |
| 216 ::testing::Values( | 213 ::testing::Values( |
| 217 TestScenario(kOneCall_3Millis, arraysize(kOneCall_3Millis)), | 214 TestScenario(kOneCall_3Millis, arraysize(kOneCall_3Millis)), |
| 218 TestScenario(kOneCall_10Millis, arraysize(kOneCall_10Millis)), | 215 TestScenario(kOneCall_10Millis, arraysize(kOneCall_10Millis)), |
| 219 TestScenario(kOneCall_13Millis, arraysize(kOneCall_13Millis)), | 216 TestScenario(kOneCall_13Millis, arraysize(kOneCall_13Millis)), |
| 220 TestScenario(kOneCall_20Millis, arraysize(kOneCall_20Millis)), | 217 TestScenario(kOneCall_20Millis, arraysize(kOneCall_20Millis)), |
| 221 TestScenario(kTwoCalls_3Millis, arraysize(kTwoCalls_3Millis)), | 218 TestScenario(kTwoCalls_3Millis, arraysize(kTwoCalls_3Millis)), |
| 222 TestScenario(kTwoCalls_10Millis, arraysize(kTwoCalls_10Millis)), | 219 TestScenario(kTwoCalls_10Millis, arraysize(kTwoCalls_10Millis)), |
| 223 TestScenario(kTwoCalls_Mixed1, arraysize(kTwoCalls_Mixed1)), | 220 TestScenario(kTwoCalls_Mixed1, arraysize(kTwoCalls_Mixed1)), |
| 224 TestScenario(kTwoCalls_Mixed2, arraysize(kTwoCalls_Mixed2)), | 221 TestScenario(kTwoCalls_Mixed2, arraysize(kTwoCalls_Mixed2)), |
| 225 TestScenario(kTwoCalls_Mixed3, arraysize(kTwoCalls_Mixed3)), | 222 TestScenario(kTwoCalls_Mixed3, arraysize(kTwoCalls_Mixed3)), |
| 226 TestScenario(kTwoCalls_Mixed4, arraysize(kTwoCalls_Mixed4)), | 223 TestScenario(kTwoCalls_Mixed4, arraysize(kTwoCalls_Mixed4)), |
| 227 TestScenario(kManyCalls_3Millis, arraysize(kManyCalls_3Millis)), | 224 TestScenario(kManyCalls_3Millis, arraysize(kManyCalls_3Millis)), |
| 228 TestScenario(kManyCalls_10Millis, arraysize(kManyCalls_10Millis)), | 225 TestScenario(kManyCalls_10Millis, arraysize(kManyCalls_10Millis)), |
| 229 TestScenario(kManyCalls_Mixed1, arraysize(kManyCalls_Mixed1)), | 226 TestScenario(kManyCalls_Mixed1, arraysize(kManyCalls_Mixed1)), |
| 230 TestScenario(kManyCalls_Mixed2, arraysize(kManyCalls_Mixed2)), | 227 TestScenario(kManyCalls_Mixed2, arraysize(kManyCalls_Mixed2)), |
| 231 TestScenario(kManyCalls_Mixed3, arraysize(kManyCalls_Mixed3)), | 228 TestScenario(kManyCalls_Mixed3, arraysize(kManyCalls_Mixed3)), |
| 232 TestScenario(kManyCalls_Mixed4, arraysize(kManyCalls_Mixed4)), | 229 TestScenario(kManyCalls_Mixed4, arraysize(kManyCalls_Mixed4)), |
| 233 TestScenario(kManyCalls_Mixed5, arraysize(kManyCalls_Mixed5)))); | 230 TestScenario(kManyCalls_Mixed5, arraysize(kManyCalls_Mixed5)))); |
| 234 | 231 |
| 235 } // namespace cast | 232 } // namespace cast |
| 236 } // namespace media | 233 } // namespace media |
| OLD | NEW |