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>
35 #include <pulse/pulseaudio.h>
39 /*****************************************************************************
40 * aout_sys_t: Pulseaudio output method descriptor
41 *****************************************************************************
42 * This structure is part of the audio output thread descriptor.
43 * It describes the specific properties of an audio device.
44 *****************************************************************************/
47 /** PulseAudio playback stream object */
48 struct pa_stream *stream;
50 /** PulseAudio connection context */
51 struct pa_context *context;
53 /** Main event loop object */
54 struct pa_threaded_mainloop *mainloop;
61 #define PULSE_CLIENT_NAME N_("VLC media player")
64 #define PULSE_DEBUG( ...) \
65 msg_Dbg( p_aout, __VA_ARGS__ )
67 #define PULSE_DEBUG( ...) \
72 #define CHECK_DEAD_GOTO(label) do { \
73 if (!p_sys->context || pa_context_get_state(p_sys->context) != PA_CONTEXT_READY || \
74 !p_sys->stream || pa_stream_get_state(p_sys->stream) != PA_STREAM_READY) { \
75 msg_Err(p_aout, "Connection died: %s", p_sys->context ? pa_strerror(pa_context_errno(p_sys->context)) : "NULL"); \
80 /*****************************************************************************
82 *****************************************************************************/
83 static int Open ( vlc_object_t * );
84 static void Close ( vlc_object_t * );
85 static void Play ( aout_instance_t * );
87 static void context_state_cb(pa_context *c, void *userdata);
88 static void stream_state_cb(pa_stream *s, void * userdata);
89 static void stream_request_cb(pa_stream *s, size_t length, void *userdata);
90 static void stream_latency_update_cb(pa_stream *s, void *userdata);
91 static void success_cb(pa_stream *s, int sucess, void *userdata);
92 static void uninit(aout_instance_t *p_aout);
94 /*****************************************************************************
96 *****************************************************************************/
98 set_shortname( "Pulse Audio" )
99 set_description( N_("Pulseaudio audio output") )
100 set_capability( "audio output", 40 )
101 set_category( CAT_AUDIO )
102 set_subcategory( SUBCAT_AUDIO_AOUT )
103 add_shortcut( "pulseaudio" )
105 set_callbacks( Open, Close )
106 linked_with_a_crap_library_which_uses_atexit()
109 /*****************************************************************************
110 * Open: open the audio device
111 *****************************************************************************/
112 static int Open ( vlc_object_t *p_this )
114 aout_instance_t *p_aout = (aout_instance_t *)p_this;
115 struct aout_sys_t * p_sys;
116 struct pa_sample_spec ss;
117 const struct pa_buffer_attr *buffer_attr;
118 struct pa_buffer_attr a;
119 struct pa_channel_map map;
121 /* Allocate structures */
122 p_aout->output.p_sys = p_sys = calloc( 1, sizeof( aout_sys_t ) );
126 PULSE_DEBUG( "Pulse start initialization");
128 ss.channels = aout_FormatNbChannels( &p_aout->output.output ); /* Get the input stream channel count */
130 /* Setup the pulse audio stream based on the input stream count */
134 p_aout->output.output.i_physical_channels
135 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
136 | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT
137 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
141 p_aout->output.output.i_physical_channels
142 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
143 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
148 p_aout->output.output.i_physical_channels
149 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
150 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
154 p_aout->output.output.i_physical_channels
155 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
159 p_aout->output.output.i_physical_channels = AOUT_CHAN_CENTER;
163 msg_Err(p_aout,"Invalid number of channels");
167 /* Add a quick command line info message */
168 msg_Info(p_aout, "No. of Audio Channels: %d", ss.channels);
170 ss.rate = p_aout->output.output.i_rate;
171 ss.format = PA_SAMPLE_FLOAT32NE;
172 p_aout->output.output.i_format = VLC_FOURCC('f','l','3','2');
174 if (!pa_sample_spec_valid(&ss)) {
175 msg_Err(p_aout,"Invalid sample spec");
179 a.maxlength = pa_bytes_per_second(&ss)/4/pa_frame_size(&ss);
180 a.tlength = a.maxlength*9/10;
181 a.prebuf = a.tlength/2;
182 a.minreq = a.tlength/10;
184 a.maxlength *= pa_frame_size(&ss);
185 a.tlength *= pa_frame_size(&ss);
186 a.prebuf *= pa_frame_size(&ss);
187 a.minreq *= pa_frame_size(&ss);
189 p_sys->buffer_size = a.minreq;
191 /* Initialise the speaker map setup above */
192 pa_channel_map_init_auto(&map, ss.channels, PA_CHANNEL_MAP_ALSA);
194 if (!(p_sys->mainloop = pa_threaded_mainloop_new())) {
195 msg_Err(p_aout, "Failed to allocate main loop");
199 if (!(p_sys->context = pa_context_new(pa_threaded_mainloop_get_api(p_sys->mainloop), _( PULSE_CLIENT_NAME )))) {
200 msg_Err(p_aout, "Failed to allocate context");
204 pa_context_set_state_callback(p_sys->context, context_state_cb, p_aout);
206 PULSE_DEBUG( "Pulse before context connect");
208 if (pa_context_connect(p_sys->context, NULL, 0, NULL) < 0) {
209 msg_Err(p_aout, "Failed to connect to server: %s", pa_strerror(pa_context_errno(p_sys->context)));
213 PULSE_DEBUG( "Pulse after context connect");
215 pa_threaded_mainloop_lock(p_sys->mainloop);
217 if (pa_threaded_mainloop_start(p_sys->mainloop) < 0) {
218 msg_Err(p_aout, "Failed to start main loop");
219 goto unlock_and_fail;
222 msg_Dbg(p_aout, "Pulse mainloop started");
224 /* Wait until the context is ready */
225 pa_threaded_mainloop_wait(p_sys->mainloop);
227 if (pa_context_get_state(p_sys->context) != PA_CONTEXT_READY) {
228 msg_Err(p_aout, "Failed to connect to server: %s", pa_strerror(pa_context_errno(p_sys->context)));
229 goto unlock_and_fail;
232 if (!(p_sys->stream = pa_stream_new(p_sys->context, "audio stream", &ss, &map))) {
233 msg_Err(p_aout, "Failed to create stream: %s", pa_strerror(pa_context_errno(p_sys->context)));
234 goto unlock_and_fail;
237 PULSE_DEBUG( "Pulse after new stream");
239 pa_stream_set_state_callback(p_sys->stream, stream_state_cb, p_aout);
240 pa_stream_set_write_callback(p_sys->stream, stream_request_cb, p_aout);
241 pa_stream_set_latency_update_callback(p_sys->stream, stream_latency_update_cb, p_aout);
243 if (pa_stream_connect_playback(p_sys->stream, NULL, &a, PA_STREAM_INTERPOLATE_TIMING|PA_STREAM_AUTO_TIMING_UPDATE, NULL, NULL) < 0) {
244 msg_Err(p_aout, "Failed to connect stream: %s", pa_strerror(pa_context_errno(p_sys->context)));
245 goto unlock_and_fail;
248 PULSE_DEBUG("Pulse stream connect");
250 /* Wait until the stream is ready */
251 pa_threaded_mainloop_wait(p_sys->mainloop);
253 msg_Dbg(p_aout,"Pulse stream connected");
255 if (pa_stream_get_state(p_sys->stream) != PA_STREAM_READY) {
256 msg_Err(p_aout, "Failed to connect to server: %s", pa_strerror(pa_context_errno(p_sys->context)));
257 goto unlock_and_fail;
261 PULSE_DEBUG("Pulse after stream get status");
263 pa_threaded_mainloop_unlock(p_sys->mainloop);
265 buffer_attr = pa_stream_get_buffer_attr(p_sys->stream);
266 p_aout->output.i_nb_samples = buffer_attr->minreq / pa_frame_size(&ss);
267 p_aout->output.pf_play = Play;
268 aout_VolumeSoftInit(p_aout);
269 msg_Dbg(p_aout, "Pulse initialized successfully");
271 char cmt[PA_CHANNEL_MAP_SNPRINT_MAX], sst[PA_SAMPLE_SPEC_SNPRINT_MAX];
273 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);
274 msg_Dbg(p_aout, "Using sample spec '%s', channel map '%s'.",
275 pa_sample_spec_snprint(sst, sizeof(sst), pa_stream_get_sample_spec(p_sys->stream)),
276 pa_channel_map_snprint(cmt, sizeof(cmt), pa_stream_get_channel_map(p_sys->stream)));
278 msg_Dbg(p_aout, "Connected to device %s (%u, %ssuspended).",
279 pa_stream_get_device_name(p_sys->stream),
280 pa_stream_get_device_index(p_sys->stream),
281 pa_stream_is_suspended(p_sys->stream) ? "" : "not ");
287 msg_Dbg(p_aout, "Pulse initialization unlock and fail");
290 pa_threaded_mainloop_unlock(p_sys->mainloop);
292 msg_Err(p_aout, "Pulse initialization failed");
297 /*****************************************************************************
298 * Play: play a sound samples buffer
299 *****************************************************************************/
300 static void Play( aout_instance_t * p_aout )
302 struct aout_sys_t * p_sys = (struct aout_sys_t *) p_aout->output.p_sys;
307 msg_Dbg(p_aout, "Pulse stream started");
309 aout_FifoFirstDate( p_aout, &p_aout->output.fifo );
312 pa_threaded_mainloop_lock(p_sys->mainloop);
313 if((o = pa_stream_flush(p_sys->stream, success_cb, p_aout))){
314 pa_operation_unref(o);
316 pa_threaded_mainloop_unlock(p_sys->mainloop);
318 pa_threaded_mainloop_signal(p_sys->mainloop, 0);
322 /*****************************************************************************
323 * Close: close the audio device
324 *****************************************************************************/
325 static void Close ( vlc_object_t *p_this )
327 aout_instance_t *p_aout = (aout_instance_t *)p_this;
328 struct aout_sys_t * p_sys = p_aout->output.p_sys;
330 msg_Dbg(p_aout, "Pulse Close");
334 pa_threaded_mainloop_lock(p_sys->mainloop);
335 pa_stream_set_write_callback(p_sys->stream, NULL, NULL);
337 if((o = pa_stream_drain(p_sys->stream, success_cb, p_aout))){
338 while (pa_operation_get_state(o) != PA_OPERATION_DONE) {
339 CHECK_DEAD_GOTO(fail);
340 pa_threaded_mainloop_wait(p_sys->mainloop);
345 pa_operation_unref(o);
348 pa_threaded_mainloop_unlock(p_sys->mainloop);
353 static void uninit(aout_instance_t *p_aout){
354 struct aout_sys_t * p_sys = p_aout->output.p_sys;
357 pa_threaded_mainloop_stop(p_sys->mainloop);
360 pa_stream_disconnect(p_sys->stream);
361 pa_stream_unref(p_sys->stream);
362 p_sys->stream = NULL;
365 if (p_sys->context) {
366 pa_context_disconnect(p_sys->context);
367 pa_context_unref(p_sys->context);
368 p_sys->context = NULL;
371 if (p_sys->mainloop) {
372 pa_threaded_mainloop_free(p_sys->mainloop);
373 p_sys->mainloop = NULL;
377 p_aout->output.p_sys = NULL;
380 static void context_state_cb(pa_context *c, void *userdata) {
381 aout_instance_t *p_aout = (aout_instance_t *)userdata;
382 struct aout_sys_t * p_sys = (struct aout_sys_t *) p_aout->output.p_sys;
386 PULSE_DEBUG( "Pulse context state changed");
388 switch (pa_context_get_state(c)) {
389 case PA_CONTEXT_READY:
390 case PA_CONTEXT_TERMINATED:
391 case PA_CONTEXT_FAILED:
392 PULSE_DEBUG( "Pulse context state changed signal");
393 pa_threaded_mainloop_signal(p_sys->mainloop, 0);
396 case PA_CONTEXT_UNCONNECTED:
397 case PA_CONTEXT_CONNECTING:
398 case PA_CONTEXT_AUTHORIZING:
399 case PA_CONTEXT_SETTING_NAME:
400 PULSE_DEBUG( "Pulse context state changed no signal");
405 static void stream_state_cb(pa_stream *s, void * userdata) {
406 aout_instance_t *p_aout = (aout_instance_t *)userdata;
407 struct aout_sys_t * p_sys = (struct aout_sys_t *) p_aout->output.p_sys;
411 PULSE_DEBUG( "Pulse stream state changed");
413 switch (pa_stream_get_state(s)) {
415 case PA_STREAM_READY:
416 case PA_STREAM_FAILED:
417 case PA_STREAM_TERMINATED:
418 pa_threaded_mainloop_signal(p_sys->mainloop, 0);
421 case PA_STREAM_UNCONNECTED:
422 case PA_STREAM_CREATING:
427 static void stream_request_cb(pa_stream *s, size_t length, void *userdata) {
428 aout_instance_t *p_aout = (aout_instance_t *)userdata;
429 struct aout_sys_t * p_sys = (struct aout_sys_t *) p_aout->output.p_sys;
435 size_t buffer_size = p_sys->buffer_size;
437 PULSE_DEBUG( "Pulse stream request %d", length);
440 aout_buffer_t * p_buffer = NULL;
444 if(pa_stream_get_latency(p_sys->stream, &latency, &negative)<0){
445 if (pa_context_errno(p_sys->context) != PA_ERR_NODATA) {
446 msg_Err(p_aout, "pa_stream_get_latency() failed: %s", pa_strerror(pa_context_errno(p_sys->context)));
451 PULSE_DEBUG( "Pulse stream request latency=%"PRId64"", latency);
452 next_date = mdate() + latency;
455 if(p_sys->start_date < next_date + AOUT_PTS_TOLERANCE ){
457 vlc_mutex_lock( &p_aout->output_fifo_lock );
458 p_buffer = aout_FifoPop( p_aout, &p_aout->output.fifo );
459 vlc_mutex_unlock( &p_aout->output_fifo_lock );
461 p_buffer = aout_OutputNextBuffer( p_aout, next_date, 0);
465 if ( p_buffer != NULL )
467 PULSE_DEBUG( "Pulse stream request write buffer %d", p_buffer->i_nb_bytes);
468 pa_stream_write(p_sys->stream, p_buffer->p_buffer, p_buffer->i_nb_bytes, NULL, 0, PA_SEEK_RELATIVE);
469 length -= p_buffer->i_nb_bytes;
470 aout_BufferFree( p_buffer );
474 PULSE_DEBUG( "Pulse stream request write zeroes");
475 void *data = pa_xmalloc(buffer_size);
476 bzero(data, buffer_size);
477 pa_stream_write(p_sys->stream, data, buffer_size, pa_xfree, 0, PA_SEEK_RELATIVE);
478 length -= buffer_size;
480 }while(length > buffer_size);
482 pa_threaded_mainloop_signal(p_sys->mainloop, 0);
485 static void stream_latency_update_cb(pa_stream *s, void *userdata) {
486 aout_instance_t *p_aout = (aout_instance_t *)userdata;
487 struct aout_sys_t * p_sys = (struct aout_sys_t *) p_aout->output.p_sys;
491 PULSE_DEBUG( "Pulse stream latency update");
493 pa_threaded_mainloop_signal(p_sys->mainloop, 0);
496 static void success_cb(pa_stream *s, int sucess, void *userdata)
498 aout_instance_t *p_aout = (aout_instance_t *)userdata;
499 struct aout_sys_t * p_sys = (struct aout_sys_t *) p_aout->output.p_sys;
505 pa_threaded_mainloop_signal(p_sys->mainloop, 0);