• 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 FAST_AUDIO_RENDER_SINK_H
17 #define FAST_AUDIO_RENDER_SINK_H
18 
19 #include "sink/i_audio_render_sink.h"
20 #include <iostream>
21 #include <cstring>
22 #include <mutex>
23 #include "v5_0/iaudio_manager.h"
24 #include "util/audio_running_lock.h"
25 #include "util/callback_wrapper.h"
26 
27 namespace OHOS {
28 namespace AudioStandard {
29 class FastAudioRenderSink : public IAudioRenderSink {
30 public:
31     FastAudioRenderSink() = default;
32     ~FastAudioRenderSink();
33 
34     int32_t Init(const IAudioSinkAttr &attr) override;
35     void DeInit(void) override;
36     bool IsInited(void) override;
37 
38     int32_t Start(void) override;
39     int32_t Stop(void) override;
40     int32_t Resume(void) override;
41     int32_t Pause(void) override;
42     int32_t Flush(void) override;
43     int32_t Reset(void) override;
44     int32_t RenderFrame(char &data, uint64_t len, uint64_t &writeLen) override;
45     int64_t GetVolumeDataCount() 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     int32_t SetSinkMuteForSwitchDevice(bool mute) final;
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     int32_t GetMmapBufferInfo(int &fd, uint32_t &totalSizeInframe, uint32_t &spanSizeInframe,
83         uint32_t &byteSizePerFrame, uint32_t &syncInfoSize) override;
84     int32_t GetMmapHandlePosition(uint64_t &frames, int64_t &timeSec, int64_t &timeNanoSec) override;
85 
86     static uint32_t PcmFormatToBit(AudioSampleFormat format);
87     static AudioFormat ConvertToHdiFormat(AudioSampleFormat format);
88     void InitAudioSampleAttr(struct AudioSampleAttributes &param);
89     void InitDeviceDesc(struct AudioDeviceDescriptor &deviceDesc);
90     int32_t CreateRender(void);
91     void UpdateSinkState(bool started);
92 
93     // low latency
94     int32_t PrepareMmapBuffer(void);
95     void ReleaseMmapBuffer(void);
96     int32_t CheckPositionTime(void);
97     void PreparePosition(void);
98 
99 private:
100     static constexpr uint32_t AUDIO_CHANNELCOUNT = 2;
101     static constexpr uint32_t DEEP_BUFFER_RENDER_PERIOD_SIZE = 3840;
102     static constexpr float DEFAULT_VOLUME_LEVEL = 1.0f;
103     static constexpr int32_t HALF_FACTOR = 2;
104     static constexpr int32_t MAX_GET_POSITION_TRY_COUNT = 50;
105     static constexpr int32_t GENERAL_MAX_GET_POSITION_HANDLE_TIME = 10000000; // 10ms = 10ns * 1000 * 1000
106     static constexpr int32_t VOIP_MAX_GET_POSITION_HANDLE_TIME = 20000000; // 20ms = 20ns * 1000 * 1000
107     static constexpr int32_t MAX_GET_POSITION_WAIT_TIME = 2000000; // 2000000us
108     static constexpr int32_t INVALID_FD = -1;
109 #ifdef FEATURE_POWER_MANAGER
110     static constexpr const char *RUNNING_LOCK_NAME = "AudioFastBackgroundPlay";
111     static constexpr int32_t RUNNING_LOCK_TIMEOUTMS_LASTING = -1;
112 #endif
113 
114     std::string halName_ = "";
115     IAudioSinkAttr attr_ = {};
116     SinkCallbackWrapper callback_ = {};
117     bool sinkInited_ = false;
118     bool started_ = false;
119     bool paused_ = false;
120     float leftVolume_ = DEFAULT_VOLUME_LEVEL;
121     float rightVolume_ = DEFAULT_VOLUME_LEVEL;
122     uint32_t hdiRenderId_ = 0;
123     struct IAudioRender *audioRender_ = nullptr;
124     // for device switch
125     std::mutex switchDeviceMutex_;
126     int32_t muteCount_ = 0;
127     std::atomic<bool> switchDeviceMute_ = false;
128 #ifdef FEATURE_POWER_MANAGER
129     std::shared_ptr<AudioRunningLock> runningLock_;
130 #endif
131     std::mutex sinkMutex_;
132     std::mutex startMutex_;
133 
134     // low latency
135     int32_t bufferFd_ = INVALID_FD;
136     uint32_t frameSizeInByte_ = 1;
137     uint32_t bufferTotalFrameSize_ = 0;
138     uint32_t eachReadFrameSize_ = 0;
139     uint32_t syncInfoSize_ = 0;
140     size_t bufferSize_ = 0;
141 #ifdef DEBUG_DIRECT_USE_HDI
142     int32_t privBufferFd_ = INVALID_FD;
143     char *bufferAddress_ = nullptr;
144     uint32_t curReadPos_ = 0;
145     uint32_t curWritePos_ = 0;
146     bool isFirstWrite_ = true;
147     uint32_t writeAheadPeriod_ = 1;
148 #endif
149 };
150 
151 } // namespace AudioStandard
152 } // namespace OHOS
153 
154 #endif // FAST_AUDIO_RENDER_SINK_H
155