| 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 <map> |
| 41 #include <stddef.h> | 42 #include <stddef.h> |
| 42 #include <stdint.h> | 43 #include <stdint.h> |
| 43 | 44 |
| 44 #include "base/atomicops.h" | 45 #include "base/atomicops.h" |
| 45 #include "base/cancelable_callback.h" | 46 #include "base/cancelable_callback.h" |
| 46 #include "base/macros.h" | 47 #include "base/macros.h" |
| 47 #include "base/memory/scoped_ptr.h" | 48 #include "base/memory/scoped_ptr.h" |
| 48 #include "base/synchronization/lock.h" | 49 #include "base/synchronization/lock.h" |
| 49 #include "base/threading/thread_checker.h" | 50 #include "base/threading/thread_checker.h" |
| 50 #include "base/time/time.h" | 51 #include "base/time/time.h" |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 87 | 88 |
| 88 // Returns true if the audio unit is active/running. | 89 // Returns true if the audio unit is active/running. |
| 89 // The result is based on the kAudioOutputUnitProperty_IsRunning property | 90 // The result is based on the kAudioOutputUnitProperty_IsRunning property |
| 90 // which exists for output units. | 91 // which exists for output units. |
| 91 bool IsRunning(); | 92 bool IsRunning(); |
| 92 | 93 |
| 93 AudioDeviceID device_id() const { return input_device_id_; } | 94 AudioDeviceID device_id() const { return input_device_id_; } |
| 94 size_t requested_buffer_size() const { return number_of_frames_; } | 95 size_t requested_buffer_size() const { return number_of_frames_; } |
| 95 | 96 |
| 96 private: | 97 private: |
| 98 static const AudioObjectPropertyAddress kDeviceChangePropertyAddress; |
| 99 |
| 97 // Callback functions called on a real-time priority I/O thread from the audio | 100 // 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. | 101 // unit. These methods are called when recorded audio is available. |
| 99 static OSStatus DataIsAvailable(void* context, | 102 static OSStatus DataIsAvailable(void* context, |
| 100 AudioUnitRenderActionFlags* flags, | 103 AudioUnitRenderActionFlags* flags, |
| 101 const AudioTimeStamp* time_stamp, | 104 const AudioTimeStamp* time_stamp, |
| 102 UInt32 bus_number, | 105 UInt32 bus_number, |
| 103 UInt32 number_of_frames, | 106 UInt32 number_of_frames, |
| 104 AudioBufferList* io_data); | 107 AudioBufferList* io_data); |
| 105 OSStatus OnDataIsAvailable(AudioUnitRenderActionFlags* flags, | 108 OSStatus OnDataIsAvailable(AudioUnitRenderActionFlags* flags, |
| 106 const AudioTimeStamp* time_stamp, | 109 const AudioTimeStamp* time_stamp, |
| 107 UInt32 bus_number, | 110 UInt32 bus_number, |
| 108 UInt32 number_of_frames); | 111 UInt32 number_of_frames); |
| 109 | 112 |
| 110 // Pushes recorded data to consumer of the input audio stream. | 113 // Pushes recorded data to consumer of the input audio stream. |
| 111 OSStatus Provide(UInt32 number_of_frames, AudioBufferList* io_data, | 114 OSStatus Provide(UInt32 number_of_frames, AudioBufferList* io_data, |
| 112 const AudioTimeStamp* time_stamp); | 115 const AudioTimeStamp* time_stamp); |
| 113 | 116 |
| 117 // Callback functions called on different system threads from the Core Audio |
| 118 // framework. These methods are called when device properties are changed. |
| 119 static OSStatus OnDevicePropertyChanged( |
| 120 AudioObjectID object_id, |
| 121 UInt32 num_addresses, |
| 122 const AudioObjectPropertyAddress addresses[], |
| 123 void* context); |
| 124 OSStatus DevicePropertyChanged(AudioObjectID object_id, |
| 125 UInt32 num_addresses, |
| 126 const AudioObjectPropertyAddress addresses[]); |
| 127 |
| 128 // Registers OnDevicePropertyChanged() to receive notifications when device |
| 129 // properties changes. |
| 130 void RegisterDeviceChangeListener(); |
| 131 // Stop listening for changes in device properties. |
| 132 void DeRegisterDeviceChangeListener(); |
| 133 |
| 114 // Gets the fixed capture hardware latency and store it during initialization. | 134 // Gets the fixed capture hardware latency and store it during initialization. |
| 115 // Returns 0 if not available. | 135 // Returns 0 if not available. |
| 116 double GetHardwareLatency(); | 136 double GetHardwareLatency(); |
| 117 | 137 |
| 118 // Gets the current capture delay value. | 138 // Gets the current capture delay value. |
| 119 double GetCaptureLatency(const AudioTimeStamp* input_time_stamp); | 139 double GetCaptureLatency(const AudioTimeStamp* input_time_stamp); |
| 120 | 140 |
| 121 // Gets the number of channels for a stream of audio data. | 141 // Gets the number of channels for a stream of audio data. |
| 122 int GetNumberOfChannelsFromStream(); | 142 int GetNumberOfChannelsFromStream(); |
| 123 | 143 |
| 124 // Issues the OnError() callback to the |sink_|. | 144 // Issues the OnError() callback to the |sink_|. |
| 125 void HandleError(OSStatus err); | 145 void HandleError(OSStatus err); |
| 126 | 146 |
| 127 // Helper function to check if the volume control is avialable on specific | 147 // Helper function to check if the volume control is avialable on specific |
| 128 // channel. | 148 // channel. |
| 129 bool IsVolumeSettableOnChannel(int channel); | 149 bool IsVolumeSettableOnChannel(int channel); |
| 130 | 150 |
| 131 // Helper methods to set and get atomic |input_callback_is_active_|. | 151 // Helper methods to set and get atomic |input_callback_is_active_|. |
| 132 void SetInputCallbackIsActive(bool active); | 152 void SetInputCallbackIsActive(bool active); |
| 133 bool GetInputCallbackIsActive(); | 153 bool GetInputCallbackIsActive(); |
| 134 | 154 |
| 155 // Helper methods to set and get atomic |device_listener_is_active_|. |
| 156 void SetDeviceListenerIsActive(bool active); |
| 157 bool GetDeviceListenerIsActive(); |
| 158 |
| 135 // Checks if a stream was started successfully and the audio unit also starts | 159 // Checks if a stream was started successfully and the audio unit also starts |
| 136 // to call InputProc() as it should. This method is called once when a timer | 160 // to call InputProc() as it should. This method is called once when a timer |
| 137 // expires 5 seconds after calling Start(). | 161 // expires 5 seconds after calling Start(). |
| 138 void CheckInputStartupSuccess(); | 162 void CheckInputStartupSuccess(); |
| 139 | 163 |
| 140 // Uninitializes the audio unit if needed. | 164 // Uninitializes the audio unit if needed. |
| 141 void CloseAudioUnit(); | 165 void CloseAudioUnit(); |
| 142 | 166 |
| 143 // Adds extra UMA stats when it has been detected that startup failed. | 167 // Adds extra UMA stats when it has been detected that startup failed. |
| 144 void AddHistogramsForFailedStartup(); | 168 void AddHistogramsForFailedStartup(); |
| 145 | 169 |
| 170 // TODO(henrika): add comments |
| 171 void AddDevicePropertyChangesToUMA(); |
| 172 |
| 146 // Verifies that Open(), Start(), Stop() and Close() are all called on the | 173 // Verifies that Open(), Start(), Stop() and Close() are all called on the |
| 147 // creating thread which is the main browser thread (CrBrowserMain) on Mac. | 174 // creating thread which is the main browser thread (CrBrowserMain) on Mac. |
| 148 base::ThreadChecker thread_checker_; | 175 base::ThreadChecker thread_checker_; |
| 149 | 176 |
| 150 // Our creator, the audio manager needs to be notified when we close. | 177 // Our creator, the audio manager needs to be notified when we close. |
| 151 AudioManagerMac* const manager_; | 178 AudioManagerMac* const manager_; |
| 152 | 179 |
| 153 // Contains the desired number of audio frames in each callback. | 180 // Contains the desired number of audio frames in each callback. |
| 154 const size_t number_of_frames_; | 181 const size_t number_of_frames_; |
| 155 | 182 |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 211 // See AudioManagerMac::ShouldDeferStreamStart() for details. | 238 // See AudioManagerMac::ShouldDeferStreamStart() for details. |
| 212 bool start_was_deferred_; | 239 bool start_was_deferred_; |
| 213 | 240 |
| 214 // Set to true if the audio unit's IO buffer was changed when Open() was | 241 // Set to true if the audio unit's IO buffer was changed when Open() was |
| 215 // called. | 242 // called. |
| 216 bool buffer_size_was_changed_; | 243 bool buffer_size_was_changed_; |
| 217 | 244 |
| 218 // Set to true once when AudioUnitRender() succeeds for the first time. | 245 // Set to true once when AudioUnitRender() succeeds for the first time. |
| 219 bool audio_unit_render_has_worked_; | 246 bool audio_unit_render_has_worked_; |
| 220 | 247 |
| 248 std::map<UInt32, int> device_property_changes_map_; |
| 249 |
| 250 // Set to true when we are listening for changes in device properties. |
| 251 base::subtle::Atomic32 device_listener_is_active_; |
| 252 |
| 221 DISALLOW_COPY_AND_ASSIGN(AUAudioInputStream); | 253 DISALLOW_COPY_AND_ASSIGN(AUAudioInputStream); |
| 222 }; | 254 }; |
| 223 | 255 |
| 224 } // namespace media | 256 } // namespace media |
| 225 | 257 |
| 226 #endif // MEDIA_AUDIO_MAC_AUDIO_LOW_LATENCY_INPUT_MAC_H_ | 258 #endif // MEDIA_AUDIO_MAC_AUDIO_LOW_LATENCY_INPUT_MAC_H_ |
| OLD | NEW |