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