1 /*
2  * Copyright (c) 2021-2022 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 OHOS_CAMERA_CAPTURE_OUTPUT_H
17 #define OHOS_CAMERA_CAPTURE_OUTPUT_H
18 
19 #include <cstdint>
20 #include <mutex>
21 #include <refbase.h>
22 #include <set>
23 #include <type_traits>
24 #include <unordered_set>
25 #include <vector>
26 
27 #include "camera_device_ability_items.h"
28 #include "camera_error_code.h"
29 #include "camera_metadata_operator.h"
30 #include "camera_output_capability.h"
31 #include "icamera_util.h"
32 #include "input/camera_death_recipient.h"
33 #include "istream_common.h"
34 
35 namespace OHOS {
36 class IBufferProducer;
37 namespace CameraStandard {
38 enum DeferredDeliveryImageType {
39     DELIVERY_NONE = 0,
40     DELIVERY_PHOTO,
41     DELIVERY_VIDEO,
42 };
43 enum CaptureOutputType {
44     CAPTURE_OUTPUT_TYPE_PREVIEW,
45     CAPTURE_OUTPUT_TYPE_PHOTO,
46     CAPTURE_OUTPUT_TYPE_VIDEO,
47     CAPTURE_OUTPUT_TYPE_METADATA,
48     CAPTURE_OUTPUT_TYPE_DEPTH_DATA,
49     CAPTURE_OUTPUT_TYPE_MAX
50 };
51 
52 class MetadataObserver {
53 public:
54     /**
55      * @brief Get Observed matadata tags
56      *        Register tags into capture session. If the tags data changes,{@link OnControlMetadataChanged} will be
57      *        called.
58      * @return Observed tags
59      */
GetObserverControlTags()60     virtual const std::set<camera_device_metadata_tag_t>& GetObserverControlTags()
61     {
62         // Empty impl
63         const static std::set<camera_device_metadata_tag_t> tags = {};
64         return tags;
65     };
66 
67     /**
68      * @brief Get Observed matadata tags
69      *        Register tags into capture session. If the tags data changes,{@link OnResultMetadataChanged} will be
70      *        called.
71      * @return Observed tags
72      */
GetObserverResultTags()73     virtual const std::set<camera_device_metadata_tag_t>& GetObserverResultTags()
74     {
75         // Empty impl
76         const static std::set<camera_device_metadata_tag_t> tags = {};
77         return tags;
78     };
79 
80     /**
81      * @brief Callback of request metadata change.
82      * @return Operate result
83      */
OnControlMetadataChanged(const camera_device_metadata_tag_t tag,const camera_metadata_item_t & metadataItem)84     virtual int32_t OnControlMetadataChanged(
85         const camera_device_metadata_tag_t tag, const camera_metadata_item_t& metadataItem)
86     {
87         // Empty impl
88         return CAM_META_SUCCESS;
89     };
90 
91     /**
92      * @brief Callback of result metadata change.
93      * @return Operate result
94      */
OnResultMetadataChanged(const camera_device_metadata_tag_t tag,const camera_metadata_item_t & metadataItem)95     virtual int32_t OnResultMetadataChanged(
96         const camera_device_metadata_tag_t tag, const camera_metadata_item_t& metadataItem)
97     {
98         // Empty impl
99         return CAM_META_SUCCESS;
100     };
101 };
102 
103 class CaptureSession;
104 class CaptureOutput : virtual public RefBase, public MetadataObserver {
105 public:
106     enum Tag { DYNAMIC_PROFILE };
107     enum ImageRotation {
108         ROTATION_0 = 0,
109         ROTATION_90 = 90,
110         ROTATION_180 = 180,
111         ROTATION_270 = 270
112     };
113     explicit CaptureOutput(CaptureOutputType outputType, StreamType streamType, sptr<IBufferProducer> bufferProducer,
114         sptr<IStreamCommon> stream);
115     virtual ~CaptureOutput();
116 
117     /**
118      * @brief Releases the instance of CaptureOutput.
119      */
120     virtual int32_t Release() = 0;
121 
122     CaptureOutputType GetOutputType();
123     const char* GetOutputTypeString();
124     StreamType GetStreamType();
125     sptr<IStreamCommon> GetStream();
126     void SetStream(sptr<IStreamCommon> stream);
127     bool IsStreamCreated();
128     sptr<CaptureSession> GetSession();
129     void SetSession(wptr<CaptureSession> captureSession);
130     std::mutex asyncOpMutex_;
131     std::mutex outputCallbackMutex_;
132     void SetPhotoProfile(Profile& profile);
133     std::shared_ptr<Profile> GetPhotoProfile();
134     void SetPreviewProfile(Profile& profile);
135     std::shared_ptr<Profile> GetPreviewProfile();
136     void SetVideoProfile(VideoProfile& videoProfile);
137     std::shared_ptr<VideoProfile> GetVideoProfile();
138     void SetDepthProfile(DepthProfile& depthProfile);
139     std::shared_ptr<DepthProfile> GetDepthProfile();
140     void ClearProfiles();
141     virtual void CameraServerDied(pid_t pid) = 0;
142 
143     virtual int32_t CreateStream() = 0;
144 
145     virtual void AddTag(Tag tag) final;
146     virtual void RemoveTag(Tag tag) final;
147     virtual bool IsTagSetted(Tag tag) final;
148 
149 protected:
150     virtual sptr<IBufferProducer> GetBufferProducer() final;
151 
152 private:
153     void OnCameraServerDied(pid_t pid);
154     void RegisterStreamBinderDied();
155     void UnregisterStreamBinderDied();
156 
157     std::mutex deathRecipientMutex_;
158     sptr<CameraDeathRecipient> deathRecipient_ = nullptr;
159 
160     CaptureOutputType outputType_;
161     StreamType streamType_;
162     sptr<IStreamCommon> stream_;
163     wptr<CaptureSession> session_;
164     std::mutex sessionMutex_;
165     std::mutex streamMutex_;
166 
167     // Multithread add same output,set profile may cause problems, let's add mutex guard.
168     std::mutex photoProfileMutex_;
169     std::shared_ptr<Profile> photoProfile_;
170     std::mutex previewProfileMutex_;
171     std::shared_ptr<Profile> previewProfile_;
172     std::mutex videoProfileMutex_;
173     std::shared_ptr<VideoProfile> videoProfile_;
174     std::mutex depthProfileMutex_;
175     std::shared_ptr<DepthProfile> depthProfile_;
176 
177     std::mutex bufferProducerMutex_;
178     sptr<IBufferProducer> bufferProducer_;
179 
180     std::mutex tagsMutex_;
181     std::unordered_set<Tag> tags_;
182 };
183 } // namespace CameraStandard
184 } // namespace OHOS
185 #endif // OHOS_CAMERA_CAPTURE_OUTPUT_H
186