1 /*****************************************************************************
2 * pulse.c : Pulseaudio output plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2008 the VideoLAN team
6 * Authors: Martin Hamrle <hamrle @ post . cz>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
23 /*****************************************************************************
25 *****************************************************************************/
30 #include <vlc_common.h>
31 #include <vlc_plugin.h>
36 #include <pulse/pulseaudio.h>
37 #ifdef HAVE_X11_XLIB_H
38 # include <X11/Xlib.h>
43 /*****************************************************************************
44 * aout_sys_t: Pulseaudio output method descriptor
45 *****************************************************************************
46 * This structure is part of the audio output thread descriptor.
47 * It describes the specific properties of an audio device.
48 *****************************************************************************/
51 /** PulseAudio playback stream object */
52 struct pa_stream *stream;
54 /** PulseAudio connection context */
55 struct pa_context *context;
57 /** Main event loop object */
58 struct pa_threaded_mainloop *mainloop;
65 #define PULSE_CLIENT_NAME N_("VLC media player")
68 #define PULSE_DEBUG( ...) \
69 msg_Dbg( p_aout, __VA_ARGS__ )
71 #define PULSE_DEBUG( ...) \
76 #define CHECK_DEAD_GOTO(label) do { \
77 if (!p_sys->context || pa_context_get_state(p_sys->context) != PA_CONTEXT_READY || \
78 !p_sys->stream || pa_stream_get_state(p_sys->stream) != PA_STREAM_READY) { \
79 msg_Err(p_aout, "Connection died: %s", p_sys->context ? pa_strerror(pa_context_errno(p_sys->context)) : "NULL"); \
84 /*****************************************************************************
86 *****************************************************************************/
87 static int Open ( vlc_object_t * );
88 static void Close ( vlc_object_t * );
89 static void Play ( aout_instance_t * );
91 static void context_state_cb(pa_context *c, void *userdata);
92 static void stream_state_cb(pa_stream *s, void * userdata);
93 static void stream_request_cb(pa_stream *s, size_t length, void *userdata);
94 static void stream_latency_update_cb(pa_stream *s, void *userdata);
95 static void success_cb(pa_stream *s, int sucess, void *userdata);
96 static void uninit(aout_instance_t *p_aout);
98 /*****************************************************************************
100 *****************************************************************************/
102 set_shortname( "PulseAudio" )
103 set_description( N_("Pulseaudio audio output") )
104 set_capability( "audio output", 160 )
105 set_category( CAT_AUDIO )
106 set_subcategory( SUBCAT_AUDIO_AOUT )
107 add_shortcut( "pulseaudio" )
109 set_callbacks( Open, Close )
110 linked_with_a_crap_library_which_uses_atexit()
113 /*****************************************************************************
114 * Open: open the audio device
115 *****************************************************************************/
116 static int Open ( vlc_object_t *p_this )
118 aout_instance_t *p_aout = (aout_instance_t *)p_this;
119 struct aout_sys_t * p_sys;
120 struct pa_sample_spec ss;
121 const struct pa_buffer_attr *buffer_attr;
122 struct pa_buffer_attr a;
123 struct pa_channel_map map;
125 #ifdef HAVE_X11_XLIB_H
126 if( !XInitThreads() )
129 /* Allocate structures */
130 p_aout->output.p_sys = p_sys = calloc( 1, sizeof( aout_sys_t ) );
134 PULSE_DEBUG( "Pulse start initialization");
136 ss.channels = aout_FormatNbChannels( &p_aout->output.output ); /* Get the input stream channel count */
138 /* Setup the pulse audio stream based on the input stream count */
142 p_aout->output.output.i_physical_channels
143 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
144 | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT
145 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
149 p_aout->output.output.i_physical_channels
150 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
151 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
156 p_aout->output.output.i_physical_channels
157 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
158 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
162 p_aout->output.output.i_physical_channels
163 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
167 p_aout->output.output.i_physical_channels = AOUT_CHAN_CENTER;
171 msg_Err(p_aout,"Invalid number of channels");
175 /* Add a quick command line info message */
176 msg_Dbg(p_aout, "%d audio channels", ss.channels);
178 ss.rate = p_aout->output.output.i_rate;
181 ss.format = PA_SAMPLE_FLOAT32NE;
182 p_aout->output.output.i_format = VLC_CODEC_FL32;
186 ss.format = PA_SAMPLE_S16NE;
187 p_aout->output.output.i_format = VLC_CODEC_S16N;
190 if (!pa_sample_spec_valid(&ss)) {
191 msg_Err(p_aout,"Invalid sample spec");
195 /* Reduce overall latency to 200mS to reduce audible clicks
196 * Also pulse minreq and internal buffers are now 20mS which reduces resampling
198 a.tlength = pa_bytes_per_second(&ss)/5;
199 a.maxlength = a.tlength * 2;
200 a.prebuf = a.tlength / 2;
201 a.minreq = a.tlength / 10;
203 /* Buffer size is 20mS */
204 p_sys->buffer_size = a.minreq;
206 /* Initialise the speaker map setup above */
207 pa_channel_map_init_auto(&map, ss.channels, PA_CHANNEL_MAP_ALSA);
209 if (!(p_sys->mainloop = pa_threaded_mainloop_new())) {
210 msg_Err(p_aout, "Failed to allocate main loop");
214 if (!(p_sys->context = pa_context_new(pa_threaded_mainloop_get_api(p_sys->mainloop), _( PULSE_CLIENT_NAME )))) {
215 msg_Err(p_aout, "Failed to allocate context");
219 pa_context_set_state_callback(p_sys->context, context_state_cb, p_aout);
221 PULSE_DEBUG( "Pulse before context connect");
223 if (pa_context_connect(p_sys->context, NULL, 0, NULL) < 0) {
224 msg_Err(p_aout, "Failed to connect to server: %s", pa_strerror(pa_context_errno(p_sys->context)));
228 PULSE_DEBUG( "Pulse after context connect");
230 pa_threaded_mainloop_lock(p_sys->mainloop);
232 if (pa_threaded_mainloop_start(p_sys->mainloop) < 0) {
233 msg_Err(p_aout, "Failed to start main loop");
234 goto unlock_and_fail;
237 msg_Dbg(p_aout, "Pulse mainloop started");
239 /* Wait until the context is ready */
240 pa_threaded_mainloop_wait(p_sys->mainloop);
242 if (pa_context_get_state(p_sys->context) != PA_CONTEXT_READY) {
243 msg_Dbg(p_aout, "Failed to connect to server: %s", pa_strerror(pa_context_errno(p_sys->context)));
244 goto unlock_and_fail;
247 if (!(p_sys->stream = pa_stream_new(p_sys->context, "audio stream", &ss, &map))) {
248 msg_Err(p_aout, "Failed to create stream: %s", pa_strerror(pa_context_errno(p_sys->context)));
249 goto unlock_and_fail;
252 PULSE_DEBUG( "Pulse after new stream");
254 pa_stream_set_state_callback(p_sys->stream, stream_state_cb, p_aout);
255 pa_stream_set_write_callback(p_sys->stream, stream_request_cb, p_aout);
256 pa_stream_set_latency_update_callback(p_sys->stream, stream_latency_update_cb, p_aout);
258 if (pa_stream_connect_playback(p_sys->stream, NULL, &a, PA_STREAM_INTERPOLATE_TIMING|PA_STREAM_AUTO_TIMING_UPDATE|PA_STREAM_ADJUST_LATENCY, NULL, NULL) < 0) {
259 msg_Err(p_aout, "Failed to connect stream: %s", pa_strerror(pa_context_errno(p_sys->context)));
260 goto unlock_and_fail;
263 PULSE_DEBUG("Pulse stream connect");
265 /* Wait until the stream is ready */
266 pa_threaded_mainloop_wait(p_sys->mainloop);
268 msg_Dbg(p_aout,"Pulse stream connected");
270 if (pa_stream_get_state(p_sys->stream) != PA_STREAM_READY) {
271 msg_Err(p_aout, "Failed to connect to server: %s", pa_strerror(pa_context_errno(p_sys->context)));
272 goto unlock_and_fail;
276 PULSE_DEBUG("Pulse after stream get status");
278 pa_threaded_mainloop_unlock(p_sys->mainloop);
280 buffer_attr = pa_stream_get_buffer_attr(p_sys->stream);
281 p_aout->output.i_nb_samples = buffer_attr->minreq / pa_frame_size(&ss);
282 p_aout->output.pf_play = Play;
283 aout_VolumeSoftInit(p_aout);
284 msg_Dbg(p_aout, "Pulse initialized successfully");
286 char cmt[PA_CHANNEL_MAP_SNPRINT_MAX], sst[PA_SAMPLE_SPEC_SNPRINT_MAX];
288 msg_Dbg(p_aout, "Buffer metrics: maxlength=%u, tlength=%u, prebuf=%u, minreq=%u", buffer_attr->maxlength, buffer_attr->tlength, buffer_attr->prebuf, buffer_attr->minreq);
289 msg_Dbg(p_aout, "Using sample spec '%s', channel map '%s'.",
290 pa_sample_spec_snprint(sst, sizeof(sst), pa_stream_get_sample_spec(p_sys->stream)),
291 pa_channel_map_snprint(cmt, sizeof(cmt), pa_stream_get_channel_map(p_sys->stream)));
293 msg_Dbg(p_aout, "Connected to device %s (%u, %ssuspended).",
294 pa_stream_get_device_name(p_sys->stream),
295 pa_stream_get_device_index(p_sys->stream),
296 pa_stream_is_suspended(p_sys->stream) ? "" : "not ");
302 msg_Dbg(p_aout, "Pulse initialization unlock and fail");
305 pa_threaded_mainloop_unlock(p_sys->mainloop);
307 msg_Dbg(p_aout, "Pulse initialization failed");
312 /*****************************************************************************
313 * Play: play a sound samples buffer
314 *****************************************************************************/
315 static void Play( aout_instance_t * p_aout )
317 struct aout_sys_t * p_sys = (struct aout_sys_t *) p_aout->output.p_sys;
322 msg_Dbg(p_aout, "Pulse stream started");
324 aout_FifoFirstDate( p_aout, &p_aout->output.fifo );
327 pa_threaded_mainloop_lock(p_sys->mainloop);
328 if((o = pa_stream_flush(p_sys->stream, success_cb, p_aout))){
329 pa_operation_unref(o);
331 pa_threaded_mainloop_unlock(p_sys->mainloop);
333 pa_threaded_mainloop_signal(p_sys->mainloop, 0);
337 /*****************************************************************************
338 * Close: close the audio device
339 *****************************************************************************/
340 static void Close ( vlc_object_t *p_this )
342 aout_instance_t *p_aout = (aout_instance_t *)p_this;
343 struct aout_sys_t * p_sys = p_aout->output.p_sys;
345 msg_Dbg(p_aout, "Pulse Close");
348 pa_threaded_mainloop_lock(p_sys->mainloop);
349 pa_stream_set_write_callback(p_sys->stream, NULL, NULL);
351 /* I didn't find any explanation why we need to do pa_stream_drain on close
352 * as we don't really care if we lose 20ms buffer in this point anyway?
353 * And disabling this speeds up closing pulseaudio quite a lot (atleast for me).
356 pa_operation *o = pa_stream_drain(p_sys->stream, success_cb, p_aout);
358 while (pa_operation_get_state(o) != PA_OPERATION_DONE) {
359 CHECK_DEAD_GOTO(fail);
360 pa_threaded_mainloop_wait(p_sys->mainloop);
365 pa_operation_unref(o);
368 pa_threaded_mainloop_unlock(p_sys->mainloop);
373 static void uninit(aout_instance_t *p_aout){
374 struct aout_sys_t * p_sys = p_aout->output.p_sys;
377 pa_threaded_mainloop_stop(p_sys->mainloop);
380 pa_stream_disconnect(p_sys->stream);
381 pa_stream_unref(p_sys->stream);
382 p_sys->stream = NULL;
385 if (p_sys->context) {
386 pa_context_disconnect(p_sys->context);
387 pa_context_unref(p_sys->context);
388 p_sys->context = NULL;
391 if (p_sys->mainloop) {
392 pa_threaded_mainloop_free(p_sys->mainloop);
393 p_sys->mainloop = NULL;
397 p_aout->output.p_sys = NULL;
400 static void context_state_cb(pa_context *c, void *userdata) {
401 aout_instance_t *p_aout = (aout_instance_t *)userdata;
402 struct aout_sys_t * p_sys = (struct aout_sys_t *) p_aout->output.p_sys;
406 PULSE_DEBUG( "Pulse context state changed");
408 switch (pa_context_get_state(c)) {
409 case PA_CONTEXT_READY:
410 case PA_CONTEXT_TERMINATED:
411 case PA_CONTEXT_FAILED:
412 PULSE_DEBUG( "Pulse context state changed signal");
413 pa_threaded_mainloop_signal(p_sys->mainloop, 0);
416 case PA_CONTEXT_UNCONNECTED:
417 case PA_CONTEXT_CONNECTING:
418 case PA_CONTEXT_AUTHORIZING:
419 case PA_CONTEXT_SETTING_NAME:
420 PULSE_DEBUG( "Pulse context state changed no signal");
425 static void stream_state_cb(pa_stream *s, void * userdata) {
426 aout_instance_t *p_aout = (aout_instance_t *)userdata;
427 struct aout_sys_t * p_sys = (struct aout_sys_t *) p_aout->output.p_sys;
431 PULSE_DEBUG( "Pulse stream state changed");
433 switch (pa_stream_get_state(s)) {
435 case PA_STREAM_READY:
436 case PA_STREAM_FAILED:
437 case PA_STREAM_TERMINATED:
438 pa_threaded_mainloop_signal(p_sys->mainloop, 0);
441 case PA_STREAM_UNCONNECTED:
442 case PA_STREAM_CREATING:
447 static void stream_request_cb(pa_stream *s, size_t length, void *userdata) {
449 aout_instance_t *p_aout = (aout_instance_t *)userdata;
450 struct aout_sys_t * p_sys = (struct aout_sys_t *) p_aout->output.p_sys;
456 size_t buffer_size = p_sys->buffer_size;
458 PULSE_DEBUG( "Pulse stream request %d", length);
461 aout_buffer_t * p_buffer = NULL;
465 if(pa_stream_get_latency(p_sys->stream, &latency, &negative)<0){
466 if (pa_context_errno(p_sys->context) != PA_ERR_NODATA) {
467 msg_Err(p_aout, "pa_stream_get_latency() failed: %s", pa_strerror(pa_context_errno(p_sys->context)));
473 PULSE_DEBUG( "Pulse stream request latency=%"PRId64"", latency);
474 next_date = mdate() + latency;
476 if(p_sys->start_date < next_date + AOUT_PTS_TOLERANCE ){
477 p_buffer = aout_OutputNextBuffer( p_aout, next_date, 0);
481 if ( p_buffer != NULL )
483 PULSE_DEBUG( "Pulse stream request write buffer %d", p_buffer->i_buffer);
484 pa_stream_write(p_sys->stream, p_buffer->p_buffer, p_buffer->i_buffer, NULL, 0, PA_SEEK_RELATIVE);
485 length -= p_buffer->i_buffer;
486 aout_BufferFree( p_buffer );
490 PULSE_DEBUG( "Pulse stream request write zeroes");
491 void *data = pa_xmalloc(buffer_size);
492 bzero(data, buffer_size);
493 pa_stream_write(p_sys->stream, data, buffer_size, pa_xfree, 0, PA_SEEK_RELATIVE);
494 length -= buffer_size;
496 }while(length > buffer_size);
498 pa_threaded_mainloop_signal(p_sys->mainloop, 0);
501 static void stream_latency_update_cb(pa_stream *s, void *userdata) {
503 aout_instance_t *p_aout = (aout_instance_t *)userdata;
504 struct aout_sys_t * p_sys = (struct aout_sys_t *) p_aout->output.p_sys;
508 PULSE_DEBUG( "Pulse stream latency update");
510 pa_threaded_mainloop_signal(p_sys->mainloop, 0);
513 static void success_cb(pa_stream *s, int sucess, void *userdata)
516 aout_instance_t *p_aout = (aout_instance_t *)userdata;
517 struct aout_sys_t * p_sys = (struct aout_sys_t *) p_aout->output.p_sys;
523 pa_threaded_mainloop_signal(p_sys->mainloop, 0);