source: roaraudio/libroarpulse/stream.c @ 5300:190af1adf91c

Last change on this file since 5300:190af1adf91c was 5300:190af1adf91c, checked in by phi, 8 years ago

first commit to move away from old roar_buffer_add() to roar_buffer_moveinto() (See: #126)

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