Skip to content
Snippets Groups Projects
Commit 5220536b authored by Emmanuel Milou's avatar Emmanuel Milou
Browse files

Protect device ALSA operation if not opened

parent be6c72d1
Branches
Tags
No related merge requests found
...@@ -58,7 +58,6 @@ AlsaLayer::closeLayer() ...@@ -58,7 +58,6 @@ AlsaLayer::closeLayer()
if (_audioThread) if (_audioThread)
{ {
_debug("Try to stop audio thread\n"); _debug("Try to stop audio thread\n");
_audioThread->stop();
delete _audioThread; _audioThread=NULL; delete _audioThread; _audioThread=NULL;
} }
...@@ -180,13 +179,10 @@ void AlsaLayer::startCaptureStream (void) ...@@ -180,13 +179,10 @@ void AlsaLayer::startCaptureStream (void)
void AlsaLayer::prepareCaptureStream (void) void AlsaLayer::prepareCaptureStream (void)
{ {
int err; if (is_capture_open() ) {
if(snd_pcm_prepare (_CaptureHandle) < 0) _debug("Error preparing the device\n");
err = snd_pcm_prepare (_CaptureHandle); prepare_capture ();
if( err<0 ) }
_debug("Error preparing the device\n");
prepare_capture ();
} }
void AlsaLayer::stopPlaybackStream (void) void AlsaLayer::stopPlaybackStream (void)
...@@ -218,13 +214,10 @@ void AlsaLayer::startPlaybackStream (void) ...@@ -218,13 +214,10 @@ void AlsaLayer::startPlaybackStream (void)
void AlsaLayer::preparePlaybackStream (void) void AlsaLayer::preparePlaybackStream (void)
{ {
int err; if(is_playback_open()){
if( snd_pcm_prepare (_PlaybackHandle) < 0) _debug("Error preparing the device\n");
err = snd_pcm_prepare (_PlaybackHandle); prepare_playback ();
if( err<0 ) }
_debug("Error preparing the device\n");
prepare_playback ();
} }
bool AlsaLayer::alsa_set_params( snd_pcm_t *pcm_handle, int type, int rate ){ 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) ...@@ -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){ 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()); _debugAlsa("Error while opening playback device %s\n", pcm_p.c_str());
setErrorMessage( ALSA_PLAYBACK_DEVICE ); setErrorMessage( ALSA_PLAYBACK_DEVICE );
close_playback ();
return false; return false;
} }
if(!alsa_set_params( _PlaybackHandle, 1, getSampleRate() )){ if(!alsa_set_params( _PlaybackHandle, 1, getSampleRate() )){
_debug("playback failed\n"); _debug("playback failed\n");
snd_pcm_close( _PlaybackHandle ); snd_pcm_close( _PlaybackHandle );
close_playback ();
return false; return false;
} }
...@@ -359,11 +354,13 @@ AlsaLayer::open_device(std::string pcm_p, std::string pcm_c, int flag) ...@@ -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){ 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()); _debugAlsa("Error while opening capture device %s\n", pcm_c.c_str());
setErrorMessage( ALSA_CAPTURE_DEVICE ); setErrorMessage( ALSA_CAPTURE_DEVICE );
close_capture ();
return false; return false;
} }
if(!alsa_set_params( _CaptureHandle, 0, 8000 /*getSampleRate()*/ )){ if(!alsa_set_params( _CaptureHandle, 0, 8000 /*getSampleRate()*/ )){
_debug("capture failed\n"); _debug("capture failed\n");
snd_pcm_close( _CaptureHandle ); snd_pcm_close( _CaptureHandle );
close_capture ();
return false; return false;
} }
......
...@@ -44,12 +44,16 @@ static void audioCallback ( pa_stream* s, size_t bytes, void* userdata ) ...@@ -44,12 +44,16 @@ static void audioCallback ( pa_stream* s, size_t bytes, void* userdata )
// Destructor // Destructor
PulseLayer::~PulseLayer (void) PulseLayer::~PulseLayer (void)
{ {
//closeLayer();
/* Delete the pointer streams */ /* Delete the pointer streams */
delete playback; delete playback;
delete record; delete record;
pa_context_disconnect( context ); pa_context_disconnect( context );
pa_context_unref( context ); pa_context_unref( context );
sleep(2);
} }
void void
...@@ -63,6 +67,9 @@ PulseLayer::closeLayer( void ) ...@@ -63,6 +67,9 @@ PulseLayer::closeLayer( void )
while(PulseLayer::streamState != 2) while(PulseLayer::streamState != 2)
; ;
PulseLayer::streamState = 0; PulseLayer::streamState = 0;
//TODO Remove this ugly hack
sleep(2);
} }
void void
......
...@@ -57,12 +57,3 @@ void AudioThread::run (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 { ...@@ -63,8 +63,6 @@ class AudioThread : public ost::Thread {
virtual void run (void); virtual void run (void);
void stop (void);
private: private:
AudioThread (const AudioThread& at); AudioThread (const AudioThread& at);
AudioThread& operator=(const AudioThread& at); AudioThread& operator=(const AudioThread& at);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment