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