1 /*
2  * Copyright (C) 2020 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 #include <log/log.h>
18 //#include <cutils/bitops.h>
19 #include <cutils/sched_policy.h>
20 #include <system/audio.h>
21 #include <sys/resource.h>
22 #include <pthread.h>
23 #include PATH(APM_XSD_ENUMS_H_FILENAME)
24 #include "util.h"
25 #include "debug.h"
26 
27 namespace xsd {
28 using namespace ::android::audio::policy::configuration::CPP_VERSION;
29 }
30 
31 namespace android {
32 namespace hardware {
33 namespace audio {
34 namespace CPP_VERSION {
35 namespace implementation {
36 namespace util {
37 
38 using ::android::hardware::audio::common::COMMON_TYPES_CPP_VERSION::AudioConfigBaseOptional;
39 using ::android::hardware::audio::common::COMMON_TYPES_CPP_VERSION::AudioPortExtendedInfo;
40 using ::android::hardware::audio::CORE_TYPES_CPP_VERSION::AudioMicrophoneDirectionality;
41 
42 namespace {
43 
44 const std::array<uint32_t, 8> kSupportedRatesHz = {
45     8000, 11025, 16000, 22050, 32000, 44100, 48000
46 };
47 
checkSampleRateHz(uint32_t value,uint32_t & suggested)48 bool checkSampleRateHz(uint32_t value, uint32_t &suggested) {
49     for (const uint32_t supported : kSupportedRatesHz) {
50         if (value <= supported) {
51             suggested = supported;
52             return value == supported;
53         }
54     }
55 
56     suggested = kSupportedRatesHz.back();
57     return FAILURE(false);
58 }
59 
checkChannelMask(const bool isOut,const AudioChannelMask & value,AudioChannelMask & suggested)60 bool checkChannelMask(const bool isOut,
61                       const AudioChannelMask &value,
62                       AudioChannelMask &suggested) {
63     switch (xsd::stringToAudioChannelMask(value)) {
64     case xsd::AudioChannelMask::AUDIO_CHANNEL_OUT_MONO:
65     case xsd::AudioChannelMask::AUDIO_CHANNEL_OUT_STEREO:
66     case xsd::AudioChannelMask::AUDIO_CHANNEL_IN_MONO:
67     case xsd::AudioChannelMask::AUDIO_CHANNEL_IN_STEREO:
68         suggested = value;
69         return true;
70 
71     default:
72         suggested = toString(isOut ?
73             xsd::AudioChannelMask::AUDIO_CHANNEL_OUT_STEREO :
74             xsd::AudioChannelMask::AUDIO_CHANNEL_IN_MONO);
75         return FAILURE(false);
76     }
77 }
78 
checkFormat(const AudioFormat & value,AudioFormat & suggested)79 bool checkFormat(const AudioFormat &value, AudioFormat &suggested) {
80     switch (xsd::stringToAudioFormat(value)) {
81     case xsd::AudioFormat::AUDIO_FORMAT_PCM_16_BIT:
82         suggested = value;
83         return true;
84 
85     default:
86         suggested = toString(xsd::AudioFormat::AUDIO_FORMAT_PCM_16_BIT);
87         return FAILURE(false);
88     }
89 }
90 
align(size_t v,size_t a)91 size_t align(size_t v, size_t a) {
92     return (v + a - 1) / a * a;
93 }
94 
getBufferSizeFrames(size_t duration_ms,size_t sample_rate,size_t extraAlignment)95 size_t getBufferSizeFrames(size_t duration_ms, size_t sample_rate, size_t extraAlignment) {
96     // AudioFlinger requires the buffer to be aligned by 16 frames
97     return align(sample_rate * duration_ms / 1000, 16 * extraAlignment);
98 }
99 
100 }  // namespace
101 
getMicrophoneInfo()102 MicrophoneInfo getMicrophoneInfo() {
103     MicrophoneInfo mic;
104 
105     mic.deviceId = "mic_goldfish";
106     mic.group = 0;
107     mic.indexInTheGroup = 0;
108     mic.sensitivity = AUDIO_MICROPHONE_SENSITIVITY_UNKNOWN;
109     mic.maxSpl = AUDIO_MICROPHONE_SENSITIVITY_UNKNOWN;
110     mic.minSpl = AUDIO_MICROPHONE_SENSITIVITY_UNKNOWN;
111     mic.directionality = AudioMicrophoneDirectionality::UNKNOWN;
112     mic.position.x = AUDIO_MICROPHONE_COORDINATE_UNKNOWN;
113     mic.position.y = AUDIO_MICROPHONE_COORDINATE_UNKNOWN;
114     mic.position.z = AUDIO_MICROPHONE_COORDINATE_UNKNOWN;
115     mic.orientation.x = AUDIO_MICROPHONE_COORDINATE_UNKNOWN;
116     mic.orientation.y = AUDIO_MICROPHONE_COORDINATE_UNKNOWN;
117     mic.orientation.z = AUDIO_MICROPHONE_COORDINATE_UNKNOWN;
118 
119     return mic;
120 }
121 
countChannels(const AudioChannelMask & mask)122 size_t countChannels(const AudioChannelMask &mask) {
123     return xsd::getChannelCount(mask);
124 }
125 
getBytesPerSample(const AudioFormat & format)126 size_t getBytesPerSample(const AudioFormat &format) {
127     if (format == "AUDIO_FORMAT_PCM_16_BIT") {
128         return 2;
129     } else {
130         ALOGE("util::%s:%d unknown format, '%s'", __func__, __LINE__, format.c_str());
131         return 0;
132     }
133 }
134 
checkAudioConfig(const AudioConfig & cfg)135 bool checkAudioConfig(const AudioConfig &cfg) {
136     if (xsd::isUnknownAudioFormat(cfg.base.format)
137             || xsd::isUnknownAudioChannelMask(cfg.base.channelMask)) {
138         return false;
139     }
140     if (cfg.offloadInfo.getDiscriminator() ==
141             AudioConfig::OffloadInfo::hidl_discriminator::info) {
142         if (const auto& info = cfg.offloadInfo.info();
143                 xsd::isUnknownAudioFormat(info.base.format)
144                 || xsd::isUnknownAudioChannelMask(info.base.channelMask)
145                 || (!info.streamType.empty() && xsd::isUnknownAudioStreamType(info.streamType))
146                 || xsd::isUnknownAudioUsage(info.usage)) {
147             return false;
148         }
149     }
150     return true;
151 }
152 
checkAudioConfig(const bool isOut,size_t duration_ms,size_t extraAlignment,const AudioConfig & src,AudioConfig & suggested)153 bool checkAudioConfig(const bool isOut,
154                       size_t duration_ms,
155                       size_t extraAlignment,
156                       const AudioConfig &src,
157                       AudioConfig &suggested) {
158     bool result = true;
159     suggested = src;
160 
161     if (!checkSampleRateHz(src.base.sampleRateHz, suggested.base.sampleRateHz)) {
162         result = false;
163     }
164 
165     if (!checkChannelMask(isOut, src.base.channelMask, suggested.base.channelMask)) {
166         result = false;
167     }
168 
169     if (!checkFormat(src.base.format, suggested.base.format)) {
170         result = false;
171     }
172 
173     if (src.frameCount == 0) {
174         suggested.frameCount = getBufferSizeFrames(duration_ms, src.base.sampleRateHz,
175                                                    extraAlignment);
176     }
177 
178     return result;
179 }
180 
checkAudioPortConfig(const AudioPortConfig & cfg)181 bool checkAudioPortConfig(const AudioPortConfig &cfg) {
182     if (cfg.base.format.getDiscriminator() ==
183             AudioConfigBaseOptional::Format::hidl_discriminator::value) {
184         if (xsd::isUnknownAudioFormat(cfg.base.format.value())) {
185             return false;
186         }
187     }
188     if (cfg.base.channelMask.getDiscriminator() ==
189             AudioConfigBaseOptional::ChannelMask::hidl_discriminator::value) {
190         if (xsd::isUnknownAudioChannelMask(cfg.base.channelMask.value())) {
191             return false;
192         }
193     }
194     if (cfg.gain.getDiscriminator() ==
195             AudioPortConfig::OptionalGain::hidl_discriminator::config) {
196         for (const auto& gainMode : cfg.gain.config().mode) {
197             if (xsd::isUnknownAudioGainMode(gainMode)) {
198                 return false;
199             }
200         }
201         if (xsd::isUnknownAudioChannelMask(cfg.gain.config().channelMask)) {
202             return false;
203         }
204     }
205     switch (cfg.ext.getDiscriminator()) {
206         case AudioPortExtendedInfo::hidl_discriminator::device:
207             if (xsd::isUnknownAudioDevice(cfg.ext.device().deviceType)) {
208                 return false;
209             }
210             break;
211         case AudioPortExtendedInfo::hidl_discriminator::mix:
212             switch (cfg.ext.mix().useCase.getDiscriminator()) {
213                 case AudioPortExtendedInfo::AudioPortMixExt::UseCase::hidl_discriminator::stream:
214                     if (const auto& stream = cfg.ext.mix().useCase.stream();
215                             !stream.empty() && xsd::isUnknownAudioStreamType(stream)) {
216                         return false;
217                     }
218                     break;
219                 case AudioPortExtendedInfo::AudioPortMixExt::UseCase::hidl_discriminator::source:
220                     if (xsd::isUnknownAudioSource(cfg.ext.mix().useCase.source())) {
221                         return false;
222                     }
223                     break;
224             }
225             break;
226         default:
227             break;
228     }
229     return true;
230 }
231 
nsecs2TimeSpec(nsecs_t ns)232 TimeSpec nsecs2TimeSpec(nsecs_t ns) {
233     TimeSpec ts;
234     ts.tvSec = ns2s(ns);
235     ts.tvNSec = ns - s2ns(ts.tvSec);
236     return ts;
237 }
238 
setThreadPriority(const SchedPolicy policy,const int prio)239 bool setThreadPriority(const SchedPolicy policy, const int prio) {
240     int e = set_sched_policy(0, policy);
241     if (e < 0) {
242         ALOGE("%s:%d set_sched_policy(%d) failed with %s (%d)",
243               __func__, __LINE__, static_cast<int>(policy), strerror(-e), -e);
244         return FAILURE(false);
245     }
246 
247     if (setpriority(PRIO_PROCESS, 0, prio) < 0) {
248         e = errno;
249         ALOGE("%s:%d setpriority(%d) failed with %s (%d)",
250               __func__, __LINE__, prio, strerror(-e), -e);
251         return FAILURE(false);
252     }
253 
254     return true;
255 }
256 
257 }  // namespace util
258 }  // namespace implementation
259 }  // namespace CPP_VERSION
260 }  // namespace audio
261 }  // namespace hardware
262 }  // namespace android
263