diff options
Diffstat (limited to 'chromium/media/audio/mac/audio_low_latency_input_mac_unittest.cc')
-rw-r--r-- | chromium/media/audio/mac/audio_low_latency_input_mac_unittest.cc | 83 |
1 files changed, 39 insertions, 44 deletions
diff --git a/chromium/media/audio/mac/audio_low_latency_input_mac_unittest.cc b/chromium/media/audio/mac/audio_low_latency_input_mac_unittest.cc index 9360befe575..79721d4f37b 100644 --- a/chromium/media/audio/mac/audio_low_latency_input_mac_unittest.cc +++ b/chromium/media/audio/mac/audio_low_latency_input_mac_unittest.cc @@ -5,6 +5,7 @@ #include "base/basictypes.h" #include "base/environment.h" #include "base/message_loop/message_loop.h" +#include "base/run_loop.h" #include "base/test/test_timeouts.h" #include "base/threading/platform_thread.h" #include "media/audio/audio_io.h" @@ -22,18 +23,19 @@ using ::testing::NotNull; namespace media { -ACTION_P3(CheckCountAndPostQuitTask, count, limit, loop) { +ACTION_P4(CheckCountAndPostQuitTask, count, limit, loop, closure) { if (++*count >= limit) { - loop->PostTask(FROM_HERE, base::MessageLoop::QuitClosure()); + loop->PostTask(FROM_HERE, closure); } } class MockAudioInputCallback : public AudioInputStream::AudioInputCallback { public: - MOCK_METHOD5(OnData, void(AudioInputStream* stream, - const uint8* src, uint32 size, - uint32 hardware_delay_bytes, double volume)); - MOCK_METHOD1(OnClose, void(AudioInputStream* stream)); + MOCK_METHOD4(OnData, + void(AudioInputStream* stream, + const AudioBus* src, + uint32 hardware_delay_bytes, + double volume)); MOCK_METHOD1(OnError, void(AudioInputStream* stream)); }; @@ -74,17 +76,23 @@ class WriteToFileAudioSink : public AudioInputStream::AudioInputCallback { // AudioInputStream::AudioInputCallback implementation. virtual void OnData(AudioInputStream* stream, - const uint8* src, uint32 size, - uint32 hardware_delay_bytes, double volume) OVERRIDE { + const AudioBus* src, + uint32 hardware_delay_bytes, + double volume) OVERRIDE { + const int num_samples = src->frames() * src->channels(); + scoped_ptr<int16> interleaved(new int16[num_samples]); + const int bytes_per_sample = sizeof(*interleaved); + src->ToInterleaved(src->frames(), bytes_per_sample, interleaved.get()); + // Store data data in a temporary buffer to avoid making blocking // fwrite() calls in the audio callback. The complete buffer will be // written to file in the destructor. - if (buffer_.Append(src, size)) { + const int size = bytes_per_sample * num_samples; + if (buffer_.Append((const uint8*)interleaved.get(), size)) { bytes_to_write_ += size; } } - virtual void OnClose(AudioInputStream* stream) OVERRIDE {} virtual void OnError(AudioInputStream* stream) OVERRIDE {} private: @@ -95,8 +103,16 @@ class WriteToFileAudioSink : public AudioInputStream::AudioInputCallback { class MacAudioInputTest : public testing::Test { protected: - MacAudioInputTest() : audio_manager_(AudioManager::CreateForTesting()) {} - virtual ~MacAudioInputTest() {} + MacAudioInputTest() + : message_loop_(base::MessageLoop::TYPE_UI), + audio_manager_(AudioManager::CreateForTesting()) { + // Wait for the AudioManager to finish any initialization on the audio loop. + base::RunLoop().RunUntilIdle(); + } + + virtual ~MacAudioInputTest() { + base::RunLoop().RunUntilIdle(); + } // Convenience method which ensures that we are not running on the build // bots and that at least one valid input device can be found. @@ -134,6 +150,7 @@ class MacAudioInputTest : public testing::Test { return ais; } + base::MessageLoop message_loop_; scoped_ptr<AudioManager> audio_manager_; }; @@ -162,8 +179,6 @@ TEST_F(MacAudioInputTest, AUAudioInputStreamOpenStartAndClose) { EXPECT_TRUE(ais->Open()); MockAudioInputCallback sink; ais->Start(&sink); - EXPECT_CALL(sink, OnClose(ais)) - .Times(1); ais->Close(); } @@ -176,8 +191,6 @@ TEST_F(MacAudioInputTest, AUAudioInputStreamOpenStartStopAndClose) { MockAudioInputCallback sink; ais->Start(&sink); ais->Stop(); - EXPECT_CALL(sink, OnClose(ais)) - .Times(1); ais->Close(); } @@ -206,8 +219,6 @@ TEST_F(MacAudioInputTest, AUAudioInputStreamMiscCallingSequences) { ais->Stop(); EXPECT_FALSE(auais->started()); - EXPECT_CALL(sink, OnClose(ais)) - .Times(1); ais->Close(); } @@ -217,32 +228,24 @@ TEST_F(MacAudioInputTest, AUAudioInputStreamVerifyMonoRecording) { return; int count = 0; - base::MessageLoopForUI loop; // Create an audio input stream which records in mono. AudioInputStream* ais = CreateAudioInputStream(CHANNEL_LAYOUT_MONO); EXPECT_TRUE(ais->Open()); - int fs = static_cast<int>(AUAudioInputStream::HardwareSampleRate()); - int samples_per_packet = fs / 100; - int bits_per_sample = 16; - uint32 bytes_per_packet = samples_per_packet * (bits_per_sample / 8); - MockAudioInputCallback sink; // We use 10ms packets and will run the test until ten packets are received. // All should contain valid packets of the same size and a valid delay // estimate. - EXPECT_CALL(sink, OnData(ais, NotNull(), bytes_per_packet, _, _)) + base::RunLoop run_loop; + EXPECT_CALL(sink, OnData(ais, NotNull(), _, _)) .Times(AtLeast(10)) - .WillRepeatedly(CheckCountAndPostQuitTask(&count, 10, &loop)); + .WillRepeatedly(CheckCountAndPostQuitTask( + &count, 10, &message_loop_, run_loop.QuitClosure())); ais->Start(&sink); - loop.Run(); + run_loop.Run(); ais->Stop(); - - // Verify that the sink receieves OnClose() call when calling Close(). - EXPECT_CALL(sink, OnClose(ais)) - .Times(1); ais->Close(); } @@ -252,17 +255,11 @@ TEST_F(MacAudioInputTest, AUAudioInputStreamVerifyStereoRecording) { return; int count = 0; - base::MessageLoopForUI loop; // Create an audio input stream which records in stereo. AudioInputStream* ais = CreateAudioInputStream(CHANNEL_LAYOUT_STEREO); EXPECT_TRUE(ais->Open()); - int fs = static_cast<int>(AUAudioInputStream::HardwareSampleRate()); - int samples_per_packet = fs / 100; - int bits_per_sample = 16; - uint32 bytes_per_packet = 2 * samples_per_packet * (bits_per_sample / 8); - MockAudioInputCallback sink; // We use 10ms packets and will run the test until ten packets are received. @@ -275,16 +272,14 @@ TEST_F(MacAudioInputTest, AUAudioInputStreamVerifyStereoRecording) { // parameter #4 does no longer pass. I am removing this restriction here to // ensure that we can land the patch but will revisit this test again when // more analysis of the delay estimates are done. - EXPECT_CALL(sink, OnData(ais, NotNull(), bytes_per_packet, _, _)) + base::RunLoop run_loop; + EXPECT_CALL(sink, OnData(ais, NotNull(), _, _)) .Times(AtLeast(10)) - .WillRepeatedly(CheckCountAndPostQuitTask(&count, 10, &loop)); + .WillRepeatedly(CheckCountAndPostQuitTask( + &count, 10, &message_loop_, run_loop.QuitClosure())); ais->Start(&sink); - loop.Run(); + run_loop.Run(); ais->Stop(); - - // Verify that the sink receieves OnClose() call when calling Close(). - EXPECT_CALL(sink, OnClose(ais)) - .Times(1); ais->Close(); } |