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