1 /* 2 * Copyright (c) 2024 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 #ifndef PRO_RENDERER_STREAM_IMPL_H 16 #define PRO_RENDERER_STREAM_IMPL_H 17 18 #include <atomic> 19 #include <queue> 20 #include <mutex> 21 #include <condition_variable> 22 #include "i_renderer_stream.h" 23 #include "audio_resample.h" 24 #include "linear_pos_time_model.h" 25 #include "audio_down_mix_stereo.h" 26 27 namespace OHOS { 28 namespace AudioStandard { 29 class ProRendererStreamImpl : public IRendererStream { 30 public: 31 ProRendererStreamImpl(AudioProcessConfig processConfig, bool isDirect); 32 ~ProRendererStreamImpl(); 33 int32_t InitParams(); 34 int32_t Start() override; 35 int32_t Pause(bool isStandby = false) override; 36 int32_t Flush() override; 37 int32_t Drain() override; 38 int32_t Stop() override; 39 int32_t Release() override; 40 int32_t GetStreamFramesWritten(uint64_t &framesWritten) override; 41 int32_t GetCurrentTimeStamp(uint64_t ×tamp) override; 42 int32_t GetCurrentPosition(uint64_t &framePosition, uint64_t ×tamp, uint64_t &latency) override; 43 int32_t GetLatency(uint64_t &latency) override; 44 int32_t SetRate(int32_t rate) override; 45 int32_t SetLowPowerVolume(float volume) override; 46 int32_t GetLowPowerVolume(float &powerVolume) override; 47 int32_t SetAudioEffectMode(int32_t effectMode) override; 48 int32_t GetAudioEffectMode(int32_t &effectMode) override; 49 int32_t SetPrivacyType(int32_t privacyType) override; 50 int32_t GetPrivacyType(int32_t &privacyType) override; 51 52 void RegisterStatusCallback(const std::weak_ptr<IStatusCallback> &callback) override; 53 void RegisterWriteCallback(const std::weak_ptr<IWriteCallback> &callback) override; 54 BufferDesc DequeueBuffer(size_t length) override; 55 int32_t EnqueueBuffer(const BufferDesc &bufferDesc) override; 56 int32_t GetMinimumBufferSize(size_t &minBufferSize) const override; 57 void GetByteSizePerFrame(size_t &byteSizePerFrame) const override; 58 void GetSpanSizePerFrame(size_t &spanSizeInFrame) const override; 59 void SetStreamIndex(uint32_t index) override; 60 uint32_t GetStreamIndex() override; 61 void AbortCallback(int32_t abortTimes) override; 62 // offload 63 int32_t SetOffloadMode(int32_t state, bool isAppBack) override; 64 int32_t UnsetOffloadMode() override; 65 int32_t GetOffloadApproximatelyCacheTime(uint64_t ×tamp, uint64_t &paWriteIndex, uint64_t &cacheTimeDsp, 66 uint64_t &cacheTimePa) override; 67 int32_t OffloadSetVolume(float volume) override; 68 size_t GetWritableSize() override; 69 // offload end 70 71 int32_t UpdateSpatializationState(bool spatializationEnabled, bool headTrackingEnabled) override; 72 int32_t UpdateMaxLength(uint32_t maxLength) override; 73 74 AudioProcessConfig GetAudioProcessConfig() const noexcept override; 75 int32_t Peek(std::vector<char> *audioBuffer, int32_t &index) override; 76 int32_t ReturnIndex(int32_t index) override; 77 int32_t SetClientVolume(float clientVolume) override; 78 79 private: 80 bool GetAudioTime(uint64_t &framePos, int64_t &sec, int64_t &nanoSec); 81 AudioSamplingRate GetDirectSampleRate(AudioSamplingRate sampleRate) const noexcept; 82 AudioSampleFormat GetDirectFormat(AudioSampleFormat format) const noexcept; 83 void ConvertSrcToFloat(uint8_t *buffer, size_t bufLength, float volume); 84 void ConvertFloatToDes(int32_t writeIndex); 85 float GetStreamVolume(); 86 void PopSinkBuffer(std::vector<char> *audioBuffer, int32_t &index); 87 int32_t PopWriteBufferIndex(); 88 void SetOffloadDisable(); 89 void InitBasicInfo(const AudioStreamInfo &streamInfo); 90 91 private: 92 bool isDirect_; 93 bool isNeedResample_; 94 bool isNeedMcr_; 95 bool isBlock_; 96 bool isDrain_; 97 bool isFirstFrame_; 98 int32_t privacyType_; 99 int32_t renderRate_; 100 uint32_t streamIndex_; // invalid index 101 int32_t abortFlag_; 102 uint32_t currentRate_; 103 uint32_t desSamplingRate_; 104 AudioSampleFormat desFormat_; 105 size_t byteSizePerFrame_; 106 size_t spanSizeInFrame_; 107 size_t totalBytesWritten_; 108 size_t sinkBytesWritten_; 109 size_t minBufferSize_; 110 float powerVolumeFactor_; 111 std::atomic<IStatus> status_; 112 std::weak_ptr<IStatusCallback> statusCallback_; 113 std::weak_ptr<IWriteCallback> writeCallback_; 114 std::vector<float> resampleSrcBuffer; 115 std::vector<float> resampleDesBuffer; 116 std::vector<std::vector<char>> sinkBuffer_; 117 std::shared_ptr<AudioResample> resample_; 118 std::queue<int32_t> readQueue_; 119 std::queue<int32_t> writeQueue_; 120 LinearPosTimeModel handleTimeModel_; 121 AudioProcessConfig processConfig_; 122 std::unique_ptr<AudioDownMixStereo> downMixer_; 123 124 std::mutex firstFrameMutex; 125 std::mutex enqueueMutex; 126 std::mutex peekMutex; 127 std::condition_variable firstFrameSync_; 128 std::condition_variable drainSync_; 129 FILE *dumpFile_; 130 131 std::atomic<bool> isFirstNoUnderrunFrame_ = false; 132 }; 133 } // namespace AudioStandard 134 } // namespace OHOS 135 #endif 136