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 "util.h"
24 #include "debug.h"
25 
26 namespace android {
27 namespace hardware {
28 namespace audio {
29 namespace V6_0 {
30 namespace implementation {
31 namespace util {
32 
33 namespace {
34 
35 const std::array<uint32_t, 8> kSupportedRatesHz = {
36     8000, 11025, 16000, 22050, 24000, 32000, 44100, 48000
37 };
38 
39 const std::array<hidl_bitfield<AudioChannelMask>, 4> kSupportedInChannelMask = {
40     AudioChannelMask::IN_LEFT | 0,
41     AudioChannelMask::IN_RIGHT | 0,
42     AudioChannelMask::IN_FRONT | 0,
43     AudioChannelMask::IN_STEREO | 0,
44 };
45 
46 const std::array<hidl_bitfield<AudioChannelMask>, 4> kSupportedOutChannelMask = {
47     AudioChannelMask::OUT_FRONT_LEFT | 0,
48     AudioChannelMask::OUT_FRONT_RIGHT | 0,
49     AudioChannelMask::OUT_FRONT_CENTER | 0,
50     AudioChannelMask::OUT_STEREO | 0,
51 };
52 
53 const std::array<AudioFormat, 1> kSupportedAudioFormats = {
54     AudioFormat::PCM_16_BIT,
55 };
56 
checkSampleRateHz(uint32_t value,uint32_t & suggest)57 bool checkSampleRateHz(uint32_t value, uint32_t &suggest) {
58     for (const uint32_t supported : kSupportedRatesHz) {
59         if (value <= supported) {
60             suggest = supported;
61             return (value == supported);
62         }
63     }
64 
65     suggest = kSupportedRatesHz.back();
66     return FAILURE(false);
67 }
68 
align(size_t v,size_t a)69 size_t align(size_t v, size_t a) {
70     return (v + a - 1) / a * a;
71 }
72 
getBufferSizeFrames(size_t duration_ms,uint32_t sample_rate)73 size_t getBufferSizeFrames(size_t duration_ms, uint32_t sample_rate) {
74     // AudioFlinger requires the buffer to be aligned by 16 frames
75     return align(sample_rate * duration_ms / 1000, 16);
76 }
77 
78 }  // namespace
79 
getMicrophoneInfo()80 MicrophoneInfo getMicrophoneInfo() {
81     MicrophoneInfo mic;
82 
83     mic.deviceId = "mic_goldfish";
84     mic.group = 0;
85     mic.indexInTheGroup = 0;
86     mic.sensitivity = AUDIO_MICROPHONE_SENSITIVITY_UNKNOWN;
87     mic.maxSpl = AUDIO_MICROPHONE_SENSITIVITY_UNKNOWN;
88     mic.minSpl = AUDIO_MICROPHONE_SENSITIVITY_UNKNOWN;
89     mic.directionality = AudioMicrophoneDirectionality::UNKNOWN;
90     mic.position.x = AUDIO_MICROPHONE_COORDINATE_UNKNOWN;
91     mic.position.y = AUDIO_MICROPHONE_COORDINATE_UNKNOWN;
92     mic.position.z = AUDIO_MICROPHONE_COORDINATE_UNKNOWN;
93     mic.orientation.x = AUDIO_MICROPHONE_COORDINATE_UNKNOWN;
94     mic.orientation.y = AUDIO_MICROPHONE_COORDINATE_UNKNOWN;
95     mic.orientation.z = AUDIO_MICROPHONE_COORDINATE_UNKNOWN;
96 
97     return mic;
98 }
99 
countChannels(hidl_bitfield<AudioChannelMask> mask)100 size_t countChannels(hidl_bitfield<AudioChannelMask> mask) {
101     return popcount(mask);
102 }
103 
getBytesPerSample(AudioFormat format)104 size_t getBytesPerSample(AudioFormat format) {
105     return audio_bytes_per_sample(static_cast<audio_format_t>(format));
106 }
107 
checkAudioConfig(bool isOut,size_t duration_ms,const AudioConfig & cfg,AudioConfig & suggested)108 bool checkAudioConfig(bool isOut,
109                       size_t duration_ms,
110                       const AudioConfig &cfg,
111                       AudioConfig &suggested) {
112     bool valid = checkSampleRateHz(cfg.sampleRateHz, suggested.sampleRateHz);
113 
114     if (isOut) {
115         if (std::find(kSupportedOutChannelMask.begin(),
116                       kSupportedOutChannelMask.end(),
117                       cfg.channelMask) == kSupportedOutChannelMask.end()) {
118             suggested.channelMask = AudioChannelMask::OUT_STEREO | 0;
119             valid = FAILURE(false);
120         } else {
121             suggested.channelMask = cfg.channelMask;
122         }
123     } else {
124         if (std::find(kSupportedInChannelMask.begin(),
125                       kSupportedInChannelMask.end(),
126                       cfg.channelMask) == kSupportedInChannelMask.end()) {
127             suggested.channelMask = AudioChannelMask::IN_STEREO | 0;
128             valid = FAILURE(false);
129         } else {
130             suggested.channelMask = cfg.channelMask;
131         }
132     }
133 
134     if (std::find(kSupportedAudioFormats.begin(),
135                   kSupportedAudioFormats.end(),
136                   cfg.format) == kSupportedAudioFormats.end()) {
137         suggested.format = AudioFormat::PCM_16_BIT;
138         valid = FAILURE(false);
139     } else {
140         suggested.format = cfg.format;
141     }
142 
143     suggested.offloadInfo = cfg.offloadInfo;    // don't care
144 
145     suggested.frameCount = (cfg.frameCount == 0)
146         ? getBufferSizeFrames(duration_ms, suggested.sampleRateHz)
147         : cfg.frameCount;
148 
149     return valid;
150 }
151 
nsecs2TimeSpec(nsecs_t ns)152 TimeSpec nsecs2TimeSpec(nsecs_t ns) {
153     TimeSpec ts;
154     ts.tvSec = ns2s(ns);
155     ts.tvNSec = ns - s2ns(ts.tvSec);
156     return ts;
157 }
158 
setThreadPriority(int prio)159 void setThreadPriority(int prio) {
160     setpriority(PRIO_PROCESS, 0, prio);
161     set_sched_policy(0, SP_FOREGROUND);
162 }
163 
164 }  // namespace util
165 }  // namespace implementation
166 }  // namespace V6_0
167 }  // namespace audio
168 }  // namespace hardware
169 }  // namespace android
170