| 6 |
| 7 | #include "../../logging.h" |
| 8 | #include "AudioPulse.h" |
| 9 |
| 10 | #include <dlfcn.h> |
| 11 |
| 12 | #include <cstring> |
| 13 |
| 14 | #define DECLARE_DL_FUNCTION(name) decltype(name)* AudioPulse::_import_##name = nullptr |
| 15 | #define CHECK_DL_ERROR(res, msg) \ |
| 16 | if (!res) \ |
| 17 | { \ |
| 18 | LOGE(msg ": %s", dlerror()); \ |
| 19 | return false; \ |
| 20 | } |
| 21 | #define LOAD_DL_FUNCTION(name) \ |
| 22 | { \ |
| 23 | _import_##name = reinterpret_cast<decltype(_import_##name)>(::dlsym(m_lib, #name)); \ |
| 24 | CHECK_DL_ERROR(_import_##name, "Error getting entry point for " #name); \ |
| 25 | } |
| 26 | #define CHECK_ERROR(res, msg) \ |
| 27 | if (res != 0) \ |
| 28 | { \ |
| 29 | LOGE(msg " failed: %s", pa_strerror(res)); \ |
| 30 | m_failed = true; \ |
| 31 | return; \ |
| 32 | } |
| 33 |
| 34 | using namespace tgvoip; |
| 35 | using namespace tgvoip::audio; |
| 36 |
| 37 | bool AudioPulse::m_loaded = false; |
| 38 | void* AudioPulse::m_lib = nullptr; |
| 39 |
| 40 | DECLARE_DL_FUNCTION(pa_threaded_mainloop_new); |
| 41 | DECLARE_DL_FUNCTION(pa_threaded_mainloop_get_api); |
| 42 | DECLARE_DL_FUNCTION(pa_context_new); |
| 43 | DECLARE_DL_FUNCTION(pa_context_new_with_proplist); |
| 44 | DECLARE_DL_FUNCTION(pa_context_set_state_callback); |
| 45 | DECLARE_DL_FUNCTION(pa_threaded_mainloop_lock); |
| 46 | DECLARE_DL_FUNCTION(pa_threaded_mainloop_unlock); |
| 47 | DECLARE_DL_FUNCTION(pa_threaded_mainloop_start); |
| 48 | DECLARE_DL_FUNCTION(pa_context_connect); |
| 49 | DECLARE_DL_FUNCTION(pa_context_get_state); |
| 50 | DECLARE_DL_FUNCTION(pa_threaded_mainloop_wait); |
| 51 | DECLARE_DL_FUNCTION(pa_stream_new_with_proplist); |
| 52 | DECLARE_DL_FUNCTION(pa_stream_set_state_callback); |
| 53 | DECLARE_DL_FUNCTION(pa_stream_set_write_callback); |
| 54 | DECLARE_DL_FUNCTION(pa_stream_connect_playback); |
| 55 | DECLARE_DL_FUNCTION(pa_operation_unref); |
| 56 | DECLARE_DL_FUNCTION(pa_stream_cork); |
| 57 | DECLARE_DL_FUNCTION(pa_threaded_mainloop_stop); |
| 58 | DECLARE_DL_FUNCTION(pa_stream_disconnect); |
| 59 | DECLARE_DL_FUNCTION(pa_stream_unref); |
| 60 | DECLARE_DL_FUNCTION(pa_context_disconnect); |
| 61 | DECLARE_DL_FUNCTION(pa_context_unref); |
| 62 | DECLARE_DL_FUNCTION(pa_threaded_mainloop_free); |
| 63 | DECLARE_DL_FUNCTION(pa_threaded_mainloop_signal); |
| 64 | DECLARE_DL_FUNCTION(pa_stream_begin_write); |
| 65 | DECLARE_DL_FUNCTION(pa_stream_write); |
| 66 | DECLARE_DL_FUNCTION(pa_stream_get_state); |
| 67 | DECLARE_DL_FUNCTION(pa_strerror); |
| 68 | DECLARE_DL_FUNCTION(pa_stream_set_read_callback); |
| 69 | DECLARE_DL_FUNCTION(pa_stream_connect_record); |
| 70 | DECLARE_DL_FUNCTION(pa_stream_peek); |
| 71 | DECLARE_DL_FUNCTION(pa_stream_drop); |
| 72 | DECLARE_DL_FUNCTION(pa_mainloop_new); |
| 73 | DECLARE_DL_FUNCTION(pa_mainloop_get_api); |
| 74 | DECLARE_DL_FUNCTION(pa_mainloop_iterate); |
| 75 | DECLARE_DL_FUNCTION(pa_mainloop_free); |
| 76 | DECLARE_DL_FUNCTION(pa_context_get_sink_info_list); |
| 77 | DECLARE_DL_FUNCTION(pa_context_get_source_info_list); |
| 78 | DECLARE_DL_FUNCTION(pa_operation_get_state); |
| 79 | DECLARE_DL_FUNCTION(pa_proplist_new); |
| 80 | DECLARE_DL_FUNCTION(pa_proplist_sets); |
| 81 | DECLARE_DL_FUNCTION(pa_proplist_free); |
| 82 | DECLARE_DL_FUNCTION(pa_stream_get_latency); |
| 83 |
| 84 | #include "PulseFunctions.h" |
| 85 |
| 86 | bool AudioPulse::Load() |
| 87 | { |
| 88 | if (m_loaded) |
| 89 | return true; |
| 90 |
| 91 | m_lib = dlopen("libpulse.so.0", RTLD_LAZY); |
| 92 | if (!m_lib) |
| 93 | m_lib = dlopen("libpulse.so", RTLD_LAZY); |
| 94 | if (!m_lib) |
| 95 | { |
| 96 | LOGE("Error loading libpulse: %s", dlerror()); |
| 97 | return false; |
| 98 | } |
| 99 |
| 100 | LOAD_DL_FUNCTION(pa_threaded_mainloop_new); |
| 101 | LOAD_DL_FUNCTION(pa_threaded_mainloop_get_api); |
| 102 | LOAD_DL_FUNCTION(pa_context_new); |
| 103 | LOAD_DL_FUNCTION(pa_context_new_with_proplist); |
| 104 | LOAD_DL_FUNCTION(pa_context_set_state_callback); |
| 105 | LOAD_DL_FUNCTION(pa_threaded_mainloop_lock); |
| 106 | LOAD_DL_FUNCTION(pa_threaded_mainloop_unlock); |
| 107 | LOAD_DL_FUNCTION(pa_threaded_mainloop_start); |
| 108 | LOAD_DL_FUNCTION(pa_context_connect); |
| 109 | LOAD_DL_FUNCTION(pa_context_get_state); |
| 110 | LOAD_DL_FUNCTION(pa_threaded_mainloop_wait); |
| 111 | LOAD_DL_FUNCTION(pa_stream_new_with_proplist); |
| 112 | LOAD_DL_FUNCTION(pa_stream_set_state_callback); |
| 113 | LOAD_DL_FUNCTION(pa_stream_set_write_callback); |
| 114 | LOAD_DL_FUNCTION(pa_stream_connect_playback); |
| 115 | LOAD_DL_FUNCTION(pa_operation_unref); |
| 116 | LOAD_DL_FUNCTION(pa_stream_cork); |
| 117 | LOAD_DL_FUNCTION(pa_threaded_mainloop_stop); |
| 118 | LOAD_DL_FUNCTION(pa_stream_disconnect); |
| 119 | LOAD_DL_FUNCTION(pa_stream_unref); |
| 120 | LOAD_DL_FUNCTION(pa_context_disconnect); |
| 121 | LOAD_DL_FUNCTION(pa_context_unref); |
| 122 | LOAD_DL_FUNCTION(pa_threaded_mainloop_free); |
| 123 | LOAD_DL_FUNCTION(pa_threaded_mainloop_signal); |
| 124 | LOAD_DL_FUNCTION(pa_stream_begin_write); |
| 125 | LOAD_DL_FUNCTION(pa_stream_write); |
| 126 | LOAD_DL_FUNCTION(pa_stream_get_state); |
| 127 | LOAD_DL_FUNCTION(pa_strerror); |
| 128 | LOAD_DL_FUNCTION(pa_stream_set_read_callback); |
| 129 | LOAD_DL_FUNCTION(pa_stream_connect_record); |
| 130 | LOAD_DL_FUNCTION(pa_stream_peek); |
| 131 | LOAD_DL_FUNCTION(pa_stream_drop); |
| 132 | LOAD_DL_FUNCTION(pa_mainloop_new); |
| 133 | LOAD_DL_FUNCTION(pa_mainloop_get_api); |
| 134 | LOAD_DL_FUNCTION(pa_mainloop_iterate); |
| 135 | LOAD_DL_FUNCTION(pa_mainloop_free); |
| 136 | LOAD_DL_FUNCTION(pa_context_get_sink_info_list); |
| 137 | LOAD_DL_FUNCTION(pa_context_get_source_info_list); |
| 138 | LOAD_DL_FUNCTION(pa_operation_get_state); |
| 139 | LOAD_DL_FUNCTION(pa_proplist_new); |
| 140 | LOAD_DL_FUNCTION(pa_proplist_sets); |
| 141 | LOAD_DL_FUNCTION(pa_proplist_free); |
| 142 | LOAD_DL_FUNCTION(pa_stream_get_latency); |
| 143 |
| 144 | m_loaded = true; |
| 145 | return true; |
| 146 | } |
| 147 |
| 148 | AudioPulse::AudioPulse(std::string inputDevice, std::string outputDevice) |
| 149 | { |
| 150 | if (!Load()) |
| 151 | { |
| 152 | m_failed = true; |
| 153 | LOGE("Failed to load libpulse"); |
| 154 | return; |
| 155 | } |
| 156 |
| 157 | m_mainloop = pa_threaded_mainloop_new(); |
| 158 | if (!m_mainloop) |
| 159 | { |
| 160 | LOGE("Error initializing PulseAudio (pa_threaded_mainloop_new)"); |
| 161 | m_failed = true; |
| 162 | return; |
| 163 | } |
| 164 | m_mainloopApi = pa_threaded_mainloop_get_api(m_mainloop); |
| 165 | #ifndef MAXPATHLEN |
| 166 | char exeName[20]; |
| 167 | #else |
| 168 | char exePath[MAXPATHLEN]; |
| 169 | char exeName[MAXPATHLEN]; |
| 170 | ssize_t lres = readlink("/proc/self/exe", exePath, sizeof(exePath)); |
| 171 | if (lres == -1) |
| 172 | lres = readlink("/proc/curproc/file", exePath, sizeof(exePath)); |
| 173 | if (lres == -1) |
| 174 | lres = readlink("/proc/curproc/exe", exePath, sizeof(exePath)); |
| 175 | if (lres > 0) |
| 176 | { |
| 177 | std::strcpy(exeName, ::basename(exePath)); |
| 178 | } |
| 179 | else |
| 180 | #endif |
| 181 | { |
| 182 | std::snprintf(exeName, sizeof(exeName), "Process %d", ::getpid()); |
| 183 | } |
| 184 | pa_proplist* proplist = pa_proplist_new(); |
| 185 | pa_proplist_sets(proplist, PA_PROP_MEDIA_ROLE, "phone"); |
| 186 | m_context = pa_context_new_with_proplist(m_mainloopApi, exeName, proplist); |
| 187 | pa_proplist_free(proplist); |
| 188 | if (m_context == nullptr) |
| 189 | { |
| 190 | LOGE("Error initializing PulseAudio (pa_context_new)"); |
| 191 | m_failed = true; |
| 192 | return; |
| 193 | } |
| 194 | pa_context_set_state_callback( |
| 195 | m_context, [](pa_context* context, void* arg) |
| 196 | { |
| 197 | AudioPulse* self = reinterpret_cast<AudioPulse*>(arg); |
| 198 | pa_threaded_mainloop_signal(self->m_mainloop, 0); |
| 199 | }, |
| 200 | this); |
| 201 | pa_threaded_mainloop_lock(m_mainloop); |
| 202 | m_isLocked = true; |
| 203 | int err = pa_threaded_mainloop_start(m_mainloop); |
| 204 | CHECK_ERROR(err, "pa_threaded_mainloop_start"); |
| 205 | m_didStart = true; |
| 206 |
| 207 | err = pa_context_connect(m_context, nullptr, PA_CONTEXT_NOAUTOSPAWN, nullptr); |
| 208 | CHECK_ERROR(err, "pa_context_connect"); |
| 209 |
| 210 | while (true) |
| 211 | { |
| 212 | pa_context_state_t contextState = pa_context_get_state(m_context); |
| 213 | if (!PA_CONTEXT_IS_GOOD(contextState)) |
| 214 | { |
| 215 | LOGE("Error initializing PulseAudio (PA_CONTEXT_IS_GOOD)"); |
| 216 | m_failed = true; |
| 217 | return; |
| 218 | } |
| 219 | if (contextState == PA_CONTEXT_READY) |
| 220 | break; |
| 221 | pa_threaded_mainloop_wait(m_mainloop); |
| 222 | } |
| 223 | pa_threaded_mainloop_unlock(m_mainloop); |
| 224 | m_isLocked = false; |
| 225 |
| 226 | m_output = new AudioOutputPulse(m_context, m_mainloop, std::move(outputDevice)); |
| 227 | m_input = new AudioInputPulse(m_context, m_mainloop, std::move(inputDevice)); |
| 228 | } |
| 229 |
| 230 | AudioPulse::~AudioPulse() |
| 231 | { |
| 232 | if (m_mainloop != nullptr && m_didStart) |
| 233 | { |
| 234 | if (m_isLocked) |
| 235 | pa_threaded_mainloop_unlock(m_mainloop); |
| 236 | pa_threaded_mainloop_stop(m_mainloop); |
| 237 | } |
| 238 |
| 239 | delete m_input; |
| 240 | delete m_output; |
| 241 |
| 242 | if (m_context != nullptr) |
| 243 | { |
| 244 | pa_context_disconnect(m_context); |
| 245 | pa_context_unref(m_context); |
| 246 | } |
| 247 | if (m_mainloop != nullptr) |
| 248 | pa_threaded_mainloop_free(m_mainloop); |
| 249 | } |
| 250 |
| 251 | AudioOutput* AudioPulse::GetOutput() |
| 252 | { |
| 253 | return m_output; |
| 254 | } |
| 255 |
| 256 | AudioInput* AudioPulse::GetInput() |
| 257 | { |
| 258 | return m_input; |
| 259 | } |
| 260 |
| 261 | bool AudioPulse::DoOneOperation(std::function<pa_operation*(pa_context*)> f) |
| 262 | { |
| 263 | if (!Load()) |
| 264 | return false; |
| 265 |
| 266 | pa_mainloop* ml; |
| 267 | pa_mainloop_api* mlAPI; |
| 268 | pa_context* ctx; |
| 269 | pa_operation* op = nullptr; |
| 270 | int paReady = 0; |
| 271 |
| 272 | ml = pa_mainloop_new(); |
| 273 | mlAPI = pa_mainloop_get_api(ml); |
| 274 | ctx = pa_context_new(mlAPI, "libtgvoip"); |
| 275 |
| 276 | pa_context_connect(ctx, nullptr, PA_CONTEXT_NOFLAGS, nullptr); |
| 277 | pa_context_set_state_callback( |
| 278 | ctx, [](pa_context* context, void* arg) |
| 279 | { |
| 280 | pa_context_state_t state; |
| 281 | int* pa_ready = reinterpret_cast<int*>(arg); |
| 282 |
| 283 | state = pa_context_get_state(context); |
| 284 | switch (state) |
| 285 | { |
| 286 | case PA_CONTEXT_UNCONNECTED: |
| 287 | case PA_CONTEXT_CONNECTING: |
| 288 | case PA_CONTEXT_AUTHORIZING: |
| 289 | case PA_CONTEXT_SETTING_NAME: |
| 290 | default: |
| 291 | break; |
| 292 | case PA_CONTEXT_FAILED: |
| 293 | case PA_CONTEXT_TERMINATED: |
| 294 | *pa_ready = 2; |
| 295 | break; |
| 296 | case PA_CONTEXT_READY: |
| 297 | *pa_ready = 1; |
| 298 | break; |
| 299 | } |
| 300 | }, |
| 301 | &paReady); |
| 302 |
| 303 | while (true) |
| 304 | { |
| 305 | if (paReady == 0) |
| 306 | { |
| 307 | pa_mainloop_iterate(ml, 1, nullptr); |
| 308 | continue; |
| 309 | } |
| 310 | if (paReady == 2) |
| 311 | { |
| 312 | pa_context_disconnect(ctx); |
| 313 | pa_context_unref(ctx); |
| 314 | pa_mainloop_free(ml); |
| 315 | return false; |
| 316 | } |
| 317 | if (op == nullptr) |
| 318 | { |
| 319 | op = f(ctx); |
| 320 | continue; |
| 321 | } |
| 322 | if (pa_operation_get_state(op) == PA_OPERATION_DONE) |
| 323 | { |
| 324 | pa_operation_unref(op); |
| 325 | pa_context_disconnect(ctx); |
| 326 | pa_context_unref(ctx); |
| 327 | pa_mainloop_free(ml); |
| 328 | return true; |
| 329 | } |
| 330 | pa_mainloop_iterate(ml, 1, nullptr); |
| 331 | } |
| 332 | } |
| 333 |