• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2023 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 "audio_render_interface_impl.h"
17 
18 #include <hdf_base.h>
19 #include <unistd.h>
20 #include "sys/time.h"
21 
22 #include "daudio_constants.h"
23 #include "daudio_events.h"
24 #include "daudio_log.h"
25 #include "daudio_utils.h"
26 
27 #undef DH_LOG_TAG
28 #define DH_LOG_TAG "AudioRenderInterfaceImpl"
29 
30 using namespace OHOS::DistributedHardware;
31 namespace OHOS {
32 namespace HDI {
33 namespace DistributedAudio {
34 namespace Audio {
35 namespace V1_0 {
AudioRenderInterfaceImpl(const std::string & adpName,const AudioDeviceDescriptor & desc,const AudioSampleAttributes & attrs,const sptr<IDAudioCallback> & callback)36 AudioRenderInterfaceImpl::AudioRenderInterfaceImpl(const std::string &adpName, const AudioDeviceDescriptor &desc,
37     const AudioSampleAttributes &attrs, const sptr<IDAudioCallback> &callback)
38     : adapterName_(adpName), devDesc_(desc),
39     devAttrs_(attrs), audioExtCallback_(callback)
40 {
41     devAttrs_.frameSize = CalculateFrameSize(attrs.sampleRate, attrs.channelCount, attrs.format, timeInterval_, false);
42     DHLOGD("Distributed audio render constructed, period(%d), frameSize(%d).",
43         attrs.period, devAttrs_.frameSize);
44 }
45 
~AudioRenderInterfaceImpl()46 AudioRenderInterfaceImpl::~AudioRenderInterfaceImpl()
47 {
48     DHLOGD("Distributed audio render destructed, id(%d).", devDesc_.pins);
49 }
50 
GetLatency(uint32_t & ms)51 int32_t AudioRenderInterfaceImpl::GetLatency(uint32_t &ms)
52 {
53     DHLOGI("Get render device latency, not support yet.");
54     ms = 0;
55     return HDF_SUCCESS;
56 }
57 
GetFadeRate(uint32_t currentIndex,const uint32_t durationIndex)58 float AudioRenderInterfaceImpl::GetFadeRate(uint32_t currentIndex, const uint32_t durationIndex)
59 {
60     if (currentIndex > durationIndex) {
61         return 1.0f;
62     }
63 
64     float fadeRate = static_cast<float>(currentIndex) / durationIndex * DAUDIO_FADE_NORMALIZATION_FACTOR;
65     if (fadeRate < 1) {
66         return pow(fadeRate, DAUDIO_FADE_POWER_NUM) / DAUDIO_FADE_NORMALIZATION_FACTOR;
67     }
68     return -pow(fadeRate - DAUDIO_FADE_MAXIMUM_VALUE, DAUDIO_FADE_POWER_NUM) /
69         DAUDIO_FADE_NORMALIZATION_FACTOR + 1;
70 }
71 
FadeInProcess(const uint32_t durationFrame,int8_t * frameData,const size_t frameLength)72 int32_t AudioRenderInterfaceImpl::FadeInProcess(const uint32_t durationFrame,
73     int8_t* frameData, const size_t frameLength)
74 {
75     int16_t* frame = reinterpret_cast<int16_t *>(frameData);
76     const size_t newFrameLength = frameLength / 2;
77 
78     for (size_t k = 0; k < newFrameLength; ++k) {
79         float rate = GetFadeRate(currentFrame_ * newFrameLength + k, durationFrame * newFrameLength);
80         frame[k] = currentFrame_ == durationFrame - 1 ? frame[k] : static_cast<int16_t>(rate * frame[k]);
81     }
82     DHLOGI("Fade-in frame[currentFrame: %d].", currentFrame_);
83     ++currentFrame_;
84     currentFrame_ = currentFrame_ >= durationFrame ? durationFrame - 1 : currentFrame_;
85 
86     return HDF_SUCCESS;
87 }
88 
RenderFrame(const std::vector<int8_t> & frame,uint64_t & replyBytes)89 int32_t AudioRenderInterfaceImpl::RenderFrame(const std::vector<int8_t> &frame, uint64_t &replyBytes)
90 {
91     DHLOGD("Render frame[sampleRate: %u, channelCount: %u, format: %d, frameSize: %u].", devAttrs_.sampleRate,
92         devAttrs_.channelCount, devAttrs_.format, devAttrs_.frameSize);
93     DHLOGD("Render frameIndex: %lld.", frameIndex_);
94 
95     std::lock_guard<std::mutex> renderLck(renderMtx_);
96     if (renderStatus_ != RENDER_STATUS_START) {
97         DHLOGE("Render status wrong, return false.");
98         return HDF_FAILURE;
99     }
100 
101     AudioParameter param = { devAttrs_.format, devAttrs_.channelCount, devAttrs_.sampleRate, 0, devAttrs_.frameSize};
102     AudioData data = { param, frame };
103     FadeInProcess(DURATION_FRAMES, data.data.data(), frame.size());
104     if (audioExtCallback_ == nullptr) {
105         DHLOGE("Callback is nullptr.");
106         return HDF_FAILURE;
107     }
108     int32_t ret = audioExtCallback_->WriteStreamData(adapterName_, devDesc_.pins, data);
109     if (ret != HDF_SUCCESS) {
110         DHLOGE("Write stream data failed.");
111         return HDF_FAILURE;
112     }
113 
114     ++frameIndex_;
115     DHLOGD("Render audio frame success.");
116     return HDF_SUCCESS;
117 }
118 
GetRenderPosition(uint64_t & frames,AudioTimeStamp & time)119 int32_t AudioRenderInterfaceImpl::GetRenderPosition(uint64_t &frames, AudioTimeStamp &time)
120 {
121     DHLOGI("Get render position, not support yet.");
122     (void)frames;
123     (void)time;
124     return HDF_SUCCESS;
125 }
126 
SetRenderSpeed(float speed)127 int32_t AudioRenderInterfaceImpl::SetRenderSpeed(float speed)
128 {
129     DHLOGI("Set render speed, control render speed is not support yet.");
130     renderSpeed_ = speed;
131     return HDF_SUCCESS;
132 }
133 
GetRenderSpeed(float & speed)134 int32_t AudioRenderInterfaceImpl::GetRenderSpeed(float &speed)
135 {
136     DHLOGI("Get render speed, control render speed is not support yet.");
137     speed = renderSpeed_;
138     return HDF_SUCCESS;
139 }
140 
SetChannelMode(AudioChannelMode mode)141 int32_t AudioRenderInterfaceImpl::SetChannelMode(AudioChannelMode mode)
142 {
143     DHLOGI("Set channel mode, control channel mode is not support yet.");
144     channelMode_ = mode;
145     return HDF_SUCCESS;
146 }
147 
GetChannelMode(AudioChannelMode & mode)148 int32_t AudioRenderInterfaceImpl::GetChannelMode(AudioChannelMode &mode)
149 {
150     DHLOGI("Get channel mode, control channel mode is not support yet.");
151     mode = channelMode_;
152     return HDF_SUCCESS;
153 }
154 
RegCallback(const sptr<IAudioCallback> & audioCallback,int8_t cookie)155 int32_t AudioRenderInterfaceImpl::RegCallback(const sptr<IAudioCallback> &audioCallback, int8_t cookie)
156 {
157     DHLOGI("Register render callback.");
158     (void)cookie;
159     renderCallback_ = audioCallback;
160     return HDF_SUCCESS;
161 }
162 
DrainBuffer(AudioDrainNotifyType & type)163 int32_t AudioRenderInterfaceImpl::DrainBuffer(AudioDrainNotifyType &type)
164 {
165     DHLOGI("Drain audio buffer, not support yet.");
166     (void)type;
167     return HDF_SUCCESS;
168 }
169 
IsSupportsDrain(bool & support)170 int32_t AudioRenderInterfaceImpl::IsSupportsDrain(bool &support)
171 {
172     DHLOGI("Check whether drain is supported, not support yet.");
173     (void)support;
174     return HDF_SUCCESS;
175 }
176 
Start()177 int32_t AudioRenderInterfaceImpl::Start()
178 {
179     DHLOGI("Start render.");
180     if (firstOpenFlag_) {
181         firstOpenFlag_ = false;
182     } else {
183         DAudioEvent event = { HDF_AUDIO_EVENT_CHANGE_PLAY_STATUS, HDF_EVENT_RESTART };
184         int32_t ret = audioExtCallback_->NotifyEvent(adapterName_, devDesc_.pins, event);
185         if (ret != HDF_SUCCESS) {
186             DHLOGE("Restart failed.");
187         }
188     }
189     std::lock_guard<std::mutex> renderLck(renderMtx_);
190     renderStatus_ = RENDER_STATUS_START;
191     currentFrame_ = CUR_FRAME_INIT_VALUE;
192     frameIndex_ = 0;
193     return HDF_SUCCESS;
194 }
195 
Stop()196 int32_t AudioRenderInterfaceImpl::Stop()
197 {
198     DHLOGI("Stop render.");
199     DAudioEvent event = { HDF_AUDIO_EVENT_CHANGE_PLAY_STATUS, HDF_EVENT_PAUSE };
200     int32_t ret = audioExtCallback_->NotifyEvent(adapterName_, devDesc_.pins, event);
201     if (ret != HDF_SUCCESS) {
202         DHLOGE("Pause and clear cache streams failed.");
203     }
204     std::lock_guard<std::mutex> renderLck(renderMtx_);
205     renderStatus_ = RENDER_STATUS_STOP;
206     return HDF_SUCCESS;
207 }
208 
Pause()209 int32_t AudioRenderInterfaceImpl::Pause()
210 {
211     DHLOGI("Pause render.");
212     std::lock_guard<std::mutex> renderLck(renderMtx_);
213     renderStatus_ = RENDER_STATUS_PAUSE;
214     return HDF_SUCCESS;
215 }
216 
Resume()217 int32_t AudioRenderInterfaceImpl::Resume()
218 {
219     return HDF_SUCCESS;
220 }
221 
Flush()222 int32_t AudioRenderInterfaceImpl::Flush()
223 {
224     return HDF_SUCCESS;
225 }
226 
TurnStandbyMode()227 int32_t AudioRenderInterfaceImpl::TurnStandbyMode()
228 {
229     DHLOGI("Turn stand by mode, not support yet.");
230     return HDF_SUCCESS;
231 }
232 
AudioDevDump(int32_t range,int32_t fd)233 int32_t AudioRenderInterfaceImpl::AudioDevDump(int32_t range, int32_t fd)
234 {
235     DHLOGI("Dump audio info, not support yet.");
236     (void)range;
237     (void)fd;
238     return HDF_SUCCESS;
239 }
240 
IsSupportsPauseAndResume(bool & supportPause,bool & supportResume)241 int32_t AudioRenderInterfaceImpl::IsSupportsPauseAndResume(bool &supportPause, bool &supportResume)
242 {
243     DHLOGI("Check whether pause and resume is supported, not support yet.");
244     (void)supportPause;
245     (void)supportResume;
246     return HDF_SUCCESS;
247 }
248 
CheckSceneCapability(const AudioSceneDescriptor & scene,bool & supported)249 int32_t AudioRenderInterfaceImpl::CheckSceneCapability(const AudioSceneDescriptor &scene, bool &supported)
250 {
251     DHLOGI("Check scene capability.");
252     (void)scene;
253     (void)supported;
254     return HDF_SUCCESS;
255 }
256 
SelectScene(const AudioSceneDescriptor & scene)257 int32_t AudioRenderInterfaceImpl::SelectScene(const AudioSceneDescriptor &scene)
258 {
259     DHLOGI("Select audio scene, not support yet.");
260     (void)scene;
261     return HDF_SUCCESS;
262 }
263 
SetMute(bool mute)264 int32_t AudioRenderInterfaceImpl::SetMute(bool mute)
265 {
266     DHLOGI("Set mute, not support yet.");
267     (void)mute;
268     return HDF_SUCCESS;
269 }
270 
GetMute(bool & mute)271 int32_t AudioRenderInterfaceImpl::GetMute(bool &mute)
272 {
273     DHLOGI("Get mute, not support yet.");
274     (void)mute;
275     return HDF_SUCCESS;
276 }
277 
SetVolume(float volume)278 int32_t AudioRenderInterfaceImpl::SetVolume(float volume)
279 {
280     DHLOGI("Can not set vol not by this interface.");
281     (void)volume;
282     return HDF_SUCCESS;
283 }
284 
GetVolume(float & volume)285 int32_t AudioRenderInterfaceImpl::GetVolume(float &volume)
286 {
287     DHLOGI("Can not get vol not by this interface.");
288     (void)volume;
289     return HDF_SUCCESS;
290 }
291 
GetGainThreshold(float & min,float & max)292 int32_t AudioRenderInterfaceImpl::GetGainThreshold(float &min, float &max)
293 {
294     DHLOGI("Get gain threshold, not support yet.");
295     min = 0;
296     max = 0;
297     return HDF_SUCCESS;
298 }
299 
SetGain(float gain)300 int32_t AudioRenderInterfaceImpl::SetGain(float gain)
301 {
302     DHLOGI("Set gain, not support yet.");
303     (void)gain;
304     return HDF_SUCCESS;
305 }
306 
GetGain(float & gain)307 int32_t AudioRenderInterfaceImpl::GetGain(float &gain)
308 {
309     DHLOGI("Get gain, not support yet.");
310     gain = 1.0;
311     return HDF_SUCCESS;
312 }
313 
GetFrameSize(uint64_t & size)314 int32_t AudioRenderInterfaceImpl::GetFrameSize(uint64_t &size)
315 {
316     (void)size;
317     return HDF_SUCCESS;
318 }
319 
GetFrameCount(uint64_t & count)320 int32_t AudioRenderInterfaceImpl::GetFrameCount(uint64_t &count)
321 {
322     (void)count;
323     return HDF_SUCCESS;
324 }
325 
SetSampleAttributes(const AudioSampleAttributes & attrs)326 int32_t AudioRenderInterfaceImpl::SetSampleAttributes(const AudioSampleAttributes &attrs)
327 {
328     DHLOGI("Set sample attributes.");
329     devAttrs_ = attrs;
330     return HDF_SUCCESS;
331 }
332 
GetSampleAttributes(AudioSampleAttributes & attrs)333 int32_t AudioRenderInterfaceImpl::GetSampleAttributes(AudioSampleAttributes &attrs)
334 {
335     DHLOGI("Get sample attributes.");
336     attrs = devAttrs_;
337     return HDF_SUCCESS;
338 }
339 
GetCurrentChannelId(uint32_t & channelId)340 int32_t AudioRenderInterfaceImpl::GetCurrentChannelId(uint32_t &channelId)
341 {
342     DHLOGI("Get current channel id, not support yet.");
343     (void)channelId;
344     return HDF_SUCCESS;
345 }
346 
SetExtraParams(const std::string & keyValueList)347 int32_t AudioRenderInterfaceImpl::SetExtraParams(const std::string &keyValueList)
348 {
349     DHLOGI("Set extra parameters, not support yet.");
350     (void)keyValueList;
351     return HDF_SUCCESS;
352 }
353 
GetExtraParams(std::string & keyValueList)354 int32_t AudioRenderInterfaceImpl::GetExtraParams(std::string &keyValueList)
355 {
356     DHLOGI("Get extra parameters, not support yet.");
357     (void)keyValueList;
358     return HDF_SUCCESS;
359 }
360 
ReqMmapBuffer(int32_t reqSize,AudioMmapBufferDescriptor & desc)361 int32_t AudioRenderInterfaceImpl::ReqMmapBuffer(int32_t reqSize, AudioMmapBufferDescriptor &desc)
362 {
363     DHLOGI("Request mmap buffer, not support yet.");
364     (void)reqSize;
365     (void)desc;
366     return HDF_SUCCESS;
367 }
368 
GetMmapPosition(uint64_t & frames,AudioTimeStamp & time)369 int32_t AudioRenderInterfaceImpl::GetMmapPosition(uint64_t &frames, AudioTimeStamp &time)
370 {
371     DHLOGI("Get mmap position, not support yet.");
372     (void)frames;
373     (void)time;
374     return HDF_SUCCESS;
375 }
376 
AddAudioEffect(uint64_t effectid)377 int32_t AudioRenderInterfaceImpl::AddAudioEffect(uint64_t effectid)
378 {
379     DHLOGI("Add audio effect, not support yet.");
380     (void)effectid;
381     return HDF_SUCCESS;
382 }
383 
RemoveAudioEffect(uint64_t effectid)384 int32_t AudioRenderInterfaceImpl::RemoveAudioEffect(uint64_t effectid)
385 {
386     DHLOGI("Remove audio effect, not support yet.");
387     (void)effectid;
388     return HDF_SUCCESS;
389 }
390 
GetFrameBufferSize(uint64_t & bufferSize)391 int32_t AudioRenderInterfaceImpl::GetFrameBufferSize(uint64_t &bufferSize)
392 {
393     DHLOGI("Get frame buffer size, not support yet.");
394     (void)bufferSize;
395     return HDF_SUCCESS;
396 }
397 
GetRenderDesc()398 const AudioDeviceDescriptor &AudioRenderInterfaceImpl::GetRenderDesc()
399 {
400     return devDesc_;
401 }
402 
SetVolumeInner(const uint32_t vol)403 void AudioRenderInterfaceImpl::SetVolumeInner(const uint32_t vol)
404 {
405     std::lock_guard<std::mutex> volLck(volMtx_);
406     vol_ = vol;
407 }
408 
SetVolumeRangeInner(const uint32_t volMax,const uint32_t volMin)409 void AudioRenderInterfaceImpl::SetVolumeRangeInner(const uint32_t volMax, const uint32_t volMin)
410 {
411     std::lock_guard<std::mutex> volLck(volMtx_);
412     volMin_ = volMin;
413     volMax_ = volMax;
414 }
415 
GetVolumeInner()416 uint32_t AudioRenderInterfaceImpl::GetVolumeInner()
417 {
418     std::lock_guard<std::mutex> volLck(volMtx_);
419     return vol_;
420 }
421 
GetMaxVolumeInner()422 uint32_t AudioRenderInterfaceImpl::GetMaxVolumeInner()
423 {
424     std::lock_guard<std::mutex> volLck(volMtx_);
425     return volMax_;
426 }
427 
GetMinVolumeInner()428 uint32_t AudioRenderInterfaceImpl::GetMinVolumeInner()
429 {
430     std::lock_guard<std::mutex> volLck(volMtx_);
431     return volMin_;
432 }
433 
SetAttrs(const std::string & adpName,const AudioDeviceDescriptor & desc,const AudioSampleAttributes & attrs,const sptr<IDAudioCallback> & callback)434 void AudioRenderInterfaceImpl::SetAttrs(const std::string &adpName, const AudioDeviceDescriptor &desc,
435     const AudioSampleAttributes &attrs, const sptr<IDAudioCallback> &callback)
436 {
437     DHLOGI("Set attrs, not support yet.");
438 }
439 } // V1_0
440 } // Audio
441 } // Distributedaudio
442 } // HDI
443 } // OHOS
444