• 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_CAPTURE_MESSAGE_H
17 #define STREAM_OPERATOR_CAPTURE_MESSAGE_H
18 
19 #include "camera.h"
20 #include "v1_0/vdi_types.h"
21 #include <condition_variable>
22 #include <list>
23 #include <thread>
24 #include <unordered_map>
25 #include <vector>
26 
27 namespace OHOS::Camera {
28 using namespace OHOS::VDI::Camera::V1_0;
29 enum CaptureMessageType {
30     CAPTURE_MESSAGE_TYPE_INVALID = 0,
31     CAPTURE_MESSAGE_TYPE_ON_STARTED,
32     CAPTURE_MESSAGE_TYPE_ON_ERROR,
33     CAPTURE_MESSAGE_TYPE_ON_ENDED,
34     CAPTURE_MESSAGE_TYPE_ON_SHUTTER,
35     CAPTURE_MESSAGE_TYPE_MAX,
36 };
37 
38 class ICaptureMessage {
39 public:
40     ICaptureMessage(int32_t streamId, int32_t captureId, uint64_t time, uint32_t count);
41     virtual ~ICaptureMessage() = default;
42     virtual uint32_t GetPeerMessageCount() const;
43     virtual uint64_t GetTimestamp() const;
44     virtual int32_t GetStreamId() const;
45     virtual CaptureMessageType GetMessageType() const;
46     virtual int32_t GetCaptureId() const;
47 
48     ICaptureMessage(const ICaptureMessage& other) = delete;
49     ICaptureMessage(ICaptureMessage&& other) = delete;
50     ICaptureMessage& operator=(const ICaptureMessage& other) = delete;
51     ICaptureMessage& operator=(ICaptureMessage&& other) = delete;
52 
53 protected:
54     uint32_t peerMessageCount_ = 1;
55     uint64_t timestamp_ = 0;
56     int32_t streamId_ = -1;
57     int32_t captureId_ = -1;
58 };
59 
60 class CaptureStartedMessage final : public ICaptureMessage {
61 public:
62     virtual ~CaptureStartedMessage() = default;
CaptureStartedMessage(int32_t streamId,int32_t captureId,uint64_t time,uint32_t count)63     CaptureStartedMessage(int32_t streamId, int32_t captureId, uint64_t time, uint32_t count)
64         : ICaptureMessage(streamId, captureId, time, count)
65     {
66     }
GetMessageType()67     CaptureMessageType GetMessageType() const override
68     {
69         return CAPTURE_MESSAGE_TYPE_ON_STARTED;
70     }
71 };
72 
73 class CaptureEndedMessage final : public ICaptureMessage {
74 public:
75     virtual ~CaptureEndedMessage() = default;
CaptureEndedMessage(int32_t streamId,int32_t captureId,uint64_t time,uint32_t peerCount,uint32_t frameCount)76     CaptureEndedMessage(int32_t streamId, int32_t captureId, uint64_t time, uint32_t peerCount, uint32_t frameCount)
77         : ICaptureMessage(streamId, captureId, time, peerCount)
78     {
79         frameCount_ = frameCount;
80     }
GetFrameCount()81     uint32_t GetFrameCount() const
82     {
83         return frameCount_;
84     }
GetMessageType()85     CaptureMessageType GetMessageType() const override
86     {
87         return CAPTURE_MESSAGE_TYPE_ON_ENDED;
88     }
89 
90 private:
91     uint32_t frameCount_ = 0;
92 };
93 
94 class CaptureErrorMessage final : public ICaptureMessage {
95 public:
96     virtual ~CaptureErrorMessage() = default;
CaptureErrorMessage(int32_t streamId,int32_t captureId,uint64_t time,uint32_t peerCount,VdiStreamError errorCode)97     CaptureErrorMessage(int32_t streamId, int32_t captureId, uint64_t time, uint32_t peerCount,
98         VdiStreamError errorCode) : ICaptureMessage(streamId, captureId, time, peerCount)
99     {
100         errorCode_ = errorCode;
101     }
102 
GetStreamError()103     VdiStreamError GetStreamError() const
104     {
105         return errorCode_;
106     }
107 
GetMessageType()108     CaptureMessageType GetMessageType() const override
109     {
110         return CAPTURE_MESSAGE_TYPE_ON_ERROR;
111     }
112 
113 private:
114     VdiStreamError errorCode_;
115 };
116 
117 class FrameShutterMessage final : public ICaptureMessage {
118 public:
119     virtual ~FrameShutterMessage() = default;
FrameShutterMessage(int32_t streamId,int32_t captureId,uint64_t time,uint32_t count)120     FrameShutterMessage(int32_t streamId, int32_t captureId, uint64_t time, uint32_t count)
121         : ICaptureMessage(streamId, captureId, time, count)
122     {
123     }
GetMessageType()124     CaptureMessageType GetMessageType() const override
125     {
126         return CAPTURE_MESSAGE_TYPE_ON_SHUTTER;
127     }
128 };
129 
130 using MessageGroup = std::vector<std::shared_ptr<ICaptureMessage>>;
131 using MessageOperatorFunc = std::function<void(MessageGroup&)>;
132 
133 class CaptureMessageOperator {
134 public:
135     CaptureMessageOperator() = default;
136     CaptureMessageOperator(MessageOperatorFunc f);
137     virtual ~CaptureMessageOperator();
138     CaptureMessageOperator(const CaptureMessageOperator& other) = delete;
139     CaptureMessageOperator(CaptureMessageOperator&& other) = delete;
140     CaptureMessageOperator& operator=(const CaptureMessageOperator& other) = delete;
141     CaptureMessageOperator& operator=(CaptureMessageOperator&& other) = delete;
142 
143     void SendMessage(std::shared_ptr<ICaptureMessage>& message);
144     RetCode StartProcess();
145 
146 private:
147     void HandleMessage();
148 
149 private:
150     MessageOperatorFunc messageOperator_ = nullptr;
151     std::atomic<bool> running_ = false;
152     std::atomic<bool> wakeup_ = false;
153     std::unique_ptr<std::thread> messageHandler_ = nullptr;
154     std::mutex lock_ = {};
155     std::condition_variable cv_ = {};
156     std::unordered_map<uint32_t, std::list<MessageGroup>> messageBox_ = {};
157 };
158 } // namespace OHOS::Camera
159 #endif
160