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"); \
79 /*****************************************************************************
81 *****************************************************************************/
82 static int Open ( vlc_object_t * );
83 static void Close ( vlc_object_t * );
84 static void Play ( aout_instance_t * );
86 static void context_state_cb(pa_context *c, void *userdata);
87 static void stream_state_cb(pa_stream *s, void * userdata);
88 static void stream_request_cb(pa_stream *s, size_t length, void *userdata);
89 static void stream_latency_update_cb(pa_stream *s, void *userdata);
90 static void success_cb(pa_stream *s, int sucess, void *userdata);
91 static void uninit(aout_instance_t *p_aout);
92 /*****************************************************************************
94 *****************************************************************************/
96 set_shortname( "Pulse Audio" )
97 set_description( N_("Pulseaudio audio output") )
98 set_capability( "audio output", 40 )
99 set_category( CAT_AUDIO )
100 set_subcategory( SUBCAT_AUDIO_AOUT )
101 add_shortcut( "pulseaudio" )
103 set_callbacks( Open, Close )
106 /*****************************************************************************
107 * Open: open the audio device
108 *****************************************************************************/
109 static int Open ( vlc_object_t *p_this )
111 aout_instance_t *p_aout = (aout_instance_t *)p_this;
112 struct aout_sys_t * p_sys;
113 struct pa_sample_spec ss;
114 const struct pa_buffer_attr *buffer_attr;
115 struct pa_buffer_attr a;
116 struct pa_channel_map map;
118 /* Allocate structures */
119 p_aout->output.p_sys = p_sys = malloc( sizeof( aout_sys_t ) );
122 memset( p_sys, 0, sizeof( aout_sys_t ) );
124 PULSE_DEBUG( "Pulse start initialization");
126 ss.channels = aout_FormatNbChannels( &p_aout->output.output ); /* Get the input stream channel count */
128 /* Setup the pulse audio stream based on the input stream count */
132 p_aout->output.output.i_physical_channels
133 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
134 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
139 p_aout->output.output.i_physical_channels
140 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
141 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
145 p_aout->output.output.i_physical_channels
146 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
150 p_aout->output.output.i_physical_channels = AOUT_CHAN_CENTER;
154 msg_Err(p_aout,"Invalid number of channels");
158 /* Add a quick command line info message */
159 msg_Info(p_aout, "No. of Audio Channels: %d", ss.channels);
161 ss.rate = p_aout->output.output.i_rate;
162 ss.format = PA_SAMPLE_S16LE;
163 p_aout->output.output.i_format = AOUT_FMT_S16_NE;
165 if (!pa_sample_spec_valid(&ss)) {
166 msg_Err(p_aout,"Invalid sample spec");
170 a.maxlength = pa_bytes_per_second(&ss)/4/pa_frame_size(&ss);
171 a.tlength = a.maxlength*9/10;
172 a.prebuf = a.tlength/2;
173 a.minreq = a.tlength/10;
175 a.maxlength *= pa_frame_size(&ss);
176 a.tlength *= pa_frame_size(&ss);
177 a.prebuf *= pa_frame_size(&ss);
178 a.minreq *= pa_frame_size(&ss);
180 p_sys->buffer_size = a.minreq;
182 /* Initialise the speaker map setup above */
183 pa_channel_map_init_auto(&map, ss.channels, PA_CHANNEL_MAP_ALSA);
185 if (!(p_sys->mainloop = pa_threaded_mainloop_new())) {
186 msg_Err(p_aout, "Failed to allocate main loop");
190 if (!(p_sys->context = pa_context_new(pa_threaded_mainloop_get_api(p_sys->mainloop), _( PULSE_CLIENT_NAME )))) {
191 msg_Err(p_aout, "Failed to allocate context");
195 pa_context_set_state_callback(p_sys->context, context_state_cb, p_aout);
197 PULSE_DEBUG( "Pulse before context connect");
199 if (pa_context_connect(p_sys->context, NULL, 0, NULL) < 0) {
200 msg_Err(p_aout, "Failed to connect to server: %s", pa_strerror(pa_context_errno(p_sys->context)));
204 PULSE_DEBUG( "Pulse after context connect");
206 pa_threaded_mainloop_lock(p_sys->mainloop);
208 if (pa_threaded_mainloop_start(p_sys->mainloop) < 0) {
209 msg_Err(p_aout, "Failed to start main loop");
210 goto unlock_and_fail;
213 msg_Dbg(p_aout, "Pulse mainloop started");
215 /* Wait until the context is ready */
216 pa_threaded_mainloop_wait(p_sys->mainloop);
218 if (pa_context_get_state(p_sys->context) != PA_CONTEXT_READY) {
219 msg_Err(p_aout, "Failed to connect to server: %s", pa_strerror(pa_context_errno(p_sys->context)));
220 goto unlock_and_fail;
223 if (!(p_sys->stream = pa_stream_new(p_sys->context, "audio stream", &ss, &map))) {
224 msg_Err(p_aout, "Failed to create stream: %s", pa_strerror(pa_context_errno(p_sys->context)));
225 goto unlock_and_fail;
228 PULSE_DEBUG( "Pulse after new stream");
230 pa_stream_set_state_callback(p_sys->stream, stream_state_cb, p_aout);
231 pa_stream_set_write_callback(p_sys->stream, stream_request_cb, p_aout);
232 pa_stream_set_latency_update_callback(p_sys->stream, stream_latency_update_cb, p_aout);
234 if (pa_stream_connect_playback(p_sys->stream, NULL, &a, PA_STREAM_INTERPOLATE_TIMING|PA_STREAM_AUTO_TIMING_UPDATE, NULL, NULL) < 0) {
235 msg_Err(p_aout, "Failed to connect stream: %s", pa_strerror(pa_context_errno(p_sys->context)));
236 goto unlock_and_fail;
239 PULSE_DEBUG("Pulse stream connect");
241 /* Wait until the stream is ready */
242 pa_threaded_mainloop_wait(p_sys->mainloop);
244 msg_Dbg(p_aout,"Pulse stream connected");
246 if (pa_stream_get_state(p_sys->stream) != PA_STREAM_READY) {
247 msg_Err(p_aout, "Failed to connect to server: %s", pa_strerror(pa_context_errno(p_sys->context)));
248 goto unlock_and_fail;
252 PULSE_DEBUG("Pulse after stream get status");
254 pa_threaded_mainloop_unlock(p_sys->mainloop);
256 buffer_attr = pa_stream_get_buffer_attr(p_sys->stream);
257 p_aout->output.i_nb_samples = buffer_attr->minreq / pa_frame_size(&ss);
258 p_aout->output.pf_play = Play;
259 aout_VolumeSoftInit(p_aout);
260 msg_Dbg(p_aout, "Pulse initialized successfully");
262 char cmt[PA_CHANNEL_MAP_SNPRINT_MAX], sst[PA_SAMPLE_SPEC_SNPRINT_MAX];
264 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);
265 msg_Dbg(p_aout, "Using sample spec '%s', channel map '%s'.",
266 pa_sample_spec_snprint(sst, sizeof(sst), pa_stream_get_sample_spec(p_sys->stream)),
267 pa_channel_map_snprint(cmt, sizeof(cmt), pa_stream_get_channel_map(p_sys->stream)));
269 msg_Dbg(p_aout, "Connected to device %s (%u, %ssuspended).",
270 pa_stream_get_device_name(p_sys->stream),
271 pa_stream_get_device_index(p_sys->stream),
272 pa_stream_is_suspended(p_sys->stream) ? "" : "not ");
278 msg_Dbg(p_aout, "Pulse initialization unlock and fail");
281 pa_threaded_mainloop_unlock(p_sys->mainloop);
283 msg_Err(p_aout, "Pulse initialization failed");
288 /*****************************************************************************
289 * Play: play a sound samples buffer
290 *****************************************************************************/
291 static void Play( aout_instance_t * p_aout )
293 struct aout_sys_t * p_sys = (struct aout_sys_t *) p_aout->output.p_sys;
298 msg_Dbg(p_aout, "Pulse stream started");
300 aout_FifoFirstDate( p_aout, &p_aout->output.fifo );
303 pa_threaded_mainloop_lock(p_sys->mainloop);
304 if((o = pa_stream_flush(p_sys->stream, success_cb, p_aout))){
305 pa_operation_unref(o);
307 pa_threaded_mainloop_unlock(p_sys->mainloop);
309 pa_threaded_mainloop_signal(p_sys->mainloop, 0);
313 /*****************************************************************************
314 * Close: close the audio device
315 *****************************************************************************/
316 static void Close ( vlc_object_t *p_this )
318 aout_instance_t *p_aout = (aout_instance_t *)p_this;
319 struct aout_sys_t * p_sys = p_aout->output.p_sys;
321 msg_Dbg(p_aout, "Pulse Close");
325 pa_threaded_mainloop_lock(p_sys->mainloop);
326 pa_stream_set_write_callback(p_sys->stream, NULL, NULL);
328 if((o = pa_stream_drain(p_sys->stream, success_cb, p_aout))){
329 while (pa_operation_get_state(o) != PA_OPERATION_DONE) {
330 CHECK_DEAD_GOTO(fail);
331 pa_threaded_mainloop_wait(p_sys->mainloop);
336 pa_operation_unref(o);
339 pa_threaded_mainloop_unlock(p_sys->mainloop);
344 static void uninit(aout_instance_t *p_aout){
345 struct aout_sys_t * p_sys = p_aout->output.p_sys;
348 pa_threaded_mainloop_stop(p_sys->mainloop);
351 pa_stream_disconnect(p_sys->stream);
352 pa_stream_unref(p_sys->stream);
353 p_sys->stream = NULL;
356 if (p_sys->context) {
357 pa_context_disconnect(p_sys->context);
358 pa_context_unref(p_sys->context);
359 p_sys->context = NULL;
362 if (p_sys->mainloop) {
363 pa_threaded_mainloop_free(p_sys->mainloop);
364 p_sys->mainloop = NULL;
368 p_aout->output.p_sys = NULL;
371 static void context_state_cb(pa_context *c, void *userdata) {
372 aout_instance_t *p_aout = (aout_instance_t *)userdata;
373 struct aout_sys_t * p_sys = (struct aout_sys_t *) p_aout->output.p_sys;
377 PULSE_DEBUG( "Pulse context state changed");
379 switch (pa_context_get_state(c)) {
380 case PA_CONTEXT_READY:
381 case PA_CONTEXT_TERMINATED:
382 case PA_CONTEXT_FAILED:
383 PULSE_DEBUG( "Pulse context state changed signal");
384 pa_threaded_mainloop_signal(p_sys->mainloop, 0);
387 case PA_CONTEXT_UNCONNECTED:
388 case PA_CONTEXT_CONNECTING:
389 case PA_CONTEXT_AUTHORIZING:
390 case PA_CONTEXT_SETTING_NAME:
391 PULSE_DEBUG( "Pulse context state changed no signal");
396 static void stream_state_cb(pa_stream *s, void * userdata) {
397 aout_instance_t *p_aout = (aout_instance_t *)userdata;
398 struct aout_sys_t * p_sys = (struct aout_sys_t *) p_aout->output.p_sys;
402 PULSE_DEBUG( "Pulse stream state changed");
404 switch (pa_stream_get_state(s)) {
406 case PA_STREAM_READY:
407 case PA_STREAM_FAILED:
408 case PA_STREAM_TERMINATED:
409 pa_threaded_mainloop_signal(p_sys->mainloop, 0);
412 case PA_STREAM_UNCONNECTED:
413 case PA_STREAM_CREATING:
418 static void stream_request_cb(pa_stream *s, size_t length, void *userdata) {
419 aout_instance_t *p_aout = (aout_instance_t *)userdata;
420 struct aout_sys_t * p_sys = (struct aout_sys_t *) p_aout->output.p_sys;
426 size_t buffer_size = p_sys->buffer_size;
428 PULSE_DEBUG( "Pulse stream request %d", length);
431 aout_buffer_t * p_buffer = NULL;
435 if(pa_stream_get_latency(p_sys->stream, &latency, &negative)<0){
436 if (pa_context_errno(p_sys->context) != PA_ERR_NODATA) {
437 msg_Err(p_aout, "pa_stream_get_latency() failed: %s", pa_strerror(pa_context_errno(p_sys->context)));
442 PULSE_DEBUG( "Pulse stream request latency=%"PRId64"", latency);
443 next_date = mdate() + latency;
446 if(p_sys->start_date < next_date + AOUT_PTS_TOLERANCE ){
448 vlc_mutex_lock( &p_aout->output_fifo_lock );
449 p_buffer = aout_FifoPop( p_aout, &p_aout->output.fifo );
450 vlc_mutex_unlock( &p_aout->output_fifo_lock );
452 p_buffer = aout_OutputNextBuffer( p_aout, next_date, 0);
456 if ( p_buffer != NULL )
458 PULSE_DEBUG( "Pulse stream request write buffer %d", p_buffer->i_nb_bytes);
459 pa_stream_write(p_sys->stream, p_buffer->p_buffer, p_buffer->i_nb_bytes, NULL, 0, PA_SEEK_RELATIVE);
460 length -= p_buffer->i_nb_bytes;
461 aout_BufferFree( p_buffer );
465 PULSE_DEBUG( "Pulse stream request write zeroes");
466 void *data = pa_xmalloc(buffer_size);
467 bzero(data, buffer_size);
468 pa_stream_write(p_sys->stream, data, buffer_size, pa_xfree, 0, PA_SEEK_RELATIVE);
469 length -= buffer_size;
471 }while(length > buffer_size);
473 pa_threaded_mainloop_signal(p_sys->mainloop, 0);
476 static void stream_latency_update_cb(pa_stream *s, void *userdata) {
477 aout_instance_t *p_aout = (aout_instance_t *)userdata;
478 struct aout_sys_t * p_sys = (struct aout_sys_t *) p_aout->output.p_sys;
482 PULSE_DEBUG( "Pulse stream latency update");
484 pa_threaded_mainloop_signal(p_sys->mainloop, 0);
487 static void success_cb(pa_stream *s, int sucess, void *userdata)
489 aout_instance_t *p_aout = (aout_instance_t *)userdata;
490 struct aout_sys_t * p_sys = (struct aout_sys_t *) p_aout->output.p_sys;
496 pa_threaded_mainloop_signal(p_sys->mainloop, 0);