[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; |
---|
[3451] | 58 | pa_timing_info timinginfo; |
---|
[3439] | 59 | struct roar_buffer * iobuffer; |
---|
[3430] | 60 | struct { |
---|
| 61 | size_t size; |
---|
| 62 | size_t num; |
---|
| 63 | } fragments; |
---|
[3425] | 64 | struct { |
---|
[3428] | 65 | struct _roar_pa_stream_cb change_state; |
---|
[3429] | 66 | struct _roar_pa_stream_cb write; |
---|
| 67 | struct _roar_pa_stream_cb read; |
---|
| 68 | struct _roar_pa_stream_cb overflow; |
---|
| 69 | struct _roar_pa_stream_cb underflow; |
---|
| 70 | struct _roar_pa_stream_cb latency; |
---|
[3449] | 71 | struct _roar_pa_stream_cb drain; |
---|
[3425] | 72 | } cb; |
---|
[3449] | 73 | struct { |
---|
| 74 | pa_operation * drain; |
---|
| 75 | } op; |
---|
[3425] | 76 | }; |
---|
| 77 | |
---|
| 78 | typedef void pa_proplist; |
---|
| 79 | void pa_stream_set_state(pa_stream *s, pa_stream_state_t st); |
---|
| 80 | |
---|
| 81 | pa_stream* pa_stream_new_with_proplist( |
---|
| 82 | pa_context *c , |
---|
| 83 | const char *name , |
---|
| 84 | const pa_sample_spec *ss , |
---|
| 85 | const pa_channel_map *map , |
---|
| 86 | pa_proplist *p ); |
---|
| 87 | |
---|
[3424] | 88 | /** Create a new, unconnected stream with the specified name and sample type */ |
---|
| 89 | pa_stream* pa_stream_new( |
---|
| 90 | pa_context *c /**< The context to create this stream in */, |
---|
| 91 | const char *name /**< A name for this stream */, |
---|
| 92 | const pa_sample_spec *ss /**< The desired sample format */, |
---|
[3425] | 93 | const pa_channel_map *map /**< The desired channel map, or NULL for default */) { |
---|
| 94 | return pa_stream_new_with_proplist(c, name, ss, map, NULL); |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | pa_stream* pa_stream_new_with_proplist( |
---|
| 98 | pa_context *c , |
---|
| 99 | const char *name , |
---|
| 100 | const pa_sample_spec *ss , |
---|
| 101 | const pa_channel_map *map , |
---|
| 102 | pa_proplist *p ) { |
---|
| 103 | pa_stream * s; |
---|
| 104 | |
---|
[3440] | 105 | ROAR_DBG("pa_stream_new_with_proplist(c=%p, name='%s', ss=%p, map=%p, p=%p) = ?", c, name, ss, map, p); |
---|
| 106 | |
---|
[3425] | 107 | if ( p != NULL ) |
---|
| 108 | return NULL; |
---|
| 109 | |
---|
| 110 | if ( (s = roar_mm_malloc(sizeof(pa_stream))) == NULL ) |
---|
| 111 | return NULL; |
---|
| 112 | |
---|
| 113 | memset(s, 0, sizeof(pa_stream)); |
---|
| 114 | |
---|
[3427] | 115 | memcpy(&(s->sspec), ss, sizeof(pa_sample_spec)); |
---|
| 116 | |
---|
[3440] | 117 | ROAR_DBG("pa_stream_new_with_proplist(c=%p, name='%s', ss=%p, map=%p, p=%p) = ?", c, name, ss, map, p); |
---|
| 118 | |
---|
[3430] | 119 | s->fragments.num = 4; |
---|
| 120 | s->fragments.size = 2048; |
---|
| 121 | |
---|
[3425] | 122 | s->state = PA_STREAM_UNCONNECTED; |
---|
| 123 | s->c = c; |
---|
| 124 | pa_context_ref(c); |
---|
| 125 | |
---|
[3440] | 126 | ROAR_DBG("pa_stream_new_with_proplist(c=%p, name='%s', ss=%p, map=%p, p=%p) = ?", c, name, ss, map, p); |
---|
| 127 | |
---|
[3425] | 128 | return s; |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | static void _pa_stream_free(pa_stream * s) { |
---|
| 132 | pa_stream_disconnect(s); |
---|
| 133 | pa_context_unref(s->c); |
---|
| 134 | roar_mm_free(s); |
---|
| 135 | } |
---|
[3424] | 136 | |
---|
| 137 | /** Decrease the reference counter by one */ |
---|
[3425] | 138 | void pa_stream_unref(pa_stream *s) { |
---|
| 139 | if ( s == NULL ) |
---|
| 140 | return; |
---|
| 141 | |
---|
| 142 | s->refc--; |
---|
| 143 | |
---|
| 144 | if (s->refc < 1 ) |
---|
| 145 | _pa_stream_free(s); |
---|
| 146 | } |
---|
[3424] | 147 | |
---|
| 148 | /** Increase the reference counter by one */ |
---|
[3425] | 149 | pa_stream *pa_stream_ref(pa_stream *s) { |
---|
| 150 | if ( s == NULL ) |
---|
| 151 | return NULL; |
---|
| 152 | |
---|
| 153 | s->refc++; |
---|
| 154 | |
---|
| 155 | return s; |
---|
| 156 | } |
---|
[3424] | 157 | |
---|
| 158 | /** Return the current state of the stream */ |
---|
[3425] | 159 | pa_stream_state_t pa_stream_get_state(pa_stream *p) { |
---|
| 160 | if ( p == NULL ) |
---|
| 161 | return PA_STREAM_FAILED; |
---|
| 162 | |
---|
| 163 | return p->state; |
---|
| 164 | } |
---|
[3424] | 165 | |
---|
| 166 | /** Return the context this stream is attached to */ |
---|
[3425] | 167 | pa_context* pa_stream_get_context(pa_stream *p) { |
---|
| 168 | if ( p == NULL ) |
---|
| 169 | return NULL; |
---|
| 170 | |
---|
| 171 | return p->c; |
---|
| 172 | } |
---|
[3424] | 173 | |
---|
| 174 | /** Return the device (sink input or source output) index this stream is connected to */ |
---|
[3426] | 175 | uint32_t pa_stream_get_index(pa_stream *s) { |
---|
| 176 | return 0; |
---|
| 177 | } |
---|
[3424] | 178 | |
---|
[3440] | 179 | static void _roar_pa_stream_ioecb(pa_mainloop_api * ea, |
---|
| 180 | pa_io_event * e, |
---|
| 181 | int fd, |
---|
| 182 | pa_io_event_flags_t events, |
---|
| 183 | void * userdata) { |
---|
| 184 | ROAR_DBG("_roar_pa_stream_ioecb(*) = (void)"); |
---|
| 185 | } |
---|
| 186 | |
---|
[3433] | 187 | static int _roar_pa_stream_open (pa_stream *s, |
---|
| 188 | const char *dev, |
---|
| 189 | const pa_buffer_attr *attr, |
---|
| 190 | pa_stream_flags_t flags, |
---|
| 191 | pa_cvolume *volume, |
---|
| 192 | pa_stream *sync_stream, |
---|
| 193 | pa_stream_direction_t dir) { |
---|
[3434] | 194 | struct roar_connection * con; |
---|
[3439] | 195 | pa_mainloop_api * api; |
---|
| 196 | pa_io_event_flags_t event_flags = PA_IO_EVENT_HANGUP; |
---|
| 197 | int fh; |
---|
| 198 | int ctl = -1; |
---|
[3434] | 199 | |
---|
| 200 | if ( s == NULL ) |
---|
| 201 | return -1; |
---|
| 202 | |
---|
| 203 | if ( attr != NULL || flags != 0 || volume != NULL || sync_stream != NULL ) { |
---|
| 204 | pa_stream_set_state(s, PA_STREAM_FAILED); |
---|
| 205 | return -1; |
---|
| 206 | } |
---|
| 207 | |
---|
| 208 | if ( (con = roar_pa_context_get_con(s->c)) == NULL ) { |
---|
| 209 | pa_stream_set_state(s, PA_STREAM_FAILED); |
---|
| 210 | return -1; |
---|
| 211 | } |
---|
| 212 | |
---|
| 213 | switch (dir) { |
---|
| 214 | case PA_STREAM_PLAYBACK: |
---|
| 215 | s->stream.dir = ROAR_DIR_PLAY; |
---|
[3439] | 216 | ctl = ROAR_VIO_CTL_GET_SELECT_WRITE_FH; |
---|
| 217 | event_flags |= PA_IO_EVENT_OUTPUT; |
---|
[3434] | 218 | break; |
---|
| 219 | case PA_STREAM_RECORD: |
---|
| 220 | s->stream.dir = ROAR_DIR_MONITOR; |
---|
[3439] | 221 | ctl = ROAR_VIO_CTL_GET_SELECT_READ_FH; |
---|
| 222 | event_flags |= PA_IO_EVENT_INPUT; |
---|
[3434] | 223 | break; |
---|
| 224 | default: |
---|
| 225 | pa_stream_set_state(s, PA_STREAM_FAILED); |
---|
| 226 | return -1; |
---|
| 227 | break; |
---|
| 228 | } |
---|
| 229 | |
---|
[3441] | 230 | if ( roar_pa_sspec2auinfo(&(s->stream.info), &(s->sspec)) == -1 ) { |
---|
| 231 | pa_stream_set_state(s, PA_STREAM_FAILED); |
---|
| 232 | return -1; |
---|
| 233 | } |
---|
| 234 | |
---|
[3434] | 235 | if ( roar_vio_simple_new_stream_obj(&(s->vio), con, &(s->stream), |
---|
| 236 | s->stream.info.rate, s->stream.info.channels, |
---|
| 237 | s->stream.info.bits, s->stream.info.codec, |
---|
| 238 | s->stream.dir) == -1 ) { |
---|
| 239 | pa_stream_set_state(s, PA_STREAM_FAILED); |
---|
| 240 | return -1; |
---|
| 241 | } |
---|
| 242 | |
---|
[3439] | 243 | api = roar_pa_context_get_api(s->c); |
---|
| 244 | |
---|
| 245 | if ( api != NULL && api->io_new != NULL ) { |
---|
| 246 | if ( roar_vio_ctl(&(s->vio), ctl, &fh) != -1 ) { |
---|
[3440] | 247 | s->io_event = api->io_new(api, fh, event_flags, _roar_pa_stream_ioecb, s); |
---|
[3439] | 248 | } |
---|
| 249 | } |
---|
| 250 | |
---|
[3434] | 251 | pa_stream_set_state(s, PA_STREAM_READY); |
---|
| 252 | |
---|
| 253 | return 0; |
---|
[3433] | 254 | } |
---|
| 255 | |
---|
[3424] | 256 | /** Connect the stream to a sink */ |
---|
| 257 | int pa_stream_connect_playback( |
---|
| 258 | pa_stream *s /**< The stream to connect to a sink */, |
---|
| 259 | const char *dev /**< Name of the sink to connect to, or NULL for default */ , |
---|
| 260 | const pa_buffer_attr *attr /**< Buffering attributes, or NULL for default */, |
---|
| 261 | pa_stream_flags_t flags /**< Additional flags, or 0 for default */, |
---|
| 262 | pa_cvolume *volume /**< Initial volume, or NULL for default */, |
---|
[3433] | 263 | pa_stream *sync_stream /**< Synchronize this stream with the specified one, or NULL for a standalone stream*/) { |
---|
| 264 | return _roar_pa_stream_open(s, dev, attr, flags, volume, sync_stream, PA_STREAM_PLAYBACK); |
---|
| 265 | } |
---|
[3424] | 266 | |
---|
| 267 | /** Connect the stream to a source */ |
---|
| 268 | int pa_stream_connect_record( |
---|
| 269 | pa_stream *s /**< The stream to connect to a source */ , |
---|
| 270 | const char *dev /**< Name of the source to connect to, or NULL for default */, |
---|
| 271 | const pa_buffer_attr *attr /**< Buffer attributes, or NULL for default */, |
---|
[3433] | 272 | pa_stream_flags_t flags /**< Additional flags, or 0 for default */) { |
---|
| 273 | return _roar_pa_stream_open(s, dev, attr, flags, NULL, NULL, PA_STREAM_RECORD); |
---|
| 274 | } |
---|
[3424] | 275 | |
---|
| 276 | /** Disconnect a stream from a source/sink */ |
---|
[3425] | 277 | int pa_stream_disconnect(pa_stream *s) { |
---|
[3439] | 278 | pa_mainloop_api * api; |
---|
| 279 | |
---|
[3425] | 280 | if ( s == NULL ) |
---|
| 281 | return -1; |
---|
| 282 | |
---|
| 283 | if ( s->state != PA_STREAM_READY ) |
---|
| 284 | return -1; |
---|
| 285 | |
---|
[3439] | 286 | if ( s->io_event != NULL ) { |
---|
| 287 | api = roar_pa_context_get_api(s->c); |
---|
| 288 | |
---|
| 289 | if ( api != NULL && api->io_free != NULL ) { |
---|
| 290 | api->io_free(s->io_event); |
---|
| 291 | s->io_event = NULL; |
---|
| 292 | } |
---|
| 293 | } |
---|
| 294 | |
---|
[3425] | 295 | roar_vio_close(&(s->vio)); |
---|
| 296 | |
---|
| 297 | pa_stream_set_state(s, PA_STREAM_TERMINATED); |
---|
| 298 | |
---|
| 299 | return 0; |
---|
| 300 | } |
---|
[3424] | 301 | |
---|
| 302 | /** Write some data to the server (for playback sinks), if free_cb is |
---|
| 303 | * non-NULL this routine is called when all data has been written out |
---|
| 304 | * and an internal reference to the specified data is kept, the data |
---|
| 305 | * is not copied. If NULL, the data is copied into an internal |
---|
| 306 | * buffer. The client my freely seek around in the output buffer. For |
---|
| 307 | * most applications passing 0 and PA_SEEK_RELATIVE as arguments for |
---|
| 308 | * offset and seek should be useful.*/ |
---|
| 309 | int pa_stream_write( |
---|
| 310 | pa_stream *p /**< The stream to use */, |
---|
| 311 | const void *data /**< The data to write */, |
---|
| 312 | size_t length /**< The length of the data to write */, |
---|
| 313 | pa_free_cb_t free_cb /**< A cleanup routine for the data or NULL to request an internal copy */, |
---|
| 314 | int64_t offset, /**< Offset for seeking, must be 0 for upload streams */ |
---|
[3432] | 315 | pa_seek_mode_t seek /**< Seek mode, must be PA_SEEK_RELATIVE for upload streams */) { |
---|
| 316 | struct roar_buffer * buf; |
---|
| 317 | void * bufdata; |
---|
| 318 | |
---|
| 319 | // TODO: implement seeking in output buffer |
---|
| 320 | |
---|
| 321 | if ( p == NULL ) |
---|
| 322 | return -1; |
---|
| 323 | |
---|
| 324 | if ( offset != 0 || seek != PA_SEEK_RELATIVE ) |
---|
| 325 | return -1; |
---|
| 326 | |
---|
| 327 | if ( data == NULL ) { |
---|
| 328 | if ( length == 0 ) { |
---|
| 329 | if ( free_cb != NULL ) |
---|
| 330 | free_cb(NULL); |
---|
| 331 | |
---|
| 332 | return 0; |
---|
| 333 | } else { |
---|
| 334 | return -1; |
---|
| 335 | } |
---|
| 336 | } |
---|
| 337 | |
---|
| 338 | // seems we have a valid write from here. |
---|
| 339 | |
---|
| 340 | if ( roar_buffer_new(&buf, length) == -1 ) { |
---|
| 341 | if ( free_cb != NULL ) |
---|
| 342 | free_cb((void*)data); |
---|
| 343 | |
---|
| 344 | return -1; |
---|
| 345 | } |
---|
| 346 | |
---|
| 347 | if ( roar_buffer_get_data(buf, &bufdata) == -1 ) { |
---|
| 348 | if ( free_cb != NULL ) |
---|
| 349 | free_cb((void*)data); |
---|
| 350 | |
---|
| 351 | return -1; |
---|
| 352 | } |
---|
| 353 | |
---|
| 354 | memcpy(bufdata, data, length); |
---|
| 355 | if ( free_cb != NULL ) |
---|
| 356 | free_cb((void*)data); |
---|
| 357 | |
---|
| 358 | if ( p->iobuffer == NULL ) { |
---|
| 359 | p->iobuffer = buf; |
---|
| 360 | } else { |
---|
| 361 | if ( roar_buffer_add(p->iobuffer, buf) == -1 ) |
---|
| 362 | return -1; |
---|
| 363 | } |
---|
| 364 | |
---|
| 365 | return 0; |
---|
| 366 | } |
---|
[3424] | 367 | |
---|
| 368 | /** Read the next fragment from the buffer (for recording). |
---|
| 369 | * data will point to the actual data and length will contain the size |
---|
| 370 | * of the data in bytes (which can be less than a complete framgnet). |
---|
| 371 | * Use pa_stream_drop() to actually remove the data from the |
---|
| 372 | * buffer. If no data is available will return a NULL pointer \since 0.8 */ |
---|
| 373 | int pa_stream_peek( |
---|
| 374 | pa_stream *p /**< The stream to use */, |
---|
| 375 | const void **data /**< Pointer to pointer that will point to data */, |
---|
[3431] | 376 | size_t *length /**< The length of the data read */) { |
---|
| 377 | if ( data == NULL || length == NULL ) |
---|
| 378 | return -1; |
---|
| 379 | |
---|
| 380 | *data = NULL; |
---|
| 381 | *length = 0; |
---|
| 382 | |
---|
| 383 | if ( p == NULL ) |
---|
| 384 | return -1; |
---|
| 385 | |
---|
| 386 | if ( p->iobuffer == NULL ) |
---|
| 387 | return 0; |
---|
| 388 | |
---|
| 389 | if ( roar_buffer_get_len(p->iobuffer, length) == -1 ) { |
---|
| 390 | *length = 0; |
---|
| 391 | return -1; |
---|
| 392 | } |
---|
| 393 | |
---|
| 394 | if ( roar_buffer_get_data(p->iobuffer, (void**)data) == -1 ) { |
---|
| 395 | *length = 0; |
---|
| 396 | *data = NULL; |
---|
| 397 | return -1; |
---|
| 398 | } |
---|
| 399 | |
---|
| 400 | return 0; |
---|
| 401 | } |
---|
[3424] | 402 | |
---|
| 403 | /** Remove the current fragment on record streams. It is invalid to do this without first |
---|
| 404 | * calling pa_stream_peek(). \since 0.8 */ |
---|
[3431] | 405 | int pa_stream_drop(pa_stream *p) { |
---|
| 406 | if ( p == NULL ) |
---|
| 407 | return -1; |
---|
| 408 | |
---|
| 409 | if ( p->iobuffer == NULL ) |
---|
| 410 | return -1; |
---|
| 411 | |
---|
| 412 | return roar_buffer_next(&(p->iobuffer)); |
---|
| 413 | } |
---|
[3424] | 414 | |
---|
| 415 | /** Return the nember of bytes that may be written using pa_stream_write() */ |
---|
[3430] | 416 | size_t pa_stream_writable_size(pa_stream *p) { |
---|
| 417 | struct roar_buffer_stats stats; |
---|
| 418 | |
---|
| 419 | if ( p == NULL ) |
---|
| 420 | return 0; |
---|
| 421 | |
---|
| 422 | if ( p->iobuffer == NULL ) |
---|
| 423 | return 0; |
---|
| 424 | |
---|
| 425 | if ( roar_buffer_ring_stats(p->iobuffer, &stats) == -1 ) |
---|
| 426 | return 0; |
---|
| 427 | |
---|
| 428 | if ( stats.parts > p->fragments.num ) |
---|
| 429 | return 0; |
---|
| 430 | |
---|
| 431 | return (p->fragments.num - stats.parts)*p->fragments.size; |
---|
| 432 | } |
---|
[3424] | 433 | |
---|
| 434 | /** Return the number of bytes that may be read using pa_stream_read() \since 0.8 */ |
---|
[3430] | 435 | size_t pa_stream_readable_size(pa_stream *p) { |
---|
| 436 | struct roar_buffer_stats stats; |
---|
| 437 | |
---|
| 438 | if ( p == NULL ) |
---|
| 439 | return 0; |
---|
| 440 | |
---|
| 441 | if ( p->iobuffer == NULL ) |
---|
| 442 | return 0; |
---|
| 443 | |
---|
| 444 | if ( roar_buffer_ring_stats(p->iobuffer, &stats) == -1 ) |
---|
| 445 | return 0; |
---|
| 446 | |
---|
| 447 | return stats.bytes; |
---|
| 448 | } |
---|
[3424] | 449 | |
---|
| 450 | /** Drain a playback stream. Use this for notification when the buffer is empty */ |
---|
[3449] | 451 | pa_operation* pa_stream_drain(pa_stream *s, pa_stream_success_cb_t cb, void *userdata) { |
---|
| 452 | if ( s == NULL ) |
---|
| 453 | return NULL; |
---|
| 454 | |
---|
| 455 | s->cb.drain.cb.scb = cb; |
---|
| 456 | s->cb.drain.userdata = userdata; |
---|
| 457 | |
---|
| 458 | if ( s->op.drain == NULL ) { |
---|
| 459 | s->op.drain = roar_pa_operation_new(PA_OPERATION_RUNNING); |
---|
| 460 | } |
---|
| 461 | |
---|
| 462 | pa_operation_ref(s->op.drain); |
---|
| 463 | |
---|
| 464 | return s->op.drain; |
---|
| 465 | } |
---|
[3424] | 466 | |
---|
| 467 | /** Request a timing info structure update for a stream. Use |
---|
| 468 | * pa_stream_get_timing_info() to get access to the raw timing data, |
---|
| 469 | * or pa_stream_get_time() or pa_stream_get_latency() to get cleaned |
---|
| 470 | * up values. */ |
---|
[3450] | 471 | pa_operation* pa_stream_update_timing_info(pa_stream *p, pa_stream_success_cb_t cb, void *userdata) { |
---|
| 472 | int suc = 1; |
---|
| 473 | |
---|
| 474 | if ( p == NULL ) |
---|
| 475 | return NULL; |
---|
| 476 | |
---|
| 477 | if ( roar_get_stream(roar_pa_context_get_con(p->c), &(p->stream), p->stream.id) == -1 ) { |
---|
| 478 | suc = 0; |
---|
| 479 | } |
---|
| 480 | |
---|
[3451] | 481 | // p->timinginfo |
---|
| 482 | pa_gettimeofday(&(p->timinginfo.timestamp)); // we should interpolate between time before call and after |
---|
| 483 | |
---|
| 484 | p->timinginfo.synchronized_clocks = 0; |
---|
| 485 | p->timinginfo.sink_usec = 0; |
---|
| 486 | p->timinginfo.source_usec = 0; |
---|
| 487 | p->timinginfo.transport_usec = 0; |
---|
| 488 | p->timinginfo.playing = p->iobuffer != NULL; |
---|
| 489 | p->timinginfo.write_index_corrupt = 1; |
---|
| 490 | p->timinginfo.write_index = p->stream.pos * pa_frame_size(&(p->sspec)); |
---|
| 491 | p->timinginfo.read_index_corrupt = 1; |
---|
| 492 | p->timinginfo.read_index = p->stream.pos * pa_frame_size(&(p->sspec)); |
---|
| 493 | #if 0 /* newer versions */ |
---|
| 494 | p->timinginfo.configured_sink_usec = p->timinginfo.sink_usec; |
---|
| 495 | p->timinginfo.configured_source_usec = p->timinginfo.source_usec; |
---|
| 496 | p->timinginfo.since_underrun = 0; |
---|
| 497 | #endif |
---|
| 498 | |
---|
[3450] | 499 | if ( cb != NULL ) { |
---|
| 500 | cb(p, suc, userdata); |
---|
| 501 | } |
---|
| 502 | |
---|
| 503 | return roar_pa_op_new_done(); |
---|
| 504 | } |
---|
[3424] | 505 | |
---|
| 506 | /** Set the callback function that is called whenever the state of the stream changes */ |
---|
[3425] | 507 | void pa_stream_set_state_callback(pa_stream *s, pa_stream_notify_cb_t cb, void *userdata) { |
---|
[3441] | 508 | ROAR_DBG("pa_stream_set_state_callback(s=%p, cb=%p, userdata=%p) = ?", s, cb, userdata); |
---|
| 509 | |
---|
[3425] | 510 | if ( s == NULL ) |
---|
| 511 | return; |
---|
| 512 | |
---|
[3428] | 513 | s->cb.change_state.cb.ncb = cb; |
---|
| 514 | s->cb.change_state.userdata = userdata; |
---|
[3425] | 515 | } |
---|
| 516 | |
---|
| 517 | void pa_stream_set_state(pa_stream *s, pa_stream_state_t st) { |
---|
| 518 | if ( s == NULL ) |
---|
| 519 | return; |
---|
| 520 | |
---|
[3441] | 521 | ROAR_DBG("pa_stream_set_state(s=%p, st=%i): State: %i->%i", s, st, s->state, st); |
---|
| 522 | |
---|
[3425] | 523 | s->state = st; |
---|
| 524 | |
---|
[3441] | 525 | if ( s->cb.change_state.cb.ncb != NULL ) { |
---|
| 526 | ROAR_DBG("pa_stream_set_state(s=%p, st=%i): calling callback at %p", s, st, s->cb.change_state.cb.ncb); |
---|
[3428] | 527 | s->cb.change_state.cb.ncb(s, s->cb.change_state.userdata); |
---|
[3425] | 528 | } |
---|
[3441] | 529 | ROAR_DBG("pa_stream_set_state(s=%p, st=%i) = (void)", s, st); |
---|
[3425] | 530 | } |
---|
[3424] | 531 | |
---|
| 532 | /** Set the callback function that is called when new data may be |
---|
| 533 | * written to the stream. */ |
---|
[3429] | 534 | void pa_stream_set_write_callback(pa_stream *p, pa_stream_request_cb_t cb, void *userdata) { |
---|
| 535 | if ( p == NULL ) |
---|
| 536 | return; |
---|
| 537 | |
---|
| 538 | p->cb.write.cb.rcb = cb; |
---|
| 539 | p->cb.write.userdata = userdata; |
---|
| 540 | } |
---|
[3424] | 541 | |
---|
| 542 | /** Set the callback function that is called when new data is available from the stream. |
---|
| 543 | * Return the number of bytes read. \since 0.8 */ |
---|
[3429] | 544 | void pa_stream_set_read_callback(pa_stream *p, pa_stream_request_cb_t cb, void *userdata) { |
---|
| 545 | if ( p == NULL ) |
---|
| 546 | return; |
---|
| 547 | |
---|
| 548 | p->cb.read.cb.rcb = cb; |
---|
| 549 | p->cb.read.userdata = userdata; |
---|
| 550 | } |
---|
[3424] | 551 | |
---|
| 552 | /** Set the callback function that is called when a buffer overflow happens. (Only for playback streams) \since 0.8 */ |
---|
[3429] | 553 | void pa_stream_set_overflow_callback(pa_stream *p, pa_stream_notify_cb_t cb, void *userdata) { |
---|
| 554 | if ( p == NULL ) |
---|
| 555 | return; |
---|
| 556 | |
---|
| 557 | p->cb.overflow.cb.ncb = cb; |
---|
| 558 | p->cb.overflow.userdata = userdata; |
---|
| 559 | } |
---|
[3424] | 560 | |
---|
| 561 | /** Set the callback function that is called when a buffer underflow happens. (Only for playback streams) \since 0.8 */ |
---|
[3429] | 562 | void pa_stream_set_underflow_callback(pa_stream *p, pa_stream_notify_cb_t cb, void *userdata) { |
---|
| 563 | if ( p == NULL ) |
---|
| 564 | return; |
---|
| 565 | |
---|
| 566 | p->cb.underflow.cb.ncb = cb; |
---|
| 567 | p->cb.underflow.userdata = userdata; |
---|
| 568 | } |
---|
[3424] | 569 | |
---|
| 570 | /** 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] | 571 | void pa_stream_set_latency_update_callback(pa_stream *p, pa_stream_notify_cb_t cb, void *userdata) { |
---|
| 572 | if ( p == NULL ) |
---|
| 573 | return; |
---|
| 574 | |
---|
| 575 | p->cb.latency.cb.ncb = cb; |
---|
| 576 | p->cb.latency.userdata = userdata; |
---|
| 577 | } |
---|
[3424] | 578 | |
---|
| 579 | /** Pause (or resume) playback of this stream temporarily. Available on both playback and recording streams. \since 0.3 */ |
---|
| 580 | pa_operation* pa_stream_cork(pa_stream *s, int b, pa_stream_success_cb_t cb, void *userdata); |
---|
| 581 | |
---|
| 582 | /** Flush the playback buffer of this stream. Most of the time you're |
---|
| 583 | * better off using the parameter delta of pa_stream_write() instead of this |
---|
| 584 | * function. Available on both playback and recording streams. \since 0.3 */ |
---|
[3452] | 585 | pa_operation* pa_stream_flush(pa_stream *s, pa_stream_success_cb_t cb, void *userdata) { |
---|
| 586 | return pa_stream_drain(s, cb, userdata); // where is the differance to drain? |
---|
| 587 | } |
---|
[3424] | 588 | /** Reenable prebuffering as specified in the pa_buffer_attr |
---|
| 589 | * structure. Available for playback streams only. \since 0.6 */ |
---|
| 590 | pa_operation* pa_stream_prebuf(pa_stream *s, pa_stream_success_cb_t cb, void *userdata); |
---|
| 591 | |
---|
| 592 | /** Request immediate start of playback on this stream. This disables |
---|
| 593 | * prebuffering as specified in the pa_buffer_attr |
---|
| 594 | * structure, temporarily. Available for playback streams only. \since 0.3 */ |
---|
| 595 | pa_operation* pa_stream_trigger(pa_stream *s, pa_stream_success_cb_t cb, void *userdata); |
---|
| 596 | |
---|
| 597 | /** Rename the stream. \since 0.5 */ |
---|
| 598 | pa_operation* pa_stream_set_name(pa_stream *s, const char *name, pa_stream_success_cb_t cb, void *userdata); |
---|
| 599 | |
---|
| 600 | /** Return the current playback/recording time. This is based on the |
---|
| 601 | * data in the timing info structure returned by |
---|
| 602 | * pa_stream_get_timing_info(). This function will usually only return |
---|
| 603 | * new data if a timing info update has been recieved. Only if timing |
---|
| 604 | * interpolation has been requested (PA_STREAM_INTERPOLATE_TIMING) |
---|
| 605 | * the data from the last timing update is used for an estimation of |
---|
| 606 | * the current playback/recording time based on the local time that |
---|
| 607 | * passed since the timing info structure has been acquired. The time |
---|
| 608 | * value returned by this function is guaranteed to increase |
---|
| 609 | * monotonically. (that means: the returned value is always greater or |
---|
| 610 | * equal to the value returned on the last call) This behaviour can |
---|
| 611 | * be disabled by using PA_STREAM_NOT_MONOTONOUS. This may be |
---|
| 612 | * desirable to deal better with bad estimations of transport |
---|
| 613 | * latencies, but may have strange effects if the application is not |
---|
| 614 | * able to deal with time going 'backwards'. \since 0.6 */ |
---|
[3453] | 615 | int pa_stream_get_time(pa_stream *s, pa_usec_t *r_usec) { |
---|
| 616 | if ( s == NULL || r_usec == NULL ) |
---|
| 617 | return -1; |
---|
| 618 | |
---|
| 619 | *r_usec = s->stream.pos * 1000000 / s->stream.info.rate / s->stream.info.channels; |
---|
| 620 | |
---|
| 621 | return 0; |
---|
| 622 | } |
---|
[3424] | 623 | |
---|
| 624 | /** Return the total stream latency. This function is based on |
---|
| 625 | * pa_stream_get_time(). In case the stream is a monitoring stream the |
---|
| 626 | * result can be negative, i.e. the captured samples are not yet |
---|
| 627 | * played. In this case *negative is set to 1. \since 0.6 */ |
---|
[3453] | 628 | int pa_stream_get_latency(pa_stream *s, pa_usec_t *r_usec, int *negative) { |
---|
| 629 | // TODO: Fix this: |
---|
| 630 | // sinks: lateny of stream, mixer, output... |
---|
| 631 | // sources: mixer, output, negative |
---|
| 632 | |
---|
| 633 | if ( r_usec != NULL ) |
---|
| 634 | *r_usec = 0; |
---|
| 635 | |
---|
| 636 | if ( negative != NULL ) |
---|
| 637 | *negative = 0; |
---|
| 638 | |
---|
| 639 | return 0; |
---|
| 640 | } |
---|
[3424] | 641 | |
---|
| 642 | /** Return the latest raw timing data structure. The returned pointer |
---|
| 643 | * points to an internal read-only instance of the timing |
---|
| 644 | * structure. The user should make a copy of this structure if he |
---|
| 645 | * wants to modify it. An in-place update to this data structure may |
---|
| 646 | * be requested using pa_stream_update_timing_info(). If no |
---|
| 647 | * pa_stream_update_timing_info() call was issued before, this |
---|
| 648 | * function will fail with PA_ERR_NODATA. Please note that the |
---|
| 649 | * write_index member field (and only this field) is updated on each |
---|
| 650 | * pa_stream_write() call, not just when a timing update has been |
---|
| 651 | * recieved. \since 0.8 */ |
---|
[3451] | 652 | const pa_timing_info* pa_stream_get_timing_info(pa_stream *s) { |
---|
| 653 | if ( s == NULL ) |
---|
| 654 | return NULL; |
---|
| 655 | |
---|
| 656 | return &(s->timinginfo); |
---|
| 657 | } |
---|
[3424] | 658 | |
---|
| 659 | /** Return a pointer to the stream's sample specification. \since 0.6 */ |
---|
[3427] | 660 | const pa_sample_spec* pa_stream_get_sample_spec(pa_stream *s) { |
---|
| 661 | if ( s == NULL ) |
---|
| 662 | return NULL; |
---|
| 663 | |
---|
| 664 | return &(s->sspec); |
---|
| 665 | } |
---|
[3424] | 666 | |
---|
| 667 | /** Return a pointer to the stream's channel map. \since 0.8 */ |
---|
| 668 | const pa_channel_map* pa_stream_get_channel_map(pa_stream *s); |
---|
| 669 | |
---|
| 670 | /** Return the buffer metrics of the stream. Only valid after the |
---|
| 671 | * stream has been connected successfuly and if the server is at least |
---|
| 672 | * PulseAudio 0.9. \since 0.9.0 */ |
---|
| 673 | const pa_buffer_attr* pa_stream_get_buffer_attr(pa_stream *s); |
---|
| 674 | |
---|
[3423] | 675 | //ll |
---|