OLD | NEW |
(Empty) | |
| 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/message_loop.h" |
| 6 #include "base/synchronization/waitable_event.h" |
| 7 #include "media/audio/audio_manager.h" |
| 8 #include "media/audio/simple_sources.h" |
| 9 #include "media/audio/virtual_audio_input_stream.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 |
| 12 namespace media { |
| 13 |
| 14 class MockInputCallback : public AudioInputStream::AudioInputCallback { |
| 15 public: |
| 16 MockInputCallback() {} |
| 17 virtual void OnData(AudioInputStream* stream, const uint8* data, |
| 18 uint32 size, uint32 hardware_delay_bytes, |
| 19 double volume) {} |
| 20 virtual void OnClose(AudioInputStream* stream) {} |
| 21 virtual void OnError(AudioInputStream* stream, int code) {} |
| 22 |
| 23 private: |
| 24 DISALLOW_COPY_AND_ASSIGN(MockInputCallback); |
| 25 }; |
| 26 |
| 27 class VirtualAudioInputStreamTest : public testing::Test { |
| 28 public: |
| 29 VirtualAudioInputStreamTest() |
| 30 : audio_manager_(AudioManager::Create()), |
| 31 params_(AudioParameters::AUDIO_MIRROR_BROWSER, |
| 32 CHANNEL_LAYOUT_MONO, 8000, 8, 128), |
| 33 output_params_(AudioParameters::AUDIO_PCM_LINEAR, |
| 34 CHANNEL_LAYOUT_MONO, 8000, 8, 128), |
| 35 stream_(NULL), |
| 36 source_(CHANNEL_LAYOUT_STEREO, 200.0, 128), |
| 37 done_(false, false) { |
| 38 } |
| 39 |
| 40 void StartStreamAndRunTestsOnAudioThread(int num_output_streams, |
| 41 int num_callback_iterations, |
| 42 int num_streams_removed_per_round, |
| 43 int num_expected_source_callbacks) { |
| 44 ASSERT_TRUE(audio_manager_->GetMessageLoop()->BelongsToCurrentThread()); |
| 45 stream_->Open(); |
| 46 stream_->Start(&input_callback_); |
| 47 AddStreamsAndDoCallbacks(num_output_streams, |
| 48 num_callback_iterations, |
| 49 num_streams_removed_per_round, |
| 50 num_expected_source_callbacks); |
| 51 } |
| 52 |
| 53 void AddStreamsAndDoCallbacks(int num_output_streams, |
| 54 int num_callback_iterations, |
| 55 int num_streams_removed_per_round, |
| 56 int num_expected_source_callbacks) { |
| 57 ASSERT_TRUE(audio_manager_->GetMessageLoop()->BelongsToCurrentThread()); |
| 58 |
| 59 for (int i = 0; i < num_output_streams; ++i) { |
| 60 AudioOutputStream* output_stream = |
| 61 audio_manager_->MakeAudioOutputStream(output_params_); |
| 62 DCHECK(output_stream); |
| 63 output_streams_.push_back(output_stream); |
| 64 |
| 65 output_stream->Open(); |
| 66 output_stream->Start(&source_); |
| 67 } |
| 68 |
| 69 if (num_streams_removed_per_round > 0) { |
| 70 AudioOutputStream* output_stream = output_streams_.back(); |
| 71 output_streams_.pop_back(); |
| 72 output_stream->Stop(); |
| 73 output_stream->Close(); |
| 74 } |
| 75 |
| 76 if (num_callback_iterations > 0) { |
| 77 // Force the next callback to be immediate. |
| 78 stream_->next_read_time_ -= base::TimeDelta::FromMilliseconds(10000); |
| 79 audio_manager_->GetMessageLoop()->PostTask(FROM_HERE, |
| 80 base::Bind(&VirtualAudioInputStreamTest::AddStreamsAndDoCallbacks, |
| 81 base::Unretained(this), |
| 82 0, |
| 83 --num_callback_iterations, |
| 84 num_streams_removed_per_round, |
| 85 num_expected_source_callbacks)); |
| 86 } else { |
| 87 // Finish the test. |
| 88 for (std::vector<AudioOutputStream*>::iterator it = |
| 89 output_streams_.begin(); it != output_streams_.end(); ++it) { |
| 90 (*it)->Stop(); |
| 91 (*it)->Close(); |
| 92 } |
| 93 |
| 94 stream_->Stop(); |
| 95 stream_->Close(); |
| 96 |
| 97 EXPECT_EQ(num_expected_source_callbacks, source_.callbacks()); |
| 98 EXPECT_EQ(0, source_.errors()); |
| 99 |
| 100 done_.Signal(); |
| 101 } |
| 102 } |
| 103 |
| 104 protected: |
| 105 scoped_ptr<AudioManager> audio_manager_; |
| 106 AudioParameters params_; |
| 107 AudioParameters output_params_; |
| 108 VirtualAudioInputStream* stream_; |
| 109 MockInputCallback input_callback_; |
| 110 std::vector<AudioOutputStream*> output_streams_; |
| 111 SineWaveAudioSource source_; |
| 112 base::WaitableEvent done_; |
| 113 |
| 114 private: |
| 115 DISALLOW_COPY_AND_ASSIGN(VirtualAudioInputStreamTest); |
| 116 }; |
| 117 |
| 118 TEST_F(VirtualAudioInputStreamTest, AttachAndDriveSingleStream) { |
| 119 stream_ = static_cast<VirtualAudioInputStream*>( |
| 120 audio_manager_->MakeAudioInputStream(params_, "1")); |
| 121 DCHECK(stream_); |
| 122 |
| 123 int num_output_streams = 1; |
| 124 int num_callback_iterations = 1; |
| 125 int num_streams_removed_per_round = 0; |
| 126 int num_expected_source_callbacks = 1; |
| 127 |
| 128 audio_manager_->GetMessageLoop()->PostTask(FROM_HERE, base::Bind( |
| 129 &VirtualAudioInputStreamTest::StartStreamAndRunTestsOnAudioThread, |
| 130 base::Unretained(this), |
| 131 num_output_streams, num_callback_iterations, |
| 132 num_streams_removed_per_round, |
| 133 num_expected_source_callbacks)); |
| 134 |
| 135 done_.Wait(); |
| 136 } |
| 137 |
| 138 TEST_F(VirtualAudioInputStreamTest, AttachAndDriveMultipleStreams) { |
| 139 stream_ = static_cast<VirtualAudioInputStream*>( |
| 140 audio_manager_->MakeAudioInputStream(params_, "1")); |
| 141 DCHECK(stream_); |
| 142 |
| 143 int num_output_streams = 5; |
| 144 int num_callback_iterations = 5; |
| 145 int num_streams_removed_per_round = 0; |
| 146 int num_expected_source_callbacks = 25; |
| 147 |
| 148 audio_manager_->GetMessageLoop()->PostTask(FROM_HERE, base::Bind( |
| 149 &VirtualAudioInputStreamTest::StartStreamAndRunTestsOnAudioThread, |
| 150 base::Unretained(this), |
| 151 num_output_streams, num_callback_iterations, |
| 152 num_streams_removed_per_round, |
| 153 num_expected_source_callbacks)); |
| 154 |
| 155 done_.Wait(); |
| 156 } |
| 157 |
| 158 TEST_F(VirtualAudioInputStreamTest, RemoveStreams) { |
| 159 stream_ = static_cast<VirtualAudioInputStream*>( |
| 160 audio_manager_->MakeAudioInputStream(params_, "1")); |
| 161 DCHECK(stream_); |
| 162 |
| 163 int num_output_streams = 8; |
| 164 int num_callback_iterations = 5; |
| 165 int num_streams_removed_per_round = 1; |
| 166 int num_expected_source_callbacks = 7 + 6 + 5 + 4 + 3; |
| 167 |
| 168 audio_manager_->GetMessageLoop()->PostTask(FROM_HERE, base::Bind( |
| 169 &VirtualAudioInputStreamTest::StartStreamAndRunTestsOnAudioThread, |
| 170 base::Unretained(this), |
| 171 num_output_streams, num_callback_iterations, |
| 172 num_streams_removed_per_round, |
| 173 num_expected_source_callbacks)); |
| 174 |
| 175 done_.Wait(); |
| 176 } |
| 177 |
| 178 } // namespace media |
OLD | NEW |