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_VIDEO_OUTPUT_H
17 #define OHOS_CAMERA_VIDEO_OUTPUT_H
18 
19 #include <iostream>
20 #include <vector>
21 
22 #include "camera_metadata_info.h"
23 #include "hstream_repeat_callback_stub.h"
24 #include "istream_repeat.h"
25 #include "istream_repeat_callback.h"
26 #include "output/capture_output.h"
27 #include "session/capture_session.h"
28 
29 namespace OHOS {
30 namespace CameraStandard {
31 
32 enum VideoMetaType {
33     VIDEO_META_MAKER_INFO = 0,
34 };
35 
36 class VideoStateCallback {
37 public:
38     VideoStateCallback() = default;
39     virtual ~VideoStateCallback() = default;
40 
41     /**
42      * @brief Called when video frame is started rendering.
43      */
44     virtual void OnFrameStarted() const = 0;
45 
46     /**
47      * @brief Called when video frame is ended.
48      *
49      * @param frameCount Indicates number of frames captured.
50      */
51     virtual void OnFrameEnded(const int32_t frameCount) const = 0;
52 
53     /**
54      * @brief Called when error occured during video rendering.
55      *
56      * @param errorCode Indicates a {@link ErrorCode} which will give information for video callback error.
57      */
58     virtual void OnError(const int32_t errorCode) const = 0;
59 
60     virtual void OnDeferredVideoEnhancementInfo(const CaptureEndedInfoExt info) const = 0;
61 };
62 
63 class VideoOutput : public CaptureOutput {
64 public:
65     explicit VideoOutput(sptr<IBufferProducer> bufferProducer);
66     virtual ~VideoOutput();
67 
68     int32_t CreateStream() override;
69 
70     /**
71      * @brief Releases a instance of the VideoOutput.
72      */
73     int32_t Release() override;
74 
75     /**
76      * @brief Set the video callback for the video output.
77      *
78      * @param VideoStateCallback pointer to be triggered.
79      */
80     void SetCallback(std::shared_ptr<VideoStateCallback> callback);
81 
82     /**
83      * @brief Start the video capture.
84      */
85     int32_t Start();
86 
87     /**
88      * @brief Stop the video capture.
89      */
90     int32_t Stop();
91 
92     /**
93      * @brief Pause the video capture.
94      */
95     int32_t Pause();
96 
97     /**
98      * @brief Resume the paused video capture.
99      */
100     int32_t Resume();
101 
102     /**
103      * @brief get the video rotation angle.
104      */
105     int32_t GetVideoRotation(int32_t imageRotation);
106 
107     /**
108      * @brief Get the application callback information.
109      *
110      * @return VideoStateCallback pointer set by application.
111      */
112     std::shared_ptr<VideoStateCallback> GetApplicationCallback();
113 
114     /**
115      * @brief Get the active video frame rate range.
116      *
117      * @return Returns vector<int32_t> of active exposure compensation range.
118      */
119     const std::vector<int32_t>& GetFrameRateRange();
120 
121     /**
122      * @brief Set the Video fps range. If fixed frame rate
123      * to be set the both min and max framerate should be same.
124      *
125      * @param min frame rate value of range.
126      * @param max frame rate value of range.
127      */
128     void SetFrameRateRange(int32_t minFrameRate, int32_t maxFrameRate);
129 
130     /**
131      * @brief Set the format
132      *
133      * @param format format of the videoOutput.
134      */
135     void SetOutputFormat(int32_t format);
136 
137     /**
138      * @brief Set the size
139      *
140      * @param size size of the videoOutput.
141      */
142     void SetSize(Size size);
143 
144     /**
145      * @brief Set the Video fps.
146      *
147      * @param frameRate value of frame rate.
148      */
149     int32_t SetFrameRate(int32_t minFrameRate, int32_t maxFrameRate);
150 
151     /**
152      * @brief Get supported frame rate ranges from device.
153      *
154      * @return Returns vector<int32_t> supported frame rate ranges.
155      */
156     std::vector<std::vector<int32_t>> GetSupportedFrameRates();
157 
158     /**
159      * @brief Set the mirror option for the video output.
160      *
161      * @param boolean true/false to set/unset mirror respectively.
162      */
163     int32_t enableMirror(bool enabled);
164 
165     /**
166      * @brief To check the video output support mirror or not.
167      *
168      * @return Returns true/false if the video output is support mirro or not.
169      */
170     bool IsMirrorSupported();
171 
172         /**
173      * @brief Querying the supported video auxiliary stream types.
174      *
175      * @return Returns the supported video auxiliary stream types.
176      */
177     std::vector<VideoMetaType> GetSupportedVideoMetaTypes();
178 
179     /**
180      * @brief Check whether the tag is supported.
181      *
182      * @return Returns true/false if the tag is support or not.
183      */
184     bool IsTagSupported(camera_device_metadata_tag tag);
185 
186     /**
187      * @brief Sets the surface of the presentation.
188      *
189      */
190     void AttachMetaSurface(sptr<Surface> surface, VideoMetaType videoMetaType);
191     /**
192      * @brief To check the autoDeferredVideoEnhance capability is supported or not.
193      *
194      * @return Returns true/false if the autoDeferredVideoEnhance is supported/not-supported respectively.
195      */
196     int32_t IsAutoDeferredVideoEnhancementSupported();
197 
198     /**
199      * @brief To check the autoDeferredVideoEnhance capability is supported or not.
200      *
201      * @return Returns true/false if the autoDeferredVideoEnhance is supported/not-supported respectively.
202      */
203     int32_t IsAutoDeferredVideoEnhancementEnabled();
204 
205     /**
206      * @brief Enable or not enable for autoDeferredVideoEnhance capability.
207      *
208      */
209     int32_t EnableAutoDeferredVideoEnhancement(bool enabled);
210 
211     /**
212     * @brief Checks if the video has started.
213     *
214     * @return true if the video is currently running; false otherwise.
215     */
216     bool IsVideoStarted();
217 
218     /**
219      * @brief Get supported rotations.
220      *
221      */
222     int32_t GetSupportedRotations(std::vector<int32_t> &supportedRotations);
223 
224     /**
225      * @brief Check whether the rotation is supported.
226      *
227      */
228     int32_t IsRotationSupported(bool &isSupported);
229 
230     /**
231      * @brief Set the rotation.
232      *
233      */
234     int32_t SetRotation(int32_t rotation);
235 
236 private:
237     int32_t videoFormat_;
238     Size videoSize_;
239     std::shared_ptr<VideoStateCallback> appCallback_;
240     sptr<IStreamRepeatCallback> svcCallback_;
241     std::vector<int32_t> videoFrameRateRange_{0, 0};
242     std::atomic_bool isVideoStarted_ = false;
243     void CameraServerDied(pid_t pid) override;
244     int32_t canSetFrameRateRange(int32_t minFrameRate, int32_t maxFrameRate);
245 };
246 
247 class VideoOutputCallbackImpl : public HStreamRepeatCallbackStub {
248 public:
249     wptr<VideoOutput> videoOutput_ = nullptr;
VideoOutputCallbackImpl()250     VideoOutputCallbackImpl() : videoOutput_(nullptr) {}
251 
VideoOutputCallbackImpl(VideoOutput * videoOutput)252     explicit VideoOutputCallbackImpl(VideoOutput* videoOutput) : videoOutput_(videoOutput) {}
253 
~VideoOutputCallbackImpl()254     ~VideoOutputCallbackImpl()
255     {
256         videoOutput_ = nullptr;
257     }
258 
259     /**
260      * @brief Called when video frame is started rendering.
261      */
262     int32_t OnFrameStarted() override;
263 
264     /**
265      * @brief Called when video frame is ended.
266      *
267      * @param frameCount Indicates number of frames captured.
268      */
269     int32_t OnFrameEnded(const int32_t frameCount) override;
270 
271     /**
272      * @brief Called when error occured during video rendering.
273      *
274      * @param errorCode Indicates a {@link ErrorCode} which will give information for video callback error.
275      */
276     int32_t OnFrameError(const int32_t errorCode) override;
277 
278     /**
279      * @brief Empty function.
280      *
281      * @param status sketch status.
282      */
283     int32_t OnSketchStatusChanged(SketchStatus status) override;
284 
285     int32_t OnDeferredVideoEnhancementInfo(CaptureEndedInfoExt captureEndedInfo) override;
286 };
287 } // namespace CameraStandard
288 } // namespace OHOS
289 #endif // OHOS_CAMERA_VIDEO_OUTPUT_H
290