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