]> code.delx.au - pulseaudio/blob - src/pulsecore/sink-input.c
commit glitch-free work
[pulseaudio] / src / pulsecore / sink-input.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 Copyright 2004-2006 Lennart Poettering
7 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
8
9 PulseAudio is free software; you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as published
11 by the Free Software Foundation; either version 2 of the License,
12 or (at your option) any later version.
13
14 PulseAudio is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public License
20 along with PulseAudio; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 USA.
23 ***/
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32
33 #include <pulse/utf8.h>
34 #include <pulse/xmalloc.h>
35
36 #include <pulsecore/sample-util.h>
37 #include <pulsecore/core-subscribe.h>
38 #include <pulsecore/log.h>
39 #include <pulsecore/play-memblockq.h>
40 #include <pulsecore/namereg.h>
41
42 #include "sink-input.h"
43
44 #define MEMBLOCKQ_MAXLENGTH (16*1024*1024)
45 #define CONVERT_BUFFER_LENGTH (PA_PAGE_SIZE)
46 #define MOVE_BUFFER_LENGTH (PA_PAGE_SIZE*256)
47
48 static PA_DEFINE_CHECK_TYPE(pa_sink_input, pa_msgobject);
49
50 static void sink_input_free(pa_object *o);
51
52 pa_sink_input_new_data* pa_sink_input_new_data_init(pa_sink_input_new_data *data) {
53 pa_assert(data);
54
55 memset(data, 0, sizeof(*data));
56 data->resample_method = PA_RESAMPLER_INVALID;
57 data->proplist = pa_proplist_new();
58
59 return data;
60 }
61
62 void pa_sink_input_new_data_set_sample_spec(pa_sink_input_new_data *data, const pa_sample_spec *spec) {
63 pa_assert(data);
64
65 if ((data->sample_spec_is_set = !!spec))
66 data->sample_spec = *spec;
67 }
68
69 void pa_sink_input_new_data_set_channel_map(pa_sink_input_new_data *data, const pa_channel_map *map) {
70 pa_assert(data);
71
72 if ((data->channel_map_is_set = !!map))
73 data->channel_map = *map;
74 }
75
76 void pa_sink_input_new_data_set_volume(pa_sink_input_new_data *data, const pa_cvolume *volume) {
77 pa_assert(data);
78
79 if ((data->volume_is_set = !!volume))
80 data->volume = *volume;
81 }
82
83 void pa_sink_input_new_data_set_muted(pa_sink_input_new_data *data, pa_bool_t mute) {
84 pa_assert(data);
85
86 data->muted_is_set = TRUE;
87 data->muted = !!mute;
88 }
89
90 void pa_sink_input_new_data_done(pa_sink_input_new_data *data) {
91 pa_assert(data);
92
93 pa_proplist_free(data->proplist);
94 }
95
96 pa_sink_input* pa_sink_input_new(
97 pa_core *core,
98 pa_sink_input_new_data *data,
99 pa_sink_input_flags_t flags) {
100
101 pa_sink_input *i;
102 pa_resampler *resampler = NULL;
103 char st[PA_SAMPLE_SPEC_SNPRINT_MAX], cm[PA_CHANNEL_MAP_SNPRINT_MAX];
104 pa_memblock *silence;
105
106 pa_assert(core);
107 pa_assert(data);
108
109 if (pa_hook_fire(&core->hooks[PA_CORE_HOOK_SINK_INPUT_NEW], data) < 0)
110 return NULL;
111
112 pa_return_null_if_fail(!data->driver || pa_utf8_valid(data->driver));
113
114 if (!data->sink)
115 data->sink = pa_namereg_get(core, NULL, PA_NAMEREG_SINK, 1);
116
117 pa_return_null_if_fail(data->sink);
118 pa_return_null_if_fail(pa_sink_get_state(data->sink) != PA_SINK_UNLINKED);
119 pa_return_null_if_fail(!data->sync_base || (data->sync_base->sink == data->sink && pa_sink_input_get_state(data->sync_base) == PA_SINK_INPUT_CORKED));
120
121 if (!data->sample_spec_is_set)
122 data->sample_spec = data->sink->sample_spec;
123
124 pa_return_null_if_fail(pa_sample_spec_valid(&data->sample_spec));
125
126 if (!data->channel_map_is_set) {
127 if (data->sink->channel_map.channels == data->sample_spec.channels)
128 data->channel_map = data->sink->channel_map;
129 else
130 pa_return_null_if_fail(pa_channel_map_init_auto(&data->channel_map, data->sample_spec.channels, PA_CHANNEL_MAP_DEFAULT));
131 }
132
133 pa_return_null_if_fail(pa_channel_map_valid(&data->channel_map));
134 pa_return_null_if_fail(data->channel_map.channels == data->sample_spec.channels);
135
136 if (!data->volume_is_set)
137 pa_cvolume_reset(&data->volume, data->sample_spec.channels);
138
139 pa_return_null_if_fail(pa_cvolume_valid(&data->volume));
140 pa_return_null_if_fail(data->volume.channels == data->sample_spec.channels);
141
142 if (!data->muted_is_set)
143 data->muted = FALSE;
144
145 if (flags & PA_SINK_INPUT_FIX_FORMAT)
146 data->sample_spec.format = data->sink->sample_spec.format;
147
148 if (flags & PA_SINK_INPUT_FIX_RATE)
149 data->sample_spec.rate = data->sink->sample_spec.rate;
150
151 if (flags & PA_SINK_INPUT_FIX_CHANNELS) {
152 data->sample_spec.channels = data->sink->sample_spec.channels;
153 data->channel_map = data->sink->channel_map;
154 }
155
156 pa_assert(pa_sample_spec_valid(&data->sample_spec));
157 pa_assert(pa_channel_map_valid(&data->channel_map));
158
159 /* Due to the fixing of the sample spec the volume might not match anymore */
160 if (data->volume.channels != data->sample_spec.channels)
161 pa_cvolume_set(&data->volume, data->sample_spec.channels, pa_cvolume_avg(&data->volume));
162
163 if (data->resample_method == PA_RESAMPLER_INVALID)
164 data->resample_method = core->resample_method;
165
166 pa_return_null_if_fail(data->resample_method < PA_RESAMPLER_MAX);
167
168 if (pa_hook_fire(&core->hooks[PA_CORE_HOOK_SINK_INPUT_FIXATE], data) < 0)
169 return NULL;
170
171 if (pa_idxset_size(data->sink->inputs) >= PA_MAX_INPUTS_PER_SINK) {
172 pa_log_warn("Failed to create sink input: too many inputs per sink.");
173 return NULL;
174 }
175
176 if ((flags & PA_SINK_INPUT_VARIABLE_RATE) ||
177 !pa_sample_spec_equal(&data->sample_spec, &data->sink->sample_spec) ||
178 !pa_channel_map_equal(&data->channel_map, &data->sink->channel_map)) {
179
180 if (!(resampler = pa_resampler_new(
181 core->mempool,
182 &data->sample_spec, &data->channel_map,
183 &data->sink->sample_spec, &data->sink->channel_map,
184 data->resample_method,
185 ((flags & PA_SINK_INPUT_VARIABLE_RATE) ? PA_RESAMPLER_VARIABLE_RATE : 0) |
186 ((flags & PA_SINK_INPUT_NO_REMAP) ? PA_RESAMPLER_NO_REMAP : 0) |
187 (core->disable_remixing || (flags & PA_SINK_INPUT_NO_REMIX) ? PA_RESAMPLER_NO_REMIX : 0)))) {
188 pa_log_warn("Unsupported resampling operation.");
189 return NULL;
190 }
191
192 data->resample_method = pa_resampler_get_method(resampler);
193 }
194
195 i = pa_msgobject_new(pa_sink_input);
196 i->parent.parent.free = sink_input_free;
197 i->parent.process_msg = pa_sink_input_process_msg;
198
199 i->core = core;
200 i->state = PA_SINK_INPUT_INIT;
201 i->flags = flags;
202 i->proplist = pa_proplist_copy(data->proplist);
203 i->driver = pa_xstrdup(data->driver);
204 i->module = data->module;
205 i->sink = data->sink;
206 i->client = data->client;
207
208 i->resample_method = data->resample_method;
209 i->sample_spec = data->sample_spec;
210 i->channel_map = data->channel_map;
211
212 i->volume = data->volume;
213 i->muted = data->muted;
214
215 if (data->sync_base) {
216 i->sync_next = data->sync_base->sync_next;
217 i->sync_prev = data->sync_base;
218
219 if (data->sync_base->sync_next)
220 data->sync_base->sync_next->sync_prev = i;
221 data->sync_base->sync_next = i;
222 } else
223 i->sync_next = i->sync_prev = NULL;
224
225 i->pop = NULL;
226 i->rewind = NULL;
227 i->set_max_rewind = NULL;
228 i->kill = NULL;
229 i->get_latency = NULL;
230 i->attach = NULL;
231 i->detach = NULL;
232 i->suspend = NULL;
233 i->moved = NULL;
234 i->userdata = NULL;
235
236 i->thread_info.state = i->state;
237 i->thread_info.attached = FALSE;
238 pa_atomic_store(&i->thread_info.drained, 1);
239 pa_atomic_store(&i->thread_info.render_memblockq_is_empty, 0);
240 i->thread_info.sample_spec = i->sample_spec;
241 i->thread_info.resampler = resampler;
242 i->thread_info.volume = i->volume;
243 i->thread_info.muted = i->muted;
244 i->thread_info.requested_sink_latency = 0;
245 i->thread_info.rewrite_nbytes = 0;
246 i->thread_info.ignore_rewind = FALSE;
247
248 silence = pa_silence_memblock_new(i->sink->core->mempool, &i->sink->sample_spec, 0);
249
250 i->thread_info.render_memblockq = pa_memblockq_new(
251 0,
252 MEMBLOCKQ_MAXLENGTH,
253 0,
254 pa_frame_size(&i->sink->sample_spec),
255 0,
256 1,
257 0,
258 silence);
259
260 pa_memblock_unref(silence);
261
262 pa_assert_se(pa_idxset_put(core->sink_inputs, pa_sink_input_ref(i), &i->index) == 0);
263 pa_assert_se(pa_idxset_put(i->sink->inputs, i, NULL) == 0);
264
265 pa_log_info("Created input %u \"%s\" on %s with sample spec %s and channel map %s",
266 i->index,
267 pa_strnull(pa_proplist_gets(i->proplist, PA_PROP_MEDIA_NAME)),
268 i->sink->name,
269 pa_sample_spec_snprint(st, sizeof(st), &i->sample_spec),
270 pa_channel_map_snprint(cm, sizeof(cm), &i->channel_map));
271
272 /* Don't forget to call pa_sink_input_put! */
273
274 return i;
275 }
276
277 static void update_n_corked(pa_sink_input *i, pa_sink_input_state_t state) {
278 pa_assert(i);
279
280 if (i->state == PA_SINK_INPUT_CORKED && state != PA_SINK_INPUT_CORKED)
281 pa_assert_se(i->sink->n_corked -- >= 1);
282 else if (i->state != PA_SINK_INPUT_CORKED && state == PA_SINK_INPUT_CORKED)
283 i->sink->n_corked++;
284
285 pa_sink_update_status(i->sink);
286 }
287
288 static int sink_input_set_state(pa_sink_input *i, pa_sink_input_state_t state) {
289 pa_sink_input *ssync;
290 pa_assert(i);
291
292 if (state == PA_SINK_INPUT_DRAINED)
293 state = PA_SINK_INPUT_RUNNING;
294
295 if (i->state == state)
296 return 0;
297
298 if (pa_asyncmsgq_send(i->sink->asyncmsgq, PA_MSGOBJECT(i), PA_SINK_INPUT_MESSAGE_SET_STATE, PA_UINT_TO_PTR(state), 0, NULL) < 0)
299 return -1;
300
301 update_n_corked(i, state);
302 i->state = state;
303
304 for (ssync = i->sync_prev; ssync; ssync = ssync->sync_prev) {
305 update_n_corked(ssync, state);
306 ssync->state = state;
307 }
308 for (ssync = i->sync_next; ssync; ssync = ssync->sync_next) {
309 update_n_corked(ssync, state);
310 ssync->state = state;
311 }
312
313 if (state != PA_SINK_INPUT_UNLINKED)
314 pa_hook_fire(&i->sink->core->hooks[PA_CORE_HOOK_SINK_INPUT_STATE_CHANGED], i);
315
316 return 0;
317 }
318
319 void pa_sink_input_unlink(pa_sink_input *i) {
320 pa_bool_t linked;
321 pa_assert(i);
322
323 /* See pa_sink_unlink() for a couple of comments how this function
324 * works */
325
326 pa_sink_input_ref(i);
327
328 linked = PA_SINK_INPUT_LINKED(i->state);
329
330 if (linked)
331 pa_hook_fire(&i->sink->core->hooks[PA_CORE_HOOK_SINK_INPUT_UNLINK], i);
332
333 if (i->sync_prev)
334 i->sync_prev->sync_next = i->sync_next;
335 if (i->sync_next)
336 i->sync_next->sync_prev = i->sync_prev;
337
338 i->sync_prev = i->sync_next = NULL;
339
340 pa_idxset_remove_by_data(i->sink->core->sink_inputs, i, NULL);
341 if (pa_idxset_remove_by_data(i->sink->inputs, i, NULL))
342 pa_sink_input_unref(i);
343
344 if (linked) {
345 pa_asyncmsgq_send(i->sink->asyncmsgq, PA_MSGOBJECT(i->sink), PA_SINK_MESSAGE_REMOVE_INPUT, i, 0, NULL);
346 sink_input_set_state(i, PA_SINK_INPUT_UNLINKED);
347 pa_sink_update_status(i->sink);
348 } else
349 i->state = PA_SINK_INPUT_UNLINKED;
350
351 i->pop = NULL;
352 i->rewind = NULL;
353 i->set_max_rewind = NULL;
354 i->kill = NULL;
355 i->get_latency = NULL;
356 i->attach = NULL;
357 i->detach = NULL;
358 i->suspend = NULL;
359 i->moved = NULL;
360
361 if (linked) {
362 pa_subscription_post(i->sink->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_REMOVE, i->index);
363 pa_hook_fire(&i->sink->core->hooks[PA_CORE_HOOK_SINK_INPUT_UNLINK_POST], i);
364 }
365
366 i->sink = NULL;
367 pa_sink_input_unref(i);
368 }
369
370 static void sink_input_free(pa_object *o) {
371 pa_sink_input* i = PA_SINK_INPUT(o);
372
373 pa_assert(i);
374 pa_assert(pa_sink_input_refcnt(i) == 0);
375
376 if (PA_SINK_INPUT_LINKED(i->state))
377 pa_sink_input_unlink(i);
378
379 pa_log_info("Freeing input %u \"%s\"", i->index, pa_strnull(pa_proplist_gets(i->proplist, PA_PROP_MEDIA_NAME)));
380
381 pa_assert(!i->thread_info.attached);
382
383 if (i->thread_info.render_memblockq)
384 pa_memblockq_free(i->thread_info.render_memblockq);
385
386 if (i->thread_info.resampler)
387 pa_resampler_free(i->thread_info.resampler);
388
389 if (i->proplist)
390 pa_proplist_free(i->proplist);
391
392 pa_xfree(i->driver);
393 pa_xfree(i);
394 }
395
396 void pa_sink_input_put(pa_sink_input *i) {
397 pa_sink_input_assert_ref(i);
398
399 pa_assert(i->state == PA_SINK_INPUT_INIT);
400 pa_assert(i->pop);
401 pa_assert(i->rewind);
402
403 i->thread_info.state = i->state = i->flags & PA_SINK_INPUT_START_CORKED ? PA_SINK_INPUT_CORKED : PA_SINK_INPUT_RUNNING;
404 i->thread_info.volume = i->volume;
405 i->thread_info.muted = i->muted;
406
407 if (i->state == PA_SINK_INPUT_CORKED)
408 i->sink->n_corked++;
409
410 pa_sink_update_status(i->sink);
411 pa_asyncmsgq_send(i->sink->asyncmsgq, PA_MSGOBJECT(i->sink), PA_SINK_MESSAGE_ADD_INPUT, i, 0, NULL);
412
413 pa_subscription_post(i->sink->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_NEW, i->index);
414 pa_hook_fire(&i->sink->core->hooks[PA_CORE_HOOK_SINK_INPUT_PUT], i);
415
416 /* Please note that if you change something here, you have to
417 change something in pa_sink_input_move() with the ghost stream
418 registration too. */
419 }
420
421 void pa_sink_input_kill(pa_sink_input*i) {
422 pa_sink_input_assert_ref(i);
423 pa_assert(PA_SINK_INPUT_LINKED(i->state));
424
425 if (i->kill)
426 i->kill(i);
427 }
428
429 pa_usec_t pa_sink_input_get_latency(pa_sink_input *i) {
430 pa_usec_t r = 0;
431
432 pa_sink_input_assert_ref(i);
433 pa_assert(PA_SINK_INPUT_LINKED(i->state));
434
435 if (pa_asyncmsgq_send(i->sink->asyncmsgq, PA_MSGOBJECT(i), PA_SINK_INPUT_MESSAGE_GET_LATENCY, &r, 0, NULL) < 0)
436 r = 0;
437
438 if (i->get_latency)
439 r += i->get_latency(i);
440
441 return r;
442 }
443
444 /* Called from thread context */
445 int pa_sink_input_peek(pa_sink_input *i, size_t slength /* in sink frames */, pa_memchunk *chunk, pa_cvolume *volume) {
446 pa_bool_t do_volume_adj_here;
447 pa_bool_t volume_is_norm;
448 size_t block_size_max_sink, block_size_max_sink_input;
449 size_t ilength;
450
451 pa_sink_input_assert_ref(i);
452 pa_assert(PA_SINK_INPUT_LINKED(i->thread_info.state));
453 pa_assert(pa_frame_aligned(slength, &i->sink->sample_spec));
454 pa_assert(chunk);
455 pa_assert(volume);
456
457 pa_log_debug("peek");
458
459 if (!i->pop || i->thread_info.state == PA_SINK_INPUT_CORKED)
460 return -1;
461
462 pa_assert(i->thread_info.state == PA_SINK_INPUT_RUNNING || i->thread_info.state == PA_SINK_INPUT_DRAINED);
463
464 /* If there's still some rewrite request the handle, but the sink
465 didn't do this for us, we do it here. However, since the sink
466 apparently doesn't support rewinding, we pass 0 here. This still
467 allows rewinding through the render buffer. */
468 pa_sink_input_rewind(i, 0);
469
470 block_size_max_sink_input = i->thread_info.resampler ?
471 pa_resampler_max_block_size(i->thread_info.resampler) :
472 pa_frame_align(pa_mempool_block_size_max(i->sink->core->mempool), &i->sample_spec);
473
474 block_size_max_sink = pa_frame_align(pa_mempool_block_size_max(i->sink->core->mempool), &i->sink->sample_spec);
475
476 /* Default buffer size */
477 if (slength <= 0)
478 slength = pa_frame_align(CONVERT_BUFFER_LENGTH, &i->sink->sample_spec);
479
480 if (slength > block_size_max_sink)
481 slength = block_size_max_sink;
482
483 if (i->thread_info.resampler) {
484 ilength = pa_resampler_request(i->thread_info.resampler, slength);
485
486 if (ilength <= 0)
487 ilength = pa_frame_align(CONVERT_BUFFER_LENGTH, &i->sample_spec);
488 } else
489 ilength = slength;
490
491 if (ilength > block_size_max_sink_input)
492 ilength = block_size_max_sink_input;
493
494 /* If the channel maps of the sink and this stream differ, we need
495 * to adjust the volume *before* we resample. Otherwise we can do
496 * it after and leave it for the sink code */
497
498 do_volume_adj_here = !pa_channel_map_equal(&i->channel_map, &i->sink->channel_map);
499 volume_is_norm = pa_cvolume_is_norm(&i->thread_info.volume) && !i->thread_info.muted;
500
501 while (!pa_memblockq_is_readable(i->thread_info.render_memblockq)) {
502 pa_memchunk tchunk;
503
504 /* There's nothing in our render queue. We need to fill it up
505 * with data from the implementor. */
506
507 if (i->pop(i, ilength, &tchunk) < 0) {
508 pa_atomic_store(&i->thread_info.drained, 1);
509
510 /* OK, we got no data from the implementor, so let's just skip ahead */
511 pa_memblockq_seek(i->thread_info.render_memblockq, slength, PA_SEEK_RELATIVE_ON_READ);
512 break;
513 }
514
515 pa_atomic_store(&i->thread_info.drained, 0);
516
517 pa_assert(tchunk.length > 0);
518 pa_assert(tchunk.memblock);
519
520 if (tchunk.length > block_size_max_sink_input)
521 tchunk.length = block_size_max_sink_input;
522
523 /* It might be necessary to adjust the volume here */
524 if (do_volume_adj_here && !volume_is_norm) {
525 pa_memchunk_make_writable(&tchunk, 0);
526
527 if (i->thread_info.muted)
528 pa_silence_memchunk(&tchunk, &i->thread_info.sample_spec);
529 else
530 pa_volume_memchunk(&tchunk, &i->thread_info.sample_spec, &i->thread_info.volume);
531 }
532
533 if (!i->thread_info.resampler)
534 pa_memblockq_push_align(i->thread_info.render_memblockq, &tchunk);
535 else {
536 pa_memchunk rchunk;
537 pa_resampler_run(i->thread_info.resampler, &tchunk, &rchunk);
538
539 if (rchunk.memblock) {
540 pa_memblockq_push_align(i->thread_info.render_memblockq, &rchunk);
541 pa_memblock_unref(rchunk.memblock);
542 }
543 }
544
545 pa_memblock_unref(tchunk.memblock);
546 }
547
548 pa_assert_se(pa_memblockq_peek(i->thread_info.render_memblockq, chunk) >= 0);
549
550 pa_assert(chunk->length > 0);
551 pa_assert(chunk->memblock);
552
553 if (chunk->length > block_size_max_sink)
554 chunk->length = block_size_max_sink;
555
556 /* Let's see if we had to apply the volume adjustment ourselves,
557 * or if this can be done by the sink for us */
558
559 if (do_volume_adj_here)
560 /* We had different channel maps, so we already did the adjustment */
561 pa_cvolume_reset(volume, i->sink->sample_spec.channels);
562 else if (i->thread_info.muted)
563 /* We've both the same channel map, so let's have the sink do the adjustment for us*/
564 pa_cvolume_mute(volume, i->sink->sample_spec.channels);
565 else
566 *volume = i->thread_info.volume;
567
568 pa_atomic_store(&i->thread_info.render_memblockq_is_empty, pa_memblockq_is_empty(i->thread_info.render_memblockq));
569
570 return 0;
571 }
572
573 /* Called from thread context */
574 void pa_sink_input_drop(pa_sink_input *i, size_t nbytes /* in sink sample spec */) {
575
576 pa_sink_input_assert_ref(i);
577
578 pa_assert(PA_SINK_INPUT_LINKED(i->thread_info.state));
579 pa_assert(pa_frame_aligned(nbytes, &i->sink->sample_spec));
580 pa_assert(nbytes > 0);
581
582 if (i->thread_info.state == PA_SINK_INPUT_CORKED)
583 return;
584
585 /* If there's still some rewrite request the handle, but the sink
586 didn't do this for us, we do it here. However, since the sink
587 apparently doesn't support rewinding, we pass 0 here. This still
588 allows rewinding through the render buffer. */
589 if (i->thread_info.rewrite_nbytes > 0)
590 pa_sink_input_rewind(i, 0);
591
592 pa_memblockq_drop(i->thread_info.render_memblockq, nbytes);
593
594 pa_atomic_store(&i->thread_info.render_memblockq_is_empty, pa_memblockq_is_empty(i->thread_info.render_memblockq));
595 }
596
597 /* Called from thread context */
598 void pa_sink_input_rewind(pa_sink_input *i, size_t nbytes /* in sink sample spec */) {
599 pa_sink_input_assert_ref(i);
600
601 pa_assert(PA_SINK_INPUT_LINKED(i->thread_info.state));
602 pa_assert(pa_frame_aligned(nbytes, &i->sink->sample_spec));
603
604 pa_log_debug("rewind(%u, %u)", nbytes, i->thread_info.rewrite_nbytes);
605
606 if (i->thread_info.state == PA_SINK_INPUT_CORKED)
607 return;
608
609 if (i->thread_info.ignore_rewind) {
610 i->thread_info.rewrite_nbytes = 0;
611 i->thread_info.ignore_rewind = FALSE;
612 return;
613 }
614
615 if (nbytes > 0)
616 pa_log_debug("Have to rewind %u bytes.", nbytes);
617
618 if (i->thread_info.rewrite_nbytes > 0) {
619 size_t max_rewrite;
620
621 /* Calculate how much make sense to rewrite at most */
622 if ((max_rewrite = nbytes + pa_memblockq_get_length(i->thread_info.render_memblockq)) > 0) {
623 size_t amount, r;
624
625 /* Transform into local domain */
626 if (i->thread_info.resampler)
627 max_rewrite = pa_resampler_request(i->thread_info.resampler, max_rewrite);
628
629 /* Calculate how much of the rewinded data should actually be rewritten */
630 amount = PA_MIN(max_rewrite, i->thread_info.rewrite_nbytes);
631
632 /* Convert back to to sink domain */
633 r = i->thread_info.resampler ? pa_resampler_result(i->thread_info.resampler, amount) : amount;
634
635 /* Ok, now update the write pointer */
636 pa_memblockq_seek(i->thread_info.render_memblockq, -r, PA_SEEK_RELATIVE);
637
638 /* Tell the implementor */
639 if (i->rewind)
640 i->rewind(i, amount);
641
642 /* And reset the resampler */
643 if (i->thread_info.resampler)
644 pa_resampler_reset(i->thread_info.resampler);
645 }
646
647
648 i->thread_info.rewrite_nbytes = 0;
649 }
650
651 if (nbytes > 0)
652 pa_memblockq_rewind(i->thread_info.render_memblockq, nbytes);
653 }
654
655 /* Called from thread context */
656 void pa_sink_input_set_max_rewind(pa_sink_input *i, size_t nbytes /* in the sink's sample spec */) {
657 pa_sink_input_assert_ref(i);
658
659 pa_assert(PA_SINK_INPUT_LINKED(i->thread_info.state));
660 pa_assert(pa_frame_aligned(nbytes, &i->sink->sample_spec));
661
662 pa_memblockq_set_maxrewind(i->thread_info.render_memblockq, nbytes);
663
664 if (i->set_max_rewind)
665 i->set_max_rewind(i, i->thread_info.resampler ? pa_resampler_request(i->thread_info.resampler, nbytes) : nbytes);
666 }
667
668 void pa_sink_input_set_requested_latency(pa_sink_input *i, pa_usec_t usec) {
669 pa_sink_input_assert_ref(i);
670 pa_assert(PA_SINK_INPUT_LINKED(i->state));
671
672 pa_asyncmsgq_post(i->sink->asyncmsgq, PA_MSGOBJECT(i), PA_SINK_INPUT_MESSAGE_SET_REQUESTED_LATENCY, NULL, (int64_t) usec, NULL, NULL);
673 }
674
675 void pa_sink_input_set_volume(pa_sink_input *i, const pa_cvolume *volume) {
676 pa_sink_input_assert_ref(i);
677 pa_assert(PA_SINK_INPUT_LINKED(i->state));
678
679 if (pa_cvolume_equal(&i->volume, volume))
680 return;
681
682 i->volume = *volume;
683
684 pa_asyncmsgq_post(i->sink->asyncmsgq, PA_MSGOBJECT(i), PA_SINK_INPUT_MESSAGE_SET_VOLUME, pa_xnewdup(struct pa_cvolume, volume, 1), 0, NULL, pa_xfree);
685 pa_subscription_post(i->sink->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_CHANGE, i->index);
686 }
687
688 const pa_cvolume *pa_sink_input_get_volume(pa_sink_input *i) {
689 pa_sink_input_assert_ref(i);
690 pa_assert(PA_SINK_INPUT_LINKED(i->state));
691
692 return &i->volume;
693 }
694
695 void pa_sink_input_set_mute(pa_sink_input *i, pa_bool_t mute) {
696 pa_assert(i);
697 pa_sink_input_assert_ref(i);
698 pa_assert(PA_SINK_INPUT_LINKED(i->state));
699
700 if (!i->muted == !mute)
701 return;
702
703 i->muted = mute;
704
705 pa_asyncmsgq_post(i->sink->asyncmsgq, PA_MSGOBJECT(i), PA_SINK_INPUT_MESSAGE_SET_MUTE, PA_UINT_TO_PTR(mute), 0, NULL, NULL);
706 pa_subscription_post(i->sink->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_CHANGE, i->index);
707 }
708
709 int pa_sink_input_get_mute(pa_sink_input *i) {
710 pa_sink_input_assert_ref(i);
711 pa_assert(PA_SINK_INPUT_LINKED(i->state));
712
713 return !!i->muted;
714 }
715
716 void pa_sink_input_cork(pa_sink_input *i, pa_bool_t b) {
717 pa_sink_input_assert_ref(i);
718 pa_assert(PA_SINK_INPUT_LINKED(i->state));
719
720 sink_input_set_state(i, b ? PA_SINK_INPUT_CORKED : PA_SINK_INPUT_RUNNING);
721 }
722
723 int pa_sink_input_set_rate(pa_sink_input *i, uint32_t rate) {
724 pa_sink_input_assert_ref(i);
725 pa_assert(PA_SINK_INPUT_LINKED(i->state));
726 pa_return_val_if_fail(i->thread_info.resampler, -1);
727
728 if (i->sample_spec.rate == rate)
729 return 0;
730
731 i->sample_spec.rate = rate;
732
733 pa_asyncmsgq_post(i->sink->asyncmsgq, PA_MSGOBJECT(i), PA_SINK_INPUT_MESSAGE_SET_RATE, PA_UINT_TO_PTR(rate), 0, NULL, NULL);
734
735 pa_subscription_post(i->sink->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_CHANGE, i->index);
736 return 0;
737 }
738
739 void pa_sink_input_set_name(pa_sink_input *i, const char *name) {
740 const char *old;
741 pa_sink_input_assert_ref(i);
742
743 if (!name && !pa_proplist_contains(i->proplist, PA_PROP_MEDIA_NAME))
744 return;
745
746 old = pa_proplist_gets(i->proplist, PA_PROP_MEDIA_NAME);
747
748 if (old && name && !strcmp(old, name))
749 return;
750
751 if (name)
752 pa_proplist_sets(i->proplist, PA_PROP_MEDIA_NAME, name);
753 else
754 pa_proplist_unset(i->proplist, PA_PROP_MEDIA_NAME);
755
756 if (PA_SINK_INPUT_LINKED(i->state)) {
757 pa_hook_fire(&i->sink->core->hooks[PA_CORE_HOOK_SINK_INPUT_PROPLIST_CHANGED], i);
758 pa_subscription_post(i->sink->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_CHANGE, i->index);
759 }
760 }
761
762 pa_resample_method_t pa_sink_input_get_resample_method(pa_sink_input *i) {
763 pa_sink_input_assert_ref(i);
764
765 return i->resample_method;
766 }
767
768 int pa_sink_input_move_to(pa_sink_input *i, pa_sink *dest, int immediately) {
769 pa_resampler *new_resampler;
770 pa_sink *origin;
771 pa_usec_t silence_usec = 0;
772 pa_sink_input_move_info info;
773 pa_sink_input_move_hook_data hook_data;
774
775 pa_sink_input_assert_ref(i);
776 pa_assert(PA_SINK_INPUT_LINKED(i->state));
777 pa_sink_assert_ref(dest);
778
779 origin = i->sink;
780
781 if (dest == origin)
782 return 0;
783
784 if (i->flags & PA_SINK_INPUT_DONT_MOVE)
785 return -1;
786
787 if (i->sync_next || i->sync_prev) {
788 pa_log_warn("Moving synchronised streams not supported.");
789 return -1;
790 }
791
792 if (pa_idxset_size(dest->inputs) >= PA_MAX_INPUTS_PER_SINK) {
793 pa_log_warn("Failed to move sink input: too many inputs per sink.");
794 return -1;
795 }
796
797 if (i->thread_info.resampler &&
798 pa_sample_spec_equal(&origin->sample_spec, &dest->sample_spec) &&
799 pa_channel_map_equal(&origin->channel_map, &dest->channel_map))
800
801 /* Try to reuse the old resampler if possible */
802 new_resampler = i->thread_info.resampler;
803
804 else if ((i->flags & PA_SINK_INPUT_VARIABLE_RATE) ||
805 !pa_sample_spec_equal(&i->sample_spec, &dest->sample_spec) ||
806 !pa_channel_map_equal(&i->channel_map, &dest->channel_map)) {
807
808 /* Okey, we need a new resampler for the new sink */
809
810 if (!(new_resampler = pa_resampler_new(
811 dest->core->mempool,
812 &i->sample_spec, &i->channel_map,
813 &dest->sample_spec, &dest->channel_map,
814 i->resample_method,
815 ((i->flags & PA_SINK_INPUT_VARIABLE_RATE) ? PA_RESAMPLER_VARIABLE_RATE : 0) |
816 ((i->flags & PA_SINK_INPUT_NO_REMAP) ? PA_RESAMPLER_NO_REMAP : 0) |
817 (i->core->disable_remixing || (i->flags & PA_SINK_INPUT_NO_REMIX) ? PA_RESAMPLER_NO_REMIX : 0)))) {
818 pa_log_warn("Unsupported resampling operation.");
819 return -1;
820 }
821 } else
822 new_resampler = NULL;
823
824 hook_data.sink_input = i;
825 hook_data.destination = dest;
826 pa_hook_fire(&i->sink->core->hooks[PA_CORE_HOOK_SINK_INPUT_MOVE], &hook_data);
827
828 memset(&info, 0, sizeof(info));
829 info.sink_input = i;
830
831 if (!immediately) {
832 pa_usec_t old_latency, new_latency;
833
834 /* Let's do a little bit of Voodoo for compensating latency
835 * differences. We assume that the accuracy for our
836 * estimations is still good enough, even though we do these
837 * operations non-atomic. */
838
839 old_latency = pa_sink_get_latency(origin);
840 new_latency = pa_sink_get_latency(dest);
841
842 /* The already resampled data should go to the old sink */
843
844 if (old_latency >= new_latency) {
845
846 /* The latency of the old sink is larger than the latency
847 * of the new sink. Therefore to compensate for the
848 * difference we to play silence on the new one for a
849 * while */
850
851 silence_usec = old_latency - new_latency;
852
853 } else {
854
855 /* The latency of new sink is larger than the latency of
856 * the old sink. Therefore we have to precompute a little
857 * and make sure that this is still played on the old
858 * sink, until we can play the first sample on the new
859 * sink.*/
860
861 info.buffer_bytes = pa_usec_to_bytes(new_latency - old_latency, &origin->sample_spec);
862 }
863
864 /* Okey, let's move it */
865
866 if (info.buffer_bytes > 0) {
867 pa_proplist *p;
868
869 p = pa_proplist_new();
870 pa_proplist_sets(p, PA_PROP_MEDIA_NAME, "Ghost For Moved Stream");
871 pa_proplist_sets(p, PA_PROP_MEDIA_ROLE, "routing");
872
873 info.ghost_sink_input = pa_memblockq_sink_input_new(
874 origin,
875 &origin->sample_spec,
876 &origin->channel_map,
877 NULL,
878 NULL,
879 p);
880
881 pa_proplist_free(p);
882
883 if (info.ghost_sink_input) {
884 info.ghost_sink_input->thread_info.state = info.ghost_sink_input->state = PA_SINK_INPUT_RUNNING;
885 info.ghost_sink_input->thread_info.volume = info.ghost_sink_input->volume;
886 info.ghost_sink_input->thread_info.muted = info.ghost_sink_input->muted;
887
888 info.buffer = pa_memblockq_new(0, MOVE_BUFFER_LENGTH, 0, pa_frame_size(&origin->sample_spec), 0, 0, 0, NULL);
889 }
890 }
891 }
892
893 pa_asyncmsgq_send(i->sink->asyncmsgq, PA_MSGOBJECT(i->sink), PA_SINK_MESSAGE_REMOVE_INPUT_AND_BUFFER, &info, 0, NULL);
894
895 if (info.ghost_sink_input) {
896 /* Basically, do what pa_sink_input_put() does ...*/
897
898 pa_subscription_post(i->sink->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_NEW, info.ghost_sink_input->index);
899 pa_hook_fire(&i->sink->core->hooks[PA_CORE_HOOK_SINK_INPUT_PUT], info.ghost_sink_input);
900 pa_sink_input_unref(info.ghost_sink_input);
901 }
902
903 pa_idxset_remove_by_data(origin->inputs, i, NULL);
904 pa_idxset_put(dest->inputs, i, NULL);
905 i->sink = dest;
906
907 if (pa_sink_input_get_state(i) == PA_SINK_INPUT_CORKED) {
908 pa_assert_se(origin->n_corked-- >= 1);
909 dest->n_corked++;
910 }
911
912 /* Replace resampler */
913 if (new_resampler != i->thread_info.resampler) {
914 pa_memblock *silence;
915
916 if (i->thread_info.resampler)
917 pa_resampler_free(i->thread_info.resampler);
918 i->thread_info.resampler = new_resampler;
919
920 /* if the resampler changed, the silence memblock is
921 * probably invalid now, too */
922
923 silence = pa_silence_memblock_new(i->sink->core->mempool, &dest->sample_spec, new_resampler ? pa_resampler_max_block_size(new_resampler) : 0);
924 pa_memblockq_set_silence(i->thread_info.render_memblockq, silence);
925 pa_memblock_unref(silence);
926
927 }
928
929 pa_memblockq_flush(i->thread_info.render_memblockq);
930
931 /* Calculate the new sleeping time */
932 if (!immediately)
933 pa_memblockq_seek(i->thread_info.render_memblockq, pa_usec_to_bytes(silence_usec, &dest->sample_spec), PA_SEEK_RELATIVE);
934
935 pa_asyncmsgq_send(i->sink->asyncmsgq, PA_MSGOBJECT(i->sink), PA_SINK_MESSAGE_ADD_INPUT, i, 0, NULL);
936
937 pa_sink_update_status(origin);
938 pa_sink_update_status(dest);
939
940 if (i->moved)
941 i->moved(i);
942
943 pa_hook_fire(&i->sink->core->hooks[PA_CORE_HOOK_SINK_INPUT_MOVE_POST], i);
944
945 pa_log_debug("Successfully moved sink input %i from %s to %s.", i->index, origin->name, dest->name);
946
947 /* Notify everyone */
948 pa_subscription_post(i->sink->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_CHANGE, i->index);
949
950 return 0;
951 }
952
953 /* Called from thread context */
954 int pa_sink_input_process_msg(pa_msgobject *o, int code, void *userdata, int64_t offset, pa_memchunk *chunk) {
955 pa_sink_input *i = PA_SINK_INPUT(o);
956
957 pa_sink_input_assert_ref(i);
958 pa_assert(PA_SINK_INPUT_LINKED(i->thread_info.state));
959
960 switch (code) {
961 case PA_SINK_INPUT_MESSAGE_SET_VOLUME:
962 i->thread_info.volume = *((pa_cvolume*) userdata);
963 pa_sink_input_request_rewrite(i, 0);
964 return 0;
965
966 case PA_SINK_INPUT_MESSAGE_SET_MUTE:
967 i->thread_info.muted = PA_PTR_TO_UINT(userdata);
968 pa_sink_input_request_rewrite(i, 0);
969 return 0;
970
971 case PA_SINK_INPUT_MESSAGE_GET_LATENCY: {
972 pa_usec_t *r = userdata;
973
974 *r += pa_bytes_to_usec(pa_memblockq_get_length(i->thread_info.render_memblockq), &i->sink->sample_spec);
975
976 return 0;
977 }
978
979 case PA_SINK_INPUT_MESSAGE_SET_RATE:
980
981 i->thread_info.sample_spec.rate = PA_PTR_TO_UINT(userdata);
982 pa_resampler_set_input_rate(i->thread_info.resampler, PA_PTR_TO_UINT(userdata));
983
984 return 0;
985
986 case PA_SINK_INPUT_MESSAGE_SET_STATE: {
987 pa_sink_input *ssync;
988
989 if ((PA_PTR_TO_UINT(userdata) == PA_SINK_INPUT_DRAINED || PA_PTR_TO_UINT(userdata) == PA_SINK_INPUT_RUNNING) &&
990 (i->thread_info.state != PA_SINK_INPUT_DRAINED) && (i->thread_info.state != PA_SINK_INPUT_RUNNING))
991 pa_atomic_store(&i->thread_info.drained, 1);
992
993 i->thread_info.state = PA_PTR_TO_UINT(userdata);
994
995 for (ssync = i->thread_info.sync_prev; ssync; ssync = ssync->thread_info.sync_prev) {
996 if ((PA_PTR_TO_UINT(userdata) == PA_SINK_INPUT_DRAINED || PA_PTR_TO_UINT(userdata) == PA_SINK_INPUT_RUNNING) &&
997 (ssync->thread_info.state != PA_SINK_INPUT_DRAINED) && (ssync->thread_info.state != PA_SINK_INPUT_RUNNING))
998 pa_atomic_store(&ssync->thread_info.drained, 1);
999 ssync->thread_info.state = PA_PTR_TO_UINT(userdata);
1000 }
1001
1002 for (ssync = i->thread_info.sync_next; ssync; ssync = ssync->thread_info.sync_next) {
1003 if ((PA_PTR_TO_UINT(userdata) == PA_SINK_INPUT_DRAINED || PA_PTR_TO_UINT(userdata) == PA_SINK_INPUT_RUNNING) &&
1004 (ssync->thread_info.state != PA_SINK_INPUT_DRAINED) && (ssync->thread_info.state != PA_SINK_INPUT_RUNNING))
1005 pa_atomic_store(&ssync->thread_info.drained, 1);
1006 ssync->thread_info.state = PA_PTR_TO_UINT(userdata);
1007 }
1008
1009 return 0;
1010 }
1011
1012 case PA_SINK_INPUT_MESSAGE_SET_REQUESTED_LATENCY:
1013
1014 i->thread_info.requested_sink_latency = (pa_usec_t) offset;
1015 pa_sink_invalidate_requested_latency(i->sink);
1016
1017 return 0;
1018 }
1019
1020 return -1;
1021 }
1022
1023 pa_sink_input_state_t pa_sink_input_get_state(pa_sink_input *i) {
1024 pa_sink_input_assert_ref(i);
1025
1026 if (i->state == PA_SINK_INPUT_RUNNING || i->state == PA_SINK_INPUT_DRAINED)
1027 return pa_atomic_load(&i->thread_info.drained) ? PA_SINK_INPUT_DRAINED : PA_SINK_INPUT_RUNNING;
1028
1029 return i->state;
1030 }
1031
1032 pa_bool_t pa_sink_input_safe_to_remove(pa_sink_input *i) {
1033 pa_sink_input_assert_ref(i);
1034
1035 if (i->state == PA_SINK_INPUT_RUNNING || i->state == PA_SINK_INPUT_DRAINED || i->state == PA_SINK_INPUT_CORKED)
1036 return pa_atomic_load(&i->thread_info.render_memblockq_is_empty);
1037
1038 return TRUE;
1039 }
1040
1041 void pa_sink_input_request_rewrite(pa_sink_input *i, size_t nbytes /* in our sample spec */) {
1042 size_t l, lbq;
1043
1044 pa_sink_input_assert_ref(i);
1045
1046 lbq = pa_memblockq_get_length(i->thread_info.render_memblockq);
1047
1048 if (nbytes <= 0) {
1049 nbytes =
1050 i->thread_info.resampler ?
1051 pa_resampler_request(i->thread_info.resampler, i->sink->thread_info.max_rewind + lbq) :
1052 (i->sink->thread_info.max_rewind + lbq);
1053 }
1054
1055 i->thread_info.rewrite_nbytes = PA_MAX(nbytes, i->thread_info.rewrite_nbytes);
1056
1057 /* Transform to sink domain */
1058 l = i->thread_info.resampler ? pa_resampler_result(i->thread_info.resampler, nbytes) : nbytes;
1059
1060 if (l <= 0)
1061 return;
1062
1063 if (l > lbq)
1064 pa_sink_request_rewind(i->sink, l - lbq);
1065 }