pulseaudio-wrapper.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 <inttypes.h>
  16. #include <pulse/stream.h>
  17. #include <pulse/context.h>
  18. #include <pulse/introspect.h>
  19. #pragma once
  20. struct pulseaudio_default_output {
  21. char *default_sink_name;
  22. };
  23. struct enum_cb {
  24. obs_enum_audio_device_cb cb;
  25. void *data;
  26. int cont;
  27. };
  28. void get_default_id(char **id);
  29. bool devices_match(const char *id1, const char *id2);
  30. /**
  31. * Initialize the pulseaudio mainloop and increase the reference count
  32. */
  33. int_fast32_t pulseaudio_init();
  34. /**
  35. * Unreference the pulseaudio mainloop, when the reference count reaches
  36. * zero the mainloop will automatically be destroyed
  37. */
  38. void pulseaudio_unref();
  39. /**
  40. * Lock the mainloop
  41. *
  42. * In order to allow for multiple threads to use the same mainloop pulseaudio
  43. * provides it's own locking mechanism. This function should be called before
  44. * using any pulseaudio function that is in any way related to the mainloop or
  45. * context.
  46. *
  47. * @note use of this function may cause deadlocks
  48. *
  49. * @warning do not use with pulseaudio_ wrapper functions
  50. */
  51. void pulseaudio_lock();
  52. /**
  53. * Unlock the mainloop
  54. *
  55. * @see pulseaudio_lock()
  56. */
  57. void pulseaudio_unlock();
  58. /**
  59. * Wait for events to happen
  60. *
  61. * This function should be called when waiting for an event to happen.
  62. */
  63. void pulseaudio_wait();
  64. /**
  65. * Wait for accept signal from calling thread
  66. *
  67. * This function tells the pulseaudio mainloop whether the data provided to
  68. * the callback should be retained until the calling thread executes
  69. * pulseaudio_accept()
  70. *
  71. * If wait_for_accept is 0 the function returns and the data is freed.
  72. */
  73. void pulseaudio_signal(int wait_for_accept);
  74. /**
  75. * Signal the waiting callback to return
  76. *
  77. * This function is used in conjunction with pulseaudio_signal()
  78. */
  79. void pulseaudio_accept();
  80. /**
  81. * Request source information
  82. *
  83. * The function will block until the operation was executed and the mainloop
  84. * called the provided callback function.
  85. *
  86. * @return negative on error
  87. *
  88. * @note The function will block until the server context is ready.
  89. *
  90. * @warning call without active locks
  91. */
  92. int_fast32_t pulseaudio_get_source_info_list(pa_source_info_cb_t cb, void *userdata);
  93. /**
  94. * Request source information from a specific source
  95. *
  96. * The function will block until the operation was executed and the mainloop
  97. * called the provided callback function.
  98. *
  99. * @param cb pointer to the callback function
  100. * @param name the source name to get information for
  101. * @param userdata pointer to userdata the callback will be called with
  102. *
  103. * @return negative on error
  104. *
  105. * @note The function will block until the server context is ready.
  106. *
  107. * @warning call without active locks
  108. */
  109. int_fast32_t pulseaudio_get_source_info(pa_source_info_cb_t cb, const char *name, void *userdata);
  110. /**
  111. * Request sink information
  112. *
  113. * The function will block until the operation was executed and the mainloop
  114. * called the provided callback function.
  115. *
  116. * @return negative on error
  117. *
  118. * @note The function will block until the server context is ready.
  119. *
  120. * @warning call without active locks
  121. */
  122. int_fast32_t pulseaudio_get_sink_info_list(pa_sink_info_cb_t cb, void *userdata);
  123. /**
  124. * Request sink information from a specific sink
  125. *
  126. * The function will block until the operation was executed and the mainloop
  127. * called the provided callback function.
  128. *
  129. * @param cb pointer to the callback function
  130. * @param name the sink name to get information for
  131. * @param userdata pointer to userdata the callback will be called with
  132. *
  133. * @return negative on error
  134. *
  135. * @note The function will block until the server context is ready.
  136. *
  137. * @warning call without active locks
  138. */
  139. int_fast32_t pulseaudio_get_sink_info(pa_sink_info_cb_t cb, const char *name, void *userdata);
  140. /**
  141. * Request server information
  142. *
  143. * The function will block until the operation was executed and the mainloop
  144. * called the provided callback function.
  145. *
  146. * @return negative on error
  147. *
  148. * @note The function will block until the server context is ready.
  149. *
  150. * @warning call without active locks
  151. */
  152. int_fast32_t pulseaudio_get_server_info(pa_server_info_cb_t cb, void *userdata);
  153. /**
  154. * Create a new stream with the default properties
  155. *
  156. * @note The function will block until the server context is ready.
  157. *
  158. * @warning call without active locks
  159. */
  160. pa_stream *pulseaudio_stream_new(const char *name, const pa_sample_spec *ss, const pa_channel_map *map);
  161. /**
  162. * Connect to a pulseaudio playback stream
  163. *
  164. * @param s pa_stream to connect to. NULL for default
  165. * @param attr pa_buffer_attr
  166. * @param name Device name. NULL for default device
  167. * @param flags pa_stream_flags_t
  168. * @return negative on error
  169. */
  170. int_fast32_t pulseaudio_connect_playback(pa_stream *s, const char *name, const pa_buffer_attr *attr,
  171. pa_stream_flags_t flags);
  172. /**
  173. * Sets a callback function for when data can be written to the stream
  174. *
  175. * @param p pa_stream to connect to. NULL for default
  176. * @param cb pa_stream_request_cb_t
  177. * @param userdata pointer to userdata the callback will be called with
  178. */
  179. void pulseaudio_write_callback(pa_stream *p, pa_stream_request_cb_t cb, void *userdata);
  180. /**
  181. * Sets a callback function for when an underflow happen
  182. *
  183. * @param p pa_stream to connect to. NULL for default
  184. * @param cb pa_stream_notify_cb_t
  185. * @param userdata pointer to userdata the callback will be called with
  186. */
  187. void pulseaudio_set_underflow_callback(pa_stream *p, pa_stream_notify_cb_t cb, void *userdata);