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 {
AudioPlayerAdapter()24 AudioPlayerAdapter::AudioPlayerAdapter() { }
25
~AudioPlayerAdapter()26 AudioPlayerAdapter::~AudioPlayerAdapter() { }
27
GetInstance()28 AudioPlayerAdapter* AudioPlayerAdapter::GetInstance()
29 {
30 static AudioPlayerAdapter audioPlayerAdapter_;
31 return &audioPlayerAdapter_;
32 }
33
GetAudioRenderById(SLuint32 id)34 AudioRenderer* AudioPlayerAdapter::GetAudioRenderById(SLuint32 id)
35 {
36 return renderMap_.find(id)->second;
37 }
38
EraseAudioRenderById(SLuint32 id)39 void AudioPlayerAdapter::EraseAudioRenderById(SLuint32 id)
40 {
41 AUDIO_INFO_LOG("AudioPlayerAdapter::EraseAudioRenderById: %{public}lu", id);
42 AudioRenderer* pRender = GetAudioRenderById(id);
43 renderMap_.erase(id);
44 callbackMap_.erase(id);
45 if (pRender) {
46 delete pRender;
47 pRender = nullptr;
48 }
49 return;
50 }
51
CreateAudioPlayerAdapter(SLuint32 id,SLDataSource * dataSource,SLDataSink * dataSink,AudioStreamType streamType)52 SLresult AudioPlayerAdapter::CreateAudioPlayerAdapter
53 (SLuint32 id, SLDataSource *dataSource, SLDataSink *dataSink, AudioStreamType streamType)
54 {
55 SLDataFormat_PCM *pcmFormat = (SLDataFormat_PCM *)dataSource->pFormat;
56 AudioRendererParams rendererParams;
57 ConvertPcmFormat(pcmFormat, &rendererParams);
58 streamType = AudioStreamType::STREAM_MUSIC;
59 AudioRendererOptions rendererOptions;
60 rendererOptions.streamInfo.samplingRate = rendererParams.sampleRate;
61 rendererOptions.streamInfo.encoding = AudioEncodingType::ENCODING_PCM;
62 rendererOptions.streamInfo.format = rendererParams.sampleFormat;
63 rendererOptions.streamInfo.channels = rendererParams.channelCount;
64 rendererOptions.rendererInfo.contentType = ContentType::CONTENT_TYPE_MUSIC;
65 rendererOptions.rendererInfo.streamUsage = StreamUsage::STREAM_USAGE_MEDIA;
66 rendererOptions.rendererInfo.rendererFlags = RENDERER_NEW;
67 string cachePath = "/data/storage/el2/base/haps/entry/files";
68 unique_ptr<AudioRenderer> rendererHolder = AudioRenderer::Create(cachePath.c_str(), rendererOptions);
69 // use release to get raw pointer, so need to delete explicitly when destory object
70 AudioRenderer *renderer = rendererHolder.release();
71 AUDIO_INFO_LOG("AudioPlayerAdapter::CreateAudioPlayer ID: %{public}lu", id);
72 renderer->SetRenderMode(RENDER_MODE_CALLBACK);
73 renderMap_.insert(make_pair(id, renderer));
74 return SL_RESULT_SUCCESS;
75 }
76
SetPlayStateAdapter(SLuint32 id,SLuint32 state)77 SLresult AudioPlayerAdapter::SetPlayStateAdapter(SLuint32 id, SLuint32 state)
78 {
79 AudioRenderer* pRender = GetAudioRenderById(id);
80 if (pRender == nullptr) {
81 AUDIO_ERR_LOG("AudioPlayerAdapter::SetPlayStateAdapter invalid id.");
82 return SL_RESULT_RESOURCE_ERROR;
83 }
84
85 SLresult slResult = SL_RESULT_SUCCESS;
86 bool result = false;
87 bool rtStop = false;
88 bool rtRelease = false;
89 int32_t rtClear = -1;
90 switch (state) {
91 case SL_PLAYSTATE_PLAYING:
92 result = pRender->Start();
93 break;
94 case SL_PLAYSTATE_PAUSED:
95 result = pRender->Pause();
96 break;
97 case SL_PLAYSTATE_STOPPED: {
98 rtStop = pRender->Stop();
99 rtClear = pRender->Clear();
100 rtRelease = pRender->Release();
101 result = rtStop && !rtClear && rtRelease;
102 break;
103 }
104 default:
105 AUDIO_ERR_LOG("AudioPlayerAdapter::play state not supported ");
106 break;
107 }
108 slResult = result ? SL_RESULT_SUCCESS : SL_RESULT_RESOURCE_ERROR;
109 return slResult;
110 }
111
GetPlayStateAdapter(SLuint32 id,SLuint32 * state)112 SLresult AudioPlayerAdapter::GetPlayStateAdapter(SLuint32 id, SLuint32 *state)
113 {
114 AudioRenderer* pRender = GetAudioRenderById(id);
115 if (pRender == nullptr) {
116 AUDIO_ERR_LOG("AudioPlayerAdapter::GetPlayStateAdapter invalid id.");
117 return SL_RESULT_RESOURCE_ERROR;
118 }
119
120 RendererState rendererState = pRender->GetStatus();
121 switch (rendererState) {
122 case RENDERER_RUNNING:
123 *state = SL_PLAYSTATE_PLAYING;
124 break;
125 case RENDERER_PAUSED:
126 *state = SL_PLAYSTATE_PAUSED;
127 break;
128 case RENDERER_STOPPED:
129 *state = SL_PLAYSTATE_STOPPED;
130 break;
131 default:
132 *state = -1;
133 break;
134 }
135 return SL_RESULT_SUCCESS;
136 }
137
SetVolumeLevelAdapter(SLuint32 id,SLmillibel level)138 SLresult AudioPlayerAdapter::SetVolumeLevelAdapter(SLuint32 id, SLmillibel level)
139 {
140 AudioRenderer *audioRenderer = GetAudioRenderById(id);
141 if (audioRenderer == nullptr) {
142 AUDIO_ERR_LOG("AudioPlayerAdapter::SetVolumeLevelAdapter invalid id.");
143 return SL_RESULT_RESOURCE_ERROR;
144 }
145
146 int base = 10;
147 float volume = pow(base, level / MAGNIFICATION);
148 float volumeMaxLevel = 15;
149 audioRenderer->SetVolume(volume / volumeMaxLevel);
150 return SL_RESULT_SUCCESS;
151 }
152
GetVolumeLevelAdapter(SLuint32 id,SLmillibel * level)153 SLresult AudioPlayerAdapter::GetVolumeLevelAdapter(SLuint32 id, SLmillibel *level)
154 {
155 AudioRenderer *audioRenderer = GetAudioRenderById(id);
156 if (audioRenderer == nullptr) {
157 AUDIO_ERR_LOG("AudioPlayerAdapter::GetVolumeLevelAdapter invalid id.");
158 return SL_RESULT_RESOURCE_ERROR;
159 }
160
161 float volume = audioRenderer->GetVolume();
162 *level = (SLmillibel) (MAGNIFICATION * log10(volume));
163 return SL_RESULT_SUCCESS;
164 }
165
GetMaxVolumeLevelAdapter(SLuint32 id,SLmillibel * level)166 SLresult AudioPlayerAdapter::GetMaxVolumeLevelAdapter(SLuint32 id, SLmillibel *level)
167 {
168 float volume = AudioSystemManager::GetInstance()->GetMaxVolume(STREAM_MUSIC);
169 *level = (SLmillibel) (MAGNIFICATION * log10(volume));
170 return SL_RESULT_SUCCESS;
171 }
172
EnqueueAdapter(SLuint32 id,const void * buffer,SLuint32 size)173 SLresult AudioPlayerAdapter::EnqueueAdapter(SLuint32 id, const void *buffer, SLuint32 size)
174 {
175 AudioRenderer *audioRenderer = GetAudioRenderById(id);
176 if (audioRenderer == nullptr) {
177 AUDIO_ERR_LOG("AudioPlayerAdapter::EnqueueAdapter invalid id.");
178 return SL_RESULT_RESOURCE_ERROR;
179 }
180
181 BufferDesc bufDesc = {};
182 bufDesc.buffer = (uint8_t*) buffer;
183 bufDesc.bufLength = size;
184 audioRenderer->Enqueue(bufDesc);
185 return SL_RESULT_SUCCESS;
186 }
187
ClearAdapter(SLuint32 id)188 SLresult AudioPlayerAdapter::ClearAdapter(SLuint32 id)
189 {
190 AudioRenderer *audioRenderer = GetAudioRenderById(id);
191 if (audioRenderer == nullptr) {
192 AUDIO_ERR_LOG("AudioPlayerAdapter::ClearAdapter invalid id.");
193 return SL_RESULT_RESOURCE_ERROR;
194 }
195
196 audioRenderer->Clear();
197 return SL_RESULT_SUCCESS;
198 }
199
GetStateAdapter(SLuint32 id,SLOHBufferQueueState * state)200 SLresult AudioPlayerAdapter::GetStateAdapter(SLuint32 id, SLOHBufferQueueState *state)
201 {
202 AudioRenderer *audioRenderer = GetAudioRenderById(id);
203 if (audioRenderer == nullptr) {
204 AUDIO_ERR_LOG("AudioPlayerAdapter::GetStateAdapter invalid id.");
205 return SL_RESULT_RESOURCE_ERROR;
206 }
207
208 BufferQueueState queueState = {0, 0};
209 audioRenderer->GetBufQueueState(queueState);
210 state->count = queueState.numBuffers;
211 state->index = queueState.currentIndex;
212 return SL_RESULT_SUCCESS;
213 }
214
GetBufferAdapter(SLuint32 id,SLuint8 ** buffer,SLuint32 * size)215 SLresult AudioPlayerAdapter::GetBufferAdapter(SLuint32 id, SLuint8 **buffer, SLuint32 *size)
216 {
217 AudioRenderer *audioRenderer = GetAudioRenderById(id);
218 if (audioRenderer == nullptr) {
219 AUDIO_ERR_LOG("AudioPlayerAdapter::GetBufferAdapter invalid id.");
220 return SL_RESULT_RESOURCE_ERROR;
221 }
222
223 BufferDesc bufferDesc = {};
224 audioRenderer->GetBufferDesc(bufferDesc);
225 *buffer = bufferDesc.buffer;
226 *size = bufferDesc.bufLength;
227 return SL_RESULT_SUCCESS;
228 }
229
RegisterCallbackAdapter(SLOHBufferQueueItf itf,SlOHBufferQueueCallback callback,void * pContext)230 SLresult AudioPlayerAdapter::RegisterCallbackAdapter
231 (SLOHBufferQueueItf itf, SlOHBufferQueueCallback callback, void *pContext)
232 {
233 IOHBufferQueue *thiz = (IOHBufferQueue *)itf;
234 AudioRenderer *audioRenderer = GetAudioRenderById(thiz->mId);
235 if (audioRenderer == nullptr) {
236 AUDIO_ERR_LOG("AudioPlayerAdapter::RegisterCallbackAdapter invalid id.");
237 return SL_RESULT_RESOURCE_ERROR;
238 }
239
240 callbackPtr_ = make_shared<ReadOrWriteCallbackAdapter>(callback, itf, pContext);
241 audioRenderer->SetRendererWriteCallback(static_pointer_cast<AudioRendererWriteCallback>(callbackPtr_));
242 callbackMap_.insert(make_pair(thiz->mId, callbackPtr_));
243 return SL_RESULT_SUCCESS;
244 }
245
ConvertPcmFormat(SLDataFormat_PCM * slFormat,AudioRendererParams * rendererParams)246 void AudioPlayerAdapter::ConvertPcmFormat(SLDataFormat_PCM *slFormat, AudioRendererParams *rendererParams)
247 {
248 AudioSampleFormat sampleFormat = SlToOhosSampelFormat(slFormat);
249 AudioSamplingRate sampleRate = SlToOhosSamplingRate(slFormat);
250 AudioChannel channelCount = SlToOhosChannel(slFormat);
251 rendererParams->sampleFormat = sampleFormat;
252 rendererParams->sampleRate = sampleRate;
253 rendererParams->channelCount = channelCount;
254 rendererParams->encodingType = ENCODING_PCM;
255 }
256
SlToOhosSampelFormat(SLDataFormat_PCM * pcmFormat)257 AudioSampleFormat AudioPlayerAdapter::SlToOhosSampelFormat(SLDataFormat_PCM *pcmFormat)
258 {
259 AudioSampleFormat sampleFormat;
260 switch (pcmFormat->bitsPerSample) {
261 case SL_PCMSAMPLEFORMAT_FIXED_8:
262 sampleFormat = SAMPLE_U8;
263 break;
264 case SL_PCMSAMPLEFORMAT_FIXED_16:
265 sampleFormat = SAMPLE_S16LE;
266 break;
267 case SL_PCMSAMPLEFORMAT_FIXED_20:
268 sampleFormat = INVALID_WIDTH;
269 break;
270 case SL_PCMSAMPLEFORMAT_FIXED_24:
271 sampleFormat = SAMPLE_S24LE;
272 break;
273 case SL_PCMSAMPLEFORMAT_FIXED_28:
274 sampleFormat = INVALID_WIDTH;
275 break;
276 case SL_PCMSAMPLEFORMAT_FIXED_32:
277 sampleFormat = SAMPLE_S32LE;
278 break;
279 default:
280 sampleFormat = INVALID_WIDTH;
281 }
282 return sampleFormat;
283 }
284
SlToOhosSamplingRate(SLDataFormat_PCM * pcmFormat)285 AudioSamplingRate AudioPlayerAdapter::SlToOhosSamplingRate(SLDataFormat_PCM *pcmFormat)
286 {
287 AudioSamplingRate sampleRate;
288 switch (pcmFormat->samplesPerSec) {
289 case SL_SAMPLINGRATE_8:
290 sampleRate = SAMPLE_RATE_8000;
291 break;
292 case SL_SAMPLINGRATE_11_025:
293 sampleRate = SAMPLE_RATE_11025;
294 break;
295 case SL_SAMPLINGRATE_12:
296 sampleRate = SAMPLE_RATE_12000;
297 break;
298 case SL_SAMPLINGRATE_16:
299 sampleRate = SAMPLE_RATE_16000;
300 break;
301 case SL_SAMPLINGRATE_22_05:
302 sampleRate = SAMPLE_RATE_22050;
303 break;
304 case SL_SAMPLINGRATE_24:
305 sampleRate = SAMPLE_RATE_24000;
306 break;
307 case SL_SAMPLINGRATE_32:
308 sampleRate = SAMPLE_RATE_32000;
309 break;
310 case SL_SAMPLINGRATE_44_1:
311 sampleRate = SAMPLE_RATE_44100;
312 break;
313 case SL_SAMPLINGRATE_48:
314 sampleRate = SAMPLE_RATE_48000;
315 break;
316 case SL_SAMPLINGRATE_64:
317 sampleRate = SAMPLE_RATE_64000;
318 break;
319 case SL_SAMPLINGRATE_88_2:
320 sampleRate = SAMPLE_RATE_44100;
321 break;
322 case SL_SAMPLINGRATE_96:
323 sampleRate = SAMPLE_RATE_96000;
324 break;
325 case SL_SAMPLINGRATE_192:
326 sampleRate = SAMPLE_RATE_44100;
327 break;
328 default:
329 sampleRate = SAMPLE_RATE_44100;
330 }
331 return sampleRate;
332 }
333
SlToOhosChannel(SLDataFormat_PCM * pcmFormat)334 AudioChannel AudioPlayerAdapter::SlToOhosChannel(SLDataFormat_PCM *pcmFormat)
335 {
336 AudioChannel channelCount;
337 switch (pcmFormat->numChannels) {
338 case MONO:
339 channelCount = MONO;
340 break;
341 case STEREO:
342 channelCount = STEREO;
343 break;
344 default:
345 channelCount = MONO;
346 AUDIO_ERR_LOG("AudioPlayerAdapter::channel count not supported ");
347 }
348 return channelCount;
349 }
350 } // namespace AudioStandard
351 } // namespace OHOS
352