• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 PA_RENDERER_STREAM_IMPL_H
17 #define PA_RENDERER_STREAM_IMPL_H
18 
19 #include <pulse/pulseaudio.h>
20 #include <mutex>
21 #include "i_renderer_stream.h"
22 
23 namespace OHOS {
24 namespace AudioStandard {
25 class PaRendererStreamImpl : public std::enable_shared_from_this<PaRendererStreamImpl>, public IRendererStream {
26 public:
27     PaRendererStreamImpl(pa_stream *paStream, AudioProcessConfig processConfig, pa_threaded_mainloop *mainloop);
28     ~PaRendererStreamImpl();
29     int32_t InitParams();
30     int32_t Start() override;
31     int32_t Pause(bool isStandby = false) override;
32     int32_t Flush() override;
33     int32_t Drain() override;
34     int32_t Stop() override;
35     int32_t Release() override;
36     int32_t GetStreamFramesWritten(uint64_t &framesWritten) override;
37     int32_t GetCurrentTimeStamp(uint64_t &timestamp) override;
38     int32_t GetCurrentPosition(uint64_t &framePosition, uint64_t &timestamp, uint64_t &latency) override;
39     int32_t GetLatency(uint64_t &latency) override;
40     int32_t SetRate(int32_t rate) override;
41     int32_t SetLowPowerVolume(float volume) override;
42     int32_t GetLowPowerVolume(float &powerVolume) override;
43     int32_t SetAudioEffectMode(int32_t effectMode) override;
44     int32_t GetAudioEffectMode(int32_t &effectMode) override;
45     int32_t SetPrivacyType(int32_t privacyType) override;
46     int32_t GetPrivacyType(int32_t &privacyType) override;
47 
48     void RegisterStatusCallback(const std::weak_ptr<IStatusCallback> &callback) override;
49     void RegisterWriteCallback(const std::weak_ptr<IWriteCallback> &callback) override;
50     BufferDesc DequeueBuffer(size_t length) override;
51     int32_t EnqueueBuffer(const BufferDesc &bufferDesc) override;
52     int32_t GetMinimumBufferSize(size_t &minBufferSize) const override;
53     void GetByteSizePerFrame(size_t &byteSizePerFrame) const override;
54     void GetSpanSizePerFrame(size_t &spanSizeInFrame) const override;
55     void SetStreamIndex(uint32_t index) override;
56     uint32_t GetStreamIndex() override;
57     void AbortCallback(int32_t abortTimes) override;
58     // offload
59     int32_t SetOffloadMode(int32_t state, bool isAppBack) override;
60     int32_t UnsetOffloadMode() override;
61     int32_t GetOffloadApproximatelyCacheTime(uint64_t &timestamp, uint64_t &paWriteIndex,
62         uint64_t &cacheTimeDsp, uint64_t &cacheTimePa) override;
63     int32_t OffloadSetVolume(float volume) override;
64     size_t GetWritableSize() override;
65     int32_t UpdateMaxLength(uint32_t maxLength) override;
66     // offload end
67 
68     int32_t UpdateSpatializationState(bool spatializationEnabled, bool headTrackingEnabled) override;
69     int32_t Peek(std::vector<char> *audioBuffer, int32_t &index) override;
70     int32_t ReturnIndex(int32_t index) override;
71     AudioProcessConfig GetAudioProcessConfig() const noexcept override;
72     int32_t SetClientVolume(float clientVolume) override;
73 
74 private:
75     static void PAStreamWriteCb(pa_stream *stream, size_t length, void *userdata);
76     static void PAStreamMovedCb(pa_stream *stream, void *userdata);
77     static void PAStreamUnderFlowCb(pa_stream *stream, void *userdata);
78     static void PAStreamSetStartedCb(pa_stream *stream, void *userdata);
79     static void PAStreamStartSuccessCb(pa_stream *stream, int32_t success, void *userdata);
80     static void PAStreamPauseSuccessCb(pa_stream *stream, int32_t success, void *userdata);
81     static void PAStreamFlushSuccessCb(pa_stream *stream, int32_t success, void *userdata);
82     static void PAStreamDrainSuccessCb(pa_stream *stream, int32_t success, void *userdata);
83     static void PAStreamDrainInStopCb(pa_stream *stream, int32_t success, void *userdata);
84     static void PAStreamAsyncStopSuccessCb(pa_stream *stream, int32_t success, void *userdata);
85     static void PAStreamUnderFlowCountAddCb(pa_stream *stream, void *userdata);
86     static void PAStreamUpdateTimingInfoSuccessCb(pa_stream *stream, int32_t success, void *userdata);
87 
88     const std::string GetEffectModeName(int32_t effectMode);
89     // offload
90     int32_t OffloadGetPresentationPosition(uint64_t& frames, int64_t& timeSec, int64_t& timeNanoSec);
91     int32_t OffloadSetBufferSize(uint32_t sizeMs);
92     void SyncOffloadMode();
93     int32_t OffloadUpdatePolicy(AudioOffloadType statePolicy, bool force);
94     void ResetOffload();
95     int32_t OffloadUpdatePolicyInWrite();
96     int32_t UpdateEffectSessionInfo();
97     // offload end
98 
99     uint32_t GetEffectChainLatency();
100     uint32_t GetA2dpOffloadLatency();
101 
102     void UpdatePaTimingInfo();
103 
104     uint32_t streamIndex_ = static_cast<uint32_t>(-1); // invalid index
105 
106     pa_stream *paStream_ = nullptr;
107     uint32_t sinkInputIndex_ = 0;
108     AudioProcessConfig processConfig_;
109     std::weak_ptr<IStatusCallback> statusCallback_;
110     std::weak_ptr<IWriteCallback> writeCallback_;
111     int32_t streamCmdStatus_ = 0;
112     int32_t streamDrainStatus_ = 0;
113     int32_t streamFlushStatus_ = 0;
114     State state_ = INVALID;
115     uint32_t underFlowCount_ = 0;
116     bool isDrain_ = false;
117     pa_threaded_mainloop *mainloop_;
118 
119     size_t byteSizePerFrame_ = 0;
120     size_t spanSizeInFrame_ = 0;
121     size_t minBufferSize_ = 0;
122 
123     size_t totalBytesWritten_ = 0;
124     int32_t renderRate_ = 0;
125     int32_t effectMode_ = -1;
126     std::string effectSceneName_ = "";
127     int32_t privacyType_ = 0;
128 
129     float powerVolumeFactor_ = 1.0f;
130     bool isStandbyPause_ = false;
131 
132     static constexpr float MAX_STREAM_VOLUME_LEVEL = 1.0f;
133     static constexpr float MIN_STREAM_VOLUME_LEVEL = 0.0f;
134     // Only for debug
135     int32_t abortFlag_ = 0;
136     // offload
137     bool offloadEnable_ = false;
138     int64_t offloadTsOffset_ = 0;
139     uint64_t offloadTsLast_ = 0;
140     AudioOffloadType offloadStatePolicy_ = OFFLOAD_DEFAULT;
141     AudioOffloadType offloadNextStateTargetPolicy_ = OFFLOAD_DEFAULT;
142     time_t lastOffloadUpdateFinishTime_ = 0;
143     // offload end
144     float clientVolume_ = 1.0f;
145 
146     static inline std::atomic<int32_t> bufferNullCount_ = 0;
147     std::mutex fadingMutex_;
148     std::condition_variable fadingCondition_;
149 
150     // record latency
151     uint64_t preLatency_ = 50000; // 50000 default
152     pa_usec_t preTimeGetLatency_ = pa_rtclock_now();
153     bool firstGetLatency_ = true;
154     pa_usec_t preTimeGetPaLatency_ = pa_rtclock_now();
155     bool firstGetPaLatency_ = true;
156     bool releasedFlag_ = false;
157 };
158 } // namespace AudioStandard
159 } // namespace OHOS
160 #endif // PA_RENDERER_STREAM_IMPL_H
161