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 #ifndef LOG_TAG
16 #define LOG_TAG "RemoteAudioRendererSinkInner"
17 #endif
18 
19 #include "remote_audio_renderer_sink.h"
20 
21 #include <chrono>
22 #include <cinttypes>
23 #include <cstdio>
24 #include <cstring>
25 #include <dlfcn.h>
26 #include <list>
27 #include <string>
28 #include <sstream>
29 #include <unistd.h>
30 #include <map>
31 #include <mutex>
32 #include "securec.h"
33 #include <algorithm>
34 
35 #include <v1_0/iaudio_manager.h>
36 
37 #include "audio_errors.h"
38 #include "audio_hdi_log.h"
39 #include "audio_utils.h"
40 #include "i_audio_device_adapter.h"
41 #include "i_audio_device_manager.h"
42 #include "audio_log_utils.h"
43 #include "audio_dump_pcm.h"
44 
45 using namespace std;
46 using OHOS::HDI::DistributedAudio::Audio::V1_0::IAudioAdapter;
47 using OHOS::HDI::DistributedAudio::Audio::V1_0::AudioAdapterDescriptor;
48 using OHOS::HDI::DistributedAudio::Audio::V1_0::AudioFormat;
49 using OHOS::HDI::DistributedAudio::Audio::V1_0::AudioPort;
50 using OHOS::HDI::DistributedAudio::Audio::V1_0::AudioPortDirection;
51 using OHOS::HDI::DistributedAudio::Audio::V1_0::IAudioManager;
52 using OHOS::HDI::DistributedAudio::Audio::V1_0::IAudioRender;
53 using OHOS::HDI::DistributedAudio::Audio::V1_0::AudioSampleAttributes;
54 using OHOS::HDI::DistributedAudio::Audio::V1_0::AudioDeviceDescriptor;
55 using OHOS::HDI::DistributedAudio::Audio::V1_0::AudioCategory;
56 using OHOS::HDI::DistributedAudio::Audio::V1_0::AudioRouteNode;
57 using OHOS::HDI::DistributedAudio::Audio::V1_0::AudioExtParamKey;
58 using OHOS::HDI::DistributedAudio::Audio::V1_0::AudioRoute;
59 using OHOS::HDI::DistributedAudio::Audio::V1_0::AudioSceneDescriptor;
60 using OHOS::HDI::DistributedAudio::Audio::V1_0::IAudioCallback;
61 using OHOS::HDI::DistributedAudio::Audio::V1_0::AudioPortPin;
62 using OHOS::HDI::DistributedAudio::Audio::V1_0::AudioPortType;
63 using OHOS::HDI::DistributedAudio::Audio::V1_0::AudioPortRole;
64 
65 namespace OHOS {
66 namespace AudioStandard {
67 namespace {
68 const int32_t HALF_FACTOR = 2;
69 const float DEFAULT_VOLUME_LEVEL = 1.0f;
70 const uint32_t AUDIO_CHANNELCOUNT = 2;
71 const uint32_t AUDIO_SAMPLE_RATE_48K = 48000;
72 const uint32_t DEEP_BUFFER_RENDER_PERIOD_SIZE = 4096;
73 const uint32_t INT_32_MAX = 0x7fffffff;
74 const uint32_t PCM_8_BIT = 8;
75 const uint32_t PCM_16_BIT = 16;
76 const uint32_t REMOTE_OUTPUT_STREAM_ID = 29; // 13 + 2 * 8
77 
78 const uint16_t GET_MAX_AMPLITUDE_FRAMES_THRESHOLD = 10;
79 
80 const string MEDIA_STREAM_TYPE = "1";
81 const string COMMUNICATION_STREAM_TYPE = "2";
82 const string NAVIGATION_STREAM_TYPE = "13";
83 uint32_t MEDIA_RENDERID = 0;
84 uint32_t NAVIGATION_RENDERID = 1;
85 uint32_t COMMUNICATION_RENDERID = 2;
86 }
87 class RemoteAudioRendererSinkInner : public RemoteAudioRendererSink, public IAudioDeviceAdapterCallback {
88 public:
89     explicit RemoteAudioRendererSinkInner(const std::string &deviceNetworkId);
90     ~RemoteAudioRendererSinkInner();
91 
92     int32_t Init(const IAudioSinkAttr &attr) override;
93     bool IsInited(void) override;
94     void DeInit(void) override;
95 
96     int32_t Start(void) override;
97     int32_t Stop(void) override;
98     int32_t Flush(void) override;
99     int32_t Reset(void) override;
100     int32_t Pause(void) override;
101     int32_t Resume(void) override;
102     int32_t SuspendRenderSink(void) override;
103     int32_t RestoreRenderSink(void) override;
104 
105     int32_t RenderFrame(char &data, uint64_t len, uint64_t &writeLen) override;
106     int32_t SplitRenderFrame(char &data, uint64_t len, uint64_t &writeLen, char *streamType) override;
107     int32_t SetVolume(float left, float right) override;
108     int32_t GetVolume(float &left, float &right) override;
109     int32_t SetVoiceVolume(float volume) override;
110     int32_t GetTransactionId(uint64_t *transactionId) override;
111     int32_t GetLatency(uint32_t *latency) override;
112     int32_t SetAudioScene(AudioScene audioScene, std::vector<DeviceType> &activeDevices) override;
113     int32_t SetOutputRoutes(std::vector<DeviceType> &outputDevices) override;
114     void SetAudioParameter(const AudioParamKey key, const std::string &condition, const std::string &value) override;
115     std::string GetAudioParameter(const AudioParamKey key, const std::string &condition) override;
116     void SetAudioMonoState(bool audioMono) override;
117     void SetAudioBalanceValue(float audioBalance) override;
118     int32_t GetPresentationPosition(uint64_t& frames, int64_t& timeSec, int64_t& timeNanoSec) override;
119     void RegisterParameterCallback(IAudioSinkCallback* callback) override;
120     void ResetOutputRouteForDisconnect(DeviceType device) override;
121     int32_t SetPaPower(int32_t flag) override;
122     int32_t SetPriPaPower() override;
123 
124     void OnAudioParamChange(const std::string &adapterName, const AudioParamKey key, const std::string &condition,
125         const std::string &value) override;
126     float GetMaxAmplitude() override;
127 
128     int32_t UpdateAppsUid(const int32_t appsUid[MAX_MIX_CHANNELS], const size_t size) final;
129     int32_t UpdateAppsUid(const std::vector<int32_t> &appsUid) final;
130 
131     std::string GetNetworkId();
132     IAudioSinkCallback* GetParamCallback();
133 
134 private:
135     int32_t CreateRender(const struct AudioPort &renderPort, AudioCategory type, uint32_t &renderId);
136     void InitAttrs(struct AudioSampleAttributes &attrs);
137     void splitStreamInit(const char *splitStreamString, vector<string> &splitStreamVector);
138     int32_t RenderFrameLogic(char &data, uint64_t len, uint64_t &writeLen, const char *streamType);
139     AudioFormat ConvertToHdiFormat(HdiAdapterFormat format);
140     int32_t OpenOutput(DeviceType outputDevice);
141     void ClearRender();
142 
143     void CheckUpdateState(char *frame, uint64_t replyBytes);
144     void DfxOperation(BufferDesc &buffer, AudioSampleFormat format, AudioChannel channel) const;
145 private:
146     std::string deviceNetworkId_ = "";
147     std::atomic<bool> rendererInited_ = false;
148     std::atomic<bool> isRenderCreated_ = false;
149     std::atomic<bool> started_ = false;
150     std::atomic<bool> paused_ = false;
151     float leftVolume_ = DEFAULT_VOLUME_LEVEL;
152     float rightVolume_ = DEFAULT_VOLUME_LEVEL;
153 
154     std::shared_ptr<IAudioDeviceManager> audioManager_ = nullptr;
155     std::mutex audioMangerMutex_;
156 
157     std::shared_ptr<IAudioDeviceAdapter> audioAdapter_ = nullptr;
158     std::mutex audioAdapterMutex_;
159 
160     IAudioSinkCallback *callback_ = nullptr;
161     IAudioSinkAttr attr_ = {};
162     unordered_map<AudioCategory, sptr<IAudioRender>> audioRenderMap_;
163     unordered_map<AudioCategory, AudioPort> audioPortMap_;
164     unordered_map<string, AudioCategory> splitStreamMap_;
165     unordered_map<AudioCategory, FILE*> dumpFileMap_;
166     unordered_map<AudioCategory, std::string> dumpFileNameMap_;
167     std::mutex createRenderMutex_;
168     vector<uint32_t> renderIdVector_ = {MEDIA_RENDERID, NAVIGATION_RENDERID, COMMUNICATION_RENDERID};
169     // for get amplitude
170     float maxAmplitude_ = 0;
171     int64_t lastGetMaxAmplitudeTime_ = 0;
172     int64_t last10FrameStartTime_ = 0;
173     bool startUpdate_ = false;
174     int renderFrameNum_ = 0;
175     std::string logUtilsTag_ = "Remote";
176     mutable int64_t volumeDataCount_ = 0;
177 };
178 
RemoteAudioRendererSinkInner(const std::string & deviceNetworkId)179 RemoteAudioRendererSinkInner::RemoteAudioRendererSinkInner(const std::string &deviceNetworkId)
180     :deviceNetworkId_(deviceNetworkId)
181 {
182     AUDIO_DEBUG_LOG("RemoteAudioRendererSinkInner constract.");
183 }
184 
~RemoteAudioRendererSinkInner()185 RemoteAudioRendererSinkInner::~RemoteAudioRendererSinkInner()
186 {
187     if (rendererInited_.load()) {
188         RemoteAudioRendererSinkInner::DeInit();
189     }
190     AUDIO_DEBUG_LOG("RemoteAudioRendererSink destruction.");
191 }
192 
193 std::mutex g_rendererSinksMutex;
194 std::map<std::string, RemoteAudioRendererSinkInner *> allsinks;
GetInstance(const std::string & deviceNetworkId)195 RemoteAudioRendererSink *RemoteAudioRendererSink::GetInstance(const std::string &deviceNetworkId)
196 {
197     std::lock_guard<std::mutex> lock(g_rendererSinksMutex);
198     AUDIO_INFO_LOG("RemoteAudioRendererSink::GetInstance");
199     CHECK_AND_RETURN_RET_LOG(!deviceNetworkId.empty(), nullptr, "Remote render device networkId is null.");
200 
201     if (allsinks.count(deviceNetworkId)) {
202         return allsinks[deviceNetworkId];
203     }
204     RemoteAudioRendererSinkInner *audioRenderer = new(std::nothrow) RemoteAudioRendererSinkInner(deviceNetworkId);
205     AUDIO_INFO_LOG("New daudio remote render device [%{public}s].", GetEncryptStr(deviceNetworkId).c_str());
206     allsinks[deviceNetworkId] = audioRenderer;
207     return audioRenderer;
208 }
209 
ClearRender()210 void RemoteAudioRendererSinkInner::ClearRender()
211 {
212     AUDIO_INFO_LOG("Clear remote audio render enter.");
213     rendererInited_.store(false);
214     isRenderCreated_.store(false);
215     started_.store(false);
216     paused_.store(false);
217 
218     auto renderId = renderIdVector_.begin();
219     std::shared_ptr<IAudioDeviceAdapter> audioAdapter;
220     {
221         std::lock_guard<std::mutex> lock(audioAdapterMutex_);
222         audioAdapter = std::move(audioAdapter_);
223         audioAdapter_ = nullptr;
224     }
225 
226     if (audioAdapter != nullptr) {
227         for (auto &audioRender : audioRenderMap_) {
228             audioAdapter->DestroyRender(audioRender.second, *renderId);
229             audioRender.second = nullptr;
230             renderId++;
231             FILE *dumpFile = dumpFileMap_[audioRender.first];
232             DumpFileUtil::CloseDumpFile(&dumpFile);
233         }
234         audioAdapter->Release();
235     }
236     audioRenderMap_.clear();
237     audioAdapter = nullptr;
238 
239     std::shared_ptr<IAudioDeviceManager> audioManager;
240     {
241         std::lock_guard<std::mutex> lock(audioMangerMutex_);
242         audioManager = std::move(audioManager_);
243         audioManager_ = nullptr;
244     }
245 
246     if (audioManager != nullptr) {
247         audioManager->UnloadAdapter(deviceNetworkId_);
248     }
249     audioManager = nullptr;
250 
251     AudioDeviceManagerFactory::GetInstance().DestoryDeviceManager(REMOTE_DEV_MGR);
252 
253     dumpFileMap_.clear();
254     dumpFileNameMap_.clear();
255     AUDIO_INFO_LOG("Clear remote audio render end.");
256 }
257 
DeInit()258 void RemoteAudioRendererSinkInner::DeInit()
259 {
260     Trace trace("RemoteAudioRendererSinkInner::DeInit");
261     std::lock_guard<std::mutex> lock(g_rendererSinksMutex);
262     AUDIO_INFO_LOG("RemoteAudioRendererSinkInner::DeInit");
263     ClearRender();
264 
265     // remove map recorder.
266     CHECK_AND_RETURN_LOG(allsinks.count(this->deviceNetworkId_) > 0,
267         "not find %{public}s", this->deviceNetworkId_.c_str());
268     RemoteAudioRendererSinkInner *temp = allsinks[this->deviceNetworkId_];
269     allsinks.erase(this->deviceNetworkId_);
270     if (temp == nullptr) {
271         AUDIO_ERR_LOG("temp is nullptr");
272     } else {
273         delete temp;
274     }
275     AUDIO_INFO_LOG("end.");
276 }
277 
PrintRemoteAttr(const IAudioSinkAttr & attr)278 inline std::string PrintRemoteAttr(const IAudioSinkAttr &attr)
279 {
280     std::stringstream value;
281     value << "adapterName[" << attr.adapterName << "] openMicSpeaker[" << attr.openMicSpeaker << "] ";
282     value << "format[" << static_cast<int32_t>(attr.format) << "] ";
283     value << "sampleRate[" << attr.sampleRate << "] channel[" << attr.channel << "] ";
284     value << "volume[" << attr.volume << "] filePath[" << attr.filePath << "] ";
285     value << "deviceNetworkId[" << attr.deviceNetworkId << "] device_type[" << attr.deviceType << "]";
286     return value.str();
287 }
288 
IsInited()289 bool RemoteAudioRendererSinkInner::IsInited()
290 {
291     return rendererInited_.load();
292 }
293 
Init(const IAudioSinkAttr & attr)294 int32_t RemoteAudioRendererSinkInner::Init(const IAudioSinkAttr &attr)
295 {
296     AUDIO_INFO_LOG("RemoteAudioRendererSinkInner::Init");
297     attr_ = attr;
298     splitStreamMap_[MEDIA_STREAM_TYPE] = AudioCategory::AUDIO_IN_MEDIA;
299     splitStreamMap_[NAVIGATION_STREAM_TYPE] = AudioCategory::AUDIO_IN_NAVIGATION;
300     splitStreamMap_[COMMUNICATION_STREAM_TYPE] = AudioCategory::AUDIO_IN_COMMUNICATION;
301     vector<string> splitStreamVector;
302     splitStreamInit(attr_.aux, splitStreamVector);
303     auto audioManager = AudioDeviceManagerFactory::GetInstance().CreatDeviceManager(REMOTE_DEV_MGR);
304     {
305         std::lock_guard<std::mutex> lock(audioMangerMutex_);
306         audioManager_ = audioManager;
307     }
308     CHECK_AND_RETURN_RET_LOG(audioManager != nullptr, ERR_NOT_STARTED, "Init audio manager fail");
309     struct AudioAdapterDescriptor *desc = audioManager->GetTargetAdapterDesc(deviceNetworkId_, false);
310     CHECK_AND_RETURN_RET_LOG(desc != nullptr, ERR_NOT_STARTED, "Get target adapters descriptor fail.");
311     auto splitStreamTypeIter = splitStreamVector.begin();
312     for (uint32_t port = 0; port < desc->ports.size(); port++) {
313         if (desc->ports[port].portId == AudioPortPin::PIN_OUT_SPEAKER) {
314             AUDIO_INFO_LOG("current audio stream type is %{public}s, port index is %{public}d",
315                 splitStreamTypeIter->c_str(), port);
316             while (splitStreamTypeIter != splitStreamVector.end()) {
317                 audioPortMap_[splitStreamMap_[*splitStreamTypeIter]] = desc->ports[port];
318                 splitStreamTypeIter++;
319             }
320         }
321     }
322 
323     auto audioAdapter = audioManager->LoadAdapters(deviceNetworkId_, false);
324     CHECK_AND_RETURN_RET_LOG(audioAdapter != nullptr, ERR_NOT_STARTED, "Load audio device adapter failed.");
325 
326     {
327         std::lock_guard<std::mutex> lock(audioAdapterMutex_);
328         audioAdapter_ = audioAdapter;
329     }
330 
331     int32_t ret = audioAdapter->Init();
332     CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, ret, "Audio adapter init fail, ret %{public}d.", ret);
333 
334     rendererInited_.store(true);
335 
336     AUDIO_DEBUG_LOG("RemoteAudioRendererSink: Init end.");
337     return SUCCESS;
338 }
339 
splitStreamInit(const char * splitStreamString,vector<string> & splitStreamVector)340 void RemoteAudioRendererSinkInner::splitStreamInit(const char *splitStreamString, vector<string> &splitStreamVector)
341 {
342     AUDIO_INFO_LOG("audio split stream is %{public}s", splitStreamString);
343     if (splitStreamString == nullptr) {
344         splitStreamVector.push_back("1");
345         AUDIO_INFO_LOG("audio split stream is default 1");
346         return;
347     }
348 
349     istringstream iss(splitStreamString);
350     std::string currentSplitStream;
351     while (getline(iss, currentSplitStream, ':')) {
352         splitStreamVector.push_back(currentSplitStream);
353         AUDIO_INFO_LOG("current split stream type is %{public}s", currentSplitStream.c_str());
354     }
355     sort(splitStreamVector.begin(), splitStreamVector.end());
356 }
357 
CreateRender(const struct AudioPort & renderPort,AudioCategory type,uint32_t & renderId)358 int32_t RemoteAudioRendererSinkInner::CreateRender(const struct AudioPort &renderPort, AudioCategory type,
359     uint32_t &renderId)
360 {
361     int64_t start = ClockTime::GetCurNano();
362     struct AudioSampleAttributes param;
363     InitAttrs(param);
364     param.type = type;
365     param.sampleRate = attr_.sampleRate;
366     param.channelCount = attr_.channel;
367     param.format = ConvertToHdiFormat(attr_.format);
368     param.frameSize = PCM_16_BIT * param.channelCount / PCM_8_BIT;
369     param.startThreshold = DEEP_BUFFER_RENDER_PERIOD_SIZE / (param.frameSize);
370     AUDIO_DEBUG_LOG("Create render format: %{public}d", param.format);
371 
372     struct AudioDeviceDescriptor deviceDesc;
373     deviceDesc.portId = renderPort.portId;
374     deviceDesc.pins = AudioPortPin::PIN_OUT_SPEAKER;
375     deviceDesc.desc = "";
376 
377     std::shared_ptr<IAudioDeviceAdapter> audioAdapter;
378     {
379         std::lock_guard<std::mutex> lock(audioAdapterMutex_);
380         audioAdapter = audioAdapter_;
381     }
382 
383     CHECK_AND_RETURN_RET_LOG(audioAdapter_ != nullptr, ERR_INVALID_HANDLE, "CreateRender: Audio adapter is null.");
384     sptr<IAudioRender> audioRender = nullptr;
385     int32_t ret = audioAdapter_->CreateRender(deviceDesc, param, audioRender, this, renderId);
386     audioRenderMap_[type] = audioRender;
387     CHECK_AND_RETURN_RET_LOG(ret == SUCCESS && audioRender != nullptr, ret,
388         "AudioDeviceCreateRender fail, ret %{public}d.", ret);
389 
390     isRenderCreated_.store(true);
391     int64_t cost = (ClockTime::GetCurNano() - start) / AUDIO_US_PER_SECOND;
392     AUDIO_INFO_LOG("CreateRender cost[%{public}" PRId64 "]ms", cost);
393     return SUCCESS;
394 }
395 
InitAttrs(struct AudioSampleAttributes & attrs)396 void RemoteAudioRendererSinkInner::InitAttrs(struct AudioSampleAttributes &attrs)
397 {
398     /* Initialization of audio parameters for playback */
399     attrs.channelCount = AUDIO_CHANNELCOUNT;
400     attrs.sampleRate = AUDIO_SAMPLE_RATE_48K;
401     attrs.interleaved = 0;
402     attrs.streamId = REMOTE_OUTPUT_STREAM_ID;
403     attrs.type = AudioCategory::AUDIO_IN_MEDIA;
404     attrs.period = DEEP_BUFFER_RENDER_PERIOD_SIZE;
405     attrs.isBigEndian = false;
406     attrs.isSignedData = true;
407     attrs.stopThreshold = INT_32_MAX;
408     attrs.silenceThreshold = 0;
409 }
410 
ConvertToHdiFormat(HdiAdapterFormat format)411 AudioFormat RemoteAudioRendererSinkInner::ConvertToHdiFormat(HdiAdapterFormat format)
412 {
413     AudioFormat hdiFormat;
414     switch (format) {
415         case SAMPLE_U8:
416             hdiFormat = AudioFormat::AUDIO_FORMAT_TYPE_PCM_8_BIT;
417             break;
418         case SAMPLE_S16:
419             hdiFormat = AudioFormat::AUDIO_FORMAT_TYPE_PCM_16_BIT;
420             break;
421         case SAMPLE_S24:
422             hdiFormat = AudioFormat::AUDIO_FORMAT_TYPE_PCM_24_BIT;
423             break;
424         case SAMPLE_S32:
425             hdiFormat = AudioFormat::AUDIO_FORMAT_TYPE_PCM_32_BIT;
426             break;
427         default:
428             hdiFormat = AudioFormat::AUDIO_FORMAT_TYPE_PCM_16_BIT;
429             break;
430     }
431 
432     return hdiFormat;
433 }
434 
RenderFrame(char & data,uint64_t len,uint64_t & writeLen)435 int32_t RemoteAudioRendererSinkInner::RenderFrame(char &data, uint64_t len, uint64_t &writeLen)
436 {
437     Trace trace("RemoteAudioRendererSinkInner::RenderFrame");
438     AUDIO_INFO_LOG("RemoteAudioRendererSinkInner::RenderFrame");
439     const char *mediaStreamType = "1";
440     return RenderFrameLogic(data, len, writeLen, mediaStreamType);
441 }
442 
SplitRenderFrame(char & data,uint64_t len,uint64_t & writeLen,char * streamType)443 int32_t RemoteAudioRendererSinkInner::SplitRenderFrame(char &data, uint64_t len, uint64_t &writeLen, char *streamType)
444 {
445         Trace trace("RemoteAudioRendererSinkInner::SplitRenderFrame");
446     AUDIO_INFO_LOG("RemoteAudioRendererSinkInner::SplitRenderFrame");
447     return RenderFrameLogic(data, len, writeLen, streamType);
448 }
449 
RenderFrameLogic(char & data,uint64_t len,uint64_t & writeLen,const char * streamType)450 int32_t RemoteAudioRendererSinkInner::RenderFrameLogic(char &data, uint64_t len, uint64_t &writeLen,
451     const char *streamType)
452 {
453     AUDIO_INFO_LOG("RemoteAudioRendererSinkInner::RenderFrameLogic, streamType is %{public}s", streamType);
454     int64_t start = ClockTime::GetCurNano();
455     sptr<IAudioRender> audioRender_ = audioRenderMap_[splitStreamMap_[streamType]];
456     CHECK_AND_RETURN_RET_LOG(audioRender_ != nullptr, ERR_INVALID_HANDLE, "RenderFrame: Audio render is null.");
457 
458     if (!started_.load()) {
459         AUDIO_DEBUG_LOG("RemoteAudioRendererSinkInner::RenderFrameLogic invalid state not started!");
460     }
461     std::vector<int8_t> frameHal(len);
462     int32_t ret = memcpy_s(frameHal.data(), len, &data, len);
463     if (ret != EOK) {
464         AUDIO_ERR_LOG("Copy render frame failed, error code %d.", ret);
465         return ERR_OPERATION_FAILED;
466     }
467 
468     BufferDesc buffer = { reinterpret_cast<uint8_t*>(&data), len, len };
469     DfxOperation(buffer, static_cast<AudioSampleFormat>(attr_.format), static_cast<AudioChannel>(attr_.channel));
470     Trace traceRenderFrame("audioRender_->RenderFrame");
471     Trace::CountVolume("RemoteAudioRendererSinkInner::RenderFrameLogic", static_cast<uint8_t>(data));
472     ret = audioRender_->RenderFrame(frameHal, writeLen);
473     CHECK_AND_RETURN_RET_LOG(ret == 0, ERR_WRITE_FAILED, "Render frame fail, ret %{public}x.", ret);
474     writeLen = len;
475 
476     FILE *dumpFile = dumpFileMap_[splitStreamMap_[streamType]];
477     std::string dumpFileName = dumpFileNameMap_[splitStreamMap_[streamType]];
478     DumpFileUtil::WriteDumpFile(dumpFile, static_cast<void *>(&data), len);
479     AudioCacheMgr::GetInstance().CacheData(dumpFileName, static_cast<void *>(&data), len);
480 
481     CheckUpdateState(&data, len);
482 
483     int64_t cost = (ClockTime::GetCurNano() - start) / AUDIO_US_PER_SECOND;
484     AUDIO_DEBUG_LOG("RenderFrame len[%{public}" PRIu64 "] cost[%{public}" PRId64 "]ms", len, cost);
485 
486     int64_t stampThreshold = 50; // 50ms
487     if (cost >= stampThreshold) {
488         AUDIO_WARNING_LOG("RenderFrame len[%{public}" PRIu64 "] cost[%{public}" PRId64 "]ms", len, cost);
489     }
490 
491     return SUCCESS;
492 }
493 
CheckUpdateState(char * frame,uint64_t replyBytes)494 void RemoteAudioRendererSinkInner::CheckUpdateState(char *frame, uint64_t replyBytes)
495 {
496     if (startUpdate_) {
497         if (renderFrameNum_ == 0) {
498             last10FrameStartTime_ = ClockTime::GetCurNano();
499         }
500         renderFrameNum_++;
501         maxAmplitude_ = UpdateMaxAmplitude(static_cast<ConvertHdiFormat>(attr_.format), frame, replyBytes);
502         if (renderFrameNum_ == GET_MAX_AMPLITUDE_FRAMES_THRESHOLD) {
503             renderFrameNum_ = 0;
504             if (last10FrameStartTime_ > lastGetMaxAmplitudeTime_) {
505                 startUpdate_ = false;
506                 maxAmplitude_ = 0;
507             }
508         }
509     }
510 }
511 
GetMaxAmplitude()512 float RemoteAudioRendererSinkInner::GetMaxAmplitude()
513 {
514     lastGetMaxAmplitudeTime_ = ClockTime::GetCurNano();
515     startUpdate_ = true;
516     return maxAmplitude_;
517 }
518 
Start(void)519 int32_t RemoteAudioRendererSinkInner::Start(void)
520 {
521     Trace trace("RemoteAudioRendererSinkInner::Start");
522     AUDIO_INFO_LOG("RemoteAudioRendererSinkInner::Start");
523     std::lock_guard<std::mutex> lock(createRenderMutex_);
524 
525     for (const auto &audioPort : audioPortMap_) {
526         FILE *dumpFile = nullptr;
527         std::string dumpFileName = std::string(DUMP_REMOTE_RENDER_SINK_FILENAME) + "_" + GetTime() + "_" +
528             std::to_string(attr_.sampleRate) + "_" + std::to_string(attr_.channel) + "_" +
529             std::to_string(attr_.format) + ".pcm";
530         DumpFileUtil::OpenDumpFile(DUMP_SERVER_PARA, dumpFileName, &dumpFile);
531         dumpFileMap_[audioPort.first] = dumpFile;
532         dumpFileNameMap_[audioPort.first] = dumpFileName;
533     }
534 
535     auto renderId = renderIdVector_.begin();
536     if (!isRenderCreated_.load()) {
537         for (const auto &audioPort : audioPortMap_) {
538             CHECK_AND_RETURN_RET_LOG(CreateRender(audioPort.second, audioPort.first, *renderId) == SUCCESS,
539                 ERR_NOT_STARTED, "Create render fail, audio port %{public}d", audioPort.second.portId);
540             renderId++;
541         }
542     }
543 
544     if (started_.load()) {
545         AUDIO_INFO_LOG("Remote render is already started.");
546         return SUCCESS;
547     }
548 
549     for (const auto &audioRender : audioRenderMap_) {
550         CHECK_AND_RETURN_RET_LOG(audioRender.second != nullptr, ERR_INVALID_HANDLE,
551             "Start: Audio render is null. Audio steam type is %{public}d", audioRender.first);
552         int32_t ret = audioRender.second->Start();
553         CHECK_AND_RETURN_RET_LOG(ret == 0, ERR_NOT_STARTED, "Start fail, ret %{public}d.", ret);
554     }
555     started_.store(true);
556     return SUCCESS;
557 }
558 
Stop(void)559 int32_t RemoteAudioRendererSinkInner::Stop(void)
560 {
561     Trace trace("RemoteAudioRendererSinkInner::Stop");
562     AUDIO_INFO_LOG("RemoteAudioRendererSinkInner::Stop");
563     if (!started_.load()) {
564         AUDIO_INFO_LOG("Remote render is already stopped.");
565         return SUCCESS;
566     }
567 
568     for (const auto &audioRender : audioRenderMap_) {
569         CHECK_AND_RETURN_RET_LOG(audioRender.second != nullptr, ERR_INVALID_HANDLE,
570             "Stop: Audio render is null.Audio stream type is %{public}d", audioRender.first);
571         int32_t ret = audioRender.second->Stop();
572         CHECK_AND_RETURN_RET_LOG(ret == 0, ERR_OPERATION_FAILED, "Stop fail, ret %{public}d.", ret);
573     }
574     started_.store(false);
575     return SUCCESS;
576 }
577 
Pause(void)578 int32_t RemoteAudioRendererSinkInner::Pause(void)
579 {
580     AUDIO_INFO_LOG("RemoteAudioRendererSinkInner::Pause");
581     CHECK_AND_RETURN_RET_LOG(started_.load(), ERR_ILLEGAL_STATE, "Pause invalid state!");
582 
583     if (paused_.load()) {
584         AUDIO_INFO_LOG("Remote render is already paused.");
585         return SUCCESS;
586     }
587 
588     for (const auto &audioRender : audioRenderMap_) {
589         CHECK_AND_RETURN_RET_LOG(audioRender.second != nullptr, ERR_INVALID_HANDLE,
590             "Pause: Audio render is null. Audio stream type is %{public}d", audioRender.first);
591         int32_t ret = audioRender.second->Pause();
592         CHECK_AND_RETURN_RET_LOG(ret == 0, ERR_OPERATION_FAILED, "Pause fail, ret %{public}d.", ret);
593     }
594     paused_.store(true);
595     return SUCCESS;
596 }
597 
Resume(void)598 int32_t RemoteAudioRendererSinkInner::Resume(void)
599 {
600     Trace trace("RemoteAudioRendererSinkInner::Resume");
601     AUDIO_INFO_LOG("RemoteAudioRendererSinkInner::Resume");
602     CHECK_AND_RETURN_RET_LOG(started_.load(), ERR_ILLEGAL_STATE, "Resume invalid state!");
603 
604     if (!paused_.load()) {
605         AUDIO_INFO_LOG("Remote render is already resumed.");
606         return SUCCESS;
607     }
608 
609     for (const auto &audioRender : audioRenderMap_) {
610         CHECK_AND_RETURN_RET_LOG(audioRender.second != nullptr, ERR_INVALID_HANDLE,
611             "Resume: Audio render is null.Audio stream type is %{public}d", audioRender.first);
612         int32_t ret = audioRender.second->Resume();
613         CHECK_AND_RETURN_RET_LOG(ret == 0, ERR_OPERATION_FAILED, "Resume fail, ret %{public}d.", ret);
614     }
615 
616     paused_.store(false);
617     return SUCCESS;
618 }
619 
Reset(void)620 int32_t RemoteAudioRendererSinkInner::Reset(void)
621 {
622     Trace trace("RemoteAudioRendererSinkInner::Reset");
623     AUDIO_INFO_LOG("RemoteAudioRendererSinkInner::Reset");
624     CHECK_AND_RETURN_RET_LOG(started_.load(), ERR_ILLEGAL_STATE, "Reset invalid state!");
625 
626     for (const auto &audioRender : audioRenderMap_) {
627         CHECK_AND_RETURN_RET_LOG(audioRender.second != nullptr, ERR_INVALID_HANDLE,
628             "Reset: Audio render is null.Audio stream type is %{public}d", audioRender.first);
629         int32_t ret = audioRender.second->Flush();
630         CHECK_AND_RETURN_RET_LOG(ret == 0, ERR_OPERATION_FAILED, "Reset fail, ret %{public}d.", ret);
631     }
632     return SUCCESS;
633 }
634 
Flush(void)635 int32_t RemoteAudioRendererSinkInner::Flush(void)
636 {
637     Trace trace("RemoteAudioRendererSinkInner::Flush");
638     AUDIO_INFO_LOG("RemoteAudioRendererSinkInner::Flush");
639     CHECK_AND_RETURN_RET_LOG(started_.load(), ERR_ILLEGAL_STATE, "Flush invalid state!");
640 
641     for (const auto &audioRender : audioRenderMap_) {
642         CHECK_AND_RETURN_RET_LOG(audioRender.second != nullptr, ERR_INVALID_HANDLE,
643             "Flush: Audio render is null.Audio stream type is %{public}d", audioRender.first);
644         int32_t ret = audioRender.second->Flush();
645         CHECK_AND_RETURN_RET_LOG(ret == 0, ERR_OPERATION_FAILED, "Flush fail, ret %{public}d.", ret);
646     }
647     return SUCCESS;
648 }
649 
SuspendRenderSink(void)650 int32_t RemoteAudioRendererSinkInner::SuspendRenderSink(void)
651 {
652     return SUCCESS;
653 }
654 
RestoreRenderSink(void)655 int32_t RemoteAudioRendererSinkInner::RestoreRenderSink(void)
656 {
657     return SUCCESS;
658 }
659 
SetVolume(float left,float right)660 int32_t RemoteAudioRendererSinkInner::SetVolume(float left, float right)
661 {
662     leftVolume_ = left;
663     rightVolume_ = right;
664     float volume;
665     if ((leftVolume_ == 0) && (rightVolume_ != 0)) {
666         volume = rightVolume_;
667     } else if ((leftVolume_ != 0) && (rightVolume_ == 0)) {
668         volume = leftVolume_;
669     } else {
670         volume = (leftVolume_ + rightVolume_) / HALF_FACTOR;
671     }
672     for (const auto &audioRender : audioRenderMap_) {
673         CHECK_AND_RETURN_RET_LOG(audioRender.second != nullptr, ERR_INVALID_HANDLE,
674             "SetVolume: Audio render is null. Audio stream type is %{public}d", audioRender.first);
675         int32_t ret = audioRender.second->SetVolume(volume);
676         CHECK_AND_RETURN_RET_LOG(ret == 0, ERR_OPERATION_FAILED, "Set volume fail, ret %{public}d.", ret);
677     }
678     return SUCCESS;
679 }
680 
GetVolume(float & left,float & right)681 int32_t RemoteAudioRendererSinkInner::GetVolume(float &left, float &right)
682 {
683     left = leftVolume_;
684     right = rightVolume_;
685     return SUCCESS;
686 }
687 
GetLatency(uint32_t * latency)688 int32_t RemoteAudioRendererSinkInner::GetLatency(uint32_t *latency)
689 {
690     CHECK_AND_RETURN_RET_LOG(latency, ERR_INVALID_PARAM,
691         "GetLatency failed latency null");
692 
693     uint32_t hdiLatency = 0;
694     for (const auto &audioRender : audioRenderMap_) {
695         CHECK_AND_RETURN_RET_LOG(audioRender.second != nullptr, ERR_INVALID_HANDLE,
696             "GetLatency: Audio render is null. Audio stream type is %{public}d", audioRender.first);
697         int32_t ret = audioRender.second->GetLatency(hdiLatency);
698         CHECK_AND_RETURN_RET_LOG(ret == 0, ERR_OPERATION_FAILED, "Get latency fail, ret %{public}d.", ret);
699     }
700 
701     *latency = hdiLatency;
702     return SUCCESS;
703 }
704 
GetAudioCategory(AudioScene audioScene)705 static AudioCategory GetAudioCategory(AudioScene audioScene)
706 {
707     AudioCategory audioCategory;
708     switch (audioScene) {
709         case AUDIO_SCENE_DEFAULT:
710             audioCategory = AudioCategory::AUDIO_IN_MEDIA;
711             break;
712         case AUDIO_SCENE_RINGING:
713         case AUDIO_SCENE_VOICE_RINGING:
714             audioCategory = AudioCategory::AUDIO_IN_RINGTONE;
715             break;
716         case AUDIO_SCENE_PHONE_CALL:
717             audioCategory = AudioCategory::AUDIO_IN_CALL;
718             break;
719         case AUDIO_SCENE_PHONE_CHAT:
720             audioCategory = AudioCategory::AUDIO_IN_COMMUNICATION;
721             break;
722         default:
723             audioCategory = AudioCategory::AUDIO_IN_MEDIA;
724             break;
725     }
726     AUDIO_DEBUG_LOG("Audio category returned is: %{public}d", audioCategory);
727 
728     return audioCategory;
729 }
730 
SetOutputPortPin(DeviceType outputDevice,AudioRouteNode & sink)731 static int32_t SetOutputPortPin(DeviceType outputDevice, AudioRouteNode &sink)
732 {
733     int32_t ret = SUCCESS;
734 
735     switch (outputDevice) {
736         case DEVICE_TYPE_SPEAKER:
737             sink.ext.device.type = AudioPortPin::PIN_OUT_SPEAKER;
738             sink.ext.device.desc = "pin_out_speaker";
739             break;
740         case DEVICE_TYPE_WIRED_HEADSET:
741             sink.ext.device.type = AudioPortPin::PIN_OUT_HEADSET;
742             sink.ext.device.desc = "pin_out_headset";
743             break;
744         case DEVICE_TYPE_USB_HEADSET:
745             sink.ext.device.type = AudioPortPin::PIN_OUT_USB_EXT;
746             sink.ext.device.desc = "pin_out_usb_ext";
747             break;
748         default:
749             ret = ERR_NOT_SUPPORTED;
750             break;
751     }
752 
753     return ret;
754 }
755 
OpenOutput(DeviceType outputDevice)756 int32_t RemoteAudioRendererSinkInner::OpenOutput(DeviceType outputDevice)
757 {
758     AudioRouteNode source = {};
759     AudioRouteNode sink = {};
760 
761     int32_t ret = SetOutputPortPin(outputDevice, sink);
762     CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, ret, "Set output port pin fail, ret %{public}d", ret);
763 
764     source.portId = 0;
765     source.role = AudioPortRole::AUDIO_PORT_SOURCE_ROLE;
766     source.type = AudioPortType::AUDIO_PORT_MIX_TYPE;
767     source.ext.mix.moduleId = 0;
768     source.ext.mix.streamId = REMOTE_OUTPUT_STREAM_ID;
769 
770     if (audioPortMap_.find(AudioCategory::AUDIO_IN_MEDIA) == audioPortMap_.end()) {
771         AUDIO_WARNING_LOG("audioPortMap_ is null, ret %{public}d.", ret);
772         return ERR_INVALID_HANDLE;
773     }
774     sink.portId = static_cast<int32_t>(audioPortMap_[AudioCategory::AUDIO_IN_MEDIA].portId);
775     sink.role = AudioPortRole::AUDIO_PORT_SINK_ROLE;
776     sink.type = AudioPortType::AUDIO_PORT_DEVICE_TYPE;
777     sink.ext.device.moduleId = 0;
778 
779     AudioRoute route;
780     route.sources.push_back(source);
781     route.sinks.push_back(sink);
782 
783     std::shared_ptr<IAudioDeviceAdapter> audioAdapter;
784     {
785         std::lock_guard<std::mutex> lock(audioAdapterMutex_);
786         audioAdapter = audioAdapter_;
787     }
788 
789     CHECK_AND_RETURN_RET_LOG(audioAdapter != nullptr, ERR_INVALID_HANDLE, "OpenOutput: Audio adapter is null.");
790     ret = audioAdapter->UpdateAudioRoute(route);
791     CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, ret, "Update audio route fail, ret %{public}d", ret);
792     return SUCCESS;
793 }
794 
SetAudioScene(AudioScene audioScene,std::vector<DeviceType> & activeDevices)795 int32_t RemoteAudioRendererSinkInner::SetAudioScene(AudioScene audioScene, std::vector<DeviceType> &activeDevices)
796 {
797     CHECK_AND_RETURN_RET_LOG(!activeDevices.empty() && activeDevices.size() <= AUDIO_CONCURRENT_ACTIVE_DEVICES_LIMIT,
798         ERR_INVALID_PARAM, "Invalid audio devices.");
799     DeviceType activeDevice = activeDevices.front();
800     AUDIO_INFO_LOG("SetAudioScene scene: %{public}d, device: %{public}d", audioScene, activeDevice);
801     CHECK_AND_RETURN_RET_LOG(audioScene >= AUDIO_SCENE_DEFAULT && audioScene < AUDIO_SCENE_MAX,
802         ERR_INVALID_PARAM, "invalid audioScene");
803 
804     int32_t ret = OpenOutput(DEVICE_TYPE_SPEAKER);
805     if (ret != SUCCESS) {
806         AUDIO_WARNING_LOG("Audio adapter update audio route fail, ret %{public}d.", ret);
807     }
808 
809     struct AudioSceneDescriptor scene;
810     scene.scene.id = GetAudioCategory(audioScene);
811     scene.desc.pins = AudioPortPin::PIN_OUT_SPEAKER;
812 
813     AUDIO_DEBUG_LOG("SelectScene start");
814     for (const auto &audioRender : audioRenderMap_) {
815         CHECK_AND_RETURN_RET_LOG(audioRender.second != nullptr, ERR_INVALID_HANDLE,
816             "SetAudioScene: Audio render is null. Audio stream type is %{public}d", audioRender.first);
817         ret = audioRender.second->SelectScene(scene);
818         CHECK_AND_RETURN_RET_LOG(ret == 0, ERR_OPERATION_FAILED,
819             "Audio render Select scene fail, ret %{public}d.", ret);
820     }
821     AUDIO_DEBUG_LOG("Select audio scene SUCCESS: %{public}d", audioScene);
822     return SUCCESS;
823 }
824 
SetAudioParameter(const AudioParamKey key,const std::string & condition,const std::string & value)825 void RemoteAudioRendererSinkInner::SetAudioParameter(const AudioParamKey key, const std::string &condition,
826     const std::string &value)
827 {
828 #ifdef FEATURE_DISTRIBUTE_AUDIO
829     AUDIO_INFO_LOG("SetParameter: key %{public}d, condition: %{public}s, value: %{public}s",
830         key, condition.c_str(), value.c_str());
831 
832     std::shared_ptr<IAudioDeviceAdapter> audioAdapter;
833     {
834         std::lock_guard<std::mutex> lock(audioAdapterMutex_);
835         audioAdapter = audioAdapter_;
836     }
837 
838     CHECK_AND_RETURN_LOG(audioAdapter != nullptr, "SetAudioParameter: Audio adapter is null.");
839     audioAdapter->SetAudioParameter(key, condition.c_str(), value.c_str());
840 #endif
841 }
842 
GetAudioParameter(const AudioParamKey key,const std::string & condition)843 std::string RemoteAudioRendererSinkInner::GetAudioParameter(const AudioParamKey key, const std::string &condition)
844 {
845 #ifdef FEATURE_DISTRIBUTE_AUDIO
846     AUDIO_INFO_LOG("key %{public}d, condition: %{public}s", key, condition.c_str());
847 
848     std::shared_ptr<IAudioDeviceAdapter> audioAdapter;
849     {
850         std::lock_guard<std::mutex> lock(audioAdapterMutex_);
851         audioAdapter = audioAdapter_;
852     }
853 
854     CHECK_AND_RETURN_RET_LOG(audioAdapter != nullptr, "", "Audio adapter is null.");
855     return audioAdapter->GetAudioParameter(key, condition);
856 #else
857     return "";
858 #endif
859 }
860 
RegisterParameterCallback(IAudioSinkCallback * callback)861 void RemoteAudioRendererSinkInner::RegisterParameterCallback(IAudioSinkCallback* callback)
862 {
863     AUDIO_INFO_LOG("register sink audio param callback.");
864     callback_ = callback;
865 #ifdef FEATURE_DISTRIBUTE_AUDIO
866     // register to remote audio adapter
867 
868     std::shared_ptr<IAudioDeviceAdapter> audioAdapter;
869     {
870         std::lock_guard<std::mutex> lock(audioAdapterMutex_);
871         audioAdapter = audioAdapter_;
872     }
873 
874     CHECK_AND_RETURN_LOG(audioAdapter != nullptr, "RegisterParameterCallback: Audio adapter is null.");
875     int32_t ret = audioAdapter->RegExtraParamObserver();
876     CHECK_AND_RETURN_LOG(ret == SUCCESS, "RegisterParameterCallback failed, ret %{public}d.", ret);
877 #endif
878 }
879 
OnAudioParamChange(const std::string & adapterName,const AudioParamKey key,const std::string & condition,const std::string & value)880 void RemoteAudioRendererSinkInner::OnAudioParamChange(const std::string &adapterName, const AudioParamKey key,
881     const std::string &condition, const std::string &value)
882 {
883     AUDIO_INFO_LOG("Audio param change event, key:%{public}d, condition:%{public}s, value:%{public}s",
884         key, condition.c_str(), value.c_str());
885     if (key == AudioParamKey::PARAM_KEY_STATE) {
886         ClearRender();
887     }
888 
889     CHECK_AND_RETURN_LOG(callback_ != nullptr, "Sink audio param callback is null.");
890     callback_->OnAudioSinkParamChange(adapterName, key, condition, value);
891 }
892 
GetTransactionId(uint64_t * transactionId)893 int32_t RemoteAudioRendererSinkInner::GetTransactionId(uint64_t *transactionId)
894 {
895     (void)transactionId;
896     AUDIO_ERR_LOG("GetTransactionId not supported");
897     return ERR_NOT_SUPPORTED;
898 }
899 
SetVoiceVolume(float volume)900 int32_t RemoteAudioRendererSinkInner::SetVoiceVolume(float volume)
901 {
902     (void)volume;
903     AUDIO_ERR_LOG("SetVoiceVolume not supported");
904     return ERR_NOT_SUPPORTED;
905 }
906 
SetOutputRoutes(std::vector<DeviceType> & outputDevices)907 int32_t RemoteAudioRendererSinkInner::SetOutputRoutes(std::vector<DeviceType> &outputDevices)
908 {
909     (void)outputDevices;
910     AUDIO_DEBUG_LOG("SetOutputRoutes not supported.");
911     return ERR_NOT_SUPPORTED;
912 }
913 
SetAudioMonoState(bool audioMono)914 void RemoteAudioRendererSinkInner::SetAudioMonoState(bool audioMono)
915 {
916     (void)audioMono;
917     AUDIO_ERR_LOG("SetAudioMonoState not supported");
918     return;
919 }
920 
SetAudioBalanceValue(float audioBalance)921 void RemoteAudioRendererSinkInner::SetAudioBalanceValue(float audioBalance)
922 {
923     (void)audioBalance;
924     AUDIO_ERR_LOG("SetAudioBalanceValue not supported");
925     return;
926 }
927 
GetPresentationPosition(uint64_t & frames,int64_t & timeSec,int64_t & timeNanoSec)928 int32_t RemoteAudioRendererSinkInner::GetPresentationPosition(uint64_t& frames, int64_t& timeSec, int64_t& timeNanoSec)
929 {
930     AUDIO_ERR_LOG("GetPresentationPosition not supported");
931     return ERR_NOT_SUPPORTED;
932 }
933 
GetNetworkId()934 std::string RemoteAudioRendererSinkInner::GetNetworkId()
935 {
936     return deviceNetworkId_;
937 }
938 
ResetOutputRouteForDisconnect(DeviceType device)939 void RemoteAudioRendererSinkInner::ResetOutputRouteForDisconnect(DeviceType device)
940 {
941     AUDIO_WARNING_LOG("not supported.");
942 }
943 
GetParamCallback()944 OHOS::AudioStandard::IAudioSinkCallback* RemoteAudioRendererSinkInner::GetParamCallback()
945 {
946     return callback_;
947 }
948 
SetPaPower(int32_t flag)949 int32_t RemoteAudioRendererSinkInner::SetPaPower(int32_t flag)
950 {
951     (void)flag;
952     return ERR_NOT_SUPPORTED;
953 }
954 
SetPriPaPower()955 int32_t RemoteAudioRendererSinkInner::SetPriPaPower()
956 {
957     return ERR_NOT_SUPPORTED;
958 }
959 
UpdateAppsUid(const int32_t appsUid[MAX_MIX_CHANNELS],const size_t size)960 int32_t RemoteAudioRendererSinkInner::UpdateAppsUid(const int32_t appsUid[MAX_MIX_CHANNELS], const size_t size)
961 {
962     return ERR_NOT_SUPPORTED;
963 }
964 
UpdateAppsUid(const std::vector<int32_t> & appsUid)965 int32_t RemoteAudioRendererSinkInner::UpdateAppsUid(const std::vector<int32_t> &appsUid)
966 {
967     return ERR_NOT_SUPPORTED;
968 }
969 
DfxOperation(BufferDesc & buffer,AudioSampleFormat format,AudioChannel channel) const970 void RemoteAudioRendererSinkInner::DfxOperation(BufferDesc &buffer, AudioSampleFormat format,
971     AudioChannel channel) const
972 {
973     ChannelVolumes vols = VolumeTools::CountVolumeLevel(buffer, format, channel);
974     if (channel == MONO) {
975         Trace::Count(logUtilsTag_, vols.volStart[0]);
976     } else {
977         Trace::Count(logUtilsTag_, (vols.volStart[0] + vols.volStart[1]) / HALF_FACTOR);
978     }
979     AudioLogUtils::ProcessVolumeData(logUtilsTag_, vols, volumeDataCount_);
980 }
981 } // namespace AudioStandard
982 } // namespace OHOS
983