1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522
|
/*
* Copyright (c) 2018 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include "modules/audio_device/win/audio_device_module_win.h"
#include <memory>
#include <utility>
#include "api/audio/audio_device.h"
#include "api/make_ref_counted.h"
#include "api/sequence_checker.h"
#include "modules/audio_device/audio_device_buffer.h"
#include "rtc_base/checks.h"
#include "rtc_base/logging.h"
#include "rtc_base/string_utils.h"
namespace webrtc {
namespace webrtc_win {
namespace {
#define RETURN_IF_OUTPUT_RESTARTS(...) \
do { \
if (output_->Restarting()) { \
return __VA_ARGS__; \
} \
} while (0)
#define RETURN_IF_INPUT_RESTARTS(...) \
do { \
if (input_->Restarting()) { \
return __VA_ARGS__; \
} \
} while (0)
#define RETURN_IF_OUTPUT_IS_INITIALIZED(...) \
do { \
if (output_->PlayoutIsInitialized()) { \
return __VA_ARGS__; \
} \
} while (0)
#define RETURN_IF_INPUT_IS_INITIALIZED(...) \
do { \
if (input_->RecordingIsInitialized()) { \
return __VA_ARGS__; \
} \
} while (0)
#define RETURN_IF_OUTPUT_IS_ACTIVE(...) \
do { \
if (output_->Playing()) { \
return __VA_ARGS__; \
} \
} while (0)
#define RETURN_IF_INPUT_IS_ACTIVE(...) \
do { \
if (input_->Recording()) { \
return __VA_ARGS__; \
} \
} while (0)
// This class combines a generic instance of an AudioInput and a generic
// instance of an AudioOutput to create an AudioDeviceModule. This is mostly
// done by delegating to the audio input/output with some glue code. This class
// also directly implements some of the AudioDeviceModule methods with dummy
// implementations.
//
// An instance must be created, destroyed and used on one and the same thread,
// i.e., all public methods must also be called on the same thread. A thread
// checker will RTC_DCHECK if any method is called on an invalid thread.
// TODO(henrika): is thread checking needed in AudioInput and AudioOutput?
class WindowsAudioDeviceModule : public AudioDeviceModuleForTest {
public:
enum class InitStatus {
OK = 0,
PLAYOUT_ERROR = 1,
RECORDING_ERROR = 2,
OTHER_ERROR = 3,
NUM_STATUSES = 4
};
WindowsAudioDeviceModule(std::unique_ptr<AudioInput> audio_input,
std::unique_ptr<AudioOutput> audio_output,
TaskQueueFactory* task_queue_factory)
: input_(std::move(audio_input)),
output_(std::move(audio_output)),
task_queue_factory_(task_queue_factory) {
RTC_CHECK(input_);
RTC_CHECK(output_);
RTC_DLOG(LS_INFO) << __FUNCTION__;
RTC_DCHECK_RUN_ON(&thread_checker_);
}
~WindowsAudioDeviceModule() override {
RTC_DLOG(LS_INFO) << __FUNCTION__;
RTC_DCHECK_RUN_ON(&thread_checker_);
Terminate();
}
WindowsAudioDeviceModule(const WindowsAudioDeviceModule&) = delete;
WindowsAudioDeviceModule& operator=(const WindowsAudioDeviceModule&) = delete;
int32_t ActiveAudioLayer(
AudioDeviceModule::AudioLayer* audioLayer) const override {
RTC_DLOG(LS_INFO) << __FUNCTION__;
RTC_DCHECK_RUN_ON(&thread_checker_);
// TODO(henrika): it might be possible to remove this unique signature.
*audioLayer = AudioDeviceModule::kWindowsCoreAudio2;
return 0;
}
int32_t RegisterAudioCallback(AudioTransport* audioCallback) override {
RTC_DLOG(LS_INFO) << __FUNCTION__;
RTC_DCHECK(audio_device_buffer_);
RTC_DCHECK_RUN_ON(&thread_checker_);
return audio_device_buffer_->RegisterAudioCallback(audioCallback);
}
int32_t Init() override {
RTC_DLOG(LS_INFO) << __FUNCTION__;
RTC_DCHECK_RUN_ON(&thread_checker_);
RETURN_IF_OUTPUT_RESTARTS(0);
RETURN_IF_INPUT_RESTARTS(0);
if (initialized_) {
return 0;
}
audio_device_buffer_ =
std::make_unique<AudioDeviceBuffer>(task_queue_factory_);
AttachAudioBuffer();
InitStatus status;
if (output_->Init() != 0) {
status = InitStatus::PLAYOUT_ERROR;
} else if (input_->Init() != 0) {
output_->Terminate();
status = InitStatus::RECORDING_ERROR;
} else {
initialized_ = true;
status = InitStatus::OK;
}
if (status != InitStatus::OK) {
RTC_LOG(LS_ERROR) << "Audio device initialization failed";
return -1;
}
return 0;
}
int32_t Terminate() override {
RTC_DLOG(LS_INFO) << __FUNCTION__;
RTC_DCHECK_RUN_ON(&thread_checker_);
RETURN_IF_OUTPUT_RESTARTS(0);
RETURN_IF_INPUT_RESTARTS(0);
if (!initialized_)
return 0;
int32_t err = input_->Terminate();
err |= output_->Terminate();
initialized_ = false;
RTC_DCHECK_EQ(err, 0);
return err;
}
bool Initialized() const override {
RTC_DCHECK_RUN_ON(&thread_checker_);
return initialized_;
}
int16_t PlayoutDevices() override {
RTC_DLOG(LS_INFO) << __FUNCTION__;
RTC_DCHECK_RUN_ON(&thread_checker_);
RETURN_IF_OUTPUT_RESTARTS(0);
return output_->NumDevices();
}
int16_t RecordingDevices() override {
RTC_DLOG(LS_INFO) << __FUNCTION__;
RTC_DCHECK_RUN_ON(&thread_checker_);
RETURN_IF_INPUT_RESTARTS(0);
return input_->NumDevices();
}
int32_t PlayoutDeviceName(uint16_t index,
char name[kAdmMaxDeviceNameSize],
char guid[kAdmMaxGuidSize]) override {
RTC_DLOG(LS_INFO) << __FUNCTION__;
RTC_DCHECK_RUN_ON(&thread_checker_);
RETURN_IF_OUTPUT_RESTARTS(0);
std::string name_str, guid_str;
int ret = -1;
if (guid != nullptr) {
ret = output_->DeviceName(index, &name_str, &guid_str);
webrtc::strcpyn(guid, kAdmMaxGuidSize, guid_str.c_str());
} else {
ret = output_->DeviceName(index, &name_str, nullptr);
}
webrtc::strcpyn(name, kAdmMaxDeviceNameSize, name_str.c_str());
return ret;
}
int32_t RecordingDeviceName(uint16_t index,
char name[kAdmMaxDeviceNameSize],
char guid[kAdmMaxGuidSize]) override {
RTC_DLOG(LS_INFO) << __FUNCTION__;
RTC_DCHECK_RUN_ON(&thread_checker_);
RETURN_IF_INPUT_RESTARTS(0);
std::string name_str, guid_str;
int ret = -1;
if (guid != nullptr) {
ret = input_->DeviceName(index, &name_str, &guid_str);
webrtc::strcpyn(guid, kAdmMaxGuidSize, guid_str.c_str());
} else {
ret = input_->DeviceName(index, &name_str, nullptr);
}
webrtc::strcpyn(name, kAdmMaxDeviceNameSize, name_str.c_str());
return ret;
}
int32_t SetPlayoutDevice(uint16_t index) override {
RTC_DLOG(LS_INFO) << __FUNCTION__;
RTC_DCHECK_RUN_ON(&thread_checker_);
RETURN_IF_OUTPUT_RESTARTS(0);
return output_->SetDevice(index);
}
int32_t SetPlayoutDevice(
AudioDeviceModule::WindowsDeviceType device) override {
RTC_DLOG(LS_INFO) << __FUNCTION__;
RTC_DCHECK_RUN_ON(&thread_checker_);
RETURN_IF_OUTPUT_RESTARTS(0);
return output_->SetDevice(device);
}
int32_t SetRecordingDevice(uint16_t index) override {
RTC_DLOG(LS_INFO) << __FUNCTION__;
RTC_DCHECK_RUN_ON(&thread_checker_);
return input_->SetDevice(index);
}
int32_t SetRecordingDevice(
AudioDeviceModule::WindowsDeviceType device) override {
RTC_DLOG(LS_INFO) << __FUNCTION__;
RTC_DCHECK_RUN_ON(&thread_checker_);
return input_->SetDevice(device);
}
int32_t PlayoutIsAvailable(bool* available) override {
RTC_DLOG(LS_INFO) << __FUNCTION__;
RTC_DCHECK_RUN_ON(&thread_checker_);
*available = true;
return 0;
}
int32_t InitPlayout() override {
RTC_DLOG(LS_INFO) << __FUNCTION__;
RTC_DCHECK_RUN_ON(&thread_checker_);
RETURN_IF_OUTPUT_RESTARTS(0);
RETURN_IF_OUTPUT_IS_INITIALIZED(0);
return output_->InitPlayout();
}
bool PlayoutIsInitialized() const override {
RTC_DLOG(LS_INFO) << __FUNCTION__;
RTC_DCHECK_RUN_ON(&thread_checker_);
RETURN_IF_OUTPUT_RESTARTS(true);
return output_->PlayoutIsInitialized();
}
int32_t RecordingIsAvailable(bool* available) override {
RTC_DLOG(LS_INFO) << __FUNCTION__;
RTC_DCHECK_RUN_ON(&thread_checker_);
*available = true;
return 0;
}
int32_t InitRecording() override {
RTC_DLOG(LS_INFO) << __FUNCTION__;
RTC_DCHECK_RUN_ON(&thread_checker_);
RETURN_IF_INPUT_RESTARTS(0);
RETURN_IF_INPUT_IS_INITIALIZED(0);
return input_->InitRecording();
}
bool RecordingIsInitialized() const override {
RTC_DLOG(LS_INFO) << __FUNCTION__;
RTC_DCHECK_RUN_ON(&thread_checker_);
RETURN_IF_INPUT_RESTARTS(true);
return input_->RecordingIsInitialized();
}
int32_t StartPlayout() override {
RTC_DLOG(LS_INFO) << __FUNCTION__;
RTC_DCHECK_RUN_ON(&thread_checker_);
RETURN_IF_OUTPUT_RESTARTS(0);
RETURN_IF_OUTPUT_IS_ACTIVE(0);
return output_->StartPlayout();
}
int32_t StopPlayout() override {
RTC_DLOG(LS_INFO) << __FUNCTION__;
RTC_DCHECK_RUN_ON(&thread_checker_);
RETURN_IF_OUTPUT_RESTARTS(-1);
return output_->StopPlayout();
}
bool Playing() const override {
RTC_DLOG(LS_INFO) << __FUNCTION__;
RTC_DCHECK_RUN_ON(&thread_checker_);
RETURN_IF_OUTPUT_RESTARTS(true);
return output_->Playing();
}
int32_t StartRecording() override {
RTC_DLOG(LS_INFO) << __FUNCTION__;
RTC_DCHECK_RUN_ON(&thread_checker_);
RETURN_IF_INPUT_RESTARTS(0);
RETURN_IF_INPUT_IS_ACTIVE(0);
return input_->StartRecording();
}
int32_t StopRecording() override {
RTC_DLOG(LS_INFO) << __FUNCTION__;
RTC_DCHECK_RUN_ON(&thread_checker_);
RETURN_IF_INPUT_RESTARTS(-1);
return input_->StopRecording();
}
bool Recording() const override {
RTC_DLOG(LS_INFO) << __FUNCTION__;
RETURN_IF_INPUT_RESTARTS(true);
return input_->Recording();
}
int32_t InitSpeaker() override {
RTC_DLOG(LS_INFO) << __FUNCTION__;
RTC_DCHECK_RUN_ON(&thread_checker_);
RTC_DLOG(LS_WARNING) << "This method has no effect";
return initialized_ ? 0 : -1;
}
bool SpeakerIsInitialized() const override {
RTC_DLOG(LS_INFO) << __FUNCTION__;
RTC_DCHECK_RUN_ON(&thread_checker_);
RTC_DLOG(LS_WARNING) << "This method has no effect";
return initialized_;
}
int32_t InitMicrophone() override {
RTC_DLOG(LS_INFO) << __FUNCTION__;
RTC_DCHECK_RUN_ON(&thread_checker_);
RTC_DLOG(LS_WARNING) << "This method has no effect";
return initialized_ ? 0 : -1;
}
bool MicrophoneIsInitialized() const override {
RTC_DLOG(LS_INFO) << __FUNCTION__;
RTC_DCHECK_RUN_ON(&thread_checker_);
RTC_DLOG(LS_WARNING) << "This method has no effect";
return initialized_;
}
int32_t SpeakerVolumeIsAvailable(bool* available) override {
// TODO(henrika): improve support.
RTC_DLOG(LS_INFO) << __FUNCTION__;
RTC_DCHECK_RUN_ON(&thread_checker_);
*available = false;
return 0;
}
int32_t SetSpeakerVolume(uint32_t volume) override { return 0; }
int32_t SpeakerVolume(uint32_t* volume) const override { return 0; }
int32_t MaxSpeakerVolume(uint32_t* maxVolume) const override { return 0; }
int32_t MinSpeakerVolume(uint32_t* minVolume) const override { return 0; }
int32_t MicrophoneVolumeIsAvailable(bool* available) override {
// TODO(henrika): improve support.
RTC_DLOG(LS_INFO) << __FUNCTION__;
RTC_DCHECK_RUN_ON(&thread_checker_);
*available = false;
return 0;
}
int32_t SetMicrophoneVolume(uint32_t volume) override { return 0; }
int32_t MicrophoneVolume(uint32_t* volume) const override { return 0; }
int32_t MaxMicrophoneVolume(uint32_t* maxVolume) const override { return 0; }
int32_t MinMicrophoneVolume(uint32_t* minVolume) const override { return 0; }
int32_t SpeakerMuteIsAvailable(bool* available) override { return 0; }
int32_t SetSpeakerMute(bool enable) override { return 0; }
int32_t SpeakerMute(bool* enabled) const override { return 0; }
int32_t MicrophoneMuteIsAvailable(bool* available) override { return 0; }
int32_t SetMicrophoneMute(bool enable) override { return 0; }
int32_t MicrophoneMute(bool* enabled) const override { return 0; }
int32_t StereoPlayoutIsAvailable(bool* available) const override {
// TODO(henrika): improve support.
RTC_DLOG(LS_INFO) << __FUNCTION__;
RTC_DCHECK_RUN_ON(&thread_checker_);
*available = true;
return 0;
}
int32_t SetStereoPlayout(bool enable) override {
// TODO(henrika): improve support.
RTC_DLOG(LS_INFO) << __FUNCTION__;
RTC_DCHECK_RUN_ON(&thread_checker_);
return 0;
}
int32_t StereoPlayout(bool* enabled) const override {
// TODO(henrika): improve support.
RTC_DLOG(LS_INFO) << __FUNCTION__;
RTC_DCHECK_RUN_ON(&thread_checker_);
*enabled = true;
return 0;
}
int32_t StereoRecordingIsAvailable(bool* available) const override {
// TODO(henrika): improve support.
RTC_DLOG(LS_INFO) << __FUNCTION__;
RTC_DCHECK_RUN_ON(&thread_checker_);
*available = true;
return 0;
}
int32_t SetStereoRecording(bool enable) override {
// TODO(henrika): improve support.
RTC_DLOG(LS_INFO) << __FUNCTION__;
RTC_DCHECK_RUN_ON(&thread_checker_);
return 0;
}
int32_t StereoRecording(bool* enabled) const override {
// TODO(henrika): improve support.
RTC_DLOG(LS_INFO) << __FUNCTION__;
RTC_DCHECK_RUN_ON(&thread_checker_);
*enabled = true;
return 0;
}
int32_t PlayoutDelay(uint16_t* delayMS) const override { return 0; }
bool BuiltInAECIsAvailable() const override { return false; }
bool BuiltInAGCIsAvailable() const override { return false; }
bool BuiltInNSIsAvailable() const override { return false; }
int32_t EnableBuiltInAEC(bool enable) override { return 0; }
int32_t EnableBuiltInAGC(bool enable) override { return 0; }
int32_t EnableBuiltInNS(bool enable) override { return 0; }
int32_t AttachAudioBuffer() {
RTC_DLOG(LS_INFO) << __FUNCTION__;
output_->AttachAudioBuffer(audio_device_buffer_.get());
input_->AttachAudioBuffer(audio_device_buffer_.get());
return 0;
}
int RestartPlayoutInternally() override {
RTC_DLOG(LS_INFO) << __FUNCTION__;
RTC_DCHECK_RUN_ON(&thread_checker_);
RETURN_IF_OUTPUT_RESTARTS(0);
return output_->RestartPlayout();
}
int RestartRecordingInternally() override {
RTC_DLOG(LS_INFO) << __FUNCTION__;
RTC_DCHECK_RUN_ON(&thread_checker_);
return input_->RestartRecording();
}
int SetPlayoutSampleRate(uint32_t sample_rate) override {
RTC_DLOG(LS_INFO) << __FUNCTION__;
RTC_DCHECK_RUN_ON(&thread_checker_);
return output_->SetSampleRate(sample_rate);
}
int SetRecordingSampleRate(uint32_t sample_rate) override {
RTC_DLOG(LS_INFO) << __FUNCTION__;
RTC_DCHECK_RUN_ON(&thread_checker_);
return input_->SetSampleRate(sample_rate);
}
private:
// Ensures that the class is used on the same thread as it is constructed
// and destroyed on.
SequenceChecker thread_checker_;
// Implements the AudioInput interface and deals with audio capturing parts.
const std::unique_ptr<AudioInput> input_;
// Implements the AudioOutput interface and deals with audio rendering parts.
const std::unique_ptr<AudioOutput> output_;
TaskQueueFactory* const task_queue_factory_;
// The AudioDeviceBuffer (ADB) instance is needed for sending/receiving audio
// to/from the WebRTC layer. Created and owned by this object. Used by
// both `input_` and `output_` but they use orthogonal parts of the ADB.
std::unique_ptr<AudioDeviceBuffer> audio_device_buffer_;
// Set to true after a successful call to Init(). Cleared by Terminate().
bool initialized_ = false;
};
} // namespace
webrtc::scoped_refptr<AudioDeviceModuleForTest>
CreateWindowsCoreAudioAudioDeviceModuleFromInputAndOutput(
std::unique_ptr<AudioInput> audio_input,
std::unique_ptr<AudioOutput> audio_output,
TaskQueueFactory* task_queue_factory) {
RTC_DLOG(LS_INFO) << __FUNCTION__;
return webrtc::make_ref_counted<WindowsAudioDeviceModule>(
std::move(audio_input), std::move(audio_output), task_queue_factory);
}
} // namespace webrtc_win
} // namespace webrtc
|