source: roaraudio/libroarpulse/stream.c @ 4896:0cd0dc3bc104

Last change on this file since 4896:0cd0dc3bc104 was 4708:c9d40761088a, checked in by phi, 13 years ago

updated copyright statements

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