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