• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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,uint32_t sample_rate)95 size_t getBufferSizeFrames(size_t duration_ms, uint32_t sample_rate) {
96     // AudioFlinger requires the buffer to be aligned by 16 frames
97     return align(sample_rate * duration_ms / 1000, 16);
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,const AudioConfig & src,AudioConfig & suggested)153 bool checkAudioConfig(const bool isOut,
154                       size_t duration_ms,
155                       const AudioConfig &src,
156                       AudioConfig &suggested) {
157     bool result = true;
158     suggested = src;
159 
160     if (!checkSampleRateHz(src.base.sampleRateHz, suggested.base.sampleRateHz)) {
161         result = false;
162     }
163 
164     if (!checkChannelMask(isOut, src.base.channelMask, suggested.base.channelMask)) {
165         result = false;
166     }
167 
168     if (!checkFormat(src.base.format, suggested.base.format)) {
169         result = false;
170     }
171 
172     if (src.frameCount == 0) {
173         suggested.frameCount = getBufferSizeFrames(duration_ms, src.base.sampleRateHz);
174     }
175 
176     return result;
177 }
178 
checkAudioPortConfig(const AudioPortConfig & cfg)179 bool checkAudioPortConfig(const AudioPortConfig &cfg) {
180     if (cfg.base.format.getDiscriminator() ==
181             AudioConfigBaseOptional::Format::hidl_discriminator::value) {
182         if (xsd::isUnknownAudioFormat(cfg.base.format.value())) {
183             return false;
184         }
185     }
186     if (cfg.base.channelMask.getDiscriminator() ==
187             AudioConfigBaseOptional::ChannelMask::hidl_discriminator::value) {
188         if (xsd::isUnknownAudioChannelMask(cfg.base.channelMask.value())) {
189             return false;
190         }
191     }
192     if (cfg.gain.getDiscriminator() ==
193             AudioPortConfig::OptionalGain::hidl_discriminator::config) {
194         for (const auto& gainMode : cfg.gain.config().mode) {
195             if (xsd::isUnknownAudioGainMode(gainMode)) {
196                 return false;
197             }
198         }
199         if (xsd::isUnknownAudioChannelMask(cfg.gain.config().channelMask)) {
200             return false;
201         }
202     }
203     switch (cfg.ext.getDiscriminator()) {
204         case AudioPortExtendedInfo::hidl_discriminator::device:
205             if (xsd::isUnknownAudioDevice(cfg.ext.device().deviceType)) {
206                 return false;
207             }
208             break;
209         case AudioPortExtendedInfo::hidl_discriminator::mix:
210             switch (cfg.ext.mix().useCase.getDiscriminator()) {
211                 case AudioPortExtendedInfo::AudioPortMixExt::UseCase::hidl_discriminator::stream:
212                     if (const auto& stream = cfg.ext.mix().useCase.stream();
213                             !stream.empty() && xsd::isUnknownAudioStreamType(stream)) {
214                         return false;
215                     }
216                     break;
217                 case AudioPortExtendedInfo::AudioPortMixExt::UseCase::hidl_discriminator::source:
218                     if (xsd::isUnknownAudioSource(cfg.ext.mix().useCase.source())) {
219                         return false;
220                     }
221                     break;
222             }
223             break;
224         default:
225             break;
226     }
227     return true;
228 }
229 
nsecs2TimeSpec(nsecs_t ns)230 TimeSpec nsecs2TimeSpec(nsecs_t ns) {
231     TimeSpec ts;
232     ts.tvSec = ns2s(ns);
233     ts.tvNSec = ns - s2ns(ts.tvSec);
234     return ts;
235 }
236 
setThreadPriority(int prio)237 void setThreadPriority(int prio) {
238     setpriority(PRIO_PROCESS, 0, prio);
239     set_sched_policy(0, SP_FOREGROUND);
240 }
241 
242 }  // namespace util
243 }  // namespace implementation
244 }  // namespace CPP_VERSION
245 }  // namespace audio
246 }  // namespace hardware
247 }  // namespace android
248