• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /***
2     This file is part of PulseAudio.
3 
4     Copyright 2010 Intel Corporation
5     Contributor: Pierre-Louis Bossart <pierre-louis.bossart@intel.com>
6     Copyright 2012 Niels Ole Salscheider <niels_ole@salscheider-online.de>
7 
8     PulseAudio is free software; you can redistribute it and/or modify
9     it under the terms of the GNU Lesser General Public License as published
10     by the Free Software Foundation; either version 2.1 of the License,
11     or (at your option) any later version.
12 
13     PulseAudio is distributed in the hope that it will be useful, but
14     WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16     General Public License for more details.
17 
18     You should have received a copy of the GNU Lesser General Public License
19     along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
20 ***/
21 
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25 
26 #include <pulse/gccmacro.h>
27 #include <pulse/xmalloc.h>
28 
29 #include <pulsecore/i18n.h>
30 #include <pulsecore/namereg.h>
31 #include <pulsecore/sink.h>
32 #include <pulsecore/module.h>
33 #include <pulsecore/core-util.h>
34 #include <pulsecore/modargs.h>
35 #include <pulsecore/log.h>
36 #include <pulsecore/rtpoll.h>
37 #include <pulsecore/sample-util.h>
38 #include <pulsecore/ltdl-helper.h>
39 #include <pulsecore/sound-file.h>
40 #include <pulsecore/resampler.h>
41 
42 #include <math.h>
43 
44 PA_MODULE_AUTHOR("Niels Ole Salscheider");
45 PA_MODULE_DESCRIPTION(_("Virtual surround sink"));
46 PA_MODULE_VERSION(PACKAGE_VERSION);
47 PA_MODULE_LOAD_ONCE(false);
48 PA_MODULE_USAGE(
49         _("sink_name=<name for the sink> "
50           "sink_properties=<properties for the sink> "
51           "master=<name of sink to filter> "
52           "sink_master=<name of sink to filter> "
53           "format=<sample format> "
54           "rate=<sample rate> "
55           "channels=<number of channels> "
56           "channel_map=<channel map> "
57           "use_volume_sharing=<yes or no> "
58           "force_flat_volume=<yes or no> "
59           "hrir=/path/to/left_hrir.wav "
60           "autoloaded=<set if this module is being loaded automatically> "
61         ));
62 
63 #define MEMBLOCKQ_MAXLENGTH (16*1024*1024)
64 #define DEFAULT_AUTOLOADED false
65 
66 struct userdata {
67     pa_module *module;
68 
69     /* FIXME: Uncomment this and take "autoloaded" as a modarg if this is a filter */
70     /* bool autoloaded; */
71 
72     pa_sink *sink;
73     pa_sink_input *sink_input;
74 
75     pa_memblockq *memblockq;
76 
77     bool auto_desc;
78     unsigned channels;
79     unsigned hrir_channels;
80 
81     unsigned fs, sink_fs;
82 
83     unsigned *mapping_left;
84     unsigned *mapping_right;
85 
86     unsigned hrir_samples;
87     float *hrir_data;
88 
89     float *input_buffer;
90     int input_buffer_offset;
91 
92     bool autoloaded;
93 };
94 
95 static const char* const valid_modargs[] = {
96     "sink_name",
97     "sink_properties",
98     "master",  /* Will be deprecated. */
99     "sink_master",
100     "format",
101     "rate",
102     "channels",
103     "channel_map",
104     "use_volume_sharing",
105     "force_flat_volume",
106     "hrir",
107     "autoloaded",
108     NULL
109 };
110 
111 /* Called from I/O thread context */
sink_process_msg_cb(pa_msgobject * o,int code,void * data,int64_t offset,pa_memchunk * chunk)112 static int sink_process_msg_cb(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
113     struct userdata *u = PA_SINK(o)->userdata;
114 
115     switch (code) {
116 
117         case PA_SINK_MESSAGE_GET_LATENCY:
118 
119             /* The sink is _put() before the sink input is, so let's
120              * make sure we don't access it in that time. Also, the
121              * sink input is first shut down, the sink second. */
122             if (!PA_SINK_IS_LINKED(u->sink->thread_info.state) ||
123                 !PA_SINK_INPUT_IS_LINKED(u->sink_input->thread_info.state)) {
124                 *((int64_t*) data) = 0;
125                 return 0;
126             }
127 
128             *((int64_t*) data) =
129 
130                 /* Get the latency of the master sink */
131                 pa_sink_get_latency_within_thread(u->sink_input->sink, true) +
132 
133                 /* Add the latency internal to our sink input on top */
134                 pa_bytes_to_usec(pa_memblockq_get_length(u->sink_input->thread_info.render_memblockq), &u->sink_input->sink->sample_spec);
135 
136             return 0;
137     }
138 
139     return pa_sink_process_msg(o, code, data, offset, chunk);
140 }
141 
142 /* Called from main context */
sink_set_state_in_main_thread_cb(pa_sink * s,pa_sink_state_t state,pa_suspend_cause_t suspend_cause)143 static int sink_set_state_in_main_thread_cb(pa_sink *s, pa_sink_state_t state, pa_suspend_cause_t suspend_cause) {
144     struct userdata *u;
145 
146     pa_sink_assert_ref(s);
147     pa_assert_se(u = s->userdata);
148 
149     if (!PA_SINK_IS_LINKED(state) ||
150         !PA_SINK_INPUT_IS_LINKED(u->sink_input->state))
151         return 0;
152 
153     pa_sink_input_cork(u->sink_input, state == PA_SINK_SUSPENDED);
154     return 0;
155 }
156 
157 /* Called from the IO thread. */
sink_set_state_in_io_thread_cb(pa_sink * s,pa_sink_state_t new_state,pa_suspend_cause_t new_suspend_cause)158 static int sink_set_state_in_io_thread_cb(pa_sink *s, pa_sink_state_t new_state, pa_suspend_cause_t new_suspend_cause) {
159     struct userdata *u;
160 
161     pa_assert(s);
162     pa_assert_se(u = s->userdata);
163 
164     /* When set to running or idle for the first time, request a rewind
165      * of the master sink to make sure we are heard immediately */
166     if (PA_SINK_IS_OPENED(new_state) && s->thread_info.state == PA_SINK_INIT) {
167         pa_log_debug("Requesting rewind due to state change.");
168         pa_sink_input_request_rewind(u->sink_input, 0, false, true, true);
169     }
170 
171     return 0;
172 }
173 
174 /* Called from I/O thread context */
sink_request_rewind_cb(pa_sink * s)175 static void sink_request_rewind_cb(pa_sink *s) {
176     struct userdata *u;
177 
178     pa_sink_assert_ref(s);
179     pa_assert_se(u = s->userdata);
180 
181     if (!PA_SINK_IS_LINKED(u->sink->thread_info.state) ||
182         !PA_SINK_INPUT_IS_LINKED(u->sink_input->thread_info.state))
183         return;
184 
185     /* Just hand this one over to the master sink */
186     pa_sink_input_request_rewind(u->sink_input,
187                                  s->thread_info.rewind_nbytes +
188                                  pa_memblockq_get_length(u->memblockq), true, false, false);
189 }
190 
191 /* Called from I/O thread context */
sink_update_requested_latency_cb(pa_sink * s)192 static void sink_update_requested_latency_cb(pa_sink *s) {
193     struct userdata *u;
194 
195     pa_sink_assert_ref(s);
196     pa_assert_se(u = s->userdata);
197 
198     if (!PA_SINK_IS_LINKED(u->sink->thread_info.state) ||
199         !PA_SINK_INPUT_IS_LINKED(u->sink_input->thread_info.state))
200         return;
201 
202     /* Just hand this one over to the master sink */
203     pa_sink_input_set_requested_latency_within_thread(
204             u->sink_input,
205             pa_sink_get_requested_latency_within_thread(s));
206 }
207 
208 /* Called from main context */
sink_set_volume_cb(pa_sink * s)209 static void sink_set_volume_cb(pa_sink *s) {
210     struct userdata *u;
211 
212     pa_sink_assert_ref(s);
213     pa_assert_se(u = s->userdata);
214 
215     if (!PA_SINK_IS_LINKED(s->state) ||
216         !PA_SINK_INPUT_IS_LINKED(u->sink_input->state))
217         return;
218 
219     pa_sink_input_set_volume(u->sink_input, &s->real_volume, s->save_volume, true);
220 }
221 
222 /* Called from main context */
sink_set_mute_cb(pa_sink * s)223 static void sink_set_mute_cb(pa_sink *s) {
224     struct userdata *u;
225 
226     pa_sink_assert_ref(s);
227     pa_assert_se(u = s->userdata);
228 
229     if (!PA_SINK_IS_LINKED(s->state) ||
230         !PA_SINK_INPUT_IS_LINKED(u->sink_input->state))
231         return;
232 
233     pa_sink_input_set_mute(u->sink_input, s->muted, s->save_muted);
234 }
235 
236 /* Called from I/O thread context */
sink_input_pop_cb(pa_sink_input * i,size_t nbytes,pa_memchunk * chunk)237 static int sink_input_pop_cb(pa_sink_input *i, size_t nbytes, pa_memchunk *chunk) {
238     struct userdata *u;
239     float *src, *dst;
240     unsigned n;
241     pa_memchunk tchunk;
242 
243     unsigned j, k, l;
244     float sum_right, sum_left;
245     float current_sample;
246 
247     pa_sink_input_assert_ref(i);
248     pa_assert(chunk);
249     pa_assert_se(u = i->userdata);
250 
251     if (!PA_SINK_IS_LINKED(u->sink->thread_info.state))
252         return -1;
253 
254     /* Hmm, process any rewind request that might be queued up */
255     pa_sink_process_rewind(u->sink, 0);
256 
257     while (pa_memblockq_peek(u->memblockq, &tchunk) < 0) {
258         pa_memchunk nchunk;
259 
260         pa_sink_render(u->sink, nbytes * u->sink_fs / u->fs, &nchunk);
261         pa_memblockq_push(u->memblockq, &nchunk);
262         pa_memblock_unref(nchunk.memblock);
263     }
264 
265     tchunk.length = PA_MIN(nbytes * u->sink_fs / u->fs, tchunk.length);
266     pa_assert(tchunk.length > 0);
267 
268     n = (unsigned) (tchunk.length / u->sink_fs);
269 
270     pa_assert(n > 0);
271 
272     chunk->index = 0;
273     chunk->length = n * u->fs;
274     chunk->memblock = pa_memblock_new(i->sink->core->mempool, chunk->length);
275 
276     pa_memblockq_drop(u->memblockq, n * u->sink_fs);
277 
278     src = pa_memblock_acquire_chunk(&tchunk);
279     dst = pa_memblock_acquire(chunk->memblock);
280 
281     for (l = 0; l < n; l++) {
282         memcpy(((char*) u->input_buffer) + u->input_buffer_offset * u->sink_fs, ((char *) src) + l * u->sink_fs, u->sink_fs);
283 
284         sum_right = 0;
285         sum_left = 0;
286 
287         /* fold the input buffer with the impulse response */
288         for (j = 0; j < u->hrir_samples; j++) {
289             for (k = 0; k < u->channels; k++) {
290                 current_sample = u->input_buffer[((u->input_buffer_offset + j) % u->hrir_samples) * u->channels + k];
291 
292                 sum_left += current_sample * u->hrir_data[j * u->hrir_channels + u->mapping_left[k]];
293                 sum_right += current_sample * u->hrir_data[j * u->hrir_channels + u->mapping_right[k]];
294             }
295         }
296 
297         dst[2 * l] = PA_CLAMP_UNLIKELY(sum_left, -1.0f, 1.0f);
298         dst[2 * l + 1] = PA_CLAMP_UNLIKELY(sum_right, -1.0f, 1.0f);
299 
300         u->input_buffer_offset--;
301         if (u->input_buffer_offset < 0)
302             u->input_buffer_offset += u->hrir_samples;
303     }
304 
305     pa_memblock_release(tchunk.memblock);
306     pa_memblock_release(chunk->memblock);
307 
308     pa_memblock_unref(tchunk.memblock);
309 
310     return 0;
311 }
312 
313 /* Called from I/O thread context */
sink_input_process_rewind_cb(pa_sink_input * i,size_t nbytes)314 static void sink_input_process_rewind_cb(pa_sink_input *i, size_t nbytes) {
315     struct userdata *u;
316     size_t amount = 0;
317 
318     pa_sink_input_assert_ref(i);
319     pa_assert_se(u = i->userdata);
320 
321     /* If the sink is not yet linked, there is nothing to rewind */
322     if (!PA_SINK_IS_LINKED(u->sink->thread_info.state))
323         return;
324 
325     if (u->sink->thread_info.rewind_nbytes > 0) {
326         size_t max_rewrite;
327 
328         max_rewrite = nbytes * u->sink_fs / u->fs + pa_memblockq_get_length(u->memblockq);
329         amount = PA_MIN(u->sink->thread_info.rewind_nbytes * u->sink_fs / u->fs, max_rewrite);
330         u->sink->thread_info.rewind_nbytes = 0;
331 
332         if (amount > 0) {
333             pa_memblockq_seek(u->memblockq, - (int64_t) amount, PA_SEEK_RELATIVE, true);
334 
335             /* Reset the input buffer */
336             memset(u->input_buffer, 0, u->hrir_samples * u->sink_fs);
337             u->input_buffer_offset = 0;
338         }
339     }
340 
341     pa_sink_process_rewind(u->sink, amount);
342     pa_memblockq_rewind(u->memblockq, nbytes * u->sink_fs / u->fs);
343 }
344 
345 /* Called from I/O thread context */
sink_input_update_max_rewind_cb(pa_sink_input * i,size_t nbytes)346 static void sink_input_update_max_rewind_cb(pa_sink_input *i, size_t nbytes) {
347     struct userdata *u;
348 
349     pa_sink_input_assert_ref(i);
350     pa_assert_se(u = i->userdata);
351 
352     /* FIXME: Too small max_rewind:
353      * https://bugs.freedesktop.org/show_bug.cgi?id=53709 */
354     pa_memblockq_set_maxrewind(u->memblockq, nbytes * u->sink_fs / u->fs);
355     pa_sink_set_max_rewind_within_thread(u->sink, nbytes * u->sink_fs / u->fs);
356 }
357 
358 /* Called from I/O thread context */
sink_input_update_max_request_cb(pa_sink_input * i,size_t nbytes)359 static void sink_input_update_max_request_cb(pa_sink_input *i, size_t nbytes) {
360     struct userdata *u;
361 
362     pa_sink_input_assert_ref(i);
363     pa_assert_se(u = i->userdata);
364 
365     pa_sink_set_max_request_within_thread(u->sink, nbytes * u->sink_fs / u->fs);
366 }
367 
368 /* Called from I/O thread context */
sink_input_update_sink_latency_range_cb(pa_sink_input * i)369 static void sink_input_update_sink_latency_range_cb(pa_sink_input *i) {
370     struct userdata *u;
371 
372     pa_sink_input_assert_ref(i);
373     pa_assert_se(u = i->userdata);
374 
375     pa_sink_set_latency_range_within_thread(u->sink, i->sink->thread_info.min_latency, i->sink->thread_info.max_latency);
376 }
377 
378 /* Called from I/O thread context */
sink_input_update_sink_fixed_latency_cb(pa_sink_input * i)379 static void sink_input_update_sink_fixed_latency_cb(pa_sink_input *i) {
380     struct userdata *u;
381 
382     pa_sink_input_assert_ref(i);
383     pa_assert_se(u = i->userdata);
384 
385     pa_sink_set_fixed_latency_within_thread(u->sink, i->sink->thread_info.fixed_latency);
386 }
387 
388 /* Called from I/O thread context */
sink_input_detach_cb(pa_sink_input * i)389 static void sink_input_detach_cb(pa_sink_input *i) {
390     struct userdata *u;
391 
392     pa_sink_input_assert_ref(i);
393     pa_assert_se(u = i->userdata);
394 
395     if (PA_SINK_IS_LINKED(u->sink->thread_info.state))
396         pa_sink_detach_within_thread(u->sink);
397 
398     pa_sink_set_rtpoll(u->sink, NULL);
399 }
400 
401 /* Called from I/O thread context */
sink_input_attach_cb(pa_sink_input * i)402 static void sink_input_attach_cb(pa_sink_input *i) {
403     struct userdata *u;
404 
405     pa_sink_input_assert_ref(i);
406     pa_assert_se(u = i->userdata);
407 
408     pa_sink_set_rtpoll(u->sink, i->sink->thread_info.rtpoll);
409     pa_sink_set_latency_range_within_thread(u->sink, i->sink->thread_info.min_latency, i->sink->thread_info.max_latency);
410 
411     pa_sink_set_fixed_latency_within_thread(u->sink, i->sink->thread_info.fixed_latency);
412 
413     pa_sink_set_max_request_within_thread(u->sink, pa_sink_input_get_max_request(i) * u->sink_fs / u->fs);
414 
415     /* FIXME: Too small max_rewind:
416      * https://bugs.freedesktop.org/show_bug.cgi?id=53709 */
417     pa_sink_set_max_rewind_within_thread(u->sink, pa_sink_input_get_max_rewind(i) * u->sink_fs / u->fs);
418 
419     if (PA_SINK_IS_LINKED(u->sink->thread_info.state))
420         pa_sink_attach_within_thread(u->sink);
421 }
422 
423 /* Called from main context */
sink_input_kill_cb(pa_sink_input * i)424 static void sink_input_kill_cb(pa_sink_input *i) {
425     struct userdata *u;
426 
427     pa_sink_input_assert_ref(i);
428     pa_assert_se(u = i->userdata);
429 
430     /* The order here matters! We first kill the sink so that streams
431      * can properly be moved away while the sink input is still connected
432      * to the master. */
433     pa_sink_input_cork(u->sink_input, true);
434     pa_sink_unlink(u->sink);
435     pa_sink_input_unlink(u->sink_input);
436 
437     pa_sink_input_unref(u->sink_input);
438     u->sink_input = NULL;
439 
440     pa_sink_unref(u->sink);
441     u->sink = NULL;
442 
443     pa_module_unload_request(u->module, true);
444 }
445 
446 /* Called from main context */
sink_input_may_move_to_cb(pa_sink_input * i,pa_sink * dest)447 static bool sink_input_may_move_to_cb(pa_sink_input *i, pa_sink *dest) {
448     struct userdata *u;
449 
450     pa_sink_input_assert_ref(i);
451     pa_assert_se(u = i->userdata);
452 
453     if (u->autoloaded)
454         return false;
455 
456     return u->sink != dest;
457 }
458 
459 /* Called from main context */
sink_input_moving_cb(pa_sink_input * i,pa_sink * dest)460 static void sink_input_moving_cb(pa_sink_input *i, pa_sink *dest) {
461     struct userdata *u;
462 
463     pa_sink_input_assert_ref(i);
464     pa_assert_se(u = i->userdata);
465 
466     if (dest) {
467         pa_sink_set_asyncmsgq(u->sink, dest->asyncmsgq);
468         pa_sink_update_flags(u->sink, PA_SINK_LATENCY|PA_SINK_DYNAMIC_LATENCY, dest->flags);
469     } else
470         pa_sink_set_asyncmsgq(u->sink, NULL);
471 
472     if (u->auto_desc && dest) {
473         const char *z;
474         pa_proplist *pl;
475 
476         pl = pa_proplist_new();
477         z = pa_proplist_gets(dest->proplist, PA_PROP_DEVICE_DESCRIPTION);
478         pa_proplist_setf(pl, PA_PROP_DEVICE_DESCRIPTION, "Virtual Surround Sink %s on %s",
479                          pa_proplist_gets(u->sink->proplist, "device.vsurroundsink.name"), z ? z : dest->name);
480 
481         pa_sink_update_proplist(u->sink, PA_UPDATE_REPLACE, pl);
482         pa_proplist_free(pl);
483     }
484 }
485 
486 /* Called from main context */
sink_input_volume_changed_cb(pa_sink_input * i)487 static void sink_input_volume_changed_cb(pa_sink_input *i) {
488     struct userdata *u;
489 
490     pa_sink_input_assert_ref(i);
491     pa_assert_se(u = i->userdata);
492 
493     pa_sink_volume_changed(u->sink, &i->volume);
494 }
495 
496 /* Called from main context */
sink_input_mute_changed_cb(pa_sink_input * i)497 static void sink_input_mute_changed_cb(pa_sink_input *i) {
498     struct userdata *u;
499 
500     pa_sink_input_assert_ref(i);
501     pa_assert_se(u = i->userdata);
502 
503     pa_sink_mute_changed(u->sink, i->muted);
504 }
505 
mirror_channel(pa_channel_position_t channel)506 static pa_channel_position_t mirror_channel(pa_channel_position_t channel) {
507     switch (channel) {
508         case PA_CHANNEL_POSITION_FRONT_LEFT:
509             return PA_CHANNEL_POSITION_FRONT_RIGHT;
510 
511         case PA_CHANNEL_POSITION_FRONT_RIGHT:
512             return PA_CHANNEL_POSITION_FRONT_LEFT;
513 
514         case PA_CHANNEL_POSITION_REAR_LEFT:
515             return PA_CHANNEL_POSITION_REAR_RIGHT;
516 
517         case PA_CHANNEL_POSITION_REAR_RIGHT:
518             return PA_CHANNEL_POSITION_REAR_LEFT;
519 
520         case PA_CHANNEL_POSITION_SIDE_LEFT:
521             return PA_CHANNEL_POSITION_SIDE_RIGHT;
522 
523         case PA_CHANNEL_POSITION_SIDE_RIGHT:
524             return PA_CHANNEL_POSITION_SIDE_LEFT;
525 
526         case PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER:
527             return PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER;
528 
529         case PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER:
530             return PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER;
531 
532         case PA_CHANNEL_POSITION_TOP_FRONT_LEFT:
533             return PA_CHANNEL_POSITION_TOP_FRONT_RIGHT;
534 
535         case PA_CHANNEL_POSITION_TOP_FRONT_RIGHT:
536             return PA_CHANNEL_POSITION_TOP_FRONT_LEFT;
537 
538         case PA_CHANNEL_POSITION_TOP_REAR_LEFT:
539             return PA_CHANNEL_POSITION_TOP_REAR_RIGHT;
540 
541         case PA_CHANNEL_POSITION_TOP_REAR_RIGHT:
542             return PA_CHANNEL_POSITION_TOP_REAR_LEFT;
543 
544         default:
545             return channel;
546     }
547 }
548 
normalize_hrir(struct userdata * u)549 static void normalize_hrir(struct userdata *u) {
550     /* normalize hrir to avoid audible clipping
551      *
552      * The following heuristic tries to avoid audible clipping. It cannot avoid
553      * clipping in the worst case though, because the scaling factor would
554      * become too large resulting in a too quiet signal.
555      * The idea of the heuristic is to avoid clipping when a single click is
556      * played back on all channels. The scaling factor describes the additional
557      * factor that is necessary to avoid clipping for "normal" signals.
558      *
559      * This algorithm doesn't pretend to be perfect, it's just something that
560      * appears to work (not too quiet, no audible clipping) on the material that
561      * it has been tested on. If you find a real-world example where this
562      * algorithm results in audible clipping, please write a patch that adjusts
563      * the scaling factor constants or improves the algorithm (or if you can't
564      * write a patch, at least report the problem to the PulseAudio mailing list
565      * or bug tracker). */
566 
567     const float scaling_factor = 2.5;
568 
569     float hrir_sum, hrir_max;
570     unsigned i, j;
571 
572     hrir_max = 0;
573     for (i = 0; i < u->hrir_samples; i++) {
574         hrir_sum = 0;
575         for (j = 0; j < u->hrir_channels; j++)
576             hrir_sum += fabs(u->hrir_data[i * u->hrir_channels + j]);
577 
578         if (hrir_sum > hrir_max)
579             hrir_max = hrir_sum;
580     }
581 
582     for (i = 0; i < u->hrir_samples; i++) {
583         for (j = 0; j < u->hrir_channels; j++)
584             u->hrir_data[i * u->hrir_channels + j] /= hrir_max * scaling_factor;
585     }
586 }
587 
pa__init(pa_module * m)588 int pa__init(pa_module*m) {
589     struct userdata *u;
590     pa_sample_spec ss, sink_input_ss;
591     pa_channel_map map, sink_input_map;
592     pa_modargs *ma;
593     const char *master_name;
594     pa_sink *master = NULL;
595     pa_sink_input_new_data sink_input_data;
596     pa_sink_new_data sink_data;
597     bool use_volume_sharing = true;
598     bool force_flat_volume = false;
599     pa_memchunk silence;
600 
601     const char *hrir_file;
602     unsigned i, j, found_channel_left, found_channel_right;
603     float *hrir_data;
604 
605     pa_sample_spec hrir_ss;
606     pa_channel_map hrir_map;
607 
608     pa_sample_spec hrir_temp_ss;
609     pa_memchunk hrir_temp_chunk, hrir_temp_chunk_resampled;
610     pa_resampler *resampler;
611 
612     size_t hrir_copied_length, hrir_total_length;
613 
614     hrir_temp_chunk.memblock = NULL;
615     hrir_temp_chunk_resampled.memblock = NULL;
616 
617     pa_assert(m);
618 
619     if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
620         pa_log("Failed to parse module arguments.");
621         goto fail;
622     }
623 
624     master_name = pa_modargs_get_value(ma, "sink_master", NULL);
625     if (!master_name) {
626         master_name = pa_modargs_get_value(ma, "master", NULL);
627         if (master_name)
628             pa_log_warn("The 'master' module argument is deprecated and may be removed in the future, "
629                         "please use the 'sink_master' argument instead.");
630     }
631 
632     master = pa_namereg_get(m->core, master_name, PA_NAMEREG_SINK);
633     if (!master) {
634         pa_log("Master sink not found.");
635         goto fail;
636     }
637 
638     pa_assert(master);
639 
640     u = pa_xnew0(struct userdata, 1);
641     u->module = m;
642     m->userdata = u;
643 
644     /* Initialize hrir and input buffer */
645     /* this is the hrir file for the left ear! */
646     if (!(hrir_file = pa_modargs_get_value(ma, "hrir", NULL))) {
647         pa_log("The mandatory 'hrir' module argument is missing.");
648         goto fail;
649     }
650 
651     if (pa_sound_file_load(master->core->mempool, hrir_file, &hrir_temp_ss, &hrir_map, &hrir_temp_chunk, NULL) < 0) {
652         pa_log("Cannot load hrir file.");
653         goto fail;
654     }
655 
656     /* sample spec / map of hrir */
657     hrir_ss.format = PA_SAMPLE_FLOAT32;
658     hrir_ss.rate = master->sample_spec.rate;
659     hrir_ss.channels = hrir_temp_ss.channels;
660 
661     /* sample spec of sink */
662     ss = hrir_ss;
663     map = hrir_map;
664     if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_DEFAULT) < 0) {
665         pa_log("Invalid sample format specification or channel map");
666         goto fail;
667     }
668     ss.format = PA_SAMPLE_FLOAT32;
669     hrir_ss.rate = ss.rate;
670     u->channels = ss.channels;
671 
672     if (pa_modargs_get_value_boolean(ma, "use_volume_sharing", &use_volume_sharing) < 0) {
673         pa_log("use_volume_sharing= expects a boolean argument");
674         goto fail;
675     }
676 
677     if (pa_modargs_get_value_boolean(ma, "force_flat_volume", &force_flat_volume) < 0) {
678         pa_log("force_flat_volume= expects a boolean argument");
679         goto fail;
680     }
681 
682     if (use_volume_sharing && force_flat_volume) {
683         pa_log("Flat volume can't be forced when using volume sharing.");
684         goto fail;
685     }
686 
687     /* sample spec / map of sink input */
688     pa_channel_map_init_stereo(&sink_input_map);
689     sink_input_ss.channels = 2;
690     sink_input_ss.format = PA_SAMPLE_FLOAT32;
691     sink_input_ss.rate = ss.rate;
692 
693     u->sink_fs = pa_frame_size(&ss);
694     u->fs = pa_frame_size(&sink_input_ss);
695 
696     /* Create sink */
697     pa_sink_new_data_init(&sink_data);
698     sink_data.driver = __FILE__;
699     sink_data.module = m;
700     if (!(sink_data.name = pa_xstrdup(pa_modargs_get_value(ma, "sink_name", NULL))))
701         sink_data.name = pa_sprintf_malloc("%s.vsurroundsink", master->name);
702     pa_sink_new_data_set_sample_spec(&sink_data, &ss);
703     pa_sink_new_data_set_channel_map(&sink_data, &map);
704     pa_proplist_sets(sink_data.proplist, PA_PROP_DEVICE_MASTER_DEVICE, master->name);
705     pa_proplist_sets(sink_data.proplist, PA_PROP_DEVICE_CLASS, "filter");
706     pa_proplist_sets(sink_data.proplist, "device.vsurroundsink.name", sink_data.name);
707 
708     if (pa_modargs_get_proplist(ma, "sink_properties", sink_data.proplist, PA_UPDATE_REPLACE) < 0) {
709         pa_log("Invalid properties");
710         pa_sink_new_data_done(&sink_data);
711         goto fail;
712     }
713 
714     u->autoloaded = DEFAULT_AUTOLOADED;
715     if (pa_modargs_get_value_boolean(ma, "autoloaded", &u->autoloaded) < 0) {
716         pa_log("Failed to parse autoloaded value");
717         goto fail;
718     }
719 
720     if ((u->auto_desc = !pa_proplist_contains(sink_data.proplist, PA_PROP_DEVICE_DESCRIPTION))) {
721         const char *z;
722 
723         z = pa_proplist_gets(master->proplist, PA_PROP_DEVICE_DESCRIPTION);
724         pa_proplist_setf(sink_data.proplist, PA_PROP_DEVICE_DESCRIPTION, "Virtual Surround Sink %s on %s", sink_data.name, z ? z : master->name);
725     }
726 
727     u->sink = pa_sink_new(m->core, &sink_data, (master->flags & (PA_SINK_LATENCY|PA_SINK_DYNAMIC_LATENCY))
728                                                | (use_volume_sharing ? PA_SINK_SHARE_VOLUME_WITH_MASTER : 0));
729     pa_sink_new_data_done(&sink_data);
730 
731     if (!u->sink) {
732         pa_log("Failed to create sink.");
733         goto fail;
734     }
735 
736     u->sink->parent.process_msg = sink_process_msg_cb;
737     u->sink->set_state_in_main_thread = sink_set_state_in_main_thread_cb;
738     u->sink->set_state_in_io_thread = sink_set_state_in_io_thread_cb;
739     u->sink->update_requested_latency = sink_update_requested_latency_cb;
740     u->sink->request_rewind = sink_request_rewind_cb;
741     pa_sink_set_set_mute_callback(u->sink, sink_set_mute_cb);
742     if (!use_volume_sharing) {
743         pa_sink_set_set_volume_callback(u->sink, sink_set_volume_cb);
744         pa_sink_enable_decibel_volume(u->sink, true);
745     }
746     /* Normally this flag would be enabled automatically be we can force it. */
747     if (force_flat_volume)
748         u->sink->flags |= PA_SINK_FLAT_VOLUME;
749     u->sink->userdata = u;
750 
751     pa_sink_set_asyncmsgq(u->sink, master->asyncmsgq);
752 
753     /* Create sink input */
754     pa_sink_input_new_data_init(&sink_input_data);
755     sink_input_data.driver = __FILE__;
756     sink_input_data.module = m;
757     pa_sink_input_new_data_set_sink(&sink_input_data, master, false, true);
758     sink_input_data.origin_sink = u->sink;
759     pa_proplist_setf(sink_input_data.proplist, PA_PROP_MEDIA_NAME, "Virtual Surround Sink Stream from %s", pa_proplist_gets(u->sink->proplist, PA_PROP_DEVICE_DESCRIPTION));
760     pa_proplist_sets(sink_input_data.proplist, PA_PROP_MEDIA_ROLE, "filter");
761     pa_sink_input_new_data_set_sample_spec(&sink_input_data, &sink_input_ss);
762     pa_sink_input_new_data_set_channel_map(&sink_input_data, &sink_input_map);
763     sink_input_data.flags |= PA_SINK_INPUT_START_CORKED;
764 
765     pa_sink_input_new(&u->sink_input, m->core, &sink_input_data);
766     pa_sink_input_new_data_done(&sink_input_data);
767 
768     if (!u->sink_input)
769         goto fail;
770 
771     u->sink_input->pop = sink_input_pop_cb;
772     u->sink_input->process_rewind = sink_input_process_rewind_cb;
773     u->sink_input->update_max_rewind = sink_input_update_max_rewind_cb;
774     u->sink_input->update_max_request = sink_input_update_max_request_cb;
775     u->sink_input->update_sink_latency_range = sink_input_update_sink_latency_range_cb;
776     u->sink_input->update_sink_fixed_latency = sink_input_update_sink_fixed_latency_cb;
777     u->sink_input->kill = sink_input_kill_cb;
778     u->sink_input->attach = sink_input_attach_cb;
779     u->sink_input->detach = sink_input_detach_cb;
780     u->sink_input->may_move_to = sink_input_may_move_to_cb;
781     u->sink_input->moving = sink_input_moving_cb;
782     u->sink_input->volume_changed = use_volume_sharing ? NULL : sink_input_volume_changed_cb;
783     u->sink_input->mute_changed = sink_input_mute_changed_cb;
784     u->sink_input->userdata = u;
785 
786     u->sink->input_to_master = u->sink_input;
787 
788     pa_sink_input_get_silence(u->sink_input, &silence);
789     u->memblockq = pa_memblockq_new("module-virtual-surround-sink memblockq", 0, MEMBLOCKQ_MAXLENGTH, 0, &ss, 1, 1, 0, &silence);
790     pa_memblock_unref(silence.memblock);
791 
792     /* resample hrir */
793     resampler = pa_resampler_new(u->sink->core->mempool, &hrir_temp_ss, &hrir_map, &hrir_ss, &hrir_map, u->sink->core->lfe_crossover_freq,
794                                  PA_RESAMPLER_SRC_SINC_BEST_QUALITY, PA_RESAMPLER_NO_REMAP);
795 
796     u->hrir_samples = hrir_temp_chunk.length / pa_frame_size(&hrir_temp_ss) * hrir_ss.rate / hrir_temp_ss.rate;
797     if (u->hrir_samples > 64) {
798         u->hrir_samples = 64;
799         pa_log("The (resampled) hrir contains more than 64 samples. Only the first 64 samples will be used to limit processor usage.");
800     }
801 
802     hrir_total_length = u->hrir_samples * pa_frame_size(&hrir_ss);
803     u->hrir_channels = hrir_ss.channels;
804 
805     u->hrir_data = (float *) pa_xmalloc(hrir_total_length);
806     hrir_copied_length = 0;
807 
808     /* add silence to the hrir until we get enough samples out of the resampler */
809     while (hrir_copied_length < hrir_total_length) {
810         pa_resampler_run(resampler, &hrir_temp_chunk, &hrir_temp_chunk_resampled);
811         if (hrir_temp_chunk.memblock != hrir_temp_chunk_resampled.memblock) {
812             /* Silence input block */
813             pa_silence_memblock(hrir_temp_chunk.memblock, &hrir_temp_ss);
814         }
815 
816         if (hrir_temp_chunk_resampled.memblock) {
817             /* Copy hrir data */
818             hrir_data = (float *) pa_memblock_acquire(hrir_temp_chunk_resampled.memblock);
819 
820             if (hrir_total_length - hrir_copied_length >= hrir_temp_chunk_resampled.length) {
821                 memcpy(u->hrir_data + hrir_copied_length, hrir_data, hrir_temp_chunk_resampled.length);
822                 hrir_copied_length += hrir_temp_chunk_resampled.length;
823             } else {
824                 memcpy(u->hrir_data + hrir_copied_length, hrir_data, hrir_total_length - hrir_copied_length);
825                 hrir_copied_length = hrir_total_length;
826             }
827 
828             pa_memblock_release(hrir_temp_chunk_resampled.memblock);
829             pa_memblock_unref(hrir_temp_chunk_resampled.memblock);
830             hrir_temp_chunk_resampled.memblock = NULL;
831         }
832     }
833 
834     pa_resampler_free(resampler);
835 
836     pa_memblock_unref(hrir_temp_chunk.memblock);
837     hrir_temp_chunk.memblock = NULL;
838 
839     if (hrir_map.channels < map.channels) {
840         pa_log("hrir file does not have enough channels!");
841         goto fail;
842     }
843 
844     normalize_hrir(u);
845 
846     /* create mapping between hrir and input */
847     u->mapping_left = (unsigned *) pa_xnew0(unsigned, u->channels);
848     u->mapping_right = (unsigned *) pa_xnew0(unsigned, u->channels);
849     for (i = 0; i < map.channels; i++) {
850         found_channel_left = 0;
851         found_channel_right = 0;
852 
853         for (j = 0; j < hrir_map.channels; j++) {
854             if (hrir_map.map[j] == map.map[i]) {
855                 u->mapping_left[i] = j;
856                 found_channel_left = 1;
857             }
858 
859             if (hrir_map.map[j] == mirror_channel(map.map[i])) {
860                 u->mapping_right[i] = j;
861                 found_channel_right = 1;
862             }
863         }
864 
865         if (!found_channel_left) {
866             pa_log("Cannot find mapping for channel %s", pa_channel_position_to_string(map.map[i]));
867             goto fail;
868         }
869         if (!found_channel_right) {
870             pa_log("Cannot find mapping for channel %s", pa_channel_position_to_string(mirror_channel(map.map[i])));
871             goto fail;
872         }
873     }
874 
875     u->input_buffer = pa_xmalloc0(u->hrir_samples * u->sink_fs);
876     u->input_buffer_offset = 0;
877 
878     /* The order here is important. The input must be put first,
879      * otherwise streams might attach to the sink before the sink
880      * input is attached to the master. */
881     pa_sink_input_put(u->sink_input);
882     pa_sink_put(u->sink);
883     pa_sink_input_cork(u->sink_input, false);
884 
885     pa_modargs_free(ma);
886     return 0;
887 
888 fail:
889     if (hrir_temp_chunk.memblock)
890         pa_memblock_unref(hrir_temp_chunk.memblock);
891 
892     if (hrir_temp_chunk_resampled.memblock)
893         pa_memblock_unref(hrir_temp_chunk_resampled.memblock);
894 
895     if (ma)
896         pa_modargs_free(ma);
897 
898     pa__done(m);
899 
900     return -1;
901 }
902 
pa__get_n_used(pa_module * m)903 int pa__get_n_used(pa_module *m) {
904     struct userdata *u;
905 
906     pa_assert(m);
907     pa_assert_se(u = m->userdata);
908 
909     return pa_sink_linked_by(u->sink);
910 }
911 
pa__done(pa_module * m)912 void pa__done(pa_module*m) {
913     struct userdata *u;
914 
915     pa_assert(m);
916 
917     if (!(u = m->userdata))
918         return;
919 
920     /* See comments in sink_input_kill_cb() above regarding
921      * destruction order! */
922 
923     if (u->sink_input)
924         pa_sink_input_cork(u->sink_input, true);
925 
926     if (u->sink)
927         pa_sink_unlink(u->sink);
928 
929     if (u->sink_input) {
930         pa_sink_input_unlink(u->sink_input);
931         pa_sink_input_unref(u->sink_input);
932     }
933 
934     if (u->sink)
935         pa_sink_unref(u->sink);
936 
937     if (u->memblockq)
938         pa_memblockq_free(u->memblockq);
939 
940     if (u->hrir_data)
941         pa_xfree(u->hrir_data);
942 
943     if (u->input_buffer)
944         pa_xfree(u->input_buffer);
945 
946     if (u->mapping_left)
947         pa_xfree(u->mapping_left);
948     if (u->mapping_right)
949         pa_xfree(u->mapping_right);
950 
951     pa_xfree(u);
952 }
953