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