OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #ifndef WEBRTC_MODULES_AUDIO_DEVICE_IOS_AUDIO_DEVICE_IOS_H_ | 11 #ifndef WEBRTC_MODULES_AUDIO_DEVICE_IOS_AUDIO_DEVICE_IOS_H_ |
12 #define WEBRTC_MODULES_AUDIO_DEVICE_IOS_AUDIO_DEVICE_IOS_H_ | 12 #define WEBRTC_MODULES_AUDIO_DEVICE_IOS_AUDIO_DEVICE_IOS_H_ |
13 | 13 |
14 #include <memory> | 14 #include <memory> |
15 | 15 |
16 #include "WebRTC/RTCMacros.h" | 16 #include "WebRTC/RTCMacros.h" |
17 #include "webrtc/base/asyncinvoker.h" | |
18 #include "webrtc/base/thread.h" | 17 #include "webrtc/base/thread.h" |
19 #include "webrtc/base/thread_checker.h" | 18 #include "webrtc/base/thread_checker.h" |
20 #include "webrtc/modules/audio_device/audio_device_generic.h" | 19 #include "webrtc/modules/audio_device/audio_device_generic.h" |
21 #include "webrtc/modules/audio_device/ios/audio_session_observer.h" | 20 #include "webrtc/modules/audio_device/ios/audio_session_observer.h" |
22 #include "webrtc/modules/audio_device/ios/voice_processing_audio_unit.h" | 21 #include "webrtc/modules/audio_device/ios/voice_processing_audio_unit.h" |
23 | 22 |
24 RTC_FWD_DECL_OBJC_CLASS(RTCAudioSessionDelegateAdapter); | 23 RTC_FWD_DECL_OBJC_CLASS(RTCAudioSessionDelegateAdapter); |
25 | 24 |
26 namespace webrtc { | 25 namespace webrtc { |
27 | 26 |
28 class FineAudioBuffer; | 27 class FineAudioBuffer; |
29 | 28 |
30 // Implements full duplex 16-bit mono PCM audio support for iOS using a | 29 // Implements full duplex 16-bit mono PCM audio support for iOS using a |
31 // Voice-Processing (VP) I/O audio unit in Core Audio. The VP I/O audio unit | 30 // Voice-Processing (VP) I/O audio unit in Core Audio. The VP I/O audio unit |
32 // supports audio echo cancellation. It also adds automatic gain control, | 31 // supports audio echo cancellation. It also adds automatic gain control, |
33 // adjustment of voice-processing quality and muting. | 32 // adjustment of voice-processing quality and muting. |
34 // | 33 // |
35 // An instance must be created and destroyed on one and the same thread. | 34 // An instance must be created and destroyed on one and the same thread. |
36 // All supported public methods must also be called on the same thread. | 35 // All supported public methods must also be called on the same thread. |
37 // A thread checker will RTC_DCHECK if any supported method is called on an | 36 // A thread checker will RTC_DCHECK if any supported method is called on an |
38 // invalid thread. | 37 // invalid thread. |
39 // | 38 // |
40 // Recorded audio will be delivered on a real-time internal I/O thread in the | 39 // Recorded audio will be delivered on a real-time internal I/O thread in the |
41 // audio unit. The audio unit will also ask for audio data to play out on this | 40 // audio unit. The audio unit will also ask for audio data to play out on this |
42 // same thread. | 41 // same thread. |
43 class AudioDeviceIOS : public AudioDeviceGeneric, | 42 class AudioDeviceIOS : public AudioDeviceGeneric, |
44 public AudioSessionObserver, | 43 public AudioSessionObserver, |
45 public VoiceProcessingAudioUnitObserver { | 44 public VoiceProcessingAudioUnitObserver, |
| 45 public rtc::MessageHandler { |
46 public: | 46 public: |
47 AudioDeviceIOS(); | 47 AudioDeviceIOS(); |
48 ~AudioDeviceIOS(); | 48 ~AudioDeviceIOS(); |
49 | 49 |
50 void AttachAudioBuffer(AudioDeviceBuffer* audioBuffer) override; | 50 void AttachAudioBuffer(AudioDeviceBuffer* audioBuffer) override; |
51 | 51 |
52 int32_t Init() override; | 52 int32_t Init() override; |
53 int32_t Terminate() override; | 53 int32_t Terminate() override; |
54 bool Initialized() const override { return initialized_; } | 54 bool Initialized() const override { return initialized_; } |
55 | 55 |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
155 bool RecordingError() const override; | 155 bool RecordingError() const override; |
156 void ClearPlayoutWarning() override {} | 156 void ClearPlayoutWarning() override {} |
157 void ClearPlayoutError() override {} | 157 void ClearPlayoutError() override {} |
158 void ClearRecordingWarning() override {} | 158 void ClearRecordingWarning() override {} |
159 void ClearRecordingError() override {} | 159 void ClearRecordingError() override {} |
160 | 160 |
161 // AudioSessionObserver methods. May be called from any thread. | 161 // AudioSessionObserver methods. May be called from any thread. |
162 void OnInterruptionBegin() override; | 162 void OnInterruptionBegin() override; |
163 void OnInterruptionEnd() override; | 163 void OnInterruptionEnd() override; |
164 void OnValidRouteChange() override; | 164 void OnValidRouteChange() override; |
165 void OnConfiguredForWebRTC() override; | 165 void OnCanPlayOrRecordChange(bool can_play_or_record) override; |
166 | 166 |
167 // VoiceProcessingAudioUnitObserver methods. | 167 // VoiceProcessingAudioUnitObserver methods. |
168 OSStatus OnDeliverRecordedData(AudioUnitRenderActionFlags* flags, | 168 OSStatus OnDeliverRecordedData(AudioUnitRenderActionFlags* flags, |
169 const AudioTimeStamp* time_stamp, | 169 const AudioTimeStamp* time_stamp, |
170 UInt32 bus_number, | 170 UInt32 bus_number, |
171 UInt32 num_frames, | 171 UInt32 num_frames, |
172 AudioBufferList* io_data) override; | 172 AudioBufferList* io_data) override; |
173 OSStatus OnGetPlayoutData(AudioUnitRenderActionFlags* flags, | 173 OSStatus OnGetPlayoutData(AudioUnitRenderActionFlags* flags, |
174 const AudioTimeStamp* time_stamp, | 174 const AudioTimeStamp* time_stamp, |
175 UInt32 bus_number, | 175 UInt32 bus_number, |
176 UInt32 num_frames, | 176 UInt32 num_frames, |
177 AudioBufferList* io_data) override; | 177 AudioBufferList* io_data) override; |
| 178 void OnSampleRateChange(float sample_rate) override; |
| 179 |
| 180 // Handles messages from posts. |
| 181 void OnMessage(rtc::Message *msg) override; |
178 | 182 |
179 private: | 183 private: |
180 // Called by the relevant AudioSessionObserver methods on |thread_|. | 184 // Called by the relevant AudioSessionObserver methods on |thread_|. |
181 void HandleInterruptionBegin(); | 185 void HandleInterruptionBegin(); |
182 void HandleInterruptionEnd(); | 186 void HandleInterruptionEnd(); |
183 void HandleValidRouteChange(); | 187 void HandleValidRouteChange(); |
184 void HandleConfiguredForWebRTC(); | 188 void HandleCanPlayOrRecordChange(bool can_play_or_record); |
| 189 void HandleSampleRateChange(float sample_rate); |
185 | 190 |
186 // Uses current |playout_parameters_| and |record_parameters_| to inform the | 191 // Uses current |playout_parameters_| and |record_parameters_| to inform the |
187 // audio device buffer (ADB) about our internal audio parameters. | 192 // audio device buffer (ADB) about our internal audio parameters. |
188 void UpdateAudioDeviceBuffer(); | 193 void UpdateAudioDeviceBuffer(); |
189 | 194 |
190 // Since the preferred audio parameters are only hints to the OS, the actual | 195 // Since the preferred audio parameters are only hints to the OS, the actual |
191 // values may be different once the AVAudioSession has been activated. | 196 // values may be different once the AVAudioSession has been activated. |
192 // This method asks for the current hardware parameters and takes actions | 197 // This method asks for the current hardware parameters and takes actions |
193 // if they should differ from what we have asked for initially. It also | 198 // if they should differ from what we have asked for initially. It also |
194 // defines |playout_parameters_| and |record_parameters_|. | 199 // defines |playout_parameters_| and |record_parameters_|. |
195 void SetupAudioBuffersForActiveAudioSession(); | 200 void SetupAudioBuffersForActiveAudioSession(); |
196 | 201 |
197 // Creates the audio unit. | 202 // Creates the audio unit. |
198 bool CreateAudioUnit(); | 203 bool CreateAudioUnit(); |
199 | 204 |
200 // Restarts active audio streams using a new sample rate. Required when e.g. | 205 // Restarts active audio streams using a new sample rate. Required when e.g. |
201 // a BT headset is enabled or disabled. | 206 // a BT headset is enabled or disabled. |
202 bool RestartAudioUnit(float sample_rate); | 207 bool RestartAudioUnit(float sample_rate); |
203 | 208 |
| 209 // Configures the audio session for WebRTC. |
| 210 void ConfigureAudioSession(); |
| 211 // Unconfigures the audio session. |
| 212 void UnconfigureAudioSession(); |
| 213 |
204 // Activates our audio session, creates and initializes the voice-processing | 214 // Activates our audio session, creates and initializes the voice-processing |
205 // audio unit and verifies that we got the preferred native audio parameters. | 215 // audio unit and verifies that we got the preferred native audio parameters. |
206 bool InitPlayOrRecord(); | 216 bool InitPlayOrRecord(); |
207 | 217 |
208 // Closes and deletes the voice-processing I/O unit. | 218 // Closes and deletes the voice-processing I/O unit. |
209 void ShutdownPlayOrRecord(); | 219 void ShutdownPlayOrRecord(); |
210 | 220 |
211 // Ensures that methods are called from the same thread as this object is | 221 // Ensures that methods are called from the same thread as this object is |
212 // created on. | 222 // created on. |
213 rtc::ThreadChecker thread_checker_; | 223 rtc::ThreadChecker thread_checker_; |
214 // Thread that this object is created on. | 224 // Thread that this object is created on. |
215 rtc::Thread* thread_; | 225 rtc::Thread* thread_; |
216 // Invoker used to execute methods on thread_. | |
217 std::unique_ptr<rtc::AsyncInvoker> async_invoker_; | |
218 | 226 |
219 // Raw pointer handle provided to us in AttachAudioBuffer(). Owned by the | 227 // Raw pointer handle provided to us in AttachAudioBuffer(). Owned by the |
220 // AudioDeviceModuleImpl class and called by AudioDeviceModule::Create(). | 228 // AudioDeviceModuleImpl class and called by AudioDeviceModule::Create(). |
221 // The AudioDeviceBuffer is a member of the AudioDeviceModuleImpl instance | 229 // The AudioDeviceBuffer is a member of the AudioDeviceModuleImpl instance |
222 // and therefore outlives this object. | 230 // and therefore outlives this object. |
223 AudioDeviceBuffer* audio_device_buffer_; | 231 AudioDeviceBuffer* audio_device_buffer_; |
224 | 232 |
225 // Contains audio parameters (sample rate, #channels, buffer size etc.) for | 233 // Contains audio parameters (sample rate, #channels, buffer size etc.) for |
226 // the playout and recording sides. These structure is set in two steps: | 234 // the playout and recording sides. These structure is set in two steps: |
227 // first, native sample rate and #channels are defined in Init(). Next, the | 235 // first, native sample rate and #channels are defined in Init(). Next, the |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
277 bool rec_is_initialized_; | 285 bool rec_is_initialized_; |
278 | 286 |
279 // Set to true after successful call to InitPlayout(), false otherwise. | 287 // Set to true after successful call to InitPlayout(), false otherwise. |
280 bool play_is_initialized_; | 288 bool play_is_initialized_; |
281 | 289 |
282 // Set to true if audio session is interrupted, false otherwise. | 290 // Set to true if audio session is interrupted, false otherwise. |
283 bool is_interrupted_; | 291 bool is_interrupted_; |
284 | 292 |
285 // Audio interruption observer instance. | 293 // Audio interruption observer instance. |
286 RTCAudioSessionDelegateAdapter* audio_session_observer_; | 294 RTCAudioSessionDelegateAdapter* audio_session_observer_; |
| 295 |
| 296 // Set to true if we've activated the audio session. |
| 297 bool has_configured_session_; |
287 }; | 298 }; |
288 | 299 |
289 } // namespace webrtc | 300 } // namespace webrtc |
290 | 301 |
291 #endif // WEBRTC_MODULES_AUDIO_DEVICE_IOS_AUDIO_DEVICE_IOS_H_ | 302 #endif // WEBRTC_MODULES_AUDIO_DEVICE_IOS_AUDIO_DEVICE_IOS_H_ |
OLD | NEW |