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 cameraInput_->Release();
282 cameraInput_= nullptr;
283 return result;
284 }
285 deviceId_ = deviceId;
286 inputInitedFlag_ = true;
287 }
288
289 result = CAMERA_OK;
290 return result;
291 }
292
InitPreviewOutput(const VideoCaptureParamsAdapter & captureParams,std::shared_ptr<CameraBufferListenerAdapter> listener)293 int32_t CameraManagerAdapterImpl::InitPreviewOutput(const VideoCaptureParamsAdapter &captureParams,
294 std::shared_ptr<CameraBufferListenerAdapter> listener)
295 {
296 int32_t result = CAMERA_ERROR;
297 Size previewSize;
298
299 if (!inputInitedFlag_) {
300 WVLOG_E("input is not inited");
301 return result;
302 }
303
304 if (cameraManager_ == nullptr) {
305 WVLOG_E("camera manager is null");
306 return result;
307 }
308
309 if (previewOutput_ == nullptr) {
310 WVLOG_I("preview output create");
311 previewSurface_ = IConsumerSurface::Create();
312 if (previewSurface_ == nullptr) {
313 WVLOG_E("previewSurface_ is null");
314 return result;
315 }
316 previewSize.width = captureParams.captureFormat.width;
317 previewSize.height = captureParams.captureFormat.height;
318 previewSurface_->SetDefaultWidthAndHeight(previewSize.width, previewSize.height);
319 previewSurface_->SetUserData(CameraManager::surfaceFormat,
320 std::to_string(TransToOriCameraFormat(captureParams.captureFormat.pixelFormat)));
321 Profile previewproFile = Profile(static_cast<CameraFormat>(
322 TransToOriCameraFormat(captureParams.captureFormat.pixelFormat)),
323 previewSize);
324 WVLOG_I("preview output format: %{public}d, w: %{public}d, h: %{public}d",
325 TransToOriCameraFormat(captureParams.captureFormat.pixelFormat), previewSize.width, previewSize.height);
326 previewSurfaceListener_ = new(std::nothrow) CameraSurfaceListener(SurfaceType::PREVIEW,
327 previewSurface_,
328 (listener));
329 previewSurface_->RegisterConsumerListener((sptr<IBufferConsumerListener> &)previewSurfaceListener_);
330 sptr<IBufferProducer> bp = previewSurface_->GetProducer();
331 sptr<Surface> pSurface = Surface::CreateSurfaceAsProducer(bp);
332 previewOutput_ = cameraManager_->CreatePreviewOutput(previewproFile, pSurface);
333 if (previewOutput_ == nullptr) {
334 WVLOG_E("Failed to create previewOutput");
335 return result;
336 }
337 captureParams_ = captureParams;
338 listener_ = listener;
339 }
340 result = CAMERA_OK;
341 return result;
342 }
343
TransToAdapterExposureModes(std::vector<ExposureMode> & exposureModes,std::vector<ExposureModeAdapter> & exposureModesAdapter)344 int32_t CameraManagerAdapterImpl::TransToAdapterExposureModes(
345 std::vector<ExposureMode>& exposureModes,
346 std::vector<ExposureModeAdapter>& exposureModesAdapter)
347 {
348 for (auto exportMode : exposureModes) {
349 exposureModesAdapter.push_back(GetAdapterExposureMode(exportMode));
350 }
351
352 return CAMERA_OK;
353 }
354
GetExposureModes(std::vector<ExposureModeAdapter> & exposureModesAdapter)355 int32_t CameraManagerAdapterImpl::GetExposureModes(std::vector<ExposureModeAdapter>& exposureModesAdapter)
356 {
357 std::lock_guard<std::mutex> lock(mutex_);
358 if (captureSession_ == nullptr) {
359 WVLOG_E("captureSession is nullptr when get exposure modes");
360 return CAMERA_ERROR;
361 }
362 std::vector<ExposureMode> exposureModes;
363 if (captureSession_->GetSupportedExposureModes(exposureModes) != SUCCESS) {
364 WVLOG_E("get inner exposure modes faileds");
365 return CAMERA_ERROR;
366 }
367
368 TransToAdapterExposureModes(exposureModes, exposureModesAdapter);
369
370 return CAMERA_OK;
371 }
372
GetCurrentExposureMode(ExposureModeAdapter & exposureModeAdapter)373 int32_t CameraManagerAdapterImpl::GetCurrentExposureMode(ExposureModeAdapter& exposureModeAdapter)
374 {
375 std::lock_guard<std::mutex> lock(mutex_);
376 if (captureSession_ == nullptr) {
377 WVLOG_E("captureSession is nullptr when get current exposure modes");
378 return CAMERA_ERROR;
379 }
380
381 ExposureMode exposureMode = captureSession_->GetExposureMode();
382 exposureModeAdapter = GetAdapterExposureMode(exposureMode);
383 return CAMERA_OK;
384 }
385
GetExposureCompensation(VideoCaptureRangeAdapter & rangeVal)386 int32_t CameraManagerAdapterImpl::GetExposureCompensation(VideoCaptureRangeAdapter& rangeVal)
387 {
388 if (captureSession_ == nullptr) {
389 return CAMERA_ERROR;
390 }
391 std::vector<float> exposureBiasRange = captureSession_->GetExposureBiasRange();
392 int32_t exposureCompos = captureSession_->GetExposureValue();
393 if (exposureBiasRange.size() == RANGE_MAX_SIZE) {
394 rangeVal.min = exposureBiasRange.at(RANGE_MIN_INDEX);
395 rangeVal.max = exposureBiasRange.at(RANGE_MAX_INDEX);
396 }
397
398 rangeVal.current = exposureCompos;
399 return CAMERA_OK;
400 }
401
GetCaptionRangeById(RangeIDAdapter rangeId,VideoCaptureRangeAdapter & rangeVal)402 int32_t CameraManagerAdapterImpl::GetCaptionRangeById(RangeIDAdapter rangeId, VideoCaptureRangeAdapter& rangeVal)
403 {
404 std::lock_guard<std::mutex> lock(mutex_);
405 if (captureSession_ == nullptr) {
406 WVLOG_E("captureSession is nullptr when get %{public}d range info", rangeId);
407 return CAMERA_ERROR;
408 }
409 int32_t result = CAMERA_OK;
410 switch (rangeId) {
411 case RangeIDAdapter::RANGE_ID_EXP_COMPENSATION:
412 if (GetExposureCompensation(rangeVal) != CAMERA_OK) {
413 WVLOG_E("get exposure compensation failed.");
414 result = CAMERA_ERROR;
415 }
416 break;
417 default:
418 result = CAMERA_ERROR;
419 break;
420 }
421 return result;
422 }
423
IsFocusModeSupported(FocusModeAdapter focusMode)424 bool CameraManagerAdapterImpl::IsFocusModeSupported(FocusModeAdapter focusMode)
425 {
426 std::lock_guard<std::mutex> lock(mutex_);
427 if (captureSession_ == nullptr) {
428 WVLOG_E("captureSession is nullptr when get support focuc mode");
429 return false;
430 }
431 if (!captureSession_->IsFocusModeSupported(GetOriFocusMode(focusMode))) {
432 return false;
433 }
434 return true;
435 }
436
GetCurrentFocusMode()437 FocusModeAdapter CameraManagerAdapterImpl::GetCurrentFocusMode()
438 {
439 std::lock_guard<std::mutex> lock(mutex_);
440 if (captureSession_ == nullptr) {
441 WVLOG_E("captureSession is nullptr when get support focuc mode");
442 return FocusModeAdapter::FOCUS_MODE_MANUAL;
443 }
444
445 FocusMode oriFocusMode = captureSession_->GetFocusMode();
446 return GetAdapterFocusMode(oriFocusMode);
447 }
448
IsFlashModeSupported(FlashModeAdapter focusMode)449 bool CameraManagerAdapterImpl::IsFlashModeSupported(FlashModeAdapter focusMode)
450 {
451 std::lock_guard<std::mutex> lock(mutex_);
452 if (captureSession_ == nullptr) {
453 WVLOG_E("captureSession is nullptr when get support flash mode");
454 return false;
455 }
456 if (!captureSession_->IsFlashModeSupported(GetOriFlashMode(focusMode))) {
457 return false;
458 }
459 return true;
460 }
461
CreateAndStartSession()462 int32_t CameraManagerAdapterImpl::CreateAndStartSession()
463 {
464 int32_t result = CAMERA_ERROR;
465 if (status_ == CameraStatusAdapter::UNAVAILABLE) {
466 WVLOG_E("camera is already opened");
467 return result;
468 }
469
470 if ((cameraInput_ == nullptr) || (previewOutput_ == nullptr)) {
471 WVLOG_E("cameraInput_ or previewOutput_ is null");
472 return result;
473 }
474
475 WVLOG_I("CreateCaptureSession");
476 captureSession_ = cameraManager_->CreateCaptureSession();
477 if (captureSession_ == nullptr) {
478 WVLOG_E("Failed to create capture session");
479 return result;
480 }
481 WVLOG_I("BeginConfig");
482 captureSession_->BeginConfig();
483 WVLOG_I("AddInput");
484
485 result = captureSession_->AddInput(cameraInput_);
486 if (result != CAMERA_OK) {
487 WVLOG_E("Failed to add in put");
488 return result;
489 }
490
491 WVLOG_I("AddOutput");
492 result = captureSession_->AddOutput(previewOutput_);
493 if (result != CAMERA_OK) {
494 WVLOG_E("Failed to add preview output");
495 return result;
496 }
497 WVLOG_I("CommitConfig");
498 result = captureSession_->CommitConfig();
499 if (result != CAMERA_OK) {
500 WVLOG_E("Failed to commit config");
501 return result;
502 }
503 WVLOG_I("Start");
504 result = captureSession_->Start();
505 if (result != CAMERA_OK) {
506 WVLOG_E("Failed to start session");
507 return result;
508 }
509 result = CAMERA_OK;
510 status_ = CameraStatusAdapter::UNAVAILABLE;
511 isCapturing_ = true;
512 return result;
513 }
514
RestartSession()515 int32_t CameraManagerAdapterImpl::RestartSession()
516 {
517 std::lock_guard<std::mutex> lock(restart_mutex_);
518 WVLOG_I("RestartSession %{public}s", deviceId_.c_str());
519 if (!isCapturing_) {
520 WVLOG_E("this web tab is not capturing");
521 return CAMERA_OK;
522 }
523
524 if (cameraManager_ == nullptr) {
525 WVLOG_E("cameraManager_ is null when start session");
526 return CAMERA_ERROR;
527 }
528
529 if (cameraInput_ != nullptr) {
530 cameraInput_->Release();
531 cameraInput_ = nullptr;
532 }
533
534 if (previewOutput_ != nullptr) {
535 ((sptr<PreviewOutput> &)previewOutput_)->Stop();
536 previewOutput_->Release();
537 previewOutput_ = nullptr;
538 }
539
540 previewSurface_ = nullptr;
541 previewSurfaceListener_ = nullptr;
542 inputInitedFlag_ = false;
543 captureSession_ = nullptr;
544 status_ = CameraStatusAdapter::AVAILABLE;
545
546 if (StartStream(deviceId_, captureParams_, listener_) != CAMERA_OK) {
547 WVLOG_E("restart stream failed");
548 ReleaseSessionResource(deviceId_);
549 ReleaseSession();
550 return CAMERA_ERROR;
551 }
552 status_ = CameraStatusAdapter::UNAVAILABLE;
553 return CAMERA_OK;
554 }
555
StopSession(CameraStopType stopType)556 int32_t CameraManagerAdapterImpl::StopSession(CameraStopType stopType)
557 {
558 std::lock_guard<std::mutex> lock(mutex_);
559 WVLOG_I("StopSession");
560 if (status_ == CameraStatusAdapter::AVAILABLE) {
561 WVLOG_E("camera is already closed when stop session");
562 return CAMERA_OK;
563 }
564 ReleaseSessionResource(deviceId_);
565 ReleaseSession();
566
567 if (stopType == CameraStopType::NORMAL) {
568 isCapturing_ = false;
569 }
570 return CAMERA_OK;
571 }
572
ReleaseSession()573 int32_t CameraManagerAdapterImpl::ReleaseSession()
574 {
575 WVLOG_I("release session");
576 if (captureSession_ != nullptr) {
577 captureSession_->Stop();
578 captureSession_->Release();
579 captureSession_ = nullptr;
580 }
581
582 return CAMERA_OK;
583 }
584
ReleaseSessionResource(const std::string & deviceId)585 int32_t CameraManagerAdapterImpl::ReleaseSessionResource(const std::string &deviceId)
586 {
587 WVLOG_I("release session resource");
588 if (deviceId_ != deviceId) {
589 WVLOG_E("deviceId is not used");
590 return CAMERA_OK;
591 }
592 if (cameraInput_ != nullptr) {
593 cameraInput_->Release();
594 cameraInput_ = nullptr;
595 }
596
597 if (previewOutput_ != nullptr) {
598 ((sptr<PreviewOutput> &)previewOutput_)->Stop();
599 previewOutput_->Release();
600 previewOutput_ = nullptr;
601 }
602
603 previewSurface_ = nullptr;
604 previewSurfaceListener_ = nullptr;
605 status_ = CameraStatusAdapter::AVAILABLE;
606 inputInitedFlag_ = false;
607 return CAMERA_OK;
608 }
609
ReleaseCameraManger()610 int32_t CameraManagerAdapterImpl::ReleaseCameraManger()
611 {
612 std::lock_guard<std::mutex> lock(mutex_);
613 WVLOG_I("release camera manger");
614 ReleaseSessionResource(deviceId_);
615 ReleaseSession();
616 cameraManager_ = nullptr;
617 status_ = CameraStatusAdapter::AVAILABLE;
618 inputInitedFlag_ = false;
619 isForegound_ = false;
620 cameraMngrCallback_ = nullptr;
621 return CAMERA_OK;
622 }
623
GetCameraStatus()624 CameraStatusAdapter CameraManagerAdapterImpl::GetCameraStatus()
625 {
626 return status_;
627 }
628
SetCameraStatus(CameraStatusAdapter status)629 void CameraManagerAdapterImpl::SetCameraStatus(CameraStatusAdapter status)
630 {
631 std::lock_guard<std::mutex> lock(mutex_);
632 WVLOG_I("set camera status %{public}d", status);
633 status_ = status;
634 }
635
GetCurrentDeviceId()636 std::string CameraManagerAdapterImpl::GetCurrentDeviceId()
637 {
638 return deviceId_;
639 }
640
IsExistCaptureTask()641 bool CameraManagerAdapterImpl::IsExistCaptureTask()
642 {
643 if (cameraManager_ == nullptr) {
644 WVLOG_E("cameraManager_ is nullptr");
645 return false;
646 }
647 return isCapturing_;
648 }
649
SetForegroundFlag(bool isForeground)650 void CameraManagerAdapterImpl::SetForegroundFlag(bool isForeground)
651 {
652 isForegound_ = isForeground;
653 }
654
655
StartStream(const std::string & deviceId,const VideoCaptureParamsAdapter & captureParams,std::shared_ptr<CameraBufferListenerAdapter> listener)656 int32_t CameraManagerAdapterImpl::StartStream(const std::string &deviceId,
657 const VideoCaptureParamsAdapter &captureParams,
658 std::shared_ptr<CameraBufferListenerAdapter> listener)
659 {
660 std::lock_guard<std::mutex> lock(mutex_);
661
662 if ((cameraManager_ == nullptr) || (listener == nullptr)) {
663 WVLOG_E("cameraManager or listener is null when start session");
664 return CAMERA_ERROR;
665 }
666
667 if (InitCameraInput(deviceId) != CAMERA_OK) {
668 WVLOG_E("init camera input failed");
669 ReleaseSessionResource(deviceId);
670 return CAMERA_ERROR;
671 }
672
673 if (InitPreviewOutput(captureParams, listener) != CAMERA_OK) {
674 WVLOG_E("init camera preview output failed");
675 ReleaseSessionResource(deviceId);
676 return CAMERA_ERROR;
677 }
678
679 if (CreateAndStartSession() != CAMERA_OK) {
680 WVLOG_E("create session failed");
681 ReleaseSession();
682 return CAMERA_ERROR;
683 }
684
685 return CAMERA_OK;
686 }
687
CameraSurfaceBufferAdapterImpl(sptr<SurfaceBuffer> buffer)688 CameraSurfaceBufferAdapterImpl::CameraSurfaceBufferAdapterImpl(sptr<SurfaceBuffer> buffer) : buffer_(buffer) {}
689
GetFileDescriptor() const690 int32_t CameraSurfaceBufferAdapterImpl::GetFileDescriptor() const
691 {
692 if (!buffer_) {
693 WVLOG_E("buffer_ is nullptr");
694 return -1;
695 }
696 return buffer_->GetFileDescriptor();
697 }
698
GetWidth() const699 int32_t CameraSurfaceBufferAdapterImpl::GetWidth() const
700 {
701 if (!buffer_) {
702 WVLOG_E("buffer_ is nullptr");
703 return -1;
704 }
705 return buffer_->GetWidth();
706 }
707
GetHeight() const708 int32_t CameraSurfaceBufferAdapterImpl::GetHeight() const
709 {
710 if (!buffer_) {
711 WVLOG_E("buffer_ is nullptr");
712 return -1;
713 }
714 return buffer_->GetHeight();
715 }
716
GetStride() const717 int32_t CameraSurfaceBufferAdapterImpl::GetStride() const
718 {
719 if (!buffer_) {
720 WVLOG_E("buffer_ is nullptr");
721 return -1;
722 }
723 return buffer_->GetStride();
724 }
725
GetFormat() const726 int32_t CameraSurfaceBufferAdapterImpl::GetFormat() const
727 {
728 if (!buffer_) {
729 WVLOG_E("buffer_ is nullptr");
730 return -1;
731 }
732 return buffer_->GetFormat();
733 }
734
GetSize() const735 uint32_t CameraSurfaceBufferAdapterImpl::GetSize() const
736 {
737 if (!buffer_) {
738 WVLOG_E("buffer_ is nullptr");
739 return 0;
740 }
741 return buffer_->GetSize();
742 }
743
GetBuffer()744 sptr<SurfaceBuffer>& CameraSurfaceBufferAdapterImpl::GetBuffer()
745 {
746 return buffer_;
747 }
748
GetBufferAddr()749 uint8_t* CameraSurfaceBufferAdapterImpl::GetBufferAddr()
750 {
751 if (!buffer_) {
752 WVLOG_E("buffer_ is nullptr");
753 return 0;
754 }
755 return static_cast<uint8_t *>(buffer_->GetVirAddr());
756 }
757
CameraSurfaceListener(SurfaceType type,sptr<IConsumerSurface> surface,std::shared_ptr<CameraBufferListenerAdapter> listener)758 CameraSurfaceListener::CameraSurfaceListener(SurfaceType type,
759 sptr<IConsumerSurface> surface,
760 std::shared_ptr<CameraBufferListenerAdapter> listener)
761 : surfaceType_(type), surface_(surface), listener_(listener) {
762 }
763
FillRotationInfo(int roration,bool isFlipX,bool isFlipY)764 CameraRotationInfo CameraSurfaceListener::FillRotationInfo(int roration, bool isFlipX, bool isFlipY)
765 {
766 CameraRotationInfo rotationInfo;
767 rotationInfo.rotation = roration;
768 rotationInfo.isFlipX = isFlipX;
769 rotationInfo.isFlipY = isFlipY;
770 return rotationInfo;
771 }
772
GetRotationInfo(GraphicTransformType transform)773 CameraRotationInfo CameraSurfaceListener::GetRotationInfo(GraphicTransformType transform)
774 {
775 switch (transform) {
776 case GraphicTransformType::GRAPHIC_ROTATE_NONE: {
777 return FillRotationInfo(ROTATION_0, false, false);
778 }
779 case GraphicTransformType::GRAPHIC_ROTATE_90: {
780 return FillRotationInfo(ROTATION_90, false, false);
781 }
782 case GraphicTransformType::GRAPHIC_ROTATE_180: {
783 return FillRotationInfo(ROTATION_180, false, false);
784 }
785 case GraphicTransformType::GRAPHIC_ROTATE_270: {
786 return FillRotationInfo(ROTATION_270, false, false);
787 }
788 case GraphicTransformType::GRAPHIC_FLIP_H: {
789 return FillRotationInfo(ROTATION_0, false, true);
790 }
791 case GraphicTransformType::GRAPHIC_FLIP_V: {
792 return FillRotationInfo(ROTATION_0, true, false);
793 }
794 case GraphicTransformType::GRAPHIC_FLIP_H_ROT90: {
795 return FillRotationInfo(ROTATION_90, false, true);
796 }
797 case GraphicTransformType::GRAPHIC_FLIP_V_ROT90: {
798 return FillRotationInfo(ROTATION_90, true, false);
799 }
800 case GraphicTransformType::GRAPHIC_FLIP_H_ROT180: {
801 return FillRotationInfo(ROTATION_180, false, true);
802 }
803 case GraphicTransformType::GRAPHIC_FLIP_V_ROT180: {
804 return FillRotationInfo(ROTATION_180, true, false);
805 }
806 case GraphicTransformType::GRAPHIC_FLIP_H_ROT270: {
807 return FillRotationInfo(ROTATION_270, false, true);
808 }
809 case GraphicTransformType::GRAPHIC_FLIP_V_ROT270: {
810 return FillRotationInfo(ROTATION_270, true, false);
811 }
812 default: {
813 return FillRotationInfo(ROTATION_0, false, false);
814 }
815 }
816 }
817
OnBufferAvailable()818 void CameraSurfaceListener::OnBufferAvailable()
819 {
820 int32_t flushFence = 0;
821 int64_t timestamp = 0;
822 OHOS::Rect damage;
823 OHOS::sptr<OHOS::SurfaceBuffer> buffer = nullptr;
824 if (surface_ == nullptr) {
825 WVLOG_E("OnBufferAvailable:surface_ is null");
826 return;
827 }
828 surface_->AcquireBuffer(buffer, flushFence, timestamp, damage);
829 if (buffer != nullptr) {
830 uint32_t size = buffer->GetSize();
831
832 CameraRotationInfo rotationInfo = GetRotationInfo(surface_->GetTransform());
833 WVLOG_D("OnBufferAvailable, surfaceType_: %{public}d, size: %{public}d, width: %{public}d,\
834 height: %{public}d, type: %{public}d, ratation: %{public}d, FilyY: %{public}d, FilyX: %{public}d",
835 surfaceType_, size, buffer->GetWidth(), buffer->GetHeight(), surface_->GetTransform(),
836 (int32_t)rotationInfo.rotation, rotationInfo.isFlipY, rotationInfo.isFlipX);
837 auto bufferAdapter = std::make_unique<CameraSurfaceBufferAdapterImpl>(buffer);
838 auto surfaceAdapter = std::make_shared<CameraSurfaceAdapterImpl>(surface_);
839 if (listener_ != nullptr) {
840 listener_->OnBufferAvailable(surfaceAdapter, std::move(bufferAdapter), rotationInfo);
841 }
842 } else {
843 WVLOG_E("AcquireBuffer failed!");
844 }
845 }
846
CameraSurfaceAdapterImpl(sptr<IConsumerSurface> surface)847 CameraSurfaceAdapterImpl::CameraSurfaceAdapterImpl(sptr<IConsumerSurface> surface) : cSurface_(surface) {}
848
ReleaseBuffer(std::unique_ptr<CameraSurfaceBufferAdapter> bufferAdapter,int32_t fence)849 int32_t CameraSurfaceAdapterImpl::ReleaseBuffer(std::unique_ptr<CameraSurfaceBufferAdapter> bufferAdapter,
850 int32_t fence)
851 {
852 if (!cSurface_ || !bufferAdapter) {
853 WVLOG_E("cSurface_ or bufferAdapter is nullptr");
854 return -1;
855 }
856 auto bufferImpl = static_cast<CameraSurfaceBufferAdapterImpl*>(bufferAdapter.get());
857 return cSurface_->ReleaseBuffer(bufferImpl->GetBuffer(), fence);
858 }
859
CameraManagerAdapterCallback(std::shared_ptr<CameraStatusCallbackAdapter> cameraStatusCallback)860 CameraManagerAdapterCallback::CameraManagerAdapterCallback(std::shared_ptr<CameraStatusCallbackAdapter>
861 cameraStatusCallback): statusCallback_(cameraStatusCallback)
862 {
863 WVLOG_I("Create CameraManagerAdapterCallback");
864 }
865
GetAdapterCameraStatus(CameraStatus status) const866 CameraStatusAdapter CameraManagerAdapterCallback::GetAdapterCameraStatus(CameraStatus status) const
867 {
868 auto item = CAMERA_STATUS_MAP.find(status);
869 if (item == CAMERA_STATUS_MAP.end()) {
870 WVLOG_E("ori camera status %{public}d not found", status);
871 return CameraStatusAdapter::APPEAR;
872 }
873 return item->second;
874 }
875
OnCameraStatusChanged(const CameraStatusInfo & cameraStatusInfo) const876 void CameraManagerAdapterCallback::OnCameraStatusChanged(const CameraStatusInfo &cameraStatusInfo) const
877 {
878 std::string callbackDeviceId;
879 if (cameraStatusInfo.cameraDevice) {
880 callbackDeviceId = cameraStatusInfo.cameraDevice->GetID();
881 }
882 std::string currentDeviceId = CameraManagerAdapterImpl::GetInstance().GetCurrentDeviceId();
883
884 WVLOG_I("OnCameraStatusChanged: callbackdeviceID %{public}s, currentDeviceId:%{public}s, status %{public}d",
885 callbackDeviceId.c_str(), currentDeviceId.c_str(), cameraStatusInfo.cameraStatus);
886 CameraStatusAdapter cameraStatusAdapter = GetAdapterCameraStatus(cameraStatusInfo.cameraStatus);
887
888 if (statusCallback_) {
889 switch (cameraStatusAdapter) {
890 case CameraStatusAdapter::AVAILABLE:
891 WVLOG_I("do not handle status AVAILABLE");
892 return;
893 case CameraStatusAdapter::UNAVAILABLE:
894 WVLOG_I("do not handle status UNAVAILABLE");
895 return;
896 case CameraStatusAdapter::APPEAR:
897 case CameraStatusAdapter::DISAPPEAR:
898 break;
899 default:
900 WVLOG_I("unknow status");
901 return;
902 }
903 WVLOG_I("start do statusCallback");
904 statusCallback_->OnCameraStatusChanged(cameraStatusAdapter, callbackDeviceId);
905 }
906 return;
907 }
908
OnFlashlightStatusChanged(const std::string & cameraID,const FlashStatus flashStatus) const909 void CameraManagerAdapterCallback::OnFlashlightStatusChanged(const std::string &cameraID,
910 const FlashStatus flashStatus) const
911 {
912 return;
913 }
914 } // namespace OHOS::NWeb