Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(485)

Side by Side Diff: media/audio/mac/audio_output_mac_unittest.cc

Issue 4661001: Simplified AudioOutputStream interface. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: - Created 10 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 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 "media/audio/audio_io.h" 6 #include "media/audio/audio_io.h"
7 #include "media/audio/audio_manager.h" 7 #include "media/audio/audio_manager.h"
8 #include "media/audio/simple_sources.h" 8 #include "media/audio/simple_sources.h"
9 #include "testing/gmock/include/gmock/gmock.h" 9 #include "testing/gmock/include/gmock/gmock.h"
10 #include "testing/gtest/include/gtest/gtest.h" 10 #include "testing/gtest/include/gtest/gtest.h"
11 11
12 using ::testing::_; 12 using ::testing::_;
13 using ::testing::AnyNumber; 13 using ::testing::AnyNumber;
14 using ::testing::DoAll; 14 using ::testing::DoAll;
15 using ::testing::Field; 15 using ::testing::Field;
16 using ::testing::InSequence; 16 using ::testing::InSequence;
17 using ::testing::Invoke; 17 using ::testing::Invoke;
18 using ::testing::NiceMock; 18 using ::testing::NiceMock;
19 using ::testing::NotNull; 19 using ::testing::NotNull;
20 using ::testing::Return; 20 using ::testing::Return;
21 21
22 class MockAudioSource : public AudioOutputStream::AudioSourceCallback { 22 class MockAudioSource : public AudioOutputStream::AudioSourceCallback {
23 public: 23 public:
24 MOCK_METHOD4(OnMoreData, uint32(AudioOutputStream* stream, uint8* dest, 24 MOCK_METHOD4(OnMoreData, uint32(AudioOutputStream* stream, uint8* dest,
25 uint32 max_size, 25 uint32 max_size,
26 AudioBuffersState buffers_state)); 26 AudioBuffersState buffers_state));
27 MOCK_METHOD1(OnClose, void(AudioOutputStream* stream));
28 MOCK_METHOD2(OnError, void(AudioOutputStream* stream, int code)); 27 MOCK_METHOD2(OnError, void(AudioOutputStream* stream, int code));
29 }; 28 };
30 29
31 // Validate that the SineWaveAudioSource writes the expected values for 30 // Validate that the SineWaveAudioSource writes the expected values for
32 // the FORMAT_16BIT_MONO. 31 // the FORMAT_16BIT_MONO.
33 TEST(MacAudioTest, SineWaveAudio16MonoTest) { 32 TEST(MacAudioTest, SineWaveAudio16MonoTest) {
34 const uint32 samples = 1024; 33 const uint32 samples = 1024;
35 const int freq = 200; 34 const int freq = 200;
36 35
37 SineWaveAudioSource source(SineWaveAudioSource::FORMAT_16BIT_LINEAR_PCM, 1, 36 SineWaveAudioSource source(SineWaveAudioSource::FORMAT_16BIT_LINEAR_PCM, 1,
(...skipping 13 matching lines...) Expand all
51 // Unlike windows, the tests can reliably detect the existense of real 50 // Unlike windows, the tests can reliably detect the existense of real
52 // audio devices on the bots thus no need for 'headless' detection. 51 // audio devices on the bots thus no need for 'headless' detection.
53 52
54 // Test that can it be created and closed. 53 // Test that can it be created and closed.
55 TEST(MacAudioTest, PCMWaveStreamGetAndClose) { 54 TEST(MacAudioTest, PCMWaveStreamGetAndClose) {
56 AudioManager* audio_man = AudioManager::GetAudioManager(); 55 AudioManager* audio_man = AudioManager::GetAudioManager();
57 ASSERT_TRUE(NULL != audio_man); 56 ASSERT_TRUE(NULL != audio_man);
58 if (!audio_man->HasAudioOutputDevices()) 57 if (!audio_man->HasAudioOutputDevices())
59 return; 58 return;
60 AudioOutputStream* oas = audio_man->MakeAudioOutputStream( 59 AudioOutputStream* oas = audio_man->MakeAudioOutputStream(
61 AudioParameters(AudioParameters::AUDIO_PCM_LINEAR, 2, 8000, 16)); 60 AudioParameters(AudioParameters::AUDIO_PCM_LINEAR, 2, 8000, 16, 1024));
62 ASSERT_TRUE(NULL != oas); 61 ASSERT_TRUE(NULL != oas);
63 oas->Close(); 62 oas->Close();
64 } 63 }
65 64
66 // Test that it can be opened and closed. 65 // Test that it can be opened and closed.
67 TEST(MacAudioTest, PCMWaveStreamOpenAndClose) { 66 TEST(MacAudioTest, PCMWaveStreamOpenAndClose) {
68 AudioManager* audio_man = AudioManager::GetAudioManager(); 67 AudioManager* audio_man = AudioManager::GetAudioManager();
69 ASSERT_TRUE(NULL != audio_man); 68 ASSERT_TRUE(NULL != audio_man);
70 if (!audio_man->HasAudioOutputDevices()) 69 if (!audio_man->HasAudioOutputDevices())
71 return; 70 return;
72 AudioOutputStream* oas = audio_man->MakeAudioOutputStream( 71 AudioOutputStream* oas = audio_man->MakeAudioOutputStream(
73 AudioParameters(AudioParameters::AUDIO_PCM_LINEAR, 2, 8000, 16)); 72 AudioParameters(AudioParameters::AUDIO_PCM_LINEAR, 2, 8000, 16, 1024));
74 ASSERT_TRUE(NULL != oas); 73 ASSERT_TRUE(NULL != oas);
75 EXPECT_TRUE(oas->Open(1024)); 74 EXPECT_TRUE(oas->Open());
76 oas->Close(); 75 oas->Close();
77 } 76 }
78 77
79 // This test produces actual audio for 1.5 seconds on the default wave device at 78 // This test produces actual audio for 1.5 seconds on the default wave device at
80 // 44.1K s/sec. Parameters have been chosen carefully so you should not hear 79 // 44.1K s/sec. Parameters have been chosen carefully so you should not hear
81 // pops or noises while the sound is playing. The sound must also be identical 80 // pops or noises while the sound is playing. The sound must also be identical
82 // to the sound of PCMWaveStreamPlay200HzTone22KssMono test. 81 // to the sound of PCMWaveStreamPlay200HzTone22KssMono test.
83 TEST(MacAudioTest, PCMWaveStreamPlay200HzTone44KssMono) { 82 TEST(MacAudioTest, PCMWaveStreamPlay200HzTone44KssMono) {
84 AudioManager* audio_man = AudioManager::GetAudioManager(); 83 AudioManager* audio_man = AudioManager::GetAudioManager();
85 ASSERT_TRUE(NULL != audio_man); 84 ASSERT_TRUE(NULL != audio_man);
86 if (!audio_man->HasAudioOutputDevices()) 85 if (!audio_man->HasAudioOutputDevices())
87 return; 86 return;
87 uint32 frames_100_ms = AudioParameters::kAudioCDSampleRate / 10;
88 AudioOutputStream* oas = audio_man->MakeAudioOutputStream( 88 AudioOutputStream* oas = audio_man->MakeAudioOutputStream(
89 AudioParameters(AudioParameters::AUDIO_PCM_LINEAR, 1, 89 AudioParameters(AudioParameters::AUDIO_PCM_LINEAR, 1,
90 AudioParameters::kAudioCDSampleRate, 16)); 90 AudioParameters::kAudioCDSampleRate, 16, frames_100_ms));
91 ASSERT_TRUE(NULL != oas); 91 ASSERT_TRUE(NULL != oas);
92 EXPECT_TRUE(oas->Open());
92 93
93 SineWaveAudioSource source(SineWaveAudioSource::FORMAT_16BIT_LINEAR_PCM, 1, 94 SineWaveAudioSource source(SineWaveAudioSource::FORMAT_16BIT_LINEAR_PCM, 1,
94 200.0, AudioParameters::kAudioCDSampleRate); 95 200.0, AudioParameters::kAudioCDSampleRate);
95 uint32 bytes_100_ms = (AudioParameters::kAudioCDSampleRate / 10) * 2;
96
97 EXPECT_TRUE(oas->Open(bytes_100_ms));
98
99 oas->SetVolume(0.5); 96 oas->SetVolume(0.5);
100 oas->Start(&source); 97 oas->Start(&source);
101 usleep(500000); 98 usleep(500000);
102 99
103 // Test that the volume is within the set limits. 100 // Test that the volume is within the set limits.
104 double volume = 0.0; 101 double volume = 0.0;
105 oas->GetVolume(&volume); 102 oas->GetVolume(&volume);
106 EXPECT_LT(volume, 0.51); 103 EXPECT_LT(volume, 0.51);
107 EXPECT_GT(volume, 0.49); 104 EXPECT_GT(volume, 0.49);
108 oas->Stop(); 105 oas->Stop();
109 oas->Close(); 106 oas->Close();
110 } 107 }
111 108
112 // This test produces actual audio for 1.5 seconds on the default wave device at 109 // This test produces actual audio for 1.5 seconds on the default wave device at
113 // 22K s/sec. Parameters have been chosen carefully so you should not hear pops 110 // 22K s/sec. Parameters have been chosen carefully so you should not hear pops
114 // or noises while the sound is playing. The sound must also be identical to the 111 // or noises while the sound is playing. The sound must also be identical to the
115 // sound of PCMWaveStreamPlay200HzTone44KssMono test. 112 // sound of PCMWaveStreamPlay200HzTone44KssMono test.
116 TEST(MacAudioTest, PCMWaveStreamPlay200HzTone22KssMono) { 113 TEST(MacAudioTest, PCMWaveStreamPlay200HzTone22KssMono) {
117 AudioManager* audio_man = AudioManager::GetAudioManager(); 114 AudioManager* audio_man = AudioManager::GetAudioManager();
118 ASSERT_TRUE(NULL != audio_man); 115 ASSERT_TRUE(NULL != audio_man);
119 if (!audio_man->HasAudioOutputDevices()) 116 if (!audio_man->HasAudioOutputDevices())
120 return; 117 return;
118 uint32 frames_100_ms = AudioParameters::kAudioCDSampleRate / 10;
121 AudioOutputStream* oas = audio_man->MakeAudioOutputStream( 119 AudioOutputStream* oas = audio_man->MakeAudioOutputStream(
122 AudioParameters(AudioParameters::AUDIO_PCM_LINEAR, 1, 120 AudioParameters(AudioParameters::AUDIO_PCM_LINEAR, 1,
123 AudioParameters::kAudioCDSampleRate / 2, 16)); 121 AudioParameters::kAudioCDSampleRate / 2, 16,
122 frames_100_ms));
124 ASSERT_TRUE(NULL != oas); 123 ASSERT_TRUE(NULL != oas);
125 124
126 SineWaveAudioSource source(SineWaveAudioSource::FORMAT_16BIT_LINEAR_PCM, 1, 125 SineWaveAudioSource source(SineWaveAudioSource::FORMAT_16BIT_LINEAR_PCM, 1,
127 200.0, AudioParameters::kAudioCDSampleRate/2); 126 200.0, AudioParameters::kAudioCDSampleRate/2);
128 uint32 bytes_100_ms = (AudioParameters::kAudioCDSampleRate / 20) * 2; 127 EXPECT_TRUE(oas->Open());
129
130 EXPECT_TRUE(oas->Open(bytes_100_ms));
131 oas->Start(&source); 128 oas->Start(&source);
132 usleep(1500000); 129 usleep(1500000);
133 oas->Stop(); 130 oas->Stop();
134 oas->Close(); 131 oas->Close();
135 } 132 }
136 133
137 // Custom action to clear a memory buffer. 134 // Custom action to clear a memory buffer.
138 static void ClearBuffer(AudioOutputStream* stream, uint8* dest, 135 static void ClearBuffer(AudioOutputStream* stream, uint8* dest,
139 uint32 max_size, AudioBuffersState buffers_state) { 136 uint32 max_size, AudioBuffersState buffers_state) {
140 memset(dest, 0, max_size); 137 memset(dest, 0, max_size);
141 } 138 }
142 139
143 TEST(MacAudioTest, PCMWaveStreamPendingBytes) { 140 TEST(MacAudioTest, PCMWaveStreamPendingBytes) {
144 AudioManager* audio_man = AudioManager::GetAudioManager(); 141 AudioManager* audio_man = AudioManager::GetAudioManager();
145 ASSERT_TRUE(NULL != audio_man); 142 ASSERT_TRUE(NULL != audio_man);
146 if (!audio_man->HasAudioOutputDevices()) 143 if (!audio_man->HasAudioOutputDevices())
147 return; 144 return;
145
146 uint32 frames_100_ms = AudioParameters::kAudioCDSampleRate / 10;
148 AudioOutputStream* oas = audio_man->MakeAudioOutputStream( 147 AudioOutputStream* oas = audio_man->MakeAudioOutputStream(
149 AudioParameters(AudioParameters::AUDIO_PCM_LINEAR, 1, 148 AudioParameters(AudioParameters::AUDIO_PCM_LINEAR, 1,
150 AudioParameters::kAudioCDSampleRate, 16)); 149 AudioParameters::kAudioCDSampleRate, 16, frames_100_ms));
151 ASSERT_TRUE(NULL != oas); 150 ASSERT_TRUE(NULL != oas);
152 151
153 NiceMock<MockAudioSource> source; 152 NiceMock<MockAudioSource> source;
154 uint32 bytes_100_ms = (AudioParameters::kAudioCDSampleRate / 10) * 2; 153 EXPECT_TRUE(oas->Open());
155 EXPECT_TRUE(oas->Open(bytes_100_ms)); 154
155 uint32 bytes_100_ms = frames_100_ms * 2;
156 156
157 // We expect the amount of pending bytes will reaching |bytes_100_ms| 157 // We expect the amount of pending bytes will reaching |bytes_100_ms|
158 // because the audio output stream has a double buffer scheme. 158 // because the audio output stream has a double buffer scheme.
159 // And then we will try to provide zero data so the amount of pending bytes 159 // And then we will try to provide zero data so the amount of pending bytes
160 // will go down and eventually read zero. 160 // will go down and eventually read zero.
161 InSequence s; 161 InSequence s;
162 EXPECT_CALL(source, OnMoreData(oas, NotNull(), bytes_100_ms, 162 EXPECT_CALL(source, OnMoreData(oas, NotNull(), bytes_100_ms,
163 Field(&AudioBuffersState::pending_bytes, 0))) 163 Field(&AudioBuffersState::pending_bytes, 0)))
164 .WillOnce(DoAll(Invoke(&ClearBuffer), Return(bytes_100_ms))); 164 .WillOnce(DoAll(Invoke(&ClearBuffer), Return(bytes_100_ms)));
165 EXPECT_CALL(source, OnMoreData(oas, NotNull(), bytes_100_ms, 165 EXPECT_CALL(source, OnMoreData(oas, NotNull(), bytes_100_ms,
166 Field(&AudioBuffersState::pending_bytes, 166 Field(&AudioBuffersState::pending_bytes,
167 bytes_100_ms))) 167 bytes_100_ms)))
168 .WillOnce(DoAll(Invoke(&ClearBuffer), Return(bytes_100_ms))); 168 .WillOnce(DoAll(Invoke(&ClearBuffer), Return(bytes_100_ms)));
169 EXPECT_CALL(source, OnMoreData(oas, NotNull(), bytes_100_ms, 169 EXPECT_CALL(source, OnMoreData(oas, NotNull(), bytes_100_ms,
170 Field(&AudioBuffersState::pending_bytes, 170 Field(&AudioBuffersState::pending_bytes,
171 bytes_100_ms))) 171 bytes_100_ms)))
172 .WillOnce(Return(0)); 172 .WillOnce(Return(0));
173 EXPECT_CALL(source, OnMoreData(oas, NotNull(), bytes_100_ms, _)) 173 EXPECT_CALL(source, OnMoreData(oas, NotNull(), bytes_100_ms, _))
174 .Times(AnyNumber()) 174 .Times(AnyNumber())
175 .WillRepeatedly(Return(0)); 175 .WillRepeatedly(Return(0));
176 176
177 oas->Start(&source); 177 oas->Start(&source);
178 usleep(500000); 178 usleep(500000);
179 oas->Stop(); 179 oas->Stop();
180 oas->Close(); 180 oas->Close();
181 } 181 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698