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