1 /*
2  * Copyright (c) 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 
16 #ifndef RENDERER_IN_SERVER_H
17 #define RENDERER_IN_SERVER_H
18 
19 #include <mutex>
20 #include "i_renderer_stream.h"
21 #include "i_stream_listener.h"
22 #include "oh_audio_buffer.h"
23 #include "i_stream_manager.h"
24 #include "audio_effect.h"
25 
26 namespace OHOS {
27 namespace AudioStandard {
28 class StreamCallbacks : public IStatusCallback, public IWriteCallback {
29 public:
30     explicit StreamCallbacks(uint32_t streamIndex);
31     virtual ~StreamCallbacks() = default;
32     void OnStatusUpdate(IOperation operation) override;
33     int32_t OnWriteData(size_t length) override;
34 private:
35     uint32_t streamIndex_ = 0;
36 };
37 
38 class RendererInServer : public IStatusCallback, public IWriteCallback,
39     public std::enable_shared_from_this<RendererInServer> {
40 public:
41     RendererInServer(AudioProcessConfig processConfig, std::weak_ptr<IStreamListener> streamListener);
42     virtual ~RendererInServer();
43     void OnStatusUpdate(IOperation operation) override;
44     void OnStatusUpdateExt(IOperation operation, std::shared_ptr<IStreamListener> stateListener);
45     void HandleOperationFlushed();
46     int32_t OnWriteData(size_t length) override;
47 
48     int32_t ResolveBuffer(std::shared_ptr<OHAudioBuffer> &buffer);
49     int32_t GetSessionId(uint32_t &sessionId);
50     int32_t Start();
51     int32_t Pause();
52     int32_t Flush();
53     int32_t Drain(bool stopFlag = false);
54     int32_t Stop();
55     int32_t Release();
56 
57     int32_t GetAudioTime(uint64_t &framePos, uint64_t &timestamp);
58     int32_t GetAudioPosition(uint64_t &framePos, uint64_t &timestamp, uint64_t &latency);
59     int32_t GetLatency(uint64_t &latency);
60     int32_t SetRate(int32_t rate);
61     int32_t SetLowPowerVolume(float volume);
62     int32_t GetLowPowerVolume(float &volume);
63     int32_t SetAudioEffectMode(int32_t effectMode);
64     int32_t GetAudioEffectMode(int32_t &effectMode);
65     int32_t SetPrivacyType(int32_t privacyType);
66     int32_t GetPrivacyType(int32_t &privacyType);
67 
68     int32_t SetOffloadMode(int32_t state, bool isAppBack);
69     int32_t UnsetOffloadMode();
70     int32_t GetOffloadApproximatelyCacheTime(uint64_t &timestamp, uint64_t &paWriteIndex,
71         uint64_t &cacheTimeDsp, uint64_t &cacheTimePa);
72     int32_t UpdateSpatializationState(bool spatializationEnabled, bool headTrackingEnabled);
73     void CheckAndWriterRenderStreamStandbySysEvent(bool standbyEnable);
74 
75     int32_t Init();
76     int32_t ConfigServerBuffer();
77     int32_t InitBufferStatus();
78     int32_t UpdateWriteIndex();
79     BufferDesc DequeueBuffer(size_t length);
80     void VolumeHandle(BufferDesc &desc);
81     int32_t WriteData();
82     void WriteEmptyData();
83     int32_t DrainAudioBuffer();
84 
85     // for inner-cap
86     int32_t EnableInnerCap();
87     int32_t DisableInnerCap();
88     int32_t InitDupStream();
89 
90     // for dual tone
91     int32_t EnableDualTone();
92     int32_t DisableDualTone();
93     int32_t InitDualToneStream();
94 
95     int32_t GetStreamManagerType() const noexcept;
96     int32_t SetSilentModeAndMixWithOthers(bool on);
97     int32_t SetClientVolume();
98     int32_t SetMute(bool isMute);
99     int32_t SetDuckFactor(float duckFactor);
100 
101     void OnDataLinkConnectionUpdate(IOperation operation);
102     int32_t GetActualStreamManagerType() const noexcept;
103 
104     bool Dump(std::string &dumpString);
105     void SetNonInterruptMute(const bool muteFlag);
106     void dualToneStreamInStart();
107 
108 public:
109     const AudioProcessConfig processConfig_;
110 private:
111     void OnStatusUpdateSub(IOperation operation);
112     bool IsHightResolution() const noexcept;
113     void WriteMuteDataSysEvent(uint8_t *buffer, size_t bufferSize);
114     void ReportDataToResSched(bool isSilent);
115     void ReportDataToResSched(std::unordered_map<std::string, std::string> payload, uint32_t type);
116     void OtherStreamEnqueue(const BufferDesc &bufferDesc);
117     void DoFadingOut(BufferDesc& bufferDesc);
118     int32_t SetStreamVolumeInfoForEnhanceChain();
119     void StandByCheck();
120     bool ShouldEnableStandBy();
121     int32_t OffloadSetVolumeInner();
122 
123 private:
124     std::mutex statusLock_;
125     std::condition_variable statusCv_;
126     std::shared_ptr<IRendererStream> stream_ = nullptr;
127     uint32_t streamIndex_ = -1;
128     std::string traceTag_;
129     IStatus status_ = I_STATUS_IDLE;
130     bool offloadEnable_ = false;
131     std::atomic<bool> standByEnable_ = false;
132     std::atomic<bool> muteFlag_ = false;
133 
134     // for inner-cap
135     std::mutex dupMutex_;
136     std::atomic<bool> isInnerCapEnabled_ = false;
137     uint32_t dupStreamIndex_ = 0;
138     std::shared_ptr<StreamCallbacks> dupStreamCallback_ = nullptr;
139     std::shared_ptr<IRendererStream> dupStream_ = nullptr;
140 
141     // for dual sink tone
142     std::mutex dualToneMutex_;
143     std::atomic<bool> isDualToneEnabled_ = false;
144     uint32_t dualToneStreamIndex_ = 0;
145     std::shared_ptr<IRendererStream> dualToneStream_ = nullptr;
146 
147     std::weak_ptr<IStreamListener> streamListener_;
148     size_t totalSizeInFrame_ = 0;
149     size_t spanSizeInFrame_ = 0;
150     size_t spanSizeInByte_ = 0;
151     size_t byteSizePerFrame_ = 0;
152     bool isBufferConfiged_  = false;
153     std::atomic<bool> isInited_ = false;
154     std::shared_ptr<OHAudioBuffer> audioServerBuffer_ = nullptr;
155     std::atomic<size_t> needForceWrite_ = 0;
156     bool afterDrain = false;
157     float lowPowerVolume_ = 1.0f;
158     bool isNeedFade_ = false;
159     float oldAppliedVolume_ = MAX_FLOAT_VOLUME;
160     std::mutex updateIndexLock_;
161     int64_t startedTime_ = 0;
162     uint32_t underrunCount_ = 0;
163     std::atomic<uint32_t> standByCounter_ = 0;
164     int64_t lastWriteTime_ = 0;
165     bool resetTime_ = false;
166     uint64_t resetTimestamp_ = 0;
167     std::mutex writeLock_;
168     FILE *dumpC2S_ = nullptr; // client to server dump file
169     std::string dumpFileName_ = "";
170     ManagerType managerType_;
171     std::atomic<bool> silentModeAndMixWithOthers_ = false;
172     std::time_t startMuteTime_ = 0;
173     int32_t silentState_ = 1; // 0:silent 1:unsilent
174     std::mutex fadeoutLock_;
175     int32_t fadeoutFlag_ = 0;
176     int32_t effectModeWhenDual_ = EFFECT_DEFAULT;
177     int32_t renderEmptyCountForInnerCap_ = 0;
178 
179     // only read & write in CheckAndWriterRenderStreamStandbySysEvent
180     bool lastWriteStandbyEnableStatus_ = false;
181 };
182 } // namespace AudioStandard
183 } // namespace OHOS
184 #endif // RENDERER_IN_SERVER_H
185