pulseaudio-wrapper.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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 = bzalloc(
  38. sizeof(struct pulseaudio_default_output));
  39. pulseaudio_get_server_info(
  40. (pa_server_info_cb_t) pulseaudio_default_devices,
  41. (void *) pdo);
  42. *id = bzalloc(strlen(pdo->default_sink_name) + 9);
  43. strcat(*id, pdo->default_sink_name);
  44. strcat(*id, ".monitor");
  45. bfree(pdo->default_sink_name);
  46. bfree(pdo);
  47. pulseaudio_unref();
  48. }
  49. bool devices_match(const char *id1, const char *id2)
  50. {
  51. bool match;
  52. char *name1 = NULL;
  53. char *name2 = NULL;
  54. if (!id1 || !id2)
  55. return false;
  56. if (strcmp(id1, "default") == 0) {
  57. get_default_id(&name1);
  58. id1 = name1;
  59. }
  60. if (strcmp(id2, "default") == 0) {
  61. get_default_id(&name2);
  62. id2 = name2;
  63. }
  64. match = strcmp(id1, id2) == 0;
  65. bfree(name1);
  66. bfree(name2);
  67. return match;
  68. }
  69. /**
  70. * context status change callback
  71. *
  72. * @todo this is currently a noop, we want to reconnect here if the connection
  73. * is lost ...
  74. */
  75. static void pulseaudio_context_state_changed(pa_context *c, void *userdata)
  76. {
  77. UNUSED_PARAMETER(userdata);
  78. UNUSED_PARAMETER(c);
  79. pulseaudio_signal(0);
  80. }
  81. /**
  82. * get the default properties
  83. */
  84. static pa_proplist *pulseaudio_properties()
  85. {
  86. pa_proplist *p = pa_proplist_new();
  87. pa_proplist_sets(p, PA_PROP_APPLICATION_NAME, "OBS");
  88. pa_proplist_sets(p, PA_PROP_APPLICATION_ICON_NAME, "obs");
  89. pa_proplist_sets(p, PA_PROP_MEDIA_ROLE, "production");
  90. return p;
  91. }
  92. /**
  93. * Initialize the pulse audio context with properties and callback
  94. */
  95. static void pulseaudio_init_context()
  96. {
  97. pulseaudio_lock();
  98. pa_proplist *p = pulseaudio_properties();
  99. pulseaudio_context = pa_context_new_with_proplist(
  100. pa_threaded_mainloop_get_api(pulseaudio_mainloop),
  101. "OBS-Monitor", p);
  102. pa_context_set_state_callback(pulseaudio_context,
  103. pulseaudio_context_state_changed, NULL);
  104. pa_context_connect(pulseaudio_context, NULL, PA_CONTEXT_NOAUTOSPAWN,
  105. NULL);
  106. pa_proplist_free(p);
  107. pulseaudio_unlock();
  108. }
  109. /**
  110. * wait for context to be ready
  111. */
  112. static int_fast32_t pulseaudio_context_ready()
  113. {
  114. pulseaudio_lock();
  115. if (!PA_CONTEXT_IS_GOOD(pa_context_get_state(pulseaudio_context))) {
  116. pulseaudio_unlock();
  117. return -1;
  118. }
  119. while (pa_context_get_state(pulseaudio_context) != PA_CONTEXT_READY)
  120. pulseaudio_wait();
  121. pulseaudio_unlock();
  122. return 0;
  123. }
  124. int_fast32_t pulseaudio_init()
  125. {
  126. pthread_mutex_lock(&pulseaudio_mutex);
  127. if (pulseaudio_refs == 0) {
  128. pulseaudio_mainloop = pa_threaded_mainloop_new();
  129. pa_threaded_mainloop_start(pulseaudio_mainloop);
  130. pulseaudio_init_context();
  131. }
  132. pulseaudio_refs++;
  133. pthread_mutex_unlock(&pulseaudio_mutex);
  134. return 0;
  135. }
  136. void pulseaudio_unref()
  137. {
  138. pthread_mutex_lock(&pulseaudio_mutex);
  139. if (--pulseaudio_refs == 0) {
  140. pulseaudio_lock();
  141. if (pulseaudio_context != NULL) {
  142. pa_context_disconnect(pulseaudio_context);
  143. pa_context_unref(pulseaudio_context);
  144. pulseaudio_context = NULL;
  145. }
  146. pulseaudio_unlock();
  147. if (pulseaudio_mainloop != NULL) {
  148. pa_threaded_mainloop_stop(pulseaudio_mainloop);
  149. pa_threaded_mainloop_free(pulseaudio_mainloop);
  150. pulseaudio_mainloop = NULL;
  151. }
  152. }
  153. pthread_mutex_unlock(&pulseaudio_mutex);
  154. }
  155. void pulseaudio_lock()
  156. {
  157. pa_threaded_mainloop_lock(pulseaudio_mainloop);
  158. }
  159. void pulseaudio_unlock()
  160. {
  161. pa_threaded_mainloop_unlock(pulseaudio_mainloop);
  162. }
  163. void pulseaudio_wait()
  164. {
  165. pa_threaded_mainloop_wait(pulseaudio_mainloop);
  166. }
  167. void pulseaudio_signal(int wait_for_accept)
  168. {
  169. pa_threaded_mainloop_signal(pulseaudio_mainloop, wait_for_accept);
  170. }
  171. void pulseaudio_accept()
  172. {
  173. pa_threaded_mainloop_accept(pulseaudio_mainloop);
  174. }
  175. int_fast32_t pulseaudio_get_source_info_list(pa_source_info_cb_t cb,
  176. void *userdata)
  177. {
  178. if (pulseaudio_context_ready() < 0)
  179. return -1;
  180. pulseaudio_lock();
  181. pa_operation *op = pa_context_get_source_info_list(
  182. pulseaudio_context, cb, userdata);
  183. if (!op) {
  184. pulseaudio_unlock();
  185. return -1;
  186. }
  187. while (pa_operation_get_state(op) == PA_OPERATION_RUNNING)
  188. pulseaudio_wait();
  189. pa_operation_unref(op);
  190. pulseaudio_unlock();
  191. return 0;
  192. }
  193. int_fast32_t pulseaudio_get_source_info(pa_source_info_cb_t cb,
  194. const char *name, void *userdata)
  195. {
  196. if (pulseaudio_context_ready() < 0)
  197. return -1;
  198. pulseaudio_lock();
  199. pa_operation *op = pa_context_get_source_info_by_name(
  200. pulseaudio_context, name, cb, userdata);
  201. if (!op) {
  202. pulseaudio_unlock();
  203. return -1;
  204. }
  205. while (pa_operation_get_state(op) == PA_OPERATION_RUNNING)
  206. pulseaudio_wait();
  207. pa_operation_unref(op);
  208. pulseaudio_unlock();
  209. return 0;
  210. }
  211. int_fast32_t pulseaudio_get_server_info(pa_server_info_cb_t cb, void *userdata)
  212. {
  213. if (pulseaudio_context_ready() < 0)
  214. return -1;
  215. pulseaudio_lock();
  216. pa_operation *op = pa_context_get_server_info(
  217. pulseaudio_context, cb, userdata);
  218. if (!op) {
  219. pulseaudio_unlock();
  220. return -1;
  221. }
  222. while (pa_operation_get_state(op) == PA_OPERATION_RUNNING)
  223. pulseaudio_wait();
  224. pa_operation_unref(op);
  225. pulseaudio_unlock();
  226. return 0;
  227. }
  228. pa_stream *pulseaudio_stream_new(const char *name, const pa_sample_spec *ss,
  229. const pa_channel_map *map)
  230. {
  231. if (pulseaudio_context_ready() < 0)
  232. return NULL;
  233. pulseaudio_lock();
  234. pa_proplist *p = pulseaudio_properties();
  235. pa_stream *s = pa_stream_new_with_proplist(
  236. pulseaudio_context, name, ss, map, p);
  237. pa_proplist_free(p);
  238. pulseaudio_unlock();
  239. return s;
  240. }
  241. int_fast32_t pulseaudio_connect_playback(pa_stream *s, const char *name,
  242. const pa_buffer_attr *attr, 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[dev_len];
  248. memcpy(device, name, dev_len);
  249. device[dev_len] = '\0';
  250. pulseaudio_lock();
  251. int_fast32_t ret = pa_stream_connect_playback(s, device, attr, flags,
  252. NULL, NULL);
  253. pulseaudio_unlock();
  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. }