| 6 |
| 7 | #include "../../logging.h" |
| 8 | #include "../../VoIPController.h" |
| 9 | #include "AudioInputALSA.h" |
| 10 |
| 11 | #include <dlfcn.h> |
| 12 |
| 13 | #include <cassert> |
| 14 | #include <cstdio> |
| 15 | #include <cstdlib> |
| 16 | #include <cstring> |
| 17 |
| 18 | using namespace tgvoip::audio; |
| 19 |
| 20 | #define BUFFER_SIZE 960 |
| 21 | #define CHECK_ERROR(res, msg) \ |
| 22 | if (res < 0) \ |
| 23 | { \ |
| 24 | LOGE(msg ": %s", _snd_strerror(res)); \ |
| 25 | m_failed = true; \ |
| 26 | return; \ |
| 27 | } |
| 28 | #define CHECK_DL_ERROR(res, msg) \ |
| 29 | if (!res) \ |
| 30 | { \ |
| 31 | LOGE(msg ": %s", ::dlerror()); \ |
| 32 | m_failed = true; \ |
| 33 | return; \ |
| 34 | } |
| 35 | #define LOAD_FUNCTION(lib, name, ref) \ |
| 36 | { \ |
| 37 | ref = reinterpret_cast<decltype(ref)>(::dlsym(lib, name)); \ |
| 38 | CHECK_DL_ERROR(ref, "Error getting entry point for " name); \ |
| 39 | } |
| 40 |
| 41 | AudioInputALSA::AudioInputALSA(std::string devID) |
| 42 | { |
| 43 | m_isRecording = false; |
| 44 | m_handle = nullptr; |
| 45 |
| 46 | m_lib = ::dlopen("libasound.so.2", RTLD_LAZY); |
| 47 | if (m_lib == nullptr) |
| 48 | m_lib = ::dlopen("libasound.so", RTLD_LAZY); |
| 49 | if (m_lib == nullptr) |
| 50 | { |
| 51 | LOGE("Error loading libasound: %s", dlerror()); |
| 52 | m_failed = true; |
| 53 | return; |
| 54 | } |
| 55 |
| 56 | LOAD_FUNCTION(m_lib, "snd_pcm_open", m_snd_pcm_open); |
| 57 | LOAD_FUNCTION(m_lib, "snd_pcm_set_params", m_snd_pcm_set_params); |
| 58 | LOAD_FUNCTION(m_lib, "snd_pcm_close", m_snd_pcm_close); |
| 59 | LOAD_FUNCTION(m_lib, "snd_pcm_readi", m_snd_pcm_readi); |
| 60 | LOAD_FUNCTION(m_lib, "snd_pcm_recover", m_snd_pcm_recover); |
| 61 | LOAD_FUNCTION(m_lib, "snd_strerror", m_snd_strerror); |
| 62 |
| 63 | SetCurrentDevice(std::move(devID)); |
| 64 | } |
| 65 |
| 66 | AudioInputALSA::~AudioInputALSA() |
| 67 | { |
| 68 | if (m_handle != nullptr) |
| 69 | m_snd_pcm_close(m_handle); |
| 70 | if (m_lib != nullptr) |
| 71 | ::dlclose(m_lib); |
| 72 | } |
| 73 |
| 74 | void AudioInputALSA::Start() |
| 75 | { |
| 76 | if (m_failed || m_isRecording) |
| 77 | return; |
| 78 |
| 79 | m_isRecording = true; |
| 80 | m_thread = new Thread(std::bind(&AudioInputALSA::RunThread, this)); |
| 81 | m_thread->SetName("AudioInputALSA"); |
| 82 | m_thread->Start(); |
| 83 | } |
| 84 |
| 85 | void AudioInputALSA::Stop() |
| 86 | { |
| 87 | if (!m_isRecording) |
| 88 | return; |
| 89 |
| 90 | m_isRecording = false; |
| 91 | m_thread->Join(); |
| 92 | delete m_thread; |
| 93 | m_thread = nullptr; |
| 94 | } |
| 95 |
| 96 | void AudioInputALSA::RunThread() |
| 97 | { |
| 98 | std::uint8_t buffer[BUFFER_SIZE * 2]; |
| 99 | snd_pcm_sframes_t frames; |
| 100 | while (m_isRecording) |
| 101 | { |
| 102 | frames = m_snd_pcm_readi(m_handle, buffer, BUFFER_SIZE); |
| 103 | if (frames < 0) |
| 104 | { |
| 105 | frames = m_snd_pcm_recover(m_handle, static_cast<int>(frames), 0); |
| 106 | } |
| 107 | if (frames < 0) |
| 108 | { |
| 109 | LOGE("snd_pcm_readi failed: %s\n", m_snd_strerror(static_cast<int>(frames))); |
| 110 | break; |
| 111 | } |
| 112 | InvokeCallback(buffer, sizeof(buffer)); |
| 113 | } |
| 114 | } |
| 115 |
| 116 | void AudioInputALSA::SetCurrentDevice(std::string devID) |
| 117 | { |
| 118 | bool wasRecording = m_isRecording; |
| 119 | m_isRecording = false; |
| 120 | if (m_handle != nullptr) |
| 121 | { |
| 122 | m_thread->Join(); |
| 123 | m_snd_pcm_close(m_handle); |
| 124 | } |
| 125 | m_currentDevice = devID; |
| 126 |
| 127 | int res = m_snd_pcm_open(&m_handle, devID.c_str(), SND_PCM_STREAM_CAPTURE, 0); |
| 128 | if (res < 0) |
| 129 | res = m_snd_pcm_open(&m_handle, "default", SND_PCM_STREAM_CAPTURE, 0); |
| 130 | CHECK_ERROR(res, "snd_pcm_open failed"); |
| 131 |
| 132 | res = m_snd_pcm_set_params(m_handle, SND_PCM_FORMAT_S16, SND_PCM_ACCESS_RW_INTERLEAVED, 1, 48000, 1, 100000); |
| 133 | CHECK_ERROR(res, "snd_pcm_set_params failed"); |
| 134 |
| 135 | if (wasRecording) |
| 136 | { |
| 137 | m_isRecording = true; |
| 138 | m_thread->Start(); |
| 139 | } |
| 140 | } |
| 141 |
| 142 | void AudioInputALSA::EnumerateDevices(std::vector<AudioInputDevice>& devs) |
| 143 | { |
| 144 | int (*_snd_device_name_hint)(int card, const char* iface, void*** hints); |
| 145 | char* (*_snd_device_name_get_hint)(const void* hint, const char* id); |
| 146 | int (*_snd_device_name_free_hint)(void** hinst); |
| 147 | void* lib = ::dlopen("libasound.so.2", RTLD_LAZY); |
| 148 | if (lib == nullptr) |
| 149 | ::dlopen("libasound.so", RTLD_LAZY); |
| 150 | if (lib == nullptr) |
| 151 | return; |
| 152 |
| 153 | _snd_device_name_hint = reinterpret_cast<decltype(_snd_device_name_hint)> (::dlsym(lib, "snd_device_name_hint")); |
| 154 | _snd_device_name_get_hint = reinterpret_cast<decltype(_snd_device_name_get_hint)> (::dlsym(lib, "snd_device_name_get_hint")); |
| 155 | _snd_device_name_free_hint = reinterpret_cast<decltype(_snd_device_name_free_hint)>(::dlsym(lib, "snd_device_name_free_hint")); |
| 156 |
| 157 | if (_snd_device_name_hint == nullptr || _snd_device_name_get_hint == nullptr || _snd_device_name_free_hint == nullptr) |
| 158 | { |
| 159 | ::dlclose(lib); |
| 160 | return; |
| 161 | } |
| 162 |
| 163 | char** hints; |
| 164 | int err = _snd_device_name_hint(-1, "pcm", reinterpret_cast<void***>(&hints)); |
| 165 | if (err != 0) |
| 166 | { |
| 167 | ::dlclose(lib); |
| 168 | return; |
| 169 | } |
| 170 |
| 171 | char** n = hints; |
| 172 | while (*n != nullptr) |
| 173 | { |
| 174 | char* name = _snd_device_name_get_hint(*n, "NAME"); |
| 175 | if (strncmp(name, "surround", 8) == 0 || std::strcmp(name, "null") == 0) |
| 176 | { |
| 177 | std::free(name); |
| 178 | ++n; |
| 179 | continue; |
| 180 | } |
| 181 | char* desc = _snd_device_name_get_hint(*n, "DESC"); |
| 182 | char* ioid = _snd_device_name_get_hint(*n, "IOID"); |
| 183 | if (ioid == nullptr || std::strcmp(ioid, "Input") == 0) |
| 184 | { |
| 185 | char* l1 = std::strtok(desc, "\n"); |
| 186 | char* l2 = std::strtok(nullptr, "\n"); |
| 187 | char* tmp = std::strtok(l1, ","); |
| 188 | char* actualName = tmp; |
| 189 | while ((tmp = std::strtok(nullptr, ","))) |
| 190 | { |
| 191 | actualName = tmp; |
| 192 | } |
| 193 | if (actualName[0] == ' ') |
| 194 | actualName++; |
| 195 | AudioInputDevice dev; |
| 196 | dev.id = std::string(name); |
| 197 | if (l2 != nullptr) |
| 198 | { |
| 199 | char buf[256]; |
| 200 | std::snprintf(buf, sizeof(buf), "%s (%s)", actualName, l2); |
| 201 | dev.displayName = std::string(buf); |
| 202 | } |
| 203 | else |
| 204 | { |
| 205 | dev.displayName = std::string(actualName); |
| 206 | } |
| 207 | devs.emplace_back(std::move(dev)); |
| 208 | } |
| 209 | std::free(name); |
| 210 | std::free(desc); |
| 211 | std::free(ioid); |
| 212 | ++n; |
| 213 | } |
| 214 |
| 215 | ::dlclose(lib); |
| 216 | } |
| 217 |