 Chromium Code Reviews
 Chromium Code Reviews Issue 10832285:
  Switch OnMoreData() to use AudioBus.  (Closed) 
  Base URL: svn://svn.chromium.org/chrome/trunk/src
    
  
    Issue 10832285:
  Switch OnMoreData() to use AudioBus.  (Closed) 
  Base URL: svn://svn.chromium.org/chrome/trunk/src| 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_METHOD3(OnMoreData, uint32(uint8* dest, | 27 MOCK_METHOD2(OnMoreData, int(AudioBus* audio_bus, | 
| 28 uint32 max_size, | 28 AudioBuffersState buffers_state)); | 
| 29 AudioBuffersState buffers_state)); | |
| 30 MOCK_METHOD2(OnError, void(AudioOutputStream* stream, int code)); | 29 MOCK_METHOD2(OnError, void(AudioOutputStream* stream, int code)); | 
| 31 }; | 30 }; | 
| 32 | 31 | 
| 33 // Validate that the SineWaveAudioSource writes the expected values for | |
| 34 // the FORMAT_16BIT_MONO. | |
| 35 TEST(MacAudioTest, SineWaveAudio16MonoTest) { | |
| 
DaleCurtis
2012/08/22 23:09:21
Duplicate test from simple sources. Copy/paste rem
 | |
| 36 const uint32 samples = 1024; | |
| 37 const int freq = 200; | |
| 38 | |
| 39 SineWaveAudioSource source(SineWaveAudioSource::FORMAT_16BIT_LINEAR_PCM, 1, | |
| 40 freq, AudioParameters::kTelephoneSampleRate); | |
| 41 | |
| 42 // TODO(cpu): Put the real test when the mock renderer is ported. | |
| 43 uint16 buffer[samples] = { 0xffff }; | |
| 44 source.OnMoreData(reinterpret_cast<uint8*>(buffer), sizeof(buffer), | |
| 45 AudioBuffersState(0, 0)); | |
| 46 EXPECT_EQ(0, buffer[0]); | |
| 47 EXPECT_EQ(5126, buffer[1]); | |
| 48 } | |
| 49 | |
| 50 // =========================================================================== | 32 // =========================================================================== | 
| 51 // Validation of AudioParameters::AUDIO_PCM_LINEAR | 33 // Validation of AudioParameters::AUDIO_PCM_LINEAR | 
| 52 // | 34 // | 
| 53 | 35 | 
| 54 // Test that can it be created and closed. | 36 // Test that can it be created and closed. | 
| 55 TEST(MacAudioTest, PCMWaveStreamGetAndClose) { | 37 TEST(MacAudioTest, PCMWaveStreamGetAndClose) { | 
| 56 scoped_ptr<AudioManager> audio_man(AudioManager::Create()); | 38 scoped_ptr<AudioManager> audio_man(AudioManager::Create()); | 
| 57 if (!audio_man->HasAudioOutputDevices()) | 39 if (!audio_man->HasAudioOutputDevices()) | 
| 58 return; | 40 return; | 
| 59 AudioOutputStream* oas = audio_man->MakeAudioOutputStream( | 41 AudioOutputStream* oas = audio_man->MakeAudioOutputStream( | 
| (...skipping 24 matching lines...) Expand all Loading... | |
| 84 scoped_ptr<AudioManager> audio_man(AudioManager::Create()); | 66 scoped_ptr<AudioManager> audio_man(AudioManager::Create()); | 
| 85 if (!audio_man->HasAudioOutputDevices()) | 67 if (!audio_man->HasAudioOutputDevices()) | 
| 86 return; | 68 return; | 
| 87 uint32 frames_100_ms = AudioParameters::kAudioCDSampleRate / 10; | 69 uint32 frames_100_ms = AudioParameters::kAudioCDSampleRate / 10; | 
| 88 AudioOutputStream* oas = audio_man->MakeAudioOutputStream( | 70 AudioOutputStream* oas = audio_man->MakeAudioOutputStream( | 
| 89 AudioParameters(AudioParameters::AUDIO_PCM_LINEAR, CHANNEL_LAYOUT_MONO, | 71 AudioParameters(AudioParameters::AUDIO_PCM_LINEAR, CHANNEL_LAYOUT_MONO, | 
| 90 AudioParameters::kAudioCDSampleRate, 16, frames_100_ms)); | 72 AudioParameters::kAudioCDSampleRate, 16, frames_100_ms)); | 
| 91 ASSERT_TRUE(NULL != oas); | 73 ASSERT_TRUE(NULL != oas); | 
| 92 EXPECT_TRUE(oas->Open()); | 74 EXPECT_TRUE(oas->Open()); | 
| 93 | 75 | 
| 94 SineWaveAudioSource source(SineWaveAudioSource::FORMAT_16BIT_LINEAR_PCM, 1, | 76 SineWaveAudioSource source(1, 200.0, AudioParameters::kAudioCDSampleRate); | 
| 95 200.0, AudioParameters::kAudioCDSampleRate); | |
| 96 oas->SetVolume(0.5); | 77 oas->SetVolume(0.5); | 
| 97 oas->Start(&source); | 78 oas->Start(&source); | 
| 98 usleep(500000); | 79 usleep(500000); | 
| 99 | 80 | 
| 100 // Test that the volume is within the set limits. | 81 // Test that the volume is within the set limits. | 
| 101 double volume = 0.0; | 82 double volume = 0.0; | 
| 102 oas->GetVolume(&volume); | 83 oas->GetVolume(&volume); | 
| 103 EXPECT_LT(volume, 0.51); | 84 EXPECT_LT(volume, 0.51); | 
| 104 EXPECT_GT(volume, 0.49); | 85 EXPECT_GT(volume, 0.49); | 
| 105 oas->Stop(); | 86 oas->Stop(); | 
| 106 oas->Close(); | 87 oas->Close(); | 
| 107 } | 88 } | 
| 108 | 89 | 
| 109 // This test produces actual audio for 1.5 seconds on the default wave device at | 90 // This test produces actual audio for 1.5 seconds on the default wave device at | 
| 110 // 22K s/sec. Parameters have been chosen carefully so you should not hear pops | 91 // 22K s/sec. Parameters have been chosen carefully so you should not hear pops | 
| 111 // or noises while the sound is playing. The sound must also be identical to the | 92 // or noises while the sound is playing. The sound must also be identical to the | 
| 112 // sound of PCMWaveStreamPlay200HzTone44KssMono test. | 93 // sound of PCMWaveStreamPlay200HzTone44KssMono test. | 
| 113 TEST(MacAudioTest, PCMWaveStreamPlay200HzTone22KssMono) { | 94 TEST(MacAudioTest, PCMWaveStreamPlay200HzTone22KssMono) { | 
| 114 scoped_ptr<AudioManager> audio_man(AudioManager::Create()); | 95 scoped_ptr<AudioManager> audio_man(AudioManager::Create()); | 
| 115 if (!audio_man->HasAudioOutputDevices()) | 96 if (!audio_man->HasAudioOutputDevices()) | 
| 116 return; | 97 return; | 
| 117 uint32 frames_100_ms = AudioParameters::kAudioCDSampleRate / 10; | 98 uint32 frames_100_ms = AudioParameters::kAudioCDSampleRate / 10; | 
| 118 AudioOutputStream* oas = audio_man->MakeAudioOutputStream( | 99 AudioOutputStream* oas = audio_man->MakeAudioOutputStream( | 
| 119 AudioParameters(AudioParameters::AUDIO_PCM_LINEAR, CHANNEL_LAYOUT_MONO, | 100 AudioParameters(AudioParameters::AUDIO_PCM_LINEAR, CHANNEL_LAYOUT_MONO, | 
| 120 AudioParameters::kAudioCDSampleRate / 2, 16, | 101 AudioParameters::kAudioCDSampleRate / 2, 16, | 
| 121 frames_100_ms)); | 102 frames_100_ms)); | 
| 122 ASSERT_TRUE(NULL != oas); | 103 ASSERT_TRUE(NULL != oas); | 
| 123 | 104 | 
| 124 SineWaveAudioSource source(SineWaveAudioSource::FORMAT_16BIT_LINEAR_PCM, 1, | 105 SineWaveAudioSource source(1, 200.0, AudioParameters::kAudioCDSampleRate/2); | 
| 125 200.0, AudioParameters::kAudioCDSampleRate/2); | |
| 126 EXPECT_TRUE(oas->Open()); | 106 EXPECT_TRUE(oas->Open()); | 
| 127 oas->Start(&source); | 107 oas->Start(&source); | 
| 128 usleep(1500000); | 108 usleep(1500000); | 
| 129 oas->Stop(); | 109 oas->Stop(); | 
| 130 oas->Close(); | 110 oas->Close(); | 
| 131 } | 111 } | 
| 132 | 112 | 
| 133 // Custom action to clear a memory buffer. | 113 // Custom action to clear a memory buffer. | 
| 134 static void ClearBuffer(uint8* dest, | 114 static int ClearBuffer(AudioBus* audio_bus, | 
| 135 uint32 max_size, | 115 AudioBuffersState buffers_state) { | 
| 136 AudioBuffersState buffers_state) { | 116 audio_bus->Zero(); | 
| 137 memset(dest, 0, max_size); | 117 return audio_bus->frames(); | 
| 138 } | 118 } | 
| 139 | 119 | 
| 140 TEST(MacAudioTest, PCMWaveStreamPendingBytes) { | 120 TEST(MacAudioTest, PCMWaveStreamPendingBytes) { | 
| 141 scoped_ptr<AudioManager> audio_man(AudioManager::Create()); | 121 scoped_ptr<AudioManager> audio_man(AudioManager::Create()); | 
| 142 if (!audio_man->HasAudioOutputDevices()) | 122 if (!audio_man->HasAudioOutputDevices()) | 
| 143 return; | 123 return; | 
| 144 | 124 | 
| 145 uint32 frames_100_ms = AudioParameters::kAudioCDSampleRate / 10; | 125 uint32 frames_100_ms = AudioParameters::kAudioCDSampleRate / 10; | 
| 146 AudioOutputStream* oas = audio_man->MakeAudioOutputStream( | 126 AudioOutputStream* oas = audio_man->MakeAudioOutputStream( | 
| 147 AudioParameters(AudioParameters::AUDIO_PCM_LINEAR, CHANNEL_LAYOUT_MONO, | 127 AudioParameters(AudioParameters::AUDIO_PCM_LINEAR, CHANNEL_LAYOUT_MONO, | 
| 148 AudioParameters::kAudioCDSampleRate, 16, frames_100_ms)); | 128 AudioParameters::kAudioCDSampleRate, 16, frames_100_ms)); | 
| 149 ASSERT_TRUE(NULL != oas); | 129 ASSERT_TRUE(NULL != oas); | 
| 150 | 130 | 
| 151 NiceMock<MockAudioSource> source; | 131 NiceMock<MockAudioSource> source; | 
| 152 EXPECT_TRUE(oas->Open()); | 132 EXPECT_TRUE(oas->Open()); | 
| 153 | 133 | 
| 154 uint32 bytes_100_ms = frames_100_ms * 2; | 134 uint32 bytes_100_ms = frames_100_ms * 2; | 
| 155 | 135 | 
| 156 // We expect the amount of pending bytes will reaching |bytes_100_ms| | 136 // We expect the amount of pending bytes will reaching |bytes_100_ms| | 
| 157 // because the audio output stream has a double buffer scheme. | 137 // because the audio output stream has a double buffer scheme. | 
| 158 // And then we will try to provide zero data so the amount of pending bytes | 138 // And then we will try to provide zero data so the amount of pending bytes | 
| 159 // will go down and eventually read zero. | 139 // will go down and eventually read zero. | 
| 160 InSequence s; | 140 InSequence s; | 
| 161 EXPECT_CALL(source, OnMoreData(NotNull(), bytes_100_ms, | 141 EXPECT_CALL(source, OnMoreData(NotNull(), | 
| 162 Field(&AudioBuffersState::pending_bytes, 0))) | 142 Field(&AudioBuffersState::pending_bytes, 0))) | 
| 163 .WillOnce(DoAll(Invoke(&ClearBuffer), Return(bytes_100_ms))); | 143 .WillOnce(Invoke(ClearBuffer)); | 
| 164 EXPECT_CALL(source, OnMoreData(NotNull(), bytes_100_ms, | 144 EXPECT_CALL(source, OnMoreData(NotNull(), | 
| 165 Field(&AudioBuffersState::pending_bytes, | 145 Field(&AudioBuffersState::pending_bytes, | 
| 166 bytes_100_ms))) | 146 bytes_100_ms))) | 
| 167 .WillOnce(DoAll(Invoke(&ClearBuffer), Return(bytes_100_ms))); | 147 .WillOnce(Invoke(ClearBuffer)); | 
| 168 EXPECT_CALL(source, OnMoreData(NotNull(), bytes_100_ms, | 148 EXPECT_CALL(source, OnMoreData(NotNull(), | 
| 169 Field(&AudioBuffersState::pending_bytes, | 149 Field(&AudioBuffersState::pending_bytes, | 
| 170 bytes_100_ms))) | 150 bytes_100_ms))) | 
| 171 .WillOnce(Return(0)); | 151 .WillOnce(Return(0)); | 
| 172 EXPECT_CALL(source, OnMoreData(NotNull(), bytes_100_ms, _)) | 152 EXPECT_CALL(source, OnMoreData(NotNull(), _)) | 
| 173 .Times(AnyNumber()) | 153 .Times(AnyNumber()) | 
| 174 .WillRepeatedly(Return(0)); | 154 .WillRepeatedly(Return(0)); | 
| 175 | 155 | 
| 176 oas->Start(&source); | 156 oas->Start(&source); | 
| 177 usleep(500000); | 157 usleep(500000); | 
| 178 oas->Stop(); | 158 oas->Stop(); | 
| 179 oas->Close(); | 159 oas->Close(); | 
| 180 } | 160 } | 
| 181 | 161 | 
| 182 } // namespace media | 162 } // namespace media | 
| OLD | NEW |