summaryrefslogtreecommitdiffstats
path: root/src/sink.c
blob: 2ecb644525edfd62ffd313a592da7b6d149f8ab0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <stdio.h>

#include "sink.h"
#include "sinkinput.h"
#include "strbuf.h"
#include "sample-util.h"
#include "namereg.h"

#define MAX_MIX_CHANNELS 32

struct sink* sink_new(struct core *core, const char *name, int fail, const struct pa_sample_spec *spec) {
    struct sink *s;
    char *n = NULL;
    int r;
    assert(core && spec);

    s = malloc(sizeof(struct sink));
    assert(s);

    if (!(name = namereg_register(core, name, NAMEREG_SINK, s, fail))) {
        free(s);
        return NULL;
    }
    
    s->name = strdup(name);
    s->core = core;
    s->sample_spec = *spec;
    s->inputs = idxset_new(NULL, NULL);

    if (name) {
        n = malloc(strlen(name)+9);
        sprintf(n, "%s_monitor", name);
    }
    
    s->monitor_source = source_new(core, n, 0, spec);
    assert(s->monitor_source);
    free(n);
    
    s->volume = VOLUME_NORM;

    s->notify = NULL;
    s->get_latency = NULL;
    s->userdata = NULL;

    r = idxset_put(core->sinks, s, &s->index);
    assert(s->index != IDXSET_INVALID && r >= 0);
    
    fprintf(stderr, "sink: created %u \"%s\".\n", s->index, s->name);
    
    return s;
}

void sink_free(struct sink *s) {
    struct sink_input *i, *j = NULL;
    assert(s);

    namereg_unregister(s->core, s->name);
    
    while ((i = idxset_first(s->inputs, NULL))) {
        assert(i != j);
        sink_input_kill(i);
        j = i;
    }
    idxset_free(s->inputs, NULL, NULL);

    source_free(s->monitor_source);
    idxset_remove_by_data(s->core->sinks, s, NULL);

    fprintf(stderr, "sink: freed %u \"%s\"\n", s->index, s->name);
    
    free(s->name);
    free(s);
}

void sink_notify(struct sink*s) {
    assert(s);

    if (s->notify)
        s->notify(s);
}

static unsigned fill_mix_info(struct sink *s, struct mix_info *info, unsigned maxinfo) {
    uint32_t index = IDXSET_INVALID;
    struct sink_input *i;
    unsigned n = 0;
    
    assert(s && info);

    for (i = idxset_first(s->inputs, &index); maxinfo > 0 && i; i = idxset_next(s->inputs, &index)) {
        if (sink_input_peek(i, &info->chunk) < 0)
            continue;

        info->volume = i->volume;
        
        assert(info->chunk.memblock && info->chunk.memblock->data && info->chunk.length);
        info->userdata = i;
        
        info++;
        maxinfo--;
        n++;
    }

    return n;
}

static void inputs_drop(struct sink *s, struct mix_info *info, unsigned maxinfo, size_t length) {
    assert(s && info);
    
    for (; maxinfo > 0; maxinfo--, info++) {
        struct sink_input *i = info->userdata;
        assert(i && info->chunk.memblock);
        
        memblock_unref(info->chunk.memblock);
        sink_input_drop(i, length);
    }
}
        
int sink_render(struct sink*s, size_t length, struct memchunk *result) {
    struct mix_info info[MAX_MIX_CHANNELS];
    unsigned n;
    size_t l;
    assert(s && length && result);
    
    n = fill_mix_info(s, info, MAX_MIX_CHANNELS);

    if (n <= 0)
        return -1;

    if (n == 1) {
        uint32_t volume = VOLUME_NORM;
        struct sink_info *i = info[0].userdata;
        assert(i);
        *result = info[0].chunk;
        memblock_ref(result->memblock);

        if (result->length > length)
            result->length = length;

        l = result->length;

        if (s->volume != VOLUME_NORM || info[0].volume != VOLUME_NORM)
            volume = volume_multiply(s->volume, info[0].volume);
        
        if (volume != VOLUME_NORM) {
            memchunk_make_writable(result);
            volume_memchunk(result, &s->sample_spec, volume);
        }
    } else {
        result->memblock = memblock_new(length);
        assert(result->memblock);

        result->length = l = mix_chunks(info, n, result->memblock->data, length, &s->sample_spec, s->volume);
        result->index = 0;
        
        assert(l);
    }

    inputs_drop(s, info, n, l);

    assert(s->monitor_source);
    source_post(s->monitor_source, result);

    return 0;
}

int sink_render_into(struct sink*s, struct memchunk *target) {
    struct mix_info info[MAX_MIX_CHANNELS];
    unsigned n;
    size_t l;
    assert(s && target && target->length && target->memblock && target->memblock->data);
    memblock_assert_exclusive(target->memblock);
    
    n = fill_mix_info(s, info, MAX_MIX_CHANNELS);

    if (n <= 0)
        return -1;

    if (n == 1) {
        uint32_t volume = VOLUME_NORM;
        struct sink_info *i = info[0].userdata;
        assert(i);

        l = target->length;
        if (l > info[0].chunk.length)
            l = info[0].chunk.length;
        
        memcpy(target->memblock->data+target->index, info[0].chunk.memblock->data + info[0].chunk.index, l);
        target->length = l;

        if (s->volume != VOLUME_NORM || info[0].volume != VOLUME_NORM)
            volume = volume_multiply(s->volume, info[0].volume);

        if (volume != VOLUME_NORM)
            volume_memchunk(target, &s->sample_spec, volume);
    } else
        target->length = l = mix_chunks(info, n, target->memblock->data+target->index, target->length, &s->sample_spec, s->volume);
    
    assert(l);
    inputs_drop(s, info, n, l);

    assert(s->monitor_source);
    source_post(s->monitor_source, target);

    return 0;
}

void sink_render_into_full(struct sink *s, struct memchunk *target) {
    struct memchunk chunk;
    size_t l, d;
    assert(s && target && target->memblock && target->length && target->memblock->data);

    l = target->length;
    d = 0;
    while (l > 0) {
        chunk = *target;
        chunk.index += d;
        chunk.length -= d;
        
        if (sink_render_into(s, &chunk) < 0)
            break;

        d += chunk.length;
        l -= chunk.length;
    }

    if (l > 0) {
        chunk = *target;
        chunk.index += d;
        chunk.length -= d;
        silence_memchunk(&chunk, &s->sample_spec);
    }
}

uint32_t sink_get_latency(struct sink *s) {
    assert(s);

    if (!s->get_latency)
        return 0;

    return s->get_latency(s);
}

struct sink* sink_get_default(struct core *c) {
    struct sink *sink;
    assert(c);

    if ((sink = idxset_get_by_index(c->sinks, c->default_sink_index)))
        return sink;

    if (!(sink = idxset_first(c->sinks, &c->default_sink_index)))
        return NULL;

    fprintf(stderr, "core: default sink vanished, setting to %u.\n", sink->index);
    return sink;
}

char *sink_list_to_string(struct core *c) {
    struct strbuf *s;
    struct sink *sink, *default_sink;
    uint32_t index = IDXSET_INVALID;
    assert(c);

    s = strbuf_new();
    assert(s);

    strbuf_printf(s, "%u sink(s) available.\n", idxset_ncontents(c->sinks));

    default_sink = sink_get_default(c);
    
    for (sink = idxset_first(c->sinks, &index); sink; sink = idxset_next(c->sinks, &index)) {
        assert(sink->monitor_source);
        strbuf_printf(s, "  %c index: %u, name: <%s>, volume: <0x%04x>, latency: <%u usec>, monitor_source: <%u>\n", sink == default_sink ? '*' : ' ', sink->index, sink->name, (unsigned) sink->volume, sink_get_latency(sink), sink->monitor_source->index);
    }
    
    return strbuf_tostring_free(s);
}