5 This file is part of PulseAudio.
7 Copyright 2004-2006 Lennart Poettering
8 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
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
12 published by the Free Software Foundation; either version 2.1 of the
13 License, or (at your option) any later version.
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 Lesser General Public License for more details.
20 You should have received a copy of the GNU Lesser General Public
21 License along with PulseAudio; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
30 #include <pulse/cdecl.h>
31 #include <pulse/sample.h>
34 * Global definitions */
38 /** The state of a connection context */
39 typedef enum pa_context_state {
40 PA_CONTEXT_UNCONNECTED, /**< The context hasn't been connected yet */
41 PA_CONTEXT_CONNECTING, /**< A connection is being established */
42 PA_CONTEXT_AUTHORIZING, /**< The client is authorizing itself to the daemon */
43 PA_CONTEXT_SETTING_NAME, /**< The client is passing its application name to the daemon */
44 PA_CONTEXT_READY, /**< The connection is established, the context is ready to execute operations */
45 PA_CONTEXT_FAILED, /**< The connection failed or was disconnected */
46 PA_CONTEXT_TERMINATED /**< The connection was terminated cleanly */
49 /** Return non-zero if the passed state is one of the connected states */
50 static inline int PA_CONTEXT_IS_GOOD(pa_context_state_t x) {
52 x == PA_CONTEXT_CONNECTING ||
53 x == PA_CONTEXT_AUTHORIZING ||
54 x == PA_CONTEXT_SETTING_NAME ||
55 x == PA_CONTEXT_READY;
58 /** The state of a stream */
59 typedef enum pa_stream_state {
60 PA_STREAM_UNCONNECTED, /**< The stream is not yet connected to any sink or source */
61 PA_STREAM_CREATING, /**< The stream is being created */
62 PA_STREAM_READY, /**< The stream is established, you may pass audio data to it now */
63 PA_STREAM_FAILED, /**< An error occured that made the stream invalid */
64 PA_STREAM_TERMINATED /**< The stream has been terminated cleanly */
67 /** Return non-zero if the passed state is one of the connected states */
68 static inline int PA_STREAM_IS_GOOD(pa_stream_state_t x) {
70 x == PA_STREAM_CREATING ||
74 /** The state of an operation */
75 typedef enum pa_operation_state {
76 PA_OPERATION_RUNNING, /**< The operation is still running */
77 PA_OPERATION_DONE, /**< The operation has been completed */
78 PA_OPERATION_CANCELED /**< The operation has been canceled */
79 } pa_operation_state_t;
81 /** An invalid index */
82 #define PA_INVALID_INDEX ((uint32_t) -1)
84 /** Some special flags for contexts. */
85 typedef enum pa_context_flags {
86 PA_CONTEXT_NOAUTOSPAWN = 1
87 /**< Disabled autospawning of the PulseAudio daemon if required */
90 /** The direction of a pa_stream object */
91 typedef enum pa_stream_direction {
92 PA_STREAM_NODIRECTION, /**< Invalid direction */
93 PA_STREAM_PLAYBACK, /**< Playback stream */
94 PA_STREAM_RECORD, /**< Record stream */
95 PA_STREAM_UPLOAD /**< Sample upload stream */
96 } pa_stream_direction_t;
98 /** Some special flags for stream connections. */
99 typedef enum pa_stream_flags {
101 PA_STREAM_START_CORKED = 0x0001U,
102 /**< Create the stream corked, requiring an explicit
103 * pa_stream_cork() call to uncork it. */
105 PA_STREAM_INTERPOLATE_TIMING = 0x0002U,
106 /**< Interpolate the latency for this stream. When enabled,
107 * pa_stream_get_latency() and pa_stream_get_time() will try to
108 * estimate the current record/playback time based on the local
109 * time that passed since the last timing info update. Using this
110 * option has the advantage of not requiring a whole roundtrip
111 * when the current playback/recording time is needed. Consider
112 * using this option when requesting latency information
113 * frequently. This is especially useful on long latency network
114 * connections. It makes a lot of sense to combine this option
115 * with PA_STREAM_AUTO_TIMING_UPDATE. */
117 PA_STREAM_NOT_MONOTONIC = 0x0004U,
118 /**< Don't force the time to increase monotonically. If this
119 * option is enabled, pa_stream_get_time() will not necessarily
120 * return always monotonically increasing time values on each
121 * call. This may confuse applications which cannot deal with time
122 * going 'backwards', but has the advantage that bad transport
123 * latency estimations that caused the time to to jump ahead can
124 * be corrected quickly, without the need to wait. (Please note
125 * that this flag was named PA_STREAM_NOT_MONOTONOUS in releases
126 * prior to 0.9.11. The old name is still defined too, for
127 * compatibility reasons. */
129 PA_STREAM_AUTO_TIMING_UPDATE = 0x0008U,
130 /**< If set timing update requests are issued periodically
131 * automatically. Combined with PA_STREAM_INTERPOLATE_TIMING you
132 * will be able to query the current time and latency with
133 * pa_stream_get_time() and pa_stream_get_latency() at all times
134 * without a packet round trip.*/
136 PA_STREAM_NO_REMAP_CHANNELS = 0x0010U,
137 /**< Don't remap channels by their name, instead map them simply
138 * by their index. Implies PA_STREAM_NO_REMIX_CHANNELS. Only
139 * supported when the server is at least PA 0.9.8. It is ignored
140 * on older servers.\since 0.9.8 */
142 PA_STREAM_NO_REMIX_CHANNELS = 0x0020U,
143 /**< When remapping channels by name, don't upmix or downmix them
144 * to related channels. Copy them into matching channels of the
145 * device 1:1. Only supported when the server is at least PA
146 * 0.9.8. It is ignored on older servers. \since 0.9.8 */
148 PA_STREAM_FIX_FORMAT = 0x0040U,
149 /**< Use the sample format of the sink/device this stream is being
150 * connected to, and possibly ignore the format the sample spec
151 * contains -- but you still have to pass a valid value in it as a
152 * hint to PulseAudio what would suit your stream best. If this is
153 * used you should query the used sample format after creating the
154 * stream by using pa_stream_get_sample_spec(). Also, if you
155 * specified manual buffer metrics it is recommended to update
156 * them with pa_stream_set_buffer_attr() to compensate for the
157 * changed frame sizes. Only supported when the server is at least
158 * PA 0.9.8. It is ignored on older servers. \since 0.9.8 */
160 PA_STREAM_FIX_RATE = 0x0080U,
161 /**< Use the sample rate of the sink, and possibly ignore the rate
162 * the sample spec contains. Usage similar to
163 * PA_STREAM_FIX_FORMAT.Only supported when the server is at least
164 * PA 0.9.8. It is ignored on older servers. \since 0.9.8 */
166 PA_STREAM_FIX_CHANNELS = 0x0100,
167 /**< Use the number of channels and the channel map of the sink,
168 * and possibly ignore the number of channels and the map the
169 * sample spec and the passed channel map contains. Usage similar
170 * to PA_STREAM_FIX_FORMAT. Only supported when the server is at
171 * least PA 0.9.8. It is ignored on older servers. \since 0.9.8 */
173 PA_STREAM_DONT_MOVE = 0x0200U,
174 /**< Don't allow moving of this stream to another
175 * sink/device. Useful if you use any of the PA_STREAM_FIX_ flags
176 * and want to make sure that resampling never takes place --
177 * which might happen if the stream is moved to another
178 * sink/source whith a different sample spec/channel map. Only
179 * supported when the server is at least PA 0.9.8. It is ignored
180 * on older servers. \since 0.9.8 */
182 PA_STREAM_VARIABLE_RATE = 0x0400U,
183 /**< Allow dynamic changing of the sampling rate during playback
184 * with pa_stream_update_sample_rate(). Only supported when the
185 * server is at least PA 0.9.8. It is ignored on older
186 * servers. \since 0.9.8 */
188 PA_STREAM_PEAK_DETECT = 0x0800U,
189 /**< Find peaks instead of resampling. \since 0.9.11 */
191 PA_STREAM_START_MUTED = 0x1000U,
192 /**< Create in muted state. \since 0.9.11 */
194 PA_STREAM_ADJUST_LATENCY = 0x2000U,
195 /**< Try to adjust the latency of the sink/source based on the
196 * requested buffer metrics and adjust buffer metrics
197 * accordingly. Also see pa_buffer_attr. This option may not be
198 * specified at the same time as PA_STREAM_EARLY_REQUESTS. \since
201 PA_STREAM_EARLY_REQUESTS = 0x4000U
202 /**< Enable compatibility mode for legacy clients that rely on a
203 * "classic" hardware device fragment-style playback model. If
204 * this option is set, the minreq value of the buffer metrics gets
205 * a new meaning: instead of just specifying that no requests
206 * asking for less new data than this value will be made to the
207 * client it will also guarantee that requests are generated as
208 * early as this limit is reached. This flag should only be set in
209 * very few situations where compatiblity with a fragment-based
210 * playback model needs to be kept and the client applications
211 * cannot deal with data requests that are delayed to the latest
212 * moment possible. (Usually these are programs that use usleep()
213 * or a similar call in their playback loops instead of sleeping
214 * on the device itself.) Also see pa_buffer_attr. This option may
215 * not be specified at the same time as
216 * PA_STREAM_ADJUST_LATENCY. \since 0.9.12 */
220 /** \cond fulldocs */
222 /** English is an evil language */
223 #define PA_STREAM_NOT_MONOTONOUS PA_STREAM_NOT_MONOTONIC
227 /** Playback and record buffer metrics */
228 typedef struct pa_buffer_attr {
230 /**< Maximum length of the buffer. Setting this to (uint32_t) -1
231 * will initialize this to the maximum value supported by server,
232 * which is recommended. */
235 /**< Playback only: target length of the buffer. The server tries
236 * to assure that at least tlength bytes are always available in
237 * the per-stream server-side playback buffer. It is recommended
238 * to set this to (uint32_t) -1, which will initialize this to a
239 * value that is deemed sensible by the server. However, this
240 * value will default to something like 2s, i.e. for applications
241 * that have specific latency requirements this value should be
242 * set to the maximum latency that the application can deal
243 * with. When PA_STREAM_ADJUST_LATENCY is not set this value will
244 * influence only the per-stream playback buffer size. When
245 * PA_STREAM_ADJUST_LATENCY is set the overall latency of the sink
246 * plus the playback buffer size is configured to this value. Set
247 * PA_STREAM_ADJUST_LATENCY if you are interested in adjusting the
248 * overall latency. Don't set it if you are interested in
249 * configuring the server-sider per-stream playback buffer
253 /**< Playback only: pre-buffering. The server does not start with
254 * playback before at least prebug bytes are available in the
255 * buffer. It is recommended to set this to (uint32_t) -1, which
256 * will initialize this to the same value as tlength, whatever
257 * that may be. Initialize to 0 to enable manual start/stop
258 * control of the stream. This means that playback will not stop
259 * on underrun and playback will not start automatically. Instead
260 * pa_stream_corked() needs to be called explicitly. If you set
261 * this value to 0 you should also set PA_STREAM_START_CORKED. */
264 /**< Playback only: minimum request. The server does not request
265 * less than minreq bytes from the client, instead waits until the
266 * buffer is free enough to request more bytes at once. It is
267 * recommended to set this to (uint32_t) -1, which will initialize
268 * this to a value that is deemed sensible by the server. This
269 * should be set to a value that gives PulseAudio enough time to
270 * move the data from the per-stream playback buffer into the
271 * hardware playback buffer. */
274 /**< Recording only: fragment size. The server sends data in
275 * blocks of fragsize bytes size. Large values deminish
276 * interactivity with other operations on the connection context
277 * but decrease control overhead. It is recommended to set this to
278 * (uint32_t) -1, which will initialize this to a value that is
279 * deemed sensible by the server. However, this value will default
280 * to something like 2s, i.e. for applications that have specific
281 * latency requirements this value should be set to the maximum
282 * latency that the application can deal with. If
283 * PA_STREAM_ADJUST_LATENCY is set the overall source latency will
284 * be adjusted according to this value. If it is not set the
285 * source latency is left unmodified. */
289 /** Error values as used by pa_context_errno(). Use pa_strerror() to convert these values to human readable strings */
291 PA_OK = 0, /**< No error */
292 PA_ERR_ACCESS, /**< Access failure */
293 PA_ERR_COMMAND, /**< Unknown command */
294 PA_ERR_INVALID, /**< Invalid argument */
295 PA_ERR_EXIST, /**< Entity exists */
296 PA_ERR_NOENTITY, /**< No such entity */
297 PA_ERR_CONNECTIONREFUSED, /**< Connection refused */
298 PA_ERR_PROTOCOL, /**< Protocol error */
299 PA_ERR_TIMEOUT, /**< Timeout */
300 PA_ERR_AUTHKEY, /**< No authorization key */
301 PA_ERR_INTERNAL, /**< Internal error */
302 PA_ERR_CONNECTIONTERMINATED, /**< Connection terminated */
303 PA_ERR_KILLED, /**< Entity killed */
304 PA_ERR_INVALIDSERVER, /**< Invalid server */
305 PA_ERR_MODINITFAILED, /**< Module initialization failed */
306 PA_ERR_BADSTATE, /**< Bad state */
307 PA_ERR_NODATA, /**< No data */
308 PA_ERR_VERSION, /**< Incompatible protocol version */
309 PA_ERR_TOOLARGE, /**< Data too large */
310 PA_ERR_NOTSUPPORTED, /**< Operation not supported \since 0.9.5 */
311 PA_ERR_UNKNOWN, /**< The error code was unknown to the client */
312 PA_ERR_NOEXTENSION, /**< Extension does not exist. \since 0.9.12 */
313 PA_ERR_MAX /**< Not really an error but the first invalid error code */
316 /** Subscription event mask, as used by pa_context_subscribe() */
317 typedef enum pa_subscription_mask {
318 PA_SUBSCRIPTION_MASK_NULL = 0x0000U,
321 PA_SUBSCRIPTION_MASK_SINK = 0x0001U,
324 PA_SUBSCRIPTION_MASK_SOURCE = 0x0002U,
325 /**< Source events */
327 PA_SUBSCRIPTION_MASK_SINK_INPUT = 0x0004U,
328 /**< Sink input events */
330 PA_SUBSCRIPTION_MASK_SOURCE_OUTPUT = 0x0008U,
331 /**< Source output events */
333 PA_SUBSCRIPTION_MASK_MODULE = 0x0010U,
334 /**< Module events */
336 PA_SUBSCRIPTION_MASK_CLIENT = 0x0020U,
337 /**< Client events */
339 PA_SUBSCRIPTION_MASK_SAMPLE_CACHE = 0x0040U,
340 /**< Sample cache events */
342 PA_SUBSCRIPTION_MASK_SERVER = 0x0080U,
343 /**< Other global server changes. */
345 PA_SUBSCRIPTION_MASK_AUTOLOAD = 0x0100U,
346 /**< Autoload table events. */
348 PA_SUBSCRIPTION_MASK_ALL = 0x01ffU
349 /**< Catch all events */
350 } pa_subscription_mask_t;
352 /** Subscription event types, as used by pa_context_subscribe() */
353 typedef enum pa_subscription_event_type {
354 PA_SUBSCRIPTION_EVENT_SINK = 0x0000U,
355 /**< Event type: Sink */
357 PA_SUBSCRIPTION_EVENT_SOURCE = 0x0001U,
358 /**< Event type: Source */
360 PA_SUBSCRIPTION_EVENT_SINK_INPUT = 0x0002U,
361 /**< Event type: Sink input */
363 PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT = 0x0003U,
364 /**< Event type: Source output */
366 PA_SUBSCRIPTION_EVENT_MODULE = 0x0004U,
367 /**< Event type: Module */
369 PA_SUBSCRIPTION_EVENT_CLIENT = 0x0005U,
370 /**< Event type: Client */
372 PA_SUBSCRIPTION_EVENT_SAMPLE_CACHE = 0x0006U,
373 /**< Event type: Sample cache item */
375 PA_SUBSCRIPTION_EVENT_SERVER = 0x0007U,
376 /**< Event type: Global server change, only occuring with PA_SUBSCRIPTION_EVENT_CHANGE. */
378 PA_SUBSCRIPTION_EVENT_AUTOLOAD = 0x0008U,
379 /**< Event type: Autoload table changes. */
381 PA_SUBSCRIPTION_EVENT_FACILITY_MASK = 0x000FU,
382 /**< A mask to extract the event type from an event value */
384 PA_SUBSCRIPTION_EVENT_NEW = 0x0000U,
385 /**< A new object was created */
387 PA_SUBSCRIPTION_EVENT_CHANGE = 0x0010U,
388 /**< A property of the object was modified */
390 PA_SUBSCRIPTION_EVENT_REMOVE = 0x0020U,
391 /**< An object was removed */
393 PA_SUBSCRIPTION_EVENT_TYPE_MASK = 0x0030U,
394 /**< A mask to extract the event operation from an event value */
396 } pa_subscription_event_type_t;
398 /** Return one if an event type t matches an event mask bitfield */
399 #define pa_subscription_match_flags(m, t) (!!((m) & (1 << ((t) & PA_SUBSCRIPTION_EVENT_FACILITY_MASK))))
401 /** A structure for all kinds of timing information of a stream. See
402 * pa_stream_update_timing_info() and pa_stream_get_timing_info(). The
403 * total output latency a sample that is written with
404 * pa_stream_write() takes to be played may be estimated by
405 * sink_usec+buffer_usec+transport_usec. (where buffer_usec is defined
406 * as pa_bytes_to_usec(write_index-read_index)) The output buffer
407 * which buffer_usec relates to may be manipulated freely (with
408 * pa_stream_write()'s seek argument, pa_stream_flush() and friends),
409 * the buffers sink_usec and source_usec relate to are first-in
410 * first-out (FIFO) buffers which cannot be flushed or manipulated in
411 * any way. The total input latency a sample that is recorded takes to
412 * be delivered to the application is:
413 * source_usec+buffer_usec+transport_usec-sink_usec. (Take care of
414 * sign issues!) When connected to a monitor source sink_usec contains
415 * the latency of the owning sink. The two latency estimations
416 * described here are implemented in pa_stream_get_latency(). Please
417 * note that this structure can be extended as part of evolutionary
418 * API updates at any time in any new release.*/
419 typedef struct pa_timing_info {
420 struct timeval timestamp;
421 /**< The time when this timing info structure was current */
423 int synchronized_clocks;
424 /**< Non-zero if the local and the remote machine have
425 * synchronized clocks. If synchronized clocks are detected
426 * transport_usec becomes much more reliable. However, the code
427 * that detects synchronized clocks is very limited und unreliable
431 /**< Time in usecs a sample takes to be played on the sink. For
432 * playback streams and record streams connected to a monitor
435 pa_usec_t source_usec;
436 /**< Time in usecs a sample takes from being recorded to being
437 * delivered to the application. Only for record streams. */
439 pa_usec_t transport_usec;
440 /**< Estimated time in usecs a sample takes to be transferred
441 * to/from the daemon. For both playback and record streams. */
444 /**< Non-zero when the stream is currently not underrun and data
445 * is being passed on to the device. Only for playback
446 * streams. This field does not say whether the data is actually
447 * already being played. To determine this check whether
448 * since_underrun (converted to usec) is larger than sink_usec.*/
450 int write_index_corrupt;
451 /**< Non-zero if write_index is not up-to-date because a local
452 * write command that corrupted it has been issued in the time
453 * since this latency info was current . Only write commands with
454 * SEEK_RELATIVE_ON_READ and SEEK_RELATIVE_END can corrupt
458 /**< Current write index into the playback buffer in bytes. Think
459 * twice before using this for seeking purposes: it might be out
460 * of date a the time you want to use it. Consider using
461 * PA_SEEK_RELATIVE instead. */
463 int read_index_corrupt;
464 /**< Non-zero if read_index is not up-to-date because a local
465 * pause or flush request that corrupted it has been issued in the
466 * time since this latency info was current. */
469 /**< Current read index into the playback buffer in bytes. Think
470 * twice before using this for seeking purposes: it might be out
471 * of date a the time you want to use it. Consider using
472 * PA_SEEK_RELATIVE_ON_READ instead. */
474 pa_usec_t configured_sink_usec;
475 /**< The configured latency for the sink. \since 0.9.11 */
477 pa_usec_t configured_source_usec;
478 /**< The configured latency for * the source. \since 0.9.11 */
480 int64_t since_underrun;
481 /**< Bytes that were handed to the sink since the last underrun
482 * happened, or since playback started again after the last
483 * underrun. playing will tell you which case it is. \since
488 /** A structure for the spawn api. This may be used to integrate auto
489 * spawned daemons into your application. For more information see
490 * pa_context_connect(). When spawning a new child process the
491 * waitpid() is used on the child's PID. The spawn routine will not
492 * block or ignore SIGCHLD signals, since this cannot be done in a
493 * thread compatible way. You might have to do this in
494 * prefork/postfork. */
495 typedef struct pa_spawn_api {
496 void (*prefork)(void);
497 /**< Is called just before the fork in the parent process. May be
500 void (*postfork)(void);
501 /**< Is called immediately after the fork in the parent
502 * process. May be NULL.*/
504 void (*atfork)(void);
505 /**< Is called immediately after the fork in the child
506 * process. May be NULL. It is not safe to close all file
507 * descriptors in this function unconditionally, since a UNIX
508 * socket (created using socketpair()) is passed to the new
512 /** Seek type for pa_stream_write(). */
513 typedef enum pa_seek_mode {
514 PA_SEEK_RELATIVE = 0,
515 /**< Seek relatively to the write index */
517 PA_SEEK_ABSOLUTE = 1,
518 /**< Seek relatively to the start of the buffer queue */
520 PA_SEEK_RELATIVE_ON_READ = 2,
521 /**< Seek relatively to the read index. */
523 PA_SEEK_RELATIVE_END = 3
524 /**< Seek relatively to the current end of the buffer queue. */
527 /** Special sink flags. */
528 typedef enum pa_sink_flags {
529 PA_SINK_HW_VOLUME_CTRL = 0x0001U,
530 /**< Supports hardware volume control */
532 PA_SINK_LATENCY = 0x0002U,
533 /**< Supports latency querying */
535 PA_SINK_HARDWARE = 0x0004U,
536 /**< Is a hardware sink of some kind, in contrast to
537 * "virtual"/software sinks \since 0.9.3 */
539 PA_SINK_NETWORK = 0x0008U,
540 /**< Is a networked sink of some kind. \since 0.9.7 */
542 PA_SINK_HW_MUTE_CTRL = 0x0010U,
543 /**< Supports hardware mute control \since 0.9.11 */
545 PA_SINK_DECIBEL_VOLUME = 0x0020U
546 /**< Volume can be translated to dB with pa_sw_volume_to_dB()
550 /** Special source flags. */
551 typedef enum pa_source_flags {
552 PA_SOURCE_HW_VOLUME_CTRL = 0x0001U,
553 /**< Supports hardware volume control */
555 PA_SOURCE_LATENCY = 0x0002U,
556 /**< Supports latency querying */
558 PA_SOURCE_HARDWARE = 0x0004U,
559 /**< Is a hardware source of some kind, in contrast to
560 * "virtual"/software source \since 0.9.3 */
562 PA_SOURCE_NETWORK = 0x0008U,
563 /**< Is a networked sink of some kind. \since 0.9.7 */
565 PA_SOURCE_HW_MUTE_CTRL = 0x0010U,
566 /**< Supports hardware mute control \since 0.9.11 */
568 PA_SOURCE_DECIBEL_VOLUME = 0x0020U
569 /**< Volume can be translated to dB with pa_sw_volume_to_dB()
573 /** A generic free() like callback prototype */
574 typedef void (*pa_free_cb_t)(void *p);