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_CAMERA_INPUT_H
17 #define OHOS_CAMERA_CAMERA_INPUT_H
18 
19 #include <iostream>
20 #include <memory>
21 #include <set>
22 #include <unordered_map>
23 #include <vector>
24 
25 #include "camera_death_recipient.h"
26 #include "camera_device.h"
27 #include "camera_device_ability_items.h"
28 #include "camera_info.h"
29 #include "capture_input.h"
30 #include "hcamera_device_callback_stub.h"
31 #include "icamera_device_service.h"
32 #include "icamera_device_service_callback.h"
33 #include "metadata_common_utils.h"
34 #include "output/camera_output_capability.h"
35 namespace OHOS {
36 namespace CameraStandard {
37 class ErrorCallback {
38 public:
39     ErrorCallback() = default;
40     virtual ~ErrorCallback() = default;
41     virtual void OnError(const int32_t errorType, const int32_t errorMsg) const = 0;
42 };
43 
44 class CameraOcclusionDetectCallback {
45 public:
46     CameraOcclusionDetectCallback() = default;
47     virtual ~CameraOcclusionDetectCallback() = default;
48     virtual void OnCameraOcclusionDetected(const uint8_t isCameraOcclusion, const uint8_t isCameraLensDirty) const = 0;
49 };
50 
51 class ResultCallback {
52 public:
53     ResultCallback() = default;
54     virtual ~ResultCallback() = default;
55     virtual void OnResult(const uint64_t timestamp,
56         const std::shared_ptr<OHOS::Camera::CameraMetadata> &result) const = 0;
57 };
58 
59 class CameraInput : public CaptureInput {
60 public:
61     [[deprecated]] explicit CameraInput(sptr<ICameraDeviceService>& deviceObj, sptr<CameraInfo>& camera);
62     explicit CameraInput(sptr<ICameraDeviceService>& deviceObj, sptr<CameraDevice>& camera);
63     virtual ~CameraInput();
64 
65     /**
66     * @brief open camera.
67     */
68     int Open() override;
69 
70     /**
71     * @brief open secure camera.
72     */
73     int Open(bool isEnableSecureCamera, uint64_t* secureSeqId) override;
74 
75     /**
76     * @brief close camera.
77     */
78     int Close() override;
79 
80     /**
81     * @brief create new device control setting.
82     */
83     [[deprecated]] void LockForControl();
84 
85     /**
86     * @brief submit device control setting.
87     *
88     * @return Returns CAMERA_OK is success.
89     */
90     [[deprecated]] int32_t UnlockForControl();
91 
92     /**
93     * @brief Get the supported format for photo.
94     *
95     * @return Returns vector of camera_format_t supported format for photo.
96     */
97     [[deprecated]] std::vector<camera_format_t> GetSupportedPhotoFormats();
98 
99     /**
100     * @brief Get the supported format for video.
101     *
102     * @return Returns vector of camera_format_t supported format for video.
103     */
104     [[deprecated]] std::vector<camera_format_t> GetSupportedVideoFormats();
105 
106     /**
107     * @brief Get the supported format for preview.
108     *
109     * @return Returns vector of camera_format_t supported format for preview.
110     */
111     [[deprecated]] std::vector<camera_format_t> GetSupportedPreviewFormats();
112 
113     /**
114     * @brief Get the supported exposure modes.
115     *
116     * @return Returns vector of camera_exposure_mode_enum_t supported exposure modes.
117     */
118     [[deprecated]] std::vector<camera_exposure_mode_enum_t> GetSupportedExposureModes();
119 
120     /**
121     * @brief Query whether given exposure mode supported.
122     *
123     * @param camera_exposure_mode_enum_t exposure mode to query.
124     * @return True is supported false otherwise.
125     */
126     [[deprecated]] bool IsExposureModeSupported(camera_exposure_mode_enum_t exposureMode);
127 
128     /**
129     * @brief Set exposure mode.
130     *
131     * @param camera_exposure_mode_enum_t exposure mode to be set.
132     */
133     [[deprecated]] void SetExposureMode(camera_exposure_mode_enum_t exposureMode);
134 
135     /**
136     * @brief Get the current exposure mode.
137     *
138     * @return Returns current exposure mode.
139     */
140     [[deprecated]] camera_exposure_mode_enum_t GetExposureMode();
141 
142     /**
143     * @brief Get the supported Focus modes.
144     *
145     * @return Returns vector of camera_focus_mode_enum_t supported exposure modes.
146     */
147     [[deprecated]] std::vector<camera_focus_mode_enum_t> GetSupportedFocusModes();
148 
149     /**
150     * @brief Get exposure compensation range.
151     *
152     * @return Returns supported exposure compensation range.
153     */
154     [[deprecated]] std::vector<float> GetExposureBiasRange();
155 
156     /**
157     * @brief Set exposure compensation value.
158     *
159     * @param exposure compensation value to be set.
160     */
161     [[deprecated]] void SetExposureBias(float exposureBias);
162 
163     /**
164     * @brief Get exposure compensation value.
165     *
166     * @return Returns current exposure compensation value.
167     */
168     [[deprecated]] float GetExposureValue();
169 
170     /**
171     * @brief Query whether given focus mode supported.
172     *
173     * @param camera_focus_mode_enum_t focus mode to query.
174     * @return True is supported false otherwise.
175     */
176     [[deprecated]] bool IsFocusModeSupported(camera_focus_mode_enum_t focusMode);
177 
178     /**
179     * @brief Set Focus mode.
180     *
181     * @param camera_focus_mode_enum_t focus mode to be set.
182     */
183     [[deprecated]] void SetFocusMode(camera_focus_mode_enum_t focusMode);
184 
185     /**
186     * @brief Get the current focus mode.
187     *
188     * @return Returns current focus mode.
189     */
190     [[deprecated]] camera_focus_mode_enum_t GetFocusMode();
191 
192     /**
193     * @brief Get focal length.
194     *
195     * @return Returns focal length value.
196     */
197     [[deprecated]] float GetFocalLength();
198 
199     /**
200     * @brief Get the supported Zoom Ratio range.
201     *
202     * @return Returns vector<float> of supported Zoom ratio range.
203     */
204     [[deprecated]] std::vector<float> GetSupportedZoomRatioRange();
205 
206     /**
207     * @brief Get the current Zoom Ratio.
208     *
209     * @return Returns current Zoom Ratio.
210     */
211     [[deprecated]] float GetZoomRatio();
212 
213     /**
214     * @brief Set Zoom ratio.
215     *
216     * @param Zoom ratio to be set.
217     */
218     [[deprecated]] void SetZoomRatio(float zoomRatio);
219 
220     /**
221     * @brief Get the supported Focus modes.
222     *
223     * @return Returns vector of camera_focus_mode_enum_t supported exposure modes.
224     */
225     [[deprecated]] std::vector<camera_flash_mode_enum_t> GetSupportedFlashModes();
226 
227     /**
228     * @brief Get the current flash mode.
229     *
230     * @return Returns current flash mode.
231     */
232     [[deprecated]] camera_flash_mode_enum_t GetFlashMode();
233 
234     /**
235     * @brief Set flash mode.
236     *
237     * @param camera_flash_mode_enum_t flash mode to be set.
238     */
239     [[deprecated]] void SetFlashMode(camera_flash_mode_enum_t flashMode);
240 
241     /**
242     * @brief Set the error callback.
243     * which will be called when error occurs.
244     *
245     * @param The ErrorCallback pointer.
246     */
247     void SetErrorCallback(std::shared_ptr<ErrorCallback> errorCallback);
248 
249     /**
250     * @brief Set the result callback.
251     * which will be called when result callback.
252     *
253     * @param The ResultCallback pointer.
254     */
255     void SetResultCallback(std::shared_ptr<ResultCallback> resultCallback);
256 
257     /**
258     * @brief Set the CameraOcclusionDetect callback.
259     * which will be called when CameraOcclusionDetect callback .
260     *
261     * @param The CameraOcclusionDetect pointer.
262     */
263     void SetOcclusionDetectCallback(std::shared_ptr<CameraOcclusionDetectCallback> cameraOcclusionDetectCallback);
264 
265     /**
266     * @brief Release camera input.
267     */
268     int Release() override;
269 
270     /**
271     * @brief Get the camera Id.
272     *
273     * @return Returns the camera Id.
274     */
275     std::string GetCameraId();
276 
277     /**
278     * @brief Get Camera Device.
279     *
280     * @return Returns Camera Device pointer.
281     */
282     sptr<ICameraDeviceService> GetCameraDevice();
283 
284     /**
285     * @brief Set Camera Device pointer.
286     *
287     */
288     void SetCameraDevice(sptr<ICameraDeviceService> deviceObj);
289 
290     /**
291     * @brief Get ErrorCallback pointer.
292     *
293     * @return Returns ErrorCallback pointer.
294     */
295     std::shared_ptr<ErrorCallback> GetErrorCallback();
296 
297     /**
298     * @brief Get ResultCallback pointer.
299     *
300     * @return Returns ResultCallback pointer.
301     */
302     std::shared_ptr<ResultCallback>  GetResultCallback();
303 
304     /**
305     * @brief Get CameraOcclusionDetectCallback pointer.
306     *
307     * @return Returns CameraOcclusionDetectCallback pointer.
308     */
309     std::shared_ptr<CameraOcclusionDetectCallback>  GetOcclusionDetectCallback();
310 
311     /**
312     * @brief get the camera info associated with the device.
313     *
314     * @return Returns camera info.
315     */
316     void SetCameraDeviceInfo(sptr<CameraDevice> CameraObj);
317 
318     /**
319     * @brief set the camera used as position with the device.
320     *
321     * @return Returns camera info.
322     */
323     void SetInputUsedAsPosition(CameraPosition usedAsPosition);
324 
325     /**
326     * @brief set the cameraObj.
327     */
328     sptr<CameraDevice> GetCameraDeviceInfo() override;
329 
330     /**
331     * @brief This function is called when there is device setting state change
332     * and process the state callback.
333     *
334     * @param result metadata got from callback from service layer.
335     */
336     void ProcessCallbackUpdates(const uint64_t timestamp, const std::shared_ptr<OHOS::Camera::CameraMetadata> &result);
337 
338     /**
339     * @brief This function is called when there is focus state change
340     * and process the focus state callback.
341     *
342     * @param result metadata got from callback from service layer.
343     */
344     [[deprecated]] void ProcessAutoFocusUpdates(const std::shared_ptr<OHOS::Camera::CameraMetadata> &result);
345 
346     /**
347     * @brief This function is called when there is exposure state change
348     * and process the exposure state callback.
349     *
350     * @param result metadata got from callback from service layer.
351     */
352     [[deprecated]] void ProcessAutoExposureUpdates(const std::shared_ptr<OHOS::Camera::CameraMetadata> &result);
353 
354     /**
355     * @brief Get current Camera Settings.
356     *
357     * @return Returns string encoded metadata setting.
358     */
359     std::string GetCameraSettings();
360 
361     /**
362     * @brief set the camera metadata setting.
363     *
364     * @param string encoded camera metadata setting.
365     * @return Returns 0 if success or appropriate error code if failed.
366     */
367     int32_t SetCameraSettings(std::string setting);
368 
369     std::shared_ptr<camera_metadata_item_t> GetMetaSetting(uint32_t metaTag);
370 
371     int32_t GetCameraAllVendorTags(std::vector<vendorTag_t> &infos);
372 
373     bool MergeMetadata(const std::shared_ptr<OHOS::Camera::CameraMetadata> srcMetadata,
374         std::shared_ptr<OHOS::Camera::CameraMetadata> dstMetadata);
375     void SwitchCameraDevice(sptr<ICameraDeviceService> &deviceObj, sptr<CameraDevice> &cameraObj);
376     void InitCameraInput();
377 private:
378     std::mutex deviceObjMutex_;
379     std::mutex errorCallbackMutex_;
380     std::mutex cameraDeviceInfoMutex_;
381     std::mutex resultCallbackMutex_;
382     std::mutex occlusionCallbackMutex_;
383     sptr<ICameraDeviceService> deviceObj_;
384     sptr<CameraDevice> cameraObj_;
385     std::shared_ptr<ResultCallback> resultCallback_;
386     std::shared_ptr<ErrorCallback> errorCallback_;
387     std::shared_ptr<CameraOcclusionDetectCallback> cameraOcclusionDetectCallback_;
388     sptr<ICameraDeviceServiceCallback> CameraDeviceSvcCallback_;
389     std::mutex interfaceMutex_;
390     sptr<CameraDeathRecipient> deathRecipient_ = nullptr;
391     void CameraServerDied(pid_t pid);
392     int32_t UpdateSetting(std::shared_ptr<OHOS::Camera::CameraMetadata> changedMetadata);
393     void InputRemoveDeathRecipient();
394     std::map<CameraPosition, camera_position_enum> positionMapping;
395 };
396 
397 class CameraDeviceServiceCallback : public HCameraDeviceCallbackStub {
398 public:
399     std::mutex deviceCallbackMutex_;
400     wptr<CameraInput> camInput_ = nullptr;
CameraDeviceServiceCallback()401     CameraDeviceServiceCallback() : camInput_(nullptr) {
402     }
403 
CameraDeviceServiceCallback(CameraInput * camInput)404     explicit CameraDeviceServiceCallback(CameraInput* camInput) : camInput_(camInput) {
405     }
406 
~CameraDeviceServiceCallback()407     ~CameraDeviceServiceCallback()
408     {
409         std::lock_guard<std::mutex> lock(deviceCallbackMutex_);
410         camInput_ = nullptr;
411     }
412 
413     int32_t OnError(const int32_t errorType, const int32_t errorMsg) override;
414 
415     int32_t OnResult(const uint64_t timestamp, const std::shared_ptr<OHOS::Camera::CameraMetadata> &result) override;
416 };
417 } // namespace CameraStandard
418 } // namespace OHOS
419 #endif // OHOS_CAMERA_CAMERA_INPUT_H
420 
421