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