2004-07-16 19:56:36 +00:00
/* $Id$ */
/***
This file is part of polypaudio .
polypaudio is free software ; you can redistribute it and / or modify
2004-11-14 14:58:54 +00:00
it under the terms of the GNU Lesser General Public License as published
2004-07-16 19:56:36 +00:00
by the Free Software Foundation ; either version 2 of the License ,
or ( at your option ) any later version .
polypaudio is distributed in the hope that it will be useful , but
WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the GNU
General Public License for more details .
2004-11-14 14:58:54 +00:00
You should have received a copy of the GNU Lesser General Public License
2004-07-16 19:56:36 +00:00
along with polypaudio ; if not , write to the Free Software
Foundation , Inc . , 59 Temple Place , Suite 330 , Boston , MA 02111 - 1307
USA .
* * */
2004-07-16 19:16:42 +00:00
# ifdef HAVE_CONFIG_H
# include <config.h>
# endif
2004-07-16 00:27:02 +00:00
# include <assert.h>
# include <stdio.h>
# include <sys/poll.h>
# include <asoundlib.h>
# include "module.h"
# include "core.h"
# include "memchunk.h"
# include "sink.h"
# include "modargs.h"
# include "util.h"
# include "sample-util.h"
2004-07-16 17:03:11 +00:00
# include "alsa-util.h"
2004-08-04 16:39:30 +00:00
# include "xmalloc.h"
2004-09-05 00:03:16 +00:00
# include "log.h"
2004-10-30 01:55:16 +00:00
# include "module-alsa-sink-symdef.h"
2004-07-16 00:27:02 +00:00
2004-09-11 23:17:38 +00:00
PA_MODULE_AUTHOR ( " Lennart Poettering " )
PA_MODULE_DESCRIPTION ( " ALSA Sink " )
PA_MODULE_VERSION ( PACKAGE_VERSION )
2004-09-20 17:19:35 +00:00
PA_MODULE_USAGE ( " sink_name=<name for the sink> device=<ALSA device> format=<sample format> channels=<number of channels> rate=<sample rate> fragments=<number of fragments> fragment_size=<fragment size> " )
2004-09-11 23:17:38 +00:00
2004-07-16 00:27:02 +00:00
struct userdata {
snd_pcm_t * pcm_handle ;
struct pa_sink * sink ;
2004-08-05 19:53:57 +00:00
struct pa_io_event * * io_events ;
unsigned n_io_events ;
2004-07-16 00:27:02 +00:00
size_t frame_size , fragment_size ;
struct pa_memchunk memchunk , silence ;
2004-08-04 16:39:30 +00:00
struct pa_module * module ;
2004-07-16 00:27:02 +00:00
} ;
static const char * const valid_modargs [ ] = {
" device " ,
" sink_name " ,
2004-07-16 14:43:25 +00:00
" format " ,
" channels " ,
" rate " ,
" fragments " ,
" fragment_size " ,
2004-07-16 00:27:02 +00:00
NULL
} ;
# define DEFAULT_SINK_NAME "alsa_output"
# define DEFAULT_DEVICE "plughw:0,0"
2004-08-04 16:39:30 +00:00
static void update_usage ( struct userdata * u ) {
pa_module_set_used ( u - > module ,
( u - > sink ? pa_idxset_ncontents ( u - > sink - > inputs ) : 0 ) +
( u - > sink ? pa_idxset_ncontents ( u - > sink - > monitor_source - > outputs ) : 0 ) ) ;
}
2004-07-16 00:27:02 +00:00
static void xrun_recovery ( struct userdata * u ) {
assert ( u ) ;
2004-09-05 00:03:16 +00:00
pa_log ( __FILE__ " : *** ALSA-XRUN (playback) *** \n " ) ;
2004-07-16 00:27:02 +00:00
if ( snd_pcm_prepare ( u - > pcm_handle ) < 0 )
2004-09-05 00:03:16 +00:00
pa_log ( __FILE__ " : snd_pcm_prepare() failed \n " ) ;
2004-07-16 00:27:02 +00:00
}
static void do_write ( struct userdata * u ) {
assert ( u ) ;
2004-08-04 16:39:30 +00:00
update_usage ( u ) ;
2004-07-16 00:27:02 +00:00
for ( ; ; ) {
struct pa_memchunk * memchunk = NULL ;
snd_pcm_sframes_t frames ;
if ( u - > memchunk . memblock )
memchunk = & u - > memchunk ;
else {
if ( pa_sink_render ( u - > sink , u - > fragment_size , & u - > memchunk ) < 0 )
memchunk = & u - > silence ;
else
memchunk = & u - > memchunk ;
}
assert ( memchunk - > memblock & & memchunk - > memblock - > data & & memchunk - > length & & memchunk - > memblock - > length & & ( memchunk - > length % u - > frame_size ) = = 0 ) ;
2004-09-01 00:23:51 +00:00
if ( ( frames = snd_pcm_writei ( u - > pcm_handle , ( uint8_t * ) memchunk - > memblock - > data + memchunk - > index , memchunk - > length / u - > frame_size ) ) < 0 ) {
2004-07-16 17:03:11 +00:00
if ( frames = = - EAGAIN )
return ;
2004-07-16 00:27:02 +00:00
if ( frames = = - EPIPE ) {
xrun_recovery ( u ) ;
continue ;
}
2004-09-05 00:03:16 +00:00
pa_log ( __FILE__ " : snd_pcm_writei() failed \n " ) ;
2004-07-16 00:27:02 +00:00
return ;
}
if ( memchunk = = & u - > memchunk ) {
2004-07-16 17:03:11 +00:00
size_t l = frames * u - > frame_size ;
memchunk - > index + = l ;
memchunk - > length - = l ;
2004-07-16 00:27:02 +00:00
if ( memchunk - > length = = 0 ) {
pa_memblock_unref ( memchunk - > memblock ) ;
memchunk - > memblock = NULL ;
memchunk - > index = memchunk - > length = 0 ;
}
}
break ;
}
}
2004-08-05 19:53:57 +00:00
static void io_callback ( struct pa_mainloop_api * a , struct pa_io_event * e , int fd , enum pa_io_event_flags f , void * userdata ) {
2004-07-16 00:27:02 +00:00
struct userdata * u = userdata ;
2004-08-05 19:53:57 +00:00
assert ( u & & a & & e ) ;
2004-07-16 00:27:02 +00:00
if ( snd_pcm_state ( u - > pcm_handle ) = = SND_PCM_STATE_XRUN )
xrun_recovery ( u ) ;
do_write ( u ) ;
}
2004-09-12 13:14:49 +00:00
static pa_usec_t sink_get_latency_cb ( struct pa_sink * s ) {
2004-09-13 13:26:44 +00:00
pa_usec_t r = 0 ;
2004-07-16 00:27:02 +00:00
struct userdata * u = s - > userdata ;
snd_pcm_sframes_t frames ;
assert ( s & & u & & u - > sink ) ;
if ( snd_pcm_delay ( u - > pcm_handle , & frames ) < 0 ) {
2004-09-05 00:03:16 +00:00
pa_log ( __FILE__ " : failed to get delay \n " ) ;
2004-07-16 00:27:02 +00:00
s - > get_latency = NULL ;
return 0 ;
}
if ( frames < 0 )
frames = 0 ;
2004-09-13 13:26:44 +00:00
r + = pa_bytes_to_usec ( frames * u - > frame_size , & s - > sample_spec ) ;
if ( u - > memchunk . memblock )
r + = pa_bytes_to_usec ( u - > memchunk . length , & s - > sample_spec ) ;
return r ;
2004-07-16 00:27:02 +00:00
}
2004-09-11 23:17:38 +00:00
int pa__init ( struct pa_core * c , struct pa_module * m ) {
2004-07-16 00:27:02 +00:00
struct pa_modargs * ma = NULL ;
int ret = - 1 ;
struct userdata * u = NULL ;
const char * dev ;
struct pa_sample_spec ss ;
2004-11-20 22:17:31 +00:00
uint32_t periods , fragsize ;
snd_pcm_uframes_t period_size ;
2004-07-16 14:43:25 +00:00
size_t frame_size ;
2004-07-20 01:07:06 +00:00
2004-07-16 00:27:02 +00:00
if ( ! ( ma = pa_modargs_new ( m - > argument , valid_modargs ) ) ) {
2004-09-05 00:03:16 +00:00
pa_log ( __FILE__ " : failed to parse module arguments \n " ) ;
2004-07-16 00:27:02 +00:00
goto fail ;
}
2004-07-16 14:43:25 +00:00
ss = c - > default_sample_spec ;
if ( pa_modargs_get_sample_spec ( ma , & ss ) < 0 ) {
2004-09-05 00:03:16 +00:00
pa_log ( __FILE__ " : failed to parse sample specification \n " ) ;
2004-07-16 14:43:25 +00:00
goto fail ;
}
2004-08-03 19:26:56 +00:00
frame_size = pa_frame_size ( & ss ) ;
2004-07-16 14:43:25 +00:00
periods = 12 ;
fragsize = 1024 ;
if ( pa_modargs_get_value_u32 ( ma , " fragments " , & periods ) < 0 | | pa_modargs_get_value_u32 ( ma , " fragment_size " , & fragsize ) < 0 ) {
2004-09-05 00:03:16 +00:00
pa_log ( __FILE__ " : failed to parse buffer metrics \n " ) ;
2004-07-16 14:43:25 +00:00
goto fail ;
}
2004-11-20 22:17:31 +00:00
period_size = fragsize ;
2004-07-16 00:27:02 +00:00
2004-08-04 16:39:30 +00:00
u = pa_xmalloc0 ( sizeof ( struct userdata ) ) ;
2004-07-16 00:27:02 +00:00
m - > userdata = u ;
2004-08-04 16:39:30 +00:00
u - > module = m ;
2004-07-16 00:27:02 +00:00
if ( snd_pcm_open ( & u - > pcm_handle , dev = pa_modargs_get_value ( ma , " device " , DEFAULT_DEVICE ) , SND_PCM_STREAM_PLAYBACK , SND_PCM_NONBLOCK ) < 0 ) {
2004-09-05 00:03:16 +00:00
pa_log ( __FILE__ " : Error opening PCM device %s \n " , dev ) ;
2004-07-16 00:27:02 +00:00
goto fail ;
}
2004-11-20 22:17:31 +00:00
if ( pa_alsa_set_hw_params ( u - > pcm_handle , & ss , & periods , & period_size ) < 0 ) {
2004-09-05 00:03:16 +00:00
pa_log ( __FILE__ " : Failed to set hardware parameters \n " ) ;
2004-07-16 00:27:02 +00:00
goto fail ;
}
u - > sink = pa_sink_new ( c , pa_modargs_get_value ( ma , " sink_name " , DEFAULT_SINK_NAME ) , 0 , & ss ) ;
assert ( u - > sink ) ;
u - > sink - > get_latency = sink_get_latency_cb ;
u - > sink - > userdata = u ;
pa_sink_set_owner ( u - > sink , m ) ;
u - > sink - > description = pa_sprintf_malloc ( " Advanced Linux Sound Architecture PCM on '%s' " , dev ) ;
2004-08-05 19:53:57 +00:00
if ( pa_create_io_events ( u - > pcm_handle , c - > mainloop , & u - > io_events , & u - > n_io_events , io_callback , u ) < 0 ) {
2004-09-05 00:03:16 +00:00
pa_log ( __FILE__ " : failed to obtain file descriptors \n " ) ;
2004-07-16 00:27:02 +00:00
goto fail ;
}
2004-07-16 14:43:25 +00:00
u - > frame_size = frame_size ;
2004-11-20 22:17:31 +00:00
u - > fragment_size = period_size ;
2004-07-16 00:27:02 +00:00
2004-09-05 00:03:16 +00:00
pa_log ( __FILE__ " : using %u fragments of size %u bytes. \n " , periods , u - > fragment_size ) ;
2004-07-16 00:27:02 +00:00
2004-08-17 19:37:29 +00:00
u - > silence . memblock = pa_memblock_new ( u - > silence . length = u - > fragment_size , c - > memblock_stat ) ;
2004-07-16 00:27:02 +00:00
assert ( u - > silence . memblock ) ;
pa_silence_memblock ( u - > silence . memblock , & ss ) ;
u - > silence . index = 0 ;
u - > memchunk . memblock = NULL ;
u - > memchunk . index = u - > memchunk . length = 0 ;
ret = 0 ;
finish :
if ( ma )
pa_modargs_free ( ma ) ;
return ret ;
fail :
if ( u )
2004-09-11 23:17:38 +00:00
pa__done ( c , m ) ;
2004-07-16 00:27:02 +00:00
goto finish ;
}
2004-09-11 23:17:38 +00:00
void pa__done ( struct pa_core * c , struct pa_module * m ) {
2004-07-16 00:27:02 +00:00
struct userdata * u ;
assert ( c & & m ) ;
2004-08-04 16:39:30 +00:00
if ( ! ( u = m - > userdata ) )
return ;
2004-09-14 20:53:25 +00:00
if ( u - > sink ) {
pa_sink_disconnect ( u - > sink ) ;
pa_sink_unref ( u - > sink ) ;
}
2004-08-04 16:39:30 +00:00
2004-08-05 19:53:57 +00:00
if ( u - > io_events )
pa_free_io_events ( c - > mainloop , u - > io_events , u - > n_io_events ) ;
2004-08-04 16:39:30 +00:00
if ( u - > pcm_handle ) {
snd_pcm_drop ( u - > pcm_handle ) ;
snd_pcm_close ( u - > pcm_handle ) ;
2004-07-16 00:27:02 +00:00
}
2004-08-04 16:39:30 +00:00
if ( u - > memchunk . memblock )
pa_memblock_unref ( u - > memchunk . memblock ) ;
if ( u - > silence . memblock )
pa_memblock_unref ( u - > silence . memblock ) ;
pa_xfree ( u ) ;
2004-07-16 00:27:02 +00:00
}