Commit 2b5bc243 authored by Emmanuel Milou's avatar Emmanuel Milou
Browse files

ALSA - MMAP instead of R/W ACCESS

Almost no glitch on playback ( asterisk vocal messages )
parent 76595618
......@@ -161,7 +161,7 @@ AudioLayer::fillHWBuffer( void)
data[4*l2+2] = (unsigned char)s2;
data[4*l2+3] = s2 >> 8;
}
while ((pcmreturn = snd_pcm_writei(_PlaybackHandle, data, frames)) < 0) {
while ((pcmreturn = snd_pcm_mmap_writei(_PlaybackHandle, data, frames)) < 0) {
snd_pcm_prepare(_PlaybackHandle);
//_debugAlsa("< Buffer Underrun >\n");
}
......@@ -271,7 +271,6 @@ AudioLayer::playTones( void )
write( out , maxBytes );
}
}
// free the temporary data buffer
free( out ); out = 0;
}
......@@ -311,6 +310,10 @@ AudioLayer::open_device(std::string pcm_p, std::string pcm_c, int flag)
snd_pcm_uframes_t period_size_out = getFrameSize() * getSampleRate() / 1000 * 2;//1024 ;
snd_pcm_uframes_t buffer_size_out = period_size_out * 4 ;
int fragment_size = 6144;
unsigned int buffer_time = 80000; //80ms
unsigned int period_time = buffer_time / 4; //20ms
if(flag == SFL_PCM_BOTH || flag == SFL_PCM_CAPTURE)
{
_debugAlsa(" Opening capture device %s\n", pcm_c.c_str());
......@@ -326,7 +329,7 @@ AudioLayer::open_device(std::string pcm_p, std::string pcm_c, int flag)
return false;
}
if( err = snd_pcm_hw_params_any(_CaptureHandle, hwParams) < 0) _debugAlsa(" Cannot initialize hardware parameter structure (%s)\n", snd_strerror(err));
if( err = snd_pcm_hw_params_set_access( _CaptureHandle, hwParams, SND_PCM_ACCESS_RW_INTERLEAVED) < 0) _debugAlsa(" Cannot set access type (%s)\n", snd_strerror(err));
if( err = snd_pcm_hw_params_set_access( _CaptureHandle, hwParams, SND_PCM_ACCESS_MMAP_INTERLEAVED) < 0) _debugAlsa(" Cannot set access type (%s)\n", snd_strerror(err));
if( err = snd_pcm_hw_params_set_format( _CaptureHandle, hwParams, SND_PCM_FORMAT_S16_LE) < 0) _debugAlsa(" Cannot set sample format (%s)\n", snd_strerror(err));
if( err = snd_pcm_hw_params_set_rate_near( _CaptureHandle, hwParams, &rate_in, &dir) < 0) _debugAlsa(" Cannot set sample rate (%s)\n", snd_strerror(err));
if( err = snd_pcm_hw_params_set_channels( _CaptureHandle, hwParams, 1) < 0) _debugAlsa(" Cannot set channel count (%s)\n", snd_strerror(err));
......@@ -352,12 +355,17 @@ AudioLayer::open_device(std::string pcm_p, std::string pcm_c, int flag)
return false;
}
if( err = snd_pcm_hw_params_any( _PlaybackHandle,hwParams) < 0) _debugAlsa(" Cannot initialize hardware parameter structure (%s)\n", snd_strerror(err));
if( err = snd_pcm_hw_params_set_access( _PlaybackHandle, hwParams, SND_PCM_ACCESS_RW_INTERLEAVED) < 0) _debugAlsa(" Cannot set access type (%s)\n", snd_strerror(err));
//if( err = snd_pcm_hw_params_set_access( _PlaybackHandle, hwParams, SND_PCM_ACCESS_RW_INTERLEAVED) < 0) _debugAlsa(" Cannot set access type (%s)\n", snd_strerror(err));
if( err = snd_pcm_hw_params_set_access( _PlaybackHandle, hwParams, SND_PCM_ACCESS_MMAP_INTERLEAVED) < 0) _debugAlsa(" Cannot set access type (%s)\n", snd_strerror(err));
if( err = snd_pcm_hw_params_set_format( _PlaybackHandle, hwParams, SND_PCM_FORMAT_S16_LE) < 0) _debugAlsa(" Cannot set sample format (%s)\n", snd_strerror(err));
if( err = snd_pcm_hw_params_set_rate( _PlaybackHandle, hwParams, rate_out, dir) < 0) _debugAlsa(" Cannot set sample rate (%s)\n", snd_strerror(err));
if( err = snd_pcm_hw_params_set_channels( _PlaybackHandle, hwParams, 1) < 0) _debugAlsa(" Cannot set channel count (%s)\n", snd_strerror(err));
if( err = snd_pcm_hw_params_set_period_size_near( _PlaybackHandle, hwParams, &period_size_out , &dir) < 0) _debugAlsa(" Cannot set period size (%s)\n", snd_strerror(err));
if( err = snd_pcm_hw_params_set_buffer_size_near( _PlaybackHandle, hwParams, &buffer_size_out ) < 0) _debugAlsa(" Cannot set buffer size (%s)\n", snd_strerror(err));
//if( err = snd_pcm_hw_params_set_period_size_near( _PlaybackHandle, hwParams, &period_size_out , &dir) < 0) _debugAlsa(" Cannot set period size (%s)\n", snd_strerror(err));
//if( err = snd_pcm_hw_params_set_buffer_size_near( _PlaybackHandle, hwParams, &buffer_size_out ) < 0) _debugAlsa(" Cannot set buffer size (%s)\n", snd_strerror(err));
if( err = snd_pcm_hw_params_set_buffer_time_near( _PlaybackHandle, hwParams, &buffer_time , &dir) < 0) _debugAlsa(" Cannot set buffer time (%s)\n", snd_strerror(err));
if( err = snd_pcm_hw_params_set_period_time_near( _PlaybackHandle, hwParams, &period_time , &dir) < 0) _debugAlsa(" Cannot set period time (%s)\n", snd_strerror(err));
if( err = snd_pcm_hw_params_get_period_size( hwParams, &period_size_out , &dir) < 0) _debugAlsa(" Cannot get period size (%s)\n", snd_strerror(err));
if( err = snd_pcm_hw_params_get_buffer_size( hwParams, &buffer_size_out ) < 0) _debugAlsa(" Cannot get buffer size (%s)\n", snd_strerror(err));
if( err = snd_pcm_hw_params( _PlaybackHandle, hwParams ) < 0) _debugAlsa(" Cannot set hw parameters (%s)\n", snd_strerror(err));
unsigned int hz;
......@@ -370,26 +378,26 @@ AudioLayer::open_device(std::string pcm_p, std::string pcm_c, int flag)
snd_pcm_sw_params_malloc( &swparams );
snd_pcm_sw_params_current( _PlaybackHandle, swparams );
if( err = snd_pcm_sw_params_set_start_threshold( _PlaybackHandle, swparams, period_size_out*2) < 0 ) _debugAlsa(" Cannot set start threshold (%s)\n", snd_strerror(err));
if( err = snd_pcm_sw_params_set_start_threshold( _PlaybackHandle, swparams, period_size_out) < 0 ) _debugAlsa(" Cannot set start threshold (%s)\n", snd_strerror(err));
snd_pcm_sw_params_get_start_threshold( swparams , &val);
_debug("Start threshold = %d\n" ,val);
if( err = snd_pcm_sw_params_set_stop_threshold( _PlaybackHandle, swparams, buffer_size_out ) < 0 ) _debugAlsa(" Cannot set stop threshold (%s)\n", snd_strerror(err));
snd_pcm_sw_params_get_stop_threshold( swparams , &val);
_debug("Stop threshold = %d\n" ,val);
if( err = snd_pcm_sw_params_set_start_mode( _PlaybackHandle, swparams, SND_PCM_START_DATA ) < 0 ) _debugAlsa(" Cannot set start mode (%s)\n", snd_strerror(err));
if( err = snd_pcm_sw_params_set_avail_min( _PlaybackHandle, swparams, period_size_out*2) < 0) _debugAlsa(" Cannot set min avail (%s)\n" , snd_strerror(err));
//if( err = snd_pcm_sw_params_set_stop_threshold( _PlaybackHandle, swparams, buffer_size_out ) < 0 ) _debugAlsa(" Cannot set stop threshold (%s)\n", snd_strerror(err));
//snd_pcm_sw_params_get_stop_threshold( swparams , &val);
//_debug("Stop threshold = %d\n" ,val);
if( err = snd_pcm_sw_params_set_avail_min( _PlaybackHandle, swparams, period_size_out) < 0) _debugAlsa(" Cannot set min avail (%s)\n" , snd_strerror(err));
if( err = snd_pcm_sw_params_set_xfer_align( _PlaybackHandle, swparams, 1) < 0) _debugAlsa(" Cannot set xfer align (%s)\n" , snd_strerror(err));
snd_pcm_sw_params_get_avail_min( swparams , &val);
_debug("Min available = %d\n" ,val);
//if( err = snd_pcm_sw_params_set_silence_threshold( _PlaybackHandle, swparams, period_size_out) < 0) _debugAlsa(" Cannot set silence threshold (%s)\n" , snd_strerror(err));
snd_pcm_sw_params_get_silence_threshold( swparams , &val);
_debug("Silence threshold = %d\n" ,val);
//snd_pcm_sw_params_get_silence_threshold( swparams , &val);
// _debug("Silence threshold = %d\n" ,val);
if( err = snd_pcm_sw_params( _PlaybackHandle, swparams ) < 0 ) _debugAlsa(" Cannot set sw parameters (%s)\n", snd_strerror(err));
snd_pcm_sw_params_free( swparams );
if ( err = snd_async_add_pcm_handler( &_AsyncHandler, _PlaybackHandle , AlsaCallBack, this ) < 0) _debugAlsa(" Unable to install the async callback handler (%s)\n", snd_strerror(err));
deviceClosed = false;
}
fillHWBuffer();
//fillHWBuffer();
_talk = false;
return true;
......@@ -405,7 +413,7 @@ AudioLayer::write(void* buffer, int length)
snd_pcm_uframes_t frames = snd_pcm_bytes_to_frames( _PlaybackHandle, length);
int err = snd_pcm_writei( _PlaybackHandle , buffer , frames );
int err = snd_pcm_mmap_writei( _PlaybackHandle , buffer , frames );
switch(err) {
case -EAGAIN:
_debugAlsa(" (%s)\n", snd_strerror( err ));
......@@ -414,7 +422,7 @@ AudioLayer::write(void* buffer, int length)
case -EPIPE:
_debugAlsa(" UNDERRUN (%s)\n", snd_strerror(err));
handle_xrun_playback();
snd_pcm_writei( _PlaybackHandle , buffer , frames );
snd_pcm_mmap_writei( _PlaybackHandle , buffer , frames );
break;
case -ESTRPIPE:
_debugAlsa(" (%s)\n", snd_strerror(err));
......@@ -441,7 +449,7 @@ AudioLayer::read( void* buffer, int toCopy)
if(snd_pcm_state( _CaptureHandle ) == SND_PCM_STATE_XRUN)
snd_pcm_prepare( _CaptureHandle );
snd_pcm_uframes_t frames = snd_pcm_bytes_to_frames( _CaptureHandle, toCopy );
if( err = snd_pcm_readi( _CaptureHandle, buffer, frames) < 0 ) {
if( err = snd_pcm_mmap_readi( _CaptureHandle, buffer, frames) < 0 ) {
switch(err){
case EPERM:
_debugAlsa(" Capture EPERM (%s)\n", snd_strerror(err));
......
......@@ -502,6 +502,7 @@ ManagerImpl::sendDtmf(const CallID& id, char code)
AccountID accountid = getAccountFromCall( id );
if (accountid == AccountNULL) {
_debug("Send DTMF: call doesn't exists\n");
//playDtmf(code);
return false;
}
......@@ -1285,7 +1286,7 @@ ManagerImpl::setOutputAudioPlugin(const std::string& audioPlugin)
_audiodriver -> getIndexOut(),
_audiodriver -> getSampleRate(),
_audiodriver -> getFrameSize(),
SFL_PCM_PLAYBACK,
SFL_PCM_BOTH,
audioPlugin);
if( _audiodriver -> getErrorMessage() != "")
notifyErrClient( _audiodriver -> getErrorMessage() );
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment