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

Commit 643783e4 authored by Ytai Ben-Tsvi's avatar Ytai Ben-Tsvi
Browse files

Break-up libaudioclient_aidl_conversion

To reduce transitive dependencies, we break out the generic parts of
libaudioclient_aidl_conversion to libaudioclient_aidl_conversion_util,
so that libaudiofoundtaion_headers could have a reduced number of
transitive dependencies (it is used in different partitions, so these
dependencies become a problem).

Test: m checkbuild
Change-Id: I1c9f385698dbc32405cc4878cd74d13e42ebb103
parent 0ec99e34
Loading
Loading
Loading
Loading
+6 −2
Original line number Original line Diff line number Diff line
@@ -34,11 +34,15 @@ cc_library_headers {
    export_include_dirs: ["include"],
    export_include_dirs: ["include"],
    static_libs: [
    static_libs: [
        "av-types-aidl-unstable-cpp",
        "av-types-aidl-unstable-cpp",
        "libaudioclient_aidl_conversion",
    ],
    ],
    export_static_lib_headers: [
    export_static_lib_headers: [
        "av-types-aidl-unstable-cpp",
        "av-types-aidl-unstable-cpp",
        "libaudioclient_aidl_conversion",
    ],
    header_libs: [
        "libaudioclient_aidl_conversion_util",
    ],
    export_header_lib_headers: [
        "libaudioclient_aidl_conversion_util",
    ],
    ],
    host_supported: true,
    host_supported: true,
    vendor_available: true,
    vendor_available: true,
+1 −1
Original line number Original line Diff line number Diff line
@@ -20,7 +20,7 @@
#include <android/media/MicrophoneInfoData.h>
#include <android/media/MicrophoneInfoData.h>
#include <binder/Parcel.h>
#include <binder/Parcel.h>
#include <binder/Parcelable.h>
#include <binder/Parcelable.h>
#include <media/AidlConversion.h>
#include <media/AidlConversionUtil.h>
#include <system/audio.h>
#include <system/audio.h>


namespace android {
namespace android {
+29 −6
Original line number Original line Diff line number Diff line
@@ -153,6 +153,31 @@ cc_library_shared {
    },
    },
}
}


// This is intended for clients needing to include AidlConversionUtil.h, without dragging in a lot of extra
// dependencies.
cc_library_headers {
    name: "libaudioclient_aidl_conversion_util",
    host_supported: true,
    vendor_available: true,
    double_loadable: true,
    min_sdk_version: "29",
    export_include_dirs: [
        "include",
    ],
    header_libs: [
        "libbase_headers",
    ],
    export_header_lib_headers: [
        "libbase_headers",
    ],
    apex_available: [
        "//apex_available:platform",
        "com.android.bluetooth.updatable",
        "com.android.media",
        "com.android.media.swcodec",
    ],
}

cc_library {
cc_library {
    name: "libaudioclient_aidl_conversion",
    name: "libaudioclient_aidl_conversion",
    srcs: ["AidlConversion.cpp"],
    srcs: ["AidlConversion.cpp"],
@@ -162,8 +187,12 @@ cc_library {
    double_loadable: true,
    double_loadable: true,
    min_sdk_version: "29",
    min_sdk_version: "29",
    header_libs: [
    header_libs: [
        "libaudioclient_aidl_conversion_util",
        "libaudio_system_headers",
        "libaudio_system_headers",
    ],
    ],
    export_header_lib_headers: [
        "libaudioclient_aidl_conversion_util",
    ],
    shared_libs: [
    shared_libs: [
        "audioclient-types-aidl-unstable-cpp",
        "audioclient-types-aidl-unstable-cpp",
        "libbase",
        "libbase",
@@ -189,12 +218,6 @@ cc_library {
            "signed-integer-overflow",
            "signed-integer-overflow",
        ],
        ],
    },
    },
    apex_available: [
        "//apex_available:platform",
        "com.android.bluetooth.updatable",
        "com.android.media",
        "com.android.media.swcodec",
    ],
}
}


// AIDL interface between libaudioclient and framework.jar
// AIDL interface between libaudioclient and framework.jar
+1 −73
Original line number Original line Diff line number Diff line
@@ -21,8 +21,6 @@


#include <system/audio.h>
#include <system/audio.h>


#include <android-base/expected.h>

#include <android/media/AudioAttributesInternal.h>
#include <android/media/AudioAttributesInternal.h>
#include <android/media/AudioClient.h>
#include <android/media/AudioClient.h>
#include <android/media/AudioConfig.h>
#include <android/media/AudioConfig.h>
@@ -41,6 +39,7 @@


#include <android/media/SharedFileRegion.h>
#include <android/media/SharedFileRegion.h>
#include <binder/IMemory.h>
#include <binder/IMemory.h>
#include <media/AidlConversionUtil.h>
#include <media/AudioClient.h>
#include <media/AudioClient.h>
#include <media/AudioIoDescriptor.h>
#include <media/AudioIoDescriptor.h>
#include <media/AudioTimestamp.h>
#include <media/AudioTimestamp.h>
@@ -48,77 +47,6 @@


namespace android {
namespace android {


template <typename T>
using ConversionResult = base::expected<T, status_t>;

// Convenience macros for working with ConversionResult, useful for writing converted for aggregate
// types.

#define VALUE_OR_RETURN(result)                                \
    ({                                                         \
        auto _tmp = (result);                                  \
        if (!_tmp.ok()) return base::unexpected(_tmp.error()); \
        std::move(_tmp.value());                               \
    })

#define RETURN_IF_ERROR(result) \
    if (status_t _tmp = (result); _tmp != OK) return base::unexpected(_tmp);

#define VALUE_OR_RETURN_STATUS(x)           \
    ({                                      \
       auto _tmp = (x);                     \
       if (!_tmp.ok()) return _tmp.error(); \
       std::move(_tmp.value());             \
     })

/**
 * A generic template to safely cast between integral types, respecting limits of the destination
 * type.
 */
template<typename To, typename From>
ConversionResult<To> convertIntegral(From from) {
    // Special handling is required for signed / vs. unsigned comparisons, since otherwise we may
    // have the signed converted to unsigned and produce wrong results.
    if (std::is_signed_v<From> && !std::is_signed_v<To>) {
        if (from < 0 || from > std::numeric_limits<To>::max()) {
            return base::unexpected(BAD_VALUE);
        }
    } else if (std::is_signed_v<To> && !std::is_signed_v<From>) {
        if (from > std::numeric_limits<To>::max()) {
            return base::unexpected(BAD_VALUE);
        }
    } else {
        if (from < std::numeric_limits<To>::min() || from > std::numeric_limits<To>::max()) {
            return base::unexpected(BAD_VALUE);
        }
    }
    return static_cast<To>(from);
}

/**
 * A generic template to safely cast between types, that are intended to be the same size, but
 * interpreted differently.
 */
template<typename To, typename From>
ConversionResult<To> convertReinterpret(From from) {
    static_assert(sizeof(From) == sizeof(To));
    return static_cast<To>(from);
}

/**
 * A generic template that helps convert containers of convertible types.
 */
template<typename OutputContainer, typename InputContainer, typename Func>
ConversionResult<OutputContainer>
convertContainer(const InputContainer& input, const Func& itemConversion) {
    OutputContainer output;
    auto ins = std::inserter(output, output.begin());
    for (const auto& item : input) {
        *ins = VALUE_OR_RETURN(itemConversion(item));
    }
    return output;
}

// maxSize is the size of the C-string buffer (including the 0-terminator), NOT the max length of
// maxSize is the size of the C-string buffer (including the 0-terminator), NOT the max length of
// the string.
// the string.
status_t aidl2legacy_string(std::string_view aidl, char* dest, size_t maxSize);
status_t aidl2legacy_string(std::string_view aidl, char* dest, size_t maxSize);
+98 −0
Original line number Original line Diff line number Diff line
/*
 * Copyright (C) 2020 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.
 */

#pragma once

#include <limits>
#include <type_traits>
#include <utility>

#include <android-base/expected.h>

namespace android {

template <typename T>
using ConversionResult = base::expected<T, status_t>;

// Convenience macros for working with ConversionResult, useful for writing converted for aggregate
// types.

#define VALUE_OR_RETURN(result)                                \
    ({                                                         \
        auto _tmp = (result);                                  \
        if (!_tmp.ok()) return base::unexpected(_tmp.error()); \
        std::move(_tmp.value());                               \
    })

#define RETURN_IF_ERROR(result) \
    if (status_t _tmp = (result); _tmp != OK) return base::unexpected(_tmp);

#define VALUE_OR_RETURN_STATUS(x)           \
    ({                                      \
       auto _tmp = (x);                     \
       if (!_tmp.ok()) return _tmp.error(); \
       std::move(_tmp.value());             \
     })

/**
 * A generic template to safely cast between integral types, respecting limits of the destination
 * type.
 */
template<typename To, typename From>
ConversionResult<To> convertIntegral(From from) {
    // Special handling is required for signed / vs. unsigned comparisons, since otherwise we may
    // have the signed converted to unsigned and produce wrong results.
    if (std::is_signed_v<From> && !std::is_signed_v<To>) {
        if (from < 0 || from > std::numeric_limits<To>::max()) {
            return base::unexpected(BAD_VALUE);
        }
    } else if (std::is_signed_v<To> && !std::is_signed_v<From>) {
        if (from > std::numeric_limits<To>::max()) {
            return base::unexpected(BAD_VALUE);
        }
    } else {
        if (from < std::numeric_limits<To>::min() || from > std::numeric_limits<To>::max()) {
            return base::unexpected(BAD_VALUE);
        }
    }
    return static_cast<To>(from);
}

/**
 * A generic template to safely cast between types, that are intended to be the same size, but
 * interpreted differently.
 */
template<typename To, typename From>
ConversionResult<To> convertReinterpret(From from) {
    static_assert(sizeof(From) == sizeof(To));
    return static_cast<To>(from);
}

/**
 * A generic template that helps convert containers of convertible types.
 */
template<typename OutputContainer, typename InputContainer, typename Func>
ConversionResult<OutputContainer>
convertContainer(const InputContainer& input, const Func& itemConversion) {
    OutputContainer output;
    auto ins = std::inserter(output, output.begin());
    for (const auto& item : input) {
        *ins = VALUE_OR_RETURN(itemConversion(item));
    }
    return output;
}

}  // namespace android
Loading