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 void pa_stream_terminate(pa_stream *s); 348 349 /** Return the context this stream is attached to. */ 350 pa_context* pa_stream_get_context(const pa_stream *p); 351 352 /** Return the sink input resp.\ source output index this stream is 353 * identified in the server with. This is useful with the 354 * introspection functions such as pa_context_get_sink_input_info() 355 * or pa_context_get_source_output_info(). This returns PA_INVALID_INDEX 356 * on failure. */ 357 uint32_t pa_stream_get_index(const pa_stream *s); 358 359 /** Return the index of the sink or source this stream is connected to 360 * in the server. This is useful with the introspection 361 * functions such as pa_context_get_sink_info_by_index() or 362 * pa_context_get_source_info_by_index(). 363 * 364 * Please note that streams may be moved between sinks/sources and thus 365 * it is recommended to use pa_stream_set_moved_callback() to be notified 366 * about this. This function will return with PA_INVALID_INDEX on failure, 367 * including the being server older than 0.9.8. \since 0.9.8 */ 368 uint32_t pa_stream_get_device_index(const pa_stream *s); 369 370 /** Return the name of the sink or source this stream is connected to 371 * in the server. This is useful with the introspection 372 * functions such as pa_context_get_sink_info_by_name() 373 * or pa_context_get_source_info_by_name(). 374 * 375 * Please note that streams may be moved between sinks/sources and thus 376 * it is recommended to use pa_stream_set_moved_callback() to be notified 377 * about this. This function will fail when the server is older than 378 * 0.9.8. \since 0.9.8 */ 379 const char *pa_stream_get_device_name(const pa_stream *s); 380 381 /** Return 1 if the sink or source this stream is connected to has 382 * been suspended. This will return 0 if not, and a negative value on 383 * error. This function will return with -PA_ERR_NOTSUPPORTED when the 384 * server is older than 0.9.8. \since 0.9.8 */ 385 int pa_stream_is_suspended(const pa_stream *s); 386 387 /** Return 1 if the this stream has been corked. This will return 0 if 388 * not, and a negative value on error. \since 0.9.11 */ 389 int pa_stream_is_corked(const pa_stream *s); 390 391 /** Connect the stream to a sink. It is strongly recommended to pass 392 * NULL in both \a dev and \a volume and to set neither 393 * PA_STREAM_START_MUTED nor PA_STREAM_START_UNMUTED -- unless these 394 * options are directly dependent on user input or configuration. 395 * 396 * If you follow this rule then the sound server will have the full 397 * flexibility to choose the device, volume and mute status 398 * automatically, based on server-side policies, heuristics and stored 399 * information from previous uses. Also the server may choose to 400 * reconfigure audio devices to make other sinks/sources or 401 * capabilities available to be able to accept the stream. 402 * 403 * Before 0.9.20 it was not defined whether the \a volume parameter was 404 * interpreted relative to the sink's current volume or treated as 405 * an absolute device volume. Since 0.9.20 it is an absolute volume when 406 * the sink is in flat volume mode, and relative otherwise, thus 407 * making sure the volume passed here has always the same semantics as 408 * the volume passed to pa_context_set_sink_input_volume(). It is possible 409 * to figure out whether flat volume mode is in effect for a given sink 410 * by calling pa_context_get_sink_info_by_name(). 411 * 412 * Since 5.0, it's possible to specify a single-channel volume even if the 413 * stream has multiple channels. In that case the same volume is applied to all 414 * channels. 415 * 416 * Returns zero on success. */ 417 int pa_stream_connect_playback( 418 pa_stream *s /**< The stream to connect to a sink */, 419 const char *dev /**< Name of the sink to connect to, or NULL to let the server decide */ , 420 const pa_buffer_attr *attr /**< Buffering attributes, or NULL for default */, 421 pa_stream_flags_t flags /**< Additional flags, or 0 for default */, 422 const pa_cvolume *volume /**< Initial volume, or NULL for default */, 423 pa_stream *sync_stream /**< Synchronize this stream with the specified one, or NULL for a standalone stream */); 424 425 /** Connect the stream to a source. Returns zero on success. */ 426 int pa_stream_connect_record( 427 pa_stream *s /**< The stream to connect to a source */ , 428 const char *dev /**< Name of the source to connect to, or NULL to let the server decide */, 429 const pa_buffer_attr *attr /**< Buffer attributes, or NULL for default */, 430 pa_stream_flags_t flags /**< Additional flags, or 0 for default */); 431 432 /** Disconnect a stream from a source/sink. Returns zero on success. */ 433 int pa_stream_disconnect(pa_stream *s); 434 435 /** Prepare writing data to the server (for playback streams). This 436 * function may be used to optimize the number of memory copies when 437 * doing playback ("zero-copy"). It is recommended to call this 438 * function before each call to pa_stream_write(). 439 * 440 * Pass in the address to a pointer and an address of the number of 441 * bytes you want to write. On return the two values will contain a 442 * pointer where you can place the data to write and the maximum number 443 * of bytes you can write. \a *nbytes can be smaller or have the same 444 * value as you passed in. You need to be able to handle both cases. 445 * Accessing memory beyond the returned \a *nbytes value is invalid. 446 * Accessing the memory returned after the following pa_stream_write() 447 * or pa_stream_cancel_write() is invalid. 448 * 449 * On invocation only \a *nbytes needs to be initialized, on return both 450 * *data and *nbytes will be valid. If you place (size_t) -1 in *nbytes 451 * on invocation the memory size will be chosen automatically (which is 452 * recommended to do). After placing your data in the memory area 453 * returned, call pa_stream_write() with \a data set to an address 454 * within this memory area and an \a nbytes value that is smaller or 455 * equal to what was returned by this function to actually execute the 456 * write. 457 * 458 * An invocation of pa_stream_write() should follow "quickly" on 459 * pa_stream_begin_write(). It is not recommended letting an unbounded 460 * amount of time pass after calling pa_stream_begin_write() and 461 * before calling pa_stream_write(). If you want to cancel a 462 * previously called pa_stream_begin_write() without calling 463 * pa_stream_write() use pa_stream_cancel_write(). Calling 464 * pa_stream_begin_write() twice without calling pa_stream_write() or 465 * pa_stream_cancel_write() in between will return exactly the same 466 * \a data pointer and \a nbytes values. 467 * 468 * On success, will return zero and a valid (non-NULL) pointer. If the 469 * return value is non-zero, or the pointer is NULL, this indicates an 470 * error. Callers should also pay careful attention to the returned 471 * length, which may not be the same as that passed in, as mentioned above. 472 * 473 * \since 0.9.16 */ 474 int pa_stream_begin_write( 475 pa_stream *p, 476 void **data, 477 size_t *nbytes); 478 479 /** Reverses the effect of pa_stream_begin_write() dropping all data 480 * that has already been placed in the memory area returned by 481 * pa_stream_begin_write(). Only valid to call if 482 * pa_stream_begin_write() was called before and neither 483 * pa_stream_cancel_write() nor pa_stream_write() have been called 484 * yet. Accessing the memory previously returned by 485 * pa_stream_begin_write() after this call is invalid. Any further 486 * explicit freeing of the memory area is not necessary. 487 * Returns zero on success. \since 0.9.16 */ 488 int pa_stream_cancel_write( 489 pa_stream *p); 490 491 /** Write some data to the server (for playback streams). 492 * If \a free_cb is non-NULL this routine is called when all data has 493 * been written out. An internal reference to the specified data is 494 * kept, the data is not copied. If NULL, the data is copied into an 495 * internal buffer. 496 * 497 * The client may freely seek around in the output buffer. For 498 * most applications it is typical to pass 0 and PA_SEEK_RELATIVE 499 * as values for the arguments \a offset and \a seek respectively. 500 * After a successful write call the write index will be at the 501 * position after where this chunk of data has been written to. 502 * 503 * As an optimization for avoiding needless memory copies you may call 504 * pa_stream_begin_write() before this call and then place your audio 505 * data directly in the memory area returned by that call. Then, pass 506 * a pointer to that memory area to pa_stream_write(). After the 507 * invocation of pa_stream_write() the memory area may no longer be 508 * accessed. Any further explicit freeing of the memory area is not 509 * necessary. It is OK to write to the memory area returned by 510 * pa_stream_begin_write() only partially with this call, skipping 511 * bytes both at the end and at the beginning of the reserved memory 512 * area. 513 * 514 * Returns zero on success. */ 515 int pa_stream_write( 516 pa_stream *p /**< The stream to use */, 517 const void *data /**< The data to write */, 518 size_t nbytes /**< The length of the data to write in bytes, must be in multiples of the stream's sample spec frame size */, 519 pa_free_cb_t free_cb /**< A cleanup routine for the data or NULL to request an internal copy */, 520 int64_t offset /**< Offset for seeking, must be 0 for upload streams, must be in multiples of the stream's sample spec frame size */, 521 pa_seek_mode_t seek /**< Seek mode, must be PA_SEEK_RELATIVE for upload streams */); 522 523 /** Function does exactly the same as pa_stream_write() with the difference 524 * that free_cb_data is passed to free_cb instead of data. \since 6.0 */ 525 int pa_stream_write_ext_free( 526 pa_stream *p /**< The stream to use */, 527 const void *data /**< The data to write */, 528 size_t nbytes /**< The length of the data to write in bytes */, 529 pa_free_cb_t free_cb /**< A cleanup routine for the data or NULL to request an internal copy */, 530 void *free_cb_data /**< Argument passed to free_cb function */, 531 int64_t offset /**< Offset for seeking, must be 0 for upload streams */, 532 pa_seek_mode_t seek /**< Seek mode, must be PA_SEEK_RELATIVE for upload streams */); 533 534 /** Read the next fragment from the buffer (for recording streams). 535 * If there is data at the current read index, \a data will point to 536 * the actual data and \a nbytes will contain the size of the data in 537 * bytes (which can be less or more than a complete fragment). 538 * 539 * If there is no data at the current read index, it means that either 540 * the buffer is empty or it contains a hole (that is, the write index 541 * is ahead of the read index but there's no data where the read index 542 * points at). If the buffer is empty, \a data will be NULL and 543 * \a nbytes will be 0. If there is a hole, \a data will be NULL and 544 * \a nbytes will contain the length of the hole. 545 * 546 * Use pa_stream_drop() to actually remove the data from the buffer 547 * and move the read index forward. pa_stream_drop() should not be 548 * called if the buffer is empty, but it should be called if there is 549 * a hole. 550 * 551 * Returns zero on success, negative on error. */ 552 int pa_stream_peek( 553 pa_stream *p /**< The stream to use */, 554 const void **data /**< Pointer to pointer that will point to data */, 555 size_t *nbytes /**< The length of the data read in bytes */); 556 557 /** Remove the current fragment on record streams. It is invalid to do this without first 558 * calling pa_stream_peek(). Returns zero on success. */ 559 int pa_stream_drop(pa_stream *p); 560 561 /** Return the number of bytes requested by the server that have not yet 562 * been written. 563 * 564 * It is possible to write more than this amount, up to the stream's 565 * buffer_attr.maxlength bytes. This is usually not desirable, though, as 566 * it would increase stream latency to be higher than requested 567 * (buffer_attr.tlength). 568 * 569 * (size_t) -1 is returned on error. 570 */ 571 size_t pa_stream_writable_size(const pa_stream *p); 572 573 /** Return the number of bytes that may be read using pa_stream_peek(). 574 * 575 * (size_t) -1 is returned on error. */ 576 size_t pa_stream_readable_size(const pa_stream *p); 577 578 /** Drain a playback stream. Use this for notification when the 579 * playback buffer is empty after playing all the audio in the buffer. 580 * Please note that only one drain operation per stream may be issued 581 * at a time. */ 582 pa_operation* pa_stream_drain(pa_stream *s, pa_stream_success_cb_t cb, void *userdata); 583 584 /** Request a timing info structure update for a stream. Use 585 * pa_stream_get_timing_info() to get access to the raw timing data, 586 * or pa_stream_get_time() or pa_stream_get_latency() to get cleaned 587 * up values. */ 588 pa_operation* pa_stream_update_timing_info(pa_stream *p, pa_stream_success_cb_t cb, void *userdata); 589 590 /** Set the callback function that is called whenever the state of the stream changes. */ 591 void pa_stream_set_state_callback(pa_stream *s, pa_stream_notify_cb_t cb, void *userdata); 592 593 /** Set the callback function that is called when new data may be 594 * written to the stream. */ 595 void pa_stream_set_write_callback(pa_stream *p, pa_stream_request_cb_t cb, void *userdata); 596 597 /** Set the callback function that is called when new data is available from the stream. */ 598 void pa_stream_set_read_callback(pa_stream *p, pa_stream_request_cb_t cb, void *userdata); 599 600 /** Set the callback function that is called when a buffer overflow happens. (Only for playback streams) */ 601 void pa_stream_set_overflow_callback(pa_stream *p, pa_stream_notify_cb_t cb, void *userdata); 602 603 /** Return at what position the latest underflow occurred, or -1 if this information is not 604 * known (e.g.\ if no underflow has occurred, or server is older than 1.0). 605 * Can be used inside the underflow callback to get information about the current underflow. 606 * (Only for playback streams) \since 1.0 */ 607 int64_t pa_stream_get_underflow_index(const pa_stream *p); 608 609 /** Set the callback function that is called when a buffer underflow happens. (Only for playback streams) */ 610 void pa_stream_set_underflow_callback(pa_stream *p, pa_stream_notify_cb_t cb, void *userdata); 611 612 void pa_stream_set_underflow_ohos_callback(pa_stream *s, pa_stream_notify_cb_t cb, void *userdata); 613 614 /** Set the callback function that is called when the server starts 615 * playback after an underrun or on initial startup. This only informs 616 * that audio is flowing again, it is no indication that audio started 617 * to reach the speakers already. (Only for playback streams) \since 618 * 0.9.11 */ 619 void pa_stream_set_started_callback(pa_stream *p, pa_stream_notify_cb_t cb, void *userdata); 620 621 /** Set the callback function that is called whenever a latency 622 * information update happens. Useful on PA_STREAM_AUTO_TIMING_UPDATE 623 * streams only. */ 624 void pa_stream_set_latency_update_callback(pa_stream *p, pa_stream_notify_cb_t cb, void *userdata); 625 626 /** Set the callback function that is called whenever the stream is 627 * moved to a different sink/source. Use pa_stream_get_device_name() or 628 * pa_stream_get_device_index() to query the new sink/source. This 629 * notification is only generated when the server is at least 630 * 0.9.8. \since 0.9.8 */ 631 void pa_stream_set_moved_callback(pa_stream *p, pa_stream_notify_cb_t cb, void *userdata); 632 633 /** Set the callback function that is called whenever the sink/source 634 * this stream is connected to is suspended or resumed. Use 635 * pa_stream_is_suspended() to query the new suspend status. Please 636 * note that the suspend status might also change when the stream is 637 * moved between devices. Thus if you call this function you very 638 * likely want to call pa_stream_set_moved_callback() too. This 639 * notification is only generated when the server is at least 640 * 0.9.8. \since 0.9.8 */ 641 void pa_stream_set_suspended_callback(pa_stream *p, pa_stream_notify_cb_t cb, void *userdata); 642 643 /** Set the callback function that is called whenever a meta/policy 644 * control event is received. \since 0.9.15 */ 645 void pa_stream_set_event_callback(pa_stream *p, pa_stream_event_cb_t cb, void *userdata); 646 647 /** Set the callback function that is called whenever the buffer 648 * attributes on the server side change. Please note that the buffer 649 * attributes can change when moving a stream to a different 650 * sink/source too, hence if you use this callback you should use 651 * pa_stream_set_moved_callback() as well. \since 0.9.15 */ 652 void pa_stream_set_buffer_attr_callback(pa_stream *p, pa_stream_notify_cb_t cb, void *userdata); 653 654 /** Pause (or resume) playback of this stream temporarily. Available 655 * on both playback and recording streams. If \a b is 1 the stream is 656 * paused. If \a b is 0 the stream is resumed. The pause/resume operation 657 * is executed as quickly as possible. If a cork is very quickly 658 * followed by an uncork or the other way round, this might not 659 * actually have any effect on the stream that is output. You can use 660 * pa_stream_is_corked() to find out whether the stream is currently 661 * paused or not. Normally a stream will be created in uncorked 662 * state. If you pass PA_STREAM_START_CORKED as a flag when connecting 663 * the stream, it will be created in corked state. */ 664 pa_operation* pa_stream_cork(pa_stream *s, int b, pa_stream_success_cb_t cb, void *userdata); 665 666 /** Flush the playback or record buffer of this stream. This discards any audio data 667 * in the buffer. Most of the time you're better off using the parameter 668 * \a seek of pa_stream_write() instead of this function. */ 669 pa_operation* pa_stream_flush(pa_stream *s, pa_stream_success_cb_t cb, void *userdata); 670 671 /** Reenable prebuffering if specified in the pa_buffer_attr 672 * structure. Available for playback streams only. */ 673 pa_operation* pa_stream_prebuf(pa_stream *s, pa_stream_success_cb_t cb, void *userdata); 674 675 /** Request immediate start of playback on this stream. This disables 676 * prebuffering temporarily if specified in the pa_buffer_attr structure. 677 * Available for playback streams only. */ 678 pa_operation* pa_stream_trigger(pa_stream *s, pa_stream_success_cb_t cb, void *userdata); 679 680 /** Rename the stream. */ 681 pa_operation* pa_stream_set_name(pa_stream *s, const char *name, pa_stream_success_cb_t cb, void *userdata); 682 683 /** Return the current playback/recording time. This is based on the 684 * data in the timing info structure returned by 685 * pa_stream_get_timing_info(). The returned time is in the sound card 686 * clock domain, which usually runs at a slightly different rate than 687 * the system clock. 688 * 689 * This function will usually only return new data if a timing info 690 * update has been received. Only if timing interpolation has been 691 * requested (PA_STREAM_INTERPOLATE_TIMING) the data from the last 692 * timing update is used for an estimation of the current 693 * playback/recording time based on the local time that passed since 694 * the timing info structure has been acquired. 695 * 696 * The time value returned by this function is guaranteed to increase 697 * monotonically (the returned value is always greater 698 * or equal to the value returned by the last call). This behaviour 699 * can be disabled by using PA_STREAM_NOT_MONOTONIC. This may be 700 * desirable to better deal with bad estimations of transport 701 * latencies, but may have strange effects if the application is not 702 * able to deal with time going 'backwards'. 703 * 704 * The time interpolator activated by PA_STREAM_INTERPOLATE_TIMING 705 * favours 'smooth' time graphs over accurate ones to improve the 706 * smoothness of UI operations that are tied to the audio clock. If 707 * accuracy is more important to you, you might need to estimate your 708 * timing based on the data from pa_stream_get_timing_info() yourself 709 * or not work with interpolated timing at all and instead always 710 * query the server side for the most up to date timing with 711 * pa_stream_update_timing_info(). 712 * 713 * If no timing information has been 714 * received yet this call will return -PA_ERR_NODATA. For more details 715 * see pa_stream_get_timing_info(). 716 * 717 * Returns zero on success, negative on error. */ 718 int pa_stream_get_time(pa_stream *s, pa_usec_t *r_usec); 719 720 /** Determine the total stream latency. This function is based on 721 * pa_stream_get_time(). The returned time is in the sound card clock 722 * domain, which usually runs at a slightly different rate than the 723 * system clock. 724 * 725 * The latency is stored in \a *r_usec. In case the stream is a 726 * monitoring stream the result can be negative, i.e. the captured 727 * samples are not yet played. In this case \a *negative is set to 1. 728 * 729 * If no timing information has been received yet, this call will 730 * return -PA_ERR_NODATA. On success, it will return 0. 731 * 732 * For more details see pa_stream_get_timing_info() and 733 * pa_stream_get_time(). */ 734 int pa_stream_get_latency(pa_stream *s, pa_usec_t *r_usec, int *negative); 735 736 /** Return the latest raw timing data structure. The returned pointer 737 * refers to an internal read-only instance of the timing 738 * structure. The user should make a copy of this structure if 739 * wanting to modify it. An in-place update to this data structure 740 * may be requested using pa_stream_update_timing_info(). 741 * 742 * If no timing information has been received before (i.e. by 743 * requesting pa_stream_update_timing_info() or by using 744 * PA_STREAM_AUTO_TIMING_UPDATE), this function will return NULL. 745 * 746 * Please note that the write_index member field (and only this field) 747 * is updated on each pa_stream_write() call, not just when a timing 748 * update has been received. */ 749 const pa_timing_info* pa_stream_get_timing_info(pa_stream *s); 750 751 /** Return a pointer to the stream's sample specification. */ 752 const pa_sample_spec* pa_stream_get_sample_spec(pa_stream *s); 753 754 /** Return a pointer to the stream's channel map. */ 755 const pa_channel_map* pa_stream_get_channel_map(pa_stream *s); 756 757 /** Return a pointer to the stream's format. \since 1.0 */ 758 const pa_format_info* pa_stream_get_format_info(const pa_stream *s); 759 760 /** Return the per-stream server-side buffer metrics of the 761 * stream. Only valid after the stream has been connected successfully 762 * and if the server is at least PulseAudio 0.9. This will return the 763 * actual configured buffering metrics, which may differ from what was 764 * requested during pa_stream_connect_record() or 765 * pa_stream_connect_playback(). This call will always return the 766 * actual per-stream server-side buffer metrics, regardless whether 767 * PA_STREAM_ADJUST_LATENCY is set or not. \since 0.9.0 */ 768 const pa_buffer_attr* pa_stream_get_buffer_attr(pa_stream *s); 769 770 /** Change the buffer metrics of the stream during playback. The 771 * server might have chosen different buffer metrics than 772 * requested. The selected metrics may be queried with 773 * pa_stream_get_buffer_attr() as soon as the callback is called. Only 774 * valid after the stream has been connected successfully and if the 775 * server is at least PulseAudio 0.9.8. Please be aware of the 776 * slightly different semantics of the call depending whether 777 * PA_STREAM_ADJUST_LATENCY is set or not. \since 0.9.8 */ 778 pa_operation *pa_stream_set_buffer_attr(pa_stream *s, const pa_buffer_attr *attr, pa_stream_success_cb_t cb, void *userdata); 779 780 /** Change the stream sampling rate during playback. You need to pass 781 * PA_STREAM_VARIABLE_RATE in the flags parameter of 782 * pa_stream_connect_playback() if you plan to use this function. Only valid 783 * after the stream has been connected successfully and if the server 784 * is at least PulseAudio 0.9.8. \since 0.9.8 */ 785 pa_operation *pa_stream_update_sample_rate(pa_stream *s, uint32_t rate, pa_stream_success_cb_t cb, void *userdata); 786 787 /** Update the property list of the sink input/source output of this 788 * stream, adding new entries. Please note that it is highly 789 * recommended to set as many properties initially via 790 * pa_stream_new_with_proplist() as possible instead a posteriori with 791 * this function, since that information may be used to route 792 * this stream to the right device. \since 0.9.11 */ 793 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); 794 795 /** Update the property list of the sink input/source output of this 796 * stream, remove entries. \since 0.9.11 */ 797 pa_operation *pa_stream_proplist_remove(pa_stream *s, const char *const keys[], pa_stream_success_cb_t cb, void *userdata); 798 799 /** For record streams connected to a monitor source: monitor only a 800 * very specific sink input of the sink. This function needs to be 801 * called before pa_stream_connect_record() is called. 802 * Returns zero on success, negative on error. \since 0.9.11 */ 803 int pa_stream_set_monitor_stream(pa_stream *s, uint32_t sink_input_idx); 804 805 /** Return the sink input index previously set with 806 * pa_stream_set_monitor_stream(). Returns PA_INVALID_INDEX 807 * on failure. \since 0.9.11 */ 808 uint32_t pa_stream_get_monitor_stream(const pa_stream *s); 809 810 PA_C_DECL_END 811 812 #endif 813