• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 GetStandbyStatus(bool &isStandby, int64_t &enterStandbyTime);
76 
77     int32_t Init();
78     int32_t ConfigServerBuffer();
79     int32_t InitBufferStatus();
80     int32_t UpdateWriteIndex();
81     BufferDesc DequeueBuffer(size_t length);
82     void VolumeHandle(BufferDesc &desc);
83     int32_t WriteData();
84     void WriteEmptyData();
85     int32_t DrainAudioBuffer();
86 
87     // for inner-cap
88     int32_t EnableInnerCap();
89     int32_t DisableInnerCap();
90     int32_t InitDupStream();
91 
92     // for dual tone
93     int32_t EnableDualTone();
94     int32_t DisableDualTone();
95     int32_t InitDualToneStream();
96 
97     int32_t GetStreamManagerType() const noexcept;
98     int32_t SetSilentModeAndMixWithOthers(bool on);
99     int32_t SetClientVolume();
100     int32_t SetMute(bool isMute);
101     int32_t SetDuckFactor(float duckFactor);
102 
103     void OnDataLinkConnectionUpdate(IOperation operation);
104     int32_t GetActualStreamManagerType() const noexcept;
105 
106     bool Dump(std::string &dumpString);
107     void SetNonInterruptMute(const bool muteFlag);
108     void dualToneStreamInStart();
109 
110 public:
111     const AudioProcessConfig processConfig_;
112 private:
113     void OnStatusUpdateSub(IOperation operation);
114     bool IsHightResolution() const noexcept;
115     void WriteMuteDataSysEvent(uint8_t *buffer, size_t bufferSize);
116     void ReportDataToResSched(bool isSilent);
117     void ReportDataToResSched(std::unordered_map<std::string, std::string> payload, uint32_t type);
118     void OtherStreamEnqueue(const BufferDesc &bufferDesc);
119     void DoFadingOut(BufferDesc& bufferDesc);
120     int32_t SetStreamVolumeInfoForEnhanceChain();
121     void StandByCheck();
122     bool ShouldEnableStandBy();
123     int32_t OffloadSetVolumeInner();
124 
125 private:
126     std::mutex statusLock_;
127     std::condition_variable statusCv_;
128     std::shared_ptr<IRendererStream> stream_ = nullptr;
129     uint32_t streamIndex_ = -1;
130     std::string traceTag_;
131     IStatus status_ = I_STATUS_IDLE;
132     bool offloadEnable_ = false;
133     std::atomic<bool> standByEnable_ = false;
134     std::atomic<bool> muteFlag_ = false;
135 
136     // for inner-cap
137     std::mutex dupMutex_;
138     std::atomic<bool> isInnerCapEnabled_ = false;
139     uint32_t dupStreamIndex_ = 0;
140     std::shared_ptr<StreamCallbacks> dupStreamCallback_ = nullptr;
141     std::shared_ptr<IRendererStream> dupStream_ = nullptr;
142 
143     // for dual sink tone
144     std::mutex dualToneMutex_;
145     std::atomic<bool> isDualToneEnabled_ = false;
146     uint32_t dualToneStreamIndex_ = 0;
147     std::shared_ptr<IRendererStream> dualToneStream_ = nullptr;
148 
149     std::weak_ptr<IStreamListener> streamListener_;
150     size_t totalSizeInFrame_ = 0;
151     size_t spanSizeInFrame_ = 0;
152     size_t spanSizeInByte_ = 0;
153     size_t byteSizePerFrame_ = 0;
154     bool isBufferConfiged_  = false;
155     std::atomic<bool> isInited_ = false;
156     std::shared_ptr<OHAudioBuffer> audioServerBuffer_ = nullptr;
157     std::atomic<size_t> needForceWrite_ = 0;
158     bool afterDrain = false;
159     float lowPowerVolume_ = 1.0f;
160     std::atomic<bool> isMuted_ = false;
161     bool isNeedFade_ = false;
162     float oldAppliedVolume_ = MAX_FLOAT_VOLUME;
163     std::mutex updateIndexLock_;
164     int64_t startedTime_ = 0;
165     uint32_t underrunCount_ = 0;
166     std::atomic<uint32_t> standByCounter_ = 0;
167     int64_t enterStandbyTime_ = 0;
168     int64_t lastWriteTime_ = 0;
169     bool resetTime_ = false;
170     uint64_t resetTimestamp_ = 0;
171     std::mutex writeLock_;
172     FILE *dumpC2S_ = nullptr; // client to server dump file
173     std::string dumpFileName_ = "";
174     ManagerType managerType_;
175     std::atomic<bool> silentModeAndMixWithOthers_ = false;
176     std::time_t startMuteTime_ = 0;
177     int32_t silentState_ = 1; // 0:silent 1:unsilent
178     std::mutex fadeoutLock_;
179     int32_t fadeoutFlag_ = 0;
180     int32_t effectModeWhenDual_ = EFFECT_DEFAULT;
181     int32_t renderEmptyCountForInnerCap_ = 0;
182 
183     // only read & write in CheckAndWriterRenderStreamStandbySysEvent
184     bool lastWriteStandbyEnableStatus_ = false;
185 };
186 } // namespace AudioStandard
187 } // namespace OHOS
188 #endif // RENDERER_IN_SERVER_H
189