big s/polyp/pulse/g
[platform/upstream/pulseaudio.git] / src / pulse / stream.h
1 #ifndef foostreamhfoo
2 #define foostreamhfoo
3
4 /* $Id$ */
5
6 /***
7   This file is part of PulseAudio.
8  
9   PulseAudio 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   PulseAudio 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 PulseAudio; 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 <pulse/sample.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
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
73  * part of the data flow.  The size of this buffer strikes a
74  * compromise between low latency and sensitivity for buffer
75  * overflows/underruns.
76  *
77  * The buffer metrics may be controlled by the application. They are
78  * described with a pa_buffer_attr structure which contains a number
79  * of fields:
80  *
81  * \li maxlength - The absolute maximum number of bytes that can be stored in
82  *                 the buffer. If this value is exceeded then data will be
83  *                 lost.
84  * \li tlength - The target length of a playback buffer. The server will only
85  *               send requests for more data as long as the buffer has less
86  *               than this number of bytes of data.
87  * \li prebuf - Number of bytes that need to be in the buffer before
88  * playback will commence. Start of playback can be forced using
89  * pa_stream_trigger() even though the prebuffer size hasn't been
90  * reached. If a buffer underrun occurs, this prebuffering will be
91  * again enabled. If the playback shall never stop in case of a buffer
92  * underrun, this value should be set to 0. In that case the read
93  * index of the output buffer overtakes the write index, and hence the
94  * fill level of the buffer is negative.
95  * \li minreq - Minimum free number of the bytes in the playback buffer before
96  *              the server will request more data.
97  * \li fragsize - Maximum number of bytes that the server will push in one
98  *                chunk for record streams.
99  *
100  * The server side playback buffers are indexed by a write and a read
101  * index. The application writes to the write index and the sound
102  * device reads from the read index. The read index is increased
103  * monotonically, while the write index may be freely controlled by
104  * the application. Substracting the read index from the write index
105  * will give you the current fill level of the buffer. The read/write
106  * indexes are 64bit values and measured in bytes, they will never
107  * wrap. The current read/write index may be queried using
108  * pa_stream_get_timing_info() (see below for more information). In
109  * case of a buffer underrun the read index is equal or larger than
110  * the write index. Unless the prebuf value is 0, Polypaudio will
111  * temporarily pause playback in such a case, and wait until the
112  * buffer is filled up to prebuf bytes again. If prebuf is 0, the
113  * read index may be larger than the write index, in which case
114  * silence is played. If the application writes data to indexes lower
115  * than the read index, the data is immediately lost.
116  *                
117  * \section transfer_sec Transferring Data
118  *
119  * Once the stream is up, data can start flowing between the client and the
120  * server. Two different access models can be used to transfer the data:
121  *
122  * \li Asynchronous - The application register a callback using
123  *                    pa_stream_set_write_callback() and
124  *                    pa_stream_set_read_callback() to receive notifications
125  *                    that data can either be written or read.
126  * \li Polled - Query the library for available data/space using
127  *              pa_stream_writable_size() and pa_stream_readable_size() and
128  *              transfer data as needed. The sizes are stored locally, in the
129  *              client end, so there is no delay when reading them.
130  *
131  * It is also possible to mix the two models freely.
132  *
133  * Once there is data/space available, it can be transferred using either
134  * pa_stream_write() for playback, or pa_stream_peek() / pa_stream_drop() for
135  * record. Make sure you do not overflow the playback buffers as data will be
136  * dropped.
137  *
138  * \section bufctl_sec Buffer Control
139  *
140  * The transfer buffers can be controlled through a number of operations:
141  *
142  * \li pa_stream_cork() - Start or stop the playback or recording.
143  * \li pa_stream_trigger() - Start playback immediatly and do not wait for
144  *                           the buffer to fill up to the set trigger level.
145  * \li pa_stream_prebuf() - Reenable the playback trigger level.
146  * \li pa_stream_drain() - Wait for the playback buffer to go empty. Will
147  *                         return a pa_operation object that will indicate when
148  *                         the buffer is completely drained.
149  * \li pa_stream_flush() - Drop all data from the playback buffer and do not
150  *                         wait for it to finish playing.
151  *
152  * \section seek_modes Seeking in the Playback Buffer
153  *
154  * A client application may freely seek in the playback buffer. To
155  * accomplish that the pa_stream_write() function takes a seek mode
156  * and an offset argument. The seek mode is one of:
157  *
158  * \li PA_SEEK_RELATIVE - seek relative to the current write index
159  * \li PA_SEEK_ABSOLUTE - seek relative to the beginning of the playback buffer, (i.e. the first that was ever played in the stream)
160  * \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
161  * \li PA_SEEK_RELATIVE_END - seek relative to the last byte ever written.
162  * 
163  * If an application just wants to append some data to the output
164  * buffer, PA_SEEK_RELATIVE and an offset of 0 should be used.
165  *
166  * After a call to pa_stream_write() the write index will be left at
167  * the position right after the last byte of the written data.
168  *                         
169  * \section latency_sec Latency
170  *
171  * A major problem with networked audio is the increased latency caused by
172  * the network. To remedy this, Polypaudio supports an advanced system of
173  * monitoring the current latency.
174  *
175  * To get the raw data needed to calculate latencies, call
176  * pa_stream_get_timing_info(). This will give you a pa_timing_info
177  * structure that contains everything that is known about the server
178  * side buffer transport delays and the backend active in the
179  * server. (Besides other things it contains the write and read index
180  * values mentioned above.)
181  *
182  * This structure is updated every time a
183  * pa_stream_update_timing_info() operation is executed. (i.e. before
184  * the first call to this function the timing information structure is
185  * not available!) Since it is a lot of work to keep this structure
186  * up-to-date manually, Polypaudio can do that automatically for you:
187  * if PA_STREAM_AUTO_TIMING_UPDATE is passed when connecting the
188  * stream Polypaudio will automatically update the structure every
189  * 100ms and every time a function is called that might invalidate the
190  * previously known timing data (such as pa_stream_write() or
191  * pa_stream_flush()). Please note however, that there always is a
192  * short time window when the data in the timing information structure
193  * is out-of-date. Polypaudio tries to mark these situations by
194  * setting the write_index_corrupt and read_index_corrupt fields
195  * accordingly.
196  *
197  * The raw timing data in the pa_timing_info structure is usually hard
198  * to deal with. Therefore a more simplistic interface is available:
199  * you can call pa_stream_get_time() or pa_stream_get_latency(). The
200  * former will return the current playback time of the hardware since
201  * the stream has been started. The latter returns the time a sample
202  * that you write now takes to be played by the hardware. These two
203  * functions base their calculations on the same data that is returned
204  * by pa_stream_get_timing_info(). Hence the same rules for keeping
205  * the timing data up-to-date apply here. In case the write or read
206  * index is corrupted, these two functions will fail with
207  * PA_ERR_NODATA set.
208  *
209  * Since updating the timing info structure usually requires a full
210  * network round trip and some applications monitor the timing very
211  * often Polypaudio offers a timing interpolation system. If
212  * PA_STREAM_INTERPOLATE_TIMING is passed when connecting the stream,
213  * pa_stream_get_time() and pa_stream_get_latency() will try to
214  * interpolate the current playback time/latency by estimating the
215  * number of samples that have been played back by the hardware since
216  * the last regular timing update. It is espcially useful to combine
217  * this option with PA_STREAM_AUTO_TIMING_UPDATE, which will enable
218  * you to monitor the current playback time/latency very precisely and
219  * very frequently without requiring a network round trip every time.
220  * 
221  * \section flow_sec Overflow and underflow
222  *
223  * Even with the best precautions, buffers will sometime over - or
224  * underflow.  To handle this gracefully, the application can be
225  * notified when this happens. Callbacks are registered using
226  * pa_stream_set_overflow_callback() and
227  * pa_stream_set_underflow_callback().
228  *
229  * \section sync_streams Sychronizing Multiple Playback Streams
230  *
231  * Polypaudio allows applications to fully synchronize multiple
232  * playback streams that are connected to the same output device. That
233  * means the streams will always be played back sample-by-sample
234  * synchronously. If stream operations like pa_stream_cork() are
235  * issued on one of the synchronized streams, they are simultaneously
236  * issued on the others.
237  *
238  * To synchronize a stream to another, just pass the "master" stream
239  * as last argument to pa_stream_connect_playack(). To make sure that
240  * the freshly created stream doesn't start playback right-away, make
241  * sure to pass PA_STREAM_START_CORKED and - after all streams have
242  * been created - uncork them all with a single call to
243  * pa_stream_cork() for the master stream.
244  *
245  * To make sure that a particular stream doesn't stop to play when a
246  * server side buffer underrun happens on it while the other
247  * synchronized streams continue playing and hence deviate you need to
248  * pass a "prebuf" pa_buffer_attr of 0 when connecting it.
249  *
250  * \section disc_sec Disconnecting
251  *
252  * When a stream has served is purpose it must be disconnected with
253  * pa_stream_disconnect(). If you only unreference it, then it will live on
254  * and eat resources both locally and on the server until you disconnect the
255  * context.
256  *
257  */
258
259 /** \file
260  * Audio streams for input, output and sample upload */
261
262 PA_C_DECL_BEGIN
263
264 /** An opaque stream for playback or recording */
265 typedef struct pa_stream pa_stream;
266
267 /** A generic callback for operation completion */
268 typedef void (*pa_stream_success_cb_t) (pa_stream*s, int success, void *userdata);
269
270 /** A generic request callback */
271 typedef void (*pa_stream_request_cb_t)(pa_stream *p, size_t length, void *userdata);
272
273 /** A generic notification callback */
274 typedef void (*pa_stream_notify_cb_t)(pa_stream *p, void *userdata);
275
276 /** Create a new, unconnected stream with the specified name and sample type */
277 pa_stream* pa_stream_new(
278         pa_context *c                     /**< The context to create this stream in */,             
279         const char *name                  /**< A name for this stream */,
280         const pa_sample_spec *ss          /**< The desired sample format */,
281         const pa_channel_map *map         /**< The desired channel map, or NULL for default */);
282
283 /** Decrease the reference counter by one */
284 void pa_stream_unref(pa_stream *s);
285
286 /** Increase the reference counter by one */
287 pa_stream *pa_stream_ref(pa_stream *s);
288
289 /** Return the current state of the stream */
290 pa_stream_state_t pa_stream_get_state(pa_stream *p);
291
292 /** Return the context this stream is attached to */
293 pa_context* pa_stream_get_context(pa_stream *p);
294
295 /** Return the device (sink input or source output) index this stream is connected to */
296 uint32_t pa_stream_get_index(pa_stream *s);
297
298 /** Connect the stream to a sink */
299 int pa_stream_connect_playback(
300         pa_stream *s                  /**< The stream to connect to a sink */,
301         const char *dev               /**< Name of the sink to connect to, or NULL for default */ ,
302         const pa_buffer_attr *attr    /**< Buffering attributes, or NULL for default */,
303         pa_stream_flags_t flags       /**< Additional flags, or 0 for default */,
304         pa_cvolume *volume            /**< Initial volume, or NULL for default */,
305         pa_stream *sync_stream        /**< Synchronize this stream with the specified one, or NULL for a standalone stream*/);
306
307 /** Connect the stream to a source */
308 int pa_stream_connect_record(
309         pa_stream *s                  /**< The stream to connect to a source */ ,
310         const char *dev               /**< Name of the source to connect to, or NULL for default */,
311         const pa_buffer_attr *attr    /**< Buffer attributes, or NULL for default */,
312         pa_stream_flags_t flags       /**< Additional flags, or 0 for default */);
313
314 /** Disconnect a stream from a source/sink */
315 int pa_stream_disconnect(pa_stream *s);
316
317 /** Write some data to the server (for playback sinks), if free_cb is
318  * non-NULL this routine is called when all data has been written out
319  * and an internal reference to the specified data is kept, the data
320  * is not copied. If NULL, the data is copied into an internal
321  * buffer. The client my freely seek around in the output buffer. For
322  * most applications passing 0 and PA_SEEK_RELATIVE as arguments for
323  * offset and seek should be useful.*/ 
324 int pa_stream_write(
325         pa_stream *p             /**< The stream to use */,
326         const void *data         /**< The data to write */,
327         size_t length            /**< The length of the data to write */,
328         pa_free_cb_t free_cb     /**< A cleanup routine for the data or NULL to request an internal copy */,
329         int64_t offset,          /**< Offset for seeking, must be 0 for upload streams */
330         pa_seek_mode_t seek      /**< Seek mode, must be PA_SEEK_RELATIVE for upload streams */);
331
332 /** Read the next fragment from the buffer (for recording).
333  * data will point to the actual data and length will contain the size
334  * of the data in bytes (which can be less than a complete framgnet).
335  * Use pa_stream_drop() to actually remove the data from the
336  * buffer. If no data is available will return a NULL pointer  \since 0.8 */ 
337 int pa_stream_peek(
338         pa_stream *p                 /**< The stream to use */,
339         const void **data            /**< Pointer to pointer that will point to data */,
340         size_t *length              /**< The length of the data read */);
341
342 /** Remove the current fragment on record streams. It is invalid to do this without first
343  * calling pa_stream_peek(). \since 0.8 */
344 int pa_stream_drop(pa_stream *p);
345
346 /** Return the nember of bytes that may be written using pa_stream_write() */
347 size_t pa_stream_writable_size(pa_stream *p);
348
349 /** Return the number of bytes that may be read using pa_stream_read() \since 0.8 */
350 size_t pa_stream_readable_size(pa_stream *p);
351
352 /** Drain a playback stream. Use this for notification when the buffer is empty */
353 pa_operation* pa_stream_drain(pa_stream *s, pa_stream_success_cb_t cb, void *userdata);
354
355 /** Request a timing info structure update for a stream. Use
356  * pa_stream_get_timing_info() to get access to the raw timing data,
357  * or pa_stream_get_time() or pa_stream_get_latency() to get cleaned
358  * up values. */
359 pa_operation* pa_stream_update_timing_info(pa_stream *p, pa_stream_success_cb_t cb, void *userdata);
360
361 /** Set the callback function that is called whenever the state of the stream changes */
362 void pa_stream_set_state_callback(pa_stream *s, pa_stream_notify_cb_t cb, void *userdata);
363
364 /** Set the callback function that is called when new data may be
365  * written to the stream. */
366 void pa_stream_set_write_callback(pa_stream *p, pa_stream_request_cb_t cb, void *userdata);
367
368 /** Set the callback function that is called when new data is available from the stream.
369  * Return the number of bytes read. \since 0.8 */
370 void pa_stream_set_read_callback(pa_stream *p, pa_stream_request_cb_t cb, void *userdata);
371
372 /** Set the callback function that is called when a buffer overflow happens. (Only for playback streams) \since 0.8 */
373 void pa_stream_set_overflow_callback(pa_stream *p, pa_stream_notify_cb_t cb, void *userdata);
374
375 /** Set the callback function that is called when a buffer underflow happens. (Only for playback streams) \since 0.8 */
376 void pa_stream_set_underflow_callback(pa_stream *p, pa_stream_notify_cb_t cb, void *userdata);
377
378 /** Set the callback function that is called whenever a latency information update happens. Useful on PA_STREAM_AUTO_TIMING_UPDATE streams only. (Only for playback streams) \since 0.8.2 */
379 void pa_stream_set_latency_update_callback(pa_stream *p, pa_stream_notify_cb_t cb, void *userdata);
380
381 /** Pause (or resume) playback of this stream temporarily. Available on both playback and recording streams. \since 0.3 */
382 pa_operation* pa_stream_cork(pa_stream *s, int b, pa_stream_success_cb_t cb, void *userdata);
383
384 /** Flush the playback buffer of this stream. Most of the time you're
385  * better off using the parameter delta of pa_stream_write() instead of this
386  * function. Available on both playback and recording streams. \since 0.3 */
387 pa_operation* pa_stream_flush(pa_stream *s, pa_stream_success_cb_t cb, void *userdata);
388
389 /** Reenable prebuffering as specified in the pa_buffer_attr
390  * structure. Available for playback streams only. \since 0.6 */
391 pa_operation* pa_stream_prebuf(pa_stream *s, pa_stream_success_cb_t cb, void *userdata);
392
393 /** Request immediate start of playback on this stream. This disables
394  * prebuffering as specified in the pa_buffer_attr
395  * structure, temporarily. Available for playback streams only. \since 0.3 */
396 pa_operation* pa_stream_trigger(pa_stream *s, pa_stream_success_cb_t cb, void *userdata);
397
398 /** Rename the stream. \since 0.5 */
399 pa_operation* pa_stream_set_name(pa_stream *s, const char *name, pa_stream_success_cb_t cb, void *userdata);
400
401 /** Return the current playback/recording time. This is based on the
402  * data in the timing info structure returned by
403  * pa_stream_get_timing_info(). This function will usually only return
404  * new data if a timing info update has been recieved. Only if timing
405  * interpolation has been requested (PA_STREAM_INTERPOLATE_TIMING)
406  * the data from the last timing update is used for an estimation of
407  * the current playback/recording time based on the local time that
408  * passed since the timing info structure has been acquired. The time
409  * value returned by this function is guaranteed to increase
410  * monotonically. (that means: the returned value is always greater or
411  * equal to the value returned on the last call) This behaviour can
412  * be disabled by using PA_STREAM_NOT_MONOTONOUS. This may be
413  * desirable to deal better with bad estimations of transport
414  * latencies, but may have strange effects if the application is not
415  * able to deal with time going 'backwards'. \since 0.6 */
416 int pa_stream_get_time(pa_stream *s, pa_usec_t *r_usec);
417
418 /** Return the total stream latency. This function is based on
419  * pa_stream_get_time(). In case the stream is a monitoring stream the
420  * result can be negative, i.e. the captured samples are not yet
421  * played. In this case *negative is set to 1. \since 0.6 */
422 int pa_stream_get_latency(pa_stream *s, pa_usec_t *r_usec, int *negative);
423
424 /** Return the latest raw timing data structure. The returned pointer
425  * points to an internal read-only instance of the timing
426  * structure. The user should make a copy of this structure if he
427  * wants to modify it. An in-place update to this data structure may
428  * be requested using pa_stream_update_timing_info(). If no
429  * pa_stream_update_timing_info() call was issued before, this
430  * function will fail with PA_ERR_NODATA. Please note that the
431  * write_index member field (and only this field) is updated on each
432  * pa_stream_write() call, not just when a timing update has been
433  * recieved. \since 0.8 */
434 const pa_timing_info* pa_stream_get_timing_info(pa_stream *s);
435
436 /** Return a pointer to the stream's sample specification. \since 0.6 */
437 const pa_sample_spec* pa_stream_get_sample_spec(pa_stream *s);
438
439 /** Return a pointer to the stream's channel map. \since 0.8 */
440 const pa_channel_map* pa_stream_get_channel_map(pa_stream *s);
441
442 /** Return the buffer metrics of the stream. Only valid after the
443  * stream has been connected successfuly and if the server is at least
444  * Polypaudio 0.9. \since 0.9.0 */
445 const pa_buffer_attr* pa_stream_get_buffer_attr(pa_stream *s);
446
447 PA_C_DECL_END
448
449 #endif