• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 
16 #ifndef MULTICHANNEL_AUDIO_RENDER_SINK_H
17 #define MULTICHANNEL_AUDIO_RENDER_SINK_H
18 
19 #include "sink/i_audio_render_sink.h"
20 #include <iostream>
21 #include <cstring>
22 #include "v5_0/iaudio_manager.h"
23 #include "audio_utils.h"
24 #include "adapter/i_device_manager.h"
25 #include "util/audio_running_lock.h"
26 #include "util/callback_wrapper.h"
27 
28 namespace OHOS {
29 namespace AudioStandard {
30 class MultichannelAudioRenderSink : public IAudioRenderSink {
31 public:
32     explicit MultichannelAudioRenderSink(const std::string &halName = "multichannel");
33     ~MultichannelAudioRenderSink();
34 
35     int32_t Init(const IAudioSinkAttr &attr) override;
36     void DeInit(void) override;
37     bool IsInited(void) override;
38 
39     int32_t Start(void) override;
40     int32_t Stop(void) override;
41     int32_t Resume(void) override;
42     int32_t Pause(void) override;
43     int32_t Flush(void) override;
44     int32_t Reset(void) override;
45     int32_t RenderFrame(char &data, uint64_t len, uint64_t &writeLen) override;
46     int64_t GetVolumeDataCount() override;
47 
48     int32_t SuspendRenderSink(void) override;
49     int32_t RestoreRenderSink(void) override;
50 
51     void SetAudioParameter(const AudioParamKey key, const std::string &condition, const std::string &value) override;
52     std::string GetAudioParameter(const AudioParamKey key, const std::string &condition) override;
53 
54     int32_t SetVolume(float left, float right) override;
55     int32_t GetVolume(float &left, float &right) override;
56 
57     int32_t GetLatency(uint32_t &latency) override;
58     int32_t GetTransactionId(uint64_t &transactionId) override;
59     int32_t GetPresentationPosition(uint64_t &frames, int64_t &timeSec, int64_t &timeNanoSec) override;
60     float GetMaxAmplitude(void) override;
61     void SetAudioMonoState(bool audioMono) override;
62     void SetAudioBalanceValue(float audioBalance) override;
63 
64     int32_t SetAudioScene(AudioScene audioScene, bool scoExcludeFlag = false) override;
65     int32_t GetAudioScene(void) override;
66 
67     int32_t UpdateActiveDevice(std::vector<DeviceType> &outputDevices) override;
68     void RegistCallback(uint32_t type, IAudioSinkCallback *callback) override;
69     void ResetActiveDeviceForDisconnect(DeviceType device) override;
70 
71     int32_t SetPaPower(int32_t flag) override;
72     int32_t SetPriPaPower(void) override;
73 
74     int32_t UpdateAppsUid(const int32_t appsUid[MAX_MIX_CHANNELS], const size_t size) final;
75     int32_t UpdateAppsUid(const std::vector<int32_t> &appsUid) final;
76 
77     void DumpInfo(std::string &dumpString) override;
78 
79     void SetDmDeviceType(uint16_t dmDeviceType, DeviceType deviceType) override;
80 
81 private:
82     static uint32_t PcmFormatToBit(AudioSampleFormat format);
83     static AudioFormat ConvertToHdiFormat(AudioSampleFormat format);
84     static AudioSampleFormat ParseAudioFormat(const std::string &format);
85     static AudioCategory GetAudioCategory(AudioScene audioScene);
86     void InitAudioSampleAttr(struct AudioSampleAttributes &param);
87     void InitDeviceDesc(struct AudioDeviceDescriptor &deviceDesc);
88     void InitSceneDesc(struct AudioSceneDescriptor &sceneDesc, AudioScene audioScene);
89     int32_t CreateRender(void);
90     int32_t DoSetOutputRoute(std::vector<DeviceType> &outputDevices);
91     int32_t InitRender(void);
92     void AdjustStereoToMono(char *data, uint64_t len);
93     void AdjustAudioBalance(char *data, uint64_t len);
94     void CheckUpdateState(char *data, uint64_t len);
95     void UpdateSinkState(bool started);
96 
97 private:
98     static constexpr uint32_t AUDIO_SAMPLE_RATE_48K = 48000;
99     static constexpr uint32_t DEEP_BUFFER_RENDER_PERIOD_SIZE = 4096;
100     static constexpr uint32_t STEREO_CHANNEL_COUNT = 2;
101     static constexpr float DEFAULT_VOLUME_LEVEL = 1.0f;
102     static constexpr uint16_t GET_MAX_AMPLITUDE_FRAMES_THRESHOLD = 10;
103     static constexpr int32_t HALF_FACTOR = 2;
104     static constexpr int32_t SLEEP_TIME_FOR_EMPTY_FRAME = 120;
105 #ifdef FEATURE_POWER_MANAGER
106     static constexpr const char *RUNNING_LOCK_NAME_BASE = "AudioMultichannelBackgroundPlay";
107     static constexpr int32_t RUNNING_LOCK_TIMEOUTMS_LASTING = -1;
108 #endif
109 
110     const std::string halName_ = "";
111     IAudioSinkAttr attr_ = {};
112     SinkCallbackWrapper callback_ = {};
113     bool sinkInited_ = false;
114     bool renderInited_ = false;
115     bool started_ = false;
116     bool paused_ = false;
117     float leftVolume_ = DEFAULT_VOLUME_LEVEL;
118     float rightVolume_ = DEFAULT_VOLUME_LEVEL;
119     uint32_t openSpeaker_ = 0;
120     uint32_t hdiRenderId_ = 0;
121     std::string adapterNameCase_ = "";
122     struct IAudioRender *audioRender_ = nullptr;
123     bool audioMonoState_ = false;
124     bool audioBalanceState_ = false;
125     float leftBalanceCoef_ = 1.0f;
126     float rightBalanceCoef_ = 1.0f;
127     // for get amplitude
128     float maxAmplitude_ = 0;
129     int64_t lastGetMaxAmplitudeTime_ = 0;
130     int64_t last10FrameStartTime_ = 0;
131     bool startUpdate_ = false;
132     int renderFrameNum_ = 0;
133     // for device switch
134     std::mutex switchDeviceMutex_;
135     std::atomic<bool> switchDeviceMute_ = false;
136     std::atomic<int32_t> emptyFrameCount_ = 0;
137     std::condition_variable switchDeviceCV_;
138     // for dfx log
139     int32_t logMode_ = 0;
140     std::string logUtilsTag_ = "MultichannelSink";
141     mutable int64_t volumeDataCount_ = 0;
142 #ifdef FEATURE_POWER_MANAGER
143     std::shared_ptr<AudioRunningLock> runningLock_;
144 #endif
145     FILE *dumpFile_ = nullptr;
146     std::string dumpFileName_ = "";
147     DeviceType currentActiveDevice_ = DEVICE_TYPE_NONE;
148     AudioScene currentAudioScene_ = AUDIO_SCENE_DEFAULT;
149     std::mutex sinkMutex_;
150 };
151 
152 } // namespace AudioStandard
153 } // namespace OHOS
154 
155 #endif // MULTICHANNEL_AUDIO_RENDER_SINK_H
156