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_CAPTURER_STREAM_IMPL_H
17 #define PA_CAPTURER_STREAM_IMPL_H
18 
19 #include <pulse/pulseaudio.h>
20 #include "i_capturer_stream.h"
21 
22 namespace OHOS {
23 namespace AudioStandard {
24 class PaCapturerStreamImpl : public std::enable_shared_from_this<PaCapturerStreamImpl>, public ICapturerStream {
25 public:
26     PaCapturerStreamImpl(pa_stream *paStream, AudioProcessConfig processConfig, pa_threaded_mainloop *mainloop);
27     ~PaCapturerStreamImpl();
28     int32_t InitParams();
29     int32_t Start() override;
30     int32_t Pause(bool isStandby = false) override;
31     int32_t Flush() override;
Drain()32     int32_t Drain() override { return 0; };
33     int32_t Stop() override;
34     int32_t Release() override;
35     int32_t GetStreamFramesRead(uint64_t &framesRead) override;
36     int32_t GetCurrentTimeStamp(uint64_t &timestamp) override;
37     int32_t GetLatency(uint64_t &latency) override;
38 
39     void RegisterStatusCallback(const std::weak_ptr<IStatusCallback> &callback) override;
40     void RegisterReadCallback(const std::weak_ptr<IReadCallback> &callback) override;
41     BufferDesc DequeueBuffer(size_t length) override;
42     int32_t EnqueueBuffer(const BufferDesc &bufferDesc) override;
43     int32_t GetMinimumBufferSize(size_t &minBufferSize) const override;
44     void GetByteSizePerFrame(size_t &byteSizePerFrame) const override;
45     void GetSpanSizePerFrame(size_t &spanSizeInFrame) const override;
46     void SetStreamIndex(uint32_t index) override;
47     uint32_t GetStreamIndex() override;
48     int32_t DropBuffer() override;
49     void AbortCallback(int32_t abortTimes) override;
50 
51 private:
52     static void PAStreamReadCb(pa_stream *stream, size_t length, void *userdata);
53     static void PAStreamMovedCb(pa_stream *stream, void *userdata);
54     static void PAStreamUnderFlowCb(pa_stream *stream, void *userdata);
55     static void PAStreamSetStartedCb(pa_stream *stream, void *userdata);
56     static void PAStreamStartSuccessCb(pa_stream *stream, int32_t success, void *userdata);
57     static void PAStreamPauseSuccessCb(pa_stream *stream, int32_t success, void *userdata);
58     static void PAStreamFlushSuccessCb(pa_stream *stream, int32_t success, void *userdata);
59     static void PAStreamStopSuccessCb(pa_stream *stream, int32_t success, void *userdata);
60     static void PAStreamUpdateTimingInfoSuccessCb(pa_stream *stream, int32_t success, void *userdata);
61 
62     uint32_t streamIndex_ = static_cast<uint32_t>(-1); // invalid index
63 
64     pa_stream *paStream_ = nullptr;
65     AudioProcessConfig processConfig_ = {};
66     std::weak_ptr<IStatusCallback> statusCallback_;
67     std::weak_ptr<IReadCallback> readCallback_;
68     std::mutex streamImplLock_;
69     int32_t streamCmdStatus_ = 0;
70     int32_t streamFlushStatus_ = 0;
71     State state_ = INVALID;
72     uint32_t underFlowCount_ = 0;
73     pa_threaded_mainloop *mainloop_ = nullptr;
74 
75     size_t byteSizePerFrame_ = 0;
76     size_t spanSizeInFrame_ = 0;
77     size_t minBufferSize_ = 0;
78 
79     size_t totalBytesRead_ = 0;
80 
81     FILE *capturerServerDumpFile_ = nullptr;
82 
83     // Only for debug
84     int32_t abortFlag_ = 0;
85 
86     bool releasedFlag_ = false;
87 };
88 } // namespace AudioStandard
89 } // namespace OHOS
90 #endif // PA_CAPTURER_STREAM_IMPL_H
91