1 /*
2  * Copyright (c) 2022 - 2023 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 DISTRIBUTED_DCAMERA_HDF_DEMO_H
17 #define DISTRIBUTED_DCAMERA_HDF_DEMO_H
18 
19 #include <vector>
20 #include <map>
21 #include <iostream>
22 #include <hdf_log.h>
23 #include <surface.h>
24 #include <sys/time.h>
25 #include <ctime>
26 #include <fcntl.h>
27 
28 #include "constants.h"
29 #include "camera_metadata_operator.h"
30 #include "v1_0/display_composer_type.h"
31 #include "v1_0/icamera_device.h"
32 #include "v1_0/icamera_host.h"
33 #include "v1_0/ioffline_stream_operator.h"
34 #include "v1_0/istream_operator.h"
35 #include "v1_0/types.h"
36 #include "v1_0/istream_operator_callback.h"
37 #include "metadata_utils.h"
38 #include "stream_customer.h"
39 #include "securec.h"
40 
41 #include "if_system_ability_manager.h"
42 #include "iservice_registry.h"
43 
44 namespace OHOS {
45 namespace DistributedHardware {
46 using namespace OHOS::Camera;
47 using namespace OHOS::HDI::Camera::V1_0;
48 using namespace OHOS::HDI::Display::Composer::V1_0;
49 using CameraAbility = OHOS::Camera::CameraMetadata;
50 using CameraSetting = OHOS::Camera::CameraMetadata;
51 
52 #define CAMERA_PREVIEW_WIDTH 640
53 #define CAMERA_PREVIEW_HEIGHT 480
54 #define CAMERA_CAPTURE_WIDTH 640
55 #define CAMERA_CAPTURE_HEIGHT 480
56 #define CAMERA_VIDEO_WIDTH 640
57 #define CAMERA_VIDEO_HEIGHT 480
58 #define CAMERA_CAPTURE_ENCODE_TYPE OHOS::HDI::Camera::V1_0::ENCODE_TYPE_JPEG
59 #define CAMERA_VIDEO_ENCODE_TYPE OHOS::HDI::Camera::V1_0::ENCODE_TYPE_H264
60 
61 #ifdef DCAMERA_DRIVER_YUV
62         #define CAMERA_FORMAT PIXEL_FMT_YCRCB_420_SP
63 #else
64         #define CAMERA_FORMAT PIXEL_FMT_RGBA_8888
65 #endif
66 
67 enum DemoActionID {
68     STREAM_ID_PREVIEW = 1001,
69     STREAM_ID_CAPTURE,
70     STREAM_ID_VIDEO,
71     CAPTURE_ID_PREVIEW = 2001,
72     CAPTURE_ID_CAPTURE,
73     CAPTURE_ID_VIDEO,
74 };
75 
76 typedef enum CameraAwbMode {
77     OHOS_CAMERA_AWB_MODE_OFF,
78     OHOS_CAMERA_AWB_MODE_AUTO,
79     OHOS_CAMERA_AWB_MODE_INCANDESCENT,
80     OHOS_CAMERA_AWB_MODE_FLUORESCENT,
81     OHOS_CAMERA_AWB_MODE_WARM_FLUORESCENT,
82     OHOS_CAMERA_AWB_MODE_DAYLIGHT,
83     OHOS_CAMERA_AWB_MODE_CLOUDY_DAYLIGHT,
84     OHOS_CAMERA_AWB_MODE_TWILIGHT,
85     OHOS_CAMERA_AWB_MODE_SHADE,
86 } CameraAwbMode;
87 
88 class DemoCameraHostCallback;
89 class DcameraHdfDemo {
90 public:
91     DcameraHdfDemo();
92     ~DcameraHdfDemo();
93 
94     RetCode InitCameraDevice();
95     void ReleaseCameraDevice();
96     RetCode InitSensors();
97 
98     RetCode StartPreviewStream();
99     RetCode StartCaptureStream();
100     RetCode StartVideoStream();
101     RetCode StartDualStreams(const int streamIdSecond);
102     RetCode CreateStream();
103     RetCode ReleaseAllStream();
104 
105     RetCode CaptureOnDualStreams(const int streamIdSecond);
106     RetCode CaptureON(const int streamId, const int captureId, CaptureMode mode);
107     RetCode CaptureOff(const int captureId, const CaptureMode mode);
108 
109     void SetAwbMode(const int mode) const;
110     void SetAeExpo();
111     void SetMetadata();
112     void SetEnableResult();
113     void FlashlightOnOff(bool onOff);
114 
115     RetCode StreamOffline(const int streamId);
116 
117     void QuitDemo();
118 
119 private:
120     void SetStreamInfo(StreamInfo& streamInfo,
121         const std::shared_ptr<StreamCustomer>& streamCustomer,
122         const int streamId, const StreamIntent intent);
123     void GetStreamOpt();
124 
125     RetCode CreateStreams(const int streamIdSecond, StreamIntent intent);
126 
127     void StoreImage(const void *bufStart, const uint32_t size) const;
128     void StoreVideo(const void *bufStart, const uint32_t size) const;
129     void OpenVideoFile();
130 
131     RetCode GetFaceDetectMode(std::shared_ptr<CameraAbility> &ability);
132     RetCode GetFocalLength(std::shared_ptr<CameraAbility> &ability);
133     RetCode GetAvailableFocusModes(std::shared_ptr<CameraAbility> &ability);
134     RetCode GetAvailableExposureModes(std::shared_ptr<CameraAbility> &ability);
135     RetCode GetExposureCompensationRange(std::shared_ptr<CameraAbility> &ability);
136     RetCode GetExposureCompensationSteps(std::shared_ptr<CameraAbility> &ability);
137     RetCode GetAvailableMeterModes(std::shared_ptr<CameraAbility> &ability);
138     RetCode GetAvailableFlashModes(std::shared_ptr<CameraAbility> &ability);
139     RetCode GetMirrorSupported(std::shared_ptr<CameraAbility> &ability);
140     RetCode GetStreamBasicConfigurations(std::shared_ptr<CameraAbility> &ability);
141     RetCode GetFpsRange(std::shared_ptr<CameraAbility> &ability);
142     RetCode GetCameraPosition(std::shared_ptr<CameraAbility> &ability);
143     RetCode GetCameraType(std::shared_ptr<CameraAbility> &ability);
144     RetCode GetCameraConnectionType(std::shared_ptr<CameraAbility> &ability);
145     RetCode GetFaceDetectMaxNum(std::shared_ptr<CameraAbility> &ability);
146     RetCode CreateStreamInfo(const int streamId, std::shared_ptr<StreamCustomer> &streamCustomer,
147         StreamIntent intent);
148 
149     int aeStatus_ = 1;
150     int videoFd_ = -1;
151     unsigned int isPreviewOn_ = 0;
152     unsigned int isCaptureOn_ = 0;
153     unsigned int isVideoOn_ = 0;
154 
155     uint8_t captureQuality_ = 0;
156     int32_t captureOrientation_ = 0;
157     uint8_t mirrorSwitch_ = 0;
158     std::vector<double> gps_;
159     CaptureInfo captureInfo_;
160 
161     std::shared_ptr<StreamCustomer> streamCustomerPreview_ = nullptr;
162     std::shared_ptr<StreamCustomer> streamCustomerCapture_ = nullptr;
163     std::shared_ptr<StreamCustomer> streamCustomerVideo_ = nullptr;
164     std::shared_ptr<CameraAbility> ability_ = nullptr;
165     std::shared_ptr<CameraSetting> captureSetting_ = nullptr;
166     std::mutex metaDatalock_;
167     std::vector<uint8_t> cameraAbility_;
168     std::vector<StreamInfo> streamInfos_;
169     std::vector<int> streamIds_;
170 
171     OHOS::sptr<DemoCameraHostCallback> hostCallback_ = nullptr;
172     OHOS::sptr<IStreamOperator> streamOperator_ = nullptr;
173     OHOS::sptr<ICameraHost> demoCameraHost_ = nullptr;
174     OHOS::sptr<ICameraDevice> demoCameraDevice_ = nullptr;
175 
176     std::vector<std::string> cameraIds_ = {};
177     friend class StreamCustomer;
178 };
179 
180 class DemoCameraDeviceCallback : public ICameraDeviceCallback {
181 public:
182     DemoCameraDeviceCallback() = default;
183     virtual ~DemoCameraDeviceCallback() = default;
184     int32_t OnError(ErrorType type, int32_t errorCode) override;
185     int32_t OnResult(uint64_t timestamp, const std::vector<uint8_t>& result) override;
186 };
187 
188 class DemoCameraHostCallback : public ICameraHostCallback {
189 public:
190     DemoCameraHostCallback() = default;
191     virtual ~DemoCameraHostCallback() = default;
192 
193     int32_t OnCameraStatus(const std::string& cameraId, CameraStatus status) override;
194 
195     int32_t OnFlashlightStatus(const std::string& cameraId, FlashlightStatus status) override;
196 
197     int32_t OnCameraEvent(const std::string& cameraId, CameraEvent event) override;
198 };
199 
200 class DemoStreamOperatorCallback : public IStreamOperatorCallback {
201 public:
202     DemoStreamOperatorCallback() = default;
203     virtual ~DemoStreamOperatorCallback() = default;
204 
205     int32_t OnCaptureStarted(int32_t captureId, const std::vector<int32_t>& streamIds) override;
206     int32_t OnCaptureEnded(int32_t captureId, const std::vector<CaptureEndedInfo>& infos) override;
207     int32_t OnCaptureError(int32_t captureId, const std::vector<CaptureErrorInfo>& infos) override;
208     int32_t OnFrameShutter(int32_t captureId, const std::vector<int32_t>& streamIds, uint64_t timestamp) override;
209 };
210 }
211 } // namespace OHOS::DistributedHardware
212 #endif // DISTRIBUTED_DCAMERA_HDF_DEMO_H