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