1 // Copyright 2013 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.
5 #include "base/basictypes.h"
6 #include "base/memory/scoped_ptr.h"
7 #include "media/audio/audio_io.h"
8 #include "media/audio/audio_manager.h"
9 #include "media/audio/simple_sources.h"
10 #include "testing/gmock/include/gmock/gmock.h"
11 #include "testing/gtest/include/gtest/gtest.h"
14 using ::testing::AnyNumber
;
15 using ::testing::DoAll
;
16 using ::testing::Field
;
17 using ::testing::InSequence
;
18 using ::testing::Invoke
;
19 using ::testing::NiceMock
;
20 using ::testing::NotNull
;
21 using ::testing::Return
;
23 static const int kBitsPerSample
= 16;
25 // TODO(crogers): Most of these tests can be made platform agnostic.
26 // http://crbug.com/223242
30 class MockAudioSourceCallback
: public AudioOutputStream::AudioSourceCallback
{
32 MOCK_METHOD2(OnMoreData
, int(AudioBus
* audio_bus
,
33 AudioBuffersState buffers_state
));
34 MOCK_METHOD3(OnMoreIOData
, int(AudioBus
* source
,
36 AudioBuffersState buffers_state
));
37 MOCK_METHOD1(OnError
, void(AudioOutputStream
* stream
));
40 // Convenience method which creates a default AudioOutputStream object but
41 // also allows the user to modify the default settings.
42 class AudioOutputStreamWrapper
{
44 explicit AudioOutputStreamWrapper()
45 : audio_man_(AudioManager::Create()),
46 format_(AudioParameters::AUDIO_PCM_LOW_LATENCY
),
47 bits_per_sample_(kBitsPerSample
) {
48 AudioParameters preferred_params
=
49 audio_man_
->GetDefaultOutputStreamParameters();
50 channel_layout_
= preferred_params
.channel_layout();
51 channels_
= preferred_params
.channels();
52 sample_rate_
= preferred_params
.sample_rate();
53 samples_per_packet_
= preferred_params
.frames_per_buffer();
56 ~AudioOutputStreamWrapper() {}
58 // Creates AudioOutputStream object using default parameters.
59 AudioOutputStream
* Create() {
60 return CreateOutputStream();
63 // Creates AudioOutputStream object using non-default parameters where the
64 // frame size is modified.
65 AudioOutputStream
* Create(int samples_per_packet
) {
66 samples_per_packet_
= samples_per_packet
;
67 return CreateOutputStream();
70 // Creates AudioOutputStream object using non-default parameters where the
71 // sample rate is modified.
72 AudioOutputStream
* CreateWithSampleRate(int sample_rate
) {
73 sample_rate_
= sample_rate
;
74 return CreateOutputStream();
77 // Creates AudioOutputStream object using non-default parameters where the
78 // channel layout is modified.
79 AudioOutputStream
* CreateWithLayout(ChannelLayout layout
) {
80 channel_layout_
= layout
;
81 channels_
= ChannelLayoutToChannelCount(layout
);
82 return CreateOutputStream();
85 AudioParameters::Format
format() const { return format_
; }
86 int channels() const { return ChannelLayoutToChannelCount(channel_layout_
); }
87 int bits_per_sample() const { return bits_per_sample_
; }
88 int sample_rate() const { return sample_rate_
; }
89 int samples_per_packet() const { return samples_per_packet_
; }
91 bool CanRunAudioTests() {
92 return audio_man_
->HasAudioOutputDevices();
96 AudioOutputStream
* CreateOutputStream() {
97 AudioParameters params
;
98 params
.Reset(format_
, channel_layout_
,
100 sample_rate_
, bits_per_sample_
,
101 samples_per_packet_
);
103 AudioOutputStream
* aos
= audio_man_
->MakeAudioOutputStream(params
);
108 scoped_ptr
<AudioManager
> audio_man_
;
110 AudioParameters::Format format_
;
111 ChannelLayout channel_layout_
;
113 int bits_per_sample_
;
115 int samples_per_packet_
;
118 // Test that we can get the hardware sample-rate.
119 TEST(AUHALStreamTest
, HardwareSampleRate
) {
120 AudioOutputStreamWrapper aosw
;
121 if (!aosw
.CanRunAudioTests())
124 int sample_rate
= aosw
.sample_rate();
125 EXPECT_GE(sample_rate
, 16000);
126 EXPECT_LE(sample_rate
, 192000);
129 // Test Create(), Close() calling sequence.
130 TEST(AUHALStreamTest
, CreateAndClose
) {
131 AudioOutputStreamWrapper aosw
;
132 if (!aosw
.CanRunAudioTests())
135 AudioOutputStream
* aos
= aosw
.Create();
139 // Test Open(), Close() calling sequence.
140 TEST(AUHALStreamTest
, OpenAndClose
) {
141 AudioOutputStreamWrapper aosw
;
142 if (!aosw
.CanRunAudioTests())
145 AudioOutputStream
* aos
= aosw
.Create();
146 EXPECT_TRUE(aos
->Open());
150 // Test Open(), Start(), Close() calling sequence.
151 TEST(AUHALStreamTest
, OpenStartAndClose
) {
152 AudioOutputStreamWrapper aosw
;
153 if (!aosw
.CanRunAudioTests())
156 AudioOutputStream
* aos
= aosw
.Create();
157 EXPECT_TRUE(aos
->Open());
158 MockAudioSourceCallback source
;
159 EXPECT_CALL(source
, OnError(aos
))
165 // Test Open(), Start(), Stop(), Close() calling sequence.
166 TEST(AUHALStreamTest
, OpenStartStopAndClose
) {
167 AudioOutputStreamWrapper aosw
;
168 if (!aosw
.CanRunAudioTests())
171 AudioOutputStream
* aos
= aosw
.Create();
172 EXPECT_TRUE(aos
->Open());
173 MockAudioSourceCallback source
;
174 EXPECT_CALL(source
, OnError(aos
))
181 // This test produces actual audio for 0.5 seconds on the default audio device
182 // at the hardware sample-rate (usually 44.1KHz).
183 // Parameters have been chosen carefully so you should not hear
184 // pops or noises while the sound is playing.
185 TEST(AUHALStreamTest
, AUHALStreamPlay200HzTone
) {
186 AudioOutputStreamWrapper aosw
;
187 if (!aosw
.CanRunAudioTests())
190 AudioOutputStream
* aos
= aosw
.CreateWithLayout(CHANNEL_LAYOUT_MONO
);
192 EXPECT_TRUE(aos
->Open());
194 SineWaveAudioSource
source(1, 200.0, aosw
.sample_rate());
202 // Test that Open() will fail with a sample-rate which isn't the hardware
204 TEST(AUHALStreamTest
, AUHALStreamInvalidSampleRate
) {
205 AudioOutputStreamWrapper aosw
;
206 if (!aosw
.CanRunAudioTests())
209 int non_default_sample_rate
= aosw
.sample_rate() == 44100 ?
211 AudioOutputStream
* aos
= aosw
.CreateWithSampleRate(non_default_sample_rate
);
213 EXPECT_FALSE(aos
->Open());