]> code.delx.au - pulseaudio/blob - src/polyp/stream.h
Documentation updates
[pulseaudio] / src / polyp / stream.h
1 #ifndef foostreamhfoo
2 #define foostreamhfoo
3
4 /* $Id$ */
5
6 /***
7 This file is part of polypaudio.
8
9 polypaudio is free software; you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as published
11 by the Free Software Foundation; either version 2 of the License,
12 or (at your option) any later version.
13
14 polypaudio is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public License
20 along with polypaudio; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 USA.
23 ***/
24
25 #include <sys/types.h>
26
27 #include <polyp/sample.h>
28 #include <polyp/channelmap.h>
29 #include <polyp/volume.h>
30 #include <polyp/def.h>
31 #include <polyp/cdecl.h>
32 #include <polyp/operation.h>
33
34 /** \page streams Audio streams
35 *
36 * \section overv_sec Overview
37 *
38 * Audio streams form the central functionality of the sound server. Data is
39 * routed, converted and mixed from several sources before it is passed along
40 * to a final output. Currently, there are three forms of audio streams:
41 *
42 * \li Playback streams - Data flows from the client to the server.
43 * \li Record streams - Data flows from the server to the client.
44 * \li Upload streams - Similar to playback streams, but the data is stored in
45 * the sample cache. See \ref scache for more information
46 * about controlling the sample cache.
47 *
48 * \section create_sec Creating
49 *
50 * To access a stream, a pa_stream object must be created using
51 * pa_stream_new(). At this point the audio sample format and mapping of
52 * channels must be specified. See \ref sample and \ref channelmap for more
53 * information about those structures.
54 *
55 * This first step will only create a client-side object, representing the
56 * stream. To use the stream, a server-side object must be created and
57 * associated with the local object. Depending on which type of stream is
58 * desired, a different function is needed:
59 *
60 * \li Playback stream - pa_stream_connect_playback()
61 * \li Record stream - pa_stream_connect_record()
62 * \li Upload stream - pa_stream_connect_upload() (see \ref scache)
63 *
64 * Similar to how connections are done in contexts, connecting a stream will
65 * not generate a pa_operation object. Also like contexts, the application
66 * should register a state change callback, using
67 * pa_stream_set_state_callback(), and wait for the stream to enter an active
68 * state.
69 *
70 * \subsection bufattr_subsec Buffer attributes
71 *
72 * Playback and record streams always have a server side buffer as part of the data flow.
73 * The size of this buffer strikes a compromise between low latency and
74 * sensitivity for buffer overflows/underruns.
75 *
76 * The buffer is described with a pa_buffer_attr structure which contains a
77 * number of field:
78 *
79 * \li maxlength - The absolute maximum number of bytes that can be stored in
80 * the buffer. If this value is exceeded then data will be
81 * lost.
82 * \li tlength - The target length of a playback buffer. The server will only
83 * send requests for more data as long as the buffer has less
84 * than this number of bytes of data.
85 * \li prebuf - Number of bytes that need to be in the buffer before playback
86 * will commence. Start of playback can be forced using
87 * pa_stream_trigger() even though the prebuffer size hasn't been
88 * reached.
89 * \li minreq - Minimum free number of the bytes in the playback buffer before
90 * the server will request more data.
91 * \li fragsize - Maximum number of bytes that the server will push in one
92 * chunk for record streams.
93 *
94 * \section transfer_sec Transferring data
95 *
96 * Once the stream is up, data can start flowing between the client and the
97 * server. Two different access models can be used to transfer the data:
98 *
99 * \li Asynchronous - The application register a callback using
100 * pa_stream_set_write_callback() and
101 * pa_stream_set_read_callback() to receive notifications
102 * that data can either be written or read.
103 * \li Polled - Query the library for available data/space using
104 * pa_stream_writable_size() and pa_stream_readable_size() and
105 * transfer data as needed. The sizes are stored locally, in the
106 * client end, so there is no delay when reading them.
107 *
108 * It is also possible to mix the two models freely.
109 *
110 * Once there is data/space available, it can be transferred using either
111 * pa_stream_write() for playback, or pa_stream_peek() / pa_stream_drop() for
112 * record. Make sure you do not overflow the playback buffers as data will be
113 * dropped.
114 *
115 * \section bufctl_sec Buffer control
116 *
117 * The transfer buffers can be controlled through a number of operations:
118 *
119 * \li pa_stream_cork() - Start or stop the playback or recording.
120 * \li pa_stream_trigger() - Start playback immediatly and do not wait for
121 * the buffer to fill up to the set trigger level.
122 * \li pa_stream_prebuf() - Reenable the playback trigger level.
123 * \li pa_stream_drain() - Wait for the playback buffer to go empty. Will
124 * return a pa_operation object that will indicate when
125 * the buffer is completely drained.
126 * \li pa_stream_flush() - Drop all data from the playback buffer and do not
127 * wait for it to finish playing.
128 *
129 * \section latency_sec Latency
130 *
131 * A major problem with networked audio is the increased latency caused by
132 * the network. To remedy this, Polypaudio supports an advanced system of
133 * monitoring the current latency.
134 *
135 * To get the raw data needed to calculate latencies, call
136 * pa_stream_get_timing_info(). This will give you a pa_timing_info
137 * structure that contains everything that is known about buffers,
138 * transport delays and the backend active in the server.
139 *
140 * This structure is updated every time a
141 * pa_stream_update_timing_info() operation is executed. (i.e. before
142 * the first call to this function the timing information structure is
143 * not available!) Since it is a lot of work to keep this structure
144 * up-to-date manually, Polypaudio can do that automatically for you:
145 * if PA_STREAM_AUTO_TIMING_UPDATE is passed when connecting the
146 * stream Polypaudio will automatically update the structure every
147 * 100ms and every time a function is called that might invalidate the
148 * previously known timing data (such as pa_stream_write() or
149 * pa_stream_flush()). Please note however, that there always is a
150 * short time window when the data in the timing information structure
151 * is out-of-date. Polypaudio tries to mark these situations by
152 * setting the write_index_corrupt and read_index_corrupt fields
153 * accordingly.
154 *
155 * The raw timing data in the pa_timing_info structure is usually hard
156 * to deal with. Therefore a more simplistic interface is available:
157 * you can call pa_stream_get_time() or pa_stream_get_latency(). The
158 * former will return the current playback time of the hardware since
159 * the stream has been started. The latter returns the time a sample
160 * that you write now takes to be played by the hardware.
161 *
162 * Since updating the timing info structure usually requires a full
163 * round trip and some applications monitor the timing very often
164 * Polypaudio offers a timing interpolation system. If
165 * PA_STREAM_INTERPOLATE_TIMING is passed when connecting the stream,
166 * pa_stream_get_time() and pa_stream_get_latency() will try to
167 * interpolate the current playback time/latency by estimating the
168 * number of samples that have been played back by the hardware since
169 * the last regular timing update. It is espcially useful to combine
170 * this option with PA_STREAM_AUTO_TIMING_UPDATE, which will enable
171 * you to monitor the current playback time/latency very precisely
172 * without requiring a network round trip every time.
173 *
174 * \section flow_sec Overflow and underflow
175 *
176 * Even with the best precautions, buffers will sometime over - or underflow.
177 * To handle this gracefully, the application can be notified when this
178 * happens. Callbacks are registered using pa_stream_set_overflow_callback()
179 * and pa_stream_set_underflow_callback().
180 *
181 * \section sync_streams Sychronizing Multiple Playback Streams.
182 *
183 * Polypaudio allows applications to fully synchronize multiple playback
184 * streams that are connected to the same output device. That means
185 * the streams will always be played back sample-by-sample
186 * synchronously. If stream operations like pa_stream_cork() are
187 * issued on one of the synchronized streams, they are simultaneously
188 * issued on the others.
189 *
190 * To synchronize a stream to another, just pass the "master" stream
191 * as last argument to pa_stream_connect_playack(). To make sure that
192 * the freshly created stream doesn't start playback right-away, make
193 * sure to pass PA_STREAM_START_CORKED and - after all streams have
194 * been created - uncork them all with a single call to
195 * pa_stream_cork() for the master stream.
196 *
197 * To make sure that a particular stream doesn't stop to play when a
198 * server side buffer underrun happens on it while the other
199 * synchronized streams continue playing and hence deviate you need to
200 * pass a "prebuf" pa_buffer_attr of 0 when connecting it.
201 *
202 * \section seek_modes Seeking in the Playback Buffer
203 *
204 * T.B.D
205 * \section disc_sec Disconnecting
206 *
207 * When a stream has served is purpose it must be disconnected with
208 * pa_stream_disconnect(). If you only unreference it, then it will live on
209 * and eat resources both locally and on the server until you disconnect the
210 * context.
211 *
212 */
213
214 /** \file
215 * Audio streams for input, output and sample upload */
216
217 PA_C_DECL_BEGIN
218
219 /** An opaque stream for playback or recording */
220 typedef struct pa_stream pa_stream;
221
222 /** A generic callback for operation completion */
223 typedef void (*pa_stream_success_cb_t) (pa_stream*s, int success, void *userdata);
224
225 /** A generic free callback */
226 typedef void (*pa_free_cb_t)(void *p);
227
228 /** A generic request callback */
229 typedef void (*pa_stream_request_cb_t)(pa_stream *p, size_t length, void *userdata);
230
231 /** A generic notification callback */
232 typedef void (*pa_stream_notify_cb_t)(pa_stream *p, void *userdata);
233
234 /** Create a new, unconnected stream with the specified name and sample type */
235 pa_stream* pa_stream_new(
236 pa_context *c /**< The context to create this stream in */,
237 const char *name /**< A name for this stream */,
238 const pa_sample_spec *ss /**< The desired sample format */,
239 const pa_channel_map *map /**< The desired channel map, or NULL for default */);
240
241 /** Decrease the reference counter by one */
242 void pa_stream_unref(pa_stream *s);
243
244 /** Increase the reference counter by one */
245 pa_stream *pa_stream_ref(pa_stream *s);
246
247 /** Return the current state of the stream */
248 pa_stream_state_t pa_stream_get_state(pa_stream *p);
249
250 /** Return the context this stream is attached to */
251 pa_context* pa_stream_get_context(pa_stream *p);
252
253 /** Return the device (sink input or source output) index this stream is connected to */
254 uint32_t pa_stream_get_index(pa_stream *s);
255
256 /** Connect the stream to a sink */
257 int pa_stream_connect_playback(
258 pa_stream *s /**< The stream to connect to a sink */,
259 const char *dev /**< Name of the sink to connect to, or NULL for default */ ,
260 const pa_buffer_attr *attr /**< Buffering attributes, or NULL for default */,
261 pa_stream_flags_t flags /**< Additional flags, or 0 for default */,
262 pa_cvolume *volume /**< Initial volume, or NULL for default */,
263 pa_stream *sync_stream /**< Synchronize this stream with the specified one, or NULL for a standalone stream*/);
264
265 /** Connect the stream to a source */
266 int pa_stream_connect_record(
267 pa_stream *s /**< The stream to connect to a source */ ,
268 const char *dev /**< Name of the source to connect to, or NULL for default */,
269 const pa_buffer_attr *attr /**< Buffer attributes, or NULL for default */,
270 pa_stream_flags_t flags /**< Additional flags, or 0 for default */);
271
272 /** Disconnect a stream from a source/sink */
273 int pa_stream_disconnect(pa_stream *s);
274
275 /** Write some data to the server (for playback sinks), if free_cb is
276 * non-NULL this routine is called when all data has been written out
277 * and an internal reference to the specified data is kept, the data
278 * is not copied. If NULL, the data is copied into an internal
279 * buffer. The client my freely seek around in the output buffer. For
280 * most applications passing 0 and PA_SEEK_RELATIVE as arguments for
281 * offset and seek should be useful.*/
282 int pa_stream_write(
283 pa_stream *p /**< The stream to use */,
284 const void *data /**< The data to write */,
285 size_t length /**< The length of the data to write */,
286 pa_free_cb_t free_cb /**< A cleanup routine for the data or NULL to request an internal copy */,
287 int64_t offset, /**< Offset for seeking, must be 0 for upload streams */
288 pa_seek_mode_t seek /**< Seek mode, must be PA_SEEK_RELATIVE for upload streams */);
289
290 /** Read the next fragment from the buffer (for recording).
291 * data will point to the actual data and length will contain the size
292 * of the data in bytes (which can be less than a complete framgnet).
293 * Use pa_stream_drop() to actually remove the data from the
294 * buffer. If no data is available will return a NULL pointer \since 0.8 */
295 int pa_stream_peek(
296 pa_stream *p /**< The stream to use */,
297 const void **data /**< Pointer to pointer that will point to data */,
298 size_t *length /**< The length of the data read */);
299
300 /** Remove the current fragment. It is invalid to do this without first
301 * calling pa_stream_peek(). \since 0.8 */
302 int pa_stream_drop(pa_stream *p);
303
304 /** Return the nember of bytes that may be written using pa_stream_write() */
305 size_t pa_stream_writable_size(pa_stream *p);
306
307 /** Return the number of bytes that may be read using pa_stream_read() \since 0.8 */
308 size_t pa_stream_readable_size(pa_stream *p);
309
310 /** Drain a playback stream. Use this for notification when the buffer is empty */
311 pa_operation* pa_stream_drain(pa_stream *s, pa_stream_success_cb_t cb, void *userdata);
312
313 /** Request a timing info structure update for a stream. Use
314 * pa_stream_get_timing_info() to get access to the raw timing data,
315 * or pa_stream_get_time() or pa_stream_get_latency() to get cleaned
316 * up values. */
317 pa_operation* pa_stream_update_timing_info(pa_stream *p, pa_stream_success_cb_t cb, void *userdata);
318
319 /** Set the callback function that is called whenever the state of the stream changes */
320 void pa_stream_set_state_callback(pa_stream *s, pa_stream_notify_cb_t cb, void *userdata);
321
322 /** Set the callback function that is called when new data may be
323 * written to the stream. */
324 void pa_stream_set_write_callback(pa_stream *p, pa_stream_request_cb_t cb, void *userdata);
325
326 /** Set the callback function that is called when new data is available from the stream.
327 * Return the number of bytes read. \since 0.8 */
328 void pa_stream_set_read_callback(pa_stream *p, pa_stream_request_cb_t cb, void *userdata);
329
330 /** Set the callback function that is called when a buffer overflow happens. (Only for playback streams) \since 0.8 */
331 void pa_stream_set_overflow_callback(pa_stream *p, pa_stream_notify_cb_t cb, void *userdata);
332
333 /** Set the callback function that is called when a buffer underflow happens. (Only for playback streams) \since 0.8 */
334 void pa_stream_set_underflow_callback(pa_stream *p, pa_stream_notify_cb_t cb, void *userdata);
335
336 /** Pause (or resume) playback of this stream temporarily. Available on both playback and recording streams. \since 0.3 */
337 pa_operation* pa_stream_cork(pa_stream *s, int b, pa_stream_success_cb_t cb, void *userdata);
338
339 /** Flush the playback buffer of this stream. Most of the time you're
340 * better off using the parameter delta of pa_stream_write() instead of this
341 * function. Available on both playback and recording streams. \since 0.3 */
342 pa_operation* pa_stream_flush(pa_stream *s, pa_stream_success_cb_t cb, void *userdata);
343
344 /** Reenable prebuffering as specified in the pa_buffer_attr
345 * structure. Available for playback streams only. \since 0.6 */
346 pa_operation* pa_stream_prebuf(pa_stream *s, pa_stream_success_cb_t cb, void *userdata);
347
348 /** Request immediate start of playback on this stream. This disables
349 * prebuffering as specified in the pa_buffer_attr
350 * structure, temporarily. Available for playback streams only. \since 0.3 */
351 pa_operation* pa_stream_trigger(pa_stream *s, pa_stream_success_cb_t cb, void *userdata);
352
353 /** Rename the stream. \since 0.5 */
354 pa_operation* pa_stream_set_name(pa_stream *s, const char *name, pa_stream_success_cb_t cb, void *userdata);
355
356 /** Return the current playback/recording time. This is based on the
357 * data in the timing info structure returned by
358 * pa_stream_get_timing_info(). This function will usually only return
359 * new data if a timing info update has been recieved. Only if timing
360 * interpolation has been requested (PA_STREAM_INTERPOLATE_TIMING)
361 * the data from the last timing update is used for an estimation of
362 * the current playback/recording time based on the local time that
363 * passed since the timing info structure has been acquired. The time
364 * value returned by this function is guaranteed to increase
365 * monotonically. (that means: the returned value is always greater or
366 * equal to the value returned on the last call) This behaviour can
367 * be disabled by using PA_STREAM_NOT_MONOTONOUS. This may be
368 * desirable to deal better with bad estimations of transport
369 * latencies, but may have strange effects if the application is not
370 * able to deal with time going 'backwards'. \since 0.6 */
371 int pa_stream_get_time(pa_stream *s, pa_usec_t *r_usec);
372
373 /** Return the total stream latency. This function is based on
374 * pa_stream_get_time(). In case the stream is a monitoring stream the
375 * result can be negative, i.e. the captured samples are not yet
376 * played. In this case *negative is set to 1. \since 0.6 */
377 int pa_stream_get_latency(pa_stream *s, pa_usec_t *r_usec, int *negative);
378
379 /** Return the latest raw timing data structure. The returned pointer
380 * points to an internal read-only instance of the timing
381 * structure. The user should make a copy of this structure if he
382 * wants to modify it. An in-place update to this data structure may
383 * be requested using pa_stream_update_timing_info(). If no
384 * pa_stream_update_timing_info() call was issued before, this
385 * function will fail with PA_ERR_NODATA. Please note that the
386 * write_index member field (and only this field) is updated on each
387 * pa_stream_write() call, not just when a timing update has been
388 * recieved. \since 0.8 */
389 const pa_timing_info* pa_stream_get_timing_info(pa_stream *s);
390
391 /** Return a pointer to the stream's sample specification. \since 0.6 */
392 const pa_sample_spec* pa_stream_get_sample_spec(pa_stream *s);
393
394 /** Return a pointer to the stream's channel map. \since 0.8 */
395 const pa_channel_map* pa_stream_get_channel_map(pa_stream *s);
396
397 PA_C_DECL_END
398
399 #endif