Chromium Code Reviews| Index: media/audio/mac/audio_manager_mac.cc |
| diff --git a/media/audio/mac/audio_manager_mac.cc b/media/audio/mac/audio_manager_mac.cc |
| index b0744eeaa1f10bbb1f87443686f18bd2a61e02f0..9324e9a21a1fe9fb6d5f5e58636e83e8174af9d4 100644 |
| --- a/media/audio/mac/audio_manager_mac.cc |
| +++ b/media/audio/mac/audio_manager_mac.cc |
| @@ -4,7 +4,10 @@ |
| #include "media/audio/mac/audio_manager_mac.h" |
| -#include <stdint.h> |
| +#include <algorithm> |
| +#include <limits> |
| +#include <map> |
| +#include <vector> |
| #include "base/bind.h" |
| #include "base/command_line.h" |
| @@ -483,6 +486,7 @@ void AudioManagerMac::GetAudioOutputDeviceNames( |
| AudioParameters AudioManagerMac::GetInputStreamParameters( |
| const std::string& device_id) { |
| + DCHECK(GetTaskRunner()->BelongsToCurrentThread()); |
| AudioDeviceID device = GetAudioDeviceIdByUId(true, device_id); |
| if (device == kAudioObjectUnknown) { |
| DLOG(ERROR) << "Invalid device " << device_id; |
| @@ -598,12 +602,14 @@ std::string AudioManagerMac::GetAssociatedOutputDeviceID( |
| AudioOutputStream* AudioManagerMac::MakeLinearOutputStream( |
| const AudioParameters& params) { |
| + DCHECK(GetTaskRunner()->BelongsToCurrentThread()); |
| return MakeLowLatencyOutputStream(params, std::string()); |
| } |
| AudioOutputStream* AudioManagerMac::MakeLowLatencyOutputStream( |
| const AudioParameters& params, |
| const std::string& device_id) { |
| + DCHECK(GetTaskRunner()->BelongsToCurrentThread()); |
| bool device_listener_first_init = false; |
| // Lazily create the audio device listener on the first stream creation, |
| // even if getting an audio device fails. Otherwise, if we have 0 audio |
| @@ -672,6 +678,7 @@ std::string AudioManagerMac::GetDefaultOutputDeviceID() { |
| AudioInputStream* AudioManagerMac::MakeLinearInputStream( |
| const AudioParameters& params, const std::string& device_id) { |
| + DCHECK(GetTaskRunner()->BelongsToCurrentThread()); |
| DCHECK_EQ(AudioParameters::AUDIO_PCM_LINEAR, params.format()); |
| AudioInputStream* stream = new PCMQueueInAudioInputStream(this, params); |
| basic_input_streams_.push_back(stream); |
| @@ -680,6 +687,7 @@ AudioInputStream* AudioManagerMac::MakeLinearInputStream( |
| AudioInputStream* AudioManagerMac::MakeLowLatencyInputStream( |
| const AudioParameters& params, const std::string& device_id) { |
| + DCHECK(GetTaskRunner()->BelongsToCurrentThread()); |
| DCHECK_EQ(AudioParameters::AUDIO_PCM_LOW_LATENCY, params.format()); |
| // Gets the AudioDeviceID that refers to the AudioInputDevice with the device |
| // unique id. This AudioDeviceID is used to set the device for Audio Unit. |
| @@ -696,6 +704,7 @@ AudioInputStream* AudioManagerMac::MakeLowLatencyInputStream( |
| AudioParameters AudioManagerMac::GetPreferredOutputStreamParameters( |
| const std::string& output_device_id, |
| const AudioParameters& input_params) { |
| + DCHECK(GetTaskRunner()->BelongsToCurrentThread()); |
| const AudioDeviceID device = GetAudioDeviceIdByUId(false, output_device_id); |
| if (device == kAudioObjectUnknown) { |
| DLOG(ERROR) << "Invalid output device " << output_device_id; |
| @@ -917,12 +926,122 @@ bool AudioManagerMac::MaybeChangeBufferSize(AudioDeviceID device_id, |
| return (result == noErr); |
| } |
| +#if !defined(NDEBUG) |
| +void AudioManagerMac::PrintOutputBufferSizes() { |
| + for (auto* stream : output_streams_) { |
| + DVLOG(1) << "[id=0x" << std::hex << stream->device_id() << "] " << std::dec |
| + << "requested: " << stream->requested_buffer_size() << ", " |
| + << "actual: " << stream->actual_buffer_size(); |
| + } |
| +} |
| +#endif // !defined(NDEBUG) |
| + |
| +bool AudioManagerMac::IncreaseIOBufferSizeIfPossible(AudioDeviceID device_id) { |
| + DCHECK(GetTaskRunner()->BelongsToCurrentThread()); |
| + DVLOG(1) << "IncreaseIOBufferSizeIfPossible(id=0x" << std::hex << device_id |
| + << ")"; |
| + |
| + // Scan all active output streams using the specified |device_id|. Store the |
| + // actual I/O buffer size once and find the minimum requested buffer size. |
| + // In addition, store a reference to first output stream using |device_id|. |
| + size_t actual_size = 0; |
| + AUHALStream* output_stream = nullptr; |
| + size_t min_requested_size = std::numeric_limits<std::size_t>::max(); |
| + for (auto* stream : output_streams_) { |
| + if (stream->device_id() == device_id) { |
| + if (output_stream == nullptr) { |
| + // All active output streams uses the same actual I/O buffer size given |
|
o1ka
2016/04/27 09:00:38
nit: uses->use
henrika (OOO until Aug 14)
2016/04/27 12:12:30
Done.
|
| + // a unique device ID. Hence, it is sufficient to store one value once. |
|
o1ka
2016/04/27 09:00:38
nit: "one value" -> "the value"
henrika (OOO until Aug 14)
2016/04/27 12:12:30
Done.
|
| + actual_size = stream->actual_buffer_size(); |
| + // Also store a reference to the first stream using the specified ID. |
| + output_stream = stream; |
|
o1ka
2016/04/27 09:00:38
nit: all you need is audio_unit, so you can probab
henrika (OOO until Aug 14)
2016/04/27 12:12:31
I can't really see any benefit by doing that.
o1ka
2016/04/27 13:24:40
Clearer intentions of what you really need?
henrika (OOO until Aug 14)
2016/04/27 13:36:43
I would like to keep it as is if possible. More si
o1ka
2016/04/27 15:04:31
You don't need to check for null, you can just che
henrika (OOO until Aug 14)
2016/04/28 11:28:44
Your word is my law ;-) Changed.
|
| + } |
| + if (stream->requested_buffer_size() < min_requested_size) |
| + min_requested_size = stream->requested_buffer_size(); |
| + DVLOG(1) << "requested:" << stream->requested_buffer_size() |
| + << " actual: " << actual_size; |
| + } |
| + } |
| + |
| + if (output_stream == nullptr) { |
| + DVLOG(1) << "No action since there is no active stream for given device id"; |
| + return false; |
| + } |
| + |
| + // It is only possible to revert to a larger buffer size if the lowest |
| + // requested is not in use. Example: if the actual I/O buffer size is 256 and |
| + // at least one output stream has asked for 256 as its buffer size, we can't |
| + // start using a larger I/O buffer size. |
| + if (min_requested_size == actual_size) { |
|
o1ka
2016/04/27 09:00:38
add DCHECK_GE(min_requested_size, actual_size)?
henrika (OOO until Aug 14)
2016/04/27 12:12:30
Sorry but where do you want me to add that check?
o1ka
2016/04/27 13:24:40
Right above this line. Makes sense?
henrika (OOO until Aug 14)
2016/04/27 13:36:43
OK, above makes sense. I figured you meant here af
o1ka
2016/04/27 15:04:31
Acknowledged.
|
| + DVLOG(1) << "No action since lowest possible size is already in use: " |
| + << actual_size; |
| + return false; |
| + } |
| + |
| + // It should now be safe to increase the I/O buffer size to a new (higher) |
| + // value using the |min_requested_size|. Doing so will save system resources. |
| + // All active output streams with the same |device_id| are affected by this |
| + // change but it is only required to apply the change to one of the streams. |
| + const size_t increased_io_buffer_frame_size = min_requested_size; |
|
o1ka
2016/04/27 09:00:38
const size_t&
Actually, I think min_requested_size
henrika (OOO until Aug 14)
2016/04/27 12:12:31
Done.
|
| + DVLOG(1) << "increased_io_buffer_frame_size: " |
| + << increased_io_buffer_frame_size; |
| + bool size_was_changed = false; |
| + size_t io_buffer_frame_size = 0; |
| + bool result = MaybeChangeBufferSize(device_id, output_stream->audio_unit(), 0, |
| + increased_io_buffer_frame_size, |
| + &size_was_changed, &io_buffer_frame_size); |
| + DCHECK_EQ(io_buffer_frame_size, increased_io_buffer_frame_size); |
|
o1ka
2016/04/27 09:00:38
DCHECK size_was_changed as well?
henrika (OOO until Aug 14)
2016/04/27 12:12:30
Actually, Yes. I could not do it earlier but now w
|
| + |
| + return result; |
| +} |
| + |
| +bool AudioManagerMac::AudioDeviceIsUsedForInput(AudioDeviceID device_id) { |
| + DCHECK(GetTaskRunner()->BelongsToCurrentThread()); |
| + if (!basic_input_streams_.empty()) { |
| + // For Audio Queues and in the default case (Mac OS X), the audio comes |
| + // from the system’s default audio input device as set by a user in System |
| + // Preferences. |
| + AudioDeviceID default_id; |
| + GetDefaultDevice(&default_id, true); |
| + if (default_id == device_id) |
| + return true; |
| + } |
| + |
| + // Each low latency streams has its own device ID. |
| + for (auto* stream : low_latency_input_streams_) { |
| + if (stream->device_id() == device_id) |
| + return true; |
| + } |
| + return false; |
| +} |
| + |
| void AudioManagerMac::ReleaseOutputStream(AudioOutputStream* stream) { |
| + DCHECK(GetTaskRunner()->BelongsToCurrentThread()); |
| + const AudioDeviceID id = static_cast<AUHALStream*>(stream)->device_id(); |
| + DVLOG(1) << "Closing down output stream with id=0x" << std::hex << id; |
| + |
| + // Start by closing down the specified output stream. |
| output_streams_.remove(static_cast<AUHALStream*>(stream)); |
| +#if !defined(NDEBUG) |
| + PrintOutputBufferSizes(); |
| +#endif // !defined(NDEBUG) |
| AudioManagerBase::ReleaseOutputStream(stream); |
| + |
| + // Prevent attempt to alter buffer size if the released stream was the last |
| + // output stream. |
| + if (output_streams_.empty()) |
| + return; |
| + |
| + if (!AudioDeviceIsUsedForInput(id)) { |
| + // The current audio device is not used for input. See if it is possible to |
| + // increase the IO buffer size (saves power) given the remaining output |
| + // audio streams and their buffer size requirements. |
| + IncreaseIOBufferSizeIfPossible(id); |
| + } |
| } |
| void AudioManagerMac::ReleaseInputStream(AudioInputStream* stream) { |
| + DCHECK(GetTaskRunner()->BelongsToCurrentThread()); |
| auto stream_it = std::find(basic_input_streams_.begin(), |
| basic_input_streams_.end(), |
| stream); |