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 // TODO: b/275135031 - move this string to AIDL interfaces.
51 static constexpr char kDumpFromAudioServerArgument[] = "dump_from_audioserver";
52
53 // Some values are reserved for use by the system code only.
54 // HALs must not accept or emit values outside from the provided list.
55 constexpr std::array<::aidl::android::media::audio::common::AudioMode, 5> kValidAudioModes = {
56 ::aidl::android::media::audio::common::AudioMode::NORMAL,
57 ::aidl::android::media::audio::common::AudioMode::RINGTONE,
58 ::aidl::android::media::audio::common::AudioMode::IN_CALL,
59 ::aidl::android::media::audio::common::AudioMode::IN_COMMUNICATION,
60 ::aidl::android::media::audio::common::AudioMode::CALL_SCREEN,
61 };
62
getPcmSampleSizeInBytes(::aidl::android::media::audio::common::PcmType pcm)63 constexpr size_t getPcmSampleSizeInBytes(::aidl::android::media::audio::common::PcmType pcm) {
64 using ::aidl::android::media::audio::common::PcmType;
65 switch (pcm) {
66 case PcmType::UINT_8_BIT:
67 return 1;
68 case PcmType::INT_16_BIT:
69 return 2;
70 case PcmType::INT_32_BIT:
71 return 4;
72 case PcmType::FIXED_Q_8_24:
73 return 4;
74 case PcmType::FLOAT_32_BIT:
75 return 4;
76 case PcmType::INT_24_BIT:
77 return 3;
78 }
79 return 0;
80 }
81
82 constexpr size_t getChannelCount(
83 const ::aidl::android::media::audio::common::AudioChannelLayout& layout,
84 int32_t mask = std::numeric_limits<int32_t>::max()) {
85 using Tag = ::aidl::android::media::audio::common::AudioChannelLayout::Tag;
86 switch (layout.getTag()) {
87 case Tag::none:
88 return 0;
89 case Tag::invalid:
90 return 0;
91 case Tag::indexMask:
92 return __builtin_popcount(layout.get<Tag::indexMask>() & mask);
93 case Tag::layoutMask:
94 return __builtin_popcount(layout.get<Tag::layoutMask>() & mask);
95 case Tag::voiceMask:
96 return __builtin_popcount(layout.get<Tag::voiceMask>() & mask);
97 }
98 return 0;
99 }
100
getFrameSizeInBytes(const::aidl::android::media::audio::common::AudioFormatDescription & format,const::aidl::android::media::audio::common::AudioChannelLayout & layout)101 constexpr size_t getFrameSizeInBytes(
102 const ::aidl::android::media::audio::common::AudioFormatDescription& format,
103 const ::aidl::android::media::audio::common::AudioChannelLayout& layout) {
104 if (format == ::aidl::android::media::audio::common::AudioFormatDescription{}) {
105 // Unspecified format.
106 return 0;
107 }
108 using ::aidl::android::media::audio::common::AudioFormatType;
109 if (format.type == AudioFormatType::PCM) {
110 return getPcmSampleSizeInBytes(format.pcm) * getChannelCount(layout);
111 } else if (format.type == AudioFormatType::NON_PCM) {
112 // For non-PCM formats always use the underlying PCM size. The default value for
113 // PCM is "UINT_8_BIT", thus non-encapsulated streams have the frame size of 1.
114 return getPcmSampleSizeInBytes(format.pcm);
115 }
116 // Something unexpected.
117 return 0;
118 }
119
isDefaultAudioFormat(const::aidl::android::media::audio::common::AudioFormatDescription & desc)120 constexpr bool isDefaultAudioFormat(
121 const ::aidl::android::media::audio::common::AudioFormatDescription& desc) {
122 return desc.type == ::aidl::android::media::audio::common::AudioFormatType::DEFAULT &&
123 desc.pcm == ::aidl::android::media::audio::common::PcmType::DEFAULT &&
124 desc.encoding.empty();
125 }
126
isTelephonyDeviceType(::aidl::android::media::audio::common::AudioDeviceType device)127 constexpr bool isTelephonyDeviceType(
128 ::aidl::android::media::audio::common::AudioDeviceType device) {
129 return device == ::aidl::android::media::audio::common::AudioDeviceType::IN_TELEPHONY_RX ||
130 device == ::aidl::android::media::audio::common::AudioDeviceType::OUT_TELEPHONY_TX;
131 }
132
isValidAudioMode(::aidl::android::media::audio::common::AudioMode mode)133 constexpr bool isValidAudioMode(::aidl::android::media::audio::common::AudioMode mode) {
134 return std::find(kValidAudioModes.begin(), kValidAudioModes.end(), mode) !=
135 kValidAudioModes.end();
136 }
137
maybeVendorExtension(const std::string & s)138 static inline bool maybeVendorExtension(const std::string& s) {
139 // Only checks whether the string starts with the "vendor prefix".
140 static const std::string vendorPrefix = "VX_";
141 return s.size() > vendorPrefix.size() && s.substr(0, vendorPrefix.size()) == vendorPrefix;
142 }
143
isVendorExtension(const std::string & s)144 static inline bool isVendorExtension(const std::string& s) {
145 // Must be the same as defined in {Playback|Record}TrackMetadata.aidl
146 static const std::regex vendorExtension("VX_[A-Z0-9]{3,}_[_A-Z0-9]+");
147 return std::regex_match(s.begin(), s.end(), vendorExtension);
148 }
149
150 // The helper functions defined below are only applicable to the case when an enum type
151 // specifies zero-based bit positions, not bit masks themselves. This is why instantiation
152 // is restricted to certain enum types.
153 template <typename E>
154 using is_bit_position_enum = std::integral_constant<
155 bool, std::is_same_v<E, ::aidl::android::media::audio::common::AudioInputFlags> ||
156 std::is_same_v<E, ::aidl::android::media::audio::common::AudioOutputFlags>>;
157
158 template <typename E, typename U = std::underlying_type_t<E>,
159 typename = std::enable_if_t<is_bit_position_enum<E>::value>>
makeBitPositionFlagMask(E flag)160 constexpr U makeBitPositionFlagMask(E flag) {
161 return 1 << static_cast<U>(flag);
162 }
163
164 template <typename E, typename U = std::underlying_type_t<E>,
165 typename = std::enable_if_t<is_bit_position_enum<E>::value>>
isBitPositionFlagSet(U mask,E flag)166 constexpr bool isBitPositionFlagSet(U mask, E flag) {
167 return (mask & makeBitPositionFlagMask(flag)) != 0;
168 }
169
170 template <typename E, typename U = std::underlying_type_t<E>,
171 typename = std::enable_if_t<is_bit_position_enum<E>::value>>
makeBitPositionFlagMask(std::initializer_list<E> flags)172 constexpr U makeBitPositionFlagMask(std::initializer_list<E> flags) {
173 U result = 0;
174 for (const auto flag : flags) {
175 result |= makeBitPositionFlagMask(flag);
176 }
177 return result;
178 }
179
180 template <typename E, typename U = std::underlying_type_t<E>,
181 typename = std::enable_if_t<is_bit_position_enum<E>::value>>
isAnyBitPositionFlagSet(U mask,std::initializer_list<E> flags)182 constexpr bool isAnyBitPositionFlagSet(U mask, std::initializer_list<E> flags) {
183 return (mask & makeBitPositionFlagMask<E>(flags)) != 0;
184 }
185
frameCountFromDurationUs(long durationUs,int32_t sampleRateHz)186 constexpr int32_t frameCountFromDurationUs(long durationUs, int32_t sampleRateHz) {
187 return (static_cast<long long>(durationUs) * sampleRateHz) / 1000000LL;
188 }
189
frameCountFromDurationMs(int32_t durationMs,int32_t sampleRateHz)190 constexpr int32_t frameCountFromDurationMs(int32_t durationMs, int32_t sampleRateHz) {
191 return frameCountFromDurationUs(durationMs * 1000, sampleRateHz);
192 }
193
194 } // namespace aidl::android::hardware::audio::common
195