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; 32 int32_t Drain(bool stopFlag = false) 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 ×tamp) 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 50 private: 51 static void PAStreamReadCb(pa_stream *stream, size_t length, void *userdata); 52 static void PAStreamMovedCb(pa_stream *stream, void *userdata); 53 static void PAStreamUnderFlowCb(pa_stream *stream, void *userdata); 54 static void PAStreamSetStartedCb(pa_stream *stream, void *userdata); 55 static void PAStreamStartSuccessCb(pa_stream *stream, int32_t success, void *userdata); 56 static void PAStreamPauseSuccessCb(pa_stream *stream, int32_t success, void *userdata); 57 static void PAStreamFlushSuccessCb(pa_stream *stream, int32_t success, void *userdata); 58 static void PAStreamStopSuccessCb(pa_stream *stream, int32_t success, void *userdata); 59 static void PAStreamUpdateTimingInfoSuccessCb(pa_stream *stream, int32_t success, void *userdata); 60 61 uint32_t streamIndex_ = static_cast<uint32_t>(-1); // invalid index 62 63 pa_stream *paStream_ = nullptr; 64 AudioProcessConfig processConfig_ = {}; 65 std::weak_ptr<IStatusCallback> statusCallback_; 66 std::weak_ptr<IReadCallback> readCallback_; 67 std::mutex streamImplLock_; 68 int32_t streamCmdStatus_ = 0; 69 int32_t streamFlushStatus_ = 0; 70 State state_ = INVALID; 71 uint32_t underFlowCount_ = 0; 72 pa_threaded_mainloop *mainloop_ = nullptr; 73 74 size_t byteSizePerFrame_ = 0; 75 size_t spanSizeInFrame_ = 0; 76 size_t minBufferSize_ = 0; 77 78 uint64_t totalBytesRead_ = 0; 79 80 FILE *capturerServerDumpFile_ = nullptr; 81 bool releasedFlag_ = false; 82 }; 83 } // namespace AudioStandard 84 } // namespace OHOS 85 #endif // PA_CAPTURER_STREAM_IMPL_H 86