source: roaraudio/libroarpulse/stream.c @ 3440:7730bc584725

Last change on this file since 3440:7730bc584725 was 3440:7730bc584725, checked in by phi, 14 years ago

added _roar_pa_stream_ioecb(), added some debug lions

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