| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "base/macros.h" | |
| 6 #include "base/synchronization/waitable_event.h" | |
| 7 #include "base/test/test_timeouts.h" | |
| 8 #include "build/build_config.h" | |
| 9 #include "content/public/renderer/media_stream_audio_sink.h" | |
| 10 #include "content/renderer/media/media_stream_audio_source.h" | |
| 11 #include "content/renderer/media/mock_media_constraint_factory.h" | |
| 12 #include "content/renderer/media/webrtc/webrtc_local_audio_track_adapter.h" | |
| 13 #include "content/renderer/media/webrtc_audio_capturer.h" | |
| 14 #include "content/renderer/media/webrtc_local_audio_track.h" | |
| 15 #include "media/audio/audio_parameters.h" | |
| 16 #include "media/base/audio_bus.h" | |
| 17 #include "media/base/audio_capturer_source.h" | |
| 18 #include "testing/gmock/include/gmock/gmock.h" | |
| 19 #include "testing/gtest/include/gtest/gtest.h" | |
| 20 #include "third_party/WebKit/public/platform/WebMediaConstraints.h" | |
| 21 #include "third_party/WebKit/public/web/WebHeap.h" | |
| 22 #include "third_party/webrtc/api/mediastreaminterface.h" | |
| 23 | |
| 24 using ::testing::_; | |
| 25 using ::testing::AnyNumber; | |
| 26 using ::testing::AtLeast; | |
| 27 using ::testing::Return; | |
| 28 | |
| 29 namespace content { | |
| 30 | |
| 31 namespace { | |
| 32 | |
| 33 ACTION_P(SignalEvent, event) { | |
| 34 event->Signal(); | |
| 35 } | |
| 36 | |
| 37 // A simple thread that we use to fake the audio thread which provides data to | |
| 38 // the |WebRtcAudioCapturer|. | |
| 39 class FakeAudioThread : public base::PlatformThread::Delegate { | |
| 40 public: | |
| 41 FakeAudioThread(WebRtcAudioCapturer* capturer, | |
| 42 const media::AudioParameters& params) | |
| 43 : capturer_(capturer), | |
| 44 thread_(), | |
| 45 closure_(false, false) { | |
| 46 DCHECK(capturer); | |
| 47 audio_bus_ = media::AudioBus::Create(params); | |
| 48 } | |
| 49 | |
| 50 ~FakeAudioThread() override { DCHECK(thread_.is_null()); } | |
| 51 | |
| 52 // base::PlatformThread::Delegate: | |
| 53 void ThreadMain() override { | |
| 54 while (true) { | |
| 55 if (closure_.IsSignaled()) | |
| 56 return; | |
| 57 | |
| 58 media::AudioCapturerSource::CaptureCallback* callback = | |
| 59 static_cast<media::AudioCapturerSource::CaptureCallback*>( | |
| 60 capturer_); | |
| 61 audio_bus_->Zero(); | |
| 62 callback->Capture(audio_bus_.get(), 0, 0, false); | |
| 63 | |
| 64 // Sleep 1ms to yield the resource for the main thread. | |
| 65 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(1)); | |
| 66 } | |
| 67 } | |
| 68 | |
| 69 void Start() { | |
| 70 base::PlatformThread::CreateWithPriority( | |
| 71 0, this, &thread_, base::ThreadPriority::REALTIME_AUDIO); | |
| 72 CHECK(!thread_.is_null()); | |
| 73 } | |
| 74 | |
| 75 void Stop() { | |
| 76 closure_.Signal(); | |
| 77 base::PlatformThread::Join(thread_); | |
| 78 thread_ = base::PlatformThreadHandle(); | |
| 79 } | |
| 80 | |
| 81 private: | |
| 82 scoped_ptr<media::AudioBus> audio_bus_; | |
| 83 WebRtcAudioCapturer* capturer_; | |
| 84 base::PlatformThreadHandle thread_; | |
| 85 base::WaitableEvent closure_; | |
| 86 DISALLOW_COPY_AND_ASSIGN(FakeAudioThread); | |
| 87 }; | |
| 88 | |
| 89 class MockCapturerSource : public media::AudioCapturerSource { | |
| 90 public: | |
| 91 explicit MockCapturerSource(WebRtcAudioCapturer* capturer) | |
| 92 : capturer_(capturer) {} | |
| 93 MOCK_METHOD3(OnInitialize, void(const media::AudioParameters& params, | |
| 94 CaptureCallback* callback, | |
| 95 int session_id)); | |
| 96 MOCK_METHOD0(OnStart, void()); | |
| 97 MOCK_METHOD0(OnStop, void()); | |
| 98 MOCK_METHOD1(SetVolume, void(double volume)); | |
| 99 MOCK_METHOD1(SetAutomaticGainControl, void(bool enable)); | |
| 100 | |
| 101 void Initialize(const media::AudioParameters& params, | |
| 102 CaptureCallback* callback, | |
| 103 int session_id) override { | |
| 104 DCHECK(params.IsValid()); | |
| 105 params_ = params; | |
| 106 OnInitialize(params, callback, session_id); | |
| 107 } | |
| 108 void Start() override { | |
| 109 audio_thread_.reset(new FakeAudioThread(capturer_, params_)); | |
| 110 audio_thread_->Start(); | |
| 111 OnStart(); | |
| 112 } | |
| 113 void Stop() override { | |
| 114 audio_thread_->Stop(); | |
| 115 audio_thread_.reset(); | |
| 116 OnStop(); | |
| 117 } | |
| 118 | |
| 119 protected: | |
| 120 ~MockCapturerSource() override {} | |
| 121 | |
| 122 private: | |
| 123 scoped_ptr<FakeAudioThread> audio_thread_; | |
| 124 WebRtcAudioCapturer* capturer_; | |
| 125 media::AudioParameters params_; | |
| 126 }; | |
| 127 | |
| 128 class MockMediaStreamAudioSink : public MediaStreamAudioSink { | |
| 129 public: | |
| 130 MockMediaStreamAudioSink() {} | |
| 131 ~MockMediaStreamAudioSink() {} | |
| 132 void OnData(const media::AudioBus& audio_bus, | |
| 133 base::TimeTicks estimated_capture_time) override { | |
| 134 EXPECT_EQ(params_.channels(), audio_bus.channels()); | |
| 135 EXPECT_EQ(params_.frames_per_buffer(), audio_bus.frames()); | |
| 136 EXPECT_FALSE(estimated_capture_time.is_null()); | |
| 137 CaptureData(); | |
| 138 } | |
| 139 MOCK_METHOD0(CaptureData, void()); | |
| 140 void OnSetFormat(const media::AudioParameters& params) { | |
| 141 params_ = params; | |
| 142 FormatIsSet(); | |
| 143 } | |
| 144 MOCK_METHOD0(FormatIsSet, void()); | |
| 145 | |
| 146 const media::AudioParameters& audio_params() const { return params_; } | |
| 147 | |
| 148 private: | |
| 149 media::AudioParameters params_; | |
| 150 }; | |
| 151 | |
| 152 } // namespace | |
| 153 | |
| 154 class WebRtcLocalAudioTrackTest : public ::testing::Test { | |
| 155 protected: | |
| 156 void SetUp() override { | |
| 157 params_.Reset(media::AudioParameters::AUDIO_PCM_LOW_LATENCY, | |
| 158 media::CHANNEL_LAYOUT_STEREO, 48000, 16, 480); | |
| 159 MockMediaConstraintFactory constraint_factory; | |
| 160 blink_source_.initialize("dummy", blink::WebMediaStreamSource::TypeAudio, | |
| 161 "dummy", | |
| 162 false /* remote */, true /* readonly */); | |
| 163 MediaStreamAudioSource* audio_source = new MediaStreamAudioSource(); | |
| 164 blink_source_.setExtraData(audio_source); | |
| 165 | |
| 166 StreamDeviceInfo device(MEDIA_DEVICE_AUDIO_CAPTURE, | |
| 167 std::string(), std::string()); | |
| 168 capturer_ = WebRtcAudioCapturer::CreateCapturer( | |
| 169 -1, device, constraint_factory.CreateWebMediaConstraints(), NULL, | |
| 170 audio_source); | |
| 171 audio_source->SetAudioCapturer(capturer_.get()); | |
| 172 capturer_source_ = new MockCapturerSource(capturer_.get()); | |
| 173 EXPECT_CALL(*capturer_source_.get(), OnInitialize(_, capturer_.get(), -1)) | |
| 174 .WillOnce(Return()); | |
| 175 EXPECT_CALL(*capturer_source_.get(), SetAutomaticGainControl(true)); | |
| 176 EXPECT_CALL(*capturer_source_.get(), OnStart()); | |
| 177 capturer_->SetCapturerSource(capturer_source_, params_); | |
| 178 } | |
| 179 | |
| 180 void TearDown() override { | |
| 181 blink_source_.reset(); | |
| 182 blink::WebHeap::collectAllGarbageForTesting(); | |
| 183 } | |
| 184 | |
| 185 media::AudioParameters params_; | |
| 186 blink::WebMediaStreamSource blink_source_; | |
| 187 scoped_refptr<MockCapturerSource> capturer_source_; | |
| 188 scoped_refptr<WebRtcAudioCapturer> capturer_; | |
| 189 }; | |
| 190 | |
| 191 // Creates a capturer and audio track, fakes its audio thread, and | |
| 192 // connect/disconnect the sink to the audio track on the fly, the sink should | |
| 193 // get data callback when the track is connected to the capturer but not when | |
| 194 // the track is disconnected from the capturer. | |
| 195 TEST_F(WebRtcLocalAudioTrackTest, ConnectAndDisconnectOneSink) { | |
| 196 scoped_refptr<WebRtcLocalAudioTrackAdapter> adapter( | |
| 197 WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL)); | |
| 198 scoped_ptr<WebRtcLocalAudioTrack> track( | |
| 199 new WebRtcLocalAudioTrack(adapter.get(), capturer_, NULL)); | |
| 200 track->Start(); | |
| 201 EXPECT_TRUE(track->GetAudioAdapter()->enabled()); | |
| 202 | |
| 203 scoped_ptr<MockMediaStreamAudioSink> sink(new MockMediaStreamAudioSink()); | |
| 204 base::WaitableEvent event(false, false); | |
| 205 EXPECT_CALL(*sink, FormatIsSet()); | |
| 206 EXPECT_CALL(*sink, | |
| 207 CaptureData()).Times(AtLeast(1)) | |
| 208 .WillRepeatedly(SignalEvent(&event)); | |
| 209 track->AddSink(sink.get()); | |
| 210 EXPECT_TRUE(event.TimedWait(TestTimeouts::tiny_timeout())); | |
| 211 track->RemoveSink(sink.get()); | |
| 212 | |
| 213 EXPECT_CALL(*capturer_source_.get(), OnStop()).WillOnce(Return()); | |
| 214 capturer_->Stop(); | |
| 215 } | |
| 216 | |
| 217 // The same setup as ConnectAndDisconnectOneSink, but enable and disable the | |
| 218 // audio track on the fly. When the audio track is disabled, there is no data | |
| 219 // callback to the sink; when the audio track is enabled, there comes data | |
| 220 // callback. | |
| 221 // TODO(xians): Enable this test after resolving the racing issue that TSAN | |
| 222 // reports on MediaStreamTrack::enabled(); | |
| 223 TEST_F(WebRtcLocalAudioTrackTest, DISABLED_DisableEnableAudioTrack) { | |
| 224 EXPECT_CALL(*capturer_source_.get(), SetAutomaticGainControl(true)); | |
| 225 EXPECT_CALL(*capturer_source_.get(), OnStart()); | |
| 226 scoped_refptr<WebRtcLocalAudioTrackAdapter> adapter( | |
| 227 WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL)); | |
| 228 scoped_ptr<WebRtcLocalAudioTrack> track( | |
| 229 new WebRtcLocalAudioTrack(adapter.get(), capturer_, NULL)); | |
| 230 track->Start(); | |
| 231 EXPECT_TRUE(track->GetAudioAdapter()->enabled()); | |
| 232 EXPECT_TRUE(track->GetAudioAdapter()->set_enabled(false)); | |
| 233 scoped_ptr<MockMediaStreamAudioSink> sink(new MockMediaStreamAudioSink()); | |
| 234 const media::AudioParameters params = capturer_->source_audio_parameters(); | |
| 235 base::WaitableEvent event(false, false); | |
| 236 EXPECT_CALL(*sink, FormatIsSet()).Times(1); | |
| 237 EXPECT_CALL(*sink, CaptureData()).Times(0); | |
| 238 EXPECT_EQ(sink->audio_params().frames_per_buffer(), | |
| 239 params.sample_rate() / 100); | |
| 240 track->AddSink(sink.get()); | |
| 241 EXPECT_FALSE(event.TimedWait(TestTimeouts::tiny_timeout())); | |
| 242 | |
| 243 event.Reset(); | |
| 244 EXPECT_CALL(*sink, CaptureData()).Times(AtLeast(1)) | |
| 245 .WillRepeatedly(SignalEvent(&event)); | |
| 246 EXPECT_TRUE(track->GetAudioAdapter()->set_enabled(true)); | |
| 247 EXPECT_TRUE(event.TimedWait(TestTimeouts::tiny_timeout())); | |
| 248 track->RemoveSink(sink.get()); | |
| 249 | |
| 250 EXPECT_CALL(*capturer_source_.get(), OnStop()).WillOnce(Return()); | |
| 251 capturer_->Stop(); | |
| 252 track.reset(); | |
| 253 } | |
| 254 | |
| 255 // Create multiple audio tracks and enable/disable them, verify that the audio | |
| 256 // callbacks appear/disappear. | |
| 257 // Flaky due to a data race, see http://crbug.com/295418 | |
| 258 TEST_F(WebRtcLocalAudioTrackTest, DISABLED_MultipleAudioTracks) { | |
| 259 scoped_refptr<WebRtcLocalAudioTrackAdapter> adapter_1( | |
| 260 WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL)); | |
| 261 scoped_ptr<WebRtcLocalAudioTrack> track_1( | |
| 262 new WebRtcLocalAudioTrack(adapter_1.get(), capturer_, NULL)); | |
| 263 track_1->Start(); | |
| 264 EXPECT_TRUE(track_1->GetAudioAdapter()->enabled()); | |
| 265 scoped_ptr<MockMediaStreamAudioSink> sink_1(new MockMediaStreamAudioSink()); | |
| 266 const media::AudioParameters params = capturer_->source_audio_parameters(); | |
| 267 base::WaitableEvent event_1(false, false); | |
| 268 EXPECT_CALL(*sink_1, FormatIsSet()).WillOnce(Return()); | |
| 269 EXPECT_CALL(*sink_1, CaptureData()).Times(AtLeast(1)) | |
| 270 .WillRepeatedly(SignalEvent(&event_1)); | |
| 271 EXPECT_EQ(sink_1->audio_params().frames_per_buffer(), | |
| 272 params.sample_rate() / 100); | |
| 273 track_1->AddSink(sink_1.get()); | |
| 274 EXPECT_TRUE(event_1.TimedWait(TestTimeouts::tiny_timeout())); | |
| 275 | |
| 276 scoped_refptr<WebRtcLocalAudioTrackAdapter> adapter_2( | |
| 277 WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL)); | |
| 278 scoped_ptr<WebRtcLocalAudioTrack> track_2( | |
| 279 new WebRtcLocalAudioTrack(adapter_2.get(), capturer_, NULL)); | |
| 280 track_2->Start(); | |
| 281 EXPECT_TRUE(track_2->GetAudioAdapter()->enabled()); | |
| 282 | |
| 283 // Verify both |sink_1| and |sink_2| get data. | |
| 284 event_1.Reset(); | |
| 285 base::WaitableEvent event_2(false, false); | |
| 286 | |
| 287 scoped_ptr<MockMediaStreamAudioSink> sink_2(new MockMediaStreamAudioSink()); | |
| 288 EXPECT_CALL(*sink_2, FormatIsSet()).WillOnce(Return()); | |
| 289 EXPECT_CALL(*sink_1, CaptureData()).Times(AtLeast(1)) | |
| 290 .WillRepeatedly(SignalEvent(&event_1)); | |
| 291 EXPECT_EQ(sink_1->audio_params().frames_per_buffer(), | |
| 292 params.sample_rate() / 100); | |
| 293 EXPECT_CALL(*sink_2, CaptureData()).Times(AtLeast(1)) | |
| 294 .WillRepeatedly(SignalEvent(&event_2)); | |
| 295 EXPECT_EQ(sink_2->audio_params().frames_per_buffer(), | |
| 296 params.sample_rate() / 100); | |
| 297 track_2->AddSink(sink_2.get()); | |
| 298 EXPECT_TRUE(event_1.TimedWait(TestTimeouts::tiny_timeout())); | |
| 299 EXPECT_TRUE(event_2.TimedWait(TestTimeouts::tiny_timeout())); | |
| 300 | |
| 301 track_1->RemoveSink(sink_1.get()); | |
| 302 track_1->Stop(); | |
| 303 track_1.reset(); | |
| 304 | |
| 305 EXPECT_CALL(*capturer_source_.get(), OnStop()).WillOnce(Return()); | |
| 306 track_2->RemoveSink(sink_2.get()); | |
| 307 track_2->Stop(); | |
| 308 track_2.reset(); | |
| 309 } | |
| 310 | |
| 311 | |
| 312 // Start one track and verify the capturer is correctly starting its source. | |
| 313 // And it should be fine to not to call Stop() explicitly. | |
| 314 TEST_F(WebRtcLocalAudioTrackTest, StartOneAudioTrack) { | |
| 315 scoped_refptr<WebRtcLocalAudioTrackAdapter> adapter( | |
| 316 WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL)); | |
| 317 scoped_ptr<WebRtcLocalAudioTrack> track( | |
| 318 new WebRtcLocalAudioTrack(adapter.get(), capturer_, NULL)); | |
| 319 track->Start(); | |
| 320 | |
| 321 // When the track goes away, it will automatically stop the | |
| 322 // |capturer_source_|. | |
| 323 EXPECT_CALL(*capturer_source_.get(), OnStop()); | |
| 324 track.reset(); | |
| 325 } | |
| 326 | |
| 327 // Start two tracks and verify the capturer is correctly starting its source. | |
| 328 // When the last track connected to the capturer is stopped, the source is | |
| 329 // stopped. | |
| 330 TEST_F(WebRtcLocalAudioTrackTest, StartTwoAudioTracks) { | |
| 331 scoped_refptr<WebRtcLocalAudioTrackAdapter> adapter1( | |
| 332 WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL)); | |
| 333 scoped_ptr<WebRtcLocalAudioTrack> track1( | |
| 334 new WebRtcLocalAudioTrack(adapter1.get(), capturer_, NULL)); | |
| 335 track1->Start(); | |
| 336 | |
| 337 scoped_refptr<WebRtcLocalAudioTrackAdapter> adapter2( | |
| 338 WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL)); | |
| 339 scoped_ptr<WebRtcLocalAudioTrack> track2( | |
| 340 new WebRtcLocalAudioTrack(adapter2.get(), capturer_, NULL)); | |
| 341 track2->Start(); | |
| 342 | |
| 343 track1->Stop(); | |
| 344 // When the last track is stopped, it will automatically stop the | |
| 345 // |capturer_source_|. | |
| 346 EXPECT_CALL(*capturer_source_.get(), OnStop()); | |
| 347 track2->Stop(); | |
| 348 } | |
| 349 | |
| 350 // Start/Stop tracks and verify the capturer is correctly starting/stopping | |
| 351 // its source. | |
| 352 TEST_F(WebRtcLocalAudioTrackTest, StartAndStopAudioTracks) { | |
| 353 base::WaitableEvent event(false, false); | |
| 354 scoped_refptr<WebRtcLocalAudioTrackAdapter> adapter_1( | |
| 355 WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL)); | |
| 356 scoped_ptr<WebRtcLocalAudioTrack> track_1( | |
| 357 new WebRtcLocalAudioTrack(adapter_1.get(), capturer_, NULL)); | |
| 358 track_1->Start(); | |
| 359 | |
| 360 // Verify the data flow by connecting the sink to |track_1|. | |
| 361 scoped_ptr<MockMediaStreamAudioSink> sink(new MockMediaStreamAudioSink()); | |
| 362 event.Reset(); | |
| 363 EXPECT_CALL(*sink, FormatIsSet()).WillOnce(SignalEvent(&event)); | |
| 364 EXPECT_CALL(*sink, CaptureData()) | |
| 365 .Times(AnyNumber()).WillRepeatedly(Return()); | |
| 366 track_1->AddSink(sink.get()); | |
| 367 EXPECT_TRUE(event.TimedWait(TestTimeouts::tiny_timeout())); | |
| 368 | |
| 369 // Start the second audio track will not start the |capturer_source_| | |
| 370 // since it has been started. | |
| 371 EXPECT_CALL(*capturer_source_.get(), OnStart()).Times(0); | |
| 372 scoped_refptr<WebRtcLocalAudioTrackAdapter> adapter_2( | |
| 373 WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL)); | |
| 374 scoped_ptr<WebRtcLocalAudioTrack> track_2( | |
| 375 new WebRtcLocalAudioTrack(adapter_2.get(), capturer_, NULL)); | |
| 376 track_2->Start(); | |
| 377 | |
| 378 // Stop the capturer will clear up the track lists in the capturer. | |
| 379 EXPECT_CALL(*capturer_source_.get(), OnStop()); | |
| 380 capturer_->Stop(); | |
| 381 | |
| 382 // Adding a new track to the capturer. | |
| 383 track_2->AddSink(sink.get()); | |
| 384 EXPECT_CALL(*sink, FormatIsSet()).Times(0); | |
| 385 | |
| 386 // Stop the capturer again will not trigger stopping the source of the | |
| 387 // capturer again.. | |
| 388 event.Reset(); | |
| 389 EXPECT_CALL(*capturer_source_.get(), OnStop()).Times(0); | |
| 390 capturer_->Stop(); | |
| 391 } | |
| 392 | |
| 393 // Create a new capturer with new source, connect it to a new audio track. | |
| 394 #if defined(THREAD_SANITIZER) | |
| 395 // Fails under TSan, see https://crbug.com/576634. | |
| 396 #define MAYBE_ConnectTracksToDifferentCapturers \ | |
| 397 DISABLED_ConnectTracksToDifferentCapturers | |
| 398 #else | |
| 399 #define MAYBE_ConnectTracksToDifferentCapturers \ | |
| 400 ConnectTracksToDifferentCapturers | |
| 401 #endif | |
| 402 TEST_F(WebRtcLocalAudioTrackTest, MAYBE_ConnectTracksToDifferentCapturers) { | |
| 403 // Setup the first audio track and start it. | |
| 404 scoped_refptr<WebRtcLocalAudioTrackAdapter> adapter_1( | |
| 405 WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL)); | |
| 406 scoped_ptr<WebRtcLocalAudioTrack> track_1( | |
| 407 new WebRtcLocalAudioTrack(adapter_1.get(), capturer_, NULL)); | |
| 408 track_1->Start(); | |
| 409 | |
| 410 // Verify the data flow by connecting the |sink_1| to |track_1|. | |
| 411 scoped_ptr<MockMediaStreamAudioSink> sink_1(new MockMediaStreamAudioSink()); | |
| 412 EXPECT_CALL(*sink_1.get(), CaptureData()) | |
| 413 .Times(AnyNumber()).WillRepeatedly(Return()); | |
| 414 EXPECT_CALL(*sink_1.get(), FormatIsSet()).Times(AnyNumber()); | |
| 415 track_1->AddSink(sink_1.get()); | |
| 416 | |
| 417 // Create a new capturer with new source with different audio format. | |
| 418 MockMediaConstraintFactory constraint_factory; | |
| 419 StreamDeviceInfo device(MEDIA_DEVICE_AUDIO_CAPTURE, | |
| 420 std::string(), std::string()); | |
| 421 scoped_refptr<WebRtcAudioCapturer> new_capturer( | |
| 422 WebRtcAudioCapturer::CreateCapturer( | |
| 423 -1, device, constraint_factory.CreateWebMediaConstraints(), NULL, | |
| 424 NULL)); | |
| 425 scoped_refptr<MockCapturerSource> new_source( | |
| 426 new MockCapturerSource(new_capturer.get())); | |
| 427 EXPECT_CALL(*new_source.get(), OnInitialize(_, new_capturer.get(), -1)); | |
| 428 EXPECT_CALL(*new_source.get(), SetAutomaticGainControl(true)); | |
| 429 EXPECT_CALL(*new_source.get(), OnStart()); | |
| 430 | |
| 431 media::AudioParameters new_param( | |
| 432 media::AudioParameters::AUDIO_PCM_LOW_LATENCY, | |
| 433 media::CHANNEL_LAYOUT_MONO, 44100, 16, 441); | |
| 434 new_capturer->SetCapturerSource(new_source, new_param); | |
| 435 | |
| 436 // Setup the second audio track, connect it to the new capturer and start it. | |
| 437 scoped_refptr<WebRtcLocalAudioTrackAdapter> adapter_2( | |
| 438 WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL)); | |
| 439 scoped_ptr<WebRtcLocalAudioTrack> track_2( | |
| 440 new WebRtcLocalAudioTrack(adapter_2.get(), new_capturer, NULL)); | |
| 441 track_2->Start(); | |
| 442 | |
| 443 // Verify the data flow by connecting the |sink_2| to |track_2|. | |
| 444 scoped_ptr<MockMediaStreamAudioSink> sink_2(new MockMediaStreamAudioSink()); | |
| 445 base::WaitableEvent event(false, false); | |
| 446 EXPECT_CALL(*sink_2, CaptureData()) | |
| 447 .Times(AnyNumber()).WillRepeatedly(Return()); | |
| 448 EXPECT_CALL(*sink_2, FormatIsSet()).WillOnce(SignalEvent(&event)); | |
| 449 track_2->AddSink(sink_2.get()); | |
| 450 EXPECT_TRUE(event.TimedWait(TestTimeouts::tiny_timeout())); | |
| 451 | |
| 452 // Stopping the new source will stop the second track. | |
| 453 event.Reset(); | |
| 454 EXPECT_CALL(*new_source.get(), OnStop()) | |
| 455 .Times(1).WillOnce(SignalEvent(&event)); | |
| 456 new_capturer->Stop(); | |
| 457 EXPECT_TRUE(event.TimedWait(TestTimeouts::tiny_timeout())); | |
| 458 | |
| 459 // Stop the capturer of the first audio track. | |
| 460 EXPECT_CALL(*capturer_source_.get(), OnStop()); | |
| 461 capturer_->Stop(); | |
| 462 } | |
| 463 | |
| 464 // Make sure a audio track can deliver packets with a buffer size smaller than | |
| 465 // 10ms when it is not connected with a peer connection. | |
| 466 TEST_F(WebRtcLocalAudioTrackTest, TrackWorkWithSmallBufferSize) { | |
| 467 // Setup a capturer which works with a buffer size smaller than 10ms. | |
| 468 media::AudioParameters params(media::AudioParameters::AUDIO_PCM_LOW_LATENCY, | |
| 469 media::CHANNEL_LAYOUT_STEREO, 48000, 16, 128); | |
| 470 | |
| 471 // Create a capturer with new source which works with the format above. | |
| 472 MockMediaConstraintFactory factory; | |
| 473 factory.DisableDefaultAudioConstraints(); | |
| 474 scoped_refptr<WebRtcAudioCapturer> capturer( | |
| 475 WebRtcAudioCapturer::CreateCapturer( | |
| 476 -1, StreamDeviceInfo(MEDIA_DEVICE_AUDIO_CAPTURE, "", "", | |
| 477 params.sample_rate(), params.channel_layout(), | |
| 478 params.frames_per_buffer()), | |
| 479 factory.CreateWebMediaConstraints(), NULL, NULL)); | |
| 480 scoped_refptr<MockCapturerSource> source( | |
| 481 new MockCapturerSource(capturer.get())); | |
| 482 EXPECT_CALL(*source.get(), OnInitialize(_, capturer.get(), -1)); | |
| 483 EXPECT_CALL(*source.get(), SetAutomaticGainControl(true)); | |
| 484 EXPECT_CALL(*source.get(), OnStart()); | |
| 485 capturer->SetCapturerSource(source, params); | |
| 486 | |
| 487 // Setup a audio track, connect it to the capturer and start it. | |
| 488 scoped_refptr<WebRtcLocalAudioTrackAdapter> adapter( | |
| 489 WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL)); | |
| 490 scoped_ptr<WebRtcLocalAudioTrack> track( | |
| 491 new WebRtcLocalAudioTrack(adapter.get(), capturer, NULL)); | |
| 492 track->Start(); | |
| 493 | |
| 494 // Verify the data flow by connecting the |sink| to |track|. | |
| 495 scoped_ptr<MockMediaStreamAudioSink> sink(new MockMediaStreamAudioSink()); | |
| 496 base::WaitableEvent event(false, false); | |
| 497 EXPECT_CALL(*sink, FormatIsSet()).Times(1); | |
| 498 // Verify the sinks are getting the packets with an expecting buffer size. | |
| 499 #if defined(OS_ANDROID) | |
| 500 const int expected_buffer_size = params.sample_rate() / 100; | |
| 501 #else | |
| 502 const int expected_buffer_size = params.frames_per_buffer(); | |
| 503 #endif | |
| 504 EXPECT_CALL(*sink, CaptureData()) | |
| 505 .Times(AtLeast(1)).WillRepeatedly(SignalEvent(&event)); | |
| 506 track->AddSink(sink.get()); | |
| 507 EXPECT_TRUE(event.TimedWait(TestTimeouts::tiny_timeout())); | |
| 508 EXPECT_EQ(expected_buffer_size, sink->audio_params().frames_per_buffer()); | |
| 509 | |
| 510 // Stopping the new source will stop the second track. | |
| 511 EXPECT_CALL(*source.get(), OnStop()).Times(1); | |
| 512 capturer->Stop(); | |
| 513 | |
| 514 // Even though this test don't use |capturer_source_| it will be stopped | |
| 515 // during teardown of the test harness. | |
| 516 EXPECT_CALL(*capturer_source_.get(), OnStop()); | |
| 517 } | |
| 518 | |
| 519 } // namespace content | |
| OLD | NEW |