Skip to content

Commit

Permalink
ALSA: Code to prevent different sampling rates being used at the same…
Browse files Browse the repository at this point in the history
… time.
  • Loading branch information
jonpry committed Oct 13, 2011
1 parent 6b01762 commit 2331a01
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 13 deletions.
2 changes: 2 additions & 0 deletions alsa_sound/ALSAStreamOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ status_t ALSAStreamOps::set(int *format,
uint32_t *rate)
{

LOGE("Stream set");

status_t status = NO_ERROR;
if (channels && *channels != 0) {
if (mHandle->channels != popCount(*channels)) {
Expand Down
16 changes: 8 additions & 8 deletions alsa_sound/AudioHardwareALSA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ static void ALSAErrorHandler(const char *file,
l = snprintf(buf, BUFSIZ, "%s:%i:(%s) ", file, line, function);
vsnprintf(buf + l, BUFSIZ - l, fmt, arg);
buf[BUFSIZ-1] = '\0';
LOG(LOG_ERROR, "ALSALib", "%s", buf);
// LOG(LOG_ERROR, "ALSALib", "%s", buf);
va_end(arg);
}

Expand Down Expand Up @@ -154,7 +154,7 @@ status_t AudioHardwareALSA::setMode(int mode)
// take care of mode change.
for(ALSAHandleList::iterator it = mDeviceList.begin();
it != mDeviceList.end(); ++it) {
status = mALSADevice->route(&(*it), it->curDev, mode);
status = mALSADevice->route((*it), (*it)->curDev, mode);
if (status != NO_ERROR)
break;
}
Expand Down Expand Up @@ -199,10 +199,10 @@ AudioHardwareALSA::openOutputStream(uint32_t devices,
// Find the appropriate alsa device
for(ALSAHandleList::iterator it = mDeviceList.begin();
it != mDeviceList.end(); ++it)
if (it->devices & devices) {
err = mALSADevice->open(&(*it), devices, mode());
if ((*it)->devices & devices) {
err = mALSADevice->open((*it), devices, mode());
if (err) break;
out = new AudioStreamOutALSA(this, &(*it));
out = new AudioStreamOutALSA(this, (*it));
err = out->set(format, channels, sampleRate);
break;
}
Expand Down Expand Up @@ -239,10 +239,10 @@ AudioHardwareALSA::openInputStream(uint32_t devices,
// Find the appropriate alsa device
for(ALSAHandleList::iterator it = mDeviceList.begin();
it != mDeviceList.end(); ++it)
if (it->devices & devices) {
err = mALSADevice->open(&(*it), devices, mode());
if ((*it)->devices & devices) {
err = mALSADevice->open((*it), devices, mode());
if (err) break;
in = new AudioStreamInALSA(this, &(*it), acoustics);
in = new AudioStreamInALSA(this, (*it), acoustics);
err = in->set(format, channels, sampleRate);
break;
}
Expand Down
4 changes: 3 additions & 1 deletion alsa_sound/AudioHardwareALSA.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,19 @@ struct alsa_handle_t {
snd_pcm_format_t format;
uint32_t channels;
uint32_t sampleRate;
uint32_t realsampleRate;
unsigned int latency; // Delay in usec
unsigned int bufferSize; // Size of sample buffer
int mmap;
int interleaved;
void * modPrivate;
unsigned period_time;
snd_pcm_uframes_t period_frames;
int id;
int chunk_bytes;
};

typedef List<alsa_handle_t> ALSAHandleList;
typedef List<alsa_handle_t*> ALSAHandleList;

struct alsa_device_t {
hw_device_t common;
Expand Down
46 changes: 42 additions & 4 deletions alsa_sound/alsa_default.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ static status_t s_open(alsa_handle_t *, uint32_t, int);
static status_t s_close(alsa_handle_t *);
static status_t s_standby(alsa_handle_t *);
static status_t s_route(alsa_handle_t *, uint32_t, int);
static status_t s_resetDefaults(alsa_handle_t *handle);

static hw_module_methods_t s_module_methods = {
open : s_device_open
Expand Down Expand Up @@ -79,6 +80,7 @@ static int s_device_open(const hw_module_t* module, const char* name,
dev->close = s_close;
dev->standby = s_standby;
dev->route = s_route;
dev->resetDefaults = s_resetDefaults;

*device = &dev->common;
return 0;
Expand Down Expand Up @@ -108,13 +110,15 @@ static alsa_handle_t _defaultsOut = {
format : SND_PCM_FORMAT_S16_LE, // AudioSystem::PCM_16_BIT
channels : 2,
sampleRate : DEFAULT_SAMPLE_RATE,
realsampleRate : 0,
latency : 0, // Desired Delay in usec
bufferSize : 0, // Desired Number of samples
mmap : 0,
interleaved : 1,
modPrivate : 0,
period_time : 0,
period_frames: 1024,
id : 0,
};

static alsa_handle_t _defaultsIn = {
Expand All @@ -126,14 +130,15 @@ static alsa_handle_t _defaultsIn = {
format : SND_PCM_FORMAT_S16_LE, // AudioSystem::PCM_16_BIT
channels : 1,
sampleRate : AudioRecord::DEFAULT_SAMPLE_RATE,
realsampleRate : 0,
latency : 0, // Desired Delay in usec
bufferSize : 0, // Desired Number of samples
mmap : 0,
interleaved : 1,
modPrivate : 0,
period_time : 0,
period_frames: 0,

id : 1,
};

struct device_suffix_t {
Expand Down Expand Up @@ -215,6 +220,24 @@ status_t setGlobalParams(alsa_handle_t *handle)

usleep(1000);

if(handle==&_defaultsOut)
{
if(_defaultsIn.handle)
{
LOGE("Opening output device but input already running!");
handle->sampleRate = _defaultsIn.realsampleRate;
}
}

if(handle==&_defaultsIn)
{
if(_defaultsOut.handle)
{
LOGE("Opening input device but output already running!");
handle->sampleRate = _defaultsOut.realsampleRate;
}
}


snd_pcm_hw_params_alloca(&params);
snd_pcm_sw_params_alloca(&swparams);
Expand Down Expand Up @@ -392,6 +415,8 @@ status_t setGlobalParams(alsa_handle_t *handle)
snd_pcm_mmap_commit(handle->handle, offset, 0);
}

handle->realsampleRate = handle->sampleRate;

// buffer_frames = buffer_size; /* for position test */
return NO_ERROR;
}
Expand All @@ -410,7 +435,7 @@ static status_t s_init(alsa_device_t *module, ALSAHandleList &list)
_defaultsOut.module = module;
_defaultsOut.bufferSize = bufferSize;

list.push_back(_defaultsOut);
list.push_back(&_defaultsOut);

bufferSize = _defaultsIn.bufferSize;

Expand All @@ -420,7 +445,7 @@ static status_t s_init(alsa_device_t *module, ALSAHandleList &list)
_defaultsIn.module = module;
_defaultsIn.bufferSize = bufferSize;

list.push_back(_defaultsIn);
list.push_back(&_defaultsIn);

return NO_ERROR;
}
Expand Down Expand Up @@ -500,7 +525,10 @@ static status_t s_close(alsa_handle_t *handle)
err = snd_pcm_close(h);
}

LOGI("ALSA Module: closing down device");
if(handle == &_defaultsIn)
LOGI("ALSA Module: closing down input device");
if(handle == &_defaultsOut)
LOGI("ALSA Module: closing down output device");

return err;
}
Expand All @@ -525,4 +553,14 @@ static status_t s_route(alsa_handle_t *handle, uint32_t devices, int mode)
return s_open(handle, devices, mode);
}

static status_t s_resetDefaults(alsa_handle_t *handle)
{
if(handle == &_defaultsIn)
LOGE("Reset defaults called on input");
if(handle == &_defaultsOut)
LOGE("Reset defaults called on output");
return NO_ERROR;
}


}

0 comments on commit 2331a01

Please sign in to comment.