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 "media/audio/audio_manager.h" | 5 #include "media/audio/audio_manager.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/environment.h" | 11 #include "base/environment.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/memory/ptr_util.h" | 13 #include "base/memory/ptr_util.h" |
14 #include "base/run_loop.h" | 14 #include "base/run_loop.h" |
15 #include "base/single_thread_task_runner.h" | 15 #include "base/single_thread_task_runner.h" |
16 #include "base/strings/string_number_conversions.h" | 16 #include "base/strings/string_number_conversions.h" |
17 #include "base/synchronization/waitable_event.h" | 17 #include "base/synchronization/waitable_event.h" |
18 #include "base/test/test_message_loop.h" | 18 #include "base/test/test_message_loop.h" |
19 #include "base/threading/thread_task_runner_handle.h" | 19 #include "base/threading/thread_task_runner_handle.h" |
20 #include "build/build_config.h" | 20 #include "build/build_config.h" |
21 #include "media/audio/audio_device_description.h" | 21 #include "media/audio/audio_device_description.h" |
22 #include "media/audio/audio_device_info_accessor_for_tests.h" | 22 #include "media/audio/audio_device_info_accessor_for_tests.h" |
23 #include "media/audio/audio_device_name.h" | 23 #include "media/audio/audio_device_name.h" |
24 #include "media/audio/audio_output_proxy.h" | 24 #include "media/audio/audio_output_proxy.h" |
25 #include "media/audio/audio_unittest_util.h" | 25 #include "media/audio/audio_unittest_util.h" |
26 #include "media/audio/fake_audio_log_factory.h" | 26 #include "media/audio/fake_audio_log_factory.h" |
27 #include "media/audio/fake_audio_manager.h" | 27 #include "media/audio/fake_audio_manager.h" |
| 28 #include "media/audio/mock_audio_source_callback.h" |
28 #include "media/audio/test_audio_thread.h" | 29 #include "media/audio/test_audio_thread.h" |
| 30 #include "media/base/limits.h" |
29 #include "testing/gmock/include/gmock/gmock.h" | 31 #include "testing/gmock/include/gmock/gmock.h" |
30 #include "testing/gtest/include/gtest/gtest.h" | 32 #include "testing/gtest/include/gtest/gtest.h" |
31 | 33 |
32 #if defined(USE_ALSA) | 34 #if defined(USE_ALSA) |
33 #include "media/audio/alsa/audio_manager_alsa.h" | 35 #include "media/audio/alsa/audio_manager_alsa.h" |
34 #endif // defined(USE_ALSA) | 36 #endif // defined(USE_ALSA) |
35 | 37 |
| 38 #if defined(OS_MACOSX) |
| 39 #include "media/audio/mac/audio_manager_mac.h" |
| 40 #include "media/base/mac/audio_util_mac.h" |
| 41 #endif |
| 42 |
36 #if defined(OS_WIN) | 43 #if defined(OS_WIN) |
37 #include "base/win/scoped_com_initializer.h" | 44 #include "base/win/scoped_com_initializer.h" |
38 #include "media/audio/win/audio_manager_win.h" | 45 #include "media/audio/win/audio_manager_win.h" |
39 #endif | 46 #endif |
40 | 47 |
41 #if defined(USE_PULSEAUDIO) | 48 #if defined(USE_PULSEAUDIO) |
42 #include "media/audio/pulse/audio_manager_pulse.h" | 49 #include "media/audio/pulse/audio_manager_pulse.h" |
43 #include "media/audio/pulse/pulse_util.h" | 50 #include "media/audio/pulse/pulse_util.h" |
44 #endif // defined(USE_PULSEAUDIO) | 51 #endif // defined(USE_PULSEAUDIO) |
45 | 52 |
(...skipping 624 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
670 audio_manager_->DisableOutputDebugRecording(); | 677 audio_manager_->DisableOutputDebugRecording(); |
671 | 678 |
672 base::FilePath file_path(FILE_PATH_LITERAL("path")); | 679 base::FilePath file_path(FILE_PATH_LITERAL("path")); |
673 EXPECT_CALL(*mock_debug_recording_manager, EnableDebugRecording(file_path)); | 680 EXPECT_CALL(*mock_debug_recording_manager, EnableDebugRecording(file_path)); |
674 audio_manager_->EnableOutputDebugRecording(file_path); | 681 audio_manager_->EnableOutputDebugRecording(file_path); |
675 | 682 |
676 EXPECT_CALL(*mock_debug_recording_manager, DisableDebugRecording()); | 683 EXPECT_CALL(*mock_debug_recording_manager, DisableDebugRecording()); |
677 audio_manager_->DisableOutputDebugRecording(); | 684 audio_manager_->DisableOutputDebugRecording(); |
678 } | 685 } |
679 | 686 |
| 687 #if defined(OS_MACOSX) || defined(USE_CRAS) || defined(USE_PULSEAUDIO) |
| 688 #if defined(OS_MACOSX) |
| 689 class TestAudioManagerBase : public AudioManagerMac { |
| 690 #elif defined(USE_CRAS) |
| 691 class TestAudioManagerBase : public AudioManagerCras { |
| 692 #elif defined(USE_PULSEAUDIO) |
| 693 class TestAudioManagerBase : public AudioManagerPulse { |
| 694 #endif |
| 695 public: |
| 696 TestAudioManagerBase(std::unique_ptr<AudioThread> audio_thread, |
| 697 AudioLogFactory* audio_log_factory) |
| 698 : AudioManagerMac(std::move(audio_thread), audio_log_factory) {} |
| 699 |
| 700 AudioParameters GetPreferredOutputStreamParametersForTesting( |
| 701 const std::string& output_device_id, |
| 702 const AudioParameters& input_params) { |
| 703 return GetPreferredOutputStreamParameters(output_device_id, input_params); |
| 704 } |
| 705 }; // namespace media |
| 706 |
| 707 ACTION(ZeroBuffer) { |
| 708 arg3->Zero(); |
| 709 } |
| 710 |
| 711 ACTION_P3(MaybeSignalEvent, counter, signal_at_count, event) { |
| 712 if (++(*counter) == signal_at_count) |
| 713 event->Signal(); |
| 714 } |
| 715 |
| 716 TEST_F(AudioManagerTest, CheckMinimumAudioBufferSizes) { |
| 717 ABORT_AUDIO_TEST_IF_NOT(OutputDevicesAvailable()); |
| 718 |
| 719 CreateAudioManagerForTesting<TestAudioManagerBase>(); |
| 720 DCHECK(audio_manager_); |
| 721 |
| 722 TestAudioManagerBase* test_audio_manager_base = |
| 723 static_cast<TestAudioManagerBase*>(audio_manager_.get()); |
| 724 |
| 725 AudioParameters default_params = |
| 726 test_audio_manager_base->GetPreferredOutputStreamParametersForTesting( |
| 727 "", AudioParameters()); |
| 728 ASSERT_LT(default_params.frames_per_buffer(), |
| 729 media::limits::kMaxAudioBufferSize); |
| 730 |
| 731 #if defined(OS_MACOSX) |
| 732 // On OSX the preferred output buffer size is higher than the minimum |
| 733 // but users may request the minimum size explicitly. |
| 734 ASSERT_GT( |
| 735 default_params.frames_per_buffer(), |
| 736 audio_util_mac::GetMinAudioBufferSizeForSampleRate( |
| 737 media::limits::kMinAudioBufferSize, default_params.sample_rate())); |
| 738 #elif defined(USE_CRAS) |
| 739 // On CRAS the preferred output buffer size varies per board and may be as low |
| 740 // as the minimum for some boards. |
| 741 ASSERT_GE(params.frames_per_buffer(), media::limits::kMinAudioBufferSize); |
| 742 #elif defined(USE_PULSEAUDIO) |
| 743 ASSERT_EQ(params.frames_per_buffer(), media::limits::kMinAudioBufferSize); |
| 744 #else |
| 745 NOTREACHED(); |
| 746 #endif |
| 747 |
| 748 AudioParameters params; |
| 749 |
| 750 AudioParameters min_params = default_params; |
| 751 // Ensure that values below the minimum size are clamped to the minimum. |
| 752 min_params.set_frames_per_buffer(media::limits::kMinAudioBufferSize / 2); |
| 753 params = |
| 754 test_audio_manager_base->GetPreferredOutputStreamParametersForTesting( |
| 755 "", min_params); |
| 756 ASSERT_EQ(params.frames_per_buffer(), media::limits::kMinAudioBufferSize); |
| 757 |
| 758 AudioParameters max_params = default_params; |
| 759 // Ensure that values above the maximum size are clamped to the maximum. |
| 760 max_params.set_frames_per_buffer(media::limits::kMaxAudioBufferSize * 2); |
| 761 params = |
| 762 test_audio_manager_base->GetPreferredOutputStreamParametersForTesting( |
| 763 "", max_params); |
| 764 ASSERT_EQ(params.frames_per_buffer(), media::limits::kMaxAudioBufferSize); |
| 765 } |
| 766 |
| 767 TEST_F(AudioManagerTest, CheckMinimumAudioBufferSizesCallbacks) { |
| 768 ABORT_AUDIO_TEST_IF_NOT(OutputDevicesAvailable()); |
| 769 |
| 770 CreateAudioManagerForTesting<TestAudioManagerBase>(); |
| 771 DCHECK(audio_manager_); |
| 772 |
| 773 TestAudioManagerBase* test_audio_manager_base = |
| 774 static_cast<TestAudioManagerBase*>(audio_manager_.get()); |
| 775 |
| 776 AudioParameters min_params = |
| 777 test_audio_manager_base->GetPreferredOutputStreamParametersForTesting( |
| 778 "", AudioParameters()); |
| 779 min_params.set_frames_per_buffer(media::limits::kMinAudioBufferSize); |
| 780 |
| 781 // Create an output stream with the minimum buffer size parameters and ensure |
| 782 // that no errors are returned. |
| 783 AudioOutputStream* stream = |
| 784 audio_manager_->MakeAudioOutputStreamProxy(min_params, ""); |
| 785 ASSERT_TRUE(stream); |
| 786 EXPECT_TRUE(stream->Open()); |
| 787 |
| 788 base::WaitableEvent event(base::WaitableEvent::ResetPolicy::AUTOMATIC, |
| 789 base::WaitableEvent::InitialState::NOT_SIGNALED); |
| 790 int callback_counter = 0; |
| 791 MockAudioSourceCallback source; |
| 792 const int number_of_callbacks = 2; |
| 793 EXPECT_CALL(source, |
| 794 OnMoreData(testing::_, testing::_, testing::_, testing::_)) |
| 795 .Times(number_of_callbacks) |
| 796 .WillRepeatedly(testing::DoAll( |
| 797 ZeroBuffer(), |
| 798 MaybeSignalEvent(&callback_counter, number_of_callbacks, &event), |
| 799 testing::Return(0))); |
| 800 EXPECT_CALL(source, OnError()).Times(0); |
| 801 stream->Start(&source); |
| 802 event.Wait(); |
| 803 |
| 804 stream->Stop(); |
| 805 stream->Close(); |
| 806 } |
| 807 #endif |
| 808 |
680 } // namespace media | 809 } // namespace media |
OLD | NEW |