Commit 971c3106 authored by Emmanuel Milou's avatar Emmanuel Milou
Browse files

nothing good

parent c83068ed
...@@ -54,15 +54,25 @@ AlsaLayer::~AlsaLayer (void) ...@@ -54,15 +54,25 @@ AlsaLayer::~AlsaLayer (void)
AlsaLayer::closeLayer() AlsaLayer::closeLayer()
{ {
_debugAlsa("Close ALSA streams\n"); _debugAlsa("Close ALSA streams\n");
if (_audioThread) try {
{ /* Stop the audio thread first */
_debug("Try to stop audio thread\n"); if (_audioThread)
delete _audioThread; _audioThread=NULL; {
delete _audioThread; _audioThread=NULL;
}
} }
catch(...) {
_debugException("! ARTP Exception: when stopping audiortp\n");
throw;
}
/* Then close the audio devices */
closeCaptureStream(); closeCaptureStream();
closePlaybackStream(); closePlaybackStream();
_CaptureHandle = 0;
_PlaybackHandle = 0;
} }
bool bool
...@@ -106,6 +116,7 @@ AlsaLayer::startStream(void) ...@@ -106,6 +116,7 @@ AlsaLayer::startStream(void)
prepareCaptureStream (); prepareCaptureStream ();
startCaptureStream (); startCaptureStream ();
startPlaybackStream (); startPlaybackStream ();
} }
void void
...@@ -116,38 +127,36 @@ AlsaLayer::stopStream(void) ...@@ -116,38 +127,36 @@ AlsaLayer::stopStream(void)
//stopPlaybackStream (); //stopPlaybackStream ();
/* Flush the ring buffers */ /* Flush the ring buffers */
flushUrgent(); flushUrgent ();
flushMain(); flushMain ();
flushMic ();
} }
int int
AlsaLayer::canGetMic() AlsaLayer::canGetMic()
{ {
int avail; if(_CaptureHandle)
return _micRingBuffer.AvailForGet();
if (! _CaptureHandle )
return 0;
avail = snd_pcm_avail_update( _CaptureHandle );
if( avail == -EPIPE ){
stop_capture ();
return 0;
}
else else
return ((avail < 0)? 0:avail); return 0;
} }
int int
AlsaLayer::getMic(void *buffer, int toCopy) AlsaLayer::getMic(void *buffer, int toCopy)
{ {
int res = 0 ; if( _CaptureHandle ){
if( _CaptureHandle ) return _micRingBuffer.Get(buffer, toCopy,100);
{ }
res = read( buffer, toCopy ); else
adjustVolume( buffer , toCopy , SFL_PCM_CAPTURE ); return 0;
} }
return res ;
bool AlsaLayer::isCaptureActive(void) {
ost::MutexLock guard( _mutex );
if( _CaptureHandle )
return (snd_pcm_state( _CaptureHandle) == SND_PCM_STATE_RUNNING ? true : false);
else
return false;
} }
////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////
...@@ -175,6 +184,7 @@ void AlsaLayer::closeCaptureStream (void) ...@@ -175,6 +184,7 @@ void AlsaLayer::closeCaptureStream (void)
void AlsaLayer::startCaptureStream (void) void AlsaLayer::startCaptureStream (void)
{ {
if(_CaptureHandle){ if(_CaptureHandle){
_debug("Start the capture\n");
snd_pcm_start (_CaptureHandle); snd_pcm_start (_CaptureHandle);
start_capture(); start_capture();
} }
...@@ -183,6 +193,7 @@ void AlsaLayer::startCaptureStream (void) ...@@ -183,6 +193,7 @@ void AlsaLayer::startCaptureStream (void)
void AlsaLayer::prepareCaptureStream (void) void AlsaLayer::prepareCaptureStream (void)
{ {
if (is_capture_open() ) { if (is_capture_open() ) {
_debug("Prepare the capture\n");
if(snd_pcm_prepare (_CaptureHandle) < 0) _debug("Error preparing the device\n"); if(snd_pcm_prepare (_CaptureHandle) < 0) _debug("Error preparing the device\n");
prepare_capture (); prepare_capture ();
} }
...@@ -360,7 +371,7 @@ AlsaLayer::open_device(std::string pcm_p, std::string pcm_c, int flag) ...@@ -360,7 +371,7 @@ AlsaLayer::open_device(std::string pcm_p, std::string pcm_c, int flag)
close_capture (); close_capture ();
return false; return false;
} }
if(!alsa_set_params( _CaptureHandle, 0, 8000 /*getSampleRate()*/ )){ if(!alsa_set_params( _CaptureHandle, 0, 8000 )){
_debug("capture failed\n"); _debug("capture failed\n");
snd_pcm_close( _CaptureHandle ); snd_pcm_close( _CaptureHandle );
close_capture (); close_capture ();
...@@ -368,12 +379,16 @@ AlsaLayer::open_device(std::string pcm_p, std::string pcm_c, int flag) ...@@ -368,12 +379,16 @@ AlsaLayer::open_device(std::string pcm_p, std::string pcm_c, int flag)
} }
open_capture (); open_capture ();
prepare_capture ();
startCaptureStream ();
} }
/* Start the secondary audio thread for callbacks */ /* Start the secondary audio thread for callbacks */
_audioThread->start(); try {
_audioThread->start();
}
catch(...){
_debug("Fail to start audio thread\n");
}
return true; return true;
} }
...@@ -382,8 +397,6 @@ AlsaLayer::open_device(std::string pcm_p, std::string pcm_c, int flag) ...@@ -382,8 +397,6 @@ AlsaLayer::open_device(std::string pcm_p, std::string pcm_c, int flag)
int int
AlsaLayer::write(void* buffer, int length) AlsaLayer::write(void* buffer, int length)
{ {
if (_trigger_request == true) if (_trigger_request == true)
{ {
_trigger_request = false; _trigger_request = false;
...@@ -417,13 +430,12 @@ AlsaLayer::write(void* buffer, int length) ...@@ -417,13 +430,12 @@ AlsaLayer::write(void* buffer, int length)
int int
AlsaLayer::read( void* buffer, int toCopy) AlsaLayer::read( void* buffer, int toCopy)
{ {
ost::MutexLock lock( _mutex ); //ost::MutexLock lock( _mutex );
int samples; int samples;
if(snd_pcm_state( _CaptureHandle ) == SND_PCM_STATE_XRUN) if(snd_pcm_state( _CaptureHandle ) == SND_PCM_STATE_XRUN)
{ {
_debug("xrun caught before start\n");
prepareCaptureStream (); prepareCaptureStream ();
startCaptureStream (); startCaptureStream ();
} }
...@@ -437,12 +449,16 @@ AlsaLayer::read( void* buffer, int toCopy) ...@@ -437,12 +449,16 @@ AlsaLayer::read( void* buffer, int toCopy)
case -EIO: case -EIO:
_debugAlsa(" XRUN capture ignored (%s)\n", snd_strerror(samples)); _debugAlsa(" XRUN capture ignored (%s)\n", snd_strerror(samples));
handle_xrun_capture(); handle_xrun_capture();
samples = snd_pcm_readi( _CaptureHandle, buffer, frames); //samples = snd_pcm_readi( _CaptureHandle, buffer, frames);
if (samples<0) samples=0; //if (samples<0) samples=0;
break;
case EPERM:
_debugAlsa(" Capture EPERM (%s)\n", snd_strerror(samples));
prepareCaptureStream ();
startCaptureStream ();
break; break;
default: default:
//_debugAlsa ("Error when capturing data ***********************************************: %s\n", snd_strerror(samples)); _debugAlsa("%s\n", snd_strerror(samples));
stopCaptureStream();
break; break;
} }
return 0; return 0;
...@@ -599,11 +615,12 @@ AlsaLayer::soundCardGetIndex( std::string description ) ...@@ -599,11 +615,12 @@ AlsaLayer::soundCardGetIndex( std::string description )
void AlsaLayer::audioCallback (void) void AlsaLayer::audioCallback (void)
{ {
int toGet, toPut, urgentAvail, normalAvail, micAvailPut, maxBytes; int toGet, toPut, urgentAvail, normalAvail, micAvailAlsa, micAvailPut, maxBytes;
unsigned short spkrVolume, micVolume; unsigned short spkrVolume, micVolume;
AudioLoop *tone; AudioLoop *tone;
SFLDataFormat *out; SFLDataFormat *out;
SFLDataFormat *in;
spkrVolume = _manager->getSpkrVolume(); spkrVolume = _manager->getSpkrVolume();
micVolume = _manager->getMicVolume(); micVolume = _manager->getMicVolume();
...@@ -650,13 +667,21 @@ void AlsaLayer::audioCallback (void) ...@@ -650,13 +667,21 @@ void AlsaLayer::audioCallback (void)
} }
// Additionally handle the mic's audio stream // Additionally handle the mic's audio stream
//micAvailPut = _micRingBuffer.AvailForPut(); //if(is_capture_running()){
//toPut = (micAvailPut <= (int)(framesPerBufferAlsa * sizeof(SFLDataFormat))) ? micAvailPut : framesPerBufferAlsa * sizeof(SFLDataFormat); micAvailAlsa = snd_pcm_avail_update(_CaptureHandle);
//_debug("AL: Nb sample: %d char, [0]=%f [1]=%f [2]=%f\n", toPut, in[0], in[1], in[2]); if(micAvailAlsa > 0) {
//_micRingBuffer.Put(in, toPut, micVolume); micAvailPut = _micRingBuffer.AvailForPut();
toPut = (micAvailAlsa <= micAvailPut) ? micAvailAlsa : micAvailPut;
in = (SFLDataFormat*)malloc(toPut * sizeof(SFLDataFormat));
toPut = read (in, toPut);
if (in != 0)
{
_micRingBuffer.Put(in, toPut, 100);
}
free(in); in=0;
}
} }
void* AlsaLayer::adjustVolume( void* buffer , int len, int stream ) void* AlsaLayer::adjustVolume( void* buffer , int len, int stream )
{ {
int vol, i, size; int vol, i, size;
......
...@@ -139,7 +139,7 @@ class AlsaLayer : public AudioLayer { ...@@ -139,7 +139,7 @@ class AlsaLayer : public AudioLayer {
void audioCallback (void); void audioCallback (void);
bool isCaptureActive (void) { return is_capture_running (); } bool isCaptureActive (void);
private: private:
......
...@@ -89,8 +89,8 @@ AudioRtp::closeRtpSession () { ...@@ -89,8 +89,8 @@ AudioRtp::closeRtpSession () {
_debugException("! ARTP Exception: when stopping audiortp\n"); _debugException("! ARTP Exception: when stopping audiortp\n");
throw; throw;
} }
//AudioLayer* audiolayer = Manager::instance().getAudioDriver(); AudioLayer* audiolayer = Manager::instance().getAudioDriver();
//audiolayer->stopStream(); audiolayer->stopStream();
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
...@@ -240,7 +240,7 @@ AudioRtpRTX::sendSessionFromMic(int timestamp) ...@@ -240,7 +240,7 @@ AudioRtpRTX::sendSessionFromMic(int timestamp)
// 2. convert it to int16 - good sample, good rate // 2. convert it to int16 - good sample, good rate
// 3. encode it // 3. encode it
// 4. send it // 4. send it
try { //try {
timestamp += time->getSecond(); timestamp += time->getSecond();
if (_ca==0) { _debug(" !ARTP: No call associated (mic)\n"); return; } // no call, so we do nothing if (_ca==0) { _debug(" !ARTP: No call associated (mic)\n"); return; } // no call, so we do nothing
...@@ -279,10 +279,10 @@ AudioRtpRTX::sendSessionFromMic(int timestamp) ...@@ -279,10 +279,10 @@ AudioRtpRTX::sendSessionFromMic(int timestamp)
} else { } else {
_session->putData(timestamp, micDataEncoded, compSize); _session->putData(timestamp, micDataEncoded, compSize);
} }
} catch(...) { /*} catch(...) {
_debugException("! ARTP: sending failed"); _debugException("! ARTP: sending failed");
throw; throw;
} }*/
} }
void void
...@@ -291,7 +291,7 @@ AudioRtpRTX::receiveSessionForSpkr (int& countTime) ...@@ -291,7 +291,7 @@ AudioRtpRTX::receiveSessionForSpkr (int& countTime)
if (_ca == 0) { return; } if (_ca == 0) { return; }
try { //try {
AudioLayer* audiolayer = Manager::instance().getAudioDriver(); AudioLayer* audiolayer = Manager::instance().getAudioDriver();
if (!audiolayer) { return; } if (!audiolayer) { return; }
...@@ -357,11 +357,10 @@ AudioRtpRTX::receiveSessionForSpkr (int& countTime) ...@@ -357,11 +357,10 @@ AudioRtpRTX::receiveSessionForSpkr (int& countTime)
countTime += time->getSecond(); countTime += time->getSecond();
} }
delete adu; adu = NULL; delete adu; adu = NULL;
} catch(...) { //} catch(...) {
_debugException("! ARTP: receiving failed"); //_debugException("! ARTP: receiving failed");
throw; //throw;
} //}
} }
...@@ -388,7 +387,7 @@ AudioRtpRTX::run () { ...@@ -388,7 +387,7 @@ AudioRtpRTX::run () {
initBuffers(); initBuffers();
int step; int step;
try { //try {
// Init the session // Init the session
initAudioRtpSession(); initAudioRtpSession();
step = (int) (_layerFrameSize * _codecSampleRate / 1000); step = (int) (_layerFrameSize * _codecSampleRate / 1000);
...@@ -425,15 +424,16 @@ AudioRtpRTX::run () { ...@@ -425,15 +424,16 @@ AudioRtpRTX::run () {
} }
//_debug("stop stream for audiortp loop\n"); //_debug("stop stream for audiortp loop\n");
audiolayer->stopStream(); audiolayer->stopStream();
} catch(std::exception &e) { _debug("- ARTP Action: Stop\n");
_start.post(); //} catch(std::exception &e) {
_debug("! ARTP: Stop %s\n", e.what()); //_start.post();
throw; //_debug("! ARTP: Stop %s\n", e.what());
} catch(...) { //throw;
_start.post(); //} catch(...) {
_debugException("* ARTP Action: Stop"); //_start.post();
throw; //_debugException("* ARTP Action: Stop");
} //throw;
//}
} }
......
...@@ -248,7 +248,7 @@ IAXVoIPLink::sendAudioFromMic(void) ...@@ -248,7 +248,7 @@ IAXVoIPLink::sendAudioFromMic(void)
// Audio codec still not determined. // Audio codec still not determined.
if (audiolayer) { if (audiolayer) {
// To keep latency low.. // To keep latency low..
//audiolayer->flushMic(); audiolayer->flushMic();
} }
return; return;
} }
...@@ -412,8 +412,8 @@ IAXVoIPLink::answer(const CallID& id) ...@@ -412,8 +412,8 @@ IAXVoIPLink::answer(const CallID& id)
call->setState(Call::Active); call->setState(Call::Active);
call->setConnectionState(Call::Connected); call->setConnectionState(Call::Connected);
// Start audio // Start audio
audiolayer->flushMic();
audiolayer->startStream(); audiolayer->startStream();
//audiolayer->flushMic();
return true; return true;
} }
...@@ -621,7 +621,7 @@ IAXVoIPLink::iaxHandleCallEvent(iax_event* event, IAXCall* call) ...@@ -621,7 +621,7 @@ IAXVoIPLink::iaxHandleCallEvent(iax_event* event, IAXCall* call)
} }
Manager::instance().peerAnsweredCall(id); Manager::instance().peerAnsweredCall(id);
//audiolayer->flushMic(); audiolayer->flushMic();
audiolayer->startStream(); audiolayer->startStream();
// start audio here? // start audio here?
} else { } else {
...@@ -637,8 +637,8 @@ IAXVoIPLink::iaxHandleCallEvent(iax_event* event, IAXCall* call) ...@@ -637,8 +637,8 @@ IAXVoIPLink::iaxHandleCallEvent(iax_event* event, IAXCall* call)
break; break;
case IAX_EVENT_VOICE: case IAX_EVENT_VOICE:
if (!audiolayer->isCaptureActive ()) //if (!audiolayer->isCaptureActive ())
audiolayer->startStream (); // audiolayer->startStream ();
iaxHandleVoiceEvent(event, call); iaxHandleVoiceEvent(event, call);
break; break;
......
...@@ -522,7 +522,7 @@ SIPVoIPLink::onhold(const CallID& id) ...@@ -522,7 +522,7 @@ SIPVoIPLink::onhold(const CallID& id)
call->setState(Call::Hold); call->setState(Call::Hold);
_debug("* SIP Info: Stopping AudioRTP for onhold action\n"); _debug("* SIP Info: Stopping AudioRTP for onhold action\n");
//_mutexSIP.enterMutex(); //_mutexSIP.enterMutex();
_audiortp->closeRtpSession(); _audiortp->closeRtpSession();
//_mutexSIP.leaveMutex(); //_mutexSIP.leaveMutex();
local_sdp = call->getLocalSDPSession(); local_sdp = call->getLocalSDPSession();
......
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