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