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