source: roaraudio/libroarpulse/stream.c @ 3465:23962b935804

Last change on this file since 3465:23962b935804 was 3465:23962b935804, checked in by phi, 14 years ago

start to support writeout of playback buffer

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