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 HPAE_RENDERER_STREAM_IMPL_H 17 #define HPAE_RENDERER_STREAM_IMPL_H 18 19 #include <mutex> 20 #include <shared_mutex> 21 #include "i_renderer_stream.h" 22 #include "audio_ring_cache.h" 23 24 namespace OHOS { 25 namespace AudioStandard { 26 27 class HpaeRendererStreamImpl : public std::enable_shared_from_this<HpaeRendererStreamImpl>, 28 public IStreamStatusCallback, 29 public IStreamCallback, 30 public IRendererStream { 31 public: 32 HpaeRendererStreamImpl(AudioProcessConfig processConfig, bool isMoveAble, bool isCallbackMode = true); 33 ~HpaeRendererStreamImpl(); 34 int32_t InitParams(const std::string &deviceName = ""); 35 int32_t Start() override; 36 int32_t StartWithSyncId(const int32_t &syncId) override; 37 int32_t Pause(bool isStandby = false) override; 38 int32_t Flush() override; 39 int32_t Drain(bool stopFlag = false) override; 40 int32_t Stop() override; 41 int32_t Release() override; 42 int32_t GetStreamFramesWritten(uint64_t &framesWritten) override; 43 int32_t GetCurrentTimeStamp(uint64_t ×tamp) override; 44 int32_t GetCurrentPosition(uint64_t &framePosition, uint64_t ×tamp, uint64_t &latency, int32_t base) override; 45 int32_t GetSpeedPosition(uint64_t &framePosition, uint64_t ×tamp, uint64_t &latency, int32_t base) override; 46 int32_t GetLatency(uint64_t &latency) override; 47 int32_t SetRate(int32_t rate) override; 48 int32_t SetAudioEffectMode(int32_t effectMode) override; 49 int32_t GetAudioEffectMode(int32_t &effectMode) override; 50 int32_t SetPrivacyType(int32_t privacyType) override; 51 int32_t GetPrivacyType(int32_t &privacyType) override; 52 int32_t SetSpeed(float speed) override; 53 54 void RegisterStatusCallback(const std::weak_ptr<IStatusCallback> &callback) override; 55 void RegisterWriteCallback(const std::weak_ptr<IWriteCallback> &callback) override; 56 BufferDesc DequeueBuffer(size_t length) override; 57 int32_t EnqueueBuffer(const BufferDesc &bufferDesc) override; 58 int32_t GetMinimumBufferSize(size_t &minBufferSize) const override; 59 void GetByteSizePerFrame(size_t &byteSizePerFrame) const override; 60 void GetSpanSizePerFrame(size_t &spanSizeInFrame) const override; 61 void SetStreamIndex(uint32_t index) override; 62 uint32_t GetStreamIndex() override; 63 void AbortCallback(int32_t abortTimes); 64 // offload 65 int32_t SetOffloadMode(int32_t state, bool isAppBack) override; 66 int32_t UnsetOffloadMode() override; 67 int32_t GetOffloadApproximatelyCacheTime(uint64_t ×tamp, uint64_t &paWriteIndex, 68 uint64_t &cacheTimeDsp, uint64_t &cacheTimePa) override; 69 int32_t OffloadSetVolume(float volume) override; 70 int32_t SetOffloadDataCallbackState(int32_t state) override; 71 size_t GetWritableSize() override; 72 int32_t UpdateMaxLength(uint32_t maxLength) override; 73 // offload end 74 75 int32_t UpdateSpatializationState(bool spatializationEnabled, bool headTrackingEnabled) override; 76 int32_t Peek(std::vector<char> *audioBuffer, int32_t &index) override; 77 int32_t ReturnIndex(int32_t index) override; 78 AudioProcessConfig GetAudioProcessConfig() const noexcept override; 79 int32_t SetClientVolume(float clientVolume) override; 80 int32_t SetLoudnessGain(float loudnessGain) override; 81 void BlockStream() noexcept override; 82 int32_t OnStreamData(AudioCallBackStreamInfo& callBackStremInfo) override; 83 void OnStatusUpdate(IOperation operation, uint32_t streamIndex) override; 84 private: 85 void SyncOffloadMode(); 86 void InitRingBuffer(); 87 int32_t WriteDataFromRingBuffer(bool forceData, int8_t *inputData, size_t &requestDataLen); 88 uint32_t GetA2dpOffloadLatency(); // unit ms 89 uint32_t GetNearlinkLatency(); // unit ms 90 void GetLatencyInner(uint64_t ×tamp, uint64_t &latencyUs, int32_t base); 91 void OnDeviceClassChange(const AudioCallBackStreamInfo &callBackStreamInfo); 92 int32_t GetRemoteOffloadSpeedPosition(uint64_t &framePosition, uint64_t ×tamp, uint64_t &latency); 93 94 uint32_t streamIndex_ = static_cast<uint32_t>(-1); // invalid index 95 AudioProcessConfig processConfig_; 96 std::weak_ptr<IStatusCallback> statusCallback_; 97 std::weak_ptr<IWriteCallback> writeCallback_; 98 State state_ = INVALID; 99 100 size_t byteSizePerFrame_ = 0; 101 size_t spanSizeInFrame_ = 0; 102 size_t minBufferSize_ = 0; 103 uint64_t expectedPlaybackDurationMs_ = 0; 104 105 int32_t renderRate_ = 0; 106 int32_t effectMode_ = -1; 107 int32_t privacyType_ = 0; 108 109 float powerVolumeFactor_ = 1.0f; 110 // Only for debug 111 int32_t abortFlag_ = 0; 112 // offload 113 bool offloadEnable_ = false; 114 std::atomic<int32_t> offloadStatePolicy_ = OFFLOAD_DEFAULT; 115 // offload end 116 float clientVolume_ = 1.0f; 117 // latency position timeStamp 118 std::shared_mutex latencyMutex_; 119 uint64_t framePosition_ = 0; 120 uint64_t lastFramePosition_ = 0; 121 uint64_t lastHdiFramePosition_ = 0; 122 std::vector<uint64_t> timestamp_ = {Timestamp::Timestampbase::BASESIZE, 0}; 123 uint64_t latency_ = 0; 124 uint64_t framesWritten_ = 0; 125 std::string deviceClass_; 126 std::string deviceNetId_; 127 // record latency 128 129 // buffer mode, write or callback 130 bool isCallbackMode_ = true; // true is callback buffer mode, false is write buffer mode 131 bool isMoveAble_ = true; 132 std::unique_ptr<AudioRingCache> ringBuffer_ = nullptr; // used by write buffer mode 133 FILE *dumpEnqueueIn_ = nullptr; 134 // buffer mode, write or callback end 135 }; 136 } // namespace AudioStandard 137 } // namespace OHOS 138 #endif // pro_renderer_stream_impl_H 139