merge glitch-free branch back into trunk
[profile/ivi/pulseaudio.git] / src / modules / module-alsa-source.c
1 /* $Id$ */
2
3 /***
4   This file is part of PulseAudio.
5
6   Copyright 2004-2008 Lennart Poettering
7   Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
8
9   PulseAudio is free software; you can redistribute it and/or modify
10   it under the terms of the GNU Lesser General Public License as published
11   by the Free Software Foundation; either version 2 of the License,
12   or (at your option) any later version.
13
14   PulseAudio is distributed in the hope that it will be useful, but
15   WITHOUT ANY WARRANTY; without even the implied warranty of
16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17   General Public License for more details.
18
19   You should have received a copy of the GNU Lesser General Public License
20   along with PulseAudio; if not, write to the Free Software
21   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22   USA.
23 ***/
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
29 #include <stdio.h>
30
31 #include <asoundlib.h>
32
33 #include <pulse/xmalloc.h>
34 #include <pulse/util.h>
35 #include <pulse/timeval.h>
36
37 #include <pulsecore/core-error.h>
38 #include <pulsecore/core.h>
39 #include <pulsecore/module.h>
40 #include <pulsecore/memchunk.h>
41 #include <pulsecore/sink.h>
42 #include <pulsecore/modargs.h>
43 #include <pulsecore/core-util.h>
44 #include <pulsecore/sample-util.h>
45 #include <pulsecore/log.h>
46 #include <pulsecore/macro.h>
47 #include <pulsecore/thread.h>
48 #include <pulsecore/core-error.h>
49 #include <pulsecore/thread-mq.h>
50 #include <pulsecore/rtpoll.h>
51 #include <pulsecore/time-smoother.h>
52 #include <pulsecore/rtclock.h>
53
54 #include "alsa-util.h"
55 #include "module-alsa-source-symdef.h"
56
57 PA_MODULE_AUTHOR("Lennart Poettering");
58 PA_MODULE_DESCRIPTION("ALSA Source");
59 PA_MODULE_VERSION(PACKAGE_VERSION);
60 PA_MODULE_LOAD_ONCE(FALSE);
61 PA_MODULE_USAGE(
62         "source_name=<name for the source> "
63         "device=<ALSA device> "
64         "device_id=<ALSA card index> "
65         "format=<sample format> "
66         "rate=<sample rate> "
67         "channels=<number of channels> "
68         "channel_map=<channel map> "
69         "fragments=<number of fragments> "
70         "fragment_size=<fragment size> "
71         "mmap=<enable memory mapping?> "
72         "tsched=<enable system timer based scheduling mode?> "
73         "tsched_buffer_size=<buffer size when using timer based scheduling> "
74         "tsched_buffer_watermark=<upper fill watermark> "
75         "mixer_reset=<reset hw volume and mute settings to sane defaults when falling back to software?>");
76
77 static const char* const valid_modargs[] = {
78     "source_name",
79     "device",
80     "device_id",
81     "format",
82     "rate",
83     "channels",
84     "channel_map",
85     "fragments",
86     "fragment_size",
87     "mmap",
88     "tsched",
89     "tsched_buffer_size",
90     "tsched_buffer_watermark",
91     "mixer_reset",
92     NULL
93 };
94
95 #define DEFAULT_DEVICE "default"
96 #define DEFAULT_TSCHED_BUFFER_USEC (2*PA_USEC_PER_SEC)       /* 2s */
97 #define DEFAULT_TSCHED_WATERMARK_USEC (20*PA_USEC_PER_MSEC)  /* 20ms */
98 #define TSCHED_MIN_SLEEP_USEC (3*PA_USEC_PER_MSEC)           /* 3ms */
99 #define TSCHED_MIN_WAKEUP_USEC (3*PA_USEC_PER_MSEC)          /* 3ms */
100
101 struct userdata {
102     pa_core *core;
103     pa_module *module;
104     pa_source *source;
105
106     pa_thread *thread;
107     pa_thread_mq thread_mq;
108     pa_rtpoll *rtpoll;
109
110     snd_pcm_t *pcm_handle;
111
112     pa_alsa_fdlist *mixer_fdl;
113     snd_mixer_t *mixer_handle;
114     snd_mixer_elem_t *mixer_elem;
115     long hw_volume_max, hw_volume_min;
116     long hw_dB_max, hw_dB_min;
117     pa_bool_t hw_dB_supported;
118
119     size_t frame_size, fragment_size, hwbuf_size, tsched_watermark;
120     unsigned nfragments;
121
122     char *device_name;
123
124     pa_bool_t use_mmap, use_tsched;
125
126     pa_rtpoll_item *alsa_rtpoll_item;
127
128     snd_mixer_selem_channel_id_t mixer_map[SND_MIXER_SCHN_LAST];
129
130     pa_smoother *smoother;
131     int64_t frame_index;
132
133     snd_pcm_sframes_t hwbuf_unused_frames;
134 };
135
136 static void fix_tsched_watermark(struct userdata *u) {
137     size_t max_use;
138     size_t min_sleep, min_wakeup;
139     pa_assert(u);
140
141     max_use = u->hwbuf_size - u->hwbuf_unused_frames * u->frame_size;
142
143     min_sleep = pa_usec_to_bytes(TSCHED_MIN_SLEEP_USEC, &u->source->sample_spec);
144     min_wakeup = pa_usec_to_bytes(TSCHED_MIN_WAKEUP_USEC, &u->source->sample_spec);
145
146     if (min_sleep > max_use/2)
147         min_sleep = pa_frame_align(max_use/2, &u->source->sample_spec);
148     if (min_sleep < u->frame_size)
149         min_sleep = u->frame_size;
150
151     if (min_wakeup > max_use/2)
152         min_wakeup = pa_frame_align(max_use/2, &u->source->sample_spec);
153     if (min_wakeup < u->frame_size)
154         min_wakeup = u->frame_size;
155
156     if (u->tsched_watermark > max_use-min_sleep)
157         u->tsched_watermark = max_use-min_sleep;
158
159     if (u->tsched_watermark < min_wakeup)
160         u->tsched_watermark = min_wakeup;
161 }
162
163 static pa_usec_t hw_sleep_time(struct userdata *u, pa_usec_t *sleep_usec, pa_usec_t*process_usec) {
164     pa_usec_t wm, usec;
165
166     pa_assert(u);
167
168     usec = pa_source_get_requested_latency_within_thread(u->source);
169
170     if (usec == (pa_usec_t) -1)
171         usec = pa_bytes_to_usec(u->hwbuf_size, &u->source->sample_spec);
172
173 /*     pa_log_debug("hw buffer time: %u ms", (unsigned) (usec / PA_USEC_PER_MSEC)); */
174
175     wm = pa_bytes_to_usec(u->tsched_watermark, &u->source->sample_spec);
176
177     if (usec >= wm) {
178         *sleep_usec = usec - wm;
179         *process_usec = wm;
180     } else
181         *process_usec = *sleep_usec = usec /= 2;
182
183 /*     pa_log_debug("after watermark: %u ms", (unsigned) (*sleep_usec / PA_USEC_PER_MSEC)); */
184
185     return usec;
186 }
187
188 static int try_recover(struct userdata *u, const char *call, int err) {
189     pa_assert(u);
190     pa_assert(call);
191     pa_assert(err < 0);
192
193     pa_log_debug("%s: %s", call, snd_strerror(err));
194
195     pa_assert(err != -EAGAIN);
196
197     if (err == -EPIPE)
198         pa_log_debug("%s: Buffer overrun!", call);
199
200     if ((err = snd_pcm_recover(u->pcm_handle, err, 1)) == 0) {
201         snd_pcm_start(u->pcm_handle);
202         return 0;
203     }
204
205     pa_log("%s: %s", call, snd_strerror(err));
206     return -1;
207 }
208
209 static size_t check_left_to_record(struct userdata *u, snd_pcm_sframes_t n) {
210     size_t left_to_record;
211
212     if (n*u->frame_size < u->hwbuf_size)
213         left_to_record = u->hwbuf_size - (n*u->frame_size);
214     else
215         left_to_record = 0;
216
217     if (left_to_record > 0) {
218 /*         pa_log_debug("%0.2f ms left to record", (double) pa_bytes_to_usec(left_to_record, &u->source->sample_spec) / PA_USEC_PER_MSEC); */
219     } else {
220         pa_log_info("Overrun!");
221
222         if (u->use_tsched) {
223             size_t old_watermark = u->tsched_watermark;
224
225             u->tsched_watermark *= 2;
226             fix_tsched_watermark(u);
227
228             if (old_watermark != u->tsched_watermark)
229                 pa_log_notice("Increasing wakeup watermark to %0.2f ms",
230                               (double) pa_bytes_to_usec(u->tsched_watermark, &u->source->sample_spec) / PA_USEC_PER_MSEC);
231         }
232     }
233
234     return left_to_record;
235 }
236
237 static int mmap_read(struct userdata *u, pa_usec_t *sleep_usec) {
238     int work_done = 0;
239     pa_usec_t max_sleep_usec, process_usec;
240     size_t left_to_record;
241
242     pa_assert(u);
243     pa_source_assert_ref(u->source);
244
245     if (u->use_tsched)
246         hw_sleep_time(u, &max_sleep_usec, &process_usec);
247
248     for (;;) {
249         snd_pcm_sframes_t n;
250         int r;
251
252         snd_pcm_hwsync(u->pcm_handle);
253
254         if (PA_UNLIKELY((n = snd_pcm_avail_update(u->pcm_handle)) < 0)) {
255
256             if ((r = try_recover(u, "snd_pcm_avail_update", n)) == 0)
257                 continue;
258
259             return r;
260         }
261
262         left_to_record = check_left_to_record(u, n);
263
264         if (u->use_tsched)
265             if (pa_bytes_to_usec(left_to_record, &u->source->sample_spec) > max_sleep_usec/2)
266                 break;
267
268         if (PA_UNLIKELY(n <= 0))
269             break;
270
271         for (;;) {
272             int err;
273             const snd_pcm_channel_area_t *areas;
274             snd_pcm_uframes_t offset, frames = (snd_pcm_uframes_t) n;
275             pa_memchunk chunk;
276             void *p;
277
278 /*             pa_log_debug("%lu frames to read", (unsigned long) frames); */
279
280             if (PA_UNLIKELY((err = snd_pcm_mmap_begin(u->pcm_handle, &areas, &offset, &frames)) < 0)) {
281
282                 if ((r = try_recover(u, "snd_pcm_mmap_begin", err)) == 0)
283                     continue;
284
285                 return r;
286             }
287
288             /* Make sure that if these memblocks need to be copied they will fit into one slot */
289             if (frames > pa_mempool_block_size_max(u->source->core->mempool)/u->frame_size)
290                 frames = pa_mempool_block_size_max(u->source->core->mempool)/u->frame_size;
291
292             /* Check these are multiples of 8 bit */
293             pa_assert((areas[0].first & 7) == 0);
294             pa_assert((areas[0].step & 7)== 0);
295
296             /* We assume a single interleaved memory buffer */
297             pa_assert((areas[0].first >> 3) == 0);
298             pa_assert((areas[0].step >> 3) == u->frame_size);
299
300             p = (uint8_t*) areas[0].addr + (offset * u->frame_size);
301
302             chunk.memblock = pa_memblock_new_fixed(u->core->mempool, p, frames * u->frame_size, TRUE);
303             chunk.length = pa_memblock_get_length(chunk.memblock);
304             chunk.index = 0;
305
306             pa_source_post(u->source, &chunk);
307             pa_memblock_unref_fixed(chunk.memblock);
308
309             if (PA_UNLIKELY((err = snd_pcm_mmap_commit(u->pcm_handle, offset, frames)) < 0)) {
310
311                 if ((r = try_recover(u, "snd_pcm_mmap_commit", err)) == 0)
312                     continue;
313
314                 return r;
315             }
316
317             work_done = 1;
318
319             u->frame_index += frames;
320
321 /*             pa_log_debug("read %lu frames", (unsigned long) frames); */
322
323             if (frames >= (snd_pcm_uframes_t) n)
324                 break;
325
326             n -= frames;
327         }
328     }
329
330     *sleep_usec = pa_bytes_to_usec(left_to_record, &u->source->sample_spec) - process_usec;
331     return work_done;
332 }
333
334 static int unix_read(struct userdata *u, pa_usec_t *sleep_usec) {
335     int work_done = 0;
336     pa_usec_t max_sleep_usec, process_usec;
337     size_t left_to_record;
338
339     pa_assert(u);
340     pa_source_assert_ref(u->source);
341
342     if (u->use_tsched)
343         hw_sleep_time(u, &max_sleep_usec, &process_usec);
344
345     for (;;) {
346         snd_pcm_sframes_t n;
347         int r;
348
349         snd_pcm_hwsync(u->pcm_handle);
350
351         if (PA_UNLIKELY((n = snd_pcm_avail_update(u->pcm_handle)) < 0)) {
352
353             if ((r = try_recover(u, "snd_pcm_avail_update", n)) == 0)
354                 continue;
355
356             return r;
357         }
358
359         left_to_record = check_left_to_record(u, n);
360
361         if (u->use_tsched)
362             if (pa_bytes_to_usec(left_to_record, &u->source->sample_spec) > max_sleep_usec/2)
363                 break;
364
365         if (PA_UNLIKELY(n <= 0))
366             return work_done;
367
368         for (;;) {
369             void *p;
370             snd_pcm_sframes_t frames;
371             pa_memchunk chunk;
372
373             chunk.memblock = pa_memblock_new(u->core->mempool, (size_t) -1);
374
375             frames = pa_memblock_get_length(chunk.memblock) / u->frame_size;
376
377             if (frames > n)
378                 frames = n;
379
380 /*             pa_log_debug("%lu frames to read", (unsigned long) n); */
381
382             p = pa_memblock_acquire(chunk.memblock);
383             frames = snd_pcm_readi(u->pcm_handle, (uint8_t*) p, frames);
384             pa_memblock_release(chunk.memblock);
385
386             pa_assert(frames != 0);
387
388             if (PA_UNLIKELY(frames < 0)) {
389                 pa_memblock_unref(chunk.memblock);
390
391                 if ((r = try_recover(u, "snd_pcm_readi", n)) == 0)
392                     continue;
393
394                 return r;
395             }
396
397             chunk.index = 0;
398             chunk.length = frames * u->frame_size;
399
400             pa_source_post(u->source, &chunk);
401             pa_memblock_unref(chunk.memblock);
402
403             work_done = 1;
404
405             u->frame_index += frames;
406
407 /*             pa_log_debug("read %lu frames", (unsigned long) frames); */
408
409             if (frames >= n)
410                 break;
411
412             n -= frames;
413         }
414     }
415
416     *sleep_usec = pa_bytes_to_usec(left_to_record, &u->source->sample_spec) - process_usec;
417     return work_done;
418 }
419
420 static void update_smoother(struct userdata *u) {
421     snd_pcm_sframes_t delay = 0;
422     int64_t frames;
423     int err;
424     pa_usec_t now1, now2;
425
426     pa_assert(u);
427     pa_assert(u->pcm_handle);
428
429     /* Let's update the time smoother */
430
431     snd_pcm_hwsync(u->pcm_handle);
432     snd_pcm_avail_update(u->pcm_handle);
433
434     if (PA_UNLIKELY((err = snd_pcm_delay(u->pcm_handle, &delay)) < 0)) {
435         pa_log_warn("Failed to get delay: %s", snd_strerror(err));
436         return;
437     }
438
439     frames = u->frame_index + delay;
440
441     now1 = pa_rtclock_usec();
442     now2 = pa_bytes_to_usec(frames * u->frame_size, &u->source->sample_spec);
443
444     pa_smoother_put(u->smoother, now1, now2);
445 }
446
447 static pa_usec_t source_get_latency(struct userdata *u) {
448     pa_usec_t r = 0;
449     int64_t delay;
450     pa_usec_t now1, now2;
451
452     pa_assert(u);
453
454     now1 = pa_rtclock_usec();
455     now2 = pa_smoother_get(u->smoother, now1);
456
457     delay = (int64_t) now2 - pa_bytes_to_usec(u->frame_index * u->frame_size, &u->source->sample_spec);
458
459     if (delay > 0)
460         r = (pa_usec_t) delay;
461
462     return r;
463 }
464
465 static int build_pollfd(struct userdata *u) {
466     pa_assert(u);
467     pa_assert(u->pcm_handle);
468
469     if (u->alsa_rtpoll_item)
470         pa_rtpoll_item_free(u->alsa_rtpoll_item);
471
472     if (!(u->alsa_rtpoll_item = pa_alsa_build_pollfd(u->pcm_handle, u->rtpoll)))
473         return -1;
474
475     return 0;
476 }
477
478 static int suspend(struct userdata *u) {
479     pa_assert(u);
480     pa_assert(u->pcm_handle);
481
482     pa_smoother_pause(u->smoother, pa_rtclock_usec());
483
484     /* Let's suspend */
485     snd_pcm_close(u->pcm_handle);
486     u->pcm_handle = NULL;
487
488     if (u->alsa_rtpoll_item) {
489         pa_rtpoll_item_free(u->alsa_rtpoll_item);
490         u->alsa_rtpoll_item = NULL;
491     }
492
493     pa_log_info("Device suspended...");
494
495     return 0;
496 }
497
498 static int update_sw_params(struct userdata *u) {
499     snd_pcm_uframes_t avail_min;
500     int err;
501
502     pa_assert(u);
503
504     /* Use the full buffer if noone asked us for anything specific */
505     u->hwbuf_unused_frames = 0;
506
507     if (u->use_tsched) {
508         pa_usec_t latency;
509
510         if ((latency = pa_source_get_requested_latency_within_thread(u->source)) != (pa_usec_t) -1) {
511             size_t b;
512
513             pa_log_debug("latency set to %0.2f", (double) latency / PA_USEC_PER_MSEC);
514
515             b = pa_usec_to_bytes(latency, &u->source->sample_spec);
516
517             /* We need at least one sample in our buffer */
518
519             if (PA_UNLIKELY(b < u->frame_size))
520                 b = u->frame_size;
521
522             u->hwbuf_unused_frames =
523                 PA_LIKELY(b < u->hwbuf_size) ?
524                 ((u->hwbuf_size - b) / u->frame_size) : 0;
525
526             fix_tsched_watermark(u);
527         }
528     }
529
530     pa_log_debug("hwbuf_unused_frames=%lu", (unsigned long) u->hwbuf_unused_frames);
531
532     avail_min = 1;
533
534     if (u->use_tsched) {
535         pa_usec_t sleep_usec, process_usec;
536
537         hw_sleep_time(u, &sleep_usec, &process_usec);
538         avail_min += pa_usec_to_bytes(sleep_usec, &u->source->sample_spec);
539     }
540
541     pa_log_debug("setting avail_min=%lu", (unsigned long) avail_min);
542
543     if ((err = pa_alsa_set_sw_params(u->pcm_handle, avail_min)) < 0) {
544         pa_log("Failed to set software parameters: %s", snd_strerror(err));
545         return err;
546     }
547
548     return 0;
549 }
550
551 static int unsuspend(struct userdata *u) {
552     pa_sample_spec ss;
553     int err;
554     pa_bool_t b, d;
555     unsigned nfrags;
556     snd_pcm_uframes_t period_size;
557
558     pa_assert(u);
559     pa_assert(!u->pcm_handle);
560
561     pa_log_info("Trying resume...");
562
563     snd_config_update_free_global();
564     if ((err = snd_pcm_open(&u->pcm_handle, u->device_name, SND_PCM_STREAM_CAPTURE, SND_PCM_NONBLOCK)) < 0) {
565         pa_log("Error opening PCM device %s: %s", u->device_name, snd_strerror(err));
566         goto fail;
567     }
568
569     ss = u->source->sample_spec;
570     nfrags = u->nfragments;
571     period_size = u->fragment_size / u->frame_size;
572     b = u->use_mmap;
573     d = u->use_tsched;
574
575     if ((err = pa_alsa_set_hw_params(u->pcm_handle, &ss, &nfrags, &period_size, u->hwbuf_size / u->frame_size, &b, &d, TRUE)) < 0) {
576         pa_log("Failed to set hardware parameters: %s", snd_strerror(err));
577         goto fail;
578     }
579
580     if (b != u->use_mmap || d != u->use_tsched) {
581         pa_log_warn("Resume failed, couldn't get original access mode.");
582         goto fail;
583     }
584
585     if (!pa_sample_spec_equal(&ss, &u->source->sample_spec)) {
586         pa_log_warn("Resume failed, couldn't restore original sample settings.");
587         goto fail;
588     }
589
590     if (nfrags != u->nfragments || period_size*u->frame_size != u->fragment_size) {
591         pa_log_warn("Resume failed, couldn't restore original fragment settings.");
592         goto fail;
593     }
594
595     if (update_sw_params(u) < 0)
596         goto fail;
597
598     if (build_pollfd(u) < 0)
599         goto fail;
600
601     /* FIXME: We need to reload the volume somehow */
602
603     snd_pcm_start(u->pcm_handle);
604     pa_smoother_resume(u->smoother, pa_rtclock_usec());
605
606     pa_log_info("Resumed successfully...");
607
608     return 0;
609
610 fail:
611     if (u->pcm_handle) {
612         snd_pcm_close(u->pcm_handle);
613         u->pcm_handle = NULL;
614     }
615
616     return -1;
617 }
618
619 static int source_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
620     struct userdata *u = PA_SOURCE(o)->userdata;
621
622     switch (code) {
623
624         case PA_SOURCE_MESSAGE_GET_LATENCY: {
625             pa_usec_t r = 0;
626
627             if (u->pcm_handle)
628                 r = source_get_latency(u);
629
630             *((pa_usec_t*) data) = r;
631
632             return 0;
633         }
634
635         case PA_SOURCE_MESSAGE_SET_STATE:
636
637             switch ((pa_source_state_t) PA_PTR_TO_UINT(data)) {
638
639                 case PA_SOURCE_SUSPENDED:
640                     pa_assert(PA_SOURCE_IS_OPENED(u->source->thread_info.state));
641
642                     if (suspend(u) < 0)
643                         return -1;
644
645                     break;
646
647                 case PA_SOURCE_IDLE:
648                 case PA_SOURCE_RUNNING:
649
650                     if (u->source->thread_info.state == PA_SOURCE_INIT) {
651                         if (build_pollfd(u) < 0)
652                             return -1;
653
654                         snd_pcm_start(u->pcm_handle);
655                     }
656
657                     if (u->source->thread_info.state == PA_SOURCE_SUSPENDED) {
658                         if (unsuspend(u) < 0)
659                             return -1;
660                     }
661
662                     break;
663
664                 case PA_SOURCE_UNLINKED:
665                 case PA_SOURCE_INIT:
666                     ;
667             }
668
669             break;
670     }
671
672     return pa_source_process_msg(o, code, data, offset, chunk);
673 }
674
675 static int mixer_callback(snd_mixer_elem_t *elem, unsigned int mask) {
676     struct userdata *u = snd_mixer_elem_get_callback_private(elem);
677
678     pa_assert(u);
679     pa_assert(u->mixer_handle);
680
681     if (mask == SND_CTL_EVENT_MASK_REMOVE)
682         return 0;
683
684     if (mask & SND_CTL_EVENT_MASK_VALUE) {
685         pa_source_get_volume(u->source);
686         pa_source_get_mute(u->source);
687     }
688
689     return 0;
690 }
691
692 static int source_get_volume_cb(pa_source *s) {
693     struct userdata *u = s->userdata;
694     int err;
695     int i;
696
697     pa_assert(u);
698     pa_assert(u->mixer_elem);
699
700     for (i = 0; i < s->sample_spec.channels; i++) {
701         long alsa_vol;
702
703         pa_assert(snd_mixer_selem_has_capture_channel(u->mixer_elem, u->mixer_map[i]));
704
705         if (u->hw_dB_supported) {
706
707             if ((err = snd_mixer_selem_get_capture_dB(u->mixer_elem, u->mixer_map[i], &alsa_vol)) >= 0) {
708                 s->volume.values[i] = pa_sw_volume_from_dB(alsa_vol / 100.0);
709                 continue;
710             }
711
712             u->hw_dB_supported = FALSE;
713         }
714
715         if ((err = snd_mixer_selem_get_capture_volume(u->mixer_elem, u->mixer_map[i], &alsa_vol)) < 0)
716             goto fail;
717
718         s->volume.values[i] = (pa_volume_t) roundf(((float) (alsa_vol - u->hw_volume_min) * PA_VOLUME_NORM) / (u->hw_volume_max - u->hw_volume_min));
719     }
720
721     return 0;
722
723 fail:
724     pa_log_error("Unable to read volume: %s", snd_strerror(err));
725
726     return -1;
727 }
728
729 static int source_set_volume_cb(pa_source *s) {
730     struct userdata *u = s->userdata;
731     int err;
732     int i;
733
734     pa_assert(u);
735     pa_assert(u->mixer_elem);
736
737     for (i = 0; i < s->sample_spec.channels; i++) {
738         long alsa_vol;
739         pa_volume_t vol;
740
741         pa_assert(snd_mixer_selem_has_capture_channel(u->mixer_elem, u->mixer_map[i]));
742
743         vol = PA_MIN(s->volume.values[i], PA_VOLUME_NORM);
744
745         if (u->hw_dB_supported) {
746             alsa_vol = (long) (pa_sw_volume_to_dB(vol) * 100);
747             alsa_vol = PA_CLAMP_UNLIKELY(alsa_vol, u->hw_dB_min, u->hw_dB_max);
748
749
750             if ((err = snd_mixer_selem_set_capture_dB(u->mixer_elem, u->mixer_map[i], alsa_vol, -1)) >= 0) {
751
752                 if (snd_mixer_selem_get_capture_dB(u->mixer_elem, u->mixer_map[i], &alsa_vol) >= 0)
753                     s->volume.values[i] = pa_sw_volume_from_dB(alsa_vol / 100.0);
754
755                 continue;
756             }
757
758             u->hw_dB_supported = FALSE;
759         }
760
761         alsa_vol = (long) roundf(((float) vol * (u->hw_volume_max - u->hw_volume_min)) / PA_VOLUME_NORM) + u->hw_volume_min;
762         alsa_vol = PA_CLAMP_UNLIKELY(alsa_vol, u->hw_volume_min, u->hw_volume_max);
763
764         if ((err = snd_mixer_selem_set_capture_volume(u->mixer_elem, u->mixer_map[i], alsa_vol)) < 0)
765             goto fail;
766
767         if (snd_mixer_selem_get_capture_volume(u->mixer_elem, u->mixer_map[i], &alsa_vol) >= 0)
768             s->volume.values[i] = (pa_volume_t) roundf(((float) (alsa_vol - u->hw_volume_min) * PA_VOLUME_NORM) / (u->hw_volume_max - u->hw_volume_min));
769     }
770
771     return 0;
772
773 fail:
774     pa_log_error("Unable to set volume: %s", snd_strerror(err));
775
776     return -1;
777 }
778
779 static int source_get_mute_cb(pa_source *s) {
780     struct userdata *u = s->userdata;
781     int err, sw;
782
783     pa_assert(u);
784     pa_assert(u->mixer_elem);
785
786     if ((err = snd_mixer_selem_get_capture_switch(u->mixer_elem, 0, &sw)) < 0) {
787         pa_log_error("Unable to get switch: %s", snd_strerror(err));
788         return -1;
789     }
790
791     s->muted = !sw;
792
793     return 0;
794 }
795
796 static int source_set_mute_cb(pa_source *s) {
797     struct userdata *u = s->userdata;
798     int err;
799
800     pa_assert(u);
801     pa_assert(u->mixer_elem);
802
803     if ((err = snd_mixer_selem_set_capture_switch_all(u->mixer_elem, !s->muted)) < 0) {
804         pa_log_error("Unable to set switch: %s", snd_strerror(err));
805         return -1;
806     }
807
808     return 0;
809 }
810
811 static void source_update_requested_latency_cb(pa_source *s) {
812     struct userdata *u = s->userdata;
813     pa_assert(u);
814
815     if (!u->pcm_handle)
816         return;
817
818     update_sw_params(u);
819 }
820
821 static void thread_func(void *userdata) {
822     struct userdata *u = userdata;
823
824     pa_assert(u);
825
826     pa_log_debug("Thread starting up");
827
828     if (u->core->realtime_scheduling)
829         pa_make_realtime(u->core->realtime_priority);
830
831     pa_thread_mq_install(&u->thread_mq);
832     pa_rtpoll_install(u->rtpoll);
833
834     for (;;) {
835         int ret;
836
837 /*         pa_log_debug("loop"); */
838
839         /* Read some data and pass it to the sources */
840         if (PA_SOURCE_IS_OPENED(u->source->thread_info.state)) {
841             int work_done = 0;
842             pa_usec_t sleep_usec;
843
844             if (u->use_mmap)
845                 work_done = mmap_read(u, &sleep_usec);
846             else
847                 work_done = unix_read(u, &sleep_usec);
848
849             if (work_done < 0)
850                 goto fail;
851
852 /*             pa_log_debug("work_done = %i", work_done); */
853
854             if (work_done)
855                 update_smoother(u);
856
857             if (u->use_tsched) {
858                 pa_usec_t cusec;
859
860                 /* OK, the capture buffer is now empty, let's
861                  * calculate when to wake up next */
862
863 /*                 pa_log_debug("Waking up in %0.2fms (sound card clock).", (double) sleep_usec / PA_USEC_PER_MSEC); */
864
865                 /* Convert from the sound card time domain to the
866                  * system time domain */
867                 cusec = pa_smoother_translate(u->smoother, pa_rtclock_usec(), sleep_usec);
868
869 /*                 pa_log_debug("Waking up in %0.2fms (system clock).", (double) cusec / PA_USEC_PER_MSEC); */
870
871                 /* We don't trust the conversion, so we wake up whatever comes first */
872                 pa_rtpoll_set_timer_relative(u->rtpoll, PA_MIN(sleep_usec, cusec));
873             }
874         } else if (u->use_tsched)
875
876             /* OK, we're in an invalid state, let's disable our timers */
877             pa_rtpoll_set_timer_disabled(u->rtpoll);
878
879         /* Hmm, nothing to do. Let's sleep */
880         if ((ret = pa_rtpoll_run(u->rtpoll, 1)) < 0)
881             goto fail;
882
883         if (ret == 0)
884             goto finish;
885
886         /* Tell ALSA about this and process its response */
887         if (PA_SOURCE_IS_OPENED(u->source->thread_info.state)) {
888             struct pollfd *pollfd;
889             unsigned short revents = 0;
890             int err;
891             unsigned n;
892
893             pollfd = pa_rtpoll_item_get_pollfd(u->alsa_rtpoll_item, &n);
894
895             if ((err = snd_pcm_poll_descriptors_revents(u->pcm_handle, pollfd, n, &revents)) < 0) {
896                 pa_log("snd_pcm_poll_descriptors_revents() failed: %s", snd_strerror(err));
897                 goto fail;
898             }
899
900             if (revents & (POLLERR|POLLNVAL|POLLHUP)) {
901                 if (pa_alsa_recover_from_poll(u->pcm_handle, revents) < 0)
902                     goto fail;
903
904                 snd_pcm_start(u->pcm_handle);
905             }
906
907             if (revents && u->use_tsched)
908                 pa_log_debug("Wakeup from ALSA! (%i)", revents);
909         }
910     }
911
912 fail:
913     /* If this was no regular exit from the loop we have to continue
914      * processing messages until we received PA_MESSAGE_SHUTDOWN */
915     pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0, NULL, NULL);
916     pa_asyncmsgq_wait_for(u->thread_mq.inq, PA_MESSAGE_SHUTDOWN);
917
918 finish:
919     pa_log_debug("Thread shutting down");
920 }
921
922 int pa__init(pa_module*m) {
923
924     pa_modargs *ma = NULL;
925     struct userdata *u = NULL;
926     const char *dev_id;
927     pa_sample_spec ss;
928     pa_channel_map map;
929     uint32_t nfrags, hwbuf_size, frag_size, tsched_size, tsched_watermark;
930     snd_pcm_uframes_t period_frames, tsched_frames;
931     size_t frame_size;
932     snd_pcm_info_t *pcm_info = NULL;
933     int err;
934     const char *name;
935     char *name_buf = NULL;
936     pa_bool_t namereg_fail;
937     pa_bool_t use_mmap = TRUE, b, use_tsched = TRUE, d, mixer_reset = TRUE;
938     pa_source_new_data data;
939
940     snd_pcm_info_alloca(&pcm_info);
941
942     pa_assert(m);
943
944     pa_alsa_redirect_errors_inc();
945
946     if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
947         pa_log("Failed to parse module arguments");
948         goto fail;
949     }
950
951     ss = m->core->default_sample_spec;
952     if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_ALSA) < 0) {
953         pa_log("Failed to parse sample specification");
954         goto fail;
955     }
956
957     frame_size = pa_frame_size(&ss);
958
959     nfrags = m->core->default_n_fragments;
960     frag_size = pa_usec_to_bytes(m->core->default_fragment_size_msec*PA_USEC_PER_MSEC, &ss);
961     if (frag_size <= 0)
962         frag_size = frame_size;
963     tsched_size = pa_usec_to_bytes(DEFAULT_TSCHED_BUFFER_USEC, &ss);
964     tsched_watermark = pa_usec_to_bytes(DEFAULT_TSCHED_WATERMARK_USEC, &ss);
965
966     if (pa_modargs_get_value_u32(ma, "fragments", &nfrags) < 0 ||
967         pa_modargs_get_value_u32(ma, "fragment_size", &frag_size) < 0 ||
968         pa_modargs_get_value_u32(ma, "tsched_buffer_size", &tsched_size) < 0 ||
969         pa_modargs_get_value_u32(ma, "tsched_buffer_watermark", &tsched_watermark) < 0) {
970         pa_log("Failed to parse buffer metrics");
971         goto fail;
972     }
973
974     hwbuf_size = frag_size * nfrags;
975     period_frames = frag_size/frame_size;
976     tsched_frames = tsched_size/frame_size;
977
978     if (pa_modargs_get_value_boolean(ma, "mmap", &use_mmap) < 0) {
979         pa_log("Failed to parse mmap argument.");
980         goto fail;
981     }
982
983     if (pa_modargs_get_value_boolean(ma, "tsched", &use_tsched) < 0) {
984         pa_log("Failed to parse timer_scheduling argument.");
985         goto fail;
986     }
987
988     if (use_tsched && !pa_rtclock_hrtimer()) {
989         pa_log("Disabling timer-based scheduling because high-resolution timers are not available from the kernel.");
990         use_tsched = FALSE;
991     }
992
993     if (pa_modargs_get_value_boolean(ma, "mixer_reset", &mixer_reset) < 0) {
994         pa_log("Failed to parse mixer_reset argument.");
995         goto fail;
996     }
997
998     u = pa_xnew0(struct userdata, 1);
999     u->core = m->core;
1000     u->module = m;
1001     m->userdata = u;
1002     u->use_mmap = use_mmap;
1003     u->use_tsched = use_tsched;
1004     u->rtpoll = pa_rtpoll_new();
1005     pa_thread_mq_init(&u->thread_mq, m->core->mainloop, u->rtpoll);
1006     u->alsa_rtpoll_item = NULL;
1007
1008     u->smoother = pa_smoother_new(DEFAULT_TSCHED_WATERMARK_USEC, DEFAULT_TSCHED_WATERMARK_USEC, TRUE, 5);
1009     pa_smoother_set_time_offset(u->smoother, pa_rtclock_usec());
1010
1011     snd_config_update_free_global();
1012
1013     b = use_mmap;
1014     d = use_tsched;
1015
1016     if ((dev_id = pa_modargs_get_value(ma, "device_id", NULL))) {
1017
1018         if (!(u->pcm_handle = pa_alsa_open_by_device_id(
1019                       dev_id,
1020                       &u->device_name,
1021                       &ss, &map,
1022                       SND_PCM_STREAM_CAPTURE,
1023                       &nfrags, &period_frames, tsched_frames,
1024                       &b, &d)))
1025             goto fail;
1026
1027     } else {
1028
1029         if (!(u->pcm_handle = pa_alsa_open_by_device_string(
1030                       pa_modargs_get_value(ma, "device", DEFAULT_DEVICE),
1031                       &u->device_name,
1032                       &ss, &map,
1033                       SND_PCM_STREAM_CAPTURE,
1034                       &nfrags, &period_frames, tsched_frames,
1035                       &b, &d)))
1036             goto fail;
1037     }
1038
1039     pa_assert(u->device_name);
1040     pa_log_info("Successfully opened device %s.", u->device_name);
1041
1042     if (use_mmap && !b) {
1043         pa_log_info("Device doesn't support mmap(), falling back to UNIX read/write mode.");
1044         u->use_mmap = use_mmap = FALSE;
1045     }
1046
1047     if (use_tsched && (!b || !d)) {
1048         pa_log_info("Cannot enabled timer-based scheduling, falling back to sound IRQ scheduling.");
1049         u->use_tsched = use_tsched = FALSE;
1050     }
1051
1052     if (u->use_mmap)
1053         pa_log_info("Successfully enabled mmap() mode.");
1054
1055     if (u->use_tsched)
1056         pa_log_info("Successfully enabled timer-based scheduling mode.");
1057
1058     if ((err = snd_pcm_info(u->pcm_handle, pcm_info)) < 0) {
1059         pa_log("Error fetching PCM info: %s", snd_strerror(err));
1060         goto fail;
1061     }
1062
1063     /* ALSA might tweak the sample spec, so recalculate the frame size */
1064     frame_size = pa_frame_size(&ss);
1065
1066     if ((err = snd_mixer_open(&u->mixer_handle, 0)) < 0)
1067         pa_log("Error opening mixer: %s", snd_strerror(err));
1068     else {
1069         pa_bool_t found = FALSE;
1070
1071         if (pa_alsa_prepare_mixer(u->mixer_handle, u->device_name) >= 0)
1072             found = TRUE;
1073         else {
1074             snd_pcm_info_t* info;
1075
1076             snd_pcm_info_alloca(&info);
1077
1078             if (snd_pcm_info(u->pcm_handle, info) >= 0) {
1079                 char *md;
1080                 int card;
1081
1082                 if ((card = snd_pcm_info_get_card(info)) >= 0) {
1083
1084                     md = pa_sprintf_malloc("hw:%i", card);
1085
1086                     if (strcmp(u->device_name, md))
1087                         if (pa_alsa_prepare_mixer(u->mixer_handle, md) >= 0)
1088                             found = TRUE;
1089                     pa_xfree(md);
1090                 }
1091             }
1092         }
1093
1094         if (found)
1095             if (!(u->mixer_elem = pa_alsa_find_elem(u->mixer_handle, "Capture", "Mic")))
1096                 found = FALSE;
1097
1098         if (!found) {
1099             snd_mixer_close(u->mixer_handle);
1100             u->mixer_handle = NULL;
1101         }
1102     }
1103
1104     if ((name = pa_modargs_get_value(ma, "source_name", NULL)))
1105         namereg_fail = TRUE;
1106     else {
1107         name = name_buf = pa_sprintf_malloc("alsa_input.%s", u->device_name);
1108         namereg_fail = FALSE;
1109     }
1110
1111     pa_source_new_data_init(&data);
1112     data.driver = __FILE__;
1113     data.module = m;
1114     pa_source_new_data_set_name(&data, name);
1115     data.namereg_fail = namereg_fail;
1116     pa_source_new_data_set_sample_spec(&data, &ss);
1117     pa_source_new_data_set_channel_map(&data, &map);
1118
1119     pa_alsa_init_proplist(data.proplist, pcm_info);
1120     pa_proplist_sets(data.proplist, PA_PROP_DEVICE_STRING, u->device_name);
1121     pa_proplist_setf(data.proplist, PA_PROP_DEVICE_BUFFERING_BUFFER_SIZE, "%lu", (unsigned long) (period_frames * frame_size * nfrags));
1122     pa_proplist_setf(data.proplist, PA_PROP_DEVICE_BUFFERING_FRAGMENT_SIZE, "%lu", (unsigned long) (period_frames * frame_size));
1123     pa_proplist_sets(data.proplist, PA_PROP_DEVICE_ACCESS_MODE, u->use_tsched ? "mmap+timer" : (u->use_mmap ? "mmap" : "serial"));
1124
1125     u->source = pa_source_new(m->core, &data, PA_SOURCE_HARDWARE|PA_SOURCE_LATENCY);
1126     pa_source_new_data_done(&data);
1127     pa_xfree(name_buf);
1128
1129     if (!u->source) {
1130         pa_log("Failed to create source object");
1131         goto fail;
1132     }
1133
1134     u->source->parent.process_msg = source_process_msg;
1135     u->source->update_requested_latency = source_update_requested_latency_cb;
1136     u->source->userdata = u;
1137
1138     pa_source_set_asyncmsgq(u->source, u->thread_mq.inq);
1139     pa_source_set_rtpoll(u->source, u->rtpoll);
1140
1141     u->frame_size = frame_size;
1142     u->fragment_size = frag_size = period_frames * frame_size;
1143     u->nfragments = nfrags;
1144     u->hwbuf_size = u->fragment_size * nfrags;
1145     u->hwbuf_unused_frames = 0;
1146     u->tsched_watermark = tsched_watermark;
1147     u->frame_index = 0;
1148     u->hw_dB_supported = FALSE;
1149     u->hw_dB_min = u->hw_dB_max = 0;
1150     u->hw_volume_min = u->hw_volume_max = 0;
1151
1152     if (use_tsched)
1153         fix_tsched_watermark(u);
1154
1155     u->source->max_latency = pa_bytes_to_usec(u->hwbuf_size, &ss);
1156     if (!use_tsched)
1157         u->source->min_latency = u->source->max_latency;
1158
1159     pa_log_info("Using %u fragments of size %lu bytes, buffer time is %0.2fms",
1160                 nfrags, (long unsigned) u->fragment_size,
1161                 (double) pa_bytes_to_usec(u->hwbuf_size, &ss) / PA_USEC_PER_MSEC);
1162
1163     if (use_tsched)
1164         pa_log_info("Time scheduling watermark is %0.2fms",
1165                     (double) pa_bytes_to_usec(u->tsched_watermark, &ss) / PA_USEC_PER_MSEC);
1166
1167     if (update_sw_params(u) < 0)
1168         goto fail;
1169
1170     if (u->mixer_handle) {
1171         pa_assert(u->mixer_elem);
1172
1173         if (snd_mixer_selem_has_capture_volume(u->mixer_elem))
1174             if (pa_alsa_calc_mixer_map(u->mixer_elem, &map, u->mixer_map, FALSE) >= 0 &&
1175                 snd_mixer_selem_get_capture_volume_range(u->mixer_elem, &u->hw_volume_min, &u->hw_volume_max) >= 0) {
1176
1177                 pa_bool_t suitable = TRUE;
1178
1179                 pa_log_info("Volume ranges from %li to %li.", u->hw_volume_min, u->hw_volume_max);
1180
1181                 if (u->hw_volume_min > u->hw_volume_max) {
1182
1183                     pa_log_info("Minimal volume %li larger than maximum volume %li. Strange stuff Falling back to software volume control.", u->hw_volume_min, u->hw_volume_max);
1184                     suitable = FALSE;
1185
1186                 } else if (u->hw_volume_max - u->hw_volume_min < 3) {
1187
1188                     pa_log_info("Device has less than 4 volume levels. Falling back to software volume control.");
1189                     suitable = FALSE;
1190
1191                 } else if (snd_mixer_selem_get_capture_dB_range(u->mixer_elem, &u->hw_dB_min, &u->hw_dB_max) >= 0) {
1192
1193                     pa_log_info("Volume ranges from %0.2f dB to %0.2f dB.", u->hw_dB_min/100.0, u->hw_dB_max/100.0);
1194
1195                     /* Let's see if this thing actually is useful for muting */
1196                     if (u->hw_dB_min > -6000) {
1197                         pa_log_info("Device cannot attenuate for more than -60 dB (only %0.2f dB supported), falling back to software volume control.", ((double) u->hw_dB_min) / 100);
1198
1199                         suitable = FALSE;
1200                     } else if (u->hw_dB_max < 0) {
1201
1202                         pa_log_info("Device is still attenuated at maximum volume setting (%0.2f dB is maximum). Strange stuff. Falling back to software volume control.", ((double) u->hw_dB_max) / 100);
1203                         suitable = FALSE;
1204
1205                     } else if (u->hw_dB_min >= u->hw_dB_max) {
1206
1207                         pa_log_info("Minimal dB (%0.2f) larger or equal to maximum dB (%0.2f). Strange stuff. Falling back to software volume control.", ((double) u->hw_dB_min) / 100, ((double) u->hw_dB_max) / 100);
1208                         suitable = FALSE;
1209
1210                     } else
1211                         u->hw_dB_supported = TRUE;
1212                 }
1213
1214                 if (suitable) {
1215                     u->source->get_volume = source_get_volume_cb;
1216                     u->source->set_volume = source_set_volume_cb;
1217                     u->source->flags |= PA_SOURCE_HW_VOLUME_CTRL | (u->hw_dB_supported ? PA_SOURCE_DECIBEL_VOLUME : 0);
1218                     pa_log_info("Using hardware volume control. Hardware dB scale %s.", u->hw_dB_supported ? "supported" : "not supported");
1219
1220                 } else if (mixer_reset) {
1221                     pa_log_info("Using software volume control. Trying to reset sound card to 0 dB.");
1222                     pa_alsa_0dB_capture(u->mixer_elem);
1223                 } else
1224                     pa_log_info("Using software volume control. Leaving hw mixer controls untouched.");
1225
1226             }
1227
1228
1229         if (snd_mixer_selem_has_capture_switch(u->mixer_elem)) {
1230             u->source->get_mute = source_get_mute_cb;
1231             u->source->set_mute = source_set_mute_cb;
1232             u->source->flags |= PA_SOURCE_HW_MUTE_CTRL;
1233         }
1234
1235         u->mixer_fdl = pa_alsa_fdlist_new();
1236
1237         if (pa_alsa_fdlist_set_mixer(u->mixer_fdl, u->mixer_handle, m->core->mainloop) < 0) {
1238             pa_log("Failed to initialize file descriptor monitoring");
1239             goto fail;
1240         }
1241
1242         snd_mixer_elem_set_callback(u->mixer_elem, mixer_callback);
1243         snd_mixer_elem_set_callback_private(u->mixer_elem, u);
1244     } else
1245         u->mixer_fdl = NULL;
1246
1247     pa_alsa_dump(u->pcm_handle);
1248
1249     if (!(u->thread = pa_thread_new(thread_func, u))) {
1250         pa_log("Failed to create thread.");
1251         goto fail;
1252     }
1253     /* Get initial mixer settings */
1254     if (data.volume_is_set) {
1255         if (u->source->set_volume)
1256             u->source->set_volume(u->source);
1257     } else {
1258         if (u->source->get_volume)
1259             u->source->get_volume(u->source);
1260     }
1261
1262     if (data.muted_is_set) {
1263         if (u->source->set_mute)
1264             u->source->set_mute(u->source);
1265     } else {
1266         if (u->source->get_mute)
1267             u->source->get_mute(u->source);
1268     }
1269
1270     pa_source_put(u->source);
1271
1272     pa_modargs_free(ma);
1273
1274     return 0;
1275
1276 fail:
1277
1278     if (ma)
1279         pa_modargs_free(ma);
1280
1281     pa__done(m);
1282
1283     return -1;
1284 }
1285
1286 void pa__done(pa_module*m) {
1287     struct userdata *u;
1288
1289     pa_assert(m);
1290
1291     if (!(u = m->userdata)) {
1292         pa_alsa_redirect_errors_dec();
1293         return;
1294     }
1295
1296     if (u->source)
1297         pa_source_unlink(u->source);
1298
1299     if (u->thread) {
1300         pa_asyncmsgq_send(u->thread_mq.inq, NULL, PA_MESSAGE_SHUTDOWN, NULL, 0, NULL);
1301         pa_thread_free(u->thread);
1302     }
1303
1304     pa_thread_mq_done(&u->thread_mq);
1305
1306     if (u->source)
1307         pa_source_unref(u->source);
1308
1309     if (u->alsa_rtpoll_item)
1310         pa_rtpoll_item_free(u->alsa_rtpoll_item);
1311
1312     if (u->rtpoll)
1313         pa_rtpoll_free(u->rtpoll);
1314
1315     if (u->mixer_fdl)
1316         pa_alsa_fdlist_free(u->mixer_fdl);
1317
1318     if (u->mixer_handle)
1319         snd_mixer_close(u->mixer_handle);
1320
1321     if (u->pcm_handle) {
1322         snd_pcm_drop(u->pcm_handle);
1323         snd_pcm_close(u->pcm_handle);
1324     }
1325
1326     if (u->smoother)
1327         pa_smoother_free(u->smoother);
1328
1329     pa_xfree(u->device_name);
1330     pa_xfree(u);
1331
1332     snd_config_update_free_global();
1333     pa_alsa_redirect_errors_dec();
1334 }