| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "base/environment.h" | 6 #include "base/environment.h" |
| 7 #include "base/message_loop/message_loop.h" | 7 #include "base/message_loop/message_loop.h" |
| 8 #include "base/run_loop.h" | 8 #include "base/run_loop.h" |
| 9 #include "base/test/test_timeouts.h" | 9 #include "base/test/test_timeouts.h" |
| 10 #include "base/threading/platform_thread.h" | 10 #include "base/threading/platform_thread.h" |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 namespace media { | 24 namespace media { |
| 25 | 25 |
| 26 ACTION_P4(CheckCountAndPostQuitTask, count, limit, loop, closure) { | 26 ACTION_P4(CheckCountAndPostQuitTask, count, limit, loop, closure) { |
| 27 if (++*count >= limit) { | 27 if (++*count >= limit) { |
| 28 loop->PostTask(FROM_HERE, closure); | 28 loop->PostTask(FROM_HERE, closure); |
| 29 } | 29 } |
| 30 } | 30 } |
| 31 | 31 |
| 32 class MockAudioInputCallback : public AudioInputStream::AudioInputCallback { | 32 class MockAudioInputCallback : public AudioInputStream::AudioInputCallback { |
| 33 public: | 33 public: |
| 34 MOCK_METHOD4(OnData, | 34 MOCK_METHOD5(OnData, void(AudioInputStream* stream, |
| 35 void(AudioInputStream* stream, | 35 const uint8* src, uint32 size, |
| 36 const AudioBus* src, | 36 uint32 hardware_delay_bytes, double volume)); |
| 37 uint32 hardware_delay_bytes, | |
| 38 double volume)); | |
| 39 MOCK_METHOD1(OnError, void(AudioInputStream* stream)); | 37 MOCK_METHOD1(OnError, void(AudioInputStream* stream)); |
| 40 }; | 38 }; |
| 41 | 39 |
| 42 // This audio sink implementation should be used for manual tests only since | 40 // This audio sink implementation should be used for manual tests only since |
| 43 // the recorded data is stored on a raw binary data file. | 41 // the recorded data is stored on a raw binary data file. |
| 44 // The last test (WriteToFileAudioSink) - which is disabled by default - | 42 // The last test (WriteToFileAudioSink) - which is disabled by default - |
| 45 // can use this audio sink to store the captured data on a file for offline | 43 // can use this audio sink to store the captured data on a file for offline |
| 46 // analysis. | 44 // analysis. |
| 47 class WriteToFileAudioSink : public AudioInputStream::AudioInputCallback { | 45 class WriteToFileAudioSink : public AudioInputStream::AudioInputCallback { |
| 48 public: | 46 public: |
| (...skipping 20 matching lines...) Expand all Loading... |
| 69 // Write recorded data chunk to the file and prepare for next chunk. | 67 // Write recorded data chunk to the file and prepare for next chunk. |
| 70 fwrite(chunk, 1, chunk_size, file_); | 68 fwrite(chunk, 1, chunk_size, file_); |
| 71 buffer_.Seek(chunk_size); | 69 buffer_.Seek(chunk_size); |
| 72 bytes_written += chunk_size; | 70 bytes_written += chunk_size; |
| 73 } | 71 } |
| 74 fclose(file_); | 72 fclose(file_); |
| 75 } | 73 } |
| 76 | 74 |
| 77 // AudioInputStream::AudioInputCallback implementation. | 75 // AudioInputStream::AudioInputCallback implementation. |
| 78 virtual void OnData(AudioInputStream* stream, | 76 virtual void OnData(AudioInputStream* stream, |
| 79 const AudioBus* src, | 77 const uint8* src, uint32 size, |
| 80 uint32 hardware_delay_bytes, | 78 uint32 hardware_delay_bytes, double volume) OVERRIDE { |
| 81 double volume) OVERRIDE { | |
| 82 const int num_samples = src->frames() * src->channels(); | |
| 83 scoped_ptr<int16> interleaved(new int16[num_samples]); | |
| 84 const int bytes_per_sample = sizeof(*interleaved); | |
| 85 src->ToInterleaved(src->frames(), bytes_per_sample, interleaved.get()); | |
| 86 | |
| 87 // Store data data in a temporary buffer to avoid making blocking | 79 // Store data data in a temporary buffer to avoid making blocking |
| 88 // fwrite() calls in the audio callback. The complete buffer will be | 80 // fwrite() calls in the audio callback. The complete buffer will be |
| 89 // written to file in the destructor. | 81 // written to file in the destructor. |
| 90 const int size = bytes_per_sample * num_samples; | 82 if (buffer_.Append(src, size)) { |
| 91 if (buffer_.Append((const uint8*)interleaved.get(), size)) { | |
| 92 bytes_to_write_ += size; | 83 bytes_to_write_ += size; |
| 93 } | 84 } |
| 94 } | 85 } |
| 95 | 86 |
| 96 virtual void OnError(AudioInputStream* stream) OVERRIDE {} | 87 virtual void OnError(AudioInputStream* stream) OVERRIDE {} |
| 97 | 88 |
| 98 private: | 89 private: |
| 99 media::SeekableBuffer buffer_; | 90 media::SeekableBuffer buffer_; |
| 100 FILE* file_; | 91 FILE* file_; |
| 101 int bytes_to_write_; | 92 int bytes_to_write_; |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 226 TEST_F(MacAudioInputTest, AUAudioInputStreamVerifyMonoRecording) { | 217 TEST_F(MacAudioInputTest, AUAudioInputStreamVerifyMonoRecording) { |
| 227 if (!CanRunAudioTests()) | 218 if (!CanRunAudioTests()) |
| 228 return; | 219 return; |
| 229 | 220 |
| 230 int count = 0; | 221 int count = 0; |
| 231 | 222 |
| 232 // Create an audio input stream which records in mono. | 223 // Create an audio input stream which records in mono. |
| 233 AudioInputStream* ais = CreateAudioInputStream(CHANNEL_LAYOUT_MONO); | 224 AudioInputStream* ais = CreateAudioInputStream(CHANNEL_LAYOUT_MONO); |
| 234 EXPECT_TRUE(ais->Open()); | 225 EXPECT_TRUE(ais->Open()); |
| 235 | 226 |
| 227 int fs = static_cast<int>(AUAudioInputStream::HardwareSampleRate()); |
| 228 int samples_per_packet = fs / 100; |
| 229 int bits_per_sample = 16; |
| 230 uint32 bytes_per_packet = samples_per_packet * (bits_per_sample / 8); |
| 231 |
| 236 MockAudioInputCallback sink; | 232 MockAudioInputCallback sink; |
| 237 | 233 |
| 238 // We use 10ms packets and will run the test until ten packets are received. | 234 // We use 10ms packets and will run the test until ten packets are received. |
| 239 // All should contain valid packets of the same size and a valid delay | 235 // All should contain valid packets of the same size and a valid delay |
| 240 // estimate. | 236 // estimate. |
| 241 base::RunLoop run_loop; | 237 base::RunLoop run_loop; |
| 242 EXPECT_CALL(sink, OnData(ais, NotNull(), _, _)) | 238 EXPECT_CALL(sink, OnData(ais, NotNull(), bytes_per_packet, _, _)) |
| 243 .Times(AtLeast(10)) | 239 .Times(AtLeast(10)) |
| 244 .WillRepeatedly(CheckCountAndPostQuitTask( | 240 .WillRepeatedly(CheckCountAndPostQuitTask( |
| 245 &count, 10, &message_loop_, run_loop.QuitClosure())); | 241 &count, 10, &message_loop_, run_loop.QuitClosure())); |
| 246 ais->Start(&sink); | 242 ais->Start(&sink); |
| 247 run_loop.Run(); | 243 run_loop.Run(); |
| 248 ais->Stop(); | 244 ais->Stop(); |
| 249 ais->Close(); | 245 ais->Close(); |
| 250 } | 246 } |
| 251 | 247 |
| 252 // Verify that recording starts and stops correctly in mono using mocked sink. | 248 // Verify that recording starts and stops correctly in mono using mocked sink. |
| 253 TEST_F(MacAudioInputTest, AUAudioInputStreamVerifyStereoRecording) { | 249 TEST_F(MacAudioInputTest, AUAudioInputStreamVerifyStereoRecording) { |
| 254 if (!CanRunAudioTests()) | 250 if (!CanRunAudioTests()) |
| 255 return; | 251 return; |
| 256 | 252 |
| 257 int count = 0; | 253 int count = 0; |
| 258 | 254 |
| 259 // Create an audio input stream which records in stereo. | 255 // Create an audio input stream which records in stereo. |
| 260 AudioInputStream* ais = CreateAudioInputStream(CHANNEL_LAYOUT_STEREO); | 256 AudioInputStream* ais = CreateAudioInputStream(CHANNEL_LAYOUT_STEREO); |
| 261 EXPECT_TRUE(ais->Open()); | 257 EXPECT_TRUE(ais->Open()); |
| 262 | 258 |
| 259 int fs = static_cast<int>(AUAudioInputStream::HardwareSampleRate()); |
| 260 int samples_per_packet = fs / 100; |
| 261 int bits_per_sample = 16; |
| 262 uint32 bytes_per_packet = 2 * samples_per_packet * (bits_per_sample / 8); |
| 263 |
| 263 MockAudioInputCallback sink; | 264 MockAudioInputCallback sink; |
| 264 | 265 |
| 265 // We use 10ms packets and will run the test until ten packets are received. | 266 // We use 10ms packets and will run the test until ten packets are received. |
| 266 // All should contain valid packets of the same size and a valid delay | 267 // All should contain valid packets of the same size and a valid delay |
| 267 // estimate. | 268 // estimate. |
| 268 // TODO(henrika): http://crbug.com/154352 forced us to run the capture side | 269 // TODO(henrika): http://crbug.com/154352 forced us to run the capture side |
| 269 // using a native buffer size of 128 audio frames and combine it with a FIFO | 270 // using a native buffer size of 128 audio frames and combine it with a FIFO |
| 270 // to match the requested size by the client. This change might also have | 271 // to match the requested size by the client. This change might also have |
| 271 // modified the delay estimates since the existing Ge(bytes_per_packet) for | 272 // modified the delay estimates since the existing Ge(bytes_per_packet) for |
| 272 // parameter #4 does no longer pass. I am removing this restriction here to | 273 // parameter #4 does no longer pass. I am removing this restriction here to |
| 273 // ensure that we can land the patch but will revisit this test again when | 274 // ensure that we can land the patch but will revisit this test again when |
| 274 // more analysis of the delay estimates are done. | 275 // more analysis of the delay estimates are done. |
| 275 base::RunLoop run_loop; | 276 base::RunLoop run_loop; |
| 276 EXPECT_CALL(sink, OnData(ais, NotNull(), _, _)) | 277 EXPECT_CALL(sink, OnData(ais, NotNull(), bytes_per_packet, _, _)) |
| 277 .Times(AtLeast(10)) | 278 .Times(AtLeast(10)) |
| 278 .WillRepeatedly(CheckCountAndPostQuitTask( | 279 .WillRepeatedly(CheckCountAndPostQuitTask( |
| 279 &count, 10, &message_loop_, run_loop.QuitClosure())); | 280 &count, 10, &message_loop_, run_loop.QuitClosure())); |
| 280 ais->Start(&sink); | 281 ais->Start(&sink); |
| 281 run_loop.Run(); | 282 run_loop.Run(); |
| 282 ais->Stop(); | 283 ais->Stop(); |
| 283 ais->Close(); | 284 ais->Close(); |
| 284 } | 285 } |
| 285 | 286 |
| 286 // This test is intended for manual tests and should only be enabled | 287 // This test is intended for manual tests and should only be enabled |
| (...skipping 16 matching lines...) Expand all Loading... |
| 303 WriteToFileAudioSink file_sink(file_name); | 304 WriteToFileAudioSink file_sink(file_name); |
| 304 fprintf(stderr, " >> Speak into the mic while recording...\n"); | 305 fprintf(stderr, " >> Speak into the mic while recording...\n"); |
| 305 ais->Start(&file_sink); | 306 ais->Start(&file_sink); |
| 306 base::PlatformThread::Sleep(TestTimeouts::action_timeout()); | 307 base::PlatformThread::Sleep(TestTimeouts::action_timeout()); |
| 307 ais->Stop(); | 308 ais->Stop(); |
| 308 fprintf(stderr, " >> Recording has stopped.\n"); | 309 fprintf(stderr, " >> Recording has stopped.\n"); |
| 309 ais->Close(); | 310 ais->Close(); |
| 310 } | 311 } |
| 311 | 312 |
| 312 } // namespace media | 313 } // namespace media |
| OLD | NEW |