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_PROFESSION_SESSION_H
17 #define OHOS_CAMERA_PROFESSION_SESSION_H
18 
19 #include <atomic>
20 #include <iostream>
21 #include <set>
22 #include <stdint.h>
23 #include <vector>
24 #include "camera_device.h"
25 #include "camera_error_code.h"
26 #include "input/capture_input.h"
27 #include "output/capture_output.h"
28 #include "icamera_util.h"
29 #include "icapture_session.h"
30 #include "icapture_session_callback.h"
31 #include "capture_session.h"
32 
33 namespace OHOS {
34 namespace CameraStandard {
35 class ExposureInfoCallback;
36 class IsoInfoCallback;
37 class ApertureInfoCallback;
38 class LuminationInfoCallback;
39 typedef enum {
40     METERING_MODE_REGION = 0,
41     METERING_MODE_CENTER_WEIGHTED,
42     METERING_MODE_SPOT,
43     METERING_MODE_OVERALL,
44 } MeteringMode;
45 
46 typedef enum {
47     FOCUS_ASSIST_FLASH_MODE_OFF = 0,
48     FOCUS_ASSIST_FLASH_MODE_ON,
49     FOCUS_ASSIST_FLASH_MODE_DEFAULT,
50     FOCUS_ASSIST_FLASH_MODE_AUTO,
51 } FocusAssistFlashMode;
52 
53 typedef enum {
54     EXPOSURE_HINT_MODE_OFF = 0,
55     EXPOSURE_HINT_MODE_ON,
56     EXPOSURE_HINT_UNSUPPORTED,
57 } ExposureHintMode;
58 
59 typedef enum {
60     OHOS_CAMERA_EXPOSURE_HINT_UNSUPPORTED = 0,
61     OHOS_CAMERA_EXPOSURE_HINT_MODE_ON,
62     OHOS_CAMERA_EXPOSURE_HINT_MODE_OFF,
63 } camera_exposure_hint_mode_enum_t;
64 
65 class ProfessionSession : public CaptureSession {
66 public:
67     class ProfessionSessionMetadataResultProcessor : public MetadataResultProcessor {
68     public:
ProfessionSessionMetadataResultProcessor(wptr<ProfessionSession> session)69         explicit ProfessionSessionMetadataResultProcessor(wptr<ProfessionSession> session) : session_(session) {}
70         void ProcessCallbacks(
71             const uint64_t timestamp, const std::shared_ptr<OHOS::Camera::CameraMetadata>& result) override;
72 
73     private:
74         wptr<ProfessionSession> session_;
75     };
76 
ProfessionSession(sptr<ICaptureSession> & session,std::vector<sptr<CameraDevice>> devices)77     explicit ProfessionSession(sptr<ICaptureSession>& session,
78         std::vector<sptr<CameraDevice>> devices) : CaptureSession(session)
79     {
80         metadataResultProcessor_ = std::make_shared<ProfessionSessionMetadataResultProcessor>(this);
81         supportedDevices_.resize(devices.size());
82         std::copy(devices.begin(), devices.end(), supportedDevices_.begin());
83     }
84 
85     ~ProfessionSession();
86 // Metering mode
87     /**
88      * @brief Get Metering mode.
89      * @param vector of Metering Mode.
90      * @return errCode.
91      */
92     int32_t GetSupportedMeteringModes(std::vector<MeteringMode>& meteringModes);
93 
94     /**
95      * @brief Query whether given meteringMode mode supported.
96      *
97      * @param camera_meter_mode_t flash mode to query.
98      * @param bool True if supported false otherwise.
99      * @return errCode.
100      */
101     int32_t IsMeteringModeSupported(MeteringMode meteringMode, bool &isSupported);
102 
103     /**
104      * @brief Set Metering Mode.
105      * @param exposure MeteringMode to be set.
106      * @return errCode.
107      */
108     int32_t SetMeteringMode(MeteringMode mode);
109 
110     /**
111      * @brief Get MeteringMode.
112      * @param exposure current MeteringMode .
113      * @return Returns errCode.
114      */
115     int32_t GetMeteringMode(MeteringMode& mode);
116 
117 // ISO
118     /**
119      * @brief Get the supported iso.
120      *
121      * @return Returns the array of iso.
122      */
123     int32_t GetIsoRange(std::vector<int32_t>& isoRange);
124 
125     /**
126      * @brief Get the iso.
127      *
128      * @return Returns the value of iso.
129      */
130     int32_t GetISO(int32_t &iso);
131 
132     /**
133      * @brief Set the iso.
134      */
135     int32_t SetISO(int32_t iso);
136 
137     /**
138      * @brief Check is support manual iso.
139      */
140     bool IsManualIsoSupported();
141 
142 // Focus mode
143     /**
144      * @brief Get Metering mode.
145      * @param vector of Metering Mode.
146      * @return errCode.
147      */
148     int32_t GetSupportedFocusModes(std::vector<FocusMode>& modes);
149 
150     /**
151      * @brief Query whether given focus mode supported.
152      *
153      * @param camera_focus_mode_enum_t focus mode to query.
154      * @param bool True if supported false otherwise.
155      * @return errCode.
156      */
157     int32_t IsFocusModeSupported(FocusMode focusMode, bool &isSupported);
158 
159     /**
160      * @brief Set Metering Mode.
161      * @param exposure MeteringMode to be set.
162      * @return errCode.
163      */
164     int32_t SetFocusMode(FocusMode mode);
165 
166     /**
167      * @brief Get MeteringMode.
168      * @param exposure current MeteringMode .
169      * @return Returns errCode.
170      */
171     int32_t GetFocusMode(FocusMode& mode);
172 
173 // ExposureHint mode
174     /**
175      * @brief Get ExposureHint mode.
176      * @param vector of ExposureHint Mode.
177      * @return errCode.
178      */
179     int32_t GetSupportedExposureHintModes(std::vector<ExposureHintMode>& modes);
180 
181     /**
182      * @brief Set ExposureHint Mode.
183      * @param mode ExposureHint Mode to be set.
184      * @return errCode.
185      */
186     int32_t SetExposureHintMode(ExposureHintMode mode);
187 
188     /**
189      * @brief Get MeteringMode.
190      * @param mode current MeteringMode .
191      * @return Returns errCode.
192      */
193     int32_t GetExposureHintMode(ExposureHintMode& mode);
194 
195 // FocusAssistFlash mode
196     /**
197      * @brief Get FocusAssistFlash mode.
198      * @param vector of FocusAssistFlash Mode.
199      * @return errCode.
200      */
201     int32_t GetSupportedFocusAssistFlashModes(std::vector<FocusAssistFlashMode>& modes);
202 
203     /**
204      * @brief Query whether given focus assist flash mode supported.
205      *
206      * @param FocusAssistFlashMode focus assist flash mode to query.
207      * @param bool True if supported false otherwise.
208      * @return errCode.
209      */
210     int32_t IsFocusAssistFlashModeSupported(FocusAssistFlashMode mode, bool &isSupported);
211 
212     /**
213      * @brief Set FocusAssistFlashMode.
214      * @param mode FocusAssistFlash Mode to be set.
215      * @return errCode.
216      */
217     int32_t SetFocusAssistFlashMode(FocusAssistFlashMode mode);
218 
219     /**
220      * @brief Get FocusAssistFlash Mode.
221      * @param mode current FocusAssistFlash Mode .
222      * @return Returns errCode.
223      */
224     int32_t GetFocusAssistFlashMode(FocusAssistFlashMode& mode);
225 
226 // Flash Mode
227     /**
228      * @brief Get the supported Focus modes.
229      * @param vector of camera_focus_mode_enum_t supported exposure modes.
230      * @return Returns errCode.
231      */
232     int32_t GetSupportedFlashModes(std::vector<FlashMode>& flashModes) override;
233 
234     /**
235      * @brief Check whether camera has flash.
236      * @param bool True is has flash false otherwise.
237      * @return Returns errCode.
238      */
239     int32_t HasFlash(bool& hasFlash);
240 
241     /**
242      * @brief Query whether given flash mode supported.
243      *
244      * @param camera_flash_mode_enum_t flash mode to query.
245      * @param bool True if supported false otherwise.
246      * @return errCode.
247      */
248     int32_t IsFlashModeSupported(FlashMode flashMode, bool& isSupported);
249 
250     /**
251      * @brief Get the current flash mode.
252      * @param current flash mode.
253      * @return Returns errCode.
254      */
255     int32_t GetFlashMode(FlashMode& flashMode) override;
256 
257     /**
258      * @brief Set flash mode.
259      *
260      * @param camera_flash_mode_enum_t flash mode to be set.
261      * @return Returns errCode.
262      */
263     int32_t SetFlashMode(FlashMode flashMode) override;
264 
265 // XMAGE
266     /**
267      * @brief Get the supported color effect.
268      *
269      * @return Returns supported color effects.
270      */
271     int32_t GetSupportedColorEffects(std::vector<ColorEffect>& colorEffects);
272 
273     /**
274      * @brief Get the current color effect.
275      *
276      * @return Returns current color effect.
277      */
278     int32_t GetColorEffect(ColorEffect& colorEffect);
279 
280     /**
281      * @brief Set the color effect.
282      */
283     int32_t SetColorEffect(ColorEffect colorEffect);
284 
285 // SensorExposureTime Callback
286     /**
287      * @brief Set the SensorExposureTime callback.
288      * which will be called when there is SensorExposureTime change.
289      *
290      * @param The ExposureInfoCallback pointer.
291      */
292     void SetExposureInfoCallback(std::shared_ptr<ExposureInfoCallback> callback);
293 // Focus Callback
294     /**
295      * @brief Set the ISO callback.
296      * which will be called when there is ISO state change.
297      *
298      * @param The IsoInfoCallback pointer.
299      */
300     void SetIsoInfoCallback(std::shared_ptr<IsoInfoCallback> callback);
301 // Exposure Callback
302     /**
303      * @brief Set the focus distance callback.
304      * which will be called when there is focus distance change.
305      *
306      * @param The ApertureInfoCallback pointer.
307      */
308     void SetApertureInfoCallback(std::shared_ptr<ApertureInfoCallback> callback);
309 // Exposurehint Callback
310     /**
311      * @brief Set the exposure hint callback.
312      * which will be called when there is exposure hint change.
313      *
314      * @param The LuminationInfoCallback pointer.
315      */
316     void SetLuminationInfoCallback(std::shared_ptr<LuminationInfoCallback> callback);
317 
318     /**
319      * @brief This function is called when there is SensorExposureTime change
320      * and process the SensorExposureTime callback.
321      *
322      * @param result Metadata got from callback from service layer.
323      */
324     void ProcessSensorExposureTimeChange(const std::shared_ptr<OHOS::Camera::CameraMetadata>& result);
325 
326     /**
327      * @brief This function is called when there is Iso change
328      * and process the Iso callback.
329      *
330      * @param result Metadata got from callback from service layer.
331      */
332     void ProcessIsoChange(const std::shared_ptr<OHOS::Camera::CameraMetadata>& result);
333 
334     /**
335      * @brief This function is called when there is Aperture change
336      * and process the Aperture callback.
337      *
338      * @param result Metadata got from callback from service layer.
339      */
340     void ProcessApertureChange(const std::shared_ptr<OHOS::Camera::CameraMetadata> &result);
341 
342     /**
343      * @brief This function is called when there is Lumination change
344      * and process the Lumination callback.
345      *
346      * @param result Metadata got from callback from service layer.
347      */
348     void ProcessLuminationChange(const std::shared_ptr<OHOS::Camera::CameraMetadata> &result);
349 
350     /**
351      * @brief This function is called when physical camera switch
352      * and process the ability change callback.
353      *
354      * @param result Metadata got from callback from service layer.
355      */
356     void ProcessPhysicalCameraSwitch(const std::shared_ptr<OHOS::Camera::CameraMetadata>& result);
357 
358     std::shared_ptr<OHOS::Camera::CameraMetadata> GetMetadata() override;
359 
360     /**
361      * @brief Determine if the given Ouput can be added to session.
362      *
363      * @param output to be added to session.
364      */
365     bool CanAddOutput(sptr<CaptureOutput>& output) override;
366 protected:
367     static const std::unordered_map<camera_meter_mode_t, MeteringMode> metaMeteringModeMap_;
368     static const std::unordered_map<MeteringMode, camera_meter_mode_t> fwkMeteringModeMap_;
369 
370     static const std::unordered_map<camera_focus_assist_flash_mode_enum_t, FocusAssistFlashMode>
371         metaFocusAssistFlashModeMap_;
372     static const std::unordered_map<FocusAssistFlashMode, camera_focus_assist_flash_mode_enum_t>
373         fwkFocusAssistFlashModeMap_;
374 
375     static const std::unordered_map<camera_exposure_hint_mode_enum_t, ExposureHintMode> metaExposureHintModeMap_;
376     static const std::unordered_map<ExposureHintMode, camera_exposure_hint_mode_enum_t> fwkExposureHintModeMap_;
377 private:
378     std::vector<float> ParsePhysicalApertureRangeFromMeta(const camera_metadata_item_t &item);
379     std::mutex sessionCallbackMutex_;
380     std::shared_ptr<ExposureInfoCallback> exposureInfoCallback_ = nullptr;
381     std::shared_ptr<IsoInfoCallback> isoInfoCallback_ = nullptr;
382     std::shared_ptr<ApertureInfoCallback> apertureInfoCallback_ = nullptr;
383     std::shared_ptr<LuminationInfoCallback> luminationInfoCallback_ = nullptr;
384     std::atomic<uint8_t> physicalCameraId_ = 0;
385     uint32_t isoValue_ = 0;
386     float luminationValue_ = 0.0;
387     std::vector<sptr<CameraDevice> > supportedDevices_;
388 };
389 
390 typedef struct {
391     uint32_t exposureDurationValue;
392 } ExposureInfo;
393 
394 typedef struct {
395     uint32_t isoValue;
396 } IsoInfo;
397 
398 typedef struct {
399     float apertureValue;
400 } ApertureInfo;
401 
402 typedef struct {
403     float luminationValue;
404 } LuminationInfo;
405 
406 class ExposureInfoCallback {
407 public:
408     ExposureInfoCallback() = default;
409     virtual ~ExposureInfoCallback() = default;
410     virtual void OnExposureInfoChanged(ExposureInfo info) = 0;
411 };
412 
413 class IsoInfoCallback {
414 public:
415     IsoInfoCallback() = default;
416     virtual ~IsoInfoCallback() = default;
417     virtual void OnIsoInfoChanged(IsoInfo info) = 0;
418 };
419 
420 class ApertureInfoCallback {
421 public:
422     ApertureInfoCallback() = default;
423     virtual ~ApertureInfoCallback() = default;
424     virtual void OnApertureInfoChanged(ApertureInfo info) = 0;
425 };
426 
427 class LuminationInfoCallback {
428 public:
429     LuminationInfoCallback() = default;
430     virtual ~LuminationInfoCallback() = default;
431     virtual void OnLuminationInfoChanged(LuminationInfo info) = 0;
432 };
433 } // namespace CameraStandard
434 } // namespace OHOS
435 #endif // OHOS_CAMERA_PROFESSION_SESSION_H
436