blob: eeebbf382f74391694296912674bd79d76ced7d8 [file] [log] [blame]
sjlee@webrtc.org4b425082012-09-10 17:58:21 +00001/*
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3 *
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
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef MODULES_AUDIO_DEVICE_IOS_AUDIO_DEVICE_IOS_H_
12#define MODULES_AUDIO_DEVICE_IOS_AUDIO_DEVICE_IOS_H_
sjlee@webrtc.org4b425082012-09-10 17:58:21 +000013
kwibergf01633e2016-02-24 05:00:36 -080014#include <memory>
15
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "modules/audio_device/audio_device_generic.h"
17#include "modules/audio_device/ios/audio_session_observer.h"
18#include "modules/audio_device/ios/voice_processing_audio_unit.h"
19#include "rtc_base/buffer.h"
20#include "rtc_base/gtest_prod_util.h"
21#include "rtc_base/thread.h"
22#include "rtc_base/thread_annotations.h"
23#include "rtc_base/thread_checker.h"
Yves Gerey665174f2018-06-19 15:03:05 +020024#include "sdk/objc/Framework/Headers/WebRTC/RTCMacros.h"
tkchine54467f2016-03-15 16:54:03 -070025
26RTC_FWD_DECL_OBJC_CLASS(RTCAudioSessionDelegateAdapter);
sjlee@webrtc.org4b425082012-09-10 17:58:21 +000027
28namespace webrtc {
sjlee@webrtc.org4b425082012-09-10 17:58:21 +000029
henrika86d907c2015-09-07 16:09:50 +020030class FineAudioBuffer;
sjlee@webrtc.org4b425082012-09-10 17:58:21 +000031
henrika86d907c2015-09-07 16:09:50 +020032// Implements full duplex 16-bit mono PCM audio support for iOS using a
33// Voice-Processing (VP) I/O audio unit in Core Audio. The VP I/O audio unit
34// supports audio echo cancellation. It also adds automatic gain control,
35// adjustment of voice-processing quality and muting.
36//
37// An instance must be created and destroyed on one and the same thread.
38// All supported public methods must also be called on the same thread.
henrikg91d6ede2015-09-17 00:24:34 -070039// A thread checker will RTC_DCHECK if any supported method is called on an
40// invalid thread.
henrika86d907c2015-09-07 16:09:50 +020041//
42// Recorded audio will be delivered on a real-time internal I/O thread in the
43// audio unit. The audio unit will also ask for audio data to play out on this
44// same thread.
tkchine54467f2016-03-15 16:54:03 -070045class AudioDeviceIOS : public AudioDeviceGeneric,
Zeke Chin1300caa2016-03-18 14:39:11 -070046 public AudioSessionObserver,
tkchind2511962016-05-06 18:54:15 -070047 public VoiceProcessingAudioUnitObserver,
48 public rtc::MessageHandler {
tkchin@webrtc.org122caa52014-07-15 20:20:47 +000049 public:
henrikaba35d052015-07-14 17:04:08 +020050 AudioDeviceIOS();
tkchin@webrtc.org122caa52014-07-15 20:20:47 +000051 ~AudioDeviceIOS();
sjlee@webrtc.org4b425082012-09-10 17:58:21 +000052
henrikaba35d052015-07-14 17:04:08 +020053 void AttachAudioBuffer(AudioDeviceBuffer* audioBuffer) override;
sjlee@webrtc.org4b425082012-09-10 17:58:21 +000054
Max Morin84cab202016-07-01 13:35:19 +020055 InitStatus Init() override;
henrikaba35d052015-07-14 17:04:08 +020056 int32_t Terminate() override;
henrikaaf35f832017-06-16 13:22:13 +020057 bool Initialized() const override;
sjlee@webrtc.org4b425082012-09-10 17:58:21 +000058
henrikaba35d052015-07-14 17:04:08 +020059 int32_t InitPlayout() override;
henrikaaf35f832017-06-16 13:22:13 +020060 bool PlayoutIsInitialized() const override;
sjlee@webrtc.org4b425082012-09-10 17:58:21 +000061
henrikaba35d052015-07-14 17:04:08 +020062 int32_t InitRecording() override;
henrikaaf35f832017-06-16 13:22:13 +020063 bool RecordingIsInitialized() const override;
sjlee@webrtc.org4b425082012-09-10 17:58:21 +000064
henrikaba35d052015-07-14 17:04:08 +020065 int32_t StartPlayout() override;
66 int32_t StopPlayout() override;
pbos46ad5422015-12-07 14:29:14 -080067 bool Playing() const override { return playing_; }
sjlee@webrtc.org4b425082012-09-10 17:58:21 +000068
henrikaba35d052015-07-14 17:04:08 +020069 int32_t StartRecording() override;
70 int32_t StopRecording() override;
pbos46ad5422015-12-07 14:29:14 -080071 bool Recording() const override { return recording_; }
sjlee@webrtc.org4b425082012-09-10 17:58:21 +000072
henrika86d907c2015-09-07 16:09:50 +020073 // These methods returns hard-coded delay values and not dynamic delay
74 // estimates. The reason is that iOS supports a built-in AEC and the WebRTC
75 // AEC will always be disabled in the Libjingle layer to avoid running two
76 // AEC implementations at the same time. And, it saves resources to avoid
77 // updating these delay values continuously.
78 // TODO(henrika): it would be possible to mark these two methods as not
79 // implemented since they are only called for A/V-sync purposes today and
80 // A/V-sync is not supported on iOS. However, we avoid adding error messages
81 // the log by using these dummy implementations instead.
henrikaba35d052015-07-14 17:04:08 +020082 int32_t PlayoutDelay(uint16_t& delayMS) const override;
sjlee@webrtc.org4b425082012-09-10 17:58:21 +000083
henrikaba35d052015-07-14 17:04:08 +020084 // Native audio parameters stored during construction.
henrika86d907c2015-09-07 16:09:50 +020085 // These methods are unique for the iOS implementation.
henrikaba35d052015-07-14 17:04:08 +020086 int GetPlayoutAudioParameters(AudioParameters* params) const override;
87 int GetRecordAudioParameters(AudioParameters* params) const override;
sjlee@webrtc.org4b425082012-09-10 17:58:21 +000088
henrika86d907c2015-09-07 16:09:50 +020089 // These methods are currently not fully implemented on iOS:
sjlee@webrtc.org4b425082012-09-10 17:58:21 +000090
henrika86d907c2015-09-07 16:09:50 +020091 // See audio_device_not_implemented.cc for trivial implementations.
kjellander080a1e32016-05-25 11:37:11 -070092 int32_t ActiveAudioLayer(
93 AudioDeviceModule::AudioLayer& audioLayer) const override;
henrikaba35d052015-07-14 17:04:08 +020094 int32_t PlayoutIsAvailable(bool& available) override;
95 int32_t RecordingIsAvailable(bool& available) override;
henrikaba35d052015-07-14 17:04:08 +020096 int16_t PlayoutDevices() override;
97 int16_t RecordingDevices() override;
98 int32_t PlayoutDeviceName(uint16_t index,
99 char name[kAdmMaxDeviceNameSize],
100 char guid[kAdmMaxGuidSize]) override;
101 int32_t RecordingDeviceName(uint16_t index,
102 char name[kAdmMaxDeviceNameSize],
103 char guid[kAdmMaxGuidSize]) override;
104 int32_t SetPlayoutDevice(uint16_t index) override;
105 int32_t SetPlayoutDevice(
106 AudioDeviceModule::WindowsDeviceType device) override;
107 int32_t SetRecordingDevice(uint16_t index) override;
108 int32_t SetRecordingDevice(
109 AudioDeviceModule::WindowsDeviceType device) override;
henrikaba35d052015-07-14 17:04:08 +0200110 int32_t InitSpeaker() override;
111 bool SpeakerIsInitialized() const override;
112 int32_t InitMicrophone() override;
113 bool MicrophoneIsInitialized() const override;
114 int32_t SpeakerVolumeIsAvailable(bool& available) override;
115 int32_t SetSpeakerVolume(uint32_t volume) override;
116 int32_t SpeakerVolume(uint32_t& volume) const override;
117 int32_t MaxSpeakerVolume(uint32_t& maxVolume) const override;
118 int32_t MinSpeakerVolume(uint32_t& minVolume) const override;
henrikaba35d052015-07-14 17:04:08 +0200119 int32_t MicrophoneVolumeIsAvailable(bool& available) override;
120 int32_t SetMicrophoneVolume(uint32_t volume) override;
121 int32_t MicrophoneVolume(uint32_t& volume) const override;
122 int32_t MaxMicrophoneVolume(uint32_t& maxVolume) const override;
123 int32_t MinMicrophoneVolume(uint32_t& minVolume) const override;
henrikaba35d052015-07-14 17:04:08 +0200124 int32_t MicrophoneMuteIsAvailable(bool& available) override;
125 int32_t SetMicrophoneMute(bool enable) override;
126 int32_t MicrophoneMute(bool& enabled) const override;
127 int32_t SpeakerMuteIsAvailable(bool& available) override;
128 int32_t SetSpeakerMute(bool enable) override;
129 int32_t SpeakerMute(bool& enabled) const override;
henrikaba35d052015-07-14 17:04:08 +0200130 int32_t StereoPlayoutIsAvailable(bool& available) override;
131 int32_t SetStereoPlayout(bool enable) override;
132 int32_t StereoPlayout(bool& enabled) const override;
133 int32_t StereoRecordingIsAvailable(bool& available) override;
134 int32_t SetStereoRecording(bool enable) override;
135 int32_t StereoRecording(bool& enabled) const override;
sjlee@webrtc.org4b425082012-09-10 17:58:21 +0000136
tkchine54467f2016-03-15 16:54:03 -0700137 // AudioSessionObserver methods. May be called from any thread.
138 void OnInterruptionBegin() override;
139 void OnInterruptionEnd() override;
140 void OnValidRouteChange() override;
tkchind2511962016-05-06 18:54:15 -0700141 void OnCanPlayOrRecordChange(bool can_play_or_record) override;
henrikaaf35f832017-06-16 13:22:13 +0200142 void OnChangedOutputVolume() override;
tkchine54467f2016-03-15 16:54:03 -0700143
Zeke Chin1300caa2016-03-18 14:39:11 -0700144 // VoiceProcessingAudioUnitObserver methods.
145 OSStatus OnDeliverRecordedData(AudioUnitRenderActionFlags* flags,
146 const AudioTimeStamp* time_stamp,
147 UInt32 bus_number,
148 UInt32 num_frames,
149 AudioBufferList* io_data) override;
150 OSStatus OnGetPlayoutData(AudioUnitRenderActionFlags* flags,
151 const AudioTimeStamp* time_stamp,
152 UInt32 bus_number,
153 UInt32 num_frames,
154 AudioBufferList* io_data) override;
155
tkchind2511962016-05-06 18:54:15 -0700156 // Handles messages from posts.
Yves Gerey665174f2018-06-19 15:03:05 +0200157 void OnMessage(rtc::Message* msg) override;
tkchind2511962016-05-06 18:54:15 -0700158
tkchin@webrtc.org122caa52014-07-15 20:20:47 +0000159 private:
tkchine54467f2016-03-15 16:54:03 -0700160 // Called by the relevant AudioSessionObserver methods on |thread_|.
161 void HandleInterruptionBegin();
162 void HandleInterruptionEnd();
163 void HandleValidRouteChange();
tkchind2511962016-05-06 18:54:15 -0700164 void HandleCanPlayOrRecordChange(bool can_play_or_record);
165 void HandleSampleRateChange(float sample_rate);
henrika7be78832017-06-13 17:34:16 +0200166 void HandlePlayoutGlitchDetected();
henrikaaf35f832017-06-16 13:22:13 +0200167 void HandleOutputVolumeChange();
tkchine54467f2016-03-15 16:54:03 -0700168
henrika8c471e72015-10-01 07:36:45 -0700169 // Uses current |playout_parameters_| and |record_parameters_| to inform the
henrika86d907c2015-09-07 16:09:50 +0200170 // audio device buffer (ADB) about our internal audio parameters.
171 void UpdateAudioDeviceBuffer();
sjlee@webrtc.org4b425082012-09-10 17:58:21 +0000172
henrika86d907c2015-09-07 16:09:50 +0200173 // Since the preferred audio parameters are only hints to the OS, the actual
174 // values may be different once the AVAudioSession has been activated.
175 // This method asks for the current hardware parameters and takes actions
176 // if they should differ from what we have asked for initially. It also
henrika8c471e72015-10-01 07:36:45 -0700177 // defines |playout_parameters_| and |record_parameters_|.
henrika86d907c2015-09-07 16:09:50 +0200178 void SetupAudioBuffersForActiveAudioSession();
sjlee@webrtc.org4b425082012-09-10 17:58:21 +0000179
Zeke Chin1300caa2016-03-18 14:39:11 -0700180 // Creates the audio unit.
181 bool CreateAudioUnit();
sjlee@webrtc.org4b425082012-09-10 17:58:21 +0000182
tkchind2511962016-05-06 18:54:15 -0700183 // Updates the audio unit state based on current state.
184 void UpdateAudioUnit(bool can_play_or_record);
185
186 // Configures the audio session for WebRTC.
jttehf84c1d62017-04-21 13:56:39 -0700187 bool ConfigureAudioSession();
tkchind2511962016-05-06 18:54:15 -0700188 // Unconfigures the audio session.
189 void UnconfigureAudioSession();
henrika45c136b2015-10-21 04:11:53 -0700190
henrika8c471e72015-10-01 07:36:45 -0700191 // Activates our audio session, creates and initializes the voice-processing
henrika86d907c2015-09-07 16:09:50 +0200192 // audio unit and verifies that we got the preferred native audio parameters.
193 bool InitPlayOrRecord();
sjlee@webrtc.org4b425082012-09-10 17:58:21 +0000194
henrika86d907c2015-09-07 16:09:50 +0200195 // Closes and deletes the voice-processing I/O unit.
henrika34911ad2015-11-20 15:47:09 +0100196 void ShutdownPlayOrRecord();
197
henrika79445ea2018-05-29 16:04:16 +0200198 // Resets thread-checkers before a call is restarted.
199 void PrepareForNewStart();
200
henrika86d907c2015-09-07 16:09:50 +0200201 // Ensures that methods are called from the same thread as this object is
202 // created on.
henrika8c471e72015-10-01 07:36:45 -0700203 rtc::ThreadChecker thread_checker_;
henrikaaf35f832017-06-16 13:22:13 +0200204
205 // Native I/O audio thread checker.
206 rtc::ThreadChecker io_thread_checker_;
207
tkchine54467f2016-03-15 16:54:03 -0700208 // Thread that this object is created on.
209 rtc::Thread* thread_;
henrikaba35d052015-07-14 17:04:08 +0200210
211 // Raw pointer handle provided to us in AttachAudioBuffer(). Owned by the
Peter Boström4adbbcf2016-05-03 15:51:26 -0400212 // AudioDeviceModuleImpl class and called by AudioDeviceModule::Create().
henrikaba35d052015-07-14 17:04:08 +0200213 // The AudioDeviceBuffer is a member of the AudioDeviceModuleImpl instance
214 // and therefore outlives this object.
henrika8c471e72015-10-01 07:36:45 -0700215 AudioDeviceBuffer* audio_device_buffer_;
sjlee@webrtc.org4b425082012-09-10 17:58:21 +0000216
henrika86d907c2015-09-07 16:09:50 +0200217 // Contains audio parameters (sample rate, #channels, buffer size etc.) for
218 // the playout and recording sides. These structure is set in two steps:
219 // first, native sample rate and #channels are defined in Init(). Next, the
220 // audio session is activated and we verify that the preferred parameters
221 // were granted by the OS. At this stage it is also possible to add a third
222 // component to the parameters; the native I/O buffer duration.
henrikg91d6ede2015-09-17 00:24:34 -0700223 // A RTC_CHECK will be hit if we for some reason fail to open an audio session
henrika86d907c2015-09-07 16:09:50 +0200224 // using the specified parameters.
henrika8c471e72015-10-01 07:36:45 -0700225 AudioParameters playout_parameters_;
226 AudioParameters record_parameters_;
sjlee@webrtc.org4b425082012-09-10 17:58:21 +0000227
Zeke Chin1300caa2016-03-18 14:39:11 -0700228 // The AudioUnit used to play and record audio.
229 std::unique_ptr<VoiceProcessingAudioUnit> audio_unit_;
sjlee@webrtc.org4b425082012-09-10 17:58:21 +0000230
henrika86d907c2015-09-07 16:09:50 +0200231 // FineAudioBuffer takes an AudioDeviceBuffer which delivers audio data
232 // in chunks of 10ms. It then allows for this data to be pulled in
233 // a finer or coarser granularity. I.e. interacting with this class instead
234 // of directly with the AudioDeviceBuffer one can ask for any number of
235 // audio data samples. Is also supports a similar scheme for the recording
236 // side.
237 // Example: native buffer size can be 128 audio frames at 16kHz sample rate.
238 // WebRTC will provide 480 audio frames per 10ms but iOS asks for 128
239 // in each callback (one every 8ms). This class can then ask for 128 and the
240 // FineAudioBuffer will ask WebRTC for new data only when needed and also
241 // cache non-utilized audio between callbacks. On the recording side, iOS
242 // can provide audio data frames of size 128 and these are accumulated until
243 // enough data to supply one 10ms call exists. This 10ms chunk is then sent
244 // to WebRTC and the remaining part is stored.
kwibergf01633e2016-02-24 05:00:36 -0800245 std::unique_ptr<FineAudioBuffer> fine_audio_buffer_;
sjlee@webrtc.org4b425082012-09-10 17:58:21 +0000246
henrika86d907c2015-09-07 16:09:50 +0200247 // Temporary storage for recorded data. AudioUnitRender() renders into this
248 // array as soon as a frame of the desired buffer size has been recorded.
henrikabc9ffad2017-06-01 14:25:45 +0200249 // On real iOS devices, the size will be fixed and set once. For iOS
250 // simulators, the size can vary from callback to callback and the size
251 // will be changed dynamically to account for this behavior.
henrika8d7393b2018-04-19 13:40:15 +0200252 rtc::BufferT<int16_t> record_audio_buffer_;
henrika86d907c2015-09-07 16:09:50 +0200253
254 // Set to 1 when recording is active and 0 otherwise.
pbos46ad5422015-12-07 14:29:14 -0800255 volatile int recording_;
henrika86d907c2015-09-07 16:09:50 +0200256
257 // Set to 1 when playout is active and 0 otherwise.
pbos46ad5422015-12-07 14:29:14 -0800258 volatile int playing_;
henrika86d907c2015-09-07 16:09:50 +0200259
260 // Set to true after successful call to Init(), false otherwise.
Niels Möller1e062892018-02-07 10:18:32 +0100261 bool initialized_ RTC_GUARDED_BY(thread_checker_);
henrika86d907c2015-09-07 16:09:50 +0200262
henrika17802ae2016-09-21 04:55:04 -0700263 // Set to true after successful call to InitRecording() or InitPlayout(),
264 // false otherwise.
265 bool audio_is_initialized_;
sjlee@webrtc.org4b425082012-09-10 17:58:21 +0000266
tkchine54467f2016-03-15 16:54:03 -0700267 // Set to true if audio session is interrupted, false otherwise.
268 bool is_interrupted_;
269
henrika86d907c2015-09-07 16:09:50 +0200270 // Audio interruption observer instance.
henrikaaf35f832017-06-16 13:22:13 +0200271 RTCAudioSessionDelegateAdapter* audio_session_observer_
Niels Möller1e062892018-02-07 10:18:32 +0100272 RTC_GUARDED_BY(thread_checker_);
tkchind2511962016-05-06 18:54:15 -0700273
274 // Set to true if we've activated the audio session.
Niels Möller1e062892018-02-07 10:18:32 +0100275 bool has_configured_session_ RTC_GUARDED_BY(thread_checker_);
jtteh5171a7f2017-05-09 15:09:37 -0700276
henrika7be78832017-06-13 17:34:16 +0200277 // Counts number of detected audio glitches on the playout side.
Niels Möller1e062892018-02-07 10:18:32 +0100278 int64_t num_detected_playout_glitches_ RTC_GUARDED_BY(thread_checker_);
279 int64_t last_playout_time_ RTC_GUARDED_BY(io_thread_checker_);
henrika7be78832017-06-13 17:34:16 +0200280
281 // Counts number of playout callbacks per call.
henrikaaf35f832017-06-16 13:22:13 +0200282 // The value isupdated on the native I/O thread and later read on the
283 // creating thread (see thread_checker_) but at this stage no audio is
284 // active. Hence, it is a "thread safe" design and no lock is needed.
henrika7be78832017-06-13 17:34:16 +0200285 int64_t num_playout_callbacks_;
286
henrikaaf35f832017-06-16 13:22:13 +0200287 // Contains the time for when the last output volume change was detected.
Niels Möller1e062892018-02-07 10:18:32 +0100288 int64_t last_output_volume_change_time_ RTC_GUARDED_BY(thread_checker_);
henrikaaf35f832017-06-16 13:22:13 +0200289
jtteh5171a7f2017-05-09 15:09:37 -0700290 // Exposes private members for testing purposes only.
291 FRIEND_TEST_ALL_PREFIXES(AudioDeviceTest, testInterruptedAudioSession);
sjlee@webrtc.org4b425082012-09-10 17:58:21 +0000292};
293
294} // namespace webrtc
295
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200296#endif // MODULES_AUDIO_DEVICE_IOS_AUDIO_DEVICE_IOS_H_