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