blob: 56ba32b0185b065554a265a98b282dc5113f64a4 [file] [log] [blame]
Lu Liu4b620012017-04-06 10:17:01 -07001/*
2 * Copyright (c) 2017 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#include "modules/audio_device/include/audio_device_data_observer.h"
Yves Gerey988cc082018-10-23 12:03:01 +020012
13#include "modules/audio_device/include/audio_device_defines.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020014#include "rtc_base/checks.h"
Steve Anton10542f22019-01-11 09:11:00 -080015#include "rtc_base/ref_counted_object.h"
Lu Liu4b620012017-04-06 10:17:01 -070016
17namespace webrtc {
18
19namespace {
20
21// A wrapper over AudioDeviceModule that registers itself as AudioTransport
22// callback and redirects the PCM data to AudioDeviceDataObserver callback.
23class ADMWrapper : public AudioDeviceModule, public AudioTransport {
24 public:
henrika616e3132017-11-13 12:47:59 +010025 ADMWrapper(const AudioLayer audio_layer, AudioDeviceDataObserver* observer)
26 : impl_(AudioDeviceModule::Create(audio_layer)), observer_(observer) {
Lu Liu4b620012017-04-06 10:17:01 -070027 // Register self as the audio transport callback for underlying ADM impl.
28 auto res = impl_->RegisterAudioCallback(this);
29 is_valid_ = (impl_.get() != nullptr) && (res == 0);
30 }
31 virtual ~ADMWrapper() {
32 audio_transport_ = nullptr;
33 observer_ = nullptr;
34 }
35
36 // Make sure we have a valid ADM before returning it to user.
37 bool IsValid() { return is_valid_; }
38
Lu Liu4b620012017-04-06 10:17:01 -070039 // AudioTransport methods overrides.
40 int32_t RecordedDataIsAvailable(const void* audioSamples,
41 const size_t nSamples,
42 const size_t nBytesPerSample,
43 const size_t nChannels,
44 const uint32_t samples_per_sec,
45 const uint32_t total_delay_ms,
46 const int32_t clockDrift,
47 const uint32_t currentMicLevel,
48 const bool keyPressed,
49 uint32_t& newMicLevel) override {
50 int32_t res = 0;
51 // Capture PCM data of locally captured audio.
52 if (observer_) {
53 observer_->OnCaptureData(audioSamples, nSamples, nBytesPerSample,
54 nChannels, samples_per_sec);
55 }
56
57 // Send to the actual audio transport.
58 if (audio_transport_) {
59 res = audio_transport_->RecordedDataIsAvailable(
60 audioSamples, nSamples, nBytesPerSample, nChannels, samples_per_sec,
61 total_delay_ms, clockDrift, currentMicLevel, keyPressed, newMicLevel);
62 }
63
64 return res;
65 }
66
67 int32_t NeedMorePlayData(const size_t nSamples,
68 const size_t nBytesPerSample,
69 const size_t nChannels,
70 const uint32_t samples_per_sec,
71 void* audioSamples,
72 size_t& nSamplesOut,
73 int64_t* elapsed_time_ms,
74 int64_t* ntp_time_ms) override {
75 int32_t res = 0;
Artem Titovac9365e2018-03-28 15:21:54 +020076 // Set out parameters to safe values to be sure not to return corrupted
77 // data.
78 nSamplesOut = 0;
79 *elapsed_time_ms = -1;
80 *ntp_time_ms = -1;
Lu Liu4b620012017-04-06 10:17:01 -070081 // Request data from audio transport.
82 if (audio_transport_) {
83 res = audio_transport_->NeedMorePlayData(
84 nSamples, nBytesPerSample, nChannels, samples_per_sec, audioSamples,
85 nSamplesOut, elapsed_time_ms, ntp_time_ms);
86 }
87
88 // Capture rendered data.
89 if (observer_) {
90 observer_->OnRenderData(audioSamples, nSamples, nBytesPerSample,
91 nChannels, samples_per_sec);
92 }
93
94 return res;
95 }
96
Lu Liu4b620012017-04-06 10:17:01 -070097 void PullRenderData(int bits_per_sample,
98 int sample_rate,
99 size_t number_of_channels,
100 size_t number_of_frames,
101 void* audio_data,
102 int64_t* elapsed_time_ms,
103 int64_t* ntp_time_ms) override {
104 RTC_NOTREACHED();
105 }
106
107 // Override AudioDeviceModule's RegisterAudioCallback method to remember the
108 // actual audio transport (e.g.: voice engine).
109 int32_t RegisterAudioCallback(AudioTransport* audio_callback) override {
110 // Remember the audio callback to forward PCM data
111 audio_transport_ = audio_callback;
112 return 0;
113 }
114
115 // AudioDeviceModule pass through method overrides.
116 int32_t ActiveAudioLayer(AudioLayer* audio_layer) const override {
117 return impl_->ActiveAudioLayer(audio_layer);
118 }
Lu Liu4b620012017-04-06 10:17:01 -0700119 int32_t Init() override { return impl_->Init(); }
120 int32_t Terminate() override { return impl_->Terminate(); }
121 bool Initialized() const override { return impl_->Initialized(); }
122 int16_t PlayoutDevices() override { return impl_->PlayoutDevices(); }
123 int16_t RecordingDevices() override { return impl_->RecordingDevices(); }
124 int32_t PlayoutDeviceName(uint16_t index,
125 char name[kAdmMaxDeviceNameSize],
126 char guid[kAdmMaxGuidSize]) override {
127 return impl_->PlayoutDeviceName(index, name, guid);
128 }
129 int32_t RecordingDeviceName(uint16_t index,
130 char name[kAdmMaxDeviceNameSize],
131 char guid[kAdmMaxGuidSize]) override {
132 return impl_->RecordingDeviceName(index, name, guid);
133 }
134 int32_t SetPlayoutDevice(uint16_t index) override {
135 return impl_->SetPlayoutDevice(index);
136 }
137 int32_t SetPlayoutDevice(WindowsDeviceType device) override {
138 return impl_->SetPlayoutDevice(device);
139 }
140 int32_t SetRecordingDevice(uint16_t index) override {
141 return impl_->SetRecordingDevice(index);
142 }
143 int32_t SetRecordingDevice(WindowsDeviceType device) override {
144 return impl_->SetRecordingDevice(device);
145 }
146 int32_t PlayoutIsAvailable(bool* available) override {
147 return impl_->PlayoutIsAvailable(available);
148 }
149 int32_t InitPlayout() override { return impl_->InitPlayout(); }
150 bool PlayoutIsInitialized() const override {
151 return impl_->PlayoutIsInitialized();
152 }
153 int32_t RecordingIsAvailable(bool* available) override {
154 return impl_->RecordingIsAvailable(available);
155 }
156 int32_t InitRecording() override { return impl_->InitRecording(); }
157 bool RecordingIsInitialized() const override {
158 return impl_->RecordingIsInitialized();
159 }
160 int32_t StartPlayout() override { return impl_->StartPlayout(); }
161 int32_t StopPlayout() override { return impl_->StopPlayout(); }
162 bool Playing() const override { return impl_->Playing(); }
163 int32_t StartRecording() override { return impl_->StartRecording(); }
164 int32_t StopRecording() override { return impl_->StopRecording(); }
165 bool Recording() const override { return impl_->Recording(); }
Lu Liu4b620012017-04-06 10:17:01 -0700166 int32_t InitSpeaker() override { return impl_->InitSpeaker(); }
167 bool SpeakerIsInitialized() const override {
168 return impl_->SpeakerIsInitialized();
169 }
170 int32_t InitMicrophone() override { return impl_->InitMicrophone(); }
171 bool MicrophoneIsInitialized() const override {
172 return impl_->MicrophoneIsInitialized();
173 }
174 int32_t SpeakerVolumeIsAvailable(bool* available) override {
175 return impl_->SpeakerVolumeIsAvailable(available);
176 }
177 int32_t SetSpeakerVolume(uint32_t volume) override {
178 return impl_->SetSpeakerVolume(volume);
179 }
180 int32_t SpeakerVolume(uint32_t* volume) const override {
181 return impl_->SpeakerVolume(volume);
182 }
183 int32_t MaxSpeakerVolume(uint32_t* max_volume) const override {
184 return impl_->MaxSpeakerVolume(max_volume);
185 }
186 int32_t MinSpeakerVolume(uint32_t* min_volume) const override {
187 return impl_->MinSpeakerVolume(min_volume);
188 }
Lu Liu4b620012017-04-06 10:17:01 -0700189 int32_t MicrophoneVolumeIsAvailable(bool* available) override {
190 return impl_->MicrophoneVolumeIsAvailable(available);
191 }
192 int32_t SetMicrophoneVolume(uint32_t volume) override {
193 return impl_->SetMicrophoneVolume(volume);
194 }
195 int32_t MicrophoneVolume(uint32_t* volume) const override {
196 return impl_->MicrophoneVolume(volume);
197 }
198 int32_t MaxMicrophoneVolume(uint32_t* max_volume) const override {
199 return impl_->MaxMicrophoneVolume(max_volume);
200 }
201 int32_t MinMicrophoneVolume(uint32_t* min_volume) const override {
202 return impl_->MinMicrophoneVolume(min_volume);
203 }
Lu Liu4b620012017-04-06 10:17:01 -0700204 int32_t SpeakerMuteIsAvailable(bool* available) override {
205 return impl_->SpeakerMuteIsAvailable(available);
206 }
207 int32_t SetSpeakerMute(bool enable) override {
208 return impl_->SetSpeakerMute(enable);
209 }
210 int32_t SpeakerMute(bool* enabled) const override {
211 return impl_->SpeakerMute(enabled);
212 }
213 int32_t MicrophoneMuteIsAvailable(bool* available) override {
214 return impl_->MicrophoneMuteIsAvailable(available);
215 }
216 int32_t SetMicrophoneMute(bool enable) override {
217 return impl_->SetMicrophoneMute(enable);
218 }
219 int32_t MicrophoneMute(bool* enabled) const override {
220 return impl_->MicrophoneMute(enabled);
221 }
Lu Liu4b620012017-04-06 10:17:01 -0700222 int32_t StereoPlayoutIsAvailable(bool* available) const override {
223 return impl_->StereoPlayoutIsAvailable(available);
224 }
225 int32_t SetStereoPlayout(bool enable) override {
226 return impl_->SetStereoPlayout(enable);
227 }
228 int32_t StereoPlayout(bool* enabled) const override {
229 return impl_->StereoPlayout(enabled);
230 }
231 int32_t StereoRecordingIsAvailable(bool* available) const override {
232 return impl_->StereoRecordingIsAvailable(available);
233 }
234 int32_t SetStereoRecording(bool enable) override {
235 return impl_->SetStereoRecording(enable);
236 }
237 int32_t StereoRecording(bool* enabled) const override {
238 return impl_->StereoRecording(enabled);
239 }
Lu Liu4b620012017-04-06 10:17:01 -0700240 int32_t PlayoutDelay(uint16_t* delay_ms) const override {
241 return impl_->PlayoutDelay(delay_ms);
242 }
Lu Liu4b620012017-04-06 10:17:01 -0700243 bool BuiltInAECIsAvailable() const override {
244 return impl_->BuiltInAECIsAvailable();
245 }
246 bool BuiltInAGCIsAvailable() const override {
247 return impl_->BuiltInAGCIsAvailable();
248 }
249 bool BuiltInNSIsAvailable() const override {
250 return impl_->BuiltInNSIsAvailable();
251 }
252 int32_t EnableBuiltInAEC(bool enable) override {
253 return impl_->EnableBuiltInAEC(enable);
254 }
255 int32_t EnableBuiltInAGC(bool enable) override {
256 return impl_->EnableBuiltInAGC(enable);
257 }
258 int32_t EnableBuiltInNS(bool enable) override {
259 return impl_->EnableBuiltInNS(enable);
260 }
261// Only supported on iOS.
262#if defined(WEBRTC_IOS)
263 int GetPlayoutAudioParameters(AudioParameters* params) const override {
264 return impl_->GetPlayoutAudioParameters(params);
265 }
266 int GetRecordAudioParameters(AudioParameters* params) const override {
267 return impl_->GetRecordAudioParameters(params);
268 }
269#endif // WEBRTC_IOS
270
271 protected:
272 rtc::scoped_refptr<AudioDeviceModule> impl_;
273 AudioDeviceDataObserver* observer_ = nullptr;
274 AudioTransport* audio_transport_ = nullptr;
275 bool is_valid_ = false;
276};
277
278} // namespace
279
280rtc::scoped_refptr<AudioDeviceModule> CreateAudioDeviceWithDataObserver(
Lu Liu4b620012017-04-06 10:17:01 -0700281 const AudioDeviceModule::AudioLayer audio_layer,
282 AudioDeviceDataObserver* observer) {
283 rtc::scoped_refptr<ADMWrapper> audio_device(
henrika616e3132017-11-13 12:47:59 +0100284 new rtc::RefCountedObject<ADMWrapper>(audio_layer, observer));
Lu Liu4b620012017-04-06 10:17:01 -0700285
286 if (!audio_device->IsValid()) {
287 return nullptr;
288 }
289
290 return audio_device;
291}
292
henrika616e3132017-11-13 12:47:59 +0100293// TODO(bugs.webrtc.org/7306): deprecated.
294rtc::scoped_refptr<AudioDeviceModule> CreateAudioDeviceWithDataObserver(
295 const int32_t id,
296 const AudioDeviceModule::AudioLayer audio_layer,
297 AudioDeviceDataObserver* observer) {
298 return CreateAudioDeviceWithDataObserver(audio_layer, observer);
299}
300
Lu Liu4b620012017-04-06 10:17:01 -0700301} // namespace webrtc