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

Commit aff2869b authored by jiabin's avatar jiabin
Browse files

Refactor for audio device type in conversion.

As audio device type can not be used as bit mask any more, refactoring
audio device type usages in conversion from/to HAL. Use a set of audio
device types instead of bit mask.

Bug: 135621476
Test: atest AudioPlaybackCapture
Test: play and capture audio with BT and usb headset
Change-Id: If4a5c5002d6d4ccb7aaf823111b371d417784b19
Merged-In: If4a5c5002d6d4ccb7aaf823111b371d417784b19
parent b8269fd0
Loading
Loading
Loading
Loading
+14 −0
Original line number Diff line number Diff line
@@ -42,6 +42,13 @@ const DeviceTypeSet& getAudioDeviceOutAllScoSet() {
    return audioDeviceOutAllScoSet;
}

const DeviceTypeSet& getAudioDeviceOutAllUsbSet() {
    static const DeviceTypeSet audioDeviceOutAllUsbSet = DeviceTypeSet(
            std::begin(AUDIO_DEVICE_OUT_ALL_USB_ARRAY),
            std::end(AUDIO_DEVICE_OUT_ALL_USB_ARRAY));
    return audioDeviceOutAllUsbSet;
}

const DeviceTypeSet& getAudioDeviceInAllSet() {
    static const DeviceTypeSet audioDeviceInAllSet = DeviceTypeSet(
            std::begin(AUDIO_DEVICE_IN_ALL_ARRAY),
@@ -49,6 +56,13 @@ const DeviceTypeSet& getAudioDeviceInAllSet() {
    return audioDeviceInAllSet;
}

const DeviceTypeSet& getAudioDeviceInAllUsbSet() {
    static const DeviceTypeSet audioDeviceInAllUsbSet = DeviceTypeSet(
            std::begin(AUDIO_DEVICE_IN_ALL_USB_ARRAY),
            std::end(AUDIO_DEVICE_IN_ALL_USB_ARRAY));
    return audioDeviceInAllUsbSet;
}

bool deviceTypesToString(const DeviceTypeSet &deviceTypes, std::string &str) {
    if (deviceTypes.empty()) {
        str = "Empty device types";
+2 −0
Original line number Diff line number Diff line
@@ -37,7 +37,9 @@ using FormatVector = std::vector<audio_format_t>;
const DeviceTypeSet& getAudioDeviceOutAllSet();
const DeviceTypeSet& getAudioDeviceOutAllA2dpSet();
const DeviceTypeSet& getAudioDeviceOutAllScoSet();
const DeviceTypeSet& getAudioDeviceOutAllUsbSet();
const DeviceTypeSet& getAudioDeviceInAllSet();
const DeviceTypeSet& getAudioDeviceInAllUsbSet();

template<typename T>
static std::vector<T> Intersection(const std::set<T>& a, const std::set<T>& b) {
+1 −0
Original line number Diff line number Diff line
@@ -27,6 +27,7 @@ cc_defaults {
        "android.hardware.audio.common-util",
        "android.hidl.allocator@1.0",
        "android.hidl.memory@1.0",
        "libaudiofoundation",
        "libaudiohal_deathhandler",
        "libaudioutils",
        "libbase",
+9 −12
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@
#include <string.h>

#define LOG_TAG "HalHidl"
#include <media/AudioContainers.h>
#include <media/AudioParameter.h>
#include <utils/Log.h>

@@ -109,26 +110,22 @@ static std::string deviceAddressToHal(const DeviceAddress& address) {
    char halAddress[AUDIO_DEVICE_MAX_ADDRESS_LEN];
    memset(halAddress, 0, sizeof(halAddress));
    audio_devices_t halDevice = static_cast<audio_devices_t>(address.device);
    const bool isInput = (halDevice & AUDIO_DEVICE_BIT_IN) != 0;
    if (isInput) halDevice &= ~AUDIO_DEVICE_BIT_IN;
    if ((!isInput && (halDevice & AUDIO_DEVICE_OUT_ALL_A2DP) != 0) ||
        (isInput && (halDevice & AUDIO_DEVICE_IN_BLUETOOTH_A2DP) != 0)) {
    if (getAudioDeviceOutAllA2dpSet().count(halDevice) > 0 ||
        halDevice == AUDIO_DEVICE_IN_BLUETOOTH_A2DP) {
        snprintf(halAddress, sizeof(halAddress), "%02X:%02X:%02X:%02X:%02X:%02X",
                 address.address.mac[0], address.address.mac[1], address.address.mac[2],
                 address.address.mac[3], address.address.mac[4], address.address.mac[5]);
    } else if ((!isInput && (halDevice & AUDIO_DEVICE_OUT_IP) != 0) ||
               (isInput && (halDevice & AUDIO_DEVICE_IN_IP) != 0)) {
    } else if (halDevice == AUDIO_DEVICE_OUT_IP || halDevice == AUDIO_DEVICE_IN_IP) {
        snprintf(halAddress, sizeof(halAddress), "%d.%d.%d.%d", address.address.ipv4[0],
                 address.address.ipv4[1], address.address.ipv4[2], address.address.ipv4[3]);
    } else if ((!isInput && (halDevice & AUDIO_DEVICE_OUT_ALL_USB) != 0) ||
               (isInput && (halDevice & AUDIO_DEVICE_IN_ALL_USB) != 0)) {
    } else if (getAudioDeviceOutAllUsbSet().count(halDevice) > 0 ||
               getAudioDeviceInAllUsbSet().count(halDevice) > 0) {
        snprintf(halAddress, sizeof(halAddress), "card=%d;device=%d", address.address.alsa.card,
                 address.address.alsa.device);
    } else if ((!isInput && (halDevice & AUDIO_DEVICE_OUT_BUS) != 0) ||
               (isInput && (halDevice & AUDIO_DEVICE_IN_BUS) != 0)) {
    } else if (halDevice == AUDIO_DEVICE_OUT_BUS || halDevice == AUDIO_DEVICE_IN_BUS) {
        snprintf(halAddress, sizeof(halAddress), "%s", address.busAddress.c_str());
    } else if ((!isInput && (halDevice & AUDIO_DEVICE_OUT_REMOTE_SUBMIX)) != 0 ||
               (isInput && (halDevice & AUDIO_DEVICE_IN_REMOTE_SUBMIX) != 0)) {
    } else if (halDevice == AUDIO_DEVICE_OUT_REMOTE_SUBMIX ||
               halDevice == AUDIO_DEVICE_IN_REMOTE_SUBMIX) {
        snprintf(halAddress, sizeof(halAddress), "%s", address.rSubmixAddress.c_str());
    } else {
        snprintf(halAddress, sizeof(halAddress), "%s", address.busAddress.c_str());
+13 −22
Original line number Diff line number Diff line
@@ -22,6 +22,7 @@
#include PATH(android/hardware/audio/FILE_VERSION/IPrimaryDevice.h)
#include <cutils/native_handle.h>
#include <hwbinder/IPCThreadState.h>
#include <media/AudioContainers.h>
#include <utils/Log.h>

#include <common/all-versions/VersionUtils.h>
@@ -51,43 +52,33 @@ status_t deviceAddressFromHal(
    if (halAddress == nullptr || strnlen(halAddress, AUDIO_DEVICE_MAX_ADDRESS_LEN) == 0) {
        return OK;
    }
    const bool isInput = (device & AUDIO_DEVICE_BIT_IN) != 0;
    if (isInput) device &= ~AUDIO_DEVICE_BIT_IN;
    if ((!isInput && (device & AUDIO_DEVICE_OUT_ALL_A2DP) != 0)
            || (isInput && (device & AUDIO_DEVICE_IN_BLUETOOTH_A2DP) != 0)) {
    if (getAudioDeviceOutAllA2dpSet().count(device) > 0
            || device == AUDIO_DEVICE_IN_BLUETOOTH_A2DP) {
        int status = sscanf(halAddress,
                "%hhX:%hhX:%hhX:%hhX:%hhX:%hhX",
                &address->address.mac[0], &address->address.mac[1], &address->address.mac[2],
                &address->address.mac[3], &address->address.mac[4], &address->address.mac[5]);
        return status == 6 ? OK : BAD_VALUE;
    } else if ((!isInput && (device & AUDIO_DEVICE_OUT_IP) != 0)
            || (isInput && (device & AUDIO_DEVICE_IN_IP) != 0)) {
    } else if (device == AUDIO_DEVICE_OUT_IP || device == AUDIO_DEVICE_IN_IP) {
        int status = sscanf(halAddress,
                "%hhu.%hhu.%hhu.%hhu",
                &address->address.ipv4[0], &address->address.ipv4[1],
                &address->address.ipv4[2], &address->address.ipv4[3]);
        return status == 4 ? OK : BAD_VALUE;
    } else if ((!isInput && (device & AUDIO_DEVICE_OUT_ALL_USB)) != 0
            || (isInput && (device & AUDIO_DEVICE_IN_ALL_USB)) != 0) {
    } else if (getAudioDeviceOutAllUsbSet().count(device) > 0
            || getAudioDeviceInAllUsbSet().count(device) > 0) {
        int status = sscanf(halAddress,
                "card=%d;device=%d",
                &address->address.alsa.card, &address->address.alsa.device);
        return status == 2 ? OK : BAD_VALUE;
    } else if ((!isInput && (device & AUDIO_DEVICE_OUT_BUS) != 0)
            || (isInput && (device & AUDIO_DEVICE_IN_BUS) != 0)) {
        if (halAddress != NULL) {
    } else if (device == AUDIO_DEVICE_OUT_BUS || device == AUDIO_DEVICE_IN_BUS) {
        address->busAddress = halAddress;
        return OK;
        }
        return BAD_VALUE;
    } else if ((!isInput && (device & AUDIO_DEVICE_OUT_REMOTE_SUBMIX)) != 0
            || (isInput && (device & AUDIO_DEVICE_IN_REMOTE_SUBMIX) != 0)) {
        if (halAddress != NULL) {
    } else if (device == AUDIO_DEVICE_OUT_REMOTE_SUBMIX
            || device == AUDIO_DEVICE_IN_REMOTE_SUBMIX) {
        address->rSubmixAddress = halAddress;
        return OK;
    }
        return BAD_VALUE;
    }
    return OK;
}