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_PREVIEW_OUTPUT_H
17 #define OHOS_CAMERA_PREVIEW_OUTPUT_H
18 
19 #include <cstdint>
20 #include "camera_output_capability.h"
21 #include "capture_output.h"
22 #include "hstream_repeat_callback_stub.h"
23 #include "icamera_service.h"
24 #include "istream_repeat.h"
25 #include "istream_repeat_callback.h"
26 #include "input/camera_device.h"
27 
28 namespace OHOS {
29 namespace CameraStandard {
30 static const std::string CONST_PREVIEW_FRAME_START = "frameStart";
31 static const std::string CONST_PREVIEW_FRAME_END = "frameEnd";
32 static const std::string CONST_PREVIEW_FRAME_ERROR = "error";
33 static const std::string CONST_SKETCH_STATUS_CHANGED = "sketchStatusChanged";
34 class SketchWrapper;
35 class PreviewStateCallback {
36 public:
37     PreviewStateCallback() = default;
38     virtual ~PreviewStateCallback() = default;
39 
40     /**
41      * @brief Called when preview frame is started rendering.
42      */
43     virtual void OnFrameStarted() const = 0;
44 
45     /**
46      * @brief Called when preview frame is ended.
47      *
48      * @param frameCount Indicates number of frames captured.
49      */
50     virtual void OnFrameEnded(const int32_t frameCount) const = 0;
51 
52     /**
53      * @brief Called when error occured during preview rendering.
54      *
55      * @param errorCode Indicates a {@link ErrorCode} which will give information for preview callback error.
56      */
57     virtual void OnError(const int32_t errorCode) const = 0;
58 
59     /**
60      * @brief Called when sketch status changed.
61      *
62      * @param statusData Indicates a {@link SketchStatusData}.
63      */
64     virtual void OnSketchStatusDataChanged(const SketchStatusData& statusData) const = 0;
65 };
66 
67 class PreviewOutput : public CaptureOutput {
68 public:
69     explicit PreviewOutput(sptr<IBufferProducer> bufferProducer);
70     explicit PreviewOutput();
71     virtual ~PreviewOutput();
72 
73     /**
74      * @brief Set the preview callback for the preview output.
75      *
76      * @param PreviewStateCallback to be triggered.
77      */
78     void SetCallback(std::shared_ptr<PreviewStateCallback> callback);
79 
80     int32_t CreateStream() override;
81 
82     /**
83      * @brief Releases a instance of the preview output.
84      */
85     int32_t Release() override;
86 
87     /**
88      * @brief Add delayed preview surface.
89      *
90      * @param surface to add.
91      */
92     void AddDeferredSurface(sptr<Surface> surface);
93 
94     /**
95      * @brief Start preview stream.
96      */
97     int32_t Start();
98 
99     /**
100      * @brief stop preview stream.
101      */
102     int32_t Stop();
103 
104     /**
105      * @brief Check whether the current preview mode supports sketch.
106      *
107      * @return Return the supported result.
108      */
109     bool IsSketchSupported();
110 
111     /**
112      * @brief Get the scaling ratio threshold for sketch callback data.
113      *
114      * @return Return the threshold value.
115      */
116     float GetSketchRatio();
117 
118     /**
119      * @brief get the preview rotation angle.
120      *
121      * @return result of the photo rotation angle.
122      */
123     int32_t GetPreviewRotation(int32_t imageRotation);
124 
125     /**
126      * @brief set the preview rotation angle.
127      */
128     int32_t SetPreviewRotation(int32_t imageRotation, bool isDisplayLocked);
129 
130     /**
131      * @brief Enable sketch
132      *
133      * @param isEnable True for enable, false otherwise.
134      *
135      */
136     int32_t EnableSketch(bool isEnable);
137 
138     /**
139      * @brief Attach sketch surface
140      *
141      * @param sketchSurface Sketch surface
142      *
143      */
144     int32_t AttachSketchSurface(sptr<Surface> sketchSurface);
145 
146     /**
147      * @brief Set the preview fps.
148      *
149      * @param frameRate value of frame rate.
150      */
151     int32_t SetFrameRate(int32_t minFrameRate, int32_t maxFrameRate);
152 
153     /**
154      * @brief Get the active preview frame rate range.
155      *
156      * @return Returns vector<int32_t> of active exposure compensation range.
157      */
158     const std::vector<int32_t>& GetFrameRateRange();
159 
160     /**
161      * @brief Set the preview fps range. If fixed frame rate
162      * to be set the both min and max framerate should be same.
163      *
164      * @param minFrameRate min frame rate value of range.
165      * @param maxFrameRate max frame rate value of range.
166      */
167     void SetFrameRateRange(int32_t minFrameRate, int32_t maxFrameRate);
168 
169     /**
170      * @brief Set the format
171      *
172      * @param format format of the previewOutput.
173      */
174     void SetOutputFormat(int32_t format);
175 
176     /**
177      * @brief Set the size
178      *
179      * @param size size of the previewOutput.
180      */
181     void SetSize(Size size);
182 
183     /**
184      * @brief Get the supported preview frame rate range.
185      *
186      * @return Returns vector<int32_t> of supported exposure compensation range.
187      */
188     std::vector<std::vector<int32_t>> GetSupportedFrameRates();
189 
190     /**
191      * @brief Get the application callback information.
192      *
193      * @return Returns the pointer application callback.
194      */
195     std::shared_ptr<PreviewStateCallback> GetApplicationCallback();
196 
197     /**
198      * @brief Get Observed matadata tags
199      *        Register tags into capture session. If the tags data changes,{@link OnControlMetadataChanged} will be
200      *        called.
201      * @return Observed tags
202      */
203     virtual const std::set<camera_device_metadata_tag_t>& GetObserverControlTags() override;
204 
205     /**
206      * @brief Callback of request metadata change.
207      * @return Operate result
208      */
209     int32_t OnControlMetadataChanged(
210         const camera_device_metadata_tag_t tag, const camera_metadata_item_t& metadataItem) override;
211 
212     /**
213      * @brief Callback of result metadata change.
214      * @return Operate result
215      */
216     int32_t OnResultMetadataChanged(
217         const camera_device_metadata_tag_t tag, const camera_metadata_item_t& metadataItem) override;
218 
219     int32_t OnSketchStatusChanged(SketchStatus status);
220 
221     void OnNativeRegisterCallback(const std::string& eventString);
222     void OnNativeUnregisterCallback(const std::string& eventString);
223 private:
224     int32_t PreviewFormat_;
225     Size PreviewSize_;
226     std::shared_ptr<PreviewStateCallback> appCallback_;
227     sptr<IStreamRepeatCallback> svcCallback_;
228     std::shared_ptr<SketchWrapper> sketchWrapper_;
229     std::shared_ptr<OHOS::Camera::CameraMetadata> GetDeviceMetadata();
230     std::shared_ptr<Size> FindSketchSize();
231     std::vector<int32_t> previewFrameRateRange_{0, 0};
232     int32_t CreateSketchWrapper(Size sketchSize);
233     int32_t StartSketch();
234     int32_t StopSketch();
235     void CameraServerDied(pid_t pid) override;
236     int32_t canSetFrameRateRange(int32_t minFrameRate, int32_t maxFrameRate);
237     int32_t JudegRotationFunc(int32_t imageRotation);
238 };
239 
240 class PreviewOutputCallbackImpl : public HStreamRepeatCallbackStub {
241 public:
242     wptr<PreviewOutput> previewOutput_ = nullptr;
PreviewOutputCallbackImpl()243     PreviewOutputCallbackImpl() : previewOutput_(nullptr) {}
244 
PreviewOutputCallbackImpl(PreviewOutput * previewOutput)245     explicit PreviewOutputCallbackImpl(PreviewOutput* previewOutput) : previewOutput_(previewOutput) {}
246 
247     /**
248      * @brief Called when preview frame is started rendering.
249      */
250     int32_t OnFrameStarted() override;
251 
252     /**
253      * @brief Called when preview frame is ended.
254      *
255      * @param frameCount Indicates number of frames captured.
256      */
257     int32_t OnFrameEnded(int32_t frameCount) override;
258 
259     /**
260      * @brief Called when error occured during preview rendering.
261      *
262      * @param errorCode Indicates a {@link ErrorCode} which will give information for preview callback error.
263      */
264     int32_t OnFrameError(int32_t errorCode) override;
265 
266     /**
267      * @brief Called when sketch status changed.
268      *
269      * @param status Indicates a {@link SketchStatus} which will give information for preview callback error.
270      */
271     int32_t OnSketchStatusChanged(SketchStatus status) override;
272 
273     int32_t OnDeferredVideoEnhancementInfo(CaptureEndedInfoExt captureEndedInfo) override;
274 };
275 } // namespace CameraStandard
276 } // namespace OHOS
277 #endif // OHOS_CAMERA_PREVIEW_OUTPUT_H
278