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