| 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 | const auto count = adm |
| 46 | ? adm->RecordingDevices() |
| 47 | : int16_t(-666); |
| 48 | if (count <= 0) { |
| 49 | RTC_LOG(LS_ERROR) << "setAudioInputDevice(" << id << "): Could not get recording devices count: " << count << "."; |
| 50 | return finish(); |
| 51 | } |
| 52 |
| 53 | int16_t order = !id.empty() && id[0] == '#' ? static_cast<int16_t>(std::stoi(id.substr(1))) : -1; |
| 54 | for (auto i = 0; i != count; ++i) { |
| 55 | char name[webrtc::kAdmMaxDeviceNameSize + 1] = { 0 }; |
| 56 | char guid[webrtc::kAdmMaxGuidSize + 1] = { 0 }; |
| 57 | adm->RecordingDeviceName(i, name, guid); |
| 58 | if ((!SkipDefaultDevice(name) && id == guid) || order == i) { |
| 59 | const auto result = adm->SetRecordingDevice(i); |
| 60 | if (result != 0) { |
| 61 | RTC_LOG(LS_ERROR) << "setAudioInputDevice(" << id << ") name '" << std::string(name) << "' failed: " << result << "."; |
| 62 | } else { |
| 63 | RTC_LOG(LS_INFO) << "setAudioInputDevice(" << id << ") name '" << std::string(name) << "' success."; |
| 64 | specific = true; |
| 65 | } |
| 66 | return finish(); |
| 67 | } |
| 68 | } |
| 69 | RTC_LOG(LS_ERROR) << "setAudioInputDevice(" << id << "): Could not find recording device."; |
| 70 | return finish(); |
| 71 | } |
| 72 |
| 73 | void SetAudioOutputDeviceById(webrtc::AudioDeviceModule *adm, const std::string &id) { |
| 74 | if (adm->Playing()) { |
| 75 | adm->StopPlayout(); |
| 76 | } |
| 77 | auto specific = false; |
| 78 | const auto finish = [&] { |
| 79 | if (!specific) { |
| 80 | if (const auto result = adm->SetPlayoutDevice(webrtc::AudioDeviceModule::kDefaultCommunicationDevice)) { |
| 81 | RTC_LOG(LS_ERROR) << "setAudioOutputDevice(" << id << "): SetPlayoutDevice(kDefaultCommunicationDevice) failed: " << result << "."; |
| 82 | } else { |
| 83 | RTC_LOG(LS_INFO) << "setAudioOutputDevice(" << id << "): SetPlayoutDevice(kDefaultCommunicationDevice) success."; |
| 84 | } |
| 85 | } |
| 86 | if (adm->InitPlayout() == 0) { |
| 87 | adm->StartPlayout(); |
| 88 | } |
| 89 | }; |
| 90 | if (id == "default" || id.empty()) { |
| 91 | return finish(); |
| 92 | } |
| 93 | const auto count = adm |
| 94 | ? adm->PlayoutDevices() |
| 95 | : int16_t(-666); |
| 96 | if (count <= 0) { |
| 97 | RTC_LOG(LS_ERROR) << "setAudioOutputDevice(" << id << "): Could not get playout devices count: " << count << "."; |
| 98 | return finish(); |
| 99 | } |
| 100 | int16_t order = !id.empty() && id[0] == '#' ? static_cast<int16_t>(std::stoi(id.substr(1))) : -1; |
| 101 | for (auto i = 0; i != count; ++i) { |
| 102 | char name[webrtc::kAdmMaxDeviceNameSize + 1] = { 0 }; |
| 103 | char guid[webrtc::kAdmMaxGuidSize + 1] = { 0 }; |
| 104 | adm->PlayoutDeviceName(i, name, guid); |
| 105 | if ((!SkipDefaultDevice(name) && id == guid) || order == i) { |
| 106 | const auto result = adm->SetPlayoutDevice(i); |
| 107 | if (result != 0) { |
| 108 | RTC_LOG(LS_ERROR) << "setAudioOutputDevice(" << id << ") name '" << std::string(name) << "' failed: " << result << "."; |
| 109 | } else { |
| 110 | RTC_LOG(LS_INFO) << "setAudioOutputDevice(" << id << ") name '" << std::string(name) << "' success."; |
| 111 | specific = true; |
| 112 | } |
| 113 | return finish(); |
| 114 | } |
| 115 | } |
| 116 | RTC_LOG(LS_ERROR) << "setAudioOutputDevice(" << id << "): Could not find playout device."; |
| 117 | return finish(); |
| 118 | } |
| 119 |
| 120 | } // namespace tgcalls |
| 121 |