audio: Fix BT AIDL HAL module implementation

In order to align with legacy behavior, when opening a stream,
the module must suggest the current configuration of the BT session.
For that to work, the BT device proxy must be opened prior
to creating a stream, code moved to ModuleBluetooth.

Fix minor inconsistencies and bugs found during testing.

Bug: 301213930
Bug: 316027906
Test: atest pts-bot
Change-Id: I04ddaf73be82f872a3f32a789563c3cbd648eb61
This commit is contained in:
Mikhail Naganov
2023-12-13 14:35:11 -08:00
parent 3b732895a8
commit a88cf60b87
7 changed files with 397 additions and 245 deletions

View File

@@ -24,13 +24,25 @@
using aidl::android::hardware::audio::common::SinkMetadata;
using aidl::android::hardware::audio::common::SourceMetadata;
using aidl::android::hardware::bluetooth::audio::ChannelMode;
using aidl::android::hardware::bluetooth::audio::PcmConfiguration;
using aidl::android::media::audio::common::AudioChannelLayout;
using aidl::android::media::audio::common::AudioConfigBase;
using aidl::android::media::audio::common::AudioDeviceDescription;
using aidl::android::media::audio::common::AudioDeviceType;
using aidl::android::media::audio::common::AudioFormatDescription;
using aidl::android::media::audio::common::AudioFormatType;
using aidl::android::media::audio::common::AudioIoFlags;
using aidl::android::media::audio::common::AudioOffloadInfo;
using aidl::android::media::audio::common::AudioPort;
using aidl::android::media::audio::common::AudioPortConfig;
using aidl::android::media::audio::common::AudioPortExt;
using aidl::android::media::audio::common::AudioProfile;
using aidl::android::media::audio::common::Int;
using aidl::android::media::audio::common::MicrophoneInfo;
using aidl::android::media::audio::common::PcmType;
using android::bluetooth::audio::aidl::BluetoothAudioPortAidl;
using android::bluetooth::audio::aidl::BluetoothAudioPortAidlIn;
using android::bluetooth::audio::aidl::BluetoothAudioPortAidlOut;
// TODO(b/312265159) bluetooth audio should be in its own process
@@ -39,6 +51,35 @@ extern "C" binder_status_t createIBluetoothAudioProviderFactory();
namespace aidl::android::hardware::audio::core {
namespace {
PcmType pcmTypeFromBitsPerSample(int8_t bitsPerSample) {
if (bitsPerSample == 8)
return PcmType::UINT_8_BIT;
else if (bitsPerSample == 16)
return PcmType::INT_16_BIT;
else if (bitsPerSample == 24)
return PcmType::INT_24_BIT;
else if (bitsPerSample == 32)
return PcmType::INT_32_BIT;
ALOGE("Unsupported bitsPerSample: %d", bitsPerSample);
return PcmType::DEFAULT;
}
AudioChannelLayout channelLayoutFromChannelMode(ChannelMode mode) {
if (mode == ChannelMode::MONO) {
return AudioChannelLayout::make<AudioChannelLayout::layoutMask>(
AudioChannelLayout::LAYOUT_MONO);
} else if (mode == ChannelMode::STEREO || mode == ChannelMode::DUALMONO) {
return AudioChannelLayout::make<AudioChannelLayout::layoutMask>(
AudioChannelLayout::LAYOUT_STEREO);
}
ALOGE("Unsupported channel mode: %s", toString(mode).c_str());
return AudioChannelLayout{};
}
} // namespace
ModuleBluetooth::ModuleBluetooth(std::unique_ptr<Module::Configuration>&& config)
: Module(Type::BLUETOOTH, std::move(config)) {
// TODO(b/312265159) bluetooth audio should be in its own process
@@ -95,66 +136,130 @@ ndk::ScopedAStatus ModuleBluetooth::setMicMute(bool in_mute __unused) {
return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
}
ndk::ScopedAStatus ModuleBluetooth::setAudioPortConfig(const AudioPortConfig& in_requested,
AudioPortConfig* out_suggested,
bool* _aidl_return) {
auto fillConfig = [this](const AudioPort& port, AudioPortConfig* config) {
if (port.ext.getTag() == AudioPortExt::device) {
CachedProxy proxy;
auto status = findOrCreateProxy(port, proxy);
if (status.isOk()) {
const auto& pcmConfig = proxy.pcmConfig;
LOG(DEBUG) << "setAudioPortConfig: suggesting port config from "
<< pcmConfig.toString();
const auto pcmType = pcmTypeFromBitsPerSample(pcmConfig.bitsPerSample);
const auto channelMask = channelLayoutFromChannelMode(pcmConfig.channelMode);
if (pcmType != PcmType::DEFAULT && channelMask != AudioChannelLayout{}) {
config->format =
AudioFormatDescription{.type = AudioFormatType::PCM, .pcm = pcmType};
config->channelMask = channelMask;
config->sampleRate = Int{.value = pcmConfig.sampleRateHz};
config->flags = port.flags;
config->ext = port.ext;
return true;
}
}
}
return generateDefaultPortConfig(port, config);
};
return Module::setAudioPortConfigImpl(in_requested, fillConfig, out_suggested, _aidl_return);
}
ndk::ScopedAStatus ModuleBluetooth::checkAudioPatchEndpointsMatch(
const std::vector<AudioPortConfig*>& sources, const std::vector<AudioPortConfig*>& sinks) {
// Both sources and sinks must be non-empty, this is guaranteed by 'setAudioPatch'.
const bool isInput = sources[0]->ext.getTag() == AudioPortExt::device;
const int32_t devicePortId = isInput ? sources[0]->portId : sinks[0]->portId;
const auto proxyIt = mProxies.find(devicePortId);
if (proxyIt == mProxies.end()) return ndk::ScopedAStatus::ok();
const auto& pcmConfig = proxyIt->second.pcmConfig;
const AudioPortConfig* mixPortConfig = isInput ? sinks[0] : sources[0];
if (!StreamBluetooth::checkConfigParams(
pcmConfig, AudioConfigBase{.sampleRate = mixPortConfig->sampleRate->value,
.channelMask = *(mixPortConfig->channelMask),
.format = *(mixPortConfig->format)})) {
return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
}
if (int32_t handle = mixPortConfig->ext.get<AudioPortExt::mix>().handle; handle > 0) {
mConnections.insert(std::pair(handle, devicePortId));
}
return ndk::ScopedAStatus::ok();
}
void ModuleBluetooth::onExternalDeviceConnectionChanged(const AudioPort& audioPort,
bool connected) {
if (!connected) mProxies.erase(audioPort.id);
}
ndk::ScopedAStatus ModuleBluetooth::createInputStream(
StreamContext&& context, const SinkMetadata& sinkMetadata,
const std::vector<MicrophoneInfo>& microphones, std::shared_ptr<StreamIn>* result) {
CachedProxy proxy;
RETURN_STATUS_IF_ERROR(fetchAndCheckProxy(context, proxy));
return createStreamInstance<StreamInBluetooth>(result, std::move(context), sinkMetadata,
microphones, getBtProfileManagerHandles());
microphones, getBtProfileManagerHandles(),
proxy.ptr, proxy.pcmConfig);
}
ndk::ScopedAStatus ModuleBluetooth::createOutputStream(
StreamContext&& context, const SourceMetadata& sourceMetadata,
const std::optional<AudioOffloadInfo>& offloadInfo, std::shared_ptr<StreamOut>* result) {
CachedProxy proxy;
RETURN_STATUS_IF_ERROR(fetchAndCheckProxy(context, proxy));
return createStreamInstance<StreamOutBluetooth>(result, std::move(context), sourceMetadata,
offloadInfo, getBtProfileManagerHandles());
offloadInfo, getBtProfileManagerHandles(),
proxy.ptr, proxy.pcmConfig);
}
ndk::ScopedAStatus ModuleBluetooth::populateConnectedDevicePort(AudioPort* audioPort, int32_t) {
ndk::ScopedAStatus ModuleBluetooth::populateConnectedDevicePort(AudioPort* audioPort,
int32_t nextPortId) {
if (audioPort->ext.getTag() != AudioPortExt::device) {
LOG(ERROR) << __func__ << ": not a device port: " << audioPort->toString();
return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
}
if (!::aidl::android::hardware::bluetooth::audio::BluetoothAudioSession::IsAidlAvailable()) {
LOG(ERROR) << __func__ << ": IBluetoothAudioProviderFactory AIDL service not available";
return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
}
const auto& devicePort = audioPort->ext.get<AudioPortExt::device>();
const auto& description = devicePort.device.type;
// Since the configuration of the BT module is static, there is nothing to populate here.
// However, this method must return an error when the device can not be connected,
// this is determined by the status of BT profiles.
// This method must return an error when the device can not be connected.
if (description.connection == AudioDeviceDescription::CONNECTION_BT_A2DP) {
bool isA2dpEnabled = false;
if (!!mBluetoothA2dp) {
RETURN_STATUS_IF_ERROR((*mBluetoothA2dp).isEnabled(&isA2dpEnabled));
}
LOG(DEBUG) << __func__ << ": isA2dpEnabled: " << isA2dpEnabled;
return isA2dpEnabled ? ndk::ScopedAStatus::ok()
: ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
if (!isA2dpEnabled) return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
} else if (description.connection == AudioDeviceDescription::CONNECTION_BT_LE) {
bool isLeEnabled = false;
if (!!mBluetoothLe) {
RETURN_STATUS_IF_ERROR((*mBluetoothLe).isEnabled(&isLeEnabled));
}
LOG(DEBUG) << __func__ << ": isLeEnabled: " << isLeEnabled;
return isLeEnabled ? ndk::ScopedAStatus::ok()
: ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
if (!isLeEnabled) return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
} else if (description.connection == AudioDeviceDescription::CONNECTION_WIRELESS &&
description.type == AudioDeviceType::OUT_HEARING_AID) {
// Hearing aids can use a number of profiles, thus the only way to check
// connectivity is to try to talk to the BT HAL.
if (!::aidl::android::hardware::bluetooth::audio::BluetoothAudioSession::
IsAidlAvailable()) {
return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
}
std::shared_ptr<BluetoothAudioPortAidl> proxy = std::shared_ptr<BluetoothAudioPortAidl>(
std::make_shared<BluetoothAudioPortAidlOut>());
if (proxy->registerPort(description)) {
LOG(DEBUG) << __func__ << ": registered hearing aid port";
proxy->unregisterPort();
return ndk::ScopedAStatus::ok();
}
LOG(DEBUG) << __func__ << ": failed to register hearing aid port";
return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
// Hearing aids can use a number of profiles, no single switch exists.
} else {
LOG(ERROR) << __func__ << ": unsupported device type: " << audioPort->toString();
return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
}
LOG(ERROR) << __func__ << ": unsupported device type: " << audioPort->toString();
return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
CachedProxy proxy;
RETURN_STATUS_IF_ERROR(createProxy(*audioPort, nextPortId, proxy));
// Since the device is already connected and configured by the BT stack, provide
// the current configuration instead of all possible profiles.
const auto& pcmConfig = proxy.pcmConfig;
audioPort->profiles.clear();
audioPort->profiles.push_back(
AudioProfile{.format = AudioFormatDescription{.type = AudioFormatType::PCM,
.pcm = pcmTypeFromBitsPerSample(
pcmConfig.bitsPerSample)},
.channelMasks = std::vector<AudioChannelLayout>(
{channelLayoutFromChannelMode(pcmConfig.channelMode)}),
.sampleRates = std::vector<int>({pcmConfig.sampleRateHz})});
LOG(DEBUG) << __func__ << ": " << audioPort->toString();
return ndk::ScopedAStatus::ok();
}
ndk::ScopedAStatus ModuleBluetooth::onMasterMuteChanged(bool) {
@@ -167,4 +272,77 @@ ndk::ScopedAStatus ModuleBluetooth::onMasterVolumeChanged(float) {
return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
}
int32_t ModuleBluetooth::getNominalLatencyMs(const AudioPortConfig& portConfig) {
const auto connectionsIt = mConnections.find(portConfig.ext.get<AudioPortExt::mix>().handle);
if (connectionsIt != mConnections.end()) {
const auto proxyIt = mProxies.find(connectionsIt->second);
if (proxyIt != mProxies.end()) {
auto proxy = proxyIt->second.ptr;
size_t dataIntervalUs = 0;
if (!proxy->getPreferredDataIntervalUs(dataIntervalUs)) {
LOG(WARNING) << __func__ << ": could not fetch preferred data interval";
}
const bool isInput = portConfig.flags->getTag() == AudioIoFlags::input;
return isInput ? StreamInBluetooth::getNominalLatencyMs(dataIntervalUs)
: StreamOutBluetooth::getNominalLatencyMs(dataIntervalUs);
}
}
LOG(ERROR) << __func__ << ": no connection or proxy found for " << portConfig.toString();
return Module::getNominalLatencyMs(portConfig);
}
ndk::ScopedAStatus ModuleBluetooth::createProxy(const AudioPort& audioPort, int32_t instancePortId,
CachedProxy& proxy) {
const bool isInput = audioPort.flags.getTag() == AudioIoFlags::input;
proxy.ptr = isInput ? std::shared_ptr<BluetoothAudioPortAidl>(
std::make_shared<BluetoothAudioPortAidlIn>())
: std::shared_ptr<BluetoothAudioPortAidl>(
std::make_shared<BluetoothAudioPortAidlOut>());
const auto& devicePort = audioPort.ext.get<AudioPortExt::device>();
if (const auto device = devicePort.device.type; !proxy.ptr->registerPort(device)) {
LOG(ERROR) << __func__ << ": failed to register BT port for " << device.toString();
return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
}
if (!proxy.ptr->loadAudioConfig(proxy.pcmConfig)) {
LOG(ERROR) << __func__ << ": state=" << proxy.ptr->getState()
<< ", failed to load audio config";
return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
}
mProxies.insert(std::pair(instancePortId, proxy));
return ndk::ScopedAStatus::ok();
}
ndk::ScopedAStatus ModuleBluetooth::fetchAndCheckProxy(const StreamContext& context,
CachedProxy& proxy) {
const auto connectionsIt = mConnections.find(context.getMixPortHandle());
if (connectionsIt != mConnections.end()) {
const auto proxyIt = mProxies.find(connectionsIt->second);
if (proxyIt != mProxies.end()) {
proxy = proxyIt->second;
mProxies.erase(proxyIt);
}
mConnections.erase(connectionsIt);
}
if (proxy.ptr != nullptr) {
if (!StreamBluetooth::checkConfigParams(
proxy.pcmConfig, AudioConfigBase{.sampleRate = context.getSampleRate(),
.channelMask = context.getChannelLayout(),
.format = context.getFormat()})) {
return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
}
}
// Not having a proxy is OK, it may happen in VTS tests when streams are opened on unconnected
// mix ports.
return ndk::ScopedAStatus::ok();
}
ndk::ScopedAStatus ModuleBluetooth::findOrCreateProxy(const AudioPort& audioPort,
CachedProxy& proxy) {
if (auto proxyIt = mProxies.find(audioPort.id); proxyIt != mProxies.end()) {
proxy = proxyIt->second;
return ndk::ScopedAStatus::ok();
}
return createProxy(audioPort, audioPort.id, proxy);
}
} // namespace aidl::android::hardware::audio::core