legacy: move legacy audio code from frameworks/base here

Change-Id: Ic5da0130af44354dffdf85c30cd99f57c6ee163c
Signed-off-by: Dima Zavin <dima@android.com>
This commit is contained in:
Dima Zavin 2011-04-19 16:33:12 -07:00
parent 2dad3e45a0
commit f01215993d
10 changed files with 4726 additions and 0 deletions

View file

@ -0,0 +1,498 @@
/*
* Copyright (C) 2008 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <math.h>
//#define LOG_NDEBUG 0
#define LOG_TAG "A2dpAudioInterface"
#include <utils/Log.h>
#include <utils/String8.h>
#include "A2dpAudioInterface.h"
#include "audio/liba2dp.h"
#include <hardware_legacy/power.h>
namespace android {
static const char *sA2dpWakeLock = "A2dpOutputStream";
#define MAX_WRITE_RETRIES 5
// ----------------------------------------------------------------------------
//AudioHardwareInterface* A2dpAudioInterface::createA2dpInterface()
//{
// AudioHardwareInterface* hw = 0;
//
// hw = AudioHardwareInterface::create();
// LOGD("new A2dpAudioInterface(hw: %p)", hw);
// hw = new A2dpAudioInterface(hw);
// return hw;
//}
A2dpAudioInterface::A2dpAudioInterface(AudioHardwareInterface* hw) :
mOutput(0), mHardwareInterface(hw), mBluetoothEnabled(true), mSuspended(false)
{
}
A2dpAudioInterface::~A2dpAudioInterface()
{
closeOutputStream((AudioStreamOut *)mOutput);
delete mHardwareInterface;
}
status_t A2dpAudioInterface::initCheck()
{
if (mHardwareInterface == 0) return NO_INIT;
return mHardwareInterface->initCheck();
}
AudioStreamOut* A2dpAudioInterface::openOutputStream(
uint32_t devices, int *format, uint32_t *channels, uint32_t *sampleRate, status_t *status)
{
if (!AudioSystem::isA2dpDevice((AudioSystem::audio_devices)devices)) {
LOGV("A2dpAudioInterface::openOutputStream() open HW device: %x", devices);
return mHardwareInterface->openOutputStream(devices, format, channels, sampleRate, status);
}
status_t err = 0;
// only one output stream allowed
if (mOutput) {
if (status)
*status = -1;
return NULL;
}
// create new output stream
A2dpAudioStreamOut* out = new A2dpAudioStreamOut();
if ((err = out->set(devices, format, channels, sampleRate)) == NO_ERROR) {
mOutput = out;
mOutput->setBluetoothEnabled(mBluetoothEnabled);
mOutput->setSuspended(mSuspended);
} else {
delete out;
}
if (status)
*status = err;
return mOutput;
}
void A2dpAudioInterface::closeOutputStream(AudioStreamOut* out) {
if (mOutput == 0 || mOutput != out) {
mHardwareInterface->closeOutputStream(out);
}
else {
delete mOutput;
mOutput = 0;
}
}
AudioStreamIn* A2dpAudioInterface::openInputStream(
uint32_t devices, int *format, uint32_t *channels, uint32_t *sampleRate, status_t *status,
AudioSystem::audio_in_acoustics acoustics)
{
return mHardwareInterface->openInputStream(devices, format, channels, sampleRate, status, acoustics);
}
void A2dpAudioInterface::closeInputStream(AudioStreamIn* in)
{
return mHardwareInterface->closeInputStream(in);
}
status_t A2dpAudioInterface::setMode(int mode)
{
return mHardwareInterface->setMode(mode);
}
status_t A2dpAudioInterface::setMicMute(bool state)
{
return mHardwareInterface->setMicMute(state);
}
status_t A2dpAudioInterface::getMicMute(bool* state)
{
return mHardwareInterface->getMicMute(state);
}
status_t A2dpAudioInterface::setParameters(const String8& keyValuePairs)
{
AudioParameter param = AudioParameter(keyValuePairs);
String8 value;
String8 key;
status_t status = NO_ERROR;
LOGV("setParameters() %s", keyValuePairs.string());
key = "bluetooth_enabled";
if (param.get(key, value) == NO_ERROR) {
mBluetoothEnabled = (value == "true");
if (mOutput) {
mOutput->setBluetoothEnabled(mBluetoothEnabled);
}
param.remove(key);
}
key = String8("A2dpSuspended");
if (param.get(key, value) == NO_ERROR) {
mSuspended = (value == "true");
if (mOutput) {
mOutput->setSuspended(mSuspended);
}
param.remove(key);
}
if (param.size()) {
status_t hwStatus = mHardwareInterface->setParameters(param.toString());
if (status == NO_ERROR) {
status = hwStatus;
}
}
return status;
}
String8 A2dpAudioInterface::getParameters(const String8& keys)
{
AudioParameter param = AudioParameter(keys);
AudioParameter a2dpParam = AudioParameter();
String8 value;
String8 key;
key = "bluetooth_enabled";
if (param.get(key, value) == NO_ERROR) {
value = mBluetoothEnabled ? "true" : "false";
a2dpParam.add(key, value);
param.remove(key);
}
key = "A2dpSuspended";
if (param.get(key, value) == NO_ERROR) {
value = mSuspended ? "true" : "false";
a2dpParam.add(key, value);
param.remove(key);
}
String8 keyValuePairs = a2dpParam.toString();
if (param.size()) {
if (keyValuePairs != "") {
keyValuePairs += ";";
}
keyValuePairs += mHardwareInterface->getParameters(param.toString());
}
LOGV("getParameters() %s", keyValuePairs.string());
return keyValuePairs;
}
size_t A2dpAudioInterface::getInputBufferSize(uint32_t sampleRate, int format, int channelCount)
{
return mHardwareInterface->getInputBufferSize(sampleRate, format, channelCount);
}
status_t A2dpAudioInterface::setVoiceVolume(float v)
{
return mHardwareInterface->setVoiceVolume(v);
}
status_t A2dpAudioInterface::setMasterVolume(float v)
{
return mHardwareInterface->setMasterVolume(v);
}
status_t A2dpAudioInterface::dump(int fd, const Vector<String16>& args)
{
return mHardwareInterface->dumpState(fd, args);
}
// ----------------------------------------------------------------------------
A2dpAudioInterface::A2dpAudioStreamOut::A2dpAudioStreamOut() :
mFd(-1), mStandby(true), mStartCount(0), mRetryCount(0), mData(NULL),
// assume BT enabled to start, this is safe because its only the
// enabled->disabled transition we are worried about
mBluetoothEnabled(true), mDevice(0), mClosing(false), mSuspended(false)
{
// use any address by default
strcpy(mA2dpAddress, "00:00:00:00:00:00");
init();
}
status_t A2dpAudioInterface::A2dpAudioStreamOut::set(
uint32_t device, int *pFormat, uint32_t *pChannels, uint32_t *pRate)
{
int lFormat = pFormat ? *pFormat : 0;
uint32_t lChannels = pChannels ? *pChannels : 0;
uint32_t lRate = pRate ? *pRate : 0;
LOGD("A2dpAudioStreamOut::set %x, %d, %d, %d\n", device, lFormat, lChannels, lRate);
// fix up defaults
if (lFormat == 0) lFormat = format();
if (lChannels == 0) lChannels = channels();
if (lRate == 0) lRate = sampleRate();
// check values
if ((lFormat != format()) ||
(lChannels != channels()) ||
(lRate != sampleRate())){
if (pFormat) *pFormat = format();
if (pChannels) *pChannels = channels();
if (pRate) *pRate = sampleRate();
return BAD_VALUE;
}
if (pFormat) *pFormat = lFormat;
if (pChannels) *pChannels = lChannels;
if (pRate) *pRate = lRate;
mDevice = device;
mBufferDurationUs = ((bufferSize() * 1000 )/ frameSize() / sampleRate()) * 1000;
return NO_ERROR;
}
A2dpAudioInterface::A2dpAudioStreamOut::~A2dpAudioStreamOut()
{
LOGV("A2dpAudioStreamOut destructor");
close();
LOGV("A2dpAudioStreamOut destructor returning from close()");
}
ssize_t A2dpAudioInterface::A2dpAudioStreamOut::write(const void* buffer, size_t bytes)
{
status_t status = -1;
{
Mutex::Autolock lock(mLock);
size_t remaining = bytes;
if (!mBluetoothEnabled || mClosing || mSuspended) {
LOGV("A2dpAudioStreamOut::write(), but bluetooth disabled \
mBluetoothEnabled %d, mClosing %d, mSuspended %d",
mBluetoothEnabled, mClosing, mSuspended);
goto Error;
}
if (mStandby) {
acquire_wake_lock (PARTIAL_WAKE_LOCK, sA2dpWakeLock);
mStandby = false;
mLastWriteTime = systemTime();
}
status = init();
if (status < 0)
goto Error;
int retries = MAX_WRITE_RETRIES;
while (remaining > 0 && retries) {
status = a2dp_write(mData, buffer, remaining);
if (status < 0) {
LOGE("a2dp_write failed err: %d\n", status);
goto Error;
}
if (status == 0) {
retries--;
}
remaining -= status;
buffer = (char *)buffer + status;
}
// if A2DP sink runs abnormally fast, sleep a little so that audioflinger mixer thread
// does no spin and starve other threads.
// NOTE: It is likely that the A2DP headset is being disconnected
nsecs_t now = systemTime();
if ((uint32_t)ns2us(now - mLastWriteTime) < (mBufferDurationUs >> 2)) {
LOGV("A2DP sink runs too fast");
usleep(mBufferDurationUs - (uint32_t)ns2us(now - mLastWriteTime));
}
mLastWriteTime = now;
return bytes;
}
Error:
standby();
// Simulate audio output timing in case of error
usleep(mBufferDurationUs);
return status;
}
status_t A2dpAudioInterface::A2dpAudioStreamOut::init()
{
if (!mData) {
status_t status = a2dp_init(44100, 2, &mData);
if (status < 0) {
LOGE("a2dp_init failed err: %d\n", status);
mData = NULL;
return status;
}
a2dp_set_sink(mData, mA2dpAddress);
}
return 0;
}
status_t A2dpAudioInterface::A2dpAudioStreamOut::standby()
{
Mutex::Autolock lock(mLock);
return standby_l();
}
status_t A2dpAudioInterface::A2dpAudioStreamOut::standby_l()
{
int result = NO_ERROR;
if (!mStandby) {
LOGV_IF(mClosing || !mBluetoothEnabled, "Standby skip stop: closing %d enabled %d",
mClosing, mBluetoothEnabled);
if (!mClosing && mBluetoothEnabled) {
result = a2dp_stop(mData);
}
release_wake_lock(sA2dpWakeLock);
mStandby = true;
}
return result;
}
status_t A2dpAudioInterface::A2dpAudioStreamOut::setParameters(const String8& keyValuePairs)
{
AudioParameter param = AudioParameter(keyValuePairs);
String8 value;
String8 key = String8("a2dp_sink_address");
status_t status = NO_ERROR;
int device;
LOGV("A2dpAudioStreamOut::setParameters() %s", keyValuePairs.string());
if (param.get(key, value) == NO_ERROR) {
if (value.length() != strlen("00:00:00:00:00:00")) {
status = BAD_VALUE;
} else {
setAddress(value.string());
}
param.remove(key);
}
key = String8("closing");
if (param.get(key, value) == NO_ERROR) {
mClosing = (value == "true");
if (mClosing) {
standby();
}
param.remove(key);
}
key = AudioParameter::keyRouting;
if (param.getInt(key, device) == NO_ERROR) {
if (AudioSystem::isA2dpDevice((AudioSystem::audio_devices)device)) {
mDevice = device;
status = NO_ERROR;
} else {
status = BAD_VALUE;
}
param.remove(key);
}
if (param.size()) {
status = BAD_VALUE;
}
return status;
}
String8 A2dpAudioInterface::A2dpAudioStreamOut::getParameters(const String8& keys)
{
AudioParameter param = AudioParameter(keys);
String8 value;
String8 key = String8("a2dp_sink_address");
if (param.get(key, value) == NO_ERROR) {
value = mA2dpAddress;
param.add(key, value);
}
key = AudioParameter::keyRouting;
if (param.get(key, value) == NO_ERROR) {
param.addInt(key, (int)mDevice);
}
LOGV("A2dpAudioStreamOut::getParameters() %s", param.toString().string());
return param.toString();
}
status_t A2dpAudioInterface::A2dpAudioStreamOut::setAddress(const char* address)
{
Mutex::Autolock lock(mLock);
if (strlen(address) != strlen("00:00:00:00:00:00"))
return -EINVAL;
strcpy(mA2dpAddress, address);
if (mData)
a2dp_set_sink(mData, mA2dpAddress);
return NO_ERROR;
}
status_t A2dpAudioInterface::A2dpAudioStreamOut::setBluetoothEnabled(bool enabled)
{
LOGD("setBluetoothEnabled %d", enabled);
Mutex::Autolock lock(mLock);
mBluetoothEnabled = enabled;
if (!enabled) {
return close_l();
}
return NO_ERROR;
}
status_t A2dpAudioInterface::A2dpAudioStreamOut::setSuspended(bool onOff)
{
LOGV("setSuspended %d", onOff);
mSuspended = onOff;
standby();
return NO_ERROR;
}
status_t A2dpAudioInterface::A2dpAudioStreamOut::close()
{
Mutex::Autolock lock(mLock);
LOGV("A2dpAudioStreamOut::close() calling close_l()");
return close_l();
}
status_t A2dpAudioInterface::A2dpAudioStreamOut::close_l()
{
standby_l();
if (mData) {
LOGV("A2dpAudioStreamOut::close_l() calling a2dp_cleanup(mData)");
a2dp_cleanup(mData);
mData = NULL;
}
return NO_ERROR;
}
status_t A2dpAudioInterface::A2dpAudioStreamOut::dump(int fd, const Vector<String16>& args)
{
return NO_ERROR;
}
status_t A2dpAudioInterface::A2dpAudioStreamOut::getRenderPosition(uint32_t *driverFrames)
{
//TODO: enable when supported by driver
return INVALID_OPERATION;
}
}; // namespace android

138
audio/A2dpAudioInterface.h Normal file
View file

@ -0,0 +1,138 @@
/*
* Copyright (C) 2008 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef A2DP_AUDIO_HARDWARE_H
#define A2DP_AUDIO_HARDWARE_H
#include <stdint.h>
#include <sys/types.h>
#include <utils/threads.h>
#include <hardware_legacy/AudioHardwareBase.h>
namespace android {
class A2dpAudioInterface : public AudioHardwareBase
{
class A2dpAudioStreamOut;
public:
A2dpAudioInterface(AudioHardwareInterface* hw);
virtual ~A2dpAudioInterface();
virtual status_t initCheck();
virtual status_t setVoiceVolume(float volume);
virtual status_t setMasterVolume(float volume);
virtual status_t setMode(int mode);
// mic mute
virtual status_t setMicMute(bool state);
virtual status_t getMicMute(bool* state);
virtual status_t setParameters(const String8& keyValuePairs);
virtual String8 getParameters(const String8& keys);
virtual size_t getInputBufferSize(uint32_t sampleRate, int format, int channelCount);
// create I/O streams
virtual AudioStreamOut* openOutputStream(
uint32_t devices,
int *format=0,
uint32_t *channels=0,
uint32_t *sampleRate=0,
status_t *status=0);
virtual void closeOutputStream(AudioStreamOut* out);
virtual AudioStreamIn* openInputStream(
uint32_t devices,
int *format,
uint32_t *channels,
uint32_t *sampleRate,
status_t *status,
AudioSystem::audio_in_acoustics acoustics);
virtual void closeInputStream(AudioStreamIn* in);
// static AudioHardwareInterface* createA2dpInterface();
protected:
virtual status_t dump(int fd, const Vector<String16>& args);
private:
class A2dpAudioStreamOut : public AudioStreamOut {
public:
A2dpAudioStreamOut();
virtual ~A2dpAudioStreamOut();
status_t set(uint32_t device,
int *pFormat,
uint32_t *pChannels,
uint32_t *pRate);
virtual uint32_t sampleRate() const { return 44100; }
// SBC codec wants a multiple of 512
virtual size_t bufferSize() const { return 512 * 20; }
virtual uint32_t channels() const { return AudioSystem::CHANNEL_OUT_STEREO; }
virtual int format() const { return AudioSystem::PCM_16_BIT; }
virtual uint32_t latency() const { return ((1000*bufferSize())/frameSize())/sampleRate() + 200; }
virtual status_t setVolume(float left, float right) { return INVALID_OPERATION; }
virtual ssize_t write(const void* buffer, size_t bytes);
status_t standby();
virtual status_t dump(int fd, const Vector<String16>& args);
virtual status_t setParameters(const String8& keyValuePairs);
virtual String8 getParameters(const String8& keys);
virtual status_t getRenderPosition(uint32_t *dspFrames);
private:
friend class A2dpAudioInterface;
status_t init();
status_t close();
status_t close_l();
status_t setAddress(const char* address);
status_t setBluetoothEnabled(bool enabled);
status_t setSuspended(bool onOff);
status_t standby_l();
private:
int mFd;
bool mStandby;
int mStartCount;
int mRetryCount;
char mA2dpAddress[20];
void* mData;
Mutex mLock;
bool mBluetoothEnabled;
uint32_t mDevice;
bool mClosing;
bool mSuspended;
nsecs_t mLastWriteTime;
uint32_t mBufferDurationUs;
};
friend class A2dpAudioStreamOut;
A2dpAudioStreamOut* mOutput;
AudioHardwareInterface *mHardwareInterface;
char mA2dpAddress[20];
bool mBluetoothEnabled;
bool mSuspended;
};
// ----------------------------------------------------------------------------
}; // namespace android
#endif // A2DP_AUDIO_HARDWARE_H

View file

@ -0,0 +1,573 @@
/* //device/servers/AudioFlinger/AudioDumpInterface.cpp
**
** Copyright 2008, The Android Open Source Project
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
** http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*/
#define LOG_TAG "AudioFlingerDump"
//#define LOG_NDEBUG 0
#include <stdint.h>
#include <sys/types.h>
#include <utils/Log.h>
#include <stdlib.h>
#include <unistd.h>
#include "AudioDumpInterface.h"
namespace android {
// ----------------------------------------------------------------------------
AudioDumpInterface::AudioDumpInterface(AudioHardwareInterface* hw)
: mPolicyCommands(String8("")), mFileName(String8(""))
{
if(hw == 0) {
LOGE("Dump construct hw = 0");
}
mFinalInterface = hw;
LOGV("Constructor %p, mFinalInterface %p", this, mFinalInterface);
}
AudioDumpInterface::~AudioDumpInterface()
{
for (size_t i = 0; i < mOutputs.size(); i++) {
closeOutputStream((AudioStreamOut *)mOutputs[i]);
}
for (size_t i = 0; i < mInputs.size(); i++) {
closeInputStream((AudioStreamIn *)mInputs[i]);
}
if(mFinalInterface) delete mFinalInterface;
}
AudioStreamOut* AudioDumpInterface::openOutputStream(
uint32_t devices, int *format, uint32_t *channels, uint32_t *sampleRate, status_t *status)
{
AudioStreamOut* outFinal = NULL;
int lFormat = AudioSystem::PCM_16_BIT;
uint32_t lChannels = AudioSystem::CHANNEL_OUT_STEREO;
uint32_t lRate = 44100;
outFinal = mFinalInterface->openOutputStream(devices, format, channels, sampleRate, status);
if (outFinal != 0) {
lFormat = outFinal->format();
lChannels = outFinal->channels();
lRate = outFinal->sampleRate();
} else {
if (format != 0) {
if (*format != 0) {
lFormat = *format;
} else {
*format = lFormat;
}
}
if (channels != 0) {
if (*channels != 0) {
lChannels = *channels;
} else {
*channels = lChannels;
}
}
if (sampleRate != 0) {
if (*sampleRate != 0) {
lRate = *sampleRate;
} else {
*sampleRate = lRate;
}
}
if (status) *status = NO_ERROR;
}
LOGV("openOutputStream(), outFinal %p", outFinal);
AudioStreamOutDump *dumOutput = new AudioStreamOutDump(this, mOutputs.size(), outFinal,
devices, lFormat, lChannels, lRate);
mOutputs.add(dumOutput);
return dumOutput;
}
void AudioDumpInterface::closeOutputStream(AudioStreamOut* out)
{
AudioStreamOutDump *dumpOut = (AudioStreamOutDump *)out;
if (mOutputs.indexOf(dumpOut) < 0) {
LOGW("Attempt to close invalid output stream");
return;
}
LOGV("closeOutputStream() output %p", out);
dumpOut->standby();
if (dumpOut->finalStream() != NULL) {
mFinalInterface->closeOutputStream(dumpOut->finalStream());
}
mOutputs.remove(dumpOut);
delete dumpOut;
}
AudioStreamIn* AudioDumpInterface::openInputStream(uint32_t devices, int *format, uint32_t *channels,
uint32_t *sampleRate, status_t *status, AudioSystem::audio_in_acoustics acoustics)
{
AudioStreamIn* inFinal = NULL;
int lFormat = AudioSystem::PCM_16_BIT;
uint32_t lChannels = AudioSystem::CHANNEL_IN_MONO;
uint32_t lRate = 8000;
inFinal = mFinalInterface->openInputStream(devices, format, channels, sampleRate, status, acoustics);
if (inFinal != 0) {
lFormat = inFinal->format();
lChannels = inFinal->channels();
lRate = inFinal->sampleRate();
} else {
if (format != 0) {
if (*format != 0) {
lFormat = *format;
} else {
*format = lFormat;
}
}
if (channels != 0) {
if (*channels != 0) {
lChannels = *channels;
} else {
*channels = lChannels;
}
}
if (sampleRate != 0) {
if (*sampleRate != 0) {
lRate = *sampleRate;
} else {
*sampleRate = lRate;
}
}
if (status) *status = NO_ERROR;
}
LOGV("openInputStream(), inFinal %p", inFinal);
AudioStreamInDump *dumInput = new AudioStreamInDump(this, mInputs.size(), inFinal,
devices, lFormat, lChannels, lRate);
mInputs.add(dumInput);
return dumInput;
}
void AudioDumpInterface::closeInputStream(AudioStreamIn* in)
{
AudioStreamInDump *dumpIn = (AudioStreamInDump *)in;
if (mInputs.indexOf(dumpIn) < 0) {
LOGW("Attempt to close invalid input stream");
return;
}
dumpIn->standby();
if (dumpIn->finalStream() != NULL) {
mFinalInterface->closeInputStream(dumpIn->finalStream());
}
mInputs.remove(dumpIn);
delete dumpIn;
}
status_t AudioDumpInterface::setParameters(const String8& keyValuePairs)
{
AudioParameter param = AudioParameter(keyValuePairs);
String8 value;
int valueInt;
LOGV("setParameters %s", keyValuePairs.string());
if (param.get(String8("test_cmd_file_name"), value) == NO_ERROR) {
mFileName = value;
param.remove(String8("test_cmd_file_name"));
}
if (param.get(String8("test_cmd_policy"), value) == NO_ERROR) {
Mutex::Autolock _l(mLock);
param.remove(String8("test_cmd_policy"));
mPolicyCommands = param.toString();
LOGV("test_cmd_policy command %s written", mPolicyCommands.string());
return NO_ERROR;
}
if (mFinalInterface != 0 ) return mFinalInterface->setParameters(keyValuePairs);
return NO_ERROR;
}
String8 AudioDumpInterface::getParameters(const String8& keys)
{
AudioParameter param = AudioParameter(keys);
AudioParameter response;
String8 value;
// LOGV("getParameters %s", keys.string());
if (param.get(String8("test_cmd_policy"), value) == NO_ERROR) {
Mutex::Autolock _l(mLock);
if (mPolicyCommands.length() != 0) {
response = AudioParameter(mPolicyCommands);
response.addInt(String8("test_cmd_policy"), 1);
} else {
response.addInt(String8("test_cmd_policy"), 0);
}
param.remove(String8("test_cmd_policy"));
// LOGV("test_cmd_policy command %s read", mPolicyCommands.string());
}
if (param.get(String8("test_cmd_file_name"), value) == NO_ERROR) {
response.add(String8("test_cmd_file_name"), mFileName);
param.remove(String8("test_cmd_file_name"));
}
String8 keyValuePairs = response.toString();
if (param.size() && mFinalInterface != 0 ) {
keyValuePairs += ";";
keyValuePairs += mFinalInterface->getParameters(param.toString());
}
return keyValuePairs;
}
status_t AudioDumpInterface::setMode(int mode)
{
return mFinalInterface->setMode(mode);
}
size_t AudioDumpInterface::getInputBufferSize(uint32_t sampleRate, int format, int channelCount)
{
return mFinalInterface->getInputBufferSize(sampleRate, format, channelCount);
}
// ----------------------------------------------------------------------------
AudioStreamOutDump::AudioStreamOutDump(AudioDumpInterface *interface,
int id,
AudioStreamOut* finalStream,
uint32_t devices,
int format,
uint32_t channels,
uint32_t sampleRate)
: mInterface(interface), mId(id),
mSampleRate(sampleRate), mFormat(format), mChannels(channels), mLatency(0), mDevice(devices),
mBufferSize(1024), mFinalStream(finalStream), mFile(0), mFileCount(0)
{
LOGV("AudioStreamOutDump Constructor %p, mInterface %p, mFinalStream %p", this, mInterface, mFinalStream);
}
AudioStreamOutDump::~AudioStreamOutDump()
{
LOGV("AudioStreamOutDump destructor");
Close();
}
ssize_t AudioStreamOutDump::write(const void* buffer, size_t bytes)
{
ssize_t ret;
if (mFinalStream) {
ret = mFinalStream->write(buffer, bytes);
} else {
usleep((((bytes * 1000) / frameSize()) / sampleRate()) * 1000);
ret = bytes;
}
if(!mFile) {
if (mInterface->fileName() != "") {
char name[255];
sprintf(name, "%s_out_%d_%d.pcm", mInterface->fileName().string(), mId, ++mFileCount);
mFile = fopen(name, "wb");
LOGV("Opening dump file %s, fh %p", name, mFile);
}
}
if (mFile) {
fwrite(buffer, bytes, 1, mFile);
}
return ret;
}
status_t AudioStreamOutDump::standby()
{
LOGV("AudioStreamOutDump standby(), mFile %p, mFinalStream %p", mFile, mFinalStream);
Close();
if (mFinalStream != 0 ) return mFinalStream->standby();
return NO_ERROR;
}
uint32_t AudioStreamOutDump::sampleRate() const
{
if (mFinalStream != 0 ) return mFinalStream->sampleRate();
return mSampleRate;
}
size_t AudioStreamOutDump::bufferSize() const
{
if (mFinalStream != 0 ) return mFinalStream->bufferSize();
return mBufferSize;
}
uint32_t AudioStreamOutDump::channels() const
{
if (mFinalStream != 0 ) return mFinalStream->channels();
return mChannels;
}
int AudioStreamOutDump::format() const
{
if (mFinalStream != 0 ) return mFinalStream->format();
return mFormat;
}
uint32_t AudioStreamOutDump::latency() const
{
if (mFinalStream != 0 ) return mFinalStream->latency();
return 0;
}
status_t AudioStreamOutDump::setVolume(float left, float right)
{
if (mFinalStream != 0 ) return mFinalStream->setVolume(left, right);
return NO_ERROR;
}
status_t AudioStreamOutDump::setParameters(const String8& keyValuePairs)
{
LOGV("AudioStreamOutDump::setParameters %s", keyValuePairs.string());
if (mFinalStream != 0 ) {
return mFinalStream->setParameters(keyValuePairs);
}
AudioParameter param = AudioParameter(keyValuePairs);
String8 value;
int valueInt;
status_t status = NO_ERROR;
if (param.getInt(String8("set_id"), valueInt) == NO_ERROR) {
mId = valueInt;
}
if (param.getInt(String8("format"), valueInt) == NO_ERROR) {
if (mFile == 0) {
mFormat = valueInt;
} else {
status = INVALID_OPERATION;
}
}
if (param.getInt(String8("channels"), valueInt) == NO_ERROR) {
if (valueInt == AudioSystem::CHANNEL_OUT_STEREO || valueInt == AudioSystem::CHANNEL_OUT_MONO) {
mChannels = valueInt;
} else {
status = BAD_VALUE;
}
}
if (param.getInt(String8("sampling_rate"), valueInt) == NO_ERROR) {
if (valueInt > 0 && valueInt <= 48000) {
if (mFile == 0) {
mSampleRate = valueInt;
} else {
status = INVALID_OPERATION;
}
} else {
status = BAD_VALUE;
}
}
return status;
}
String8 AudioStreamOutDump::getParameters(const String8& keys)
{
if (mFinalStream != 0 ) return mFinalStream->getParameters(keys);
AudioParameter param = AudioParameter(keys);
return param.toString();
}
status_t AudioStreamOutDump::dump(int fd, const Vector<String16>& args)
{
if (mFinalStream != 0 ) return mFinalStream->dump(fd, args);
return NO_ERROR;
}
void AudioStreamOutDump::Close()
{
if(mFile) {
fclose(mFile);
mFile = 0;
}
}
status_t AudioStreamOutDump::getRenderPosition(uint32_t *dspFrames)
{
if (mFinalStream != 0 ) return mFinalStream->getRenderPosition(dspFrames);
return INVALID_OPERATION;
}
// ----------------------------------------------------------------------------
AudioStreamInDump::AudioStreamInDump(AudioDumpInterface *interface,
int id,
AudioStreamIn* finalStream,
uint32_t devices,
int format,
uint32_t channels,
uint32_t sampleRate)
: mInterface(interface), mId(id),
mSampleRate(sampleRate), mFormat(format), mChannels(channels), mDevice(devices),
mBufferSize(1024), mFinalStream(finalStream), mFile(0), mFileCount(0)
{
LOGV("AudioStreamInDump Constructor %p, mInterface %p, mFinalStream %p", this, mInterface, mFinalStream);
}
AudioStreamInDump::~AudioStreamInDump()
{
Close();
}
ssize_t AudioStreamInDump::read(void* buffer, ssize_t bytes)
{
ssize_t ret;
if (mFinalStream) {
ret = mFinalStream->read(buffer, bytes);
if(!mFile) {
if (mInterface->fileName() != "") {
char name[255];
sprintf(name, "%s_in_%d_%d.pcm", mInterface->fileName().string(), mId, ++mFileCount);
mFile = fopen(name, "wb");
LOGV("Opening input dump file %s, fh %p", name, mFile);
}
}
if (mFile) {
fwrite(buffer, bytes, 1, mFile);
}
} else {
usleep((((bytes * 1000) / frameSize()) / sampleRate()) * 1000);
ret = bytes;
if(!mFile) {
char name[255];
strcpy(name, "/sdcard/music/sine440");
if (channels() == AudioSystem::CHANNEL_IN_MONO) {
strcat(name, "_mo");
} else {
strcat(name, "_st");
}
if (format() == AudioSystem::PCM_16_BIT) {
strcat(name, "_16b");
} else {
strcat(name, "_8b");
}
if (sampleRate() < 16000) {
strcat(name, "_8k");
} else if (sampleRate() < 32000) {
strcat(name, "_22k");
} else if (sampleRate() < 48000) {
strcat(name, "_44k");
} else {
strcat(name, "_48k");
}
strcat(name, ".wav");
mFile = fopen(name, "rb");
LOGV("Opening input read file %s, fh %p", name, mFile);
if (mFile) {
fseek(mFile, AUDIO_DUMP_WAVE_HDR_SIZE, SEEK_SET);
}
}
if (mFile) {
ssize_t bytesRead = fread(buffer, bytes, 1, mFile);
if (bytesRead >=0 && bytesRead < bytes) {
fseek(mFile, AUDIO_DUMP_WAVE_HDR_SIZE, SEEK_SET);
fread((uint8_t *)buffer+bytesRead, bytes-bytesRead, 1, mFile);
}
}
}
return ret;
}
status_t AudioStreamInDump::standby()
{
LOGV("AudioStreamInDump standby(), mFile %p, mFinalStream %p", mFile, mFinalStream);
Close();
if (mFinalStream != 0 ) return mFinalStream->standby();
return NO_ERROR;
}
status_t AudioStreamInDump::setGain(float gain)
{
if (mFinalStream != 0 ) return mFinalStream->setGain(gain);
return NO_ERROR;
}
uint32_t AudioStreamInDump::sampleRate() const
{
if (mFinalStream != 0 ) return mFinalStream->sampleRate();
return mSampleRate;
}
size_t AudioStreamInDump::bufferSize() const
{
if (mFinalStream != 0 ) return mFinalStream->bufferSize();
return mBufferSize;
}
uint32_t AudioStreamInDump::channels() const
{
if (mFinalStream != 0 ) return mFinalStream->channels();
return mChannels;
}
int AudioStreamInDump::format() const
{
if (mFinalStream != 0 ) return mFinalStream->format();
return mFormat;
}
status_t AudioStreamInDump::setParameters(const String8& keyValuePairs)
{
LOGV("AudioStreamInDump::setParameters()");
if (mFinalStream != 0 ) return mFinalStream->setParameters(keyValuePairs);
return NO_ERROR;
}
String8 AudioStreamInDump::getParameters(const String8& keys)
{
if (mFinalStream != 0 ) return mFinalStream->getParameters(keys);
AudioParameter param = AudioParameter(keys);
return param.toString();
}
unsigned int AudioStreamInDump::getInputFramesLost() const
{
if (mFinalStream != 0 ) return mFinalStream->getInputFramesLost();
return 0;
}
status_t AudioStreamInDump::dump(int fd, const Vector<String16>& args)
{
if (mFinalStream != 0 ) return mFinalStream->dump(fd, args);
return NO_ERROR;
}
void AudioStreamInDump::Close()
{
if(mFile) {
fclose(mFile);
mFile = 0;
}
}
}; // namespace android

170
audio/AudioDumpInterface.h Normal file
View file

@ -0,0 +1,170 @@
/* //device/servers/AudioFlinger/AudioDumpInterface.h
**
** Copyright 2008, The Android Open Source Project
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
** http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*/
#ifndef ANDROID_AUDIO_DUMP_INTERFACE_H
#define ANDROID_AUDIO_DUMP_INTERFACE_H
#include <stdint.h>
#include <sys/types.h>
#include <utils/String8.h>
#include <utils/SortedVector.h>
#include <hardware_legacy/AudioHardwareBase.h>
namespace android {
#define AUDIO_DUMP_WAVE_HDR_SIZE 44
class AudioDumpInterface;
class AudioStreamOutDump : public AudioStreamOut {
public:
AudioStreamOutDump(AudioDumpInterface *interface,
int id,
AudioStreamOut* finalStream,
uint32_t devices,
int format,
uint32_t channels,
uint32_t sampleRate);
~AudioStreamOutDump();
virtual ssize_t write(const void* buffer, size_t bytes);
virtual uint32_t sampleRate() const;
virtual size_t bufferSize() const;
virtual uint32_t channels() const;
virtual int format() const;
virtual uint32_t latency() const;
virtual status_t setVolume(float left, float right);
virtual status_t standby();
virtual status_t setParameters(const String8& keyValuePairs);
virtual String8 getParameters(const String8& keys);
virtual status_t dump(int fd, const Vector<String16>& args);
void Close(void);
AudioStreamOut* finalStream() { return mFinalStream; }
uint32_t device() { return mDevice; }
int getId() { return mId; }
virtual status_t getRenderPosition(uint32_t *dspFrames);
private:
AudioDumpInterface *mInterface;
int mId;
uint32_t mSampleRate; //
uint32_t mFormat; //
uint32_t mChannels; // output configuration
uint32_t mLatency; //
uint32_t mDevice; // current device this output is routed to
size_t mBufferSize;
AudioStreamOut *mFinalStream;
FILE *mFile; // output file
int mFileCount;
};
class AudioStreamInDump : public AudioStreamIn {
public:
AudioStreamInDump(AudioDumpInterface *interface,
int id,
AudioStreamIn* finalStream,
uint32_t devices,
int format,
uint32_t channels,
uint32_t sampleRate);
~AudioStreamInDump();
virtual uint32_t sampleRate() const;
virtual size_t bufferSize() const;
virtual uint32_t channels() const;
virtual int format() const;
virtual status_t setGain(float gain);
virtual ssize_t read(void* buffer, ssize_t bytes);
virtual status_t standby();
virtual status_t setParameters(const String8& keyValuePairs);
virtual String8 getParameters(const String8& keys);
virtual unsigned int getInputFramesLost() const;
virtual status_t dump(int fd, const Vector<String16>& args);
void Close(void);
AudioStreamIn* finalStream() { return mFinalStream; }
uint32_t device() { return mDevice; }
private:
AudioDumpInterface *mInterface;
int mId;
uint32_t mSampleRate; //
uint32_t mFormat; //
uint32_t mChannels; // output configuration
uint32_t mDevice; // current device this output is routed to
size_t mBufferSize;
AudioStreamIn *mFinalStream;
FILE *mFile; // output file
int mFileCount;
};
class AudioDumpInterface : public AudioHardwareBase
{
public:
AudioDumpInterface(AudioHardwareInterface* hw);
virtual AudioStreamOut* openOutputStream(
uint32_t devices,
int *format=0,
uint32_t *channels=0,
uint32_t *sampleRate=0,
status_t *status=0);
virtual void closeOutputStream(AudioStreamOut* out);
virtual ~AudioDumpInterface();
virtual status_t initCheck()
{return mFinalInterface->initCheck();}
virtual status_t setVoiceVolume(float volume)
{return mFinalInterface->setVoiceVolume(volume);}
virtual status_t setMasterVolume(float volume)
{return mFinalInterface->setMasterVolume(volume);}
virtual status_t setMode(int mode);
// mic mute
virtual status_t setMicMute(bool state)
{return mFinalInterface->setMicMute(state);}
virtual status_t getMicMute(bool* state)
{return mFinalInterface->getMicMute(state);}
virtual status_t setParameters(const String8& keyValuePairs);
virtual String8 getParameters(const String8& keys);
virtual size_t getInputBufferSize(uint32_t sampleRate, int format, int channelCount);
virtual AudioStreamIn* openInputStream(uint32_t devices, int *format, uint32_t *channels,
uint32_t *sampleRate, status_t *status, AudioSystem::audio_in_acoustics acoustics);
virtual void closeInputStream(AudioStreamIn* in);
virtual status_t dump(int fd, const Vector<String16>& args) { return mFinalInterface->dumpState(fd, args); }
String8 fileName() const { return mFileName; }
protected:
AudioHardwareInterface *mFinalInterface;
SortedVector<AudioStreamOutDump *> mOutputs;
SortedVector<AudioStreamInDump *> mInputs;
Mutex mLock;
String8 mPolicyCommands;
String8 mFileName;
};
}; // namespace android
#endif // ANDROID_AUDIO_DUMP_INTERFACE_H

View file

@ -0,0 +1,411 @@
/*
**
** Copyright 2007, The Android Open Source Project
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
** http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*/
#include <stdint.h>
#include <sys/types.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sched.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#define LOG_TAG "AudioHardware"
#include <utils/Log.h>
#include <utils/String8.h>
#include "AudioHardwareGeneric.h"
#include <media/AudioRecord.h>
namespace android {
// ----------------------------------------------------------------------------
static char const * const kAudioDeviceName = "/dev/eac";
// ----------------------------------------------------------------------------
AudioHardwareGeneric::AudioHardwareGeneric()
: mOutput(0), mInput(0), mFd(-1), mMicMute(false)
{
mFd = ::open(kAudioDeviceName, O_RDWR);
}
AudioHardwareGeneric::~AudioHardwareGeneric()
{
if (mFd >= 0) ::close(mFd);
closeOutputStream((AudioStreamOut *)mOutput);
closeInputStream((AudioStreamIn *)mInput);
}
status_t AudioHardwareGeneric::initCheck()
{
if (mFd >= 0) {
if (::access(kAudioDeviceName, O_RDWR) == NO_ERROR)
return NO_ERROR;
}
return NO_INIT;
}
AudioStreamOut* AudioHardwareGeneric::openOutputStream(
uint32_t devices, int *format, uint32_t *channels, uint32_t *sampleRate, status_t *status)
{
AutoMutex lock(mLock);
// only one output stream allowed
if (mOutput) {
if (status) {
*status = INVALID_OPERATION;
}
return 0;
}
// create new output stream
AudioStreamOutGeneric* out = new AudioStreamOutGeneric();
status_t lStatus = out->set(this, mFd, devices, format, channels, sampleRate);
if (status) {
*status = lStatus;
}
if (lStatus == NO_ERROR) {
mOutput = out;
} else {
delete out;
}
return mOutput;
}
void AudioHardwareGeneric::closeOutputStream(AudioStreamOut* out) {
if (mOutput && out == mOutput) {
delete mOutput;
mOutput = 0;
}
}
AudioStreamIn* AudioHardwareGeneric::openInputStream(
uint32_t devices, int *format, uint32_t *channels, uint32_t *sampleRate,
status_t *status, AudioSystem::audio_in_acoustics acoustics)
{
// check for valid input source
if (!AudioSystem::isInputDevice((AudioSystem::audio_devices)devices)) {
return 0;
}
AutoMutex lock(mLock);
// only one input stream allowed
if (mInput) {
if (status) {
*status = INVALID_OPERATION;
}
return 0;
}
// create new output stream
AudioStreamInGeneric* in = new AudioStreamInGeneric();
status_t lStatus = in->set(this, mFd, devices, format, channels, sampleRate, acoustics);
if (status) {
*status = lStatus;
}
if (lStatus == NO_ERROR) {
mInput = in;
} else {
delete in;
}
return mInput;
}
void AudioHardwareGeneric::closeInputStream(AudioStreamIn* in) {
if (mInput && in == mInput) {
delete mInput;
mInput = 0;
}
}
status_t AudioHardwareGeneric::setVoiceVolume(float v)
{
// Implement: set voice volume
return NO_ERROR;
}
status_t AudioHardwareGeneric::setMasterVolume(float v)
{
// Implement: set master volume
// return error - software mixer will handle it
return INVALID_OPERATION;
}
status_t AudioHardwareGeneric::setMicMute(bool state)
{
mMicMute = state;
return NO_ERROR;
}
status_t AudioHardwareGeneric::getMicMute(bool* state)
{
*state = mMicMute;
return NO_ERROR;
}
status_t AudioHardwareGeneric::dumpInternals(int fd, const Vector<String16>& args)
{
const size_t SIZE = 256;
char buffer[SIZE];
String8 result;
result.append("AudioHardwareGeneric::dumpInternals\n");
snprintf(buffer, SIZE, "\tmFd: %d mMicMute: %s\n", mFd, mMicMute? "true": "false");
result.append(buffer);
::write(fd, result.string(), result.size());
return NO_ERROR;
}
status_t AudioHardwareGeneric::dump(int fd, const Vector<String16>& args)
{
dumpInternals(fd, args);
if (mInput) {
mInput->dump(fd, args);
}
if (mOutput) {
mOutput->dump(fd, args);
}
return NO_ERROR;
}
// ----------------------------------------------------------------------------
status_t AudioStreamOutGeneric::set(
AudioHardwareGeneric *hw,
int fd,
uint32_t devices,
int *pFormat,
uint32_t *pChannels,
uint32_t *pRate)
{
int lFormat = pFormat ? *pFormat : 0;
uint32_t lChannels = pChannels ? *pChannels : 0;
uint32_t lRate = pRate ? *pRate : 0;
// fix up defaults
if (lFormat == 0) lFormat = format();
if (lChannels == 0) lChannels = channels();
if (lRate == 0) lRate = sampleRate();
// check values
if ((lFormat != format()) ||
(lChannels != channels()) ||
(lRate != sampleRate())) {
if (pFormat) *pFormat = format();
if (pChannels) *pChannels = channels();
if (pRate) *pRate = sampleRate();
return BAD_VALUE;
}
if (pFormat) *pFormat = lFormat;
if (pChannels) *pChannels = lChannels;
if (pRate) *pRate = lRate;
mAudioHardware = hw;
mFd = fd;
mDevice = devices;
return NO_ERROR;
}
AudioStreamOutGeneric::~AudioStreamOutGeneric()
{
}
ssize_t AudioStreamOutGeneric::write(const void* buffer, size_t bytes)
{
Mutex::Autolock _l(mLock);
return ssize_t(::write(mFd, buffer, bytes));
}
status_t AudioStreamOutGeneric::standby()
{
// Implement: audio hardware to standby mode
return NO_ERROR;
}
status_t AudioStreamOutGeneric::dump(int fd, const Vector<String16>& args)
{
const size_t SIZE = 256;
char buffer[SIZE];
String8 result;
snprintf(buffer, SIZE, "AudioStreamOutGeneric::dump\n");
result.append(buffer);
snprintf(buffer, SIZE, "\tsample rate: %d\n", sampleRate());
result.append(buffer);
snprintf(buffer, SIZE, "\tbuffer size: %d\n", bufferSize());
result.append(buffer);
snprintf(buffer, SIZE, "\tchannels: %d\n", channels());
result.append(buffer);
snprintf(buffer, SIZE, "\tformat: %d\n", format());
result.append(buffer);
snprintf(buffer, SIZE, "\tdevice: %d\n", mDevice);
result.append(buffer);
snprintf(buffer, SIZE, "\tmAudioHardware: %p\n", mAudioHardware);
result.append(buffer);
snprintf(buffer, SIZE, "\tmFd: %d\n", mFd);
result.append(buffer);
::write(fd, result.string(), result.size());
return NO_ERROR;
}
status_t AudioStreamOutGeneric::setParameters(const String8& keyValuePairs)
{
AudioParameter param = AudioParameter(keyValuePairs);
String8 key = String8(AudioParameter::keyRouting);
status_t status = NO_ERROR;
int device;
LOGV("setParameters() %s", keyValuePairs.string());
if (param.getInt(key, device) == NO_ERROR) {
mDevice = device;
param.remove(key);
}
if (param.size()) {
status = BAD_VALUE;
}
return status;
}
String8 AudioStreamOutGeneric::getParameters(const String8& keys)
{
AudioParameter param = AudioParameter(keys);
String8 value;
String8 key = String8(AudioParameter::keyRouting);
if (param.get(key, value) == NO_ERROR) {
param.addInt(key, (int)mDevice);
}
LOGV("getParameters() %s", param.toString().string());
return param.toString();
}
status_t AudioStreamOutGeneric::getRenderPosition(uint32_t *dspFrames)
{
return INVALID_OPERATION;
}
// ----------------------------------------------------------------------------
// record functions
status_t AudioStreamInGeneric::set(
AudioHardwareGeneric *hw,
int fd,
uint32_t devices,
int *pFormat,
uint32_t *pChannels,
uint32_t *pRate,
AudioSystem::audio_in_acoustics acoustics)
{
if (pFormat == 0 || pChannels == 0 || pRate == 0) return BAD_VALUE;
LOGV("AudioStreamInGeneric::set(%p, %d, %d, %d, %u)", hw, fd, *pFormat, *pChannels, *pRate);
// check values
if ((*pFormat != format()) ||
(*pChannels != channels()) ||
(*pRate != sampleRate())) {
LOGE("Error opening input channel");
*pFormat = format();
*pChannels = channels();
*pRate = sampleRate();
return BAD_VALUE;
}
mAudioHardware = hw;
mFd = fd;
mDevice = devices;
return NO_ERROR;
}
AudioStreamInGeneric::~AudioStreamInGeneric()
{
}
ssize_t AudioStreamInGeneric::read(void* buffer, ssize_t bytes)
{
AutoMutex lock(mLock);
if (mFd < 0) {
LOGE("Attempt to read from unopened device");
return NO_INIT;
}
return ::read(mFd, buffer, bytes);
}
status_t AudioStreamInGeneric::dump(int fd, const Vector<String16>& args)
{
const size_t SIZE = 256;
char buffer[SIZE];
String8 result;
snprintf(buffer, SIZE, "AudioStreamInGeneric::dump\n");
result.append(buffer);
snprintf(buffer, SIZE, "\tsample rate: %d\n", sampleRate());
result.append(buffer);
snprintf(buffer, SIZE, "\tbuffer size: %d\n", bufferSize());
result.append(buffer);
snprintf(buffer, SIZE, "\tchannels: %d\n", channels());
result.append(buffer);
snprintf(buffer, SIZE, "\tformat: %d\n", format());
result.append(buffer);
snprintf(buffer, SIZE, "\tdevice: %d\n", mDevice);
result.append(buffer);
snprintf(buffer, SIZE, "\tmAudioHardware: %p\n", mAudioHardware);
result.append(buffer);
snprintf(buffer, SIZE, "\tmFd: %d\n", mFd);
result.append(buffer);
::write(fd, result.string(), result.size());
return NO_ERROR;
}
status_t AudioStreamInGeneric::setParameters(const String8& keyValuePairs)
{
AudioParameter param = AudioParameter(keyValuePairs);
String8 key = String8(AudioParameter::keyRouting);
status_t status = NO_ERROR;
int device;
LOGV("setParameters() %s", keyValuePairs.string());
if (param.getInt(key, device) == NO_ERROR) {
mDevice = device;
param.remove(key);
}
if (param.size()) {
status = BAD_VALUE;
}
return status;
}
String8 AudioStreamInGeneric::getParameters(const String8& keys)
{
AudioParameter param = AudioParameter(keys);
String8 value;
String8 key = String8(AudioParameter::keyRouting);
if (param.get(key, value) == NO_ERROR) {
param.addInt(key, (int)mDevice);
}
LOGV("getParameters() %s", param.toString().string());
return param.toString();
}
// ----------------------------------------------------------------------------
}; // namespace android

View file

@ -0,0 +1,151 @@
/*
**
** Copyright 2007, The Android Open Source Project
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
** http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*/
#ifndef ANDROID_AUDIO_HARDWARE_GENERIC_H
#define ANDROID_AUDIO_HARDWARE_GENERIC_H
#include <stdint.h>
#include <sys/types.h>
#include <utils/threads.h>
#include <hardware_legacy/AudioHardwareBase.h>
namespace android {
// ----------------------------------------------------------------------------
class AudioHardwareGeneric;
class AudioStreamOutGeneric : public AudioStreamOut {
public:
AudioStreamOutGeneric() : mAudioHardware(0), mFd(-1) {}
virtual ~AudioStreamOutGeneric();
virtual status_t set(
AudioHardwareGeneric *hw,
int mFd,
uint32_t devices,
int *pFormat,
uint32_t *pChannels,
uint32_t *pRate);
virtual uint32_t sampleRate() const { return 44100; }
virtual size_t bufferSize() const { return 4096; }
virtual uint32_t channels() const { return AudioSystem::CHANNEL_OUT_STEREO; }
virtual int format() const { return AudioSystem::PCM_16_BIT; }
virtual uint32_t latency() const { return 20; }
virtual status_t setVolume(float left, float right) { return INVALID_OPERATION; }
virtual ssize_t write(const void* buffer, size_t bytes);
virtual status_t standby();
virtual status_t dump(int fd, const Vector<String16>& args);
virtual status_t setParameters(const String8& keyValuePairs);
virtual String8 getParameters(const String8& keys);
virtual status_t getRenderPosition(uint32_t *dspFrames);
private:
AudioHardwareGeneric *mAudioHardware;
Mutex mLock;
int mFd;
uint32_t mDevice;
};
class AudioStreamInGeneric : public AudioStreamIn {
public:
AudioStreamInGeneric() : mAudioHardware(0), mFd(-1) {}
virtual ~AudioStreamInGeneric();
virtual status_t set(
AudioHardwareGeneric *hw,
int mFd,
uint32_t devices,
int *pFormat,
uint32_t *pChannels,
uint32_t *pRate,
AudioSystem::audio_in_acoustics acoustics);
virtual uint32_t sampleRate() const { return 8000; }
virtual size_t bufferSize() const { return 320; }
virtual uint32_t channels() const { return AudioSystem::CHANNEL_IN_MONO; }
virtual int format() const { return AudioSystem::PCM_16_BIT; }
virtual status_t setGain(float gain) { return INVALID_OPERATION; }
virtual ssize_t read(void* buffer, ssize_t bytes);
virtual status_t dump(int fd, const Vector<String16>& args);
virtual status_t standby() { return NO_ERROR; }
virtual status_t setParameters(const String8& keyValuePairs);
virtual String8 getParameters(const String8& keys);
virtual unsigned int getInputFramesLost() const { return 0; }
private:
AudioHardwareGeneric *mAudioHardware;
Mutex mLock;
int mFd;
uint32_t mDevice;
};
class AudioHardwareGeneric : public AudioHardwareBase
{
public:
AudioHardwareGeneric();
virtual ~AudioHardwareGeneric();
virtual status_t initCheck();
virtual status_t setVoiceVolume(float volume);
virtual status_t setMasterVolume(float volume);
// mic mute
virtual status_t setMicMute(bool state);
virtual status_t getMicMute(bool* state);
// create I/O streams
virtual AudioStreamOut* openOutputStream(
uint32_t devices,
int *format=0,
uint32_t *channels=0,
uint32_t *sampleRate=0,
status_t *status=0);
virtual void closeOutputStream(AudioStreamOut* out);
virtual AudioStreamIn* openInputStream(
uint32_t devices,
int *format,
uint32_t *channels,
uint32_t *sampleRate,
status_t *status,
AudioSystem::audio_in_acoustics acoustics);
virtual void closeInputStream(AudioStreamIn* in);
void closeOutputStream(AudioStreamOutGeneric* out);
void closeInputStream(AudioStreamInGeneric* in);
protected:
virtual status_t dump(int fd, const Vector<String16>& args);
private:
status_t dumpInternals(int fd, const Vector<String16>& args);
Mutex mLock;
AudioStreamOutGeneric *mOutput;
AudioStreamInGeneric *mInput;
int mFd;
bool mMicMute;
};
// ----------------------------------------------------------------------------
}; // namespace android
#endif // ANDROID_AUDIO_HARDWARE_GENERIC_H

View file

@ -0,0 +1,183 @@
/*
**
** Copyright 2007, The Android Open Source Project
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
** http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*/
#include <cutils/properties.h>
#include <string.h>
#include <unistd.h>
//#define LOG_NDEBUG 0
#define LOG_TAG "AudioHardwareInterface"
#include <utils/Log.h>
#include <utils/String8.h>
#include "AudioHardwareStub.h"
#include "AudioHardwareGeneric.h"
#ifdef WITH_A2DP
#include "A2dpAudioInterface.h"
#endif
#ifdef ENABLE_AUDIO_DUMP
#include "AudioDumpInterface.h"
#endif
// change to 1 to log routing calls
#define LOG_ROUTING_CALLS 1
namespace android {
#if LOG_ROUTING_CALLS
static const char* routingModeStrings[] =
{
"OUT OF RANGE",
"INVALID",
"CURRENT",
"NORMAL",
"RINGTONE",
"IN_CALL",
"IN_COMMUNICATION"
};
static const char* routeNone = "NONE";
static const char* displayMode(int mode)
{
if ((mode < AudioSystem::MODE_INVALID) || (mode >= AudioSystem::NUM_MODES))
return routingModeStrings[0];
return routingModeStrings[mode+3];
}
#endif
// ----------------------------------------------------------------------------
AudioHardwareInterface* AudioHardwareInterface::create()
{
/*
* FIXME: This code needs to instantiate the correct audio device
* interface. For now - we use compile-time switches.
*/
AudioHardwareInterface* hw = 0;
char value[PROPERTY_VALUE_MAX];
#ifdef GENERIC_AUDIO
hw = new AudioHardwareGeneric();
#else
// if running in emulation - use the emulator driver
if (property_get("ro.kernel.qemu", value, 0)) {
LOGD("Running in emulation - using generic audio driver");
hw = new AudioHardwareGeneric();
}
else {
LOGV("Creating Vendor Specific AudioHardware");
hw = createAudioHardware();
}
#endif
if (hw->initCheck() != NO_ERROR) {
LOGW("Using stubbed audio hardware. No sound will be produced.");
delete hw;
hw = new AudioHardwareStub();
}
#ifdef WITH_A2DP
hw = new A2dpAudioInterface(hw);
#endif
#ifdef ENABLE_AUDIO_DUMP
// This code adds a record of buffers in a file to write calls made by AudioFlinger.
// It replaces the current AudioHardwareInterface object by an intermediate one which
// will record buffers in a file (after sending them to hardware) for testing purpose.
// This feature is enabled by defining symbol ENABLE_AUDIO_DUMP.
// The output file is set with setParameters("test_cmd_file_name=<name>"). Pause are not recorded in the file.
LOGV("opening PCM dump interface");
hw = new AudioDumpInterface(hw); // replace interface
#endif
return hw;
}
AudioStreamOut::~AudioStreamOut()
{
}
AudioStreamIn::~AudioStreamIn() {}
AudioHardwareBase::AudioHardwareBase()
{
mMode = 0;
}
status_t AudioHardwareBase::setMode(int mode)
{
#if LOG_ROUTING_CALLS
LOGD("setMode(%s)", displayMode(mode));
#endif
if ((mode < 0) || (mode >= AudioSystem::NUM_MODES))
return BAD_VALUE;
if (mMode == mode)
return ALREADY_EXISTS;
mMode = mode;
return NO_ERROR;
}
// default implementation
status_t AudioHardwareBase::setParameters(const String8& keyValuePairs)
{
return NO_ERROR;
}
// default implementation
String8 AudioHardwareBase::getParameters(const String8& keys)
{
AudioParameter param = AudioParameter(keys);
return param.toString();
}
// default implementation
size_t AudioHardwareBase::getInputBufferSize(uint32_t sampleRate, int format, int channelCount)
{
if (sampleRate != 8000) {
LOGW("getInputBufferSize bad sampling rate: %d", sampleRate);
return 0;
}
if (format != AudioSystem::PCM_16_BIT) {
LOGW("getInputBufferSize bad format: %d", format);
return 0;
}
if (channelCount != 1) {
LOGW("getInputBufferSize bad channel count: %d", channelCount);
return 0;
}
return 320;
}
status_t AudioHardwareBase::dumpState(int fd, const Vector<String16>& args)
{
const size_t SIZE = 256;
char buffer[SIZE];
String8 result;
snprintf(buffer, SIZE, "AudioHardwareBase::dumpState\n");
result.append(buffer);
snprintf(buffer, SIZE, "\tmMode: %d\n", mMode);
result.append(buffer);
::write(fd, result.string(), result.size());
dump(fd, args); // Dump the state of the concrete child.
return NO_ERROR;
}
// ----------------------------------------------------------------------------
}; // namespace android

209
audio/AudioHardwareStub.cpp Normal file
View file

@ -0,0 +1,209 @@
/* //device/servers/AudioFlinger/AudioHardwareStub.cpp
**
** Copyright 2007, The Android Open Source Project
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
** http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*/
#include <stdint.h>
#include <sys/types.h>
#include <stdlib.h>
#include <unistd.h>
#include <utils/String8.h>
#include "AudioHardwareStub.h"
#include <media/AudioRecord.h>
namespace android {
// ----------------------------------------------------------------------------
AudioHardwareStub::AudioHardwareStub() : mMicMute(false)
{
}
AudioHardwareStub::~AudioHardwareStub()
{
}
status_t AudioHardwareStub::initCheck()
{
return NO_ERROR;
}
AudioStreamOut* AudioHardwareStub::openOutputStream(
uint32_t devices, int *format, uint32_t *channels, uint32_t *sampleRate, status_t *status)
{
AudioStreamOutStub* out = new AudioStreamOutStub();
status_t lStatus = out->set(format, channels, sampleRate);
if (status) {
*status = lStatus;
}
if (lStatus == NO_ERROR)
return out;
delete out;
return 0;
}
void AudioHardwareStub::closeOutputStream(AudioStreamOut* out)
{
delete out;
}
AudioStreamIn* AudioHardwareStub::openInputStream(
uint32_t devices, int *format, uint32_t *channels, uint32_t *sampleRate,
status_t *status, AudioSystem::audio_in_acoustics acoustics)
{
// check for valid input source
if (!AudioSystem::isInputDevice((AudioSystem::audio_devices)devices)) {
return 0;
}
AudioStreamInStub* in = new AudioStreamInStub();
status_t lStatus = in->set(format, channels, sampleRate, acoustics);
if (status) {
*status = lStatus;
}
if (lStatus == NO_ERROR)
return in;
delete in;
return 0;
}
void AudioHardwareStub::closeInputStream(AudioStreamIn* in)
{
delete in;
}
status_t AudioHardwareStub::setVoiceVolume(float volume)
{
return NO_ERROR;
}
status_t AudioHardwareStub::setMasterVolume(float volume)
{
return NO_ERROR;
}
status_t AudioHardwareStub::dumpInternals(int fd, const Vector<String16>& args)
{
const size_t SIZE = 256;
char buffer[SIZE];
String8 result;
result.append("AudioHardwareStub::dumpInternals\n");
snprintf(buffer, SIZE, "\tmMicMute: %s\n", mMicMute? "true": "false");
result.append(buffer);
::write(fd, result.string(), result.size());
return NO_ERROR;
}
status_t AudioHardwareStub::dump(int fd, const Vector<String16>& args)
{
dumpInternals(fd, args);
return NO_ERROR;
}
// ----------------------------------------------------------------------------
status_t AudioStreamOutStub::set(int *pFormat, uint32_t *pChannels, uint32_t *pRate)
{
if (pFormat) *pFormat = format();
if (pChannels) *pChannels = channels();
if (pRate) *pRate = sampleRate();
return NO_ERROR;
}
ssize_t AudioStreamOutStub::write(const void* buffer, size_t bytes)
{
// fake timing for audio output
usleep(bytes * 1000000 / sizeof(int16_t) / AudioSystem::popCount(channels()) / sampleRate());
return bytes;
}
status_t AudioStreamOutStub::standby()
{
return NO_ERROR;
}
status_t AudioStreamOutStub::dump(int fd, const Vector<String16>& args)
{
const size_t SIZE = 256;
char buffer[SIZE];
String8 result;
snprintf(buffer, SIZE, "AudioStreamOutStub::dump\n");
snprintf(buffer, SIZE, "\tsample rate: %d\n", sampleRate());
snprintf(buffer, SIZE, "\tbuffer size: %d\n", bufferSize());
snprintf(buffer, SIZE, "\tchannels: %d\n", channels());
snprintf(buffer, SIZE, "\tformat: %d\n", format());
result.append(buffer);
::write(fd, result.string(), result.size());
return NO_ERROR;
}
String8 AudioStreamOutStub::getParameters(const String8& keys)
{
AudioParameter param = AudioParameter(keys);
return param.toString();
}
status_t AudioStreamOutStub::getRenderPosition(uint32_t *dspFrames)
{
return INVALID_OPERATION;
}
// ----------------------------------------------------------------------------
status_t AudioStreamInStub::set(int *pFormat, uint32_t *pChannels, uint32_t *pRate,
AudioSystem::audio_in_acoustics acoustics)
{
return NO_ERROR;
}
ssize_t AudioStreamInStub::read(void* buffer, ssize_t bytes)
{
// fake timing for audio input
usleep(bytes * 1000000 / sizeof(int16_t) / AudioSystem::popCount(channels()) / sampleRate());
memset(buffer, 0, bytes);
return bytes;
}
status_t AudioStreamInStub::dump(int fd, const Vector<String16>& args)
{
const size_t SIZE = 256;
char buffer[SIZE];
String8 result;
snprintf(buffer, SIZE, "AudioStreamInStub::dump\n");
result.append(buffer);
snprintf(buffer, SIZE, "\tsample rate: %d\n", sampleRate());
result.append(buffer);
snprintf(buffer, SIZE, "\tbuffer size: %d\n", bufferSize());
result.append(buffer);
snprintf(buffer, SIZE, "\tchannels: %d\n", channels());
result.append(buffer);
snprintf(buffer, SIZE, "\tformat: %d\n", format());
result.append(buffer);
::write(fd, result.string(), result.size());
return NO_ERROR;
}
String8 AudioStreamInStub::getParameters(const String8& keys)
{
AudioParameter param = AudioParameter(keys);
return param.toString();
}
// ----------------------------------------------------------------------------
}; // namespace android

106
audio/AudioHardwareStub.h Normal file
View file

@ -0,0 +1,106 @@
/* //device/servers/AudioFlinger/AudioHardwareStub.h
**
** Copyright 2007, The Android Open Source Project
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
** http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*/
#ifndef ANDROID_AUDIO_HARDWARE_STUB_H
#define ANDROID_AUDIO_HARDWARE_STUB_H
#include <stdint.h>
#include <sys/types.h>
#include <hardware_legacy/AudioHardwareBase.h>
namespace android {
// ----------------------------------------------------------------------------
class AudioStreamOutStub : public AudioStreamOut {
public:
virtual status_t set(int *pFormat, uint32_t *pChannels, uint32_t *pRate);
virtual uint32_t sampleRate() const { return 44100; }
virtual size_t bufferSize() const { return 4096; }
virtual uint32_t channels() const { return AudioSystem::CHANNEL_OUT_STEREO; }
virtual int format() const { return AudioSystem::PCM_16_BIT; }
virtual uint32_t latency() const { return 0; }
virtual status_t setVolume(float left, float right) { return NO_ERROR; }
virtual ssize_t write(const void* buffer, size_t bytes);
virtual status_t standby();
virtual status_t dump(int fd, const Vector<String16>& args);
virtual status_t setParameters(const String8& keyValuePairs) { return NO_ERROR;}
virtual String8 getParameters(const String8& keys);
virtual status_t getRenderPosition(uint32_t *dspFrames);
};
class AudioStreamInStub : public AudioStreamIn {
public:
virtual status_t set(int *pFormat, uint32_t *pChannels, uint32_t *pRate, AudioSystem::audio_in_acoustics acoustics);
virtual uint32_t sampleRate() const { return 8000; }
virtual size_t bufferSize() const { return 320; }
virtual uint32_t channels() const { return AudioSystem::CHANNEL_IN_MONO; }
virtual int format() const { return AudioSystem::PCM_16_BIT; }
virtual status_t setGain(float gain) { return NO_ERROR; }
virtual ssize_t read(void* buffer, ssize_t bytes);
virtual status_t dump(int fd, const Vector<String16>& args);
virtual status_t standby() { return NO_ERROR; }
virtual status_t setParameters(const String8& keyValuePairs) { return NO_ERROR;}
virtual String8 getParameters(const String8& keys);
virtual unsigned int getInputFramesLost() const { return 0; }
};
class AudioHardwareStub : public AudioHardwareBase
{
public:
AudioHardwareStub();
virtual ~AudioHardwareStub();
virtual status_t initCheck();
virtual status_t setVoiceVolume(float volume);
virtual status_t setMasterVolume(float volume);
// mic mute
virtual status_t setMicMute(bool state) { mMicMute = state; return NO_ERROR; }
virtual status_t getMicMute(bool* state) { *state = mMicMute ; return NO_ERROR; }
// create I/O streams
virtual AudioStreamOut* openOutputStream(
uint32_t devices,
int *format=0,
uint32_t *channels=0,
uint32_t *sampleRate=0,
status_t *status=0);
virtual void closeOutputStream(AudioStreamOut* out);
virtual AudioStreamIn* openInputStream(
uint32_t devices,
int *format,
uint32_t *channels,
uint32_t *sampleRate,
status_t *status,
AudioSystem::audio_in_acoustics acoustics);
virtual void closeInputStream(AudioStreamIn* in);
protected:
virtual status_t dump(int fd, const Vector<String16>& args);
bool mMicMute;
private:
status_t dumpInternals(int fd, const Vector<String16>& args);
};
// ----------------------------------------------------------------------------
}; // namespace android
#endif // ANDROID_AUDIO_HARDWARE_STUB_H

File diff suppressed because it is too large Load diff