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