AudioPulse.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. //
  2. // libtgvoip is free and unencumbered public domain software.
  3. // For more information, see http://unlicense.org or the UNLICENSE file
  4. // you should have received with this source code distribution.
  5. //
  6. #include "AudioPulse.h"
  7. #include <dlfcn.h>
  8. #include <libgen.h>
  9. #include "../../logging.h"
  10. #define DECLARE_DL_FUNCTION(name) typeof(name)* AudioPulse::_import_##name=NULL
  11. #define CHECK_DL_ERROR(res, msg) if(!res){LOGE(msg ": %s", dlerror()); return false;}
  12. #define LOAD_DL_FUNCTION(name) {_import_##name=(typeof(_import_##name))dlsym(lib, #name); CHECK_DL_ERROR(_import_##name, "Error getting entry point for " #name);}
  13. #define CHECK_ERROR(res, msg) if(res!=0){LOGE(msg " failed: %s", pa_strerror(res)); failed=true; return;}
  14. using namespace tgvoip;
  15. using namespace tgvoip::audio;
  16. bool AudioPulse::loaded=false;
  17. void* AudioPulse::lib=NULL;
  18. DECLARE_DL_FUNCTION(pa_threaded_mainloop_new);
  19. DECLARE_DL_FUNCTION(pa_threaded_mainloop_get_api);
  20. DECLARE_DL_FUNCTION(pa_context_new);
  21. DECLARE_DL_FUNCTION(pa_context_new_with_proplist);
  22. DECLARE_DL_FUNCTION(pa_context_set_state_callback);
  23. DECLARE_DL_FUNCTION(pa_threaded_mainloop_lock);
  24. DECLARE_DL_FUNCTION(pa_threaded_mainloop_unlock);
  25. DECLARE_DL_FUNCTION(pa_threaded_mainloop_start);
  26. DECLARE_DL_FUNCTION(pa_context_connect);
  27. DECLARE_DL_FUNCTION(pa_context_get_state);
  28. DECLARE_DL_FUNCTION(pa_threaded_mainloop_wait);
  29. DECLARE_DL_FUNCTION(pa_stream_new_with_proplist);
  30. DECLARE_DL_FUNCTION(pa_stream_set_state_callback);
  31. DECLARE_DL_FUNCTION(pa_stream_set_write_callback);
  32. DECLARE_DL_FUNCTION(pa_stream_connect_playback);
  33. DECLARE_DL_FUNCTION(pa_operation_unref);
  34. DECLARE_DL_FUNCTION(pa_stream_cork);
  35. DECLARE_DL_FUNCTION(pa_threaded_mainloop_stop);
  36. DECLARE_DL_FUNCTION(pa_stream_disconnect);
  37. DECLARE_DL_FUNCTION(pa_stream_unref);
  38. DECLARE_DL_FUNCTION(pa_context_disconnect);
  39. DECLARE_DL_FUNCTION(pa_context_unref);
  40. DECLARE_DL_FUNCTION(pa_threaded_mainloop_free);
  41. DECLARE_DL_FUNCTION(pa_threaded_mainloop_signal);
  42. DECLARE_DL_FUNCTION(pa_stream_begin_write);
  43. DECLARE_DL_FUNCTION(pa_stream_write);
  44. DECLARE_DL_FUNCTION(pa_stream_get_state);
  45. DECLARE_DL_FUNCTION(pa_strerror);
  46. DECLARE_DL_FUNCTION(pa_stream_set_read_callback);
  47. DECLARE_DL_FUNCTION(pa_stream_connect_record);
  48. DECLARE_DL_FUNCTION(pa_stream_peek);
  49. DECLARE_DL_FUNCTION(pa_stream_drop);
  50. DECLARE_DL_FUNCTION(pa_mainloop_new);
  51. DECLARE_DL_FUNCTION(pa_mainloop_get_api);
  52. DECLARE_DL_FUNCTION(pa_mainloop_iterate);
  53. DECLARE_DL_FUNCTION(pa_mainloop_free);
  54. DECLARE_DL_FUNCTION(pa_context_get_sink_info_list);
  55. DECLARE_DL_FUNCTION(pa_context_get_source_info_list);
  56. DECLARE_DL_FUNCTION(pa_operation_get_state);
  57. DECLARE_DL_FUNCTION(pa_proplist_new);
  58. DECLARE_DL_FUNCTION(pa_proplist_sets);
  59. DECLARE_DL_FUNCTION(pa_proplist_free);
  60. DECLARE_DL_FUNCTION(pa_stream_get_latency);
  61. #include "PulseFunctions.h"
  62. bool AudioPulse::Load(){
  63. if(loaded)
  64. return true;
  65. lib=dlopen("libpulse.so.0", RTLD_LAZY);
  66. if(!lib)
  67. lib=dlopen("libpulse.so", RTLD_LAZY);
  68. if(!lib){
  69. LOGE("Error loading libpulse: %s", dlerror());
  70. return false;
  71. }
  72. LOAD_DL_FUNCTION(pa_threaded_mainloop_new);
  73. LOAD_DL_FUNCTION(pa_threaded_mainloop_get_api);
  74. LOAD_DL_FUNCTION(pa_context_new);
  75. LOAD_DL_FUNCTION(pa_context_new_with_proplist);
  76. LOAD_DL_FUNCTION(pa_context_set_state_callback);
  77. LOAD_DL_FUNCTION(pa_threaded_mainloop_lock);
  78. LOAD_DL_FUNCTION(pa_threaded_mainloop_unlock);
  79. LOAD_DL_FUNCTION(pa_threaded_mainloop_start);
  80. LOAD_DL_FUNCTION(pa_context_connect);
  81. LOAD_DL_FUNCTION(pa_context_get_state);
  82. LOAD_DL_FUNCTION(pa_threaded_mainloop_wait);
  83. LOAD_DL_FUNCTION(pa_stream_new_with_proplist);
  84. LOAD_DL_FUNCTION(pa_stream_set_state_callback);
  85. LOAD_DL_FUNCTION(pa_stream_set_write_callback);
  86. LOAD_DL_FUNCTION(pa_stream_connect_playback);
  87. LOAD_DL_FUNCTION(pa_operation_unref);
  88. LOAD_DL_FUNCTION(pa_stream_cork);
  89. LOAD_DL_FUNCTION(pa_threaded_mainloop_stop);
  90. LOAD_DL_FUNCTION(pa_stream_disconnect);
  91. LOAD_DL_FUNCTION(pa_stream_unref);
  92. LOAD_DL_FUNCTION(pa_context_disconnect);
  93. LOAD_DL_FUNCTION(pa_context_unref);
  94. LOAD_DL_FUNCTION(pa_threaded_mainloop_free);
  95. LOAD_DL_FUNCTION(pa_threaded_mainloop_signal);
  96. LOAD_DL_FUNCTION(pa_stream_begin_write);
  97. LOAD_DL_FUNCTION(pa_stream_write);
  98. LOAD_DL_FUNCTION(pa_stream_get_state);
  99. LOAD_DL_FUNCTION(pa_strerror);
  100. LOAD_DL_FUNCTION(pa_stream_set_read_callback);
  101. LOAD_DL_FUNCTION(pa_stream_connect_record);
  102. LOAD_DL_FUNCTION(pa_stream_peek);
  103. LOAD_DL_FUNCTION(pa_stream_drop);
  104. LOAD_DL_FUNCTION(pa_mainloop_new);
  105. LOAD_DL_FUNCTION(pa_mainloop_get_api);
  106. LOAD_DL_FUNCTION(pa_mainloop_iterate);
  107. LOAD_DL_FUNCTION(pa_mainloop_free);
  108. LOAD_DL_FUNCTION(pa_context_get_sink_info_list);
  109. LOAD_DL_FUNCTION(pa_context_get_source_info_list);
  110. LOAD_DL_FUNCTION(pa_operation_get_state);
  111. LOAD_DL_FUNCTION(pa_proplist_new);
  112. LOAD_DL_FUNCTION(pa_proplist_sets);
  113. LOAD_DL_FUNCTION(pa_proplist_free);
  114. LOAD_DL_FUNCTION(pa_stream_get_latency);
  115. loaded=true;
  116. return true;
  117. }
  118. AudioPulse::AudioPulse(std::string inputDevice, std::string outputDevice){
  119. if(!Load()){
  120. failed=true;
  121. LOGE("Failed to load libpulse");
  122. return;
  123. }
  124. mainloop=pa_threaded_mainloop_new();
  125. if(!mainloop){
  126. LOGE("Error initializing PulseAudio (pa_threaded_mainloop_new)");
  127. failed=true;
  128. return;
  129. }
  130. mainloopApi=pa_threaded_mainloop_get_api(mainloop);
  131. #ifndef MAXPATHLEN
  132. char exeName[20];
  133. #else
  134. char exePath[MAXPATHLEN];
  135. char exeName[MAXPATHLEN];
  136. ssize_t lres=readlink("/proc/self/exe", exePath, sizeof(exePath));
  137. if(lres==-1)
  138. lres=readlink("/proc/curproc/file", exePath, sizeof(exePath));
  139. if(lres==-1)
  140. lres=readlink("/proc/curproc/exe", exePath, sizeof(exePath));
  141. if(lres>0){
  142. strcpy(exeName, basename(exePath));
  143. }else
  144. #endif
  145. {
  146. snprintf(exeName, sizeof(exeName), "Process %d", getpid());
  147. }
  148. pa_proplist* proplist=pa_proplist_new();
  149. pa_proplist_sets(proplist, PA_PROP_MEDIA_ROLE, "phone");
  150. context=pa_context_new_with_proplist(mainloopApi, exeName, proplist);
  151. pa_proplist_free(proplist);
  152. if(!context){
  153. LOGE("Error initializing PulseAudio (pa_context_new)");
  154. failed=true;
  155. return;
  156. }
  157. pa_context_set_state_callback(context, [](pa_context* context, void* arg){
  158. AudioPulse* self=reinterpret_cast<AudioPulse*>(arg);
  159. pa_threaded_mainloop_signal(self->mainloop, 0);
  160. }, this);
  161. pa_threaded_mainloop_lock(mainloop);
  162. isLocked=true;
  163. int err=pa_threaded_mainloop_start(mainloop);
  164. CHECK_ERROR(err, "pa_threaded_mainloop_start");
  165. didStart=true;
  166. err=pa_context_connect(context, NULL, PA_CONTEXT_NOAUTOSPAWN, NULL);
  167. CHECK_ERROR(err, "pa_context_connect");
  168. while(true){
  169. pa_context_state_t contextState=pa_context_get_state(context);
  170. if(!PA_CONTEXT_IS_GOOD(contextState)){
  171. LOGE("Error initializing PulseAudio (PA_CONTEXT_IS_GOOD)");
  172. failed=true;
  173. return;
  174. }
  175. if(contextState==PA_CONTEXT_READY)
  176. break;
  177. pa_threaded_mainloop_wait(mainloop);
  178. }
  179. pa_threaded_mainloop_unlock(mainloop);
  180. isLocked=false;
  181. output=new AudioOutputPulse(context, mainloop, outputDevice);
  182. input=new AudioInputPulse(context, mainloop, inputDevice);
  183. }
  184. AudioPulse::~AudioPulse(){
  185. if(mainloop && didStart){
  186. if(isLocked)
  187. pa_threaded_mainloop_unlock(mainloop);
  188. pa_threaded_mainloop_stop(mainloop);
  189. }
  190. if(input)
  191. delete input;
  192. if(output)
  193. delete output;
  194. if(context){
  195. pa_context_disconnect(context);
  196. pa_context_unref(context);
  197. }
  198. if(mainloop)
  199. pa_threaded_mainloop_free(mainloop);
  200. }
  201. AudioOutput* AudioPulse::GetOutput(){
  202. return output;
  203. }
  204. AudioInput* AudioPulse::GetInput(){
  205. return input;
  206. }
  207. bool AudioPulse::DoOneOperation(std::function<pa_operation*(pa_context*)> f){
  208. if(!Load())
  209. return false;
  210. pa_mainloop* ml;
  211. pa_mainloop_api* mlAPI;
  212. pa_context* ctx;
  213. pa_operation* op=NULL;
  214. int paReady=0;
  215. ml=pa_mainloop_new();
  216. mlAPI=pa_mainloop_get_api(ml);
  217. ctx=pa_context_new(mlAPI, "libtgvoip");
  218. pa_context_connect(ctx, NULL, PA_CONTEXT_NOFLAGS, NULL);
  219. pa_context_set_state_callback(ctx, [](pa_context* context, void* arg){
  220. pa_context_state_t state;
  221. int* pa_ready=(int*)arg;
  222. state=pa_context_get_state(context);
  223. switch(state){
  224. case PA_CONTEXT_UNCONNECTED:
  225. case PA_CONTEXT_CONNECTING:
  226. case PA_CONTEXT_AUTHORIZING:
  227. case PA_CONTEXT_SETTING_NAME:
  228. default:
  229. break;
  230. case PA_CONTEXT_FAILED:
  231. case PA_CONTEXT_TERMINATED:
  232. *pa_ready=2;
  233. break;
  234. case PA_CONTEXT_READY:
  235. *pa_ready=1;
  236. break;
  237. }
  238. }, &paReady);
  239. while(true){
  240. if(paReady==0){
  241. pa_mainloop_iterate(ml, 1, NULL);
  242. continue;
  243. }
  244. if(paReady==2){
  245. pa_context_disconnect(ctx);
  246. pa_context_unref(ctx);
  247. pa_mainloop_free(ml);
  248. return false;
  249. }
  250. if(!op){
  251. op=f(ctx);
  252. continue;
  253. }
  254. if(pa_operation_get_state(op)==PA_OPERATION_DONE){
  255. pa_operation_unref(op);
  256. pa_context_disconnect(ctx);
  257. pa_context_unref(ctx);
  258. pa_mainloop_free(ml);
  259. return true;
  260. }
  261. pa_mainloop_iterate(ml, 1, NULL);
  262. }
  263. }