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

Commit b52e93f5 authored by Mikhail Naganov's avatar Mikhail Naganov
Browse files

audio: Extend HidlUtils for the default wrapper needs

Add conversions used by the default wrapper.

Promote some conversions to pre-V7 interface to reduce
version-based forking in the default wrapper code.

Bug: 142480271
Test: atest android.hardware.audio.common@7.0-util_tests
Change-Id: I93c482eeaf08442271be2656693be5395ca53762
Merged-In: I93c482eeaf08442271be2656693be5395ca53762
parent 74e043ba
Loading
Loading
Loading
Loading
+8 −0
Original line number Diff line number Diff line
@@ -225,6 +225,10 @@ static inline bool isUnknownAudioChannelMask(const std::string& mask) {
    return stringToAudioChannelMask(mask) == AudioChannelMask::UNKNOWN;
}

static inline bool isUnknownAudioContentType(const std::string& contentType) {
    return stringToAudioContentType(contentType) == AudioContentType::UNKNOWN;
}

static inline bool isUnknownAudioDevice(const std::string& device) {
    return stringToAudioDevice(device) == AudioDevice::UNKNOWN && !isVendorExtension(device);
}
@@ -237,6 +241,10 @@ static inline bool isUnknownAudioGainMode(const std::string& mode) {
    return stringToAudioGainMode(mode) == AudioGainMode::UNKNOWN;
}

static inline bool isUnknownAudioInOutFlag(const std::string& flag) {
    return stringToAudioInOutFlag(flag) == AudioInOutFlag::UNKNOWN;
}

static inline bool isUnknownAudioSource(const std::string& source) {
    return stringToAudioSource(source) == AudioSource::UNKNOWN;
}
+59 −0
Original line number Diff line number Diff line
@@ -95,6 +95,25 @@ status_t HidlUtils::audioChannelMaskFromHal(audio_channel_mask_t halChannelMask,
    return NO_ERROR;
}

status_t HidlUtils::audioChannelMasksFromHal(const std::vector<std::string>& halChannelMasks,
                                             hidl_vec<AudioChannelMask>* channelMasks) {
    hidl_vec<AudioChannelMask> tempChannelMasks;
    tempChannelMasks.resize(halChannelMasks.size());
    size_t tempPos = 0;
    for (const auto& halChannelMask : halChannelMasks) {
        if (!halChannelMask.empty() && !xsd::isUnknownAudioChannelMask(halChannelMask)) {
            tempChannelMasks[tempPos++] = halChannelMask;
        }
    }
    if (tempPos == tempChannelMasks.size()) {
        *channelMasks = std::move(tempChannelMasks);
    } else {
        *channelMasks = hidl_vec<AudioChannelMask>(tempChannelMasks.begin(),
                                                   tempChannelMasks.begin() + tempPos);
    }
    return halChannelMasks.size() == channelMasks->size() ? NO_ERROR : BAD_VALUE;
}

status_t HidlUtils::audioChannelMaskToHal(const AudioChannelMask& channelMask,
                                          audio_channel_mask_t* halChannelMask) {
    if (!xsd::isUnknownAudioChannelMask(channelMask) &&
@@ -127,6 +146,28 @@ status_t HidlUtils::audioConfigBaseToHal(const AudioConfigBase& configBase,
    return result;
}

status_t HidlUtils::audioContentTypeFromHal(const audio_content_type_t halContentType,
                                            AudioContentType* contentType) {
    *contentType = audio_content_type_to_string(halContentType);
    if (!contentType->empty() && !xsd::isUnknownAudioContentType(*contentType)) {
        return NO_ERROR;
    }
    ALOGE("Unknown audio content type value 0x%X", halContentType);
    *contentType = toString(xsd::AudioContentType::AUDIO_CONTENT_TYPE_UNKNOWN);
    return BAD_VALUE;
}

status_t HidlUtils::audioContentTypeToHal(const AudioContentType& contentType,
                                          audio_content_type_t* halContentType) {
    if (!xsd::isUnknownAudioContentType(contentType) &&
        audio_content_type_from_string(contentType.c_str(), halContentType)) {
        return NO_ERROR;
    }
    ALOGE("Unknown audio content type \"%s\"", contentType.c_str());
    *halContentType = AUDIO_CONTENT_TYPE_UNKNOWN;
    return BAD_VALUE;
}

status_t HidlUtils::audioDeviceTypeFromHal(audio_devices_t halDevice, AudioDevice* device) {
    *device = audio_device_to_string(halDevice);
    if (!device->empty() && !xsd::isUnknownAudioDevice(*device)) {
@@ -155,6 +196,24 @@ status_t HidlUtils::audioFormatFromHal(audio_format_t halFormat, AudioFormat* fo
    return BAD_VALUE;
}

status_t HidlUtils::audioFormatsFromHal(const std::vector<std::string>& halFormats,
                                        hidl_vec<AudioFormat>* formats) {
    hidl_vec<AudioFormat> tempFormats;
    tempFormats.resize(halFormats.size());
    size_t tempPos = 0;
    for (const auto& halFormat : halFormats) {
        if (!halFormat.empty() && !xsd::isUnknownAudioFormat(halFormat)) {
            tempFormats[tempPos++] = halFormat;
        }
    }
    if (tempPos == tempFormats.size()) {
        *formats = std::move(tempFormats);
    } else {
        *formats = hidl_vec<AudioFormat>(tempFormats.begin(), tempFormats.begin() + tempPos);
    }
    return halFormats.size() == formats->size() ? NO_ERROR : BAD_VALUE;
}

status_t HidlUtils::audioFormatToHal(const AudioFormat& format, audio_format_t* halFormat) {
    if (!xsd::isUnknownAudioFormat(format) && audio_format_from_string(format.c_str(), halFormat)) {
        return NO_ERROR;
+2 −0
Original line number Diff line number Diff line
@@ -43,6 +43,7 @@ filegroup {
    name: "android.hardware.audio.common-util@2-6",
    srcs: [
        "HidlUtils.cpp",
        "HidlUtilsCommon.cpp",
        "UuidUtils.cpp",
    ],
}
@@ -132,6 +133,7 @@ cc_library {
    defaults: ["android.hardware.audio.common-util_default"],
    srcs: [
        "7.0/HidlUtils.cpp",
        "HidlUtilsCommon.cpp",
        "UuidUtils.cpp",
    ],
    shared_libs: [
+20 −6
Original line number Diff line number Diff line
@@ -28,7 +28,7 @@ namespace common {
namespace CPP_VERSION {
namespace implementation {

status_t HidlUtils::audioConfigFromHal(const audio_config_t& halConfig, AudioConfig* config) {
status_t HidlUtils::audioConfigFromHal(const audio_config_t& halConfig, bool, AudioConfig* config) {
    config->sampleRateHz = halConfig.sample_rate;
    config->channelMask = EnumBitfield<AudioChannelMask>(halConfig.channel_mask);
    config->format = AudioFormat(halConfig.format);
@@ -47,7 +47,7 @@ status_t HidlUtils::audioConfigToHal(const AudioConfig& config, audio_config_t*
    return NO_ERROR;
}

void HidlUtils::audioGainConfigFromHal(const struct audio_gain_config& halConfig,
status_t HidlUtils::audioGainConfigFromHal(const struct audio_gain_config& halConfig, bool,
                                           AudioGainConfig* config) {
    config->index = halConfig.index;
    config->mode = EnumBitfield<AudioGainMode>(halConfig.mode);
@@ -56,6 +56,7 @@ void HidlUtils::audioGainConfigFromHal(const struct audio_gain_config& halConfig
        config->values[i] = halConfig.values[i];
    }
    config->rampDurationMs = halConfig.ramp_duration_ms;
    return NO_ERROR;
}

status_t HidlUtils::audioGainConfigToHal(const AudioGainConfig& config,
@@ -71,7 +72,7 @@ status_t HidlUtils::audioGainConfigToHal(const AudioGainConfig& config,
    return NO_ERROR;
}

void HidlUtils::audioGainFromHal(const struct audio_gain& halGain, AudioGain* gain) {
status_t HidlUtils::audioGainFromHal(const struct audio_gain& halGain, bool, AudioGain* gain) {
    gain->mode = EnumBitfield<AudioGainMode>(halGain.mode);
    gain->channelMask = EnumBitfield<AudioChannelMask>(halGain.channel_mask);
    gain->minValue = halGain.min_value;
@@ -80,6 +81,7 @@ void HidlUtils::audioGainFromHal(const struct audio_gain& halGain, AudioGain* ga
    gain->stepValue = halGain.step_value;
    gain->minRampMs = halGain.min_ramp_ms;
    gain->maxRampMs = halGain.max_ramp_ms;
    return NO_ERROR;
}

status_t HidlUtils::audioGainToHal(const AudioGain& gain, struct audio_gain* halGain) {
@@ -182,7 +184,7 @@ status_t HidlUtils::audioPortConfigFromHal(const struct audio_port_config& halCo
    config->sampleRateHz = halConfig.sample_rate;
    config->channelMask = EnumBitfield<AudioChannelMask>(halConfig.channel_mask);
    config->format = AudioFormat(halConfig.format);
    audioGainConfigFromHal(halConfig.gain, &config->gain);
    audioGainConfigFromHal(halConfig.gain, false /*isInput--ignored*/, &config->gain);
    switch (halConfig.type) {
        case AUDIO_PORT_TYPE_NONE:
            break;
@@ -272,7 +274,7 @@ status_t HidlUtils::audioPortFromHal(const struct audio_port& halPort, AudioPort
    }
    port->gains.resize(halPort.num_gains);
    for (size_t i = 0; i < halPort.num_gains; ++i) {
        audioGainFromHal(halPort.gains[i], &port->gains[i]);
        audioGainFromHal(halPort.gains[i], false /*isInput--ignored*/, &port->gains[i]);
    }
    audioPortConfigFromHal(halPort.active_config, &port->activeConfig);
    switch (halPort.type) {
@@ -351,6 +353,18 @@ status_t HidlUtils::audioPortToHal(const AudioPort& port, struct audio_port* hal
    return NO_ERROR;
}

#if MAJOR_VERSION >= 5
status_t HidlUtils::deviceAddressToHal(const DeviceAddress& device, audio_devices_t* halDeviceType,
                                       char* halDeviceAddress) {
    return deviceAddressToHalImpl(device, halDeviceType, halDeviceAddress);
}

status_t HidlUtils::deviceAddressFromHal(audio_devices_t halDeviceType,
                                         const char* halDeviceAddress, DeviceAddress* device) {
    return deviceAddressFromHalImpl(halDeviceType, halDeviceAddress, device);
}
#endif

}  // namespace implementation
}  // namespace CPP_VERSION
}  // namespace common
+138 −45
Original line number Diff line number Diff line
@@ -23,8 +23,6 @@

#include <system/audio.h>

using ::android::hardware::hidl_vec;

namespace android {
namespace hardware {
namespace audio {
@@ -32,25 +30,25 @@ namespace common {
namespace CPP_VERSION {
namespace implementation {

using ::android::hardware::hidl_vec;
using namespace ::android::hardware::audio::common::CPP_VERSION;

struct HidlUtils {
#if MAJOR_VERSION < 7
    static status_t audioConfigFromHal(const audio_config_t& halConfig, AudioConfig* config);
    static void audioGainConfigFromHal(const struct audio_gain_config& halConfig,
                                       AudioGainConfig* config);
    static void audioGainFromHal(const struct audio_gain& halGain, AudioGain* gain);
#else
    static status_t audioConfigFromHal(const audio_config_t& halConfig, bool isInput,
                                       AudioConfig* config);
    static status_t audioConfigToHal(const AudioConfig& config, audio_config_t* halConfig);
#if MAJOR_VERSION >= 4
    static status_t audioContentTypeFromHal(const audio_content_type_t halContentType,
                                            AudioContentType* contentType);
    static status_t audioContentTypeToHal(const AudioContentType& contentType,
                                          audio_content_type_t* halContentType);
#endif
    static status_t audioGainConfigFromHal(const struct audio_gain_config& halConfig, bool isInput,
                                           AudioGainConfig* config);
    static status_t audioGainFromHal(const struct audio_gain& halGain, bool isInput,
                                     AudioGain* gain);
#endif
    static status_t audioConfigToHal(const AudioConfig& config, audio_config_t* halConfig);
    static status_t audioGainConfigToHal(const AudioGainConfig& config,
                                         struct audio_gain_config* halConfig);
    static status_t audioGainFromHal(const struct audio_gain& halGain, bool isInput,
                                     AudioGain* gain);
    static status_t audioGainToHal(const AudioGain& gain, struct audio_gain* halGain);
    static status_t audioUsageFromHal(audio_usage_t halUsage, AudioUsage* usage);
    static status_t audioUsageToHal(const AudioUsage& usage, audio_usage_t* halUsage);
@@ -64,43 +62,37 @@ struct HidlUtils {
                                         struct audio_port_config* halConfig);
    static status_t audioPortConfigsFromHal(unsigned int numHalConfigs,
                                            const struct audio_port_config* halConfigs,
                                            hidl_vec<AudioPortConfig>* configs) {
        status_t result = NO_ERROR;
        configs->resize(numHalConfigs);
        for (unsigned int i = 0; i < numHalConfigs; ++i) {
            if (status_t status = audioPortConfigFromHal(halConfigs[i], &(*configs)[i]);
                status != NO_ERROR) {
                result = status;
            }
        }
        return result;
    }
                                            hidl_vec<AudioPortConfig>* configs);
    static status_t audioPortConfigsToHal(const hidl_vec<AudioPortConfig>& configs,
                                          std::unique_ptr<audio_port_config[]>* halConfigs) {
        status_t result = NO_ERROR;
        halConfigs->reset(new audio_port_config[configs.size()]);
        for (size_t i = 0; i < configs.size(); ++i) {
            if (status_t status = audioPortConfigToHal(configs[i], &(*halConfigs)[i]);
                status != NO_ERROR) {
                result = status;
            }
        }
        return result;
    }
                                          std::unique_ptr<audio_port_config[]>* halConfigs);
    static status_t audioPortFromHal(const struct audio_port& halPort, AudioPort* port);
    static status_t audioPortToHal(const AudioPort& port, struct audio_port* halPort);
    static status_t audioSourceFromHal(audio_source_t halSource, AudioSource* source);
    static status_t audioSourceToHal(const AudioSource& source, audio_source_t* halSource);
#if MAJOR_VERSION >= 5
    static status_t deviceAddressToHal(const DeviceAddress& device, audio_devices_t* halDeviceType,
                                       char* halDeviceAddress);
    static status_t deviceAddressFromHal(audio_devices_t halDeviceType,
                                         const char* halDeviceAddress, DeviceAddress* device);
#endif

    // PLEASE DO NOT USE, will be removed in a couple of days
#if MAJOR_VERSION <= 6
    // Temporary versions for compatibility with forks of the default implementation.
    // Will be removed, do not use!
    static status_t audioConfigFromHal(const audio_config_t& halConfig, AudioConfig* config) {
        return audioConfigFromHal(halConfig, false /*isInput--ignored*/, config);
    }
    static std::unique_ptr<audio_port_config[]> audioPortConfigsToHal(
            const hidl_vec<AudioPortConfig>& configs) {
        std::unique_ptr<audio_port_config[]> halConfigs;
        (void)audioPortConfigsToHal(configs, &halConfigs);
        return halConfigs;
    }

    static status_t audioPortFromHal(const struct audio_port& halPort, AudioPort* port);
    static status_t audioPortToHal(const AudioPort& port, struct audio_port* halPort);
#if MAJOR_VERSION >= 7
#else  // V7 and above
    static status_t audioChannelMaskFromHal(audio_channel_mask_t halChannelMask, bool isInput,
                                            AudioChannelMask* channelMask);
    static status_t audioChannelMasksFromHal(const std::vector<std::string>& halChannelMasks,
                                             hidl_vec<AudioChannelMask>* channelMasks);
    static status_t audioChannelMaskToHal(const AudioChannelMask& channelMask,
                                          audio_channel_mask_t* halChannelMask);
    static status_t audioConfigBaseFromHal(const audio_config_base_t& halConfigBase, bool isInput,
@@ -110,6 +102,8 @@ struct HidlUtils {
    static status_t audioDeviceTypeFromHal(audio_devices_t halDevice, AudioDevice* device);
    static status_t audioDeviceTypeToHal(const AudioDevice& device, audio_devices_t* halDevice);
    static status_t audioFormatFromHal(audio_format_t halFormat, AudioFormat* format);
    static status_t audioFormatsFromHal(const std::vector<std::string>& halFormats,
                                        hidl_vec<AudioFormat>* formats);
    static status_t audioFormatToHal(const AudioFormat& format, audio_format_t* halFormat);
    static status_t audioGainModeMaskFromHal(audio_gain_mode_t halGainModeMask,
                                             hidl_vec<AudioGainMode>* gainModeMask);
@@ -121,16 +115,10 @@ struct HidlUtils {
                                        AudioProfile* profile);
    static status_t audioProfileToHal(const AudioProfile& profile,
                                      struct audio_profile* halProfile);
    static status_t audioSourceFromHal(audio_source_t halSource, AudioSource* source);
    static status_t audioSourceToHal(const AudioSource& source, audio_source_t* halSource);
    static status_t audioStreamTypeFromHal(audio_stream_type_t halStreamType,
                                           AudioStreamType* streamType);
    static status_t audioStreamTypeToHal(const AudioStreamType& streamType,
                                         audio_stream_type_t* halStreamType);
    static status_t deviceAddressToHal(const DeviceAddress& device, audio_devices_t* halDeviceType,
                                       char* halDeviceAddress);
    static status_t deviceAddressFromHal(audio_devices_t halDeviceType,
                                         const char* halDeviceAddress, DeviceAddress* device);

  private:
    static status_t audioIndexChannelMaskFromHal(audio_channel_mask_t halChannelMask,
@@ -151,8 +139,113 @@ struct HidlUtils {
                                               struct audio_port_config_mix_ext* mix,
                                               struct audio_port_config_session_ext* session);
#endif

    // V4 and below have DeviceAddress defined in the 'core' interface.
    // To avoid duplicating code, the implementations of deviceAddressTo/FromHal
    // are defined as templates. These templates can be only used directly by V4
    // and below.
#if MAJOR_VERSION >= 5
  private:
#endif
    template <typename DA>
    static status_t deviceAddressToHalImpl(const DA& device, audio_devices_t* halDeviceType,
                                           char* halDeviceAddress);
    template <typename DA>
    static status_t deviceAddressFromHalImpl(audio_devices_t halDeviceType,
                                             const char* halDeviceAddress, DA* device);
};

#if MAJOR_VERSION <= 6
#if MAJOR_VERSION >= 4
inline status_t HidlUtils::audioContentTypeFromHal(const audio_content_type_t halContentType,
                                                   AudioContentType* contentType) {
    *contentType = AudioContentType(halContentType);
    return NO_ERROR;
}

inline status_t HidlUtils::audioContentTypeToHal(const AudioContentType& contentType,
                                                 audio_content_type_t* halContentType) {
    *halContentType = static_cast<audio_content_type_t>(contentType);
    return NO_ERROR;
}
#endif

inline status_t HidlUtils::audioSourceFromHal(audio_source_t halSource, AudioSource* source) {
    *source = AudioSource(halSource);
    return NO_ERROR;
}

inline status_t HidlUtils::audioSourceToHal(const AudioSource& source, audio_source_t* halSource) {
    *halSource = static_cast<audio_source_t>(source);
    return NO_ERROR;
}

template <typename DA>
status_t HidlUtils::deviceAddressToHalImpl(const DA& device, audio_devices_t* halDeviceType,
                                           char* halDeviceAddress) {
    *halDeviceType = static_cast<audio_devices_t>(device.device);
    memset(halDeviceAddress, 0, AUDIO_DEVICE_MAX_ADDRESS_LEN);
    if (audio_is_a2dp_out_device(*halDeviceType) || audio_is_a2dp_in_device(*halDeviceType)) {
        snprintf(halDeviceAddress, AUDIO_DEVICE_MAX_ADDRESS_LEN, "%02X:%02X:%02X:%02X:%02X:%02X",
                 device.address.mac[0], device.address.mac[1], device.address.mac[2],
                 device.address.mac[3], device.address.mac[4], device.address.mac[5]);
    } else if (*halDeviceType == AUDIO_DEVICE_OUT_IP || *halDeviceType == AUDIO_DEVICE_IN_IP) {
        snprintf(halDeviceAddress, AUDIO_DEVICE_MAX_ADDRESS_LEN, "%d.%d.%d.%d",
                 device.address.ipv4[0], device.address.ipv4[1], device.address.ipv4[2],
                 device.address.ipv4[3]);
    } else if (audio_is_usb_out_device(*halDeviceType) || audio_is_usb_in_device(*halDeviceType)) {
        snprintf(halDeviceAddress, AUDIO_DEVICE_MAX_ADDRESS_LEN, "card=%d;device=%d",
                 device.address.alsa.card, device.address.alsa.device);
    } else if (*halDeviceType == AUDIO_DEVICE_OUT_BUS || *halDeviceType == AUDIO_DEVICE_IN_BUS) {
        snprintf(halDeviceAddress, AUDIO_DEVICE_MAX_ADDRESS_LEN, "%s", device.busAddress.c_str());
    } else if (*halDeviceType == AUDIO_DEVICE_OUT_REMOTE_SUBMIX ||
               *halDeviceType == AUDIO_DEVICE_IN_REMOTE_SUBMIX) {
        snprintf(halDeviceAddress, AUDIO_DEVICE_MAX_ADDRESS_LEN, "%s",
                 device.rSubmixAddress.c_str());
    }
    return NO_ERROR;
}

template <typename DA>
status_t HidlUtils::deviceAddressFromHalImpl(audio_devices_t halDeviceType,
                                             const char* halDeviceAddress, DA* device) {
    if (device == nullptr) {
        return BAD_VALUE;
    }
    device->device = AudioDevice(halDeviceType);
    if (halDeviceAddress == nullptr ||
        strnlen(halDeviceAddress, AUDIO_DEVICE_MAX_ADDRESS_LEN) == 0) {
        return NO_ERROR;
    }

    if (audio_is_a2dp_out_device(halDeviceType) || audio_is_a2dp_in_device(halDeviceType)) {
        int status =
                sscanf(halDeviceAddress, "%hhX:%hhX:%hhX:%hhX:%hhX:%hhX", &device->address.mac[0],
                       &device->address.mac[1], &device->address.mac[2], &device->address.mac[3],
                       &device->address.mac[4], &device->address.mac[5]);
        return status == 6 ? OK : BAD_VALUE;
    } else if (halDeviceType == AUDIO_DEVICE_OUT_IP || halDeviceType == AUDIO_DEVICE_IN_IP) {
        int status = sscanf(halDeviceAddress, "%hhu.%hhu.%hhu.%hhu", &device->address.ipv4[0],
                            &device->address.ipv4[1], &device->address.ipv4[2],
                            &device->address.ipv4[3]);
        return status == 4 ? OK : BAD_VALUE;
    } else if (audio_is_usb_out_device(halDeviceType) || audio_is_usb_in_device(halDeviceType)) {
        int status = sscanf(halDeviceAddress, "card=%d;device=%d", &device->address.alsa.card,
                            &device->address.alsa.device);
        return status == 2 ? OK : BAD_VALUE;
    } else if (halDeviceType == AUDIO_DEVICE_OUT_BUS || halDeviceType == AUDIO_DEVICE_IN_BUS) {
        device->busAddress = halDeviceAddress;
        return OK;
    } else if (halDeviceType == AUDIO_DEVICE_OUT_REMOTE_SUBMIX ||
               halDeviceType == AUDIO_DEVICE_IN_REMOTE_SUBMIX) {
        device->rSubmixAddress = halDeviceAddress;
        return OK;
    }
    device->busAddress = halDeviceAddress;
    return NO_ERROR;
}
#endif  // MAJOR_VERSION <= 6

}  // namespace implementation
}  // namespace CPP_VERSION
}  // namespace common
Loading