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