source: roaraudio/libroarpulse/stream.c @ 5348:83fcc9598253

Last change on this file since 5348:83fcc9598253 was 5348:83fcc9598253, checked in by phi, 8 years ago

Converted roar_buffer API to fully use refcounter (Closes: #126)

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