1 /*
2 * Copyright (c) 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 #include "dcamera_client_demo.h"
17
18 using namespace OHOS;
19 using namespace OHOS::Camera;
20 using namespace OHOS::CameraStandard;
21 using namespace OHOS::DistributedHardware;
22
23 namespace OHOS {
24 namespace DistributedHardware {
25 constexpr double LOCATION_LATITUDE = 22.306;
26 constexpr double LOCATION_LONGITUDE = 52.12;
27 constexpr double LOCATION_ALTITUDE = 2.365;
28 #ifdef DCAMERA_COMMON
29 constexpr int32_t PHOTO_WIDTH = 1280;
30 constexpr int32_t PHOTO_HEIGTH = 960;
31 #else
32 constexpr int32_t PHOTO_WIDTH = 1920;
33 constexpr int32_t PHOTO_HEIGTH = 1080;
34 #endif
35 constexpr int32_t PREVIEW_WIDTH = 640;
36 constexpr int32_t PREVIEW_HEIGTH = 480;
37 constexpr int32_t VIDEO_WIDTH = 640;
38 constexpr int32_t VIDEO_HEIGTH = 480;
39
40 static sptr<CameraDevice> g_cameraInfo = nullptr;
41 static sptr<CameraManager> g_cameraManager = nullptr;
42 static sptr<CaptureInput> g_cameraInput = nullptr;
43 static sptr<CaptureOutput> g_photoOutput = nullptr;
44 static sptr<CaptureOutput> g_previewOutput = nullptr;
45 static sptr<CaptureOutput> g_videoOutput = nullptr;
46 static sptr<CaptureSession> g_captureSession = nullptr;
47 static std::shared_ptr<DCameraCaptureInfo> g_photoInfo = nullptr;
48 static std::shared_ptr<DCameraCaptureInfo> g_previewInfo = nullptr;
49 static std::shared_ptr<DCameraCaptureInfo> g_videoInfo = nullptr;
50
51 #ifdef DCAMERA_COMMON
52 constexpr int32_t PHOTO_FORMAT = camera_format_t::OHOS_CAMERA_FORMAT_JPEG;
53 constexpr int32_t PREVIEW_FORMAT = camera_format_t::OHOS_CAMERA_FORMAT_RGBA_8888;
54 constexpr int32_t VIDEO_FORMAT = camera_format_t::OHOS_CAMERA_FORMAT_RGBA_8888;
55 #else
56 constexpr int32_t PHOTO_FORMAT = camera_format_t::OHOS_CAMERA_FORMAT_JPEG;
57 constexpr int32_t PREVIEW_FORMAT = camera_format_t::OHOS_CAMERA_FORMAT_YCRCB_420_SP;
58 constexpr int32_t VIDEO_FORMAT = camera_format_t::OHOS_CAMERA_FORMAT_YCRCB_420_SP;
59 #endif
60
InitCameraStandard(CameraPosition position)61 int32_t InitCameraStandard(CameraPosition position)
62 {
63 g_cameraManager = CameraManager::GetInstance();
64 g_cameraManager->SetCallback(std::make_shared<DemoDCameraManagerCallback>());
65
66 int rv = g_cameraManager->CreateCaptureSession(&g_captureSession);
67 if (rv != DCAMERA_OK) {
68 DHLOGE("InitCameraStandard create captureSession failed, rv: %d", rv);
69 return rv;
70 }
71 std::shared_ptr<DemoDCameraSessionCallback> sessionCallback = std::make_shared<DemoDCameraSessionCallback>();
72 g_captureSession->SetCallback(sessionCallback);
73 g_captureSession->SetFocusCallback(sessionCallback);
74
75 std::vector<sptr<CameraDevice>> cameraObjList = g_cameraManager->GetSupportedCameras();
76 for (auto info : cameraObjList) {
77 DHLOGI("Camera: %s, position: %d, camera type: %d, connection type: %d", GetAnonyString(info->GetID()).c_str(),
78 info->GetPosition(), info->GetCameraType(), info->GetConnectionType());
79 // CAMERA_POSITION_BACK or CAMERA_POSITION_FRONT
80 if ((info->GetPosition() == position) &&
81 (info->GetConnectionType() == ConnectionType::CAMERA_CONNECTION_REMOTE)) {
82 g_cameraInfo = info;
83 break;
84 }
85 }
86
87 if (g_cameraInfo == nullptr) {
88 DHLOGE("Distributed Camera Demo: have no remote camera");
89 return DCAMERA_BAD_VALUE;
90 }
91
92 rv = g_cameraManager->CreateCameraInput(g_cameraInfo, &((sptr<CameraInput> &)g_cameraInput));
93 if (rv != DCAMERA_OK) {
94 DHLOGE("InitCameraStandard create cameraInput failed, rv: %d", rv);
95 return rv;
96 }
97 int32_t ret = ((sptr<CameraInput> &)g_cameraInput)->Open();
98 if (ret != DCAMERA_OK) {
99 DHLOGE("InitCameraStandard g_cameraInput Open failed, ret: %d", ret);
100 return ret;
101 }
102 std::shared_ptr<DemoDCameraInputCallback> inputCallback = std::make_shared<DemoDCameraInputCallback>();
103 ((sptr<CameraInput> &)g_cameraInput)->SetErrorCallback(inputCallback);
104 return DCAMERA_OK;
105 }
106
InitCaptureInfo(int32_t width,int32_t height)107 void InitCaptureInfo(int32_t width, int32_t height)
108 {
109 g_photoInfo = std::make_shared<DCameraCaptureInfo>();
110 g_photoInfo->width_ = PHOTO_WIDTH;
111 g_photoInfo->height_ = PHOTO_HEIGTH;
112 g_photoInfo->format_ = PHOTO_FORMAT;
113
114 g_previewInfo = std::make_shared<DCameraCaptureInfo>();
115 if (width == 0 && height == 0) {
116 g_previewInfo->width_ = PREVIEW_WIDTH;
117 g_previewInfo->height_ = PREVIEW_HEIGTH;
118 } else {
119 g_previewInfo->width_ = width;
120 g_previewInfo->height_ = height;
121 }
122 g_previewInfo->format_ = PREVIEW_FORMAT;
123
124 g_videoInfo = std::make_shared<DCameraCaptureInfo>();
125 g_videoInfo->width_ = VIDEO_WIDTH;
126 g_videoInfo->height_ = VIDEO_HEIGTH;
127 g_videoInfo->format_ = VIDEO_FORMAT;
128 }
129
ConvertToCameraFormat(int32_t format)130 CameraFormat ConvertToCameraFormat(int32_t format)
131 {
132 CameraFormat ret = CameraFormat::CAMERA_FORMAT_INVALID;
133 DCameraFormat df = static_cast<DCameraFormat>(format);
134 switch (df) {
135 case DCameraFormat::OHOS_CAMERA_FORMAT_RGBA_8888:
136 ret = CameraFormat::CAMERA_FORMAT_RGBA_8888;
137 break;
138 case DCameraFormat::OHOS_CAMERA_FORMAT_YCBCR_420_888:
139 ret = CameraFormat::CAMERA_FORMAT_YCBCR_420_888;
140 break;
141 case DCameraFormat::OHOS_CAMERA_FORMAT_YCRCB_420_SP:
142 ret = CameraFormat::CAMERA_FORMAT_YUV_420_SP;
143 break;
144 case DCameraFormat::OHOS_CAMERA_FORMAT_JPEG:
145 ret = CameraFormat::CAMERA_FORMAT_JPEG;
146 break;
147 default:
148 break;
149 }
150 return ret;
151 }
152
InitPhotoOutput(void)153 void InitPhotoOutput(void)
154 {
155 DHLOGI("Distributed Camera Demo: Create PhotoOutput, width = %d, height = %d, format = %d",
156 g_photoInfo->width_, g_photoInfo->height_, g_photoInfo->format_);
157 sptr<IConsumerSurface> photoSurface = IConsumerSurface::Create();
158 sptr<IBufferConsumerListener> photoListener = new DemoDCameraPhotoSurfaceListener(photoSurface);
159 photoSurface->RegisterConsumerListener(photoListener);
160 CameraFormat photoFormat = ConvertToCameraFormat(g_photoInfo->format_);
161 Size photoSize = {g_photoInfo->width_, g_photoInfo->height_};
162 Profile photoProfile(photoFormat, photoSize);
163 sptr<IBufferProducer> photoProducer = photoSurface->GetProducer();
164 int rv = g_cameraManager->CreatePhotoOutput(photoProfile, photoProducer, &((sptr<PhotoOutput> &)g_photoOutput));
165 if (rv != DCAMERA_OK) {
166 DHLOGE("InitPhotoOutput create photoOutput failed, rv: %d", rv);
167 return;
168 }
169 ((sptr<PhotoOutput> &)g_photoOutput)->SetCallback(std::make_shared<DemoDCameraPhotoCallback>());
170 }
171
InitPreviewOutput(void)172 void InitPreviewOutput(void)
173 {
174 DHLOGI("Distributed Camera Demo: Create PreviewOutput, width = %d, height = %d, format = %d",
175 g_previewInfo->width_, g_previewInfo->height_, g_previewInfo->format_);
176 sptr<IConsumerSurface> previewSurface = IConsumerSurface::Create();
177 sptr<IBufferConsumerListener> previewListener = new DemoDCameraPreviewSurfaceListener(previewSurface);
178 previewSurface->RegisterConsumerListener(previewListener);
179 CameraFormat previewFormat = ConvertToCameraFormat(g_previewInfo->format_);
180 Size previewSize = {g_previewInfo->width_, g_previewInfo->height_};
181 Profile previewProfile(previewFormat, previewSize);
182 sptr<IBufferProducer> previewProducer = previewSurface->GetProducer();
183 sptr<Surface> previewProducerSurface = Surface::CreateSurfaceAsProducer(previewProducer);
184 int rv = g_cameraManager->CreatePreviewOutput(
185 previewProfile, previewProducerSurface, &((sptr<PreviewOutput> &)g_previewOutput));
186 if (rv != DCAMERA_OK) {
187 DHLOGE("InitPhotoOutput create previewOutput failed, rv: %d", rv);
188 return;
189 }
190 ((sptr<PreviewOutput> &)g_previewOutput)->SetCallback(std::make_shared<DemoDCameraPreviewCallback>());
191 }
192
InitVideoOutput(void)193 void InitVideoOutput(void)
194 {
195 DHLOGI("Distributed Camera Demo: Create VideoOutput, width = %d, height = %d, format = %d",
196 g_videoInfo->width_, g_videoInfo->height_, g_videoInfo->format_);
197 sptr<IConsumerSurface> videoSurface = IConsumerSurface::Create();
198 sptr<IBufferConsumerListener> videoListener = new DemoDCameraVideoSurfaceListener(videoSurface);
199 videoSurface->RegisterConsumerListener(videoListener);
200 CameraFormat videoFormat = ConvertToCameraFormat(g_videoInfo->format_);
201 Size videoSize = {g_videoInfo->width_, g_videoInfo->height_};
202 std::vector<int32_t> framerates = {};
203 VideoProfile videoSettings(videoFormat, videoSize, framerates);
204 sptr<IBufferProducer> videoProducer = videoSurface->GetProducer();
205 sptr<Surface> pSurface = Surface::CreateSurfaceAsProducer(videoProducer);
206 int rv = g_cameraManager->CreateVideoOutput(videoSettings, pSurface, &((sptr<VideoOutput> &)g_videoOutput));
207 if (rv != DCAMERA_OK) {
208 DHLOGE("InitPhotoOutput create videoOutput failed, rv: %d", rv);
209 return;
210 }
211 ((sptr<VideoOutput> &)g_videoOutput)->SetCallback(std::make_shared<DemoDCameraVideoCallback>());
212 }
213
ConfigCaptureSession(void)214 void ConfigCaptureSession(void)
215 {
216 g_captureSession->BeginConfig();
217 g_captureSession->AddInput(g_cameraInput);
218 g_captureSession->AddOutput(g_previewOutput);
219 g_captureSession->AddOutput(g_videoOutput);
220 g_captureSession->AddOutput(g_photoOutput);
221 g_captureSession->CommitConfig();
222
223 std::vector<VideoStabilizationMode> stabilizationModes;
224 int32_t rv = g_captureSession->GetSupportedStabilizationMode(stabilizationModes);
225 if (rv != DCAMERA_OK) {
226 DHLOGE("ConfigCaptureSession get supported stabilization mode failed, rv: %d", rv);
227 return;
228 }
229 if (!stabilizationModes.empty()) {
230 for (auto mode : stabilizationModes) {
231 DHLOGI("Distributed Camera Demo: video stabilization mode %d", mode);
232 }
233 g_captureSession->SetVideoStabilizationMode(stabilizationModes.back());
234 }
235 g_captureSession->Start();
236 }
237
ConfigFocusFlashAndExposure(bool isVideo)238 void ConfigFocusFlashAndExposure(bool isVideo)
239 {
240 g_captureSession->LockForControl();
241 FocusMode focusMode = FOCUS_MODE_CONTINUOUS_AUTO;
242 ExposureMode exposureMode = EXPOSURE_MODE_AUTO;
243 float exposureValue = 0;
244 std::vector<float> biasRange;
245 int32_t rv = g_captureSession->GetExposureBiasRange(biasRange);
246 if (rv != DCAMERA_OK) {
247 DHLOGE("ConfigFocusAndExposure get exposure bias range failed, rv: %d", rv);
248 return;
249 }
250 if (!biasRange.empty()) {
251 DHLOGI("Distributed Camera Demo: get %d exposure compensation range", biasRange.size());
252 exposureValue = biasRange[0];
253 }
254 FlashMode flash = FLASH_MODE_OPEN;
255 if (isVideo) {
256 flash = FLASH_MODE_ALWAYS_OPEN;
257 }
258 g_captureSession->SetFlashMode(flash);
259 g_captureSession->SetFocusMode(focusMode);
260 g_captureSession->SetExposureMode(exposureMode);
261 g_captureSession->SetExposureBias(exposureValue);
262 g_captureSession->UnlockForControl();
263 }
264
ConfigPhotoCaptureSetting()265 std::shared_ptr<PhotoCaptureSetting> ConfigPhotoCaptureSetting()
266 {
267 std::shared_ptr<PhotoCaptureSetting> photoCaptureSettings = std::make_shared<PhotoCaptureSetting>();
268 // Rotation
269 PhotoCaptureSetting::RotationConfig rotation = PhotoCaptureSetting::RotationConfig::Rotation_0;
270 photoCaptureSettings->SetRotation(rotation);
271 // QualityLevel
272 PhotoCaptureSetting::QualityLevel quality = PhotoCaptureSetting::QualityLevel::QUALITY_LEVEL_HIGH;
273 photoCaptureSettings->SetQuality(quality);
274 // Location
275 std::unique_ptr<Location> location = std::make_unique<Location>();
276 location->latitude = LOCATION_LATITUDE;
277 location->longitude = LOCATION_LONGITUDE;
278 location->altitude = LOCATION_ALTITUDE;
279 photoCaptureSettings->SetLocation(location);
280 return photoCaptureSettings;
281 }
282
ReleaseResource(void)283 void ReleaseResource(void)
284 {
285 if (g_previewOutput != nullptr) {
286 ((sptr<CameraStandard::PreviewOutput> &)g_previewOutput)->Stop();
287 g_previewOutput->Release();
288 g_previewOutput = nullptr;
289 }
290 if (g_photoOutput != nullptr) {
291 g_photoOutput->Release();
292 g_photoOutput = nullptr;
293 }
294 if (g_videoOutput != nullptr) {
295 g_videoOutput->Release();
296 g_videoOutput = nullptr;
297 }
298 if (g_cameraInput != nullptr) {
299 g_cameraInput->Close();
300 g_cameraInput->Release();
301 g_cameraInput = nullptr;
302 }
303 if (g_captureSession != nullptr) {
304 g_captureSession->Stop();
305 g_captureSession->Release();
306 g_captureSession = nullptr;
307 }
308 if (g_cameraManager != nullptr) {
309 g_cameraManager->SetCallback(nullptr);
310 }
311 g_cameraInfo = nullptr;
312 g_cameraManager = nullptr;
313 }
314
Capture()315 int32_t Capture()
316 {
317 int32_t ret = ((sptr<PhotoOutput> &)g_photoOutput)->Capture(ConfigPhotoCaptureSetting());
318 if (ret != DCAMERA_OK) {
319 DHLOGE("main g_photoOutput Capture failed, ret: %d", ret);
320 return ret;
321 }
322 return DCAMERA_OK;
323 }
324
Video()325 int32_t Video()
326 {
327 int32_t ret = ((sptr<VideoOutput> &)g_videoOutput)->Start();
328 if (ret != DCAMERA_OK) {
329 DHLOGE("main VideoOutput Start failed, ret: %d", ret);
330 return ret;
331 }
332 return DCAMERA_OK;
333 }
334
GetPreviewProfiles(std::vector<CameraStandard::Size> & previewResolution)335 int32_t GetPreviewProfiles(std::vector<CameraStandard::Size> &previewResolution)
336 {
337 sptr<CameraOutputCapability> capability = g_cameraManager->GetSupportedOutputCapability(g_cameraInfo);
338 if (capability == nullptr) {
339 DHLOGI("get supported capability is null");
340 return DCAMERA_BAD_VALUE;
341 }
342 std::vector<CameraStandard::Profile> previewProfiles = capability->GetPreviewProfiles();
343 DHLOGI("size: %d", previewProfiles.size());
344 for (auto& profile : previewProfiles) {
345 CameraStandard::Size picSize = profile.GetSize();
346 DHLOGI("width: %d, height: %d", picSize.width, picSize.height);
347 if (IsValid(picSize)) {
348 previewResolution.push_back(picSize);
349 }
350 }
351 return DCAMERA_OK;
352 }
353
IsValid(const CameraStandard::Size & size)354 bool IsValid(const CameraStandard::Size& size)
355 {
356 return (size.width >= RESOLUTION_MIN_WIDTH) && (size.height >= RESOLUTION_MIN_HEIGHT) &&
357 (size.width <= RESOLUTION_MAX_WIDTH_CONTINUOUS) && (size.height <= RESOLUTION_MAX_HEIGHT_CONTINUOUS);
358 }
359 } // namespace DistributedHardware
360 } // namespace OHOS
361