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>
40 /*****************************************************************************
41 * aout_sys_t: Pulseaudio output method descriptor
42 *****************************************************************************
43 * This structure is part of the audio output thread descriptor.
44 * It describes the specific properties of an audio device.
45 *****************************************************************************/
48 /** PulseAudio playback stream object */
49 struct pa_stream *stream;
51 /** PulseAudio connection context */
52 struct pa_context *context;
54 /** Main event loop object */
55 struct pa_threaded_mainloop *mainloop;
62 #define PULSE_CLIENT_NAME N_("VLC media player")
65 #define PULSE_DEBUG( ...) \
66 msg_Dbg( p_aout, __VA_ARGS__ )
68 #define PULSE_DEBUG( ...) \
73 #define CHECK_DEAD_GOTO(label) do { \
74 if (!p_sys->context || pa_context_get_state(p_sys->context) != PA_CONTEXT_READY || \
75 !p_sys->stream || pa_stream_get_state(p_sys->stream) != PA_STREAM_READY) { \
76 msg_Err(p_aout, "Connection died: %s", p_sys->context ? pa_strerror(pa_context_errno(p_sys->context)) : "NULL"); \
81 /*****************************************************************************
83 *****************************************************************************/
84 static int Open ( vlc_object_t * );
85 static void Close ( vlc_object_t * );
86 static void Play ( aout_instance_t * );
88 static void context_state_cb(pa_context *c, void *userdata);
89 static void stream_state_cb(pa_stream *s, void * userdata);
90 static void stream_request_cb(pa_stream *s, size_t length, void *userdata);
91 static void stream_latency_update_cb(pa_stream *s, void *userdata);
92 static void success_cb(pa_stream *s, int sucess, void *userdata);
93 static void uninit(aout_instance_t *p_aout);
95 /*****************************************************************************
97 *****************************************************************************/
99 set_shortname( "Pulse Audio" )
100 set_description( N_("Pulseaudio audio output") )
101 set_capability( "audio output", 160 )
102 set_category( CAT_AUDIO )
103 set_subcategory( SUBCAT_AUDIO_AOUT )
104 add_shortcut( "pulseaudio" )
106 set_callbacks( Open, Close )
107 linked_with_a_crap_library_which_uses_atexit()
110 /*****************************************************************************
111 * Open: open the audio device
112 *****************************************************************************/
113 static int Open ( vlc_object_t *p_this )
115 aout_instance_t *p_aout = (aout_instance_t *)p_this;
116 struct aout_sys_t * p_sys;
117 struct pa_sample_spec ss;
118 const struct pa_buffer_attr *buffer_attr;
119 struct pa_buffer_attr a;
120 struct pa_channel_map map;
122 /* Allocate structures */
123 p_aout->output.p_sys = p_sys = calloc( 1, sizeof( aout_sys_t ) );
127 PULSE_DEBUG( "Pulse start initialization");
129 ss.channels = aout_FormatNbChannels( &p_aout->output.output ); /* Get the input stream channel count */
131 /* Setup the pulse audio stream based on the input stream count */
135 p_aout->output.output.i_physical_channels
136 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
137 | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT
138 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
142 p_aout->output.output.i_physical_channels
143 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
144 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
149 p_aout->output.output.i_physical_channels
150 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
151 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
155 p_aout->output.output.i_physical_channels
156 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
160 p_aout->output.output.i_physical_channels = AOUT_CHAN_CENTER;
164 msg_Err(p_aout,"Invalid number of channels");
168 /* Add a quick command line info message */
169 msg_Dbg(p_aout, "%d audio channels", ss.channels);
171 ss.rate = p_aout->output.output.i_rate;
174 ss.format = PA_SAMPLE_FLOAT32NE;
175 p_aout->output.output.i_format = VLC_CODEC_FL32;
179 ss.format = PA_SAMPLE_S16NE;
180 p_aout->output.output.i_format = VLC_CODEC_S16N;
183 if (!pa_sample_spec_valid(&ss)) {
184 msg_Err(p_aout,"Invalid sample spec");
188 /* Reduce overall latency to 200mS to reduce audible clicks
189 * Also pulse minreq and internal buffers are now 20mS which reduces resampling
191 a.tlength = pa_bytes_per_second(&ss)/5;
192 a.maxlength = a.tlength * 2;
193 a.prebuf = a.tlength / 2;
194 a.minreq = a.tlength / 10;
196 /* Buffer size is 20mS */
197 p_sys->buffer_size = a.minreq;
199 /* Initialise the speaker map setup above */
200 pa_channel_map_init_auto(&map, ss.channels, PA_CHANNEL_MAP_ALSA);
202 if (!(p_sys->mainloop = pa_threaded_mainloop_new())) {
203 msg_Err(p_aout, "Failed to allocate main loop");
207 if (!(p_sys->context = pa_context_new(pa_threaded_mainloop_get_api(p_sys->mainloop), _( PULSE_CLIENT_NAME )))) {
208 msg_Err(p_aout, "Failed to allocate context");
212 pa_context_set_state_callback(p_sys->context, context_state_cb, p_aout);
214 PULSE_DEBUG( "Pulse before context connect");
216 if (pa_context_connect(p_sys->context, NULL, 0, NULL) < 0) {
217 msg_Err(p_aout, "Failed to connect to server: %s", pa_strerror(pa_context_errno(p_sys->context)));
221 PULSE_DEBUG( "Pulse after context connect");
223 pa_threaded_mainloop_lock(p_sys->mainloop);
225 if (pa_threaded_mainloop_start(p_sys->mainloop) < 0) {
226 msg_Err(p_aout, "Failed to start main loop");
227 goto unlock_and_fail;
230 msg_Dbg(p_aout, "Pulse mainloop started");
232 /* Wait until the context is ready */
233 pa_threaded_mainloop_wait(p_sys->mainloop);
235 if (pa_context_get_state(p_sys->context) != PA_CONTEXT_READY) {
236 msg_Dbg(p_aout, "Failed to connect to server: %s", pa_strerror(pa_context_errno(p_sys->context)));
237 goto unlock_and_fail;
240 if (!(p_sys->stream = pa_stream_new(p_sys->context, "audio stream", &ss, &map))) {
241 msg_Err(p_aout, "Failed to create stream: %s", pa_strerror(pa_context_errno(p_sys->context)));
242 goto unlock_and_fail;
245 PULSE_DEBUG( "Pulse after new stream");
247 pa_stream_set_state_callback(p_sys->stream, stream_state_cb, p_aout);
248 pa_stream_set_write_callback(p_sys->stream, stream_request_cb, p_aout);
249 pa_stream_set_latency_update_callback(p_sys->stream, stream_latency_update_cb, p_aout);
251 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) {
252 msg_Err(p_aout, "Failed to connect stream: %s", pa_strerror(pa_context_errno(p_sys->context)));
253 goto unlock_and_fail;
256 PULSE_DEBUG("Pulse stream connect");
258 /* Wait until the stream is ready */
259 pa_threaded_mainloop_wait(p_sys->mainloop);
261 msg_Dbg(p_aout,"Pulse stream connected");
263 if (pa_stream_get_state(p_sys->stream) != PA_STREAM_READY) {
264 msg_Err(p_aout, "Failed to connect to server: %s", pa_strerror(pa_context_errno(p_sys->context)));
265 goto unlock_and_fail;
269 PULSE_DEBUG("Pulse after stream get status");
271 pa_threaded_mainloop_unlock(p_sys->mainloop);
273 buffer_attr = pa_stream_get_buffer_attr(p_sys->stream);
274 p_aout->output.i_nb_samples = buffer_attr->minreq / pa_frame_size(&ss);
275 p_aout->output.pf_play = Play;
276 aout_VolumeSoftInit(p_aout);
277 msg_Dbg(p_aout, "Pulse initialized successfully");
279 char cmt[PA_CHANNEL_MAP_SNPRINT_MAX], sst[PA_SAMPLE_SPEC_SNPRINT_MAX];
281 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);
282 msg_Dbg(p_aout, "Using sample spec '%s', channel map '%s'.",
283 pa_sample_spec_snprint(sst, sizeof(sst), pa_stream_get_sample_spec(p_sys->stream)),
284 pa_channel_map_snprint(cmt, sizeof(cmt), pa_stream_get_channel_map(p_sys->stream)));
286 msg_Dbg(p_aout, "Connected to device %s (%u, %ssuspended).",
287 pa_stream_get_device_name(p_sys->stream),
288 pa_stream_get_device_index(p_sys->stream),
289 pa_stream_is_suspended(p_sys->stream) ? "" : "not ");
295 msg_Dbg(p_aout, "Pulse initialization unlock and fail");
298 pa_threaded_mainloop_unlock(p_sys->mainloop);
300 msg_Dbg(p_aout, "Pulse initialization failed");
305 /*****************************************************************************
306 * Play: play a sound samples buffer
307 *****************************************************************************/
308 static void Play( aout_instance_t * p_aout )
310 struct aout_sys_t * p_sys = (struct aout_sys_t *) p_aout->output.p_sys;
315 msg_Dbg(p_aout, "Pulse stream started");
317 aout_FifoFirstDate( p_aout, &p_aout->output.fifo );
320 pa_threaded_mainloop_lock(p_sys->mainloop);
321 if((o = pa_stream_flush(p_sys->stream, success_cb, p_aout))){
322 pa_operation_unref(o);
324 pa_threaded_mainloop_unlock(p_sys->mainloop);
326 pa_threaded_mainloop_signal(p_sys->mainloop, 0);
330 /*****************************************************************************
331 * Close: close the audio device
332 *****************************************************************************/
333 static void Close ( vlc_object_t *p_this )
335 aout_instance_t *p_aout = (aout_instance_t *)p_this;
336 struct aout_sys_t * p_sys = p_aout->output.p_sys;
338 msg_Dbg(p_aout, "Pulse Close");
342 pa_threaded_mainloop_lock(p_sys->mainloop);
343 pa_stream_set_write_callback(p_sys->stream, NULL, NULL);
345 if((o = pa_stream_drain(p_sys->stream, success_cb, p_aout))){
346 while (pa_operation_get_state(o) != PA_OPERATION_DONE) {
347 CHECK_DEAD_GOTO(fail);
348 pa_threaded_mainloop_wait(p_sys->mainloop);
353 pa_operation_unref(o);
356 pa_threaded_mainloop_unlock(p_sys->mainloop);
361 static void uninit(aout_instance_t *p_aout){
362 struct aout_sys_t * p_sys = p_aout->output.p_sys;
365 pa_threaded_mainloop_stop(p_sys->mainloop);
368 pa_stream_disconnect(p_sys->stream);
369 pa_stream_unref(p_sys->stream);
370 p_sys->stream = NULL;
373 if (p_sys->context) {
374 pa_context_disconnect(p_sys->context);
375 pa_context_unref(p_sys->context);
376 p_sys->context = NULL;
379 if (p_sys->mainloop) {
380 pa_threaded_mainloop_free(p_sys->mainloop);
381 p_sys->mainloop = NULL;
385 p_aout->output.p_sys = NULL;
388 static void context_state_cb(pa_context *c, void *userdata) {
389 aout_instance_t *p_aout = (aout_instance_t *)userdata;
390 struct aout_sys_t * p_sys = (struct aout_sys_t *) p_aout->output.p_sys;
394 PULSE_DEBUG( "Pulse context state changed");
396 switch (pa_context_get_state(c)) {
397 case PA_CONTEXT_READY:
398 case PA_CONTEXT_TERMINATED:
399 case PA_CONTEXT_FAILED:
400 PULSE_DEBUG( "Pulse context state changed signal");
401 pa_threaded_mainloop_signal(p_sys->mainloop, 0);
404 case PA_CONTEXT_UNCONNECTED:
405 case PA_CONTEXT_CONNECTING:
406 case PA_CONTEXT_AUTHORIZING:
407 case PA_CONTEXT_SETTING_NAME:
408 PULSE_DEBUG( "Pulse context state changed no signal");
413 static void stream_state_cb(pa_stream *s, void * userdata) {
414 aout_instance_t *p_aout = (aout_instance_t *)userdata;
415 struct aout_sys_t * p_sys = (struct aout_sys_t *) p_aout->output.p_sys;
419 PULSE_DEBUG( "Pulse stream state changed");
421 switch (pa_stream_get_state(s)) {
423 case PA_STREAM_READY:
424 case PA_STREAM_FAILED:
425 case PA_STREAM_TERMINATED:
426 pa_threaded_mainloop_signal(p_sys->mainloop, 0);
429 case PA_STREAM_UNCONNECTED:
430 case PA_STREAM_CREATING:
435 static void stream_request_cb(pa_stream *s, size_t length, void *userdata) {
437 aout_instance_t *p_aout = (aout_instance_t *)userdata;
438 struct aout_sys_t * p_sys = (struct aout_sys_t *) p_aout->output.p_sys;
444 size_t buffer_size = p_sys->buffer_size;
446 PULSE_DEBUG( "Pulse stream request %d", length);
449 aout_buffer_t * p_buffer = NULL;
453 if(pa_stream_get_latency(p_sys->stream, &latency, &negative)<0){
454 if (pa_context_errno(p_sys->context) != PA_ERR_NODATA) {
455 msg_Err(p_aout, "pa_stream_get_latency() failed: %s", pa_strerror(pa_context_errno(p_sys->context)));
461 PULSE_DEBUG( "Pulse stream request latency=%"PRId64"", latency);
462 next_date = mdate() + latency;
464 if(p_sys->start_date < next_date + AOUT_PTS_TOLERANCE ){
465 p_buffer = aout_OutputNextBuffer( p_aout, next_date, 0);
469 if ( p_buffer != NULL )
471 PULSE_DEBUG( "Pulse stream request write buffer %d", p_buffer->i_buffer);
472 pa_stream_write(p_sys->stream, p_buffer->p_buffer, p_buffer->i_buffer, NULL, 0, PA_SEEK_RELATIVE);
473 length -= p_buffer->i_buffer;
474 aout_BufferFree( p_buffer );
478 PULSE_DEBUG( "Pulse stream request write zeroes");
479 void *data = pa_xmalloc(buffer_size);
480 bzero(data, buffer_size);
481 pa_stream_write(p_sys->stream, data, buffer_size, pa_xfree, 0, PA_SEEK_RELATIVE);
482 length -= buffer_size;
484 }while(length > buffer_size);
486 pa_threaded_mainloop_signal(p_sys->mainloop, 0);
489 static void stream_latency_update_cb(pa_stream *s, void *userdata) {
491 aout_instance_t *p_aout = (aout_instance_t *)userdata;
492 struct aout_sys_t * p_sys = (struct aout_sys_t *) p_aout->output.p_sys;
496 PULSE_DEBUG( "Pulse stream latency update");
498 pa_threaded_mainloop_signal(p_sys->mainloop, 0);
501 static void success_cb(pa_stream *s, int sucess, void *userdata)
504 aout_instance_t *p_aout = (aout_instance_t *)userdata;
505 struct aout_sys_t * p_sys = (struct aout_sys_t *) p_aout->output.p_sys;
511 pa_threaded_mainloop_signal(p_sys->mainloop, 0);