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