• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "audio_common_converter.h"
27 
28 namespace OHOS {
29 namespace AudioStandard {
30 class ProRendererStreamImpl : public IRendererStream {
31 public:
32     ProRendererStreamImpl(AudioProcessConfig processConfig, bool isDirect);
33     ~ProRendererStreamImpl();
34     int32_t InitParams();
35     int32_t Start() override;
36     int32_t Pause(bool isStandby = false) override;
37     int32_t Flush() override;
38     int32_t Drain(bool stopFlag = false) override;
39     int32_t Stop() override;
40     int32_t Release() override;
41     int32_t GetStreamFramesWritten(uint64_t &framesWritten) override;
42     int32_t GetCurrentTimeStamp(uint64_t &timestamp) override;
43     int32_t GetCurrentPosition(uint64_t &framePosition, uint64_t &timestamp, uint64_t &latency) override;
44     int32_t GetLatency(uint64_t &latency) override;
45     int32_t SetRate(int32_t rate) override;
46     int32_t SetAudioEffectMode(int32_t effectMode) override;
47     int32_t GetAudioEffectMode(int32_t &effectMode) override;
48     int32_t SetPrivacyType(int32_t privacyType) override;
49     int32_t GetPrivacyType(int32_t &privacyType) override;
50 
51     void RegisterStatusCallback(const std::weak_ptr<IStatusCallback> &callback) override;
52     void RegisterWriteCallback(const std::weak_ptr<IWriteCallback> &callback) override;
53     BufferDesc DequeueBuffer(size_t length) override;
54     int32_t EnqueueBuffer(const BufferDesc &bufferDesc) override;
55     int32_t GetMinimumBufferSize(size_t &minBufferSize) const override;
56     void GetByteSizePerFrame(size_t &byteSizePerFrame) const override;
57     void GetSpanSizePerFrame(size_t &spanSizeInFrame) const override;
58     void SetStreamIndex(uint32_t index) override;
59     uint32_t GetStreamIndex() override;
60     // offload
61     int32_t SetOffloadMode(int32_t state, bool isAppBack) override;
62     int32_t UnsetOffloadMode() override;
63     int32_t GetOffloadApproximatelyCacheTime(uint64_t &timestamp, 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     void BlockStream() noexcept override;
77 
78 private:
79     bool GetAudioTime(uint64_t &framePos, int64_t &sec, int64_t &nanoSec);
80     AudioSamplingRate GetDirectSampleRate(AudioSamplingRate sampleRate) const noexcept;
81     AudioSampleFormat GetDirectFormat(AudioSampleFormat format) const noexcept;
82     void ConvertSrcToFloat(const BufferDesc &bufferDesc);
83     void ConvertFloatToDes(int32_t writeIndex);
84     void GetStreamVolume();
85     void PopSinkBuffer(std::vector<char> *audioBuffer, int32_t &index);
86     int32_t PopWriteBufferIndex();
87     void SetOffloadDisable();
88     void InitBasicInfo(const AudioStreamInfo &streamInfo);
89 
90 private:
91     bool isDirect_;
92     bool isNeedResample_;
93     bool isNeedMcr_;
94     bool isBlock_;
95     bool isDrain_;
96     bool isFirstFrame_;
97     int32_t privacyType_;
98     int32_t renderRate_;
99     uint32_t streamIndex_; // invalid index
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     BufferBaseInfo bufferInfo_;
121 
122     std::mutex firstFrameMutex;
123     std::mutex enqueueMutex;
124     std::mutex peekMutex;
125     std::condition_variable firstFrameSync_;
126     std::condition_variable drainSync_;
127     FILE *dumpFile_;
128 
129     std::atomic<bool> isFirstNoUnderrunFrame_ = false;
130 };
131 } // namespace AudioStandard
132 } // namespace OHOS
133 #endif
134