1 /*
2 * Copyright (c) 2024 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 "fast_audio_render.h"
17 #include "audio_adapter_info_common.h"
18 #include "audio_bluetooth_manager.h"
19 #include "audio_internal.h"
20 #include "hdf_log.h"
21 #include <string>
22
23 namespace OHOS::HDI::Audio_Bluetooth {
24 #ifdef A2DP_HDI_SERVICE
25 const uint32_t MIN_TIME_INTERVAL = 30;
26 const uint32_t MAX_TIME_INTERVAL = 80;
27 const int32_t MAX_ASHMEM_LEN = 100000;
28 const int32_t MIN_ASHMEM_LEN = 10;
29 const int32_t RENDER_TIME_INTERVAL = 5;
30 const int32_t PER_MS_SECOND = 1000;
31
CalculateSampleNum(uint32_t sampleRate, uint32_t timeMs)32 static int32_t CalculateSampleNum(uint32_t sampleRate, uint32_t timeMs)
33 {
34 return (sampleRate * timeMs) / PER_MS_SECOND;
35 }
36 #endif
37
FastRenderStart(AudioHandle handle)38 int32_t FastRenderStart(AudioHandle handle)
39 {
40 HDF_LOGI("%{public}s enter", __func__);
41 #ifdef A2DP_HDI_SERVICE
42 AudioHwRender *hwRender = reinterpret_cast<struct AudioHwRender>(handle);
43 if (hwRender == nullptr) {
44 return AUDIO_HAL_ERR_INVALID_PARAM;
45 }
46 AudioSampleAttributes *attr = &hwRender->RenderParam.frameRenderMode.attrs;
47 uint32_t format = static_cast<uint32_t>(attrs->format);
48 return OHOS::Bluetooth::FastStartPlaying(attrs->sampleRate, attrs->channelCount, format);
49 #endif
50 return AUDIO_HAL_ERR_NOT_SUPPORT;
51 }
52
FastRenderStop(AudioHandle handle)53 int32_t FastRenderStop(AudioHandle handle)
54 {
55 HDF_LOGI("%{public}s enter", __func__);
56 (void)handle;
57 #ifdef A2DP_HDI_SERVICE
58 return OHOS::Bluetooth::FastStopPlaying();
59 #endif
60 return AUDIO_HAL_ERR_NOT_SUPPORT;
61 }
62
FastRenderPause(AudioHandle handle)63 int32_t FastRenderPause(AudioHandle handle)
64 {
65 HDF_LOGI("%{public}s enter", __func__);
66 (void)handle;
67 #ifdef A2DP_HDI_SERVICE
68 return OHOS::Bluetooth::FastSuspendPlaying();
69 #endif
70 return AUDIO_HAL_ERR_NOT_SUPPORT;
71 }
72
FastRenderResume(AudioHandle handle)73 int32_t FastRenderResume(AudioHandle handle)
74 {
75 HDF_LOGI("%{public}s enter", __func__);
76 #ifdef A2DP_HDI_SERVICE
77 AudioHwRender *hwRender = reinterpret_cast<struct AudioHwRender>(handle);
78 if (hwRender == nullptr) {
79 return AUDIO_HAL_ERR_INVALID_PARAM;
80 }
81 AudioSampleAttributes *attr = &hwRender->RenderParam.frameRenderMode.attrs;
82 uint32_t format = static_cast<uint32_t>(attrs->format);
83 return OHOS::Bluetooth::FastStartPlaying(attrs->sampleRate, attrs->channelCount, format);
84 #endif
85 return AUDIO_HAL_ERR_NOT_SUPPORT;
86 }
87
FastRenderFlush(AudioHandle handle)88 int32_t FastRenderFlush(AudioHandle handle)
89 {
90 HDF_LOGI("%{public}s enter", __func__);
91 (void)handle;
92 return AUDIO_HAL_ERR_NOT_SUPPORT;
93 }
94
FastRenderGetFrameSize(AudioHandle handle, uint64_t *size)95 int32_t FastRenderGetFrameSize(AudioHandle handle, uint64_t *size)
96 {
97 HDF_LOGI("%{public}s enter", __func__);
98 (void)handle;
99 (void)size;
100 return AUDIO_HAL_ERR_NOT_SUPPORT;
101 }
102
FastRenderGetFrameCount(AudioHandle handle, uint64_t *count)103 int32_t FastRenderGetFrameCount(AudioHandle handle, uint64_t *count)
104 {
105 HDF_LOGI("%{public}s enter", __func__);
106 (void)handle;
107 (void)count;
108 return AUDIO_HAL_ERR_NOT_SUPPORT;
109 }
110
FastRenderSetSampleAttributes(AudioHandle handle, const struct AudioSampleAttributes *attrs)111 int32_t FastRenderSetSampleAttributes(AudioHandle handle, const struct AudioSampleAttributes *attrs)
112 {
113 HDF_LOGI("%{public}s enter", __func__);
114 (void)handle;
115 (void)attrs;
116 return AUDIO_HAL_ERR_NOT_SUPPORT;
117 }
118
FastRenderGetSampleAttributes(AudioHandle handle, struct AudioSampleAttributes *attrs)119 int32_t FastRenderGetSampleAttributes(AudioHandle handle, struct AudioSampleAttributes *attrs)
120 {
121 HDF_LOGI("%{public}s enter", __func__);
122 (void)handle;
123 (void)attrs;
124 return AUDIO_HAL_ERR_NOT_SUPPORT;
125 }
126
FastRenderGetCurrentChannelId(AudioHandle handle, uint32_t *channelId)127 int32_t FastRenderGetCurrentChannelId(AudioHandle handle, uint32_t *channelId)
128 {
129 HDF_LOGI("%{public}s enter", __func__);
130 (void)handle;
131 (void)channelId;
132 return AUDIO_HAL_ERR_NOT_SUPPORT;
133 }
134
FastRenderCheckSceneCapability(AudioHandle handle, const struct AudioSceneDescriptor *scene, bool *supported)135 int32_t FastRenderCheckSceneCapability(AudioHandle handle, const struct AudioSceneDescriptor *scene, bool *supported)
136 {
137 HDF_LOGI("%{public}s enter", __func__);
138 (void)handle;
139 (void)scene;
140 (void)supported;
141 return AUDIO_HAL_ERR_NOT_SUPPORT;
142 }
143
FastRenderSelectScene(AudioHandle handle, const struct AudioSceneDescriptor *scene)144 int32_t FastRenderSelectScene(AudioHandle handle, const struct AudioSceneDescriptor *scene)
145 {
146 HDF_LOGI("%{public}s enter", __func__);
147 (void)handle;
148 (void)scene;
149 return AUDIO_HAL_ERR_NOT_SUPPORT;
150 }
151
FastRenderSetMute(AudioHandle handle, bool mute)152 int32_t FastRenderSetMute(AudioHandle handle, bool mute)
153 {
154 HDF_LOGI("%{public}s enter", __func__);
155 (void)handle;
156 (void)mute;
157 return AUDIO_HAL_ERR_NOT_SUPPORT;
158 }
159
FastRenderGetMute(AudioHandle handle, bool *mute)160 int32_t FastRenderGetMute(AudioHandle handle, bool *mute)
161 {
162 HDF_LOGI("%{public}s enter", __func__);
163 (void)handle;
164 (void)mute;
165 return AUDIO_HAL_ERR_NOT_SUPPORT;
166 }
167
FastRenderSetVolume(AudioHandle handle, float volume)168 int32_t FastRenderSetVolume(AudioHandle handle, float volume)
169 {
170 HDF_LOGI("%{public}s enter", __func__);
171 (void)handle;
172 (void)volume;
173 return AUDIO_HAL_ERR_NOT_SUPPORT;
174 }
175
FastRenderGetVolume(AudioHandle handle, float *volume)176 int32_t FastRenderGetVolume(AudioHandle handle, float *volume)
177 {
178 HDF_LOGI("%{public}s enter", __func__);
179 (void)handle;
180 (void)volume;
181 return AUDIO_HAL_ERR_NOT_SUPPORT;
182 }
183
FastRenderGetGainThreshold(AudioHandle handle, float *min, float *max)184 int32_t FastRenderGetGainThreshold(AudioHandle handle, float *min, float *max)
185 {
186 HDF_LOGI("%{public}s enter", __func__);
187 (void)handle;
188 (void)min;
189 (void)max;
190 return AUDIO_HAL_ERR_NOT_SUPPORT;
191 }
192
FastRenderGetGain(AudioHandle handle, float *gain)193 int32_t FastRenderGetGain(AudioHandle handle, float *gain)
194 {
195 HDF_LOGI("%{public}s enter", __func__);
196 (void)handle;
197 (void)gain;
198 return AUDIO_HAL_ERR_NOT_SUPPORT;
199 }
200
FastRenderSetGain(AudioHandle handle, float gain)201 int32_t FastRenderSetGain(AudioHandle handle, float gain)
202 {
203 HDF_LOGI("%{public}s enter", __func__);
204 (void)handle;
205 (void)gain;
206 return AUDIO_HAL_ERR_NOT_SUPPORT;
207 }
208
FastRenderGetLatency(struct AudioRender *render, uint32_t *ms)209 int32_t FastRenderGetLatency(struct AudioRender *render, uint32_t *ms)
210 {
211 HDF_LOGI("%{public}s enter", __func__);
212 (void)render;
213 (void)ms;
214 #ifdef A2DP_HDI_SERVICE
215 uint32_t latency = 0;
216 OHOS::Bluetooth::FastGetLatency(latency);
217 *ms = latency;
218 return AUDIO_HAL_SUCCESS;
219 #endif
220 return AUDIO_HAL_ERR_NOT_SUPPORT;
221 }
222
FastRenderRenderFrame( struct AudioRender *render, const void *frame, uint64_t requestBytes, uint64_t *replyBytes)223 int32_t FastRenderRenderFrame(
224 struct AudioRender *render, const void *frame, uint64_t requestBytes, uint64_t *replyBytes)
225 {
226 HDF_LOGI("%{public}s enter", __func__);
227 (void)render;
228 (void)frame;
229 (void)requestBytes;
230 (void)replyBytes;
231 return AUDIO_HAL_ERR_NOT_SUPPORT;
232 }
233
FastRenderGetRenderPosition(struct AudioRender *render, uint64_t *frames, struct AudioTimeStamp *time)234 int32_t FastRenderGetRenderPosition(struct AudioRender *render, uint64_t *frames, struct AudioTimeStamp *time)
235 {
236 HDF_LOGI("%{public}s enter", __func__);
237 (void)render;
238 (void)frames;
239 (void)time;
240 return AUDIO_HAL_ERR_NOT_SUPPORT;
241 }
242
FastRenderSetRenderSpeed(struct AudioRender *render, float speed)243 int32_t FastRenderSetRenderSpeed(struct AudioRender *render, float speed)
244 {
245 HDF_LOGI("%{public}s enter", __func__);
246 (void)render;
247 (void)speed;
248 return AUDIO_HAL_ERR_NOT_SUPPORT;
249 }
250
FastRenderGetRenderSpeed(struct AudioRender *render, float *speed)251 int32_t FastRenderGetRenderSpeed(struct AudioRender *render, float *speed)
252 {
253 HDF_LOGI("%{public}s enter", __func__);
254 (void)render;
255 (void)speed;
256 return AUDIO_HAL_ERR_NOT_SUPPORT;
257 }
258
FastRenderSetChannelMode(struct AudioRender *render, AudioChannelMode mode)259 int32_t FastRenderSetChannelMode(struct AudioRender *render, AudioChannelMode mode)
260 {
261 HDF_LOGI("%{public}s enter", __func__);
262 (void)render;
263 (void)mode;
264 return AUDIO_HAL_ERR_NOT_SUPPORT;
265 }
266
FastRenderGetChannelMode(struct AudioRender *render, AudioChannelMode *mode)267 int32_t FastRenderGetChannelMode(struct AudioRender *render, AudioChannelMode *mode)
268 {
269 HDF_LOGI("%{public}s enter", __func__);
270 (void)render;
271 (void)mode;
272 return AUDIO_HAL_ERR_NOT_SUPPORT;
273 }
274
FastRenderSetExtraParams(AudioHandle handle, const char *keyValueList)275 int32_t FastRenderSetExtraParams(AudioHandle handle, const char *keyValueList)
276 {
277 HDF_LOGI("%{public}s enter", __func__);
278 (void)handle;
279 (void)keyValueList;
280 return AUDIO_HAL_ERR_NOT_SUPPORT;
281 }
282
FastRenderGetExtraParams(AudioHandle handle, char *keyValueList, int32_t listLength)283 int32_t FastRenderGetExtraParams(AudioHandle handle, char *keyValueList, int32_t listLength)
284 {
285 HDF_LOGI("%{public}s enter", __func__);
286 (void)handle;
287 (void)keyValueList;
288 (void)listLength;
289 return AUDIO_HAL_ERR_NOT_SUPPORT;
290 }
291
FastRenderReqMmapBuffer(AudioHandle handle, int32_t reqSize, struct AudioMmapBufferDescriptor *desc)292 int32_t FastRenderReqMmapBuffer(AudioHandle handle, int32_t reqSize, struct AudioMmapBufferDescriptor *desc)
293 {
294 HDF_LOGI("%{public}s enter", __func__);
295 #ifdef A2DP_HDI_SERVICE
296 AudioHwRender *render = reinterpret_cast<AudioHwRender *>(handle);
297 if (render == nullptr) {
298 return AUDIO_HAL_ERR_INVALID_PARAM;
299 }
300 AudioSampleAttributes attr = render->renderParam.frameRenderMode.attrs;
301
302 int32_t minSize = CalculateSampleNum(attr.sampleRate, MIN_TIME_INTERVAL);
303 int32_t maxSize = CalculateSampleNum(attr.sampleRate, MAX_TIME_INTERVAL);
304 int32_t realSize = reqSize;
305 if (reqSize < minSize) {
306 realSize = minSize;
307 } else if (reqSize > maxSize) {
308 realSize = maxSize;
309 }
310 int32_t ashmemLength = realSize * static_cast<int32_t>(attr.channelCount) * attr.format;
311 if (ashmemLength < MIN_ASHMEM_LEN || ashmemLength > MAX_ASHMEM_LEN) {
312 HDF_LOGE("reqMmapBuffer failed, length is illegal %{public}d", ashmemLength);
313 return AUDIO_HAL_ERR_INVALID_PARAM;
314 }
315 int32_t fd = OHOS::Bluetooth::FastReqMmapBuffer(ashmemLength);
316 if (fd < 0) {
317 HDF_LOGE("reqMmapBuffer failed");
318 return HDF_FAILURE;
319 }
320 desc->memoryFd = fd;
321 desc->transferFrameSize = static_cast<int32_t>(CalculateSampleNum(attr.sampleRate, RENDER_TIME_INTERVAL));
322 desc->totalBufferFrames = realSize;
323 desc->isShareable = false;
324 HDF_LOGI("%{public}s, fd=%{public}d, length=%{public}d, transferFrameSize=%{public}d, totalBufferFrames=%{public}d",
325 __func__, desc->memoryFd, desc->transferFrameSize, desc->totalBufferFrames);
326 #endif
327 return AUDIO_HAL_ERR_NOT_SUPPORT;
328 }
329
FastRenderGetMmapPosition(AudioHandle handle, uint64_t *frames, struct AudioTimeStamp *time)330 int32_t FastRenderGetMmapPosition(AudioHandle handle, uint64_t *frames, struct AudioTimeStamp *time)
331 {
332 HDF_LOGI("%{public}s enter", __func__);
333 (void)handle;
334 #ifdef A2DP_HDI_SERVICE
335 int64_t sec = 0;
336 int64_t nSec = 0;
337 uint64_t readFrames = 0;
338 OHOS::Bluetooth::FastReadMmapPosition(sec, nSec, readFrames);
339 *frames = readFrames;
340 time->tvSec = sec;
341 time->tvNSec = nSec;
342 return AUDIO_HAL_ERR_NOT_SUPPORT;
343 #endif
344 return AUDIO_HAL_ERR_NOT_SUPPORT;
345 }
346
FastRenderTurnStandbyMode(AudioHandle handle)347 int32_t FastRenderTurnStandbyMode(AudioHandle handle)
348 {
349 HDF_LOGI("%{public}s enter", __func__);
350 (void)handle;
351 return AUDIO_HAL_ERR_NOT_SUPPORT;
352 }
353
FastRenderAudioDevDump(AudioHandle handle, int32_t range, int32_t fd)354 int32_t FastRenderAudioDevDump(AudioHandle handle, int32_t range, int32_t fd)
355 {
356 HDF_LOGI("%{public}s enter", __func__);
357 (void)handle;
358 (void)range;
359 (void)fd;
360 return AUDIO_HAL_ERR_NOT_SUPPORT;
361 }
362
FastRenderRegCallback(struct AudioRender *render, RenderCallback callback, void *cookie)363 int32_t FastRenderRegCallback(struct AudioRender *render, RenderCallback callback, void *cookie)
364 {
365 HDF_LOGI("%{public}s enter", __func__);
366 (void)render;
367 (void)callback;
368 (void)cookie;
369 return AUDIO_HAL_ERR_NOT_SUPPORT;
370 }
371
FastRenderDrainBuffer(struct AudioRender *render, AudioDrainNotifyType *type)372 int32_t FastRenderDrainBuffer(struct AudioRender *render, AudioDrainNotifyType *type)
373 {
374 HDF_LOGI("%{public}s enter", __func__);
375 (void)render;
376 (void)type;
377 return AUDIO_HAL_ERR_NOT_SUPPORT;
378 }
379 } // namespace OHOS::HDI::Audio_Bluetooth