blob: 7902889477e4e7efe2c9f020ae4aa2fb6dba42fa [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
11#include "webrtc/media/engine/adm_helpers.h"
12
solenberg9a5f032222017-03-15 06:14:12 -070013#include "webrtc/modules/audio_device/include/audio_device.h"
Edward Lemurc20978e2017-07-06 19:44:34 +020014#include "webrtc/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) {
41 LOG(LS_ERROR) << "Unable to stop recording.";
42 return;
43 }
44
45 // Set device and stereo mode.
46 if (adm->SetRecordingChannel(AudioDeviceModule::kChannelBoth) != 0) {
47 LOG(LS_ERROR) << "Unable to set recording channel to kChannelBoth.";
48 }
49 if (adm->SetRecordingDevice(AUDIO_DEVICE_ID) != 0) {
50 LOG(LS_ERROR) << "Unable to set recording device.";
51 return;
52 }
53
54 // Init microphone, so user can do volume settings etc.
55 if (adm->InitMicrophone() != 0) {
56 LOG(LS_ERROR) << "Unable to access microphone.";
57 }
58
59 // Set number of channels
60 bool available = false;
61 if (adm->StereoRecordingIsAvailable(&available) != 0) {
62 LOG(LS_ERROR) << "Failed to query stereo recording.";
63 }
64 if (adm->SetStereoRecording(available) != 0) {
65 LOG(LS_ERROR) << "Failed to set stereo recording mode.";
66 }
67
68 // Restore recording if it was enabled already when calling this function.
69 if (was_recording) {
70 if (adm->InitRecording() != 0) {
71 LOG(LS_ERROR) << "Failed to initialize recording.";
72 return;
73 }
74 if (adm->StartRecording() != 0) {
75 LOG(LS_ERROR) << "Failed to start recording.";
76 return;
77 }
78 }
79
80 LOG(LS_INFO) << "Set recording device.";
81}
82
83void SetPlayoutDevice(AudioDeviceModule* adm) {
84 RTC_DCHECK(adm);
85
86 // Save playing status and stop playout.
87 const bool was_playing = adm->Playing();
88 if (was_playing && adm->StopPlayout() != 0) {
89 LOG(LS_ERROR) << "Unable to stop playout.";
90 }
91
92 // Set device.
93 if (adm->SetPlayoutDevice(AUDIO_DEVICE_ID) != 0) {
94 LOG(LS_ERROR) << "Unable to set playout device.";
95 return;
96 }
97
98 // Init speaker, so user can do volume settings etc.
99 if (adm->InitSpeaker() != 0) {
100 LOG(LS_ERROR) << "Unable to access speaker.";
101 }
102
103 // Set number of channels
104 bool available = false;
105 if (adm->StereoPlayoutIsAvailable(&available) != 0) {
106 LOG(LS_ERROR) << "Failed to query stereo playout.";
107 }
108 if (adm->SetStereoPlayout(available) != 0) {
109 LOG(LS_ERROR) << "Failed to set stereo playout mode.";
110 }
111
112 // Restore recording if it was enabled already when calling this function.
113 if (was_playing) {
114 if (adm->InitPlayout() != 0) {
115 LOG(LS_ERROR) << "Failed to initialize playout.";
116 return;
117 }
118 if (adm->StartPlayout() != 0) {
119 LOG(LS_ERROR) << "Failed to start playout.";
120 return;
121 }
122 }
123
124 LOG(LS_INFO) << "Set playout device.";
125}
126
127} // namespace adm_helpers
128} // namespace webrtc