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 ×tamp); 58 int32_t GetAudioPosition(uint64_t &framePos, uint64_t ×tamp, 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 ×tamp, uint64_t &paWriteIndex, 71 uint64_t &cacheTimeDsp, uint64_t &cacheTimePa); 72 int32_t OffloadSetVolume(float volume); 73 int32_t UpdateSpatializationState(bool spatializationEnabled, bool headTrackingEnabled); 74 void WriterRenderStreamStandbySysEvent(); 75 76 int32_t Init(); 77 int32_t ConfigServerBuffer(); 78 int32_t InitBufferStatus(); 79 int32_t UpdateWriteIndex(); 80 BufferDesc DequeueBuffer(size_t length); 81 void VolumeHandle(BufferDesc &desc); 82 int32_t WriteData(); 83 void WriteEmptyData(); 84 int32_t DrainAudioBuffer(); 85 86 // for inner-cap 87 int32_t EnableInnerCap(); 88 int32_t DisableInnerCap(); 89 int32_t InitDupStream(); 90 91 // for dual tone 92 int32_t EnableDualTone(); 93 int32_t DisableDualTone(); 94 int32_t InitDualToneStream(); 95 96 int32_t GetStreamManagerType() const noexcept; 97 int32_t SetSilentModeAndMixWithOthers(bool on); 98 int32_t SetClientVolume(); 99 int32_t SetMute(bool isMute); 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 107 public: 108 const AudioProcessConfig processConfig_; 109 private: 110 void OnStatusUpdateSub(IOperation operation); 111 bool IsHightResolution() const noexcept; 112 void WriteMuteDataSysEvent(uint8_t *buffer, size_t bufferSize); 113 void ReportDataToResSched(bool isSilent); 114 void OtherStreamEnqueue(const BufferDesc &bufferDesc); 115 void DoFadingOut(BufferDesc& bufferDesc); 116 int32_t SetStreamVolumeInfoForEnhanceChain(); 117 void StandByCheck(); 118 bool ShouldEnableStandBy(); 119 120 private: 121 std::mutex statusLock_; 122 std::condition_variable statusCv_; 123 std::shared_ptr<IRendererStream> stream_ = nullptr; 124 uint32_t streamIndex_ = -1; 125 std::string traceTag_; 126 IStatus status_ = I_STATUS_IDLE; 127 bool offloadEnable_ = false; 128 std::atomic<bool> standByEnable_ = false; 129 std::atomic<bool> muteFlag_ = false; 130 131 // for inner-cap 132 std::mutex dupMutex_; 133 std::atomic<bool> isInnerCapEnabled_ = false; 134 uint32_t dupStreamIndex_ = 0; 135 std::shared_ptr<StreamCallbacks> dupStreamCallback_ = nullptr; 136 std::shared_ptr<IRendererStream> dupStream_ = nullptr; 137 138 // for dual sink tone 139 std::mutex dualToneMutex_; 140 std::atomic<bool> isDualToneEnabled_ = false; 141 uint32_t dualToneStreamIndex_ = 0; 142 std::shared_ptr<IRendererStream> dualToneStream_ = nullptr; 143 144 std::weak_ptr<IStreamListener> streamListener_; 145 size_t totalSizeInFrame_ = 0; 146 size_t spanSizeInFrame_ = 0; 147 size_t spanSizeInByte_ = 0; 148 size_t byteSizePerFrame_ = 0; 149 bool isBufferConfiged_ = false; 150 std::atomic<bool> isInited_ = false; 151 std::shared_ptr<OHAudioBuffer> audioServerBuffer_ = nullptr; 152 std::atomic<size_t> needForceWrite_ = 0; 153 bool afterDrain = false; 154 float lowPowerVolume_ = 1.0f; 155 bool isNeedFade_ = false; 156 float oldAppliedVolume_ = MAX_FLOAT_VOLUME; 157 std::mutex updateIndexLock_; 158 int64_t startedTime_ = 0; 159 uint32_t underrunCount_ = 0; 160 std::atomic<uint32_t> standByCounter_ = 0; 161 int64_t lastWriteTime_ = 0; 162 bool resetTime_ = false; 163 uint64_t resetTimestamp_ = 0; 164 std::mutex writeLock_; 165 FILE *dumpC2S_ = nullptr; // client to server dump file 166 std::string dumpFileName_ = ""; 167 ManagerType managerType_; 168 std::atomic<bool> silentModeAndMixWithOthers_ = false; 169 std::time_t startMuteTime_ = 0; 170 int32_t silentState_ = 1; // 0:silent 1:unsilent 171 std::mutex fadeoutLock_; 172 int32_t fadeoutFlag_ = 0; 173 int32_t effectModeWhenDual_ = EFFECT_DEFAULT; 174 int32_t renderEmptyCountForInnerCap_ = 0; 175 }; 176 } // namespace AudioStandard 177 } // namespace OHOS 178 #endif // RENDERER_IN_SERVER_H 179