source: roaraudio/libroarpulse/stream.c @ 3433:5fb9bfd5e810

Last change on this file since 3433:5fb9bfd5e810 was 3433:5fb9bfd5e810, checked in by phi, 14 years ago

pa_stream_connect_record() and pa_stream_connect_playback() -> _roar_pa_stream_open()

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