| 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 <stdint.h> | 5 #include <stdint.h> |
| 6 | 6 |
| 7 #include <sstream> | 7 #include <sstream> |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/bind_helpers.h" | 11 #include "base/bind_helpers.h" |
| 12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "media/base/audio_bus.h" | 13 #include "media/base/audio_bus.h" |
| 14 #include "media/base/media.h" | 14 #include "media/base/media.h" |
| 15 #include "media/cast/audio_sender/audio_encoder.h" | |
| 16 #include "media/cast/cast_environment.h" | 15 #include "media/cast/cast_environment.h" |
| 16 #include "media/cast/sender/audio_encoder.h" |
| 17 #include "media/cast/test/fake_single_thread_task_runner.h" | 17 #include "media/cast/test/fake_single_thread_task_runner.h" |
| 18 #include "media/cast/test/utility/audio_utility.h" | 18 #include "media/cast/test/utility/audio_utility.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
| 20 | 20 |
| 21 namespace media { | 21 namespace media { |
| 22 namespace cast { | 22 namespace cast { |
| 23 | 23 |
| 24 static const int kNumChannels = 2; | 24 static const int kNumChannels = 2; |
| 25 | 25 |
| 26 namespace { | 26 namespace { |
| 27 | 27 |
| 28 class TestEncodedAudioFrameReceiver { | 28 class TestEncodedAudioFrameReceiver { |
| 29 public: | 29 public: |
| 30 explicit TestEncodedAudioFrameReceiver(transport::Codec codec) | 30 explicit TestEncodedAudioFrameReceiver(Codec codec) |
| 31 : codec_(codec), frames_received_(0), rtp_lower_bound_(0) {} | 31 : codec_(codec), frames_received_(0), rtp_lower_bound_(0) {} |
| 32 virtual ~TestEncodedAudioFrameReceiver() {} | 32 virtual ~TestEncodedAudioFrameReceiver() {} |
| 33 | 33 |
| 34 int frames_received() const { return frames_received_; } | 34 int frames_received() const { return frames_received_; } |
| 35 | 35 |
| 36 void SetCaptureTimeBounds(const base::TimeTicks& lower_bound, | 36 void SetCaptureTimeBounds(const base::TimeTicks& lower_bound, |
| 37 const base::TimeTicks& upper_bound) { | 37 const base::TimeTicks& upper_bound) { |
| 38 lower_bound_ = lower_bound; | 38 lower_bound_ = lower_bound; |
| 39 upper_bound_ = upper_bound; | 39 upper_bound_ = upper_bound; |
| 40 } | 40 } |
| 41 | 41 |
| 42 void FrameEncoded(scoped_ptr<transport::EncodedFrame> encoded_frame) { | 42 void FrameEncoded(scoped_ptr<EncodedFrame> encoded_frame) { |
| 43 EXPECT_EQ(encoded_frame->dependency, transport::EncodedFrame::KEY); | 43 EXPECT_EQ(encoded_frame->dependency, EncodedFrame::KEY); |
| 44 EXPECT_EQ(static_cast<uint8>(frames_received_ & 0xff), | 44 EXPECT_EQ(static_cast<uint8>(frames_received_ & 0xff), |
| 45 encoded_frame->frame_id); | 45 encoded_frame->frame_id); |
| 46 EXPECT_EQ(encoded_frame->frame_id, encoded_frame->referenced_frame_id); | 46 EXPECT_EQ(encoded_frame->frame_id, encoded_frame->referenced_frame_id); |
| 47 // RTP timestamps should be monotonically increasing and integer multiples | 47 // RTP timestamps should be monotonically increasing and integer multiples |
| 48 // of the fixed frame size. | 48 // of the fixed frame size. |
| 49 EXPECT_LE(rtp_lower_bound_, encoded_frame->rtp_timestamp); | 49 EXPECT_LE(rtp_lower_bound_, encoded_frame->rtp_timestamp); |
| 50 rtp_lower_bound_ = encoded_frame->rtp_timestamp; | 50 rtp_lower_bound_ = encoded_frame->rtp_timestamp; |
| 51 // Note: In audio_encoder.cc, 100 is the fixed audio frame rate. | 51 // Note: In audio_encoder.cc, 100 is the fixed audio frame rate. |
| 52 const int kSamplesPerFrame = kDefaultAudioSamplingRate / 100; | 52 const int kSamplesPerFrame = kDefaultAudioSamplingRate / 100; |
| 53 EXPECT_EQ(0u, encoded_frame->rtp_timestamp % kSamplesPerFrame); | 53 EXPECT_EQ(0u, encoded_frame->rtp_timestamp % kSamplesPerFrame); |
| 54 EXPECT_TRUE(!encoded_frame->data.empty()); | 54 EXPECT_TRUE(!encoded_frame->data.empty()); |
| 55 | 55 |
| 56 EXPECT_LE(lower_bound_, encoded_frame->reference_time); | 56 EXPECT_LE(lower_bound_, encoded_frame->reference_time); |
| 57 lower_bound_ = encoded_frame->reference_time; | 57 lower_bound_ = encoded_frame->reference_time; |
| 58 EXPECT_GT(upper_bound_, encoded_frame->reference_time); | 58 EXPECT_GT(upper_bound_, encoded_frame->reference_time); |
| 59 | 59 |
| 60 ++frames_received_; | 60 ++frames_received_; |
| 61 } | 61 } |
| 62 | 62 |
| 63 private: | 63 private: |
| 64 const transport::Codec codec_; | 64 const Codec codec_; |
| 65 int frames_received_; | 65 int frames_received_; |
| 66 uint32 rtp_lower_bound_; | 66 uint32 rtp_lower_bound_; |
| 67 base::TimeTicks lower_bound_; | 67 base::TimeTicks lower_bound_; |
| 68 base::TimeTicks upper_bound_; | 68 base::TimeTicks upper_bound_; |
| 69 | 69 |
| 70 DISALLOW_COPY_AND_ASSIGN(TestEncodedAudioFrameReceiver); | 70 DISALLOW_COPY_AND_ASSIGN(TestEncodedAudioFrameReceiver); |
| 71 }; | 71 }; |
| 72 | 72 |
| 73 struct TestScenario { | 73 struct TestScenario { |
| 74 const int64* durations_in_ms; | 74 const int64* durations_in_ms; |
| (...skipping 27 matching lines...) Expand all Loading... |
| 102 task_runner_ = new test::FakeSingleThreadTaskRunner(testing_clock_); | 102 task_runner_ = new test::FakeSingleThreadTaskRunner(testing_clock_); |
| 103 cast_environment_ = | 103 cast_environment_ = |
| 104 new CastEnvironment(scoped_ptr<base::TickClock>(testing_clock_).Pass(), | 104 new CastEnvironment(scoped_ptr<base::TickClock>(testing_clock_).Pass(), |
| 105 task_runner_, | 105 task_runner_, |
| 106 task_runner_, | 106 task_runner_, |
| 107 task_runner_); | 107 task_runner_); |
| 108 } | 108 } |
| 109 | 109 |
| 110 virtual ~AudioEncoderTest() {} | 110 virtual ~AudioEncoderTest() {} |
| 111 | 111 |
| 112 void RunTestForCodec(transport::Codec codec) { | 112 void RunTestForCodec(Codec codec) { |
| 113 const TestScenario& scenario = GetParam(); | 113 const TestScenario& scenario = GetParam(); |
| 114 SCOPED_TRACE(::testing::Message() << "Durations: " << scenario.ToString()); | 114 SCOPED_TRACE(::testing::Message() << "Durations: " << scenario.ToString()); |
| 115 | 115 |
| 116 CreateObjectsForCodec(codec); | 116 CreateObjectsForCodec(codec); |
| 117 | 117 |
| 118 // Note: In audio_encoder.cc, 10 ms is the fixed frame duration. | 118 // Note: In audio_encoder.cc, 10 ms is the fixed frame duration. |
| 119 const base::TimeDelta frame_duration = | 119 const base::TimeDelta frame_duration = |
| 120 base::TimeDelta::FromMilliseconds(10); | 120 base::TimeDelta::FromMilliseconds(10); |
| 121 | 121 |
| 122 for (size_t i = 0; i < scenario.num_durations; ++i) { | 122 for (size_t i = 0; i < scenario.num_durations; ++i) { |
| (...skipping 12 matching lines...) Expand all Loading... |
| 135 task_runner_->RunTasks(); | 135 task_runner_->RunTasks(); |
| 136 testing_clock_->Advance(duration); | 136 testing_clock_->Advance(duration); |
| 137 } | 137 } |
| 138 } | 138 } |
| 139 | 139 |
| 140 DVLOG(1) << "Received " << receiver_->frames_received() | 140 DVLOG(1) << "Received " << receiver_->frames_received() |
| 141 << " frames for this test run: " << scenario.ToString(); | 141 << " frames for this test run: " << scenario.ToString(); |
| 142 } | 142 } |
| 143 | 143 |
| 144 private: | 144 private: |
| 145 void CreateObjectsForCodec(transport::Codec codec) { | 145 void CreateObjectsForCodec(Codec codec) { |
| 146 audio_bus_factory_.reset( | 146 audio_bus_factory_.reset( |
| 147 new TestAudioBusFactory(kNumChannels, | 147 new TestAudioBusFactory(kNumChannels, |
| 148 kDefaultAudioSamplingRate, | 148 kDefaultAudioSamplingRate, |
| 149 TestAudioBusFactory::kMiddleANoteFreq, | 149 TestAudioBusFactory::kMiddleANoteFreq, |
| 150 0.5f)); | 150 0.5f)); |
| 151 | 151 |
| 152 receiver_.reset(new TestEncodedAudioFrameReceiver(codec)); | 152 receiver_.reset(new TestEncodedAudioFrameReceiver(codec)); |
| 153 | 153 |
| 154 audio_encoder_.reset(new AudioEncoder( | 154 audio_encoder_.reset(new AudioEncoder( |
| 155 cast_environment_, | 155 cast_environment_, |
| 156 kNumChannels, | 156 kNumChannels, |
| 157 kDefaultAudioSamplingRate, | 157 kDefaultAudioSamplingRate, |
| 158 kDefaultAudioEncoderBitrate, | 158 kDefaultAudioEncoderBitrate, |
| 159 codec, | 159 codec, |
| 160 base::Bind(&TestEncodedAudioFrameReceiver::FrameEncoded, | 160 base::Bind(&TestEncodedAudioFrameReceiver::FrameEncoded, |
| 161 base::Unretained(receiver_.get())))); | 161 base::Unretained(receiver_.get())))); |
| 162 } | 162 } |
| 163 | 163 |
| 164 base::SimpleTestTickClock* testing_clock_; // Owned by CastEnvironment. | 164 base::SimpleTestTickClock* testing_clock_; // Owned by CastEnvironment. |
| 165 scoped_refptr<test::FakeSingleThreadTaskRunner> task_runner_; | 165 scoped_refptr<test::FakeSingleThreadTaskRunner> task_runner_; |
| 166 scoped_ptr<TestAudioBusFactory> audio_bus_factory_; | 166 scoped_ptr<TestAudioBusFactory> audio_bus_factory_; |
| 167 scoped_ptr<TestEncodedAudioFrameReceiver> receiver_; | 167 scoped_ptr<TestEncodedAudioFrameReceiver> receiver_; |
| 168 scoped_ptr<AudioEncoder> audio_encoder_; | 168 scoped_ptr<AudioEncoder> audio_encoder_; |
| 169 scoped_refptr<CastEnvironment> cast_environment_; | 169 scoped_refptr<CastEnvironment> cast_environment_; |
| 170 | 170 |
| 171 DISALLOW_COPY_AND_ASSIGN(AudioEncoderTest); | 171 DISALLOW_COPY_AND_ASSIGN(AudioEncoderTest); |
| 172 }; | 172 }; |
| 173 | 173 |
| 174 TEST_P(AudioEncoderTest, EncodeOpus) { | 174 TEST_P(AudioEncoderTest, EncodeOpus) { |
| 175 RunTestForCodec(transport::CODEC_AUDIO_OPUS); | 175 RunTestForCodec(CODEC_AUDIO_OPUS); |
| 176 } | 176 } |
| 177 | 177 |
| 178 TEST_P(AudioEncoderTest, EncodePcm16) { | 178 TEST_P(AudioEncoderTest, EncodePcm16) { |
| 179 RunTestForCodec(transport::CODEC_AUDIO_PCM16); | 179 RunTestForCodec(CODEC_AUDIO_PCM16); |
| 180 } | 180 } |
| 181 | 181 |
| 182 static const int64 kOneCall_3Millis[] = {3}; | 182 static const int64 kOneCall_3Millis[] = {3}; |
| 183 static const int64 kOneCall_10Millis[] = {10}; | 183 static const int64 kOneCall_10Millis[] = {10}; |
| 184 static const int64 kOneCall_13Millis[] = {13}; | 184 static const int64 kOneCall_13Millis[] = {13}; |
| 185 static const int64 kOneCall_20Millis[] = {20}; | 185 static const int64 kOneCall_20Millis[] = {20}; |
| 186 | 186 |
| 187 static const int64 kTwoCalls_3Millis[] = {3, 3}; | 187 static const int64 kTwoCalls_3Millis[] = {3, 3}; |
| 188 static const int64 kTwoCalls_10Millis[] = {10, 10}; | 188 static const int64 kTwoCalls_10Millis[] = {10, 10}; |
| 189 static const int64 kTwoCalls_Mixed1[] = {3, 10}; | 189 static const int64 kTwoCalls_Mixed1[] = {3, 10}; |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 232 TestScenario(kManyCalls_Mixed2, arraysize(kManyCalls_Mixed2)), | 232 TestScenario(kManyCalls_Mixed2, arraysize(kManyCalls_Mixed2)), |
| 233 TestScenario(kManyCalls_Mixed3, arraysize(kManyCalls_Mixed3)), | 233 TestScenario(kManyCalls_Mixed3, arraysize(kManyCalls_Mixed3)), |
| 234 TestScenario(kManyCalls_Mixed4, arraysize(kManyCalls_Mixed4)), | 234 TestScenario(kManyCalls_Mixed4, arraysize(kManyCalls_Mixed4)), |
| 235 TestScenario(kManyCalls_Mixed5, arraysize(kManyCalls_Mixed5)), | 235 TestScenario(kManyCalls_Mixed5, arraysize(kManyCalls_Mixed5)), |
| 236 TestScenario(kOneBigUnderrun, arraysize(kOneBigUnderrun)), | 236 TestScenario(kOneBigUnderrun, arraysize(kOneBigUnderrun)), |
| 237 TestScenario(kTwoBigUnderruns, arraysize(kTwoBigUnderruns)), | 237 TestScenario(kTwoBigUnderruns, arraysize(kTwoBigUnderruns)), |
| 238 TestScenario(kMixedUnderruns, arraysize(kMixedUnderruns)))); | 238 TestScenario(kMixedUnderruns, arraysize(kMixedUnderruns)))); |
| 239 | 239 |
| 240 } // namespace cast | 240 } // namespace cast |
| 241 } // namespace media | 241 } // namespace media |
| OLD | NEW |