• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2025 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 "FastAudioRendererSinkInner"
17 #endif
18 
19 #include "fast_audio_renderer_sink.h"
20 
21 #include <cinttypes>
22 #include <climits>
23 #include <cstdio>
24 #include <cstring>
25 #include <dlfcn.h>
26 #include <list>
27 #include <mutex>
28 #include <string>
29 #include <unistd.h>
30 
31 #include <sys/mman.h>
32 #ifdef FEATURE_POWER_MANAGER
33 #include "power_mgr_client.h"
34 #include "running_lock.h"
35 #include "audio_running_lock_manager.h"
36 #endif
37 #include "securec.h"
38 #include "v4_0/iaudio_manager.h"
39 
40 #include "audio_errors.h"
41 #include "audio_hdi_log.h"
42 #include "audio_performance_monitor.h"
43 
44 using namespace std;
45 
46 namespace OHOS {
47 namespace AudioStandard {
48 namespace {
49 const int32_t HALF_FACTOR = 2;
50 const uint32_t MAX_AUDIO_ADAPTER_NUM = 5;
51 const float DEFAULT_VOLUME_LEVEL = 1.0f;
52 const uint32_t AUDIO_CHANNELCOUNT = 2;
53 const uint32_t DEEP_BUFFER_RENDER_PERIOD_SIZE = 3840;
54 const uint32_t INT_32_MAX = 0x7fffffff;
55 const uint32_t PCM_8_BIT = 8;
56 const uint32_t PCM_16_BIT = 16;
57 const uint32_t PCM_24_BIT = 24;
58 const uint32_t PCM_32_BIT = 32;
59 const int64_t GENERAL_MAX_HANDLE_COST_IN_NANOSEC = 10000000; // 10ms = 10ns * 1000 * 1000
60 const int64_t VOIP_MAX_HANDLE_COST_IN_NANOSEC = 20000000; // 20ms = 20ns * 1000 * 1000
61 const int64_t SECOND_TO_NANOSECOND = 1000000000;
62 const int INVALID_FD = -1;
63 const unsigned int XCOLLIE_TIME_OUT_SECONDS = 10;
64 const std::string MMAP_PRIMARY_HAL_NAME = "mmap primary";
65 const std::string MMAP_VOIP_HAL_NAME = "mmap voip";
66 }
67 
68 class FastAudioRendererSinkInner : public FastAudioRendererSink {
69 public:
70     int32_t Init(const IAudioSinkAttr &attr) override;
71     bool IsInited(void) override;
72     void DeInit(void) override;
73 
74     int32_t Start(void) override;
75     int32_t Stop(void) override;
76     int32_t Flush(void) override;
77     int32_t Reset(void) override;
78     int32_t Pause(void) override;
79     int32_t Resume(void) override;
80 
81     int32_t SuspendRenderSink(void) override;
82     int32_t RestoreRenderSink(void) override;
83 
84     int32_t RenderFrame(char &data, uint64_t len, uint64_t &writeLen) override;
85     int32_t SetVolume(float left, float right) override;
86     int32_t GetVolume(float &left, float &right) override;
87     int32_t SetVoiceVolume(float volume) override;
88     int32_t GetLatency(uint32_t *latency) override;
89     int32_t GetTransactionId(uint64_t *transactionId) override;
90     int32_t GetAudioScene() override;
91     int32_t SetAudioScene(AudioScene audioScene, std::vector<DeviceType> &activeDevices) override;
92     int32_t SetOutputRoutes(std::vector<DeviceType> &outputDevices) override;
93     void ResetOutputRouteForDisconnect(DeviceType device) override;
94 
95     void SetAudioParameter(const AudioParamKey key, const std::string &condition, const std::string &value) override;
96     std::string GetAudioParameter(const AudioParamKey key, const std::string &condition) override;
97     void RegisterAudioSinkCallback(IAudioSinkCallback* callback) override;
98 
99     void SetAudioMonoState(bool audioMono) override;
100     void SetAudioBalanceValue(float audioBalance) override;
101     int32_t SetSinkMuteForSwitchDevice(bool mute) final;
102 
103     int32_t GetPresentationPosition(uint64_t& frames, int64_t& timeSec, int64_t& timeNanoSec) override;
104 
105     int32_t GetMmapBufferInfo(int &fd, uint32_t &totalSizeInframe, uint32_t &spanSizeInframe,
106         uint32_t &byteSizePerFrame) override;
107     int32_t GetMmapHandlePosition(uint64_t &frames, int64_t &timeSec, int64_t &timeNanoSec) override;
108     float GetMaxAmplitude() override;
109     int32_t SetPaPower(int32_t flag) override;
110     int32_t SetPriPaPower() override;
111 
112     int32_t UpdateAppsUid(const int32_t appsUid[MAX_MIX_CHANNELS], const size_t size) final;
113     int32_t UpdateAppsUid(const std::vector<int32_t> &appsUid) final;
114     int32_t GetRenderId(uint32_t &renderId) const override;
115 
116     FastAudioRendererSinkInner();
117     ~FastAudioRendererSinkInner();
118 
119 private:
120 #ifdef FEATURE_POWER_MANAGER
121     void KeepRunningLock();
122     void KeepRunningUnlock();
123 #endif
124     int32_t PrepareMmapBuffer();
125     void ReleaseMmapBuffer();
126 
127     int32_t CheckPositionTime();
128     void PreparePosition();
129 
130     void InitAttrs(struct AudioSampleAttributes &attrs);
131     AudioFormat ConvertToHdiFormat(HdiAdapterFormat format);
132     int32_t CreateRender(const struct AudioPort &renderPort);
133     int32_t InitAudioManager();
134     void UpdateSinkState(bool started);
135 
136 private:
137     IAudioSinkAttr attr_ = {};
138     bool rendererInited_ = false;
139     bool started_ = false;
140     bool paused_ = false;
141     float leftVolume_ = 0.0f;
142     float rightVolume_ = 0.0f;
143     int32_t routeHandle_ = -1;
144     std::string adapterNameCase_ = "";
145     struct IAudioManager *audioManager_ = nullptr;
146     struct IAudioAdapter *audioAdapter_ = nullptr;
147     struct IAudioRender *audioRender_ = nullptr;
148     struct AudioAdapterDescriptor adapterDesc_ = {};
149     struct AudioPort audioPort_ = {};
150     uint32_t renderId_ = 0;
151     uint32_t sinkId_ = 0;
152     std::string halName_ = "";
153 
154     size_t bufferSize_ = 0;
155     uint32_t bufferTotalFrameSize_ = 0;
156 
157     int bufferFd_ = INVALID_FD;
158     uint32_t frameSizeInByte_ = 1;
159     uint32_t eachReadFrameSize_ = 0;
160     std::mutex mutex_;
161     IAudioSinkCallback *callback_ = nullptr;
162     // for device switch
163     std::mutex switchDeviceMutex_;
164     int32_t muteCount_ = 0;
165     std::atomic<bool> switchDeviceMute_ = false;
166 #ifdef FEATURE_POWER_MANAGER
167     std::shared_ptr<AudioRunningLockManager<PowerMgr::RunningLock>> runningLockManager_;
168 #endif
169 
170 #ifdef DEBUG_DIRECT_USE_HDI
171     char *bufferAddresss_ = nullptr;
172     bool isFirstWrite_ = true;
173     uint64_t alreadyReadFrames_ = 0;
174     uint32_t curReadPos_ = 0;
175     uint32_t curWritePos_ = 0;
176     uint32_t writeAheadPeriod_ = 1;
177 
178     int privFd_ = INVALID_FD; // invalid fd
179 #endif
180 };  // FastAudioRendererSinkInner
181 
FastAudioRendererSinkInner()182 FastAudioRendererSinkInner::FastAudioRendererSinkInner()
183     : rendererInited_(false), started_(false), paused_(false), leftVolume_(DEFAULT_VOLUME_LEVEL),
184       rightVolume_(DEFAULT_VOLUME_LEVEL), audioManager_(nullptr), audioAdapter_(nullptr),
185       audioRender_(nullptr)
186 {
187     attr_ = {};
188 }
189 
~FastAudioRendererSinkInner()190 FastAudioRendererSinkInner::~FastAudioRendererSinkInner()
191 {
192     AUDIO_INFO_LOG("In");
193     FastAudioRendererSinkInner::DeInit();
194 }
195 
GetInstance()196 IMmapAudioRendererSink *FastAudioRendererSink::GetInstance()
197 {
198     static FastAudioRendererSinkInner audioRenderer;
199 
200     return &audioRenderer;
201 }
202 
GetVoipInstance()203 IMmapAudioRendererSink *FastAudioRendererSink::GetVoipInstance()
204 {
205     static FastAudioRendererSinkInner audioVoipRenderer;
206 
207     return &audioVoipRenderer;
208 }
209 
CreateFastRendererSink()210 std::shared_ptr<IMmapAudioRendererSink> FastAudioRendererSink::CreateFastRendererSink()
211 {
212     std::shared_ptr<IMmapAudioRendererSink> audioRenderer = std::make_shared<FastAudioRendererSinkInner>();
213 
214     return audioRenderer;
215 }
216 
IsInited()217 bool FastAudioRendererSinkInner::IsInited()
218 {
219     return rendererInited_;
220 }
221 
DeInit()222 void FastAudioRendererSinkInner::DeInit()
223 {
224     AUDIO_INFO_LOG("In");
225 #ifdef FEATURE_POWER_MANAGER
226     KeepRunningUnlock();
227 
228 #endif
229 
230     started_ = false;
231     rendererInited_ = false;
232     if ((audioRender_ != nullptr) && (audioAdapter_ != nullptr)) {
233         AUDIO_INFO_LOG("Destroy render");
234         audioAdapter_->DestroyRender(audioAdapter_, renderId_);
235     }
236     audioRender_ = nullptr;
237 
238     if ((audioManager_ != nullptr) && (audioAdapter_ != nullptr)) {
239         AUDIO_INFO_LOG("Unload adapter");
240         if (routeHandle_ != -1) {
241             audioAdapter_->ReleaseAudioRoute(audioAdapter_, routeHandle_);
242         }
243         audioManager_->UnloadAdapter(audioManager_, adapterDesc_.adapterName);
244     }
245     audioAdapter_ = nullptr;
246     audioManager_ = nullptr;
247 
248     ReleaseMmapBuffer();
249 }
250 
InitAttrs(struct AudioSampleAttributes & attrs)251 void FastAudioRendererSinkInner::InitAttrs(struct AudioSampleAttributes &attrs)
252 {
253     /* Initialization of audio parameters for playback */
254     attrs.channelCount = AUDIO_CHANNELCOUNT;
255     attrs.interleaved = true;
256     attrs.streamId = attr_.audioStreamFlag == AUDIO_FLAG_VOIP_FAST ?
257         static_cast<int32_t>(GenerateUniqueID(AUDIO_HDI_RENDER_ID_BASE, HDI_RENDER_OFFSET_VOIP_FAST)) :
258         static_cast<int32_t>(GenerateUniqueID(AUDIO_HDI_RENDER_ID_BASE, HDI_RENDER_OFFSET_FAST));
259     attrs.period = DEEP_BUFFER_RENDER_PERIOD_SIZE;
260     attrs.isBigEndian = false;
261     attrs.isSignedData = true;
262     attrs.stopThreshold = INT_32_MAX;
263     attrs.silenceThreshold = 0;
264 }
265 
SwitchAdapterRender(struct AudioAdapterDescriptor * descs,string adapterNameCase,enum AudioPortDirection portFlag,struct AudioPort & renderPort,int32_t size)266 static int32_t SwitchAdapterRender(struct AudioAdapterDescriptor *descs, string adapterNameCase,
267     enum AudioPortDirection portFlag, struct AudioPort &renderPort, int32_t size)
268 {
269     if (descs == nullptr) {
270         return ERROR;
271     }
272     for (int32_t index = 0; index < size; index++) {
273         struct AudioAdapterDescriptor *desc = &descs[index];
274         if (desc == nullptr || desc->adapterName == nullptr) {
275             continue;
276         }
277         if (strcmp(desc->adapterName, adapterNameCase.c_str())) {
278             continue;
279         }
280         for (uint32_t port = 0; port < desc->portsLen; port++) {
281             // Only find out the port of out in the sound card
282             if (desc->ports[port].dir == portFlag) {
283                 renderPort = desc->ports[port];
284                 return index;
285             }
286         }
287     }
288     AUDIO_ERR_LOG("SwitchAdapterRender Fail");
289 
290     return ERR_INVALID_INDEX;
291 }
292 
InitAudioManager()293 int32_t FastAudioRendererSinkInner::InitAudioManager()
294 {
295     AUDIO_INFO_LOG("Initialize audio proxy manager");
296 
297     audioManager_ = IAudioManagerGet(false);
298     if (audioManager_ == nullptr) {
299         return ERR_INVALID_HANDLE;
300     }
301 
302     return 0;
303 }
304 
305 // UpdateSinkState must be called with FastAudioRendererSinkInner::mutex_ held
UpdateSinkState(bool started)306 void FastAudioRendererSinkInner::UpdateSinkState(bool started)
307 {
308     if (callback_) {
309         callback_->OnAudioSinkStateChange(sinkId_, started);
310     } else {
311         AUDIO_WARNING_LOG("AudioSinkCallback is nullptr");
312     }
313 }
314 
PcmFormatToBits(HdiAdapterFormat format)315 uint32_t PcmFormatToBits(HdiAdapterFormat format)
316 {
317     switch (format) {
318         case SAMPLE_U8:
319             return PCM_8_BIT;
320         case SAMPLE_S16LE:
321             return PCM_16_BIT;
322         case SAMPLE_S24LE:
323             return PCM_24_BIT;
324         case SAMPLE_S32LE:
325             return PCM_32_BIT;
326         case SAMPLE_F32LE:
327             return PCM_32_BIT;
328         default:
329             return PCM_24_BIT;
330     }
331 }
332 
GetMmapBufferInfo(int & fd,uint32_t & totalSizeInframe,uint32_t & spanSizeInframe,uint32_t & byteSizePerFrame)333 int32_t FastAudioRendererSinkInner::GetMmapBufferInfo(int &fd, uint32_t &totalSizeInframe, uint32_t &spanSizeInframe,
334     uint32_t &byteSizePerFrame)
335 {
336     CHECK_AND_RETURN_RET_LOG(bufferFd_ != INVALID_FD, ERR_INVALID_HANDLE, "buffer fd has been released!");
337     fd = bufferFd_;
338     totalSizeInframe = bufferTotalFrameSize_;
339     spanSizeInframe = eachReadFrameSize_;
340     byteSizePerFrame = PcmFormatToBits(attr_.format) * attr_.channel / PCM_8_BIT;
341     return SUCCESS;
342 }
343 
SetSinkMuteForSwitchDevice(bool mute)344 int32_t FastAudioRendererSinkInner::SetSinkMuteForSwitchDevice(bool mute)
345 {
346     std::lock_guard<std::mutex> lock(switchDeviceMutex_);
347     AUDIO_INFO_LOG("set %{public}s mute %{public}d", halName_.c_str(), mute);
348 
349     if (mute) {
350         muteCount_++;
351         if (switchDeviceMute_) {
352             AUDIO_INFO_LOG("%{public}s already muted", halName_.c_str());
353             return SUCCESS;
354         }
355         switchDeviceMute_ = true;
356         if (halName_ == MMAP_VOIP_HAL_NAME && audioRender_ != nullptr) {
357             audioRender_->SetVolume(audioRender_, 0.0f);
358         }
359     } else {
360         muteCount_--;
361         if (muteCount_ > 0) {
362             AUDIO_WARNING_LOG("%{public}s not all unmuted", halName_.c_str());
363             return SUCCESS;
364         }
365         switchDeviceMute_ = false;
366         muteCount_ = 0;
367         if (halName_ == MMAP_VOIP_HAL_NAME) {
368             SetVolume(leftVolume_, rightVolume_);
369         }
370     }
371 
372     return SUCCESS;
373 }
374 
GetMmapHandlePosition(uint64_t & frames,int64_t & timeSec,int64_t & timeNanoSec)375 int32_t FastAudioRendererSinkInner::GetMmapHandlePosition(uint64_t &frames, int64_t &timeSec, int64_t &timeNanoSec)
376 {
377     CHECK_AND_RETURN_RET_LOG(audioRender_ != nullptr, ERR_INVALID_HANDLE, "Audio render is null!");
378 
379     struct AudioTimeStamp timestamp = {};
380     int32_t ret = audioRender_->GetMmapPosition(audioRender_, &frames, &timestamp);
381     CHECK_AND_RETURN_RET_LOG(ret == 0, ERR_OPERATION_FAILED, "Hdi GetMmapPosition filed, ret:%{public}d!", ret);
382 #ifdef DEBUG_DIRECT_USE_HDI
383     alreadyReadFrames_ = frames; // frames already read.
384     curReadPos_ = frameSizeInByte_ * (frames - bufferTotalFrameSize_ * (frames / bufferTotalFrameSize_));
385     CHECK_AND_RETURN_RET_LOG((curReadPos_ >= 0 && curReadPos_ < bufferSize_), ERR_INVALID_PARAM, "curReadPos invalid");
386     AUDIO_DEBUG_LOG("GetMmapHandlePosition frames[:%{public}" PRIu64 "] tvsec:%{public}" PRId64 " tvNSec:"
387         "%{public}" PRId64 " alreadyReadFrames:%{public}" PRId64 " curReadPos[%{public}d]",
388         frames, timestamp.tvSec, timestamp.tvNSec, alreadyReadFrames_, curReadPos_);
389 #endif
390 
391     int64_t maxSec = 9223372036; // (9223372036 + 1) * 10^9 > INT64_MAX, seconds should not bigger than it.
392     CHECK_AND_RETURN_RET_LOG(timestamp.tvSec >= 0 && timestamp.tvSec <= maxSec && timestamp.tvNSec >= 0 &&
393         timestamp.tvNSec <= SECOND_TO_NANOSECOND, ERR_OPERATION_FAILED,
394         "Hdi GetMmapPosition get invaild second:%{public}" PRId64 " or nanosecond:%{public}" PRId64 " !",
395         timestamp.tvSec, timestamp.tvNSec);
396     timeSec = timestamp.tvSec;
397     timeNanoSec = timestamp.tvNSec;
398 
399     return SUCCESS;
400 }
401 
ReleaseMmapBuffer()402 void FastAudioRendererSinkInner::ReleaseMmapBuffer()
403 {
404 #ifdef DEBUG_DIRECT_USE_HDI
405     if (bufferAddresss_ != nullptr) {
406         munmap(bufferAddresss_, bufferSize_);
407         bufferAddresss_ = nullptr;
408         bufferSize_ = 0;
409         AUDIO_INFO_LOG("ReleaseMmapBuffer end.");
410     } else {
411         AUDIO_WARNING_LOG("ReleaseMmapBuffer buffer already null.");
412     }
413     if (privFd_ != INVALID_FD) {
414         CloseFd(privFd_);
415         privFd_ = INVALID_FD;
416     }
417 #endif
418     if (bufferFd_ != INVALID_FD) {
419         CloseFd(bufferFd_);
420         bufferFd_ = INVALID_FD;
421     }
422 }
423 
PrepareMmapBuffer()424 int32_t FastAudioRendererSinkInner::PrepareMmapBuffer()
425 {
426     uint32_t totalBufferInMs = 40; // 5 * (6 + 2 * (1)) = 40ms, the buffer size, not latency.
427     frameSizeInByte_ = PcmFormatToBits(attr_.format) * attr_.channel / PCM_8_BIT;
428     uint32_t reqBufferFrameSize = totalBufferInMs * (attr_.sampleRate / 1000);
429 
430     struct AudioMmapBufferDescriptor desc = {0};
431     int32_t ret = audioRender_->ReqMmapBuffer(audioRender_, reqBufferFrameSize, &desc);
432     CHECK_AND_RETURN_RET_LOG(ret == 0, ERR_OPERATION_FAILED, "ReqMmapBuffer failed, ret:%{public}d", ret);
433     AUDIO_INFO_LOG("AudioMmapBufferDescriptor memoryAddress[%{private}p] memoryFd[%{public}d] totalBufferFrames"
434         "[%{public}d] transferFrameSize[%{public}d] isShareable[%{public}d] offset[%{public}d]", desc.memoryAddress,
435         desc.memoryFd, desc.totalBufferFrames, desc.transferFrameSize, desc.isShareable, desc.offset);
436 
437     bufferFd_ = desc.memoryFd; // fcntl(fd, 1030,3) after dup?
438     int32_t periodFrameMaxSize = 1920000; // 192khz * 10s
439     CHECK_AND_RETURN_RET_LOG(desc.totalBufferFrames >= 0 && desc.transferFrameSize >= 0 &&
440         desc.transferFrameSize <= periodFrameMaxSize, ERR_OPERATION_FAILED,
441         "ReqMmapBuffer invalid values: totalBufferFrames[%{public}d] transferFrameSize[%{public}d]",
442         desc.totalBufferFrames, desc.transferFrameSize);
443     bufferTotalFrameSize_ = static_cast<uint32_t>(desc.totalBufferFrames); // 1440 ~ 3840
444     eachReadFrameSize_ = desc.transferFrameSize; // 240
445 
446     CHECK_AND_RETURN_RET_LOG(frameSizeInByte_ <= ULLONG_MAX / bufferTotalFrameSize_, ERR_OPERATION_FAILED,
447         "BufferSize will overflow!");
448     bufferSize_ = bufferTotalFrameSize_ * frameSizeInByte_;
449 #ifdef DEBUG_DIRECT_USE_HDI
450     privFd_ = dup(bufferFd_);
451     bufferAddresss_ = (char *)mmap(nullptr, bufferSize_, PROT_READ | PROT_WRITE, MAP_SHARED, privFd_, 0);
452     CHECK_AND_RETURN_RET_LOG(bufferAddresss_ != nullptr && bufferAddresss_ != MAP_FAILED, ERR_OPERATION_FAILED,
453         "mmap buffer failed!");
454 #endif
455     return SUCCESS;
456 }
457 
ConvertToHdiFormat(HdiAdapterFormat format)458 AudioFormat FastAudioRendererSinkInner::ConvertToHdiFormat(HdiAdapterFormat format)
459 {
460     AudioFormat hdiFormat;
461     switch (format) {
462         case SAMPLE_U8:
463             hdiFormat = AUDIO_FORMAT_TYPE_PCM_8_BIT;
464             break;
465         case SAMPLE_S16:
466             hdiFormat = AUDIO_FORMAT_TYPE_PCM_16_BIT;
467             break;
468         case SAMPLE_S24:
469             hdiFormat = AUDIO_FORMAT_TYPE_PCM_24_BIT;
470             break;
471         case SAMPLE_S32:
472             hdiFormat = AUDIO_FORMAT_TYPE_PCM_32_BIT;
473             break;
474         case SAMPLE_F32:
475             hdiFormat = AUDIO_FORMAT_TYPE_PCM_32_BIT;
476             break;
477         default:
478             hdiFormat = AUDIO_FORMAT_TYPE_PCM_16_BIT;
479             break;
480     }
481 
482     return hdiFormat;
483 }
484 
CreateRender(const struct AudioPort & renderPort)485 int32_t FastAudioRendererSinkInner::CreateRender(const struct AudioPort &renderPort)
486 {
487     int32_t ret;
488     struct AudioSampleAttributes param;
489     InitAttrs(param);
490     param.type = attr_.audioStreamFlag == AUDIO_FLAG_VOIP_FAST ? AUDIO_MMAP_VOIP : AUDIO_MMAP_NOIRQ;
491     param.sampleRate = attr_.sampleRate;
492     param.channelCount = attr_.channel;
493     if (param.channelCount == MONO) {
494         param.channelLayout = CH_LAYOUT_MONO;
495     } else if (param.channelCount == STEREO) {
496         param.channelLayout = CH_LAYOUT_STEREO;
497     }
498     param.format = ConvertToHdiFormat(attr_.format);
499     param.frameSize = PcmFormatToBits(attr_.format) * param.channelCount / PCM_8_BIT;
500     param.startThreshold = DEEP_BUFFER_RENDER_PERIOD_SIZE / (param.frameSize); // not passed in hdi
501     AUDIO_INFO_LOG("Type: %{public}d, sampleRate: %{public}u, channel: %{public}d, format: %{public}d, "
502         "device:%{public}d", param.type, param.sampleRate, param.channelCount, param.format, attr_.deviceType);
503     struct AudioDeviceDescriptor deviceDesc;
504     deviceDesc.portId = renderPort.portId;
505     switch (static_cast<DeviceType>(attr_.deviceType)) {
506         case DEVICE_TYPE_EARPIECE:
507             deviceDesc.pins = PIN_OUT_EARPIECE;
508             break;
509         case DEVICE_TYPE_SPEAKER:
510             deviceDesc.pins = PIN_OUT_SPEAKER;
511             break;
512         case DEVICE_TYPE_WIRED_HEADSET:
513             deviceDesc.pins = PIN_OUT_HEADSET;
514             break;
515         case DEVICE_TYPE_USB_HEADSET:
516             deviceDesc.pins = PIN_OUT_USB_EXT;
517             break;
518         case DEVICE_TYPE_BLUETOOTH_SCO:
519             deviceDesc.pins = PIN_OUT_BLUETOOTH_SCO;
520             break;
521         default:
522             deviceDesc.pins = PIN_OUT_SPEAKER;
523             break;
524     }
525     char desc[] = "";
526     deviceDesc.desc = desc;
527     ret = audioAdapter_->CreateRender(audioAdapter_, &deviceDesc, &param, &audioRender_, &renderId_);
528     if (ret != 0 || audioRender_ == nullptr) {
529         AUDIO_ERR_LOG("AudioDeviceCreateRender failed, ret is :%{public}d", ret);
530         audioManager_->UnloadAdapter(audioManager_, adapterDesc_.adapterName);
531         return ERR_NOT_STARTED;
532     }
533 
534     return SUCCESS;
535 }
536 
Init(const IAudioSinkAttr & attr)537 int32_t FastAudioRendererSinkInner::Init(const IAudioSinkAttr &attr)
538 {
539     AUDIO_INFO_LOG("FastAudioRendererSinkInner::Init");
540     attr_ = attr;
541     adapterNameCase_ = attr_.adapterName;  // Set sound card information
542     halName_ = attr_.audioStreamFlag == AUDIO_FLAG_MMAP ? MMAP_PRIMARY_HAL_NAME : MMAP_VOIP_HAL_NAME;
543     enum AudioPortDirection port = PORT_OUT; // Set port information
544 
545     CHECK_AND_RETURN_RET_LOG(InitAudioManager() == 0, ERR_NOT_STARTED, "Init audio manager Fail");
546 
547     uint32_t size = MAX_AUDIO_ADAPTER_NUM;
548     AudioAdapterDescriptor descs[MAX_AUDIO_ADAPTER_NUM];
549     if (audioManager_ == nullptr) {
550         AUDIO_ERR_LOG("The audioManager is nullptr!");
551         return ERROR;
552     }
553     int32_t ret = audioManager_->GetAllAdapters(audioManager_,
554         (struct AudioAdapterDescriptor *)&descs, &size);
555     CHECK_AND_RETURN_RET_LOG(size <= MAX_AUDIO_ADAPTER_NUM && size != 0 && ret == 0, ERR_NOT_STARTED,
556         "Get adapters Fail");
557 
558     int32_t index = SwitchAdapterRender((struct AudioAdapterDescriptor *)&descs, adapterNameCase_, port, audioPort_,
559         size);
560     CHECK_AND_RETURN_RET_LOG(index >= 0, ERR_NOT_STARTED, "Switch Adapter Fail");
561 
562     adapterDesc_ = descs[index];
563     int32_t result = audioManager_->LoadAdapter(audioManager_, &adapterDesc_, &audioAdapter_);
564     CHECK_AND_RETURN_RET_LOG(result == 0, ERR_NOT_STARTED, "Load Adapter Fail");
565     CHECK_AND_RETURN_RET_LOG(audioAdapter_ != nullptr, ERR_NOT_STARTED, "Load audio device failed");
566 
567     // Initialization port information, can fill through mode and other parameters
568     ret = audioAdapter_->InitAllPorts(audioAdapter_);
569     CHECK_AND_RETURN_RET_LOG(ret == 0, ERR_NOT_STARTED, "InitAllPorts failed");
570 
571     CHECK_AND_RETURN_RET_LOG(CreateRender(audioPort_) == SUCCESS && PrepareMmapBuffer() == SUCCESS,
572         ERR_NOT_STARTED, "Create render failed, Audio Port: %{public}d", audioPort_.portId);
573 
574     rendererInited_ = true;
575     GetRenderId(sinkId_);
576 
577     return SUCCESS;
578 }
579 
PreparePosition()580 void FastAudioRendererSinkInner::PreparePosition()
581 {
582 #ifdef DEBUG_DIRECT_USE_HDI
583     isFirstWrite_ = false;
584     uint64_t frames = 0;
585     int64_t timeSec = 0;
586     int64_t timeNanoSec = 0;
587     GetMmapHandlePosition(frames, timeSec, timeNanoSec); // get first start position
588     int32_t periodByteSize = eachReadFrameSize_ * frameSizeInByte_;
589     CHECK_AND_RETURN_LOG(periodByteSize * writeAheadPeriod_ <= ULLONG_MAX - curReadPos_, "TempPos will overflow!");
590     size_t tempPos = curReadPos_ + periodByteSize * writeAheadPeriod_; // 1 period ahead
591     curWritePos_ = (tempPos < bufferSize_ ? tempPos : tempPos - bufferSize_);
592     AUDIO_INFO_LOG("First render frame start with curReadPos_[%{public}d] curWritePos_[%{public}d]", curReadPos_,
593         curWritePos_);
594 #endif
595 }
596 
RenderFrame(char & data,uint64_t len,uint64_t & writeLen)597 int32_t FastAudioRendererSinkInner::RenderFrame(char &data, uint64_t len, uint64_t &writeLen)
598 {
599 #ifdef DEBUG_DIRECT_USE_HDI
600     int64_t stamp = ClockTime::GetCurNano();
601     CHECK_AND_RETURN_RET_LOG(audioRender_ != nullptr, ERR_INVALID_HANDLE, "Audio Render Handle is nullptr!");
602 
603     if (len > (bufferSize_ - eachReadFrameSize_ * frameSizeInByte_ * writeAheadPeriod_)) {
604         writeLen = 0;
605         AUDIO_ERR_LOG("RenderFrame failed,too large len[%{public}" PRIu64 "]!", len);
606         return ERR_WRITE_FAILED;
607     }
608 
609     if (isFirstWrite_) {
610         PreparePosition();
611     }
612 
613     CHECK_AND_RETURN_RET_LOG((curWritePos_ >= 0 && curWritePos_ < bufferSize_), ERR_INVALID_PARAM,
614         "curWritePos_ invalid");
615     char *writePtr = bufferAddresss_ + curWritePos_;
616     uint64_t dataBefore = *(uint64_t *)writePtr;
617     uint64_t dataAfter = 0;
618     uint64_t tempPos = curWritePos_ + len;
619     if (tempPos <= bufferSize_) {
620         if (memcpy_s(writePtr, (bufferSize_ - curWritePos_), static_cast<void *>(&data), len)) {
621             AUDIO_ERR_LOG("copy failed");
622             return ERR_WRITE_FAILED;
623         }
624         dataAfter = *(uint64_t *)writePtr;
625         curWritePos_ = (tempPos == bufferSize_ ? 0 : tempPos);
626     } else {
627         AUDIO_DEBUG_LOG("(tempPos%{public}" PRIu64 ")curWritePos_ + len > bufferSize_", tempPos);
628         size_t writeableSize = bufferSize_ - curWritePos_;
629         if (memcpy_s(writePtr, writeableSize, static_cast<void *>(&data), writeableSize) ||
630             memcpy_s(bufferAddresss_, bufferSize_, static_cast<void *>((char *)&data + writeableSize),
631             (len - writeableSize))) {
632             AUDIO_ERR_LOG("copy failed");
633             return ERR_WRITE_FAILED;
634         }
635         curWritePos_ = len - writeableSize;
636     }
637     writeLen = len;
638 
639     stamp = (ClockTime::GetCurNano() - stamp) / AUDIO_US_PER_SECOND;
640     AUDIO_DEBUG_LOG("Render len[%{public}" PRIu64 "] cost[%{public}" PRId64 "]ms curWritePos[%{public}d] dataBefore"
641         "<%{public}" PRIu64 "> dataAfter<%{public}" PRIu64 ">", len, stamp, curWritePos_, dataBefore, dataAfter);
642     return SUCCESS;
643 #else
644     AUDIO_WARNING_LOG("RenderFrame is not supported.");
645     return ERR_NOT_SUPPORTED;
646 #endif
647 }
648 
GetMaxAmplitude()649 float FastAudioRendererSinkInner::GetMaxAmplitude()
650 {
651     AUDIO_WARNING_LOG("getMaxAmplitude in fast_audio_renderder_sink not support");
652     return 0;
653 }
654 
SetPaPower(int32_t flag)655 int32_t FastAudioRendererSinkInner::SetPaPower(int32_t flag)
656 {
657     (void)flag;
658     return ERR_NOT_SUPPORTED;
659 }
660 
SetPriPaPower()661 int32_t FastAudioRendererSinkInner::SetPriPaPower()
662 {
663     return ERR_NOT_SUPPORTED;
664 }
665 
CheckPositionTime()666 int32_t FastAudioRendererSinkInner::CheckPositionTime()
667 {
668     int32_t tryCount = 50;
669     uint64_t frames = 0;
670     int64_t timeSec = 0;
671     int64_t timeNanoSec = 0;
672     int64_t maxHandleCost = attr_.audioStreamFlag == AUDIO_FLAG_VOIP_FAST ? VOIP_MAX_HANDLE_COST_IN_NANOSEC :
673         GENERAL_MAX_HANDLE_COST_IN_NANOSEC;
674     int64_t waitTime = 2000000; // 2ms
675     while (tryCount-- > 0) {
676         ClockTime::RelativeSleep(waitTime); // us
677         int64_t timeBeforeGetPos = ClockTime::GetCurNano();
678         int32_t ret = GetMmapHandlePosition(frames, timeSec, timeNanoSec);
679         int64_t curSec = timeBeforeGetPos / AUDIO_NS_PER_SECOND;
680         int64_t curNanoSec = timeBeforeGetPos - curSec * AUDIO_NS_PER_SECOND;
681         AUDIO_WARNING_LOG("DspSec: %{public}" PRId64 ", dspNanoSec: %{public}" PRId64 ", Time before get pos: "
682             "%{public}" PRId64 ", time cost: %{public}" PRId64 "", timeSec, timeNanoSec, timeBeforeGetPos,
683             ClockTime::GetCurNano() - timeBeforeGetPos);
684         if (ret != SUCCESS || curSec != timeSec || curNanoSec - timeNanoSec > maxHandleCost) {
685             continue;
686         } else {
687             AUDIO_INFO_LOG("CheckPositionTime end, position and time is ok.");
688             return SUCCESS;
689         }
690     }
691 #ifdef FEATURE_POWER_MANAGER
692     KeepRunningUnlock();
693 #endif
694     AUDIO_ERR_LOG("Stop hdi fast renderer when GetMmapPosition failed");
695     CHECK_AND_RETURN_RET_LOG(audioRender_ != nullptr, ERR_INVALID_HANDLE,
696         "audioRenderer_ is nullptr when trying to stop");
697     int32_t ret = audioRender_->Stop(audioRender_);
698     UpdateSinkState(false);
699     CHECK_AND_RETURN_RET_LOG(ret == 0, ERR_OPERATION_FAILED, "Stop failed! ret: %{public}d.", ret);
700     return ERROR;
701 }
702 
Start(void)703 int32_t FastAudioRendererSinkInner::Start(void)
704 {
705     std::lock_guard<std::mutex> lock(mutex_);
706     Trace trace("FastAudioRendererSinkInner::Start");
707     AudioXCollie sourceXCollie("FastAudioRendererSinkInner::Start", XCOLLIE_TIME_OUT_SECONDS);
708     AUDIO_INFO_LOG("FastAudioRendererSinkInner::Start, sinkId %{public}u", sinkId_);
709     int64_t stamp = ClockTime::GetCurNano();
710     int32_t ret;
711 
712     CHECK_AND_RETURN_RET_LOG(audioRender_ != nullptr, ERR_INVALID_HANDLE,
713         "FastAudioRendererSink::Start audioRender_ null!");
714 
715     if (!started_) {
716         ret = audioRender_->Start(audioRender_);
717         CHECK_AND_RETURN_RET_LOG(ret == 0, ERR_NOT_STARTED,
718             "FastAudioRendererSink::Start failed!");
719         UpdateSinkState(true);
720         int32_t err = CheckPositionTime();
721         CHECK_AND_RETURN_RET_LOG(err == SUCCESS, ERR_NOT_STARTED,
722             "FastAudioRendererSink::CheckPositionTime failed!");
723     }
724 #ifdef FEATURE_POWER_MANAGER
725     KeepRunningLock();
726 #endif
727     started_ = true;
728     AUDIO_DEBUG_LOG("Start cost[%{public}" PRId64 "]ms", (ClockTime::GetCurNano() - stamp) / AUDIO_US_PER_SECOND);
729     AudioPerformanceMonitor::GetInstance().RecordTimeStamp(ADAPTER_TYPE_FAST, INIT_LASTWRITTEN_TIME);
730     return SUCCESS;
731 }
732 #ifdef FEATURE_POWER_MANAGER
KeepRunningLock()733 void FastAudioRendererSinkInner::KeepRunningLock()
734 {
735     std::shared_ptr<PowerMgr::RunningLock> keepRunningLock;
736     if (runningLockManager_ == nullptr) {
737         WatchTimeout guard("PowerMgr::PowerMgrClient::GetInstance().CreateRunningLock:KeepRunningLock");
738         keepRunningLock = PowerMgr::PowerMgrClient::GetInstance().CreateRunningLock("AudioFastBackgroundPlay",
739             PowerMgr::RunningLockType::RUNNINGLOCK_BACKGROUND_AUDIO);
740         guard.CheckCurrTimeout();
741         if (keepRunningLock) {
742             runningLockManager_ = std::make_shared<AudioRunningLockManager<PowerMgr::RunningLock>> (keepRunningLock);
743         }
744     }
745 
746     if (runningLockManager_ != nullptr) {
747         int32_t timeOut = -1; // -1 for lasting.
748         AUDIO_INFO_LOG("keepRunningLock lock result: %{public}d",
749             runningLockManager_->Lock(timeOut)); // -1 for lasting.
750     } else {
751         AUDIO_ERR_LOG("keepRunningLock is null, playback can not work well!");
752     }
753 }
754 #endif
755 
756 #ifdef FEATURE_POWER_MANAGER
KeepRunningUnlock()757 void FastAudioRendererSinkInner::KeepRunningUnlock()
758 {
759     if (runningLockManager_ != nullptr) {
760         AUDIO_INFO_LOG("keepRunningLock unLock");
761         runningLockManager_->UnLock();
762     } else {
763         AUDIO_WARNING_LOG("keepRunningLock is null, playback can not work well!");
764     }
765 }
766 #endif
767 
768 
SetVolume(float left,float right)769 int32_t FastAudioRendererSinkInner::SetVolume(float left, float right)
770 {
771     int32_t ret;
772     float volume;
773 
774     CHECK_AND_RETURN_RET_LOG(audioRender_ != nullptr, ERR_INVALID_HANDLE,
775         "FastAudioRendererSink::SetVolume failed audioRender_ null");
776     if (halName_ == MMAP_VOIP_HAL_NAME && switchDeviceMute_ && (abs(left) > FLOAT_EPS || abs(right) > FLOAT_EPS)) {
777         AUDIO_ERR_LOG("Mmap voip scene. No need set to volume when switch device and volume is 0");
778         leftVolume_ = left;
779         rightVolume_ = right;
780         return ERR_INVALID_HANDLE;
781     }
782 
783     leftVolume_ = left;
784     rightVolume_ = right;
785     if ((abs(leftVolume_) < FLOAT_EPS) && (abs(rightVolume_) > FLOAT_EPS)) {
786         volume = rightVolume_;
787     } else if ((abs(leftVolume_) > FLOAT_EPS) && (abs(rightVolume_) < FLOAT_EPS)) {
788         volume = leftVolume_;
789     } else {
790         volume = (leftVolume_ + rightVolume_) / HALF_FACTOR;
791     }
792 
793     AUDIO_INFO_LOG("Set hdi volume to %{public}f", volume);
794     ret = audioRender_->SetVolume(audioRender_, volume);
795     if (ret) {
796         AUDIO_ERR_LOG("FastAudioRendererSink::Set volume failed!");
797     }
798 
799     return ret;
800 }
801 
GetVolume(float & left,float & right)802 int32_t FastAudioRendererSinkInner::GetVolume(float &left, float &right)
803 {
804     left = leftVolume_;
805     right = rightVolume_;
806     return SUCCESS;
807 }
808 
SetVoiceVolume(float volume)809 int32_t FastAudioRendererSinkInner::SetVoiceVolume(float volume)
810 {
811     AUDIO_ERR_LOG("FastAudioRendererSink SetVoiceVolume not supported.");
812     return ERR_NOT_SUPPORTED;
813 }
814 
GetAudioScene()815 int32_t FastAudioRendererSinkInner::GetAudioScene()
816 {
817     AUDIO_ERR_LOG("FastAudioRendererSink GetAudioScene not supported.");
818     return ERR_NOT_SUPPORTED;
819 }
820 
SetAudioScene(AudioScene audioScene,std::vector<DeviceType> & activeDevices)821 int32_t FastAudioRendererSinkInner::SetAudioScene(AudioScene audioScene, std::vector<DeviceType> &activeDevices)
822 {
823     AUDIO_ERR_LOG("FastAudioRendererSink SetAudioScene not supported.");
824     return ERR_NOT_SUPPORTED;
825 }
826 
SetOutputRoutes(std::vector<DeviceType> & outputDevices)827 int32_t FastAudioRendererSinkInner::SetOutputRoutes(std::vector<DeviceType> &outputDevices)
828 {
829     AUDIO_ERR_LOG("SetOutputRoutes not supported.");
830     return ERR_NOT_SUPPORTED;
831 }
832 
SetAudioParameter(const AudioParamKey key,const std::string & condition,const std::string & value)833 void FastAudioRendererSinkInner::SetAudioParameter(const AudioParamKey key, const std::string &condition,
834     const std::string &value)
835 {
836     AUDIO_ERR_LOG("FastAudioRendererSink SetAudioParameter not supported.");
837     return;
838 }
839 
GetAudioParameter(const AudioParamKey key,const std::string & condition)840 std::string FastAudioRendererSinkInner::GetAudioParameter(const AudioParamKey key, const std::string &condition)
841 {
842     AUDIO_INFO_LOG("GetAudioParameter, key: %{public}d, condition: %{public}s",
843         key, condition.c_str());
844     AudioExtParamKey hdiKey = AudioExtParamKey(key);
845     char value[DumpFileUtil::PARAM_VALUE_LENTH];
846     CHECK_AND_RETURN_RET_LOG(audioAdapter_ != nullptr, "",
847         "GetAudioParameter failed, audioAdapter_ is null");
848     int32_t ret = audioAdapter_->GetExtraParams(audioAdapter_, hdiKey, condition.c_str(), value,
849         DumpFileUtil::PARAM_VALUE_LENTH);
850     CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, "",
851         "FRSink GetAudioParameter failed, error code:%{public}d", ret);
852     return value;
853 }
854 
RegisterAudioSinkCallback(IAudioSinkCallback * callback)855 void FastAudioRendererSinkInner::RegisterAudioSinkCallback(IAudioSinkCallback* callback)
856 {
857     std::lock_guard<std::mutex> lock(mutex_);
858     if (callback_) {
859         AUDIO_INFO_LOG("AudioSinkCallback registered");
860     } else {
861         callback_ = callback;
862         AUDIO_INFO_LOG("Register AudioSinkCallback");
863     }
864 }
865 
SetAudioMonoState(bool audioMono)866 void FastAudioRendererSinkInner::SetAudioMonoState(bool audioMono)
867 {
868     AUDIO_ERR_LOG("FastAudioRendererSink SetAudioMonoState not supported.");
869     return;
870 }
871 
SetAudioBalanceValue(float audioBalance)872 void FastAudioRendererSinkInner::SetAudioBalanceValue(float audioBalance)
873 {
874     AUDIO_ERR_LOG("FastAudioRendererSink SetAudioBalanceValue not supported.");
875     return;
876 }
877 
GetPresentationPosition(uint64_t & frames,int64_t & timeSec,int64_t & timeNanoSec)878 int32_t FastAudioRendererSinkInner::GetPresentationPosition(uint64_t& frames, int64_t& timeSec, int64_t& timeNanoSec)
879 {
880     AUDIO_ERR_LOG("FastAudioRendererSink GetPresentationPosition not supported.");
881     return ERR_NOT_SUPPORTED;
882 }
883 
GetTransactionId(uint64_t * transactionId)884 int32_t FastAudioRendererSinkInner::GetTransactionId(uint64_t *transactionId)
885 {
886     AUDIO_ERR_LOG("FastAudioRendererSink %{public}s", __func__);
887     *transactionId = 6; // 6 is the mmap device.
888     return ERR_NOT_SUPPORTED;
889 }
890 
GetLatency(uint32_t * latency)891 int32_t FastAudioRendererSinkInner::GetLatency(uint32_t *latency)
892 {
893     Trace trace("FastAudioRendererSinkInner::GetLatency");
894     CHECK_AND_RETURN_RET_LOG(audioRender_ != nullptr, ERR_INVALID_HANDLE,
895         "GetLatency failed audio render null");
896 
897     CHECK_AND_RETURN_RET_LOG(latency, ERR_INVALID_PARAM,
898         "GetLatency failed latency null");
899 
900     uint32_t hdiLatency;
901     if (audioRender_->GetLatency(audioRender_, &hdiLatency) == 0) {
902         *latency = hdiLatency;
903         return SUCCESS;
904     } else {
905         return ERR_OPERATION_FAILED;
906     }
907 }
908 
Stop(void)909 int32_t FastAudioRendererSinkInner::Stop(void)
910 {
911     std::lock_guard<std::mutex> lock(mutex_);
912     Trace trace("FastAudioRendererSinkInner::Stop");
913     AudioXCollie sourceXCollie("FastAudioRendererSinkInner::Stop", XCOLLIE_TIME_OUT_SECONDS);
914     AUDIO_INFO_LOG("Stop, sinkId %{public}u", sinkId_);
915 
916     CHECK_AND_RETURN_RET_LOG(audioRender_ != nullptr, ERR_INVALID_HANDLE,
917         "Stop failed audioRender_ null");
918 #ifdef FEATURE_POWER_MANAGER
919     KeepRunningUnlock();
920 #endif
921 
922     if (started_) {
923         int32_t ret = audioRender_->Stop(audioRender_);
924         UpdateSinkState(false);
925         CHECK_AND_RETURN_RET_LOG(ret == 0, ERR_OPERATION_FAILED,
926             "Stop failed! ret: %{public}d.", ret);
927     }
928     started_ = false;
929 
930     return SUCCESS;
931 }
932 
Pause(void)933 int32_t FastAudioRendererSinkInner::Pause(void)
934 {
935     int32_t ret;
936 
937     CHECK_AND_RETURN_RET_LOG(audioRender_ != nullptr, ERR_INVALID_HANDLE,
938         "Pause failed audioRender_ null");
939 
940     CHECK_AND_RETURN_RET_LOG(started_, ERR_OPERATION_FAILED,
941         "Pause invalid state!");
942 
943     if (!paused_) {
944         ret = audioRender_->Pause(audioRender_);
945         CHECK_AND_RETURN_RET_LOG(ret == 0, ERR_OPERATION_FAILED,
946             "Pause failed!");
947     }
948     paused_ = true;
949 
950     return SUCCESS;
951 }
952 
Resume(void)953 int32_t FastAudioRendererSinkInner::Resume(void)
954 {
955     Trace trace("FastAudioRendererSinkInner::Resume");
956     int32_t ret;
957 
958     CHECK_AND_RETURN_RET_LOG(audioRender_ != nullptr, ERR_INVALID_HANDLE,
959         "Resume failed audioRender_ null");
960 
961     CHECK_AND_RETURN_RET_LOG(started_, ERR_OPERATION_FAILED,
962         "Resume invalid state!");
963 
964     if (paused_) {
965         ret = audioRender_->Resume(audioRender_);
966         CHECK_AND_RETURN_RET_LOG(ret == 0, ERR_OPERATION_FAILED,
967             "Resume failed!");
968     }
969     paused_ = false;
970     AudioPerformanceMonitor::GetInstance().RecordTimeStamp(ADAPTER_TYPE_FAST, INIT_LASTWRITTEN_TIME);
971     return SUCCESS;
972 }
973 
SuspendRenderSink(void)974 int32_t FastAudioRendererSinkInner::SuspendRenderSink(void)
975 {
976     return SUCCESS;
977 }
978 
RestoreRenderSink(void)979 int32_t FastAudioRendererSinkInner::RestoreRenderSink(void)
980 {
981     return SUCCESS;
982 }
983 
Reset(void)984 int32_t FastAudioRendererSinkInner::Reset(void)
985 {
986     Trace trace("FastAudioRendererSinkInner::Reset");
987     int32_t ret;
988 
989     if (started_ && audioRender_ != nullptr) {
990         ret = audioRender_->Flush(audioRender_);
991 
992         CHECK_AND_RETURN_RET_LOG(ret == 0, ERR_OPERATION_FAILED,
993             "Reset failed!");
994     }
995 
996     return SUCCESS;
997 }
998 
Flush(void)999 int32_t FastAudioRendererSinkInner::Flush(void)
1000 {
1001     Trace trace("FastAudioRendererSinkInner::Flush");
1002     int32_t ret;
1003 
1004     if (started_ && audioRender_ != nullptr) {
1005         ret = audioRender_->Flush(audioRender_);
1006         CHECK_AND_RETURN_RET_LOG(ret == 0, ERR_OPERATION_FAILED,
1007             "Flush failed!");
1008     }
1009 
1010     return SUCCESS;
1011 }
1012 
ResetOutputRouteForDisconnect(DeviceType device)1013 void FastAudioRendererSinkInner::ResetOutputRouteForDisconnect(DeviceType device)
1014 {
1015 }
1016 
UpdateAppsUid(const int32_t appsUid[MAX_MIX_CHANNELS],const size_t size)1017 int32_t FastAudioRendererSinkInner::UpdateAppsUid(const int32_t appsUid[MAX_MIX_CHANNELS],
1018     const size_t size)
1019 {
1020     return SUCCESS;
1021 }
1022 
UpdateAppsUid(const std::vector<int32_t> & appsUid)1023 int32_t FastAudioRendererSinkInner::UpdateAppsUid(const std::vector<int32_t> &appsUid)
1024 {
1025 #ifdef FEATURE_POWER_MANAGER
1026     if (!runningLockManager_) {
1027         return ERROR;
1028     }
1029 
1030     runningLockManager_->UpdateAppsUid(appsUid.cbegin(), appsUid.cend());
1031     runningLockManager_->UpdateAppsUidToPowerMgr();
1032 #endif
1033 
1034     return SUCCESS;
1035 }
1036 
GetRenderId(uint32_t & renderId) const1037 int32_t FastAudioRendererSinkInner::GetRenderId(uint32_t &renderId) const
1038 {
1039     renderId = GenerateUniqueID(AUDIO_HDI_RENDER_ID_BASE, HDI_RENDER_OFFSET_FAST);
1040     return SUCCESS;
1041 }
1042 } // namespace AudioStandard
1043 } // namespace OHOS
1044