[3423] | 1 | //stream.c: |
---|
| 2 | |
---|
| 3 | /* |
---|
| 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2010 |
---|
| 5 | * The code (may) include prototypes and comments (and maybe |
---|
| 6 | * other code fragements) from libpulse*. They are mostly copyrighted by: |
---|
| 7 | * Lennart Poettering <poettering@users.sourceforge.net> and |
---|
| 8 | * Pierre Ossman <drzeus@drzeus.cx> |
---|
| 9 | * |
---|
| 10 | * This file is part of libroarpulse a part of RoarAudio, |
---|
| 11 | * a cross-platform sound system for both, home and professional use. |
---|
| 12 | * See README for details. |
---|
| 13 | * |
---|
| 14 | * This file is free software; you can redistribute it and/or modify |
---|
| 15 | * it under the terms of the GNU General Public License version 3 |
---|
| 16 | * as published by the Free Software Foundation. |
---|
| 17 | * |
---|
| 18 | * RoarAudio is distributed in the hope that it will be useful, |
---|
| 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 21 | * GNU General Public License for more details. |
---|
| 22 | * |
---|
| 23 | * You should have received a copy of the GNU General Public License |
---|
| 24 | * along with this software; see the file COPYING. If not, write to |
---|
| 25 | * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
| 26 | * |
---|
| 27 | * NOTE for everyone want's to change something and send patches: |
---|
| 28 | * read README and HACKING! There a addition information on |
---|
| 29 | * the license of this document you need to read before you send |
---|
| 30 | * any patches. |
---|
| 31 | * |
---|
| 32 | * NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc |
---|
| 33 | * or libpulse*: |
---|
| 34 | * The libs libroaresd, libroararts and libroarpulse link this libroar |
---|
| 35 | * and are therefore GPL. Because of this it may be illigal to use |
---|
| 36 | * them with any software that uses libesd, libartsc or libpulse*. |
---|
| 37 | */ |
---|
| 38 | |
---|
| 39 | #include <libroarpulse/libroarpulse.h> |
---|
| 40 | |
---|
[3428] | 41 | struct _roar_pa_stream_cb { |
---|
| 42 | union { |
---|
| 43 | pa_stream_notify_cb_t ncb; |
---|
| 44 | pa_stream_request_cb_t rcb; |
---|
| 45 | pa_stream_success_cb_t scb; |
---|
| 46 | } cb; |
---|
| 47 | void * userdata; |
---|
| 48 | }; |
---|
| 49 | |
---|
[3425] | 50 | struct pa_stream { |
---|
[3439] | 51 | size_t refc; |
---|
| 52 | pa_context * c; |
---|
| 53 | struct roar_vio_calls vio; |
---|
| 54 | struct roar_stream stream; |
---|
| 55 | pa_stream_state_t state; |
---|
| 56 | pa_sample_spec sspec; |
---|
| 57 | pa_io_event * io_event; |
---|
| 58 | struct roar_buffer * iobuffer; |
---|
[3430] | 59 | struct { |
---|
| 60 | size_t size; |
---|
| 61 | size_t num; |
---|
| 62 | } fragments; |
---|
[3425] | 63 | struct { |
---|
[3428] | 64 | struct _roar_pa_stream_cb change_state; |
---|
[3429] | 65 | struct _roar_pa_stream_cb write; |
---|
| 66 | struct _roar_pa_stream_cb read; |
---|
| 67 | struct _roar_pa_stream_cb overflow; |
---|
| 68 | struct _roar_pa_stream_cb underflow; |
---|
| 69 | struct _roar_pa_stream_cb latency; |
---|
[3425] | 70 | } cb; |
---|
| 71 | }; |
---|
| 72 | |
---|
| 73 | typedef void pa_proplist; |
---|
| 74 | void pa_stream_set_state(pa_stream *s, pa_stream_state_t st); |
---|
| 75 | |
---|
| 76 | pa_stream* pa_stream_new_with_proplist( |
---|
| 77 | pa_context *c , |
---|
| 78 | const char *name , |
---|
| 79 | const pa_sample_spec *ss , |
---|
| 80 | const pa_channel_map *map , |
---|
| 81 | pa_proplist *p ); |
---|
| 82 | |
---|
[3424] | 83 | /** Create a new, unconnected stream with the specified name and sample type */ |
---|
| 84 | pa_stream* pa_stream_new( |
---|
| 85 | pa_context *c /**< The context to create this stream in */, |
---|
| 86 | const char *name /**< A name for this stream */, |
---|
| 87 | const pa_sample_spec *ss /**< The desired sample format */, |
---|
[3425] | 88 | const pa_channel_map *map /**< The desired channel map, or NULL for default */) { |
---|
| 89 | return pa_stream_new_with_proplist(c, name, ss, map, NULL); |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | pa_stream* pa_stream_new_with_proplist( |
---|
| 93 | pa_context *c , |
---|
| 94 | const char *name , |
---|
| 95 | const pa_sample_spec *ss , |
---|
| 96 | const pa_channel_map *map , |
---|
| 97 | pa_proplist *p ) { |
---|
| 98 | pa_stream * s; |
---|
| 99 | |
---|
[3440] | 100 | ROAR_DBG("pa_stream_new_with_proplist(c=%p, name='%s', ss=%p, map=%p, p=%p) = ?", c, name, ss, map, p); |
---|
| 101 | |
---|
[3425] | 102 | if ( p != NULL ) |
---|
| 103 | return NULL; |
---|
| 104 | |
---|
| 105 | if ( (s = roar_mm_malloc(sizeof(pa_stream))) == NULL ) |
---|
| 106 | return NULL; |
---|
| 107 | |
---|
| 108 | memset(s, 0, sizeof(pa_stream)); |
---|
| 109 | |
---|
[3427] | 110 | memcpy(&(s->sspec), ss, sizeof(pa_sample_spec)); |
---|
| 111 | |
---|
[3440] | 112 | ROAR_DBG("pa_stream_new_with_proplist(c=%p, name='%s', ss=%p, map=%p, p=%p) = ?", c, name, ss, map, p); |
---|
| 113 | |
---|
[3430] | 114 | s->fragments.num = 4; |
---|
| 115 | s->fragments.size = 2048; |
---|
| 116 | |
---|
[3425] | 117 | s->state = PA_STREAM_UNCONNECTED; |
---|
| 118 | s->c = c; |
---|
| 119 | pa_context_ref(c); |
---|
| 120 | |
---|
[3440] | 121 | ROAR_DBG("pa_stream_new_with_proplist(c=%p, name='%s', ss=%p, map=%p, p=%p) = ?", c, name, ss, map, p); |
---|
| 122 | |
---|
[3425] | 123 | return s; |
---|
| 124 | } |
---|
| 125 | |
---|
| 126 | static void _pa_stream_free(pa_stream * s) { |
---|
| 127 | pa_stream_disconnect(s); |
---|
| 128 | pa_context_unref(s->c); |
---|
| 129 | roar_mm_free(s); |
---|
| 130 | } |
---|
[3424] | 131 | |
---|
| 132 | /** Decrease the reference counter by one */ |
---|
[3425] | 133 | void pa_stream_unref(pa_stream *s) { |
---|
| 134 | if ( s == NULL ) |
---|
| 135 | return; |
---|
| 136 | |
---|
| 137 | s->refc--; |
---|
| 138 | |
---|
| 139 | if (s->refc < 1 ) |
---|
| 140 | _pa_stream_free(s); |
---|
| 141 | } |
---|
[3424] | 142 | |
---|
| 143 | /** Increase the reference counter by one */ |
---|
[3425] | 144 | pa_stream *pa_stream_ref(pa_stream *s) { |
---|
| 145 | if ( s == NULL ) |
---|
| 146 | return NULL; |
---|
| 147 | |
---|
| 148 | s->refc++; |
---|
| 149 | |
---|
| 150 | return s; |
---|
| 151 | } |
---|
[3424] | 152 | |
---|
| 153 | /** Return the current state of the stream */ |
---|
[3425] | 154 | pa_stream_state_t pa_stream_get_state(pa_stream *p) { |
---|
| 155 | if ( p == NULL ) |
---|
| 156 | return PA_STREAM_FAILED; |
---|
| 157 | |
---|
| 158 | return p->state; |
---|
| 159 | } |
---|
[3424] | 160 | |
---|
| 161 | /** Return the context this stream is attached to */ |
---|
[3425] | 162 | pa_context* pa_stream_get_context(pa_stream *p) { |
---|
| 163 | if ( p == NULL ) |
---|
| 164 | return NULL; |
---|
| 165 | |
---|
| 166 | return p->c; |
---|
| 167 | } |
---|
[3424] | 168 | |
---|
| 169 | /** Return the device (sink input or source output) index this stream is connected to */ |
---|
[3426] | 170 | uint32_t pa_stream_get_index(pa_stream *s) { |
---|
| 171 | return 0; |
---|
| 172 | } |
---|
[3424] | 173 | |
---|
[3440] | 174 | static void _roar_pa_stream_ioecb(pa_mainloop_api * ea, |
---|
| 175 | pa_io_event * e, |
---|
| 176 | int fd, |
---|
| 177 | pa_io_event_flags_t events, |
---|
| 178 | void * userdata) { |
---|
| 179 | ROAR_DBG("_roar_pa_stream_ioecb(*) = (void)"); |
---|
| 180 | } |
---|
| 181 | |
---|
[3433] | 182 | static int _roar_pa_stream_open (pa_stream *s, |
---|
| 183 | const char *dev, |
---|
| 184 | const pa_buffer_attr *attr, |
---|
| 185 | pa_stream_flags_t flags, |
---|
| 186 | pa_cvolume *volume, |
---|
| 187 | pa_stream *sync_stream, |
---|
| 188 | pa_stream_direction_t dir) { |
---|
[3434] | 189 | struct roar_connection * con; |
---|
[3439] | 190 | pa_mainloop_api * api; |
---|
| 191 | pa_io_event_flags_t event_flags = PA_IO_EVENT_HANGUP; |
---|
| 192 | int fh; |
---|
| 193 | int ctl = -1; |
---|
[3434] | 194 | |
---|
| 195 | if ( s == NULL ) |
---|
| 196 | return -1; |
---|
| 197 | |
---|
| 198 | if ( attr != NULL || flags != 0 || volume != NULL || sync_stream != NULL ) { |
---|
| 199 | pa_stream_set_state(s, PA_STREAM_FAILED); |
---|
| 200 | return -1; |
---|
| 201 | } |
---|
| 202 | |
---|
| 203 | if ( (con = roar_pa_context_get_con(s->c)) == NULL ) { |
---|
| 204 | pa_stream_set_state(s, PA_STREAM_FAILED); |
---|
| 205 | return -1; |
---|
| 206 | } |
---|
| 207 | |
---|
| 208 | switch (dir) { |
---|
| 209 | case PA_STREAM_PLAYBACK: |
---|
| 210 | s->stream.dir = ROAR_DIR_PLAY; |
---|
[3439] | 211 | ctl = ROAR_VIO_CTL_GET_SELECT_WRITE_FH; |
---|
| 212 | event_flags |= PA_IO_EVENT_OUTPUT; |
---|
[3434] | 213 | break; |
---|
| 214 | case PA_STREAM_RECORD: |
---|
| 215 | s->stream.dir = ROAR_DIR_MONITOR; |
---|
[3439] | 216 | ctl = ROAR_VIO_CTL_GET_SELECT_READ_FH; |
---|
| 217 | event_flags |= PA_IO_EVENT_INPUT; |
---|
[3434] | 218 | break; |
---|
| 219 | default: |
---|
| 220 | pa_stream_set_state(s, PA_STREAM_FAILED); |
---|
| 221 | return -1; |
---|
| 222 | break; |
---|
| 223 | } |
---|
| 224 | |
---|
[3441] | 225 | if ( roar_pa_sspec2auinfo(&(s->stream.info), &(s->sspec)) == -1 ) { |
---|
| 226 | pa_stream_set_state(s, PA_STREAM_FAILED); |
---|
| 227 | return -1; |
---|
| 228 | } |
---|
| 229 | |
---|
[3434] | 230 | if ( roar_vio_simple_new_stream_obj(&(s->vio), con, &(s->stream), |
---|
| 231 | s->stream.info.rate, s->stream.info.channels, |
---|
| 232 | s->stream.info.bits, s->stream.info.codec, |
---|
| 233 | s->stream.dir) == -1 ) { |
---|
| 234 | pa_stream_set_state(s, PA_STREAM_FAILED); |
---|
| 235 | return -1; |
---|
| 236 | } |
---|
| 237 | |
---|
[3439] | 238 | api = roar_pa_context_get_api(s->c); |
---|
| 239 | |
---|
| 240 | if ( api != NULL && api->io_new != NULL ) { |
---|
| 241 | if ( roar_vio_ctl(&(s->vio), ctl, &fh) != -1 ) { |
---|
[3440] | 242 | s->io_event = api->io_new(api, fh, event_flags, _roar_pa_stream_ioecb, s); |
---|
[3439] | 243 | } |
---|
| 244 | } |
---|
| 245 | |
---|
[3434] | 246 | pa_stream_set_state(s, PA_STREAM_READY); |
---|
| 247 | |
---|
| 248 | return 0; |
---|
[3433] | 249 | } |
---|
| 250 | |
---|
[3424] | 251 | /** Connect the stream to a sink */ |
---|
| 252 | int pa_stream_connect_playback( |
---|
| 253 | pa_stream *s /**< The stream to connect to a sink */, |
---|
| 254 | const char *dev /**< Name of the sink to connect to, or NULL for default */ , |
---|
| 255 | const pa_buffer_attr *attr /**< Buffering attributes, or NULL for default */, |
---|
| 256 | pa_stream_flags_t flags /**< Additional flags, or 0 for default */, |
---|
| 257 | pa_cvolume *volume /**< Initial volume, or NULL for default */, |
---|
[3433] | 258 | pa_stream *sync_stream /**< Synchronize this stream with the specified one, or NULL for a standalone stream*/) { |
---|
| 259 | return _roar_pa_stream_open(s, dev, attr, flags, volume, sync_stream, PA_STREAM_PLAYBACK); |
---|
| 260 | } |
---|
[3424] | 261 | |
---|
| 262 | /** Connect the stream to a source */ |
---|
| 263 | int pa_stream_connect_record( |
---|
| 264 | pa_stream *s /**< The stream to connect to a source */ , |
---|
| 265 | const char *dev /**< Name of the source to connect to, or NULL for default */, |
---|
| 266 | const pa_buffer_attr *attr /**< Buffer attributes, or NULL for default */, |
---|
[3433] | 267 | pa_stream_flags_t flags /**< Additional flags, or 0 for default */) { |
---|
| 268 | return _roar_pa_stream_open(s, dev, attr, flags, NULL, NULL, PA_STREAM_RECORD); |
---|
| 269 | } |
---|
[3424] | 270 | |
---|
| 271 | /** Disconnect a stream from a source/sink */ |
---|
[3425] | 272 | int pa_stream_disconnect(pa_stream *s) { |
---|
[3439] | 273 | pa_mainloop_api * api; |
---|
| 274 | |
---|
[3425] | 275 | if ( s == NULL ) |
---|
| 276 | return -1; |
---|
| 277 | |
---|
| 278 | if ( s->state != PA_STREAM_READY ) |
---|
| 279 | return -1; |
---|
| 280 | |
---|
[3439] | 281 | if ( s->io_event != NULL ) { |
---|
| 282 | api = roar_pa_context_get_api(s->c); |
---|
| 283 | |
---|
| 284 | if ( api != NULL && api->io_free != NULL ) { |
---|
| 285 | api->io_free(s->io_event); |
---|
| 286 | s->io_event = NULL; |
---|
| 287 | } |
---|
| 288 | } |
---|
| 289 | |
---|
[3425] | 290 | roar_vio_close(&(s->vio)); |
---|
| 291 | |
---|
| 292 | pa_stream_set_state(s, PA_STREAM_TERMINATED); |
---|
| 293 | |
---|
| 294 | return 0; |
---|
| 295 | } |
---|
[3424] | 296 | |
---|
| 297 | /** Write some data to the server (for playback sinks), if free_cb is |
---|
| 298 | * non-NULL this routine is called when all data has been written out |
---|
| 299 | * and an internal reference to the specified data is kept, the data |
---|
| 300 | * is not copied. If NULL, the data is copied into an internal |
---|
| 301 | * buffer. The client my freely seek around in the output buffer. For |
---|
| 302 | * most applications passing 0 and PA_SEEK_RELATIVE as arguments for |
---|
| 303 | * offset and seek should be useful.*/ |
---|
| 304 | int pa_stream_write( |
---|
| 305 | pa_stream *p /**< The stream to use */, |
---|
| 306 | const void *data /**< The data to write */, |
---|
| 307 | size_t length /**< The length of the data to write */, |
---|
| 308 | pa_free_cb_t free_cb /**< A cleanup routine for the data or NULL to request an internal copy */, |
---|
| 309 | int64_t offset, /**< Offset for seeking, must be 0 for upload streams */ |
---|
[3432] | 310 | pa_seek_mode_t seek /**< Seek mode, must be PA_SEEK_RELATIVE for upload streams */) { |
---|
| 311 | struct roar_buffer * buf; |
---|
| 312 | void * bufdata; |
---|
| 313 | |
---|
| 314 | // TODO: implement seeking in output buffer |
---|
| 315 | |
---|
| 316 | if ( p == NULL ) |
---|
| 317 | return -1; |
---|
| 318 | |
---|
| 319 | if ( offset != 0 || seek != PA_SEEK_RELATIVE ) |
---|
| 320 | return -1; |
---|
| 321 | |
---|
| 322 | if ( data == NULL ) { |
---|
| 323 | if ( length == 0 ) { |
---|
| 324 | if ( free_cb != NULL ) |
---|
| 325 | free_cb(NULL); |
---|
| 326 | |
---|
| 327 | return 0; |
---|
| 328 | } else { |
---|
| 329 | return -1; |
---|
| 330 | } |
---|
| 331 | } |
---|
| 332 | |
---|
| 333 | // seems we have a valid write from here. |
---|
| 334 | |
---|
| 335 | if ( roar_buffer_new(&buf, length) == -1 ) { |
---|
| 336 | if ( free_cb != NULL ) |
---|
| 337 | free_cb((void*)data); |
---|
| 338 | |
---|
| 339 | return -1; |
---|
| 340 | } |
---|
| 341 | |
---|
| 342 | if ( roar_buffer_get_data(buf, &bufdata) == -1 ) { |
---|
| 343 | if ( free_cb != NULL ) |
---|
| 344 | free_cb((void*)data); |
---|
| 345 | |
---|
| 346 | return -1; |
---|
| 347 | } |
---|
| 348 | |
---|
| 349 | memcpy(bufdata, data, length); |
---|
| 350 | if ( free_cb != NULL ) |
---|
| 351 | free_cb((void*)data); |
---|
| 352 | |
---|
| 353 | if ( p->iobuffer == NULL ) { |
---|
| 354 | p->iobuffer = buf; |
---|
| 355 | } else { |
---|
| 356 | if ( roar_buffer_add(p->iobuffer, buf) == -1 ) |
---|
| 357 | return -1; |
---|
| 358 | } |
---|
| 359 | |
---|
| 360 | return 0; |
---|
| 361 | } |
---|
[3424] | 362 | |
---|
| 363 | /** Read the next fragment from the buffer (for recording). |
---|
| 364 | * data will point to the actual data and length will contain the size |
---|
| 365 | * of the data in bytes (which can be less than a complete framgnet). |
---|
| 366 | * Use pa_stream_drop() to actually remove the data from the |
---|
| 367 | * buffer. If no data is available will return a NULL pointer \since 0.8 */ |
---|
| 368 | int pa_stream_peek( |
---|
| 369 | pa_stream *p /**< The stream to use */, |
---|
| 370 | const void **data /**< Pointer to pointer that will point to data */, |
---|
[3431] | 371 | size_t *length /**< The length of the data read */) { |
---|
| 372 | if ( data == NULL || length == NULL ) |
---|
| 373 | return -1; |
---|
| 374 | |
---|
| 375 | *data = NULL; |
---|
| 376 | *length = 0; |
---|
| 377 | |
---|
| 378 | if ( p == NULL ) |
---|
| 379 | return -1; |
---|
| 380 | |
---|
| 381 | if ( p->iobuffer == NULL ) |
---|
| 382 | return 0; |
---|
| 383 | |
---|
| 384 | if ( roar_buffer_get_len(p->iobuffer, length) == -1 ) { |
---|
| 385 | *length = 0; |
---|
| 386 | return -1; |
---|
| 387 | } |
---|
| 388 | |
---|
| 389 | if ( roar_buffer_get_data(p->iobuffer, (void**)data) == -1 ) { |
---|
| 390 | *length = 0; |
---|
| 391 | *data = NULL; |
---|
| 392 | return -1; |
---|
| 393 | } |
---|
| 394 | |
---|
| 395 | return 0; |
---|
| 396 | } |
---|
[3424] | 397 | |
---|
| 398 | /** Remove the current fragment on record streams. It is invalid to do this without first |
---|
| 399 | * calling pa_stream_peek(). \since 0.8 */ |
---|
[3431] | 400 | int pa_stream_drop(pa_stream *p) { |
---|
| 401 | if ( p == NULL ) |
---|
| 402 | return -1; |
---|
| 403 | |
---|
| 404 | if ( p->iobuffer == NULL ) |
---|
| 405 | return -1; |
---|
| 406 | |
---|
| 407 | return roar_buffer_next(&(p->iobuffer)); |
---|
| 408 | } |
---|
[3424] | 409 | |
---|
| 410 | /** Return the nember of bytes that may be written using pa_stream_write() */ |
---|
[3430] | 411 | size_t pa_stream_writable_size(pa_stream *p) { |
---|
| 412 | struct roar_buffer_stats stats; |
---|
| 413 | |
---|
| 414 | if ( p == NULL ) |
---|
| 415 | return 0; |
---|
| 416 | |
---|
| 417 | if ( p->iobuffer == NULL ) |
---|
| 418 | return 0; |
---|
| 419 | |
---|
| 420 | if ( roar_buffer_ring_stats(p->iobuffer, &stats) == -1 ) |
---|
| 421 | return 0; |
---|
| 422 | |
---|
| 423 | if ( stats.parts > p->fragments.num ) |
---|
| 424 | return 0; |
---|
| 425 | |
---|
| 426 | return (p->fragments.num - stats.parts)*p->fragments.size; |
---|
| 427 | } |
---|
[3424] | 428 | |
---|
| 429 | /** Return the number of bytes that may be read using pa_stream_read() \since 0.8 */ |
---|
[3430] | 430 | size_t pa_stream_readable_size(pa_stream *p) { |
---|
| 431 | struct roar_buffer_stats stats; |
---|
| 432 | |
---|
| 433 | if ( p == NULL ) |
---|
| 434 | return 0; |
---|
| 435 | |
---|
| 436 | if ( p->iobuffer == NULL ) |
---|
| 437 | return 0; |
---|
| 438 | |
---|
| 439 | if ( roar_buffer_ring_stats(p->iobuffer, &stats) == -1 ) |
---|
| 440 | return 0; |
---|
| 441 | |
---|
| 442 | return stats.bytes; |
---|
| 443 | } |
---|
[3424] | 444 | |
---|
| 445 | /** Drain a playback stream. Use this for notification when the buffer is empty */ |
---|
| 446 | pa_operation* pa_stream_drain(pa_stream *s, pa_stream_success_cb_t cb, void *userdata); |
---|
| 447 | |
---|
| 448 | /** Request a timing info structure update for a stream. Use |
---|
| 449 | * pa_stream_get_timing_info() to get access to the raw timing data, |
---|
| 450 | * or pa_stream_get_time() or pa_stream_get_latency() to get cleaned |
---|
| 451 | * up values. */ |
---|
| 452 | pa_operation* pa_stream_update_timing_info(pa_stream *p, pa_stream_success_cb_t cb, void *userdata); |
---|
| 453 | |
---|
| 454 | /** Set the callback function that is called whenever the state of the stream changes */ |
---|
[3425] | 455 | void pa_stream_set_state_callback(pa_stream *s, pa_stream_notify_cb_t cb, void *userdata) { |
---|
[3441] | 456 | ROAR_DBG("pa_stream_set_state_callback(s=%p, cb=%p, userdata=%p) = ?", s, cb, userdata); |
---|
| 457 | |
---|
[3425] | 458 | if ( s == NULL ) |
---|
| 459 | return; |
---|
| 460 | |
---|
[3428] | 461 | s->cb.change_state.cb.ncb = cb; |
---|
| 462 | s->cb.change_state.userdata = userdata; |
---|
[3425] | 463 | } |
---|
| 464 | |
---|
| 465 | void pa_stream_set_state(pa_stream *s, pa_stream_state_t st) { |
---|
| 466 | if ( s == NULL ) |
---|
| 467 | return; |
---|
| 468 | |
---|
[3441] | 469 | ROAR_DBG("pa_stream_set_state(s=%p, st=%i): State: %i->%i", s, st, s->state, st); |
---|
| 470 | |
---|
[3425] | 471 | s->state = st; |
---|
| 472 | |
---|
[3441] | 473 | if ( s->cb.change_state.cb.ncb != NULL ) { |
---|
| 474 | ROAR_DBG("pa_stream_set_state(s=%p, st=%i): calling callback at %p", s, st, s->cb.change_state.cb.ncb); |
---|
[3428] | 475 | s->cb.change_state.cb.ncb(s, s->cb.change_state.userdata); |
---|
[3425] | 476 | } |
---|
[3441] | 477 | ROAR_DBG("pa_stream_set_state(s=%p, st=%i) = (void)", s, st); |
---|
[3425] | 478 | } |
---|
[3424] | 479 | |
---|
| 480 | /** Set the callback function that is called when new data may be |
---|
| 481 | * written to the stream. */ |
---|
[3429] | 482 | void pa_stream_set_write_callback(pa_stream *p, pa_stream_request_cb_t cb, void *userdata) { |
---|
| 483 | if ( p == NULL ) |
---|
| 484 | return; |
---|
| 485 | |
---|
| 486 | p->cb.write.cb.rcb = cb; |
---|
| 487 | p->cb.write.userdata = userdata; |
---|
| 488 | } |
---|
[3424] | 489 | |
---|
| 490 | /** Set the callback function that is called when new data is available from the stream. |
---|
| 491 | * Return the number of bytes read. \since 0.8 */ |
---|
[3429] | 492 | void pa_stream_set_read_callback(pa_stream *p, pa_stream_request_cb_t cb, void *userdata) { |
---|
| 493 | if ( p == NULL ) |
---|
| 494 | return; |
---|
| 495 | |
---|
| 496 | p->cb.read.cb.rcb = cb; |
---|
| 497 | p->cb.read.userdata = userdata; |
---|
| 498 | } |
---|
[3424] | 499 | |
---|
| 500 | /** Set the callback function that is called when a buffer overflow happens. (Only for playback streams) \since 0.8 */ |
---|
[3429] | 501 | void pa_stream_set_overflow_callback(pa_stream *p, pa_stream_notify_cb_t cb, void *userdata) { |
---|
| 502 | if ( p == NULL ) |
---|
| 503 | return; |
---|
| 504 | |
---|
| 505 | p->cb.overflow.cb.ncb = cb; |
---|
| 506 | p->cb.overflow.userdata = userdata; |
---|
| 507 | } |
---|
[3424] | 508 | |
---|
| 509 | /** Set the callback function that is called when a buffer underflow happens. (Only for playback streams) \since 0.8 */ |
---|
[3429] | 510 | void pa_stream_set_underflow_callback(pa_stream *p, pa_stream_notify_cb_t cb, void *userdata) { |
---|
| 511 | if ( p == NULL ) |
---|
| 512 | return; |
---|
| 513 | |
---|
| 514 | p->cb.underflow.cb.ncb = cb; |
---|
| 515 | p->cb.underflow.userdata = userdata; |
---|
| 516 | } |
---|
[3424] | 517 | |
---|
| 518 | /** Set the callback function that is called whenever a latency information update happens. Useful on PA_STREAM_AUTO_TIMING_UPDATE streams only. (Only for playback streams) \since 0.8.2 */ |
---|
[3429] | 519 | void pa_stream_set_latency_update_callback(pa_stream *p, pa_stream_notify_cb_t cb, void *userdata) { |
---|
| 520 | if ( p == NULL ) |
---|
| 521 | return; |
---|
| 522 | |
---|
| 523 | p->cb.latency.cb.ncb = cb; |
---|
| 524 | p->cb.latency.userdata = userdata; |
---|
| 525 | } |
---|
[3424] | 526 | |
---|
| 527 | /** Pause (or resume) playback of this stream temporarily. Available on both playback and recording streams. \since 0.3 */ |
---|
| 528 | pa_operation* pa_stream_cork(pa_stream *s, int b, pa_stream_success_cb_t cb, void *userdata); |
---|
| 529 | |
---|
| 530 | /** Flush the playback buffer of this stream. Most of the time you're |
---|
| 531 | * better off using the parameter delta of pa_stream_write() instead of this |
---|
| 532 | * function. Available on both playback and recording streams. \since 0.3 */ |
---|
| 533 | pa_operation* pa_stream_flush(pa_stream *s, pa_stream_success_cb_t cb, void *userdata); |
---|
| 534 | /** Reenable prebuffering as specified in the pa_buffer_attr |
---|
| 535 | * structure. Available for playback streams only. \since 0.6 */ |
---|
| 536 | pa_operation* pa_stream_prebuf(pa_stream *s, pa_stream_success_cb_t cb, void *userdata); |
---|
| 537 | |
---|
| 538 | /** Request immediate start of playback on this stream. This disables |
---|
| 539 | * prebuffering as specified in the pa_buffer_attr |
---|
| 540 | * structure, temporarily. Available for playback streams only. \since 0.3 */ |
---|
| 541 | pa_operation* pa_stream_trigger(pa_stream *s, pa_stream_success_cb_t cb, void *userdata); |
---|
| 542 | |
---|
| 543 | /** Rename the stream. \since 0.5 */ |
---|
| 544 | pa_operation* pa_stream_set_name(pa_stream *s, const char *name, pa_stream_success_cb_t cb, void *userdata); |
---|
| 545 | |
---|
| 546 | /** Return the current playback/recording time. This is based on the |
---|
| 547 | * data in the timing info structure returned by |
---|
| 548 | * pa_stream_get_timing_info(). This function will usually only return |
---|
| 549 | * new data if a timing info update has been recieved. Only if timing |
---|
| 550 | * interpolation has been requested (PA_STREAM_INTERPOLATE_TIMING) |
---|
| 551 | * the data from the last timing update is used for an estimation of |
---|
| 552 | * the current playback/recording time based on the local time that |
---|
| 553 | * passed since the timing info structure has been acquired. The time |
---|
| 554 | * value returned by this function is guaranteed to increase |
---|
| 555 | * monotonically. (that means: the returned value is always greater or |
---|
| 556 | * equal to the value returned on the last call) This behaviour can |
---|
| 557 | * be disabled by using PA_STREAM_NOT_MONOTONOUS. This may be |
---|
| 558 | * desirable to deal better with bad estimations of transport |
---|
| 559 | * latencies, but may have strange effects if the application is not |
---|
| 560 | * able to deal with time going 'backwards'. \since 0.6 */ |
---|
| 561 | int pa_stream_get_time(pa_stream *s, pa_usec_t *r_usec); |
---|
| 562 | |
---|
| 563 | /** Return the total stream latency. This function is based on |
---|
| 564 | * pa_stream_get_time(). In case the stream is a monitoring stream the |
---|
| 565 | * result can be negative, i.e. the captured samples are not yet |
---|
| 566 | * played. In this case *negative is set to 1. \since 0.6 */ |
---|
| 567 | int pa_stream_get_latency(pa_stream *s, pa_usec_t *r_usec, int *negative); |
---|
| 568 | |
---|
| 569 | /** Return the latest raw timing data structure. The returned pointer |
---|
| 570 | * points to an internal read-only instance of the timing |
---|
| 571 | * structure. The user should make a copy of this structure if he |
---|
| 572 | * wants to modify it. An in-place update to this data structure may |
---|
| 573 | * be requested using pa_stream_update_timing_info(). If no |
---|
| 574 | * pa_stream_update_timing_info() call was issued before, this |
---|
| 575 | * function will fail with PA_ERR_NODATA. Please note that the |
---|
| 576 | * write_index member field (and only this field) is updated on each |
---|
| 577 | * pa_stream_write() call, not just when a timing update has been |
---|
| 578 | * recieved. \since 0.8 */ |
---|
| 579 | const pa_timing_info* pa_stream_get_timing_info(pa_stream *s); |
---|
| 580 | |
---|
| 581 | /** Return a pointer to the stream's sample specification. \since 0.6 */ |
---|
[3427] | 582 | const pa_sample_spec* pa_stream_get_sample_spec(pa_stream *s) { |
---|
| 583 | if ( s == NULL ) |
---|
| 584 | return NULL; |
---|
| 585 | |
---|
| 586 | return &(s->sspec); |
---|
| 587 | } |
---|
[3424] | 588 | |
---|
| 589 | /** Return a pointer to the stream's channel map. \since 0.8 */ |
---|
| 590 | const pa_channel_map* pa_stream_get_channel_map(pa_stream *s); |
---|
| 591 | |
---|
| 592 | /** Return the buffer metrics of the stream. Only valid after the |
---|
| 593 | * stream has been connected successfuly and if the server is at least |
---|
| 594 | * PulseAudio 0.9. \since 0.9.0 */ |
---|
| 595 | const pa_buffer_attr* pa_stream_get_buffer_attr(pa_stream *s); |
---|
| 596 | |
---|
[3423] | 597 | //ll |
---|