Chromium Code Reviews| 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 "base/time.h" | |
| 10 #include "media/audio/linux/audio_manager_linux.h" | |
| 11 #include "media/audio/linux/cras_input.h" | |
| 12 #include "testing/gmock/include/gmock/gmock.h" | |
| 13 #include "testing/gtest/include/gtest/gtest.h" | |
| 14 | |
| 15 using testing::_; | |
| 16 using testing::AtLeast; | |
| 17 using testing::Ge; | |
| 18 using testing::StrictMock; | |
| 19 | |
| 20 namespace media { | |
| 21 | |
| 22 class MockAudioInputCallback : public AudioInputStream::AudioInputCallback { | |
| 23 public: | |
| 24 MOCK_METHOD5(OnData, void( | |
| 25 AudioInputStream*, const uint8*, uint32, uint32, double)); | |
| 26 MOCK_METHOD2(OnError, void(AudioInputStream*, int)); | |
| 27 MOCK_METHOD1(OnClose, void(AudioInputStream*)); | |
| 28 }; | |
| 29 | |
| 30 class MockAudioManagerLinuxInput : public AudioManagerLinux { | |
| 31 public: | |
| 32 // We need to override this function in order to skip the checking the number | |
|
no longer working on chromium
2012/06/25 15:59:27
skip checking
dgreid
2012/06/25 21:29:28
Done.
| |
| 33 // of active output streams. It is because the number of active streams | |
| 34 // is managed inside MakeAudioInputStream, and we don't use | |
| 35 // MakeAudioInputStream to create the stream in the tests. | |
| 36 virtual void ReleaseInputStream(AudioInputStream* stream) OVERRIDE { | |
| 37 DCHECK(stream); | |
| 38 delete stream; | |
| 39 } | |
| 40 }; | |
| 41 | |
| 42 class CrasInputStreamTest : public testing::Test { | |
| 43 protected: | |
| 44 CrasInputStreamTest() { | |
| 45 mock_manager_.reset(new StrictMock<MockAudioManagerLinuxInput>()); | |
| 46 } | |
| 47 | |
| 48 virtual ~CrasInputStreamTest() { | |
| 49 } | |
| 50 | |
| 51 CrasInputStream* CreateStream(ChannelLayout layout) { | |
| 52 return CreateStream(layout, kTestFramesPerPacket); | |
| 53 } | |
| 54 | |
| 55 CrasInputStream* CreateStream(ChannelLayout layout, | |
| 56 int32 samples_per_packet) { | |
| 57 AudioParameters params(kTestFormat, | |
| 58 layout, | |
| 59 kTestSampleRate, | |
| 60 kTestBitsPerSample, | |
| 61 samples_per_packet); | |
| 62 return new CrasInputStream(params, mock_manager_.get()); | |
|
no longer working on chromium
2012/06/25 15:59:27
I am wondering what is preventing using audio_mana
dgreid
2012/06/25 21:29:28
Isn't that something that belongs in an AudioManag
no longer working on chromium
2012/06/26 21:42:49
We don't have the specific AudioManager unit test,
| |
| 63 } | |
| 64 | |
| 65 void CaptureSomeFrames(const AudioParameters ¶ms, | |
| 66 unsigned int duration_ms) { | |
| 67 CrasInputStream* test_stream = new CrasInputStream(params, | |
|
no longer working on chromium
2012/06/25 15:59:27
Use CreateStream() here?
dgreid
2012/06/25 21:29:28
CreateStream is used to create a stream with the d
no longer working on chromium
2012/06/26 21:42:49
The code looks much cleaner if we only use one way
| |
| 68 mock_manager_.get()); | |
| 69 | |
| 70 ASSERT_TRUE(test_stream->Open()); | |
| 71 | |
| 72 // Allow 8 frames variance for SRC in the callback. Different numbers of | |
| 73 // samples can be provided when doing non-integer SRC. For example | |
| 74 // converting from 192k to 44.1k is a ratio of 4.35 to 1. | |
| 75 MockAudioInputCallback mock_callback; | |
| 76 unsigned int expected_size = (kTestFramesPerPacket - 8) * | |
| 77 params.channels() * | |
| 78 params.bits_per_sample() / 8; | |
| 79 EXPECT_CALL(mock_callback, | |
| 80 OnData(test_stream, _, Ge(expected_size), _, _)) | |
| 81 .Times(AtLeast(1)); | |
| 82 | |
| 83 test_stream->Start(&mock_callback); | |
| 84 | |
| 85 usleep(duration_ms * base::Time::kMillisecondsPerSecond); | |
| 86 | |
| 87 test_stream->Stop(); | |
| 88 | |
| 89 EXPECT_CALL(mock_callback, OnClose(test_stream)).Times(1); | |
| 90 test_stream->Close(); | |
| 91 } | |
| 92 | |
| 93 static const unsigned int kTestBitsPerSample; | |
|
no longer working on chromium
2012/06/25 15:59:27
Isn't it easier to extend the tests if we put thes
dgreid
2012/06/25 21:29:28
These are just meant as defaults, When they need t
| |
| 94 static const unsigned int kTestCaptureDurationMs; | |
| 95 static const ChannelLayout kTestChannelLayout; | |
| 96 static const AudioParameters::Format kTestFormat; | |
| 97 static const uint32 kTestFramesPerPacket; | |
| 98 static const int kTestSampleRate; | |
| 99 | |
| 100 scoped_ptr<StrictMock<MockAudioManagerLinuxInput> > mock_manager_; | |
| 101 | |
| 102 private: | |
| 103 DISALLOW_COPY_AND_ASSIGN(CrasInputStreamTest); | |
| 104 }; | |
| 105 | |
| 106 const unsigned int CrasInputStreamTest::kTestBitsPerSample = 16; | |
| 107 const unsigned int CrasInputStreamTest::kTestCaptureDurationMs = 250; | |
| 108 const ChannelLayout CrasInputStreamTest::kTestChannelLayout = | |
| 109 CHANNEL_LAYOUT_STEREO; | |
| 110 const AudioParameters::Format CrasInputStreamTest::kTestFormat = | |
| 111 AudioParameters::AUDIO_PCM_LINEAR; | |
| 112 const uint32 CrasInputStreamTest::kTestFramesPerPacket = 1000; | |
| 113 const int CrasInputStreamTest::kTestSampleRate = 44100; | |
| 114 | |
| 115 TEST_F(CrasInputStreamTest, OpenMono) { | |
| 116 CrasInputStream* test_stream = CreateStream(CHANNEL_LAYOUT_MONO); | |
| 117 EXPECT_TRUE(test_stream->Open()); | |
| 118 test_stream->Close(); | |
| 119 } | |
| 120 | |
| 121 TEST_F(CrasInputStreamTest, OpenStereo) { | |
| 122 CrasInputStream* test_stream = CreateStream(CHANNEL_LAYOUT_STEREO); | |
| 123 EXPECT_TRUE(test_stream->Open()); | |
| 124 test_stream->Close(); | |
| 125 } | |
| 126 | |
| 127 TEST_F(CrasInputStreamTest, BadBitsPerSample) { | |
| 128 AudioParameters bad_bps_params(kTestFormat, | |
| 129 kTestChannelLayout, | |
| 130 kTestSampleRate, | |
| 131 kTestBitsPerSample - 1, | |
| 132 kTestFramesPerPacket); | |
| 133 CrasInputStream* test_stream = | |
| 134 new CrasInputStream(bad_bps_params, mock_manager_.get()); | |
|
no longer working on chromium
2012/06/25 15:59:27
We can use CreateStream instead.
| |
| 135 EXPECT_FALSE(test_stream->Open()); | |
| 136 test_stream->Close(); | |
| 137 } | |
| 138 | |
| 139 TEST_F(CrasInputStreamTest, BadFormat) { | |
| 140 AudioParameters bad_format_params(AudioParameters::AUDIO_LAST_FORMAT, | |
| 141 kTestChannelLayout, | |
| 142 kTestSampleRate, | |
| 143 kTestBitsPerSample, | |
| 144 kTestFramesPerPacket); | |
| 145 CrasInputStream* test_stream = | |
| 146 new CrasInputStream(bad_format_params, mock_manager_.get()); | |
|
no longer working on chromium
2012/06/25 15:59:27
ditto
| |
| 147 EXPECT_FALSE(test_stream->Open()); | |
| 148 test_stream->Close(); | |
| 149 } | |
| 150 | |
| 151 TEST_F(CrasInputStreamTest, BadSampleRate) { | |
| 152 AudioParameters bad_rate_params(kTestFormat, | |
| 153 kTestChannelLayout, | |
| 154 0, | |
| 155 kTestBitsPerSample, | |
| 156 kTestFramesPerPacket); | |
| 157 CrasInputStream* test_stream = | |
| 158 new CrasInputStream(bad_rate_params, mock_manager_.get()); | |
| 159 EXPECT_FALSE(test_stream->Open()); | |
| 160 test_stream->Close(); | |
| 161 } | |
| 162 | |
| 163 TEST_F(CrasInputStreamTest, CaptureFrames) { | |
| 164 const unsigned int rates[] = | |
| 165 {8000, 16000, 22050, 32000, 44100, 48000, 96000, 192000}; | |
| 166 | |
| 167 for (unsigned int i = 0; i < ARRAY_SIZE(rates); i++) { | |
| 168 AudioParameters params_mono(kTestFormat, | |
| 169 CHANNEL_LAYOUT_MONO, | |
| 170 rates[i], | |
| 171 kTestBitsPerSample, | |
| 172 kTestFramesPerPacket); | |
| 173 CaptureSomeFrames(params_mono, kTestCaptureDurationMs); | |
| 174 | |
| 175 AudioParameters params_stereo(kTestFormat, | |
| 176 CHANNEL_LAYOUT_STEREO, | |
| 177 rates[i], | |
| 178 kTestBitsPerSample, | |
| 179 kTestFramesPerPacket); | |
| 180 CaptureSomeFrames(params_stereo, kTestCaptureDurationMs); | |
| 181 } | |
| 182 } | |
| 183 | |
| 184 } // namespace media | |
| OLD | NEW |