Mailbox support for texture layers.
[chromium-blink-merge.git] / media / audio / audio_input_unittest.cc
blob5a02323bbe2f9534a7acd909ac751e474e241397
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.
5 #include "base/basictypes.h"
6 #include "base/environment.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "base/message_loop.h"
9 #include "base/threading/platform_thread.h"
10 #include "media/audio/audio_io.h"
11 #include "media/audio/audio_manager_base.h"
12 #include "testing/gtest/include/gtest/gtest.h"
14 namespace media {
16 static const int kSamplingRate = 8000;
17 static const int kSamplesPerPacket = kSamplingRate / 20;
19 // This class allows to find out if the callbacks are occurring as
20 // expected and if any error has been reported.
21 class TestInputCallback : public AudioInputStream::AudioInputCallback {
22 public:
23 explicit TestInputCallback(int max_data_bytes)
24 : callback_count_(0),
25 had_error_(0),
26 max_data_bytes_(max_data_bytes) {
28 virtual void OnData(AudioInputStream* stream, const uint8* data,
29 uint32 size, uint32 hardware_delay_bytes, double volume) {
30 ++callback_count_;
31 // Read the first byte to make sure memory is good.
32 if (size) {
33 ASSERT_LE(static_cast<int>(size), max_data_bytes_);
34 int value = data[0];
35 EXPECT_GE(value, 0);
38 virtual void OnClose(AudioInputStream* stream) {}
39 virtual void OnError(AudioInputStream* stream, int code) {
40 ++had_error_;
42 // Returns how many times OnData() has been called.
43 int callback_count() const {
44 return callback_count_;
46 // Returns how many times the OnError callback was called.
47 int had_error() const {
48 return had_error_;
51 private:
52 int callback_count_;
53 int had_error_;
54 int max_data_bytes_;
57 static bool CanRunAudioTests(AudioManager* audio_man) {
58 bool has_input = audio_man->HasAudioInputDevices();
60 if (!has_input)
61 LOG(WARNING) << "No input devices detected";
63 return has_input;
66 static AudioInputStream* CreateTestAudioInputStream(AudioManager* audio_man) {
67 AudioInputStream* ais = audio_man->MakeAudioInputStream(
68 AudioParameters(AudioParameters::AUDIO_PCM_LINEAR, CHANNEL_LAYOUT_STEREO,
69 kSamplingRate, 16, kSamplesPerPacket),
70 AudioManagerBase::kDefaultDeviceId);
71 EXPECT_TRUE(NULL != ais);
72 return ais;
75 // Test that AudioInputStream rejects out of range parameters.
76 TEST(AudioInputTest, SanityOnMakeParams) {
77 scoped_ptr<AudioManager> audio_man(AudioManager::Create());
78 if (!CanRunAudioTests(audio_man.get()))
79 return;
81 AudioParameters::Format fmt = AudioParameters::AUDIO_PCM_LINEAR;
82 EXPECT_TRUE(NULL == audio_man->MakeAudioInputStream(
83 AudioParameters(fmt, CHANNEL_LAYOUT_7_1, 8000, 16,
84 kSamplesPerPacket), AudioManagerBase::kDefaultDeviceId));
85 EXPECT_TRUE(NULL == audio_man->MakeAudioInputStream(
86 AudioParameters(fmt, CHANNEL_LAYOUT_MONO, 1024 * 1024, 16,
87 kSamplesPerPacket), AudioManagerBase::kDefaultDeviceId));
88 EXPECT_TRUE(NULL == audio_man->MakeAudioInputStream(
89 AudioParameters(fmt, CHANNEL_LAYOUT_STEREO, 8000, 80,
90 kSamplesPerPacket), AudioManagerBase::kDefaultDeviceId));
91 EXPECT_TRUE(NULL == audio_man->MakeAudioInputStream(
92 AudioParameters(fmt, CHANNEL_LAYOUT_STEREO, 8000, 80,
93 1000 * kSamplesPerPacket),
94 AudioManagerBase::kDefaultDeviceId));
95 EXPECT_TRUE(NULL == audio_man->MakeAudioInputStream(
96 AudioParameters(fmt, CHANNEL_LAYOUT_UNSUPPORTED, 8000, 16,
97 kSamplesPerPacket), AudioManagerBase::kDefaultDeviceId));
98 EXPECT_TRUE(NULL == audio_man->MakeAudioInputStream(
99 AudioParameters(fmt, CHANNEL_LAYOUT_STEREO, -8000, 16,
100 kSamplesPerPacket), AudioManagerBase::kDefaultDeviceId));
101 EXPECT_TRUE(NULL == audio_man->MakeAudioInputStream(
102 AudioParameters(fmt, CHANNEL_LAYOUT_STEREO, 8000, -16,
103 kSamplesPerPacket), AudioManagerBase::kDefaultDeviceId));
104 EXPECT_TRUE(NULL == audio_man->MakeAudioInputStream(
105 AudioParameters(fmt, CHANNEL_LAYOUT_STEREO, 8000, 16, -1024),
106 AudioManagerBase::kDefaultDeviceId));
109 // Test create and close of an AudioInputStream without recording audio.
110 TEST(AudioInputTest, CreateAndClose) {
111 scoped_ptr<AudioManager> audio_man(AudioManager::Create());
112 if (!CanRunAudioTests(audio_man.get()))
113 return;
114 AudioInputStream* ais = CreateTestAudioInputStream(audio_man.get());
115 ais->Close();
118 // Test create, open and close of an AudioInputStream without recording audio.
119 TEST(AudioInputTest, OpenAndClose) {
120 scoped_ptr<AudioManager> audio_man(AudioManager::Create());
121 if (!CanRunAudioTests(audio_man.get()))
122 return;
123 AudioInputStream* ais = CreateTestAudioInputStream(audio_man.get());
124 EXPECT_TRUE(ais->Open());
125 ais->Close();
128 // Test create, open, stop and close of an AudioInputStream without recording.
129 TEST(AudioInputTest, OpenStopAndClose) {
130 scoped_ptr<AudioManager> audio_man(AudioManager::Create());
131 if (!CanRunAudioTests(audio_man.get()))
132 return;
133 AudioInputStream* ais = CreateTestAudioInputStream(audio_man.get());
134 EXPECT_TRUE(ais->Open());
135 ais->Stop();
136 ais->Close();
139 // Test a normal recording sequence using an AudioInputStream.
140 TEST(AudioInputTest, Record) {
141 scoped_ptr<AudioManager> audio_man(AudioManager::Create());
142 if (!CanRunAudioTests(audio_man.get()))
143 return;
144 MessageLoop message_loop(MessageLoop::TYPE_DEFAULT);
145 AudioInputStream* ais = CreateTestAudioInputStream(audio_man.get());
146 EXPECT_TRUE(ais->Open());
148 TestInputCallback test_callback(kSamplesPerPacket * 4);
149 ais->Start(&test_callback);
150 // Verify at least 500ms worth of audio was recorded, after giving sufficient
151 // extra time.
152 message_loop.PostDelayedTask(
153 FROM_HERE,
154 MessageLoop::QuitClosure(),
155 base::TimeDelta::FromMilliseconds(690));
156 message_loop.Run();
157 EXPECT_GE(test_callback.callback_count(), 1);
158 EXPECT_FALSE(test_callback.had_error());
160 ais->Stop();
161 ais->Close();
164 } // namespace media