• 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_CAPTURE_SESSION_H
17 #define OHOS_CAMERA_CAPTURE_SESSION_H
18 
19 #include <iostream>
20 #include <set>
21 #include <vector>
22 #include "camera_error_code.h"
23 #include "input/capture_input.h"
24 #include "output/capture_output.h"
25 #include "icamera_util.h"
26 #include "icapture_session.h"
27 #include "icapture_session_callback.h"
28 #include "hcapture_session_callback_stub.h"
29 
30 namespace OHOS {
31 namespace CameraStandard {
32 enum ExposureMode {
33     EXPOSURE_MODE_UNSUPPORTED = -1,
34     EXPOSURE_MODE_LOCKED = 0,
35     EXPOSURE_MODE_AUTO,
36     EXPOSURE_MODE_CONTINUOUS_AUTO
37 };
38 
39 enum FlashMode {
40     FLASH_MODE_CLOSE = 0,
41     FLASH_MODE_OPEN,
42     FLASH_MODE_AUTO,
43     FLASH_MODE_ALWAYS_OPEN,
44 };
45 
46 enum FocusMode {
47     FOCUS_MODE_MANUAL = 0,
48     FOCUS_MODE_CONTINUOUS_AUTO,
49     FOCUS_MODE_AUTO,
50     FOCUS_MODE_LOCKED,
51 };
52 
53 enum FocusState {
54     FOCUS_STATE_SCAN = 0,
55     FOCUS_STATE_FOCUSED,
56     FOCUS_STATE_UNFOCUSED
57 };
58 
59 enum ExposureState {
60     EXPOSURE_STATE_SCAN = 0,
61     EXPOSURE_STATE_CONVERGED
62 };
63 
64 enum FilterType {
65     NONE = 0,
66     CLASSIC = 1,
67     DAWN = 2,
68     PURE = 3,
69     GREY = 4,
70     NATURAL = 5,
71     MORI = 6,
72     FAIR = 7,
73     PINK = 8,
74 };
75 
76 enum BeautyType {
77     AUTO_TYPE = 0,
78     SKIN_SMOOTH = 1,
79     FACE_SLENDER = 2,
80     SKIN_TONE = 3,
81 };
82 
83 typedef struct {
84     float x;
85     float y;
86 }Point;
87 
88 class SessionCallback {
89 public:
90     SessionCallback() = default;
91     virtual ~SessionCallback() = default;
92     /**
93      * @brief Called when error occured during capture session callback.
94      *
95      * @param errorCode Indicates a {@link ErrorCode} which will give information for capture session callback error.
96      */
97     virtual void OnError(int32_t errorCode) = 0;
98 };
99 
100 class ExposureCallback {
101 public:
102     enum ExposureState {
103         SCAN = 0,
104         CONVERGED,
105     };
106     ExposureCallback() = default;
107     virtual ~ExposureCallback() = default;
108     virtual void OnExposureState(ExposureState state) = 0;
109 };
110 
111 class FocusCallback {
112 public:
113     enum FocusState {
114         SCAN = 0,
115         FOCUSED,
116         UNFOCUSED
117     };
118     FocusCallback() = default;
119     virtual ~FocusCallback() = default;
120     virtual void OnFocusState(FocusState state) = 0;
121     FocusState currentState;
122 };
123 
124 class CaptureSessionCallback : public HCaptureSessionCallbackStub {
125 public:
126     CaptureSession* captureSession_ = nullptr;
CaptureSessionCallback()127     CaptureSessionCallback() : captureSession_(nullptr) {
128     }
129 
CaptureSessionCallback(CaptureSession * captureSession)130     explicit CaptureSessionCallback(CaptureSession* captureSession) : captureSession_(captureSession) {
131     }
132 
~CaptureSessionCallback()133     ~CaptureSessionCallback()
134     {
135         captureSession_ = nullptr;
136     }
137 
138     int32_t OnError(int32_t errorCode) override;
139 };
140 
141 enum VideoStabilizationMode {
142     OFF = 0,
143     LOW,
144     MIDDLE,
145     HIGH,
146     AUTO
147 };
148 
149 class CaptureOutput;
150 class CaptureSession : public RefBase {
151 public:
152     sptr<CaptureInput> inputDevice_;
153     explicit CaptureSession(sptr<ICaptureSession> &captureSession);
CaptureSession()154     CaptureSession() {};
155     virtual ~CaptureSession();
156 
157     /**
158      * @brief Begin the capture session config.
159      */
160     int32_t BeginConfig();
161 
162     /**
163      * @brief Commit the capture session config.
164      */
165     int32_t CommitConfig();
166 
167     /**
168      * @brief Determine if the given Input can be added to session.
169      *
170      * @param CaptureInput to be added to session.
171      */
172     int32_t CanAddInput(sptr<CaptureInput> &input);
173 
174     /**
175      * @brief Add CaptureInput for the capture session.
176      *
177      * @param CaptureInput to be added to session.
178      */
179     int32_t AddInput(sptr<CaptureInput> &input);
180 
181     /**
182      * @brief Determine if the given Ouput can be added to session.
183      *
184      * @param CaptureOutput to be added to session.
185      */
186     int32_t CanAddOutput(sptr<CaptureOutput> &output);
187 
188     /**
189      * @brief Add CaptureOutput for the capture session.
190      *
191      * @param CaptureOutput to be added to session.
192      */
193     int32_t AddOutput(sptr<CaptureOutput> &output);
194 
195     /**
196      * @brief Remove CaptureInput for the capture session.
197      *
198      * @param CaptureInput to be removed from session.
199      */
200     int32_t RemoveInput(sptr<CaptureInput> &input);
201 
202     /**
203      * @brief Remove CaptureOutput for the capture session.
204      *
205      * @param CaptureOutput to be removed from session.
206      */
207     int32_t RemoveOutput(sptr<CaptureOutput> &output);
208 
209     /**
210      * @brief Starts session and preview.
211      */
212     int32_t Start();
213 
214     /**
215      * @brief Stop session and preview..
216      */
217     int32_t Stop();
218 
219     /**
220      * @brief Set the session callback for the capture session.
221      *
222      * @param SessionCallback pointer to be triggered.
223      */
224     void SetCallback(std::shared_ptr<SessionCallback> callback);
225 
226     /**
227      * @brief Get the application callback information.
228      *
229      * @return Returns the pointer to SessionCallback set by application.
230      */
231     std::shared_ptr<SessionCallback> GetApplicationCallback();
232 
233     /**
234      * @brief Releases CaptureSession instance.
235      * @return Returns errCode.
236      */
237     int32_t Release();
238 
239     /**
240     * @brief create new device control setting.
241     */
242     void LockForControl();
243 
244     /**
245     * @brief submit device control setting.
246     *
247     * @return Returns CAMERA_OK is success.
248     */
249     int32_t UnlockForControl();
250 
251     /**
252     * @brief Get the supported video sabilization modes.
253     *
254     * @return Returns vector of CameraVideoStabilizationMode supported stabilization modes.
255     */
256     std::vector<VideoStabilizationMode> GetSupportedStabilizationMode();
257 
258     /**
259     * @brief Get the supported video sabilization modes.
260     * @param vector of CameraVideoStabilizationMode supported stabilization modes.
261     * @return Returns errCode.
262     */
263     int32_t GetSupportedStabilizationMode(std::vector<VideoStabilizationMode> &modes);
264 
265     /**
266     * @brief Query whether given stabilization mode supported.
267     *
268     * @param VideoStabilizationMode stabilization mode to query.
269     * @return True is supported false otherwise.
270     */
271     bool IsVideoStabilizationModeSupported(VideoStabilizationMode stabilizationMode);
272 
273     /**
274     * @brief Query whether given stabilization mode supported.
275     *
276     * @param VideoStabilizationMode stabilization mode to query.
277     * @param bool True is supported false otherwise.
278     * @return errCode.
279     */
280     int32_t IsVideoStabilizationModeSupported(VideoStabilizationMode stabilizationMode, bool &isSupported);
281 
282     /**
283     * @brief Get the current Video Stabilizaion mode.
284     *
285     * @return Returns current Video Stabilizaion mode.
286     */
287     VideoStabilizationMode GetActiveVideoStabilizationMode();
288 
289     /**
290     * @brief Get the current Video Stabilizaion mode.
291     * @param current Video Stabilizaion mode.
292     * @return errCode
293     */
294     int32_t GetActiveVideoStabilizationMode(VideoStabilizationMode &mode);
295 
296     /**
297     * @brief Set the Video Stabilizaion mode.
298     * @param VideoStabilizationMode stabilization mode to set.
299     * @return errCode
300     */
301     int32_t SetVideoStabilizationMode(VideoStabilizationMode stabilizationMode);
302 
303     /**
304     * @brief Get the supported exposure modes.
305     *
306     * @return Returns vector of ExposureMode supported exposure modes.
307     */
308     std::vector<ExposureMode> GetSupportedExposureModes();
309 
310     /**
311     * @brief Get the supported exposure modes.
312     * @param vector of ExposureMode supported exposure modes.
313     * @return errCode.
314     */
315     int32_t GetSupportedExposureModes(std::vector<ExposureMode> &exposureModes);
316 
317     /**
318     * @brief Query whether given exposure mode supported.
319     *
320     * @param ExposureMode exposure mode to query.
321     * @return True is supported false otherwise.
322     */
323     bool IsExposureModeSupported(ExposureMode exposureMode);
324 
325     /**
326     * @brief Query whether given exposure mode supported.
327     *
328     * @param ExposureMode exposure mode to query.
329     * @param bool True is supported false otherwise.
330     * @return errCode.
331     */
332     int32_t IsExposureModeSupported(ExposureMode exposureMode, bool &isSupported);
333 
334     /**
335     * @brief Set exposure mode.
336     * @param ExposureMode exposure mode to be set.
337     * @return errCode
338     */
339     int32_t SetExposureMode(ExposureMode exposureMode);
340 
341     /**
342     * @brief Get the current exposure mode.
343     *
344     * @return Returns current exposure mode.
345     */
346     ExposureMode GetExposureMode();
347 
348     /**
349     * @brief Get the current exposure mode.
350     * @param ExposureMode current exposure mode.
351     * @return errCode.
352     */
353     int32_t GetExposureMode(ExposureMode &exposureMode);
354 
355     /**
356     * @brief Set the centre point of exposure area.
357     * @param Point which specifies the area to expose.
358     * @return errCode
359     */
360     int32_t SetMeteringPoint(Point exposurePoint);
361 
362     /**
363     * @brief Get centre point of exposure area.
364     *
365     * @return Returns current exposure point.
366     */
367     Point GetMeteringPoint();
368 
369     /**
370     * @brief Get centre point of exposure area.
371     * @param Point current exposure point.
372     * @return errCode
373     */
374     int32_t GetMeteringPoint(Point &exposurePoint);
375 
376     /**
377     * @brief Get exposure compensation range.
378     *
379     * @return Returns supported exposure compensation range.
380     */
381     std::vector<float> GetExposureBiasRange();
382 
383     /**
384     * @brief Get exposure compensation range.
385     * @param vector of exposure bias range.
386     * @return errCode.
387     */
388     int32_t GetExposureBiasRange(std::vector<float> &exposureBiasRange);
389 
390     /**
391     * @brief Set exposure compensation value.
392     * @param exposure compensation value to be set.
393     * @return errCode.
394     */
395     int32_t SetExposureBias(float exposureBias);
396 
397     /**
398     * @brief Get exposure compensation value.
399     *
400     * @return Returns current exposure compensation value.
401     */
402     float GetExposureValue();
403 
404     /**
405     * @brief Get exposure compensation value.
406     * @param exposure current exposure compensation value .
407     * @return Returns errCode.
408     */
409     int32_t GetExposureValue(float &exposure);
410 
411     /**
412     * @brief Set the exposure callback.
413     * which will be called when there is exposure state change.
414     *
415     * @param The ExposureCallback pointer.
416     */
417     void SetExposureCallback(std::shared_ptr<ExposureCallback> exposureCallback);
418 
419     /**
420     * @brief This function is called when there is exposure state change
421     * and process the exposure state callback.
422     *
423     * @param result metadata got from callback from service layer.
424     */
425     void ProcessAutoExposureUpdates(const std::shared_ptr<OHOS::Camera::CameraMetadata> &result);
426 
427     /**
428     * @brief Get the supported Focus modes.
429     *
430     * @return Returns vector of FocusMode supported exposure modes.
431     */
432     std::vector<FocusMode> GetSupportedFocusModes();
433 
434     /**
435     * @brief Get the supported Focus modes.
436     * @param vector of FocusMode supported.
437     * @return Returns errCode.
438     */
439     int32_t GetSupportedFocusModes(std::vector<FocusMode> &modes);
440 
441     /**
442     * @brief Query whether given focus mode supported.
443     *
444     * @param camera_focus_mode_enum_t focus mode to query.
445     * @return True is supported false otherwise.
446     */
447     bool IsFocusModeSupported(FocusMode focusMode);
448 
449     /**
450     * @brief Query whether given focus mode supported.
451     *
452     * @param camera_focus_mode_enum_t focus mode to query.
453     * @param bool True is supported false otherwise.
454     * @return Returns errCode.
455     */
456     int32_t IsFocusModeSupported(FocusMode focusMode, bool &isSupported);
457 
458     /**
459     * @brief Set Focus mode.
460     *
461     * @param FocusMode focus mode to be set.
462     * @return Returns errCode.
463     */
464     int32_t SetFocusMode(FocusMode focusMode);
465 
466     /**
467     * @brief Get the current focus mode.
468     *
469     * @return Returns current focus mode.
470     */
471     FocusMode GetFocusMode();
472 
473     /**
474     * @brief Get the current focus mode.
475     * @param FocusMode current focus mode.
476     * @return Returns errCode.
477     */
478     int32_t GetFocusMode(FocusMode &focusMode);
479 
480     /**
481     * @brief Set the focus callback.
482     * which will be called when there is focus state change.
483     *
484     * @param The ExposureCallback pointer.
485     */
486     void SetFocusCallback(std::shared_ptr<FocusCallback> focusCallback);
487 
488     /**
489     * @brief This function is called when there is focus state change
490     * and process the focus state callback.
491     *
492     * @param result metadata got from callback from service layer.
493     */
494     void ProcessAutoFocusUpdates(const std::shared_ptr<OHOS::Camera::CameraMetadata> &result);
495 
496     /**
497     * @brief Set the Focus area.
498     *
499     * @param Point which specifies the area to focus.
500     * @return Returns errCode.
501     */
502     int32_t SetFocusPoint(Point focusPoint);
503 
504     /**
505     * @brief Get centre point of focus area.
506     *
507     * @return Returns current focus point.
508     */
509     Point GetFocusPoint();
510 
511     /**
512     * @brief Get centre point of focus area.
513     * @param Point current focus point.
514     * @return Returns errCode.
515     */
516     int32_t GetFocusPoint(Point &focusPoint);
517 
518     /**
519     * @brief Get focal length.
520     *
521     * @return Returns focal length value.
522     */
523     float GetFocalLength();
524 
525     /**
526     * @brief Get focal length.
527     * @param focalLength current focal length compensation value .
528     * @return Returns errCode.
529     */
530     int32_t GetFocalLength(float &focalLength);
531 
532     /**
533     * @brief Get the supported Focus modes.
534     *
535     * @return Returns vector of camera_focus_mode_enum_t supported exposure modes.
536     */
537     std::vector<FlashMode> GetSupportedFlashModes();
538 
539     /**
540     * @brief Get the supported Focus modes.
541     * @param vector of camera_focus_mode_enum_t supported exposure modes.
542     * @return Returns errCode.
543     */
544     int32_t GetSupportedFlashModes(std::vector<FlashMode> &flashModes);
545 
546     /**
547     * @brief Check whether camera has flash.
548     */
549     bool HasFlash();
550 
551     /**
552     * @brief Check whether camera has flash.
553     * @param bool True is has flash false otherwise.
554     * @return Returns errCode.
555     */
556     int32_t HasFlash(bool &hasFlash);
557 
558     /**
559     * @brief Query whether given flash mode supported.
560     *
561     * @param camera_flash_mode_enum_t flash mode to query.
562     * @return True if supported false otherwise.
563     */
564     bool IsFlashModeSupported(FlashMode flashMode);
565 
566     /**
567     * @brief Query whether given flash mode supported.
568     *
569     * @param camera_flash_mode_enum_t flash mode to query.
570     * @param bool True if supported false otherwise.
571     * @return errCode.
572     */
573     int32_t IsFlashModeSupported(FlashMode flashMode, bool &isSupported);
574 
575     /**
576     * @brief Get the current flash mode.
577     *
578     * @return Returns current flash mode.
579     */
580     FlashMode GetFlashMode();
581 
582     /**
583     * @brief Get the current flash mode.
584     * @param current flash mode.
585     * @return Returns errCode.
586     */
587     int32_t GetFlashMode(FlashMode &flashMode);
588 
589     /**
590     * @brief Set flash mode.
591     *
592     * @param camera_flash_mode_enum_t flash mode to be set.
593     * @return Returns errCode.
594     */
595     int32_t SetFlashMode(FlashMode flashMode);
596 
597     /**
598     * @brief Get the supported Zoom Ratio range.
599     *
600     * @return Returns vector<float> of supported Zoom ratio range.
601     */
602     std::vector<float> GetZoomRatioRange();
603 
604     /**
605     * @brief Get the supported Zoom Ratio range.
606     *
607     * @param vector<float> of supported Zoom ratio range.
608     * @return Returns errCode.
609     */
610     int32_t GetZoomRatioRange(std::vector<float> &zoomRatioRange);
611 
612     /**
613     * @brief Get the current Zoom Ratio.
614     *
615     * @return Returns current Zoom Ratio.
616     */
617     float GetZoomRatio();
618 
619     /**
620     * @brief Get the current Zoom Ratio.
621     * @param zoomRatio current Zoom Ratio.
622     * @return Returns errCode.
623     */
624     int32_t GetZoomRatio(float &zoomRatio);
625 
626     /**
627     * @brief Set Zoom ratio.
628     *
629     * @param Zoom ratio to be set.
630     * @return Returns errCode.
631     */
632     int32_t SetZoomRatio(float zoomRatio);
633 
634     /**
635     * @brief Set Metadata Object types.
636     *
637     * @param set of camera_face_detect_mode_t types.
638     */
639     void SetCaptureMetadataObjectTypes(std::set<camera_face_detect_mode_t> metadataObjectTypes);
640 
641     /**
642      * @brief Get the supported filters.
643      *
644      * @return Returns the array of filter.
645      */
646     std::vector<FilterType> getSupportedFilters();
647 
648     /**
649      * @brief Get the current filter.
650      *
651      * @return Returns the array of filter.
652      */
653     FilterType getFilter();
654 
655     /**
656      * @brief Set the filter.
657      */
658     void setFilter(FilterType filter);
659 
660     /**
661      * @brief Get the supported beauty type.
662      *
663      * @return Returns the array of beautytype.
664      */
665     std::vector<BeautyType> getSupportedBeautyTypes();
666 
667     /**
668      * @brief Get the supported beauty range.
669      *
670      * @return Returns the array of beauty range.
671      */
672     std::vector<int32_t> getSupportedBeautyRange(BeautyType type);
673 
674     /**
675      * @brief Set the beauty.
676      */
677     void setBeauty(BeautyType type, int value);
678     /**
679      * @brief according type to get the strength.
680      */
681     int32_t getBeauty(BeautyType type);
682 
683     /**
684     * @brief Get whether or not commit config.
685     *
686     * @return Returns whether or not commit config.
687     */
688     bool IsSessionCommited();
689 
690     /**
691     * @brief Get whether or not commit config.
692     *
693     * @return Returns whether or not commit config.
694     */
695     bool IsSessionConfiged();
696 private:
697     std::mutex changeMetaMutex_;
698     std::mutex sessionCallbackMutex_;
699     std::shared_ptr<OHOS::Camera::CameraMetadata> changedMetadata_;
700     sptr<ICaptureSession> captureSession_;
701     std::shared_ptr<SessionCallback> appCallback_;
702     sptr<ICaptureSessionCallback> captureSessionCallback_;
703     std::shared_ptr<ExposureCallback> exposureCallback_;
704     std::shared_ptr<FocusCallback> focusCallback_;
705     static const std::unordered_map<camera_focus_state_t, FocusCallback::FocusState> metaToFwFocusState_;
706     static const std::unordered_map<camera_exposure_state_t, ExposureCallback::ExposureState> metaToFwExposureState_;
707     static const std::unordered_map<camera_exposure_mode_enum_t, ExposureMode> metaToFwExposureMode_;
708     static const std::unordered_map<ExposureMode, camera_exposure_mode_enum_t> fwToMetaExposureMode_;
709     static const std::unordered_map<camera_focus_mode_enum_t, FocusMode> metaToFwFocusMode_;
710     static const std::unordered_map<FocusMode, camera_focus_mode_enum_t> fwToMetaFocusMode_;
711     static const std::unordered_map<camera_flash_mode_enum_t, FlashMode> metaToFwFlashMode_;
712     static const std::unordered_map<FlashMode, camera_flash_mode_enum_t> fwToMetaFlashMode_;
713     static const std::unordered_map<CameraVideoStabilizationMode, VideoStabilizationMode> metaToFwVideoStabModes_;
714     static const std::unordered_map<VideoStabilizationMode, CameraVideoStabilizationMode> fwToMetaVideoStabModes_;
715 
716     int32_t UpdateSetting(std::shared_ptr<OHOS::Camera::CameraMetadata> changedMetadata);
717     void SetFrameRateRange(const std::vector<int32_t>& frameRateRange);
718     Point CoordinateTransform(Point point);
719     int32_t CalculateExposureValue(float exposureValue);
720     Point VerifyFocusCorrectness(Point point);
721 };
722 } // namespace CameraStandard
723 } // namespace OHOS
724 #endif // OHOS_CAMERA_CAPTURE_SESSION_H
725