~ubuntu-branches/ubuntu/jaunty/pulseaudio/jaunty-updates

« back to all changes in this revision

Viewing changes to src/modules/module-combine.c

  • Committer: Bazaar Package Importer
  • Author(s): Oliver Grawert
  • Date: 2006-11-12 20:00:18 UTC
  • Revision ID: james.westby@ubuntu.com-20061112200018-oji9njq7rr3te53k
Tags: upstream-0.9.5
ImportĀ upstreamĀ versionĀ 0.9.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $Id: module-combine.c 1333 2006-08-26 19:00:22Z lennart $ */
 
2
 
 
3
/***
 
4
  This file is part of PulseAudio.
 
5
 
 
6
  PulseAudio is free software; you can redistribute it and/or modify
 
7
  it under the terms of the GNU Lesser General Public License as published
 
8
  by the Free Software Foundation; either version 2 of the License,
 
9
  or (at your option) any later version.
 
10
 
 
11
  PulseAudio is distributed in the hope that it will be useful, but
 
12
  WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 
14
  General Public License for more details.
 
15
 
 
16
  You should have received a copy of the GNU Lesser General Public License
 
17
  along with PulseAudio; if not, write to the Free Software
 
18
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 
19
  USA.
 
20
***/
 
21
 
 
22
#ifdef HAVE_CONFIG_H
 
23
#include <config.h>
 
24
#endif
 
25
 
 
26
#include <assert.h>
 
27
#include <stdio.h>
 
28
 
 
29
#include <pulse/timeval.h>
 
30
#include <pulse/xmalloc.h>
 
31
 
 
32
#include <pulsecore/module.h>
 
33
#include <pulsecore/llist.h>
 
34
#include <pulsecore/sink.h>
 
35
#include <pulsecore/sink-input.h>
 
36
#include <pulsecore/memblockq.h>
 
37
#include <pulsecore/log.h>
 
38
#include <pulsecore/core-util.h>
 
39
#include <pulsecore/modargs.h>
 
40
#include <pulsecore/namereg.h>
 
41
 
 
42
#include "module-combine-symdef.h"
 
43
 
 
44
PA_MODULE_AUTHOR("Lennart Poettering")
 
45
PA_MODULE_DESCRIPTION("Combine multiple sinks to one")
 
46
PA_MODULE_VERSION(PACKAGE_VERSION)
 
47
PA_MODULE_USAGE(
 
48
        "sink_name=<name for the sink> "
 
49
        "master=<master sink> "
 
50
        "slaves=<slave sinks> "
 
51
        "adjust_time=<seconds> "
 
52
        "resample_method=<method> "
 
53
        "format=<sample format> "
 
54
        "channels=<number of channels> "
 
55
        "rate=<sample rate> "
 
56
        "channel_map=<channel map> ")
 
57
 
 
58
#define DEFAULT_SINK_NAME "combined"
 
59
#define MEMBLOCKQ_MAXLENGTH (1024*170)
 
60
#define RENDER_SIZE (1024*10)
 
61
 
 
62
#define DEFAULT_ADJUST_TIME 20
 
63
 
 
64
static const char* const valid_modargs[] = {
 
65
    "sink_name",
 
66
    "master",
 
67
    "slaves",
 
68
    "adjust_time",
 
69
    "resample_method",
 
70
    "format",
 
71
    "channels",
 
72
    "rate",
 
73
    "channel_map",
 
74
    NULL
 
75
};
 
76
 
 
77
struct output {
 
78
    struct userdata *userdata;
 
79
    pa_sink_input *sink_input;
 
80
    size_t counter;
 
81
    pa_memblockq *memblockq;
 
82
    pa_usec_t total_latency;
 
83
    PA_LLIST_FIELDS(struct output);
 
84
};
 
85
 
 
86
struct userdata {
 
87
    pa_module *module;
 
88
    pa_core *core;
 
89
    pa_sink *sink;
 
90
    unsigned n_outputs;
 
91
    struct output *master;
 
92
    pa_time_event *time_event;
 
93
    uint32_t adjust_time;
 
94
    
 
95
    PA_LLIST_HEAD(struct output, outputs);
 
96
};
 
97
 
 
98
static void output_free(struct output *o);
 
99
static void clear_up(struct userdata *u);
 
100
 
 
101
static void update_usage(struct userdata *u) {
 
102
    pa_module_set_used(u->module, u->sink ? pa_sink_used_by(u->sink) : 0);
 
103
}
 
104
 
 
105
static void adjust_rates(struct userdata *u) {
 
106
    struct output *o;
 
107
    pa_usec_t max_sink_latency = 0, min_total_latency = (pa_usec_t) -1, target_latency;
 
108
    uint32_t base_rate;
 
109
    assert(u && u->sink);
 
110
 
 
111
    for (o = u->outputs; o; o = o->next) {
 
112
        uint32_t sink_latency = o->sink_input->sink ? pa_sink_get_latency(o->sink_input->sink) : 0;
 
113
        
 
114
        o->total_latency = sink_latency + pa_sink_input_get_latency(o->sink_input);
 
115
        
 
116
        if (sink_latency > max_sink_latency)
 
117
            max_sink_latency = sink_latency;
 
118
 
 
119
        if (o->total_latency < min_total_latency)
 
120
            min_total_latency = o->total_latency;
 
121
    }
 
122
 
 
123
    assert(min_total_latency != (pa_usec_t) -1);
 
124
 
 
125
    target_latency = max_sink_latency > min_total_latency ? max_sink_latency : min_total_latency;
 
126
    
 
127
    pa_log_info("[%s] target latency is %0.0f usec.", u->sink->name, (float) target_latency);
 
128
 
 
129
    base_rate = u->sink->sample_spec.rate;
 
130
 
 
131
    for (o = u->outputs; o; o = o->next) {
 
132
        uint32_t r = base_rate; 
 
133
        
 
134
        if (o->total_latency < target_latency)
 
135
            r -= (uint32_t) (((((double) target_latency - o->total_latency))/u->adjust_time)*r/ 1000000);
 
136
        else if (o->total_latency > target_latency)
 
137
            r += (uint32_t) (((((double) o->total_latency - target_latency))/u->adjust_time)*r/ 1000000);
 
138
 
 
139
        if (r < (uint32_t) (base_rate*0.9) || r > (uint32_t) (base_rate*1.1))
 
140
            pa_log_warn("[%s] sample rates too different, not adjusting (%u vs. %u).", o->sink_input->name, base_rate, r);
 
141
        else {
 
142
            pa_log_info("[%s] new rate is %u Hz; ratio is %0.3f; latency is %0.0f usec.", o->sink_input->name, r, (double) r / base_rate, (float) o->total_latency);
 
143
            pa_sink_input_set_rate(o->sink_input, r);
 
144
        }
 
145
    }
 
146
}
 
147
 
 
148
static void request_memblock(struct userdata *u) {
 
149
    pa_memchunk chunk;
 
150
    struct output *o;
 
151
    assert(u && u->sink);
 
152
 
 
153
    update_usage(u);
 
154
    
 
155
    if (pa_sink_render(u->sink, RENDER_SIZE, &chunk) < 0)
 
156
        return;
 
157
 
 
158
    for (o = u->outputs; o; o = o->next)
 
159
        pa_memblockq_push_align(o->memblockq, &chunk);
 
160
 
 
161
    pa_memblock_unref(chunk.memblock);
 
162
}
 
163
 
 
164
static void time_callback(pa_mainloop_api*a, pa_time_event* e, PA_GCC_UNUSED const struct timeval *tv, void *userdata) {
 
165
    struct userdata *u = userdata;
 
166
    struct timeval n;
 
167
    assert(u && a && u->time_event == e);
 
168
 
 
169
    adjust_rates(u);
 
170
 
 
171
    pa_gettimeofday(&n);
 
172
    n.tv_sec += u->adjust_time;
 
173
    u->sink->core->mainloop->time_restart(e, &n);
 
174
}
 
175
 
 
176
static int sink_input_peek_cb(pa_sink_input *i, pa_memchunk *chunk) {
 
177
    struct output *o = i->userdata;
 
178
    assert(i && o && o->sink_input && chunk);
 
179
 
 
180
    if (pa_memblockq_peek(o->memblockq, chunk) >= 0)
 
181
        return 0;
 
182
    
 
183
    /* Try harder */
 
184
    request_memblock(o->userdata);
 
185
    
 
186
    return pa_memblockq_peek(o->memblockq, chunk);
 
187
}
 
188
 
 
189
static void sink_input_drop_cb(pa_sink_input *i, const pa_memchunk *chunk, size_t length) {
 
190
    struct output *o = i->userdata;
 
191
    assert(i && o && o->sink_input && chunk && length);
 
192
 
 
193
    pa_memblockq_drop(o->memblockq, chunk, length);
 
194
    o->counter += length;
 
195
}
 
196
 
 
197
static void sink_input_kill_cb(pa_sink_input *i) {
 
198
    struct output *o = i->userdata;
 
199
    assert(i && o && o->sink_input);
 
200
    pa_module_unload_request(o->userdata->module);
 
201
    clear_up(o->userdata);
 
202
}
 
203
 
 
204
static pa_usec_t sink_input_get_latency_cb(pa_sink_input *i) {
 
205
    struct output *o = i->userdata;
 
206
    assert(i && o && o->sink_input);
 
207
    
 
208
    return pa_bytes_to_usec(pa_memblockq_get_length(o->memblockq), &i->sample_spec);
 
209
}
 
210
 
 
211
static pa_usec_t sink_get_latency_cb(pa_sink *s) {
 
212
    struct userdata *u = s->userdata;
 
213
    assert(s && u && u->sink && u->master);
 
214
 
 
215
    return
 
216
        pa_sink_input_get_latency(u->master->sink_input) +
 
217
        pa_sink_get_latency(u->master->sink_input->sink);
 
218
}
 
219
 
 
220
static void sink_notify(pa_sink *s) {
 
221
    struct userdata *u;
 
222
    struct output *o;
 
223
    
 
224
    assert(s);
 
225
    u = s->userdata;
 
226
    assert(u);
 
227
    
 
228
    for (o = u->outputs; o; o = o->next)
 
229
        pa_sink_notify(o->sink_input->sink);
 
230
}
 
231
 
 
232
static struct output *output_new(struct userdata *u, pa_sink *sink, int resample_method) {
 
233
    struct output *o = NULL;
 
234
    char t[256];
 
235
    pa_sink_input_new_data data;
 
236
    
 
237
    assert(u && sink && u->sink);
 
238
    
 
239
    o = pa_xmalloc(sizeof(struct output));
 
240
    o->userdata = u;
 
241
    
 
242
    o->counter = 0;
 
243
    o->memblockq = pa_memblockq_new(
 
244
            0,
 
245
            MEMBLOCKQ_MAXLENGTH,
 
246
            MEMBLOCKQ_MAXLENGTH,
 
247
            pa_frame_size(&u->sink->sample_spec),
 
248
            1,
 
249
            0,
 
250
            NULL);
 
251
 
 
252
    snprintf(t, sizeof(t), "Output stream #%u of sink %s", u->n_outputs+1, u->sink->name);
 
253
 
 
254
    pa_sink_input_new_data_init(&data);
 
255
    data.sink = sink;
 
256
    data.driver = __FILE__;
 
257
    data.name = t;
 
258
    pa_sink_input_new_data_set_sample_spec(&data, &u->sink->sample_spec);
 
259
    pa_sink_input_new_data_set_channel_map(&data, &u->sink->channel_map);
 
260
    data.module = u->module;
 
261
    
 
262
    if (!(o->sink_input = pa_sink_input_new(u->core, &data, PA_SINK_INPUT_VARIABLE_RATE)))
 
263
        goto fail;
 
264
 
 
265
    o->sink_input->get_latency = sink_input_get_latency_cb;
 
266
    o->sink_input->peek = sink_input_peek_cb;
 
267
    o->sink_input->drop = sink_input_drop_cb;
 
268
    o->sink_input->kill = sink_input_kill_cb;
 
269
    o->sink_input->userdata = o;
 
270
    
 
271
    PA_LLIST_PREPEND(struct output, u->outputs, o);
 
272
    u->n_outputs++;
 
273
    return o;
 
274
 
 
275
fail:
 
276
 
 
277
    if (o) {
 
278
        if (o->sink_input) {
 
279
            pa_sink_input_disconnect(o->sink_input);
 
280
            pa_sink_input_unref(o->sink_input);
 
281
        }
 
282
 
 
283
        if (o->memblockq)
 
284
            pa_memblockq_free(o->memblockq);
 
285
        
 
286
        pa_xfree(o);
 
287
    }
 
288
 
 
289
    return NULL;
 
290
}
 
291
 
 
292
static void output_free(struct output *o) {
 
293
    assert(o);
 
294
    PA_LLIST_REMOVE(struct output, o->userdata->outputs, o);
 
295
    o->userdata->n_outputs--;
 
296
    pa_memblockq_free(o->memblockq);
 
297
    pa_sink_input_disconnect(o->sink_input);
 
298
    pa_sink_input_unref(o->sink_input);
 
299
    pa_xfree(o);
 
300
}
 
301
 
 
302
static void clear_up(struct userdata *u) {
 
303
    struct output *o;
 
304
    assert(u);
 
305
    
 
306
    if (u->time_event) {
 
307
        u->core->mainloop->time_free(u->time_event);
 
308
        u->time_event = NULL;
 
309
    }
 
310
    
 
311
    while ((o = u->outputs))
 
312
        output_free(o);
 
313
 
 
314
    u->master = NULL;
 
315
    
 
316
    if (u->sink) {
 
317
        pa_sink_disconnect(u->sink);
 
318
        pa_sink_unref(u->sink);
 
319
        u->sink = NULL;
 
320
    }
 
321
}
 
322
 
 
323
int pa__init(pa_core *c, pa_module*m) {
 
324
    struct userdata *u;
 
325
    pa_modargs *ma = NULL;
 
326
    const char *master_name, *slaves, *rm;
 
327
    pa_sink *master_sink;
 
328
    char *n = NULL;
 
329
    const char*split_state;
 
330
    struct timeval tv;
 
331
    int resample_method = -1;
 
332
    pa_sample_spec ss;
 
333
    pa_channel_map map;
 
334
    
 
335
    assert(c && m);
 
336
 
 
337
    if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
 
338
        pa_log("failed to parse module arguments");
 
339
        goto fail;
 
340
    }
 
341
 
 
342
    if ((rm = pa_modargs_get_value(ma, "resample_method", NULL))) {
 
343
        if ((resample_method = pa_parse_resample_method(rm)) < 0) {
 
344
            pa_log("invalid resample method '%s'", rm);
 
345
            goto fail;
 
346
        }
 
347
    }
 
348
    
 
349
    u = pa_xnew(struct userdata, 1);
 
350
    m->userdata = u;
 
351
    u->sink = NULL;
 
352
    u->n_outputs = 0;
 
353
    u->master = NULL;
 
354
    u->module = m;
 
355
    u->core = c;
 
356
    u->time_event = NULL;
 
357
    u->adjust_time = DEFAULT_ADJUST_TIME;
 
358
    PA_LLIST_HEAD_INIT(struct output, u->outputs);
 
359
 
 
360
    if (pa_modargs_get_value_u32(ma, "adjust_time", &u->adjust_time) < 0) {
 
361
        pa_log("failed to parse adjust_time value");
 
362
        goto fail;
 
363
    }
 
364
    
 
365
    if (!(master_name = pa_modargs_get_value(ma, "master", NULL)) || !(slaves = pa_modargs_get_value(ma, "slaves", NULL))) {
 
366
        pa_log("no master or slave sinks specified");
 
367
        goto fail;
 
368
    }
 
369
 
 
370
    if (!(master_sink = pa_namereg_get(c, master_name, PA_NAMEREG_SINK, 1))) {
 
371
        pa_log("invalid master sink '%s'", master_name);
 
372
        goto fail;
 
373
    }
 
374
 
 
375
    ss = master_sink->sample_spec;
 
376
    if ((pa_modargs_get_sample_spec(ma, &ss) < 0)) {
 
377
        pa_log("invalid sample specification.");
 
378
        goto fail;
 
379
    }
 
380
 
 
381
    if (ss.channels == master_sink->sample_spec.channels)
 
382
        map = master_sink->channel_map;
 
383
    else
 
384
        pa_channel_map_init_auto(&map, ss.channels, PA_CHANNEL_MAP_DEFAULT);
 
385
 
 
386
    if ((pa_modargs_get_channel_map(ma, &map) < 0)) {
 
387
        pa_log("invalid channel map.");
 
388
        goto fail;
 
389
    }
 
390
 
 
391
    if (ss.channels != map.channels) {
 
392
        pa_log("channel map and sample specification don't match.");
 
393
        goto fail;
 
394
    }
 
395
    
 
396
    if (!(u->sink = pa_sink_new(c, __FILE__, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME), 0, &ss, &map))) {
 
397
        pa_log("failed to create sink");
 
398
        goto fail;
 
399
    }
 
400
 
 
401
    pa_sink_set_owner(u->sink, m);
 
402
    pa_sink_set_description(u->sink, "Combined Sink");
 
403
    u->sink->get_latency = sink_get_latency_cb;
 
404
    u->sink->notify = sink_notify;
 
405
    u->sink->userdata = u;
 
406
    
 
407
    if (!(u->master = output_new(u, master_sink, resample_method))) {
 
408
        pa_log("failed to create master sink input on sink '%s'.", u->sink->name);
 
409
        goto fail;
 
410
    }
 
411
    
 
412
    split_state = NULL;
 
413
    while ((n = pa_split(slaves, ",", &split_state))) {
 
414
        pa_sink *slave_sink;
 
415
        
 
416
        if (!(slave_sink = pa_namereg_get(c, n, PA_NAMEREG_SINK, 1))) {
 
417
            pa_log("invalid slave sink '%s'", n);
 
418
            goto fail;
 
419
        }
 
420
 
 
421
        pa_xfree(n);
 
422
 
 
423
        if (!output_new(u, slave_sink, resample_method)) {
 
424
            pa_log("failed to create slave sink input on sink '%s'.", slave_sink->name);
 
425
            goto fail;
 
426
        }
 
427
    }
 
428
           
 
429
    if (u->n_outputs <= 1)
 
430
        pa_log_warn("WARNING: no slave sinks specified.");
 
431
 
 
432
    if (u->adjust_time > 0) {
 
433
        pa_gettimeofday(&tv);
 
434
        tv.tv_sec += u->adjust_time;
 
435
        u->time_event = c->mainloop->time_new(c->mainloop, &tv, time_callback, u);
 
436
    }
 
437
    
 
438
    pa_modargs_free(ma);
 
439
    return 0;    
 
440
 
 
441
fail:
 
442
    pa_xfree(n);
 
443
    
 
444
    if (ma)
 
445
        pa_modargs_free(ma);
 
446
 
 
447
    pa__done(c, m);
 
448
    return -1;
 
449
}
 
450
 
 
451
void pa__done(pa_core *c, pa_module*m) {
 
452
    struct userdata *u;
 
453
    assert(c && m);
 
454
 
 
455
    if (!(u = m->userdata))
 
456
        return;
 
457
 
 
458
    clear_up(u);
 
459
    pa_xfree(u);
 
460
}
 
461
 
 
462