context.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. #ifndef foocontexthfoo
  2. #define foocontexthfoo
  3. /***
  4. This file is part of PulseAudio.
  5. Copyright 2004-2006 Lennart Poettering
  6. Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
  7. PulseAudio is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU Lesser General Public License as published
  9. by the Free Software Foundation; either version 2.1 of the License,
  10. or (at your option) any later version.
  11. PulseAudio is distributed in the hope that it will be useful, but
  12. WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. General Public License for more details.
  15. You should have received a copy of the GNU Lesser General Public License
  16. along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
  17. ***/
  18. #include <pulse/sample.h>
  19. #include <pulse/def.h>
  20. #include <pulse/mainloop-api.h>
  21. #include <pulse/cdecl.h>
  22. #include <pulse/operation.h>
  23. #include <pulse/proplist.h>
  24. #include <pulse/version.h>
  25. /** \page async Asynchronous API
  26. *
  27. * \section overv_sec Overview
  28. *
  29. * The asynchronous API is the native interface to the PulseAudio library.
  30. * It allows full access to all available functionality. This however means that
  31. * it is rather complex and can take some time to fully master.
  32. *
  33. * \section mainloop_sec Main Loop Abstraction
  34. *
  35. * The API is based around an asynchronous event loop, or main loop,
  36. * abstraction. This abstraction contains three basic elements:
  37. *
  38. * \li Deferred events - Events that will trigger as soon as possible. Note
  39. * that some implementations may block all other events
  40. * when a deferred event is active.
  41. * \li I/O events - Events that trigger on file descriptor activities.
  42. * \li Times events - Events that trigger after a fixed amount of time.
  43. *
  44. * The abstraction is represented as a number of function pointers in the
  45. * pa_mainloop_api structure.
  46. *
  47. * To actually be able to use these functions, an implementation needs to
  48. * be coupled to the abstraction. There are three of these shipped with
  49. * PulseAudio, but any other can be used with a minimal amount of work,
  50. * provided it supports the three basic events listed above.
  51. *
  52. * The implementations shipped with PulseAudio are:
  53. *
  54. * \li \subpage mainloop - A minimal but fast implementation based on poll().
  55. * \li \subpage threaded_mainloop - A special version of the previous
  56. * implementation where all of PulseAudio's
  57. * internal handling runs in a separate
  58. * thread.
  59. * \li \subpage glib-mainloop - A wrapper around GLib's main loop.
  60. *
  61. * UNIX signals may be hooked to a main loop using the functions from
  62. * \ref mainloop-signal.h. These rely only on the main loop abstraction
  63. * and can therefore be used with any of the implementations.
  64. *
  65. * \section refcnt_sec Reference Counting
  66. *
  67. * Almost all objects in PulseAudio are reference counted. What that means
  68. * is that you rarely malloc() or free() any objects. Instead you increase
  69. * and decrease their reference counts. Whenever an object's reference
  70. * count reaches zero, that object gets destroy and any resources it uses
  71. * get freed.
  72. *
  73. * The benefit of this design is that an application need not worry about
  74. * whether or not it needs to keep an object around in case the library is
  75. * using it internally. If it is, then it has made sure it has its own
  76. * reference to it.
  77. *
  78. * Whenever the library creates an object, it will have an initial
  79. * reference count of one. Most of the time, this single reference will be
  80. * sufficient for the application, so all required reference count
  81. * interaction will be a single call to the object's unref function.
  82. *
  83. * \section context_sec Context
  84. *
  85. * A context is the basic object for a connection to a PulseAudio server.
  86. * It multiplexes commands, data streams and events through a single
  87. * channel.
  88. *
  89. * There is no need for more than one context per application, unless
  90. * connections to multiple servers are needed.
  91. *
  92. * \subsection ops_subsec Operations
  93. *
  94. * All operations on the context are performed asynchronously. I.e. the
  95. * client will not wait for the server to complete the request. To keep
  96. * track of all these in-flight operations, the application is given a
  97. * pa_operation object for each asynchronous operation.
  98. *
  99. * There are only two actions (besides reference counting) that can be
  100. * performed on a pa_operation: querying its state with
  101. * pa_operation_get_state() and aborting it with pa_operation_cancel().
  102. *
  103. * A pa_operation object is reference counted, so an application must
  104. * make sure to unreference it, even if it has no intention of using it.
  105. *
  106. * \subsection conn_subsec Connecting
  107. *
  108. * A context must be connected to a server before any operation can be
  109. * issued. Calling pa_context_connect() will initiate the connection
  110. * procedure. Unlike most asynchronous operations, connecting does not
  111. * result in a pa_operation object. Instead, the application should
  112. * register a callback using pa_context_set_state_callback().
  113. *
  114. * \subsection disc_subsec Disconnecting
  115. *
  116. * When the sound support is no longer needed, the connection needs to be
  117. * closed using pa_context_disconnect(). This is an immediate function that
  118. * works synchronously.
  119. *
  120. * Since the context object has references to other objects it must be
  121. * disconnected after use or there is a high risk of memory leaks. If the
  122. * connection has terminated by itself, then there is no need to explicitly
  123. * disconnect the context using pa_context_disconnect().
  124. *
  125. * \section Functions
  126. *
  127. * The sound server's functionality can be divided into a number of
  128. * subsections:
  129. *
  130. * \li \subpage streams
  131. * \li \subpage scache
  132. * \li \subpage introspect
  133. * \li \subpage subscribe
  134. */
  135. /** \file
  136. * Connection contexts for asynchronous communication with a
  137. * server. A pa_context object wraps a connection to a PulseAudio
  138. * server using its native protocol.
  139. *
  140. * See also \subpage async
  141. */
  142. PA_C_DECL_BEGIN
  143. /** An opaque connection context to a daemon */
  144. typedef struct pa_context pa_context;
  145. /** Generic notification callback prototype */
  146. typedef void (*pa_context_notify_cb_t)(pa_context *c, void *userdata);
  147. /** A generic callback for operation completion */
  148. typedef void (*pa_context_success_cb_t) (pa_context *c, int success, void *userdata);
  149. /** A callback for asynchronous meta/policy event messages. The set
  150. * of defined events can be extended at any time. Also, server modules
  151. * may introduce additional message types so make sure that your
  152. * callback function ignores messages it doesn't know. \since
  153. * 0.9.15 */
  154. typedef void (*pa_context_event_cb_t)(pa_context *c, const char *name, pa_proplist *p, void *userdata);
  155. /** Instantiate a new connection context with an abstract mainloop API
  156. * and an application name. It is recommended to use pa_context_new_with_proplist()
  157. * instead and specify some initial properties.*/
  158. pa_context *pa_context_new(pa_mainloop_api *mainloop, const char *name);
  159. /** Instantiate a new connection context with an abstract mainloop API
  160. * and an application name, and specify the initial client property
  161. * list. \since 0.9.11 */
  162. pa_context *pa_context_new_with_proplist(pa_mainloop_api *mainloop, const char *name, pa_proplist *proplist);
  163. /** Decrease the reference counter of the context by one */
  164. void pa_context_unref(pa_context *c);
  165. /** Increase the reference counter of the context by one */
  166. pa_context* pa_context_ref(pa_context *c);
  167. /** Set a callback function that is called whenever the context status changes */
  168. void pa_context_set_state_callback(pa_context *c, pa_context_notify_cb_t cb, void *userdata);
  169. /** Set a callback function that is called whenever a meta/policy
  170. * control event is received. \since 0.9.15 */
  171. void pa_context_set_event_callback(pa_context *p, pa_context_event_cb_t cb, void *userdata);
  172. /** Return the error number of the last failed operation */
  173. int pa_context_errno(pa_context *c);
  174. /** Return non-zero if some data is pending to be written to the connection */
  175. int pa_context_is_pending(pa_context *c);
  176. /** Return the current context status */
  177. pa_context_state_t pa_context_get_state(pa_context *c);
  178. /** Connect the context to the specified server. If server is NULL,
  179. connect to the default server. This routine may but will not always
  180. return synchronously on error. Use pa_context_set_state_callback() to
  181. be notified when the connection is established. If flags doesn't have
  182. PA_CONTEXT_NOAUTOSPAWN set and no specific server is specified or
  183. accessible a new daemon is spawned. If api is non-NULL, the functions
  184. specified in the structure are used when forking a new child
  185. process. */
  186. int pa_context_connect(pa_context *c, const char *server, pa_context_flags_t flags, const pa_spawn_api *api);
  187. /** Terminate the context connection immediately */
  188. void pa_context_disconnect(pa_context *c);
  189. /** Drain the context. If there is nothing to drain, the function returns NULL */
  190. pa_operation* pa_context_drain(pa_context *c, pa_context_notify_cb_t cb, void *userdata);
  191. /** Tell the daemon to exit. The returned operation is unlikely to
  192. * complete successfully, since the daemon probably died before
  193. * returning a success notification */
  194. pa_operation* pa_context_exit_daemon(pa_context *c, pa_context_success_cb_t cb, void *userdata);
  195. /** Set the name of the default sink. */
  196. pa_operation* pa_context_set_default_sink(pa_context *c, const char *name, pa_context_success_cb_t cb, void *userdata);
  197. /** Set the name of the default source. */
  198. pa_operation* pa_context_set_default_source(pa_context *c, const char *name, pa_context_success_cb_t cb, void *userdata);
  199. /** Returns 1 when the connection is to a local daemon. Returns negative when no connection has been made yet. */
  200. int pa_context_is_local(pa_context *c);
  201. /** Set a different application name for context on the server. */
  202. pa_operation* pa_context_set_name(pa_context *c, const char *name, pa_context_success_cb_t cb, void *userdata);
  203. /** Return the server name this context is connected to. */
  204. const char* pa_context_get_server(pa_context *c);
  205. /** Return the protocol version of the library. */
  206. uint32_t pa_context_get_protocol_version(pa_context *c);
  207. /** Return the protocol version of the connected server. */
  208. uint32_t pa_context_get_server_protocol_version(pa_context *c);
  209. /** Update the property list of the client, adding new entries. Please
  210. * note that it is highly recommended to set as much properties
  211. * initially via pa_context_new_with_proplist() as possible instead a
  212. * posteriori with this function, since that information may then be
  213. * used to route streams of the client to the right device. \since 0.9.11 */
  214. pa_operation *pa_context_proplist_update(pa_context *c, pa_update_mode_t mode, pa_proplist *p, pa_context_success_cb_t cb, void *userdata);
  215. /** Update the property list of the client, remove entries. \since 0.9.11 */
  216. pa_operation *pa_context_proplist_remove(pa_context *c, const char *const keys[], pa_context_success_cb_t cb, void *userdata);
  217. /** Return the client index this context is
  218. * identified in the server with. This is useful for usage with the
  219. * introspection functions, such as pa_context_get_client_info(). \since 0.9.11 */
  220. uint32_t pa_context_get_index(pa_context *s);
  221. /** Create a new timer event source for the specified time (wrapper
  222. * for mainloop->time_new). \since 0.9.16 */
  223. pa_time_event* pa_context_rttime_new(pa_context *c, pa_usec_t usec, pa_time_event_cb_t cb, void *userdata);
  224. /** Restart a running or expired timer event source (wrapper for
  225. * mainloop->time_restart). \since 0.9.16 */
  226. void pa_context_rttime_restart(pa_context *c, pa_time_event *e, pa_usec_t usec);
  227. /** Return the optimal block size for passing around audio buffers. It
  228. * is recommended to allocate buffers of the size returned here when
  229. * writing audio data to playback streams, if the latency constraints
  230. * permit this. It is not recommended writing larger blocks than this
  231. * because usually they will then be split up internally into chunks
  232. * of this size. It is not recommended writing smaller blocks than
  233. * this (unless required due to latency demands) because this
  234. * increases CPU usage. If ss is NULL you will be returned the
  235. * byte-exact tile size. If you pass a valid ss, then the tile size
  236. * will be rounded down to multiple of the frame size. This is
  237. * supposed to be used in a construct such as
  238. * pa_context_get_tile_size(pa_stream_get_context(s),
  239. * pa_stream_get_sample_spec(ss)); \since 0.9.20 */
  240. size_t pa_context_get_tile_size(pa_context *c, const pa_sample_spec *ss);
  241. /** Load the authentication cookie from a file. This function is primarily
  242. * meant for PulseAudio's own tunnel modules, which need to load the cookie
  243. * from a custom location. Applications don't usually need to care about the
  244. * cookie at all, but if it happens that you know what the authentication
  245. * cookie is and your application needs to load it from a non-standard
  246. * location, feel free to use this function. \since 5.0 */
  247. int pa_context_load_cookie_from_file(pa_context *c, const char *cookie_file_path);
  248. PA_C_DECL_END
  249. #endif