• 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 "v4_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 
47     int32_t SuspendRenderSink(void) override;
48     int32_t RestoreRenderSink(void) override;
49 
50     void SetAudioParameter(const AudioParamKey key, const std::string &condition, const std::string &value) override;
51     std::string GetAudioParameter(const AudioParamKey key, const std::string &condition) override;
52 
53     int32_t SetVolume(float left, float right) override;
54     int32_t GetVolume(float &left, float &right) override;
55 
56     int32_t GetLatency(uint32_t &latency) override;
57     int32_t GetTransactionId(uint64_t &transactionId) override;
58     int32_t GetPresentationPosition(uint64_t &frames, int64_t &timeSec, int64_t &timeNanoSec) override;
59     float GetMaxAmplitude(void) override;
60     void SetAudioMonoState(bool audioMono) override;
61     void SetAudioBalanceValue(float audioBalance) override;
62 
63     int32_t SetAudioScene(AudioScene audioScene, std::vector<DeviceType> &activeDevices) override;
64     int32_t GetAudioScene(void) override;
65 
66     int32_t UpdateActiveDevice(std::vector<DeviceType> &outputDevices) override;
67     void RegistCallback(uint32_t type, IAudioSinkCallback *callback) override;
68     void ResetActiveDeviceForDisconnect(DeviceType device) override;
69 
70     int32_t SetPaPower(int32_t flag) override;
71     int32_t SetPriPaPower(void) override;
72 
73     int32_t UpdateAppsUid(const int32_t appsUid[MAX_MIX_CHANNELS], const size_t size) final;
74     int32_t UpdateAppsUid(const std::vector<int32_t> &appsUid) final;
75 
76     void DumpInfo(std::string &dumpString) override;
77 
78 private:
79     static uint32_t PcmFormatToBit(AudioSampleFormat format);
80     static AudioFormat ConvertToHdiFormat(AudioSampleFormat format);
81     static AudioSampleFormat ParseAudioFormat(const std::string &format);
82     static AudioCategory GetAudioCategory(AudioScene audioScene);
83     void InitAudioSampleAttr(struct AudioSampleAttributes &param);
84     void InitDeviceDesc(struct AudioDeviceDescriptor &deviceDesc);
85     void InitSceneDesc(struct AudioSceneDescriptor &sceneDesc, AudioScene audioScene);
86     int32_t CreateRender(void);
87     int32_t DoSetOutputRoute(std::vector<DeviceType> &outputDevices);
88     int32_t InitRender(void);
89     void AdjustStereoToMono(char *data, uint64_t len);
90     void AdjustAudioBalance(char *data, uint64_t len);
91     void CheckUpdateState(char *data, uint64_t len);
92     void UpdateSinkState(bool started);
93 
94 private:
95     static constexpr uint32_t AUDIO_SAMPLE_RATE_48K = 48000;
96     static constexpr uint32_t DEEP_BUFFER_RENDER_PERIOD_SIZE = 4096;
97     static constexpr uint32_t STEREO_CHANNEL_COUNT = 2;
98     static constexpr float DEFAULT_VOLUME_LEVEL = 1.0f;
99     static constexpr uint16_t GET_MAX_AMPLITUDE_FRAMES_THRESHOLD = 10;
100     static constexpr int32_t HALF_FACTOR = 2;
101     static constexpr int32_t SLEEP_TIME_FOR_EMPTY_FRAME = 120;
102 #ifdef FEATURE_POWER_MANAGER
103     static constexpr const char *RUNNING_LOCK_NAME_BASE = "AudioMultichannelBackgroundPlay";
104     static constexpr int32_t RUNNING_LOCK_TIMEOUTMS_LASTING = -1;
105 #endif
106 
107     const std::string halName_ = "";
108     IAudioSinkAttr attr_ = {};
109     SinkCallbackWrapper callback_ = {};
110     bool sinkInited_ = false;
111     bool renderInited_ = false;
112     bool started_ = false;
113     bool paused_ = false;
114     float leftVolume_ = DEFAULT_VOLUME_LEVEL;
115     float rightVolume_ = DEFAULT_VOLUME_LEVEL;
116     uint32_t openSpeaker_ = 0;
117     uint32_t hdiRenderId_ = 0;
118     std::string adapterNameCase_ = "";
119     struct IAudioRender *audioRender_ = nullptr;
120     bool audioMonoState_ = false;
121     bool audioBalanceState_ = false;
122     float leftBalanceCoef_ = 1.0f;
123     float rightBalanceCoef_ = 1.0f;
124     // for get amplitude
125     float maxAmplitude_ = 0;
126     int64_t lastGetMaxAmplitudeTime_ = 0;
127     int64_t last10FrameStartTime_ = 0;
128     bool startUpdate_ = false;
129     int renderFrameNum_ = 0;
130     // for device switch
131     std::mutex switchDeviceMutex_;
132     std::atomic<bool> switchDeviceMute_ = false;
133     std::atomic<int32_t> emptyFrameCount_ = 0;
134     std::condition_variable switchDeviceCV_;
135     // for dfx log
136     int32_t logMode_ = 0;
137     std::string logUtilsTag_ = "MultichannelSink";
138     mutable int64_t volumeDataCount_ = 0;
139 #ifdef FEATURE_POWER_MANAGER
140     std::shared_ptr<AudioRunningLock> runningLock_;
141 #endif
142     FILE *dumpFile_ = nullptr;
143     std::string dumpFileName_ = "";
144     DeviceType currentActiveDevice_ = DEVICE_TYPE_NONE;
145     AudioScene currentAudioScene_ = AUDIO_SCENE_INVALID;
146     std::mutex sinkMutex_;
147 };
148 
149 } // namespace AudioStandard
150 } // namespace OHOS
151 
152 #endif // MULTICHANNEL_AUDIO_RENDER_SINK_H
153