OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/memory/weak_ptr.h" |
| 6 #include "base/run_loop.h" |
| 7 #include "base/threading/thread_task_runner_handle.h" |
| 8 #include "content/renderer/media/html_audio_element_capturer_source.h" |
| 9 #include "media/audio/null_audio_sink.h" |
| 10 #include "media/base/audio_parameters.h" |
| 11 #include "media/blink/webaudiosourceprovider_impl.h" |
| 12 #include "testing/gmock/include/gmock/gmock.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 |
| 15 using ::testing::_; |
| 16 using ::testing::AllOf; |
| 17 using ::testing::InSequence; |
| 18 using ::testing::Mock; |
| 19 using ::testing::Property; |
| 20 |
| 21 namespace content { |
| 22 |
| 23 // These constants represent the sampling rates and samples per buffer for input |
| 24 // and output as seen by HTMLAudioElementCapturerSource. For the purpose of the |
| 25 // test, both are chunks of 10ms, which happens to be the preferred one of |
| 26 // MediaStream Audio Tracks. |
| 27 static const int kNumChannelsForTest = 1; |
| 28 static const int kBufferDurationMs = 10; |
| 29 |
| 30 // This value mimics what is provided by WebAudioSourceProviderImpl. |
| 31 static const int kPlaybackAudioTrackSamplesPerBuffer = 1024; |
| 32 |
| 33 static const int kMediaStreamAudioTrackSampleRate = 48000; |
| 34 static const int kMediaStreamAudioTrackSamplesPerBuffer = |
| 35 kMediaStreamAudioTrackSampleRate * kBufferDurationMs / 1000; |
| 36 |
| 37 ACTION_P(RunClosure, closure) { |
| 38 closure.Run(); |
| 39 } |
| 40 |
| 41 class MockWebAudioSourceProvider : public media::WebAudioSourceProviderImpl { |
| 42 public: |
| 43 MockWebAudioSourceProvider() |
| 44 : WebAudioSourceProviderImpl( |
| 45 new media::NullAudioSink(base::ThreadTaskRunnerHandle::Get())) {} |
| 46 |
| 47 void SetCopyAudioCallback(const CopyAudioCB& callback) override { |
| 48 DVLOG(1) << __FUNCTION__; |
| 49 callback_ = callback; |
| 50 |
| 51 // We're super eager and send an AudioBus right after Start(). |
| 52 callback_.Run(media::AudioBus::Create(kNumChannelsForTest, |
| 53 kPlaybackAudioTrackSamplesPerBuffer), |
| 54 0 /* delay_milliseconds */, 44100 /* sample_rate */); |
| 55 } |
| 56 void ClearCopyAudioCallback() override { |
| 57 callback_.Reset(); |
| 58 } |
| 59 |
| 60 protected: |
| 61 ~MockWebAudioSourceProvider() override = default; |
| 62 |
| 63 private: |
| 64 CopyAudioCB callback_; |
| 65 }; |
| 66 |
| 67 class HTMLAudioElementCapturerSourceTest |
| 68 : public testing::Test, |
| 69 public media::AudioCapturerSource::CaptureCallback { |
| 70 public: |
| 71 HTMLAudioElementCapturerSourceTest() |
| 72 : audio_source_(new MockWebAudioSourceProvider()), |
| 73 html_audio_capturer_( |
| 74 new HtmlAudioElementCapturerSource(audio_source_->AsWeakPtr())) {} |
| 75 |
| 76 // media::AudioCapturerSource::CaptureCallback implementation |
| 77 MOCK_METHOD4(Capture, |
| 78 void(const media::AudioBus* audio_source, |
| 79 int audio_delay_milliseconds, |
| 80 double volume, |
| 81 bool key_pressed)); |
| 82 MOCK_METHOD1(OnCaptureError, void(const std::string& message)); |
| 83 |
| 84 protected: |
| 85 // We need some kind of message loop to allow |html_audio_capturer_| to |
| 86 // schedule capture events. |
| 87 const base::MessageLoopForUI message_loop_; |
| 88 |
| 89 scoped_refptr<MockWebAudioSourceProvider> audio_source_; |
| 90 scoped_refptr<HtmlAudioElementCapturerSource> html_audio_capturer_; |
| 91 }; |
| 92 |
| 93 // Constructs and destructs all objects, in particular |html_audio_capturer_| |
| 94 // and its inner object(s). This is a non trivial sequence. |
| 95 TEST_F(HTMLAudioElementCapturerSourceTest, ConstructAndDestruct) {} |
| 96 |
| 97 TEST_F(HTMLAudioElementCapturerSourceTest, InitializeStartAndStop) { |
| 98 InSequence s; |
| 99 |
| 100 base::RunLoop run_loop; |
| 101 base::Closure quit_closure = run_loop.QuitClosure(); |
| 102 |
| 103 // Similar to what WebRtcAudioCapturer::SetCapturerSourceInternal() does. |
| 104 const media::AudioParameters params( |
| 105 media::AudioParameters::AUDIO_PCM_LOW_LATENCY, |
| 106 media::GuessChannelLayout(kNumChannelsForTest), |
| 107 kMediaStreamAudioTrackSampleRate /* sample_rate */, |
| 108 16 /* bits_per_sample */, |
| 109 kMediaStreamAudioTrackSamplesPerBuffer /* frames_per_buffer */); |
| 110 html_audio_capturer_->Initialize(params, this, 0 /* session_id */); |
| 111 |
| 112 EXPECT_CALL( |
| 113 *this, |
| 114 Capture(AllOf(Property(&media::AudioBus::channels, kNumChannelsForTest), |
| 115 Property(&media::AudioBus::frames, |
| 116 kMediaStreamAudioTrackSamplesPerBuffer)), |
| 117 _, 1.0, false)) |
| 118 .Times(1); |
| 119 EXPECT_CALL( |
| 120 *this, |
| 121 Capture(AllOf(Property(&media::AudioBus::channels, kNumChannelsForTest), |
| 122 Property(&media::AudioBus::frames, |
| 123 kMediaStreamAudioTrackSamplesPerBuffer)), |
| 124 _, 1.0, false)) |
| 125 .Times(1) |
| 126 .WillOnce(RunClosure(quit_closure)); |
| 127 html_audio_capturer_->Start(); |
| 128 run_loop.Run(); |
| 129 |
| 130 html_audio_capturer_->Stop(); |
| 131 Mock::VerifyAndClearExpectations(this); |
| 132 } |
| 133 |
| 134 } // namespace content |
OLD | NEW |