• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2020 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_DEMO_H
17 #define OHOS_CAMERA_CAMERA_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 "camera.h"
27 #include "camera_metadata_info.h"
28 #include "ibuffer.h"
29 #include "v1_0/ioffline_stream_operator.h"
30 #include "v1_0/icamera_host.h"
31 #include "camera_host_callback.h"
32 #include "camera_device_callback.h"
33 #include "v1_0/icamera_device.h"
34 #include "stream_operator_callback.h"
35 #include "v1_0/istream_operator_callback.h"
36 #include "stream_customer.h"
37 #include "securec.h"
38 #include "project_camera_demo.h"
39 #include "if_system_ability_manager.h"
40 #include "iservice_registry.h"
41 #include "v1_0/camera_host_proxy.h"
42 
43 namespace OHOS::Camera {
44 using namespace OHOS::HDI::Camera::V1_0;
45 
46 enum CaptureMode {
47     CAPTURE_PREVIEW = 0,
48     CAPTURE_SNAPSHOT,
49     CAPTURE_VIDEO,
50 };
51 
52 enum DemoActionID {
53     STREAM_ID_PREVIEW = 1001,
54     STREAM_ID_CAPTURE,
55     STREAM_ID_VIDEO,
56     CAPTURE_ID_PREVIEW = 2001,
57     CAPTURE_ID_CAPTURE,
58     CAPTURE_ID_VIDEO,
59 };
60 
61 class DemoCameraHostCallback;
62 class OhosCameraDemo {
63 public:
64     OhosCameraDemo();
65     ~OhosCameraDemo();
66 
67     RetCode InitCameraDevice();
68     void ReleaseCameraDevice();
69     RetCode InitSensors();
70 
71     RetCode StartPreviewStream();
72     RetCode StartCaptureStream();
73     RetCode StartVideoStream();
74     RetCode StartDualStreams(const int streamIdSecond);
75 
76     RetCode ReleaseAllStream();
77 
78     RetCode CaptureOnDualStreams(const int streamIdSecond);
79     RetCode CaptureON(const int streamId, const int captureId, CaptureMode mode);
80     RetCode CaptureOff(const int captureId, const CaptureMode mode);
81 
82     void SetAwbMode(const int mode) const;
83     void SetAeExpo();
84     void SetMetadata();
85     void SetEnableResult();
86     void FlashlightOnOff(bool onOff);
87 
88     RetCode StreamOffline(const int streamId);
89 
90     void QuitDemo();
91 
92 private:
93     void SetStreamInfo(StreamInfo& streamInfo,
94         const std::shared_ptr<StreamCustomer>& streamCustomer,
95         const int streamId, const StreamIntent intent);
96     void GetStreamOpt();
97 
98     RetCode CreateStream(const int streamId, std::shared_ptr<StreamCustomer> &streamCustomer,
99         StreamIntent intent);
100     RetCode CreateStreams(const int streamIdSecond, StreamIntent intent);
101 
102     void StoreImage(const void *bufStart, const uint32_t size) const;
103     void StoreVideo(const void *bufStart, const uint32_t size) const;
104     void OpenVideoFile();
105 
106     RetCode GetFaceDetectMode(std::shared_ptr<CameraAbility> &ability);
107     RetCode GetFocalLength(std::shared_ptr<CameraAbility> &ability);
108     RetCode GetAvailableFocusModes(std::shared_ptr<CameraAbility> &ability);
109     RetCode GetAvailableExposureModes(std::shared_ptr<CameraAbility> &ability);
110     RetCode GetExposureCompensationRange(std::shared_ptr<CameraAbility> &ability);
111     RetCode GetExposureCompensationSteps(std::shared_ptr<CameraAbility> &ability);
112     RetCode GetAvailableMeterModes(std::shared_ptr<CameraAbility> &ability);
113     RetCode GetAvailableFlashModes(std::shared_ptr<CameraAbility> &ability);
114     RetCode GetMirrorSupported(std::shared_ptr<CameraAbility> &ability);
115     RetCode GetStreamBasicConfigurations(std::shared_ptr<CameraAbility> &ability);
116     RetCode GetFpsRange(std::shared_ptr<CameraAbility> &ability);
117     RetCode GetCameraPosition(std::shared_ptr<CameraAbility> &ability);
118     RetCode GetCameraType(std::shared_ptr<CameraAbility> &ability);
119     RetCode GetCameraConnectionType(std::shared_ptr<CameraAbility> &ability);
120     RetCode GetFaceDetectMaxNum(std::shared_ptr<CameraAbility> &ability);
121 
122     bool aeStatus_ = true;
123     int videoFd_ = -1;
124     unsigned int isPreviewOn_ = 0;
125     unsigned int isCaptureOn_ = 0;
126     unsigned int isVideoOn_ = 0;
127 
128     uint8_t captureQuality_ = 0;
129     int32_t captureOrientation_ = 0;
130     uint8_t mirrorSwitch_ = 0;
131     std::vector<double> gps_;
132     CaptureInfo captureInfo_;
133 
134     std::shared_ptr<StreamCustomer> streamCustomerPreview_ = nullptr;
135     std::shared_ptr<StreamCustomer> streamCustomerCapture_ = nullptr;
136     std::shared_ptr<StreamCustomer> streamCustomerVideo_ = nullptr;
137     std::shared_ptr<CameraAbility> ability_ = nullptr;
138     std::shared_ptr<CameraSetting> captureSetting_ = nullptr;
139     std::mutex metaDatalock_;
140     std::vector<uint8_t> cameraAbility_;
141 
142 #ifdef CAMERA_BUILT_ON_OHOS_LITE
143     std::shared_ptr<DemoCameraHostCallback> hostCallback_ = nullptr;
144     std::shared_ptr<IStreamOperator> streamOperator_ = nullptr;
145     std::shared_ptr<CameraHost> demoCameraHost_ = nullptr;
146     std::shared_ptr<ICameraDevice> demoCameraDevice_ = nullptr;
147 #else
148     OHOS::sptr<DemoCameraHostCallback> hostCallback_ = nullptr;
149     OHOS::sptr<IStreamOperator> streamOperator_ = nullptr;
150     OHOS::sptr<ICameraHost> demoCameraHost_ = nullptr;
151     OHOS::sptr<ICameraDevice> demoCameraDevice_ = nullptr;
152 #endif
153     std::vector<std::string> cameraIds_ = {};
154 };
155 #ifdef CAMERA_BUILT_ON_OHOS_LITE
156 
157 #else
158 class DemoCameraDeviceCallback : public ICameraDeviceCallback {
159 public:
160     DemoCameraDeviceCallback() = default;
161     virtual ~DemoCameraDeviceCallback() = default;
162     int32_t OnError(ErrorType type, int32_t errorCode) override;
163     int32_t OnResult(uint64_t timestamp, const std::vector<uint8_t>& result) override;
164 };
165 
166 class DemoCameraHostCallback : public ICameraHostCallback {
167 public:
168     DemoCameraHostCallback() = default;
169     virtual ~DemoCameraHostCallback() = default;
170 
171 public:
172     int32_t OnCameraStatus(const std::string& cameraId, CameraStatus status) override;
173 
174     int32_t OnFlashlightStatus(const std::string& cameraId, FlashlightStatus status) override;
175 
176     int32_t OnCameraEvent(const std::string& cameraId, CameraEvent event) override;
177 };
178 
179 class DemoStreamOperatorCallback : public IStreamOperatorCallback {
180 public:
181     DemoStreamOperatorCallback() = default;
182     virtual ~DemoStreamOperatorCallback() = default;
183 
184 public:
185     int32_t OnCaptureStarted(int32_t captureId, const std::vector<int32_t>& streamIds) override;
186     int32_t OnCaptureEnded(int32_t captureId, const std::vector<CaptureEndedInfo>& infos) override;
187     int32_t OnCaptureError(int32_t captureId, const std::vector<CaptureErrorInfo>& infos) override;
188     int32_t OnFrameShutter(int32_t captureId, const std::vector<int32_t>& streamIds, uint64_t timestamp) override;
189 };
190 
191 #endif
192 } // namespace OHOS::Camera
193 #endif // OHOS_CAMERA_CAMERA_DEMO_H
194