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 MEDIA_INFO_LOG("AudioPlayerAdapter::GetAudioRenderById: %{public}lu", id);
37 return renderMap_.find(id)->second;
38 }
39
EraseAudioRenderById(SLuint32 id)40 void AudioPlayerAdapter::EraseAudioRenderById(SLuint32 id)
41 {
42 MEDIA_INFO_LOG("AudioPlayerAdapter::EraseAudioRenderById: %{public}lu", id);
43 renderMap_.erase(id);
44 callbackMap_.erase(id);
45 return;
46 }
47
CreateAudioPlayerAdapter(SLuint32 id,SLDataSource * dataSource,SLDataSink * dataSink,AudioStreamType streamType)48 SLresult AudioPlayerAdapter::CreateAudioPlayerAdapter
49 (SLuint32 id, SLDataSource *dataSource, SLDataSink *dataSink, AudioStreamType streamType)
50 {
51 SLDataFormat_PCM *pcmFormat = (SLDataFormat_PCM *)dataSource->pFormat;
52 AudioRendererParams rendererParams;
53 ConvertPcmFormat(pcmFormat, &rendererParams);
54 streamType = AudioStreamType::STREAM_MUSIC;
55 unique_ptr<AudioRenderer> rendererHolder = AudioRenderer::Create(streamType);
56 rendererHolder->SetParams(rendererParams);
57 AudioRenderer *renderer = rendererHolder.release();
58 MEDIA_INFO_LOG("AudioPlayerAdapter::CreateAudioPlayer ID: %{public}lu", id);
59 renderer->SetRenderMode(RENDER_MODE_CALLBACK);
60 renderMap_.insert(make_pair(id, renderer));
61 return SL_RESULT_SUCCESS;
62 }
63
SetPlayStateAdapter(SLuint32 id,SLuint32 state)64 SLresult AudioPlayerAdapter::SetPlayStateAdapter(SLuint32 id, SLuint32 state)
65 {
66 AudioRenderer* pRender = GetAudioRenderById(id);
67 SLresult slResult = SL_RESULT_SUCCESS;
68 bool reseult = false;
69 switch (state) {
70 case SL_PLAYSTATE_PLAYING:
71 reseult = pRender->Start();
72 break;
73 case SL_PLAYSTATE_PAUSED:
74 reseult = pRender->Pause();
75 break;
76 case SL_PLAYSTATE_STOPPED: {
77 reseult = pRender->Clear();
78 reseult = reseult & pRender->Stop();
79 reseult = reseult & pRender->Release();
80 break;
81 }
82 default:
83 MEDIA_ERR_LOG("AudioPlayerAdapter::play state not supported ");
84 break;
85 }
86 return slResult;
87 }
88
GetPlayStateAdapter(SLuint32 id,SLuint32 * state)89 SLresult AudioPlayerAdapter::GetPlayStateAdapter(SLuint32 id, SLuint32 *state)
90 {
91 AudioRenderer* pRender = GetAudioRenderById(id);
92 RendererState rendererState = pRender->GetStatus();
93 switch (rendererState) {
94 case RENDERER_RUNNING:
95 *state = SL_PLAYSTATE_PLAYING;
96 break;
97 case RENDERER_PAUSED:
98 *state = SL_PLAYSTATE_PAUSED;
99 break;
100 case RENDERER_STOPPED:
101 *state = SL_PLAYSTATE_STOPPED;
102 break;
103 default:
104 *state = -1;
105 break;
106 }
107 return SL_RESULT_SUCCESS;
108 }
109
SetVolumeLevelAdapter(SLuint32 id,SLmillibel level)110 SLresult AudioPlayerAdapter::SetVolumeLevelAdapter(SLuint32 id, SLmillibel level)
111 {
112 AudioRenderer *audioRenderer = GetAudioRenderById(id);
113 int base = 10;
114 float volume = pow(base, level / MAGNIFICATION);
115 float volumeMaxLevel = 15;
116 audioRenderer->SetVolume(volume / volumeMaxLevel);
117 return SL_RESULT_SUCCESS;
118 }
119
GetVolumeLevelAdapter(SLuint32 id,SLmillibel * level)120 SLresult AudioPlayerAdapter::GetVolumeLevelAdapter(SLuint32 id, SLmillibel *level)
121 {
122 AudioRenderer *audioRenderer = GetAudioRenderById(id);
123 float volume = audioRenderer->GetVolume();
124 *level = (SLmillibel) (MAGNIFICATION * log10(volume));
125 return SL_RESULT_SUCCESS;
126 }
127
GetMaxVolumeLevelAdapter(SLuint32 id,SLmillibel * level)128 SLresult AudioPlayerAdapter::GetMaxVolumeLevelAdapter(SLuint32 id, SLmillibel *level)
129 {
130 float volume = AudioSystemManager::GetInstance()->GetMaxVolume(AudioSystemManager::STREAM_MUSIC);
131 *level = (SLmillibel) (MAGNIFICATION * log10(volume));
132 return SL_RESULT_SUCCESS;
133 }
134
EnqueueAdapter(SLuint32 id,const void * buffer,SLuint32 size)135 SLresult AudioPlayerAdapter::EnqueueAdapter(SLuint32 id, const void *buffer, SLuint32 size)
136 {
137 AudioRenderer *audioRenderer = GetAudioRenderById(id);
138 BufferDesc bufDesc = {};
139 bufDesc.buffer = (uint8_t*) buffer;
140 bufDesc.dataLength = size;
141 audioRenderer->Enqueue(bufDesc);
142 return SL_RESULT_SUCCESS;
143 }
144
ClearAdapter(SLuint32 id)145 SLresult AudioPlayerAdapter::ClearAdapter(SLuint32 id)
146 {
147 AudioRenderer *audioRenderer = GetAudioRenderById(id);
148 audioRenderer->Clear();
149 return SL_RESULT_SUCCESS;
150 }
151
GetStateAdapter(SLuint32 id,SLOHBufferQueueState * state)152 SLresult AudioPlayerAdapter::GetStateAdapter(SLuint32 id, SLOHBufferQueueState *state)
153 {
154 AudioRenderer *audioRenderer = GetAudioRenderById(id);
155 BufferQueueState queueState = {0, 0};
156 audioRenderer->GetBufQueueState(queueState);
157 state->count = queueState.numBuffers;
158 state->playIndex = queueState.currentIndex;
159 return SL_RESULT_SUCCESS;
160 }
161
GetBufferAdapter(SLuint32 id,SLuint8 ** buffer,SLuint32 & size)162 SLresult AudioPlayerAdapter::GetBufferAdapter(SLuint32 id, SLuint8 **buffer, SLuint32 &size)
163 {
164 AudioRenderer *audioRenderer = GetAudioRenderById(id);
165 BufferDesc bufferDesc = {};
166 audioRenderer->GetBufferDesc(bufferDesc);
167 *buffer = bufferDesc.buffer;
168 size = bufferDesc.bufLength;
169 return SL_RESULT_SUCCESS;
170 }
171
RegisterCallbackAdapter(SLOHBufferQueueItf itf,SlOHBufferQueueCallback callback,void * pContext)172 SLresult AudioPlayerAdapter::RegisterCallbackAdapter
173 (SLOHBufferQueueItf itf, SlOHBufferQueueCallback callback, void *pContext)
174 {
175 IOHBufferQueue *thiz = (IOHBufferQueue *)itf;
176 AudioRenderer *audioRenderer = GetAudioRenderById(thiz->mId);
177 callbackPtr_ = make_shared<WriteCallbackAdapter>(callback, itf, pContext);
178 audioRenderer->SetRendererWriteCallback(static_pointer_cast<AudioRendererWriteCallback>(callbackPtr_));
179 callbackMap_.insert(make_pair(thiz->mId, callbackPtr_));
180 return SL_RESULT_SUCCESS;
181 }
182
ConvertPcmFormat(SLDataFormat_PCM * slFormat,AudioRendererParams * rendererParams)183 void AudioPlayerAdapter::ConvertPcmFormat(SLDataFormat_PCM *slFormat, AudioRendererParams *rendererParams)
184 {
185 AudioSampleFormat sampleFormat = SlToOhosSampelFormat(slFormat);
186 AudioSamplingRate sampleRate = SlToOhosSamplingRate(slFormat);
187 AudioChannel channelCount = SlToOhosChannel(slFormat);
188 rendererParams->sampleFormat = sampleFormat;
189 rendererParams->sampleRate = sampleRate;
190 rendererParams->channelCount = channelCount;
191 rendererParams->encodingType = ENCODING_PCM;
192 }
193
SlToOhosSampelFormat(SLDataFormat_PCM * pcmFormat)194 AudioSampleFormat AudioPlayerAdapter::SlToOhosSampelFormat(SLDataFormat_PCM *pcmFormat)
195 {
196 AudioSampleFormat sampleFormat;
197 switch (pcmFormat->bitsPerSample) {
198 case SL_PCMSAMPLEFORMAT_FIXED_8:
199 sampleFormat = SAMPLE_U8;
200 break;
201 case SL_PCMSAMPLEFORMAT_FIXED_16:
202 sampleFormat = SAMPLE_S16LE;
203 break;
204 case SL_PCMSAMPLEFORMAT_FIXED_20:
205 sampleFormat = INVALID_WIDTH;
206 break;
207 case SL_PCMSAMPLEFORMAT_FIXED_24:
208 sampleFormat = SAMPLE_S24LE;
209 break;
210 case SL_PCMSAMPLEFORMAT_FIXED_28:
211 sampleFormat = INVALID_WIDTH;
212 break;
213 case SL_PCMSAMPLEFORMAT_FIXED_32:
214 sampleFormat = SAMPLE_S32LE;
215 break;
216 default:
217 sampleFormat = INVALID_WIDTH;
218 }
219 return sampleFormat;
220 }
221
SlToOhosSamplingRate(SLDataFormat_PCM * pcmFormat)222 AudioSamplingRate AudioPlayerAdapter::SlToOhosSamplingRate(SLDataFormat_PCM *pcmFormat)
223 {
224 AudioSamplingRate sampleRate;
225 switch (pcmFormat->samplesPerSec) {
226 case SL_SAMPLINGRATE_8:
227 sampleRate = SAMPLE_RATE_8000;
228 break;
229 case SL_SAMPLINGRATE_11_025:
230 sampleRate = SAMPLE_RATE_11025;
231 break;
232 case SL_SAMPLINGRATE_12:
233 sampleRate = SAMPLE_RATE_12000;
234 break;
235 case SL_SAMPLINGRATE_16:
236 sampleRate = SAMPLE_RATE_16000;
237 break;
238 case SL_SAMPLINGRATE_22_05:
239 sampleRate = SAMPLE_RATE_22050;
240 break;
241 case SL_SAMPLINGRATE_24:
242 sampleRate = SAMPLE_RATE_24000;
243 break;
244 case SL_SAMPLINGRATE_32:
245 sampleRate = SAMPLE_RATE_32000;
246 break;
247 case SL_SAMPLINGRATE_44_1:
248 sampleRate = SAMPLE_RATE_44100;
249 break;
250 case SL_SAMPLINGRATE_48:
251 sampleRate = SAMPLE_RATE_48000;
252 break;
253 case SL_SAMPLINGRATE_64:
254 sampleRate = SAMPLE_RATE_64000;
255 break;
256 case SL_SAMPLINGRATE_88_2:
257 sampleRate = SAMPLE_RATE_44100;
258 break;
259 case SL_SAMPLINGRATE_96:
260 sampleRate = SAMPLE_RATE_96000;
261 break;
262 case SL_SAMPLINGRATE_192:
263 sampleRate = SAMPLE_RATE_44100;
264 break;
265 default:
266 sampleRate = SAMPLE_RATE_44100;
267 }
268 return sampleRate;
269 }
270
SlToOhosChannel(SLDataFormat_PCM * pcmFormat)271 AudioChannel AudioPlayerAdapter::SlToOhosChannel(SLDataFormat_PCM *pcmFormat)
272 {
273 AudioChannel channelCount;
274 switch (pcmFormat->numChannels) {
275 case MONO:
276 channelCount = MONO;
277 break;
278 case STEREO:
279 channelCount = STEREO;
280 break;
281 default:
282 channelCount = MONO;
283 MEDIA_ERR_LOG("AudioPlayerAdapter::channel count not supported ");
284 }
285 return channelCount;
286 }
287 } // namespace AudioStandard
288 } // namespace OHOS
289