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