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