Home | History | Annotate | Download | only in audio
      1 // Copyright (c) 2012 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/basictypes.h"
      6 #include "base/bind.h"
      7 #include "base/message_loop/message_loop.h"
      8 #include "base/synchronization/waitable_event.h"
      9 #include "base/test/test_timeouts.h"
     10 #include "media/audio/audio_input_controller.h"
     11 #include "media/audio/audio_manager_base.h"
     12 #include "testing/gmock/include/gmock/gmock.h"
     13 #include "testing/gtest/include/gtest/gtest.h"
     14 
     15 using ::testing::_;
     16 using ::testing::AtLeast;
     17 using ::testing::Exactly;
     18 using ::testing::InvokeWithoutArgs;
     19 using ::testing::NotNull;
     20 
     21 namespace media {
     22 
     23 static const int kSampleRate = AudioParameters::kAudioCDSampleRate;
     24 static const int kBitsPerSample = 16;
     25 static const ChannelLayout kChannelLayout = CHANNEL_LAYOUT_STEREO;
     26 static const int kSamplesPerPacket = kSampleRate / 10;
     27 
     28 // Posts base::MessageLoop::QuitClosure() on specified message loop.
     29 ACTION_P(QuitMessageLoop, loop_or_proxy) {
     30   loop_or_proxy->PostTask(FROM_HERE, base::MessageLoop::QuitClosure());
     31 }
     32 
     33 // Posts base::MessageLoop::QuitClosure() on specified message loop after a
     34 // certain number of calls given by |limit|.
     35 ACTION_P3(CheckCountAndPostQuitTask, count, limit, loop_or_proxy) {
     36   if (++*count >= limit) {
     37     loop_or_proxy->PostTask(FROM_HERE, base::MessageLoop::QuitClosure());
     38   }
     39 }
     40 
     41 // Closes AudioOutputController synchronously.
     42 static void CloseAudioController(AudioInputController* controller) {
     43   controller->Close(base::MessageLoop::QuitClosure());
     44   base::MessageLoop::current()->Run();
     45 }
     46 
     47 class MockAudioInputControllerEventHandler
     48     : public AudioInputController::EventHandler {
     49  public:
     50   MockAudioInputControllerEventHandler() {}
     51 
     52   MOCK_METHOD1(OnCreated, void(AudioInputController* controller));
     53   MOCK_METHOD1(OnRecording, void(AudioInputController* controller));
     54   MOCK_METHOD1(OnError, void(AudioInputController* controller));
     55   MOCK_METHOD3(OnData, void(AudioInputController* controller,
     56                             const uint8* data, uint32 size));
     57 
     58  private:
     59   DISALLOW_COPY_AND_ASSIGN(MockAudioInputControllerEventHandler);
     60 };
     61 
     62 // Test fixture.
     63 class AudioInputControllerTest : public testing::Test {
     64  public:
     65   AudioInputControllerTest() {}
     66   virtual ~AudioInputControllerTest() {}
     67 
     68  protected:
     69   base::MessageLoop message_loop_;
     70 
     71  private:
     72   DISALLOW_COPY_AND_ASSIGN(AudioInputControllerTest);
     73 };
     74 
     75 // Test AudioInputController for create and close without recording audio.
     76 TEST_F(AudioInputControllerTest, CreateAndClose) {
     77   MockAudioInputControllerEventHandler event_handler;
     78 
     79   // OnCreated() will be posted once.
     80   EXPECT_CALL(event_handler, OnCreated(NotNull()))
     81       .WillOnce(QuitMessageLoop(&message_loop_));
     82 
     83   scoped_ptr<AudioManager> audio_manager(AudioManager::Create());
     84   AudioParameters params(AudioParameters::AUDIO_FAKE, kChannelLayout,
     85                          kSampleRate, kBitsPerSample, kSamplesPerPacket);
     86   scoped_refptr<AudioInputController> controller =
     87       AudioInputController::Create(audio_manager.get(), &event_handler, params,
     88                                    AudioManagerBase::kDefaultDeviceId);
     89   ASSERT_TRUE(controller.get());
     90 
     91   // Wait for OnCreated() to fire.
     92   message_loop_.Run();
     93 
     94   // Close the AudioInputController synchronously.
     95   CloseAudioController(controller.get());
     96 }
     97 
     98 // Test a normal call sequence of create, record and close.
     99 TEST_F(AudioInputControllerTest, RecordAndClose) {
    100   MockAudioInputControllerEventHandler event_handler;
    101   int count = 0;
    102 
    103   // OnCreated() will be called once.
    104   EXPECT_CALL(event_handler, OnCreated(NotNull()))
    105       .Times(Exactly(1));
    106 
    107   // OnRecording() will be called only once.
    108   EXPECT_CALL(event_handler, OnRecording(NotNull()))
    109       .Times(Exactly(1));
    110 
    111   // OnData() shall be called ten times.
    112   EXPECT_CALL(event_handler, OnData(NotNull(), NotNull(), _))
    113       .Times(AtLeast(10))
    114       .WillRepeatedly(CheckCountAndPostQuitTask(&count, 10,
    115           message_loop_.message_loop_proxy()));
    116 
    117   scoped_ptr<AudioManager> audio_manager(AudioManager::Create());
    118   AudioParameters params(AudioParameters::AUDIO_FAKE, kChannelLayout,
    119                          kSampleRate, kBitsPerSample, kSamplesPerPacket);
    120 
    121   // Creating the AudioInputController should render an OnCreated() call.
    122   scoped_refptr<AudioInputController> controller =
    123       AudioInputController::Create(audio_manager.get(), &event_handler, params,
    124                                    AudioManagerBase::kDefaultDeviceId);
    125   ASSERT_TRUE(controller.get());
    126 
    127   // Start recording and trigger one OnRecording() call.
    128   controller->Record();
    129 
    130   // Record and wait until ten OnData() callbacks are received.
    131   message_loop_.Run();
    132 
    133   // Close the AudioInputController synchronously.
    134   CloseAudioController(controller.get());
    135 }
    136 
    137 // Test that the AudioInputController reports an error when the input stream
    138 // stops without an OnClose() callback. This can happen when the underlying
    139 // audio layer stops feeding data as a result of a removed microphone device.
    140 TEST_F(AudioInputControllerTest, RecordAndError) {
    141   MockAudioInputControllerEventHandler event_handler;
    142   int count = 0;
    143 
    144   // OnCreated() will be called once.
    145   EXPECT_CALL(event_handler, OnCreated(NotNull()))
    146       .Times(Exactly(1));
    147 
    148   // OnRecording() will be called only once.
    149   EXPECT_CALL(event_handler, OnRecording(NotNull()))
    150       .Times(Exactly(1));
    151 
    152   // OnData() shall be called ten times.
    153   EXPECT_CALL(event_handler, OnData(NotNull(), NotNull(), _))
    154       .Times(AtLeast(10))
    155       .WillRepeatedly(CheckCountAndPostQuitTask(&count, 10,
    156           message_loop_.message_loop_proxy()));
    157 
    158   // OnError() will be called after the data stream stops while the
    159   // controller is in a recording state.
    160   EXPECT_CALL(event_handler, OnError(NotNull()))
    161       .Times(Exactly(1))
    162       .WillOnce(QuitMessageLoop(&message_loop_));
    163 
    164   scoped_ptr<AudioManager> audio_manager(AudioManager::Create());
    165   AudioParameters params(AudioParameters::AUDIO_FAKE, kChannelLayout,
    166                          kSampleRate, kBitsPerSample, kSamplesPerPacket);
    167 
    168   // Creating the AudioInputController should render an OnCreated() call.
    169   scoped_refptr<AudioInputController> controller =
    170       AudioInputController::Create(audio_manager.get(), &event_handler, params,
    171                                    AudioManagerBase::kDefaultDeviceId);
    172   ASSERT_TRUE(controller.get());
    173 
    174   // Start recording and trigger one OnRecording() call.
    175   controller->Record();
    176 
    177   // Record and wait until ten OnData() callbacks are received.
    178   message_loop_.Run();
    179 
    180   // Stop the stream and verify that OnError() is posted.
    181   AudioInputStream* stream = controller->stream_for_testing();
    182   stream->Stop();
    183   message_loop_.Run();
    184 
    185   // Close the AudioInputController synchronously.
    186   CloseAudioController(controller.get());
    187 }
    188 
    189 // Test that AudioInputController rejects insanely large packet sizes.
    190 TEST_F(AudioInputControllerTest, SamplesPerPacketTooLarge) {
    191   // Create an audio device with a very large packet size.
    192   MockAudioInputControllerEventHandler event_handler;
    193 
    194   // OnCreated() shall not be called in this test.
    195   EXPECT_CALL(event_handler, OnCreated(NotNull()))
    196     .Times(Exactly(0));
    197 
    198   scoped_ptr<AudioManager> audio_manager(AudioManager::Create());
    199   AudioParameters params(AudioParameters::AUDIO_FAKE, kChannelLayout,
    200                          kSampleRate, kBitsPerSample, kSamplesPerPacket * 1000);
    201   scoped_refptr<AudioInputController> controller =
    202       AudioInputController::Create(audio_manager.get(), &event_handler, params,
    203                                    AudioManagerBase::kDefaultDeviceId);
    204   ASSERT_FALSE(controller.get());
    205 }
    206 
    207 // Test calling AudioInputController::Close multiple times.
    208 TEST_F(AudioInputControllerTest, CloseTwice) {
    209   MockAudioInputControllerEventHandler event_handler;
    210 
    211   // OnRecording() will be called only once.
    212   EXPECT_CALL(event_handler, OnCreated(NotNull()));
    213 
    214   // OnRecording() will be called only once.
    215   EXPECT_CALL(event_handler, OnRecording(NotNull()))
    216       .Times(Exactly(1));
    217 
    218   scoped_ptr<AudioManager> audio_manager(AudioManager::Create());
    219   AudioParameters params(AudioParameters::AUDIO_FAKE, kChannelLayout,
    220                          kSampleRate, kBitsPerSample, kSamplesPerPacket);
    221   scoped_refptr<AudioInputController> controller =
    222       AudioInputController::Create(audio_manager.get(), &event_handler, params,
    223                                    AudioManagerBase::kDefaultDeviceId);
    224   ASSERT_TRUE(controller.get());
    225 
    226   controller->Record();
    227 
    228   controller->Close(base::MessageLoop::QuitClosure());
    229   base::MessageLoop::current()->Run();
    230 
    231   controller->Close(base::MessageLoop::QuitClosure());
    232   base::MessageLoop::current()->Run();
    233 }
    234 
    235 }  // namespace media
    236