153a5a1b3Sopenharmony_ci#ifndef foocontexthfoo 253a5a1b3Sopenharmony_ci#define foocontexthfoo 353a5a1b3Sopenharmony_ci 453a5a1b3Sopenharmony_ci/*** 553a5a1b3Sopenharmony_ci This file is part of PulseAudio. 653a5a1b3Sopenharmony_ci 753a5a1b3Sopenharmony_ci Copyright 2004-2006 Lennart Poettering 853a5a1b3Sopenharmony_ci Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB 953a5a1b3Sopenharmony_ci 1053a5a1b3Sopenharmony_ci PulseAudio is free software; you can redistribute it and/or modify 1153a5a1b3Sopenharmony_ci it under the terms of the GNU Lesser General Public License as published 1253a5a1b3Sopenharmony_ci by the Free Software Foundation; either version 2.1 of the License, 1353a5a1b3Sopenharmony_ci or (at your option) any later version. 1453a5a1b3Sopenharmony_ci 1553a5a1b3Sopenharmony_ci PulseAudio is distributed in the hope that it will be useful, but 1653a5a1b3Sopenharmony_ci WITHOUT ANY WARRANTY; without even the implied warranty of 1753a5a1b3Sopenharmony_ci MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 1853a5a1b3Sopenharmony_ci General Public License for more details. 1953a5a1b3Sopenharmony_ci 2053a5a1b3Sopenharmony_ci You should have received a copy of the GNU Lesser General Public License 2153a5a1b3Sopenharmony_ci along with PulseAudio; if not, see <http://www.gnu.org/licenses/>. 2253a5a1b3Sopenharmony_ci***/ 2353a5a1b3Sopenharmony_ci 2453a5a1b3Sopenharmony_ci#include <pulse/sample.h> 2553a5a1b3Sopenharmony_ci#include <pulse/def.h> 2653a5a1b3Sopenharmony_ci#include <pulse/mainloop-api.h> 2753a5a1b3Sopenharmony_ci#include <pulse/cdecl.h> 2853a5a1b3Sopenharmony_ci#include <pulse/operation.h> 2953a5a1b3Sopenharmony_ci#include <pulse/proplist.h> 3053a5a1b3Sopenharmony_ci#include <pulse/version.h> 3153a5a1b3Sopenharmony_ci 3253a5a1b3Sopenharmony_ci/** \page async Asynchronous API 3353a5a1b3Sopenharmony_ci * 3453a5a1b3Sopenharmony_ci * \section overv_sec Overview 3553a5a1b3Sopenharmony_ci * 3653a5a1b3Sopenharmony_ci * The asynchronous API is the native interface to the PulseAudio library. 3753a5a1b3Sopenharmony_ci * It allows full access to all available functionality. This however means that 3853a5a1b3Sopenharmony_ci * it is rather complex and can take some time to fully master. 3953a5a1b3Sopenharmony_ci * 4053a5a1b3Sopenharmony_ci * \section mainloop_sec Main Loop Abstraction 4153a5a1b3Sopenharmony_ci * 4253a5a1b3Sopenharmony_ci * The API is based around an asynchronous event loop, or main loop, 4353a5a1b3Sopenharmony_ci * abstraction. This abstraction contains three basic elements: 4453a5a1b3Sopenharmony_ci * 4553a5a1b3Sopenharmony_ci * \li Deferred events - Events that will trigger as soon as possible. Note 4653a5a1b3Sopenharmony_ci * that some implementations may block all other events 4753a5a1b3Sopenharmony_ci * when a deferred event is active. 4853a5a1b3Sopenharmony_ci * \li I/O events - Events that trigger on file descriptor activities. 4953a5a1b3Sopenharmony_ci * \li Timer events - Events that trigger after a fixed amount of time. 5053a5a1b3Sopenharmony_ci * 5153a5a1b3Sopenharmony_ci * The abstraction is represented as a number of function pointers in the 5253a5a1b3Sopenharmony_ci * pa_mainloop_api structure. 5353a5a1b3Sopenharmony_ci * 5453a5a1b3Sopenharmony_ci * To actually be able to use these functions, an implementation needs to 5553a5a1b3Sopenharmony_ci * be coupled to the abstraction. There are three of these shipped with 5653a5a1b3Sopenharmony_ci * PulseAudio, but any other can be used with a minimal amount of work, 5753a5a1b3Sopenharmony_ci * provided it supports the three basic events listed above. 5853a5a1b3Sopenharmony_ci * 5953a5a1b3Sopenharmony_ci * The implementations shipped with PulseAudio are: 6053a5a1b3Sopenharmony_ci * 6153a5a1b3Sopenharmony_ci * \li \subpage mainloop - A minimal but fast implementation based on poll(). 6253a5a1b3Sopenharmony_ci * \li \subpage threaded_mainloop - A special version of the previous 6353a5a1b3Sopenharmony_ci * implementation where all of PulseAudio's 6453a5a1b3Sopenharmony_ci * internal handling runs in a separate 6553a5a1b3Sopenharmony_ci * thread. 6653a5a1b3Sopenharmony_ci * \li \subpage glib-mainloop - A wrapper around GLib's main loop. 6753a5a1b3Sopenharmony_ci * 6853a5a1b3Sopenharmony_ci * UNIX signals may be hooked to a main loop using the functions from 6953a5a1b3Sopenharmony_ci * \ref mainloop-signal.h. These rely only on the main loop abstraction 7053a5a1b3Sopenharmony_ci * and can therefore be used with any of the implementations. 7153a5a1b3Sopenharmony_ci * 7253a5a1b3Sopenharmony_ci * \section refcnt_sec Reference Counting 7353a5a1b3Sopenharmony_ci * 7453a5a1b3Sopenharmony_ci * Almost all objects in PulseAudio are reference counted. What that means 7553a5a1b3Sopenharmony_ci * is that you rarely malloc() or free() any objects. Instead you increase 7653a5a1b3Sopenharmony_ci * and decrease their reference counts. Whenever an object's reference 7753a5a1b3Sopenharmony_ci * count reaches zero, that object gets destroy and any resources it uses 7853a5a1b3Sopenharmony_ci * get freed. 7953a5a1b3Sopenharmony_ci * 8053a5a1b3Sopenharmony_ci * The benefit of this design is that an application need not worry about 8153a5a1b3Sopenharmony_ci * whether or not it needs to keep an object around in case the library is 8253a5a1b3Sopenharmony_ci * using it internally. If it is, then it has made sure it has its own 8353a5a1b3Sopenharmony_ci * reference to it. 8453a5a1b3Sopenharmony_ci * 8553a5a1b3Sopenharmony_ci * Whenever the library creates an object, it will have an initial 8653a5a1b3Sopenharmony_ci * reference count of one. Most of the time, this single reference will be 8753a5a1b3Sopenharmony_ci * sufficient for the application, so all required reference count 8853a5a1b3Sopenharmony_ci * interaction will be a single call to the object's unref function. 8953a5a1b3Sopenharmony_ci * 9053a5a1b3Sopenharmony_ci * \section context_sec Context 9153a5a1b3Sopenharmony_ci * 9253a5a1b3Sopenharmony_ci * A context is the basic object for a connection to a PulseAudio server. 9353a5a1b3Sopenharmony_ci * It multiplexes commands, data streams and events through a single 9453a5a1b3Sopenharmony_ci * channel. 9553a5a1b3Sopenharmony_ci * 9653a5a1b3Sopenharmony_ci * There is no need for more than one context per application, unless 9753a5a1b3Sopenharmony_ci * connections to multiple servers are needed. 9853a5a1b3Sopenharmony_ci * 9953a5a1b3Sopenharmony_ci * \subsection ops_subsec Operations 10053a5a1b3Sopenharmony_ci * 10153a5a1b3Sopenharmony_ci * All operations on the context are performed asynchronously. I.e. the 10253a5a1b3Sopenharmony_ci * client will not wait for the server to complete the request. To keep 10353a5a1b3Sopenharmony_ci * track of all these in-flight operations, the application is given a 10453a5a1b3Sopenharmony_ci * pa_operation object for each asynchronous operation. 10553a5a1b3Sopenharmony_ci * 10653a5a1b3Sopenharmony_ci * There are only two actions (besides reference counting) that can be 10753a5a1b3Sopenharmony_ci * performed on a pa_operation: querying its state with 10853a5a1b3Sopenharmony_ci * pa_operation_get_state() and aborting it with pa_operation_cancel(). 10953a5a1b3Sopenharmony_ci * 11053a5a1b3Sopenharmony_ci * A pa_operation object is reference counted, so an application must 11153a5a1b3Sopenharmony_ci * make sure to unreference it, even if it has no intention of using it. 11253a5a1b3Sopenharmony_ci * 11353a5a1b3Sopenharmony_ci * \subsection conn_subsec Connecting 11453a5a1b3Sopenharmony_ci * 11553a5a1b3Sopenharmony_ci * A context must be connected to a server before any operation can be 11653a5a1b3Sopenharmony_ci * issued. Calling pa_context_connect() will initiate the connection 11753a5a1b3Sopenharmony_ci * procedure. Unlike most asynchronous operations, connecting does not 11853a5a1b3Sopenharmony_ci * result in a pa_operation object. Instead, the application should 11953a5a1b3Sopenharmony_ci * register a callback using pa_context_set_state_callback(). 12053a5a1b3Sopenharmony_ci * 12153a5a1b3Sopenharmony_ci * \subsection disc_subsec Disconnecting 12253a5a1b3Sopenharmony_ci * 12353a5a1b3Sopenharmony_ci * When the sound support is no longer needed, the connection needs to be 12453a5a1b3Sopenharmony_ci * closed using pa_context_disconnect(). This is an immediate function that 12553a5a1b3Sopenharmony_ci * works synchronously. 12653a5a1b3Sopenharmony_ci * 12753a5a1b3Sopenharmony_ci * Since the context object has references to other objects it must be 12853a5a1b3Sopenharmony_ci * disconnected after use or there is a high risk of memory leaks. If the 12953a5a1b3Sopenharmony_ci * connection has terminated by itself, then there is no need to explicitly 13053a5a1b3Sopenharmony_ci * disconnect the context using pa_context_disconnect(). 13153a5a1b3Sopenharmony_ci * 13253a5a1b3Sopenharmony_ci * \section Functions 13353a5a1b3Sopenharmony_ci * 13453a5a1b3Sopenharmony_ci * The sound server's functionality can be divided into a number of 13553a5a1b3Sopenharmony_ci * subsections: 13653a5a1b3Sopenharmony_ci * 13753a5a1b3Sopenharmony_ci * \li \subpage streams 13853a5a1b3Sopenharmony_ci * \li \subpage scache 13953a5a1b3Sopenharmony_ci * \li \subpage introspect 14053a5a1b3Sopenharmony_ci * \li \subpage subscribe 14153a5a1b3Sopenharmony_ci */ 14253a5a1b3Sopenharmony_ci 14353a5a1b3Sopenharmony_ci/** \file 14453a5a1b3Sopenharmony_ci * Connection contexts for asynchronous communication with a 14553a5a1b3Sopenharmony_ci * server. A pa_context object wraps a connection to a PulseAudio 14653a5a1b3Sopenharmony_ci * server using its native protocol. 14753a5a1b3Sopenharmony_ci * 14853a5a1b3Sopenharmony_ci * See also \subpage async 14953a5a1b3Sopenharmony_ci */ 15053a5a1b3Sopenharmony_ci 15153a5a1b3Sopenharmony_ciPA_C_DECL_BEGIN 15253a5a1b3Sopenharmony_ci 15353a5a1b3Sopenharmony_ci/** An opaque connection context to a daemon */ 15453a5a1b3Sopenharmony_citypedef struct pa_context pa_context; 15553a5a1b3Sopenharmony_ci 15653a5a1b3Sopenharmony_ci/** Generic notification callback prototype */ 15753a5a1b3Sopenharmony_citypedef void (*pa_context_notify_cb_t)(pa_context *c, void *userdata); 15853a5a1b3Sopenharmony_ci 15953a5a1b3Sopenharmony_ci/** A generic callback for operation completion */ 16053a5a1b3Sopenharmony_citypedef void (*pa_context_success_cb_t) (pa_context *c, int success, void *userdata); 16153a5a1b3Sopenharmony_ci 16253a5a1b3Sopenharmony_ci/** A callback for asynchronous meta/policy event messages. The set 16353a5a1b3Sopenharmony_ci * of defined events can be extended at any time. Also, server modules 16453a5a1b3Sopenharmony_ci * may introduce additional message types so make sure that your 16553a5a1b3Sopenharmony_ci * callback function ignores messages it doesn't know. \since 16653a5a1b3Sopenharmony_ci * 0.9.15 */ 16753a5a1b3Sopenharmony_citypedef void (*pa_context_event_cb_t)(pa_context *c, const char *name, pa_proplist *p, void *userdata); 16853a5a1b3Sopenharmony_ci 16953a5a1b3Sopenharmony_ci/** Instantiate a new connection context with an abstract mainloop API 17053a5a1b3Sopenharmony_ci * and an application name. It is recommended to use pa_context_new_with_proplist() 17153a5a1b3Sopenharmony_ci * instead and specify some initial properties.*/ 17253a5a1b3Sopenharmony_cipa_context *pa_context_new(pa_mainloop_api *mainloop, const char *name); 17353a5a1b3Sopenharmony_ci 17453a5a1b3Sopenharmony_ci/** Instantiate a new connection context with an abstract mainloop API 17553a5a1b3Sopenharmony_ci * and an application name, and specify the initial client property 17653a5a1b3Sopenharmony_ci * list. \since 0.9.11 */ 17753a5a1b3Sopenharmony_cipa_context *pa_context_new_with_proplist(pa_mainloop_api *mainloop, const char *name, const pa_proplist *proplist); 17853a5a1b3Sopenharmony_ci 17953a5a1b3Sopenharmony_ci/** Decrease the reference counter of the context by one */ 18053a5a1b3Sopenharmony_civoid pa_context_unref(pa_context *c); 18153a5a1b3Sopenharmony_ci 18253a5a1b3Sopenharmony_ci/** Increase the reference counter of the context by one */ 18353a5a1b3Sopenharmony_cipa_context* pa_context_ref(pa_context *c); 18453a5a1b3Sopenharmony_ci 18553a5a1b3Sopenharmony_ci/** Set a callback function that is called whenever the context status changes */ 18653a5a1b3Sopenharmony_civoid pa_context_set_state_callback(pa_context *c, pa_context_notify_cb_t cb, void *userdata); 18753a5a1b3Sopenharmony_ci 18853a5a1b3Sopenharmony_ci/** Set a callback function that is called whenever a meta/policy 18953a5a1b3Sopenharmony_ci * control event is received. \since 0.9.15 */ 19053a5a1b3Sopenharmony_civoid pa_context_set_event_callback(pa_context *p, pa_context_event_cb_t cb, void *userdata); 19153a5a1b3Sopenharmony_ci 19253a5a1b3Sopenharmony_ci/** Return the error number of the last failed operation */ 19353a5a1b3Sopenharmony_ciint pa_context_errno(const pa_context *c); 19453a5a1b3Sopenharmony_ci 19553a5a1b3Sopenharmony_ci/** Return non-zero if some data is pending to be written to the connection */ 19653a5a1b3Sopenharmony_ciint pa_context_is_pending(const pa_context *c); 19753a5a1b3Sopenharmony_ci 19853a5a1b3Sopenharmony_ci/** Return the current context status */ 19953a5a1b3Sopenharmony_cipa_context_state_t pa_context_get_state(const pa_context *c); 20053a5a1b3Sopenharmony_ci 20153a5a1b3Sopenharmony_ci/** Connect the context to the specified server. If server is NULL, 20253a5a1b3Sopenharmony_ci * connect to the default server. This routine may but will not always 20353a5a1b3Sopenharmony_ci * return synchronously on error. Use pa_context_set_state_callback() to 20453a5a1b3Sopenharmony_ci * be notified when the connection is established. If flags doesn't have 20553a5a1b3Sopenharmony_ci * PA_CONTEXT_NOAUTOSPAWN set and no specific server is specified or 20653a5a1b3Sopenharmony_ci * accessible a new daemon is spawned. If api is non-NULL, the functions 20753a5a1b3Sopenharmony_ci * specified in the structure are used when forking a new child 20853a5a1b3Sopenharmony_ci * process. Returns negative on certain errors such as invalid state 20953a5a1b3Sopenharmony_ci * or parameters. */ 21053a5a1b3Sopenharmony_ciint pa_context_connect(pa_context *c, const char *server, pa_context_flags_t flags, const pa_spawn_api *api); 21153a5a1b3Sopenharmony_ci 21253a5a1b3Sopenharmony_ci/** Terminate the context connection immediately */ 21353a5a1b3Sopenharmony_civoid pa_context_disconnect(pa_context *c); 21453a5a1b3Sopenharmony_ci 21553a5a1b3Sopenharmony_ci/** Drain the context. If there is nothing to drain, the function returns NULL */ 21653a5a1b3Sopenharmony_cipa_operation* pa_context_drain(pa_context *c, pa_context_notify_cb_t cb, void *userdata); 21753a5a1b3Sopenharmony_ci 21853a5a1b3Sopenharmony_ci/** Tell the daemon to exit. The returned operation is unlikely to 21953a5a1b3Sopenharmony_ci * complete successfully, since the daemon probably died before 22053a5a1b3Sopenharmony_ci * returning a success notification */ 22153a5a1b3Sopenharmony_cipa_operation* pa_context_exit_daemon(pa_context *c, pa_context_success_cb_t cb, void *userdata); 22253a5a1b3Sopenharmony_ci 22353a5a1b3Sopenharmony_ci/** Set the name of the default sink. */ 22453a5a1b3Sopenharmony_cipa_operation* pa_context_set_default_sink(pa_context *c, const char *name, pa_context_success_cb_t cb, void *userdata); 22553a5a1b3Sopenharmony_ci 22653a5a1b3Sopenharmony_ci/** Set the name of the default source. */ 22753a5a1b3Sopenharmony_cipa_operation* pa_context_set_default_source(pa_context *c, const char *name, pa_context_success_cb_t cb, void *userdata); 22853a5a1b3Sopenharmony_ci 22953a5a1b3Sopenharmony_ci/** Returns 1 when the connection is to a local daemon. Returns negative when no connection has been made yet. */ 23053a5a1b3Sopenharmony_ciint pa_context_is_local(const pa_context *c); 23153a5a1b3Sopenharmony_ci 23253a5a1b3Sopenharmony_ci/** Set a different application name for context on the server. */ 23353a5a1b3Sopenharmony_cipa_operation* pa_context_set_name(pa_context *c, const char *name, pa_context_success_cb_t cb, void *userdata); 23453a5a1b3Sopenharmony_ci 23553a5a1b3Sopenharmony_ci/** Return the server name this context is connected to. */ 23653a5a1b3Sopenharmony_ciconst char* pa_context_get_server(const pa_context *c); 23753a5a1b3Sopenharmony_ci 23853a5a1b3Sopenharmony_ci/** Return the protocol version of the library. */ 23953a5a1b3Sopenharmony_ciuint32_t pa_context_get_protocol_version(const pa_context *c); 24053a5a1b3Sopenharmony_ci 24153a5a1b3Sopenharmony_ci/** Return the protocol version of the connected server. 24253a5a1b3Sopenharmony_ci * Returns PA_INVALID_INDEX on error. */ 24353a5a1b3Sopenharmony_ciuint32_t pa_context_get_server_protocol_version(const pa_context *c); 24453a5a1b3Sopenharmony_ci 24553a5a1b3Sopenharmony_ci/** Update the property list of the client, adding new entries. Please 24653a5a1b3Sopenharmony_ci * note that it is highly recommended to set as many properties 24753a5a1b3Sopenharmony_ci * initially via pa_context_new_with_proplist() as possible instead a 24853a5a1b3Sopenharmony_ci * posteriori with this function, since that information may then be 24953a5a1b3Sopenharmony_ci * used to route streams of the client to the right device. \since 0.9.11 */ 25053a5a1b3Sopenharmony_cipa_operation *pa_context_proplist_update(pa_context *c, pa_update_mode_t mode, const pa_proplist *p, pa_context_success_cb_t cb, void *userdata); 25153a5a1b3Sopenharmony_ci 25253a5a1b3Sopenharmony_ci/** Update the property list of the client, remove entries. \since 0.9.11 */ 25353a5a1b3Sopenharmony_cipa_operation *pa_context_proplist_remove(pa_context *c, const char *const keys[], pa_context_success_cb_t cb, void *userdata); 25453a5a1b3Sopenharmony_ci 25553a5a1b3Sopenharmony_ci/** Return the client index this context is 25653a5a1b3Sopenharmony_ci * identified in the server with. This is useful for usage with the 25753a5a1b3Sopenharmony_ci * introspection functions, such as pa_context_get_client_info(). 25853a5a1b3Sopenharmony_ci * Returns PA_INVALID_INDEX on error. \since 0.9.11 */ 25953a5a1b3Sopenharmony_ciuint32_t pa_context_get_index(const pa_context *s); 26053a5a1b3Sopenharmony_ci 26153a5a1b3Sopenharmony_ci/** Create a new timer event source for the specified time (wrapper 26253a5a1b3Sopenharmony_ci * for mainloop->time_new). \since 0.9.16 */ 26353a5a1b3Sopenharmony_cipa_time_event* pa_context_rttime_new(const pa_context *c, pa_usec_t usec, pa_time_event_cb_t cb, void *userdata); 26453a5a1b3Sopenharmony_ci 26553a5a1b3Sopenharmony_ci/** Restart a running or expired timer event source (wrapper for 26653a5a1b3Sopenharmony_ci * mainloop->time_restart). \since 0.9.16 */ 26753a5a1b3Sopenharmony_civoid pa_context_rttime_restart(const pa_context *c, pa_time_event *e, pa_usec_t usec); 26853a5a1b3Sopenharmony_ci 26953a5a1b3Sopenharmony_ci/** Return the optimal block size for passing around audio buffers. It 27053a5a1b3Sopenharmony_ci * is recommended to allocate buffers of the size returned here when 27153a5a1b3Sopenharmony_ci * writing audio data to playback streams, if the latency constraints 27253a5a1b3Sopenharmony_ci * permit this. It is not recommended writing larger blocks than this 27353a5a1b3Sopenharmony_ci * because usually they will then be split up internally into chunks 27453a5a1b3Sopenharmony_ci * of this size. It is not recommended writing smaller blocks than 27553a5a1b3Sopenharmony_ci * this (unless required due to latency demands) because this 27653a5a1b3Sopenharmony_ci * increases CPU usage. If ss is NULL you will be returned the 27753a5a1b3Sopenharmony_ci * byte-exact tile size. if ss is invalid, (size_t) -1 will be 27853a5a1b3Sopenharmony_ci * returned. If you pass a valid ss, then the tile size 27953a5a1b3Sopenharmony_ci * will be rounded down to multiple of the frame size. This is 28053a5a1b3Sopenharmony_ci * supposed to be used in a construct such as 28153a5a1b3Sopenharmony_ci * pa_context_get_tile_size(pa_stream_get_context(s), 28253a5a1b3Sopenharmony_ci * pa_stream_get_sample_spec(ss)); \since 0.9.20 */ 28353a5a1b3Sopenharmony_cisize_t pa_context_get_tile_size(const pa_context *c, const pa_sample_spec *ss); 28453a5a1b3Sopenharmony_ci 28553a5a1b3Sopenharmony_ci/** Load the authentication cookie from a file. This function is primarily 28653a5a1b3Sopenharmony_ci * meant for PulseAudio's own tunnel modules, which need to load the cookie 28753a5a1b3Sopenharmony_ci * from a custom location. Applications don't usually need to care about the 28853a5a1b3Sopenharmony_ci * cookie at all, but if it happens that you know what the authentication 28953a5a1b3Sopenharmony_ci * cookie is and your application needs to load it from a non-standard 29053a5a1b3Sopenharmony_ci * location, feel free to use this function. \since 5.0 */ 29153a5a1b3Sopenharmony_ciint pa_context_load_cookie_from_file(pa_context *c, const char *cookie_file_path); 29253a5a1b3Sopenharmony_ci 29353a5a1b3Sopenharmony_ciPA_C_DECL_END 29453a5a1b3Sopenharmony_ci 29553a5a1b3Sopenharmony_ci#endif 296