]> code.delx.au - pulseaudio/blob - src/pulse/stream.h
010f968fea6e507c84758454eea9c7f91c80f6e9
[pulseaudio] / src / pulse / stream.h
1 #ifndef foostreamhfoo
2 #define foostreamhfoo
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 <sys/types.h>
27
28 #include <pulse/sample.h>
29 #include <pulse/format.h>
30 #include <pulse/channelmap.h>
31 #include <pulse/volume.h>
32 #include <pulse/def.h>
33 #include <pulse/cdecl.h>
34 #include <pulse/operation.h>
35 #include <pulse/context.h>
36 #include <pulse/proplist.h>
37
38 /** \page streams Audio Streams
39 *
40 * \section overv_sec Overview
41 *
42 * Audio streams form the central functionality of the sound server. Data is
43 * routed, converted and mixed from several sources before it is passed along
44 * to a final output. Currently, there are three forms of audio streams:
45 *
46 * \li Playback streams - Data flows from the client to the server.
47 * \li Record streams - Data flows from the server to the client.
48 * \li Upload streams - Similar to playback streams, but the data is stored in
49 * the sample cache. See \ref scache for more information
50 * about controlling the sample cache.
51 *
52 * \section create_sec Creating
53 *
54 * To access a stream, a pa_stream object must be created using
55 * pa_stream_new(). At this point the audio sample format and mapping of
56 * channels must be specified. See \ref sample and \ref channelmap for more
57 * information about those structures.
58 *
59 * This first step will only create a client-side object, representing the
60 * stream. To use the stream, a server-side object must be created and
61 * associated with the local object. Depending on which type of stream is
62 * desired, a different function is needed:
63 *
64 * \li Playback stream - pa_stream_connect_playback()
65 * \li Record stream - pa_stream_connect_record()
66 * \li Upload stream - pa_stream_connect_upload() (see \ref scache)
67 *
68 * Similar to how connections are done in contexts, connecting a stream will
69 * not generate a pa_operation object. Also like contexts, the application
70 * should register a state change callback, using
71 * pa_stream_set_state_callback(), and wait for the stream to enter an active
72 * state.
73 *
74 * \subsection bufattr_subsec Buffer Attributes
75 *
76 * Playback and record streams always have a server-side buffer as
77 * part of the data flow. The size of this buffer needs to be chosen
78 * in a compromise between low latency and sensitivity for buffer
79 * overflows/underruns.
80 *
81 * The buffer metrics may be controlled by the application. They are
82 * described with a pa_buffer_attr structure which contains a number
83 * of fields:
84 *
85 * \li maxlength - The absolute maximum number of bytes that can be
86 * stored in the buffer. If this value is exceeded
87 * then data will be lost. It is recommended to pass
88 * (uint32_t) -1 here which will cause the server to
89 * fill in the maximum possible value.
90 *
91 * \li tlength - The target fill level of the playback buffer. The
92 * server will only send requests for more data as long
93 * as the buffer has less than this number of bytes of
94 * data. If you pass (uint32_t) -1 (which is
95 * recommended) here the server will choose the longest
96 * target buffer fill level possible to minimize the
97 * number of necessary wakeups and maximize drop-out
98 * safety. This can exceed 2s of buffering. For
99 * low-latency applications or applications where
100 * latency matters you should pass a proper value here.
101 *
102 * \li prebuf - Number of bytes that need to be in the buffer before
103 * playback will commence. Start of playback can be
104 * forced using pa_stream_trigger() even though the
105 * prebuffer size hasn't been reached. If a buffer
106 * underrun occurs, this prebuffering will be again
107 * enabled. If the playback shall never stop in case of a
108 * buffer underrun, this value should be set to 0. In
109 * that case the read index of the output buffer
110 * overtakes the write index, and hence the fill level of
111 * the buffer is negative. If you pass (uint32_t) -1 here
112 * (which is recommended) the server will choose the same
113 * value as tlength here.
114 *
115 * \li minreq - Minimum free number of the bytes in the playback
116 * buffer before the server will request more data. It is
117 * recommended to fill in (uint32_t) -1 here. This value
118 * influences how much time the sound server has to move
119 * data from the per-stream server-side playback buffer
120 * to the hardware playback buffer.
121 *
122 * \li fragsize - Maximum number of bytes that the server will push in
123 * one chunk for record streams. If you pass (uint32_t)
124 * -1 (which is recommended) here, the server will
125 * choose the longest fragment setting possible to
126 * minimize the number of necessary wakeups and
127 * maximize drop-out safety. This can exceed 2s of
128 * buffering. For low-latency applications or
129 * applications where latency matters you should pass a
130 * proper value here.
131 *
132 * If PA_STREAM_ADJUST_LATENCY is set, then the tlength/fragsize
133 * parameters will be interpreted slightly differently than described
134 * above when passed to pa_stream_connect_record() and
135 * pa_stream_connect_playback(): the overall latency that is comprised
136 * of both the server side playback buffer length, the hardware
137 * playback buffer length and additional latencies will be adjusted in
138 * a way that it matches tlength resp. fragsize. Set
139 * PA_STREAM_ADJUST_LATENCY if you want to control the overall
140 * playback latency for your stream. Unset it if you want to control
141 * only the latency induced by the server-side, rewritable playback
142 * buffer. The server will try to fulfill the clients latency requests
143 * as good as possible. However if the underlying hardware cannot
144 * change the hardware buffer length or only in a limited range, the
145 * actually resulting latency might be different from what the client
146 * requested. Thus, for synchronization clients always need to check
147 * the actual measured latency via pa_stream_get_latency() or a
148 * similar call, and not make any assumptions. about the latency
149 * available. The function pa_stream_get_buffer_attr() will always
150 * return the actual size of the server-side per-stream buffer in
151 * tlength/fragsize, regardless whether PA_STREAM_ADJUST_LATENCY is
152 * set or not.
153 *
154 * The server-side per-stream playback buffers are indexed by a write and a read
155 * index. The application writes to the write index and the sound
156 * device reads from the read index. The read index is increased
157 * monotonically, while the write index may be freely controlled by
158 * the application. Substracting the read index from the write index
159 * will give you the current fill level of the buffer. The read/write
160 * indexes are 64bit values and measured in bytes, they will never
161 * wrap. The current read/write index may be queried using
162 * pa_stream_get_timing_info() (see below for more information). In
163 * case of a buffer underrun the read index is equal or larger than
164 * the write index. Unless the prebuf value is 0, PulseAudio will
165 * temporarily pause playback in such a case, and wait until the
166 * buffer is filled up to prebuf bytes again. If prebuf is 0, the
167 * read index may be larger than the write index, in which case
168 * silence is played. If the application writes data to indexes lower
169 * than the read index, the data is immediately lost.
170 *
171 * \section transfer_sec Transferring Data
172 *
173 * Once the stream is up, data can start flowing between the client and the
174 * server. Two different access models can be used to transfer the data:
175 *
176 * \li Asynchronous - The application register a callback using
177 * pa_stream_set_write_callback() and
178 * pa_stream_set_read_callback() to receive notifications
179 * that data can either be written or read.
180 * \li Polled - Query the library for available data/space using
181 * pa_stream_writable_size() and pa_stream_readable_size() and
182 * transfer data as needed. The sizes are stored locally, in the
183 * client end, so there is no delay when reading them.
184 *
185 * It is also possible to mix the two models freely.
186 *
187 * Once there is data/space available, it can be transferred using either
188 * pa_stream_write() for playback, or pa_stream_peek() / pa_stream_drop() for
189 * record. Make sure you do not overflow the playback buffers as data will be
190 * dropped.
191 *
192 * \section bufctl_sec Buffer Control
193 *
194 * The transfer buffers can be controlled through a number of operations:
195 *
196 * \li pa_stream_cork() - Start or stop the playback or recording.
197 * \li pa_stream_trigger() - Start playback immediatly and do not wait for
198 * the buffer to fill up to the set trigger level.
199 * \li pa_stream_prebuf() - Reenable the playback trigger level.
200 * \li pa_stream_drain() - Wait for the playback buffer to go empty. Will
201 * return a pa_operation object that will indicate when
202 * the buffer is completely drained.
203 * \li pa_stream_flush() - Drop all data from the playback buffer and do not
204 * wait for it to finish playing.
205 *
206 * \section seek_modes Seeking in the Playback Buffer
207 *
208 * A client application may freely seek in the playback buffer. To
209 * accomplish that the pa_stream_write() function takes a seek mode
210 * and an offset argument. The seek mode is one of:
211 *
212 * \li PA_SEEK_RELATIVE - seek relative to the current write index
213 * \li PA_SEEK_ABSOLUTE - seek relative to the beginning of the playback buffer, (i.e. the first that was ever played in the stream)
214 * \li PA_SEEK_RELATIVE_ON_READ - seek relative to the current read index. Use this to write data to the output buffer that should be played as soon as possible
215 * \li PA_SEEK_RELATIVE_END - seek relative to the last byte ever written.
216 *
217 * If an application just wants to append some data to the output
218 * buffer, PA_SEEK_RELATIVE and an offset of 0 should be used.
219 *
220 * After a call to pa_stream_write() the write index will be left at
221 * the position right after the last byte of the written data.
222 *
223 * \section latency_sec Latency
224 *
225 * A major problem with networked audio is the increased latency caused by
226 * the network. To remedy this, PulseAudio supports an advanced system of
227 * monitoring the current latency.
228 *
229 * To get the raw data needed to calculate latencies, call
230 * pa_stream_get_timing_info(). This will give you a pa_timing_info
231 * structure that contains everything that is known about the server
232 * side buffer transport delays and the backend active in the
233 * server. (Besides other things it contains the write and read index
234 * values mentioned above.)
235 *
236 * This structure is updated every time a
237 * pa_stream_update_timing_info() operation is executed. (i.e. before
238 * the first call to this function the timing information structure is
239 * not available!) Since it is a lot of work to keep this structure
240 * up-to-date manually, PulseAudio can do that automatically for you:
241 * if PA_STREAM_AUTO_TIMING_UPDATE is passed when connecting the
242 * stream PulseAudio will automatically update the structure every
243 * 100ms and every time a function is called that might invalidate the
244 * previously known timing data (such as pa_stream_write() or
245 * pa_stream_flush()). Please note however, that there always is a
246 * short time window when the data in the timing information structure
247 * is out-of-date. PulseAudio tries to mark these situations by
248 * setting the write_index_corrupt and read_index_corrupt fields
249 * accordingly.
250 *
251 * The raw timing data in the pa_timing_info structure is usually hard
252 * to deal with. Therefore a simpler interface is available:
253 * you can call pa_stream_get_time() or pa_stream_get_latency(). The
254 * former will return the current playback time of the hardware since
255 * the stream has been started. The latter returns the overall time a sample
256 * that you write now takes to be played by the hardware. These two
257 * functions base their calculations on the same data that is returned
258 * by pa_stream_get_timing_info(). Hence the same rules for keeping
259 * the timing data up-to-date apply here. In case the write or read
260 * index is corrupted, these two functions will fail with
261 * PA_ERR_NODATA set.
262 *
263 * Since updating the timing info structure usually requires a full
264 * network round trip and some applications monitor the timing very
265 * often PulseAudio offers a timing interpolation system. If
266 * PA_STREAM_INTERPOLATE_TIMING is passed when connecting the stream,
267 * pa_stream_get_time() and pa_stream_get_latency() will try to
268 * interpolate the current playback time/latency by estimating the
269 * number of samples that have been played back by the hardware since
270 * the last regular timing update. It is especially useful to combine
271 * this option with PA_STREAM_AUTO_TIMING_UPDATE, which will enable
272 * you to monitor the current playback time/latency very precisely and
273 * very frequently without requiring a network round trip every time.
274 *
275 * \section flow_sec Overflow and underflow
276 *
277 * Even with the best precautions, buffers will sometime over - or
278 * underflow. To handle this gracefully, the application can be
279 * notified when this happens. Callbacks are registered using
280 * pa_stream_set_overflow_callback() and
281 * pa_stream_set_underflow_callback().
282 *
283 * \section sync_streams Sychronizing Multiple Playback Streams
284 *
285 * PulseAudio allows applications to fully synchronize multiple
286 * playback streams that are connected to the same output device. That
287 * means the streams will always be played back sample-by-sample
288 * synchronously. If stream operations like pa_stream_cork() are
289 * issued on one of the synchronized streams, they are simultaneously
290 * issued on the others.
291 *
292 * To synchronize a stream to another, just pass the "master" stream
293 * as last argument to pa_stream_connect_playback(). To make sure that
294 * the freshly created stream doesn't start playback right-away, make
295 * sure to pass PA_STREAM_START_CORKED and - after all streams have
296 * been created - uncork them all with a single call to
297 * pa_stream_cork() for the master stream.
298 *
299 * To make sure that a particular stream doesn't stop to play when a
300 * server side buffer underrun happens on it while the other
301 * synchronized streams continue playing and hence deviate you need to
302 * pass a "prebuf" pa_buffer_attr of 0 when connecting it.
303 *
304 * \section disc_sec Disconnecting
305 *
306 * When a stream has served is purpose it must be disconnected with
307 * pa_stream_disconnect(). If you only unreference it, then it will live on
308 * and eat resources both locally and on the server until you disconnect the
309 * context.
310 *
311 */
312
313 /** \file
314 * Audio streams for input, output and sample upload
315 *
316 * See also \subpage streams
317 */
318
319 PA_C_DECL_BEGIN
320
321 /** An opaque stream for playback or recording */
322 typedef struct pa_stream pa_stream;
323
324 /** A generic callback for operation completion */
325 typedef void (*pa_stream_success_cb_t) (pa_stream*s, int success, void *userdata);
326
327 /** A generic request callback */
328 typedef void (*pa_stream_request_cb_t)(pa_stream *p, size_t nbytes, void *userdata);
329
330 /** A generic notification callback */
331 typedef void (*pa_stream_notify_cb_t)(pa_stream *p, void *userdata);
332
333 /** A callback for asynchronous meta/policy event messages. Well known
334 * event names are PA_STREAM_EVENT_REQUEST_CORK and
335 * PA_STREAM_EVENT_REQUEST_UNCORK. The set of defined events can be
336 * extended at any time. Also, server modules may introduce additional
337 * message types so make sure that your callback function ignores messages
338 * it doesn't know. \since 0.9.15 */
339 typedef void (*pa_stream_event_cb_t)(pa_stream *p, const char *name, pa_proplist *pl, void *userdata);
340
341 /** Create a new, unconnected stream with the specified name and
342 * sample type. It is recommended to use pa_stream_new_with_proplist()
343 * instead and specify some initial properties. */
344 pa_stream* pa_stream_new(
345 pa_context *c /**< The context to create this stream in */,
346 const char *name /**< A name for this stream */,
347 const pa_sample_spec *ss /**< The desired sample format */,
348 const pa_channel_map *map /**< The desired channel map, or NULL for default */);
349
350 /** Create a new, unconnected stream with the specified name and
351 * sample type, and specify the the initial stream property
352 * list. \since 0.9.11 */
353 pa_stream* pa_stream_new_with_proplist(
354 pa_context *c /**< The context to create this stream in */,
355 const char *name /**< A name for this stream */,
356 const pa_sample_spec *ss /**< The desired sample format */,
357 const pa_channel_map *map /**< The desired channel map, or NULL for default */,
358 pa_proplist *p /**< The initial property list */);
359
360 /* Create a new, unconnected stream with the specified name, the set of formats
361 * this client can provide, and an initial list of properties. While
362 * connecting, the server will select the most appropriate format which the
363 * client must then provide. \since 1.0 */
364 pa_stream *pa_stream_new_extended(
365 pa_context *c /**< The context to create this stream in */,
366 const char *name /**< A name for this stream */,
367 pa_format_info * const * formats /**< The list of formats that can be provided */,
368 unsigned int n_formats /**< The number of formats being passed in */,
369 pa_proplist *p /**< The initial property list */);
370
371 /** Decrease the reference counter by one */
372 void pa_stream_unref(pa_stream *s);
373
374 /** Increase the reference counter by one */
375 pa_stream *pa_stream_ref(pa_stream *s);
376
377 /** Return the current state of the stream */
378 pa_stream_state_t pa_stream_get_state(pa_stream *p);
379
380 /** Return the context this stream is attached to */
381 pa_context* pa_stream_get_context(pa_stream *p);
382
383 /** Return the sink input resp. source output index this stream is
384 * identified in the server with. This is useful for usage with the
385 * introspection functions, such as pa_context_get_sink_input_info()
386 * resp. pa_context_get_source_output_info(). */
387 uint32_t pa_stream_get_index(pa_stream *s);
388
389 /** Return the index of the sink or source this stream is connected to
390 * in the server. This is useful for usage with the introspection
391 * functions, such as pa_context_get_sink_info_by_index()
392 * resp. pa_context_get_source_info_by_index(). Please note that
393 * streams may be moved between sinks/sources and thus it is
394 * recommended to use pa_stream_set_moved_callback() to be notified
395 * about this. This function will return with PA_ERR_NOTSUPPORTED when the
396 * server is older than 0.9.8. \since 0.9.8 */
397 uint32_t pa_stream_get_device_index(pa_stream *s);
398
399 /** Return the name of the sink or source this stream is connected to
400 * in the server. This is useful for usage with the introspection
401 * functions, such as pa_context_get_sink_info_by_name()
402 * resp. pa_context_get_source_info_by_name(). Please note that
403 * streams may be moved between sinks/sources and thus it is
404 * recommended to use pa_stream_set_moved_callback() to be notified
405 * about this. This function will return with PA_ERR_NOTSUPPORTED when the
406 * server is older than 0.9.8. \since 0.9.8 */
407 const char *pa_stream_get_device_name(pa_stream *s);
408
409 /** Return 1 if the sink or source this stream is connected to has
410 * been suspended. This will return 0 if not, and negative on
411 * error. This function will return with PA_ERR_NOTSUPPORTED when the
412 * server is older than 0.9.8. \since 0.9.8 */
413 int pa_stream_is_suspended(pa_stream *s);
414
415 /** Return 1 if the this stream has been corked. This will return 0 if
416 * not, and negative on error. \since 0.9.11 */
417 int pa_stream_is_corked(pa_stream *s);
418
419 /** Connect the stream to a sink. It is strongly recommended to pass
420 * NULL in both dev and volume and not to set either
421 * PA_STREAM_START_MUTED nor PA_STREAM_START_UNMUTED -- unless these
422 * options are directly dependant on user input or configuration. If
423 * you follow this rule then the sound server will have the full
424 * flexibility to choose the device, volume and mute status
425 * automatically, based on server-side policies, heuristics and stored
426 * information from previous uses. Also the server may choose to
427 * reconfigure audio devices to make other sinks/sources or
428 * capabilities available to be able to accept the stream. Before
429 * 0.9.20 it was not defined whether the 'volume' parameter was
430 * interpreted relative to the sink's current volume or treated as
431 * absolute device volume. Since 0.9.20 it is an absolute volume when
432 * the sink is in flat volume mode, and relative otherwise, thus
433 * making sure the volume passed here has always the same semantics as
434 * the volume passed to pa_context_set_sink_input_volume(). */
435 int pa_stream_connect_playback(
436 pa_stream *s /**< The stream to connect to a sink */,
437 const char *dev /**< Name of the sink to connect to, or NULL for default */ ,
438 const pa_buffer_attr *attr /**< Buffering attributes, or NULL for default */,
439 pa_stream_flags_t flags /**< Additional flags, or 0 for default */,
440 const pa_cvolume *volume /**< Initial volume, or NULL for default */,
441 pa_stream *sync_stream /**< Synchronize this stream with the specified one, or NULL for a standalone stream*/);
442
443 /** Connect the stream to a source */
444 int pa_stream_connect_record(
445 pa_stream *s /**< The stream to connect to a source */ ,
446 const char *dev /**< Name of the source to connect to, or NULL for default */,
447 const pa_buffer_attr *attr /**< Buffer attributes, or NULL for default */,
448 pa_stream_flags_t flags /**< Additional flags, or 0 for default */);
449
450 /** Disconnect a stream from a source/sink */
451 int pa_stream_disconnect(pa_stream *s);
452
453 /** Prepare writing data to the server (for playback streams). This
454 * function may be used to optimize the number of memory copies when
455 * doing playback ("zero-copy"). It is recommended to call this
456 * function before each call to pa_stream_write(). Pass in the address
457 * to a pointer and an address of the number of bytes you want to
458 * write. On return the two values will contain a pointer where you
459 * can place the data to write and the maximum number of bytes you can
460 * write. On return *nbytes can be smaller or have the same value as
461 * you passed in. You need to be able to handle both cases. Accessing
462 * memory beyond the returned *nbytes value is invalid. Acessing the
463 * memory returned after the following pa_stream_write() or
464 * pa_stream_cancel_write() is invalid. On invocation only *nbytes
465 * needs to be initialized, on return both *data and *nbytes will be
466 * valid. If you place (size_t) -1 in *nbytes on invocation the memory
467 * size will be chosen automatically (which is recommended to
468 * do). After placing your data in the memory area returned call
469 * pa_stream_write() with data set to an address within this memory
470 * area and an nbytes value that is smaller or equal to what was
471 * returned by this function to actually execute the write. An
472 * invocation of pa_stream_write() should follow "quickly" on
473 * pa_stream_begin_write(). It is not recommended letting an unbounded
474 * amount of time pass after calling pa_stream_begin_write() and
475 * before calling pa_stream_write(). If you want to cancel a
476 * previously called pa_stream_begin_write() without calling
477 * pa_stream_write() use pa_stream_cancel_write(). Calling
478 * pa_stream_begin_write() twice without calling pa_stream_write() or
479 * pa_stream_cancel_write() in between will return exactly the same
480 * pointer/nbytes values.\since 0.9.16 */
481 int pa_stream_begin_write(
482 pa_stream *p,
483 void **data,
484 size_t *nbytes);
485
486 /** Reverses the effect of pa_stream_begin_write() dropping all data
487 * that has already been placed in the memory area returned by
488 * pa_stream_begin_write(). Only valid to call if
489 * pa_stream_begin_write() was called before and neither
490 * pa_stream_cancel_write() nor pa_stream_write() have been called
491 * yet. Accessing the memory previously returned by
492 * pa_stream_begin_write() after this call is invalid. Any further
493 * explicit freeing of the memory area is not necessary. \since
494 * 0.9.16 */
495 int pa_stream_cancel_write(
496 pa_stream *p);
497
498 /** Write some data to the server (for playback streams), if free_cb
499 * is non-NULL this routine is called when all data has been written
500 * out and an internal reference to the specified data is kept, the
501 * data is not copied. If NULL, the data is copied into an internal
502 * buffer. The client may freely seek around in the output buffer. For
503 * most applications passing 0 and PA_SEEK_RELATIVE as arguments for
504 * offset and seek should be useful. After the write call succeeded
505 * the write index will be at the position after where this chunk of
506 * data has been written to.
507 *
508 * As an optimization for avoiding needless memory copies you may call
509 * pa_stream_begin_write() before this call and then place your audio
510 * data directly in the memory area returned by that call. Then, pass
511 * a pointer to that memory area to pa_stream_write(). After the
512 * invocation of pa_stream_write() the memory area may no longer be
513 * accessed. Any further explicit freeing of the memory area is not
514 * necessary. It is OK to write the memory area returned by
515 * pa_stream_begin_write() only partially with this call, skipping
516 * bytes both at the end and at the beginning of the reserved memory
517 * area.*/
518 int pa_stream_write(
519 pa_stream *p /**< The stream to use */,
520 const void *data /**< The data to write */,
521 size_t nbytes /**< The length of the data to write in bytes*/,
522 pa_free_cb_t free_cb /**< A cleanup routine for the data or NULL to request an internal copy */,
523 int64_t offset, /**< Offset for seeking, must be 0 for upload streams */
524 pa_seek_mode_t seek /**< Seek mode, must be PA_SEEK_RELATIVE for upload streams */);
525
526 /** Read the next fragment from the buffer (for recording streams).
527 * data will point to the actual data and nbytes will contain the size
528 * of the data in bytes (which can be less or more than a complete
529 * fragment). Use pa_stream_drop() to actually remove the data from
530 * the buffer. If no data is available this will return a NULL
531 * pointer */
532 int pa_stream_peek(
533 pa_stream *p /**< The stream to use */,
534 const void **data /**< Pointer to pointer that will point to data */,
535 size_t *nbytes /**< The length of the data read in bytes */);
536
537 /** Remove the current fragment on record streams. It is invalid to do this without first
538 * calling pa_stream_peek(). */
539 int pa_stream_drop(pa_stream *p);
540
541 /** Return the number of bytes that may be written using pa_stream_write() */
542 size_t pa_stream_writable_size(pa_stream *p);
543
544 /** Return the number of bytes that may be read using pa_stream_peek()*/
545 size_t pa_stream_readable_size(pa_stream *p);
546
547 /** Drain a playback stream. Use this for notification when the
548 * playback buffer is empty after playing all the audio in the buffer.
549 * Please note that only one drain operation per stream may be issued
550 * at a time. */
551 pa_operation* pa_stream_drain(pa_stream *s, pa_stream_success_cb_t cb, void *userdata);
552
553 /** Request a timing info structure update for a stream. Use
554 * pa_stream_get_timing_info() to get access to the raw timing data,
555 * or pa_stream_get_time() or pa_stream_get_latency() to get cleaned
556 * up values. */
557 pa_operation* pa_stream_update_timing_info(pa_stream *p, pa_stream_success_cb_t cb, void *userdata);
558
559 /** Set the callback function that is called whenever the state of the stream changes */
560 void pa_stream_set_state_callback(pa_stream *s, pa_stream_notify_cb_t cb, void *userdata);
561
562 /** Set the callback function that is called when new data may be
563 * written to the stream. */
564 void pa_stream_set_write_callback(pa_stream *p, pa_stream_request_cb_t cb, void *userdata);
565
566 /** Set the callback function that is called when new data is available from the stream.
567 * Return the number of bytes read.*/
568 void pa_stream_set_read_callback(pa_stream *p, pa_stream_request_cb_t cb, void *userdata);
569
570 /** Set the callback function that is called when a buffer overflow happens. (Only for playback streams) */
571 void pa_stream_set_overflow_callback(pa_stream *p, pa_stream_notify_cb_t cb, void *userdata);
572
573 /** Set the callback function that is called when a buffer underflow happens. (Only for playback streams) */
574 void pa_stream_set_underflow_callback(pa_stream *p, pa_stream_notify_cb_t cb, void *userdata);
575
576 /** Set the callback function that is called when a the server starts
577 * playback after an underrun or on initial startup. This only informs
578 * that audio is flowing again, it is no indication that audio started
579 * to reach the speakers already. (Only for playback streams). \since
580 * 0.9.11 */
581 void pa_stream_set_started_callback(pa_stream *p, pa_stream_notify_cb_t cb, void *userdata);
582
583 /** Set the callback function that is called whenever a latency
584 * information update happens. Useful on PA_STREAM_AUTO_TIMING_UPDATE
585 * streams only. (Only for playback streams) */
586 void pa_stream_set_latency_update_callback(pa_stream *p, pa_stream_notify_cb_t cb, void *userdata);
587
588 /** Set the callback function that is called whenever the stream is
589 * moved to a different sink/source. Use pa_stream_get_device_name()or
590 * pa_stream_get_device_index() to query the new sink/source. This
591 * notification is only generated when the server is at least
592 * 0.9.8. \since 0.9.8 */
593 void pa_stream_set_moved_callback(pa_stream *p, pa_stream_notify_cb_t cb, void *userdata);
594
595 /** Set the callback function that is called whenever the sink/source
596 * this stream is connected to is suspended or resumed. Use
597 * pa_stream_is_suspended() to query the new suspend status. Please
598 * note that the suspend status might also change when the stream is
599 * moved between devices. Thus if you call this function you very
600 * likely want to call pa_stream_set_moved_callback, too. This
601 * notification is only generated when the server is at least
602 * 0.9.8. \since 0.9.8 */
603 void pa_stream_set_suspended_callback(pa_stream *p, pa_stream_notify_cb_t cb, void *userdata);
604
605 /** Set the callback function that is called whenver a meta/policy
606 * control event is received.\since 0.9.15 */
607 void pa_stream_set_event_callback(pa_stream *p, pa_stream_event_cb_t cb, void *userdata);
608
609 /** Set the callback function that is called whenver the buffer
610 * attributes on the server side change. Please note that the buffer
611 * attributes can change when moving a stream to a different
612 * sink/source too, hence if you use this callback you should use
613 * pa_stream_set_moved_callback() as well. \since 0.9.15 */
614 void pa_stream_set_buffer_attr_callback(pa_stream *p, pa_stream_notify_cb_t cb, void *userdata);
615
616 /** Pause (or resume) playback of this stream temporarily. Available
617 * on both playback and recording streams. If b is 1 the stream is
618 * paused. If b is 0 the stream is resumed. The pause/resume operation
619 * is executed as quickly as possible. If a cork is very quickly
620 * followed by an uncork or the other way round this might not
621 * actually have any effect on the stream that is output. You can use
622 * pa_stream_is_corked() to find out whether the stream is currently
623 * paused or not. Normally a stream will be created in uncorked
624 * state. If you pass PA_STREAM_START_CORKED as flag during connection
625 * of the stream it will be created in corked state. */
626 pa_operation* pa_stream_cork(pa_stream *s, int b, pa_stream_success_cb_t cb, void *userdata);
627
628 /** Flush the playback buffer of this stream. This discards any audio
629 * in the buffer. Most of the time you're better off using the parameter
630 * delta of pa_stream_write() instead of this function. Available on both
631 * playback and recording streams. */
632 pa_operation* pa_stream_flush(pa_stream *s, pa_stream_success_cb_t cb, void *userdata);
633
634 /** Reenable prebuffering as specified in the pa_buffer_attr
635 * structure. Available for playback streams only. */
636 pa_operation* pa_stream_prebuf(pa_stream *s, pa_stream_success_cb_t cb, void *userdata);
637
638 /** Request immediate start of playback on this stream. This disables
639 * prebuffering as specified in the pa_buffer_attr structure,
640 * temporarily. Available for playback streams only. */
641 pa_operation* pa_stream_trigger(pa_stream *s, pa_stream_success_cb_t cb, void *userdata);
642
643 /** Rename the stream. */
644 pa_operation* pa_stream_set_name(pa_stream *s, const char *name, pa_stream_success_cb_t cb, void *userdata);
645
646 /** Return the current playback/recording time. This is based on the
647 * data in the timing info structure returned by
648 * pa_stream_get_timing_info().
649 *
650 * This function will usually only return new data if a timing info
651 * update has been recieved. Only if timing interpolation has been
652 * requested (PA_STREAM_INTERPOLATE_TIMING) the data from the last
653 * timing update is used for an estimation of the current
654 * playback/recording time based on the local time that passed since
655 * the timing info structure has been acquired.
656 *
657 * The time value returned by this function is guaranteed to increase
658 * monotonically. (that means: the returned value is always greater
659 * or equal to the value returned on the last call). This behaviour
660 * can be disabled by using PA_STREAM_NOT_MONOTONIC. This may be
661 * desirable to deal better with bad estimations of transport
662 * latencies, but may have strange effects if the application is not
663 * able to deal with time going 'backwards'.
664 *
665 * The time interpolator activated by PA_STREAM_INTERPOLATE_TIMING
666 * favours 'smooth' time graphs over accurate ones to improve the
667 * smoothness of UI operations that are tied to the audio clock. If
668 * accuracy is more important to you you might need to estimate your
669 * timing based on the data from pa_stream_get_timing_info() yourself
670 * or not work with interpolated timing at all and instead always
671 * query on the server side for the most up to date timing with
672 * pa_stream_update_timing_info().
673 *
674 * If no timing information has been
675 * recieved yet this call will return PA_ERR_NODATA. For more details
676 * see pa_stream_get_timing_info(). */
677 int pa_stream_get_time(pa_stream *s, pa_usec_t *r_usec);
678
679 /** Return the total stream latency. This function is based on
680 * pa_stream_get_time().
681 *
682 * In case the stream is a monitoring stream the result can be
683 * negative, i.e. the captured samples are not yet played. In this
684 * case *negative is set to 1.
685 *
686 * If no timing information has been recieved yet this call will
687 * return PA_ERR_NODATA. For more details see
688 * pa_stream_get_timing_info() and pa_stream_get_time(). */
689 int pa_stream_get_latency(pa_stream *s, pa_usec_t *r_usec, int *negative);
690
691 /** Return the latest raw timing data structure. The returned pointer
692 * points to an internal read-only instance of the timing
693 * structure. The user should make a copy of this structure if he
694 * wants to modify it. An in-place update to this data structure may
695 * be requested using pa_stream_update_timing_info().
696 *
697 * If no timing information has been received before (i.e. by
698 * requesting pa_stream_update_timing_info() or by using
699 * PA_STREAM_AUTO_TIMING_UPDATE), this function will fail with
700 * PA_ERR_NODATA.
701 *
702 * Please note that the write_index member field (and only this field)
703 * is updated on each pa_stream_write() call, not just when a timing
704 * update has been recieved. */
705 const pa_timing_info* pa_stream_get_timing_info(pa_stream *s);
706
707 /** Return a pointer to the stream's sample specification. */
708 const pa_sample_spec* pa_stream_get_sample_spec(pa_stream *s);
709
710 /** Return a pointer to the stream's channel map. */
711 const pa_channel_map* pa_stream_get_channel_map(pa_stream *s);
712
713 /** Return a pointer to the stream's format \since 1.0 */
714 const pa_format_info* pa_stream_get_format_info(pa_stream *s);
715
716 /** Return the per-stream server-side buffer metrics of the
717 * stream. Only valid after the stream has been connected successfuly
718 * and if the server is at least PulseAudio 0.9. This will return the
719 * actual configured buffering metrics, which may differ from what was
720 * requested during pa_stream_connect_record() or
721 * pa_stream_connect_playback(). This call will always return the
722 * actually per-stream server-side buffer metrics, regardless whether
723 * PA_STREAM_ADJUST_LATENCY is set or not. \since 0.9.0 */
724 const pa_buffer_attr* pa_stream_get_buffer_attr(pa_stream *s);
725
726 /** Change the buffer metrics of the stream during playback. The
727 * server might have chosen different buffer metrics then
728 * requested. The selected metrics may be queried with
729 * pa_stream_get_buffer_attr() as soon as the callback is called. Only
730 * valid after the stream has been connected successfully and if the
731 * server is at least PulseAudio 0.9.8. Please be aware of the
732 * slightly different semantics of the call depending whether
733 * PA_STREAM_ADJUST_LATENCY is set or not. \since 0.9.8 */
734 pa_operation *pa_stream_set_buffer_attr(pa_stream *s, const pa_buffer_attr *attr, pa_stream_success_cb_t cb, void *userdata);
735
736 /** Change the stream sampling rate during playback. You need to pass
737 * PA_STREAM_VARIABLE_RATE in the flags parameter of
738 * pa_stream_connect_...() if you plan to use this function. Only valid
739 * after the stream has been connected successfully and if the server
740 * is at least PulseAudio 0.9.8. \since 0.9.8 */
741 pa_operation *pa_stream_update_sample_rate(pa_stream *s, uint32_t rate, pa_stream_success_cb_t cb, void *userdata);
742
743 /** Update the property list of the sink input/source output of this
744 * stream, adding new entries. Please note that it is highly
745 * recommended to set as much properties initially via
746 * pa_stream_new_with_proplist() as possible instead a posteriori with
747 * this function, since that information may then be used to route
748 * this stream to the right device. \since 0.9.11 */
749 pa_operation *pa_stream_proplist_update(pa_stream *s, pa_update_mode_t mode, pa_proplist *p, pa_stream_success_cb_t cb, void *userdata);
750
751 /** Update the property list of the sink input/source output of this
752 * stream, remove entries. \since 0.9.11 */
753 pa_operation *pa_stream_proplist_remove(pa_stream *s, const char *const keys[], pa_stream_success_cb_t cb, void *userdata);
754
755 /** For record streams connected to a monitor source: monitor only a
756 * very specific sink input of the sink. Thus function needs to be
757 * called before pa_stream_connect_record() is called. \since
758 * 0.9.11 */
759 int pa_stream_set_monitor_stream(pa_stream *s, uint32_t sink_input_idx);
760
761 /** Return the sink input index previously set with
762 * pa_stream_set_monitor_stream().
763 * \since 0.9.11 */
764 uint32_t pa_stream_get_monitor_stream(pa_stream *s);
765
766 PA_C_DECL_END
767
768 #endif