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         deviceDisc->SetDisplayName(cameraObj->GetID());
296         deviceDisc->SetDeviceId(cameraObj->GetID());
297         deviceDisc->SetModelId(cameraObj->GetID());
298 
299         std::shared_ptr<VideoControlSupportAdapterImpl> controlSupport =
300             std::make_shared<VideoControlSupportAdapterImpl>();
301         if (!controlSupport) {
302             WVLOG_E("new VideoControlSupportAdapter failed");
303             return devicesDiscriptor;
304         }
305 
306         controlSupport->SetPan(false);
307         controlSupport->SetTilt(false);
308         controlSupport->SetZoom(false);
309         deviceDisc->SetControlSupport(controlSupport);
310 
311         deviceDisc->SetTransportType(GetCameraTransportType(cameraObj->GetConnectionType()));
312         deviceDisc->SetFacingMode(GetCameraFacingMode(cameraObj->GetPosition()));
313 
314         deviceDisc->SetSupportCaptureFormats(GetCameraSupportFormats(outputcapability));
315         WVLOG_I("deviceDisc  id:%{public}s, control pan:%{public}d tilt:%{public}d, zoom:%{public}d \
316             transType:%{public}d, facingMode:%{public}d",
317             deviceDisc->GetDeviceId().c_str(), deviceDisc->GetControlSupport()->GetPan(),
318             deviceDisc->GetControlSupport()->GetTilt(), deviceDisc->GetControlSupport()->GetZoom(),
319             deviceDisc->GetTransportType(), deviceDisc->GetFacingMode());
320         devicesDiscriptor.emplace_back(std::move(deviceDisc));
321     }
322     return devicesDiscriptor;
323 }
324 
InitCameraInput(const std::string & deviceId)325 int32_t CameraManagerAdapterImpl::InitCameraInput(const std::string& deviceId)
326 {
327     int32_t result = CAMERA_ERROR;
328 
329     if (status_ == CameraStatusAdapter::UNAVAILABLE) {
330         WVLOG_E("camera is not closed");
331         ReportErrorSysEvent(CameraErrorType::CAMERA_NOT_CLOSE);
332         return result;
333     }
334 
335     if (inputInitedFlag_) {
336         WVLOG_E("input is already inited");
337         ReportErrorSysEvent(CameraErrorType::INPUT_ALREADY_INIT);
338         return result;
339     }
340 
341     if (cameraManager_ == nullptr) {
342         WVLOG_E("camera manager is nullptr");
343         ReportErrorSysEvent(CameraErrorType::CAMERA_MANAGER_IS_NULL);
344         return CAMERA_NULL_ERROR;
345     }
346 
347     if (cameraInput_ == nullptr) {
348         WVLOG_I("camera input create %{public}s", deviceId.c_str());
349         sptr<CameraDevice> cameraObj = cameraManager_->GetCameraDeviceFromId(deviceId);
350         if (cameraObj == nullptr) {
351             WVLOG_E("No cameras are available!!!");
352             ReportErrorSysEvent(CameraErrorType::GET_CAMERA_OBJ_FAILED);
353             return CAMERA_NULL_ERROR;
354         }
355 
356         cameraInput_ = cameraManager_->CreateCameraInput(cameraObj);
357         if (cameraInput_ == nullptr) {
358             WVLOG_E("Failed to create CameraInput");
359             ReportErrorSysEvent(CameraErrorType::CREATE_INPUT_FAILED);
360             return result;
361         }
362         int32_t ret = cameraInput_->Open();
363         if (ret != CAMERA_OK) {
364             WVLOG_E("Failed to open CameraInput, err code %{public}d.", ret);
365             cameraInput_->Release();
366             cameraInput_ = nullptr;
367             ReportErrorSysEvent(CameraErrorType::INPUT_OPEN_FAILED);
368             return result;
369         }
370         deviceId_ = deviceId;
371         inputInitedFlag_ = true;
372     }
373 
374     result = CAMERA_OK;
375     return result;
376 }
377 
InitPreviewOutput(const std::shared_ptr<VideoCaptureParamsAdapter> captureParams,std::shared_ptr<CameraBufferListenerAdapter> listener)378 int32_t CameraManagerAdapterImpl::InitPreviewOutput(const std::shared_ptr<VideoCaptureParamsAdapter> captureParams,
379     std::shared_ptr<CameraBufferListenerAdapter> listener)
380 {
381     int32_t result = CAMERA_ERROR;
382     Size previewSize;
383 
384     if (!inputInitedFlag_) {
385         WVLOG_E("input is not inited");
386         ReportErrorSysEvent(CameraErrorType::INPUT_NOT_INIT);
387         return result;
388     }
389 
390     if (cameraManager_ == nullptr) {
391         WVLOG_E("camera manager is null");
392         ReportErrorSysEvent(CameraErrorType::CAMERA_MANAGER_IS_NULL);
393         return result;
394     }
395 
396     if (previewOutput_ == nullptr) {
397         WVLOG_I("preview output create");
398         previewSurface_ = IConsumerSurface::Create();
399         if (previewSurface_ == nullptr) {
400             WVLOG_E("previewSurface_ is null");
401             ReportErrorSysEvent(CameraErrorType::CREATE_PREVIEW_SURFACE_FAILED);
402             return result;
403         }
404         previewSize.width = captureParams->GetWidth();
405         previewSize.height = captureParams->GetHeight();
406         previewSurface_->SetDefaultUsage(BUFFER_USAGE_CPU_READ);
407         previewSurface_->SetDefaultWidthAndHeight(previewSize.width, previewSize.height);
408         previewSurface_->SetUserData(
409             CameraManager::surfaceFormat, std::to_string(TransToOriCameraFormat(captureParams->GetPixelFormat())));
410         Profile previewproFile =
411             Profile(static_cast<CameraFormat>(TransToOriCameraFormat(captureParams->GetPixelFormat())), previewSize);
412         WVLOG_I("preview output format: %{public}d, w: %{public}d, h: %{public}d",
413             TransToOriCameraFormat(captureParams->GetPixelFormat()), previewSize.width, previewSize.height);
414         previewSurfaceListener_ =
415             new (std::nothrow) CameraSurfaceListener(SurfaceType::PREVIEW, previewSurface_, (listener));
416         previewSurface_->RegisterConsumerListener((sptr<IBufferConsumerListener>&)previewSurfaceListener_);
417         sptr<IBufferProducer> bp = previewSurface_->GetProducer();
418         sptr<Surface> pSurface = Surface::CreateSurfaceAsProducer(bp);
419         previewOutput_ = cameraManager_->CreatePreviewOutput(previewproFile, pSurface);
420         if (previewOutput_ == nullptr) {
421             WVLOG_E("Failed to create previewOutput");
422             ReportErrorSysEvent(CameraErrorType::CREATE_PREVIEW_OUTPUT_FAILED);
423             return result;
424         }
425         captureParams_ = captureParams;
426         listener_ = listener;
427     }
428     result = CAMERA_OK;
429     return result;
430 }
431 
TransToAdapterExposureModes(std::vector<ExposureMode> & exposureModes,std::vector<ExposureModeAdapter> & exposureModesAdapter)432 int32_t CameraManagerAdapterImpl::TransToAdapterExposureModes(
433     std::vector<ExposureMode>& exposureModes, std::vector<ExposureModeAdapter>& exposureModesAdapter)
434 {
435     for (auto exportMode : exposureModes) {
436         exposureModesAdapter.push_back(GetAdapterExposureMode(exportMode));
437     }
438 
439     return CAMERA_OK;
440 }
441 
GetExposureModes(std::vector<ExposureModeAdapter> & exposureModesAdapter)442 int32_t CameraManagerAdapterImpl::GetExposureModes(std::vector<ExposureModeAdapter>& exposureModesAdapter)
443 {
444     std::lock_guard<std::mutex> lock(mutex_);
445     if (captureSession_ == nullptr) {
446         WVLOG_E("captureSession is nullptr when get exposure modes");
447         return CAMERA_ERROR;
448     }
449     std::vector<ExposureMode> exposureModes;
450     if (captureSession_->GetSupportedExposureModes(exposureModes) != SUCCESS) {
451         WVLOG_E("get inner exposure modes faileds");
452         return CAMERA_ERROR;
453     }
454 
455     TransToAdapterExposureModes(exposureModes, exposureModesAdapter);
456 
457     return CAMERA_OK;
458 }
459 
GetCurrentExposureMode(ExposureModeAdapter & exposureModeAdapter)460 int32_t CameraManagerAdapterImpl::GetCurrentExposureMode(ExposureModeAdapter& exposureModeAdapter)
461 {
462     std::lock_guard<std::mutex> lock(mutex_);
463     if (captureSession_ == nullptr) {
464         WVLOG_E("captureSession is nullptr when get current exposure modes");
465         return CAMERA_ERROR;
466     }
467 
468     ExposureMode exposureMode = captureSession_->GetExposureMode();
469     exposureModeAdapter = GetAdapterExposureMode(exposureMode);
470     return CAMERA_OK;
471 }
472 
GetExposureCompensation()473 std::shared_ptr<VideoCaptureRangeAdapter> CameraManagerAdapterImpl::GetExposureCompensation()
474 {
475     if (captureSession_ == nullptr) {
476         return nullptr;
477     }
478 
479     std::shared_ptr<VideoCaptureRangeAdapterImpl> rangeVal = std::make_shared<VideoCaptureRangeAdapterImpl>();
480     if (!rangeVal) {
481         WVLOG_E("new VideoCaptureRangeAdapter failed");
482         return nullptr;
483     }
484 
485     std::vector<float> exposureBiasRange = captureSession_->GetExposureBiasRange();
486     int32_t exposureCompos = captureSession_->GetExposureValue();
487     if (exposureBiasRange.size() == RANGE_MAX_SIZE) {
488         rangeVal->SetMin(exposureBiasRange.at(RANGE_MIN_INDEX));
489         rangeVal->SetMax(exposureBiasRange.at(RANGE_MAX_INDEX));
490     }
491 
492     rangeVal->SetCurrent(exposureCompos);
493     return rangeVal;
494 }
495 
GetCaptionRangeById(RangeIDAdapter rangeId)496 std::shared_ptr<VideoCaptureRangeAdapter> CameraManagerAdapterImpl::GetCaptionRangeById(RangeIDAdapter rangeId)
497 {
498     std::lock_guard<std::mutex> lock(mutex_);
499     if (captureSession_ == nullptr) {
500         WVLOG_E("captureSession is nullptr when get %{public}d range info", rangeId);
501         return nullptr;
502     }
503 
504     std::shared_ptr<VideoCaptureRangeAdapter> result = nullptr;
505     if (rangeId == RangeIDAdapter::RANGE_ID_EXP_COMPENSATION) {
506         result = GetExposureCompensation();
507         if (!result) {
508             WVLOG_E("get exposure compensation failed.");
509         }
510     }
511 
512     return result;
513 }
514 
IsFocusModeSupported(FocusModeAdapter focusMode)515 bool CameraManagerAdapterImpl::IsFocusModeSupported(FocusModeAdapter focusMode)
516 {
517     std::lock_guard<std::mutex> lock(mutex_);
518     if (captureSession_ == nullptr) {
519         WVLOG_E("captureSession is nullptr when get support focuc mode");
520         return false;
521     }
522     if (!captureSession_->IsFocusModeSupported(GetOriFocusMode(focusMode))) {
523         return false;
524     }
525     return true;
526 }
527 
GetCurrentFocusMode()528 FocusModeAdapter CameraManagerAdapterImpl::GetCurrentFocusMode()
529 {
530     std::lock_guard<std::mutex> lock(mutex_);
531     if (captureSession_ == nullptr) {
532         WVLOG_E("captureSession is nullptr when get support focuc mode");
533         return FocusModeAdapter::FOCUS_MODE_MANUAL;
534     }
535 
536     FocusMode oriFocusMode = captureSession_->GetFocusMode();
537     return GetAdapterFocusMode(oriFocusMode);
538 }
539 
IsFlashModeSupported(FlashModeAdapter focusMode)540 bool CameraManagerAdapterImpl::IsFlashModeSupported(FlashModeAdapter focusMode)
541 {
542     std::lock_guard<std::mutex> lock(mutex_);
543     if (captureSession_ == nullptr) {
544         WVLOG_E("captureSession is nullptr when get support flash mode");
545         return false;
546     }
547     if (!captureSession_->IsFlashModeSupported(GetOriFlashMode(focusMode))) {
548         return false;
549     }
550     return true;
551 }
552 
CreateAndStartSession()553 int32_t CameraManagerAdapterImpl::CreateAndStartSession()
554 {
555     int32_t result = CAMERA_ERROR;
556     if (status_ == CameraStatusAdapter::UNAVAILABLE) {
557         WVLOG_E("camera is already opened");
558         ReportErrorSysEvent(CameraErrorType::CAMERA_NOT_CLOSE);
559         return result;
560     }
561 
562     if ((cameraInput_ == nullptr) || (previewOutput_ == nullptr)) {
563         WVLOG_E("cameraInput_ or previewOutput_ is null");
564         ReportErrorSysEvent(CameraErrorType::INPUT_OR_OUTPUT_IS_NULL);
565         return result;
566     }
567 
568     WVLOG_I("CreateCaptureSession");
569     captureSession_ = cameraManager_->CreateCaptureSession();
570     if (captureSession_ == nullptr) {
571         WVLOG_E("Failed to create capture session");
572         ReportErrorSysEvent(CameraErrorType::CREATE_CAPTURE_SESSION_FAILED);
573         return result;
574     }
575 
576     captureSession_->BeginConfig();
577     result = captureSession_->AddInput(cameraInput_);
578     if (result != CAMERA_OK) {
579         WVLOG_E("Failed to add input");
580         ReportErrorSysEvent(CameraErrorType::ADD_INPUT_FAILED);
581         return result;
582     }
583 
584     result = captureSession_->AddOutput(previewOutput_);
585     if (result != CAMERA_OK) {
586         WVLOG_E("Failed to add preview output");
587         ReportErrorSysEvent(CameraErrorType::ADD_OUTPUT_FAILED);
588         return result;
589     }
590     result = captureSession_->CommitConfig();
591     if (result != CAMERA_OK) {
592         WVLOG_E("Failed to commit config");
593         ReportErrorSysEvent(CameraErrorType::COMMIT_CONFIG_FAILED);
594         return result;
595     }
596     result = captureSession_->Start();
597     if (result != CAMERA_OK) {
598         WVLOG_E("Failed to start session");
599         ReportErrorSysEvent(CameraErrorType::START_SESSION_FAILED);
600         return result;
601     }
602     result = CAMERA_OK;
603     status_ = CameraStatusAdapter::UNAVAILABLE;
604     isCapturing_ = true;
605     return result;
606 }
607 
RestartSession()608 int32_t CameraManagerAdapterImpl::RestartSession()
609 {
610     std::lock_guard<std::mutex> lock(mutex_);
611     WVLOG_I("RestartSession %{public}s", deviceId_.c_str());
612     if (!isCapturing_) {
613         WVLOG_E("this web tab is not capturing");
614         return CAMERA_OK;
615     }
616 
617     if (cameraManager_ == nullptr) {
618         WVLOG_E("cameraManager_ is null when start session");
619         return CAMERA_ERROR;
620     }
621 
622     if (cameraInput_ != nullptr) {
623         cameraInput_->Release();
624         cameraInput_ = nullptr;
625     }
626 
627     if (previewOutput_ != nullptr) {
628         ((sptr<PreviewOutput>&)previewOutput_)->Stop();
629         previewOutput_->Release();
630         previewOutput_ = nullptr;
631     }
632 
633     previewSurface_ = nullptr;
634     previewSurfaceListener_ = nullptr;
635     inputInitedFlag_ = false;
636     captureSession_ = nullptr;
637     status_ = CameraStatusAdapter::AVAILABLE;
638 
639     if (StartStreamInner(deviceId_, captureParams_, listener_) != CAMERA_OK) {
640         WVLOG_E("restart stream failed");
641         ReleaseSessionResource(deviceId_);
642         ReleaseSession();
643         return CAMERA_ERROR;
644     }
645     status_ = CameraStatusAdapter::UNAVAILABLE;
646     return CAMERA_OK;
647 }
648 
StopSession(CameraStopType stopType)649 int32_t CameraManagerAdapterImpl::StopSession(CameraStopType stopType)
650 {
651     std::lock_guard<std::mutex> lock(mutex_);
652     WVLOG_I("StopSession");
653     if (status_ == CameraStatusAdapter::AVAILABLE) {
654         WVLOG_E("camera is already closed when stop session");
655         return CAMERA_OK;
656     }
657     ReleaseSessionResource(deviceId_);
658     ReleaseSession();
659 
660     if (stopType == CameraStopType::NORMAL) {
661         isCapturing_ = false;
662     }
663     return CAMERA_OK;
664 }
665 
ReleaseSession()666 int32_t CameraManagerAdapterImpl::ReleaseSession()
667 {
668     WVLOG_I("release session");
669     if (captureSession_ != nullptr) {
670         captureSession_->Stop();
671         captureSession_->Release();
672         captureSession_ = nullptr;
673     }
674 
675     return CAMERA_OK;
676 }
677 
ReleaseSessionResource(const std::string & deviceId)678 int32_t CameraManagerAdapterImpl::ReleaseSessionResource(const std::string& deviceId)
679 {
680     WVLOG_I("release session resource");
681     if (deviceId_ != deviceId) {
682         WVLOG_E("deviceId is not used");
683         return CAMERA_OK;
684     }
685     if (cameraInput_ != nullptr) {
686         cameraInput_->Release();
687         cameraInput_ = nullptr;
688     }
689 
690     if (previewOutput_ != nullptr) {
691         ((sptr<PreviewOutput>&)previewOutput_)->Stop();
692         previewOutput_->Release();
693         previewOutput_ = nullptr;
694     }
695 
696     previewSurface_ = nullptr;
697     previewSurfaceListener_ = nullptr;
698     status_ = CameraStatusAdapter::AVAILABLE;
699     inputInitedFlag_ = false;
700     return CAMERA_OK;
701 }
702 
ReleaseCameraManger()703 int32_t CameraManagerAdapterImpl::ReleaseCameraManger()
704 {
705     std::lock_guard<std::mutex> lock(mutex_);
706     WVLOG_I("release camera manger");
707     ReleaseSessionResource(deviceId_);
708     ReleaseSession();
709     cameraManager_ = nullptr;
710     status_ = CameraStatusAdapter::AVAILABLE;
711     inputInitedFlag_ = false;
712     isForegound_ = false;
713     cameraMngrCallback_ = nullptr;
714     return CAMERA_OK;
715 }
716 
GetCameraStatus()717 CameraStatusAdapter CameraManagerAdapterImpl::GetCameraStatus()
718 {
719     return status_;
720 }
721 
SetCameraStatus(CameraStatusAdapter status)722 void CameraManagerAdapterImpl::SetCameraStatus(CameraStatusAdapter status)
723 {
724     std::lock_guard<std::mutex> lock(mutex_);
725     WVLOG_I("set camera status %{public}d", status);
726     status_ = status;
727 }
728 
GetCurrentDeviceId()729 std::string CameraManagerAdapterImpl::GetCurrentDeviceId()
730 {
731     return deviceId_;
732 }
733 
IsExistCaptureTask()734 bool CameraManagerAdapterImpl::IsExistCaptureTask()
735 {
736     if (cameraManager_ == nullptr) {
737         WVLOG_E("cameraManager_ is nullptr");
738         return false;
739     }
740     return isCapturing_;
741 }
742 
SetForegroundFlag(bool isForeground)743 void CameraManagerAdapterImpl::SetForegroundFlag(bool isForeground)
744 {
745     isForegound_ = isForeground;
746 }
747 
StartStream(const std::string & deviceId,const std::shared_ptr<VideoCaptureParamsAdapter> captureParams,std::shared_ptr<CameraBufferListenerAdapter> listener)748 int32_t CameraManagerAdapterImpl::StartStream(const std::string& deviceId,
749     const std::shared_ptr<VideoCaptureParamsAdapter> captureParams,
750     std::shared_ptr<CameraBufferListenerAdapter> listener)
751 {
752     std::lock_guard<std::mutex> lock(mutex_);
753     return StartStreamInner(deviceId, captureParams, listener);
754 }
755 
StartStreamInner(const std::string & deviceId,const std::shared_ptr<VideoCaptureParamsAdapter> captureParams,std::shared_ptr<CameraBufferListenerAdapter> listener)756 int32_t CameraManagerAdapterImpl::StartStreamInner(const std::string& deviceId,
757     const std::shared_ptr<VideoCaptureParamsAdapter> captureParams,
758     std::shared_ptr<CameraBufferListenerAdapter> listener)
759 {
760     wantedDeviceId_ = deviceId;
761     if ((cameraManager_ == nullptr) || (listener == nullptr)) {
762         WVLOG_E("cameraManager or listener is null when start session");
763         return CAMERA_ERROR;
764     }
765 
766     if (captureParams == nullptr) {
767         WVLOG_E("captureParams is null");
768         return CAMERA_ERROR;
769     }
770 
771     if (InitCameraInput(deviceId) != CAMERA_OK) {
772         WVLOG_E("init camera input failed");
773         ReleaseSessionResource(deviceId);
774         return CAMERA_ERROR;
775     }
776 
777     if (InitPreviewOutput(captureParams, listener) != CAMERA_OK) {
778         WVLOG_E("init camera preview output failed");
779         ReleaseSessionResource(deviceId);
780         return CAMERA_ERROR;
781     }
782 
783     if (CreateAndStartSession() != CAMERA_OK) {
784         WVLOG_E("create session failed");
785         ReleaseSession();
786         return CAMERA_ERROR;
787     }
788 
789     return CAMERA_OK;
790 }
791 
CameraSurfaceBufferAdapterImpl(sptr<SurfaceBuffer> buffer)792 CameraSurfaceBufferAdapterImpl::CameraSurfaceBufferAdapterImpl(sptr<SurfaceBuffer> buffer) : buffer_(buffer) {}
793 
GetFileDescriptor()794 int32_t CameraSurfaceBufferAdapterImpl::GetFileDescriptor()
795 {
796     if (!buffer_) {
797         WVLOG_E("buffer_ is nullptr");
798         return -1;
799     }
800     return buffer_->GetFileDescriptor();
801 }
802 
GetWidth()803 int32_t CameraSurfaceBufferAdapterImpl::GetWidth()
804 {
805     if (!buffer_) {
806         WVLOG_E("buffer_ is nullptr");
807         return -1;
808     }
809     return buffer_->GetWidth();
810 }
811 
GetHeight()812 int32_t CameraSurfaceBufferAdapterImpl::GetHeight()
813 {
814     if (!buffer_) {
815         WVLOG_E("buffer_ is nullptr");
816         return -1;
817     }
818     return buffer_->GetHeight();
819 }
820 
GetStride()821 int32_t CameraSurfaceBufferAdapterImpl::GetStride()
822 {
823     if (!buffer_) {
824         WVLOG_E("buffer_ is nullptr");
825         return -1;
826     }
827     return buffer_->GetStride();
828 }
829 
GetFormat()830 int32_t CameraSurfaceBufferAdapterImpl::GetFormat()
831 {
832     if (!buffer_) {
833         WVLOG_E("buffer_ is nullptr");
834         return -1;
835     }
836     return buffer_->GetFormat();
837 }
838 
GetSize()839 uint32_t CameraSurfaceBufferAdapterImpl::GetSize()
840 {
841     if (!buffer_) {
842         WVLOG_E("buffer_ is nullptr");
843         return 0;
844     }
845     return buffer_->GetSize();
846 }
847 
GetBuffer()848 sptr<SurfaceBuffer>& CameraSurfaceBufferAdapterImpl::GetBuffer()
849 {
850     return buffer_;
851 }
852 
GetBufferAddr()853 uint8_t* CameraSurfaceBufferAdapterImpl::GetBufferAddr()
854 {
855     if (!buffer_) {
856         WVLOG_E("buffer_ is nullptr");
857         return 0;
858     }
859     return static_cast<uint8_t*>(buffer_->GetVirAddr());
860 }
861 
CameraSurfaceListener(SurfaceType type,sptr<IConsumerSurface> surface,std::shared_ptr<CameraBufferListenerAdapter> listener)862 CameraSurfaceListener::CameraSurfaceListener(
863     SurfaceType type, sptr<IConsumerSurface> surface, std::shared_ptr<CameraBufferListenerAdapter> listener)
864     : surfaceType_(type), surface_(surface), listener_(listener)
865 {}
866 
GetScreenRotation()867 int32_t CameraSurfaceListener::GetScreenRotation()
868 {
869     sptr<OHOS::Rosen::Display> display = OHOS::Rosen::DisplayManager::GetInstance().GetDefaultDisplaySync();
870     if (display == nullptr) {
871         WVLOG_E("Get display manager failed, rotation maybe incorrect.");
872         return 0;
873     }
874     auto displayRotation = display->GetRotation();
875     int32_t screenRotation = 0;
876     switch (displayRotation) {
877         case OHOS::Rosen::Rotation::ROTATION_0:
878             screenRotation = ROTATION_0;
879             break;
880         case OHOS::Rosen::Rotation::ROTATION_90:
881             screenRotation = ROTATION_90;
882             break;
883         case OHOS::Rosen::Rotation::ROTATION_180:
884             screenRotation = ROTATION_180;
885             break;
886         case OHOS::Rosen::Rotation::ROTATION_270:
887             screenRotation = ROTATION_270;
888             break;
889         default:
890             WVLOG_E("Get invalid displayRotation");
891             break;
892     }
893     // current tablet screen rotation is different from of other devices.
894     // when display framework modify this problem, we need to delete this transform
895     std::string deviceType = OHOS::system::GetDeviceType();
896     if (deviceType == "tablet") {
897         screenRotation = (screenRotation + ROTATION_270) % ROTATION_MAX;
898     }
899     return screenRotation;
900 }
901 
GetPictureRotation()902 int32_t CameraSurfaceListener::GetPictureRotation()
903 {
904     int32_t screenRotation = GetScreenRotation();
905     std::string currentDeviceId = CameraManagerAdapterImpl::GetInstance().GetCurrentDeviceId();
906     sptr<CameraDevice> cameraObj = CameraManager::GetInstance()->GetCameraDeviceFromId(currentDeviceId);
907     if (cameraObj == nullptr) {
908         WVLOG_E("cameraObj is nullptr");
909         return screenRotation;
910     }
911     int32_t cameraOrientation = static_cast<int32_t>(cameraObj->GetCameraOrientation());
912     auto cameraPosition = cameraObj->GetPosition(); // 1: back, 2: front
913 
914     int32_t pictureRotation = 0;
915     if (cameraPosition == OHOS::CameraStandard::CameraPosition::CAMERA_POSITION_FRONT) {
916         pictureRotation = (cameraOrientation - screenRotation) % ROTATION_MAX;
917     } else {
918         pictureRotation = (cameraOrientation + screenRotation) % ROTATION_MAX;
919     }
920 
921     WVLOG_D("GetPictureRotation, cameraOrientation:%{public}d, screenRotation:%{public}d, pictureRotation:%{public}d",
922         cameraOrientation, screenRotation, pictureRotation);
923     return pictureRotation;
924 }
925 
IsNeedCorrectRotation()926 bool CameraSurfaceListener::IsNeedCorrectRotation()
927 {
928     std::string deviceType = OHOS::system::GetDeviceType();
929     return (deviceType == "phone" || deviceType == "tablet");
930 }
931 
FillRotationInfo(int32_t rotation,bool isFlipX,bool isFlipY)932 std::shared_ptr<CameraRotationInfoAdapter> CameraSurfaceListener::FillRotationInfo(int32_t rotation,
933     bool isFlipX, bool isFlipY)
934 {
935     std::shared_ptr<CameraRotationInfoAdapterImpl> rotationInfo = std::make_shared<CameraRotationInfoAdapterImpl>();
936     if (!rotationInfo) {
937         WVLOG_E("new CameraRotationInfo failed");
938         return nullptr;
939     }
940 
941     if (IsNeedCorrectRotation()) {
942         rotation = GetPictureRotation();
943     }
944     rotationInfo->SetRotation(rotation);
945     rotationInfo->SetIsFlipX(isFlipX);
946     rotationInfo->SetIsFlipY(isFlipY);
947     return rotationInfo;
948 }
949 
GetRotationInfo(GraphicTransformType transform)950 std::shared_ptr<CameraRotationInfoAdapter> CameraSurfaceListener::GetRotationInfo(GraphicTransformType transform)
951 {
952     switch (transform) {
953         case GraphicTransformType::GRAPHIC_ROTATE_NONE: {
954             return FillRotationInfo(ROTATION_0, false, false);
955         }
956         case GraphicTransformType::GRAPHIC_ROTATE_90: {
957             return FillRotationInfo(ROTATION_90, false, false);
958         }
959         case GraphicTransformType::GRAPHIC_ROTATE_180: {
960             return FillRotationInfo(ROTATION_180, false, false);
961         }
962         case GraphicTransformType::GRAPHIC_ROTATE_270: {
963             return FillRotationInfo(ROTATION_270, false, false);
964         }
965         case GraphicTransformType::GRAPHIC_FLIP_H: {
966             return FillRotationInfo(ROTATION_0, false, true);
967         }
968         case GraphicTransformType::GRAPHIC_FLIP_V: {
969             return FillRotationInfo(ROTATION_0, true, false);
970         }
971         case GraphicTransformType::GRAPHIC_FLIP_H_ROT90: {
972             return FillRotationInfo(ROTATION_90, false, true);
973         }
974         case GraphicTransformType::GRAPHIC_FLIP_V_ROT90: {
975             return FillRotationInfo(ROTATION_90, true, false);
976         }
977         case GraphicTransformType::GRAPHIC_FLIP_H_ROT180: {
978             return FillRotationInfo(ROTATION_180, false, true);
979         }
980         case GraphicTransformType::GRAPHIC_FLIP_V_ROT180: {
981             return FillRotationInfo(ROTATION_180, true, false);
982         }
983         case GraphicTransformType::GRAPHIC_FLIP_H_ROT270: {
984             return FillRotationInfo(ROTATION_270, false, true);
985         }
986         case GraphicTransformType::GRAPHIC_FLIP_V_ROT270: {
987             return FillRotationInfo(ROTATION_270, true, false);
988         }
989         default: {
990             return FillRotationInfo(ROTATION_0, false, false);
991         }
992     }
993 }
994 
OnBufferAvailable()995 void CameraSurfaceListener::OnBufferAvailable()
996 {
997     int32_t flushFence = 0;
998     int64_t timestamp = 0;
999     OHOS::Rect damage;
1000     OHOS::sptr<OHOS::SurfaceBuffer> buffer = nullptr;
1001     if (surface_ == nullptr) {
1002         WVLOG_E("OnBufferAvailable:surface_ is null");
1003         return;
1004     }
1005     surface_->AcquireBuffer(buffer, flushFence, timestamp, damage);
1006     if (buffer != nullptr) {
1007         uint32_t size = buffer->GetSize();
1008 
1009         std::shared_ptr<CameraRotationInfoAdapter> rotationInfo = GetRotationInfo(surface_->GetTransform());
1010         if (!rotationInfo) {
1011             WVLOG_E("rotationInfo is null");
1012             return;
1013         }
1014 
1015         WVLOG_D("OnBufferAvailable, surfaceType_: %{public}d, size: %{public}d, width: %{public}d,\
1016             height: %{public}d, type: %{public}d, ratation: %{public}d, FilyY: %{public}d, FilyX: %{public}d",
1017             surfaceType_, size, buffer->GetWidth(), buffer->GetHeight(), surface_->GetTransform(),
1018             (int32_t)rotationInfo->GetRotation(), rotationInfo->GetIsFlipY(), rotationInfo->GetIsFlipX());
1019         auto bufferAdapter = std::make_shared<CameraSurfaceBufferAdapterImpl>(buffer);
1020         auto surfaceAdapter = std::make_shared<CameraSurfaceAdapterImpl>(surface_);
1021         if (listener_ != nullptr) {
1022             listener_->OnBufferAvailable(surfaceAdapter, std::move(bufferAdapter), std::move(rotationInfo));
1023         }
1024     } else {
1025         WVLOG_E("AcquireBuffer failed!");
1026     }
1027 }
1028 
CameraSurfaceAdapterImpl(sptr<IConsumerSurface> surface)1029 CameraSurfaceAdapterImpl::CameraSurfaceAdapterImpl(sptr<IConsumerSurface> surface) : cSurface_(surface) {}
1030 
ReleaseBuffer(std::shared_ptr<CameraSurfaceBufferAdapter> bufferAdapter,int32_t fence)1031 int32_t CameraSurfaceAdapterImpl::ReleaseBuffer(
1032     std::shared_ptr<CameraSurfaceBufferAdapter> bufferAdapter, int32_t fence)
1033 {
1034     if (!cSurface_ || !bufferAdapter) {
1035         WVLOG_E("cSurface_ or bufferAdapter is nullptr");
1036         return -1;
1037     }
1038     auto bufferImpl = static_cast<CameraSurfaceBufferAdapterImpl*>(bufferAdapter.get());
1039     return cSurface_->ReleaseBuffer(bufferImpl->GetBuffer(), fence);
1040 }
1041 
CameraManagerAdapterCallback(std::shared_ptr<CameraStatusCallbackAdapter> cameraStatusCallback)1042 CameraManagerAdapterCallback::CameraManagerAdapterCallback(
1043     std::shared_ptr<CameraStatusCallbackAdapter> cameraStatusCallback)
1044     : statusCallback_(cameraStatusCallback)
1045 {
1046     WVLOG_I("Create CameraManagerAdapterCallback");
1047 }
1048 
GetAdapterCameraStatus(CameraStatus status) const1049 CameraStatusAdapter CameraManagerAdapterCallback::GetAdapterCameraStatus(CameraStatus status) const
1050 {
1051     auto item = CAMERA_STATUS_MAP.find(status);
1052     if (item == CAMERA_STATUS_MAP.end()) {
1053         WVLOG_E("ori camera status %{public}d not found", status);
1054         return CameraStatusAdapter::APPEAR;
1055     }
1056     return item->second;
1057 }
1058 
OnCameraStatusChanged(const CameraStatusInfo & cameraStatusInfo) const1059 void CameraManagerAdapterCallback::OnCameraStatusChanged(const CameraStatusInfo& cameraStatusInfo) const
1060 {
1061     std::string callbackDeviceId;
1062     if (cameraStatusInfo.cameraDevice) {
1063         callbackDeviceId = cameraStatusInfo.cameraDevice->GetID();
1064     }
1065     std::string currentDeviceId = CameraManagerAdapterImpl::GetInstance().GetCurrentDeviceId();
1066 
1067     WVLOG_I("OnCameraStatusChanged: callbackdeviceID %{public}s, currentDeviceId:%{public}s, status %{public}d",
1068         callbackDeviceId.c_str(), currentDeviceId.c_str(), cameraStatusInfo.cameraStatus);
1069     CameraStatusAdapter cameraStatusAdapter = GetAdapterCameraStatus(cameraStatusInfo.cameraStatus);
1070 
1071     if (statusCallback_) {
1072         switch (cameraStatusAdapter) {
1073             case CameraStatusAdapter::AVAILABLE:
1074                 WVLOG_I("do not handle status AVAILABLE");
1075                 return;
1076             case CameraStatusAdapter::UNAVAILABLE:
1077                 WVLOG_I("do not handle status UNAVAILABLE");
1078                 return;
1079             case CameraStatusAdapter::APPEAR:
1080             case CameraStatusAdapter::DISAPPEAR:
1081                 break;
1082             default:
1083                 WVLOG_I("unknow status");
1084                 return;
1085         }
1086         WVLOG_I("start do statusCallback");
1087         statusCallback_->OnCameraStatusChanged(cameraStatusAdapter, callbackDeviceId);
1088     }
1089     return;
1090 }
1091 
OnFlashlightStatusChanged(const std::string & cameraID,const FlashStatus flashStatus) const1092 void CameraManagerAdapterCallback::OnFlashlightStatusChanged(
1093     const std::string& cameraID, const FlashStatus flashStatus) const
1094 {
1095     return;
1096 }
1097 } // namespace OHOS::NWeb
1098