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