]> code.delx.au - pulseaudio/blob - src/polyp/stream.h
bb5aa7646e22125f1407a78fcb94f72e4d8f5bdb
[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 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 structure
137 * that contains everything that is known about buffers, transport delays
138 * and the backend active in the server.
139 *
140 * If a more simplistic interface is prefered, you can call
141 * pa_stream_get_time() or pa_stream_get_latency(). These will do all the
142 * necessary calculations for you.
143 *
144 * The latency information is constantly updated from the server. Be aware
145 * that between updates, old data will be returned. If you specify the flag
146 * PA_STREAM_INTERPOLATE_TIMING when creating the stream, pa_stream_get_time()
147 * and pa_stream_get_latency() will calculate the latency between updates
148 * based on the time elapsed.
149 *
150 * \section flow_sec Overflow and underflow
151 *
152 * Even with the best precautions, buffers will sometime over- or underflow.
153 * To handle this gracefully, the application can be notified when this
154 * happens. Callbacks are registered using pa_stream_set_overflow_callback()
155 * and pa_stream_set_underflow_callback().
156 *
157 * \section disc_sec Disconnecting
158 *
159 * When a stream has served is purpose it must be disconnected with
160 * pa_stream_disconnect(). If you only unreference it, then it will live on
161 * and eat resources both locally and on the server until you disconnect the
162 * context.
163 */
164
165 /** \file
166 * Audio streams for input, output and sample upload */
167
168 PA_C_DECL_BEGIN
169
170 /** An opaque stream for playback or recording */
171 typedef struct pa_stream pa_stream;
172
173 /** A generic callback for operation completion */
174 typedef void (*pa_stream_success_cb_t) (pa_stream*s, int success, void *userdata);
175
176 /** A generic free callback */
177 typedef void (*pa_free_cb_t)(void *p);
178
179 /** A generic request callback */
180 typedef void (*pa_stream_request_cb_t)(pa_stream *p, size_t length, void *userdata);
181
182 /** A generic notification callback */
183 typedef void (*pa_stream_notify_cb_t)(pa_stream *p, void *userdata);
184
185 /** Create a new, unconnected stream with the specified name and sample type */
186 pa_stream* pa_stream_new(
187 pa_context *c /**< The context to create this stream in */,
188 const char *name /**< A name for this stream */,
189 const pa_sample_spec *ss /**< The desired sample format */,
190 const pa_channel_map *map /**< The desired channel map, or NULL for default */);
191
192 /** Decrease the reference counter by one */
193 void pa_stream_unref(pa_stream *s);
194
195 /** Increase the reference counter by one */
196 pa_stream *pa_stream_ref(pa_stream *s);
197
198 /** Return the current state of the stream */
199 pa_stream_state_t pa_stream_get_state(pa_stream *p);
200
201 /** Return the context this stream is attached to */
202 pa_context* pa_stream_get_context(pa_stream *p);
203
204 /** Return the device (sink input or source output) index this stream is connected to */
205 uint32_t pa_stream_get_index(pa_stream *s);
206
207 /** Connect the stream to a sink */
208 int pa_stream_connect_playback(
209 pa_stream *s /**< The stream to connect to a sink */,
210 const char *dev /**< Name of the sink to connect to, or NULL for default */ ,
211 const pa_buffer_attr *attr /**< Buffering attributes, or NULL for default */,
212 pa_stream_flags_t flags /**< Additional flags, or 0 for default */,
213 pa_cvolume *volume /**< Initial volume, or NULL for default */,
214 pa_stream *sync_stream /**< Synchronize this stream with the specified one, or NULL for a standalone stream*/);
215
216 /** Connect the stream to a source */
217 int pa_stream_connect_record(
218 pa_stream *s /**< The stream to connect to a source */ ,
219 const char *dev /**< Name of the source to connect to, or NULL for default */,
220 const pa_buffer_attr *attr /**< Buffer attributes, or NULL for default */,
221 pa_stream_flags_t flags /**< Additional flags, or 0 for default */);
222
223 /** Disconnect a stream from a source/sink */
224 int pa_stream_disconnect(pa_stream *s);
225
226 /** Write some data to the server (for playback sinks), if free_cb is
227 * non-NULL this routine is called when all data has been written out
228 * and an internal reference to the specified data is kept, the data
229 * is not copied. If NULL, the data is copied into an internal
230 * buffer. The client my freely seek around in the output buffer. For
231 * most applications passing 0 and PA_SEEK_RELATIVE as arguments for
232 * offset and seek should be useful.*/
233 int pa_stream_write(
234 pa_stream *p /**< The stream to use */,
235 const void *data /**< The data to write */,
236 size_t length /**< The length of the data to write */,
237 pa_free_cb_t free_cb /**< A cleanup routine for the data or NULL to request an internal copy */,
238 int64_t offset, /**< Offset for seeking, must be 0 for upload streams */
239 pa_seek_mode_t seek /**< Seek mode, must be PA_SEEK_RELATIVE for upload streams */);
240
241 /** Read the next fragment from the buffer (for recording).
242 * data will point to the actual data and length will contain the size
243 * of the data in bytes (which can be less than a complete framgnet).
244 * Use pa_stream_drop() to actually remove the data from the
245 * buffer. If no data is available will return a NULL pointer \since 0.8 */
246 int pa_stream_peek(
247 pa_stream *p /**< The stream to use */,
248 const void **data /**< Pointer to pointer that will point to data */,
249 size_t *length /**< The length of the data read */);
250
251 /** Remove the current fragment. It is invalid to do this without first
252 * calling pa_stream_peek(). \since 0.8 */
253 int pa_stream_drop(pa_stream *p);
254
255 /** Return the nember of bytes that may be written using pa_stream_write() */
256 size_t pa_stream_writable_size(pa_stream *p);
257
258 /** Return the number of bytes that may be read using pa_stream_read() \since 0.8 */
259 size_t pa_stream_readable_size(pa_stream *p);
260
261 /** Drain a playback stream. Use this for notification when the buffer is empty */
262 pa_operation* pa_stream_drain(pa_stream *s, pa_stream_success_cb_t cb, void *userdata);
263
264 /** Request a timing info structure update for a stream. Use
265 * pa_stream_get_timing_info() to get access to the raw timing data,
266 * or pa_stream_get_time() or pa_stream_get_latency() to get cleaned
267 * up values. */
268 pa_operation* pa_stream_update_timing_info(pa_stream *p, pa_stream_success_cb_t cb, void *userdata);
269
270 /** Set the callback function that is called whenever the state of the stream changes */
271 void pa_stream_set_state_callback(pa_stream *s, pa_stream_notify_cb_t cb, void *userdata);
272
273 /** Set the callback function that is called when new data may be
274 * written to the stream. */
275 void pa_stream_set_write_callback(pa_stream *p, pa_stream_request_cb_t cb, void *userdata);
276
277 /** Set the callback function that is called when new data is available from the stream.
278 * Return the number of bytes read. \since 0.8 */
279 void pa_stream_set_read_callback(pa_stream *p, pa_stream_request_cb_t cb, void *userdata);
280
281 /** Set the callback function that is called when a buffer overflow happens. (Only for playback streams) \since 0.8 */
282 void pa_stream_set_overflow_callback(pa_stream *p, pa_stream_notify_cb_t cb, void *userdata);
283
284 /** Set the callback function that is called when a buffer underflow happens. (Only for playback streams) \since 0.8 */
285 void pa_stream_set_underflow_callback(pa_stream *p, pa_stream_notify_cb_t cb, void *userdata);
286
287 /** Pause (or resume) playback of this stream temporarily. Available on both playback and recording streams. \since 0.3 */
288 pa_operation* pa_stream_cork(pa_stream *s, int b, pa_stream_success_cb_t cb, void *userdata);
289
290 /** Flush the playback buffer of this stream. Most of the time you're
291 * better off using the parameter delta of pa_stream_write() instead of this
292 * function. Available on both playback and recording streams. \since 0.3 */
293 pa_operation* pa_stream_flush(pa_stream *s, pa_stream_success_cb_t cb, void *userdata);
294
295 /** Reenable prebuffering as specified in the pa_buffer_attr
296 * structure. Available for playback streams only. \since 0.6 */
297 pa_operation* pa_stream_prebuf(pa_stream *s, pa_stream_success_cb_t cb, void *userdata);
298
299 /** Request immediate start of playback on this stream. This disables
300 * prebuffering as specified in the pa_buffer_attr
301 * structure, temporarily. Available for playback streams only. \since 0.3 */
302 pa_operation* pa_stream_trigger(pa_stream *s, pa_stream_success_cb_t cb, void *userdata);
303
304 /** Rename the stream. \since 0.5 */
305 pa_operation* pa_stream_set_name(pa_stream *s, const char *name, pa_stream_success_cb_t cb, void *userdata);
306
307 /** Return the current playback/recording time. This is based on the
308 * data in the timing info structure returned by
309 * pa_stream_get_timing_info(). This function will usually only return
310 * new data if a timing info update has been recieved. Only if timing
311 * interpolation has been requested (PA_STREAM_INTERPOLATE_TIMING)
312 * the data from the last timing update is used for an estimation of
313 * the current playback/recording time based on the local time that
314 * passed since the timing info structure has been acquired. The time
315 * value returned by this function is guaranteed to increase
316 * monotonically. (that means: the returned value is always greater or
317 * equal to the value returned on the last call) This behaviour can
318 * be disabled by using PA_STREAM_NOT_MONOTONOUS. This may be
319 * desirable to deal better with bad estimations of transport
320 * latencies, but may have strange effects if the application is not
321 * able to deal with time going 'backwards'. \since 0.6 */
322 int pa_stream_get_time(pa_stream *s, pa_usec_t *r_usec);
323
324 /** Return the total stream latency. This function is based on
325 * pa_stream_get_time(). In case the stream is a monitoring stream the
326 * result can be negative, i.e. the captured samples are not yet
327 * played. In this case *negative is set to 1. \since 0.6 */
328 int pa_stream_get_latency(pa_stream *s, pa_usec_t *r_usec, int *negative);
329
330 /** Return the latest raw timing data structure. The returned pointer
331 * points to an internal read-only instance of the timing
332 * structure. The user should make a copy of this structure if he
333 * wants to modify it. An in-place update to this data structure may
334 * be requested using pa_stream_update_timing_info(). If no
335 * pa_stream_update_timing_info() call was issued before, this
336 * function will fail with PA_ERR_NODATA. Please note that the
337 * write_index member field (and only this field) is updated on each
338 * pa_stream_write() call, not just when a timing update has been
339 * recieved. \since 0.8 */
340 const pa_timing_info* pa_stream_get_timing_info(pa_stream *s);
341
342 /** Return a pointer to the stream's sample specification. \since 0.6 */
343 const pa_sample_spec* pa_stream_get_sample_spec(pa_stream *s);
344
345 /** Return a pointer to the stream's channel map. \since 0.8 */
346 const pa_channel_map* pa_stream_get_channel_map(pa_stream *s);
347
348 PA_C_DECL_END
349
350 #endif