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" );
102 add_shortcut( "pa" );
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.rate = p_aout->output.output.i_rate;
129 ss.format = PA_SAMPLE_S16LE;
130 p_aout->output.output.i_physical_channels =
131 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
132 p_aout->output.output.i_format = AOUT_FMT_S16_NE;
134 if (!pa_sample_spec_valid(&ss)) {
135 msg_Err(p_aout,"Invalid sample spec");
139 a.maxlength = pa_bytes_per_second(&ss)/4/pa_frame_size(&ss);
140 a.tlength = a.maxlength*9/10;
141 a.prebuf = a.tlength/2;
142 a.minreq = a.tlength/10;
144 a.maxlength *= pa_frame_size(&ss);
145 a.tlength *= pa_frame_size(&ss);
146 a.prebuf *= pa_frame_size(&ss);
147 a.minreq *= pa_frame_size(&ss);
149 p_sys->buffer_size = a.minreq;
151 pa_channel_map_init_stereo(&map);
154 if (!(p_sys->mainloop = pa_threaded_mainloop_new())) {
155 msg_Err(p_aout, "Failed to allocate main loop");
159 if (!(p_sys->context = pa_context_new(pa_threaded_mainloop_get_api(p_sys->mainloop), _( PULSE_CLIENT_NAME )))) {
160 msg_Err(p_aout, "Failed to allocate context");
164 pa_context_set_state_callback(p_sys->context, context_state_cb, p_aout);
166 PULSE_DEBUG( "Pulse before context connect");
168 if (pa_context_connect(p_sys->context, NULL, 0, NULL) < 0) {
169 msg_Err(p_aout, "Failed to connect to server: %s", pa_strerror(pa_context_errno(p_sys->context)));
173 PULSE_DEBUG( "Pulse after context connect");
175 pa_threaded_mainloop_lock(p_sys->mainloop);
177 if (pa_threaded_mainloop_start(p_sys->mainloop) < 0) {
178 msg_Err(p_aout, "Failed to start main loop");
179 goto unlock_and_fail;
182 msg_Dbg(p_aout, "Pulse mainloop started");
184 /* Wait until the context is ready */
185 pa_threaded_mainloop_wait(p_sys->mainloop);
187 if (pa_context_get_state(p_sys->context) != PA_CONTEXT_READY) {
188 msg_Err(p_aout, "Failed to connect to server: %s", pa_strerror(pa_context_errno(p_sys->context)));
189 goto unlock_and_fail;
192 if (!(p_sys->stream = pa_stream_new(p_sys->context, "audio stream", &ss, &map))) {
193 msg_Err(p_aout, "Failed to create stream: %s", pa_strerror(pa_context_errno(p_sys->context)));
194 goto unlock_and_fail;
197 PULSE_DEBUG( "Pulse after new stream");
199 pa_stream_set_state_callback(p_sys->stream, stream_state_cb, p_aout);
200 pa_stream_set_write_callback(p_sys->stream, stream_request_cb, p_aout);
201 pa_stream_set_latency_update_callback(p_sys->stream, stream_latency_update_cb, p_aout);
203 if (pa_stream_connect_playback(p_sys->stream, NULL, &a, PA_STREAM_INTERPOLATE_TIMING|PA_STREAM_AUTO_TIMING_UPDATE, NULL, NULL) < 0) {
204 msg_Err(p_aout, "Failed to connect stream: %s", pa_strerror(pa_context_errno(p_sys->context)));
205 goto unlock_and_fail;
208 PULSE_DEBUG("Pulse stream connect");
210 /* Wait until the stream is ready */
211 pa_threaded_mainloop_wait(p_sys->mainloop);
213 msg_Dbg(p_aout,"Pulse stream connected");
215 if (pa_stream_get_state(p_sys->stream) != PA_STREAM_READY) {
216 msg_Err(p_aout, "Failed to connect to server: %s", pa_strerror(pa_context_errno(p_sys->context)));
217 goto unlock_and_fail;
221 PULSE_DEBUG("Pulse after stream get status");
223 pa_threaded_mainloop_unlock(p_sys->mainloop);
225 buffer_attr = pa_stream_get_buffer_attr(p_sys->stream);
226 p_aout->output.i_nb_samples = buffer_attr->minreq / pa_frame_size(&ss);
227 p_aout->output.pf_play = Play;
228 aout_VolumeSoftInit(p_aout);
229 msg_Dbg(p_aout, "Pulse initialized successfully");
231 char cmt[PA_CHANNEL_MAP_SNPRINT_MAX], sst[PA_SAMPLE_SPEC_SNPRINT_MAX];
233 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);
234 msg_Dbg(p_aout, "Using sample spec '%s', channel map '%s'.",
235 pa_sample_spec_snprint(sst, sizeof(sst), pa_stream_get_sample_spec(p_sys->stream)),
236 pa_channel_map_snprint(cmt, sizeof(cmt), pa_stream_get_channel_map(p_sys->stream)));
238 msg_Dbg(p_aout, "Connected to device %s (%u, %ssuspended).",
239 pa_stream_get_device_name(p_sys->stream),
240 pa_stream_get_device_index(p_sys->stream),
241 pa_stream_is_suspended(p_sys->stream) ? "" : "not ");
247 msg_Dbg(p_aout, "Pulse initialization unlock and fail");
250 pa_threaded_mainloop_unlock(p_sys->mainloop);
252 msg_Err(p_aout, "Pulse initialization failed");
257 /*****************************************************************************
258 * Play: play a sound samples buffer
259 *****************************************************************************/
260 static void Play( aout_instance_t * p_aout )
262 struct aout_sys_t * p_sys = (struct aout_sys_t *) p_aout->output.p_sys;
267 msg_Dbg(p_aout, "Pulse stream started");
269 aout_FifoFirstDate( p_aout, &p_aout->output.fifo );
272 pa_threaded_mainloop_lock(p_sys->mainloop);
273 if((o = pa_stream_flush(p_sys->stream, success_cb, p_aout))){
274 pa_operation_unref(o);
276 pa_threaded_mainloop_unlock(p_sys->mainloop);
278 pa_threaded_mainloop_signal(p_sys->mainloop, 0);
282 /*****************************************************************************
283 * Close: close the audio device
284 *****************************************************************************/
285 static void Close ( vlc_object_t *p_this )
287 aout_instance_t *p_aout = (aout_instance_t *)p_this;
288 struct aout_sys_t * p_sys = p_aout->output.p_sys;
290 msg_Dbg(p_aout, "Pulse Close");
294 pa_threaded_mainloop_lock(p_sys->mainloop);
295 pa_stream_set_write_callback(p_sys->stream, NULL, NULL);
297 if((o = pa_stream_drain(p_sys->stream, success_cb, p_aout))){
298 while (pa_operation_get_state(o) != PA_OPERATION_DONE) {
299 CHECK_DEAD_GOTO(fail);
300 pa_threaded_mainloop_wait(p_sys->mainloop);
305 pa_operation_unref(o);
308 pa_threaded_mainloop_unlock(p_sys->mainloop);
313 static void uninit(aout_instance_t *p_aout){
314 struct aout_sys_t * p_sys = p_aout->output.p_sys;
317 pa_threaded_mainloop_stop(p_sys->mainloop);
320 pa_stream_disconnect(p_sys->stream);
321 pa_stream_unref(p_sys->stream);
322 p_sys->stream = NULL;
325 if (p_sys->context) {
326 pa_context_disconnect(p_sys->context);
327 pa_context_unref(p_sys->context);
328 p_sys->context = NULL;
331 if (p_sys->mainloop) {
332 pa_threaded_mainloop_free(p_sys->mainloop);
333 p_sys->mainloop = NULL;
337 p_aout->output.p_sys = NULL;
340 static void context_state_cb(pa_context *c, void *userdata) {
341 aout_instance_t *p_aout = (aout_instance_t *)userdata;
342 struct aout_sys_t * p_sys = (struct aout_sys_t *) p_aout->output.p_sys;
346 PULSE_DEBUG( "Pulse context state changed");
348 switch (pa_context_get_state(c)) {
349 case PA_CONTEXT_READY:
350 case PA_CONTEXT_TERMINATED:
351 case PA_CONTEXT_FAILED:
352 PULSE_DEBUG( "Pulse context state changed signal");
353 pa_threaded_mainloop_signal(p_sys->mainloop, 0);
356 case PA_CONTEXT_UNCONNECTED:
357 case PA_CONTEXT_CONNECTING:
358 case PA_CONTEXT_AUTHORIZING:
359 case PA_CONTEXT_SETTING_NAME:
360 PULSE_DEBUG( "Pulse context state changed no signal");
365 static void stream_state_cb(pa_stream *s, void * userdata) {
366 aout_instance_t *p_aout = (aout_instance_t *)userdata;
367 struct aout_sys_t * p_sys = (struct aout_sys_t *) p_aout->output.p_sys;
371 PULSE_DEBUG( "Pulse stream state changed");
373 switch (pa_stream_get_state(s)) {
375 case PA_STREAM_READY:
376 case PA_STREAM_FAILED:
377 case PA_STREAM_TERMINATED:
378 pa_threaded_mainloop_signal(p_sys->mainloop, 0);
381 case PA_STREAM_UNCONNECTED:
382 case PA_STREAM_CREATING:
387 static void stream_request_cb(pa_stream *s, size_t length, void *userdata) {
388 aout_instance_t *p_aout = (aout_instance_t *)userdata;
389 struct aout_sys_t * p_sys = (struct aout_sys_t *) p_aout->output.p_sys;
395 size_t buffer_size = p_sys->buffer_size;
397 PULSE_DEBUG( "Pulse stream request %d", length);
400 aout_buffer_t * p_buffer = NULL;
404 if(pa_stream_get_latency(p_sys->stream, &latency, &negative)<0){
405 if (pa_context_errno(p_sys->context) != PA_ERR_NODATA) {
406 msg_Err(p_aout, "pa_stream_get_latency() failed: %s", pa_strerror(pa_context_errno(p_sys->context)));
411 PULSE_DEBUG( "Pulse stream request latency=%"PRId64"", latency);
412 next_date = mdate() + latency;
415 if(p_sys->start_date < next_date + AOUT_PTS_TOLERANCE ){
417 vlc_mutex_lock( &p_aout->output_fifo_lock );
418 p_buffer = aout_FifoPop( p_aout, &p_aout->output.fifo );
419 vlc_mutex_unlock( &p_aout->output_fifo_lock );
421 p_buffer = aout_OutputNextBuffer( p_aout, next_date, 0);
425 if ( p_buffer != NULL )
427 PULSE_DEBUG( "Pulse stream request write buffer %d", p_buffer->i_nb_bytes);
428 pa_stream_write(p_sys->stream, p_buffer->p_buffer, p_buffer->i_nb_bytes, NULL, 0, PA_SEEK_RELATIVE);
429 length -= p_buffer->i_nb_bytes;
430 aout_BufferFree( p_buffer );
434 PULSE_DEBUG( "Pulse stream request write zeroes");
435 void *data = pa_xmalloc(buffer_size);
436 bzero(data, buffer_size);
437 pa_stream_write(p_sys->stream, data, buffer_size, pa_xfree, 0, PA_SEEK_RELATIVE);
438 length -= buffer_size;
440 }while(length > buffer_size);
442 pa_threaded_mainloop_signal(p_sys->mainloop, 0);
445 static void stream_latency_update_cb(pa_stream *s, void *userdata) {
446 aout_instance_t *p_aout = (aout_instance_t *)userdata;
447 struct aout_sys_t * p_sys = (struct aout_sys_t *) p_aout->output.p_sys;
451 PULSE_DEBUG( "Pulse stream latency update");
453 pa_threaded_mainloop_signal(p_sys->mainloop, 0);
456 static void success_cb(pa_stream *s, int sucess, void *userdata)
458 aout_instance_t *p_aout = (aout_instance_t *)userdata;
459 struct aout_sys_t * p_sys = (struct aout_sys_t *) p_aout->output.p_sys;
465 pa_threaded_mainloop_signal(p_sys->mainloop, 0);