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