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 "media/audio/mac/audio_manager_mac.h" | 5 #include "media/audio/mac/audio_manager_mac.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <limits> | 8 #include <limits> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/bind.h" | 11 #include "base/bind.h" |
12 #include "base/command_line.h" | 12 #include "base/command_line.h" |
13 #include "base/mac/mac_logging.h" | 13 #include "base/mac/mac_logging.h" |
14 #include "base/mac/scoped_cftyperef.h" | 14 #include "base/mac/scoped_cftyperef.h" |
15 #include "base/macros.h" | 15 #include "base/macros.h" |
16 #include "base/memory/free_deleter.h" | 16 #include "base/memory/free_deleter.h" |
17 #include "base/power_monitor/power_monitor.h" | 17 #include "base/power_monitor/power_monitor.h" |
18 #include "base/power_monitor/power_observer.h" | 18 #include "base/power_monitor/power_observer.h" |
19 #include "base/strings/sys_string_conversions.h" | 19 #include "base/strings/sys_string_conversions.h" |
20 #include "base/threading/thread_checker.h" | 20 #include "base/threading/thread_checker.h" |
21 #include "media/audio/audio_device_description.h" | 21 #include "media/audio/audio_device_description.h" |
22 #include "media/audio/mac/audio_auhal_mac.h" | 22 #include "media/audio/mac/audio_auhal_mac.h" |
23 #include "media/audio/mac/audio_input_mac.h" | 23 #include "media/audio/mac/audio_input_mac.h" |
24 #include "media/audio/mac/audio_low_latency_input_mac.h" | 24 #include "media/audio/mac/audio_low_latency_input_mac.h" |
25 #include "media/audio/mac/scoped_audio_unit.h" | 25 #include "media/audio/mac/scoped_audio_unit.h" |
26 #include "media/base/audio_parameters.h" | 26 #include "media/base/audio_parameters.h" |
27 #include "media/base/bind_to_current_loop.h" | 27 #include "media/base/bind_to_current_loop.h" |
28 #include "media/base/channel_layout.h" | 28 #include "media/base/channel_layout.h" |
29 #include "media/base/limits.h" | 29 #include "media/base/limits.h" |
| 30 #include "media/base/mac/audio_util_mac.h" |
30 #include "media/base/media_switches.h" | 31 #include "media/base/media_switches.h" |
31 | 32 |
32 namespace media { | 33 namespace media { |
33 | 34 |
34 // Maximum number of output streams that can be open simultaneously. | 35 // Maximum number of output streams that can be open simultaneously. |
35 static const int kMaxOutputStreams = 50; | 36 static const int kMaxOutputStreams = 50; |
36 | 37 |
37 // Define bounds for for low-latency input and output streams. | |
38 static const int kMinimumInputOutputBufferSize = 128; | |
39 static const int kMaximumInputOutputBufferSize = 4096; | |
40 | |
41 // Default sample-rate on most Apple hardware. | 38 // Default sample-rate on most Apple hardware. |
42 static const int kFallbackSampleRate = 44100; | 39 static const int kFallbackSampleRate = 44100; |
43 | 40 |
44 // Helper method to construct AudioObjectPropertyAddress structure given | 41 // Helper method to construct AudioObjectPropertyAddress structure given |
45 // property selector and scope. The property element is always set to | 42 // property selector and scope. The property element is always set to |
46 // kAudioObjectPropertyElementMaster. | 43 // kAudioObjectPropertyElementMaster. |
47 static AudioObjectPropertyAddress GetAudioObjectPropertyAddress( | 44 static AudioObjectPropertyAddress GetAudioObjectPropertyAddress( |
48 AudioObjectPropertySelector selector, | 45 AudioObjectPropertySelector selector, |
49 bool is_input) { | 46 bool is_input) { |
50 AudioObjectPropertyScope scope = is_input ? kAudioObjectPropertyScopeInput | 47 AudioObjectPropertyScope scope = is_input ? kAudioObjectPropertyScopeInput |
(...skipping 778 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
829 AudioParameters::AUDIO_PCM_LOW_LATENCY, CHANNEL_LAYOUT_STEREO, | 826 AudioParameters::AUDIO_PCM_LOW_LATENCY, CHANNEL_LAYOUT_STEREO, |
830 kFallbackSampleRate, 16, ChooseBufferSize(false, kFallbackSampleRate)); | 827 kFallbackSampleRate, 16, ChooseBufferSize(false, kFallbackSampleRate)); |
831 } | 828 } |
832 | 829 |
833 const bool has_valid_input_params = input_params.IsValid(); | 830 const bool has_valid_input_params = input_params.IsValid(); |
834 const int hardware_sample_rate = HardwareSampleRateForDevice(device); | 831 const int hardware_sample_rate = HardwareSampleRateForDevice(device); |
835 | 832 |
836 // Allow pass through buffer sizes. If concurrent input and output streams | 833 // Allow pass through buffer sizes. If concurrent input and output streams |
837 // exist, they will use the smallest buffer size amongst them. As such, each | 834 // exist, they will use the smallest buffer size amongst them. As such, each |
838 // stream must be able to FIFO requests appropriately when this happens. | 835 // stream must be able to FIFO requests appropriately when this happens. |
839 int buffer_size = ChooseBufferSize(false, hardware_sample_rate); | 836 int buffer_size = |
840 if (has_valid_input_params) { | 837 has_valid_input_params |
841 buffer_size = | 838 // If passed in via the input_params we allow buffer sizes to go as |
842 std::min(kMaximumInputOutputBufferSize, | 839 // low as the the kMinAudioBufferSize, ignoring what |
843 std::max(input_params.frames_per_buffer(), buffer_size)); | 840 // ChooseBufferSize() normally returns. |
844 } | 841 ? std::min(static_cast<int>(limits::kMaxAudioBufferSize), |
| 842 std::max(input_params.frames_per_buffer(), |
| 843 static_cast<int>(limits::kMinAudioBufferSize))) |
| 844 : ChooseBufferSize(false, hardware_sample_rate); |
845 | 845 |
846 int hardware_channels; | 846 int hardware_channels; |
847 if (!GetDeviceChannels(device, AUElement::OUTPUT, &hardware_channels)) | 847 if (!GetDeviceChannels(device, AUElement::OUTPUT, &hardware_channels)) |
848 hardware_channels = 2; | 848 hardware_channels = 2; |
849 | 849 |
850 // Use the input channel count and channel layout if possible. Let OSX take | 850 // Use the input channel count and channel layout if possible. Let OSX take |
851 // care of remapping the channels; this lets user specified channel layouts | 851 // care of remapping the channels; this lets user specified channel layouts |
852 // work correctly. | 852 // work correctly. |
853 int output_channels = input_params.channels(); | 853 int output_channels = input_params.channels(); |
854 ChannelLayout channel_layout = input_params.channel_layout(); | 854 ChannelLayout channel_layout = input_params.channel_layout(); |
(...skipping 25 matching lines...) Expand all Loading... |
880 current_output_device_ == new_output_device) { | 880 current_output_device_ == new_output_device) { |
881 return; | 881 return; |
882 } | 882 } |
883 | 883 |
884 current_sample_rate_ = new_sample_rate; | 884 current_sample_rate_ = new_sample_rate; |
885 current_output_device_ = new_output_device; | 885 current_output_device_ = new_output_device; |
886 NotifyAllOutputDeviceChangeListeners(); | 886 NotifyAllOutputDeviceChangeListeners(); |
887 } | 887 } |
888 | 888 |
889 int AudioManagerMac::ChooseBufferSize(bool is_input, int sample_rate) { | 889 int AudioManagerMac::ChooseBufferSize(bool is_input, int sample_rate) { |
890 // kMinimumInputOutputBufferSize is too small for the output side because | 890 // kMinAudioBufferSize is too small for the output side because |
891 // CoreAudio can get into under-run if the renderer fails delivering data | 891 // CoreAudio can get into under-run if the renderer fails delivering data |
892 // to the browser within the allowed time by the OS. The workaround is to | 892 // to the browser within the allowed time by the OS. The workaround is to |
893 // use 256 samples as the default output buffer size for sample rates | 893 // use 256 samples as the default output buffer size for sample rates |
894 // smaller than 96KHz. | 894 // smaller than 96KHz. |
895 // TODO(xians): Remove this workaround after WebAudio supports user defined | 895 // TODO(xians): Remove this workaround after WebAudio supports user defined |
896 // buffer size. See https://github.com/WebAudio/web-audio-api/issues/348 | 896 // buffer size. See https://github.com/WebAudio/web-audio-api/issues/348 |
897 // for details. | 897 // for details. |
898 int buffer_size = is_input ? | 898 int buffer_size = |
899 kMinimumInputOutputBufferSize : 2 * kMinimumInputOutputBufferSize; | 899 is_input ? limits::kMinAudioBufferSize : 2 * limits::kMinAudioBufferSize; |
900 const int user_buffer_size = GetUserBufferSize(); | 900 const int user_buffer_size = GetUserBufferSize(); |
901 if (user_buffer_size) { | 901 buffer_size = user_buffer_size |
902 buffer_size = user_buffer_size; | 902 ? user_buffer_size |
903 } else if (sample_rate > 48000) { | 903 : audio_util_mac::GetMinAudioBufferSizeForSampleRate( |
904 // The default buffer size is too small for higher sample rates and may lead | 904 buffer_size, sample_rate); |
905 // to glitching. Adjust upwards by multiples of the default size. | |
906 if (sample_rate <= 96000) | |
907 buffer_size = 2 * kMinimumInputOutputBufferSize; | |
908 else if (sample_rate <= 192000) | |
909 buffer_size = 4 * kMinimumInputOutputBufferSize; | |
910 } | |
911 | |
912 return buffer_size; | 905 return buffer_size; |
913 } | 906 } |
914 | 907 |
915 bool AudioManagerMac::IsSuspending() const { | 908 bool AudioManagerMac::IsSuspending() const { |
916 DCHECK(GetTaskRunner()->BelongsToCurrentThread()); | 909 DCHECK(GetTaskRunner()->BelongsToCurrentThread()); |
917 return power_observer_->IsSuspending(); | 910 return power_observer_->IsSuspending(); |
918 } | 911 } |
919 | 912 |
920 bool AudioManagerMac::ShouldDeferStreamStart() const { | 913 bool AudioManagerMac::ShouldDeferStreamStart() const { |
921 DCHECK(GetTaskRunner()->BelongsToCurrentThread()); | 914 DCHECK(GetTaskRunner()->BelongsToCurrentThread()); |
(...skipping 261 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1183 } | 1176 } |
1184 | 1177 |
1185 std::unique_ptr<AudioManager> CreateAudioManager( | 1178 std::unique_ptr<AudioManager> CreateAudioManager( |
1186 std::unique_ptr<AudioThread> audio_thread, | 1179 std::unique_ptr<AudioThread> audio_thread, |
1187 AudioLogFactory* audio_log_factory) { | 1180 AudioLogFactory* audio_log_factory) { |
1188 return base::MakeUnique<AudioManagerMac>(std::move(audio_thread), | 1181 return base::MakeUnique<AudioManagerMac>(std::move(audio_thread), |
1189 audio_log_factory); | 1182 audio_log_factory); |
1190 } | 1183 } |
1191 | 1184 |
1192 } // namespace media | 1185 } // namespace media |
OLD | NEW |