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

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

Issue 1973503003: (Relanding) 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: Improved comments 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
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 #ifndef MEDIA_AUDIO_MAC_AUDIO_MANAGER_MAC_H_ 5 #ifndef MEDIA_AUDIO_MAC_AUDIO_MANAGER_MAC_H_
6 #define MEDIA_AUDIO_MAC_AUDIO_MANAGER_MAC_H_ 6 #define MEDIA_AUDIO_MAC_AUDIO_MANAGER_MAC_H_
7 7
8 #include <AudioUnit/AudioUnit.h> 8 #include <AudioUnit/AudioUnit.h>
9 #include <CoreAudio/AudioHardware.h> 9 #include <CoreAudio/AudioHardware.h>
10 #include <stddef.h> 10 #include <stddef.h>
11 11
12 #include <list> 12 #include <list>
13 #include <map>
13 #include <memory> 14 #include <memory>
14 #include <string> 15 #include <string>
15 16
16 #include "base/compiler_specific.h" 17 #include "base/compiler_specific.h"
17 #include "base/macros.h" 18 #include "base/macros.h"
18 #include "media/audio/audio_manager_base.h" 19 #include "media/audio/audio_manager_base.h"
19 #include "media/audio/mac/audio_device_listener_mac.h" 20 #include "media/audio/mac/audio_device_listener_mac.h"
20 21
21 namespace media { 22 namespace media {
22 23
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
97 // |size_was_changed| is set to true if the device's buffer size was changed 98 // |size_was_changed| is set to true if the device's buffer size was changed
98 // and |io_buffer_frame_size| contains the new buffer size. 99 // and |io_buffer_frame_size| contains the new buffer size.
99 // Returns false if an error occurred. 100 // Returns false if an error occurred.
100 bool MaybeChangeBufferSize(AudioDeviceID device_id, 101 bool MaybeChangeBufferSize(AudioDeviceID device_id,
101 AudioUnit audio_unit, 102 AudioUnit audio_unit,
102 AudioUnitElement element, 103 AudioUnitElement element,
103 size_t desired_buffer_size, 104 size_t desired_buffer_size,
104 bool* size_was_changed, 105 bool* size_was_changed,
105 size_t* io_buffer_frame_size); 106 size_t* io_buffer_frame_size);
106 107
108 // Called by AUHALStream::Close() before releasing the stream.
109 // This method is a special contract between the real stream and the audio
110 // manager and it ensures that we only try to increase the IO buffer size
111 // for real streams and not for fake or mocked streams.
112 void PrepareForAttemptToIncreaseIOBufferSize(AudioDeviceID device_id);
113
107 // Number of constructed output and input streams. 114 // Number of constructed output and input streams.
108 size_t output_streams() const { return output_streams_.size(); } 115 size_t output_streams() const { return output_streams_.size(); }
109 size_t low_latency_input_streams() const { 116 size_t low_latency_input_streams() const {
110 return low_latency_input_streams_.size(); 117 return low_latency_input_streams_.size();
111 } 118 }
112 size_t basic_input_streams() const { return basic_input_streams_.size(); } 119 size_t basic_input_streams() const { return basic_input_streams_.size(); }
113 120
114 protected: 121 protected:
115 ~AudioManagerMac() override; 122 ~AudioManagerMac() override;
116 123
117 AudioParameters GetPreferredOutputStreamParameters( 124 AudioParameters GetPreferredOutputStreamParameters(
118 const std::string& output_device_id, 125 const std::string& output_device_id,
119 const AudioParameters& input_params) override; 126 const AudioParameters& input_params) override;
120 127
121 private: 128 private:
122 void InitializeOnAudioThread(); 129 void InitializeOnAudioThread();
123 130
124 int ChooseBufferSize(bool is_input, int sample_rate); 131 int ChooseBufferSize(bool is_input, int sample_rate);
125 132
126 // Notify streams of a device change if the default output device or its 133 // Notify streams of a device change if the default output device or its
127 // sample rate has changed, otherwise does nothing. 134 // sample rate has changed, otherwise does nothing.
128 void HandleDeviceChanges(); 135 void HandleDeviceChanges();
129 136
137 // Returns true if any active input stream is using the specified |device_id|.
138 bool AudioDeviceIsUsedForInput(AudioDeviceID device_id);
139
140 // This method is called when an output stream has been released and it takes
141 // the given |device_id| and scans all active output streams that are
142 // using this id. The goal is to find a new (larger) I/O buffer size which
143 // can be applied to all active output streams since doing so will save
144 // system resources.
145 // Note that, it is only called if no input stream is also using the device.
146 // Example: two active output streams where #1 wants 1024 as buffer size but
147 // is using 256 since stream #2 wants it. Now, if stream #2 is closed down,
148 // the native I/O buffer size will be increased to 1024 instead of 256.
149 // Returns true if it was possible to increase the I/O buffer size and
150 // false otherwise.
151 // TODO(henrika): possibly extend the scheme to also take input streams into
152 // account.
153 bool IncreaseIOBufferSizeIfPossible(AudioDeviceID device_id);
154
130 std::unique_ptr<AudioDeviceListenerMac> output_device_listener_; 155 std::unique_ptr<AudioDeviceListenerMac> output_device_listener_;
131 156
132 // Track the output sample-rate and the default output device 157 // Track the output sample-rate and the default output device
133 // so we can intelligently handle device notifications only when necessary. 158 // so we can intelligently handle device notifications only when necessary.
134 int current_sample_rate_; 159 int current_sample_rate_;
135 AudioDeviceID current_output_device_; 160 AudioDeviceID current_output_device_;
136 161
137 // Helper class which monitors power events to determine if output streams 162 // Helper class which monitors power events to determine if output streams
138 // should defer Start() calls. Required to workaround an OSX bug. See 163 // should defer Start() calls. Required to workaround an OSX bug. See
139 // http://crbug.com/160920 for more details. 164 // http://crbug.com/160920 for more details.
140 class AudioPowerObserver; 165 class AudioPowerObserver;
141 std::unique_ptr<AudioPowerObserver> power_observer_; 166 std::unique_ptr<AudioPowerObserver> power_observer_;
142 167
143 // Tracks all constructed input and output streams. 168 // Tracks all constructed input and output streams.
144 // TODO(alokp): We used to track these streams to close before destruction. 169 // TODO(alokp): We used to track these streams to close before destruction.
145 // We no longer close the streams, so we may be able to get rid of these 170 // We no longer close the streams, so we may be able to get rid of these
146 // member variables. They are currently used by MaybeChangeBufferSize(). 171 // member variables. They are currently used by MaybeChangeBufferSize().
147 // Investigate if we can remove these. 172 // Investigate if we can remove these.
148 std::list<AudioInputStream*> basic_input_streams_; 173 std::list<AudioInputStream*> basic_input_streams_;
149 std::list<AUAudioInputStream*> low_latency_input_streams_; 174 std::list<AUAudioInputStream*> low_latency_input_streams_;
150 std::list<AUHALStream*> output_streams_; 175 std::list<AUHALStream*> output_streams_;
151 176
177 // Maps device IDs and their corresponding actual (I/O) buffer sizes for
178 // all output streams using the specific device.
179 std::map<AudioDeviceID, size_t> output_io_buffer_size_map_;
180
181 // Device ID set bu the PrepareForAttemptToIncreaseIOBufferSize() method.
182 // Only modified by real audio output stream implementations, i.e., not
183 // fake audio output streams.
184 AudioDeviceID device_id_used_by_closing_output_stream_;
185
186 // Set to true in PrepareForAttemptToIncreaseIOBufferSize() to prepare for
187 // a possible modification of the native I/O buffer size. Set to false after
188 // an attempt to increase the I/O buffer size has finalized.
189 bool try_to_increase_io_buffer_size_;
190
152 DISALLOW_COPY_AND_ASSIGN(AudioManagerMac); 191 DISALLOW_COPY_AND_ASSIGN(AudioManagerMac);
153 }; 192 };
154 193
155 } // namespace media 194 } // namespace media
156 195
157 #endif // MEDIA_AUDIO_MAC_AUDIO_MANAGER_MAC_H_ 196 #endif // MEDIA_AUDIO_MAC_AUDIO_MANAGER_MAC_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698