Chromium Code Reviews| 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 <vector> | |
| 8 | 10 |
| 9 #include "base/bind.h" | 11 #include "base/bind.h" |
| 10 #include "base/command_line.h" | 12 #include "base/command_line.h" |
| 11 #include "base/mac/mac_logging.h" | 13 #include "base/mac/mac_logging.h" |
| 12 #include "base/mac/scoped_cftyperef.h" | 14 #include "base/mac/scoped_cftyperef.h" |
| 13 #include "base/macros.h" | 15 #include "base/macros.h" |
| 14 #include "base/memory/free_deleter.h" | 16 #include "base/memory/free_deleter.h" |
| 15 #include "base/power_monitor/power_monitor.h" | 17 #include "base/power_monitor/power_monitor.h" |
| 16 #include "base/power_monitor/power_observer.h" | 18 #include "base/power_monitor/power_observer.h" |
| 17 #include "base/strings/sys_string_conversions.h" | 19 #include "base/strings/sys_string_conversions.h" |
| (...skipping 458 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 476 } | 478 } |
| 477 | 479 |
| 478 void AudioManagerMac::GetAudioOutputDeviceNames( | 480 void AudioManagerMac::GetAudioOutputDeviceNames( |
| 479 media::AudioDeviceNames* device_names) { | 481 media::AudioDeviceNames* device_names) { |
| 480 DCHECK(device_names->empty()); | 482 DCHECK(device_names->empty()); |
| 481 GetAudioDeviceInfo(false, device_names); | 483 GetAudioDeviceInfo(false, device_names); |
| 482 } | 484 } |
| 483 | 485 |
| 484 AudioParameters AudioManagerMac::GetInputStreamParameters( | 486 AudioParameters AudioManagerMac::GetInputStreamParameters( |
| 485 const std::string& device_id) { | 487 const std::string& device_id) { |
| 488 DCHECK(GetTaskRunner()->BelongsToCurrentThread()); | |
| 486 AudioDeviceID device = GetAudioDeviceIdByUId(true, device_id); | 489 AudioDeviceID device = GetAudioDeviceIdByUId(true, device_id); |
| 487 if (device == kAudioObjectUnknown) { | 490 if (device == kAudioObjectUnknown) { |
| 488 DLOG(ERROR) << "Invalid device " << device_id; | 491 DLOG(ERROR) << "Invalid device " << device_id; |
| 489 return AudioParameters( | 492 return AudioParameters( |
| 490 AudioParameters::AUDIO_PCM_LOW_LATENCY, CHANNEL_LAYOUT_STEREO, | 493 AudioParameters::AUDIO_PCM_LOW_LATENCY, CHANNEL_LAYOUT_STEREO, |
| 491 kFallbackSampleRate, 16, ChooseBufferSize(true, kFallbackSampleRate)); | 494 kFallbackSampleRate, 16, ChooseBufferSize(true, kFallbackSampleRate)); |
| 492 } | 495 } |
| 493 | 496 |
| 494 int channels = 0; | 497 int channels = 0; |
| 495 ChannelLayout channel_layout = CHANNEL_LAYOUT_STEREO; | 498 ChannelLayout channel_layout = CHANNEL_LAYOUT_STEREO; |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 591 if (default_device == *iter) | 594 if (default_device == *iter) |
| 592 return *iter; | 595 return *iter; |
| 593 } | 596 } |
| 594 | 597 |
| 595 // Failed to figure out which is the matching device, return an empty string. | 598 // Failed to figure out which is the matching device, return an empty string. |
| 596 return std::string(); | 599 return std::string(); |
| 597 } | 600 } |
| 598 | 601 |
| 599 AudioOutputStream* AudioManagerMac::MakeLinearOutputStream( | 602 AudioOutputStream* AudioManagerMac::MakeLinearOutputStream( |
| 600 const AudioParameters& params) { | 603 const AudioParameters& params) { |
| 604 DCHECK(GetTaskRunner()->BelongsToCurrentThread()); | |
| 601 return MakeLowLatencyOutputStream(params, std::string()); | 605 return MakeLowLatencyOutputStream(params, std::string()); |
| 602 } | 606 } |
| 603 | 607 |
| 604 AudioOutputStream* AudioManagerMac::MakeLowLatencyOutputStream( | 608 AudioOutputStream* AudioManagerMac::MakeLowLatencyOutputStream( |
| 605 const AudioParameters& params, | 609 const AudioParameters& params, |
| 606 const std::string& device_id) { | 610 const std::string& device_id) { |
| 611 DCHECK(GetTaskRunner()->BelongsToCurrentThread()); | |
| 607 bool device_listener_first_init = false; | 612 bool device_listener_first_init = false; |
| 608 // Lazily create the audio device listener on the first stream creation, | 613 // 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 | 614 // 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 | 615 // devices, the listener will never be initialized, and new valid devices |
| 611 // will never be detected. | 616 // will never be detected. |
| 612 if (!output_device_listener_) { | 617 if (!output_device_listener_) { |
| 613 // NOTE: Use BindToCurrentLoop() to ensure the callback is always PostTask'd | 618 // 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 | 619 // 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 | 620 // fire the callbacks during stream creation, leading to re-entrancy issues |
| 616 // otherwise. See http://crbug.com/349604 | 621 // otherwise. See http://crbug.com/349604 |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 665 return std::string(); | 670 return std::string(); |
| 666 | 671 |
| 667 std::string ret(base::SysCFStringRefToUTF8(device_uid)); | 672 std::string ret(base::SysCFStringRefToUTF8(device_uid)); |
| 668 CFRelease(device_uid); | 673 CFRelease(device_uid); |
| 669 | 674 |
| 670 return ret; | 675 return ret; |
| 671 } | 676 } |
| 672 | 677 |
| 673 AudioInputStream* AudioManagerMac::MakeLinearInputStream( | 678 AudioInputStream* AudioManagerMac::MakeLinearInputStream( |
| 674 const AudioParameters& params, const std::string& device_id) { | 679 const AudioParameters& params, const std::string& device_id) { |
| 680 DCHECK(GetTaskRunner()->BelongsToCurrentThread()); | |
| 675 DCHECK_EQ(AudioParameters::AUDIO_PCM_LINEAR, params.format()); | 681 DCHECK_EQ(AudioParameters::AUDIO_PCM_LINEAR, params.format()); |
| 676 AudioInputStream* stream = new PCMQueueInAudioInputStream(this, params); | 682 AudioInputStream* stream = new PCMQueueInAudioInputStream(this, params); |
| 677 basic_input_streams_.push_back(stream); | 683 basic_input_streams_.push_back(stream); |
| 678 return stream; | 684 return stream; |
| 679 } | 685 } |
| 680 | 686 |
| 681 AudioInputStream* AudioManagerMac::MakeLowLatencyInputStream( | 687 AudioInputStream* AudioManagerMac::MakeLowLatencyInputStream( |
| 682 const AudioParameters& params, const std::string& device_id) { | 688 const AudioParameters& params, const std::string& device_id) { |
| 689 DCHECK(GetTaskRunner()->BelongsToCurrentThread()); | |
| 683 DCHECK_EQ(AudioParameters::AUDIO_PCM_LOW_LATENCY, params.format()); | 690 DCHECK_EQ(AudioParameters::AUDIO_PCM_LOW_LATENCY, params.format()); |
| 684 // Gets the AudioDeviceID that refers to the AudioInputDevice with the device | 691 // 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. | 692 // unique id. This AudioDeviceID is used to set the device for Audio Unit. |
| 686 AudioDeviceID audio_device_id = GetAudioDeviceIdByUId(true, device_id); | 693 AudioDeviceID audio_device_id = GetAudioDeviceIdByUId(true, device_id); |
| 687 AUAudioInputStream* stream = NULL; | 694 AUAudioInputStream* stream = NULL; |
| 688 if (audio_device_id != kAudioObjectUnknown) { | 695 if (audio_device_id != kAudioObjectUnknown) { |
| 689 stream = new AUAudioInputStream(this, params, audio_device_id); | 696 stream = new AUAudioInputStream(this, params, audio_device_id); |
| 690 low_latency_input_streams_.push_back(stream); | 697 low_latency_input_streams_.push_back(stream); |
| 691 } | 698 } |
| 692 | 699 |
| 693 return stream; | 700 return stream; |
| 694 } | 701 } |
| 695 | 702 |
| 696 AudioParameters AudioManagerMac::GetPreferredOutputStreamParameters( | 703 AudioParameters AudioManagerMac::GetPreferredOutputStreamParameters( |
| 697 const std::string& output_device_id, | 704 const std::string& output_device_id, |
| 698 const AudioParameters& input_params) { | 705 const AudioParameters& input_params) { |
| 706 DCHECK(GetTaskRunner()->BelongsToCurrentThread()); | |
| 699 const AudioDeviceID device = GetAudioDeviceIdByUId(false, output_device_id); | 707 const AudioDeviceID device = GetAudioDeviceIdByUId(false, output_device_id); |
| 700 if (device == kAudioObjectUnknown) { | 708 if (device == kAudioObjectUnknown) { |
| 701 DLOG(ERROR) << "Invalid output device " << output_device_id; | 709 DLOG(ERROR) << "Invalid output device " << output_device_id; |
| 702 return input_params.IsValid() ? input_params : AudioParameters( | 710 return input_params.IsValid() ? input_params : AudioParameters( |
| 703 AudioParameters::AUDIO_PCM_LOW_LATENCY, CHANNEL_LAYOUT_STEREO, | 711 AudioParameters::AUDIO_PCM_LOW_LATENCY, CHANNEL_LAYOUT_STEREO, |
| 704 kFallbackSampleRate, 16, ChooseBufferSize(false, kFallbackSampleRate)); | 712 kFallbackSampleRate, 16, ChooseBufferSize(false, kFallbackSampleRate)); |
| 705 } | 713 } |
| 706 | 714 |
| 707 const bool has_valid_input_params = input_params.IsValid(); | 715 const bool has_valid_input_params = input_params.IsValid(); |
| 708 const int hardware_sample_rate = HardwareSampleRateForDevice(device); | 716 const int hardware_sample_rate = HardwareSampleRateForDevice(device); |
| (...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 850 | 858 |
| 851 // Check if a buffer size change is required. If the caller asks for a | 859 // Check if a buffer size change is required. If the caller asks for a |
| 852 // reduced size (|desired_buffer_size| < |buffer_size|), the new lower size | 860 // reduced size (|desired_buffer_size| < |buffer_size|), the new lower size |
| 853 // will be set. For larger buffer sizes, we have to perform some checks to | 861 // will be set. For larger buffer sizes, we have to perform some checks to |
| 854 // see if the size can actually be changed. If there is any other active | 862 // see if the size can actually be changed. If there is any other active |
| 855 // streams on the same device, either input or output, a larger size than | 863 // streams on the same device, either input or output, a larger size than |
| 856 // their requested buffer size can't be set. The reason is that an existing | 864 // their requested buffer size can't be set. The reason is that an existing |
| 857 // stream can't handle buffer size larger than its requested buffer size. | 865 // stream can't handle buffer size larger than its requested buffer size. |
| 858 // See http://crbug.com/428706 for a reason why. | 866 // See http://crbug.com/428706 for a reason why. |
| 859 | 867 |
| 868 // Update map of actual buffer size given device id if the map is empty. | |
| 869 // Stores a base value that most likely be modified as last action in this | |
| 870 // method. | |
| 871 if (!is_input && output_io_buffer_size_map_.count(device_id) == 0) | |
| 872 output_io_buffer_size_map_[device_id] = buffer_size; | |
| 873 | |
| 860 if (buffer_size == desired_buffer_size) | 874 if (buffer_size == desired_buffer_size) |
| 861 return true; | 875 return true; |
| 862 | 876 |
| 863 if (desired_buffer_size > buffer_size) { | 877 if (desired_buffer_size > buffer_size) { |
| 864 // Do NOT set the buffer size if there is another output stream using | 878 // Do NOT set the buffer size if there is another output stream using |
| 865 // the same device with a smaller requested buffer size. | 879 // the same device with a smaller requested buffer size. |
| 866 // Note, for the caller stream, its requested_buffer_size() will be the same | 880 // Note, for the caller stream, its requested_buffer_size() will be the same |
| 867 // as |desired_buffer_size|, so it won't return true due to comparing with | 881 // as |desired_buffer_size|, so it won't return true due to comparing with |
| 868 // itself. | 882 // itself. |
| 869 for (auto* stream : output_streams_) { | 883 for (auto* stream : output_streams_) { |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 907 kAudioUnitScope_Global, 0, &buffer_size, | 921 kAudioUnitScope_Global, 0, &buffer_size, |
| 908 sizeof(buffer_size)); | 922 sizeof(buffer_size)); |
| 909 OSSTATUS_DLOG_IF(ERROR, result != noErr, result) | 923 OSSTATUS_DLOG_IF(ERROR, result != noErr, result) |
| 910 << "AudioUnitSetProperty(kAudioDevicePropertyBufferFrameSize) failed. " | 924 << "AudioUnitSetProperty(kAudioDevicePropertyBufferFrameSize) failed. " |
| 911 << "Size:: " << buffer_size; | 925 << "Size:: " << buffer_size; |
| 912 *size_was_changed = (result == noErr); | 926 *size_was_changed = (result == noErr); |
| 913 DVLOG_IF(1, result == noErr) << "IO buffer size changed to: " << buffer_size; | 927 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. | 928 // Store the currently used (after a change) I/O buffer frame size. |
| 915 *io_buffer_frame_size = buffer_size; | 929 *io_buffer_frame_size = buffer_size; |
| 916 | 930 |
| 931 // Update the actual output buffer size used for the given device ID. | |
| 932 DCHECK(!output_io_buffer_size_map_.empty()); | |
| 933 if (!is_input) { | |
| 934 output_io_buffer_size_map_[device_id] = buffer_size; | |
|
o1ka
2016/04/28 12:57:40
You update it even in case of error; is it correct
henrika (OOO until Aug 14)
2016/04/28 14:40:23
Thanks. Now fixed.
| |
| 935 } | |
| 936 | |
| 917 return (result == noErr); | 937 return (result == noErr); |
| 918 } | 938 } |
| 919 | 939 |
| 940 bool AudioManagerMac::IncreaseIOBufferSizeIfPossible(AudioDeviceID device_id) { | |
| 941 DCHECK(GetTaskRunner()->BelongsToCurrentThread()); | |
| 942 DVLOG(1) << "IncreaseIOBufferSizeIfPossible(id=0x" << std::hex << device_id | |
| 943 << ")"; | |
| 944 | |
| 945 // Scan all active output streams using the specified |device_id|. Store the | |
| 946 // actual I/O buffer size once and find the minimum requested buffer size. | |
| 947 // In addition, store a reference to the audio unit of the first output | |
| 948 // stream using |device_id|. | |
| 949 DCHECK(!output_io_buffer_size_map_.empty()); | |
| 950 size_t actual_size = 0; | |
| 951 AudioUnit audio_unit; | |
| 952 size_t min_requested_size = std::numeric_limits<std::size_t>::max(); | |
| 953 for (auto* stream : output_streams_) { | |
| 954 if (stream->device_id() == device_id) { | |
| 955 if (actual_size == 0) { | |
| 956 // All active output streams use the same actual I/O buffer size given | |
| 957 // a unique device ID. Hence, it is sufficient to store the value once. | |
| 958 actual_size = output_io_buffer_size_map_[device_id]; | |
|
o1ka
2016/04/28 12:57:40
nit: Now when you use a map it does not look quite
henrika (OOO until Aug 14)
2016/04/28 14:40:23
Done.
| |
| 959 // Also store reference to the first audio unit using the specified ID. | |
| 960 audio_unit = stream->audio_unit(); | |
| 961 } | |
| 962 if (stream->requested_buffer_size() < min_requested_size) | |
| 963 min_requested_size = stream->requested_buffer_size(); | |
| 964 DVLOG(1) << "requested:" << stream->requested_buffer_size() | |
| 965 << " actual: " << actual_size; | |
| 966 } | |
| 967 } | |
| 968 | |
| 969 if (actual_size == 0) { | |
| 970 DVLOG(1) << "No action since there is no active stream for given device id"; | |
| 971 return false; | |
| 972 } | |
| 973 | |
| 974 // It is only possible to revert to a larger buffer size if the lowest | |
| 975 // requested is not in use. Example: if the actual I/O buffer size is 256 and | |
| 976 // at least one output stream has asked for 256 as its buffer size, we can't | |
| 977 // start using a larger I/O buffer size. | |
| 978 DCHECK_GE(min_requested_size, actual_size); | |
| 979 if (min_requested_size == actual_size) { | |
| 980 DVLOG(1) << "No action since lowest possible size is already in use: " | |
| 981 << actual_size; | |
| 982 return false; | |
| 983 } | |
| 984 | |
| 985 // It should now be safe to increase the I/O buffer size to a new (higher) | |
| 986 // value using the |min_requested_size|. Doing so will save system resources. | |
| 987 // All active output streams with the same |device_id| are affected by this | |
| 988 // change but it is only required to apply the change to one of the streams. | |
| 989 DVLOG(1) << "min_requested_size: " << min_requested_size; | |
| 990 bool size_was_changed = false; | |
| 991 size_t io_buffer_frame_size = 0; | |
| 992 bool result = | |
| 993 MaybeChangeBufferSize(device_id, audio_unit, 0, min_requested_size, | |
| 994 &size_was_changed, &io_buffer_frame_size); | |
| 995 DCHECK_EQ(io_buffer_frame_size, min_requested_size); | |
| 996 DCHECK(size_was_changed); | |
| 997 return result; | |
| 998 } | |
| 999 | |
| 1000 bool AudioManagerMac::AudioDeviceIsUsedForInput(AudioDeviceID device_id) { | |
| 1001 DCHECK(GetTaskRunner()->BelongsToCurrentThread()); | |
| 1002 if (!basic_input_streams_.empty()) { | |
| 1003 // For Audio Queues and in the default case (Mac OS X), the audio comes | |
| 1004 // from the system’s default audio input device as set by a user in System | |
| 1005 // Preferences. | |
| 1006 AudioDeviceID default_id; | |
| 1007 GetDefaultDevice(&default_id, true); | |
| 1008 if (default_id == device_id) | |
| 1009 return true; | |
| 1010 } | |
| 1011 | |
| 1012 // Each low latency streams has its own device ID. | |
| 1013 for (auto* stream : low_latency_input_streams_) { | |
| 1014 if (stream->device_id() == device_id) | |
| 1015 return true; | |
| 1016 } | |
| 1017 return false; | |
| 1018 } | |
| 1019 | |
| 920 void AudioManagerMac::ReleaseOutputStream(AudioOutputStream* stream) { | 1020 void AudioManagerMac::ReleaseOutputStream(AudioOutputStream* stream) { |
| 1021 DCHECK(GetTaskRunner()->BelongsToCurrentThread()); | |
| 1022 const AudioDeviceID id = static_cast<AUHALStream*>(stream)->device_id(); | |
| 1023 DVLOG(1) << "Closing down output stream with id=0x" << std::hex << id; | |
| 1024 | |
| 1025 // Start by closing down the specified output stream. | |
| 921 output_streams_.remove(static_cast<AUHALStream*>(stream)); | 1026 output_streams_.remove(static_cast<AUHALStream*>(stream)); |
| 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 |