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