OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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 <stdint.h> | 5 #include <stdint.h> |
6 | 6 |
| 7 #include <memory> |
| 8 |
7 #include "base/android/build_info.h" | 9 #include "base/android/build_info.h" |
8 #include "base/bind.h" | 10 #include "base/bind.h" |
9 #include "base/files/file_util.h" | 11 #include "base/files/file_util.h" |
10 #include "base/macros.h" | 12 #include "base/macros.h" |
11 #include "base/memory/scoped_ptr.h" | |
12 #include "base/message_loop/message_loop.h" | 13 #include "base/message_loop/message_loop.h" |
13 #include "base/path_service.h" | 14 #include "base/path_service.h" |
14 #include "base/run_loop.h" | 15 #include "base/run_loop.h" |
15 #include "base/strings/stringprintf.h" | 16 #include "base/strings/stringprintf.h" |
16 #include "base/synchronization/lock.h" | 17 #include "base/synchronization/lock.h" |
17 #include "base/synchronization/waitable_event.h" | 18 #include "base/synchronization/waitable_event.h" |
18 #include "base/test/test_timeouts.h" | 19 #include "base/test/test_timeouts.h" |
19 #include "base/time/time.h" | 20 #include "base/time/time.h" |
20 #include "build/build_config.h" | 21 #include "build/build_config.h" |
21 #include "media/audio/android/audio_manager_android.h" | 22 #include "media/audio/android/audio_manager_android.h" |
(...skipping 243 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
265 } | 266 } |
266 base::CloseFile(binary_file_); | 267 base::CloseFile(binary_file_); |
267 } | 268 } |
268 | 269 |
269 // AudioInputStream::AudioInputCallback implementation. | 270 // AudioInputStream::AudioInputCallback implementation. |
270 void OnData(AudioInputStream* stream, | 271 void OnData(AudioInputStream* stream, |
271 const AudioBus* src, | 272 const AudioBus* src, |
272 uint32_t hardware_delay_bytes, | 273 uint32_t hardware_delay_bytes, |
273 double volume) override { | 274 double volume) override { |
274 const int num_samples = src->frames() * src->channels(); | 275 const int num_samples = src->frames() * src->channels(); |
275 scoped_ptr<int16_t> interleaved(new int16_t[num_samples]); | 276 std::unique_ptr<int16_t> interleaved(new int16_t[num_samples]); |
276 const int bytes_per_sample = sizeof(*interleaved); | 277 const int bytes_per_sample = sizeof(*interleaved); |
277 src->ToInterleaved(src->frames(), bytes_per_sample, interleaved.get()); | 278 src->ToInterleaved(src->frames(), bytes_per_sample, interleaved.get()); |
278 | 279 |
279 // Store data data in a temporary buffer to avoid making blocking | 280 // Store data data in a temporary buffer to avoid making blocking |
280 // fwrite() calls in the audio callback. The complete buffer will be | 281 // fwrite() calls in the audio callback. The complete buffer will be |
281 // written to file in the destructor. | 282 // written to file in the destructor. |
282 const int size = bytes_per_sample * num_samples; | 283 const int size = bytes_per_sample * num_samples; |
283 if (!buffer_->Append((const uint8_t*)interleaved.get(), size)) | 284 if (!buffer_->Append((const uint8_t*)interleaved.get(), size)) |
284 event_->Signal(); | 285 event_->Signal(); |
285 } | 286 } |
286 | 287 |
287 void OnError(AudioInputStream* stream) override {} | 288 void OnError(AudioInputStream* stream) override {} |
288 | 289 |
289 private: | 290 private: |
290 base::WaitableEvent* event_; | 291 base::WaitableEvent* event_; |
291 AudioParameters params_; | 292 AudioParameters params_; |
292 scoped_ptr<media::SeekableBuffer> buffer_; | 293 std::unique_ptr<media::SeekableBuffer> buffer_; |
293 FILE* binary_file_; | 294 FILE* binary_file_; |
294 | 295 |
295 DISALLOW_COPY_AND_ASSIGN(FileAudioSink); | 296 DISALLOW_COPY_AND_ASSIGN(FileAudioSink); |
296 }; | 297 }; |
297 | 298 |
298 // Implements AudioInputCallback and AudioSourceCallback to support full | 299 // Implements AudioInputCallback and AudioSourceCallback to support full |
299 // duplex audio where captured samples are played out in loopback after | 300 // duplex audio where captured samples are played out in loopback after |
300 // reading from a temporary FIFO storage. | 301 // reading from a temporary FIFO storage. |
301 class FullDuplexAudioSinkSource | 302 class FullDuplexAudioSinkSource |
302 : public AudioInputStream::AudioInputCallback, | 303 : public AudioInputStream::AudioInputCallback, |
(...skipping 14 matching lines...) Expand all Loading... |
317 // AudioInputStream::AudioInputCallback implementation | 318 // AudioInputStream::AudioInputCallback implementation |
318 void OnData(AudioInputStream* stream, | 319 void OnData(AudioInputStream* stream, |
319 const AudioBus* src, | 320 const AudioBus* src, |
320 uint32_t hardware_delay_bytes, | 321 uint32_t hardware_delay_bytes, |
321 double volume) override { | 322 double volume) override { |
322 const base::TimeTicks now_time = base::TimeTicks::Now(); | 323 const base::TimeTicks now_time = base::TimeTicks::Now(); |
323 const int diff = (now_time - previous_time_).InMilliseconds(); | 324 const int diff = (now_time - previous_time_).InMilliseconds(); |
324 | 325 |
325 EXPECT_EQ(params_.bits_per_sample(), 16); | 326 EXPECT_EQ(params_.bits_per_sample(), 16); |
326 const int num_samples = src->frames() * src->channels(); | 327 const int num_samples = src->frames() * src->channels(); |
327 scoped_ptr<int16_t> interleaved(new int16_t[num_samples]); | 328 std::unique_ptr<int16_t> interleaved(new int16_t[num_samples]); |
328 const int bytes_per_sample = sizeof(*interleaved); | 329 const int bytes_per_sample = sizeof(*interleaved); |
329 src->ToInterleaved(src->frames(), bytes_per_sample, interleaved.get()); | 330 src->ToInterleaved(src->frames(), bytes_per_sample, interleaved.get()); |
330 const int size = bytes_per_sample * num_samples; | 331 const int size = bytes_per_sample * num_samples; |
331 | 332 |
332 base::AutoLock lock(lock_); | 333 base::AutoLock lock(lock_); |
333 if (diff > 1000) { | 334 if (diff > 1000) { |
334 started_ = true; | 335 started_ = true; |
335 previous_time_ = now_time; | 336 previous_time_ = now_time; |
336 | 337 |
337 // Log out the extra delay added by the FIFO. This is a best effort | 338 // Log out the extra delay added by the FIFO. This is a best effort |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
396 double BytesToMilliseconds(int bytes) const { | 397 double BytesToMilliseconds(int bytes) const { |
397 const int frames = bytes / params_.GetBytesPerFrame(); | 398 const int frames = bytes / params_.GetBytesPerFrame(); |
398 return (base::TimeDelta::FromMicroseconds( | 399 return (base::TimeDelta::FromMicroseconds( |
399 frames * base::Time::kMicrosecondsPerSecond / | 400 frames * base::Time::kMicrosecondsPerSecond / |
400 static_cast<double>(params_.sample_rate()))).InMillisecondsF(); | 401 static_cast<double>(params_.sample_rate()))).InMillisecondsF(); |
401 } | 402 } |
402 | 403 |
403 AudioParameters params_; | 404 AudioParameters params_; |
404 base::TimeTicks previous_time_; | 405 base::TimeTicks previous_time_; |
405 base::Lock lock_; | 406 base::Lock lock_; |
406 scoped_ptr<media::SeekableBuffer> fifo_; | 407 std::unique_ptr<media::SeekableBuffer> fifo_; |
407 scoped_ptr<uint8_t[]> buffer_; | 408 std::unique_ptr<uint8_t[]> buffer_; |
408 bool started_; | 409 bool started_; |
409 | 410 |
410 DISALLOW_COPY_AND_ASSIGN(FullDuplexAudioSinkSource); | 411 DISALLOW_COPY_AND_ASSIGN(FullDuplexAudioSinkSource); |
411 }; | 412 }; |
412 | 413 |
413 // Test fixture class for tests which only exercise the output path. | 414 // Test fixture class for tests which only exercise the output path. |
414 class AudioAndroidOutputTest : public testing::Test { | 415 class AudioAndroidOutputTest : public testing::Test { |
415 public: | 416 public: |
416 AudioAndroidOutputTest() | 417 AudioAndroidOutputTest() |
417 : loop_(new base::MessageLoopForUI()), | 418 : loop_(new base::MessageLoopForUI()), |
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
558 audio_output_stream_->Start(source); | 559 audio_output_stream_->Start(source); |
559 } | 560 } |
560 | 561 |
561 void StopAndClose() { | 562 void StopAndClose() { |
562 DCHECK(audio_manager()->GetTaskRunner()->BelongsToCurrentThread()); | 563 DCHECK(audio_manager()->GetTaskRunner()->BelongsToCurrentThread()); |
563 audio_output_stream_->Stop(); | 564 audio_output_stream_->Stop(); |
564 audio_output_stream_->Close(); | 565 audio_output_stream_->Close(); |
565 audio_output_stream_ = NULL; | 566 audio_output_stream_ = NULL; |
566 } | 567 } |
567 | 568 |
568 scoped_ptr<base::MessageLoopForUI> loop_; | 569 std::unique_ptr<base::MessageLoopForUI> loop_; |
569 ScopedAudioManagerPtr audio_manager_; | 570 ScopedAudioManagerPtr audio_manager_; |
570 AudioParameters audio_output_parameters_; | 571 AudioParameters audio_output_parameters_; |
571 AudioOutputStream* audio_output_stream_; | 572 AudioOutputStream* audio_output_stream_; |
572 base::TimeTicks start_time_; | 573 base::TimeTicks start_time_; |
573 base::TimeTicks end_time_; | 574 base::TimeTicks end_time_; |
574 | 575 |
575 private: | 576 private: |
576 DISALLOW_COPY_AND_ASSIGN(AudioAndroidOutputTest); | 577 DISALLOW_COPY_AND_ASSIGN(AudioAndroidOutputTest); |
577 }; | 578 }; |
578 | 579 |
(...skipping 379 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
958 base::PlatformThread::Sleep(base::TimeDelta::FromSeconds(20)); | 959 base::PlatformThread::Sleep(base::TimeDelta::FromSeconds(20)); |
959 printf("\n"); | 960 printf("\n"); |
960 StopAndCloseAudioOutputStreamOnAudioThread(); | 961 StopAndCloseAudioOutputStreamOnAudioThread(); |
961 StopAndCloseAudioInputStreamOnAudioThread(); | 962 StopAndCloseAudioInputStreamOnAudioThread(); |
962 } | 963 } |
963 | 964 |
964 INSTANTIATE_TEST_CASE_P(AudioAndroidInputTest, AudioAndroidInputTest, | 965 INSTANTIATE_TEST_CASE_P(AudioAndroidInputTest, AudioAndroidInputTest, |
965 testing::ValuesIn(RunAudioRecordInputPathTests())); | 966 testing::ValuesIn(RunAudioRecordInputPathTests())); |
966 | 967 |
967 } // namespace media | 968 } // namespace media |
OLD | NEW |