4 /* $Id: context.h 1426 2007-02-13 15:35:19Z ossman $ */
7 This file is part of PulseAudio.
9 Copyright 2004-2006 Lennart Poettering
10 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
12 PulseAudio is free software; you can redistribute it and/or modify
13 it under the terms of the GNU Lesser General Public License as published
14 by the Free Software Foundation; either version 2 of the License,
15 or (at your option) any later version.
17 PulseAudio is distributed in the hope that it will be useful, but
18 WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 General Public License for more details.
22 You should have received a copy of the GNU Lesser General Public License
23 along with PulseAudio; if not, write to the Free Software
24 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
28 #include <pulse/sample.h>
29 #include <pulse/def.h>
30 #include <pulse/mainloop-api.h>
31 #include <pulse/cdecl.h>
32 #include <pulse/operation.h>
34 /** \page async Asynchronous API
36 * \section overv_sec Overview
38 * The asynchronous API is the native interface to the PulseAudio library.
39 * It allows full access to all available functions. This also means that
40 * it is rather complex and can take some time to fully master.
42 * \section mainloop_sec Main Loop Abstraction
44 * The API is based around an asynchronous event loop, or main loop,
45 * abstraction. This abstraction contains three basic elements:
47 * \li Deferred events - Events that will trigger as soon as possible. Note
48 * that some implementations may block all other events
49 * when a deferred event is active.
50 * \li I/O events - Events that trigger on file descriptor activities.
51 * \li Times events - Events that trigger after a fixed ammount of time.
53 * The abstraction is represented as a number of function pointers in the
54 * pa_mainloop_api structure.
56 * To actually be able to use these functions, an implementation needs to
57 * be coupled to the abstraction. There are three of these shipped with
58 * PulseAudio, but any other can be used with a minimal ammount of work,
59 * provided it supports the three basic events listed above.
61 * The implementations shipped with PulseAudio are:
63 * \li \subpage mainloop - A minimal but fast implementation based on poll().
64 * \li \subpage threaded_mainloop - A special version of the previous
65 * implementation where all of PulseAudio's
66 * internal handling runs in a separate
68 * \li \subpage glib-mainloop - A wrapper around GLIB's main loop. Available
69 * for both GLIB 1.2 and GLIB 2.x.
71 * UNIX signals may be hooked to a main loop using the functions from
72 * \ref mainloop-signal.h. These rely only on the main loop abstraction
73 * and can therefore be used with any of the implementations.
75 * \section refcnt_sec Reference Counting
77 * Almost all objects in PulseAudio are reference counted. What that means
78 * is that you rarely malloc() or free() any objects. Instead you increase
79 * and decrease their reference counts. Whenever an object's reference
80 * count reaches zero, that object gets destroy and any resources it uses
83 * The benefit of this design is that an application need not worry about
84 * whether or not it needs to keep an object around in case the library is
85 * using it internally. If it is, then it has made sure it has its own
88 * Whenever the library creates an object, it will have an initial
89 * reference count of one. Most of the time, this single reference will be
90 * sufficient for the application, so all required reference count
91 * interaction will be a single call to the objects unref function.
93 * \section context_sec Context
95 * A context is the basic object for a connection to a PulseAudio server.
96 * It multiplexes commands, data streams and events through a single
99 * There is no need for more than one context per application, unless
100 * connections to multiple servers are needed.
102 * \subsection ops_subsec Operations
104 * All operations on the context are performed asynchronously. I.e. the
105 * client will not wait for the server to complete the request. To keep
106 * track of all these in-flight operations, the application is given a
107 * pa_operation object for each asynchronous operation.
109 * There are only two actions (besides reference counting) that can be
110 * performed on a pa_operation: querying its state with
111 * pa_operation_get_state() and aborting it with pa_operation_cancel().
113 * A pa_operation object is reference counted, so an application must
114 * make sure to unreference it, even if it has no intention of using it.
116 * \subsection conn_subsec Connecting
118 * A context must be connected to a server before any operation can be
119 * issued. Calling pa_context_connect() will initiate the connection
120 * procedure. Unlike most asynchronous operations, connecting does not
121 * result in a pa_operation object. Instead, the application should
122 * register a callback using pa_context_set_state_callback().
124 * \subsection disc_subsec Disconnecting
126 * When the sound support is no longer needed, the connection needs to be
127 * closed using pa_context_disconnect(). This is an immediate function that
128 * works synchronously.
130 * Since the context object has references to other objects it must be
131 * disconnected after use or there is a high risk of memory leaks. If the
132 * connection has terminated by itself, then there is no need to explicitly
133 * disconnect the context using pa_context_disconnect().
137 * The sound server's functionality can be divided into a number of
140 * \li \subpage streams
141 * \li \subpage scache
142 * \li \subpage introspect
143 * \li \subpage subscribe
147 * Connection contexts for asynchrononous communication with a
148 * server. A pa_context object wraps a connection to a PulseAudio
149 * server using its native protocol. */
152 * A playback and recording tool using the asynchronous API */
154 /** \example paplay.c
155 * A sound file playback tool using the asynchronous API, based on libsndfile */
159 /** An opaque connection context to a daemon */
160 typedef struct pa_context pa_context;
162 /** Generic notification callback prototype */
163 typedef void (*pa_context_notify_cb_t)(pa_context *c, void *userdata);
165 /** A generic callback for operation completion */
166 typedef void (*pa_context_success_cb_t) (pa_context *c, int success, void *userdata);
168 /** Instantiate a new connection context with an abstract mainloop API
169 * and an application name */
170 pa_context *pa_context_new(pa_mainloop_api *mainloop, const char *name);
172 /** Decrease the reference counter of the context by one */
173 void pa_context_unref(pa_context *c);
175 /** Increase the reference counter of the context by one */
176 pa_context* pa_context_ref(pa_context *c);
178 /** Set a callback function that is called whenever the context status changes */
179 void pa_context_set_state_callback(pa_context *c, pa_context_notify_cb_t cb, void *userdata);
181 /** Return the error number of the last failed operation */
182 int pa_context_errno(pa_context *c);
184 /** Return non-zero if some data is pending to be written to the connection */
185 int pa_context_is_pending(pa_context *c);
187 /** Return the current context status */
188 pa_context_state_t pa_context_get_state(pa_context *c);
190 /** Connect the context to the specified server. If server is NULL,
191 connect to the default server. This routine may but will not always
192 return synchronously on error. Use pa_context_set_state_callback() to
193 be notified when the connection is established. If flags doesn't have
194 PA_NOAUTOSPAWN set and no specific server is specified or accessible a
195 new daemon is spawned. If api is non-NULL, the functions specified in
196 the structure are used when forking a new child process. */
197 int pa_context_connect(pa_context *c, const char *server, pa_context_flags_t flags, const pa_spawn_api *api);
199 /** Terminate the context connection immediately */
200 void pa_context_disconnect(pa_context *c);
202 /** Drain the context. If there is nothing to drain, the function returns NULL */
203 pa_operation* pa_context_drain(pa_context *c, pa_context_notify_cb_t cb, void *userdata);
205 /** Tell the daemon to exit. The returned operation is unlikely to
206 * complete succesfully, since the daemon probably died before
207 * returning a success notification */
208 pa_operation* pa_context_exit_daemon(pa_context *c, pa_context_success_cb_t cb, void *userdata);
210 /** Set the name of the default sink. \since 0.4 */
211 pa_operation* pa_context_set_default_sink(pa_context *c, const char *name, pa_context_success_cb_t cb, void *userdata);
213 /** Set the name of the default source. \since 0.4 */
214 pa_operation* pa_context_set_default_source(pa_context *c, const char *name, pa_context_success_cb_t cb, void *userdata);
216 /** Returns 1 when the connection is to a local daemon. Returns negative when no connection has been made yet. \since 0.5 */
217 int pa_context_is_local(pa_context *c);
219 /** Set a different application name for context on the server. \since 0.5 */
220 pa_operation* pa_context_set_name(pa_context *c, const char *name, pa_context_success_cb_t cb, void *userdata);
222 /** Return the server name this context is connected to. \since 0.7 */
223 const char* pa_context_get_server(pa_context *c);
225 /** Return the protocol version of the library. \since 0.8 */
226 uint32_t pa_context_get_protocol_version(pa_context *c);
228 /** Return the protocol version of the connected server. \since 0.8 */
229 uint32_t pa_context_get_server_protocol_version(pa_context *c);