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