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