1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <common.h>
17
18 using namespace std;
19 using namespace OHOS;
20 using namespace OHOS::AudioStandard;
21
22 namespace OHOS {
23 namespace AudioStandard {
AudioCapturerAdapter()24 AudioCapturerAdapter::AudioCapturerAdapter() { }
25
~AudioCapturerAdapter()26 AudioCapturerAdapter::~AudioCapturerAdapter() { }
27
GetInstance()28 AudioCapturerAdapter* AudioCapturerAdapter::GetInstance()
29 {
30 static AudioCapturerAdapter audioCapturerAdapter_;
31 return &audioCapturerAdapter_;
32 }
33
GetAudioCapturerById(SLuint32 id)34 AudioCapturer *AudioCapturerAdapter::GetAudioCapturerById(SLuint32 id)
35 {
36 AUDIO_INFO_LOG("AudioCapturerAdapter::GetAudioCapturerById: %{public}lu", id);
37 return captureMap_.find(id)->second;
38 }
39
EraseAudioCapturerById(SLuint32 id)40 void AudioCapturerAdapter::EraseAudioCapturerById(SLuint32 id)
41 {
42 AUDIO_INFO_LOG("AudioCapturerAdapter::EraseAudioCapturerById: %{public}lu", id);
43 captureMap_.erase(id);
44 callbackMap_.erase(id);
45 }
46
CreateAudioCapturerAdapter(SLuint32 id,SLDataSource * dataSource,SLDataSink * dataSink,AudioStreamType streamType)47 SLresult AudioCapturerAdapter::CreateAudioCapturerAdapter(SLuint32 id, SLDataSource *dataSource,
48 SLDataSink *dataSink, AudioStreamType streamType)
49 {
50 AUDIO_INFO_LOG("AudioCapturerAdapter::CreateAudioCapturerAdapter .");
51 SLDataFormat_PCM *pcmFormat = (SLDataFormat_PCM *)dataSink->pFormat;
52 AudioCapturerParams capturerParams;
53 ConvertPcmFormat(pcmFormat, &capturerParams);
54 streamType = AudioStreamType::STREAM_MUSIC;
55 AudioCapturerOptions capturerOptions;
56 capturerOptions.streamInfo.samplingRate = capturerParams.samplingRate;
57 capturerOptions.streamInfo.encoding = AudioEncodingType::ENCODING_PCM;
58 capturerOptions.streamInfo.format = capturerParams.audioSampleFormat;
59 capturerOptions.streamInfo.channels = capturerParams.audioChannel;
60 capturerOptions.capturerInfo.sourceType = SourceType::SOURCE_TYPE_MIC;
61 capturerOptions.capturerInfo.capturerFlags = 0;
62 string cachePath = "/data/storage/el2/base/haps/entry/files";
63 unique_ptr<AudioCapturer> capturerHolder = AudioCapturer::Create(capturerOptions, cachePath.c_str());
64 capturerHolder->SetParams(capturerParams);
65 AudioCapturer *capturer = capturerHolder.release();
66 AUDIO_INFO_LOG("AudioCapturerAdapter::CreateAudioCapturerAdapter ID: %{public}lu", id);
67 capturer->SetCaptureMode(CAPTURE_MODE_CALLBACK);
68 captureMap_.insert(make_pair(id, capturer));
69 return SL_RESULT_SUCCESS;
70 }
71
SetCaptureStateAdapter(SLuint32 id,SLuint32 state)72 SLresult AudioCapturerAdapter::SetCaptureStateAdapter(SLuint32 id, SLuint32 state)
73 {
74 AUDIO_INFO_LOG("AudioCapturerAdapter::SetCaptureStateAdapter state: %{public}lu.", state);
75 AudioCapturer *audioCapturer = GetAudioCapturerById(id);
76 if (audioCapturer == nullptr) {
77 AUDIO_ERR_LOG("AudioCapturerAdapter::SetCaptureStateAdapter invalid id.");
78 return SL_RESULT_RESOURCE_ERROR;
79 }
80
81 SLresult slResult = SL_RESULT_SUCCESS;
82 bool result = false;
83 bool rtStop = false;
84 bool rtRelease = false;
85 int32_t rtClear = -1;
86 switch (state) {
87 case SL_RECORDSTATE_RECORDING:
88 result = audioCapturer->Start();
89 break;
90 case SL_RECORDSTATE_PAUSED:
91 result = audioCapturer->Pause();
92 break;
93 case SL_RECORDSTATE_STOPPED: {
94 rtStop = audioCapturer->Stop();
95 rtClear = audioCapturer->Clear();
96 rtRelease = audioCapturer->Release();
97 result = rtStop && !rtClear && rtRelease;
98 break;
99 }
100 default:
101 AUDIO_ERR_LOG("AudioPlayerAdapter::play state not supported.");
102 break;
103 }
104 slResult = result ? SL_RESULT_SUCCESS : SL_RESULT_RESOURCE_ERROR;
105 return slResult;
106 }
107
GetCaptureStateAdapter(SLuint32 id,SLuint32 * state)108 SLresult AudioCapturerAdapter::GetCaptureStateAdapter(SLuint32 id, SLuint32 *state)
109 {
110 AudioCapturer *audioCapturer = GetAudioCapturerById(id);
111 if (audioCapturer == nullptr) {
112 AUDIO_ERR_LOG("AudioCapturerAdapter::GetCaptureStateAdapter invalid id.");
113 return SL_RESULT_RESOURCE_ERROR;
114 }
115
116 CapturerState capturerState = audioCapturer->GetStatus();
117 switch (capturerState) {
118 case CAPTURER_RUNNING:
119 *state = SL_RECORDSTATE_RECORDING;
120 break;
121 case CAPTURER_PAUSED:
122 *state = SL_RECORDSTATE_PAUSED;
123 break;
124 case CAPTURER_STOPPED:
125 *state = SL_RECORDSTATE_STOPPED;
126 break;
127 default:
128 *state = -1;
129 break;
130 }
131 AUDIO_INFO_LOG("AudioCapturerAdapter::GetCaptureStateAdapter state: %{public}lu.", *state);
132 return SL_RESULT_SUCCESS;
133 }
134
EnqueueAdapter(SLuint32 id,const void * buffer,SLuint32 size)135 SLresult AudioCapturerAdapter::EnqueueAdapter(SLuint32 id, const void *buffer, SLuint32 size)
136 {
137 AudioCapturer *audioCapturer = GetAudioCapturerById(id);
138 if (audioCapturer == nullptr) {
139 AUDIO_ERR_LOG("AudioCapturerAdapter::EnqueueAdapter invalid id.");
140 return SL_RESULT_RESOURCE_ERROR;
141 }
142
143 BufferDesc bufDesc = {};
144 bufDesc.buffer = (uint8_t*) buffer;
145 bufDesc.bufLength = size;
146 AUDIO_INFO_LOG("AudioCapturerAdapter::EnqueueAdapter bufferlength: %{public}zu", bufDesc.bufLength);
147 audioCapturer->Enqueue(bufDesc);
148 return SL_RESULT_SUCCESS;
149 }
150
ClearAdapter(SLuint32 id)151 SLresult AudioCapturerAdapter::ClearAdapter(SLuint32 id)
152 {
153 AudioCapturer *audioCapturer = GetAudioCapturerById(id);
154 if (audioCapturer == nullptr) {
155 AUDIO_ERR_LOG("AudioCapturerAdapter::ClearAdapter invalid id.");
156 return SL_RESULT_RESOURCE_ERROR;
157 }
158
159 audioCapturer->Clear();
160 return SL_RESULT_SUCCESS;
161 }
162
GetStateAdapter(SLuint32 id,SLOHBufferQueueState * state)163 SLresult AudioCapturerAdapter::GetStateAdapter(SLuint32 id, SLOHBufferQueueState *state)
164 {
165 AudioCapturer *audioCapturer = GetAudioCapturerById(id);
166 if (audioCapturer == nullptr) {
167 AUDIO_ERR_LOG("AudioCapturerAdapter::GetStateAdapter invalid id.");
168 return SL_RESULT_RESOURCE_ERROR;
169 }
170
171 BufferQueueState queueState = {0, 0};
172 audioCapturer->GetBufQueueState(queueState);
173 state->count = queueState.numBuffers;
174 state->index = queueState.currentIndex;
175 return SL_RESULT_SUCCESS;
176 }
177
GetBufferAdapter(SLuint32 id,SLuint8 ** buffer,SLuint32 * size)178 SLresult AudioCapturerAdapter::GetBufferAdapter(SLuint32 id, SLuint8 **buffer, SLuint32 *size)
179 {
180 AudioCapturer *audioCapturer = GetAudioCapturerById(id);
181 if (audioCapturer == nullptr) {
182 AUDIO_ERR_LOG("AudioCapturerAdapter::GetBufferAdapter invalid id.");
183 return SL_RESULT_RESOURCE_ERROR;
184 }
185
186 BufferDesc bufferDesc = {};
187 audioCapturer->GetBufferDesc(bufferDesc);
188 *buffer = bufferDesc.buffer;
189 *size = bufferDesc.bufLength;
190 return SL_RESULT_SUCCESS;
191 }
192
RegisterCallbackAdapter(SLOHBufferQueueItf itf,SlOHBufferQueueCallback callback,void * pContext)193 SLresult AudioCapturerAdapter::RegisterCallbackAdapter(SLOHBufferQueueItf itf,
194 SlOHBufferQueueCallback callback, void *pContext)
195 {
196 IOHBufferQueue *thiz = (IOHBufferQueue *)itf;
197 AudioCapturer *audioCapturer = GetAudioCapturerById(thiz->mId);
198 if (audioCapturer == nullptr) {
199 AUDIO_ERR_LOG("AudioCapturerAdapter::RegisterCallbackAdapter invalid id.");
200 return SL_RESULT_RESOURCE_ERROR;
201 }
202
203 callbackPtr_ = make_shared<ReadOrWriteCallbackAdapter>(callback, itf, pContext);
204 audioCapturer->SetCapturerReadCallback(static_pointer_cast<AudioCapturerReadCallback>(callbackPtr_));
205 callbackMap_.insert(make_pair(thiz->mId, callbackPtr_));
206 return SL_RESULT_SUCCESS;
207 }
208
ConvertPcmFormat(SLDataFormat_PCM * slFormat,AudioCapturerParams * capturerParams)209 void AudioCapturerAdapter::ConvertPcmFormat(SLDataFormat_PCM *slFormat, AudioCapturerParams *capturerParams)
210 {
211 AudioSampleFormat sampleFormat = SlToOhosSampelFormat(slFormat);
212 AudioSamplingRate sampleRate = SlToOhosSamplingRate(slFormat);
213 AudioChannel channelCount = SlToOhosChannel(slFormat);
214 capturerParams->audioSampleFormat = sampleFormat;
215 capturerParams->samplingRate = sampleRate;
216 capturerParams->audioChannel = channelCount;
217 capturerParams->audioEncoding = ENCODING_PCM;
218 }
219
SlToOhosSampelFormat(SLDataFormat_PCM * pcmFormat)220 AudioSampleFormat AudioCapturerAdapter::SlToOhosSampelFormat(SLDataFormat_PCM *pcmFormat)
221 {
222 AudioSampleFormat sampleFormat;
223 switch (pcmFormat->bitsPerSample) {
224 case SL_PCMSAMPLEFORMAT_FIXED_8:
225 sampleFormat = SAMPLE_U8;
226 break;
227 case SL_PCMSAMPLEFORMAT_FIXED_16:
228 sampleFormat = SAMPLE_S16LE;
229 break;
230 case SL_PCMSAMPLEFORMAT_FIXED_20:
231 sampleFormat = INVALID_WIDTH;
232 break;
233 case SL_PCMSAMPLEFORMAT_FIXED_24:
234 sampleFormat = SAMPLE_S24LE;
235 break;
236 case SL_PCMSAMPLEFORMAT_FIXED_28:
237 sampleFormat = INVALID_WIDTH;
238 break;
239 case SL_PCMSAMPLEFORMAT_FIXED_32:
240 sampleFormat = SAMPLE_S32LE;
241 break;
242 default:
243 sampleFormat = INVALID_WIDTH;
244 }
245 return sampleFormat;
246 }
247
SlToOhosSamplingRate(SLDataFormat_PCM * pcmFormat)248 AudioSamplingRate AudioCapturerAdapter::SlToOhosSamplingRate(SLDataFormat_PCM *pcmFormat)
249 {
250 AudioSamplingRate sampleRate;
251 switch (pcmFormat->samplesPerSec) {
252 case SL_SAMPLINGRATE_8:
253 sampleRate = SAMPLE_RATE_8000;
254 break;
255 case SL_SAMPLINGRATE_11_025:
256 sampleRate = SAMPLE_RATE_11025;
257 break;
258 case SL_SAMPLINGRATE_12:
259 sampleRate = SAMPLE_RATE_12000;
260 break;
261 case SL_SAMPLINGRATE_16:
262 sampleRate = SAMPLE_RATE_16000;
263 break;
264 case SL_SAMPLINGRATE_22_05:
265 sampleRate = SAMPLE_RATE_22050;
266 break;
267 case SL_SAMPLINGRATE_24:
268 sampleRate = SAMPLE_RATE_24000;
269 break;
270 case SL_SAMPLINGRATE_32:
271 sampleRate = SAMPLE_RATE_32000;
272 break;
273 case SL_SAMPLINGRATE_44_1:
274 sampleRate = SAMPLE_RATE_44100;
275 break;
276 case SL_SAMPLINGRATE_48:
277 sampleRate = SAMPLE_RATE_48000;
278 break;
279 case SL_SAMPLINGRATE_64:
280 sampleRate = SAMPLE_RATE_64000;
281 break;
282 case SL_SAMPLINGRATE_88_2:
283 sampleRate = SAMPLE_RATE_44100;
284 break;
285 case SL_SAMPLINGRATE_96:
286 sampleRate = SAMPLE_RATE_96000;
287 break;
288 case SL_SAMPLINGRATE_192:
289 sampleRate = SAMPLE_RATE_44100;
290 break;
291 default: {
292 AUDIO_ERR_LOG("AudioCapturerAdapter::SlToOhosSamplingRate mismatch, use default.");
293 sampleRate = SAMPLE_RATE_44100;
294 }
295 }
296 return sampleRate;
297 }
298
SlToOhosChannel(SLDataFormat_PCM * pcmFormat)299 AudioChannel AudioCapturerAdapter::SlToOhosChannel(SLDataFormat_PCM *pcmFormat)
300 {
301 AudioChannel channelCount;
302 switch (pcmFormat->numChannels) {
303 case MONO:
304 channelCount = MONO;
305 break;
306 case STEREO:
307 channelCount = STEREO;
308 break;
309 default:
310 channelCount = MONO;
311 AUDIO_ERR_LOG("AudioPlayerAdapter::channel count not supported ");
312 }
313 return channelCount;
314 }
315 }
316 }
317