2009-03-04 03:31:44 +00:00
|
|
|
/*
|
|
|
|
* 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"
|
|
|
|
|
|
|
|
|
|
|
|
namespace android {
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
//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)
|
2009-03-04 03:31:44 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
A2dpAudioInterface::~A2dpAudioInterface()
|
|
|
|
{
|
2009-07-17 19:17:14 +00:00
|
|
|
closeOutputStream((AudioStreamOut *)mOutput);
|
|
|
|
delete mHardwareInterface;
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
status_t A2dpAudioInterface::initCheck()
|
|
|
|
{
|
2009-07-17 19:17:14 +00:00
|
|
|
if (mHardwareInterface == 0) return NO_INIT;
|
|
|
|
return mHardwareInterface->initCheck();
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AudioStreamOut* A2dpAudioInterface::openOutputStream(
|
2009-07-17 19:17:14 +00:00
|
|
|
uint32_t devices, int *format, uint32_t *channels, uint32_t *sampleRate, status_t *status)
|
2009-03-04 03:31:44 +00:00
|
|
|
{
|
2009-07-17 19:17:14 +00:00
|
|
|
if (!AudioSystem::isA2dpDevice((AudioSystem::audio_devices)devices)) {
|
|
|
|
LOGV("A2dpAudioInterface::openOutputStream() open HW device: %x", devices);
|
|
|
|
return mHardwareInterface->openOutputStream(devices, format, channels, sampleRate, status);
|
|
|
|
}
|
|
|
|
|
2009-03-04 03:31:44 +00:00
|
|
|
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();
|
2009-07-17 19:17:14 +00:00
|
|
|
if ((err = out->set(devices, format, channels, sampleRate)) == NO_ERROR) {
|
2009-03-04 03:31:44 +00:00
|
|
|
mOutput = out;
|
2009-07-17 19:17:14 +00:00
|
|
|
mOutput->setBluetoothEnabled(mBluetoothEnabled);
|
2009-03-04 03:31:44 +00:00
|
|
|
} else {
|
|
|
|
delete out;
|
|
|
|
}
|
2009-07-17 19:17:14 +00:00
|
|
|
|
2009-03-04 03:31:44 +00:00
|
|
|
if (status)
|
|
|
|
*status = err;
|
|
|
|
return mOutput;
|
|
|
|
}
|
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
void A2dpAudioInterface::closeOutputStream(AudioStreamOut* out) {
|
|
|
|
if (mOutput == 0 || mOutput != out) {
|
|
|
|
LOGW("Attempt to close invalid output stream");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
delete mOutput;
|
|
|
|
mOutput = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-04 03:31:44 +00:00
|
|
|
AudioStreamIn* A2dpAudioInterface::openInputStream(
|
2009-07-17 19:17:14 +00:00
|
|
|
uint32_t devices, int *format, uint32_t *channels, uint32_t *sampleRate, status_t *status,
|
|
|
|
AudioSystem::audio_in_acoustics acoustics)
|
2009-03-04 03:31:44 +00:00
|
|
|
{
|
2009-07-17 19:17:14 +00:00
|
|
|
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);
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
status_t A2dpAudioInterface::setMicMute(bool state)
|
|
|
|
{
|
2009-07-17 19:17:14 +00:00
|
|
|
return mHardwareInterface->setMicMute(state);
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
status_t A2dpAudioInterface::getMicMute(bool* state)
|
|
|
|
{
|
2009-07-17 19:17:14 +00:00
|
|
|
return mHardwareInterface->getMicMute(state);
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
status_t A2dpAudioInterface::setParameters(const String8& keyValuePairs)
|
2009-03-04 03:31:44 +00:00
|
|
|
{
|
2009-07-17 19:17:14 +00:00
|
|
|
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);
|
|
|
|
}
|
2009-04-02 08:21:13 +00:00
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
if (param.size()) {
|
|
|
|
status_t hwStatus = mHardwareInterface->setParameters(param.toString());
|
|
|
|
if (status == NO_ERROR) {
|
|
|
|
status = hwStatus;
|
|
|
|
}
|
|
|
|
}
|
2009-04-02 08:21:13 +00:00
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
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);
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
2009-07-17 19:17:14 +00:00
|
|
|
|
|
|
|
String8 keyValuePairs = a2dpParam.toString();
|
|
|
|
|
|
|
|
if (param.size()) {
|
|
|
|
keyValuePairs += ";";
|
|
|
|
keyValuePairs += mHardwareInterface->getParameters(param.toString());
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
LOGV("getParameters() %s", keyValuePairs.string());
|
|
|
|
return keyValuePairs;
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
size_t A2dpAudioInterface::getInputBufferSize(uint32_t sampleRate, int format, int channelCount)
|
2009-03-04 03:31:44 +00:00
|
|
|
{
|
2009-07-17 19:17:14 +00:00
|
|
|
return mHardwareInterface->getInputBufferSize(sampleRate, format, channelCount);
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
status_t A2dpAudioInterface::setVoiceVolume(float v)
|
2009-03-04 03:31:44 +00:00
|
|
|
{
|
2009-07-17 19:17:14 +00:00
|
|
|
return mHardwareInterface->setVoiceVolume(v);
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
status_t A2dpAudioInterface::setMasterVolume(float v)
|
2009-03-04 03:31:44 +00:00
|
|
|
{
|
2009-07-17 19:17:14 +00:00
|
|
|
return mHardwareInterface->setMasterVolume(v);
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
status_t A2dpAudioInterface::dump(int fd, const Vector<String16>& args)
|
|
|
|
{
|
2009-07-17 19:17:14 +00:00
|
|
|
return mHardwareInterface->dumpState(fd, args);
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
A2dpAudioInterface::A2dpAudioStreamOut::A2dpAudioStreamOut() :
|
2009-04-02 08:21:13 +00:00
|
|
|
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
|
2009-07-17 19:17:14 +00:00
|
|
|
mBluetoothEnabled(true), mDevice(0)
|
2009-03-04 03:31:44 +00:00
|
|
|
{
|
|
|
|
// use any address by default
|
2009-03-09 18:52:12 +00:00
|
|
|
strcpy(mA2dpAddress, "00:00:00:00:00:00");
|
|
|
|
init();
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
status_t A2dpAudioInterface::A2dpAudioStreamOut::set(
|
2009-07-17 19:17:14 +00:00
|
|
|
uint32_t device, int *pFormat, uint32_t *pChannels, uint32_t *pRate)
|
2009-03-04 03:31:44 +00:00
|
|
|
{
|
2009-07-17 19:17:14 +00:00
|
|
|
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);
|
2009-03-04 03:31:44 +00:00
|
|
|
|
|
|
|
// fix up defaults
|
2009-07-17 19:17:14 +00:00
|
|
|
if (lFormat == 0) lFormat = format();
|
|
|
|
if (lChannels == 0) lChannels = channels();
|
|
|
|
if (lRate == 0) lRate = sampleRate();
|
2009-03-04 03:31:44 +00:00
|
|
|
|
|
|
|
// check values
|
2009-07-17 19:17:14 +00:00
|
|
|
if ((lFormat != format()) ||
|
|
|
|
(lChannels != channels()) ||
|
|
|
|
(lRate != sampleRate())){
|
|
|
|
if (pFormat) *pFormat = format();
|
|
|
|
if (pChannels) *pChannels = channels();
|
|
|
|
if (pRate) *pRate = sampleRate();
|
2009-03-04 03:31:44 +00:00
|
|
|
return BAD_VALUE;
|
2009-07-17 19:17:14 +00:00
|
|
|
}
|
2009-03-04 03:31:44 +00:00
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
if (pFormat) *pFormat = lFormat;
|
|
|
|
if (pChannels) *pChannels = lChannels;
|
|
|
|
if (pRate) *pRate = lRate;
|
|
|
|
|
|
|
|
mDevice = device;
|
2009-03-04 03:31:44 +00:00
|
|
|
return NO_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
A2dpAudioInterface::A2dpAudioStreamOut::~A2dpAudioStreamOut()
|
|
|
|
{
|
2009-07-17 19:17:14 +00:00
|
|
|
LOGV("A2dpAudioStreamOut destructor");
|
|
|
|
standby();
|
2009-03-04 03:31:44 +00:00
|
|
|
close();
|
2009-07-17 19:17:14 +00:00
|
|
|
LOGV("A2dpAudioStreamOut destructor returning from close()");
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t A2dpAudioInterface::A2dpAudioStreamOut::write(const void* buffer, size_t bytes)
|
2009-04-02 08:21:13 +00:00
|
|
|
{
|
2009-03-09 18:52:12 +00:00
|
|
|
Mutex::Autolock lock(mLock);
|
2009-03-04 03:31:44 +00:00
|
|
|
|
2009-03-09 18:52:12 +00:00
|
|
|
size_t remaining = bytes;
|
2009-04-02 08:21:13 +00:00
|
|
|
status_t status = -1;
|
|
|
|
|
|
|
|
if (!mBluetoothEnabled) {
|
|
|
|
LOGW("A2dpAudioStreamOut::write(), but bluetooth disabled");
|
|
|
|
goto Error;
|
|
|
|
}
|
|
|
|
|
|
|
|
status = init();
|
2009-03-09 18:52:12 +00:00
|
|
|
if (status < 0)
|
|
|
|
goto Error;
|
2009-04-02 08:21:13 +00:00
|
|
|
|
2009-03-04 03:31:44 +00:00
|
|
|
while (remaining > 0) {
|
|
|
|
status = a2dp_write(mData, buffer, remaining);
|
|
|
|
if (status <= 0) {
|
|
|
|
LOGE("a2dp_write failed err: %d\n", status);
|
|
|
|
goto Error;
|
|
|
|
}
|
|
|
|
remaining -= status;
|
|
|
|
buffer = ((char *)buffer) + status;
|
|
|
|
}
|
|
|
|
|
|
|
|
mStandby = false;
|
2009-04-02 08:21:13 +00:00
|
|
|
|
2009-03-04 03:31:44 +00:00
|
|
|
return bytes;
|
|
|
|
|
|
|
|
Error:
|
|
|
|
// Simulate audio output timing in case of error
|
|
|
|
usleep(bytes * 1000000 / frameSize() / sampleRate());
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2009-03-09 18:52:12 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2009-03-04 03:31:44 +00:00
|
|
|
status_t A2dpAudioInterface::A2dpAudioStreamOut::standby()
|
|
|
|
{
|
|
|
|
int result = 0;
|
|
|
|
|
2009-03-09 18:52:12 +00:00
|
|
|
Mutex::Autolock lock(mLock);
|
|
|
|
|
2009-03-04 03:31:44 +00:00
|
|
|
if (!mStandby) {
|
|
|
|
result = a2dp_stop(mData);
|
|
|
|
if (result == 0)
|
|
|
|
mStandby = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
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 = 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();
|
|
|
|
}
|
|
|
|
|
2009-03-04 03:31:44 +00:00
|
|
|
status_t A2dpAudioInterface::A2dpAudioStreamOut::setAddress(const char* address)
|
|
|
|
{
|
2009-03-09 18:52:12 +00:00
|
|
|
Mutex::Autolock lock(mLock);
|
|
|
|
|
|
|
|
if (strlen(address) != strlen("00:00:00:00:00:00"))
|
2009-03-04 03:31:44 +00:00
|
|
|
return -EINVAL;
|
|
|
|
|
2009-03-09 18:52:12 +00:00
|
|
|
strcpy(mA2dpAddress, address);
|
|
|
|
if (mData)
|
|
|
|
a2dp_set_sink(mData, mA2dpAddress);
|
|
|
|
|
2009-03-04 03:31:44 +00:00
|
|
|
return NO_ERROR;
|
|
|
|
}
|
|
|
|
|
2009-04-02 08:21:13 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2009-03-04 03:31:44 +00:00
|
|
|
status_t A2dpAudioInterface::A2dpAudioStreamOut::close()
|
2009-04-02 08:21:13 +00:00
|
|
|
{
|
|
|
|
Mutex::Autolock lock(mLock);
|
2009-07-17 19:17:14 +00:00
|
|
|
LOGV("A2dpAudioStreamOut::close() calling close_l()");
|
2009-04-02 08:21:13 +00:00
|
|
|
return close_l();
|
|
|
|
}
|
|
|
|
|
|
|
|
status_t A2dpAudioInterface::A2dpAudioStreamOut::close_l()
|
2009-03-04 03:31:44 +00:00
|
|
|
{
|
|
|
|
if (mData) {
|
2009-07-17 19:17:14 +00:00
|
|
|
LOGV("A2dpAudioStreamOut::close_l() calling a2dp_cleanup(mData)");
|
2009-03-04 03:31:44 +00:00
|
|
|
a2dp_cleanup(mData);
|
|
|
|
mData = NULL;
|
|
|
|
}
|
|
|
|
return NO_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
status_t A2dpAudioInterface::A2dpAudioStreamOut::dump(int fd, const Vector<String16>& args)
|
|
|
|
{
|
|
|
|
return NO_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
}; // namespace android
|