solenberg | 9a5f03222 | 2017-03-15 06:14:12 -0700 | [diff] [blame] | 1 | /* |
| 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 Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "media/engine/adm_helpers.h" |
solenberg | 9a5f03222 | 2017-03-15 06:14:12 -0700 | [diff] [blame] | 12 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 13 | #include "modules/audio_device/include/audio_device.h" |
| 14 | #include "rtc_base/logging.h" |
solenberg | 9a5f03222 | 2017-03-15 06:14:12 -0700 | [diff] [blame] | 15 | |
| 16 | namespace webrtc { |
| 17 | namespace 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 | |
| 35 | void 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 Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 41 | RTC_LOG(LS_ERROR) << "Unable to stop recording."; |
solenberg | 9a5f03222 | 2017-03-15 06:14:12 -0700 | [diff] [blame] | 42 | return; |
| 43 | } |
| 44 | |
henrika | 8962b54 | 2017-11-13 16:57:58 +0100 | [diff] [blame] | 45 | // Set device to default. |
solenberg | 9a5f03222 | 2017-03-15 06:14:12 -0700 | [diff] [blame] | 46 | if (adm->SetRecordingDevice(AUDIO_DEVICE_ID) != 0) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 47 | RTC_LOG(LS_ERROR) << "Unable to set recording device."; |
solenberg | 9a5f03222 | 2017-03-15 06:14:12 -0700 | [diff] [blame] | 48 | return; |
| 49 | } |
| 50 | |
| 51 | // Init microphone, so user can do volume settings etc. |
| 52 | if (adm->InitMicrophone() != 0) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 53 | RTC_LOG(LS_ERROR) << "Unable to access microphone."; |
solenberg | 9a5f03222 | 2017-03-15 06:14:12 -0700 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | // Set number of channels |
| 57 | bool available = false; |
| 58 | if (adm->StereoRecordingIsAvailable(&available) != 0) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 59 | RTC_LOG(LS_ERROR) << "Failed to query stereo recording."; |
solenberg | 9a5f03222 | 2017-03-15 06:14:12 -0700 | [diff] [blame] | 60 | } |
| 61 | if (adm->SetStereoRecording(available) != 0) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 62 | RTC_LOG(LS_ERROR) << "Failed to set stereo recording mode."; |
solenberg | 9a5f03222 | 2017-03-15 06:14:12 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | // Restore recording if it was enabled already when calling this function. |
| 66 | if (was_recording) { |
| 67 | if (adm->InitRecording() != 0) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 68 | RTC_LOG(LS_ERROR) << "Failed to initialize recording."; |
solenberg | 9a5f03222 | 2017-03-15 06:14:12 -0700 | [diff] [blame] | 69 | return; |
| 70 | } |
| 71 | if (adm->StartRecording() != 0) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 72 | RTC_LOG(LS_ERROR) << "Failed to start recording."; |
solenberg | 9a5f03222 | 2017-03-15 06:14:12 -0700 | [diff] [blame] | 73 | return; |
| 74 | } |
| 75 | } |
| 76 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 77 | RTC_LOG(LS_INFO) << "Set recording device."; |
solenberg | 9a5f03222 | 2017-03-15 06:14:12 -0700 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | void 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 Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 86 | RTC_LOG(LS_ERROR) << "Unable to stop playout."; |
solenberg | 9a5f03222 | 2017-03-15 06:14:12 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | // Set device. |
| 90 | if (adm->SetPlayoutDevice(AUDIO_DEVICE_ID) != 0) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 91 | RTC_LOG(LS_ERROR) << "Unable to set playout device."; |
solenberg | 9a5f03222 | 2017-03-15 06:14:12 -0700 | [diff] [blame] | 92 | return; |
| 93 | } |
| 94 | |
| 95 | // Init speaker, so user can do volume settings etc. |
| 96 | if (adm->InitSpeaker() != 0) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 97 | RTC_LOG(LS_ERROR) << "Unable to access speaker."; |
solenberg | 9a5f03222 | 2017-03-15 06:14:12 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | // Set number of channels |
| 101 | bool available = false; |
| 102 | if (adm->StereoPlayoutIsAvailable(&available) != 0) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 103 | RTC_LOG(LS_ERROR) << "Failed to query stereo playout."; |
solenberg | 9a5f03222 | 2017-03-15 06:14:12 -0700 | [diff] [blame] | 104 | } |
| 105 | if (adm->SetStereoPlayout(available) != 0) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 106 | RTC_LOG(LS_ERROR) << "Failed to set stereo playout mode."; |
solenberg | 9a5f03222 | 2017-03-15 06:14:12 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | // Restore recording if it was enabled already when calling this function. |
| 110 | if (was_playing) { |
| 111 | if (adm->InitPlayout() != 0) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 112 | RTC_LOG(LS_ERROR) << "Failed to initialize playout."; |
solenberg | 9a5f03222 | 2017-03-15 06:14:12 -0700 | [diff] [blame] | 113 | return; |
| 114 | } |
| 115 | if (adm->StartPlayout() != 0) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 116 | RTC_LOG(LS_ERROR) << "Failed to start playout."; |
solenberg | 9a5f03222 | 2017-03-15 06:14:12 -0700 | [diff] [blame] | 117 | return; |
| 118 | } |
| 119 | } |
| 120 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 121 | RTC_LOG(LS_INFO) << "Set playout device."; |
solenberg | 9a5f03222 | 2017-03-15 06:14:12 -0700 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | } // namespace adm_helpers |
| 125 | } // namespace webrtc |