| 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(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 { |
| 34 return frames_received_; | 34 return frames_received_; |
| 35 } | 35 } |
| 36 | 36 |
| 37 void SetRecordedTimeLowerBound(const base::TimeTicks& t) { | 37 void SetRecordedTimeLowerBound(const base::TimeTicks& t) { |
| 38 lower_bound_ = t; | 38 lower_bound_ = t; |
| 39 } | 39 } |
| 40 | 40 |
| 41 void SetRecordedTimeUpperBound(const base::TimeTicks& t) { | 41 void SetRecordedTimeUpperBound(const base::TimeTicks& t) { |
| 42 upper_bound_ = t; | 42 upper_bound_ = t; |
| 43 } | 43 } |
| 44 | 44 |
| 45 void FrameEncoded(scoped_ptr<transport::EncodedAudioFrame> encoded_frame, | 45 void FrameEncoded(scoped_ptr<EncodedAudioFrame> encoded_frame, |
| 46 const base::TimeTicks& recorded_time) { | 46 const base::TimeTicks& recorded_time) { |
| 47 EXPECT_EQ(codec_, encoded_frame->codec); | 47 EXPECT_EQ(codec_, encoded_frame->codec); |
| 48 EXPECT_EQ(static_cast<uint8>(frames_received_ & 0xff), | 48 EXPECT_EQ(static_cast<uint8>(frames_received_ & 0xff), |
| 49 encoded_frame->frame_id); | 49 encoded_frame->frame_id); |
| 50 EXPECT_LT(0, encoded_frame->samples); | 50 EXPECT_LT(0, encoded_frame->samples); |
| 51 EXPECT_TRUE(!encoded_frame->data.empty()); | 51 EXPECT_TRUE(!encoded_frame->data.empty()); |
| 52 | 52 |
| 53 EXPECT_LE(lower_bound_, recorded_time); | 53 EXPECT_LE(lower_bound_, recorded_time); |
| 54 lower_bound_ = recorded_time; | 54 lower_bound_ = recorded_time; |
| 55 EXPECT_GT(upper_bound_, recorded_time); | 55 EXPECT_GT(upper_bound_, recorded_time); |
| 56 | 56 |
| 57 ++frames_received_; | 57 ++frames_received_; |
| 58 } | 58 } |
| 59 | 59 |
| 60 private: | 60 private: |
| 61 const transport::AudioCodec codec_; | 61 const AudioCodec codec_; |
| 62 int frames_received_; | 62 int frames_received_; |
| 63 base::TimeTicks lower_bound_; | 63 base::TimeTicks lower_bound_; |
| 64 base::TimeTicks upper_bound_; | 64 base::TimeTicks upper_bound_; |
| 65 | 65 |
| 66 DISALLOW_COPY_AND_ASSIGN(TestEncodedAudioFrameReceiver); | 66 DISALLOW_COPY_AND_ASSIGN(TestEncodedAudioFrameReceiver); |
| 67 }; | 67 }; |
| 68 | 68 |
| 69 struct TestScenario { | 69 struct TestScenario { |
| 70 const int64* durations_in_ms; | 70 const int64* durations_in_ms; |
| 71 size_t num_durations; | 71 size_t num_durations; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 90 public: | 90 public: |
| 91 AudioEncoderTest() { | 91 AudioEncoderTest() { |
| 92 InitializeMediaLibraryForTesting(); | 92 InitializeMediaLibraryForTesting(); |
| 93 testing_clock_.Advance( | 93 testing_clock_.Advance( |
| 94 base::TimeDelta::FromMilliseconds(kStartMillisecond)); | 94 base::TimeDelta::FromMilliseconds(kStartMillisecond)); |
| 95 } | 95 } |
| 96 | 96 |
| 97 virtual void SetUp() { | 97 virtual void SetUp() { |
| 98 task_runner_ = new test::FakeTaskRunner(&testing_clock_); | 98 task_runner_ = new test::FakeTaskRunner(&testing_clock_); |
| 99 cast_environment_ = new CastEnvironment(&testing_clock_, task_runner_, | 99 cast_environment_ = new CastEnvironment(&testing_clock_, task_runner_, |
| 100 task_runner_, task_runner_, task_runner_, task_runner_, task_runner_, | 100 task_runner_, task_runner_, task_runner_, task_runner_, |
| 101 GetDefaultCastLoggingConfig()); | 101 GetDefaultCastLoggingConfig()); |
| 102 } | 102 } |
| 103 | 103 |
| 104 virtual ~AudioEncoderTest() {} | 104 virtual ~AudioEncoderTest() {} |
| 105 | 105 |
| 106 void RunTestForCodec(transport::AudioCodec codec) { | 106 void RunTestForCodec(AudioCodec codec) { |
| 107 const TestScenario& scenario = GetParam(); | 107 const TestScenario& scenario = GetParam(); |
| 108 SCOPED_TRACE(::testing::Message() | 108 SCOPED_TRACE(::testing::Message() |
| 109 << "Durations: " << scenario.ToString()); | 109 << "Durations: " << scenario.ToString()); |
| 110 | 110 |
| 111 CreateObjectsForCodec(codec); | 111 CreateObjectsForCodec(codec); |
| 112 | 112 |
| 113 receiver_->SetRecordedTimeLowerBound(testing_clock_.NowTicks()); | 113 receiver_->SetRecordedTimeLowerBound(testing_clock_.NowTicks()); |
| 114 for (size_t i = 0; i < scenario.num_durations; ++i) { | 114 for (size_t i = 0; i < scenario.num_durations; ++i) { |
| 115 const base::TimeDelta duration = | 115 const base::TimeDelta duration = |
| 116 base::TimeDelta::FromMilliseconds(scenario.durations_in_ms[i]); | 116 base::TimeDelta::FromMilliseconds(scenario.durations_in_ms[i]); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 130 << "Release callback was not invoked once."; | 130 << "Release callback was not invoked once."; |
| 131 | 131 |
| 132 testing_clock_.Advance(duration); | 132 testing_clock_.Advance(duration); |
| 133 } | 133 } |
| 134 | 134 |
| 135 DVLOG(1) << "Received " << receiver_->frames_received() | 135 DVLOG(1) << "Received " << receiver_->frames_received() |
| 136 << " frames for this test run: " << scenario.ToString(); | 136 << " frames for this test run: " << scenario.ToString(); |
| 137 } | 137 } |
| 138 | 138 |
| 139 private: | 139 private: |
| 140 void CreateObjectsForCodec(transport::AudioCodec codec) { | 140 void CreateObjectsForCodec(AudioCodec codec) { |
| 141 AudioSenderConfig audio_config; | 141 AudioSenderConfig audio_config; |
| 142 audio_config.codec = codec; | 142 audio_config.codec = codec; |
| 143 audio_config.use_external_encoder = false; | 143 audio_config.use_external_encoder = false; |
| 144 audio_config.frequency = kDefaultAudioSamplingRate; | 144 audio_config.frequency = kDefaultAudioSamplingRate; |
| 145 audio_config.channels = 2; | 145 audio_config.channels = 2; |
| 146 audio_config.bitrate = kDefaultAudioEncoderBitrate; | 146 audio_config.bitrate = kDefaultAudioEncoderBitrate; |
| 147 audio_config.rtp_payload_type = 127; | 147 audio_config.rtp_payload_type = 127; |
| 148 | 148 |
| 149 audio_bus_factory_.reset(new TestAudioBusFactory( | 149 audio_bus_factory_.reset(new TestAudioBusFactory( |
| 150 audio_config.channels, audio_config.frequency, | 150 audio_config.channels, audio_config.frequency, |
| (...skipping 17 matching lines...) Expand all Loading... |
| 168 scoped_ptr<TestAudioBusFactory> audio_bus_factory_; | 168 scoped_ptr<TestAudioBusFactory> audio_bus_factory_; |
| 169 scoped_ptr<TestEncodedAudioFrameReceiver> receiver_; | 169 scoped_ptr<TestEncodedAudioFrameReceiver> receiver_; |
| 170 scoped_refptr<AudioEncoder> audio_encoder_; | 170 scoped_refptr<AudioEncoder> audio_encoder_; |
| 171 scoped_refptr<CastEnvironment> cast_environment_; | 171 scoped_refptr<CastEnvironment> cast_environment_; |
| 172 int release_callback_count_; | 172 int release_callback_count_; |
| 173 | 173 |
| 174 DISALLOW_COPY_AND_ASSIGN(AudioEncoderTest); | 174 DISALLOW_COPY_AND_ASSIGN(AudioEncoderTest); |
| 175 }; | 175 }; |
| 176 | 176 |
| 177 TEST_P(AudioEncoderTest, EncodeOpus) { | 177 TEST_P(AudioEncoderTest, EncodeOpus) { |
| 178 RunTestForCodec(transport::kOpus); | 178 RunTestForCodec(kOpus); |
| 179 } | 179 } |
| 180 | 180 |
| 181 TEST_P(AudioEncoderTest, EncodePcm16) { | 181 TEST_P(AudioEncoderTest, EncodePcm16) { |
| 182 RunTestForCodec(transport::kPcm16); | 182 RunTestForCodec(kPcm16); |
| 183 } | 183 } |
| 184 | 184 |
| 185 static const int64 kOneCall_3Millis[] = { 3 }; | 185 static const int64 kOneCall_3Millis[] = { 3 }; |
| 186 static const int64 kOneCall_10Millis[] = { 10 }; | 186 static const int64 kOneCall_10Millis[] = { 10 }; |
| 187 static const int64 kOneCall_13Millis[] = { 13 }; | 187 static const int64 kOneCall_13Millis[] = { 13 }; |
| 188 static const int64 kOneCall_20Millis[] = { 20 }; | 188 static const int64 kOneCall_20Millis[] = { 20 }; |
| 189 | 189 |
| 190 static const int64 kTwoCalls_3Millis[] = { 3, 3 }; | 190 static const int64 kTwoCalls_3Millis[] = { 3, 3 }; |
| 191 static const int64 kTwoCalls_10Millis[] = { 10, 10 }; | 191 static const int64 kTwoCalls_10Millis[] = { 10, 10 }; |
| 192 static const int64 kTwoCalls_Mixed1[] = { 3, 10 }; | 192 static const int64 kTwoCalls_Mixed1[] = { 3, 10 }; |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 225 TestScenario(kManyCalls_3Millis, arraysize(kManyCalls_3Millis)), | 225 TestScenario(kManyCalls_3Millis, arraysize(kManyCalls_3Millis)), |
| 226 TestScenario(kManyCalls_10Millis, arraysize(kManyCalls_10Millis)), | 226 TestScenario(kManyCalls_10Millis, arraysize(kManyCalls_10Millis)), |
| 227 TestScenario(kManyCalls_Mixed1, arraysize(kManyCalls_Mixed1)), | 227 TestScenario(kManyCalls_Mixed1, arraysize(kManyCalls_Mixed1)), |
| 228 TestScenario(kManyCalls_Mixed2, arraysize(kManyCalls_Mixed2)), | 228 TestScenario(kManyCalls_Mixed2, arraysize(kManyCalls_Mixed2)), |
| 229 TestScenario(kManyCalls_Mixed3, arraysize(kManyCalls_Mixed3)), | 229 TestScenario(kManyCalls_Mixed3, arraysize(kManyCalls_Mixed3)), |
| 230 TestScenario(kManyCalls_Mixed4, arraysize(kManyCalls_Mixed4)), | 230 TestScenario(kManyCalls_Mixed4, arraysize(kManyCalls_Mixed4)), |
| 231 TestScenario(kManyCalls_Mixed5, arraysize(kManyCalls_Mixed5)))); | 231 TestScenario(kManyCalls_Mixed5, arraysize(kManyCalls_Mixed5)))); |
| 232 | 232 |
| 233 } // namespace cast | 233 } // namespace cast |
| 234 } // namespace media | 234 } // namespace media |
| OLD | NEW |