From 96d2cd9d5c87d8bf9766a7190cf358adccab349a Mon Sep 17 00:00:00 2001 From: Kevin Rocard Date: Wed, 14 Nov 2018 16:22:07 -0800 Subject: Audio HAL: Factorize V2 and V4 like libaudiohal Use the same technique developed for the simpler libaudiohal to factorize the audio HAL impl. Generated with script: set -euo pipefail cd hardware/interfaces/audio # Make 2.0 and 4.0 files mostly identical by removing all 2.0, V4.0... # and replacing it by CPP_VERSION and FILE_VERSION # This allow both implementation to be mostly identical, except for code difference # Also remove IncludeGuard.h as it will be included from compiler flag # Also update license to 2018 find -name *.cpp -o -name *h | grep -v -e service.cpp | xargs sed -Ei \ -e 's/(#.*)V[42]_0_(.*_H_?)$/\1\2/' \ -e 's/(LOG_TAG.*)[24]_0/\1/' \ -e '/#ifndef AUDIO_HAL_VERSION/,/#endif/d' \ -e '/^#.*def.*AUDIO_HAL_VERSION\b/d' \ -e's/V[42]_0/CPP_VERSION/' \ -e 's/(#include ).(android.*)[24].0(.*).$/\1PATH(\2FILE_VERSION\3)/' \ -e 's|(#include) .(.*)[24].0/(default/.*).$|\1 "\2\3"|' \ -e 's/\bAUDIO_HAL_VERSION\b/CPP_VERSION/' \ -e '/IncludeGuard.h/d' \ -e 's/(Copyright .C. 201)./\18/' # set MAJOR_VERSION instead of having a different macro for each version # this allow to do preprocessor arithmetic (eg: MAJOR_VERSION >= 4) for v in 2 4; do find -ipath '*/'$v'.0/*/Android.bp' | xargs sed -i -e '/cflags:/,/^$/d' -e '/vndk/{:a;/}/!{N;ba};d}' -e '$i\ cflags: [\ "-DMAJOR_VERSION='$v'",\ "-DMINOR_VERSION=0",\ "-include common/all-versions/VersionMacro.h",\ ]'; done # replace# ifdef VERSION_X by #if MAJOR_VERSION == X find -path *all-versions/*.h | xargs sed -Ei -e 's/def.*AUDIO_HAL_VERSION_([24])_0.*/ MAJOR_VERSION == \1/' \ -e 'T;s/ +/ /' # copy all-versions impl in each version impl find -path '*/[24].0/*.[hc]*' | grep -ve all-versions -e test | xargs -P99 -n4 sed -i -Ee '/include <.*all-versions.default/!b' \ -e 's#.*<#find -path */#' -e 's/>$/|xargs tail -n +16/' -e e # remove all-versions impl rm -r {core,effect}/all-versions/ # merge version impl into a single all-version for dir in core/*.0/vts/functional/*.0/ ;do dest=$(echo $dir | sed 's#/..0/#/all-versions/#') mkdir -p $dest mv -T $dir $dest done find -mindepth 3 -path '*/2.0/*' -a \( -name '*cpp' -o -name '*h' \) | grep -v 'all-versions' | sed -E 'h;s/2/4/g;H;s/4.0/all-versions/;s/4.0//;H;g;s/\n/ /g;'| xargs -P99 -L1 sh -c ' set -euo pipefail mkdir -p $(dirname $2); diff --old-group-format="#if MAJOR_VERSION == 2 %<#endif " \ --new-group-format="#if MAJOR_VERSION == 4 %>#endif " $0 $1 > $2 || true; rm $0 $1' # merge the X.0 Android.bp & OWNERS in the all-versions for dir in common effect core; do for sub in default vts/functional; do test -f $dir/2.0/$sub/Android.bp || continue awk 1 $dir/*.0/$sub/Android.bp >> $dir/all-versions/$sub/Android.bp # delete licenses except for the first one and add whitelines sed -i -e '/^}$/{N;/^}\n$/!s/\n/&\n/}' $dir/all-versions/$sub/Android.bp sed -i -e '1,17b;/^\/\//,/^$/{d}' $dir/all-versions/$sub/Android.bp done for sub in default vts; do test -d $dir/2.0/$sub || continue test -f $dir/2.0/$sub/OWNERS && awk 1 $dir/*.0/$sub/OWNERS | sort -u > $dir/all-versions/$sub/OWNERS || true rm -r $dir/*.0/$sub done done # delete all-versions-impl dependencies find -name 'Android.bp' | xargs sed -i -e '/all-versions-impl/d' # cleanup unused files rm common/all-versions/default/include/common/all-versions/default/HidlUtils* rm common/all-versions/util/include/common/all-versions/IncludeGuard.h find -depth -type d -empty -delete # Clamp consecutive number of empty lines to 2 find -name *.cpp -o -name *h | xargs sed -Ei ':a;/^\n*$/{N;ba};s/\n\n+/\n\n/' # transform #endif\n#if to #elif find -name *.cpp -o -name *h | xargs sed -i '/^#endif/{N;s/.*\n#if/#elif/}' # remove leftover include guard in cpp find -name *.cpp |xargs sed -Ei '/^#.*_H_?$/d' # apply clang-format find -name *cpp -o -name *h | xargs ../../../prebuilts/clang/host/linux-x86/clang-stable/bin/clang-format --style file -i # clang format breaks PATH(a/b) to PATH(a / b), remove the space surrounding / find -name *cpp -o -name *h | xargs sed -i "/#include PATH/s# / #/#g" Test: compile Bug: 118203066 Change-Id: I3692a444307afc5f71064fe0b9e6b8af3c9ff1dd Signed-off-by: Kevin Rocard --- audio/core/all-versions/default/Conversions.cpp | 194 ++++++++++++++++++++++++ 1 file changed, 194 insertions(+) create mode 100644 audio/core/all-versions/default/Conversions.cpp (limited to 'audio/core/all-versions/default/Conversions.cpp') diff --git a/audio/core/all-versions/default/Conversions.cpp b/audio/core/all-versions/default/Conversions.cpp new file mode 100644 index 0000000000..7ce0c59d18 --- /dev/null +++ b/audio/core/all-versions/default/Conversions.cpp @@ -0,0 +1,194 @@ +/* + * Copyright (C) 2018 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 "core/default/Conversions.h" + +#include + +#include + +namespace android { +namespace hardware { +namespace audio { +namespace CPP_VERSION { +namespace implementation { + +using ::android::hardware::audio::common::CPP_VERSION::AudioDevice; + +std::string deviceAddressToHal(const DeviceAddress& address) { + // HAL assumes that the address is NUL-terminated. + char halAddress[AUDIO_DEVICE_MAX_ADDRESS_LEN]; + memset(halAddress, 0, sizeof(halAddress)); + uint32_t halDevice = static_cast(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)) { + 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)) { + 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)) { + 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)) { + 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)) { + snprintf(halAddress, sizeof(halAddress), "%s", address.rSubmixAddress.c_str()); + } + return halAddress; +} + +#if MAJOR_VERSION == 4 +status_t deviceAddressFromHal(audio_devices_t device, const char* halAddress, + DeviceAddress* address) { + if (address == nullptr) { + return BAD_VALUE; + } + address->device = AudioDevice(device); + 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)) { + 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)) { + 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) { + 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)) { + address->busAddress = halAddress; + return OK; + } else if ((!isInput && (device & AUDIO_DEVICE_OUT_REMOTE_SUBMIX)) != 0 || + (isInput && (device & AUDIO_DEVICE_IN_REMOTE_SUBMIX) != 0)) { + address->rSubmixAddress = halAddress; + return OK; + } + address->busAddress = halAddress; + return OK; +} + +AudioMicrophoneChannelMapping halToChannelMapping(audio_microphone_channel_mapping_t mapping) { + switch (mapping) { + case AUDIO_MICROPHONE_CHANNEL_MAPPING_UNUSED: + return AudioMicrophoneChannelMapping::UNUSED; + case AUDIO_MICROPHONE_CHANNEL_MAPPING_DIRECT: + return AudioMicrophoneChannelMapping::DIRECT; + case AUDIO_MICROPHONE_CHANNEL_MAPPING_PROCESSED: + return AudioMicrophoneChannelMapping::PROCESSED; + default: + ALOGE("Invalid channel mapping type: %d", mapping); + return AudioMicrophoneChannelMapping::UNUSED; + } +} + +AudioMicrophoneLocation halToLocation(audio_microphone_location_t location) { + switch (location) { + default: + case AUDIO_MICROPHONE_LOCATION_UNKNOWN: + return AudioMicrophoneLocation::UNKNOWN; + case AUDIO_MICROPHONE_LOCATION_MAINBODY: + return AudioMicrophoneLocation::MAINBODY; + case AUDIO_MICROPHONE_LOCATION_MAINBODY_MOVABLE: + return AudioMicrophoneLocation::MAINBODY_MOVABLE; + case AUDIO_MICROPHONE_LOCATION_PERIPHERAL: + return AudioMicrophoneLocation::PERIPHERAL; + } +} + +AudioMicrophoneDirectionality halToDirectionality(audio_microphone_directionality_t dir) { + switch (dir) { + default: + case AUDIO_MICROPHONE_DIRECTIONALITY_UNKNOWN: + return AudioMicrophoneDirectionality::UNKNOWN; + case AUDIO_MICROPHONE_DIRECTIONALITY_OMNI: + return AudioMicrophoneDirectionality::OMNI; + case AUDIO_MICROPHONE_DIRECTIONALITY_BI_DIRECTIONAL: + return AudioMicrophoneDirectionality::BI_DIRECTIONAL; + case AUDIO_MICROPHONE_DIRECTIONALITY_CARDIOID: + return AudioMicrophoneDirectionality::CARDIOID; + case AUDIO_MICROPHONE_DIRECTIONALITY_HYPER_CARDIOID: + return AudioMicrophoneDirectionality::HYPER_CARDIOID; + case AUDIO_MICROPHONE_DIRECTIONALITY_SUPER_CARDIOID: + return AudioMicrophoneDirectionality::SUPER_CARDIOID; + } +} + +bool halToMicrophoneCharacteristics(MicrophoneInfo* pDst, + const struct audio_microphone_characteristic_t& src) { + bool status = false; + if (pDst != NULL) { + pDst->deviceId = src.device_id; + + if (deviceAddressFromHal(src.device, src.address, &pDst->deviceAddress) != OK) { + return false; + } + pDst->channelMapping.resize(AUDIO_CHANNEL_COUNT_MAX); + for (size_t ch = 0; ch < pDst->channelMapping.size(); ch++) { + pDst->channelMapping[ch] = halToChannelMapping(src.channel_mapping[ch]); + } + pDst->location = halToLocation(src.location); + pDst->group = (AudioMicrophoneGroup)src.group; + pDst->indexInTheGroup = (uint32_t)src.index_in_the_group; + pDst->sensitivity = src.sensitivity; + pDst->maxSpl = src.max_spl; + pDst->minSpl = src.min_spl; + pDst->directionality = halToDirectionality(src.directionality); + pDst->frequencyResponse.resize(src.num_frequency_responses); + for (size_t k = 0; k < src.num_frequency_responses; k++) { + pDst->frequencyResponse[k].frequency = src.frequency_responses[0][k]; + pDst->frequencyResponse[k].level = src.frequency_responses[1][k]; + } + pDst->position.x = src.geometric_location.x; + pDst->position.y = src.geometric_location.y; + pDst->position.z = src.geometric_location.z; + + pDst->orientation.x = src.orientation.x; + pDst->orientation.y = src.orientation.y; + pDst->orientation.z = src.orientation.z; + + status = true; + } + return status; +} +#endif + +} // namespace implementation +} // namespace CPP_VERSION +} // namespace audio +} // namespace hardware +} // namespace android -- cgit v1.2.3 From 20614ba60443d820737c966a197998214d645189 Mon Sep 17 00:00:00 2001 From: Kevin Rocard Date: Sat, 10 Nov 2018 07:20:17 -0800 Subject: Audio HAL V5: Introduce HAL V5, equal to V4 for now find -name 4.0 | xargs -I@ cp -r @ @/../5.0 find 5.0 */5.0 -type f | xargs sed -Ei 's/V4/V5/;s#(@|/)4.0#\15.0#' find -name *cpp -o -name *.h | xargs sed -i 's/VERSION == 4/VERSION >= 4/g' mv {4.1,5.0}/config/audio_policy_configuration.xsd Then a manual update to Android.bp to add V5 support. Then a manual update to service.cpp to add support for loading 5.0. If someone knows a way to avoid copying the .hal, it would be great. They will be 99% identical between V4 and V5. Bug: 118203066 Test: vts-tradefed run commandAndExit vts --module VtsHalAudioEffectV2_0Target vts-tradefed run commandAndExit vts --module VtsHalAudioV2_0Target vts-tradefed run commandAndExit vts --module VtsHalAudioEffectV4_0Target vts-tradefed run commandAndExit vts --module VtsHalAudioV4_0Target Change-Id: If99a5645d19c9780019704ea4f51f8114d83ee8f Signed-off-by: Kevin Rocard --- audio/core/all-versions/default/Conversions.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'audio/core/all-versions/default/Conversions.cpp') diff --git a/audio/core/all-versions/default/Conversions.cpp b/audio/core/all-versions/default/Conversions.cpp index 7ce0c59d18..b1e6c603b3 100644 --- a/audio/core/all-versions/default/Conversions.cpp +++ b/audio/core/all-versions/default/Conversions.cpp @@ -58,7 +58,7 @@ std::string deviceAddressToHal(const DeviceAddress& address) { return halAddress; } -#if MAJOR_VERSION == 4 +#if MAJOR_VERSION >= 4 status_t deviceAddressFromHal(audio_devices_t device, const char* halAddress, DeviceAddress* address) { if (address == nullptr) { -- cgit v1.2.3 From 543bf9cb161e173bed2a733c2ce442cea90bacd8 Mon Sep 17 00:00:00 2001 From: Mikhail Naganov Date: Tue, 11 Dec 2018 16:36:53 -0800 Subject: Audio HAL: Simplify types updates for 'common' and 'audio' packages Instead of importing every single type individually ("using "), import entire namespaces of ::android::hardware::audio::CPP_VERSION and ::android::hardware::audio::common::CPP_VERSION inside the "implementation" namespace in the default implementation. This simplifies modifying types, as it is not needed anymore to surround "using " directives with #if MAJOR_VERSION ... Note that the contents of the namespaces are imported inside the "implementation" namespace, which reduces risks of name collision Also, fixed the namespace for HidlUtils--it needs to be in 'implementation'. Test: make Change-Id: I81bbb3074b5763717ae72e747a075fd46dc36d18 --- audio/core/all-versions/default/Conversions.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'audio/core/all-versions/default/Conversions.cpp') diff --git a/audio/core/all-versions/default/Conversions.cpp b/audio/core/all-versions/default/Conversions.cpp index b1e6c603b3..9249780e93 100644 --- a/audio/core/all-versions/default/Conversions.cpp +++ b/audio/core/all-versions/default/Conversions.cpp @@ -26,7 +26,7 @@ namespace audio { namespace CPP_VERSION { namespace implementation { -using ::android::hardware::audio::common::CPP_VERSION::AudioDevice; +using namespace ::android::hardware::audio::common::CPP_VERSION; std::string deviceAddressToHal(const DeviceAddress& address) { // HAL assumes that the address is NUL-terminated. -- cgit v1.2.3 From 00a44c0fc2bb46b5b93b6b426b97a0b9591d9f5a Mon Sep 17 00:00:00 2001 From: Mikhail Naganov Date: Fri, 14 Dec 2018 14:31:45 -0800 Subject: Audio HAL: More generalization for types imports Complement imports of audio::CPP_VERSION with audio::common::CPP_VERSION for core. Import namespaces in VTS tests. Remove "using namespace" from .cpp files where there are already imports in the .h file. Test: try moving types between audio/types.hal and common/types.hal. Change-Id: I49c2e2466c8175f2516c2273f5c26d125afd4801 --- audio/core/all-versions/default/Conversions.cpp | 2 -- 1 file changed, 2 deletions(-) (limited to 'audio/core/all-versions/default/Conversions.cpp') diff --git a/audio/core/all-versions/default/Conversions.cpp b/audio/core/all-versions/default/Conversions.cpp index 9249780e93..11872c0e00 100644 --- a/audio/core/all-versions/default/Conversions.cpp +++ b/audio/core/all-versions/default/Conversions.cpp @@ -26,8 +26,6 @@ namespace audio { namespace CPP_VERSION { namespace implementation { -using namespace ::android::hardware::audio::common::CPP_VERSION; - std::string deviceAddressToHal(const DeviceAddress& address) { // HAL assumes that the address is NUL-terminated. char halAddress[AUDIO_DEVICE_MAX_ADDRESS_LEN]; -- cgit v1.2.3