• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "nweb_log.h"
21 
22 namespace OHOS::NWeb {
23 using namespace OHOS::CameraStandard;
24 const std::unordered_map<ConnectionType, VideoTransportType> TRANS_TYPE_MAP = {
25     {CAMERA_CONNECTION_BUILT_IN, VideoTransportType::VIDEO_TRANS_TYPE_BUILD_IN},
26     {CAMERA_CONNECTION_USB_PLUGIN, VideoTransportType::VIDEO_TRANS_TYPE_USB},
27     {CAMERA_CONNECTION_REMOTE, VideoTransportType::VIDEO_TRANS_TYPE_REMOTE},
28 };
29 
30 const std::unordered_map<CameraPosition, VideoFacingModeAdapter> FACING_MODE_MAP = {
31     {CAMERA_POSITION_UNSPECIFIED, VideoFacingModeAdapter::FACING_NONE},
32     {CAMERA_POSITION_FRONT, VideoFacingModeAdapter::FACING_USER},
33     {CAMERA_POSITION_BACK, VideoFacingModeAdapter::FACING_ENVIRONMENT},
34 };
35 
36 const std::unordered_map<CameraFormat, VideoPixelFormatAdapter> TO_ADAPTER_PIXEL_FORMAT_MAP = {
37     {CAMERA_FORMAT_RGBA_8888, VideoPixelFormatAdapter::FORMAT_RGBA_8888},
38     {CAMERA_FORMAT_YCBCR_420_888, VideoPixelFormatAdapter::FORMAT_YCBCR_420_888},
39     {CAMERA_FORMAT_YUV_420_SP, VideoPixelFormatAdapter::FORMAT_YUV_420_SP},
40     {CAMERA_FORMAT_JPEG, VideoPixelFormatAdapter::FORMAT_JPEG},
41     {CAMERA_FORMAT_INVALID, VideoPixelFormatAdapter::FORMAT_UNKNOWN},
42 };
43 
44 const std::unordered_map<VideoPixelFormatAdapter, CameraFormat> TO_OHOS_PIXEL_FORMAT_MAP = {
45     {VideoPixelFormatAdapter::FORMAT_RGBA_8888, CAMERA_FORMAT_RGBA_8888},
46     {VideoPixelFormatAdapter::FORMAT_YCBCR_420_888, CAMERA_FORMAT_YCBCR_420_888},
47     {VideoPixelFormatAdapter::FORMAT_YUV_420_SP, CAMERA_FORMAT_YUV_420_SP},
48     {VideoPixelFormatAdapter::FORMAT_JPEG, CAMERA_FORMAT_JPEG},
49     {VideoPixelFormatAdapter::FORMAT_UNKNOWN, CAMERA_FORMAT_INVALID},
50 };
51 
52 const std::unordered_map<ExposureMode, ExposureModeAdapter> EXPOSURE_MODE_MAP = {
53     {EXPOSURE_MODE_UNSUPPORTED, ExposureModeAdapter::EXPOSURE_MODE_UNSUPPORTED},
54     {EXPOSURE_MODE_LOCKED, ExposureModeAdapter::EXPOSURE_MODE_LOCKED},
55     {EXPOSURE_MODE_AUTO, ExposureModeAdapter::EXPOSURE_MODE_AUTO},
56     {EXPOSURE_MODE_CONTINUOUS_AUTO, ExposureModeAdapter::EXPOSURE_MODE_CONTINUOUS_AUTO},
57 };
58 
59 const std::unordered_map<FocusModeAdapter, FocusMode> FOCUS_MODE_MAP = {
60     {FocusModeAdapter::FOCUS_MODE_MANUAL, FOCUS_MODE_MANUAL},
61     {FocusModeAdapter::FOCUS_MODE_CONTINUOUS_AUTO, FOCUS_MODE_CONTINUOUS_AUTO},
62     {FocusModeAdapter::FOCUS_MODE_AUTO, FOCUS_MODE_AUTO},
63     {FocusModeAdapter::FOCUS_MODE_LOCKED, FOCUS_MODE_LOCKED},
64 };
65 
66 const std::unordered_map<FocusMode, FocusModeAdapter> ADAPTER_FOCUS_MODE_MAP = {
67     {FOCUS_MODE_MANUAL, FocusModeAdapter::FOCUS_MODE_MANUAL},
68     {FOCUS_MODE_CONTINUOUS_AUTO, FocusModeAdapter::FOCUS_MODE_CONTINUOUS_AUTO},
69     {FOCUS_MODE_AUTO, FocusModeAdapter::FOCUS_MODE_AUTO},
70     {FOCUS_MODE_LOCKED, FocusModeAdapter::FOCUS_MODE_LOCKED},
71 };
72 
73 const std::unordered_map<FlashModeAdapter, FlashMode> FLASH_MODE_MAP = {
74     {FlashModeAdapter::FLASH_MODE_CLOSE, FLASH_MODE_CLOSE},
75     {FlashModeAdapter::FLASH_MODE_OPEN, FLASH_MODE_OPEN},
76     {FlashModeAdapter::FLASH_MODE_AUTO, FLASH_MODE_AUTO},
77     {FlashModeAdapter::FLASH_MODE_ALWAYS_OPEN, FLASH_MODE_ALWAYS_OPEN},
78 };
79 
80 const std::unordered_map<CameraStatus, CameraStatusAdapter> CAMERA_STATUS_MAP = {
81     {CAMERA_STATUS_APPEAR, CameraStatusAdapter::APPEAR},
82     {CAMERA_STATUS_DISAPPEAR, CameraStatusAdapter::DISAPPEAR},
83     {CAMERA_STATUS_AVAILABLE, CameraStatusAdapter::AVAILABLE},
84     {CAMERA_STATUS_UNAVAILABLE, CameraStatusAdapter::UNAVAILABLE},
85 };
86 
GetCameraTransportType(ConnectionType connectType)87 VideoTransportType CameraManagerAdapterImpl::GetCameraTransportType(ConnectionType connectType)
88 {
89     auto item = TRANS_TYPE_MAP.find(connectType);
90     if (item == TRANS_TYPE_MAP.end()) {
91         WVLOG_E("concect type %{public}d not found", connectType);
92         return VideoTransportType::VIDEO_TRANS_TYPE_OTHER;
93     }
94     return item->second;
95 }
96 
GetCameraFacingMode(CameraPosition position)97 VideoFacingModeAdapter CameraManagerAdapterImpl::GetCameraFacingMode(CameraPosition position)
98 {
99     auto item = FACING_MODE_MAP.find(position);
100     if (item == FACING_MODE_MAP.end()) {
101         WVLOG_E("position type %{public}d not found", position);
102         return VideoFacingModeAdapter::FACING_NONE;
103     }
104     return item->second;
105 }
106 
TransToAdapterCameraFormat(CameraFormat format)107 VideoPixelFormatAdapter CameraManagerAdapterImpl::TransToAdapterCameraFormat(CameraFormat format)
108 {
109     auto item = TO_ADAPTER_PIXEL_FORMAT_MAP.find(format);
110     if (item == TO_ADAPTER_PIXEL_FORMAT_MAP.end()) {
111         WVLOG_E("to adapter pixel format type %{public}d not found", format);
112         return VideoPixelFormatAdapter::FORMAT_UNKNOWN;
113     }
114     return item->second;
115 }
116 
TransToOriCameraFormat(VideoPixelFormatAdapter format)117 CameraFormat CameraManagerAdapterImpl::TransToOriCameraFormat(VideoPixelFormatAdapter format)
118 {
119     auto item = TO_OHOS_PIXEL_FORMAT_MAP.find(format);
120     if (item == TO_OHOS_PIXEL_FORMAT_MAP.end()) {
121         WVLOG_E("to pixel format type %{public}d not found", format);
122         return CAMERA_FORMAT_INVALID;
123     }
124     return item->second;
125 }
126 
GetAdapterExposureMode(ExposureMode exportMode)127 ExposureModeAdapter CameraManagerAdapterImpl::GetAdapterExposureMode(ExposureMode exportMode)
128 {
129     auto item = EXPOSURE_MODE_MAP.find(exportMode);
130     if (item == EXPOSURE_MODE_MAP.end()) {
131         WVLOG_E("to exposure mode %{public}d not found", exportMode);
132         return ExposureModeAdapter::EXPOSURE_MODE_UNSUPPORTED;
133     }
134     return item->second;
135 }
136 
GetOriFocusMode(FocusModeAdapter focusMode)137 FocusMode CameraManagerAdapterImpl::GetOriFocusMode(FocusModeAdapter focusMode)
138 {
139     auto item = FOCUS_MODE_MAP.find(focusMode);
140     if (item == FOCUS_MODE_MAP.end()) {
141         WVLOG_E("adapter focus mode %{public}d not found", focusMode);
142         return FOCUS_MODE_MANUAL;
143     }
144     return item->second;
145 }
146 
GetOriFlashMode(FlashModeAdapter flashMode)147 FlashMode CameraManagerAdapterImpl::GetOriFlashMode(FlashModeAdapter flashMode)
148 {
149     auto item = FLASH_MODE_MAP.find(flashMode);
150     if (item == FLASH_MODE_MAP.end()) {
151         WVLOG_E("adapter flash mode %{public}d not found", flashMode);
152         return FLASH_MODE_CLOSE;
153     }
154     return item->second;
155 }
156 
GetAdapterFocusMode(FocusMode focusMode)157 FocusModeAdapter CameraManagerAdapterImpl::GetAdapterFocusMode(FocusMode focusMode)
158 {
159     auto item = ADAPTER_FOCUS_MODE_MAP.find(focusMode);
160     if (item == ADAPTER_FOCUS_MODE_MAP.end()) {
161         WVLOG_E("ori focus mode %{public}d not found", focusMode);
162         return FocusModeAdapter::FOCUS_MODE_MANUAL;
163     }
164     return item->second;
165 }
166 
GetInstance()167 CameraManagerAdapterImpl& CameraManagerAdapterImpl::GetInstance()
168 {
169     static CameraManagerAdapterImpl instance;
170     return instance;
171 }
172 
Create(std::shared_ptr<CameraStatusCallbackAdapter> cameraStatusCallback)173 int32_t CameraManagerAdapterImpl::Create(std::shared_ptr<CameraStatusCallbackAdapter> cameraStatusCallback)
174 {
175     std::lock_guard<std::mutex> lock(mutex_);
176 
177     WVLOG_I("create CameraManagerAdapterImpl");
178     if (cameraManager_ == nullptr) {
179         cameraManager_ = CameraManager::GetInstance();
180         if (cameraManager_ == nullptr) {
181             WVLOG_E("Failed to get camera manager!");
182             return CAMERA_ERROR;
183         }
184     }
185     cameraMngrCallback_ = std::make_shared<CameraManagerAdapterCallback>(cameraStatusCallback);
186     cameraManager_->SetCallback(cameraMngrCallback_);
187     return CAMERA_OK;
188 }
189 
GetCameraSupportFormats(sptr<CameraOutputCapability> outputcapability)190 std::vector<FormatAdapter> CameraManagerAdapterImpl::GetCameraSupportFormats(
191     sptr<CameraOutputCapability> outputcapability)
192 {
193     std::vector<FormatAdapter> captureFormats;
194 
195     std::vector<Profile> previewProfiles = outputcapability->GetPreviewProfiles();
196     for (auto i : previewProfiles) {
197         FormatAdapter format;
198         format.width = i.GetSize().width;
199         format.height = i.GetSize().height;
200         format.frameRate = DEFAULT_FRAME_RATE;
201         format.pixelFormat = TransToAdapterCameraFormat(i.GetCameraFormat());
202         captureFormats.push_back(format);
203     }
204     return captureFormats;
205 }
206 
GetDevicesInfo(std::vector<VideoDeviceDescriptor> & devicesDiscriptor)207 void CameraManagerAdapterImpl::GetDevicesInfo(std::vector<VideoDeviceDescriptor> &devicesDiscriptor)
208 {
209     std::lock_guard<std::mutex> lock(mutex_);
210     if (cameraManager_ == nullptr) {
211         WVLOG_E("camera manager is nullptr");
212         return;
213     }
214 
215     std::vector<sptr<CameraDevice>> cameraObjList = cameraManager_->GetSupportedCameras();
216     if (cameraObjList.size() == 0) {
217         WVLOG_E("No cameras are available!!!");
218         return;
219     }
220     for (auto cameraObj : cameraObjList) {
221         sptr<CameraOutputCapability> outputcapability =
222             cameraManager_->GetSupportedOutputCapability(cameraObj);
223         VideoDeviceDescriptor deviceDisc;
224         deviceDisc.displayName = cameraObj->GetID();
225         deviceDisc.deviceId = cameraObj->GetID();
226         deviceDisc.modelId = cameraObj->GetID();
227         deviceDisc.controlSupport.pan = false;
228         deviceDisc.controlSupport.tilt = false;
229         deviceDisc.controlSupport.zoom = false;
230         deviceDisc.transportType = GetCameraTransportType(cameraObj->GetConnectionType());
231         deviceDisc.facing = GetCameraFacingMode(cameraObj->GetPosition());
232 
233         deviceDisc.supportCaptureFormats = GetCameraSupportFormats(outputcapability);
234         WVLOG_I("deviceDisc  id:%{public}s, control pan:%{public}d tilt:%{public}d, zoom:%{public}d \
235             transType:%{public}d, facingMode:%{public}d",
236             deviceDisc.deviceId.c_str(),
237             deviceDisc.controlSupport.pan,
238             deviceDisc.controlSupport.tilt,
239             deviceDisc.controlSupport.zoom,
240             deviceDisc.transportType,
241             deviceDisc.facing);
242         devicesDiscriptor.emplace_back(std::move(deviceDisc));
243     }
244 }
245 
InitCameraInput(const std::string & deviceId)246 int32_t CameraManagerAdapterImpl::InitCameraInput(const std::string &deviceId)
247 {
248     int32_t result = CAMERA_ERROR;
249 
250     if (status_ == CameraStatusAdapter::UNAVAILABLE) {
251         WVLOG_E("camera is not closed");
252         return result;
253     }
254 
255     if (inputInitedFlag_) {
256         WVLOG_E("input is already inited");
257         return result;
258     }
259 
260     if (cameraManager_ == nullptr) {
261         WVLOG_E("camera manager is nullptr");
262         return CAMERA_NULL_ERROR;
263     }
264 
265     if (cameraInput_ == nullptr) {
266         WVLOG_I("camera input create %{public}s", deviceId.c_str());
267         sptr<CameraDevice> cameraObj = cameraManager_->GetCameraDeviceFromId(deviceId);
268         if (cameraObj == nullptr) {
269             WVLOG_E("No cameras are available!!!");
270             return CAMERA_NULL_ERROR;
271         }
272 
273         cameraInput_ = cameraManager_->CreateCameraInput(cameraObj);
274         if (cameraInput_ == nullptr) {
275             WVLOG_E("Failed to create CameraInput");
276             return result;
277         }
278         int32_t ret = cameraInput_->Open();
279         if (ret != CAMERA_OK) {
280             WVLOG_E("Failed to open CameraInput, err code %{public}d.", ret);
281             return result;
282         }
283         deviceId_ = deviceId;
284         inputInitedFlag_ = true;
285     }
286 
287     result = CAMERA_OK;
288     return result;
289 }
290 
InitPreviewOutput(const VideoCaptureParamsAdapter & captureParams,std::shared_ptr<CameraBufferListenerAdapter> listener)291 int32_t CameraManagerAdapterImpl::InitPreviewOutput(const VideoCaptureParamsAdapter &captureParams,
292     std::shared_ptr<CameraBufferListenerAdapter> listener)
293 {
294     int32_t result = CAMERA_ERROR;
295     Size previewSize;
296 
297     if (!inputInitedFlag_) {
298         WVLOG_E("input is not inited");
299         return result;
300     }
301 
302     if (cameraManager_ == nullptr) {
303         WVLOG_E("camera manager is null");
304         return result;
305     }
306 
307     if (previewOutput_ == nullptr) {
308         WVLOG_I("preview output create");
309         previewSurface_ = IConsumerSurface::Create();
310         if (previewSurface_ ==  nullptr) {
311             WVLOG_E("previewSurface_ is null");
312             return result;
313         }
314         previewSize.width = captureParams.captureFormat.width;
315         previewSize.height = captureParams.captureFormat.height;
316         previewSurface_->SetDefaultWidthAndHeight(previewSize.width, previewSize.height);
317         previewSurface_->SetUserData(CameraManager::surfaceFormat,
318             std::to_string(TransToOriCameraFormat(captureParams.captureFormat.pixelFormat)));
319         Profile previewproFile = Profile(static_cast<CameraFormat>(
320                                         TransToOriCameraFormat(captureParams.captureFormat.pixelFormat)),
321                                          previewSize);
322         WVLOG_I("preview output format: %{public}d, w: %{public}d, h: %{public}d",
323             TransToOriCameraFormat(captureParams.captureFormat.pixelFormat), previewSize.width, previewSize.height);
324         previewSurfaceListener_ = new(std::nothrow) CameraSurfaceListener(SurfaceType::PREVIEW,
325                                                                     previewSurface_,
326                                                                     (listener));
327         previewSurface_->RegisterConsumerListener((sptr<IBufferConsumerListener> &)previewSurfaceListener_);
328         sptr<IBufferProducer> bp = previewSurface_->GetProducer();
329         sptr<Surface> pSurface = Surface::CreateSurfaceAsProducer(bp);
330         previewOutput_ = cameraManager_->CreatePreviewOutput(previewproFile, pSurface);
331         if (previewOutput_ == nullptr) {
332             WVLOG_E("Failed to create previewOutput");
333             return result;
334         }
335         captureParams_ = captureParams;
336         listener_ = listener;
337     }
338     result = CAMERA_OK;
339     return result;
340 }
341 
TransToAdapterExposureModes(std::vector<ExposureMode> & exposureModes,std::vector<ExposureModeAdapter> & exposureModesAdapter)342 int32_t CameraManagerAdapterImpl::TransToAdapterExposureModes(
343     std::vector<ExposureMode>& exposureModes,
344     std::vector<ExposureModeAdapter>& exposureModesAdapter)
345 {
346     for (auto exportMode : exposureModes) {
347         exposureModesAdapter.push_back(GetAdapterExposureMode(exportMode));
348     }
349 
350     return CAMERA_OK;
351 }
352 
GetExposureModes(std::vector<ExposureModeAdapter> & exposureModesAdapter)353 int32_t CameraManagerAdapterImpl::GetExposureModes(std::vector<ExposureModeAdapter>& exposureModesAdapter)
354 {
355     std::lock_guard<std::mutex> lock(mutex_);
356     if (captureSession_ == nullptr) {
357         WVLOG_E("captureSession is nullptr when get exposure modes");
358         return CAMERA_ERROR;
359     }
360     std::vector<ExposureMode> exposureModes;
361     if (captureSession_->GetSupportedExposureModes(exposureModes) != SUCCESS) {
362         WVLOG_E("get inner exposure modes faileds");
363         return CAMERA_ERROR;
364     }
365 
366     TransToAdapterExposureModes(exposureModes, exposureModesAdapter);
367 
368     return CAMERA_OK;
369 }
370 
GetCurrentExposureMode(ExposureModeAdapter & exposureModeAdapter)371 int32_t CameraManagerAdapterImpl::GetCurrentExposureMode(ExposureModeAdapter& exposureModeAdapter)
372 {
373     std::lock_guard<std::mutex> lock(mutex_);
374     if (captureSession_ == nullptr) {
375         WVLOG_E("captureSession is nullptr when get current exposure modes");
376         return CAMERA_ERROR;
377     }
378 
379     ExposureMode exposureMode = captureSession_->GetExposureMode();
380     exposureModeAdapter = GetAdapterExposureMode(exposureMode);
381     return CAMERA_OK;
382 }
383 
GetExposureCompensation(VideoCaptureRangeAdapter & rangeVal)384 int32_t CameraManagerAdapterImpl::GetExposureCompensation(VideoCaptureRangeAdapter& rangeVal)
385 {
386     if (captureSession_ == nullptr) {
387         return CAMERA_ERROR;
388     }
389     std::vector<float> exposureBiasRange = captureSession_->GetExposureBiasRange();
390     int32_t exposureCompos = captureSession_->GetExposureValue();
391     if (exposureBiasRange.size() == RANGE_MAX_SIZE) {
392         rangeVal.min = exposureBiasRange.at(RANGE_MIN_INDEX);
393         rangeVal.max = exposureBiasRange.at(RANGE_MAX_INDEX);
394     }
395 
396     rangeVal.current = exposureCompos;
397     return CAMERA_OK;
398 }
399 
GetCaptionRangeById(RangeIDAdapter rangeId,VideoCaptureRangeAdapter & rangeVal)400 int32_t CameraManagerAdapterImpl::GetCaptionRangeById(RangeIDAdapter rangeId, VideoCaptureRangeAdapter& rangeVal)
401 {
402     std::lock_guard<std::mutex> lock(mutex_);
403     if (captureSession_ == nullptr) {
404         WVLOG_E("captureSession is nullptr when get %{public}d range info", rangeId);
405         return CAMERA_ERROR;
406     }
407     int32_t result = CAMERA_OK;
408     switch (rangeId) {
409         case RangeIDAdapter::RANGE_ID_EXP_COMPENSATION:
410             if (GetExposureCompensation(rangeVal) != CAMERA_OK) {
411                 WVLOG_E("get exposure compensation failed.");
412                 result = CAMERA_ERROR;
413             }
414             break;
415         default:
416             result = CAMERA_ERROR;
417             break;
418     }
419     return result;
420 }
421 
IsFocusModeSupported(FocusModeAdapter focusMode)422 bool CameraManagerAdapterImpl::IsFocusModeSupported(FocusModeAdapter focusMode)
423 {
424     std::lock_guard<std::mutex> lock(mutex_);
425     if (captureSession_ == nullptr) {
426         WVLOG_E("captureSession is nullptr when get support focuc mode");
427         return false;
428     }
429     if (!captureSession_->IsFocusModeSupported(GetOriFocusMode(focusMode))) {
430         return false;
431     }
432     return true;
433 }
434 
GetCurrentFocusMode()435 FocusModeAdapter CameraManagerAdapterImpl::GetCurrentFocusMode()
436 {
437     std::lock_guard<std::mutex> lock(mutex_);
438     if (captureSession_ == nullptr) {
439         WVLOG_E("captureSession is nullptr when get support focuc mode");
440         return FocusModeAdapter::FOCUS_MODE_MANUAL;
441     }
442 
443     FocusMode oriFocusMode = captureSession_->GetFocusMode();
444     return GetAdapterFocusMode(oriFocusMode);
445 }
446 
IsFlashModeSupported(FlashModeAdapter focusMode)447 bool CameraManagerAdapterImpl::IsFlashModeSupported(FlashModeAdapter focusMode)
448 {
449     std::lock_guard<std::mutex> lock(mutex_);
450     if (captureSession_ == nullptr) {
451         WVLOG_E("captureSession is nullptr when get support flash mode");
452         return false;
453     }
454     if (!captureSession_->IsFlashModeSupported(GetOriFlashMode(focusMode))) {
455         return false;
456     }
457     return true;
458 }
459 
CreateAndStartSession()460 int32_t CameraManagerAdapterImpl::CreateAndStartSession()
461 {
462     int32_t result = CAMERA_ERROR;
463     if (status_ == CameraStatusAdapter::UNAVAILABLE) {
464         WVLOG_E("camera is already opened");
465         return result;
466     }
467 
468     if ((cameraInput_ == nullptr) || (previewOutput_ == nullptr)) {
469         WVLOG_E("cameraInput_ or previewOutput_ is null");
470         return result;
471     }
472 
473     WVLOG_I("CreateCaptureSession");
474     captureSession_ = cameraManager_->CreateCaptureSession();
475     if (captureSession_ == nullptr) {
476         WVLOG_E("Failed to create capture session");
477         return result;
478     }
479     WVLOG_I("BeginConfig");
480     captureSession_->BeginConfig();
481     WVLOG_I("AddInput");
482 
483     result = captureSession_->AddInput(cameraInput_);
484     if (result != CAMERA_OK) {
485         WVLOG_E("Failed to add in put");
486         return result;
487     }
488 
489     WVLOG_I("AddOutput");
490     result = captureSession_->AddOutput(previewOutput_);
491     if (result != CAMERA_OK) {
492         WVLOG_E("Failed to add preview output");
493         return result;
494     }
495     WVLOG_I("CommitConfig");
496     result = captureSession_->CommitConfig();
497     if (result != CAMERA_OK) {
498         WVLOG_E("Failed to commit config");
499         return result;
500     }
501     WVLOG_I("Start");
502     result = captureSession_->Start();
503     if (result != CAMERA_OK) {
504         WVLOG_E("Failed to start session");
505         return result;
506     }
507     result = CAMERA_OK;
508     status_ = CameraStatusAdapter::UNAVAILABLE;
509     isCapturing_ = true;
510     return result;
511 }
512 
RestartSession()513 int32_t CameraManagerAdapterImpl::RestartSession()
514 {
515     std::lock_guard<std::mutex> lock(restart_mutex_);
516     WVLOG_I("RestartSession %{public}s", deviceId_.c_str());
517     if (!isCapturing_) {
518         WVLOG_E("this web tab is not capturing");
519         return CAMERA_OK;
520     }
521 
522     if (!((isForegound_) && (status_ == CameraStatusAdapter::AVAILABLE))) {
523         WVLOG_E("can not restart session, status:%{public}d, isForeground:%{public}d", status_, isForegound_);
524         return CAMERA_OK;
525     }
526 
527     if (cameraManager_ == nullptr) {
528         WVLOG_E("cameraManager_ is null when start session");
529         return CAMERA_ERROR;
530     }
531 
532     cameraInput_ = nullptr;
533     previewOutput_ = nullptr;
534     previewSurface_ = nullptr;
535     previewSurfaceListener_ = nullptr;
536     inputInitedFlag_ = false;
537     captureSession_ = nullptr;
538 
539     if (StartStream(deviceId_, captureParams_, listener_) != CAMERA_OK) {
540         WVLOG_E("restart stream failed");
541         ReleaseSessionResource(deviceId_);
542         ReleaseSession();
543         return CAMERA_ERROR;
544     }
545     status_ = CameraStatusAdapter::UNAVAILABLE;
546     return CAMERA_OK;
547 }
548 
StopSession(CameraStopType stopType)549 int32_t CameraManagerAdapterImpl::StopSession(CameraStopType stopType)
550 {
551     std::lock_guard<std::mutex> lock(mutex_);
552     WVLOG_I("StopSession");
553     if (status_ == CameraStatusAdapter::AVAILABLE) {
554         WVLOG_E("camera is already closed when stop session");
555         return CAMERA_OK;
556     }
557     ReleaseSessionResource(deviceId_);
558     ReleaseSession();
559 
560     if (stopType == CameraStopType::NORMAL) {
561         isCapturing_ = false;
562     }
563     return CAMERA_OK;
564 }
565 
ReleaseSession()566 int32_t CameraManagerAdapterImpl::ReleaseSession()
567 {
568     WVLOG_I("release session");
569     if (captureSession_ != nullptr) {
570         captureSession_->Stop();
571         captureSession_->Release();
572         captureSession_ = nullptr;
573     }
574 
575     return CAMERA_OK;
576 }
577 
ReleaseSessionResource(const std::string & deviceId)578 int32_t CameraManagerAdapterImpl::ReleaseSessionResource(const std::string &deviceId)
579 {
580     WVLOG_I("release session resource");
581     if (deviceId_ != deviceId) {
582         WVLOG_E("deviceId is not used");
583         return CAMERA_OK;
584     }
585     if (cameraInput_ != nullptr) {
586         cameraInput_->Release();
587         cameraInput_ = nullptr;
588     }
589 
590     if (previewOutput_ != nullptr) {
591         ((sptr<PreviewOutput> &)previewOutput_)->Stop();
592         previewOutput_->Release();
593         previewOutput_ = nullptr;
594     }
595 
596     previewSurface_ = nullptr;
597     previewSurfaceListener_ = nullptr;
598     status_ = CameraStatusAdapter::AVAILABLE;
599     inputInitedFlag_ = false;
600     isForegound_ = false;
601 
602     return CAMERA_OK;
603 }
604 
ReleaseCameraManger()605 int32_t CameraManagerAdapterImpl::ReleaseCameraManger()
606 {
607     std::lock_guard<std::mutex> lock(mutex_);
608     WVLOG_I("release camera manger");
609     ReleaseSessionResource(deviceId_);
610     ReleaseSession();
611     cameraManager_ = nullptr;
612     status_ = CameraStatusAdapter::AVAILABLE;
613     inputInitedFlag_ = false;
614     isForegound_ = false;
615     cameraMngrCallback_ = nullptr;
616     return CAMERA_OK;
617 }
618 
GetCameraStatus()619 CameraStatusAdapter CameraManagerAdapterImpl::GetCameraStatus()
620 {
621     std::lock_guard<std::mutex> lock(mutex_);
622     return status_;
623 }
624 
SetCameraStatus(CameraStatusAdapter status)625 void CameraManagerAdapterImpl::SetCameraStatus(CameraStatusAdapter status)
626 {
627     std::lock_guard<std::mutex> lock(mutex_);
628     status_ = status;
629 }
630 
IsExistCaptureTask()631 bool CameraManagerAdapterImpl::IsExistCaptureTask()
632 {
633     std::lock_guard<std::mutex> lock(mutex_);
634     if (cameraManager_ == nullptr) {
635         WVLOG_E("cameraManager_ is nullptr");
636         return false;
637     }
638     return isCapturing_;
639 }
640 
SetForegroundFlag(bool isForeground)641 void CameraManagerAdapterImpl::SetForegroundFlag(bool isForeground)
642 {
643     isForegound_ = isForeground;
644 }
645 
646 
StartStream(const std::string & deviceId,const VideoCaptureParamsAdapter & captureParams,std::shared_ptr<CameraBufferListenerAdapter> listener)647 int32_t CameraManagerAdapterImpl::StartStream(const std::string &deviceId,
648     const VideoCaptureParamsAdapter &captureParams,
649     std::shared_ptr<CameraBufferListenerAdapter> listener)
650 {
651     std::lock_guard<std::mutex> lock(mutex_);
652 
653     if ((cameraManager_ == nullptr) || (listener == nullptr)) {
654         WVLOG_E("cameraManager or listener is null when start session");
655         return CAMERA_ERROR;
656     }
657 
658     if (InitCameraInput(deviceId) != CAMERA_OK) {
659         WVLOG_E("init camera input failed");
660         ReleaseSessionResource(deviceId);
661         return CAMERA_ERROR;
662     }
663 
664     if (InitPreviewOutput(captureParams, listener) != CAMERA_OK) {
665         WVLOG_E("init camera preview output failed");
666         ReleaseSessionResource(deviceId);
667         return CAMERA_ERROR;
668     }
669 
670     if (CreateAndStartSession() != CAMERA_OK) {
671         WVLOG_E("create session failed");
672         ReleaseSession();
673         return CAMERA_ERROR;
674     }
675 
676     return CAMERA_OK;
677 }
678 
CameraSurfaceBufferAdapterImpl(sptr<SurfaceBuffer> buffer)679 CameraSurfaceBufferAdapterImpl::CameraSurfaceBufferAdapterImpl(sptr<SurfaceBuffer> buffer) : buffer_(buffer) {}
680 
GetFileDescriptor() const681 int32_t CameraSurfaceBufferAdapterImpl::GetFileDescriptor() const
682 {
683     if (!buffer_) {
684         WVLOG_E("buffer_ is nullptr");
685         return -1;
686     }
687     return buffer_->GetFileDescriptor();
688 }
689 
GetWidth() const690 int32_t CameraSurfaceBufferAdapterImpl::GetWidth() const
691 {
692     if (!buffer_) {
693         WVLOG_E("buffer_ is nullptr");
694         return -1;
695     }
696     return buffer_->GetWidth();
697 }
698 
GetHeight() const699 int32_t CameraSurfaceBufferAdapterImpl::GetHeight() const
700 {
701     if (!buffer_) {
702         WVLOG_E("buffer_ is nullptr");
703         return -1;
704     }
705     return buffer_->GetHeight();
706 }
707 
GetStride() const708 int32_t CameraSurfaceBufferAdapterImpl::GetStride() const
709 {
710     if (!buffer_) {
711         WVLOG_E("buffer_ is nullptr");
712         return -1;
713     }
714     return buffer_->GetStride();
715 }
716 
GetFormat() const717 int32_t CameraSurfaceBufferAdapterImpl::GetFormat() const
718 {
719     if (!buffer_) {
720         WVLOG_E("buffer_ is nullptr");
721         return -1;
722     }
723     return buffer_->GetFormat();
724 }
725 
GetSize() const726 uint32_t CameraSurfaceBufferAdapterImpl::GetSize() const
727 {
728     if (!buffer_) {
729         WVLOG_E("buffer_ is nullptr");
730         return 0;
731     }
732     return buffer_->GetSize();
733 }
734 
GetBuffer()735 sptr<SurfaceBuffer>& CameraSurfaceBufferAdapterImpl::GetBuffer()
736 {
737     return buffer_;
738 }
739 
GetBufferAddr()740 uint8_t* CameraSurfaceBufferAdapterImpl::GetBufferAddr()
741 {
742     if (!buffer_) {
743         WVLOG_E("buffer_ is nullptr");
744         return 0;
745     }
746     return static_cast<uint8_t *>(buffer_->GetVirAddr());
747 }
748 
CameraSurfaceListener(SurfaceType type,sptr<IConsumerSurface> surface,std::shared_ptr<CameraBufferListenerAdapter> listener)749 CameraSurfaceListener::CameraSurfaceListener(SurfaceType type,
750     sptr<IConsumerSurface> surface,
751     std::shared_ptr<CameraBufferListenerAdapter> listener)
752     : surfaceType_(type), surface_(surface), listener_(listener) {
753 }
754 
FillRotationInfo(int roration,bool isFlipX,bool isFlipY)755 CameraRotationInfo CameraSurfaceListener::FillRotationInfo(int roration, bool isFlipX, bool isFlipY)
756 {
757     CameraRotationInfo rotationInfo;
758     rotationInfo.rotation = roration;
759     rotationInfo.isFlipX = isFlipX;
760     rotationInfo.isFlipY = isFlipY;
761     return rotationInfo;
762 }
763 
GetRotationInfo(GraphicTransformType transform)764 CameraRotationInfo CameraSurfaceListener::GetRotationInfo(GraphicTransformType transform)
765 {
766     switch (transform) {
767         case GraphicTransformType::GRAPHIC_ROTATE_NONE: {
768             return FillRotationInfo(ROTATION_0, false, false);
769         }
770         case GraphicTransformType::GRAPHIC_ROTATE_90: {
771             return FillRotationInfo(ROTATION_90, false, false);
772         }
773         case GraphicTransformType::GRAPHIC_ROTATE_180: {
774             return FillRotationInfo(ROTATION_180, false, false);
775         }
776         case GraphicTransformType::GRAPHIC_ROTATE_270: {
777             return FillRotationInfo(ROTATION_270, false, false);
778         }
779         case GraphicTransformType::GRAPHIC_FLIP_H: {
780             return FillRotationInfo(ROTATION_0, false, true);
781         }
782         case GraphicTransformType::GRAPHIC_FLIP_V: {
783             return FillRotationInfo(ROTATION_0, true, false);
784         }
785         case GraphicTransformType::GRAPHIC_FLIP_H_ROT90: {
786             return FillRotationInfo(ROTATION_90, false, true);
787         }
788         case GraphicTransformType::GRAPHIC_FLIP_V_ROT90: {
789             return FillRotationInfo(ROTATION_90, true, false);
790         }
791         case GraphicTransformType::GRAPHIC_FLIP_H_ROT180: {
792             return FillRotationInfo(ROTATION_180, false, true);
793         }
794         case GraphicTransformType::GRAPHIC_FLIP_V_ROT180: {
795             return FillRotationInfo(ROTATION_180, true, false);
796         }
797         case GraphicTransformType::GRAPHIC_FLIP_H_ROT270: {
798             return FillRotationInfo(ROTATION_270, false, true);
799         }
800         case GraphicTransformType::GRAPHIC_FLIP_V_ROT270: {
801             return FillRotationInfo(ROTATION_270, true, false);
802         }
803         default: {
804             return FillRotationInfo(ROTATION_0, false, false);
805         }
806     }
807 }
808 
OnBufferAvailable()809 void CameraSurfaceListener::OnBufferAvailable()
810 {
811     int32_t flushFence = 0;
812     int64_t timestamp = 0;
813     OHOS::Rect damage;
814     OHOS::sptr<OHOS::SurfaceBuffer> buffer = nullptr;
815     if (surface_ == nullptr) {
816         WVLOG_E("OnBufferAvailable:surface_ is null");
817         return;
818     }
819     surface_->AcquireBuffer(buffer, flushFence, timestamp, damage);
820     if (buffer != nullptr) {
821         uint32_t size = buffer->GetSize();
822 
823         CameraRotationInfo rotationInfo = GetRotationInfo(surface_->GetTransform());
824         WVLOG_D("OnBufferAvailable, surfaceType_: %{public}d, size: %{public}d, width: %{public}d,\
825             height: %{public}d, type: %{public}d, ratation: %{public}d, FilyY: %{public}d, FilyX: %{public}d",
826             surfaceType_, size, buffer->GetWidth(), buffer->GetHeight(), surface_->GetTransform(),
827             (int32_t)rotationInfo.rotation, rotationInfo.isFlipY, rotationInfo.isFlipX);
828         auto bufferAdapter = std::make_unique<CameraSurfaceBufferAdapterImpl>(buffer);
829         auto surfaceAdapter = std::make_shared<CameraSurfaceAdapterImpl>(surface_);
830         if (listener_ != nullptr) {
831             listener_->OnBufferAvailable(surfaceAdapter, std::move(bufferAdapter), rotationInfo);
832         }
833     } else {
834         WVLOG_E("AcquireBuffer failed!");
835     }
836 }
837 
CameraSurfaceAdapterImpl(sptr<IConsumerSurface> surface)838 CameraSurfaceAdapterImpl::CameraSurfaceAdapterImpl(sptr<IConsumerSurface> surface) : cSurface_(surface) {}
839 
ReleaseBuffer(std::unique_ptr<CameraSurfaceBufferAdapter> bufferAdapter,int32_t fence)840 int32_t CameraSurfaceAdapterImpl::ReleaseBuffer(std::unique_ptr<CameraSurfaceBufferAdapter> bufferAdapter,
841                                                 int32_t fence)
842 {
843     if (!cSurface_ || !bufferAdapter) {
844         WVLOG_E("cSurface_ or bufferAdapter is nullptr");
845         return -1;
846     }
847     auto bufferImpl = static_cast<CameraSurfaceBufferAdapterImpl*>(bufferAdapter.get());
848     return cSurface_->ReleaseBuffer(bufferImpl->GetBuffer(), fence);
849 }
850 
CameraManagerAdapterCallback(std::shared_ptr<CameraStatusCallbackAdapter> cameraStatusCallback)851 CameraManagerAdapterCallback::CameraManagerAdapterCallback(std::shared_ptr<CameraStatusCallbackAdapter>
852     cameraStatusCallback): statusCallback_(cameraStatusCallback)
853 {
854     WVLOG_I("Create CameraManagerAdapterCallback");
855 }
856 
GetAdapterCameraStatus(CameraStatus status) const857 CameraStatusAdapter CameraManagerAdapterCallback::GetAdapterCameraStatus(CameraStatus status) const
858 {
859     auto item = CAMERA_STATUS_MAP.find(status);
860     if (item == CAMERA_STATUS_MAP.end()) {
861         WVLOG_E("ori camera status %{public}d not found", status);
862         return CameraStatusAdapter::APPEAR;
863     }
864     return item->second;
865 }
866 
OnCameraStatusChanged(const CameraStatusInfo & cameraStatusInfo) const867 void CameraManagerAdapterCallback::OnCameraStatusChanged(const CameraStatusInfo &cameraStatusInfo) const
868 {
869     WVLOG_I("OnCameraStatusChanged: %{public}d", cameraStatusInfo.cameraStatus);
870     CameraStatusAdapter cameraStatusAdapter = GetAdapterCameraStatus(cameraStatusInfo.cameraStatus);
871     if (statusCallback_) {
872         WVLOG_I("adapter status callback");
873         statusCallback_->OnCameraStatusChanged(cameraStatusAdapter);
874     }
875     return;
876 }
877 
OnFlashlightStatusChanged(const std::string & cameraID,const FlashStatus flashStatus) const878 void CameraManagerAdapterCallback::OnFlashlightStatusChanged(const std::string &cameraID,
879                                                              const FlashStatus flashStatus) const
880 {
881     return;
882 }
883 }  // namespace OHOS::NWeb