1 /*
2 * Copyright (C) 2022 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #pragma once
18
19 #include <algorithm>
20 #include <array>
21 #include <initializer_list>
22 #include <regex>
23 #include <type_traits>
24
25 #include <aidl/android/media/audio/common/AudioChannelLayout.h>
26 #include <aidl/android/media/audio/common/AudioDeviceType.h>
27 #include <aidl/android/media/audio/common/AudioFormatDescription.h>
28 #include <aidl/android/media/audio/common/AudioInputFlags.h>
29 #include <aidl/android/media/audio/common/AudioMode.h>
30 #include <aidl/android/media/audio/common/AudioOutputFlags.h>
31 #include <aidl/android/media/audio/common/PcmType.h>
32 #include <android/binder_auto_utils.h>
33
34 namespace ndk {
35
36 // This enables use of 'error/expected_utils' for ScopedAStatus.
37
errorIsOk(const ScopedAStatus & s)38 inline bool errorIsOk(const ScopedAStatus& s) {
39 return s.isOk();
40 }
41
errorToString(const ScopedAStatus & s)42 inline std::string errorToString(const ScopedAStatus& s) {
43 return s.getDescription();
44 }
45
46 } // namespace ndk
47
48 namespace aidl::android::hardware::audio::common {
49
50 // Some values are reserved for use by the system code only.
51 // HALs must not accept or emit values outside from the provided list.
52 constexpr std::array<::aidl::android::media::audio::common::AudioMode, 5> kValidAudioModes = {
53 ::aidl::android::media::audio::common::AudioMode::NORMAL,
54 ::aidl::android::media::audio::common::AudioMode::RINGTONE,
55 ::aidl::android::media::audio::common::AudioMode::IN_CALL,
56 ::aidl::android::media::audio::common::AudioMode::IN_COMMUNICATION,
57 ::aidl::android::media::audio::common::AudioMode::CALL_SCREEN,
58 };
59
getPcmSampleSizeInBytes(::aidl::android::media::audio::common::PcmType pcm)60 constexpr size_t getPcmSampleSizeInBytes(::aidl::android::media::audio::common::PcmType pcm) {
61 using ::aidl::android::media::audio::common::PcmType;
62 switch (pcm) {
63 case PcmType::UINT_8_BIT:
64 return 1;
65 case PcmType::INT_16_BIT:
66 return 2;
67 case PcmType::INT_32_BIT:
68 return 4;
69 case PcmType::FIXED_Q_8_24:
70 return 4;
71 case PcmType::FLOAT_32_BIT:
72 return 4;
73 case PcmType::INT_24_BIT:
74 return 3;
75 }
76 return 0;
77 }
78
79 constexpr size_t getChannelCount(
80 const ::aidl::android::media::audio::common::AudioChannelLayout& layout,
81 int32_t mask = std::numeric_limits<int32_t>::max()) {
82 using Tag = ::aidl::android::media::audio::common::AudioChannelLayout::Tag;
83 switch (layout.getTag()) {
84 case Tag::none:
85 return 0;
86 case Tag::invalid:
87 return 0;
88 case Tag::indexMask:
89 return __builtin_popcount(layout.get<Tag::indexMask>() & mask);
90 case Tag::layoutMask:
91 return __builtin_popcount(layout.get<Tag::layoutMask>() & mask);
92 case Tag::voiceMask:
93 return __builtin_popcount(layout.get<Tag::voiceMask>() & mask);
94 }
95 return 0;
96 }
97
getFrameSizeInBytes(const::aidl::android::media::audio::common::AudioFormatDescription & format,const::aidl::android::media::audio::common::AudioChannelLayout & layout)98 constexpr size_t getFrameSizeInBytes(
99 const ::aidl::android::media::audio::common::AudioFormatDescription& format,
100 const ::aidl::android::media::audio::common::AudioChannelLayout& layout) {
101 if (format == ::aidl::android::media::audio::common::AudioFormatDescription{}) {
102 // Unspecified format.
103 return 0;
104 }
105 using ::aidl::android::media::audio::common::AudioFormatType;
106 if (format.type == AudioFormatType::PCM) {
107 return getPcmSampleSizeInBytes(format.pcm) * getChannelCount(layout);
108 } else if (format.type == AudioFormatType::NON_PCM) {
109 // For non-PCM formats always use the underlying PCM size. The default value for
110 // PCM is "UINT_8_BIT", thus non-encapsulated streams have the frame size of 1.
111 return getPcmSampleSizeInBytes(format.pcm);
112 }
113 // Something unexpected.
114 return 0;
115 }
116
isDefaultAudioFormat(const::aidl::android::media::audio::common::AudioFormatDescription & desc)117 constexpr bool isDefaultAudioFormat(
118 const ::aidl::android::media::audio::common::AudioFormatDescription& desc) {
119 return desc.type == ::aidl::android::media::audio::common::AudioFormatType::DEFAULT &&
120 desc.pcm == ::aidl::android::media::audio::common::PcmType::DEFAULT &&
121 desc.encoding.empty();
122 }
123
isTelephonyDeviceType(::aidl::android::media::audio::common::AudioDeviceType device)124 constexpr bool isTelephonyDeviceType(
125 ::aidl::android::media::audio::common::AudioDeviceType device) {
126 return device == ::aidl::android::media::audio::common::AudioDeviceType::IN_TELEPHONY_RX ||
127 device == ::aidl::android::media::audio::common::AudioDeviceType::OUT_TELEPHONY_TX;
128 }
129
isValidAudioMode(::aidl::android::media::audio::common::AudioMode mode)130 constexpr bool isValidAudioMode(::aidl::android::media::audio::common::AudioMode mode) {
131 return std::find(kValidAudioModes.begin(), kValidAudioModes.end(), mode) !=
132 kValidAudioModes.end();
133 }
134
maybeVendorExtension(const std::string & s)135 static inline bool maybeVendorExtension(const std::string& s) {
136 // Only checks whether the string starts with the "vendor prefix".
137 static const std::string vendorPrefix = "VX_";
138 return s.size() > vendorPrefix.size() && s.substr(0, vendorPrefix.size()) == vendorPrefix;
139 }
140
isVendorExtension(const std::string & s)141 static inline bool isVendorExtension(const std::string& s) {
142 // Must be the same as defined in {Playback|Record}TrackMetadata.aidl
143 static const std::regex vendorExtension("VX_[A-Z0-9]{3,}_[_A-Z0-9]+");
144 return std::regex_match(s.begin(), s.end(), vendorExtension);
145 }
146
147 // The helper functions defined below are only applicable to the case when an enum type
148 // specifies zero-based bit positions, not bit masks themselves. This is why instantiation
149 // is restricted to certain enum types.
150 template <typename E>
151 using is_bit_position_enum = std::integral_constant<
152 bool, std::is_same_v<E, ::aidl::android::media::audio::common::AudioInputFlags> ||
153 std::is_same_v<E, ::aidl::android::media::audio::common::AudioOutputFlags>>;
154
155 template <typename E, typename U = std::underlying_type_t<E>,
156 typename = std::enable_if_t<is_bit_position_enum<E>::value>>
makeBitPositionFlagMask(E flag)157 constexpr U makeBitPositionFlagMask(E flag) {
158 return 1 << static_cast<U>(flag);
159 }
160
161 template <typename E, typename U = std::underlying_type_t<E>,
162 typename = std::enable_if_t<is_bit_position_enum<E>::value>>
isBitPositionFlagSet(U mask,E flag)163 constexpr bool isBitPositionFlagSet(U mask, E flag) {
164 return (mask & makeBitPositionFlagMask(flag)) != 0;
165 }
166
167 template <typename E, typename U = std::underlying_type_t<E>,
168 typename = std::enable_if_t<is_bit_position_enum<E>::value>>
makeBitPositionFlagMask(std::initializer_list<E> flags)169 constexpr U makeBitPositionFlagMask(std::initializer_list<E> flags) {
170 U result = 0;
171 for (const auto flag : flags) {
172 result |= makeBitPositionFlagMask(flag);
173 }
174 return result;
175 }
176
177 } // namespace aidl::android::hardware::audio::common
178