]> git.sesse.net Git - vlc/blob - modules/audio_output/pulse.c
PulseAudio: add some callbacks for debug
[vlc] / modules / audio_output / pulse.c
1 /*****************************************************************************
2  * pulse.c : Pulseaudio output plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2008 the VideoLAN team
5  * Copyright (C) 2009-2011 RĂ©mi Denis-Courmont
6  *
7  * Authors: Martin Hamrle <hamrle @ post . cz>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27
28 #include <vlc_common.h>
29 #include <vlc_plugin.h>
30 #include <vlc_aout.h>
31 #include <vlc_cpu.h>
32
33 #include <pulse/pulseaudio.h>
34
35 static int  Open        ( vlc_object_t * );
36 static void Close       ( vlc_object_t * );
37
38 vlc_module_begin ()
39     set_shortname( "PulseAudio" )
40     set_description( N_("Pulseaudio audio output") )
41     set_capability( "audio output", 160 )
42     set_category( CAT_AUDIO )
43     set_subcategory( SUBCAT_AUDIO_AOUT )
44     add_shortcut( "pulseaudio", "pa" )
45     set_callbacks( Open, Close )
46 vlc_module_end ()
47
48 /* TODO: single static mainloop */
49
50 struct aout_sys_t
51 {
52     pa_stream *stream; /**< PulseAudio playback stream object */
53     pa_context *context; /**< PulseAudio connection context */
54     pa_threaded_mainloop *mainloop; /**< PulseAudio event loop */
55     pa_volume_t base_volume; /**< 0dB reference volume */
56     pa_cvolume cvolume; /**< actual sink input volume */
57     //uint32_t byterate; /**< bytes per second */
58 };
59
60 /* Context helpers */
61 static void context_state_cb(pa_context *c, void *userdata)
62 {
63     pa_threaded_mainloop *mainloop = userdata;
64
65     switch (pa_context_get_state(c)) {
66         case PA_CONTEXT_READY:
67         case PA_CONTEXT_FAILED:
68         case PA_CONTEXT_TERMINATED:
69             pa_threaded_mainloop_signal(mainloop, 0);
70         default:
71             break;
72     }
73 }
74
75 static bool context_wait(pa_threaded_mainloop *mainloop, pa_context *context)
76 {
77     pa_context_state_t state;
78
79     while ((state = pa_context_get_state(context)) != PA_CONTEXT_READY) {
80         if (state == PA_CONTEXT_FAILED || state == PA_CONTEXT_TERMINATED)
81             return -1;
82         pa_threaded_mainloop_wait(mainloop);
83     }
84     return 0;
85 }
86
87 static void error(aout_instance_t *aout, const char *msg, pa_context *context)
88 {
89     msg_Err(aout, "%s: %s", msg, pa_strerror(pa_context_errno(context)));
90 }
91
92 /* Sink */
93 static void sink_info_cb(pa_context *c, const pa_sink_info *i, int eol,
94                          void *userdata)
95 {
96     aout_instance_t *aout = userdata;
97     aout_sys_t *sys = aout->output.p_sys;
98
99     if (eol)
100         return;
101     (void) c;
102
103     /* PulseAudio flat volume NORM / 100% / 0dB corresponds to no software
104      * amplification and maximum hardware amplification.
105      * VLC maps DEFAULT / 100% to no gain at all (software/hardware).
106      * Thus we need to use the sink base_volume as a multiplier,
107      * if and only if flat volume is active for our current sink. */
108     if (i->flags & PA_SINK_FLAT_VOLUME)
109         sys->base_volume = i->base_volume;
110     else
111         sys->base_volume = PA_VOLUME_NORM;
112     msg_Dbg(aout, "base volume: %f", pa_sw_volume_to_linear(sys->base_volume));
113 }
114
115 /* Stream helpers */
116 static void stream_state_cb(pa_stream *s, void *userdata)
117 {
118     pa_threaded_mainloop *mainloop = userdata;
119
120     switch (pa_stream_get_state(s)) {
121         case PA_STREAM_READY:
122         case PA_STREAM_FAILED:
123         case PA_STREAM_TERMINATED:
124             pa_threaded_mainloop_signal(mainloop, 0);
125         default:
126             break;
127     }
128 }
129
130 static void stream_moved_cb(pa_stream *s, void *userdata)
131 {
132     aout_instance_t *aout = userdata;
133     aout_sys_t *sys = aout->output.p_sys;
134     pa_operation *op;
135     uint32_t idx = pa_stream_get_device_index(s);
136
137     msg_Dbg(aout, "connected to sink %"PRIu32": %s", idx,
138                   pa_stream_get_device_name(s));
139     op = pa_context_get_sink_info_by_index(sys->context, idx,
140                                            sink_info_cb, aout);
141     if (likely(op != NULL))
142         pa_operation_unref(op);
143 }
144
145 static void stream_overflow_cb(pa_stream *s, void *userdata)
146 {
147     aout_instance_t *aout = userdata;
148
149     msg_Err(aout, "overflow");
150     (void) s;
151 }
152
153 static void stream_started_cb(pa_stream *s, void *userdata)
154 {
155     aout_instance_t *aout = userdata;
156
157     msg_Dbg(aout, "started");
158     (void) s;
159 }
160
161 static void stream_suspended_cb(pa_stream *s, void *userdata)
162 {
163     aout_instance_t *aout = userdata;
164
165     msg_Dbg(aout, "suspended");
166     (void) s;
167 }
168
169 static void stream_underflow_cb(pa_stream *s, void *userdata)
170 {
171     aout_instance_t *aout = userdata;
172
173     msg_Dbg(aout, "underflow");
174     (void) s;
175 }
176
177 static int stream_wait(pa_threaded_mainloop *mainloop, pa_stream *stream)
178 {
179     pa_stream_state_t state;
180
181     while ((state = pa_stream_get_state(stream)) != PA_STREAM_READY) {
182         if (state == PA_STREAM_FAILED || state == PA_STREAM_TERMINATED)
183             return -1;
184         pa_threaded_mainloop_wait(mainloop);
185     }
186     return 0;
187 }
188
189 /* Memory free callback. The block_t address is in front of the data. */
190 static void data_free(void *data)
191 {
192     block_t **pp = data, *block;
193
194     memcpy(&block, pp - 1, sizeof (block));
195     block_Release(block);
196 }
197
198 static void *data_convert(block_t **pp)
199 {
200     block_t *block = *pp;
201     /* In most cases, there is enough head room, and this is really cheap: */
202     block = block_Realloc(block, sizeof (block), block->i_buffer);
203     *pp = block;
204     if (unlikely(block == NULL))
205         return NULL;
206
207     memcpy(block->p_buffer, &block, sizeof (block));
208     block->p_buffer += sizeof (block);
209     block->i_buffer -= sizeof (block);
210     return block->p_buffer;
211 }
212
213 /*****************************************************************************
214  * Play: play a sound samples buffer
215  *****************************************************************************/
216 static void Play(aout_instance_t *aout)
217 {
218     aout_sys_t *sys = aout->output.p_sys;
219     pa_stream *s = sys->stream;
220
221     /* Note: The core already holds the output FIFO lock at this point.
222      * Therefore we must not under any circumstances (try to) acquire the
223      * output FIFO lock while the PulseAudio threaded main loop lock is held
224      * (including from PulseAudio stream callbacks). Otherwise lock inversion
225      * will take place, and sooner or later a deadlock. */
226     pa_threaded_mainloop_lock(sys->mainloop);
227
228     if (pa_stream_is_corked(sys->stream) > 0) {
229         pa_operation *op = pa_stream_cork(s, 0, NULL, NULL);
230         if (op != NULL)
231             pa_operation_unref(op);
232         msg_Dbg(aout, "uncorking");
233     }
234
235 #if 0
236     /* This function should be called by the LibVLC core a header of time,
237      * but not more than AOUT_MAX_PREPARE. The PulseAudio latency should be
238      * shorter than that (though it might not be the case with some evil piece
239      * of audio output hardware). So we may need to trigger playback early,
240      * (that is to say, short cut the PulseAudio prebuffering). Otherwise,
241      * audio and video may be out of synchronization. */
242     pa_usec_t latency;
243     int negative;
244     if (pa_stream_get_latency(s, &latency, &negative) < 0) {
245         /* Especially at start of stream, latency may not be known (yet). */
246         if (pa_context_errno(sys->context) != PA_ERR_NODATA)
247             error(aout, "cannot determine latency", sys->context);
248     } else {
249         mtime_t gap = aout_FifoFirstDate(aout, &aout->output.fifo) - mdate()
250                 - latency;
251
252         if (gap > AOUT_PTS_TOLERANCE)
253             msg_Dbg(aout, "buffer too early (%"PRId64" us)", gap);
254         else if (gap < -AOUT_PTS_TOLERANCE)
255             msg_Err(aout, "buffer too late (%"PRId64" us)", -gap);
256     }
257 #endif
258 #if 0 /* Fault injector to test underrun recovery */
259     static unsigned u = 0;
260     if ((++u % 500) == 0) {
261         msg_Err(aout, "fault injection");
262         msleep(CLOCK_FREQ*2);
263     }
264 #endif
265
266     /* This function is called exactly once per block in the output FIFO, so
267      * this for-loop is not necessary.
268      * If this function is changed to not always dequeue blocks, be sure to
269      * limit the queue size to a reasonable limit to avoid huge leaks. */
270     for (;;) {
271         block_t *block = aout_FifoPop(aout, &aout->output.fifo);
272         if (block == NULL)
273             break;
274
275         const void *ptr = data_convert(&block);
276         if (unlikely(ptr == NULL))
277             break;
278
279         size_t len = block->i_buffer;
280         //mtime_t pts = block->i_pts, duration = block->i_length;
281
282         if (pa_stream_write(s, ptr, len, data_free, 0, PA_SEEK_RELATIVE) < 0)
283         {
284             block_Release(block);
285             msg_Err(aout, "cannot write: %s",
286                     pa_strerror(pa_context_errno(sys->context)));
287         }
288     }
289
290     pa_threaded_mainloop_unlock(sys->mainloop);
291 }
292
293 static int VolumeSet(aout_instance_t *aout, audio_volume_t vol, bool mute)
294 {
295     aout_sys_t *sys = aout->output.p_sys;
296     pa_threaded_mainloop *mainloop = sys->mainloop;
297     pa_operation *op;
298
299     uint32_t idx = pa_stream_get_index(sys->stream);
300     pa_volume_t volume = pa_sw_volume_from_linear(vol / (float)AOUT_VOLUME_DEFAULT);
301     pa_cvolume cvolume;
302
303     /* TODO: do not ruin the channel balance (if set outside VLC) */
304     /* TODO: notify UI about volume changes by other PulseAudio clients */
305     pa_cvolume_set(&sys->cvolume, sys->cvolume.channels, volume);
306     pa_sw_cvolume_multiply_scalar(&cvolume, &sys->cvolume, sys->base_volume);
307     assert(pa_cvolume_valid(&cvolume));
308
309     pa_threaded_mainloop_lock(mainloop);
310     op = pa_context_set_sink_input_volume(sys->context, idx, &cvolume, NULL, NULL);
311     if (likely(op != NULL))
312         pa_operation_unref(op);
313     op = pa_context_set_sink_input_mute(sys->context, idx, mute, NULL, NULL);
314     if (likely(op != NULL))
315         pa_operation_unref(op);
316     pa_threaded_mainloop_unlock(mainloop);
317
318     return 0;
319 }
320
321 /*****************************************************************************
322  * Open: open the audio device
323  *****************************************************************************/
324 static int Open(vlc_object_t *obj)
325 {
326     aout_instance_t *aout = (aout_instance_t *)obj;
327
328     /* Sample format specification */
329     struct pa_sample_spec ss;
330
331     switch(aout->output.output.i_format)
332     {
333         case VLC_CODEC_F64B:
334             aout->output.output.i_format = VLC_CODEC_F32B;
335         case VLC_CODEC_F32B:
336             ss.format = PA_SAMPLE_FLOAT32BE;
337             break;
338         case VLC_CODEC_F64L:
339             aout->output.output.i_format = VLC_CODEC_F32L;
340         case VLC_CODEC_F32L:
341             ss.format = PA_SAMPLE_FLOAT32LE;
342             break;
343         case VLC_CODEC_FI32:
344             aout->output.output.i_format = VLC_CODEC_FL32;
345             ss.format = PA_SAMPLE_FLOAT32NE;
346             break;
347         case VLC_CODEC_S32B:
348             ss.format = PA_SAMPLE_S32BE;
349             break;
350         case VLC_CODEC_S32L:
351             ss.format = PA_SAMPLE_S32LE;
352             break;
353         case VLC_CODEC_S24B:
354             ss.format = PA_SAMPLE_S24BE;
355             break;
356         case VLC_CODEC_S24L:
357             ss.format = PA_SAMPLE_S24LE;
358             break;
359         case VLC_CODEC_S16B:
360             ss.format = PA_SAMPLE_S16BE;
361             break;
362         case VLC_CODEC_S16L:
363             ss.format = PA_SAMPLE_S16LE;
364             break;
365         case VLC_CODEC_S8:
366             aout->output.output.i_format = VLC_CODEC_U8;
367         case VLC_CODEC_U8:
368             ss.format = PA_SAMPLE_U8;
369             break;
370         default:
371             if (HAVE_FPU)
372             {
373                 aout->output.output.i_format = VLC_CODEC_FL32;
374                 ss.format = PA_SAMPLE_FLOAT32NE;
375             }
376             else
377             {
378                 aout->output.output.i_format = VLC_CODEC_S16N;
379                 ss.format = PA_SAMPLE_S16NE;
380             }
381             break;
382     }
383
384     ss.rate = aout->output.output.i_rate;
385     ss.channels = aout_FormatNbChannels(&aout->output.output);
386     if (!pa_sample_spec_valid(&ss)) {
387         msg_Err(aout, "unsupported sample specification");
388         return VLC_EGENERIC;
389     }
390
391     /* Channel mapping (order defined in vlc_aout.h) */
392     struct pa_channel_map map;
393     map.channels = 0;
394
395     if (aout->output.output.i_physical_channels & AOUT_CHAN_LEFT)
396         map.map[map.channels++] = PA_CHANNEL_POSITION_FRONT_LEFT;
397     if (aout->output.output.i_physical_channels & AOUT_CHAN_RIGHT)
398         map.map[map.channels++] = PA_CHANNEL_POSITION_FRONT_RIGHT;
399     if (aout->output.output.i_physical_channels & AOUT_CHAN_MIDDLELEFT)
400         map.map[map.channels++] = PA_CHANNEL_POSITION_SIDE_LEFT;
401     if (aout->output.output.i_physical_channels & AOUT_CHAN_MIDDLERIGHT)
402         map.map[map.channels++] = PA_CHANNEL_POSITION_SIDE_RIGHT;
403     if (aout->output.output.i_physical_channels & AOUT_CHAN_REARLEFT)
404         map.map[map.channels++] = PA_CHANNEL_POSITION_REAR_LEFT;
405     if (aout->output.output.i_physical_channels & AOUT_CHAN_REARRIGHT)
406         map.map[map.channels++] = PA_CHANNEL_POSITION_REAR_RIGHT;
407     if (aout->output.output.i_physical_channels & AOUT_CHAN_REARCENTER)
408         map.map[map.channels++] = PA_CHANNEL_POSITION_REAR_CENTER;
409     if (aout->output.output.i_physical_channels & AOUT_CHAN_CENTER)
410     {
411         if (ss.channels == 1)
412             map.map[map.channels++] = PA_CHANNEL_POSITION_MONO;
413         else
414             map.map[map.channels++] = PA_CHANNEL_POSITION_FRONT_CENTER;
415     }
416     if (aout->output.output.i_physical_channels & AOUT_CHAN_LFE)
417         map.map[map.channels++] = PA_CHANNEL_POSITION_LFE;
418
419     for (unsigned i = 0; map.channels < ss.channels; i++) {
420         map.map[map.channels++] = PA_CHANNEL_POSITION_AUX0 + i;
421         msg_Warn(aout, "mapping channel %"PRIu8" to AUX%u", map.channels, i);
422     }
423
424     if (!pa_channel_map_valid(&map)) {
425         msg_Err(aout, "unsupported channel map");
426         return VLC_EGENERIC;
427     } else {
428         const char *name = pa_channel_map_to_pretty_name(&map);
429         msg_Dbg(aout, "using %s channel map", (name != NULL) ? name : "?");
430     }
431
432     /* Stream parameters */
433     const pa_stream_flags_t flags = PA_STREAM_INTERPOLATE_TIMING
434                                   | PA_STREAM_AUTO_TIMING_UPDATE
435                                   | PA_STREAM_ADJUST_LATENCY
436                                   | PA_STREAM_START_CORKED;
437
438     const uint32_t byterate = pa_bytes_per_second(&ss);
439     struct pa_buffer_attr attr;
440     /* no point in larger buffers on PA side than VLC */
441     attr.maxlength = -1;
442     attr.tlength = byterate * AOUT_MAX_ADVANCE_TIME / CLOCK_FREQ;
443     attr.prebuf = byterate * AOUT_MAX_PREPARE_TIME / CLOCK_FREQ;
444     attr.minreq = -1;
445     attr.fragsize = 0; /* not used for output */
446
447     /* Allocate structures */
448     aout_sys_t *sys = malloc(sizeof(*sys));
449     if (unlikely(sys == NULL))
450         return VLC_ENOMEM;
451     aout->output.p_sys = sys;
452     sys->context = NULL;
453     sys->stream = NULL;
454     //sys->byterate = byterate;
455
456     /* Channel volume */
457     sys->base_volume = PA_VOLUME_NORM;
458     pa_cvolume_set(&sys->cvolume, ss.channels, PA_VOLUME_NORM);
459
460     /* Allocate threaded main loop */
461     pa_threaded_mainloop *mainloop = pa_threaded_mainloop_new();
462     if (unlikely(mainloop == NULL)) {
463         free(sys);
464         return VLC_ENOMEM;
465     }
466     sys->mainloop = mainloop;
467
468     if (pa_threaded_mainloop_start(mainloop) < 0) {
469         pa_threaded_mainloop_free(mainloop);
470         free(sys);
471         return VLC_ENOMEM;
472     }
473     pa_threaded_mainloop_lock(mainloop);
474
475     /* Connect to PulseAudio server */
476     char *user_agent = var_InheritString(aout, "user-agent");
477     pa_context *ctx = pa_context_new(pa_threaded_mainloop_get_api(mainloop),
478                                      user_agent);
479     free(user_agent);
480     if (unlikely(ctx == NULL))
481         goto fail;
482     sys->context = ctx;
483
484     pa_context_set_state_callback(ctx, context_state_cb, mainloop);
485     if (pa_context_connect(ctx, NULL, 0, NULL) < 0
486      || context_wait(mainloop, ctx)) {
487         error(aout, "cannot connect to server", ctx);
488         goto fail;
489     }
490
491     /* Create a playback stream */
492     pa_stream *s = pa_stream_new(ctx, "audio stream", &ss, &map);
493     if (s == NULL) {
494         error(aout, "cannot create stream", ctx);
495         goto fail;
496     }
497     sys->stream = s;
498     pa_stream_set_state_callback(s, stream_state_cb, mainloop);
499     pa_stream_set_moved_callback(s, stream_moved_cb, aout);
500     pa_stream_set_overflow_callback(s, stream_overflow_cb, aout);
501     pa_stream_set_started_callback(s, stream_started_cb, aout);
502     pa_stream_set_suspended_callback(s, stream_suspended_cb, aout);
503     pa_stream_set_underflow_callback(s, stream_underflow_cb, aout);
504
505     if (pa_stream_connect_playback(s, NULL, &attr, flags, NULL, NULL) < 0
506      || stream_wait(mainloop, s)) {
507         error(aout, "cannot connect stream", ctx);
508         goto fail;
509     }
510     stream_moved_cb(s, aout);
511
512     const struct pa_buffer_attr *pba = pa_stream_get_buffer_attr(s);
513     msg_Dbg(aout, "using buffer metrics: maxlength=%u, tlength=%u, "
514             "prebuf=%u, minreq=%u",
515             pba->maxlength, pba->tlength, pba->prebuf, pba->minreq);
516
517     aout->output.i_nb_samples = pba->minreq / pa_frame_size(&ss);
518     pa_threaded_mainloop_unlock(mainloop);
519
520     aout->output.pf_play = Play;
521     aout->output.pf_volume_set = VolumeSet;
522     return VLC_SUCCESS;
523
524 fail:
525     pa_threaded_mainloop_unlock(mainloop);
526     Close(obj);
527     return VLC_EGENERIC;
528 }
529
530 /*****************************************************************************
531  * Close: close the audio device
532  *****************************************************************************/
533 static void Close (vlc_object_t *obj)
534 {
535     aout_instance_t *aout = (aout_instance_t *)obj;
536     aout_sys_t *sys = aout->output.p_sys;
537     pa_threaded_mainloop *mainloop = sys->mainloop;
538     pa_context *ctx = sys->context;
539     pa_stream *s = sys->stream;
540
541     pa_threaded_mainloop_lock(mainloop);
542     if (s != NULL) {
543         pa_operation *op;
544
545         op = pa_stream_flush(s, NULL, NULL);
546         if (op != NULL)
547             pa_operation_unref(op);
548         op = pa_stream_drain(s, NULL, NULL);
549         if (op != NULL)
550             pa_operation_unref(op);
551         pa_stream_disconnect(s);
552         pa_stream_unref(s);
553     }
554     if (ctx != NULL)
555         pa_context_unref(ctx);
556     pa_threaded_mainloop_unlock(mainloop);
557     pa_threaded_mainloop_free(mainloop);
558     free(sys);
559 }