OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "chromecast/media/cma/backend/alsa/stream_mixer_alsa.h" | 5 #include "chromecast/media/cma/backend/alsa/stream_mixer_alsa.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <cmath> | 8 #include <cmath> |
9 #include <limits> | 9 #include <limits> |
10 #include <utility> | 10 #include <utility> |
11 | 11 |
12 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
13 #include "base/memory/scoped_vector.h" | 13 #include "base/memory/scoped_vector.h" |
14 #include "base/message_loop/message_loop.h" | 14 #include "base/message_loop/message_loop.h" |
15 #include "base/run_loop.h" | 15 #include "base/run_loop.h" |
16 #include "base/threading/thread_task_runner_handle.h" | 16 #include "base/threading/thread_task_runner_handle.h" |
17 #include "chromecast/media/cma/backend/alsa/mock_alsa_wrapper.h" | 17 #include "chromecast/media/cma/backend/alsa/mock_alsa_wrapper.h" |
18 #include "media/audio/audio_device_description.h" | |
19 #include "media/base/audio_bus.h" | 18 #include "media/base/audio_bus.h" |
20 #include "media/base/vector_math.h" | 19 #include "media/base/vector_math.h" |
21 #include "testing/gmock/include/gmock/gmock.h" | 20 #include "testing/gmock/include/gmock/gmock.h" |
22 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
23 | 22 |
24 using testing::_; | 23 using testing::_; |
25 | 24 |
26 namespace chromecast { | 25 namespace chromecast { |
27 namespace media { | 26 namespace media { |
28 | 27 |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
120 std::unique_ptr<::media::AudioBus> GetTestData(size_t index) { | 119 std::unique_ptr<::media::AudioBus> GetTestData(size_t index) { |
121 CHECK_LT(index, NUM_DATA_SETS); | 120 CHECK_LT(index, NUM_DATA_SETS); |
122 int frames = NUM_SAMPLES / kNumChannels; | 121 int frames = NUM_SAMPLES / kNumChannels; |
123 auto data = ::media::AudioBus::Create(kNumChannels, frames); | 122 auto data = ::media::AudioBus::Create(kNumChannels, frames); |
124 data->FromInterleaved(kTestData[index], frames, kBytesPerSample); | 123 data->FromInterleaved(kTestData[index], frames, kBytesPerSample); |
125 return data; | 124 return data; |
126 } | 125 } |
127 | 126 |
128 class MockInputQueue : public StreamMixerAlsa::InputQueue { | 127 class MockInputQueue : public StreamMixerAlsa::InputQueue { |
129 public: | 128 public: |
130 explicit MockInputQueue(int samples_per_second, | 129 explicit MockInputQueue(int samples_per_second) |
131 const std::string& device_id = | |
132 ::media::AudioDeviceDescription::kDefaultDeviceId) | |
133 : paused_(true), | 130 : paused_(true), |
134 samples_per_second_(samples_per_second), | 131 samples_per_second_(samples_per_second), |
135 max_read_size_(kTestMaxReadSize), | 132 max_read_size_(kTestMaxReadSize), |
136 multiplier_(1.0), | 133 multiplier_(1.0), |
137 primary_(true), | 134 primary_(true), |
138 deleting_(false), | 135 deleting_(false) { |
139 device_id_(device_id), | |
140 filter_group_(nullptr) { | |
141 ON_CALL(*this, GetResampledData(_, _)).WillByDefault( | 136 ON_CALL(*this, GetResampledData(_, _)).WillByDefault( |
142 testing::Invoke(this, &MockInputQueue::DoGetResampledData)); | 137 testing::Invoke(this, &MockInputQueue::DoGetResampledData)); |
143 ON_CALL(*this, VolumeScaleAccumulate(_, _, _, _)).WillByDefault( | 138 ON_CALL(*this, VolumeScaleAccumulate(_, _, _, _)).WillByDefault( |
144 testing::Invoke(this, &MockInputQueue::DoVolumeScaleAccumulate)); | 139 testing::Invoke(this, &MockInputQueue::DoVolumeScaleAccumulate)); |
145 ON_CALL(*this, PrepareToDelete(_)).WillByDefault( | 140 ON_CALL(*this, PrepareToDelete(_)).WillByDefault( |
146 testing::Invoke(this, &MockInputQueue::DoPrepareToDelete)); | 141 testing::Invoke(this, &MockInputQueue::DoPrepareToDelete)); |
147 } | 142 } |
148 ~MockInputQueue() override {} | 143 ~MockInputQueue() override {} |
149 | 144 |
150 bool paused() const { return paused_; } | 145 bool paused() const { return paused_; } |
151 | 146 |
152 // StreamMixerAlsa::InputQueue implementation: | 147 // StreamMixerAlsa::InputQueue implementation: |
153 int input_samples_per_second() const override { return samples_per_second_; } | 148 int input_samples_per_second() const override { return samples_per_second_; } |
154 bool primary() const override { return primary_; } | 149 bool primary() const override { return primary_; } |
155 bool IsDeleting() const override { return deleting_; } | 150 bool IsDeleting() const override { return deleting_; } |
156 MOCK_METHOD1(Initialize, | 151 MOCK_METHOD1(Initialize, |
157 void(const MediaPipelineBackendAlsa::RenderingDelay& | 152 void(const MediaPipelineBackendAlsa::RenderingDelay& |
158 mixer_rendering_delay)); | 153 mixer_rendering_delay)); |
159 std::string device_id() const override { return device_id_; } | |
160 void set_filter_group(FilterGroup* group) override { filter_group_ = group; } | |
161 FilterGroup* filter_group() override { return filter_group_; } | |
162 int MaxReadSize() override { return max_read_size_; } | 154 int MaxReadSize() override { return max_read_size_; } |
163 MOCK_METHOD2(GetResampledData, void(::media::AudioBus* dest, int frames)); | 155 MOCK_METHOD2(GetResampledData, void(::media::AudioBus* dest, int frames)); |
164 MOCK_METHOD4( | 156 MOCK_METHOD4( |
165 VolumeScaleAccumulate, | 157 VolumeScaleAccumulate, |
166 void(bool repeat_transition, const float* src, int frames, float* dest)); | 158 void(bool repeat_transition, const float* src, int frames, float* dest)); |
167 MOCK_METHOD0(OnSkipped, void()); | 159 MOCK_METHOD0(OnSkipped, void()); |
168 MOCK_METHOD1(AfterWriteFrames, | 160 MOCK_METHOD1(AfterWriteFrames, |
169 void(const MediaPipelineBackendAlsa::RenderingDelay& | 161 void(const MediaPipelineBackendAlsa::RenderingDelay& |
170 mixer_rendering_delay)); | 162 mixer_rendering_delay)); |
171 MOCK_METHOD1(SignalError, void(StreamMixerAlsaInput::MixerError error)); | 163 MOCK_METHOD1(SignalError, void(StreamMixerAlsaInput::MixerError error)); |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
215 deleting_ = true; | 207 deleting_ = true; |
216 delete_cb.Run(this); | 208 delete_cb.Run(this); |
217 } | 209 } |
218 | 210 |
219 bool paused_; | 211 bool paused_; |
220 int samples_per_second_; | 212 int samples_per_second_; |
221 int max_read_size_; | 213 int max_read_size_; |
222 float multiplier_; | 214 float multiplier_; |
223 bool primary_; | 215 bool primary_; |
224 bool deleting_; | 216 bool deleting_; |
225 const std::string device_id_; | |
226 FilterGroup* filter_group_; | |
227 | |
228 std::unique_ptr<::media::AudioBus> data_; | 217 std::unique_ptr<::media::AudioBus> data_; |
229 | 218 |
230 DISALLOW_COPY_AND_ASSIGN(MockInputQueue); | 219 DISALLOW_COPY_AND_ASSIGN(MockInputQueue); |
231 }; | 220 }; |
232 | 221 |
233 // Given |inputs|, returns mixed audio data according to the mixing method used | 222 // Given |inputs|, returns mixed audio data according to the mixing method used |
234 // by the mixer. | 223 // by the mixer. |
235 std::unique_ptr<::media::AudioBus> GetMixedAudioData( | 224 std::unique_ptr<::media::AudioBus> GetMixedAudioData( |
236 const std::vector<testing::StrictMock<MockInputQueue>*>& inputs) { | 225 const std::vector<testing::StrictMock<MockInputQueue>*>& inputs) { |
237 int read_size = std::numeric_limits<int>::max(); | 226 int read_size = std::numeric_limits<int>::max(); |
(...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
495 std::vector<testing::StrictMock<MockInputQueue>*> inputs; | 484 std::vector<testing::StrictMock<MockInputQueue>*> inputs; |
496 const int kNumInputs = 2; | 485 const int kNumInputs = 2; |
497 for (int i = 0; i < kNumInputs; ++i) { | 486 for (int i = 0; i < kNumInputs; ++i) { |
498 inputs.push_back( | 487 inputs.push_back( |
499 new testing::StrictMock<MockInputQueue>(kTestSamplesPerSecond)); | 488 new testing::StrictMock<MockInputQueue>(kTestSamplesPerSecond)); |
500 inputs.back()->SetPaused(false); | 489 inputs.back()->SetPaused(false); |
501 } | 490 } |
502 | 491 |
503 StreamMixerAlsa* mixer = StreamMixerAlsa::Get(); | 492 StreamMixerAlsa* mixer = StreamMixerAlsa::Get(); |
504 for (size_t i = 0; i < inputs.size(); ++i) { | 493 for (size_t i = 0; i < inputs.size(); ++i) { |
505 EXPECT_CALL(*inputs[i], Initialize(_)).Times(1); | |
506 mixer->AddInput(base::WrapUnique(inputs[i])); | |
507 } | |
508 | |
509 // Poll the inputs for data. | |
510 const int kNumFrames = 32; | |
511 for (size_t i = 0; i < inputs.size(); ++i) { | |
512 inputs[i]->SetData(GetTestData(i)); | |
513 EXPECT_CALL(*inputs[i], GetResampledData(_, kNumFrames)); | |
514 EXPECT_CALL(*inputs[i], VolumeScaleAccumulate(_, _, kNumFrames, _)) | |
515 .Times(kNumChannels); | |
516 EXPECT_CALL(*inputs[i], AfterWriteFrames(_)); | |
517 } | |
518 | |
519 EXPECT_CALL(*mock_alsa(), PcmWritei(_, _, kNumFrames)).Times(1); | |
520 mixer->WriteFramesForTest(); | |
521 | |
522 // Mix the inputs manually. | |
523 auto expected = GetMixedAudioData(inputs); | |
524 | |
525 // Get the actual stream rendered to ALSA, and compare it against the | |
526 // expected stream. The stream should match exactly. | |
527 auto actual = ::media::AudioBus::Create(kNumChannels, kNumFrames); | |
528 actual->FromInterleaved(&(mock_alsa()->data()[0]), kNumFrames, | |
529 kBytesPerSample); | |
530 CompareAudioData(*expected, *actual); | |
531 } | |
532 | |
533 TEST_F(StreamMixerAlsaTest, TwoUnscaledStreamsWithDifferentIdsMixProperly) { | |
534 // Create a group of input streams. | |
535 std::vector<testing::StrictMock<MockInputQueue>*> inputs; | |
536 inputs.push_back(new testing::StrictMock<MockInputQueue>( | |
537 kTestSamplesPerSecond, | |
538 ::media::AudioDeviceDescription::kDefaultDeviceId)); | |
539 inputs.back()->SetPaused(false); | |
540 inputs.push_back(new testing::StrictMock<MockInputQueue>( | |
541 kTestSamplesPerSecond, | |
542 ::media::AudioDeviceDescription::kCommunicationsDeviceId)); | |
543 inputs.back()->SetPaused(false); | |
544 | |
545 StreamMixerAlsa* mixer = StreamMixerAlsa::Get(); | |
546 for (size_t i = 0; i < inputs.size(); ++i) { | |
547 EXPECT_CALL(*inputs[i], Initialize(_)).Times(1); | 494 EXPECT_CALL(*inputs[i], Initialize(_)).Times(1); |
548 mixer->AddInput(base::WrapUnique(inputs[i])); | 495 mixer->AddInput(base::WrapUnique(inputs[i])); |
549 } | 496 } |
550 | 497 |
551 // Poll the inputs for data. | 498 // Poll the inputs for data. |
552 const int kNumFrames = 32; | 499 const int kNumFrames = 32; |
553 for (size_t i = 0; i < inputs.size(); ++i) { | 500 for (size_t i = 0; i < inputs.size(); ++i) { |
554 inputs[i]->SetData(GetTestData(i)); | 501 inputs[i]->SetData(GetTestData(i)); |
555 EXPECT_CALL(*inputs[i], GetResampledData(_, kNumFrames)); | 502 EXPECT_CALL(*inputs[i], GetResampledData(_, kNumFrames)); |
556 EXPECT_CALL(*inputs[i], VolumeScaleAccumulate(_, _, kNumFrames, _)) | 503 EXPECT_CALL(*inputs[i], VolumeScaleAccumulate(_, _, kNumFrames, _)) |
(...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
785 EXPECT_CALL(*inputs[1], VolumeScaleAccumulate(_, _, _, _)).Times(0); | 732 EXPECT_CALL(*inputs[1], VolumeScaleAccumulate(_, _, _, _)).Times(0); |
786 EXPECT_CALL(*inputs[1], OnSkipped()); | 733 EXPECT_CALL(*inputs[1], OnSkipped()); |
787 EXPECT_CALL(*inputs[1], AfterWriteFrames(_)); | 734 EXPECT_CALL(*inputs[1], AfterWriteFrames(_)); |
788 | 735 |
789 EXPECT_CALL(*mock_alsa(), PcmWritei(_, _, kNumFrames)).Times(1); | 736 EXPECT_CALL(*mock_alsa(), PcmWritei(_, _, kNumFrames)).Times(1); |
790 mixer->WriteFramesForTest(); | 737 mixer->WriteFramesForTest(); |
791 } | 738 } |
792 | 739 |
793 } // namespace media | 740 } // namespace media |
794 } // namespace chromecast | 741 } // namespace chromecast |
OLD | NEW |