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 *****************************************************************************/
34 #include <pulse/pulseaudio.h>
36 /*****************************************************************************
37 * aout_sys_t: Pulseaudio output method descriptor
38 *****************************************************************************
39 * This structure is part of the audio output thread descriptor.
40 * It describes the specific properties of an audio device.
41 *****************************************************************************/
44 /** PulseAudio playback stream object */
45 struct pa_stream *stream;
47 /** PulseAudio connection context */
48 struct pa_context *context;
50 /** Main event loop object */
51 struct pa_threaded_mainloop *mainloop;
58 #define PULSE_CLIENT_NAME N_("VLC media player")
61 #define PULSE_DEBUG( ...) \
62 msg_Dbg( p_aout, __VA_ARGS__ )
64 #define PULSE_DEBUG( ...) \
69 #define CHECK_DEAD_GOTO(label) do { \
70 if (!p_sys->context || pa_context_get_state(p_sys->context) != PA_CONTEXT_READY || \
71 !p_sys->stream || pa_stream_get_state(p_sys->stream) != PA_STREAM_READY) { \
72 msg_Err(p_aout, "Connection died: %s", p_sys->context ? pa_strerror(pa_context_errno(p_sys->context)) : "NULL"); \
76 /*****************************************************************************
78 *****************************************************************************/
79 static int Open ( vlc_object_t * );
80 static void Close ( vlc_object_t * );
81 static void Play ( aout_instance_t * );
83 static void context_state_cb(pa_context *c, void *userdata);
84 static void stream_state_cb(pa_stream *s, void * userdata);
85 static void stream_request_cb(pa_stream *s, size_t length, void *userdata);
86 static void stream_latency_update_cb(pa_stream *s, void *userdata);
87 static void success_cb(pa_stream *s, int sucess, void *userdata);
88 static void uninit(aout_instance_t *p_aout);
89 /*****************************************************************************
91 *****************************************************************************/
93 set_shortname( "Pulse Audio" );
94 set_description( _("Pulseaudio audio output") );
95 set_capability( "audio output", 40 );
96 set_category( CAT_AUDIO );
97 set_subcategory( SUBCAT_AUDIO_AOUT );
98 add_shortcut( "pulseaudio" );
100 set_callbacks( Open, Close );
103 /*****************************************************************************
104 * Open: open the audio device
105 *****************************************************************************/
106 static int Open ( vlc_object_t *p_this )
108 aout_instance_t *p_aout = (aout_instance_t *)p_this;
109 struct aout_sys_t * p_sys;
110 struct pa_sample_spec ss;
111 const struct pa_buffer_attr *buffer_attr;
112 struct pa_buffer_attr a;
113 struct pa_channel_map map;
115 /* Allocate structures */
116 p_aout->output.p_sys = p_sys = malloc( sizeof( aout_sys_t ) );
119 memset( p_sys, 0, sizeof( aout_sys_t ) );
121 PULSE_DEBUG( "Pulse start initialization");
123 ss.rate = p_aout->output.output.i_rate;
126 ss.format = PA_SAMPLE_S16LE;
127 p_aout->output.output.i_physical_channels =
128 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
129 p_aout->output.output.i_format = AOUT_FMT_S16_NE;
131 if (!pa_sample_spec_valid(&ss)) {
132 msg_Err(p_aout,"Invalid sample spec");
136 a.maxlength = pa_bytes_per_second(&ss)/4/pa_frame_size(&ss);
137 a.tlength = a.maxlength*9/10;
138 a.prebuf = a.tlength/2;
139 a.minreq = a.tlength/10;
141 a.maxlength *= pa_frame_size(&ss);
142 a.tlength *= pa_frame_size(&ss);
143 a.prebuf *= pa_frame_size(&ss);
144 a.minreq *= pa_frame_size(&ss);
146 p_sys->buffer_size = a.minreq;
148 pa_channel_map_init_stereo(&map);
151 if (!(p_sys->mainloop = pa_threaded_mainloop_new())) {
152 msg_Err(p_aout, "Failed to allocate main loop");
156 if (!(p_sys->context = pa_context_new(pa_threaded_mainloop_get_api(p_sys->mainloop), _( PULSE_CLIENT_NAME )))) {
157 msg_Err(p_aout, "Failed to allocate context");
161 pa_context_set_state_callback(p_sys->context, context_state_cb, p_aout);
163 PULSE_DEBUG( "Pulse before context connect");
165 if (pa_context_connect(p_sys->context, NULL, 0, NULL) < 0) {
166 msg_Err(p_aout, "Failed to connect to server: %s", pa_strerror(pa_context_errno(p_sys->context)));
170 PULSE_DEBUG( "Pulse after context connect");
172 pa_threaded_mainloop_lock(p_sys->mainloop);
174 if (pa_threaded_mainloop_start(p_sys->mainloop) < 0) {
175 msg_Err(p_aout, "Failed to start main loop");
176 goto unlock_and_fail;
179 msg_Dbg(p_aout, "Pulse mainloop started");
181 /* Wait until the context is ready */
182 pa_threaded_mainloop_wait(p_sys->mainloop);
184 if (pa_context_get_state(p_sys->context) != PA_CONTEXT_READY) {
185 msg_Err(p_aout, "Failed to connect to server: %s", pa_strerror(pa_context_errno(p_sys->context)));
186 goto unlock_and_fail;
189 if (!(p_sys->stream = pa_stream_new(p_sys->context, "audio stream", &ss, &map))) {
190 msg_Err(p_aout, "Failed to create stream: %s", pa_strerror(pa_context_errno(p_sys->context)));
191 goto unlock_and_fail;
194 PULSE_DEBUG( "Pulse after new stream");
196 pa_stream_set_state_callback(p_sys->stream, stream_state_cb, p_aout);
197 pa_stream_set_write_callback(p_sys->stream, stream_request_cb, p_aout);
198 pa_stream_set_latency_update_callback(p_sys->stream, stream_latency_update_cb, p_aout);
200 if (pa_stream_connect_playback(p_sys->stream, NULL, &a, PA_STREAM_INTERPOLATE_TIMING|PA_STREAM_AUTO_TIMING_UPDATE, NULL, NULL) < 0) {
201 msg_Err(p_aout, "Failed to connect stream: %s", pa_strerror(pa_context_errno(p_sys->context)));
202 goto unlock_and_fail;
205 PULSE_DEBUG("Pulse stream connect");
207 /* Wait until the stream is ready */
208 pa_threaded_mainloop_wait(p_sys->mainloop);
210 msg_Dbg(p_aout,"Pulse stream connected");
212 if (pa_stream_get_state(p_sys->stream) != PA_STREAM_READY) {
213 msg_Err(p_aout, "Failed to connect to server: %s", pa_strerror(pa_context_errno(p_sys->context)));
214 goto unlock_and_fail;
218 PULSE_DEBUG("Pulse after stream get status");
220 pa_threaded_mainloop_unlock(p_sys->mainloop);
222 buffer_attr = pa_stream_get_buffer_attr(p_sys->stream);
223 p_aout->output.i_nb_samples = buffer_attr->minreq / pa_frame_size(&ss);
224 p_aout->output.pf_play = Play;
225 aout_VolumeSoftInit(p_aout);
226 msg_Dbg(p_aout, "Pulse initialized successfully");
228 char cmt[PA_CHANNEL_MAP_SNPRINT_MAX], sst[PA_SAMPLE_SPEC_SNPRINT_MAX];
230 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);
231 msg_Dbg(p_aout, "Using sample spec '%s', channel map '%s'.",
232 pa_sample_spec_snprint(sst, sizeof(sst), pa_stream_get_sample_spec(p_sys->stream)),
233 pa_channel_map_snprint(cmt, sizeof(cmt), pa_stream_get_channel_map(p_sys->stream)));
235 msg_Dbg(p_aout, "Connected to device %s (%u, %ssuspended).",
236 pa_stream_get_device_name(p_sys->stream),
237 pa_stream_get_device_index(p_sys->stream),
238 pa_stream_is_suspended(p_sys->stream) ? "" : "not ");
244 msg_Dbg(p_aout, "Pulse initialization unlock and fail");
247 pa_threaded_mainloop_unlock(p_sys->mainloop);
249 msg_Err(p_aout, "Pulse initialization failed");
254 /*****************************************************************************
255 * Play: play a sound samples buffer
256 *****************************************************************************/
257 static void Play( aout_instance_t * p_aout )
259 struct aout_sys_t * p_sys = (struct aout_sys_t *) p_aout->output.p_sys;
264 msg_Dbg(p_aout, "Pulse stream started");
266 aout_FifoFirstDate( p_aout, &p_aout->output.fifo );
269 pa_threaded_mainloop_lock(p_sys->mainloop);
270 if((o = pa_stream_flush(p_sys->stream, success_cb, p_aout))){
271 pa_operation_unref(o);
273 pa_threaded_mainloop_unlock(p_sys->mainloop);
275 pa_threaded_mainloop_signal(p_sys->mainloop, 0);
279 /*****************************************************************************
280 * Close: close the audio device
281 *****************************************************************************/
282 static void Close ( vlc_object_t *p_this )
284 aout_instance_t *p_aout = (aout_instance_t *)p_this;
285 struct aout_sys_t * p_sys = p_aout->output.p_sys;
287 msg_Dbg(p_aout, "Pulse Close");
291 pa_threaded_mainloop_lock(p_sys->mainloop);
292 pa_stream_set_write_callback(p_sys->stream, NULL, NULL);
294 if((o = pa_stream_drain(p_sys->stream, success_cb, p_aout))){
295 while (pa_operation_get_state(o) != PA_OPERATION_DONE) {
296 CHECK_DEAD_GOTO(fail);
297 pa_threaded_mainloop_wait(p_sys->mainloop);
302 pa_operation_unref(o);
305 pa_threaded_mainloop_unlock(p_sys->mainloop);
310 static void uninit(aout_instance_t *p_aout){
311 struct aout_sys_t * p_sys = p_aout->output.p_sys;
314 pa_threaded_mainloop_stop(p_sys->mainloop);
317 pa_stream_disconnect(p_sys->stream);
318 pa_stream_unref(p_sys->stream);
319 p_sys->stream = NULL;
322 if (p_sys->context) {
323 pa_context_disconnect(p_sys->context);
324 pa_context_unref(p_sys->context);
325 p_sys->context = NULL;
328 if (p_sys->mainloop) {
329 pa_threaded_mainloop_free(p_sys->mainloop);
330 p_sys->mainloop = NULL;
334 p_aout->output.p_sys = NULL;
337 static void context_state_cb(pa_context *c, void *userdata) {
338 aout_instance_t *p_aout = (aout_instance_t *)userdata;
339 struct aout_sys_t * p_sys = (struct aout_sys_t *) p_aout->output.p_sys;
343 PULSE_DEBUG( "Pulse context state changed");
345 switch (pa_context_get_state(c)) {
346 case PA_CONTEXT_READY:
347 case PA_CONTEXT_TERMINATED:
348 case PA_CONTEXT_FAILED:
349 PULSE_DEBUG( "Pulse context state changed signal");
350 pa_threaded_mainloop_signal(p_sys->mainloop, 0);
353 case PA_CONTEXT_UNCONNECTED:
354 case PA_CONTEXT_CONNECTING:
355 case PA_CONTEXT_AUTHORIZING:
356 case PA_CONTEXT_SETTING_NAME:
357 PULSE_DEBUG( "Pulse context state changed no signal");
362 static void stream_state_cb(pa_stream *s, void * userdata) {
363 aout_instance_t *p_aout = (aout_instance_t *)userdata;
364 struct aout_sys_t * p_sys = (struct aout_sys_t *) p_aout->output.p_sys;
368 PULSE_DEBUG( "Pulse stream state changed");
370 switch (pa_stream_get_state(s)) {
372 case PA_STREAM_READY:
373 case PA_STREAM_FAILED:
374 case PA_STREAM_TERMINATED:
375 pa_threaded_mainloop_signal(p_sys->mainloop, 0);
378 case PA_STREAM_UNCONNECTED:
379 case PA_STREAM_CREATING:
384 static void stream_request_cb(pa_stream *s, size_t length, void *userdata) {
385 aout_instance_t *p_aout = (aout_instance_t *)userdata;
386 struct aout_sys_t * p_sys = (struct aout_sys_t *) p_aout->output.p_sys;
392 size_t buffer_size = p_sys->buffer_size;
394 PULSE_DEBUG( "Pulse stream request %d", length);
397 aout_buffer_t * p_buffer = NULL;
401 if(pa_stream_get_latency(p_sys->stream, &latency, &negative)<0){
402 if (pa_context_errno(p_sys->context) != PA_ERR_NODATA) {
403 msg_Err(p_aout, "pa_stream_get_latency() failed: %s", pa_strerror(pa_context_errno(p_sys->context)));
408 PULSE_DEBUG( "Pulse stream request latency="I64Fd"", latency);
409 next_date = mdate() + latency;
412 if(p_sys->start_date < next_date + AOUT_PTS_TOLERANCE ){
414 vlc_mutex_lock( &p_aout->output_fifo_lock );
415 p_buffer = aout_FifoPop( p_aout, &p_aout->output.fifo );
416 vlc_mutex_unlock( &p_aout->output_fifo_lock );
418 p_buffer = aout_OutputNextBuffer( p_aout, next_date, 0);
422 if ( p_buffer != NULL )
424 PULSE_DEBUG( "Pulse stream request write buffer %d", p_buffer->i_nb_bytes);
425 pa_stream_write(p_sys->stream, p_buffer->p_buffer, p_buffer->i_nb_bytes, NULL, 0, PA_SEEK_RELATIVE);
426 length -= p_buffer->i_nb_bytes;
427 aout_BufferFree( p_buffer );
431 PULSE_DEBUG( "Pulse stream request write zeroes");
432 void *data = pa_xmalloc(buffer_size);
433 bzero(data, buffer_size);
434 pa_stream_write(p_sys->stream, data, buffer_size, pa_xfree, 0, PA_SEEK_RELATIVE);
435 length -= buffer_size;
437 }while(length > buffer_size);
439 pa_threaded_mainloop_signal(p_sys->mainloop, 0);
442 static void stream_latency_update_cb(pa_stream *s, void *userdata) {
443 aout_instance_t *p_aout = (aout_instance_t *)userdata;
444 struct aout_sys_t * p_sys = (struct aout_sys_t *) p_aout->output.p_sys;
448 PULSE_DEBUG( "Pulse stream latency update");
450 pa_threaded_mainloop_signal(p_sys->mainloop, 0);
453 static void success_cb(pa_stream *s, int sucess, void *userdata)
455 aout_instance_t *p_aout = (aout_instance_t *)userdata;
456 struct aout_sys_t * p_sys = (struct aout_sys_t *) p_aout->output.p_sys;
462 pa_threaded_mainloop_signal(p_sys->mainloop, 0);