pulse-wrapper.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 Studio");
  43. pa_proplist_sets(p, PA_PROP_APPLICATION_ICON_NAME, "application-exit");
  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 Studio", 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 = pa_context_get_source_info_list(
  134. pulse_context, cb, userdata);
  135. while (pa_operation_get_state(op) == PA_OPERATION_RUNNING)
  136. pulse_wait();
  137. pa_operation_unref(op);
  138. pulse_unlock();
  139. return 0;
  140. }
  141. int_fast32_t pulse_get_server_info(pa_server_info_cb_t cb, void* userdata)
  142. {
  143. if (pulse_context_ready() < 0)
  144. return -1;
  145. pulse_lock();
  146. pa_operation *op = pa_context_get_server_info(
  147. pulse_context, cb, userdata);
  148. while (pa_operation_get_state(op) == PA_OPERATION_RUNNING)
  149. pulse_wait();
  150. pa_operation_unref(op);
  151. pulse_unlock();
  152. return 0;
  153. }
  154. pa_stream* pulse_stream_new(const char* name, const pa_sample_spec* ss,
  155. const pa_channel_map* map)
  156. {
  157. if (pulse_context_ready() < 0)
  158. return NULL;
  159. pulse_lock();
  160. pa_proplist *p = pulse_properties();
  161. pa_stream *s = pa_stream_new_with_proplist(
  162. pulse_context, name, ss, map, p);
  163. pa_proplist_free(p);
  164. pulse_unlock();
  165. return s;
  166. }