blob: b973ce6ebde41927696220963fab3a95e9f67cf4 [file] [log] [blame]
solenberg9a5f032222017-03-15 06:14:12 -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 "media/engine/adm_helpers.h"
solenberg9a5f032222017-03-15 06:14:12 -070012
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020013#include "modules/audio_device/include/audio_device.h"
14#include "rtc_base/logging.h"
solenberg9a5f032222017-03-15 06:14:12 -070015
16namespace webrtc {
17namespace adm_helpers {
18
19// On Windows Vista and newer, Microsoft introduced the concept of "Default
20// Communications Device". This means that there are two types of default
21// devices (old Wave Audio style default and Default Communications Device).
22//
23// On Windows systems which only support Wave Audio style default, uses either
24// -1 or 0 to select the default device.
25//
26// Using a #define for AUDIO_DEVICE since we will call *different* versions of
27// the ADM functions, depending on the ID type.
28#if defined(WEBRTC_WIN)
29#define AUDIO_DEVICE_ID \
30 (AudioDeviceModule::WindowsDeviceType::kDefaultCommunicationDevice)
31#else
32#define AUDIO_DEVICE_ID (0u)
33#endif // defined(WEBRTC_WIN)
34
35void SetRecordingDevice(AudioDeviceModule* adm) {
36 RTC_DCHECK(adm);
37
38 // Save recording status and stop recording.
39 const bool was_recording = adm->Recording();
40 if (was_recording && adm->StopRecording() != 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010041 RTC_LOG(LS_ERROR) << "Unable to stop recording.";
solenberg9a5f032222017-03-15 06:14:12 -070042 return;
43 }
44
henrika8962b542017-11-13 16:57:58 +010045 // Set device to default.
solenberg9a5f032222017-03-15 06:14:12 -070046 if (adm->SetRecordingDevice(AUDIO_DEVICE_ID) != 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010047 RTC_LOG(LS_ERROR) << "Unable to set recording device.";
solenberg9a5f032222017-03-15 06:14:12 -070048 return;
49 }
50
51 // Init microphone, so user can do volume settings etc.
52 if (adm->InitMicrophone() != 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010053 RTC_LOG(LS_ERROR) << "Unable to access microphone.";
solenberg9a5f032222017-03-15 06:14:12 -070054 }
55
56 // Set number of channels
57 bool available = false;
58 if (adm->StereoRecordingIsAvailable(&available) != 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010059 RTC_LOG(LS_ERROR) << "Failed to query stereo recording.";
solenberg9a5f032222017-03-15 06:14:12 -070060 }
61 if (adm->SetStereoRecording(available) != 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010062 RTC_LOG(LS_ERROR) << "Failed to set stereo recording mode.";
solenberg9a5f032222017-03-15 06:14:12 -070063 }
64
65 // Restore recording if it was enabled already when calling this function.
66 if (was_recording) {
67 if (adm->InitRecording() != 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010068 RTC_LOG(LS_ERROR) << "Failed to initialize recording.";
solenberg9a5f032222017-03-15 06:14:12 -070069 return;
70 }
71 if (adm->StartRecording() != 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010072 RTC_LOG(LS_ERROR) << "Failed to start recording.";
solenberg9a5f032222017-03-15 06:14:12 -070073 return;
74 }
75 }
76
Mirko Bonadei675513b2017-11-09 11:09:25 +010077 RTC_LOG(LS_INFO) << "Set recording device.";
solenberg9a5f032222017-03-15 06:14:12 -070078}
79
80void SetPlayoutDevice(AudioDeviceModule* adm) {
81 RTC_DCHECK(adm);
82
83 // Save playing status and stop playout.
84 const bool was_playing = adm->Playing();
85 if (was_playing && adm->StopPlayout() != 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010086 RTC_LOG(LS_ERROR) << "Unable to stop playout.";
solenberg9a5f032222017-03-15 06:14:12 -070087 }
88
89 // Set device.
90 if (adm->SetPlayoutDevice(AUDIO_DEVICE_ID) != 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010091 RTC_LOG(LS_ERROR) << "Unable to set playout device.";
solenberg9a5f032222017-03-15 06:14:12 -070092 return;
93 }
94
95 // Init speaker, so user can do volume settings etc.
96 if (adm->InitSpeaker() != 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010097 RTC_LOG(LS_ERROR) << "Unable to access speaker.";
solenberg9a5f032222017-03-15 06:14:12 -070098 }
99
100 // Set number of channels
101 bool available = false;
102 if (adm->StereoPlayoutIsAvailable(&available) != 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100103 RTC_LOG(LS_ERROR) << "Failed to query stereo playout.";
solenberg9a5f032222017-03-15 06:14:12 -0700104 }
105 if (adm->SetStereoPlayout(available) != 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100106 RTC_LOG(LS_ERROR) << "Failed to set stereo playout mode.";
solenberg9a5f032222017-03-15 06:14:12 -0700107 }
108
109 // Restore recording if it was enabled already when calling this function.
110 if (was_playing) {
111 if (adm->InitPlayout() != 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100112 RTC_LOG(LS_ERROR) << "Failed to initialize playout.";
solenberg9a5f032222017-03-15 06:14:12 -0700113 return;
114 }
115 if (adm->StartPlayout() != 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100116 RTC_LOG(LS_ERROR) << "Failed to start playout.";
solenberg9a5f032222017-03-15 06:14:12 -0700117 return;
118 }
119 }
120
Mirko Bonadei675513b2017-11-09 11:09:25 +0100121 RTC_LOG(LS_INFO) << "Set playout device.";
solenberg9a5f032222017-03-15 06:14:12 -0700122}
123
124} // namespace adm_helpers
125} // namespace webrtc