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 SetAudioEffectMode(int32_t effectMode) override; 46 int32_t GetAudioEffectMode(int32_t &effectMode) override; 47 int32_t SetPrivacyType(int32_t privacyType) override; 48 int32_t GetPrivacyType(int32_t &privacyType) override; 49 50 void RegisterStatusCallback(const std::weak_ptr<IStatusCallback> &callback) override; 51 void RegisterWriteCallback(const std::weak_ptr<IWriteCallback> &callback) override; 52 BufferDesc DequeueBuffer(size_t length) override; 53 int32_t EnqueueBuffer(const BufferDesc &bufferDesc) override; 54 int32_t GetMinimumBufferSize(size_t &minBufferSize) const override; 55 void GetByteSizePerFrame(size_t &byteSizePerFrame) const override; 56 void GetSpanSizePerFrame(size_t &spanSizeInFrame) const override; 57 void SetStreamIndex(uint32_t index) override; 58 uint32_t GetStreamIndex() override; 59 void AbortCallback(int32_t abortTimes) override; 60 // offload 61 int32_t SetOffloadMode(int32_t state, bool isAppBack) override; 62 int32_t UnsetOffloadMode() override; 63 int32_t GetOffloadApproximatelyCacheTime(uint64_t ×tamp, uint64_t &paWriteIndex, uint64_t &cacheTimeDsp, 64 uint64_t &cacheTimePa) override; 65 int32_t OffloadSetVolume(float volume) override; 66 size_t GetWritableSize() override; 67 // offload end 68 69 int32_t UpdateSpatializationState(bool spatializationEnabled, bool headTrackingEnabled) override; 70 int32_t UpdateMaxLength(uint32_t maxLength) override; 71 72 AudioProcessConfig GetAudioProcessConfig() const noexcept override; 73 int32_t Peek(std::vector<char> *audioBuffer, int32_t &index) override; 74 int32_t ReturnIndex(int32_t index) override; 75 int32_t SetClientVolume(float clientVolume) override; 76 77 private: 78 bool GetAudioTime(uint64_t &framePos, int64_t &sec, int64_t &nanoSec); 79 AudioSamplingRate GetDirectSampleRate(AudioSamplingRate sampleRate) const noexcept; 80 AudioSampleFormat GetDirectFormat(AudioSampleFormat format) const noexcept; 81 void ConvertSrcToFloat(uint8_t *buffer, size_t bufLength, float volume); 82 void ConvertFloatToDes(int32_t writeIndex); 83 float GetStreamVolume(); 84 void PopSinkBuffer(std::vector<char> *audioBuffer, int32_t &index); 85 int32_t PopWriteBufferIndex(); 86 void SetOffloadDisable(); 87 void InitBasicInfo(const AudioStreamInfo &streamInfo); 88 89 private: 90 bool isDirect_; 91 bool isNeedResample_; 92 bool isNeedMcr_; 93 bool isBlock_; 94 bool isDrain_; 95 bool isFirstFrame_; 96 int32_t privacyType_; 97 int32_t renderRate_; 98 uint32_t streamIndex_; // invalid index 99 int32_t abortFlag_; 100 uint32_t currentRate_; 101 uint32_t desSamplingRate_; 102 AudioSampleFormat desFormat_; 103 size_t byteSizePerFrame_; 104 size_t spanSizeInFrame_; 105 size_t totalBytesWritten_; 106 size_t sinkBytesWritten_; 107 size_t minBufferSize_; 108 std::atomic<IStatus> status_; 109 std::weak_ptr<IStatusCallback> statusCallback_; 110 std::weak_ptr<IWriteCallback> writeCallback_; 111 std::vector<float> resampleSrcBuffer; 112 std::vector<float> resampleDesBuffer; 113 std::vector<std::vector<char>> sinkBuffer_; 114 std::shared_ptr<AudioResample> resample_; 115 std::queue<int32_t> readQueue_; 116 std::queue<int32_t> writeQueue_; 117 LinearPosTimeModel handleTimeModel_; 118 AudioProcessConfig processConfig_; 119 std::unique_ptr<AudioDownMixStereo> downMixer_; 120 121 std::mutex firstFrameMutex; 122 std::mutex enqueueMutex; 123 std::mutex peekMutex; 124 std::condition_variable firstFrameSync_; 125 std::condition_variable drainSync_; 126 FILE *dumpFile_; 127 128 std::atomic<bool> isFirstNoUnderrunFrame_ = false; 129 }; 130 } // namespace AudioStandard 131 } // namespace OHOS 132 #endif 133