1
0

pulse-wrapper.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /*
  2. Copyright (C) 2014 by Leonhard Oelke <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #include <pthread.h>
  15. #include <pulse/thread-mainloop.h>
  16. #include <util/base.h>
  17. #include <obs.h>
  18. #include "pulse-wrapper.h"
  19. /* global data */
  20. static uint_fast32_t pulse_refs = 0;
  21. static pthread_mutex_t pulse_mutex = PTHREAD_MUTEX_INITIALIZER;
  22. static pa_threaded_mainloop *pulse_mainloop = NULL;
  23. static pa_context *pulse_context = NULL;
  24. /**
  25. * context status change callback
  26. *
  27. * @todo this is currently a noop, we want to reconnect here if the connection
  28. * is lost ...
  29. */
  30. static void pulse_context_state_changed(pa_context *c, void *userdata)
  31. {
  32. UNUSED_PARAMETER(userdata);
  33. UNUSED_PARAMETER(c);
  34. pulse_signal(0);
  35. }
  36. /**
  37. * get the default properties
  38. */
  39. static pa_proplist *pulse_properties()
  40. {
  41. pa_proplist *p = pa_proplist_new();
  42. pa_proplist_sets(p, PA_PROP_APPLICATION_NAME, "OBS");
  43. pa_proplist_sets(p, PA_PROP_APPLICATION_ICON_NAME, "obs");
  44. pa_proplist_sets(p, PA_PROP_MEDIA_ROLE, "production");
  45. return p;
  46. }
  47. /**
  48. * Initialize the pulse audio context with properties and callback
  49. */
  50. static void pulse_init_context()
  51. {
  52. pulse_lock();
  53. pa_proplist *p = pulse_properties();
  54. pulse_context = pa_context_new_with_proplist(
  55. pa_threaded_mainloop_get_api(pulse_mainloop), "OBS", p);
  56. pa_context_set_state_callback(pulse_context,
  57. pulse_context_state_changed, NULL);
  58. pa_context_connect(pulse_context, NULL, PA_CONTEXT_NOAUTOSPAWN, NULL);
  59. pa_proplist_free(p);
  60. pulse_unlock();
  61. }
  62. /**
  63. * wait for context to be ready
  64. */
  65. static int_fast32_t pulse_context_ready()
  66. {
  67. pulse_lock();
  68. if (!PA_CONTEXT_IS_GOOD(pa_context_get_state(pulse_context))) {
  69. pulse_unlock();
  70. return -1;
  71. }
  72. while (pa_context_get_state(pulse_context) != PA_CONTEXT_READY)
  73. pulse_wait();
  74. pulse_unlock();
  75. return 0;
  76. }
  77. int_fast32_t pulse_init()
  78. {
  79. pthread_mutex_lock(&pulse_mutex);
  80. if (pulse_refs == 0) {
  81. pulse_mainloop = pa_threaded_mainloop_new();
  82. pa_threaded_mainloop_start(pulse_mainloop);
  83. pulse_init_context();
  84. }
  85. pulse_refs++;
  86. pthread_mutex_unlock(&pulse_mutex);
  87. return 0;
  88. }
  89. void pulse_unref()
  90. {
  91. pthread_mutex_lock(&pulse_mutex);
  92. if (--pulse_refs == 0) {
  93. pulse_lock();
  94. if (pulse_context != NULL) {
  95. pa_context_disconnect(pulse_context);
  96. pa_context_unref(pulse_context);
  97. pulse_context = NULL;
  98. }
  99. pulse_unlock();
  100. if (pulse_mainloop != NULL) {
  101. pa_threaded_mainloop_stop(pulse_mainloop);
  102. pa_threaded_mainloop_free(pulse_mainloop);
  103. pulse_mainloop = NULL;
  104. }
  105. }
  106. pthread_mutex_unlock(&pulse_mutex);
  107. }
  108. void pulse_lock()
  109. {
  110. pa_threaded_mainloop_lock(pulse_mainloop);
  111. }
  112. void pulse_unlock()
  113. {
  114. pa_threaded_mainloop_unlock(pulse_mainloop);
  115. }
  116. void pulse_wait()
  117. {
  118. pa_threaded_mainloop_wait(pulse_mainloop);
  119. }
  120. void pulse_signal(int wait_for_accept)
  121. {
  122. pa_threaded_mainloop_signal(pulse_mainloop, wait_for_accept);
  123. }
  124. void pulse_accept()
  125. {
  126. pa_threaded_mainloop_accept(pulse_mainloop);
  127. }
  128. int_fast32_t pulse_get_source_info_list(pa_source_info_cb_t cb, void *userdata)
  129. {
  130. if (pulse_context_ready() < 0)
  131. return -1;
  132. pulse_lock();
  133. pa_operation *op =
  134. pa_context_get_source_info_list(pulse_context, cb, userdata);
  135. if (!op) {
  136. pulse_unlock();
  137. return -1;
  138. }
  139. while (pa_operation_get_state(op) == PA_OPERATION_RUNNING)
  140. pulse_wait();
  141. pa_operation_unref(op);
  142. pulse_unlock();
  143. return 0;
  144. }
  145. int_fast32_t pulse_get_sink_info_list(pa_sink_info_cb_t cb, void *userdata)
  146. {
  147. if (pulse_context_ready() < 0)
  148. return -1;
  149. pulse_lock();
  150. pa_operation *op =
  151. pa_context_get_sink_info_list(pulse_context, cb, userdata);
  152. if (!op) {
  153. pulse_unlock();
  154. return -1;
  155. }
  156. while (pa_operation_get_state(op) == PA_OPERATION_RUNNING)
  157. pulse_wait();
  158. pa_operation_unref(op);
  159. pulse_unlock();
  160. return 0;
  161. }
  162. int_fast32_t pulse_get_source_info(pa_source_info_cb_t cb, const char *name,
  163. void *userdata)
  164. {
  165. if (pulse_context_ready() < 0)
  166. return -1;
  167. pulse_lock();
  168. pa_operation *op = pa_context_get_source_info_by_name(
  169. pulse_context, name, cb, userdata);
  170. if (!op) {
  171. pulse_unlock();
  172. return -1;
  173. }
  174. while (pa_operation_get_state(op) == PA_OPERATION_RUNNING)
  175. pulse_wait();
  176. pa_operation_unref(op);
  177. pulse_unlock();
  178. return 0;
  179. }
  180. int_fast32_t pulse_get_server_info(pa_server_info_cb_t cb, void *userdata)
  181. {
  182. if (pulse_context_ready() < 0)
  183. return -1;
  184. pulse_lock();
  185. pa_operation *op =
  186. pa_context_get_server_info(pulse_context, cb, userdata);
  187. if (!op) {
  188. pulse_unlock();
  189. return -1;
  190. }
  191. while (pa_operation_get_state(op) == PA_OPERATION_RUNNING)
  192. pulse_wait();
  193. pa_operation_unref(op);
  194. pulse_unlock();
  195. return 0;
  196. }
  197. pa_stream *pulse_stream_new(const char *name, const pa_sample_spec *ss,
  198. const pa_channel_map *map)
  199. {
  200. if (pulse_context_ready() < 0)
  201. return NULL;
  202. pulse_lock();
  203. pa_proplist *p = pulse_properties();
  204. pa_stream *s =
  205. pa_stream_new_with_proplist(pulse_context, name, ss, map, p);
  206. pa_proplist_free(p);
  207. pulse_unlock();
  208. return s;
  209. }