| 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 <stdint.h> | 7 #include <algorithm> |
| 8 #include <limits> |
| 9 #include <map> |
| 10 #include <vector> |
| 8 | 11 |
| 9 #include "base/bind.h" | 12 #include "base/bind.h" |
| 10 #include "base/command_line.h" | 13 #include "base/command_line.h" |
| 11 #include "base/mac/mac_logging.h" | 14 #include "base/mac/mac_logging.h" |
| 12 #include "base/mac/scoped_cftyperef.h" | 15 #include "base/mac/scoped_cftyperef.h" |
| 13 #include "base/macros.h" | 16 #include "base/macros.h" |
| 14 #include "base/memory/free_deleter.h" | 17 #include "base/memory/free_deleter.h" |
| 15 #include "base/power_monitor/power_monitor.h" | 18 #include "base/power_monitor/power_monitor.h" |
| 16 #include "base/power_monitor/power_observer.h" | 19 #include "base/power_monitor/power_observer.h" |
| 17 #include "base/strings/sys_string_conversions.h" | 20 #include "base/strings/sys_string_conversions.h" |
| (...skipping 458 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 476 } | 479 } |
| 477 | 480 |
| 478 void AudioManagerMac::GetAudioOutputDeviceNames( | 481 void AudioManagerMac::GetAudioOutputDeviceNames( |
| 479 media::AudioDeviceNames* device_names) { | 482 media::AudioDeviceNames* device_names) { |
| 480 DCHECK(device_names->empty()); | 483 DCHECK(device_names->empty()); |
| 481 GetAudioDeviceInfo(false, device_names); | 484 GetAudioDeviceInfo(false, device_names); |
| 482 } | 485 } |
| 483 | 486 |
| 484 AudioParameters AudioManagerMac::GetInputStreamParameters( | 487 AudioParameters AudioManagerMac::GetInputStreamParameters( |
| 485 const std::string& device_id) { | 488 const std::string& device_id) { |
| 489 DCHECK(GetTaskRunner()->BelongsToCurrentThread()); |
| 486 AudioDeviceID device = GetAudioDeviceIdByUId(true, device_id); | 490 AudioDeviceID device = GetAudioDeviceIdByUId(true, device_id); |
| 487 if (device == kAudioObjectUnknown) { | 491 if (device == kAudioObjectUnknown) { |
| 488 DLOG(ERROR) << "Invalid device " << device_id; | 492 DLOG(ERROR) << "Invalid device " << device_id; |
| 489 return AudioParameters( | 493 return AudioParameters( |
| 490 AudioParameters::AUDIO_PCM_LOW_LATENCY, CHANNEL_LAYOUT_STEREO, | 494 AudioParameters::AUDIO_PCM_LOW_LATENCY, CHANNEL_LAYOUT_STEREO, |
| 491 kFallbackSampleRate, 16, ChooseBufferSize(true, kFallbackSampleRate)); | 495 kFallbackSampleRate, 16, ChooseBufferSize(true, kFallbackSampleRate)); |
| 492 } | 496 } |
| 493 | 497 |
| 494 int channels = 0; | 498 int channels = 0; |
| 495 ChannelLayout channel_layout = CHANNEL_LAYOUT_STEREO; | 499 ChannelLayout channel_layout = CHANNEL_LAYOUT_STEREO; |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 591 if (default_device == *iter) | 595 if (default_device == *iter) |
| 592 return *iter; | 596 return *iter; |
| 593 } | 597 } |
| 594 | 598 |
| 595 // Failed to figure out which is the matching device, return an empty string. | 599 // Failed to figure out which is the matching device, return an empty string. |
| 596 return std::string(); | 600 return std::string(); |
| 597 } | 601 } |
| 598 | 602 |
| 599 AudioOutputStream* AudioManagerMac::MakeLinearOutputStream( | 603 AudioOutputStream* AudioManagerMac::MakeLinearOutputStream( |
| 600 const AudioParameters& params) { | 604 const AudioParameters& params) { |
| 605 DCHECK(GetTaskRunner()->BelongsToCurrentThread()); |
| 601 return MakeLowLatencyOutputStream(params, std::string()); | 606 return MakeLowLatencyOutputStream(params, std::string()); |
| 602 } | 607 } |
| 603 | 608 |
| 604 AudioOutputStream* AudioManagerMac::MakeLowLatencyOutputStream( | 609 AudioOutputStream* AudioManagerMac::MakeLowLatencyOutputStream( |
| 605 const AudioParameters& params, | 610 const AudioParameters& params, |
| 606 const std::string& device_id) { | 611 const std::string& device_id) { |
| 612 DCHECK(GetTaskRunner()->BelongsToCurrentThread()); |
| 607 bool device_listener_first_init = false; | 613 bool device_listener_first_init = false; |
| 608 // Lazily create the audio device listener on the first stream creation, | 614 // Lazily create the audio device listener on the first stream creation, |
| 609 // even if getting an audio device fails. Otherwise, if we have 0 audio | 615 // even if getting an audio device fails. Otherwise, if we have 0 audio |
| 610 // devices, the listener will never be initialized, and new valid devices | 616 // devices, the listener will never be initialized, and new valid devices |
| 611 // will never be detected. | 617 // will never be detected. |
| 612 if (!output_device_listener_) { | 618 if (!output_device_listener_) { |
| 613 // NOTE: Use BindToCurrentLoop() to ensure the callback is always PostTask'd | 619 // NOTE: Use BindToCurrentLoop() to ensure the callback is always PostTask'd |
| 614 // even if OSX calls us on the right thread. Some CoreAudio drivers will | 620 // even if OSX calls us on the right thread. Some CoreAudio drivers will |
| 615 // fire the callbacks during stream creation, leading to re-entrancy issues | 621 // fire the callbacks during stream creation, leading to re-entrancy issues |
| 616 // otherwise. See http://crbug.com/349604 | 622 // otherwise. See http://crbug.com/349604 |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 665 return std::string(); | 671 return std::string(); |
| 666 | 672 |
| 667 std::string ret(base::SysCFStringRefToUTF8(device_uid)); | 673 std::string ret(base::SysCFStringRefToUTF8(device_uid)); |
| 668 CFRelease(device_uid); | 674 CFRelease(device_uid); |
| 669 | 675 |
| 670 return ret; | 676 return ret; |
| 671 } | 677 } |
| 672 | 678 |
| 673 AudioInputStream* AudioManagerMac::MakeLinearInputStream( | 679 AudioInputStream* AudioManagerMac::MakeLinearInputStream( |
| 674 const AudioParameters& params, const std::string& device_id) { | 680 const AudioParameters& params, const std::string& device_id) { |
| 681 DCHECK(GetTaskRunner()->BelongsToCurrentThread()); |
| 675 DCHECK_EQ(AudioParameters::AUDIO_PCM_LINEAR, params.format()); | 682 DCHECK_EQ(AudioParameters::AUDIO_PCM_LINEAR, params.format()); |
| 676 AudioInputStream* stream = new PCMQueueInAudioInputStream(this, params); | 683 AudioInputStream* stream = new PCMQueueInAudioInputStream(this, params); |
| 677 basic_input_streams_.push_back(stream); | 684 basic_input_streams_.push_back(stream); |
| 678 return stream; | 685 return stream; |
| 679 } | 686 } |
| 680 | 687 |
| 681 AudioInputStream* AudioManagerMac::MakeLowLatencyInputStream( | 688 AudioInputStream* AudioManagerMac::MakeLowLatencyInputStream( |
| 682 const AudioParameters& params, const std::string& device_id) { | 689 const AudioParameters& params, const std::string& device_id) { |
| 690 DCHECK(GetTaskRunner()->BelongsToCurrentThread()); |
| 683 DCHECK_EQ(AudioParameters::AUDIO_PCM_LOW_LATENCY, params.format()); | 691 DCHECK_EQ(AudioParameters::AUDIO_PCM_LOW_LATENCY, params.format()); |
| 684 // Gets the AudioDeviceID that refers to the AudioInputDevice with the device | 692 // Gets the AudioDeviceID that refers to the AudioInputDevice with the device |
| 685 // unique id. This AudioDeviceID is used to set the device for Audio Unit. | 693 // unique id. This AudioDeviceID is used to set the device for Audio Unit. |
| 686 AudioDeviceID audio_device_id = GetAudioDeviceIdByUId(true, device_id); | 694 AudioDeviceID audio_device_id = GetAudioDeviceIdByUId(true, device_id); |
| 687 AUAudioInputStream* stream = NULL; | 695 AUAudioInputStream* stream = NULL; |
| 688 if (audio_device_id != kAudioObjectUnknown) { | 696 if (audio_device_id != kAudioObjectUnknown) { |
| 689 stream = new AUAudioInputStream(this, params, audio_device_id); | 697 stream = new AUAudioInputStream(this, params, audio_device_id); |
| 690 low_latency_input_streams_.push_back(stream); | 698 low_latency_input_streams_.push_back(stream); |
| 691 } | 699 } |
| 692 | 700 |
| 693 return stream; | 701 return stream; |
| 694 } | 702 } |
| 695 | 703 |
| 696 AudioParameters AudioManagerMac::GetPreferredOutputStreamParameters( | 704 AudioParameters AudioManagerMac::GetPreferredOutputStreamParameters( |
| 697 const std::string& output_device_id, | 705 const std::string& output_device_id, |
| 698 const AudioParameters& input_params) { | 706 const AudioParameters& input_params) { |
| 707 DCHECK(GetTaskRunner()->BelongsToCurrentThread()); |
| 699 const AudioDeviceID device = GetAudioDeviceIdByUId(false, output_device_id); | 708 const AudioDeviceID device = GetAudioDeviceIdByUId(false, output_device_id); |
| 700 if (device == kAudioObjectUnknown) { | 709 if (device == kAudioObjectUnknown) { |
| 701 DLOG(ERROR) << "Invalid output device " << output_device_id; | 710 DLOG(ERROR) << "Invalid output device " << output_device_id; |
| 702 return input_params.IsValid() ? input_params : AudioParameters( | 711 return input_params.IsValid() ? input_params : AudioParameters( |
| 703 AudioParameters::AUDIO_PCM_LOW_LATENCY, CHANNEL_LAYOUT_STEREO, | 712 AudioParameters::AUDIO_PCM_LOW_LATENCY, CHANNEL_LAYOUT_STEREO, |
| 704 kFallbackSampleRate, 16, ChooseBufferSize(false, kFallbackSampleRate)); | 713 kFallbackSampleRate, 16, ChooseBufferSize(false, kFallbackSampleRate)); |
| 705 } | 714 } |
| 706 | 715 |
| 707 const bool has_valid_input_params = input_params.IsValid(); | 716 const bool has_valid_input_params = input_params.IsValid(); |
| 708 const int hardware_sample_rate = HardwareSampleRateForDevice(device); | 717 const int hardware_sample_rate = HardwareSampleRateForDevice(device); |
| (...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 910 << "AudioUnitSetProperty(kAudioDevicePropertyBufferFrameSize) failed. " | 919 << "AudioUnitSetProperty(kAudioDevicePropertyBufferFrameSize) failed. " |
| 911 << "Size:: " << buffer_size; | 920 << "Size:: " << buffer_size; |
| 912 *size_was_changed = (result == noErr); | 921 *size_was_changed = (result == noErr); |
| 913 DVLOG_IF(1, result == noErr) << "IO buffer size changed to: " << buffer_size; | 922 DVLOG_IF(1, result == noErr) << "IO buffer size changed to: " << buffer_size; |
| 914 // Store the currently used (after a change) I/O buffer frame size. | 923 // Store the currently used (after a change) I/O buffer frame size. |
| 915 *io_buffer_frame_size = buffer_size; | 924 *io_buffer_frame_size = buffer_size; |
| 916 | 925 |
| 917 return (result == noErr); | 926 return (result == noErr); |
| 918 } | 927 } |
| 919 | 928 |
| 929 #if !defined(NDEBUG) |
| 930 void AudioManagerMac::PrintOutputBufferSizes() { |
| 931 for (auto* stream : output_streams_) { |
| 932 DVLOG(1) << "[id=0x" << std::hex << stream->device_id() << "] " << std::dec |
| 933 << "requested: " << stream->requested_buffer_size() << ", " |
| 934 << "actual: " << stream->actual_buffer_size(); |
| 935 } |
| 936 } |
| 937 #endif // !defined(NDEBUG) |
| 938 |
| 939 bool AudioManagerMac::IncreaseIOBufferSizeIfPossible(AudioDeviceID device_id) { |
| 940 DCHECK(GetTaskRunner()->BelongsToCurrentThread()); |
| 941 DVLOG(1) << "IncreaseIOBufferSizeIfPossible(id=0x" << std::hex << device_id |
| 942 << ")"; |
| 943 |
| 944 // Scan all active output streams using the specified |device_id|. Store the |
| 945 // actual I/O buffer size once and find the minimum requested buffer size. |
| 946 // In addition, store a reference to first output stream using |device_id|. |
| 947 size_t actual_size = 0; |
| 948 AUHALStream* output_stream = nullptr; |
| 949 size_t min_requested_size = std::numeric_limits<std::size_t>::max(); |
| 950 for (auto* stream : output_streams_) { |
| 951 if (stream->device_id() == device_id) { |
| 952 if (output_stream == nullptr) { |
| 953 // All active output streams use the same actual I/O buffer size given |
| 954 // a unique device ID. Hence, it is sufficient to store the value once. |
| 955 actual_size = stream->actual_buffer_size(); |
| 956 // Also store a reference to the first stream using the specified ID. |
| 957 output_stream = stream; |
| 958 } |
| 959 if (stream->requested_buffer_size() < min_requested_size) |
| 960 min_requested_size = stream->requested_buffer_size(); |
| 961 DVLOG(1) << "requested:" << stream->requested_buffer_size() |
| 962 << " actual: " << actual_size; |
| 963 } |
| 964 } |
| 965 |
| 966 if (output_stream == nullptr) { |
| 967 DVLOG(1) << "No action since there is no active stream for given device id"; |
| 968 return false; |
| 969 } |
| 970 |
| 971 // It is only possible to revert to a larger buffer size if the lowest |
| 972 // requested is not in use. Example: if the actual I/O buffer size is 256 and |
| 973 // at least one output stream has asked for 256 as its buffer size, we can't |
| 974 // start using a larger I/O buffer size. |
| 975 if (min_requested_size == actual_size) { |
| 976 DVLOG(1) << "No action since lowest possible size is already in use: " |
| 977 << actual_size; |
| 978 return false; |
| 979 } |
| 980 |
| 981 // It should now be safe to increase the I/O buffer size to a new (higher) |
| 982 // value using the |min_requested_size|. Doing so will save system resources. |
| 983 // All active output streams with the same |device_id| are affected by this |
| 984 // change but it is only required to apply the change to one of the streams. |
| 985 DVLOG(1) << "min_requested_size: " << min_requested_size; |
| 986 bool size_was_changed = false; |
| 987 size_t io_buffer_frame_size = 0; |
| 988 bool result = MaybeChangeBufferSize(device_id, output_stream->audio_unit(), 0, |
| 989 min_requested_size, &size_was_changed, |
| 990 &io_buffer_frame_size); |
| 991 DCHECK_EQ(io_buffer_frame_size, min_requested_size); |
| 992 DCHECK(size_was_changed); |
| 993 |
| 994 return result; |
| 995 } |
| 996 |
| 997 bool AudioManagerMac::AudioDeviceIsUsedForInput(AudioDeviceID device_id) { |
| 998 DCHECK(GetTaskRunner()->BelongsToCurrentThread()); |
| 999 if (!basic_input_streams_.empty()) { |
| 1000 // For Audio Queues and in the default case (Mac OS X), the audio comes |
| 1001 // from the system’s default audio input device as set by a user in System |
| 1002 // Preferences. |
| 1003 AudioDeviceID default_id; |
| 1004 GetDefaultDevice(&default_id, true); |
| 1005 if (default_id == device_id) |
| 1006 return true; |
| 1007 } |
| 1008 |
| 1009 // Each low latency streams has its own device ID. |
| 1010 for (auto* stream : low_latency_input_streams_) { |
| 1011 if (stream->device_id() == device_id) |
| 1012 return true; |
| 1013 } |
| 1014 return false; |
| 1015 } |
| 1016 |
| 920 void AudioManagerMac::ReleaseOutputStream(AudioOutputStream* stream) { | 1017 void AudioManagerMac::ReleaseOutputStream(AudioOutputStream* stream) { |
| 1018 DCHECK(GetTaskRunner()->BelongsToCurrentThread()); |
| 1019 const AudioDeviceID id = static_cast<AUHALStream*>(stream)->device_id(); |
| 1020 DVLOG(1) << "Closing down output stream with id=0x" << std::hex << id; |
| 1021 |
| 1022 // Start by closing down the specified output stream. |
| 921 output_streams_.remove(static_cast<AUHALStream*>(stream)); | 1023 output_streams_.remove(static_cast<AUHALStream*>(stream)); |
| 1024 #if !defined(NDEBUG) |
| 1025 PrintOutputBufferSizes(); |
| 1026 #endif // !defined(NDEBUG) |
| 922 AudioManagerBase::ReleaseOutputStream(stream); | 1027 AudioManagerBase::ReleaseOutputStream(stream); |
| 1028 |
| 1029 // Prevent attempt to alter buffer size if the released stream was the last |
| 1030 // output stream. |
| 1031 if (output_streams_.empty()) |
| 1032 return; |
| 1033 |
| 1034 if (!AudioDeviceIsUsedForInput(id)) { |
| 1035 // The current audio device is not used for input. See if it is possible to |
| 1036 // increase the IO buffer size (saves power) given the remaining output |
| 1037 // audio streams and their buffer size requirements. |
| 1038 IncreaseIOBufferSizeIfPossible(id); |
| 1039 } |
| 923 } | 1040 } |
| 924 | 1041 |
| 925 void AudioManagerMac::ReleaseInputStream(AudioInputStream* stream) { | 1042 void AudioManagerMac::ReleaseInputStream(AudioInputStream* stream) { |
| 1043 DCHECK(GetTaskRunner()->BelongsToCurrentThread()); |
| 926 auto stream_it = std::find(basic_input_streams_.begin(), | 1044 auto stream_it = std::find(basic_input_streams_.begin(), |
| 927 basic_input_streams_.end(), | 1045 basic_input_streams_.end(), |
| 928 stream); | 1046 stream); |
| 929 if (stream_it == basic_input_streams_.end()) | 1047 if (stream_it == basic_input_streams_.end()) |
| 930 low_latency_input_streams_.remove(static_cast<AUAudioInputStream*>(stream)); | 1048 low_latency_input_streams_.remove(static_cast<AUAudioInputStream*>(stream)); |
| 931 else | 1049 else |
| 932 basic_input_streams_.erase(stream_it); | 1050 basic_input_streams_.erase(stream_it); |
| 933 | 1051 |
| 934 AudioManagerBase::ReleaseInputStream(stream); | 1052 AudioManagerBase::ReleaseInputStream(stream); |
| 935 } | 1053 } |
| 936 | 1054 |
| 937 ScopedAudioManagerPtr CreateAudioManager( | 1055 ScopedAudioManagerPtr CreateAudioManager( |
| 938 scoped_refptr<base::SingleThreadTaskRunner> task_runner, | 1056 scoped_refptr<base::SingleThreadTaskRunner> task_runner, |
| 939 scoped_refptr<base::SingleThreadTaskRunner> worker_task_runner, | 1057 scoped_refptr<base::SingleThreadTaskRunner> worker_task_runner, |
| 940 AudioLogFactory* audio_log_factory) { | 1058 AudioLogFactory* audio_log_factory) { |
| 941 return ScopedAudioManagerPtr( | 1059 return ScopedAudioManagerPtr( |
| 942 new AudioManagerMac(std::move(task_runner), std::move(worker_task_runner), | 1060 new AudioManagerMac(std::move(task_runner), std::move(worker_task_runner), |
| 943 audio_log_factory)); | 1061 audio_log_factory)); |
| 944 } | 1062 } |
| 945 | 1063 |
| 946 } // namespace media | 1064 } // namespace media |
| OLD | NEW |