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

remove async_pcm_handler; replaced by an extra thread

parent d7195d17
No related branches found
No related tags found
No related merge requests found
...@@ -21,6 +21,11 @@ ...@@ -21,6 +21,11 @@
#include "alsalayer.h" #include "alsalayer.h"
void* ringtoneThreadEntry( void *ptr);
static pthread_t ringtone_thread;
bool ringtone_thread_is_running;
// Constructor // Constructor
AlsaLayer::AlsaLayer( ManagerImpl* manager ) AlsaLayer::AlsaLayer( ManagerImpl* manager )
: AudioLayer( manager , ALSA ) : AudioLayer( manager , ALSA )
...@@ -35,6 +40,9 @@ ...@@ -35,6 +40,9 @@
, IDSoundCards() , IDSoundCards()
{ {
_debug(" Constructor of AlsaLayer called\n"); _debug(" Constructor of AlsaLayer called\n");
// The flag to stop the ringtone thread loop
ringtone_thread_is_running = false;
} }
// Destructor // Destructor
...@@ -44,6 +52,9 @@ AlsaLayer::~AlsaLayer (void) ...@@ -44,6 +52,9 @@ AlsaLayer::~AlsaLayer (void)
closeCaptureStream(); closeCaptureStream();
closePlaybackStream(); closePlaybackStream();
deviceClosed = true; deviceClosed = true;
ringtone_thread_is_running = false;
pthread_join(ringtone_thread, NULL);
} }
void void
...@@ -53,6 +64,8 @@ AlsaLayer::closeLayer() ...@@ -53,6 +64,8 @@ AlsaLayer::closeLayer()
closeCaptureStream(); closeCaptureStream();
closePlaybackStream(); closePlaybackStream();
deviceClosed = true; deviceClosed = true;
ringtone_thread_is_running = false;
} }
bool bool
...@@ -132,11 +145,18 @@ AlsaLayer::stopStream(void) ...@@ -132,11 +145,18 @@ AlsaLayer::stopStream(void)
} }
} }
void AlsaLayer::AlsaCallBack( snd_async_handler_t* pcm_callback )
void* ringtoneThreadEntry( void *ptr )
{ {
( ( AlsaLayer *)snd_async_handler_get_callback_private( pcm_callback )) -> playTones(); while( ringtone_thread_is_running )
{
( ( AlsaLayer *) ptr) -> playTones();
}
return 0;
} }
void void
AlsaLayer::fillHWBuffer( void) AlsaLayer::fillHWBuffer( void)
{ {
...@@ -248,6 +268,7 @@ void AlsaLayer::restorePulseAppsVolume( void ){} ...@@ -248,6 +268,7 @@ void AlsaLayer::restorePulseAppsVolume( void ){}
void void
AlsaLayer::playTones( void ) AlsaLayer::playTones( void )
{ {
int frames = _periodSize ; int frames = _periodSize ;
int maxBytes = frames * sizeof(SFLDataFormat) ; int maxBytes = frames * sizeof(SFLDataFormat) ;
SFLDataFormat* out = (SFLDataFormat*)malloc(maxBytes * sizeof(SFLDataFormat)); SFLDataFormat* out = (SFLDataFormat*)malloc(maxBytes * sizeof(SFLDataFormat));
...@@ -268,6 +289,7 @@ AlsaLayer::playTones( void ) ...@@ -268,6 +289,7 @@ AlsaLayer::playTones( void )
free( out ); out = 0; free( out ); out = 0;
} }
bool bool
AlsaLayer::isPlaybackActive(void) { AlsaLayer::isPlaybackActive(void) {
ost::MutexLock guard( _mutex ); ost::MutexLock guard( _mutex );
...@@ -383,10 +405,21 @@ bool AlsaLayer::alsa_set_params( snd_pcm_t *pcm_handle, int type, int rate ){ ...@@ -383,10 +405,21 @@ bool AlsaLayer::alsa_set_params( snd_pcm_t *pcm_handle, int type, int rate ){
return false; return false;
} }
if( type == 1 ){ if( type == 1 ){
if( (err = snd_async_add_pcm_handler( &_AsyncHandler, pcm_handle , AlsaCallBack, this ) < 0)){ /*if( (err = snd_async_add_pcm_handler( &_AsyncHandler, pcm_handle , AlsaCallBack, this ) < 0)){
_debugAlsa(" Unable to install the async callback handler (%s)\n", snd_strerror(err)); _debugAlsa(" Unable to install the async callback handler (%s)\n", snd_strerror(err));
return false; return false;
}*/
// So the loop could start when the ringtone thread entry function is reached
ringtone_thread_is_running = true;
if( pthread_create(&ringtone_thread, NULL, ringtoneThreadEntry, this) != 0 )
{
_debug("Unable to start the ringtone posix thread\n");
return false;
} }
} }
snd_pcm_sw_params_free( swparams ); snd_pcm_sw_params_free( swparams );
......
...@@ -22,6 +22,8 @@ ...@@ -22,6 +22,8 @@
#include "audiolayer.h" #include "audiolayer.h"
#include <pthread.h>
class RingBuffer; class RingBuffer;
class ManagerImpl; class ManagerImpl;
...@@ -202,6 +204,15 @@ class AlsaLayer : public AudioLayer { ...@@ -202,6 +204,15 @@ class AlsaLayer : public AudioLayer {
*/ */
void setPlaybackVolume( double volume ); void setPlaybackVolume( double volume );
/**
* Callback used for asynchronous playback.
* Write tones buffer to the alsa internal ring buffer.
*/
void playTones( void );
private: private:
// Copy Constructor // Copy Constructor
...@@ -232,13 +243,7 @@ class AlsaLayer : public AudioLayer { ...@@ -232,13 +243,7 @@ class AlsaLayer : public AudioLayer {
* Called when a certain amount of data is written ot the device * Called when a certain amount of data is written ot the device
* @param pcm_callback The callback pointer * @param pcm_callback The callback pointer
*/ */
static void AlsaCallBack( snd_async_handler_t* pcm_callback); //static void AlsaCallBack( snd_async_handler_t* pcm_callback);
/**
* Callback used for asynchronous playback.
* Write tones buffer to the alsa internal ring buffer.
*/
void playTones( void );
/** /**
* Open the specified device. * Open the specified device.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment