/* * Copyright (C) 2022 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 #include #include #include #include #include #include #include #include #include #include #include namespace aidl::android::hardware::audio::common { // Some values are reserved for use by the system code only. // HALs must not accept or emit values outside from the provided list. constexpr std::array<::aidl::android::media::audio::common::AudioMode, 5> kValidAudioModes = { ::aidl::android::media::audio::common::AudioMode::NORMAL, ::aidl::android::media::audio::common::AudioMode::RINGTONE, ::aidl::android::media::audio::common::AudioMode::IN_CALL, ::aidl::android::media::audio::common::AudioMode::IN_COMMUNICATION, ::aidl::android::media::audio::common::AudioMode::CALL_SCREEN, }; constexpr size_t getPcmSampleSizeInBytes(::aidl::android::media::audio::common::PcmType pcm) { using ::aidl::android::media::audio::common::PcmType; switch (pcm) { case PcmType::UINT_8_BIT: return 1; case PcmType::INT_16_BIT: return 2; case PcmType::INT_32_BIT: return 4; case PcmType::FIXED_Q_8_24: return 4; case PcmType::FLOAT_32_BIT: return 4; case PcmType::INT_24_BIT: return 3; } return 0; } constexpr size_t getChannelCount( const ::aidl::android::media::audio::common::AudioChannelLayout& layout, int32_t mask = std::numeric_limits::max()) { using Tag = ::aidl::android::media::audio::common::AudioChannelLayout::Tag; switch (layout.getTag()) { case Tag::none: return 0; case Tag::invalid: return 0; case Tag::indexMask: return __builtin_popcount(layout.get() & mask); case Tag::layoutMask: return __builtin_popcount(layout.get() & mask); case Tag::voiceMask: return __builtin_popcount(layout.get() & mask); } return 0; } constexpr size_t getFrameSizeInBytes( const ::aidl::android::media::audio::common::AudioFormatDescription& format, const ::aidl::android::media::audio::common::AudioChannelLayout& layout) { if (format == ::aidl::android::media::audio::common::AudioFormatDescription{}) { // Unspecified format. return 0; } using ::aidl::android::media::audio::common::AudioFormatType; if (format.type == AudioFormatType::PCM) { return getPcmSampleSizeInBytes(format.pcm) * getChannelCount(layout); } else if (format.type == AudioFormatType::NON_PCM) { // For non-PCM formats always use the underlying PCM size. The default value for // PCM is "UINT_8_BIT", thus non-encapsulated streams have the frame size of 1. return getPcmSampleSizeInBytes(format.pcm); } // Something unexpected. return 0; } 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; } constexpr bool isTelephonyDeviceType( ::aidl::android::media::audio::common::AudioDeviceType device) { return device == ::aidl::android::media::audio::common::AudioDeviceType::IN_TELEPHONY_RX || device == ::aidl::android::media::audio::common::AudioDeviceType::OUT_TELEPHONY_TX; } constexpr bool isUsbInputDeviceType(::aidl::android::media::audio::common::AudioDeviceType type) { switch (type) { case ::aidl::android::media::audio::common::AudioDeviceType::IN_DOCK: case ::aidl::android::media::audio::common::AudioDeviceType::IN_ACCESSORY: case ::aidl::android::media::audio::common::AudioDeviceType::IN_DEVICE: case ::aidl::android::media::audio::common::AudioDeviceType::IN_HEADSET: return true; default: return false; } } constexpr bool isUsbOutputtDeviceType(::aidl::android::media::audio::common::AudioDeviceType type) { switch (type) { case ::aidl::android::media::audio::common::AudioDeviceType::OUT_DOCK: case ::aidl::android::media::audio::common::AudioDeviceType::OUT_ACCESSORY: case ::aidl::android::media::audio::common::AudioDeviceType::OUT_DEVICE: case ::aidl::android::media::audio::common::AudioDeviceType::OUT_HEADSET: return true; default: return false; } } constexpr bool isValidAudioMode(::aidl::android::media::audio::common::AudioMode mode) { return std::find(kValidAudioModes.begin(), kValidAudioModes.end(), mode) != kValidAudioModes.end(); } static inline bool maybeVendorExtension(const std::string& s) { // Only checks whether the string starts with the "vendor prefix". static const std::string vendorPrefix = "VX_"; return s.size() > vendorPrefix.size() && s.substr(0, vendorPrefix.size()) == vendorPrefix; } static inline bool isVendorExtension(const std::string& s) { // Must be the same as defined in {Playback|Record}TrackMetadata.aidl static const std::regex vendorExtension("VX_[A-Z0-9]{3,}_[_A-Z0-9]+"); return std::regex_match(s.begin(), s.end(), vendorExtension); } // The helper functions defined below are only applicable to the case when an enum type // specifies zero-based bit positions, not bit masks themselves. This is why instantiation // is restricted to certain enum types. template using is_bit_position_enum = std::integral_constant< bool, std::is_same_v || std::is_same_v>; template , typename = std::enable_if_t::value>> constexpr U makeBitPositionFlagMask(E flag) { return 1 << static_cast(flag); } template , typename = std::enable_if_t::value>> constexpr bool isBitPositionFlagSet(U mask, E flag) { return (mask & makeBitPositionFlagMask(flag)) != 0; } template , typename = std::enable_if_t::value>> constexpr U makeBitPositionFlagMask(std::initializer_list flags) { U result = 0; for (const auto flag : flags) { result |= makeBitPositionFlagMask(flag); } return result; } } // namespace aidl::android::hardware::audio::common