| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/basictypes.h" | 5 #include "base/basictypes.h" |
| 6 #include "base/memory/scoped_ptr.h" | 6 #include "base/memory/scoped_ptr.h" |
| 7 #include "media/audio/audio_io.h" | 7 #include "media/audio/audio_io.h" |
| 8 #include "media/audio/audio_manager.h" | 8 #include "media/audio/audio_manager.h" |
| 9 #include "media/audio/simple_sources.h" | 9 #include "media/audio/simple_sources.h" |
| 10 #include "testing/gmock/include/gmock/gmock.h" | 10 #include "testing/gmock/include/gmock/gmock.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
| 12 | 12 |
| 13 using ::testing::_; | 13 using ::testing::_; |
| 14 using ::testing::AnyNumber; | 14 using ::testing::AnyNumber; |
| 15 using ::testing::DoAll; | 15 using ::testing::DoAll; |
| 16 using ::testing::Field; | 16 using ::testing::Field; |
| 17 using ::testing::InSequence; | 17 using ::testing::InSequence; |
| 18 using ::testing::Invoke; | 18 using ::testing::Invoke; |
| 19 using ::testing::NiceMock; | 19 using ::testing::NiceMock; |
| 20 using ::testing::NotNull; | 20 using ::testing::NotNull; |
| 21 using ::testing::Return; | 21 using ::testing::Return; |
| 22 | 22 |
| 23 namespace media { | 23 namespace media { |
| 24 | 24 |
| 25 class MockAudioSource : public AudioOutputStream::AudioSourceCallback { | 25 class MockAudioSource : public AudioOutputStream::AudioSourceCallback { |
| 26 public: | 26 public: |
| 27 MOCK_METHOD4(OnMoreData, uint32(AudioOutputStream* stream, uint8* dest, | 27 MOCK_METHOD3(OnMoreData, uint32(uint8* dest, |
| 28 uint32 max_size, | 28 uint32 max_size, |
| 29 AudioBuffersState buffers_state)); | 29 AudioBuffersState buffers_state)); |
| 30 MOCK_METHOD2(OnError, void(AudioOutputStream* stream, int code)); | 30 MOCK_METHOD2(OnError, void(AudioOutputStream* stream, int code)); |
| 31 }; | 31 }; |
| 32 | 32 |
| 33 // Validate that the SineWaveAudioSource writes the expected values for | 33 // Validate that the SineWaveAudioSource writes the expected values for |
| 34 // the FORMAT_16BIT_MONO. | 34 // the FORMAT_16BIT_MONO. |
| 35 TEST(MacAudioTest, SineWaveAudio16MonoTest) { | 35 TEST(MacAudioTest, SineWaveAudio16MonoTest) { |
| 36 const uint32 samples = 1024; | 36 const uint32 samples = 1024; |
| 37 const int freq = 200; | 37 const int freq = 200; |
| 38 | 38 |
| 39 SineWaveAudioSource source(SineWaveAudioSource::FORMAT_16BIT_LINEAR_PCM, 1, | 39 SineWaveAudioSource source(SineWaveAudioSource::FORMAT_16BIT_LINEAR_PCM, 1, |
| 40 freq, AudioParameters::kTelephoneSampleRate); | 40 freq, AudioParameters::kTelephoneSampleRate); |
| 41 | 41 |
| 42 // TODO(cpu): Put the real test when the mock renderer is ported. | 42 // TODO(cpu): Put the real test when the mock renderer is ported. |
| 43 uint16 buffer[samples] = { 0xffff }; | 43 uint16 buffer[samples] = { 0xffff }; |
| 44 source.OnMoreData(NULL, reinterpret_cast<uint8*>(buffer), sizeof(buffer), | 44 source.OnMoreData(reinterpret_cast<uint8*>(buffer), sizeof(buffer), |
| 45 AudioBuffersState(0, 0)); | 45 AudioBuffersState(0, 0)); |
| 46 EXPECT_EQ(0, buffer[0]); | 46 EXPECT_EQ(0, buffer[0]); |
| 47 EXPECT_EQ(5126, buffer[1]); | 47 EXPECT_EQ(5126, buffer[1]); |
| 48 } | 48 } |
| 49 | 49 |
| 50 // =========================================================================== | 50 // =========================================================================== |
| 51 // Validation of AudioParameters::AUDIO_PCM_LINEAR | 51 // Validation of AudioParameters::AUDIO_PCM_LINEAR |
| 52 // | 52 // |
| 53 | 53 |
| 54 // Test that can it be created and closed. | 54 // Test that can it be created and closed. |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 124 SineWaveAudioSource source(SineWaveAudioSource::FORMAT_16BIT_LINEAR_PCM, 1, | 124 SineWaveAudioSource source(SineWaveAudioSource::FORMAT_16BIT_LINEAR_PCM, 1, |
| 125 200.0, AudioParameters::kAudioCDSampleRate/2); | 125 200.0, AudioParameters::kAudioCDSampleRate/2); |
| 126 EXPECT_TRUE(oas->Open()); | 126 EXPECT_TRUE(oas->Open()); |
| 127 oas->Start(&source); | 127 oas->Start(&source); |
| 128 usleep(1500000); | 128 usleep(1500000); |
| 129 oas->Stop(); | 129 oas->Stop(); |
| 130 oas->Close(); | 130 oas->Close(); |
| 131 } | 131 } |
| 132 | 132 |
| 133 // Custom action to clear a memory buffer. | 133 // Custom action to clear a memory buffer. |
| 134 static void ClearBuffer(AudioOutputStream* stream, uint8* dest, | 134 static void ClearBuffer(uint8* dest, |
| 135 uint32 max_size, AudioBuffersState buffers_state) { | 135 uint32 max_size, |
| 136 AudioBuffersState buffers_state) { |
| 136 memset(dest, 0, max_size); | 137 memset(dest, 0, max_size); |
| 137 } | 138 } |
| 138 | 139 |
| 139 TEST(MacAudioTest, PCMWaveStreamPendingBytes) { | 140 TEST(MacAudioTest, PCMWaveStreamPendingBytes) { |
| 140 scoped_ptr<AudioManager> audio_man(AudioManager::Create()); | 141 scoped_ptr<AudioManager> audio_man(AudioManager::Create()); |
| 141 if (!audio_man->HasAudioOutputDevices()) | 142 if (!audio_man->HasAudioOutputDevices()) |
| 142 return; | 143 return; |
| 143 | 144 |
| 144 uint32 frames_100_ms = AudioParameters::kAudioCDSampleRate / 10; | 145 uint32 frames_100_ms = AudioParameters::kAudioCDSampleRate / 10; |
| 145 AudioOutputStream* oas = audio_man->MakeAudioOutputStream( | 146 AudioOutputStream* oas = audio_man->MakeAudioOutputStream( |
| 146 AudioParameters(AudioParameters::AUDIO_PCM_LINEAR, CHANNEL_LAYOUT_MONO, | 147 AudioParameters(AudioParameters::AUDIO_PCM_LINEAR, CHANNEL_LAYOUT_MONO, |
| 147 AudioParameters::kAudioCDSampleRate, 16, frames_100_ms)); | 148 AudioParameters::kAudioCDSampleRate, 16, frames_100_ms)); |
| 148 ASSERT_TRUE(NULL != oas); | 149 ASSERT_TRUE(NULL != oas); |
| 149 | 150 |
| 150 NiceMock<MockAudioSource> source; | 151 NiceMock<MockAudioSource> source; |
| 151 EXPECT_TRUE(oas->Open()); | 152 EXPECT_TRUE(oas->Open()); |
| 152 | 153 |
| 153 uint32 bytes_100_ms = frames_100_ms * 2; | 154 uint32 bytes_100_ms = frames_100_ms * 2; |
| 154 | 155 |
| 155 // We expect the amount of pending bytes will reaching |bytes_100_ms| | 156 // We expect the amount of pending bytes will reaching |bytes_100_ms| |
| 156 // because the audio output stream has a double buffer scheme. | 157 // because the audio output stream has a double buffer scheme. |
| 157 // And then we will try to provide zero data so the amount of pending bytes | 158 // And then we will try to provide zero data so the amount of pending bytes |
| 158 // will go down and eventually read zero. | 159 // will go down and eventually read zero. |
| 159 InSequence s; | 160 InSequence s; |
| 160 EXPECT_CALL(source, OnMoreData(oas, NotNull(), bytes_100_ms, | 161 EXPECT_CALL(source, OnMoreData(NotNull(), bytes_100_ms, |
| 161 Field(&AudioBuffersState::pending_bytes, 0))) | 162 Field(&AudioBuffersState::pending_bytes, 0))) |
| 162 .WillOnce(DoAll(Invoke(&ClearBuffer), Return(bytes_100_ms))); | 163 .WillOnce(DoAll(Invoke(&ClearBuffer), Return(bytes_100_ms))); |
| 163 EXPECT_CALL(source, OnMoreData(oas, NotNull(), bytes_100_ms, | 164 EXPECT_CALL(source, OnMoreData(NotNull(), bytes_100_ms, |
| 164 Field(&AudioBuffersState::pending_bytes, | 165 Field(&AudioBuffersState::pending_bytes, |
| 165 bytes_100_ms))) | 166 bytes_100_ms))) |
| 166 .WillOnce(DoAll(Invoke(&ClearBuffer), Return(bytes_100_ms))); | 167 .WillOnce(DoAll(Invoke(&ClearBuffer), Return(bytes_100_ms))); |
| 167 EXPECT_CALL(source, OnMoreData(oas, NotNull(), bytes_100_ms, | 168 EXPECT_CALL(source, OnMoreData(NotNull(), bytes_100_ms, |
| 168 Field(&AudioBuffersState::pending_bytes, | 169 Field(&AudioBuffersState::pending_bytes, |
| 169 bytes_100_ms))) | 170 bytes_100_ms))) |
| 170 .WillOnce(Return(0)); | 171 .WillOnce(Return(0)); |
| 171 EXPECT_CALL(source, OnMoreData(oas, NotNull(), bytes_100_ms, _)) | 172 EXPECT_CALL(source, OnMoreData(NotNull(), bytes_100_ms, _)) |
| 172 .Times(AnyNumber()) | 173 .Times(AnyNumber()) |
| 173 .WillRepeatedly(Return(0)); | 174 .WillRepeatedly(Return(0)); |
| 174 | 175 |
| 175 oas->Start(&source); | 176 oas->Start(&source); |
| 176 usleep(500000); | 177 usleep(500000); |
| 177 oas->Stop(); | 178 oas->Stop(); |
| 178 oas->Close(); | 179 oas->Close(); |
| 179 } | 180 } |
| 180 | 181 |
| 181 } // namespace media | 182 } // namespace media |
| OLD | NEW |