Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(125)

Side by Side Diff: media/audio/mac/audio_manager_mac.cc

Issue 1903753002: Restores larger output buffer size when output stream requiring smaller size is closed (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: nit Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « media/audio/mac/audio_manager_mac.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 <map>
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
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
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
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 201 matching lines...) Expand 10 before | Expand all | Expand 10 after
910 << "AudioUnitSetProperty(kAudioDevicePropertyBufferFrameSize) failed. " 918 << "AudioUnitSetProperty(kAudioDevicePropertyBufferFrameSize) failed. "
911 << "Size:: " << buffer_size; 919 << "Size:: " << buffer_size;
912 *size_was_changed = (result == noErr); 920 *size_was_changed = (result == noErr);
913 DVLOG_IF(1, result == noErr) << "IO buffer size changed to: " << buffer_size; 921 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. 922 // Store the currently used (after a change) I/O buffer frame size.
915 *io_buffer_frame_size = buffer_size; 923 *io_buffer_frame_size = buffer_size;
916 924
917 return (result == noErr); 925 return (result == noErr);
918 } 926 }
919 927
928 #if !defined(NDEBUG)
929 void AudioManagerMac::PrintOutputBufferSizes() {
930 for (auto* stream : output_streams_) {
931 DVLOG(1) << "[id=0x" << std::hex << stream->device_id() << "] " << std::dec
932 << "requested: " << stream->requested_buffer_size() << ", "
933 << "actual: " << stream->actual_buffer_size();
934 }
935 }
936 #endif // !defined(NDEBUG)
937
938 bool AudioManagerMac::IncreaseIOBufferSizeIfPossible(AudioDeviceID device_id) {
939 DCHECK(GetTaskRunner()->BelongsToCurrentThread());
940 DVLOG(1) << "IncreaseIOBufferSizeIfPossible(id=0x" << std::hex << device_id
941 << ")";
942
943 // Scan all active output streams using the specified |device_id|. Store the
944 // actual I/O buffer size once and add all requested buffer sizes to a list.
945 // In addition, build a list of output streams using |device_id|.
o1ka 2016/04/25 15:13:26 It does not seem to be used - am I missing somethi
henrika (OOO until Aug 14) 2016/04/26 11:47:28 I actually only use the first stream in the list.
946 size_t actual_size = 0;
947 std::vector<size_t> requested_sizes;
948 std::list<AUHALStream*> output_streams;
949 for (auto* stream : output_streams_) {
950 if (stream->device_id() == device_id) {
951 if (actual_size == 0) {
952 // All active output streams uses the same actual I/O buffer size given
953 // a unique device ID. Hence, it is sufficient to store one value once.
954 actual_size = stream->actual_buffer_size();
955 }
956 requested_sizes.push_back(stream->requested_buffer_size());
957 output_streams.push_back(stream);
958 }
959 }
960
961 if (output_streams.empty()) {
962 DVLOG(1) << "No action since there is no active stream for given device id";
963 return false;
964 }
965
966 // Sort the list of requested buffer sizes (lowest value comes first).
967 std::sort(requested_sizes.begin(), requested_sizes.end());
968 // It is only possible to revert to a larger buffer size if the lowest
969 // requested is not in use. Example: if the actual I/O buffer size is 256 and
970 // at least one output stream has asked for 256 as its buffer size, we can't
971 // start using a larger I/O buffer size.
972 if (requested_sizes[0] == actual_size) {
973 DVLOG(1) << "No action since lowest possible size is already in use: "
974 << actual_size;
975 return false;
976 }
977
978 // Add a handy log message to track the current status before trying to make
979 // a change.
980 for (auto it = requested_sizes.begin(); it != requested_sizes.end(); ++it) {
o1ka 2016/04/25 15:13:26 Do I understand it correctly that this information
981 DVLOG(1) << "requested:" << *it << " actual: " << actual_size;
982 }
983
984 // It should now be safe to increase the I/O buffer size to a new (higher)
985 // value using the first value in |requested_sizes|. Doing so will save
986 // system resources. All active output streams with the same |device_id| are
987 // affected by this change but it is only required to apply the change to one
988 // of the streams. Currently the first stream in the list is utilized.
989 const size_t increased_io_buffer_frame_size = requested_sizes[0];
990 DVLOG(1) << "increased_io_buffer_frame_size: "
991 << increased_io_buffer_frame_size;
992 bool size_was_changed = false;
993 size_t io_buffer_frame_size = 0;
994 bool result = MaybeChangeBufferSize(
995 device_id, output_streams.front()->audio_unit(), 0,
996 increased_io_buffer_frame_size, &size_was_changed, &io_buffer_frame_size);
997 DCHECK_EQ(io_buffer_frame_size, increased_io_buffer_frame_size);
998
999 return result;
1000 }
1001
1002 bool AudioManagerMac::AudioDeviceIsUsedForInput(AudioDeviceID device_id) {
1003 DCHECK(GetTaskRunner()->BelongsToCurrentThread());
1004 if (!basic_input_streams_.empty()) {
1005 // For Audio Queues and in the default case (Mac OS X), the audio comes
1006 // from the system’s default audio input device as set by a user in System
1007 // Preferences.
1008 AudioDeviceID default_id;
1009 GetDefaultDevice(&default_id, true);
1010 if (default_id == device_id)
1011 return true;
1012 }
1013
1014 // Each low latency streams has its own device ID.
1015 for (auto* stream : low_latency_input_streams_) {
1016 if (stream->device_id() == device_id)
1017 return true;
1018 }
1019 return false;
1020 }
1021
920 void AudioManagerMac::ReleaseOutputStream(AudioOutputStream* stream) { 1022 void AudioManagerMac::ReleaseOutputStream(AudioOutputStream* stream) {
1023 DCHECK(GetTaskRunner()->BelongsToCurrentThread());
1024 const AudioDeviceID id = static_cast<AUHALStream*>(stream)->device_id();
1025 DVLOG(1) << "Closing down output stream with id=0x" << std::hex << id;
1026
1027 // Start by closing down the specified output stream.
921 output_streams_.remove(static_cast<AUHALStream*>(stream)); 1028 output_streams_.remove(static_cast<AUHALStream*>(stream));
1029 #if !defined(NDEBUG)
1030 PrintOutputBufferSizes();
1031 #endif // !defined(NDEBUG)
922 AudioManagerBase::ReleaseOutputStream(stream); 1032 AudioManagerBase::ReleaseOutputStream(stream);
1033
1034 // Prevent attempt to alter buffer size if the released stream was the last
1035 // output stream.
1036 if (output_streams_.empty())
1037 return;
1038
1039 if (!AudioDeviceIsUsedForInput(id)) {
1040 // The current audio device is not used for input. See if it is possible to
1041 // increase the IO buffer size (saves power) given the remaining output
1042 // audio streams and their buffer size requirements.
1043 IncreaseIOBufferSizeIfPossible(id);
1044 }
923 } 1045 }
924 1046
925 void AudioManagerMac::ReleaseInputStream(AudioInputStream* stream) { 1047 void AudioManagerMac::ReleaseInputStream(AudioInputStream* stream) {
1048 DCHECK(GetTaskRunner()->BelongsToCurrentThread());
926 auto stream_it = std::find(basic_input_streams_.begin(), 1049 auto stream_it = std::find(basic_input_streams_.begin(),
927 basic_input_streams_.end(), 1050 basic_input_streams_.end(),
928 stream); 1051 stream);
929 if (stream_it == basic_input_streams_.end()) 1052 if (stream_it == basic_input_streams_.end())
930 low_latency_input_streams_.remove(static_cast<AUAudioInputStream*>(stream)); 1053 low_latency_input_streams_.remove(static_cast<AUAudioInputStream*>(stream));
931 else 1054 else
932 basic_input_streams_.erase(stream_it); 1055 basic_input_streams_.erase(stream_it);
933 1056
934 AudioManagerBase::ReleaseInputStream(stream); 1057 AudioManagerBase::ReleaseInputStream(stream);
935 } 1058 }
936 1059
937 ScopedAudioManagerPtr CreateAudioManager( 1060 ScopedAudioManagerPtr CreateAudioManager(
938 scoped_refptr<base::SingleThreadTaskRunner> task_runner, 1061 scoped_refptr<base::SingleThreadTaskRunner> task_runner,
939 scoped_refptr<base::SingleThreadTaskRunner> worker_task_runner, 1062 scoped_refptr<base::SingleThreadTaskRunner> worker_task_runner,
940 AudioLogFactory* audio_log_factory) { 1063 AudioLogFactory* audio_log_factory) {
941 return ScopedAudioManagerPtr( 1064 return ScopedAudioManagerPtr(
942 new AudioManagerMac(std::move(task_runner), std::move(worker_task_runner), 1065 new AudioManagerMac(std::move(task_runner), std::move(worker_task_runner),
943 audio_log_factory)); 1066 audio_log_factory));
944 } 1067 }
945 1068
946 } // namespace media 1069 } // namespace media
OLDNEW
« no previous file with comments | « media/audio/mac/audio_manager_mac.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698