Commit 5220536b authored by Emmanuel Milou's avatar Emmanuel Milou
Browse files

Protect device ALSA operation if not opened

parent be6c72d1
......@@ -58,7 +58,6 @@ AlsaLayer::closeLayer()
if (_audioThread)
{
_debug("Try to stop audio thread\n");
_audioThread->stop();
delete _audioThread; _audioThread=NULL;
}
......@@ -180,13 +179,10 @@ void AlsaLayer::startCaptureStream (void)
void AlsaLayer::prepareCaptureStream (void)
{
int err;
err = snd_pcm_prepare (_CaptureHandle);
if( err<0 )
_debug("Error preparing the device\n");
prepare_capture ();
if (is_capture_open() ) {
if(snd_pcm_prepare (_CaptureHandle) < 0) _debug("Error preparing the device\n");
prepare_capture ();
}
}
void AlsaLayer::stopPlaybackStream (void)
......@@ -218,13 +214,10 @@ void AlsaLayer::startPlaybackStream (void)
void AlsaLayer::preparePlaybackStream (void)
{
int err;
err = snd_pcm_prepare (_PlaybackHandle);
if( err<0 )
_debug("Error preparing the device\n");
prepare_playback ();
if(is_playback_open()){
if( snd_pcm_prepare (_PlaybackHandle) < 0) _debug("Error preparing the device\n");
prepare_playback ();
}
}
bool AlsaLayer::alsa_set_params( snd_pcm_t *pcm_handle, int type, int rate ){
......@@ -343,11 +336,13 @@ AlsaLayer::open_device(std::string pcm_p, std::string pcm_c, int flag)
if((err = snd_pcm_open(&_PlaybackHandle, pcm_p.c_str(), SND_PCM_STREAM_PLAYBACK, 0 )) < 0){
_debugAlsa("Error while opening playback device %s\n", pcm_p.c_str());
setErrorMessage( ALSA_PLAYBACK_DEVICE );
close_playback ();
return false;
}
if(!alsa_set_params( _PlaybackHandle, 1, getSampleRate() )){
_debug("playback failed\n");
snd_pcm_close( _PlaybackHandle );
close_playback ();
return false;
}
......@@ -359,11 +354,13 @@ AlsaLayer::open_device(std::string pcm_p, std::string pcm_c, int flag)
if( (err = snd_pcm_open(&_CaptureHandle, pcm_c.c_str(), SND_PCM_STREAM_CAPTURE, 0)) < 0){
_debugAlsa("Error while opening capture device %s\n", pcm_c.c_str());
setErrorMessage( ALSA_CAPTURE_DEVICE );
close_capture ();
return false;
}
if(!alsa_set_params( _CaptureHandle, 0, 8000 /*getSampleRate()*/ )){
_debug("capture failed\n");
snd_pcm_close( _CaptureHandle );
close_capture ();
return false;
}
......
......@@ -44,12 +44,16 @@ static void audioCallback ( pa_stream* s, size_t bytes, void* userdata )
// Destructor
PulseLayer::~PulseLayer (void)
{
//closeLayer();
/* Delete the pointer streams */
delete playback;
delete record;
pa_context_disconnect( context );
pa_context_unref( context );
sleep(2);
}
void
......@@ -63,6 +67,9 @@ PulseLayer::closeLayer( void )
while(PulseLayer::streamState != 2)
;
PulseLayer::streamState = 0;
//TODO Remove this ugly hack
sleep(2);
}
void
......
......@@ -57,12 +57,3 @@ void AudioThread::run (void)
}
}
void AudioThread::stop (void)
{
try{
terminate();
} catch(...){
_debugException("! ARTP: Thread destructor didn't terminate correctly");
throw;
}
}
......@@ -63,8 +63,6 @@ class AudioThread : public ost::Thread {
virtual void run (void);
void stop (void);
private:
AudioThread (const AudioThread& at);
AudioThread& operator=(const AudioThread& at);
......
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