pulse-wrapper.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. blog(LOG_DEBUG, "pulse: context state changed");
  35. pulse_signal(0);
  36. }
  37. /**
  38. * get the default properties
  39. */
  40. static pa_proplist *pulse_properties()
  41. {
  42. pa_proplist *p = pa_proplist_new();
  43. pa_proplist_sets(p, PA_PROP_APPLICATION_NAME, "OBS Studio");
  44. pa_proplist_sets(p, PA_PROP_APPLICATION_ICON_NAME, "application-exit");
  45. pa_proplist_sets(p, PA_PROP_MEDIA_ROLE, "production");
  46. return p;
  47. }
  48. /**
  49. * Initialize the pulse audio context with properties and callback
  50. */
  51. static void pulse_init_context()
  52. {
  53. pulse_lock();
  54. pa_proplist *p = pulse_properties();
  55. pulse_context = pa_context_new_with_proplist(
  56. pa_threaded_mainloop_get_api(pulse_mainloop), "OBS Studio", p);
  57. pa_context_set_state_callback(pulse_context,
  58. pulse_context_state_changed, NULL);
  59. pa_context_connect(pulse_context, NULL, PA_CONTEXT_NOAUTOSPAWN, NULL);
  60. pa_proplist_free(p);
  61. pulse_unlock();
  62. }
  63. /**
  64. * wait for context to be ready
  65. */
  66. static int_fast32_t pulse_context_ready()
  67. {
  68. pulse_lock();
  69. if (!PA_CONTEXT_IS_GOOD(pa_context_get_state(pulse_context))) {
  70. pulse_unlock();
  71. return -1;
  72. }
  73. while (pa_context_get_state(pulse_context) != PA_CONTEXT_READY)
  74. pulse_wait();
  75. pulse_unlock();
  76. return 0;
  77. }
  78. int_fast32_t pulse_init()
  79. {
  80. pthread_mutex_lock(&pulse_mutex);
  81. if (pulse_refs == 0) {
  82. pulse_mainloop = pa_threaded_mainloop_new();
  83. pa_threaded_mainloop_start(pulse_mainloop);
  84. pulse_init_context();
  85. }
  86. pulse_refs++;
  87. blog(LOG_DEBUG, "pulse: Reference count increased to %"PRIuFAST32,
  88. pulse_refs);
  89. pthread_mutex_unlock(&pulse_mutex);
  90. return 0;
  91. }
  92. void pulse_unref()
  93. {
  94. pthread_mutex_lock(&pulse_mutex);
  95. pulse_refs--;
  96. blog(LOG_DEBUG, "pulse: Reference count decreased to %"PRIuFAST32,
  97. pulse_refs);
  98. if (pulse_refs == 0) {
  99. pulse_lock();
  100. if (pulse_context != NULL) {
  101. pa_context_disconnect(pulse_context);
  102. pa_context_unref(pulse_context);
  103. pulse_context = NULL;
  104. }
  105. pulse_unlock();
  106. if (pulse_mainloop != NULL) {
  107. pa_threaded_mainloop_stop(pulse_mainloop);
  108. pa_threaded_mainloop_free(pulse_mainloop);
  109. pulse_mainloop = NULL;
  110. }
  111. }
  112. pthread_mutex_unlock(&pulse_mutex);
  113. }
  114. void pulse_lock()
  115. {
  116. pa_threaded_mainloop_lock(pulse_mainloop);
  117. }
  118. void pulse_unlock()
  119. {
  120. pa_threaded_mainloop_unlock(pulse_mainloop);
  121. }
  122. void pulse_wait()
  123. {
  124. pa_threaded_mainloop_wait(pulse_mainloop);
  125. }
  126. void pulse_signal(int wait_for_accept)
  127. {
  128. pa_threaded_mainloop_signal(pulse_mainloop, wait_for_accept);
  129. }
  130. void pulse_accept()
  131. {
  132. pa_threaded_mainloop_accept(pulse_mainloop);
  133. }
  134. int_fast32_t pulse_get_source_info_list(pa_source_info_cb_t cb, void* userdata)
  135. {
  136. if (pulse_context_ready() < 0)
  137. return -1;
  138. pulse_lock();
  139. pa_operation *op = pa_context_get_source_info_list(
  140. pulse_context, cb, userdata);
  141. while (pa_operation_get_state(op) == PA_OPERATION_RUNNING)
  142. pulse_wait();
  143. pa_operation_unref(op);
  144. pulse_unlock();
  145. return 0;
  146. }
  147. int_fast32_t pulse_get_server_info(pa_server_info_cb_t cb, void* userdata)
  148. {
  149. if (pulse_context_ready() < 0)
  150. return -1;
  151. pulse_lock();
  152. pa_operation *op = pa_context_get_server_info(
  153. pulse_context, cb, userdata);
  154. while (pa_operation_get_state(op) == PA_OPERATION_RUNNING)
  155. pulse_wait();
  156. pa_operation_unref(op);
  157. pulse_unlock();
  158. return 0;
  159. }
  160. pa_stream* pulse_stream_new(const char* name, const pa_sample_spec* ss,
  161. const pa_channel_map* map)
  162. {
  163. if (pulse_context_ready() < 0)
  164. return NULL;
  165. pulse_lock();
  166. pa_proplist *p = pulse_properties();
  167. pa_stream *s = pa_stream_new_with_proplist(
  168. pulse_context, name, ss, map, p);
  169. pa_proplist_free(p);
  170. pulse_unlock();
  171. return s;
  172. }