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 "base/command_line.h" |
5 #include "base/logging.h" | 6 #include "base/logging.h" |
| 7 #include "content/public/common/content_switches.h" |
| 8 #include "content/renderer/media/mock_media_constraint_factory.h" |
6 #include "content/renderer/media/webrtc/webrtc_local_audio_track_adapter.h" | 9 #include "content/renderer/media/webrtc/webrtc_local_audio_track_adapter.h" |
7 #include "content/renderer/media/webrtc_audio_capturer.h" | 10 #include "content/renderer/media/webrtc_audio_capturer.h" |
8 #include "content/renderer/media/webrtc_local_audio_track.h" | 11 #include "content/renderer/media/webrtc_local_audio_track.h" |
9 #include "media/audio/audio_parameters.h" | 12 #include "media/audio/audio_parameters.h" |
10 #include "media/base/audio_bus.h" | 13 #include "media/base/audio_bus.h" |
11 #include "testing/gmock/include/gmock/gmock.h" | 14 #include "testing/gmock/include/gmock/gmock.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
13 #include "third_party/WebKit/public/platform/WebMediaConstraints.h" | 16 #include "third_party/WebKit/public/platform/WebMediaConstraints.h" |
14 | 17 |
15 using ::testing::_; | 18 using ::testing::_; |
(...skipping 15 matching lines...) Expand all Loading... |
31 MOCK_METHOD1(SetAutomaticGainControl, void(bool enable)); | 34 MOCK_METHOD1(SetAutomaticGainControl, void(bool enable)); |
32 | 35 |
33 protected: | 36 protected: |
34 virtual ~MockCapturerSource() {} | 37 virtual ~MockCapturerSource() {} |
35 }; | 38 }; |
36 | 39 |
37 class MockPeerConnectionAudioSink : public PeerConnectionAudioSink { | 40 class MockPeerConnectionAudioSink : public PeerConnectionAudioSink { |
38 public: | 41 public: |
39 MockPeerConnectionAudioSink() {} | 42 MockPeerConnectionAudioSink() {} |
40 ~MockPeerConnectionAudioSink() {} | 43 ~MockPeerConnectionAudioSink() {} |
41 MOCK_METHOD9(OnData, int(const int16* audio_data, | 44 virtual int OnData(const int16* audio_data, int sample_rate, |
42 int sample_rate, | 45 int number_of_channels, int number_of_frames, |
43 int number_of_channels, | 46 const std::vector<int>& channels, |
44 int number_of_frames, | 47 int audio_delay_milliseconds, int current_volume, |
45 const std::vector<int>& channels, | 48 bool need_audio_processing, bool key_pressed) OVERRIDE { |
46 int audio_delay_milliseconds, | 49 EXPECT_EQ(sample_rate, params_.sample_rate()); |
47 int current_volume, | 50 EXPECT_EQ(number_of_channels, params_.channels()); |
48 bool need_audio_processing, | 51 EXPECT_EQ(number_of_frames, params_.frames_per_buffer()); |
49 bool key_pressed)); | 52 OnDataCallback(audio_data, channels, audio_delay_milliseconds, |
50 MOCK_METHOD1(OnSetFormat, void(const media::AudioParameters& params)); | 53 current_volume, need_audio_processing, key_pressed); |
| 54 return 0; |
| 55 } |
| 56 MOCK_METHOD6(OnDataCallback, void(const int16* audio_data, |
| 57 const std::vector<int>& channels, |
| 58 int audio_delay_milliseconds, |
| 59 int current_volume, |
| 60 bool need_audio_processing, |
| 61 bool key_pressed)); |
| 62 virtual void OnSetFormat(const media::AudioParameters& params) OVERRIDE { |
| 63 params_ = params; |
| 64 FormatIsSet(); |
| 65 } |
| 66 MOCK_METHOD0(FormatIsSet, void()); |
| 67 |
| 68 private: |
| 69 media::AudioParameters params_; |
51 }; | 70 }; |
52 | 71 |
53 } // namespace | 72 } // namespace |
54 | 73 |
55 class WebRtcAudioCapturerTest : public testing::Test { | 74 class WebRtcAudioCapturerTest : public testing::Test { |
56 protected: | 75 protected: |
57 WebRtcAudioCapturerTest() | 76 WebRtcAudioCapturerTest() |
58 #if defined(OS_ANDROID) | 77 #if defined(OS_ANDROID) |
59 : params_(media::AudioParameters::AUDIO_PCM_LOW_LATENCY, | 78 : params_(media::AudioParameters::AUDIO_PCM_LOW_LATENCY, |
60 media::CHANNEL_LAYOUT_STEREO, 48000, 16, 960) { | 79 media::CHANNEL_LAYOUT_STEREO, 48000, 16, 960) { |
61 // Android works with a buffer size bigger than 20ms. | 80 // Android works with a buffer size bigger than 20ms. |
62 #else | 81 #else |
63 : params_(media::AudioParameters::AUDIO_PCM_LOW_LATENCY, | 82 : params_(media::AudioParameters::AUDIO_PCM_LOW_LATENCY, |
64 media::CHANNEL_LAYOUT_STEREO, 48000, 16, 128) { | 83 media::CHANNEL_LAYOUT_STEREO, 48000, 16, 128) { |
65 #endif | 84 #endif |
66 blink::WebMediaConstraints constraints; | 85 } |
| 86 |
| 87 void EnableAudioTrackProcessing() { |
| 88 CommandLine::ForCurrentProcess()->AppendSwitch( |
| 89 switches::kEnableAudioTrackProcessing); |
| 90 } |
| 91 |
| 92 void VerifyAudioParams(const blink::WebMediaConstraints& constraints, |
| 93 bool need_audio_processing) { |
67 capturer_ = WebRtcAudioCapturer::CreateCapturer( | 94 capturer_ = WebRtcAudioCapturer::CreateCapturer( |
68 -1, StreamDeviceInfo(MEDIA_DEVICE_AUDIO_CAPTURE, | 95 -1, StreamDeviceInfo(MEDIA_DEVICE_AUDIO_CAPTURE, |
69 "", "", params_.sample_rate(), | 96 "", "", params_.sample_rate(), |
70 params_.channel_layout(), | 97 params_.channel_layout(), |
71 params_.frames_per_buffer()), | 98 params_.frames_per_buffer()), |
72 constraints, | 99 constraints, NULL); |
73 NULL); | |
74 capturer_source_ = new MockCapturerSource(); | 100 capturer_source_ = new MockCapturerSource(); |
75 EXPECT_CALL(*capturer_source_.get(), Initialize(_, capturer_.get(), -1)); | 101 EXPECT_CALL(*capturer_source_.get(), Initialize(_, capturer_.get(), -1)); |
76 capturer_->SetCapturerSourceForTesting(capturer_source_, params_); | 102 capturer_->SetCapturerSourceForTesting(capturer_source_, params_); |
77 | 103 |
78 EXPECT_CALL(*capturer_source_.get(), SetAutomaticGainControl(true)); | 104 EXPECT_CALL(*capturer_source_.get(), SetAutomaticGainControl(true)); |
79 EXPECT_CALL(*capturer_source_.get(), Start()); | 105 EXPECT_CALL(*capturer_source_.get(), Start()); |
80 scoped_refptr<WebRtcLocalAudioTrackAdapter> adapter( | 106 scoped_refptr<WebRtcLocalAudioTrackAdapter> adapter( |
81 WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL)); | 107 WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL)); |
82 track_.reset(new WebRtcLocalAudioTrack(adapter, capturer_, NULL)); | 108 track_.reset(new WebRtcLocalAudioTrack(adapter, capturer_, NULL)); |
83 static_cast<WebRtcLocalAudioSourceProvider*>( | 109 static_cast<WebRtcLocalAudioSourceProvider*>( |
84 track_->audio_source_provider())->SetSinkParamsForTesting(params_); | 110 track_->audio_source_provider())->SetSinkParamsForTesting(params_); |
85 track_->Start(); | 111 track_->Start(); |
| 112 |
| 113 // Connect a mock sink to the track. |
| 114 scoped_ptr<MockPeerConnectionAudioSink> sink( |
| 115 new MockPeerConnectionAudioSink()); |
| 116 track_->AddSink(sink.get()); |
| 117 |
| 118 int delay_ms = 65; |
| 119 bool key_pressed = true; |
| 120 double volume = 0.9; |
| 121 |
| 122 // MaxVolume() in WebRtcAudioCapturer is hard-coded to return 255, we add |
| 123 // 0.5 to do the correct truncation like the production code does. |
| 124 int expected_volume_value = volume * capturer_->MaxVolume() + 0.5; |
| 125 scoped_ptr<media::AudioBus> audio_bus = media::AudioBus::Create(params_); |
| 126 audio_bus->Zero(); |
| 127 |
| 128 media::AudioCapturerSource::CaptureCallback* callback = |
| 129 static_cast<media::AudioCapturerSource::CaptureCallback*>(capturer_); |
| 130 |
| 131 // Verify the sink is getting the correct values. |
| 132 EXPECT_CALL(*sink, FormatIsSet()); |
| 133 EXPECT_CALL(*sink, |
| 134 OnDataCallback(_, _, delay_ms, expected_volume_value, |
| 135 need_audio_processing, key_pressed)); |
| 136 callback->Capture(audio_bus.get(), delay_ms, volume, key_pressed); |
| 137 |
| 138 // Verify the cached values in the capturer fits what we expect. |
| 139 base::TimeDelta cached_delay; |
| 140 int cached_volume = !expected_volume_value; |
| 141 bool cached_key_pressed = !key_pressed; |
| 142 capturer_->GetAudioProcessingParams(&cached_delay, &cached_volume, |
| 143 &cached_key_pressed); |
| 144 EXPECT_EQ(cached_delay.InMilliseconds(), delay_ms); |
| 145 EXPECT_EQ(cached_volume, expected_volume_value); |
| 146 EXPECT_EQ(cached_key_pressed, key_pressed); |
| 147 |
| 148 track_->RemoveSink(sink.get()); |
| 149 EXPECT_CALL(*capturer_source_.get(), Stop()); |
| 150 capturer_->Stop(); |
86 } | 151 } |
87 | 152 |
88 media::AudioParameters params_; | 153 media::AudioParameters params_; |
89 scoped_refptr<MockCapturerSource> capturer_source_; | 154 scoped_refptr<MockCapturerSource> capturer_source_; |
90 scoped_refptr<WebRtcAudioCapturer> capturer_; | 155 scoped_refptr<WebRtcAudioCapturer> capturer_; |
91 scoped_ptr<WebRtcLocalAudioTrack> track_; | 156 scoped_ptr<WebRtcLocalAudioTrack> track_; |
92 }; | 157 }; |
93 | 158 |
94 // Pass the delay value, volume and key_pressed info via capture callback, and | 159 // Pass the delay value, volume and key_pressed info via capture callback, and |
95 // those values should be correctly stored and passed to the track. | 160 // those values should be correctly stored and passed to the track. |
96 TEST_F(WebRtcAudioCapturerTest, VerifyAudioParams) { | 161 TEST_F(WebRtcAudioCapturerTest, VerifyAudioParams) { |
97 // Connect a mock sink to the track. | 162 // Use constraints with default settings. |
98 scoped_ptr<MockPeerConnectionAudioSink> sink( | 163 blink::WebMediaConstraints constraints; |
99 new MockPeerConnectionAudioSink()); | 164 VerifyAudioParams(constraints, true); |
100 track_->AddSink(sink.get()); | 165 } |
101 | 166 |
102 int delay_ms = 65; | 167 TEST_F(WebRtcAudioCapturerTest, VerifyAudioParamsWithAudioProcessing) { |
103 bool key_pressed = true; | 168 EnableAudioTrackProcessing(); |
104 double volume = 0.9; | 169 // Turn off the default constraints to verify that the sink will get packets |
105 // MaxVolume() in WebRtcAudioCapturer is hard-coded to return 255, we add 0.5 | 170 // with a buffer size smaller than 10ms. |
106 // to do the correct truncation as how the production code does. | 171 MockMediaConstraintFactory constraint_factory; |
107 int expected_volume_value = volume * capturer_->MaxVolume() + 0.5; | 172 constraint_factory.DisableDefaultAudioConstraints(); |
108 scoped_ptr<media::AudioBus> audio_bus = media::AudioBus::Create(params_); | 173 VerifyAudioParams(constraint_factory.CreateWebMediaConstraints(), false); |
109 audio_bus->Zero(); | |
110 #if defined(OS_ANDROID) | |
111 const int expected_buffer_size = params_.sample_rate() / 100; | |
112 #else | |
113 const int expected_buffer_size = params_.frames_per_buffer(); | |
114 #endif | |
115 bool expected_need_audio_processing = true; | |
116 media::AudioCapturerSource::CaptureCallback* callback = | |
117 static_cast<media::AudioCapturerSource::CaptureCallback*>(capturer_); | |
118 // Verify the sink is getting the correct values. | |
119 EXPECT_CALL(*sink, OnSetFormat(_)); | |
120 EXPECT_CALL(*sink, | |
121 OnData(_, params_.sample_rate(), params_.channels(), | |
122 expected_buffer_size, _, delay_ms, | |
123 expected_volume_value, expected_need_audio_processing, | |
124 key_pressed)).Times(AtLeast(1)); | |
125 callback->Capture(audio_bus.get(), delay_ms, volume, key_pressed); | |
126 | |
127 // Verify the cached values in the capturer fits what we expect. | |
128 base::TimeDelta cached_delay; | |
129 int cached_volume = !expected_volume_value; | |
130 bool cached_key_pressed = !key_pressed; | |
131 capturer_->GetAudioProcessingParams(&cached_delay, &cached_volume, | |
132 &cached_key_pressed); | |
133 EXPECT_EQ(cached_delay.InMilliseconds(), delay_ms); | |
134 EXPECT_EQ(cached_volume, expected_volume_value); | |
135 EXPECT_EQ(cached_key_pressed, key_pressed); | |
136 | |
137 track_->RemoveSink(sink.get()); | |
138 EXPECT_CALL(*capturer_source_.get(), Stop()); | |
139 capturer_->Stop(); | |
140 } | 174 } |
141 | 175 |
142 } // namespace content | 176 } // namespace content |
OLD | NEW |