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 #ifndef LOG_TAG
16 #define LOG_TAG "AudioCapturerAdapter"
17 #endif
18
19 #include <common.h>
20
21 using namespace std;
22 using namespace OHOS;
23 using namespace OHOS::AudioStandard;
24
25 namespace OHOS {
26 namespace AudioStandard {
AudioCapturerAdapter()27 AudioCapturerAdapter::AudioCapturerAdapter() { }
28
~AudioCapturerAdapter()29 AudioCapturerAdapter::~AudioCapturerAdapter() { }
30
GetInstance()31 AudioCapturerAdapter* AudioCapturerAdapter::GetInstance()
32 {
33 static AudioCapturerAdapter audioCapturerAdapter_;
34 return &audioCapturerAdapter_;
35 }
36
GetAudioCapturerById(SLuint32 id)37 shared_ptr<AudioCapturer> AudioCapturerAdapter::GetAudioCapturerById(SLuint32 id)
38 {
39 AUDIO_INFO_LOG("id: %{public}lu", id);
40 auto it = captureMap_.find(id);
41 if (it == captureMap_.end()) {
42 AUDIO_ERR_LOG("GetAudioCapturerById: %{public}lu not found", id);
43 return nullptr;
44 }
45 return captureMap_.find(id)->second;
46 }
47
EraseAudioCapturerById(SLuint32 id)48 void AudioCapturerAdapter::EraseAudioCapturerById(SLuint32 id)
49 {
50 AUDIO_INFO_LOG("id: %{public}lu", id);
51 captureMap_.erase(id);
52 callbackMap_.erase(id);
53 }
54
CreateAudioCapturerAdapter(SLuint32 id,SLDataSource * dataSource,SLDataSink * dataSink,AudioStreamType streamType)55 SLresult AudioCapturerAdapter::CreateAudioCapturerAdapter(SLuint32 id, SLDataSource *dataSource,
56 SLDataSink *dataSink, AudioStreamType streamType)
57 {
58 AUDIO_INFO_LOG("in");
59 SLDataFormat_PCM *pcmFormat = (SLDataFormat_PCM *)dataSink->pFormat;
60 AudioCapturerParams capturerParams;
61 ConvertPcmFormat(pcmFormat, &capturerParams);
62 streamType = AudioStreamType::STREAM_MUSIC;
63 AudioCapturerOptions capturerOptions;
64 capturerOptions.streamInfo.samplingRate = capturerParams.samplingRate;
65 capturerOptions.streamInfo.encoding = AudioEncodingType::ENCODING_PCM;
66 capturerOptions.streamInfo.format = capturerParams.audioSampleFormat;
67 capturerOptions.streamInfo.channels = capturerParams.audioChannel;
68 capturerOptions.capturerInfo.sourceType = SourceType::SOURCE_TYPE_MIC;
69 capturerOptions.capturerInfo.capturerFlags = 0;
70 capturerOptions.capturerInfo.recorderType = RECORDER_TYPE_OPENSL_ES;
71 shared_ptr<AudioCapturer> capturerHolder = AudioCapturer::CreateCapturer(capturerOptions);
72 CHECK_AND_RETURN_RET_LOG(capturerHolder, SL_RESULT_RESOURCE_ERROR,
73 "CreateAudioCapturerAdapter fail, ID: %{public}lu", id);
74 capturerHolder->SetParams(capturerParams);
75 AUDIO_INFO_LOG("CreateAudioCapturerAdapter ID: %{public}lu", id);
76 capturerHolder->SetCaptureMode(CAPTURE_MODE_CALLBACK);
77 captureMap_.insert(make_pair(id, capturerHolder));
78 return SL_RESULT_SUCCESS;
79 }
80
SetCaptureStateAdapter(SLuint32 id,SLuint32 state)81 SLresult AudioCapturerAdapter::SetCaptureStateAdapter(SLuint32 id, SLuint32 state)
82 {
83 AUDIO_INFO_LOG("state: %{public}lu.", state);
84 shared_ptr<AudioCapturer> audioCapturer = GetAudioCapturerById(id);
85 CHECK_AND_RETURN_RET_LOG(audioCapturer != nullptr, SL_RESULT_RESOURCE_ERROR,
86 "invalid id.");
87
88 SLresult slResult = SL_RESULT_SUCCESS;
89 bool result = false;
90 bool rtStop = false;
91 bool rtRelease = false;
92 int32_t rtClear = -1;
93 switch (state) {
94 case SL_RECORDSTATE_RECORDING:
95 result = audioCapturer->Start();
96 break;
97 case SL_RECORDSTATE_PAUSED:
98 result = audioCapturer->Pause();
99 break;
100 case SL_RECORDSTATE_STOPPED: {
101 rtStop = audioCapturer->Stop();
102 rtClear = audioCapturer->Clear();
103 rtRelease = audioCapturer->Release();
104 result = rtStop && !rtClear && rtRelease;
105 break;
106 }
107 default:
108 AUDIO_ERR_LOG("AudioPlayerAdapter::play state not supported.");
109 break;
110 }
111 slResult = result ? SL_RESULT_SUCCESS : SL_RESULT_RESOURCE_ERROR;
112 return slResult;
113 }
114
GetCaptureStateAdapter(SLuint32 id,SLuint32 * state)115 SLresult AudioCapturerAdapter::GetCaptureStateAdapter(SLuint32 id, SLuint32 *state)
116 {
117 shared_ptr<AudioCapturer> audioCapturer = GetAudioCapturerById(id);
118 CHECK_AND_RETURN_RET_LOG(audioCapturer != nullptr, SL_RESULT_RESOURCE_ERROR,
119 "invalid id.");
120
121 CapturerState capturerState = audioCapturer->GetStatus();
122 switch (capturerState) {
123 case CAPTURER_RUNNING:
124 *state = SL_RECORDSTATE_RECORDING;
125 break;
126 case CAPTURER_PAUSED:
127 *state = SL_RECORDSTATE_PAUSED;
128 break;
129 case CAPTURER_STOPPED:
130 *state = SL_RECORDSTATE_STOPPED;
131 break;
132 default:
133 *state = -1;
134 break;
135 }
136 AUDIO_INFO_LOG("state: %{public}lu.", *state);
137 return SL_RESULT_SUCCESS;
138 }
139
EnqueueAdapter(SLuint32 id,const void * buffer,SLuint32 size)140 SLresult AudioCapturerAdapter::EnqueueAdapter(SLuint32 id, const void *buffer, SLuint32 size)
141 {
142 shared_ptr<AudioCapturer> audioCapturer = GetAudioCapturerById(id);
143 CHECK_AND_RETURN_RET_LOG(audioCapturer != nullptr, SL_RESULT_RESOURCE_ERROR,
144 "invalid id.");
145
146 BufferDesc bufDesc = {};
147 bufDesc.buffer = (uint8_t*) buffer;
148 bufDesc.bufLength = size;
149 bufDesc.dataLength = size;
150 AUDIO_INFO_LOG("bufferlength: %{public}zu", bufDesc.bufLength);
151 audioCapturer->Enqueue(bufDesc);
152 return SL_RESULT_SUCCESS;
153 }
154
ClearAdapter(SLuint32 id)155 SLresult AudioCapturerAdapter::ClearAdapter(SLuint32 id)
156 {
157 shared_ptr<AudioCapturer> audioCapturer = GetAudioCapturerById(id);
158 CHECK_AND_RETURN_RET_LOG(audioCapturer != nullptr, SL_RESULT_RESOURCE_ERROR,
159 "invalid id.");
160
161 audioCapturer->Clear();
162 return SL_RESULT_SUCCESS;
163 }
164
GetStateAdapter(SLuint32 id,SLOHBufferQueueState * state)165 SLresult AudioCapturerAdapter::GetStateAdapter(SLuint32 id, SLOHBufferQueueState *state)
166 {
167 shared_ptr<AudioCapturer> audioCapturer = GetAudioCapturerById(id);
168 CHECK_AND_RETURN_RET_LOG(audioCapturer != nullptr, SL_RESULT_RESOURCE_ERROR,
169 "invalid id.");
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 shared_ptr<AudioCapturer> audioCapturer = GetAudioCapturerById(id);
181 CHECK_AND_RETURN_RET_LOG(audioCapturer != nullptr, SL_RESULT_RESOURCE_ERROR,
182 "invalid id.");
183
184 BufferDesc bufferDesc = {};
185 audioCapturer->GetBufferDesc(bufferDesc);
186 *buffer = bufferDesc.buffer;
187 *size = bufferDesc.bufLength;
188 return SL_RESULT_SUCCESS;
189 }
190
RegisterCallbackAdapter(SLOHBufferQueueItf itf,SlOHBufferQueueCallback callback,void * pContext)191 SLresult AudioCapturerAdapter::RegisterCallbackAdapter(SLOHBufferQueueItf itf,
192 SlOHBufferQueueCallback callback, void *pContext)
193 {
194 IOHBufferQueue *thiz = (IOHBufferQueue *)itf;
195 shared_ptr<AudioCapturer> audioCapturer = GetAudioCapturerById(thiz->mId);
196 CHECK_AND_RETURN_RET_LOG(audioCapturer != nullptr, SL_RESULT_RESOURCE_ERROR,
197 "invalid id.");
198
199 callbackPtr_ = make_shared<ReadOrWriteCallbackAdapter>(callback, itf, pContext);
200 audioCapturer->SetCapturerReadCallback(static_pointer_cast<AudioCapturerReadCallback>(callbackPtr_));
201 callbackMap_.insert(make_pair(thiz->mId, callbackPtr_));
202 return SL_RESULT_SUCCESS;
203 }
204
ConvertPcmFormat(SLDataFormat_PCM * slFormat,AudioCapturerParams * capturerParams)205 void AudioCapturerAdapter::ConvertPcmFormat(SLDataFormat_PCM *slFormat, AudioCapturerParams *capturerParams)
206 {
207 AudioSampleFormat sampleFormat = SlToOhosSampelFormat(slFormat);
208 AudioSamplingRate sampleRate = SlToOhosSamplingRate(slFormat);
209 AudioChannel channelCount = SlToOhosChannel(slFormat);
210 capturerParams->audioSampleFormat = sampleFormat;
211 capturerParams->samplingRate = sampleRate;
212 capturerParams->audioChannel = channelCount;
213 capturerParams->audioEncoding = ENCODING_PCM;
214 }
215
SlToOhosSampelFormat(SLDataFormat_PCM * pcmFormat)216 AudioSampleFormat AudioCapturerAdapter::SlToOhosSampelFormat(SLDataFormat_PCM *pcmFormat)
217 {
218 AudioSampleFormat sampleFormat;
219 switch (pcmFormat->bitsPerSample) {
220 case SL_PCMSAMPLEFORMAT_FIXED_8:
221 sampleFormat = SAMPLE_U8;
222 break;
223 case SL_PCMSAMPLEFORMAT_FIXED_16:
224 sampleFormat = SAMPLE_S16LE;
225 break;
226 case SL_PCMSAMPLEFORMAT_FIXED_20:
227 sampleFormat = INVALID_WIDTH;
228 break;
229 case SL_PCMSAMPLEFORMAT_FIXED_24:
230 sampleFormat = SAMPLE_S24LE;
231 break;
232 case SL_PCMSAMPLEFORMAT_FIXED_28:
233 sampleFormat = INVALID_WIDTH;
234 break;
235 case SL_PCMSAMPLEFORMAT_FIXED_32:
236 sampleFormat = SAMPLE_S32LE;
237 break;
238 default:
239 sampleFormat = INVALID_WIDTH;
240 }
241 return sampleFormat;
242 }
243
SlToOhosSamplingRate(SLDataFormat_PCM * pcmFormat)244 AudioSamplingRate AudioCapturerAdapter::SlToOhosSamplingRate(SLDataFormat_PCM *pcmFormat)
245 {
246 AudioSamplingRate sampleRate;
247 switch (pcmFormat->samplesPerSec) {
248 case SL_SAMPLINGRATE_8:
249 sampleRate = SAMPLE_RATE_8000;
250 break;
251 case SL_SAMPLINGRATE_11_025:
252 sampleRate = SAMPLE_RATE_11025;
253 break;
254 case SL_SAMPLINGRATE_12:
255 sampleRate = SAMPLE_RATE_12000;
256 break;
257 case SL_SAMPLINGRATE_16:
258 sampleRate = SAMPLE_RATE_16000;
259 break;
260 case SL_SAMPLINGRATE_22_05:
261 sampleRate = SAMPLE_RATE_22050;
262 break;
263 case SL_SAMPLINGRATE_24:
264 sampleRate = SAMPLE_RATE_24000;
265 break;
266 case SL_SAMPLINGRATE_32:
267 sampleRate = SAMPLE_RATE_32000;
268 break;
269 case SL_SAMPLINGRATE_44_1:
270 sampleRate = SAMPLE_RATE_44100;
271 break;
272 case SL_SAMPLINGRATE_48:
273 sampleRate = SAMPLE_RATE_48000;
274 break;
275 case SL_SAMPLINGRATE_64:
276 sampleRate = SAMPLE_RATE_64000;
277 break;
278 case SL_SAMPLINGRATE_88_2:
279 sampleRate = SAMPLE_RATE_44100;
280 break;
281 case SL_SAMPLINGRATE_96:
282 sampleRate = SAMPLE_RATE_96000;
283 break;
284 case SL_SAMPLINGRATE_192:
285 sampleRate = SAMPLE_RATE_44100;
286 break;
287 default: {
288 AUDIO_ERR_LOG("AudioCapturerAdapter::SlToOhosSamplingRate mismatch, use default.");
289 sampleRate = SAMPLE_RATE_44100;
290 }
291 }
292 return sampleRate;
293 }
294
SlToOhosChannel(SLDataFormat_PCM * pcmFormat)295 AudioChannel AudioCapturerAdapter::SlToOhosChannel(SLDataFormat_PCM *pcmFormat)
296 {
297 AudioChannel channelCount;
298 switch (pcmFormat->numChannels) {
299 case MONO:
300 channelCount = MONO;
301 break;
302 case STEREO:
303 channelCount = STEREO;
304 break;
305 default:
306 channelCount = MONO;
307 AUDIO_ERR_LOG("AudioPlayerAdapter::channel count not supported ");
308 }
309 return channelCount;
310 }
311 }
312 }
313