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