pulseaudio-wrapper.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /*
  2. Copyright (C) 2014 by Leonhard Oelke <[email protected]>
  3. Copyright (C) 2017 by Fabio Madia <[email protected]>
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #include <pthread.h>
  16. #include <pulse/thread-mainloop.h>
  17. #include <util/base.h>
  18. #include <obs.h>
  19. #include "pulseaudio-wrapper.h"
  20. /* global data */
  21. static uint_fast32_t pulseaudio_refs = 0;
  22. static pthread_mutex_t pulseaudio_mutex = PTHREAD_MUTEX_INITIALIZER;
  23. static pa_threaded_mainloop *pulseaudio_mainloop = NULL;
  24. static pa_context *pulseaudio_context = NULL;
  25. static void pulseaudio_default_devices(pa_context *c, const pa_server_info *i,
  26. void *userdata)
  27. {
  28. UNUSED_PARAMETER(c);
  29. struct pulseaudio_default_output *d =
  30. (struct pulseaudio_default_output *)userdata;
  31. d->default_sink_name = bstrdup(i->default_sink_name);
  32. pulseaudio_signal(0);
  33. }
  34. void get_default_id(char **id)
  35. {
  36. pulseaudio_init();
  37. struct pulseaudio_default_output *pdo =
  38. bzalloc(sizeof(struct pulseaudio_default_output));
  39. pulseaudio_get_server_info(
  40. (pa_server_info_cb_t)pulseaudio_default_devices, (void *)pdo);
  41. *id = bzalloc(strlen(pdo->default_sink_name) + 9);
  42. strcat(*id, pdo->default_sink_name);
  43. strcat(*id, ".monitor");
  44. bfree(pdo->default_sink_name);
  45. bfree(pdo);
  46. pulseaudio_unref();
  47. }
  48. bool devices_match(const char *id1, const char *id2)
  49. {
  50. bool match;
  51. char *name1 = NULL;
  52. char *name2 = NULL;
  53. if (!id1 || !id2)
  54. return false;
  55. if (strcmp(id1, "default") == 0) {
  56. get_default_id(&name1);
  57. id1 = name1;
  58. }
  59. if (strcmp(id2, "default") == 0) {
  60. get_default_id(&name2);
  61. id2 = name2;
  62. }
  63. match = strcmp(id1, id2) == 0;
  64. bfree(name1);
  65. bfree(name2);
  66. return match;
  67. }
  68. /**
  69. * context status change callback
  70. *
  71. * @todo this is currently a noop, we want to reconnect here if the connection
  72. * is lost ...
  73. */
  74. static void pulseaudio_context_state_changed(pa_context *c, void *userdata)
  75. {
  76. UNUSED_PARAMETER(userdata);
  77. UNUSED_PARAMETER(c);
  78. pulseaudio_signal(0);
  79. }
  80. /**
  81. * get the default properties
  82. */
  83. static pa_proplist *pulseaudio_properties()
  84. {
  85. pa_proplist *p = pa_proplist_new();
  86. pa_proplist_sets(p, PA_PROP_APPLICATION_NAME, "OBS");
  87. pa_proplist_sets(p, PA_PROP_APPLICATION_ICON_NAME, "obs");
  88. pa_proplist_sets(p, PA_PROP_MEDIA_ROLE, "production");
  89. return p;
  90. }
  91. /**
  92. * Initialize the pulse audio context with properties and callback
  93. */
  94. static void pulseaudio_init_context()
  95. {
  96. pulseaudio_lock();
  97. pa_proplist *p = pulseaudio_properties();
  98. pulseaudio_context = pa_context_new_with_proplist(
  99. pa_threaded_mainloop_get_api(pulseaudio_mainloop),
  100. "OBS-Monitor", p);
  101. pa_context_set_state_callback(pulseaudio_context,
  102. pulseaudio_context_state_changed, NULL);
  103. pa_context_connect(pulseaudio_context, NULL, PA_CONTEXT_NOAUTOSPAWN,
  104. NULL);
  105. pa_proplist_free(p);
  106. pulseaudio_unlock();
  107. }
  108. /**
  109. * wait for context to be ready
  110. */
  111. static int_fast32_t pulseaudio_context_ready()
  112. {
  113. pulseaudio_lock();
  114. if (!PA_CONTEXT_IS_GOOD(pa_context_get_state(pulseaudio_context))) {
  115. pulseaudio_unlock();
  116. return -1;
  117. }
  118. while (pa_context_get_state(pulseaudio_context) != PA_CONTEXT_READY)
  119. pulseaudio_wait();
  120. pulseaudio_unlock();
  121. return 0;
  122. }
  123. int_fast32_t pulseaudio_init()
  124. {
  125. pthread_mutex_lock(&pulseaudio_mutex);
  126. if (pulseaudio_refs == 0) {
  127. pulseaudio_mainloop = pa_threaded_mainloop_new();
  128. pa_threaded_mainloop_start(pulseaudio_mainloop);
  129. pulseaudio_init_context();
  130. }
  131. pulseaudio_refs++;
  132. pthread_mutex_unlock(&pulseaudio_mutex);
  133. return 0;
  134. }
  135. void pulseaudio_unref()
  136. {
  137. pthread_mutex_lock(&pulseaudio_mutex);
  138. if (--pulseaudio_refs == 0) {
  139. pulseaudio_lock();
  140. if (pulseaudio_context != NULL) {
  141. pa_context_disconnect(pulseaudio_context);
  142. pa_context_unref(pulseaudio_context);
  143. pulseaudio_context = NULL;
  144. }
  145. pulseaudio_unlock();
  146. if (pulseaudio_mainloop != NULL) {
  147. pa_threaded_mainloop_stop(pulseaudio_mainloop);
  148. pa_threaded_mainloop_free(pulseaudio_mainloop);
  149. pulseaudio_mainloop = NULL;
  150. }
  151. }
  152. pthread_mutex_unlock(&pulseaudio_mutex);
  153. }
  154. void pulseaudio_lock()
  155. {
  156. pa_threaded_mainloop_lock(pulseaudio_mainloop);
  157. }
  158. void pulseaudio_unlock()
  159. {
  160. pa_threaded_mainloop_unlock(pulseaudio_mainloop);
  161. }
  162. void pulseaudio_wait()
  163. {
  164. pa_threaded_mainloop_wait(pulseaudio_mainloop);
  165. }
  166. void pulseaudio_signal(int wait_for_accept)
  167. {
  168. pa_threaded_mainloop_signal(pulseaudio_mainloop, wait_for_accept);
  169. }
  170. void pulseaudio_accept()
  171. {
  172. pa_threaded_mainloop_accept(pulseaudio_mainloop);
  173. }
  174. int_fast32_t pulseaudio_get_source_info_list(pa_source_info_cb_t cb,
  175. void *userdata)
  176. {
  177. if (pulseaudio_context_ready() < 0)
  178. return -1;
  179. pulseaudio_lock();
  180. pa_operation *op = pa_context_get_source_info_list(pulseaudio_context,
  181. cb, userdata);
  182. if (!op) {
  183. pulseaudio_unlock();
  184. return -1;
  185. }
  186. while (pa_operation_get_state(op) == PA_OPERATION_RUNNING)
  187. pulseaudio_wait();
  188. pa_operation_unref(op);
  189. pulseaudio_unlock();
  190. return 0;
  191. }
  192. int_fast32_t pulseaudio_get_source_info(pa_source_info_cb_t cb,
  193. const char *name, void *userdata)
  194. {
  195. if (pulseaudio_context_ready() < 0)
  196. return -1;
  197. pulseaudio_lock();
  198. pa_operation *op = pa_context_get_source_info_by_name(
  199. pulseaudio_context, name, cb, userdata);
  200. if (!op) {
  201. pulseaudio_unlock();
  202. return -1;
  203. }
  204. while (pa_operation_get_state(op) == PA_OPERATION_RUNNING)
  205. pulseaudio_wait();
  206. pa_operation_unref(op);
  207. pulseaudio_unlock();
  208. return 0;
  209. }
  210. int_fast32_t pulseaudio_get_server_info(pa_server_info_cb_t cb, void *userdata)
  211. {
  212. if (pulseaudio_context_ready() < 0)
  213. return -1;
  214. pulseaudio_lock();
  215. pa_operation *op =
  216. pa_context_get_server_info(pulseaudio_context, cb, userdata);
  217. if (!op) {
  218. pulseaudio_unlock();
  219. return -1;
  220. }
  221. while (pa_operation_get_state(op) == PA_OPERATION_RUNNING)
  222. pulseaudio_wait();
  223. pa_operation_unref(op);
  224. pulseaudio_unlock();
  225. return 0;
  226. }
  227. pa_stream *pulseaudio_stream_new(const char *name, const pa_sample_spec *ss,
  228. const pa_channel_map *map)
  229. {
  230. if (pulseaudio_context_ready() < 0)
  231. return NULL;
  232. pulseaudio_lock();
  233. pa_proplist *p = pulseaudio_properties();
  234. pa_stream *s = pa_stream_new_with_proplist(pulseaudio_context, name, ss,
  235. map, p);
  236. pa_proplist_free(p);
  237. pulseaudio_unlock();
  238. return s;
  239. }
  240. int_fast32_t pulseaudio_connect_playback(pa_stream *s, const char *name,
  241. const pa_buffer_attr *attr,
  242. pa_stream_flags_t flags)
  243. {
  244. if (pulseaudio_context_ready() < 0)
  245. return -1;
  246. size_t dev_len = strlen(name) - 8;
  247. char *device = bzalloc(dev_len + 1);
  248. memcpy(device, name, dev_len);
  249. pulseaudio_lock();
  250. int_fast32_t ret =
  251. pa_stream_connect_playback(s, device, attr, flags, NULL, NULL);
  252. pulseaudio_unlock();
  253. bfree(device);
  254. return ret;
  255. }
  256. void pulseaudio_write_callback(pa_stream *p, pa_stream_request_cb_t cb,
  257. void *userdata)
  258. {
  259. if (pulseaudio_context_ready() < 0)
  260. return;
  261. pulseaudio_lock();
  262. pa_stream_set_write_callback(p, cb, userdata);
  263. pulseaudio_unlock();
  264. }
  265. void pulseaudio_set_underflow_callback(pa_stream *p, pa_stream_notify_cb_t cb,
  266. void *userdata)
  267. {
  268. if (pulseaudio_context_ready() < 0)
  269. return;
  270. pulseaudio_lock();
  271. pa_stream_set_underflow_callback(p, cb, userdata);
  272. pulseaudio_unlock();
  273. }