Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit aa8f76af authored by Lorena Torres-Huerta's avatar Lorena Torres-Huerta Committed by Mikhail Naganov
Browse files

audio: Provide code for parsing surround sound config from XML

The main change is to convert the result of parsing from XSDC
types to AIDL, and add a VTS test for IConfig.getSurroundSoundConfig.

Extra changes:
  - add 'Unchecked' suffix to conversion functions that do not
    wrap the result into ConversionResult;
  - enable more compile-time checks for the default AIDL service,
    fix issues found.

Bug: 205884982
Test: atest VtsHalAudioCoreTargetTest
Change-Id: Icf578b8d28647e6355ed5328599c77d2ca1234f9
parent b511b8aa
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -102,7 +102,8 @@ constexpr size_t getFrameSizeInBytes(
constexpr bool isDefaultAudioFormat(
        const ::aidl::android::media::audio::common::AudioFormatDescription& desc) {
    return desc.type == ::aidl::android::media::audio::common::AudioFormatType::DEFAULT &&
           desc.pcm == ::aidl::android::media::audio::common::PcmType::DEFAULT;
           desc.pcm == ::aidl::android::media::audio::common::PcmType::DEFAULT &&
           desc.encoding.empty();
}

constexpr bool isTelephonyDeviceType(
+56 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 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 "AHAL_AidlXsdc"
#include <android-base/logging.h>
#include <error/expected_utils.h>
#include <media/AidlConversionCppNdk.h>
#include <media/TypeConverter.h>

#include "core-impl/AidlConversionXsdc.h"

using aidl::android::media::audio::common::AudioFormatDescription;

namespace xsd = android::audio::policy::configuration;

namespace aidl::android::hardware::audio::core::internal {

ConversionResult<AudioFormatDescription> xsdc2aidl_AudioFormatDescription(const std::string& xsdc) {
    return legacy2aidl_audio_format_t_AudioFormatDescription(::android::formatFromString(xsdc));
}

ConversionResult<SurroundSoundConfig::SurroundFormatFamily> xsdc2aidl_SurroundFormatFamily(
        const ::xsd::SurroundFormats::Format& xsdc) {
    SurroundSoundConfig::SurroundFormatFamily aidl;
    aidl.primaryFormat = VALUE_OR_RETURN(xsdc2aidl_AudioFormatDescription(xsdc.getName()));
    if (xsdc.hasSubformats()) {
        aidl.subFormats = VALUE_OR_RETURN(convertContainer<std::vector<AudioFormatDescription>>(
                xsdc.getSubformats(), xsdc2aidl_AudioFormatDescription));
    }
    return aidl;
}

ConversionResult<SurroundSoundConfig> xsdc2aidl_SurroundSoundConfig(
        const ::xsd::SurroundSound& xsdc) {
    SurroundSoundConfig aidl;
    if (!xsdc.hasFormats() || !xsdc.getFirstFormats()->hasFormat()) return aidl;
    aidl.formatFamilies = VALUE_OR_RETURN(
            convertContainer<std::vector<SurroundSoundConfig::SurroundFormatFamily>>(
                    xsdc.getFirstFormats()->getFormat(), xsdc2aidl_SurroundFormatFamily));
    return aidl;
}

}  // namespace aidl::android::hardware::audio::core::internal
+22 −0
Original line number Diff line number Diff line
@@ -65,6 +65,7 @@ cc_library {
    ],
    export_include_dirs: ["include"],
    srcs: [
        "AidlConversionXsdc.cpp",
        "AudioPolicyConfigXmlConverter.cpp",
        "Bluetooth.cpp",
        "Config.cpp",
@@ -92,11 +93,20 @@ cc_library {
        "audio_policy_configuration_aidl_default",
        "audio_policy_engine_configuration_aidl_default",
    ],
    shared_libs: [
        "libaudio_aidl_conversion_common_ndk",
        "libmedia_helper",
        "libstagefright_foundation",
    ],
    export_shared_lib_headers: [
        "libaudio_aidl_conversion_common_ndk",
    ],
    cflags: [
        "-Wall",
        "-Wextra",
        "-Werror",
        "-Wthread-safety",
        "-DBACKEND_NDK",
    ],
}

@@ -114,7 +124,19 @@ cc_binary {
    static_libs: [
        "libaudioserviceexampleimpl",
    ],
    shared_libs: [
        "libaudio_aidl_conversion_common_ndk",
        "libmedia_helper",
        "libstagefright_foundation",
    ],
    srcs: ["main.cpp"],
    cflags: [
        "-Wall",
        "-Wextra",
        "-Werror",
        "-Wthread-safety",
        "-DBACKEND_NDK",
    ],
}

cc_defaults {
+65 −2
Original line number Diff line number Diff line
@@ -21,11 +21,17 @@
#include <functional>
#include <unordered_map>

#define LOG_TAG "AHAL_ApmXmlConverter"
#include <android-base/logging.h>

#include <aidl/android/media/audio/common/AudioHalEngineConfig.h>
#include <media/stagefright/foundation/MediaDefs.h>
#include <system/audio-base-utils.h>

#include "core-impl/AidlConversionXsdc.h"
#include "core-impl/AudioPolicyConfigXmlConverter.h"

using aidl::android::media::audio::common::AudioFormatDescription;
using aidl::android::media::audio::common::AudioHalEngineConfig;
using aidl::android::media::audio::common::AudioHalVolumeCurve;
using aidl::android::media::audio::common::AudioHalVolumeGroup;
@@ -68,13 +74,13 @@ AudioHalVolumeCurve AudioPolicyConfigXmlConverter::convertVolumeCurveToAidl(
                    getXsdcConfig()->getVolumes());
        }
        aidlVolumeCurve.curvePoints =
                convertCollectionToAidl<std::string, AudioHalVolumeCurve::CurvePoint>(
                convertCollectionToAidlUnchecked<std::string, AudioHalVolumeCurve::CurvePoint>(
                        mVolumesReferenceMap.at(xsdcVolumeCurve.getRef()).getPoint(),
                        std::bind(&AudioPolicyConfigXmlConverter::convertCurvePointToAidl, this,
                                  std::placeholders::_1));
    } else {
        aidlVolumeCurve.curvePoints =
                convertCollectionToAidl<std::string, AudioHalVolumeCurve::CurvePoint>(
                convertCollectionToAidlUnchecked<std::string, AudioHalVolumeCurve::CurvePoint>(
                        xsdcVolumeCurve.getPoint(),
                        std::bind(&AudioPolicyConfigXmlConverter::convertCurvePointToAidl, this,
                                  std::placeholders::_1));
@@ -87,6 +93,22 @@ void AudioPolicyConfigXmlConverter::mapStreamToVolumeCurve(const xsd::Volume& xs
            convertVolumeCurveToAidl(xsdcVolumeCurve));
}

const SurroundSoundConfig& AudioPolicyConfigXmlConverter::getSurroundSoundConfig() {
    static const SurroundSoundConfig aidlSurroundSoundConfig = [this]() {
        if (auto xsdcConfig = getXsdcConfig(); xsdcConfig && xsdcConfig->hasSurroundSound()) {
            auto configConv = xsdc2aidl_SurroundSoundConfig(*xsdcConfig->getFirstSurroundSound());
            if (configConv.ok()) {
                return configConv.value();
            }
            LOG(ERROR) << "There was an error converting surround formats to AIDL: "
                       << configConv.error();
        }
        LOG(WARNING) << "Audio policy config does not have <surroundSound> section, using default";
        return getDefaultSurroundSoundConfig();
    }();
    return aidlSurroundSoundConfig;
}

const AudioHalEngineConfig& AudioPolicyConfigXmlConverter::getAidlEngineConfig() {
    if (mAidlEngineConfig.volumeGroups.empty() && getXsdcConfig() &&
        getXsdcConfig()->hasVolumes()) {
@@ -95,6 +117,47 @@ const AudioHalEngineConfig& AudioPolicyConfigXmlConverter::getAidlEngineConfig()
    return mAidlEngineConfig;
}

// static
const SurroundSoundConfig& AudioPolicyConfigXmlConverter::getDefaultSurroundSoundConfig() {
    // Provide a config similar to the one used by the framework by default
    // (see AudioPolicyConfig::setDefaultSurroundFormats).
#define ENCODED_FORMAT(format)        \
    AudioFormatDescription {          \
        .encoding = ::android::format \
    }
#define SIMPLE_FORMAT(format)                   \
    SurroundSoundConfig::SurroundFormatFamily { \
        .primaryFormat = ENCODED_FORMAT(format) \
    }

    static const SurroundSoundConfig defaultConfig = {
            .formatFamilies = {
                    SIMPLE_FORMAT(MEDIA_MIMETYPE_AUDIO_AC3),
                    SIMPLE_FORMAT(MEDIA_MIMETYPE_AUDIO_EAC3),
                    SIMPLE_FORMAT(MEDIA_MIMETYPE_AUDIO_DTS),
                    SIMPLE_FORMAT(MEDIA_MIMETYPE_AUDIO_DTS_HD),
                    SIMPLE_FORMAT(MEDIA_MIMETYPE_AUDIO_DTS_HD_MA),
                    SIMPLE_FORMAT(MEDIA_MIMETYPE_AUDIO_DTS_UHD),
                    SIMPLE_FORMAT(MEDIA_MIMETYPE_AUDIO_DTS_UHD_P2),
                    SIMPLE_FORMAT(MEDIA_MIMETYPE_AUDIO_DOLBY_TRUEHD),
                    SIMPLE_FORMAT(MEDIA_MIMETYPE_AUDIO_EAC3_JOC),
                    SurroundSoundConfig::SurroundFormatFamily{
                            .primaryFormat = ENCODED_FORMAT(MEDIA_MIMETYPE_AUDIO_AAC_LC),
                            .subFormats =
                                    {
                                            ENCODED_FORMAT(MEDIA_MIMETYPE_AUDIO_AAC_HE_V1),
                                            ENCODED_FORMAT(MEDIA_MIMETYPE_AUDIO_AAC_HE_V2),
                                            ENCODED_FORMAT(MEDIA_MIMETYPE_AUDIO_AAC_ELD),
                                            ENCODED_FORMAT(MEDIA_MIMETYPE_AUDIO_AAC_XHE),
                                    }},
                    SIMPLE_FORMAT(MEDIA_MIMETYPE_AUDIO_AC4),
            }};
#undef SIMPLE_FORMAT
#undef ENCODED_FORMAT

    return defaultConfig;
}

void AudioPolicyConfigXmlConverter::mapStreamsToVolumeCurves() {
    if (getXsdcConfig()->hasVolumes()) {
        for (const xsd::Volumes& xsdcWrapperType : getXsdcConfig()->getVolumes()) {
+10 −3
Original line number Diff line number Diff line
@@ -27,9 +27,16 @@ using aidl::android::media::audio::common::AudioHalEngineConfig;

namespace aidl::android::hardware::audio::core {
ndk::ScopedAStatus Config::getSurroundSoundConfig(SurroundSoundConfig* _aidl_return) {
    SurroundSoundConfig surroundSoundConfig;
    // TODO: parse from XML; for now, use empty config as default
    *_aidl_return = std::move(surroundSoundConfig);
    static const SurroundSoundConfig surroundSoundConfig = [this]() {
        SurroundSoundConfig surroundCfg;
        if (mAudioPolicyConverter.getStatus() == ::android::OK) {
            surroundCfg = mAudioPolicyConverter.getSurroundSoundConfig();
        } else {
            LOG(WARNING) << __func__ << mAudioPolicyConverter.getError();
        }
        return surroundCfg;
    }();
    *_aidl_return = surroundSoundConfig;
    LOG(DEBUG) << __func__ << ": returning " << _aidl_return->toString();
    return ndk::ScopedAStatus::ok();
}
Loading