/* * Copyright (C) 2022 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 #include #define LOG_TAG "VtsHalAudio.ModuleConfig" #include #include #include #include #include #include #include "ModuleConfig.h" using namespace android; using namespace std::chrono_literals; using aidl::android::hardware::audio::common::isBitPositionFlagSet; using aidl::android::hardware::audio::core::IModule; using aidl::android::media::audio::common::AudioChannelLayout; using aidl::android::media::audio::common::AudioDeviceDescription; using aidl::android::media::audio::common::AudioDeviceType; using aidl::android::media::audio::common::AudioEncapsulationMode; using aidl::android::media::audio::common::AudioFormatDescription; using aidl::android::media::audio::common::AudioFormatType; using aidl::android::media::audio::common::AudioInputFlags; using aidl::android::media::audio::common::AudioIoFlags; using aidl::android::media::audio::common::AudioOffloadInfo; using aidl::android::media::audio::common::AudioOutputFlags; 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::AudioUsage; using aidl::android::media::audio::common::Int; // static std::optional ModuleConfig::generateOffloadInfoIfNeeded( const AudioPortConfig& portConfig) { if (portConfig.flags.has_value() && portConfig.flags.value().getTag() == AudioIoFlags::Tag::output && isBitPositionFlagSet(portConfig.flags.value().get(), AudioOutputFlags::COMPRESS_OFFLOAD)) { AudioOffloadInfo offloadInfo; offloadInfo.base.sampleRate = portConfig.sampleRate.value().value; offloadInfo.base.channelMask = portConfig.channelMask.value(); offloadInfo.base.format = portConfig.format.value(); offloadInfo.bitRatePerSecond = 256000; // Arbitrary value. offloadInfo.durationUs = std::chrono::microseconds(1min).count(); // Arbitrary value. offloadInfo.usage = AudioUsage::MEDIA; offloadInfo.encapsulationMode = AudioEncapsulationMode::NONE; return offloadInfo; } return {}; } // static std::vector ModuleConfig::getAudioPortsForDeviceTypes( const std::vector& ports, const std::vector& deviceTypes, const std::string& connection) { std::vector result; for (const auto& port : ports) { if (port.ext.getTag() != AudioPortExt::Tag::device) continue; const auto type = port.ext.get().device.type; if (type.connection == connection) { for (auto deviceType : deviceTypes) { if (type.type == deviceType) { result.push_back(port); } } } } return result; } // static std::vector ModuleConfig::getBuiltInMicPorts( const std::vector& ports) { return getAudioPortsForDeviceTypes( ports, std::vector{AudioDeviceType::IN_MICROPHONE, AudioDeviceType::IN_MICROPHONE_BACK}); } template auto findById(const std::vector& v, int32_t id) { return std::find_if(v.begin(), v.end(), [&](const auto& p) { return p.id == id; }); } ModuleConfig::ModuleConfig(IModule* module) { mStatus = module->getAudioPorts(&mPorts); if (!mStatus.isOk()) return; for (const auto& port : mPorts) { if (port.ext.getTag() != AudioPortExt::Tag::device) continue; const auto& devicePort = port.ext.get(); if (devicePort.device.type.connection.empty()) { const bool isInput = port.flags.getTag() == AudioIoFlags::Tag::input; // Permanently attached device. if (isInput) { mAttachedSourceDevicePorts.insert(port.id); } else { mAttachedSinkDevicePorts.insert(port.id); } } else { mExternalDevicePorts.insert(port.id); } } if (!mStatus.isOk()) return; mStatus = module->getAudioRoutes(&mRoutes); if (!mStatus.isOk()) return; mStatus = module->getAudioPortConfigs(&mInitialConfigs); } std::vector ModuleConfig::getAttachedDevicePorts() const { std::vector result; std::copy_if(mPorts.begin(), mPorts.end(), std::back_inserter(result), [&](const auto& port) { return mAttachedSinkDevicePorts.count(port.id) != 0 || mAttachedSourceDevicePorts.count(port.id) != 0; }); return result; } std::vector ModuleConfig::getAudioPortsForDeviceTypes(const std::vector& deviceTypes, const std::string& connection) const { return getAudioPortsForDeviceTypes(mPorts, deviceTypes, connection); } std::vector ModuleConfig::getConnectedExternalDevicePorts() const { std::vector result; std::copy_if(mPorts.begin(), mPorts.end(), std::back_inserter(result), [&](const auto& port) { return mConnectedExternalSinkDevicePorts.count(port.id) != 0 || mConnectedExternalSourceDevicePorts.count(port.id) != 0; }); return result; } std::set ModuleConfig::getConnectedSinkDevicePorts() const { std::set result; result.insert(mAttachedSinkDevicePorts.begin(), mAttachedSinkDevicePorts.end()); result.insert(mConnectedExternalSinkDevicePorts.begin(), mConnectedExternalSinkDevicePorts.end()); return result; } std::set ModuleConfig::getConnectedSourceDevicePorts() const { std::set result; result.insert(mAttachedSourceDevicePorts.begin(), mAttachedSourceDevicePorts.end()); result.insert(mConnectedExternalSourceDevicePorts.begin(), mConnectedExternalSourceDevicePorts.end()); return result; } std::vector ModuleConfig::getExternalDevicePorts() const { std::vector result; std::copy_if(mPorts.begin(), mPorts.end(), std::back_inserter(result), [&](const auto& port) { return mExternalDevicePorts.count(port.id) != 0; }); return result; } std::vector ModuleConfig::getInputMixPorts(bool connectedOnly) const { std::vector result; std::copy_if(mPorts.begin(), mPorts.end(), std::back_inserter(result), [&](const auto& port) { return port.ext.getTag() == AudioPortExt::Tag::mix && port.flags.getTag() == AudioIoFlags::Tag::input && (!connectedOnly || !getConnectedSourceDevicesPortsForMixPort(port).empty()); }); return result; } std::vector ModuleConfig::getOutputMixPorts(bool connectedOnly) const { std::vector result; std::copy_if(mPorts.begin(), mPorts.end(), std::back_inserter(result), [&](const auto& port) { return port.ext.getTag() == AudioPortExt::Tag::mix && port.flags.getTag() == AudioIoFlags::Tag::output && (!connectedOnly || !getConnectedSinkDevicesPortsForMixPort(port).empty()); }); return result; } std::vector ModuleConfig::getNonBlockingMixPorts(bool connectedOnly, bool singlePort) const { return findMixPorts(false /*isInput*/, connectedOnly, singlePort, [&](const AudioPort& port) { return isBitPositionFlagSet(port.flags.get(), AudioOutputFlags::NON_BLOCKING); }); } std::vector ModuleConfig::getOffloadMixPorts(bool connectedOnly, bool singlePort) const { return findMixPorts(false /*isInput*/, connectedOnly, singlePort, [&](const AudioPort& port) { return isBitPositionFlagSet(port.flags.get(), AudioOutputFlags::COMPRESS_OFFLOAD); }); } std::vector ModuleConfig::getPrimaryMixPorts(bool connectedOnly, bool singlePort) const { return findMixPorts(false /*isInput*/, connectedOnly, singlePort, [&](const AudioPort& port) { return isBitPositionFlagSet(port.flags.get(), AudioOutputFlags::PRIMARY); }); } std::vector ModuleConfig::getMmapOutMixPorts(bool connectedOnly, bool singlePort) const { return findMixPorts(false /*isInput*/, connectedOnly, singlePort, [&](const AudioPort& port) { return isBitPositionFlagSet(port.flags.get(), AudioOutputFlags::MMAP_NOIRQ); }); } std::vector ModuleConfig::getMmapInMixPorts(bool connectedOnly, bool singlePort) const { return findMixPorts(true /*isInput*/, connectedOnly, singlePort, [&](const AudioPort& port) { return isBitPositionFlagSet(port.flags.get(), AudioInputFlags::MMAP_NOIRQ); }); } std::vector ModuleConfig::getRemoteSubmixPorts(bool isInput, bool singlePort) const { AudioDeviceType deviceType = isInput ? AudioDeviceType::IN_SUBMIX : AudioDeviceType::OUT_SUBMIX; auto ports = getAudioPortsForDeviceTypes(std::vector{deviceType}, AudioDeviceDescription::CONNECTION_VIRTUAL); if (singlePort) { if (!ports.empty()) ports.resize(1); } return ports; } std::vector ModuleConfig::getConnectedDevicesPortsForMixPort( bool isInput, const AudioPortConfig& mixPortConfig) const { const auto mixPortIt = findById(mPorts, mixPortConfig.portId); if (mixPortIt != mPorts.end()) { return getConnectedDevicesPortsForMixPort(isInput, *mixPortIt); } return {}; } std::vector ModuleConfig::getConnectedSinkDevicesPortsForMixPort( const AudioPort& mixPort) const { std::vector result; std::set connectedSinkDevicePorts = getConnectedSinkDevicePorts(); for (const auto& route : mRoutes) { if ((connectedSinkDevicePorts.count(route.sinkPortId) != 0) && std::find(route.sourcePortIds.begin(), route.sourcePortIds.end(), mixPort.id) != route.sourcePortIds.end()) { const auto devicePortIt = findById(mPorts, route.sinkPortId); if (devicePortIt != mPorts.end()) result.push_back(*devicePortIt); } } return result; } std::vector ModuleConfig::getConnectedSourceDevicesPortsForMixPort( const AudioPort& mixPort) const { std::vector result; std::set connectedSourceDevicePorts = getConnectedSourceDevicePorts(); for (const auto& route : mRoutes) { if (route.sinkPortId == mixPort.id) { for (const auto srcId : route.sourcePortIds) { if (connectedSourceDevicePorts.count(srcId) != 0) { const auto devicePortIt = findById(mPorts, srcId); if (devicePortIt != mPorts.end()) result.push_back(*devicePortIt); } } } } return result; } std::optional ModuleConfig::getSourceMixPortForConnectedDevice() const { std::set connectedSinkDevicePorts = getConnectedSinkDevicePorts(); for (const auto& route : mRoutes) { if (connectedSinkDevicePorts.count(route.sinkPortId) != 0) { const auto mixPortIt = findById(mPorts, route.sourcePortIds[0]); if (mixPortIt != mPorts.end()) return *mixPortIt; } } return {}; } std::vector ModuleConfig::getRoutableDevicePortsForMixPort(const AudioPort& port, bool connectedOnly) const { std::set portIds = findRoutablePortIds(port.id); const bool isInput = port.flags.getTag() == AudioIoFlags::input; std::set devicePortIds; if (connectedOnly) { devicePortIds = isInput ? getConnectedSourceDevicePorts() : getConnectedSinkDevicePorts(); } else { devicePortIds = portIds; } std::vector result; std::copy_if(mPorts.begin(), mPorts.end(), std::back_inserter(result), [&](const auto& port) { return port.ext.getTag() == AudioPortExt::Tag::device && portIds.count(port.id) > 0 && devicePortIds.count(port.id) > 0; }); return result; } std::vector ModuleConfig::getRoutableMixPortsForDevicePort(const AudioPort& port, bool connectedOnly) const { std::set portIds = findRoutablePortIds(port.id); const bool isInput = port.flags.getTag() == AudioIoFlags::input; return findMixPorts(isInput, connectedOnly, false /*singlePort*/, [&portIds](const AudioPort& p) { return portIds.count(p.id) > 0; }); } std::optional ModuleConfig::getNonRoutableSrcSinkPair( bool isInput) const { const auto mixPorts = getMixPorts(isInput, false /*connectedOnly*/); std::set> allowedRoutes; for (const auto& route : mRoutes) { for (const auto srcPortId : route.sourcePortIds) { allowedRoutes.emplace(std::make_pair(srcPortId, route.sinkPortId)); } } auto make_pair = [isInput](auto& device, auto& mix) { return isInput ? std::make_pair(device, mix) : std::make_pair(mix, device); }; for (const auto portId : isInput ? getConnectedSourceDevicePorts() : getConnectedSinkDevicePorts()) { const auto devicePortIt = findById(mPorts, portId); if (devicePortIt == mPorts.end()) continue; auto devicePortConfig = getSingleConfigForDevicePort(*devicePortIt); for (const auto& mixPort : mixPorts) { if (std::find(allowedRoutes.begin(), allowedRoutes.end(), make_pair(portId, mixPort.id)) == allowedRoutes.end()) { auto mixPortConfig = getSingleConfigForMixPort(isInput, mixPort); if (mixPortConfig.has_value()) { return make_pair(devicePortConfig, mixPortConfig.value()); } } } } return {}; } std::optional ModuleConfig::getRoutableSrcSinkPair(bool isInput) const { if (isInput) { std::set connectedSourceDevicePorts = getConnectedSourceDevicePorts(); for (const auto& route : mRoutes) { auto srcPortIdIt = std::find_if( route.sourcePortIds.begin(), route.sourcePortIds.end(), [&](const auto& portId) { return connectedSourceDevicePorts.count(portId); }); if (srcPortIdIt == route.sourcePortIds.end()) continue; const auto devicePortIt = findById(mPorts, *srcPortIdIt); const auto mixPortIt = findById(mPorts, route.sinkPortId); if (devicePortIt == mPorts.end() || mixPortIt == mPorts.end()) continue; auto devicePortConfig = getSingleConfigForDevicePort(*devicePortIt); auto mixPortConfig = getSingleConfigForMixPort(isInput, *mixPortIt); if (!mixPortConfig.has_value()) continue; return std::make_pair(devicePortConfig, mixPortConfig.value()); } } else { std::set connectedSinkDevicePorts = getConnectedSinkDevicePorts(); for (const auto& route : mRoutes) { if (connectedSinkDevicePorts.count(route.sinkPortId) == 0) continue; const auto mixPortIt = findById(mPorts, route.sourcePortIds[0]); const auto devicePortIt = findById(mPorts, route.sinkPortId); if (devicePortIt == mPorts.end() || mixPortIt == mPorts.end()) continue; auto mixPortConfig = getSingleConfigForMixPort(isInput, *mixPortIt); auto devicePortConfig = getSingleConfigForDevicePort(*devicePortIt); if (!mixPortConfig.has_value()) continue; return std::make_pair(mixPortConfig.value(), devicePortConfig); } } return {}; } std::vector ModuleConfig::getRoutableSrcSinkGroups(bool isInput) const { std::vector result; if (isInput) { std::set connectedSourceDevicePorts = getConnectedSourceDevicePorts(); for (const auto& route : mRoutes) { std::vector srcPortIds; std::copy_if(route.sourcePortIds.begin(), route.sourcePortIds.end(), std::back_inserter(srcPortIds), [&](const auto& portId) { return connectedSourceDevicePorts.count(portId); }); if (srcPortIds.empty()) continue; const auto mixPortIt = findById(mPorts, route.sinkPortId); if (mixPortIt == mPorts.end()) continue; auto mixPortConfig = getSingleConfigForMixPort(isInput, *mixPortIt); if (!mixPortConfig.has_value()) continue; std::vector pairs; for (const auto srcPortId : srcPortIds) { const auto devicePortIt = findById(mPorts, srcPortId); if (devicePortIt == mPorts.end()) continue; // Using all configs for every source would be too much. auto devicePortConfig = getSingleConfigForDevicePort(*devicePortIt); pairs.emplace_back(devicePortConfig, mixPortConfig.value()); } if (!pairs.empty()) { result.emplace_back(route, std::move(pairs)); } } } else { std::set connectedSinkDevicePorts = getConnectedSinkDevicePorts(); for (const auto& route : mRoutes) { if (connectedSinkDevicePorts.count(route.sinkPortId) == 0) continue; const auto devicePortIt = findById(mPorts, route.sinkPortId); if (devicePortIt == mPorts.end()) continue; auto devicePortConfig = getSingleConfigForDevicePort(*devicePortIt); std::vector pairs; for (const auto srcPortId : route.sourcePortIds) { const auto mixPortIt = findById(mPorts, srcPortId); if (mixPortIt == mPorts.end()) continue; // Using all configs for every source would be too much. auto mixPortConfig = getSingleConfigForMixPort(isInput, *mixPortIt); if (mixPortConfig.has_value()) { pairs.emplace_back(mixPortConfig.value(), devicePortConfig); } } if (!pairs.empty()) { result.emplace_back(route, std::move(pairs)); } } } return result; } std::string ModuleConfig::toString() const { std::string result; result.append("Ports: "); result.append(android::internal::ToString(mPorts)); result.append("\nInitial configs: "); result.append(android::internal::ToString(mInitialConfigs)); result.append("\nAttached sink device ports: "); result.append(android::internal::ToString(mAttachedSinkDevicePorts)); result.append("\nAttached source device ports: "); result.append(android::internal::ToString(mAttachedSourceDevicePorts)); result.append("\nExternal device ports: "); result.append(android::internal::ToString(mExternalDevicePorts)); result.append("\nConnected external device ports: "); result.append(android::internal::ToString(getConnectedExternalDevicePorts())); result.append("\nRoutes: "); result.append(android::internal::ToString(mRoutes)); return result; } static size_t combineAudioConfigs(const AudioPort& port, const AudioProfile& profile, std::vector* result) { const size_t newConfigCount = profile.channelMasks.size() * profile.sampleRates.size(); result->reserve(result->capacity() + newConfigCount); for (auto channelMask : profile.channelMasks) { for (auto sampleRate : profile.sampleRates) { AudioPortConfig config{}; config.portId = port.id; Int sr; sr.value = sampleRate; config.sampleRate = sr; config.channelMask = channelMask; config.format = profile.format; config.flags = port.flags; config.ext = port.ext; result->push_back(std::move(config)); } } return newConfigCount; } static bool isDynamicProfile(const AudioProfile& profile) { return (profile.format.type == AudioFormatType::DEFAULT && profile.format.encoding.empty()) || profile.sampleRates.empty() || profile.channelMasks.empty(); } std::vector ModuleConfig::findMixPorts( bool isInput, bool connectedOnly, bool singlePort, const std::function& pred) const { std::vector result; const auto mixPorts = getMixPorts(isInput, connectedOnly); for (auto mixPortIt = mixPorts.begin(); mixPortIt != mixPorts.end();) { mixPortIt = std::find_if(mixPortIt, mixPorts.end(), pred); if (mixPortIt == mixPorts.end()) break; result.push_back(*mixPortIt++); if (singlePort) break; } return result; } std::set ModuleConfig::findRoutablePortIds(int32_t portId) const { std::set portIds; for (const auto& route : mRoutes) { if (portId == route.sinkPortId) { portIds.insert(route.sourcePortIds.begin(), route.sourcePortIds.end()); } else if (auto it = std::find(route.sourcePortIds.begin(), route.sourcePortIds.end(), portId); it != route.sourcePortIds.end()) { portIds.insert(route.sinkPortId); } } return portIds; } std::vector ModuleConfig::generateAudioMixPortConfigs( const std::vector& ports, bool isInput, bool singleProfile) const { std::vector result; for (const auto& mixPort : ports) { if (getConnectedDevicesPortsForMixPort(isInput, mixPort).empty()) { continue; } for (const auto& profile : mixPort.profiles) { if (isDynamicProfile(profile)) continue; combineAudioConfigs(mixPort, profile, &result); if (singleProfile && !result.empty()) { result.resize(1); return result; } } } return result; } std::vector ModuleConfig::generateAudioDevicePortConfigs( const std::vector& ports, bool singleProfile) const { std::vector result; for (const auto& devicePort : ports) { const size_t resultSizeBefore = result.size(); for (const auto& profile : devicePort.profiles) { combineAudioConfigs(devicePort, profile, &result); if (singleProfile && !result.empty()) { result.resize(1); return result; } } if (resultSizeBefore == result.size()) { std::copy_if(mInitialConfigs.begin(), mInitialConfigs.end(), std::back_inserter(result), [&](const auto& config) { return config.portId == devicePort.id; }); if (resultSizeBefore == result.size()) { AudioPortConfig empty; empty.portId = devicePort.id; empty.ext = devicePort.ext; result.push_back(empty); } } if (singleProfile) return result; } return result; } std::optional ModuleConfig::getPort(int32_t portId) { auto portsIt = findById(mPorts, portId); return portsIt != mPorts.end() ? std::optional(*portsIt) : std::nullopt; } ndk::ScopedAStatus ModuleConfig::onExternalDeviceConnected(IModule* module, const AudioPort& port) { RETURN_STATUS_IF_ERROR(module->getAudioPorts(&mPorts)); RETURN_STATUS_IF_ERROR(module->getAudioRoutes(&mRoutes)); // Validate port is present in module if (std::find(mPorts.begin(), mPorts.end(), port) == mPorts.end()) { return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); } if (port.flags.getTag() == aidl::android::media::audio::common::AudioIoFlags::Tag::input) { mConnectedExternalSourceDevicePorts.insert(port.id); } else { mConnectedExternalSinkDevicePorts.insert(port.id); } return ndk::ScopedAStatus::ok(); } ndk::ScopedAStatus ModuleConfig::onExternalDeviceDisconnected(IModule* module, const AudioPort& port) { RETURN_STATUS_IF_ERROR(module->getAudioPorts(&mPorts)); RETURN_STATUS_IF_ERROR(module->getAudioRoutes(&mRoutes)); if (port.flags.getTag() == aidl::android::media::audio::common::AudioIoFlags::Tag::input) { mConnectedExternalSourceDevicePorts.erase(port.id); } else { mConnectedExternalSinkDevicePorts.erase(port.id); } return ndk::ScopedAStatus::ok(); } bool ModuleConfig::isMmapSupported() const { const std::vector mmapOutMixPorts = getMmapOutMixPorts(false /*connectedOnly*/, false /*singlePort*/); const std::vector mmapInMixPorts = getMmapInMixPorts(false /*connectedOnly*/, false /*singlePort*/); return !mmapOutMixPorts.empty() || !mmapInMixPorts.empty(); }