pulse-wrapper.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /*
  2. Copyright (C) 2014 by Leonhard Oelke <leonhard@in-verted.de>
  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(pa_threaded_mainloop_get_api(pulse_mainloop), "OBS", p);
  55. pa_context_set_state_callback(pulse_context, pulse_context_state_changed, NULL);
  56. pa_context_connect(pulse_context, NULL, PA_CONTEXT_NOAUTOSPAWN, NULL);
  57. pa_proplist_free(p);
  58. pulse_unlock();
  59. }
  60. /**
  61. * wait for context to be ready
  62. */
  63. static int_fast32_t pulse_context_ready()
  64. {
  65. pulse_lock();
  66. if (!PA_CONTEXT_IS_GOOD(pa_context_get_state(pulse_context))) {
  67. pulse_unlock();
  68. return -1;
  69. }
  70. while (pa_context_get_state(pulse_context) != PA_CONTEXT_READY)
  71. pulse_wait();
  72. pulse_unlock();
  73. return 0;
  74. }
  75. int_fast32_t pulse_init()
  76. {
  77. pthread_mutex_lock(&pulse_mutex);
  78. if (pulse_refs == 0) {
  79. pulse_mainloop = pa_threaded_mainloop_new();
  80. pa_threaded_mainloop_start(pulse_mainloop);
  81. pulse_init_context();
  82. }
  83. pulse_refs++;
  84. pthread_mutex_unlock(&pulse_mutex);
  85. return 0;
  86. }
  87. void pulse_unref()
  88. {
  89. pthread_mutex_lock(&pulse_mutex);
  90. if (--pulse_refs == 0) {
  91. pulse_lock();
  92. if (pulse_context != NULL) {
  93. pa_context_disconnect(pulse_context);
  94. pa_context_unref(pulse_context);
  95. pulse_context = NULL;
  96. }
  97. pulse_unlock();
  98. if (pulse_mainloop != NULL) {
  99. pa_threaded_mainloop_stop(pulse_mainloop);
  100. pa_threaded_mainloop_free(pulse_mainloop);
  101. pulse_mainloop = NULL;
  102. }
  103. }
  104. pthread_mutex_unlock(&pulse_mutex);
  105. }
  106. void pulse_lock()
  107. {
  108. pa_threaded_mainloop_lock(pulse_mainloop);
  109. }
  110. void pulse_unlock()
  111. {
  112. pa_threaded_mainloop_unlock(pulse_mainloop);
  113. }
  114. void pulse_wait()
  115. {
  116. pa_threaded_mainloop_wait(pulse_mainloop);
  117. }
  118. void pulse_signal(int wait_for_accept)
  119. {
  120. pa_threaded_mainloop_signal(pulse_mainloop, wait_for_accept);
  121. }
  122. void pulse_accept()
  123. {
  124. pa_threaded_mainloop_accept(pulse_mainloop);
  125. }
  126. int_fast32_t pulse_get_source_info_list(pa_source_info_cb_t cb, void *userdata)
  127. {
  128. if (pulse_context_ready() < 0)
  129. return -1;
  130. pulse_lock();
  131. pa_operation *op = pa_context_get_source_info_list(pulse_context, cb, userdata);
  132. if (!op) {
  133. pulse_unlock();
  134. return -1;
  135. }
  136. while (pa_operation_get_state(op) == PA_OPERATION_RUNNING)
  137. pulse_wait();
  138. pa_operation_unref(op);
  139. pulse_unlock();
  140. return 0;
  141. }
  142. int_fast32_t pulse_get_sink_info_list(pa_sink_info_cb_t cb, void *userdata)
  143. {
  144. if (pulse_context_ready() < 0)
  145. return -1;
  146. pulse_lock();
  147. pa_operation *op = pa_context_get_sink_info_list(pulse_context, cb, userdata);
  148. if (!op) {
  149. pulse_unlock();
  150. return -1;
  151. }
  152. while (pa_operation_get_state(op) == PA_OPERATION_RUNNING)
  153. pulse_wait();
  154. pa_operation_unref(op);
  155. pulse_unlock();
  156. return 0;
  157. }
  158. int_fast32_t pulse_get_source_info(pa_source_info_cb_t cb, const char *name, void *userdata)
  159. {
  160. if (pulse_context_ready() < 0)
  161. return -1;
  162. pulse_lock();
  163. pa_operation *op = pa_context_get_source_info_by_name(pulse_context, name, cb, userdata);
  164. if (!op) {
  165. pulse_unlock();
  166. return -1;
  167. }
  168. while (pa_operation_get_state(op) == PA_OPERATION_RUNNING)
  169. pulse_wait();
  170. pa_operation_unref(op);
  171. pulse_unlock();
  172. return 0;
  173. }
  174. int_fast32_t pulse_get_server_info(pa_server_info_cb_t cb, void *userdata)
  175. {
  176. if (pulse_context_ready() < 0)
  177. return -1;
  178. pulse_lock();
  179. pa_operation *op = pa_context_get_server_info(pulse_context, cb, userdata);
  180. if (!op) {
  181. pulse_unlock();
  182. return -1;
  183. }
  184. while (pa_operation_get_state(op) == PA_OPERATION_RUNNING)
  185. pulse_wait();
  186. pa_operation_unref(op);
  187. pulse_unlock();
  188. return 0;
  189. }
  190. pa_stream *pulse_stream_new(const char *name, const pa_sample_spec *ss, const pa_channel_map *map)
  191. {
  192. if (pulse_context_ready() < 0)
  193. return NULL;
  194. pulse_lock();
  195. pa_proplist *p = pulse_properties();
  196. pa_stream *s = pa_stream_new_with_proplist(pulse_context, name, ss, map, p);
  197. pa_proplist_free(p);
  198. pulse_unlock();
  199. return s;
  200. }