1 /*
2 * Copyright (c) 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 #include "camera_manager_adapter_impl.h"
17
18 #include <unordered_map>
19
20 #include "camera_rotation_info_adapter_impl.h"
21 #include "display.h"
22 #include "display_manager.h"
23 #include "format_adapter_impl.h"
24 #include "hisysevent_adapter.h"
25 #include "nweb_log.h"
26 #include "ohos_adapter_helper.h"
27 #include "syspara/parameters.h"
28 #include "video_capture_range_adapter_impl.h"
29 #include "video_control_support_adapter_impl.h"
30 #include "video_device_descriptor_adapter_impl.h"
31
32 namespace OHOS::NWeb {
33 using namespace OHOS::CameraStandard;
34 const std::unordered_map<ConnectionType, VideoTransportType> TRANS_TYPE_MAP = {
35 { CAMERA_CONNECTION_BUILT_IN, VideoTransportType::VIDEO_TRANS_TYPE_BUILD_IN },
36 { CAMERA_CONNECTION_USB_PLUGIN, VideoTransportType::VIDEO_TRANS_TYPE_USB },
37 { CAMERA_CONNECTION_REMOTE, VideoTransportType::VIDEO_TRANS_TYPE_REMOTE },
38 };
39
40 const std::unordered_map<CameraPosition, VideoFacingModeAdapter> FACING_MODE_MAP = {
41 { CAMERA_POSITION_UNSPECIFIED, VideoFacingModeAdapter::FACING_NONE },
42 { CAMERA_POSITION_FRONT, VideoFacingModeAdapter::FACING_USER },
43 { CAMERA_POSITION_BACK, VideoFacingModeAdapter::FACING_ENVIRONMENT },
44 };
45
46 const std::unordered_map<CameraFormat, VideoPixelFormatAdapter> TO_ADAPTER_PIXEL_FORMAT_MAP = {
47 { CAMERA_FORMAT_RGBA_8888, VideoPixelFormatAdapter::FORMAT_RGBA_8888 },
48 { CAMERA_FORMAT_YCBCR_420_888, VideoPixelFormatAdapter::FORMAT_YCBCR_420_888 },
49 { CAMERA_FORMAT_YUV_420_SP, VideoPixelFormatAdapter::FORMAT_YUV_420_SP },
50 { CAMERA_FORMAT_JPEG, VideoPixelFormatAdapter::FORMAT_JPEG },
51 { CAMERA_FORMAT_INVALID, VideoPixelFormatAdapter::FORMAT_UNKNOWN },
52 };
53
54 const std::unordered_map<VideoPixelFormatAdapter, CameraFormat> TO_OHOS_PIXEL_FORMAT_MAP = {
55 { VideoPixelFormatAdapter::FORMAT_RGBA_8888, CAMERA_FORMAT_RGBA_8888 },
56 { VideoPixelFormatAdapter::FORMAT_YCBCR_420_888, CAMERA_FORMAT_YCBCR_420_888 },
57 { VideoPixelFormatAdapter::FORMAT_YUV_420_SP, CAMERA_FORMAT_YUV_420_SP },
58 { VideoPixelFormatAdapter::FORMAT_JPEG, CAMERA_FORMAT_JPEG },
59 { VideoPixelFormatAdapter::FORMAT_UNKNOWN, CAMERA_FORMAT_INVALID },
60 };
61
62 const std::unordered_map<ExposureMode, ExposureModeAdapter> EXPOSURE_MODE_MAP = {
63 { EXPOSURE_MODE_UNSUPPORTED, ExposureModeAdapter::EXPOSURE_MODE_UNSUPPORTED },
64 { EXPOSURE_MODE_LOCKED, ExposureModeAdapter::EXPOSURE_MODE_LOCKED },
65 { EXPOSURE_MODE_AUTO, ExposureModeAdapter::EXPOSURE_MODE_AUTO },
66 { EXPOSURE_MODE_CONTINUOUS_AUTO, ExposureModeAdapter::EXPOSURE_MODE_CONTINUOUS_AUTO },
67 };
68
69 const std::unordered_map<FocusModeAdapter, FocusMode> FOCUS_MODE_MAP = {
70 { FocusModeAdapter::FOCUS_MODE_MANUAL, FOCUS_MODE_MANUAL },
71 { FocusModeAdapter::FOCUS_MODE_CONTINUOUS_AUTO, FOCUS_MODE_CONTINUOUS_AUTO },
72 { FocusModeAdapter::FOCUS_MODE_AUTO, FOCUS_MODE_AUTO },
73 { FocusModeAdapter::FOCUS_MODE_LOCKED, FOCUS_MODE_LOCKED },
74 };
75
76 const std::unordered_map<FocusMode, FocusModeAdapter> ADAPTER_FOCUS_MODE_MAP = {
77 { FOCUS_MODE_MANUAL, FocusModeAdapter::FOCUS_MODE_MANUAL },
78 { FOCUS_MODE_CONTINUOUS_AUTO, FocusModeAdapter::FOCUS_MODE_CONTINUOUS_AUTO },
79 { FOCUS_MODE_AUTO, FocusModeAdapter::FOCUS_MODE_AUTO },
80 { FOCUS_MODE_LOCKED, FocusModeAdapter::FOCUS_MODE_LOCKED },
81 };
82
83 const std::unordered_map<FlashModeAdapter, FlashMode> FLASH_MODE_MAP = {
84 { FlashModeAdapter::FLASH_MODE_CLOSE, FLASH_MODE_CLOSE },
85 { FlashModeAdapter::FLASH_MODE_OPEN, FLASH_MODE_OPEN },
86 { FlashModeAdapter::FLASH_MODE_AUTO, FLASH_MODE_AUTO },
87 { FlashModeAdapter::FLASH_MODE_ALWAYS_OPEN, FLASH_MODE_ALWAYS_OPEN },
88 };
89
90 const std::unordered_map<CameraStatus, CameraStatusAdapter> CAMERA_STATUS_MAP = {
91 { CAMERA_STATUS_APPEAR, CameraStatusAdapter::APPEAR },
92 { CAMERA_STATUS_DISAPPEAR, CameraStatusAdapter::DISAPPEAR },
93 { CAMERA_STATUS_AVAILABLE, CameraStatusAdapter::AVAILABLE },
94 { CAMERA_STATUS_UNAVAILABLE, CameraStatusAdapter::UNAVAILABLE },
95 };
96
97 const std::unordered_map<CameraErrorType, std::string> ERROR_TYPE_MAP = {
98 { CameraErrorType::CAMERA_NOT_CLOSE, "CAMERA_NOT_CLOSE" },
99 { CameraErrorType::INPUT_ALREADY_INIT, "INPUT_ALREADY_INIT" },
100 { CameraErrorType::CAMERA_MANAGER_IS_NULL, "CAMERA_MANAGER_IS_NULL" },
101 { CameraErrorType::GET_CAMERA_OBJ_FAILED, "GET_CAMERA_OBJ_FAILED" },
102 { CameraErrorType::CREATE_INPUT_FAILED, "CREATE_INPUT_FAILED" },
103 { CameraErrorType::INPUT_OPEN_FAILED, "INPUT_OPEN_FAILED" },
104 { CameraErrorType::INPUT_NOT_INIT, "INPUT_NOT_INIT" },
105 { CameraErrorType::CREATE_PREVIEW_SURFACE_FAILED, "CREATE_PREVIEW_SURFACE_FAILED" },
106 { CameraErrorType::CREATE_PREVIEW_OUTPUT_FAILED, "CREATE_PREVIEW_OUTPUT_FAILED" },
107 { CameraErrorType::CREATE_CAPTURE_SESSION_FAILED, "CREATE_CAPTURE_SESSION_FAILED" },
108 { CameraErrorType::ADD_INPUT_FAILED, "ADD_INPUT_FAILED" },
109 { CameraErrorType::ADD_OUTPUT_FAILED, "ADD_OUTPUT_FAILED" },
110 { CameraErrorType::START_SESSION_FAILED, "START_SESSION_FAILED" },
111 { CameraErrorType::INPUT_OR_OUTPUT_IS_NULL, "INPUT_OR_OUTPUT_IS_NULL" },
112 { CameraErrorType::COMMIT_CONFIG_FAILED, "COMMIT_CONFIG_FAILED" },
113 };
114
GetCameraTransportType(ConnectionType connectType)115 VideoTransportType CameraManagerAdapterImpl::GetCameraTransportType(ConnectionType connectType)
116 {
117 auto item = TRANS_TYPE_MAP.find(connectType);
118 if (item == TRANS_TYPE_MAP.end()) {
119 WVLOG_E("concect type %{public}d not found", connectType);
120 return VideoTransportType::VIDEO_TRANS_TYPE_OTHER;
121 }
122 return item->second;
123 }
124
GetCameraFacingMode(CameraPosition position)125 VideoFacingModeAdapter CameraManagerAdapterImpl::GetCameraFacingMode(CameraPosition position)
126 {
127 auto item = FACING_MODE_MAP.find(position);
128 if (item == FACING_MODE_MAP.end()) {
129 WVLOG_E("position type %{public}d not found", position);
130 return VideoFacingModeAdapter::FACING_NONE;
131 }
132 return item->second;
133 }
134
TransToAdapterCameraFormat(CameraFormat format)135 VideoPixelFormatAdapter CameraManagerAdapterImpl::TransToAdapterCameraFormat(CameraFormat format)
136 {
137 auto item = TO_ADAPTER_PIXEL_FORMAT_MAP.find(format);
138 if (item == TO_ADAPTER_PIXEL_FORMAT_MAP.end()) {
139 WVLOG_E("to adapter pixel format type %{public}d not found", format);
140 return VideoPixelFormatAdapter::FORMAT_UNKNOWN;
141 }
142 return item->second;
143 }
144
TransToOriCameraFormat(VideoPixelFormatAdapter format)145 CameraFormat CameraManagerAdapterImpl::TransToOriCameraFormat(VideoPixelFormatAdapter format)
146 {
147 auto item = TO_OHOS_PIXEL_FORMAT_MAP.find(format);
148 if (item == TO_OHOS_PIXEL_FORMAT_MAP.end()) {
149 WVLOG_E("to pixel format type %{public}d not found", format);
150 return CAMERA_FORMAT_INVALID;
151 }
152 return item->second;
153 }
154
GetAdapterExposureMode(ExposureMode exportMode)155 ExposureModeAdapter CameraManagerAdapterImpl::GetAdapterExposureMode(ExposureMode exportMode)
156 {
157 auto item = EXPOSURE_MODE_MAP.find(exportMode);
158 if (item == EXPOSURE_MODE_MAP.end()) {
159 WVLOG_E("to exposure mode %{public}d not found", exportMode);
160 return ExposureModeAdapter::EXPOSURE_MODE_UNSUPPORTED;
161 }
162 return item->second;
163 }
164
GetOriFocusMode(FocusModeAdapter focusMode)165 FocusMode CameraManagerAdapterImpl::GetOriFocusMode(FocusModeAdapter focusMode)
166 {
167 auto item = FOCUS_MODE_MAP.find(focusMode);
168 if (item == FOCUS_MODE_MAP.end()) {
169 WVLOG_E("adapter focus mode %{public}d not found", focusMode);
170 return FOCUS_MODE_MANUAL;
171 }
172 return item->second;
173 }
174
GetOriFlashMode(FlashModeAdapter flashMode)175 FlashMode CameraManagerAdapterImpl::GetOriFlashMode(FlashModeAdapter flashMode)
176 {
177 auto item = FLASH_MODE_MAP.find(flashMode);
178 if (item == FLASH_MODE_MAP.end()) {
179 WVLOG_E("adapter flash mode %{public}d not found", flashMode);
180 return FLASH_MODE_CLOSE;
181 }
182 return item->second;
183 }
184
GetAdapterFocusMode(FocusMode focusMode)185 FocusModeAdapter CameraManagerAdapterImpl::GetAdapterFocusMode(FocusMode focusMode)
186 {
187 auto item = ADAPTER_FOCUS_MODE_MAP.find(focusMode);
188 if (item == ADAPTER_FOCUS_MODE_MAP.end()) {
189 WVLOG_E("ori focus mode %{public}d not found", focusMode);
190 return FocusModeAdapter::FOCUS_MODE_MANUAL;
191 }
192 return item->second;
193 }
194
GetInstance()195 CameraManagerAdapterImpl& CameraManagerAdapterImpl::GetInstance()
196 {
197 static CameraManagerAdapterImpl instance;
198 return instance;
199 }
200
ErrorTypeToString(CameraErrorType errorType,std::string & errnoTypeString)201 int32_t CameraManagerAdapterImpl::ErrorTypeToString(CameraErrorType errorType, std::string& errnoTypeString)
202 {
203 auto item = ERROR_TYPE_MAP.find(errorType);
204 if (item == ERROR_TYPE_MAP.end()) {
205 WVLOG_E("ori error type %{public}d not found", errorType);
206 return CAMERA_ERROR;
207 }
208
209 errnoTypeString = item->second;
210 return CAMERA_OK;
211 }
212
ReportErrorSysEvent(CameraErrorType errorType)213 void CameraManagerAdapterImpl::ReportErrorSysEvent(CameraErrorType errorType)
214 {
215 WVLOG_I("ReportErrorSysEvent: %{public}d", errorType);
216 const std::string CAMERA_CAPTURE_ERROR = "CAMERA_CAPTURE_ERROR";
217 const std::string DEVICE_ID = "DEVICE_ID";
218 const std::string ERROR_DESC = "ERROR_DESC";
219 std::string errnoTypeString = "";
220 if (ErrorTypeToString(errorType, errnoTypeString) != CAMERA_OK) {
221 return;
222 }
223 OhosAdapterHelper::GetInstance().GetHiSysEventAdapterInstance().Write(CAMERA_CAPTURE_ERROR,
224 HiSysEventAdapter::EventType::FAULT, { DEVICE_ID, wantedDeviceId_, ERROR_DESC, errnoTypeString });
225 }
226
Create(std::shared_ptr<CameraStatusCallbackAdapter> cameraStatusCallback)227 int32_t CameraManagerAdapterImpl::Create(std::shared_ptr<CameraStatusCallbackAdapter> cameraStatusCallback)
228 {
229 std::lock_guard<std::mutex> lock(mutex_);
230
231 WVLOG_I("create CameraManagerAdapterImpl");
232 if (cameraManager_ == nullptr) {
233 cameraManager_ = CameraManager::GetInstance();
234 if (cameraManager_ == nullptr) {
235 WVLOG_E("Failed to get camera manager!");
236 return CAMERA_ERROR;
237 }
238 }
239 cameraMngrCallback_ = std::make_shared<CameraManagerAdapterCallback>(cameraStatusCallback);
240 cameraManager_->SetCallback(cameraMngrCallback_);
241 return CAMERA_OK;
242 }
243
GetCameraSupportFormats(sptr<CameraOutputCapability> outputcapability)244 std::vector<std::shared_ptr<FormatAdapter>> CameraManagerAdapterImpl::GetCameraSupportFormats(
245 sptr<CameraOutputCapability> outputcapability)
246 {
247 std::vector<std::shared_ptr<FormatAdapter>> captureFormats;
248
249 std::vector<Profile> previewProfiles = outputcapability->GetPreviewProfiles();
250 for (auto i : previewProfiles) {
251 std::shared_ptr<FormatAdapterImpl> format = std::make_shared<FormatAdapterImpl>();
252 if (!format) {
253 WVLOG_E("new FormatAdapter failed");
254 return captureFormats;
255 }
256
257 format->SetWidth(i.GetSize().width);
258 format->SetHeight(i.GetSize().height);
259 format->SetFrameRate(DEFAULT_FRAME_RATE);
260 format->SetPixelFormat(TransToAdapterCameraFormat(i.GetCameraFormat()));
261 captureFormats.push_back(format);
262 }
263 return captureFormats;
264 }
265
GetDevicesInfo()266 std::vector<std::shared_ptr<VideoDeviceDescriptorAdapter>> CameraManagerAdapterImpl::GetDevicesInfo()
267 {
268 std::lock_guard<std::mutex> lock(mutex_);
269 if (cameraManager_ == nullptr) {
270 WVLOG_E("camera manager is nullptr");
271 return std::vector<std::shared_ptr<VideoDeviceDescriptorAdapter>>();
272 }
273
274 std::vector<sptr<CameraDevice>> cameraObjList = cameraManager_->GetSupportedCameras();
275 if (cameraObjList.size() == 0) {
276 WVLOG_E("No cameras are available!!!");
277 return std::vector<std::shared_ptr<VideoDeviceDescriptorAdapter>>();
278 }
279
280 std::vector<std::shared_ptr<VideoDeviceDescriptorAdapter>> devicesDiscriptor;
281 for (auto cameraObj : cameraObjList) {
282 sptr<CameraOutputCapability> outputcapability = cameraManager_->GetSupportedOutputCapability(cameraObj);
283 if (outputcapability == nullptr) {
284 WVLOG_E("outputcapability is null");
285 continue;
286 }
287
288 std::shared_ptr<VideoDeviceDescriptorAdapterImpl> deviceDisc =
289 std::make_shared<VideoDeviceDescriptorAdapterImpl>();
290 if (!deviceDisc) {
291 WVLOG_E("new VideoDeviceDescriptorAdapter failed");
292 return devicesDiscriptor;
293 }
294
295 std::string displayName = GetCameraDisplayName(cameraObj->GetID(), cameraObj->GetPosition());
296 deviceDisc->SetDisplayName(displayName);
297 deviceDisc->SetDeviceId(cameraObj->GetID());
298 deviceDisc->SetModelId(cameraObj->GetID());
299
300 std::shared_ptr<VideoControlSupportAdapterImpl> controlSupport =
301 std::make_shared<VideoControlSupportAdapterImpl>();
302 if (!controlSupport) {
303 WVLOG_E("new VideoControlSupportAdapter failed");
304 return devicesDiscriptor;
305 }
306
307 controlSupport->SetPan(false);
308 controlSupport->SetTilt(false);
309 controlSupport->SetZoom(false);
310 deviceDisc->SetControlSupport(controlSupport);
311
312 deviceDisc->SetTransportType(GetCameraTransportType(cameraObj->GetConnectionType()));
313 deviceDisc->SetFacingMode(GetCameraFacingMode(cameraObj->GetPosition()));
314
315 deviceDisc->SetSupportCaptureFormats(GetCameraSupportFormats(outputcapability));
316 WVLOG_I("deviceDisc id:%{public}s, control pan:%{public}d tilt:%{public}d, zoom:%{public}d \
317 transType:%{public}d, facingMode:%{public}d",
318 deviceDisc->GetDeviceId().c_str(), deviceDisc->GetControlSupport()->GetPan(),
319 deviceDisc->GetControlSupport()->GetTilt(), deviceDisc->GetControlSupport()->GetZoom(),
320 deviceDisc->GetTransportType(), deviceDisc->GetFacingMode());
321 devicesDiscriptor.emplace_back(std::move(deviceDisc));
322 }
323 return devicesDiscriptor;
324 }
325
InitCameraInput(const std::string & deviceId)326 int32_t CameraManagerAdapterImpl::InitCameraInput(const std::string& deviceId)
327 {
328 int32_t result = CAMERA_ERROR;
329
330 if (status_ == CameraStatusAdapter::UNAVAILABLE) {
331 WVLOG_E("camera is not closed");
332 ReportErrorSysEvent(CameraErrorType::CAMERA_NOT_CLOSE);
333 return result;
334 }
335
336 if (inputInitedFlag_) {
337 WVLOG_E("input is already inited");
338 ReportErrorSysEvent(CameraErrorType::INPUT_ALREADY_INIT);
339 return result;
340 }
341
342 if (cameraManager_ == nullptr) {
343 WVLOG_E("camera manager is nullptr");
344 ReportErrorSysEvent(CameraErrorType::CAMERA_MANAGER_IS_NULL);
345 return CAMERA_NULL_ERROR;
346 }
347
348 if (cameraInput_ == nullptr) {
349 WVLOG_I("camera input create %{public}s", deviceId.c_str());
350 sptr<CameraDevice> cameraObj = cameraManager_->GetCameraDeviceFromId(deviceId);
351 if (cameraObj == nullptr) {
352 WVLOG_E("No cameras are available!!!");
353 ReportErrorSysEvent(CameraErrorType::GET_CAMERA_OBJ_FAILED);
354 return CAMERA_NULL_ERROR;
355 }
356
357 cameraInput_ = cameraManager_->CreateCameraInput(cameraObj);
358 if (cameraInput_ == nullptr) {
359 WVLOG_E("Failed to create CameraInput");
360 ReportErrorSysEvent(CameraErrorType::CREATE_INPUT_FAILED);
361 return result;
362 }
363 int32_t ret = cameraInput_->Open();
364 if (ret != CAMERA_OK) {
365 WVLOG_E("Failed to open CameraInput, err code %{public}d.", ret);
366 cameraInput_->Release();
367 cameraInput_ = nullptr;
368 ReportErrorSysEvent(CameraErrorType::INPUT_OPEN_FAILED);
369 return result;
370 }
371 deviceId_ = deviceId;
372 inputInitedFlag_ = true;
373 }
374
375 result = CAMERA_OK;
376 return result;
377 }
378
InitPreviewOutput(const std::shared_ptr<VideoCaptureParamsAdapter> captureParams,std::shared_ptr<CameraBufferListenerAdapter> listener)379 int32_t CameraManagerAdapterImpl::InitPreviewOutput(const std::shared_ptr<VideoCaptureParamsAdapter> captureParams,
380 std::shared_ptr<CameraBufferListenerAdapter> listener)
381 {
382 int32_t result = CAMERA_ERROR;
383 Size previewSize;
384
385 if (!inputInitedFlag_) {
386 WVLOG_E("input is not inited");
387 ReportErrorSysEvent(CameraErrorType::INPUT_NOT_INIT);
388 return result;
389 }
390
391 if (cameraManager_ == nullptr) {
392 WVLOG_E("camera manager is null");
393 ReportErrorSysEvent(CameraErrorType::CAMERA_MANAGER_IS_NULL);
394 return result;
395 }
396
397 if (previewOutput_ == nullptr) {
398 WVLOG_I("preview output create");
399 previewSurface_ = IConsumerSurface::Create();
400 if (previewSurface_ == nullptr) {
401 WVLOG_E("previewSurface_ is null");
402 ReportErrorSysEvent(CameraErrorType::CREATE_PREVIEW_SURFACE_FAILED);
403 return result;
404 }
405 previewSize.width = captureParams->GetWidth();
406 previewSize.height = captureParams->GetHeight();
407 previewSurface_->SetDefaultUsage(BUFFER_USAGE_CPU_READ);
408 previewSurface_->SetDefaultWidthAndHeight(previewSize.width, previewSize.height);
409 previewSurface_->SetUserData(
410 CameraManager::surfaceFormat, std::to_string(TransToOriCameraFormat(captureParams->GetPixelFormat())));
411 Profile previewproFile =
412 Profile(static_cast<CameraFormat>(TransToOriCameraFormat(captureParams->GetPixelFormat())), previewSize);
413 WVLOG_I("preview output format: %{public}d, w: %{public}d, h: %{public}d",
414 TransToOriCameraFormat(captureParams->GetPixelFormat()), previewSize.width, previewSize.height);
415 previewSurfaceListener_ =
416 new (std::nothrow) CameraSurfaceListener(SurfaceType::PREVIEW, previewSurface_, (listener));
417 previewSurface_->RegisterConsumerListener((sptr<IBufferConsumerListener>&)previewSurfaceListener_);
418 sptr<IBufferProducer> bp = previewSurface_->GetProducer();
419 sptr<Surface> pSurface = Surface::CreateSurfaceAsProducer(bp);
420 previewOutput_ = cameraManager_->CreatePreviewOutput(previewproFile, pSurface);
421 if (previewOutput_ == nullptr) {
422 WVLOG_E("Failed to create previewOutput");
423 ReportErrorSysEvent(CameraErrorType::CREATE_PREVIEW_OUTPUT_FAILED);
424 return result;
425 }
426 captureParams_ = captureParams;
427 listener_ = listener;
428 }
429 result = CAMERA_OK;
430 return result;
431 }
432
TransToAdapterExposureModes(std::vector<ExposureMode> & exposureModes,std::vector<ExposureModeAdapter> & exposureModesAdapter)433 int32_t CameraManagerAdapterImpl::TransToAdapterExposureModes(
434 std::vector<ExposureMode>& exposureModes, std::vector<ExposureModeAdapter>& exposureModesAdapter)
435 {
436 for (auto exportMode : exposureModes) {
437 exposureModesAdapter.push_back(GetAdapterExposureMode(exportMode));
438 }
439
440 return CAMERA_OK;
441 }
442
GetExposureModes(std::vector<ExposureModeAdapter> & exposureModesAdapter)443 int32_t CameraManagerAdapterImpl::GetExposureModes(std::vector<ExposureModeAdapter>& exposureModesAdapter)
444 {
445 std::lock_guard<std::mutex> lock(mutex_);
446 if (captureSession_ == nullptr) {
447 WVLOG_E("captureSession is nullptr when get exposure modes");
448 return CAMERA_ERROR;
449 }
450 std::vector<ExposureMode> exposureModes;
451 if (captureSession_->GetSupportedExposureModes(exposureModes) != SUCCESS) {
452 WVLOG_E("get inner exposure modes faileds");
453 return CAMERA_ERROR;
454 }
455
456 TransToAdapterExposureModes(exposureModes, exposureModesAdapter);
457
458 return CAMERA_OK;
459 }
460
GetCurrentExposureMode(ExposureModeAdapter & exposureModeAdapter)461 int32_t CameraManagerAdapterImpl::GetCurrentExposureMode(ExposureModeAdapter& exposureModeAdapter)
462 {
463 std::lock_guard<std::mutex> lock(mutex_);
464 if (captureSession_ == nullptr) {
465 WVLOG_E("captureSession is nullptr when get current exposure modes");
466 return CAMERA_ERROR;
467 }
468
469 ExposureMode exposureMode = captureSession_->GetExposureMode();
470 exposureModeAdapter = GetAdapterExposureMode(exposureMode);
471 return CAMERA_OK;
472 }
473
GetExposureCompensation()474 std::shared_ptr<VideoCaptureRangeAdapter> CameraManagerAdapterImpl::GetExposureCompensation()
475 {
476 if (captureSession_ == nullptr) {
477 return nullptr;
478 }
479
480 std::shared_ptr<VideoCaptureRangeAdapterImpl> rangeVal = std::make_shared<VideoCaptureRangeAdapterImpl>();
481 if (!rangeVal) {
482 WVLOG_E("new VideoCaptureRangeAdapter failed");
483 return nullptr;
484 }
485
486 std::vector<float> exposureBiasRange = captureSession_->GetExposureBiasRange();
487 int32_t exposureCompos = captureSession_->GetExposureValue();
488 if (exposureBiasRange.size() == RANGE_MAX_SIZE) {
489 rangeVal->SetMin(exposureBiasRange.at(RANGE_MIN_INDEX));
490 rangeVal->SetMax(exposureBiasRange.at(RANGE_MAX_INDEX));
491 }
492
493 rangeVal->SetCurrent(exposureCompos);
494 return rangeVal;
495 }
496
GetCaptionRangeById(RangeIDAdapter rangeId)497 std::shared_ptr<VideoCaptureRangeAdapter> CameraManagerAdapterImpl::GetCaptionRangeById(RangeIDAdapter rangeId)
498 {
499 std::lock_guard<std::mutex> lock(mutex_);
500 if (captureSession_ == nullptr) {
501 WVLOG_E("captureSession is nullptr when get %{public}d range info", rangeId);
502 return nullptr;
503 }
504
505 std::shared_ptr<VideoCaptureRangeAdapter> result = nullptr;
506 if (rangeId == RangeIDAdapter::RANGE_ID_EXP_COMPENSATION) {
507 result = GetExposureCompensation();
508 if (!result) {
509 WVLOG_E("get exposure compensation failed.");
510 }
511 }
512
513 return result;
514 }
515
IsFocusModeSupported(FocusModeAdapter focusMode)516 bool CameraManagerAdapterImpl::IsFocusModeSupported(FocusModeAdapter focusMode)
517 {
518 std::lock_guard<std::mutex> lock(mutex_);
519 if (captureSession_ == nullptr) {
520 WVLOG_E("captureSession is nullptr when get support focuc mode");
521 return false;
522 }
523 if (!captureSession_->IsFocusModeSupported(GetOriFocusMode(focusMode))) {
524 return false;
525 }
526 return true;
527 }
528
GetCurrentFocusMode()529 FocusModeAdapter CameraManagerAdapterImpl::GetCurrentFocusMode()
530 {
531 std::lock_guard<std::mutex> lock(mutex_);
532 if (captureSession_ == nullptr) {
533 WVLOG_E("captureSession is nullptr when get support focuc mode");
534 return FocusModeAdapter::FOCUS_MODE_MANUAL;
535 }
536
537 FocusMode oriFocusMode = captureSession_->GetFocusMode();
538 return GetAdapterFocusMode(oriFocusMode);
539 }
540
IsFlashModeSupported(FlashModeAdapter focusMode)541 bool CameraManagerAdapterImpl::IsFlashModeSupported(FlashModeAdapter focusMode)
542 {
543 std::lock_guard<std::mutex> lock(mutex_);
544 if (captureSession_ == nullptr) {
545 WVLOG_E("captureSession is nullptr when get support flash mode");
546 return false;
547 }
548 if (!captureSession_->IsFlashModeSupported(GetOriFlashMode(focusMode))) {
549 return false;
550 }
551 return true;
552 }
553
CreateAndStartSession()554 int32_t CameraManagerAdapterImpl::CreateAndStartSession()
555 {
556 int32_t result = CAMERA_ERROR;
557 if (status_ == CameraStatusAdapter::UNAVAILABLE) {
558 WVLOG_E("camera is already opened");
559 ReportErrorSysEvent(CameraErrorType::CAMERA_NOT_CLOSE);
560 return result;
561 }
562
563 if ((cameraInput_ == nullptr) || (previewOutput_ == nullptr)) {
564 WVLOG_E("cameraInput_ or previewOutput_ is null");
565 ReportErrorSysEvent(CameraErrorType::INPUT_OR_OUTPUT_IS_NULL);
566 return result;
567 }
568
569 WVLOG_I("CreateCaptureSession");
570 captureSession_ = cameraManager_->CreateCaptureSession();
571 if (captureSession_ == nullptr) {
572 WVLOG_E("Failed to create capture session");
573 ReportErrorSysEvent(CameraErrorType::CREATE_CAPTURE_SESSION_FAILED);
574 return result;
575 }
576
577 captureSession_->BeginConfig();
578 result = captureSession_->AddInput(cameraInput_);
579 if (result != CAMERA_OK) {
580 WVLOG_E("Failed to add input");
581 ReportErrorSysEvent(CameraErrorType::ADD_INPUT_FAILED);
582 return result;
583 }
584
585 result = captureSession_->AddOutput(previewOutput_);
586 if (result != CAMERA_OK) {
587 WVLOG_E("Failed to add preview output");
588 ReportErrorSysEvent(CameraErrorType::ADD_OUTPUT_FAILED);
589 return result;
590 }
591 result = captureSession_->CommitConfig();
592 if (result != CAMERA_OK) {
593 WVLOG_E("Failed to commit config");
594 ReportErrorSysEvent(CameraErrorType::COMMIT_CONFIG_FAILED);
595 return result;
596 }
597 result = captureSession_->Start();
598 if (result != CAMERA_OK) {
599 WVLOG_E("Failed to start session");
600 ReportErrorSysEvent(CameraErrorType::START_SESSION_FAILED);
601 return result;
602 }
603 result = CAMERA_OK;
604 status_ = CameraStatusAdapter::UNAVAILABLE;
605 isCapturing_ = true;
606 return result;
607 }
608
RestartSession()609 int32_t CameraManagerAdapterImpl::RestartSession()
610 {
611 std::lock_guard<std::mutex> lock(mutex_);
612 WVLOG_I("RestartSession %{public}s", deviceId_.c_str());
613 if (!isCapturing_) {
614 WVLOG_E("this web tab is not capturing");
615 return CAMERA_OK;
616 }
617
618 if (cameraManager_ == nullptr) {
619 WVLOG_E("cameraManager_ is null when start session");
620 return CAMERA_ERROR;
621 }
622
623 if (cameraInput_ != nullptr) {
624 cameraInput_->Release();
625 cameraInput_ = nullptr;
626 }
627
628 if (previewOutput_ != nullptr) {
629 ((sptr<PreviewOutput>&)previewOutput_)->Stop();
630 previewOutput_->Release();
631 previewOutput_ = nullptr;
632 }
633
634 previewSurface_ = nullptr;
635 previewSurfaceListener_ = nullptr;
636 inputInitedFlag_ = false;
637 captureSession_ = nullptr;
638 status_ = CameraStatusAdapter::AVAILABLE;
639
640 if (StartStreamInner(deviceId_, captureParams_, listener_) != CAMERA_OK) {
641 WVLOG_E("restart stream failed");
642 ReleaseSessionResource(deviceId_);
643 ReleaseSession();
644 return CAMERA_ERROR;
645 }
646 status_ = CameraStatusAdapter::UNAVAILABLE;
647 return CAMERA_OK;
648 }
649
StopSession(CameraStopType stopType)650 int32_t CameraManagerAdapterImpl::StopSession(CameraStopType stopType)
651 {
652 std::lock_guard<std::mutex> lock(mutex_);
653 WVLOG_I("StopSession");
654 if (status_ == CameraStatusAdapter::AVAILABLE) {
655 WVLOG_E("camera is already closed when stop session");
656 return CAMERA_OK;
657 }
658 ReleaseSessionResource(deviceId_);
659 ReleaseSession();
660
661 if (stopType == CameraStopType::NORMAL) {
662 isCapturing_ = false;
663 }
664 return CAMERA_OK;
665 }
666
ReleaseSession()667 int32_t CameraManagerAdapterImpl::ReleaseSession()
668 {
669 WVLOG_I("release session");
670 if (captureSession_ != nullptr) {
671 captureSession_->Stop();
672 captureSession_->Release();
673 captureSession_ = nullptr;
674 }
675
676 return CAMERA_OK;
677 }
678
ReleaseSessionResource(const std::string & deviceId)679 int32_t CameraManagerAdapterImpl::ReleaseSessionResource(const std::string& deviceId)
680 {
681 WVLOG_I("release session resource");
682 if (deviceId_ != deviceId) {
683 WVLOG_E("deviceId is not used");
684 return CAMERA_OK;
685 }
686 if (cameraInput_ != nullptr) {
687 cameraInput_->Release();
688 cameraInput_ = nullptr;
689 }
690
691 if (previewOutput_ != nullptr) {
692 ((sptr<PreviewOutput>&)previewOutput_)->Stop();
693 previewOutput_->Release();
694 previewOutput_ = nullptr;
695 }
696
697 previewSurface_ = nullptr;
698 previewSurfaceListener_ = nullptr;
699 status_ = CameraStatusAdapter::AVAILABLE;
700 inputInitedFlag_ = false;
701 return CAMERA_OK;
702 }
703
ReleaseCameraManger()704 int32_t CameraManagerAdapterImpl::ReleaseCameraManger()
705 {
706 std::lock_guard<std::mutex> lock(mutex_);
707 WVLOG_I("release camera manger");
708 ReleaseSessionResource(deviceId_);
709 ReleaseSession();
710 cameraManager_ = nullptr;
711 status_ = CameraStatusAdapter::AVAILABLE;
712 inputInitedFlag_ = false;
713 isForegound_ = false;
714 cameraMngrCallback_ = nullptr;
715 return CAMERA_OK;
716 }
717
GetCameraStatus()718 CameraStatusAdapter CameraManagerAdapterImpl::GetCameraStatus()
719 {
720 return status_;
721 }
722
SetCameraStatus(CameraStatusAdapter status)723 void CameraManagerAdapterImpl::SetCameraStatus(CameraStatusAdapter status)
724 {
725 std::lock_guard<std::mutex> lock(mutex_);
726 WVLOG_I("set camera status %{public}d", status);
727 status_ = status;
728 }
729
GetCurrentDeviceId()730 std::string CameraManagerAdapterImpl::GetCurrentDeviceId()
731 {
732 return deviceId_;
733 }
734
IsExistCaptureTask()735 bool CameraManagerAdapterImpl::IsExistCaptureTask()
736 {
737 if (cameraManager_ == nullptr) {
738 WVLOG_E("cameraManager_ is nullptr");
739 return false;
740 }
741 return isCapturing_;
742 }
743
SetForegroundFlag(bool isForeground)744 void CameraManagerAdapterImpl::SetForegroundFlag(bool isForeground)
745 {
746 isForegound_ = isForeground;
747 }
748
StartStream(const std::string & deviceId,const std::shared_ptr<VideoCaptureParamsAdapter> captureParams,std::shared_ptr<CameraBufferListenerAdapter> listener)749 int32_t CameraManagerAdapterImpl::StartStream(const std::string& deviceId,
750 const std::shared_ptr<VideoCaptureParamsAdapter> captureParams,
751 std::shared_ptr<CameraBufferListenerAdapter> listener)
752 {
753 std::lock_guard<std::mutex> lock(mutex_);
754 return StartStreamInner(deviceId, captureParams, listener);
755 }
756
StartStreamInner(const std::string & deviceId,const std::shared_ptr<VideoCaptureParamsAdapter> captureParams,std::shared_ptr<CameraBufferListenerAdapter> listener)757 int32_t CameraManagerAdapterImpl::StartStreamInner(const std::string& deviceId,
758 const std::shared_ptr<VideoCaptureParamsAdapter> captureParams,
759 std::shared_ptr<CameraBufferListenerAdapter> listener)
760 {
761 wantedDeviceId_ = deviceId;
762 if ((cameraManager_ == nullptr) || (listener == nullptr)) {
763 WVLOG_E("cameraManager or listener is null when start session");
764 return CAMERA_ERROR;
765 }
766
767 if (captureParams == nullptr) {
768 WVLOG_E("captureParams is null");
769 return CAMERA_ERROR;
770 }
771
772 if (InitCameraInput(deviceId) != CAMERA_OK) {
773 WVLOG_E("init camera input failed");
774 ReleaseSessionResource(deviceId);
775 return CAMERA_ERROR;
776 }
777
778 if (InitPreviewOutput(captureParams, listener) != CAMERA_OK) {
779 WVLOG_E("init camera preview output failed");
780 ReleaseSessionResource(deviceId);
781 return CAMERA_ERROR;
782 }
783
784 if (CreateAndStartSession() != CAMERA_OK) {
785 WVLOG_E("create session failed");
786 ReleaseSession();
787 return CAMERA_ERROR;
788 }
789
790 return CAMERA_OK;
791 }
792
GetCameraDisplayName(const std::string & cameraId,const CameraPosition & position)793 std::string CameraManagerAdapterImpl::GetCameraDisplayName(const std::string& cameraId, const CameraPosition& position)
794 {
795 std::string displayName;
796 switch (position) {
797 case CAMERA_POSITION_FRONT:
798 displayName = cameraId + ", facing front";
799 return displayName;
800 case CAMERA_POSITION_BACK:
801 displayName = cameraId + ", facing back";
802 return displayName;
803 case CAMERA_POSITION_FOLD_INNER:
804 displayName = cameraId + ", facing fold inner";
805 return displayName;
806 default:
807 displayName = cameraId;
808 return displayName;
809 }
810 }
811
CameraSurfaceBufferAdapterImpl(sptr<SurfaceBuffer> buffer)812 CameraSurfaceBufferAdapterImpl::CameraSurfaceBufferAdapterImpl(sptr<SurfaceBuffer> buffer) : buffer_(buffer) {}
813
GetFileDescriptor()814 int32_t CameraSurfaceBufferAdapterImpl::GetFileDescriptor()
815 {
816 if (!buffer_) {
817 WVLOG_E("buffer_ is nullptr");
818 return -1;
819 }
820 return buffer_->GetFileDescriptor();
821 }
822
GetWidth()823 int32_t CameraSurfaceBufferAdapterImpl::GetWidth()
824 {
825 if (!buffer_) {
826 WVLOG_E("buffer_ is nullptr");
827 return -1;
828 }
829 return buffer_->GetWidth();
830 }
831
GetHeight()832 int32_t CameraSurfaceBufferAdapterImpl::GetHeight()
833 {
834 if (!buffer_) {
835 WVLOG_E("buffer_ is nullptr");
836 return -1;
837 }
838 return buffer_->GetHeight();
839 }
840
GetStride()841 int32_t CameraSurfaceBufferAdapterImpl::GetStride()
842 {
843 if (!buffer_) {
844 WVLOG_E("buffer_ is nullptr");
845 return -1;
846 }
847 return buffer_->GetStride();
848 }
849
GetFormat()850 int32_t CameraSurfaceBufferAdapterImpl::GetFormat()
851 {
852 if (!buffer_) {
853 WVLOG_E("buffer_ is nullptr");
854 return -1;
855 }
856 return buffer_->GetFormat();
857 }
858
GetSize()859 uint32_t CameraSurfaceBufferAdapterImpl::GetSize()
860 {
861 if (!buffer_) {
862 WVLOG_E("buffer_ is nullptr");
863 return 0;
864 }
865 return buffer_->GetSize();
866 }
867
GetBuffer()868 sptr<SurfaceBuffer>& CameraSurfaceBufferAdapterImpl::GetBuffer()
869 {
870 return buffer_;
871 }
872
GetBufferAddr()873 uint8_t* CameraSurfaceBufferAdapterImpl::GetBufferAddr()
874 {
875 if (!buffer_) {
876 WVLOG_E("buffer_ is nullptr");
877 return 0;
878 }
879 return static_cast<uint8_t*>(buffer_->GetVirAddr());
880 }
881
CameraSurfaceListener(SurfaceType type,sptr<IConsumerSurface> surface,std::shared_ptr<CameraBufferListenerAdapter> listener)882 CameraSurfaceListener::CameraSurfaceListener(
883 SurfaceType type, sptr<IConsumerSurface> surface, std::shared_ptr<CameraBufferListenerAdapter> listener)
884 : surfaceType_(type), surface_(surface), listener_(listener)
885 {}
886
GetScreenRotation()887 int32_t CameraSurfaceListener::GetScreenRotation()
888 {
889 sptr<OHOS::Rosen::Display> display = OHOS::Rosen::DisplayManager::GetInstance().GetDefaultDisplaySync();
890 if (display == nullptr) {
891 WVLOG_E("Get display manager failed, rotation maybe incorrect.");
892 return 0;
893 }
894 auto displayRotation = display->GetRotation();
895 int32_t screenRotation = 0;
896 switch (displayRotation) {
897 case OHOS::Rosen::Rotation::ROTATION_0:
898 screenRotation = ROTATION_0;
899 break;
900 case OHOS::Rosen::Rotation::ROTATION_90:
901 screenRotation = ROTATION_90;
902 break;
903 case OHOS::Rosen::Rotation::ROTATION_180:
904 screenRotation = ROTATION_180;
905 break;
906 case OHOS::Rosen::Rotation::ROTATION_270:
907 screenRotation = ROTATION_270;
908 break;
909 default:
910 WVLOG_E("Get invalid displayRotation");
911 break;
912 }
913 return screenRotation;
914 }
915
GetPictureRotation()916 int32_t CameraSurfaceListener::GetPictureRotation()
917 {
918 int32_t screenRotation = GetScreenRotation();
919 std::string currentDeviceId = CameraManagerAdapterImpl::GetInstance().GetCurrentDeviceId();
920 sptr<CameraDevice> cameraObj = CameraManager::GetInstance()->GetCameraDeviceFromId(currentDeviceId);
921 if (cameraObj == nullptr) {
922 WVLOG_E("cameraObj is nullptr");
923 return screenRotation;
924 }
925 int32_t cameraOrientation = static_cast<int32_t>(cameraObj->GetCameraOrientation());
926 auto cameraPosition = cameraObj->GetPosition(); // 1: back, 2: front
927
928 int32_t pictureRotation = 0;
929 if (cameraPosition == OHOS::CameraStandard::CameraPosition::CAMERA_POSITION_FRONT) {
930 pictureRotation = (cameraOrientation - screenRotation) % ROTATION_MAX;
931 } else {
932 pictureRotation = (cameraOrientation + screenRotation) % ROTATION_MAX;
933 }
934
935 WVLOG_D("GetPictureRotation, cameraOrientation:%{public}d, screenRotation:%{public}d, pictureRotation:%{public}d",
936 cameraOrientation, screenRotation, pictureRotation);
937 return pictureRotation;
938 }
939
IsNeedCorrectRotation()940 bool CameraSurfaceListener::IsNeedCorrectRotation()
941 {
942 return true;
943 }
944
FillRotationInfo(int32_t rotation,bool isFlipX,bool isFlipY)945 std::shared_ptr<CameraRotationInfoAdapter> CameraSurfaceListener::FillRotationInfo(int32_t rotation,
946 bool isFlipX, bool isFlipY)
947 {
948 std::shared_ptr<CameraRotationInfoAdapterImpl> rotationInfo = std::make_shared<CameraRotationInfoAdapterImpl>();
949 if (!rotationInfo) {
950 WVLOG_E("new CameraRotationInfo failed");
951 return nullptr;
952 }
953
954 if (IsNeedCorrectRotation()) {
955 rotation = GetPictureRotation();
956 }
957 rotationInfo->SetRotation(rotation);
958 rotationInfo->SetIsFlipX(isFlipX);
959 rotationInfo->SetIsFlipY(isFlipY);
960 return rotationInfo;
961 }
962
GetRotationInfo(GraphicTransformType transform)963 std::shared_ptr<CameraRotationInfoAdapter> CameraSurfaceListener::GetRotationInfo(GraphicTransformType transform)
964 {
965 switch (transform) {
966 case GraphicTransformType::GRAPHIC_ROTATE_NONE: {
967 return FillRotationInfo(ROTATION_0, false, false);
968 }
969 case GraphicTransformType::GRAPHIC_ROTATE_90: {
970 return FillRotationInfo(ROTATION_90, false, false);
971 }
972 case GraphicTransformType::GRAPHIC_ROTATE_180: {
973 return FillRotationInfo(ROTATION_180, false, false);
974 }
975 case GraphicTransformType::GRAPHIC_ROTATE_270: {
976 return FillRotationInfo(ROTATION_270, false, false);
977 }
978 case GraphicTransformType::GRAPHIC_FLIP_H: {
979 return FillRotationInfo(ROTATION_0, false, true);
980 }
981 case GraphicTransformType::GRAPHIC_FLIP_V: {
982 return FillRotationInfo(ROTATION_0, true, false);
983 }
984 case GraphicTransformType::GRAPHIC_FLIP_H_ROT90: {
985 return FillRotationInfo(ROTATION_90, false, true);
986 }
987 case GraphicTransformType::GRAPHIC_FLIP_V_ROT90: {
988 return FillRotationInfo(ROTATION_90, true, false);
989 }
990 case GraphicTransformType::GRAPHIC_FLIP_H_ROT180: {
991 return FillRotationInfo(ROTATION_180, false, true);
992 }
993 case GraphicTransformType::GRAPHIC_FLIP_V_ROT180: {
994 return FillRotationInfo(ROTATION_180, true, false);
995 }
996 case GraphicTransformType::GRAPHIC_FLIP_H_ROT270: {
997 return FillRotationInfo(ROTATION_270, false, true);
998 }
999 case GraphicTransformType::GRAPHIC_FLIP_V_ROT270: {
1000 return FillRotationInfo(ROTATION_270, true, false);
1001 }
1002 default: {
1003 return FillRotationInfo(ROTATION_0, false, false);
1004 }
1005 }
1006 }
1007
OnBufferAvailable()1008 void CameraSurfaceListener::OnBufferAvailable()
1009 {
1010 int32_t flushFence = 0;
1011 int64_t timestamp = 0;
1012 OHOS::Rect damage;
1013 OHOS::sptr<OHOS::SurfaceBuffer> buffer = nullptr;
1014 if (surface_ == nullptr) {
1015 WVLOG_E("OnBufferAvailable:surface_ is null");
1016 return;
1017 }
1018 surface_->AcquireBuffer(buffer, flushFence, timestamp, damage);
1019 if (buffer != nullptr) {
1020 uint32_t size = buffer->GetSize();
1021
1022 std::shared_ptr<CameraRotationInfoAdapter> rotationInfo = GetRotationInfo(surface_->GetTransform());
1023 if (!rotationInfo) {
1024 WVLOG_E("rotationInfo is null");
1025 return;
1026 }
1027
1028 WVLOG_D("OnBufferAvailable, surfaceType_: %{public}d, size: %{public}d, width: %{public}d,\
1029 height: %{public}d, type: %{public}d, ratation: %{public}d, FilyY: %{public}d, FilyX: %{public}d",
1030 surfaceType_, size, buffer->GetWidth(), buffer->GetHeight(), surface_->GetTransform(),
1031 (int32_t)rotationInfo->GetRotation(), rotationInfo->GetIsFlipY(), rotationInfo->GetIsFlipX());
1032 auto bufferAdapter = std::make_shared<CameraSurfaceBufferAdapterImpl>(buffer);
1033 auto surfaceAdapter = std::make_shared<CameraSurfaceAdapterImpl>(surface_);
1034 if (listener_ != nullptr) {
1035 listener_->OnBufferAvailable(surfaceAdapter, std::move(bufferAdapter), std::move(rotationInfo));
1036 }
1037 } else {
1038 WVLOG_E("AcquireBuffer failed!");
1039 }
1040 }
1041
CameraSurfaceAdapterImpl(sptr<IConsumerSurface> surface)1042 CameraSurfaceAdapterImpl::CameraSurfaceAdapterImpl(sptr<IConsumerSurface> surface) : cSurface_(surface) {}
1043
ReleaseBuffer(std::shared_ptr<CameraSurfaceBufferAdapter> bufferAdapter,int32_t fence)1044 int32_t CameraSurfaceAdapterImpl::ReleaseBuffer(
1045 std::shared_ptr<CameraSurfaceBufferAdapter> bufferAdapter, int32_t fence)
1046 {
1047 if (!cSurface_ || !bufferAdapter) {
1048 WVLOG_E("cSurface_ or bufferAdapter is nullptr");
1049 return -1;
1050 }
1051 auto bufferImpl = static_cast<CameraSurfaceBufferAdapterImpl*>(bufferAdapter.get());
1052 return cSurface_->ReleaseBuffer(bufferImpl->GetBuffer(), fence);
1053 }
1054
CameraManagerAdapterCallback(std::shared_ptr<CameraStatusCallbackAdapter> cameraStatusCallback)1055 CameraManagerAdapterCallback::CameraManagerAdapterCallback(
1056 std::shared_ptr<CameraStatusCallbackAdapter> cameraStatusCallback)
1057 : statusCallback_(cameraStatusCallback)
1058 {
1059 WVLOG_I("Create CameraManagerAdapterCallback");
1060 }
1061
GetAdapterCameraStatus(CameraStatus status) const1062 CameraStatusAdapter CameraManagerAdapterCallback::GetAdapterCameraStatus(CameraStatus status) const
1063 {
1064 auto item = CAMERA_STATUS_MAP.find(status);
1065 if (item == CAMERA_STATUS_MAP.end()) {
1066 WVLOG_E("ori camera status %{public}d not found", status);
1067 return CameraStatusAdapter::APPEAR;
1068 }
1069 return item->second;
1070 }
1071
OnCameraStatusChanged(const CameraStatusInfo & cameraStatusInfo) const1072 void CameraManagerAdapterCallback::OnCameraStatusChanged(const CameraStatusInfo& cameraStatusInfo) const
1073 {
1074 std::string callbackDeviceId;
1075 if (cameraStatusInfo.cameraDevice) {
1076 callbackDeviceId = cameraStatusInfo.cameraDevice->GetID();
1077 }
1078 std::string currentDeviceId = CameraManagerAdapterImpl::GetInstance().GetCurrentDeviceId();
1079
1080 WVLOG_I("OnCameraStatusChanged: callbackdeviceID %{public}s, currentDeviceId:%{public}s, status %{public}d",
1081 callbackDeviceId.c_str(), currentDeviceId.c_str(), cameraStatusInfo.cameraStatus);
1082 CameraStatusAdapter cameraStatusAdapter = GetAdapterCameraStatus(cameraStatusInfo.cameraStatus);
1083
1084 if (statusCallback_) {
1085 switch (cameraStatusAdapter) {
1086 case CameraStatusAdapter::AVAILABLE:
1087 WVLOG_I("do not handle status AVAILABLE");
1088 return;
1089 case CameraStatusAdapter::UNAVAILABLE:
1090 WVLOG_I("do not handle status UNAVAILABLE");
1091 return;
1092 case CameraStatusAdapter::APPEAR:
1093 case CameraStatusAdapter::DISAPPEAR:
1094 break;
1095 default:
1096 WVLOG_I("unknow status");
1097 return;
1098 }
1099 WVLOG_I("start do statusCallback");
1100 statusCallback_->OnCameraStatusChanged(cameraStatusAdapter, callbackDeviceId);
1101 }
1102 return;
1103 }
1104
OnFlashlightStatusChanged(const std::string & cameraID,const FlashStatus flashStatus) const1105 void CameraManagerAdapterCallback::OnFlashlightStatusChanged(
1106 const std::string& cameraID, const FlashStatus flashStatus) const
1107 {
1108 return;
1109 }
1110 } // namespace OHOS::NWeb
1111