~diwic/ubuntu/lucid/pulseaudio/bugfixes

« back to all changes in this revision

Viewing changes to src/pulsecore/sink-input.c

  • Committer: Bazaar Package Importer
  • Author(s): Luke Yelavich
  • Date: 2008-11-04 15:46:00 UTC
  • mfrom: (1.2.1 upstream) (1.1.6 lenny)
  • Revision ID: james.westby@ubuntu.com-20081104154600-hlzknpcazaam0nxm
Tags: 0.9.13-1ubuntu1
* Merge from Debian unstable, remaining changes:
  - Don't build against, and create jack package. Jack is not in main.
  - Remove --disable-per-user-esound-socket from configure flags, as we still
    want per user esound sockets.
  - Remove stop links from rc0 and rc6.
  - Change default resample algorithm and bubffer size.
  - Add alsa configuration files to route alsa applications via pulseaudio.
  - Move libasound2-plugins from Recommends to Depends.
* debian/pulseaudio.preinst: When upgrading from intrepid, remove
  /etc/X11/Xsession.d/70pulseaudio, as this was used to minimize a race
  condition when starting GNOME in intrepid. This race should not exist in
  jaunty once libcanberra is built to use pulseaudio as a backend.
* Do not spawn a pulseaudio server if clients fail to find a running server.
* Remove explicit version dependency for libspeex-dev to allow the package
  to be built for now.
* Regenerate autotools files to work with Ubuntu's newer libtool/libltdl.
* debian/control: libpulsecore5 -> libpulsecore8 to match the library
  soname.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* $Id: sink-input.c 2159 2008-03-27 23:29:32Z lennart $ */
2
 
 
3
1
/***
4
2
  This file is part of PulseAudio.
5
3
 
38
36
#include <pulsecore/log.h>
39
37
#include <pulsecore/play-memblockq.h>
40
38
#include <pulsecore/namereg.h>
 
39
#include <pulsecore/core-util.h>
41
40
 
42
41
#include "sink-input.h"
43
42
 
 
43
#define MEMBLOCKQ_MAXLENGTH (32*1024*1024)
44
44
#define CONVERT_BUFFER_LENGTH (PA_PAGE_SIZE)
45
 
#define SILENCE_BUFFER_LENGTH (PA_PAGE_SIZE*12)
46
 
#define MOVE_BUFFER_LENGTH (PA_PAGE_SIZE*256)
47
45
 
48
46
static PA_DEFINE_CHECK_TYPE(pa_sink_input, pa_msgobject);
49
47
 
54
52
 
55
53
    memset(data, 0, sizeof(*data));
56
54
    data->resample_method = PA_RESAMPLER_INVALID;
 
55
    data->proplist = pa_proplist_new();
57
56
 
58
57
    return data;
59
58
}
60
59
 
 
60
void pa_sink_input_new_data_set_sample_spec(pa_sink_input_new_data *data, const pa_sample_spec *spec) {
 
61
    pa_assert(data);
 
62
 
 
63
    if ((data->sample_spec_is_set = !!spec))
 
64
        data->sample_spec = *spec;
 
65
}
 
66
 
61
67
void pa_sink_input_new_data_set_channel_map(pa_sink_input_new_data *data, const pa_channel_map *map) {
62
68
    pa_assert(data);
63
69
 
72
78
        data->volume = *volume;
73
79
}
74
80
 
75
 
void pa_sink_input_new_data_set_sample_spec(pa_sink_input_new_data *data, const pa_sample_spec *spec) {
76
 
    pa_assert(data);
77
 
 
78
 
    if ((data->sample_spec_is_set = !!spec))
79
 
        data->sample_spec = *spec;
80
 
}
81
 
 
82
81
void pa_sink_input_new_data_set_muted(pa_sink_input_new_data *data, pa_bool_t mute) {
83
82
    pa_assert(data);
84
83
 
86
85
    data->muted = !!mute;
87
86
}
88
87
 
 
88
void pa_sink_input_new_data_done(pa_sink_input_new_data *data) {
 
89
    pa_assert(data);
 
90
 
 
91
    pa_proplist_free(data->proplist);
 
92
}
 
93
 
 
94
/* Called from main context */
 
95
static void reset_callbacks(pa_sink_input *i) {
 
96
    pa_assert(i);
 
97
 
 
98
    i->pop = NULL;
 
99
    i->process_rewind = NULL;
 
100
    i->update_max_rewind = NULL;
 
101
    i->update_max_request = NULL;
 
102
    i->update_sink_requested_latency = NULL;
 
103
    i->update_sink_latency_range = NULL;
 
104
    i->attach = NULL;
 
105
    i->detach = NULL;
 
106
    i->suspend = NULL;
 
107
    i->moved = NULL;
 
108
    i->kill = NULL;
 
109
    i->get_latency = NULL;
 
110
    i->state_change = NULL;
 
111
}
 
112
 
 
113
/* Called from main context */
89
114
pa_sink_input* pa_sink_input_new(
90
115
        pa_core *core,
91
116
        pa_sink_input_new_data *data,
102
127
        return NULL;
103
128
 
104
129
    pa_return_null_if_fail(!data->driver || pa_utf8_valid(data->driver));
105
 
    pa_return_null_if_fail(!data->name || pa_utf8_valid(data->name));
106
130
 
107
131
    if (!data->sink)
108
 
        data->sink = pa_namereg_get(core, NULL, PA_NAMEREG_SINK, 1);
 
132
        data->sink = pa_namereg_get(core, NULL, PA_NAMEREG_SINK, TRUE);
109
133
 
110
134
    pa_return_null_if_fail(data->sink);
111
135
    pa_return_null_if_fail(pa_sink_get_state(data->sink) != PA_SINK_UNLINKED);
132
156
    pa_return_null_if_fail(pa_cvolume_valid(&data->volume));
133
157
    pa_return_null_if_fail(data->volume.channels == data->sample_spec.channels);
134
158
 
 
159
    if (!data->muted_is_set)
 
160
        data->muted = FALSE;
 
161
 
135
162
    if (flags & PA_SINK_INPUT_FIX_FORMAT)
136
163
        data->sample_spec.format = data->sink->sample_spec.format;
137
164
 
150
177
    if (data->volume.channels != data->sample_spec.channels)
151
178
        pa_cvolume_set(&data->volume, data->sample_spec.channels, pa_cvolume_avg(&data->volume));
152
179
 
153
 
    if (!data->muted_is_set)
154
 
        data->muted = 0;
155
 
 
156
180
    if (data->resample_method == PA_RESAMPLER_INVALID)
157
181
        data->resample_method = core->resample_method;
158
182
 
177
201
                      data->resample_method,
178
202
                      ((flags & PA_SINK_INPUT_VARIABLE_RATE) ? PA_RESAMPLER_VARIABLE_RATE : 0) |
179
203
                      ((flags & PA_SINK_INPUT_NO_REMAP) ? PA_RESAMPLER_NO_REMAP : 0) |
180
 
                      (core->disable_remixing || (flags & PA_SINK_INPUT_NO_REMIX) ? PA_RESAMPLER_NO_REMIX : 0)))) {
 
204
                      (core->disable_remixing || (flags & PA_SINK_INPUT_NO_REMIX) ? PA_RESAMPLER_NO_REMIX : 0) |
 
205
                      (core->disable_lfe_remixing ? PA_RESAMPLER_NO_LFE : 0)))) {
181
206
            pa_log_warn("Unsupported resampling operation.");
182
207
            return NULL;
183
208
        }
192
217
    i->core = core;
193
218
    i->state = PA_SINK_INPUT_INIT;
194
219
    i->flags = flags;
195
 
    i->name = pa_xstrdup(data->name);
 
220
    i->proplist = pa_proplist_copy(data->proplist);
196
221
    i->driver = pa_xstrdup(data->driver);
197
222
    i->module = data->module;
198
223
    i->sink = data->sink;
215
240
    } else
216
241
        i->sync_next = i->sync_prev = NULL;
217
242
 
218
 
    i->peek = NULL;
219
 
    i->drop = NULL;
220
 
    i->kill = NULL;
221
 
    i->get_latency = NULL;
222
 
    i->attach = NULL;
223
 
    i->detach = NULL;
224
 
    i->suspend = NULL;
225
 
    i->moved = NULL;
 
243
    i->direct_outputs = pa_idxset_new(NULL, NULL);
 
244
 
 
245
    reset_callbacks(i);
226
246
    i->userdata = NULL;
227
247
 
228
248
    i->thread_info.state = i->state;
 
249
    i->thread_info.attached = FALSE;
229
250
    pa_atomic_store(&i->thread_info.drained, 1);
230
251
    i->thread_info.sample_spec = i->sample_spec;
231
 
    i->thread_info.silence_memblock = NULL;
232
 
    i->thread_info.move_silence = 0;
233
 
    pa_memchunk_reset(&i->thread_info.resampled_chunk);
234
252
    i->thread_info.resampler = resampler;
235
253
    i->thread_info.volume = i->volume;
236
254
    i->thread_info.muted = i->muted;
237
 
    i->thread_info.attached = FALSE;
 
255
    i->thread_info.requested_sink_latency = (pa_usec_t) -1;
 
256
    i->thread_info.rewrite_nbytes = 0;
 
257
    i->thread_info.rewrite_flush = FALSE;
 
258
    i->thread_info.underrun_for = (uint64_t) -1;
 
259
    i->thread_info.playing_for = 0;
 
260
    i->thread_info.direct_outputs = pa_hashmap_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
 
261
 
 
262
    i->thread_info.render_memblockq = pa_memblockq_new(
 
263
            0,
 
264
            MEMBLOCKQ_MAXLENGTH,
 
265
            0,
 
266
            pa_frame_size(&i->sink->sample_spec),
 
267
            0,
 
268
            1,
 
269
            0,
 
270
            &i->sink->silence);
238
271
 
239
272
    pa_assert_se(pa_idxset_put(core->sink_inputs, pa_sink_input_ref(i), &i->index) == 0);
240
273
    pa_assert_se(pa_idxset_put(i->sink->inputs, i, NULL) == 0);
241
274
 
242
275
    pa_log_info("Created input %u \"%s\" on %s with sample spec %s and channel map %s",
243
276
                i->index,
244
 
                i->name,
 
277
                pa_strnull(pa_proplist_gets(i->proplist, PA_PROP_MEDIA_NAME)),
245
278
                i->sink->name,
246
279
                pa_sample_spec_snprint(st, sizeof(st), &i->sample_spec),
247
280
                pa_channel_map_snprint(cm, sizeof(cm), &i->channel_map));
251
284
    return i;
252
285
}
253
286
 
 
287
/* Called from main context */
254
288
static void update_n_corked(pa_sink_input *i, pa_sink_input_state_t state) {
255
289
    pa_assert(i);
256
290
 
262
296
    pa_sink_update_status(i->sink);
263
297
}
264
298
 
 
299
/* Called from main context */
265
300
static int sink_input_set_state(pa_sink_input *i, pa_sink_input_state_t state) {
266
301
    pa_sink_input *ssync;
267
302
    pa_assert(i);
272
307
    if (i->state == state)
273
308
        return 0;
274
309
 
275
 
    if (pa_asyncmsgq_send(i->sink->asyncmsgq, PA_MSGOBJECT(i), PA_SINK_INPUT_MESSAGE_SET_STATE, PA_UINT_TO_PTR(state), 0, NULL) < 0)
276
 
        return -1;
 
310
    pa_assert_se(pa_asyncmsgq_send(i->sink->asyncmsgq, PA_MSGOBJECT(i), PA_SINK_INPUT_MESSAGE_SET_STATE, PA_UINT_TO_PTR(state), 0, NULL) == 0);
277
311
 
278
312
    update_n_corked(i, state);
279
313
    i->state = state;
287
321
        ssync->state = state;
288
322
    }
289
323
 
290
 
    if (state != PA_SINK_INPUT_UNLINKED)
 
324
    if (state != PA_SINK_INPUT_UNLINKED) {
291
325
        pa_hook_fire(&i->sink->core->hooks[PA_CORE_HOOK_SINK_INPUT_STATE_CHANGED], i);
292
326
 
 
327
        for (ssync = i->sync_prev; ssync; ssync = ssync->sync_prev)
 
328
            pa_hook_fire(&i->sink->core->hooks[PA_CORE_HOOK_SINK_INPUT_STATE_CHANGED], ssync);
 
329
 
 
330
        for (ssync = i->sync_next; ssync; ssync = ssync->sync_next)
 
331
            pa_hook_fire(&i->sink->core->hooks[PA_CORE_HOOK_SINK_INPUT_STATE_CHANGED], ssync);
 
332
    }
 
333
 
293
334
    return 0;
294
335
}
295
336
 
 
337
/* Called from main context */
296
338
void pa_sink_input_unlink(pa_sink_input *i) {
297
339
    pa_bool_t linked;
 
340
    pa_source_output *o, *p =  NULL;
298
341
    pa_assert(i);
299
342
 
300
343
    /* See pa_sink_unlink() for a couple of comments how this function
302
345
 
303
346
    pa_sink_input_ref(i);
304
347
 
305
 
    linked = PA_SINK_INPUT_LINKED(i->state);
 
348
    linked = PA_SINK_INPUT_IS_LINKED(i->state);
306
349
 
307
350
    if (linked)
308
351
        pa_hook_fire(&i->sink->core->hooks[PA_CORE_HOOK_SINK_INPUT_UNLINK], i);
318
361
    if (pa_idxset_remove_by_data(i->sink->inputs, i, NULL))
319
362
        pa_sink_input_unref(i);
320
363
 
321
 
    if (linked) {
322
 
        pa_asyncmsgq_send(i->sink->asyncmsgq, PA_MSGOBJECT(i->sink), PA_SINK_MESSAGE_REMOVE_INPUT, i, 0, NULL);
323
 
        sink_input_set_state(i, PA_SINK_INPUT_UNLINKED);
324
 
        pa_sink_update_status(i->sink);
325
 
    } else
326
 
        i->state = PA_SINK_INPUT_UNLINKED;
327
 
 
328
 
    i->peek = NULL;
329
 
    i->drop = NULL;
330
 
    i->kill = NULL;
331
 
    i->get_latency = NULL;
332
 
    i->attach = NULL;
333
 
    i->detach = NULL;
334
 
    i->suspend = NULL;
335
 
    i->moved = NULL;
 
364
    while ((o = pa_idxset_first(i->direct_outputs, NULL))) {
 
365
        pa_assert(o != p);
 
366
        pa_source_output_kill(o);
 
367
        p = o;
 
368
    }
 
369
 
 
370
    update_n_corked(i, PA_SINK_INPUT_UNLINKED);
 
371
    i->state = PA_SINK_INPUT_UNLINKED;
 
372
 
 
373
    if (linked)
 
374
        if (i->sink->asyncmsgq)
 
375
            pa_assert_se(pa_asyncmsgq_send(i->sink->asyncmsgq, PA_MSGOBJECT(i->sink), PA_SINK_MESSAGE_REMOVE_INPUT, i, 0, NULL) == 0);
 
376
 
 
377
    reset_callbacks(i);
336
378
 
337
379
    if (linked) {
338
380
        pa_subscription_post(i->sink->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_REMOVE, i->index);
343
385
    pa_sink_input_unref(i);
344
386
}
345
387
 
 
388
/* Called from main context */
346
389
static void sink_input_free(pa_object *o) {
347
390
    pa_sink_input* i = PA_SINK_INPUT(o);
348
391
 
349
392
    pa_assert(i);
350
393
    pa_assert(pa_sink_input_refcnt(i) == 0);
351
394
 
352
 
    if (PA_SINK_INPUT_LINKED(i->state))
 
395
    if (PA_SINK_INPUT_IS_LINKED(i->state))
353
396
        pa_sink_input_unlink(i);
354
397
 
355
 
    pa_log_info("Freeing output %u \"%s\"", i->index, i->name);
 
398
    pa_log_info("Freeing input %u \"%s\"", i->index, pa_strnull(pa_proplist_gets(i->proplist, PA_PROP_MEDIA_NAME)));
356
399
 
357
400
    pa_assert(!i->thread_info.attached);
358
401
 
359
 
    if (i->thread_info.resampled_chunk.memblock)
360
 
        pa_memblock_unref(i->thread_info.resampled_chunk.memblock);
 
402
    if (i->thread_info.render_memblockq)
 
403
        pa_memblockq_free(i->thread_info.render_memblockq);
361
404
 
362
405
    if (i->thread_info.resampler)
363
406
        pa_resampler_free(i->thread_info.resampler);
364
407
 
365
 
    if (i->thread_info.silence_memblock)
366
 
        pa_memblock_unref(i->thread_info.silence_memblock);
367
 
 
368
 
    pa_xfree(i->name);
 
408
    if (i->proplist)
 
409
        pa_proplist_free(i->proplist);
 
410
 
 
411
    if (i->direct_outputs)
 
412
        pa_idxset_free(i->direct_outputs, NULL, NULL);
 
413
 
 
414
    if (i->thread_info.direct_outputs)
 
415
        pa_hashmap_free(i->thread_info.direct_outputs, NULL, NULL);
 
416
 
369
417
    pa_xfree(i->driver);
370
418
    pa_xfree(i);
371
419
}
372
420
 
 
421
/* Called from main context */
373
422
void pa_sink_input_put(pa_sink_input *i) {
 
423
    pa_sink_input_state_t state;
374
424
    pa_sink_input_assert_ref(i);
375
425
 
376
426
    pa_assert(i->state == PA_SINK_INPUT_INIT);
377
 
    pa_assert(i->peek);
378
 
    pa_assert(i->drop);
379
 
 
380
 
    i->thread_info.state = i->state = i->flags & PA_SINK_INPUT_START_CORKED ? PA_SINK_INPUT_CORKED : PA_SINK_INPUT_RUNNING;
 
427
 
 
428
    /* The following fields must be initialized properly */
 
429
    pa_assert(i->pop);
 
430
    pa_assert(i->process_rewind);
 
431
    pa_assert(i->kill);
 
432
 
381
433
    i->thread_info.volume = i->volume;
382
434
    i->thread_info.muted = i->muted;
383
435
 
384
 
    if (i->state == PA_SINK_INPUT_CORKED)
385
 
        i->sink->n_corked++;
386
 
 
387
 
    pa_asyncmsgq_send(i->sink->asyncmsgq, PA_MSGOBJECT(i->sink), PA_SINK_MESSAGE_ADD_INPUT, i, 0, NULL);
388
 
    pa_sink_update_status(i->sink);
 
436
    state = i->flags & PA_SINK_INPUT_START_CORKED ? PA_SINK_INPUT_CORKED : PA_SINK_INPUT_RUNNING;
 
437
 
 
438
    update_n_corked(i, state);
 
439
    i->state = state;
 
440
 
 
441
    pa_assert_se(pa_asyncmsgq_send(i->sink->asyncmsgq, PA_MSGOBJECT(i->sink), PA_SINK_MESSAGE_ADD_INPUT, i, 0, NULL) == 0);
389
442
 
390
443
    pa_subscription_post(i->sink->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_NEW, i->index);
391
444
    pa_hook_fire(&i->sink->core->hooks[PA_CORE_HOOK_SINK_INPUT_PUT], i);
392
 
 
393
 
    /* Please note that if you change something here, you have to
394
 
       change something in pa_sink_input_move() with the ghost stream
395
 
       registration too. */
396
445
}
397
446
 
 
447
/* Called from main context */
398
448
void pa_sink_input_kill(pa_sink_input*i) {
399
449
    pa_sink_input_assert_ref(i);
400
 
    pa_assert(PA_SINK_INPUT_LINKED(i->state));
 
450
    pa_assert(PA_SINK_INPUT_IS_LINKED(i->state));
401
451
 
402
 
    if (i->kill)
403
 
        i->kill(i);
 
452
    i->kill(i);
404
453
}
405
454
 
406
 
pa_usec_t pa_sink_input_get_latency(pa_sink_input *i) {
407
 
    pa_usec_t r = 0;
 
455
/* Called from main context */
 
456
pa_usec_t pa_sink_input_get_latency(pa_sink_input *i, pa_usec_t *sink_latency) {
 
457
    pa_usec_t r[2] = { 0, 0 };
408
458
 
409
459
    pa_sink_input_assert_ref(i);
410
 
    pa_assert(PA_SINK_INPUT_LINKED(i->state));
 
460
    pa_assert(PA_SINK_INPUT_IS_LINKED(i->state));
411
461
 
412
 
    if (pa_asyncmsgq_send(i->sink->asyncmsgq, PA_MSGOBJECT(i), PA_SINK_INPUT_MESSAGE_GET_LATENCY, &r, 0, NULL) < 0)
413
 
        r = 0;
 
462
    pa_assert_se(pa_asyncmsgq_send(i->sink->asyncmsgq, PA_MSGOBJECT(i), PA_SINK_INPUT_MESSAGE_GET_LATENCY, r, 0, NULL) == 0);
414
463
 
415
464
    if (i->get_latency)
416
 
        r += i->get_latency(i);
417
 
 
418
 
    return r;
 
465
        r[0] += i->get_latency(i);
 
466
 
 
467
    if (sink_latency)
 
468
        *sink_latency = r[1];
 
469
 
 
470
    return r[0];
419
471
}
420
472
 
421
473
/* Called from thread context */
422
 
int pa_sink_input_peek(pa_sink_input *i, size_t length, pa_memchunk *chunk, pa_cvolume *volume) {
423
 
    int ret = -1;
424
 
    int do_volume_adj_here;
425
 
    int volume_is_norm;
426
 
    size_t block_size_max;
 
474
int pa_sink_input_peek(pa_sink_input *i, size_t slength /* in sink frames */, pa_memchunk *chunk, pa_cvolume *volume) {
 
475
    pa_bool_t do_volume_adj_here;
 
476
    pa_bool_t volume_is_norm;
 
477
    size_t block_size_max_sink, block_size_max_sink_input;
 
478
    size_t ilength;
427
479
 
428
480
    pa_sink_input_assert_ref(i);
429
 
    pa_assert(PA_SINK_INPUT_LINKED(i->thread_info.state));
430
 
    pa_assert(pa_frame_aligned(length, &i->sink->sample_spec));
 
481
    pa_assert(PA_SINK_INPUT_IS_LINKED(i->thread_info.state));
 
482
    pa_assert(pa_frame_aligned(slength, &i->sink->sample_spec));
431
483
    pa_assert(chunk);
432
484
    pa_assert(volume);
433
485
 
434
 
    if (!i->peek || !i->drop || i->thread_info.state == PA_SINK_INPUT_CORKED)
435
 
        goto finish;
436
 
 
437
 
    pa_assert(i->thread_info.state == PA_SINK_INPUT_RUNNING || i->thread_info.state == PA_SINK_INPUT_DRAINED);
 
486
/*     pa_log_debug("peek"); */
 
487
 
 
488
    if (!i->pop)
 
489
        return -1;
 
490
 
 
491
    pa_assert(i->thread_info.state == PA_SINK_INPUT_RUNNING ||
 
492
              i->thread_info.state == PA_SINK_INPUT_CORKED ||
 
493
              i->thread_info.state == PA_SINK_INPUT_DRAINED);
 
494
 
 
495
    block_size_max_sink_input = i->thread_info.resampler ?
 
496
        pa_resampler_max_block_size(i->thread_info.resampler) :
 
497
        pa_frame_align(pa_mempool_block_size_max(i->sink->core->mempool), &i->sample_spec);
 
498
 
 
499
    block_size_max_sink = pa_frame_align(pa_mempool_block_size_max(i->sink->core->mempool), &i->sink->sample_spec);
438
500
 
439
501
    /* Default buffer size */
440
 
    if (length <= 0)
441
 
        length = pa_frame_align(CONVERT_BUFFER_LENGTH, &i->sink->sample_spec);
442
 
 
443
 
    /* Make sure the buffer fits in the mempool tile */
444
 
    block_size_max = pa_mempool_block_size_max(i->sink->core->mempool);
445
 
    if (length > block_size_max)
446
 
        length = pa_frame_align(block_size_max, &i->sink->sample_spec);
447
 
 
448
 
    if (i->thread_info.move_silence > 0) {
449
 
        size_t l;
450
 
 
451
 
        /* We have just been moved and shall play some silence for a
452
 
         * while until the old sink has drained its playback buffer */
453
 
 
454
 
        if (!i->thread_info.silence_memblock)
455
 
            i->thread_info.silence_memblock = pa_silence_memblock_new(
456
 
                    i->sink->core->mempool,
457
 
                    &i->sink->sample_spec,
458
 
                    pa_frame_align(SILENCE_BUFFER_LENGTH, &i->sink->sample_spec));
459
 
 
460
 
        chunk->memblock = pa_memblock_ref(i->thread_info.silence_memblock);
461
 
        chunk->index = 0;
462
 
        l = pa_memblock_get_length(chunk->memblock);
463
 
        chunk->length = i->thread_info.move_silence < l ? i->thread_info.move_silence : l;
464
 
 
465
 
        ret = 0;
466
 
        do_volume_adj_here = 1;
467
 
        goto finish;
468
 
    }
469
 
 
470
 
    if (!i->thread_info.resampler) {
471
 
        do_volume_adj_here = 0; /* FIXME??? */
472
 
        ret = i->peek(i, length, chunk);
473
 
        goto finish;
474
 
    }
 
502
    if (slength <= 0)
 
503
        slength = pa_frame_align(CONVERT_BUFFER_LENGTH, &i->sink->sample_spec);
 
504
 
 
505
    if (slength > block_size_max_sink)
 
506
        slength = block_size_max_sink;
 
507
 
 
508
    if (i->thread_info.resampler) {
 
509
        ilength = pa_resampler_request(i->thread_info.resampler, slength);
 
510
 
 
511
        if (ilength <= 0)
 
512
            ilength = pa_frame_align(CONVERT_BUFFER_LENGTH, &i->sample_spec);
 
513
    } else
 
514
        ilength = slength;
 
515
 
 
516
    if (ilength > block_size_max_sink_input)
 
517
        ilength = block_size_max_sink_input;
 
518
 
 
519
    /* If the channel maps of the sink and this stream differ, we need
 
520
     * to adjust the volume *before* we resample. Otherwise we can do
 
521
     * it after and leave it for the sink code */
475
522
 
476
523
    do_volume_adj_here = !pa_channel_map_equal(&i->channel_map, &i->sink->channel_map);
477
524
    volume_is_norm = pa_cvolume_is_norm(&i->thread_info.volume) && !i->thread_info.muted;
478
525
 
479
 
    while (!i->thread_info.resampled_chunk.memblock) {
 
526
    while (!pa_memblockq_is_readable(i->thread_info.render_memblockq)) {
480
527
        pa_memchunk tchunk;
481
 
        size_t l, rmbs;
482
 
 
483
 
        l = pa_resampler_request(i->thread_info.resampler, length);
484
 
 
485
 
        if (l <= 0)
486
 
            l = pa_frame_align(CONVERT_BUFFER_LENGTH, &i->sample_spec);
487
 
 
488
 
        rmbs = pa_resampler_max_block_size(i->thread_info.resampler);
489
 
        if (l > rmbs)
490
 
            l = rmbs;
491
 
 
492
 
        if ((ret = i->peek(i, l, &tchunk)) < 0)
493
 
            goto finish;
 
528
 
 
529
        /* There's nothing in our render queue. We need to fill it up
 
530
         * with data from the implementor. */
 
531
 
 
532
        if (i->thread_info.state == PA_SINK_INPUT_CORKED ||
 
533
            i->pop(i, ilength, &tchunk) < 0) {
 
534
 
 
535
            /* OK, we're corked or the implementor didn't give us any
 
536
             * data, so let's just hand out silence */
 
537
            pa_atomic_store(&i->thread_info.drained, 1);
 
538
 
 
539
            pa_memblockq_seek(i->thread_info.render_memblockq, (int64_t) slength, PA_SEEK_RELATIVE);
 
540
            i->thread_info.playing_for = 0;
 
541
            if (i->thread_info.underrun_for != (uint64_t) -1)
 
542
                i->thread_info.underrun_for += ilength;
 
543
            break;
 
544
        }
 
545
 
 
546
        pa_atomic_store(&i->thread_info.drained, 0);
494
547
 
495
548
        pa_assert(tchunk.length > 0);
496
 
 
497
 
        if (tchunk.length > l)
498
 
            tchunk.length = l;
499
 
 
500
 
        i->drop(i, tchunk.length);
501
 
 
502
 
        /* It might be necessary to adjust the volume here */
503
 
        if (do_volume_adj_here && !volume_is_norm) {
504
 
            pa_memchunk_make_writable(&tchunk, 0);
505
 
 
506
 
            if (i->thread_info.muted)
507
 
                pa_silence_memchunk(&tchunk, &i->thread_info.sample_spec);
508
 
            else
509
 
                pa_volume_memchunk(&tchunk, &i->thread_info.sample_spec, &i->thread_info.volume);
 
549
        pa_assert(tchunk.memblock);
 
550
 
 
551
        i->thread_info.underrun_for = 0;
 
552
        i->thread_info.playing_for += tchunk.length;
 
553
 
 
554
        while (tchunk.length > 0) {
 
555
            pa_memchunk wchunk;
 
556
 
 
557
            wchunk = tchunk;
 
558
            pa_memblock_ref(wchunk.memblock);
 
559
 
 
560
            if (wchunk.length > block_size_max_sink_input)
 
561
                wchunk.length = block_size_max_sink_input;
 
562
 
 
563
            /* It might be necessary to adjust the volume here */
 
564
            if (do_volume_adj_here && !volume_is_norm) {
 
565
                pa_memchunk_make_writable(&wchunk, 0);
 
566
 
 
567
                if (i->thread_info.muted)
 
568
                    pa_silence_memchunk(&wchunk, &i->thread_info.sample_spec);
 
569
                else
 
570
                    pa_volume_memchunk(&wchunk, &i->thread_info.sample_spec, &i->thread_info.volume);
 
571
            }
 
572
 
 
573
            if (!i->thread_info.resampler)
 
574
                pa_memblockq_push_align(i->thread_info.render_memblockq, &wchunk);
 
575
            else {
 
576
                pa_memchunk rchunk;
 
577
                pa_resampler_run(i->thread_info.resampler, &wchunk, &rchunk);
 
578
 
 
579
/*                 pa_log_debug("pushing %lu", (unsigned long) rchunk.length); */
 
580
 
 
581
                if (rchunk.memblock) {
 
582
                    pa_memblockq_push_align(i->thread_info.render_memblockq, &rchunk);
 
583
                    pa_memblock_unref(rchunk.memblock);
 
584
                }
 
585
            }
 
586
 
 
587
            pa_memblock_unref(wchunk.memblock);
 
588
 
 
589
            tchunk.index += wchunk.length;
 
590
            tchunk.length -= wchunk.length;
510
591
        }
511
592
 
512
 
        pa_resampler_run(i->thread_info.resampler, &tchunk, &i->thread_info.resampled_chunk);
513
593
        pa_memblock_unref(tchunk.memblock);
514
594
    }
515
595
 
516
 
    pa_assert(i->thread_info.resampled_chunk.memblock);
517
 
    pa_assert(i->thread_info.resampled_chunk.length > 0);
518
 
 
519
 
    *chunk = i->thread_info.resampled_chunk;
520
 
    pa_memblock_ref(i->thread_info.resampled_chunk.memblock);
521
 
 
522
 
    ret = 0;
523
 
 
524
 
finish:
525
 
 
526
 
    if (ret >= 0)
527
 
        pa_atomic_store(&i->thread_info.drained, 0);
528
 
    else if (ret < 0)
529
 
        pa_atomic_store(&i->thread_info.drained, 1);
530
 
 
531
 
    if (ret >= 0) {
532
 
        /* Let's see if we had to apply the volume adjustment
533
 
         * ourselves, or if this can be done by the sink for us */
534
 
 
535
 
        if (do_volume_adj_here)
536
 
            /* We had different channel maps, so we already did the adjustment */
537
 
            pa_cvolume_reset(volume, i->sink->sample_spec.channels);
538
 
        else if (i->thread_info.muted)
539
 
            /* We've both the same channel map, so let's have the sink do the adjustment for us*/
540
 
            pa_cvolume_mute(volume, i->sink->sample_spec.channels);
541
 
        else
542
 
            *volume = i->thread_info.volume;
543
 
    }
544
 
 
545
 
    return ret;
546
 
}
547
 
 
548
 
/* Called from thread context */
549
 
void pa_sink_input_drop(pa_sink_input *i, size_t length) {
550
 
    pa_sink_input_assert_ref(i);
551
 
    pa_assert(PA_SINK_INPUT_LINKED(i->thread_info.state));
552
 
    pa_assert(pa_frame_aligned(length, &i->sink->sample_spec));
553
 
    pa_assert(length > 0);
554
 
 
555
 
    if (!i->peek || !i->drop || i->thread_info.state == PA_SINK_INPUT_CORKED)
556
 
        return;
557
 
 
558
 
    if (i->thread_info.move_silence > 0) {
559
 
 
560
 
        if (i->thread_info.move_silence >= length) {
561
 
            i->thread_info.move_silence -= length;
562
 
            length = 0;
563
 
        } else {
564
 
            length -= i->thread_info.move_silence;
565
 
            i->thread_info.move_silence = 0;
566
 
        }
567
 
 
568
 
        if (i->thread_info.move_silence <= 0) {
569
 
            if (i->thread_info.silence_memblock) {
570
 
                pa_memblock_unref(i->thread_info.silence_memblock);
571
 
                i->thread_info.silence_memblock = NULL;
572
 
            }
573
 
        }
574
 
 
575
 
        if (length <= 0)
576
 
            return;
577
 
    }
578
 
 
579
 
    if (i->thread_info.resampled_chunk.memblock) {
580
 
        size_t l = length;
581
 
 
582
 
        if (l > i->thread_info.resampled_chunk.length)
583
 
            l = i->thread_info.resampled_chunk.length;
584
 
 
585
 
        i->thread_info.resampled_chunk.index += l;
586
 
        i->thread_info.resampled_chunk.length -= l;
587
 
 
588
 
        if (i->thread_info.resampled_chunk.length <= 0) {
589
 
            pa_memblock_unref(i->thread_info.resampled_chunk.memblock);
590
 
            pa_memchunk_reset(&i->thread_info.resampled_chunk);
591
 
        }
592
 
 
593
 
        length -= l;
594
 
    }
595
 
 
596
 
    if (length > 0) {
597
 
 
598
 
        if (i->thread_info.resampler) {
599
 
            /* So, we have a resampler. To avoid discontinuities we
600
 
             * have to actually read all data that could be read and
601
 
             * pass it through the resampler. */
602
 
 
603
 
            while (length > 0) {
604
 
                pa_memchunk chunk;
605
 
                pa_cvolume volume;
606
 
 
607
 
                if (pa_sink_input_peek(i, length, &chunk, &volume) >= 0) {
608
 
                    size_t l;
609
 
 
610
 
                    pa_memblock_unref(chunk.memblock);
611
 
 
612
 
                    l = chunk.length;
613
 
                    if (l > length)
614
 
                        l = length;
615
 
 
616
 
                    pa_sink_input_drop(i, l);
617
 
                    length -= l;
618
 
 
619
 
                } else {
620
 
                    size_t l;
621
 
 
622
 
                    l = pa_resampler_request(i->thread_info.resampler, length);
623
 
 
624
 
                    /* Hmmm, peeking failed, so let's at least drop
625
 
                     * the right amount of data */
626
 
 
627
 
                    if (l > 0)
628
 
                        if (i->drop)
629
 
                            i->drop(i, l);
630
 
 
631
 
                    break;
632
 
                }
633
 
            }
634
 
 
635
 
        } else {
636
 
 
637
 
            /* We have no resampler, hence let's just drop the data */
638
 
 
639
 
            if (i->drop)
640
 
                i->drop(i, length);
641
 
        }
642
 
    }
643
 
}
644
 
 
 
596
    pa_assert_se(pa_memblockq_peek(i->thread_info.render_memblockq, chunk) >= 0);
 
597
 
 
598
    pa_assert(chunk->length > 0);
 
599
    pa_assert(chunk->memblock);
 
600
 
 
601
/*     pa_log_debug("peeking %lu", (unsigned long) chunk->length); */
 
602
 
 
603
    if (chunk->length > block_size_max_sink)
 
604
        chunk->length = block_size_max_sink;
 
605
 
 
606
    /* Let's see if we had to apply the volume adjustment ourselves,
 
607
     * or if this can be done by the sink for us */
 
608
 
 
609
    if (do_volume_adj_here)
 
610
        /* We had different channel maps, so we already did the adjustment */
 
611
        pa_cvolume_reset(volume, i->sink->sample_spec.channels);
 
612
    else if (i->thread_info.muted)
 
613
        /* We've both the same channel map, so let's have the sink do the adjustment for us*/
 
614
        pa_cvolume_mute(volume, i->sink->sample_spec.channels);
 
615
    else
 
616
        *volume = i->thread_info.volume;
 
617
 
 
618
    return 0;
 
619
}
 
620
 
 
621
/* Called from thread context */
 
622
void pa_sink_input_drop(pa_sink_input *i, size_t nbytes /* in sink sample spec */) {
 
623
    pa_sink_input_assert_ref(i);
 
624
 
 
625
    pa_assert(PA_SINK_INPUT_IS_LINKED(i->thread_info.state));
 
626
    pa_assert(pa_frame_aligned(nbytes, &i->sink->sample_spec));
 
627
    pa_assert(nbytes > 0);
 
628
 
 
629
/*     pa_log_debug("dropping %lu", (unsigned long) nbytes); */
 
630
 
 
631
    pa_memblockq_drop(i->thread_info.render_memblockq, nbytes);
 
632
}
 
633
 
 
634
/* Called from thread context */
 
635
void pa_sink_input_process_rewind(pa_sink_input *i, size_t nbytes /* in sink sample spec */) {
 
636
    size_t lbq;
 
637
    pa_bool_t called = FALSE;
 
638
    pa_sink_input_assert_ref(i);
 
639
 
 
640
    pa_assert(PA_SINK_INPUT_IS_LINKED(i->thread_info.state));
 
641
    pa_assert(pa_frame_aligned(nbytes, &i->sink->sample_spec));
 
642
 
 
643
/*     pa_log_debug("rewind(%lu, %lu)", (unsigned long) nbytes, (unsigned long) i->thread_info.rewrite_nbytes); */
 
644
 
 
645
    lbq = pa_memblockq_get_length(i->thread_info.render_memblockq);
 
646
 
 
647
    if (nbytes > 0) {
 
648
        pa_log_debug("Have to rewind %lu bytes on render memblockq.", (unsigned long) nbytes);
 
649
        pa_memblockq_rewind(i->thread_info.render_memblockq, nbytes);
 
650
    }
 
651
 
 
652
    if (i->thread_info.rewrite_nbytes == (size_t) -1) {
 
653
 
 
654
        /* We were asked to drop all buffered data, and rerequest new
 
655
         * data from implementor the next time push() is called */
 
656
 
 
657
        pa_memblockq_flush_write(i->thread_info.render_memblockq);
 
658
 
 
659
    } else if (i->thread_info.rewrite_nbytes > 0) {
 
660
        size_t max_rewrite, amount;
 
661
 
 
662
        /* Calculate how much make sense to rewrite at most */
 
663
        max_rewrite = nbytes + lbq;
 
664
 
 
665
        /* Transform into local domain */
 
666
        if (i->thread_info.resampler)
 
667
            max_rewrite = pa_resampler_request(i->thread_info.resampler, max_rewrite);
 
668
 
 
669
        /* Calculate how much of the rewinded data should actually be rewritten */
 
670
        amount = PA_MIN(i->thread_info.rewrite_nbytes, max_rewrite);
 
671
 
 
672
        if (amount > 0) {
 
673
            pa_log_debug("Have to rewind %lu bytes on implementor.", (unsigned long) amount);
 
674
 
 
675
            /* Tell the implementor */
 
676
            if (i->process_rewind)
 
677
                i->process_rewind(i, amount);
 
678
            called = TRUE;
 
679
 
 
680
            /* Convert back to to sink domain */
 
681
            if (i->thread_info.resampler)
 
682
                amount = pa_resampler_result(i->thread_info.resampler, amount);
 
683
 
 
684
            if (amount > 0)
 
685
                /* Ok, now update the write pointer */
 
686
                pa_memblockq_seek(i->thread_info.render_memblockq, - ((int64_t) amount), PA_SEEK_RELATIVE);
 
687
 
 
688
            if (i->thread_info.rewrite_flush)
 
689
                pa_memblockq_silence(i->thread_info.render_memblockq);
 
690
 
 
691
            /* And reset the resampler */
 
692
            if (i->thread_info.resampler)
 
693
                pa_resampler_reset(i->thread_info.resampler);
 
694
        }
 
695
    }
 
696
 
 
697
    if (!called)
 
698
        if (i->process_rewind)
 
699
            i->process_rewind(i, 0);
 
700
 
 
701
    i->thread_info.rewrite_nbytes = 0;
 
702
    i->thread_info.rewrite_flush = FALSE;
 
703
}
 
704
 
 
705
/* Called from thread context */
 
706
void pa_sink_input_update_max_rewind(pa_sink_input *i, size_t nbytes  /* in the sink's sample spec */) {
 
707
    pa_sink_input_assert_ref(i);
 
708
    pa_assert(PA_SINK_INPUT_IS_LINKED(i->thread_info.state));
 
709
    pa_assert(pa_frame_aligned(nbytes, &i->sink->sample_spec));
 
710
 
 
711
    pa_memblockq_set_maxrewind(i->thread_info.render_memblockq, nbytes);
 
712
 
 
713
    if (i->update_max_rewind)
 
714
        i->update_max_rewind(i, i->thread_info.resampler ? pa_resampler_request(i->thread_info.resampler, nbytes) : nbytes);
 
715
}
 
716
 
 
717
/* Called from thread context */
 
718
void pa_sink_input_update_max_request(pa_sink_input *i, size_t nbytes  /* in the sink's sample spec */) {
 
719
    pa_sink_input_assert_ref(i);
 
720
    pa_assert(PA_SINK_INPUT_IS_LINKED(i->thread_info.state));
 
721
    pa_assert(pa_frame_aligned(nbytes, &i->sink->sample_spec));
 
722
 
 
723
    if (i->update_max_request)
 
724
        i->update_max_request(i, i->thread_info.resampler ? pa_resampler_request(i->thread_info.resampler, nbytes) : nbytes);
 
725
}
 
726
 
 
727
/* Called from thread context */
 
728
static pa_usec_t fixup_latency(pa_sink *s, pa_usec_t usec) {
 
729
    pa_sink_assert_ref(s);
 
730
 
 
731
    if (usec == (pa_usec_t) -1)
 
732
        return usec;
 
733
 
 
734
    if (s->thread_info.max_latency > 0 && usec > s->thread_info.max_latency)
 
735
        usec = s->thread_info.max_latency;
 
736
 
 
737
    if (s->thread_info.min_latency > 0 && usec < s->thread_info.min_latency)
 
738
        usec = s->thread_info.min_latency;
 
739
 
 
740
    return usec;
 
741
}
 
742
 
 
743
/* Called from thread context */
 
744
pa_usec_t pa_sink_input_set_requested_latency_within_thread(pa_sink_input *i, pa_usec_t usec) {
 
745
    pa_sink_input_assert_ref(i);
 
746
 
 
747
    usec = fixup_latency(i->sink, usec);
 
748
    i->thread_info.requested_sink_latency = usec;
 
749
    pa_sink_invalidate_requested_latency(i->sink);
 
750
 
 
751
    return usec;
 
752
}
 
753
 
 
754
/* Called from main context */
 
755
pa_usec_t pa_sink_input_set_requested_latency(pa_sink_input *i, pa_usec_t usec) {
 
756
    pa_sink_input_assert_ref(i);
 
757
 
 
758
    if (PA_SINK_INPUT_IS_LINKED(i->state))
 
759
        pa_assert_se(pa_asyncmsgq_send(i->sink->asyncmsgq, PA_MSGOBJECT(i), PA_SINK_INPUT_MESSAGE_SET_REQUESTED_LATENCY, &usec, 0, NULL) == 0);
 
760
    else
 
761
        /* If this sink input is not realized yet, we have to touch
 
762
         * the thread info data directly */
 
763
 
 
764
        i->thread_info.requested_sink_latency = usec;
 
765
 
 
766
    return usec;
 
767
}
 
768
 
 
769
/* Called from main context */
 
770
pa_usec_t pa_sink_input_get_requested_latency(pa_sink_input *i) {
 
771
    pa_usec_t usec = 0;
 
772
 
 
773
    pa_sink_input_assert_ref(i);
 
774
 
 
775
    if (PA_SINK_INPUT_IS_LINKED(i->state))
 
776
        pa_assert_se(pa_asyncmsgq_send(i->sink->asyncmsgq, PA_MSGOBJECT(i), PA_SINK_INPUT_MESSAGE_GET_REQUESTED_LATENCY, &usec, 0, NULL) == 0);
 
777
    else
 
778
        /* If this sink input is not realized yet, we have to touch
 
779
         * the thread info data directly */
 
780
        usec = i->thread_info.requested_sink_latency;
 
781
 
 
782
    return usec;
 
783
}
 
784
 
 
785
/* Called from main context */
645
786
void pa_sink_input_set_volume(pa_sink_input *i, const pa_cvolume *volume) {
646
787
    pa_sink_input_assert_ref(i);
647
 
    pa_assert(PA_SINK_INPUT_LINKED(i->state));
 
788
    pa_assert(PA_SINK_INPUT_IS_LINKED(i->state));
 
789
    pa_assert(volume);
648
790
 
649
791
    if (pa_cvolume_equal(&i->volume, volume))
650
792
        return;
651
793
 
652
794
    i->volume = *volume;
653
795
 
654
 
    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);
 
796
    pa_assert_se(pa_asyncmsgq_send(i->sink->asyncmsgq, PA_MSGOBJECT(i), PA_SINK_INPUT_MESSAGE_SET_VOLUME, &i->volume, 0, NULL) == 0);
655
797
    pa_subscription_post(i->sink->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_CHANGE, i->index);
656
798
}
657
799
 
 
800
/* Called from main context */
658
801
const pa_cvolume *pa_sink_input_get_volume(pa_sink_input *i) {
659
802
    pa_sink_input_assert_ref(i);
660
 
    pa_assert(PA_SINK_INPUT_LINKED(i->state));
 
803
    pa_assert(PA_SINK_INPUT_IS_LINKED(i->state));
661
804
 
662
805
    return &i->volume;
663
806
}
664
807
 
 
808
/* Called from main context */
665
809
void pa_sink_input_set_mute(pa_sink_input *i, pa_bool_t mute) {
666
810
    pa_assert(i);
667
811
    pa_sink_input_assert_ref(i);
668
 
    pa_assert(PA_SINK_INPUT_LINKED(i->state));
 
812
    pa_assert(PA_SINK_INPUT_IS_LINKED(i->state));
669
813
 
670
814
    if (!i->muted == !mute)
671
815
        return;
676
820
    pa_subscription_post(i->sink->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_CHANGE, i->index);
677
821
}
678
822
 
679
 
int pa_sink_input_get_mute(pa_sink_input *i) {
 
823
/* Called from main context */
 
824
pa_bool_t pa_sink_input_get_mute(pa_sink_input *i) {
680
825
    pa_sink_input_assert_ref(i);
681
 
    pa_assert(PA_SINK_INPUT_LINKED(i->state));
 
826
    pa_assert(PA_SINK_INPUT_IS_LINKED(i->state));
682
827
 
683
 
    return !!i->muted;
 
828
    return i->muted;
684
829
}
685
830
 
 
831
/* Called from main context */
686
832
void pa_sink_input_cork(pa_sink_input *i, pa_bool_t b) {
687
833
    pa_sink_input_assert_ref(i);
688
 
    pa_assert(PA_SINK_INPUT_LINKED(i->state));
 
834
    pa_assert(PA_SINK_INPUT_IS_LINKED(i->state));
689
835
 
690
836
    sink_input_set_state(i, b ? PA_SINK_INPUT_CORKED : PA_SINK_INPUT_RUNNING);
691
837
}
692
838
 
 
839
/* Called from main context */
693
840
int pa_sink_input_set_rate(pa_sink_input *i, uint32_t rate) {
694
841
    pa_sink_input_assert_ref(i);
695
 
    pa_assert(PA_SINK_INPUT_LINKED(i->state));
 
842
    pa_assert(PA_SINK_INPUT_IS_LINKED(i->state));
696
843
    pa_return_val_if_fail(i->thread_info.resampler, -1);
697
844
 
698
845
    if (i->sample_spec.rate == rate)
706
853
    return 0;
707
854
}
708
855
 
 
856
/* Called from main context */
709
857
void pa_sink_input_set_name(pa_sink_input *i, const char *name) {
 
858
    const char *old;
710
859
    pa_sink_input_assert_ref(i);
711
860
 
712
 
    if (!i->name && !name)
713
 
        return;
714
 
 
715
 
    if (i->name && name && !strcmp(i->name, name))
716
 
        return;
717
 
 
718
 
    pa_xfree(i->name);
719
 
    i->name = pa_xstrdup(name);
720
 
 
721
 
    if (PA_SINK_INPUT_LINKED(i->state)) {
722
 
        pa_hook_fire(&i->sink->core->hooks[PA_CORE_HOOK_SINK_INPUT_NAME_CHANGED], i);
 
861
    if (!name && !pa_proplist_contains(i->proplist, PA_PROP_MEDIA_NAME))
 
862
        return;
 
863
 
 
864
    old = pa_proplist_gets(i->proplist, PA_PROP_MEDIA_NAME);
 
865
 
 
866
    if (old && name && !strcmp(old, name))
 
867
        return;
 
868
 
 
869
    if (name)
 
870
        pa_proplist_sets(i->proplist, PA_PROP_MEDIA_NAME, name);
 
871
    else
 
872
        pa_proplist_unset(i->proplist, PA_PROP_MEDIA_NAME);
 
873
 
 
874
    if (PA_SINK_INPUT_IS_LINKED(i->state)) {
 
875
        pa_hook_fire(&i->sink->core->hooks[PA_CORE_HOOK_SINK_INPUT_PROPLIST_CHANGED], i);
723
876
        pa_subscription_post(i->sink->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_CHANGE, i->index);
724
877
    }
725
878
}
726
879
 
 
880
/* Called from main context */
727
881
pa_resample_method_t pa_sink_input_get_resample_method(pa_sink_input *i) {
728
882
    pa_sink_input_assert_ref(i);
729
883
 
730
884
    return i->resample_method;
731
885
}
732
886
 
733
 
int pa_sink_input_move_to(pa_sink_input *i, pa_sink *dest, int immediately) {
 
887
/* Called from main context */
 
888
int pa_sink_input_move_to(pa_sink_input *i, pa_sink *dest) {
734
889
    pa_resampler *new_resampler;
735
890
    pa_sink *origin;
736
 
    pa_usec_t silence_usec = 0;
737
 
    pa_sink_input_move_info info;
738
891
    pa_sink_input_move_hook_data hook_data;
 
892
    pa_source_output *o, *p = NULL;
739
893
 
740
894
    pa_sink_input_assert_ref(i);
741
 
    pa_assert(PA_SINK_INPUT_LINKED(i->state));
 
895
    pa_assert(PA_SINK_INPUT_IS_LINKED(i->state));
742
896
    pa_sink_assert_ref(dest);
743
897
 
744
898
    origin = i->sink;
759
913
        return -1;
760
914
    }
761
915
 
 
916
    /* Kill directly connected outputs */
 
917
    while ((o = pa_idxset_first(i->direct_outputs, NULL))) {
 
918
        pa_assert(o != p);
 
919
        pa_source_output_kill(o);
 
920
        p = o;
 
921
    }
 
922
 
762
923
    if (i->thread_info.resampler &&
763
924
        pa_sample_spec_equal(&origin->sample_spec, &dest->sample_spec) &&
764
925
        pa_channel_map_equal(&origin->channel_map, &dest->channel_map))
790
951
    hook_data.destination = dest;
791
952
    pa_hook_fire(&i->sink->core->hooks[PA_CORE_HOOK_SINK_INPUT_MOVE], &hook_data);
792
953
 
793
 
    memset(&info, 0, sizeof(info));
794
 
    info.sink_input = i;
795
 
 
796
 
    if (!immediately) {
797
 
        pa_usec_t old_latency, new_latency;
798
 
 
799
 
        /* Let's do a little bit of Voodoo for compensating latency
800
 
         * differences. We assume that the accuracy for our
801
 
         * estimations is still good enough, even though we do these
802
 
         * operations non-atomic. */
803
 
 
804
 
        old_latency = pa_sink_get_latency(origin);
805
 
        new_latency = pa_sink_get_latency(dest);
806
 
 
807
 
        /* The already resampled data should go to the old sink */
808
 
 
809
 
        if (old_latency >= new_latency) {
810
 
 
811
 
            /* The latency of the old sink is larger than the latency
812
 
             * of the new sink. Therefore to compensate for the
813
 
             * difference we to play silence on the new one for a
814
 
             * while */
815
 
 
816
 
            silence_usec = old_latency - new_latency;
817
 
 
818
 
        } else {
819
 
 
820
 
            /* The latency of new sink is larger than the latency of
821
 
             * the old sink. Therefore we have to precompute a little
822
 
             * and make sure that this is still played on the old
823
 
             * sink, until we can play the first sample on the new
824
 
             * sink.*/
825
 
 
826
 
            info.buffer_bytes = pa_usec_to_bytes(new_latency - old_latency, &origin->sample_spec);
827
 
        }
828
 
 
829
 
        /* Okey, let's move it */
830
 
 
831
 
        if (info.buffer_bytes > 0) {
832
 
 
833
 
            info.ghost_sink_input = pa_memblockq_sink_input_new(
834
 
                    origin,
835
 
                    "Ghost Stream",
836
 
                    &origin->sample_spec,
837
 
                    &origin->channel_map,
838
 
                    NULL,
839
 
                    NULL);
840
 
 
841
 
            info.ghost_sink_input->thread_info.state = info.ghost_sink_input->state = PA_SINK_INPUT_RUNNING;
842
 
            info.ghost_sink_input->thread_info.volume = info.ghost_sink_input->volume;
843
 
            info.ghost_sink_input->thread_info.muted = info.ghost_sink_input->muted;
844
 
 
845
 
            info.buffer = pa_memblockq_new(0, MOVE_BUFFER_LENGTH, 0, pa_frame_size(&origin->sample_spec), 0, 0, NULL);
846
 
        }
847
 
    }
848
 
 
849
 
    pa_asyncmsgq_send(i->sink->asyncmsgq, PA_MSGOBJECT(i->sink), PA_SINK_MESSAGE_REMOVE_INPUT_AND_BUFFER, &info, 0, NULL);
850
 
 
851
 
    if (info.ghost_sink_input) {
852
 
        /* Basically, do what pa_sink_input_put() does ...*/
853
 
 
854
 
        pa_subscription_post(i->sink->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_NEW, info.ghost_sink_input->index);
855
 
        pa_hook_fire(&i->sink->core->hooks[PA_CORE_HOOK_SINK_INPUT_PUT], info.ghost_sink_input);
856
 
        pa_sink_input_unref(info.ghost_sink_input);
857
 
    }
 
954
    pa_assert_se(pa_asyncmsgq_send(i->sink->asyncmsgq, PA_MSGOBJECT(i->sink), PA_SINK_MESSAGE_START_MOVE, i, 0, NULL) == 0);
858
955
 
859
956
    pa_idxset_remove_by_data(origin->inputs, i, NULL);
860
957
    pa_idxset_put(dest->inputs, i, NULL);
865
962
        dest->n_corked++;
866
963
    }
867
964
 
868
 
    /* Replace resampler */
 
965
    /* Replace resampler and render queue */
869
966
    if (new_resampler != i->thread_info.resampler) {
 
967
 
870
968
        if (i->thread_info.resampler)
871
969
            pa_resampler_free(i->thread_info.resampler);
872
970
        i->thread_info.resampler = new_resampler;
873
971
 
874
 
        /* if the resampler changed, the silence memblock is
875
 
         * probably invalid now, too */
876
 
        if (i->thread_info.silence_memblock) {
877
 
            pa_memblock_unref(i->thread_info.silence_memblock);
878
 
            i->thread_info.silence_memblock = NULL;
879
 
        }
880
 
    }
881
 
 
882
 
    /* Dump already resampled data */
883
 
    if (i->thread_info.resampled_chunk.memblock) {
884
 
        /* Hmm, this data has already been added to the ghost queue, presumably, hence let's sleep a little bit longer */
885
 
        silence_usec += pa_bytes_to_usec(i->thread_info.resampled_chunk.length, &origin->sample_spec);
886
 
        pa_memblock_unref(i->thread_info.resampled_chunk.memblock);
887
 
        pa_memchunk_reset(&i->thread_info.resampled_chunk);
888
 
    }
889
 
 
890
 
    /* Calculate the new sleeping time */
891
 
    if (immediately)
892
 
        i->thread_info.move_silence = 0;
893
 
    else
894
 
        i->thread_info.move_silence = pa_usec_to_bytes(
895
 
                pa_bytes_to_usec(i->thread_info.move_silence, &origin->sample_spec) +
896
 
                silence_usec,
897
 
                &dest->sample_spec);
898
 
 
899
 
    pa_asyncmsgq_send(i->sink->asyncmsgq, PA_MSGOBJECT(i->sink), PA_SINK_MESSAGE_ADD_INPUT, i, 0, NULL);
 
972
        pa_memblockq_free(i->thread_info.render_memblockq);
 
973
 
 
974
        i->thread_info.render_memblockq = pa_memblockq_new(
 
975
                0,
 
976
                MEMBLOCKQ_MAXLENGTH,
 
977
                0,
 
978
                pa_frame_size(&i->sink->sample_spec),
 
979
                0,
 
980
                1,
 
981
                0,
 
982
                &i->sink->silence);
 
983
    }
900
984
 
901
985
    pa_sink_update_status(origin);
902
986
    pa_sink_update_status(dest);
903
987
 
 
988
    pa_assert_se(pa_asyncmsgq_send(i->sink->asyncmsgq, PA_MSGOBJECT(i->sink), PA_SINK_MESSAGE_FINISH_MOVE, i, 0, NULL) == 0);
 
989
 
904
990
    if (i->moved)
905
991
        i->moved(i);
906
992
 
914
1000
    return 0;
915
1001
}
916
1002
 
917
 
/* Called from thread context */
 
1003
/* Called from IO thread context */
 
1004
void pa_sink_input_set_state_within_thread(pa_sink_input *i, pa_sink_input_state_t state) {
 
1005
    pa_bool_t corking, uncorking;
 
1006
    pa_sink_input_assert_ref(i);
 
1007
 
 
1008
    if (state == i->thread_info.state)
 
1009
        return;
 
1010
 
 
1011
    if ((state == PA_SINK_INPUT_DRAINED || state == PA_SINK_INPUT_RUNNING) &&
 
1012
        !(i->thread_info.state == PA_SINK_INPUT_DRAINED || i->thread_info.state != PA_SINK_INPUT_RUNNING))
 
1013
        pa_atomic_store(&i->thread_info.drained, 1);
 
1014
 
 
1015
    corking = state == PA_SINK_INPUT_CORKED && i->thread_info.state == PA_SINK_INPUT_RUNNING;
 
1016
    uncorking = i->thread_info.state == PA_SINK_INPUT_CORKED && state == PA_SINK_INPUT_RUNNING;
 
1017
 
 
1018
    if (i->state_change)
 
1019
        i->state_change(i, state);
 
1020
 
 
1021
    i->thread_info.state = state;
 
1022
 
 
1023
    if (corking) {
 
1024
 
 
1025
        pa_log_debug("Requesting rewind due to corking");
 
1026
 
 
1027
        /* This will tell the implementing sink input driver to rewind
 
1028
         * so that the unplayed already mixed data is not lost */
 
1029
        pa_sink_input_request_rewind(i, 0, TRUE, TRUE);
 
1030
 
 
1031
    } else if (uncorking) {
 
1032
 
 
1033
        i->thread_info.underrun_for = (uint64_t) -1;
 
1034
        i->thread_info.playing_for = 0;
 
1035
 
 
1036
        pa_log_debug("Requesting rewind due to uncorking");
 
1037
 
 
1038
        /* OK, we're being uncorked. Make sure we're not rewound when
 
1039
         * the hw buffer is remixed and request a remix. */
 
1040
        pa_sink_input_request_rewind(i, 0, FALSE, TRUE);
 
1041
    }
 
1042
}
 
1043
 
 
1044
/* Called from thread context, except when it is not. */
918
1045
int pa_sink_input_process_msg(pa_msgobject *o, int code, void *userdata, int64_t offset, pa_memchunk *chunk) {
919
1046
    pa_sink_input *i = PA_SINK_INPUT(o);
920
 
 
921
1047
    pa_sink_input_assert_ref(i);
922
 
    pa_assert(PA_SINK_INPUT_LINKED(i->thread_info.state));
923
1048
 
924
1049
    switch (code) {
 
1050
 
925
1051
        case PA_SINK_INPUT_MESSAGE_SET_VOLUME:
926
1052
            i->thread_info.volume = *((pa_cvolume*) userdata);
 
1053
            pa_sink_input_request_rewind(i, 0, TRUE, FALSE);
927
1054
            return 0;
928
1055
 
929
1056
        case PA_SINK_INPUT_MESSAGE_SET_MUTE:
930
1057
            i->thread_info.muted = PA_PTR_TO_UINT(userdata);
 
1058
            pa_sink_input_request_rewind(i, 0, TRUE, FALSE);
931
1059
            return 0;
932
1060
 
933
1061
        case PA_SINK_INPUT_MESSAGE_GET_LATENCY: {
934
1062
            pa_usec_t *r = userdata;
935
 
 
936
 
            if (i->thread_info.resampled_chunk.memblock)
937
 
                *r += pa_bytes_to_usec(i->thread_info.resampled_chunk.length, &i->sink->sample_spec);
938
 
 
939
 
            if (i->thread_info.move_silence)
940
 
                *r += pa_bytes_to_usec(i->thread_info.move_silence, &i->sink->sample_spec);
 
1063
            pa_usec_t sink_usec = 0;
 
1064
 
 
1065
            r[0] += pa_bytes_to_usec(pa_memblockq_get_length(i->thread_info.render_memblockq), &i->sink->sample_spec);
 
1066
 
 
1067
            if (i->sink->parent.process_msg(PA_MSGOBJECT(i->sink), PA_SINK_MESSAGE_GET_LATENCY, &sink_usec, 0, NULL) >= 0)
 
1068
                r[1] += sink_usec;
941
1069
 
942
1070
            return 0;
943
1071
        }
952
1080
        case PA_SINK_INPUT_MESSAGE_SET_STATE: {
953
1081
            pa_sink_input *ssync;
954
1082
 
955
 
            if ((PA_PTR_TO_UINT(userdata) == PA_SINK_INPUT_DRAINED || PA_PTR_TO_UINT(userdata) == PA_SINK_INPUT_RUNNING) &&
956
 
                (i->thread_info.state != PA_SINK_INPUT_DRAINED) && (i->thread_info.state != PA_SINK_INPUT_RUNNING))
957
 
                pa_atomic_store(&i->thread_info.drained, 1);
958
 
 
959
 
            i->thread_info.state = PA_PTR_TO_UINT(userdata);
960
 
 
961
 
            for (ssync = i->thread_info.sync_prev; ssync; ssync = ssync->thread_info.sync_prev) {
962
 
                if ((PA_PTR_TO_UINT(userdata) == PA_SINK_INPUT_DRAINED || PA_PTR_TO_UINT(userdata) == PA_SINK_INPUT_RUNNING) &&
963
 
                    (ssync->thread_info.state != PA_SINK_INPUT_DRAINED) && (ssync->thread_info.state != PA_SINK_INPUT_RUNNING))
964
 
                    pa_atomic_store(&ssync->thread_info.drained, 1);
965
 
                ssync->thread_info.state = PA_PTR_TO_UINT(userdata);
966
 
            }
967
 
 
968
 
            for (ssync = i->thread_info.sync_next; ssync; ssync = ssync->thread_info.sync_next) {
969
 
                if ((PA_PTR_TO_UINT(userdata) == PA_SINK_INPUT_DRAINED || PA_PTR_TO_UINT(userdata) == PA_SINK_INPUT_RUNNING) &&
970
 
                    (ssync->thread_info.state != PA_SINK_INPUT_DRAINED) && (ssync->thread_info.state != PA_SINK_INPUT_RUNNING))
971
 
                    pa_atomic_store(&ssync->thread_info.drained, 1);
972
 
                ssync->thread_info.state = PA_PTR_TO_UINT(userdata);
973
 
            }
974
 
 
 
1083
            pa_sink_input_set_state_within_thread(i, PA_PTR_TO_UINT(userdata));
 
1084
 
 
1085
            for (ssync = i->thread_info.sync_prev; ssync; ssync = ssync->thread_info.sync_prev)
 
1086
                pa_sink_input_set_state_within_thread(ssync, PA_PTR_TO_UINT(userdata));
 
1087
 
 
1088
            for (ssync = i->thread_info.sync_next; ssync; ssync = ssync->thread_info.sync_next)
 
1089
                pa_sink_input_set_state_within_thread(ssync, PA_PTR_TO_UINT(userdata));
 
1090
 
 
1091
            return 0;
 
1092
        }
 
1093
 
 
1094
        case PA_SINK_INPUT_MESSAGE_SET_REQUESTED_LATENCY: {
 
1095
            pa_usec_t *usec = userdata;
 
1096
 
 
1097
            *usec = pa_sink_input_set_requested_latency_within_thread(i, *usec);
 
1098
            return 0;
 
1099
        }
 
1100
 
 
1101
        case PA_SINK_INPUT_MESSAGE_GET_REQUESTED_LATENCY: {
 
1102
            pa_usec_t *r = userdata;
 
1103
 
 
1104
            *r = i->thread_info.requested_sink_latency;
975
1105
            return 0;
976
1106
        }
977
1107
    }
979
1109
    return -1;
980
1110
}
981
1111
 
 
1112
/* Called from main thread */
982
1113
pa_sink_input_state_t pa_sink_input_get_state(pa_sink_input *i) {
983
1114
    pa_sink_input_assert_ref(i);
984
1115
 
987
1118
 
988
1119
    return i->state;
989
1120
}
 
1121
 
 
1122
/* Called from IO context */
 
1123
pa_bool_t pa_sink_input_safe_to_remove(pa_sink_input *i) {
 
1124
    pa_sink_input_assert_ref(i);
 
1125
 
 
1126
    if (PA_SINK_INPUT_IS_LINKED(i->thread_info.state))
 
1127
        return pa_memblockq_is_empty(i->thread_info.render_memblockq);
 
1128
 
 
1129
    return TRUE;
 
1130
}
 
1131
 
 
1132
/* Called from IO context */
 
1133
void pa_sink_input_request_rewind(pa_sink_input *i, size_t nbytes  /* in our sample spec */, pa_bool_t rewrite, pa_bool_t flush) {
 
1134
    size_t lbq;
 
1135
 
 
1136
    /* If 'rewrite' is TRUE the sink is rewound as far as requested
 
1137
     * and possible and the exact value of this is passed back the
 
1138
     * implementor via process_rewind(). If 'flush' is also TRUE all
 
1139
     * already rendered data is also dropped.
 
1140
     *
 
1141
     * If 'rewrite' is FALSE the sink is rewound as far as requested
 
1142
     * and possible and the already rendered data is dropped so that
 
1143
     * in the next iteration we read new data from the
 
1144
     * implementor. This implies 'flush' is TRUE. */
 
1145
 
 
1146
    pa_sink_input_assert_ref(i);
 
1147
    pa_assert(i->thread_info.rewrite_nbytes == 0);
 
1148
 
 
1149
/*     pa_log_debug("request rewrite %lu", (unsigned long) nbytes); */
 
1150
 
 
1151
    /* We don't take rewind requests while we are corked */
 
1152
    if (i->thread_info.state == PA_SINK_INPUT_CORKED)
 
1153
        return;
 
1154
 
 
1155
    pa_assert(rewrite || flush);
 
1156
 
 
1157
    /* Calculate how much we can rewind locally without having to
 
1158
     * touch the sink */
 
1159
    if (rewrite)
 
1160
        lbq = pa_memblockq_get_length(i->thread_info.render_memblockq);
 
1161
    else
 
1162
        lbq = 0;
 
1163
 
 
1164
    /* Check if rewinding for the maximum is requested, and if so, fix up */
 
1165
    if (nbytes <= 0) {
 
1166
 
 
1167
        /* Calculate maximum number of bytes that could be rewound in theory */
 
1168
        nbytes = i->sink->thread_info.max_rewind + lbq;
 
1169
 
 
1170
        /* Transform from sink domain */
 
1171
        if (i->thread_info.resampler)
 
1172
            nbytes = pa_resampler_request(i->thread_info.resampler, nbytes);
 
1173
    }
 
1174
 
 
1175
    if (rewrite) {
 
1176
        /* Make sure to not overwrite over underruns */
 
1177
        if (nbytes > i->thread_info.playing_for)
 
1178
            nbytes = (size_t) i->thread_info.playing_for;
 
1179
 
 
1180
        i->thread_info.rewrite_nbytes = nbytes;
 
1181
    } else
 
1182
        i->thread_info.rewrite_nbytes = (size_t) -1;
 
1183
 
 
1184
    i->thread_info.rewrite_flush = flush && i->thread_info.rewrite_nbytes != 0;
 
1185
 
 
1186
    /* Transform to sink domain */
 
1187
    if (i->thread_info.resampler)
 
1188
        nbytes = pa_resampler_result(i->thread_info.resampler, nbytes);
 
1189
 
 
1190
    if (nbytes > lbq)
 
1191
        pa_sink_request_rewind(i->sink, nbytes - lbq);
 
1192
    else
 
1193
        /* This call will make sure process_rewind() is called later */
 
1194
        pa_sink_request_rewind(i->sink, 0);
 
1195
}
 
1196
 
 
1197
/* Called from main context */
 
1198
pa_memchunk* pa_sink_input_get_silence(pa_sink_input *i, pa_memchunk *ret) {
 
1199
    pa_sink_input_assert_ref(i);
 
1200
    pa_assert(ret);
 
1201
 
 
1202
    pa_silence_memchunk_get(
 
1203
                &i->sink->core->silence_cache,
 
1204
                i->sink->core->mempool,
 
1205
                ret,
 
1206
                &i->sample_spec,
 
1207
                i->thread_info.resampler ? pa_resampler_max_block_size(i->thread_info.resampler) : 0);
 
1208
 
 
1209
    return ret;
 
1210
}