| 6 | namespace tgcalls { |
| 7 | namespace { |
| 8 |
| 9 | bool SkipDefaultDevice(const char *name) { |
| 10 | const auto utfName = std::string(name); |
| 11 | #ifdef WEBRTC_WIN |
| 12 | return (utfName.rfind("Default - ", 0) == 0) |
| 13 | || (utfName.rfind("Communication - ", 0) == 0); |
| 14 | #elif defined WEBRTC_MAC |
| 15 | return (utfName.rfind("default (", 0) == 0) |
| 16 | && (utfName.find(")", utfName.size() - 1) == utfName.size() - 1); |
| 17 | #else |
| 18 | return false; |
| 19 | #endif // WEBRTC_WIN || WEBRTC_MAC |
| 20 | } |
| 21 |
| 22 | } // namespace |
| 23 |
| 24 | void SetAudioInputDeviceById(webrtc::AudioDeviceModule *adm, const std::string &id) { |
| 25 | const auto recording = adm->Recording() || adm->RecordingIsInitialized(); |
| 26 | if (recording) { |
| 27 | adm->StopRecording(); |
| 28 | } |
| 29 | auto specific = false; |
| 30 | const auto finish = [&] { |
| 31 | if (!specific) { |
| 32 | if (const auto result = adm->SetRecordingDevice(webrtc::AudioDeviceModule::kDefaultCommunicationDevice)) { |
| 33 | RTC_LOG(LS_ERROR) << "setAudioInputDevice(" << id << "): SetRecordingDevice(kDefaultCommunicationDevice) failed: " << result << "."; |
| 34 | } else { |
| 35 | RTC_LOG(LS_INFO) << "setAudioInputDevice(" << id << "): SetRecordingDevice(kDefaultCommunicationDevice) success."; |
| 36 | } |
| 37 | } |
| 38 | if (recording && adm->InitRecording() == 0) { |
| 39 | adm->StartRecording(); |
| 40 | } |
| 41 | }; |
| 42 | if (id == "default" || id.empty()) { |
| 43 | return finish(); |
| 44 | } |
| 45 | #ifdef TGCALLS_UWP_DESKTOP |
| 46 | const auto result = adm->SetRecordingDevice(id); |
| 47 | if (result != 0) { |
| 48 | RTC_LOG(LS_ERROR) << "setAudioInputDevice(" << id << ") failed: " << result << "."; |
| 49 | } else { |
| 50 | RTC_LOG(LS_INFO) << "setAudioInputDevice(" << id << ") success."; |
| 51 | specific = true; |
| 52 | } |
| 53 | return finish(); |
| 54 | #else // TGCALLS_UWP_DESKTOP |
| 55 | const auto count = adm |
| 56 | ? adm->RecordingDevices() |
| 57 | : int16_t(-666); |
| 58 | if (count <= 0) { |
| 59 | RTC_LOG(LS_ERROR) << "setAudioInputDevice(" << id << "): Could not get recording devices count: " << count << "."; |
| 60 | return finish(); |
| 61 | } |
| 62 |
| 63 | int16_t order = !id.empty() && id[0] == '#' ? static_cast<int16_t>(std::stoi(id.substr(1))) : -1; |
| 64 | for (auto i = 0; i != count; ++i) { |
| 65 | char name[webrtc::kAdmMaxDeviceNameSize + 1] = { 0 }; |
| 66 | char guid[webrtc::kAdmMaxGuidSize + 1] = { 0 }; |
| 67 | adm->RecordingDeviceName(i, name, guid); |
| 68 | if ((!SkipDefaultDevice(name) && id == guid) || order == i) { |
| 69 | const auto result = adm->SetRecordingDevice(i); |
| 70 | if (result != 0) { |
| 71 | RTC_LOG(LS_ERROR) << "setAudioInputDevice(" << id << ") name '" << std::string(name) << "' failed: " << result << "."; |
| 72 | } else { |
| 73 | RTC_LOG(LS_INFO) << "setAudioInputDevice(" << id << ") name '" << std::string(name) << "' success."; |
| 74 | specific = true; |
| 75 | } |
| 76 | return finish(); |
| 77 | } |
| 78 | } |
| 79 | RTC_LOG(LS_ERROR) << "setAudioInputDevice(" << id << "): Could not find recording device."; |
| 80 | return finish(); |
| 81 | #endif // TGCALLS_UWP_DESKTOP |
| 82 | } |
| 83 |
| 84 | void SetAudioOutputDeviceById(webrtc::AudioDeviceModule *adm, const std::string &id) { |
| 85 | if (adm->Playing()) { |
| 86 | adm->StopPlayout(); |
| 87 | } |
| 88 | auto specific = false; |
| 89 | const auto finish = [&] { |
| 90 | if (!specific) { |
| 91 | if (const auto result = adm->SetPlayoutDevice(webrtc::AudioDeviceModule::kDefaultCommunicationDevice)) { |
| 92 | RTC_LOG(LS_ERROR) << "setAudioOutputDevice(" << id << "): SetPlayoutDevice(kDefaultCommunicationDevice) failed: " << result << "."; |
| 93 | } else { |
| 94 | RTC_LOG(LS_INFO) << "setAudioOutputDevice(" << id << "): SetPlayoutDevice(kDefaultCommunicationDevice) success."; |
| 95 | } |
| 96 | } |
| 97 | if (adm->InitPlayout() == 0) { |
| 98 | adm->StartPlayout(); |
| 99 | } |
| 100 | }; |
| 101 | if (id == "default" || id.empty()) { |
| 102 | return finish(); |
| 103 | } |
| 104 | #ifdef TGCALLS_UWP_DESKTOP |
| 105 | const auto result = adm->SetPlayoutDevice(id); |
| 106 | if (result != 0) { |
| 107 | RTC_LOG(LS_ERROR) << "setAudioOutputDevice(" << id << ") failed: " << result << "."; |
| 108 | } else { |
| 109 | RTC_LOG(LS_INFO) << "setAudioOutputDevice(" << id << ") success."; |
| 110 | specific = true; |
| 111 | } |
| 112 | return finish(); |
| 113 | #else // TGCALLS_UWP_DESKTOP |
| 114 | const auto count = adm |
| 115 | ? adm->PlayoutDevices() |
| 116 | : int16_t(-666); |
| 117 | if (count <= 0) { |
| 118 | RTC_LOG(LS_ERROR) << "setAudioOutputDevice(" << id << "): Could not get playout devices count: " << count << "."; |
| 119 | return finish(); |
| 120 | } |
| 121 | int16_t order = !id.empty() && id[0] == '#' ? static_cast<int16_t>(std::stoi(id.substr(1))) : -1; |
| 122 | for (auto i = 0; i != count; ++i) { |
| 123 | char name[webrtc::kAdmMaxDeviceNameSize + 1] = { 0 }; |
| 124 | char guid[webrtc::kAdmMaxGuidSize + 1] = { 0 }; |
| 125 | adm->PlayoutDeviceName(i, name, guid); |
| 126 | if ((!SkipDefaultDevice(name) && id == guid) || order == i) { |
| 127 | const auto result = adm->SetPlayoutDevice(i); |
| 128 | if (result != 0) { |
| 129 | RTC_LOG(LS_ERROR) << "setAudioOutputDevice(" << id << ") name '" << std::string(name) << "' failed: " << result << "."; |
| 130 | } else { |
| 131 | RTC_LOG(LS_INFO) << "setAudioOutputDevice(" << id << ") name '" << std::string(name) << "' success."; |
| 132 | specific = true; |
| 133 | } |
| 134 | return finish(); |
| 135 | } |
| 136 | } |
| 137 | RTC_LOG(LS_ERROR) << "setAudioOutputDevice(" << id << "): Could not find playout device."; |
| 138 | return finish(); |
| 139 | #endif // TGCALLS_UWP_DESKTOP |
| 140 | } |
| 141 |
| 142 | } // namespace tgcalls |
| 143 |