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