| 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 // Implementation of AudioInputStream for Mac OS X using the special AUHAL | 5 // Implementation of AudioInputStream for Mac OS X using the special AUHAL |
| 6 // input Audio Unit present in OS 10.4 and later. | 6 // input Audio Unit present in OS 10.4 and later. |
| 7 // The AUHAL input Audio Unit is for low-latency audio I/O. | 7 // The AUHAL input Audio Unit is for low-latency audio I/O. |
| 8 // | 8 // |
| 9 // Overview of operation: | 9 // Overview of operation: |
| 10 // | 10 // |
| (...skipping 20 matching lines...) Expand all Loading... |
| 31 // 1) Hardware latency, which includes Audio Unit latency, audio device | 31 // 1) Hardware latency, which includes Audio Unit latency, audio device |
| 32 // latency; | 32 // latency; |
| 33 // 2) The delay between the actual recording instant and the time when the | 33 // 2) The delay between the actual recording instant and the time when the |
| 34 // data packet is provided as a callback. | 34 // data packet is provided as a callback. |
| 35 // | 35 // |
| 36 #ifndef MEDIA_AUDIO_MAC_AUDIO_LOW_LATENCY_INPUT_MAC_H_ | 36 #ifndef MEDIA_AUDIO_MAC_AUDIO_LOW_LATENCY_INPUT_MAC_H_ |
| 37 #define MEDIA_AUDIO_MAC_AUDIO_LOW_LATENCY_INPUT_MAC_H_ | 37 #define MEDIA_AUDIO_MAC_AUDIO_LOW_LATENCY_INPUT_MAC_H_ |
| 38 | 38 |
| 39 #include <AudioUnit/AudioUnit.h> | 39 #include <AudioUnit/AudioUnit.h> |
| 40 #include <CoreAudio/CoreAudio.h> | 40 #include <CoreAudio/CoreAudio.h> |
| 41 #include <stddef.h> | 41 #include <map> |
| 42 #include <stdint.h> | |
| 43 | 42 |
| 44 #include "base/atomicops.h" | 43 #include "base/atomicops.h" |
| 45 #include "base/cancelable_callback.h" | 44 #include "base/cancelable_callback.h" |
| 46 #include "base/macros.h" | 45 #include "base/macros.h" |
| 47 #include "base/memory/scoped_ptr.h" | 46 #include "base/memory/scoped_ptr.h" |
| 48 #include "base/synchronization/lock.h" | |
| 49 #include "base/threading/thread_checker.h" | 47 #include "base/threading/thread_checker.h" |
| 50 #include "base/time/time.h" | 48 #include "base/time/time.h" |
| 51 #include "base/timer/timer.h" | 49 #include "base/timer/timer.h" |
| 52 #include "media/audio/agc_audio_stream.h" | 50 #include "media/audio/agc_audio_stream.h" |
| 53 #include "media/audio/audio_io.h" | 51 #include "media/audio/audio_io.h" |
| 54 #include "media/audio/audio_parameters.h" | 52 #include "media/audio/audio_parameters.h" |
| 55 #include "media/base/audio_block_fifo.h" | 53 #include "media/base/audio_block_fifo.h" |
| 56 | 54 |
| 57 namespace media { | 55 namespace media { |
| 58 | 56 |
| (...skipping 28 matching lines...) Expand all Loading... |
| 87 | 85 |
| 88 // Returns true if the audio unit is active/running. | 86 // Returns true if the audio unit is active/running. |
| 89 // The result is based on the kAudioOutputUnitProperty_IsRunning property | 87 // The result is based on the kAudioOutputUnitProperty_IsRunning property |
| 90 // which exists for output units. | 88 // which exists for output units. |
| 91 bool IsRunning(); | 89 bool IsRunning(); |
| 92 | 90 |
| 93 AudioDeviceID device_id() const { return input_device_id_; } | 91 AudioDeviceID device_id() const { return input_device_id_; } |
| 94 size_t requested_buffer_size() const { return number_of_frames_; } | 92 size_t requested_buffer_size() const { return number_of_frames_; } |
| 95 | 93 |
| 96 private: | 94 private: |
| 95 static const AudioObjectPropertyAddress kDeviceChangePropertyAddress; |
| 96 |
| 97 // Callback functions called on a real-time priority I/O thread from the audio | 97 // Callback functions called on a real-time priority I/O thread from the audio |
| 98 // unit. These methods are called when recorded audio is available. | 98 // unit. These methods are called when recorded audio is available. |
| 99 static OSStatus DataIsAvailable(void* context, | 99 static OSStatus DataIsAvailable(void* context, |
| 100 AudioUnitRenderActionFlags* flags, | 100 AudioUnitRenderActionFlags* flags, |
| 101 const AudioTimeStamp* time_stamp, | 101 const AudioTimeStamp* time_stamp, |
| 102 UInt32 bus_number, | 102 UInt32 bus_number, |
| 103 UInt32 number_of_frames, | 103 UInt32 number_of_frames, |
| 104 AudioBufferList* io_data); | 104 AudioBufferList* io_data); |
| 105 OSStatus OnDataIsAvailable(AudioUnitRenderActionFlags* flags, | 105 OSStatus OnDataIsAvailable(AudioUnitRenderActionFlags* flags, |
| 106 const AudioTimeStamp* time_stamp, | 106 const AudioTimeStamp* time_stamp, |
| 107 UInt32 bus_number, | 107 UInt32 bus_number, |
| 108 UInt32 number_of_frames); | 108 UInt32 number_of_frames); |
| 109 | 109 |
| 110 // Pushes recorded data to consumer of the input audio stream. | 110 // Pushes recorded data to consumer of the input audio stream. |
| 111 OSStatus Provide(UInt32 number_of_frames, AudioBufferList* io_data, | 111 OSStatus Provide(UInt32 number_of_frames, AudioBufferList* io_data, |
| 112 const AudioTimeStamp* time_stamp); | 112 const AudioTimeStamp* time_stamp); |
| 113 | 113 |
| 114 // Callback functions called on different system threads from the Core Audio |
| 115 // framework. These methods are called when device properties are changed. |
| 116 static OSStatus OnDevicePropertyChanged( |
| 117 AudioObjectID object_id, |
| 118 UInt32 num_addresses, |
| 119 const AudioObjectPropertyAddress addresses[], |
| 120 void* context); |
| 121 OSStatus DevicePropertyChanged(AudioObjectID object_id, |
| 122 UInt32 num_addresses, |
| 123 const AudioObjectPropertyAddress addresses[]); |
| 124 |
| 125 // Registers OnDevicePropertyChanged() to receive notifications when device |
| 126 // properties changes. |
| 127 void RegisterDeviceChangeListener(); |
| 128 // Stop listening for changes in device properties. |
| 129 void DeRegisterDeviceChangeListener(); |
| 130 |
| 114 // Gets the fixed capture hardware latency and store it during initialization. | 131 // Gets the fixed capture hardware latency and store it during initialization. |
| 115 // Returns 0 if not available. | 132 // Returns 0 if not available. |
| 116 double GetHardwareLatency(); | 133 double GetHardwareLatency(); |
| 117 | 134 |
| 118 // Gets the current capture delay value. | 135 // Gets the current capture delay value. |
| 119 double GetCaptureLatency(const AudioTimeStamp* input_time_stamp); | 136 double GetCaptureLatency(const AudioTimeStamp* input_time_stamp); |
| 120 | 137 |
| 121 // Gets the number of channels for a stream of audio data. | 138 // Gets the number of channels for a stream of audio data. |
| 122 int GetNumberOfChannelsFromStream(); | 139 int GetNumberOfChannelsFromStream(); |
| 123 | 140 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 136 // to call InputProc() as it should. This method is called once when a timer | 153 // to call InputProc() as it should. This method is called once when a timer |
| 137 // expires 5 seconds after calling Start(). | 154 // expires 5 seconds after calling Start(). |
| 138 void CheckInputStartupSuccess(); | 155 void CheckInputStartupSuccess(); |
| 139 | 156 |
| 140 // Uninitializes the audio unit if needed. | 157 // Uninitializes the audio unit if needed. |
| 141 void CloseAudioUnit(); | 158 void CloseAudioUnit(); |
| 142 | 159 |
| 143 // Adds extra UMA stats when it has been detected that startup failed. | 160 // Adds extra UMA stats when it has been detected that startup failed. |
| 144 void AddHistogramsForFailedStartup(); | 161 void AddHistogramsForFailedStartup(); |
| 145 | 162 |
| 163 // Scans the map of all available property changes (notification types) and |
| 164 // filters out some that make sense to add to UMA stats. |
| 165 void AddDevicePropertyChangesToUMA(bool startup_failed); |
| 166 |
| 146 // Verifies that Open(), Start(), Stop() and Close() are all called on the | 167 // Verifies that Open(), Start(), Stop() and Close() are all called on the |
| 147 // creating thread which is the main browser thread (CrBrowserMain) on Mac. | 168 // creating thread which is the main browser thread (CrBrowserMain) on Mac. |
| 148 base::ThreadChecker thread_checker_; | 169 base::ThreadChecker thread_checker_; |
| 149 | 170 |
| 150 // Our creator, the audio manager needs to be notified when we close. | 171 // Our creator, the audio manager needs to be notified when we close. |
| 151 AudioManagerMac* const manager_; | 172 AudioManagerMac* const manager_; |
| 152 | 173 |
| 153 // Contains the desired number of audio frames in each callback. | 174 // Contains the desired number of audio frames in each callback. |
| 154 const size_t number_of_frames_; | 175 const size_t number_of_frames_; |
| 155 | 176 |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 211 // See AudioManagerMac::ShouldDeferStreamStart() for details. | 232 // See AudioManagerMac::ShouldDeferStreamStart() for details. |
| 212 bool start_was_deferred_; | 233 bool start_was_deferred_; |
| 213 | 234 |
| 214 // Set to true if the audio unit's IO buffer was changed when Open() was | 235 // Set to true if the audio unit's IO buffer was changed when Open() was |
| 215 // called. | 236 // called. |
| 216 bool buffer_size_was_changed_; | 237 bool buffer_size_was_changed_; |
| 217 | 238 |
| 218 // Set to true once when AudioUnitRender() succeeds for the first time. | 239 // Set to true once when AudioUnitRender() succeeds for the first time. |
| 219 bool audio_unit_render_has_worked_; | 240 bool audio_unit_render_has_worked_; |
| 220 | 241 |
| 242 // Maps unique representations of device property notification types and |
| 243 // number of times we have been notified about a change in such a type. |
| 244 // While the notifier is active, this member is modified by several different |
| 245 // internal thread. My guess is that a serial dispatch queue is used under |
| 246 // the hood and it executes one task at a time in the order in which they are |
| 247 // added to the queue. The currently executing task runs on a distinct thread |
| 248 // (which can vary from task to task) that is managed by the dispatch queue. |
| 249 // The map is always read on the creating thread but only while the notifier |
| 250 // is disabled, hence no lock is required. |
| 251 std::map<UInt32, int> device_property_changes_map_; |
| 252 |
| 253 // Set to true when we are listening for changes in device properties. |
| 254 // Only touched on the creating thread. |
| 255 bool device_listener_is_active_; |
| 256 |
| 221 DISALLOW_COPY_AND_ASSIGN(AUAudioInputStream); | 257 DISALLOW_COPY_AND_ASSIGN(AUAudioInputStream); |
| 222 }; | 258 }; |
| 223 | 259 |
| 224 } // namespace media | 260 } // namespace media |
| 225 | 261 |
| 226 #endif // MEDIA_AUDIO_MAC_AUDIO_LOW_LATENCY_INPUT_MAC_H_ | 262 #endif // MEDIA_AUDIO_MAC_AUDIO_LOW_LATENCY_INPUT_MAC_H_ |
| OLD | NEW |