| OLD | NEW |
| (Empty) | |
| 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. |
| 4 |
| 5 #include <unistd.h> |
| 6 |
| 7 #include <string> |
| 8 |
| 9 #include "media/audio/linux/audio_manager_linux.h" |
| 10 #include "media/audio/linux/cras_input.h" |
| 11 #include "testing/gmock/include/gmock/gmock.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 |
| 14 using testing::_; |
| 15 using testing::AtLeast; |
| 16 using testing::Ge; |
| 17 using testing::StrictMock; |
| 18 |
| 19 namespace media { |
| 20 |
| 21 class MockAudioInputCallback : public AudioInputStream::AudioInputCallback { |
| 22 public: |
| 23 MOCK_METHOD5(OnData, void(AudioInputStream* stream, const uint8* src, |
| 24 uint32 size, uint32 hardware_delay_bytes, |
| 25 double volume)); |
| 26 MOCK_METHOD2(OnError, void(AudioInputStream* stream, int code)); |
| 27 MOCK_METHOD1(OnClose, void(AudioInputStream* stream)); |
| 28 }; |
| 29 |
| 30 class MockAudioManagerLinuxInput : public AudioManagerLinux { |
| 31 public: |
| 32 MOCK_METHOD0(Init, void()); |
| 33 MOCK_METHOD0(HasAudioOutputDevices, bool()); |
| 34 MOCK_METHOD0(HasAudioInputDevices, bool()); |
| 35 MOCK_METHOD0(MuteAll, void()); |
| 36 MOCK_METHOD0(UnMuteAll, void()); |
| 37 MOCK_METHOD1(MakeLinearInputStream, AudioInputStream*( |
| 38 const AudioParameters& params)); |
| 39 MOCK_METHOD1(MakeLowLatencyInputStream, AudioInputStream*( |
| 40 const AudioParameters& params)); |
| 41 MOCK_METHOD2(MakeLinearInputStream, AudioInputStream*( |
| 42 const AudioParameters& params, const std::string& device_id)); |
| 43 MOCK_METHOD2(MakeLowLatencyInputStream, AudioInputStream*( |
| 44 const AudioParameters& params, const std::string& device_id)); |
| 45 MOCK_METHOD0(GetMessageLoop, scoped_refptr<base::MessageLoopProxy>()); |
| 46 |
| 47 // We need to override this function in order to skip the checking the number |
| 48 // of active output streams. It is because the number of active streams |
| 49 // is managed inside MakeAudioInputStream, and we don't use |
| 50 // MakeAudioInputStream to create the stream in the tests. |
| 51 virtual void ReleaseInputStream(AudioInputStream* stream) OVERRIDE { |
| 52 DCHECK(stream); |
| 53 delete stream; |
| 54 } |
| 55 }; |
| 56 |
| 57 class CrasInputStreamTest : public testing::Test { |
| 58 protected: |
| 59 CrasInputStreamTest() { |
| 60 mock_manager_.reset(new StrictMock<MockAudioManagerLinuxInput>()); |
| 61 } |
| 62 |
| 63 virtual ~CrasInputStreamTest() { |
| 64 } |
| 65 |
| 66 CrasInputStream* CreateStream(ChannelLayout layout) { |
| 67 return CreateStream(layout, kTestFramesPerPacket); |
| 68 } |
| 69 |
| 70 CrasInputStream* CreateStream(ChannelLayout layout, |
| 71 int32 samples_per_packet) { |
| 72 AudioParameters params(kTestFormat, |
| 73 layout, |
| 74 kTestSampleRate, |
| 75 kTestBitsPerSample, |
| 76 samples_per_packet); |
| 77 return new CrasInputStream(params, mock_manager_.get()); |
| 78 } |
| 79 |
| 80 void CaptureSomeFrames(const AudioParameters ¶ms, |
| 81 unsigned int duration_ms) { |
| 82 CrasInputStream* test_stream = new CrasInputStream(params, |
| 83 mock_manager_.get()); |
| 84 |
| 85 ASSERT_TRUE(test_stream->Open()); |
| 86 |
| 87 // Render Callback allow 8 frames variance for SRC |
| 88 MockAudioInputCallback mock_callback; |
| 89 unsigned int expected_size = (kTestFramesPerPacket - 8) * |
| 90 params.channels() * |
| 91 params.bits_per_sample() / 8; |
| 92 EXPECT_CALL(mock_callback, |
| 93 OnData(test_stream, _, Ge(expected_size), _, _)) |
| 94 .Times(AtLeast(1)); |
| 95 |
| 96 test_stream->Start(&mock_callback); |
| 97 |
| 98 usleep(duration_ms * 1000); |
| 99 |
| 100 test_stream->Stop(); |
| 101 |
| 102 EXPECT_CALL(mock_callback, OnClose(test_stream)).Times(1); |
| 103 test_stream->Close(); |
| 104 } |
| 105 |
| 106 static const unsigned int kTestBitsPerSample; |
| 107 static const unsigned int kTestCaptureDurationMs; |
| 108 static const ChannelLayout kTestChannelLayout; |
| 109 static const AudioParameters::Format kTestFormat; |
| 110 static const uint32 kTestFramesPerPacket; |
| 111 static const int kTestSampleRate; |
| 112 |
| 113 scoped_ptr<StrictMock<MockAudioManagerLinuxInput> > mock_manager_; |
| 114 |
| 115 private: |
| 116 DISALLOW_COPY_AND_ASSIGN(CrasInputStreamTest); |
| 117 }; |
| 118 |
| 119 const unsigned int CrasInputStreamTest::kTestBitsPerSample = 16; |
| 120 const unsigned int CrasInputStreamTest::kTestCaptureDurationMs = 250; |
| 121 const ChannelLayout CrasInputStreamTest::kTestChannelLayout = |
| 122 CHANNEL_LAYOUT_STEREO; |
| 123 const AudioParameters::Format CrasInputStreamTest::kTestFormat = |
| 124 AudioParameters::AUDIO_PCM_LINEAR; |
| 125 const uint32 CrasInputStreamTest::kTestFramesPerPacket = 1000; |
| 126 const int CrasInputStreamTest::kTestSampleRate = 44100; |
| 127 |
| 128 TEST_F(CrasInputStreamTest, OpenMono) { |
| 129 CrasInputStream* test_stream = CreateStream(CHANNEL_LAYOUT_MONO); |
| 130 EXPECT_TRUE(test_stream->Open()); |
| 131 test_stream->Close(); |
| 132 } |
| 133 |
| 134 TEST_F(CrasInputStreamTest, OpenStereo) { |
| 135 CrasInputStream* test_stream = CreateStream(CHANNEL_LAYOUT_STEREO); |
| 136 EXPECT_TRUE(test_stream->Open()); |
| 137 test_stream->Close(); |
| 138 } |
| 139 |
| 140 TEST_F(CrasInputStreamTest, BadBitsPerSample) { |
| 141 AudioParameters bad_bps_params(kTestFormat, |
| 142 kTestChannelLayout, |
| 143 kTestSampleRate, |
| 144 kTestBitsPerSample - 1, |
| 145 kTestFramesPerPacket); |
| 146 CrasInputStream* test_stream = |
| 147 new CrasInputStream(bad_bps_params, mock_manager_.get()); |
| 148 EXPECT_FALSE(test_stream->Open()); |
| 149 test_stream->Close(); |
| 150 } |
| 151 |
| 152 TEST_F(CrasInputStreamTest, BadFormat) { |
| 153 AudioParameters bad_format_params(AudioParameters::AUDIO_LAST_FORMAT, |
| 154 kTestChannelLayout, |
| 155 kTestSampleRate, |
| 156 kTestBitsPerSample, |
| 157 kTestFramesPerPacket); |
| 158 CrasInputStream* test_stream = |
| 159 new CrasInputStream(bad_format_params, mock_manager_.get()); |
| 160 EXPECT_FALSE(test_stream->Open()); |
| 161 test_stream->Close(); |
| 162 } |
| 163 |
| 164 TEST_F(CrasInputStreamTest, BadSampleRate) { |
| 165 AudioParameters bad_rate_params(kTestFormat, |
| 166 kTestChannelLayout, |
| 167 0, |
| 168 kTestBitsPerSample, |
| 169 kTestFramesPerPacket); |
| 170 CrasInputStream* test_stream = |
| 171 new CrasInputStream(bad_rate_params, mock_manager_.get()); |
| 172 EXPECT_FALSE(test_stream->Open()); |
| 173 test_stream->Close(); |
| 174 } |
| 175 |
| 176 TEST_F(CrasInputStreamTest, CaptureFrames) { |
| 177 const unsigned int rates[] = |
| 178 {8000, 16000, 22050, 32000, 44100, 48000, 96000, 192000}; |
| 179 |
| 180 for (unsigned int i = 0; i < ARRAY_SIZE(rates); i++) { |
| 181 AudioParameters params_mono(kTestFormat, |
| 182 CHANNEL_LAYOUT_MONO, |
| 183 rates[i], |
| 184 kTestBitsPerSample, |
| 185 kTestFramesPerPacket); |
| 186 CaptureSomeFrames(params_mono, kTestCaptureDurationMs); |
| 187 |
| 188 AudioParameters params_stereo(kTestFormat, |
| 189 CHANNEL_LAYOUT_STEREO, |
| 190 rates[i], |
| 191 kTestBitsPerSample, |
| 192 kTestFramesPerPacket); |
| 193 CaptureSomeFrames(params_stereo, kTestCaptureDurationMs); |
| 194 } |
| 195 } |
| 196 |
| 197 } // namespace media |
| OLD | NEW |