1 /* 2 * Copyright (c) 2021 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 STREAM_OPERATOR_STREAM_BASE_H 17 #define STREAM_OPERATOR_STREAM_BASE_H 18 19 #include "ibuffer.h" 20 #include "ibuffer_pool.h" 21 #include "istream.h" 22 23 namespace OHOS::Camera { 24 class StreamBase : public IStream, public std::enable_shared_from_this<StreamBase> { 25 public: 26 StreamBase(const int32_t id, 27 const StreamIntent type, 28 std::shared_ptr<IPipelineCore>& p, 29 std::shared_ptr<CaptureMessageOperator>& m); 30 virtual ~StreamBase(); 31 StreamBase(const StreamBase& other) = delete; 32 StreamBase(StreamBase&& other) = delete; 33 StreamBase& operator=(const StreamBase& other) = delete; 34 StreamBase& operator=(StreamBase&& other) = delete; 35 36 public: 37 virtual RetCode ConfigStream(StreamConfiguration& config) override; 38 virtual RetCode CommitStream() override; 39 virtual RetCode StartStream() override; 40 virtual RetCode StopStream() override; 41 virtual RetCode AddRequest(std::shared_ptr<CaptureRequest>& request) override; 42 virtual RetCode CancelRequest(const std::shared_ptr<CaptureRequest>& request) override; 43 virtual RetCode AttachStreamTunnel(std::shared_ptr<StreamTunnel>& tunnel) override; 44 virtual RetCode DetachStreamTunnel() override; 45 virtual RetCode ChangeToOfflineStream(std::shared_ptr<OfflineStream> offlineStream) override; 46 virtual bool GetTunnelMode() const override; 47 virtual StreamConfiguration GetStreamAttribute() const override; 48 virtual int32_t GetStreamId() const override; 49 virtual RetCode Capture(const std::shared_ptr<CaptureRequest>& request) override; 50 virtual RetCode OnFrame(const std::shared_ptr<CaptureRequest>& request) override; 51 virtual bool IsRunning() const override; 52 53 virtual void HandleRequest(); 54 virtual uint64_t GetUsage(); 55 virtual uint32_t GetBufferCount(); 56 virtual void HandleResult(std::shared_ptr<IBuffer>& buffer); 57 virtual RetCode DeliverBuffer(); 58 virtual RetCode ReceiveBuffer(std::shared_ptr<IBuffer>& buffer); 59 virtual uint64_t GetFrameCount() const; 60 61 enum StreamState { 62 STREAM_STATE_IDLE = 0, 63 STREAM_STATE_ACTIVE, 64 STREAM_STATE_BUSY, 65 STREAM_STATE_OFFLINE, 66 }; 67 68 protected: 69 int32_t streamId_ = -1; 70 int32_t streamType_ = -1; 71 bool isFirstRequest = true; 72 uint64_t frameCount = 0; 73 std::shared_ptr<IPipelineCore> pipelineCore_ = nullptr; 74 std::shared_ptr<IStreamPipelineCore> pipeline_ = nullptr; 75 std::shared_ptr<HostStreamMgr> hostStreamMgr_ = nullptr; 76 std::shared_ptr<CaptureMessageOperator> messenger_ = nullptr; 77 StreamConfiguration streamConfig_ = {}; 78 std::atomic<StreamState> state_ = STREAM_STATE_IDLE; 79 std::shared_ptr<StreamTunnel> tunnel_ = nullptr; 80 std::shared_ptr<IBufferPool> bufferPool_ = nullptr; 81 uint64_t poolId_ = 0; 82 83 std::mutex wtLock_ = {}; 84 std::list<std::shared_ptr<CaptureRequest>> waitingList_ = {}; 85 std::condition_variable cv_ = {}; 86 87 std::mutex tsLock_ = {}; 88 std::list<std::shared_ptr<CaptureRequest>> inTransitList_ = {}; 89 90 std::unique_ptr<std::thread> handler_ = nullptr; 91 std::shared_ptr<CaptureRequest> lastRequest_ = nullptr; 92 }; 93 } // end namespace OHOS::Camera 94 #endif // STREAM_OPERATOR_STREAM_BASE_H 95