1 /*
2 * Copyright (c) 2021-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 "input/camera_manager.h"
17 #include <cstring>
18
19 #include "camera_util.h"
20 #include "ipc_skeleton.h"
21 #include "iservice_registry.h"
22 #include "camera_log.h"
23 #include "system_ability_definition.h"
24 #include "camera_error_code.h"
25 #include "icamera_util.h"
26
27 using namespace std;
28 namespace OHOS {
29 namespace CameraStandard {
30 namespace {
31 constexpr uint32_t UNIT_LENGTH = 3;
32 }
33 sptr<CameraManager> CameraManager::cameraManager_;
34
35 const std::string CameraManager::surfaceFormat = "CAMERA_SURFACE_FORMAT";
36
37 const std::unordered_map<camera_format_t, CameraFormat> CameraManager::metaToFwCameraFormat_ = {
38 {OHOS_CAMERA_FORMAT_YCRCB_420_SP, CAMERA_FORMAT_YUV_420_SP},
39 {OHOS_CAMERA_FORMAT_JPEG, CAMERA_FORMAT_JPEG},
40 {OHOS_CAMERA_FORMAT_RGBA_8888, CAMERA_FORMAT_RGBA_8888}
41 };
42
43 const std::unordered_map<CameraFormat, camera_format_t> CameraManager::fwToMetaCameraFormat_ = {
44 {CAMERA_FORMAT_YUV_420_SP, OHOS_CAMERA_FORMAT_YCRCB_420_SP},
45 {CAMERA_FORMAT_JPEG, OHOS_CAMERA_FORMAT_JPEG},
46 {CAMERA_FORMAT_RGBA_8888, OHOS_CAMERA_FORMAT_RGBA_8888},
47 };
48
CameraManager()49 CameraManager::CameraManager()
50 {
51 Init();
52 cameraObjList = {};
53 dcameraObjList = {};
54 }
55
~CameraManager()56 CameraManager::~CameraManager()
57 {
58 serviceProxy_ = nullptr;
59 listenerStub_ = nullptr;
60 deathRecipient_ = nullptr;
61 cameraSvcCallback_ = nullptr;
62 cameraMuteSvcCallback_ = nullptr;
63 cameraMngrCallback_ = nullptr;
64 for (unsigned int i = 0; i < cameraMuteListenerList.size(); i++) {
65 if (cameraMuteListenerList[i]) {
66 cameraMuteListenerList[i] = nullptr;
67 }
68 }
69 cameraMuteListenerList.clear();
70 for (unsigned int i = 0; i < cameraObjList.size(); i++) {
71 cameraObjList[i] = nullptr;
72 }
73 cameraObjList.clear();
74 dcameraObjList.clear();
75 CameraManager::cameraManager_ = nullptr;
76 }
77
CreateListenerObject()78 int32_t CameraManager::CreateListenerObject()
79 {
80 listenerStub_ = new(std::nothrow) CameraListenerStub();
81 CHECK_AND_RETURN_RET_LOG(listenerStub_ != nullptr, CAMERA_ALLOC_ERROR,
82 "failed to new CameraListenerStub object");
83 CHECK_AND_RETURN_RET_LOG(serviceProxy_ != nullptr, CAMERA_ALLOC_ERROR,
84 "Camera service does not exist.");
85
86 sptr<IRemoteObject> object = listenerStub_->AsObject();
87 CHECK_AND_RETURN_RET_LOG(object != nullptr, CAMERA_ALLOC_ERROR, "listener object is nullptr..");
88
89 MEDIA_DEBUG_LOG("CreateListenerObject");
90 return serviceProxy_->SetListenerObject(object);
91 }
92
93 class CameraStatusServiceCallback : public HCameraServiceCallbackStub {
94 public:
95 sptr<CameraManager> camMngr_ = nullptr;
CameraStatusServiceCallback()96 CameraStatusServiceCallback() : camMngr_(nullptr) {
97 }
98
CameraStatusServiceCallback(const sptr<CameraManager> & cameraManager)99 explicit CameraStatusServiceCallback(const sptr<CameraManager>& cameraManager) : camMngr_(cameraManager) {
100 }
101
~CameraStatusServiceCallback()102 ~CameraStatusServiceCallback()
103 {
104 camMngr_ = nullptr;
105 }
106
OnCameraStatusChanged(const std::string & cameraId,const CameraStatus status)107 int32_t OnCameraStatusChanged(const std::string& cameraId, const CameraStatus status) override
108 {
109 MEDIA_INFO_LOG("CameraStatusServiceCallback::OnCameraStatusChanged: cameraId: %{public}s, status: %{public}d",
110 cameraId.c_str(), status);
111 CameraStatusInfo cameraStatusInfo;
112 if (camMngr_ != nullptr && camMngr_->GetApplicationCallback() != nullptr) {
113 cameraStatusInfo.cameraDevice = camMngr_->GetCameraDeviceFromId(cameraId);
114 cameraStatusInfo.cameraStatus = status;
115 if (cameraStatusInfo.cameraDevice) {
116 MEDIA_INFO_LOG("OnCameraStatusChanged: cameraId: %{public}s, status: %{public}d",
117 cameraId.c_str(), status);
118 camMngr_->GetApplicationCallback()->OnCameraStatusChanged(cameraStatusInfo);
119 }
120 } else {
121 MEDIA_INFO_LOG("CameraManager::Callback not registered!, Ignore the callback");
122 }
123 return CAMERA_OK;
124 }
125
OnFlashlightStatusChanged(const std::string & cameraId,const FlashStatus status)126 int32_t OnFlashlightStatusChanged(const std::string& cameraId, const FlashStatus status) override
127 {
128 MEDIA_INFO_LOG("OnFlashlightStatusChanged: "
129 "cameraId: %{public}s, status: %{public}d", cameraId.c_str(), status);
130 if (camMngr_ != nullptr && camMngr_->GetApplicationCallback() != nullptr) {
131 camMngr_->GetApplicationCallback()->OnFlashlightStatusChanged(cameraId, status);
132 } else {
133 MEDIA_INFO_LOG("CameraManager::Callback not registered!, Ignore the callback");
134 }
135 return CAMERA_OK;
136 }
137 };
138
CreateCaptureSession()139 sptr<CaptureSession> CameraManager::CreateCaptureSession()
140 {
141 CAMERA_SYNC_TRACE;
142 sptr<CaptureSession> captureSession = nullptr;
143 int ret = CreateCaptureSession(&captureSession);
144 if (ret != CameraErrorCode::SUCCESS) {
145 MEDIA_ERR_LOG("Failed to CreateCaptureSession with error code:%{public}d", ret);
146 return nullptr;
147 }
148
149 return captureSession;
150 }
151
CreateCaptureSession(sptr<CaptureSession> * pCaptureSession)152 int CameraManager::CreateCaptureSession(sptr<CaptureSession> *pCaptureSession)
153 {
154 CAMERA_SYNC_TRACE;
155 sptr<ICaptureSession> session = nullptr;
156 sptr<CaptureSession> captureSession = nullptr;
157 int32_t retCode = CAMERA_OK;
158
159 if (serviceProxy_ == nullptr) {
160 MEDIA_ERR_LOG("CameraManager::CreateCaptureSession serviceProxy_ is null");
161 return CameraErrorCode::INVALID_ARGUMENT;
162 }
163
164 retCode = serviceProxy_->CreateCaptureSession(session);
165 if (retCode == CAMERA_OK) {
166 if (session != nullptr) {
167 captureSession = new(std::nothrow) CaptureSession(session);
168 if (captureSession == nullptr) {
169 MEDIA_ERR_LOG("Failed to new CaptureSession");
170 return CameraErrorCode::SERVICE_FATL_ERROR;
171 }
172 } else {
173 MEDIA_ERR_LOG("Failed to CreateCaptureSession with session is null");
174 return CameraErrorCode::SERVICE_FATL_ERROR;
175 }
176 } else {
177 MEDIA_ERR_LOG("Failed to get capture session object from hcamera service!, %{public}d", retCode);
178 return ServiceToCameraError(retCode);
179 }
180
181 *pCaptureSession = captureSession;
182
183 return CameraErrorCode::SUCCESS;
184 }
185
CreatePhotoOutput(sptr<Surface> & surface)186 sptr<PhotoOutput> CameraManager::CreatePhotoOutput(sptr<Surface> &surface)
187 {
188 CAMERA_SYNC_TRACE;
189 sptr<PhotoOutput> result = nullptr;
190 return result;
191 }
192
CreatePhotoOutput(Profile & profile,sptr<Surface> & surface)193 sptr<PhotoOutput> CameraManager::CreatePhotoOutput(Profile &profile, sptr<Surface> &surface)
194 {
195 CAMERA_SYNC_TRACE;
196 sptr<PhotoOutput> photoOutput = nullptr;
197 int ret = CreatePhotoOutput(profile, surface, &photoOutput);
198 if (ret != CameraErrorCode::SUCCESS) {
199 MEDIA_ERR_LOG("Failed to CreatePhotoOutput with error code:%{public}d", ret);
200 return nullptr;
201 }
202
203 return photoOutput;
204 }
205
CreatePhotoOutput(Profile & profile,sptr<Surface> & surface,sptr<PhotoOutput> * pPhotoOutput)206 int CameraManager::CreatePhotoOutput(Profile &profile, sptr<Surface> &surface, sptr<PhotoOutput> *pPhotoOutput)
207 {
208 CAMERA_SYNC_TRACE;
209 sptr<IStreamCapture> streamCapture = nullptr;
210 sptr<PhotoOutput> photoOutput = nullptr;
211 int32_t retCode = CAMERA_OK;
212 camera_format_t metaFormat;
213
214 if ((serviceProxy_ == nullptr) || (surface == nullptr)) {
215 MEDIA_ERR_LOG("CameraManager::CreatePhotoOutput serviceProxy_ is null or surface/profile is null");
216 return CameraErrorCode::INVALID_ARGUMENT;
217 }
218
219 if ((profile.GetCameraFormat() == CAMERA_FORMAT_INVALID) ||
220 (profile.GetSize().width == 0) ||
221 (profile.GetSize().height == 0)) {
222 MEDIA_ERR_LOG("CameraManager::CreatePhotoOutput invalid fomrat or width or height is zero");
223 return CameraErrorCode::INVALID_ARGUMENT;
224 }
225
226 metaFormat = GetCameraMetadataFormat(profile.GetCameraFormat());
227 retCode = serviceProxy_->CreatePhotoOutput(surface->GetProducer(), metaFormat, profile.GetSize().width,
228 profile.GetSize().height, streamCapture);
229 if (retCode == CAMERA_OK) {
230 photoOutput = new(std::nothrow) PhotoOutput(streamCapture);
231 if (photoOutput == nullptr) {
232 MEDIA_ERR_LOG("Failed to new PhotoOutput ");
233 return CameraErrorCode::SERVICE_FATL_ERROR;
234 } else {
235 POWERMGR_SYSEVENT_CAMERA_CONFIG(PHOTO, profile.GetSize().width,
236 profile.GetSize().height);
237 }
238 } else {
239 MEDIA_ERR_LOG("Failed to get stream capture object from hcamera service!, %{public}d", retCode);
240 return ServiceToCameraError(retCode);
241 }
242
243 *pPhotoOutput = photoOutput;
244
245 return CameraErrorCode::SUCCESS;
246 }
247
CreatePreviewOutput(Profile & profile,sptr<Surface> surface)248 sptr<PreviewOutput> CameraManager::CreatePreviewOutput(Profile &profile, sptr<Surface> surface)
249 {
250 CAMERA_SYNC_TRACE;
251 sptr<PreviewOutput> previewOutput = nullptr;
252 int ret = CreatePreviewOutput(profile, surface, &previewOutput);
253 if (ret != CameraErrorCode::SUCCESS) {
254 MEDIA_ERR_LOG("Failed to CreatePreviewOutput with error code:%{public}d", ret);
255 return nullptr;
256 }
257
258 return previewOutput;
259 }
260
CreatePreviewOutput(Profile & profile,sptr<Surface> surface,sptr<PreviewOutput> * pPreviewOutput)261 int CameraManager::CreatePreviewOutput(Profile &profile, sptr<Surface> surface, sptr<PreviewOutput> *pPreviewOutput)
262 {
263 CAMERA_SYNC_TRACE;
264 sptr<IStreamRepeat> streamRepeat = nullptr;
265 sptr<PreviewOutput> previewOutput = nullptr;
266 int32_t retCode = CAMERA_OK;
267 camera_format_t metaFormat;
268
269 if ((serviceProxy_ == nullptr) || (surface == nullptr)) {
270 MEDIA_ERR_LOG("CameraManager::CreatePhotoOutput serviceProxy_ is null or surface/profile is null");
271 return CameraErrorCode::INVALID_ARGUMENT;
272 }
273
274 if ((profile.GetCameraFormat() == CAMERA_FORMAT_INVALID) ||
275 (profile.GetSize().width == 0) ||
276 (profile.GetSize().height == 0)) {
277 MEDIA_ERR_LOG("CameraManager::CreatePhotoOutput width or height is zero");
278 return CameraErrorCode::INVALID_ARGUMENT;
279 }
280
281 metaFormat = GetCameraMetadataFormat(profile.GetCameraFormat());
282 retCode = serviceProxy_->CreatePreviewOutput(surface->GetProducer(), metaFormat,
283 profile.GetSize().width, profile.GetSize().height, streamRepeat);
284 if (retCode == CAMERA_OK) {
285 previewOutput = new(std::nothrow) PreviewOutput(streamRepeat);
286 if (previewOutput == nullptr) {
287 MEDIA_ERR_LOG("Failed to new PreviewOutput");
288 return CameraErrorCode::SERVICE_FATL_ERROR;
289 } else {
290 POWERMGR_SYSEVENT_CAMERA_CONFIG(PREVIEW,
291 profile.GetSize().width,
292 profile.GetSize().height);
293 }
294 } else {
295 MEDIA_ERR_LOG("PreviewOutput: Failed to get stream repeat object from hcamera service! %{public}d", retCode);
296 return ServiceToCameraError(retCode);
297 }
298
299 *pPreviewOutput = previewOutput;
300
301 return CameraErrorCode::SUCCESS;
302 }
303
CreateDeferredPreviewOutput(Profile & profile)304 sptr<PreviewOutput> CameraManager::CreateDeferredPreviewOutput(Profile &profile)
305 {
306 CAMERA_SYNC_TRACE;
307 sptr<PreviewOutput> previewOutput = nullptr;
308 int ret = CreateDeferredPreviewOutput(profile, &previewOutput);
309 if (ret != CameraErrorCode::SUCCESS) {
310 MEDIA_ERR_LOG("Failed to CreateDeferredPreviewOutput with error code:%{public}d", ret);
311 return nullptr;
312 }
313
314 return previewOutput;
315 }
316
CreateDeferredPreviewOutput(Profile & profile,sptr<PreviewOutput> * pPreviewOutput)317 int CameraManager::CreateDeferredPreviewOutput(Profile &profile, sptr<PreviewOutput> *pPreviewOutput)
318 {
319 CAMERA_SYNC_TRACE;
320 sptr<IStreamRepeat> streamRepeat = nullptr;
321 sptr<PreviewOutput> previewOutput = nullptr;
322 int32_t retCode = CAMERA_OK;
323 camera_format_t metaFormat;
324
325 if ((serviceProxy_ == nullptr)) {
326 MEDIA_ERR_LOG("CameraManager::CreatePreviewOutput serviceProxy_ is null or profile is null");
327 return CameraErrorCode::INVALID_ARGUMENT;
328 }
329
330 if ((profile.GetCameraFormat() == CAMERA_FORMAT_INVALID) ||
331 (profile.GetSize().width == 0) ||
332 (profile.GetSize().height == 0)) {
333 MEDIA_ERR_LOG("CameraManager::CreatePhotoOutput width or height is zero");
334 return CameraErrorCode::INVALID_ARGUMENT;
335 }
336
337 metaFormat = GetCameraMetadataFormat(profile.GetCameraFormat());
338 retCode = serviceProxy_->CreateDeferredPreviewOutput(metaFormat, profile.GetSize().width,
339 profile.GetSize().height, streamRepeat);
340 if (retCode == CAMERA_OK) {
341 previewOutput = new(std::nothrow) PreviewOutput(streamRepeat);
342 if (previewOutput == nullptr) {
343 MEDIA_ERR_LOG("Failed to new PreviewOutput");
344 return CameraErrorCode::SERVICE_FATL_ERROR;
345 } else {
346 POWERMGR_SYSEVENT_CAMERA_CONFIG(PREVIEW,
347 profile.GetSize().width,
348 profile.GetSize().height);
349 }
350 } else {
351 MEDIA_ERR_LOG("CreateDeferredPreviewOutput PreviewOutput: "
352 "Failed to get stream repeat object from hcamera service!, %{public}d", retCode);
353 return ServiceToCameraError(retCode);
354 }
355
356 *pPreviewOutput = previewOutput;
357
358 return CameraErrorCode::SUCCESS;
359 }
360
CreatePreviewOutput(const sptr<OHOS::IBufferProducer> & producer,int32_t format)361 sptr<PreviewOutput> CameraManager::CreatePreviewOutput(const sptr<OHOS::IBufferProducer> &producer, int32_t format)
362 {
363 CAMERA_SYNC_TRACE;
364 sptr<PreviewOutput> result = nullptr;
365 return result;
366 }
367
CreateCustomPreviewOutput(sptr<Surface> surface,int32_t width,int32_t height)368 sptr<PreviewOutput> CameraManager::CreateCustomPreviewOutput(sptr<Surface> surface, int32_t width, int32_t height)
369 {
370 CAMERA_SYNC_TRACE;
371 sptr<PreviewOutput> result = nullptr;
372 return result;
373 }
374
CreateMetadataOutput()375 sptr<MetadataOutput> CameraManager::CreateMetadataOutput()
376 {
377 CAMERA_SYNC_TRACE;
378 sptr<MetadataOutput> metadataOutput = nullptr;
379 int ret = CreateMetadataOutput(&metadataOutput);
380 if (ret != CameraErrorCode::SUCCESS) {
381 MEDIA_ERR_LOG("Failed to CreateMetadataOutput with error code:%{public}d", ret);
382 return nullptr;
383 }
384
385 return metadataOutput;
386 }
387
CreateMetadataOutput(sptr<MetadataOutput> * pMetadataOutput)388 int CameraManager::CreateMetadataOutput(sptr<MetadataOutput> *pMetadataOutput)
389 {
390 CAMERA_SYNC_TRACE;
391 sptr<IStreamMetadata> streamMetadata = nullptr;
392 sptr<MetadataOutput> metadataOutput = nullptr;
393 int32_t retCode = CAMERA_OK;
394
395 if (!serviceProxy_) {
396 MEDIA_ERR_LOG("CameraManager::CreateMetadataOutput serviceProxy_ is null");
397 return CameraErrorCode::INVALID_ARGUMENT;
398 }
399
400 sptr<Surface> surface = Surface::CreateSurfaceAsConsumer();
401 if (!surface) {
402 MEDIA_ERR_LOG("CameraManager::CreateMetadataOutput Failed to create surface");
403 return CameraErrorCode::INVALID_ARGUMENT;
404 }
405
406 // only for face recognize
407 int32_t format = OHOS_CAMERA_FORMAT_YCRCB_420_SP;
408 int32_t width = 1920;
409 int32_t height = 1080;
410 surface->SetDefaultWidthAndHeight(width, height);
411 retCode = serviceProxy_->CreateMetadataOutput(surface->GetProducer(), format, streamMetadata);
412 if (retCode) {
413 MEDIA_ERR_LOG("CameraManager::CreateMetadataOutput Failed to get stream metadata object from hcamera service! "
414 "%{public}d", retCode);
415 return ServiceToCameraError(retCode);
416 }
417
418 metadataOutput = new(std::nothrow) MetadataOutput(surface, streamMetadata);
419 if (!metadataOutput) {
420 MEDIA_ERR_LOG("CameraManager::CreateMetadataOutput Failed to allocate MetadataOutput");
421 return CameraErrorCode::SERVICE_FATL_ERROR;
422 }
423 sptr<IBufferConsumerListener> listener = new(std::nothrow) MetadataObjectListener(metadataOutput);
424 if (!listener) {
425 MEDIA_ERR_LOG("CameraManager::CreateMetadataOutput Failed to allocate metadata object listener");
426 return CameraErrorCode::SERVICE_FATL_ERROR;
427 }
428 SurfaceError ret = surface->RegisterConsumerListener(listener);
429 if (ret != SURFACE_ERROR_OK) {
430 MEDIA_ERR_LOG("CameraManager::CreateMetadataOutput Surface consumer listener registration failed");
431 return CameraErrorCode::SERVICE_FATL_ERROR;
432 }
433
434 *pMetadataOutput = metadataOutput;
435
436 return CameraErrorCode::SUCCESS;
437 }
438
CreateVideoOutput(sptr<Surface> & surface)439 sptr<VideoOutput> CameraManager::CreateVideoOutput(sptr<Surface> &surface)
440 {
441 CAMERA_SYNC_TRACE;
442 sptr<VideoOutput> result = nullptr;
443 return result;
444 }
445
CreateVideoOutput(VideoProfile & profile,sptr<Surface> & surface)446 sptr<VideoOutput> CameraManager::CreateVideoOutput(VideoProfile &profile, sptr<Surface> &surface)
447 {
448 CAMERA_SYNC_TRACE;
449 sptr<VideoOutput> videoOutput = nullptr;
450 int ret = CreateVideoOutput(profile, surface, &videoOutput);
451 if (ret != CameraErrorCode::SUCCESS) {
452 MEDIA_ERR_LOG("Failed to CreateVideoOutput with error code:%{public}d", ret);
453 return nullptr;
454 }
455
456 return videoOutput;
457 }
458
CreateVideoOutput(VideoProfile & profile,sptr<Surface> & surface,sptr<VideoOutput> * pVideoOutput)459 int CameraManager::CreateVideoOutput(VideoProfile &profile, sptr<Surface> &surface, sptr<VideoOutput> *pVideoOutput)
460 {
461 CAMERA_SYNC_TRACE;
462 sptr<IStreamRepeat> streamRepeat = nullptr;
463 sptr<VideoOutput> videoOutput = nullptr;
464 int32_t retCode = CAMERA_OK;
465 camera_format_t metaFormat;
466
467 if ((serviceProxy_ == nullptr) || (surface == nullptr)) {
468 MEDIA_ERR_LOG("CameraManager::CreatePhotoOutput serviceProxy_ is null or surface/profile is null");
469 return CameraErrorCode::INVALID_ARGUMENT;
470 }
471
472 if ((profile.GetCameraFormat() == CAMERA_FORMAT_INVALID) ||
473 (profile.GetSize().width == 0) ||
474 (profile.GetSize().height == 0)) {
475 MEDIA_ERR_LOG("CameraManager::CreatePhotoOutput width or height is zero");
476 return CameraErrorCode::INVALID_ARGUMENT;
477 }
478
479 // todo: need to set FPS range passed in video profile.
480 metaFormat = GetCameraMetadataFormat(profile.GetCameraFormat());
481 retCode = serviceProxy_->CreateVideoOutput(surface->GetProducer(), metaFormat,
482 profile.GetSize().width, profile.GetSize().height, streamRepeat);
483 if (retCode == CAMERA_OK) {
484 videoOutput = new(std::nothrow) VideoOutput(streamRepeat);
485 if (videoOutput == nullptr) {
486 MEDIA_ERR_LOG("Failed to new VideoOutput");
487 return CameraErrorCode::SERVICE_FATL_ERROR;
488 } else {
489 std::vector<int32_t> videoFrameRates = profile.GetFrameRates();
490 if (videoFrameRates.size() >= 2) { // vaild frame rate range length is 2
491 videoOutput->SetFrameRateRange(videoFrameRates[0], videoFrameRates[1]);
492 }
493 POWERMGR_SYSEVENT_CAMERA_CONFIG(VIDEO,
494 profile.GetSize().width,
495 profile.GetSize().height);
496 }
497 } else {
498 MEDIA_ERR_LOG("VideoOutpout: Failed to get stream repeat object from hcamera service! %{public}d", retCode);
499 return ServiceToCameraError(retCode);
500 }
501
502 *pVideoOutput = videoOutput;
503
504 return CameraErrorCode::SUCCESS;
505 }
506
Init()507 void CameraManager::Init()
508 {
509 CAMERA_SYNC_TRACE;
510 sptr<IRemoteObject> object = nullptr;
511 cameraMngrCallback_ = nullptr;
512 auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
513 if (samgr == nullptr) {
514 MEDIA_ERR_LOG("Failed to get System ability manager");
515 return;
516 }
517 object = samgr->GetSystemAbility(CAMERA_SERVICE_ID);
518 if (object == nullptr) {
519 MEDIA_ERR_LOG("CameraManager::GetSystemAbility() is failed");
520 return;
521 }
522 serviceProxy_ = iface_cast<ICameraService>(object);
523 if (serviceProxy_ == nullptr) {
524 MEDIA_ERR_LOG("CameraManager::init serviceProxy_ is null.");
525 return;
526 } else {
527 cameraSvcCallback_ = new(std::nothrow) CameraStatusServiceCallback(this);
528 if (cameraSvcCallback_) {
529 SetCameraServiceCallback(cameraSvcCallback_);
530 } else {
531 MEDIA_ERR_LOG("CameraManager::init Failed to new CameraStatusServiceCallback.");
532 }
533 cameraMuteSvcCallback_ = new(std::nothrow) CameraMuteServiceCallback(this);
534 if (cameraMuteSvcCallback_) {
535 SetCameraMuteServiceCallback(cameraMuteSvcCallback_);
536 } else {
537 MEDIA_ERR_LOG("CameraManager::init Failed to new CameraMuteServiceCallback.");
538 }
539 }
540 pid_t pid = 0;
541 deathRecipient_ = new(std::nothrow) CameraDeathRecipient(pid);
542 CHECK_AND_RETURN_LOG(deathRecipient_ != nullptr, "failed to new CameraDeathRecipient.");
543
544 deathRecipient_->SetNotifyCb(std::bind(&CameraManager::CameraServerDied, this, std::placeholders::_1));
545 bool result = object->AddDeathRecipient(deathRecipient_);
546 if (!result) {
547 MEDIA_ERR_LOG("failed to add deathRecipient");
548 return;
549 }
550
551 int32_t ret = CreateListenerObject();
552 CHECK_AND_RETURN_LOG(ret == CAMERA_OK, "failed to new MediaListener, ret = %{public}d", ret);
553 }
554
CameraServerDied(pid_t pid)555 void CameraManager::CameraServerDied(pid_t pid)
556 {
557 MEDIA_ERR_LOG("camera server has died, pid:%{public}d!", pid);
558 if (serviceProxy_ != nullptr) {
559 (void)serviceProxy_->AsObject()->RemoveDeathRecipient(deathRecipient_);
560 serviceProxy_ = nullptr;
561 }
562 listenerStub_ = nullptr;
563 deathRecipient_ = nullptr;
564 }
565
CreateCameraDevice(std::string cameraId,sptr<ICameraDeviceService> * pICameraDeviceService)566 int CameraManager::CreateCameraDevice(std::string cameraId, sptr<ICameraDeviceService> *pICameraDeviceService)
567 {
568 CAMERA_SYNC_TRACE;
569 sptr<ICameraDeviceService> device = nullptr;
570 int32_t retCode = CAMERA_OK;
571
572 if (serviceProxy_ == nullptr || cameraId.empty()) {
573 MEDIA_ERR_LOG("GetCameaDevice() serviceProxy_ is null or CameraID is empty: %{public}s", cameraId.c_str());
574 return CameraErrorCode::INVALID_ARGUMENT;
575 }
576 retCode = serviceProxy_->CreateCameraDevice(cameraId, device);
577 if (retCode != CAMERA_OK) {
578 MEDIA_ERR_LOG("ret value from CreateCameraDevice, %{public}d", retCode);
579 return ServiceToCameraError(retCode);
580 }
581
582 *pICameraDeviceService = device;
583
584 return CameraErrorCode::SUCCESS;
585 }
586
SetCallback(std::shared_ptr<CameraManagerCallback> callback)587 void CameraManager::SetCallback(std::shared_ptr<CameraManagerCallback> callback)
588 {
589 if (callback == nullptr) {
590 MEDIA_INFO_LOG("CameraManager::SetCallback(): Application unregistering the callback");
591 }
592 cameraMngrCallback_ = callback;
593 }
594
GetApplicationCallback()595 std::shared_ptr<CameraManagerCallback> CameraManager::GetApplicationCallback()
596 {
597 MEDIA_INFO_LOG("CameraManager::GetApplicationCallback callback! isExist = %{public}d",
598 cameraMngrCallback_ != nullptr);
599 return cameraMngrCallback_;
600 }
601
GetCameraDeviceFromId(std::string cameraId)602 sptr<CameraDevice> CameraManager::GetCameraDeviceFromId(std::string cameraId)
603 {
604 sptr<CameraDevice> cameraObj = nullptr;
605
606 for (size_t i = 0; i < cameraObjList.size(); i++) {
607 if (cameraObjList[i]->GetID() == cameraId) {
608 cameraObj = cameraObjList[i];
609 break;
610 }
611 }
612 return cameraObj;
613 }
614
GetInstance()615 sptr<CameraManager> &CameraManager::GetInstance()
616 {
617 if (CameraManager::cameraManager_ == nullptr) {
618 MEDIA_INFO_LOG("Initializing camera manager for first time!");
619 CameraManager::cameraManager_ = new(std::nothrow) CameraManager();
620 if (CameraManager::cameraManager_ == nullptr) {
621 MEDIA_ERR_LOG("CameraManager::GetInstance failed to new CameraManager");
622 }
623 }
624 return CameraManager::cameraManager_;
625 }
626
GetCameras()627 std::vector<sptr<CameraInfo>> CameraManager::GetCameras()
628 {
629 CAMERA_SYNC_TRACE;
630 dcameraObjList.clear();
631 return dcameraObjList;
632 }
633
GetSupportedCameras()634 std::vector<sptr<CameraDevice>> CameraManager::GetSupportedCameras()
635 {
636 CAMERA_SYNC_TRACE;
637
638 std::lock_guard<std::mutex> lock(mutex_);
639 std::vector<std::string> cameraIds;
640 std::vector<std::shared_ptr<Camera::CameraMetadata>> cameraAbilityList;
641 int32_t retCode = -1;
642 sptr<CameraDevice> cameraObj = nullptr;
643 int32_t index = 0;
644
645 for (unsigned int i = 0; i < cameraObjList.size(); i++) {
646 cameraObjList[i] = nullptr;
647 }
648 cameraObjList.clear();
649 if (serviceProxy_ == nullptr) {
650 MEDIA_ERR_LOG("CameraManager::GetCameras serviceProxy_ is null, returning empty list!");
651 return cameraObjList;
652 }
653 std::vector<sptr<CameraDevice>> supportedCameras;
654 retCode = serviceProxy_->GetCameras(cameraIds, cameraAbilityList);
655 if (retCode == CAMERA_OK) {
656 for (auto& it : cameraIds) {
657 cameraObj = new(std::nothrow) CameraDevice(it, cameraAbilityList[index++]);
658 if (cameraObj == nullptr) {
659 MEDIA_ERR_LOG("CameraManager::GetCameras new CameraDevice failed for id={public}%s", it.c_str());
660 continue;
661 }
662 supportedCameras.emplace_back(cameraObj);
663 }
664 } else {
665 MEDIA_ERR_LOG("CameraManager::GetCameras failed!, retCode: %{public}d", retCode);
666 }
667
668 ChooseDeFaultCameras(supportedCameras);
669 return cameraObjList;
670 }
671
ChooseDeFaultCameras(std::vector<sptr<CameraDevice>> & supportedCameras)672 void CameraManager::ChooseDeFaultCameras(std::vector<sptr<CameraDevice>>& supportedCameras)
673 {
674 for (auto& camera : supportedCameras) {
675 bool hasDefaultCamera = false;
676 for (auto& defaultCamera : cameraObjList) {
677 if ((defaultCamera->GetPosition() == camera->GetPosition()) &&
678 (defaultCamera->GetConnectionType() == camera->GetConnectionType())) {
679 hasDefaultCamera = true;
680 MEDIA_INFO_LOG("CameraManager::alreadly has default camera");
681 } else {
682 MEDIA_INFO_LOG("CameraManager::need add default camera");
683 }
684 }
685 if (!hasDefaultCamera) {
686 cameraObjList.emplace_back(camera);
687 }
688 }
689 }
690
CreateCameraInput(sptr<CameraInfo> & camera)691 sptr<CameraInput> CameraManager::CreateCameraInput(sptr<CameraInfo> &camera)
692 {
693 CAMERA_SYNC_TRACE;
694 sptr<CameraInput> cameraInput = nullptr;
695 return cameraInput;
696 }
697
CreateCameraInput(sptr<CameraDevice> & camera)698 sptr<CameraInput> CameraManager::CreateCameraInput(sptr<CameraDevice> &camera)
699 {
700 CAMERA_SYNC_TRACE;
701 sptr<CameraInput> cameraInput = nullptr;
702 int ret = CreateCameraInput(camera, &cameraInput);
703 if (ret != CameraErrorCode::SUCCESS) {
704 MEDIA_ERR_LOG("Failed to CreateCameraInput with error code:%{public}d", ret);
705 return nullptr;
706 }
707
708 return cameraInput;
709 }
710
CreateCameraInput(sptr<CameraDevice> & camera,sptr<CameraInput> * pCameraInput)711 int CameraManager::CreateCameraInput(sptr<CameraDevice> &camera, sptr<CameraInput> *pCameraInput)
712 {
713 CAMERA_SYNC_TRACE;
714 sptr<CameraInput> cameraInput = nullptr;
715 sptr<ICameraDeviceService> deviceObj = nullptr;
716
717 if (camera != nullptr) {
718 int ret = CreateCameraDevice(camera->GetID(), &deviceObj);
719 if (ret == CameraErrorCode::SUCCESS) {
720 cameraInput = new(std::nothrow) CameraInput(deviceObj, camera);
721 if (cameraInput == nullptr) {
722 MEDIA_ERR_LOG("failed to new CameraInput Returning null in CreateCameraInput");
723 return CameraErrorCode::SERVICE_FATL_ERROR;
724 }
725 } else {
726 MEDIA_ERR_LOG("Returning null in CreateCameraInput");
727 return ret;
728 }
729 } else {
730 MEDIA_ERR_LOG("CameraManager::CreateCameraInput: Camera object is null");
731 return CameraErrorCode::INVALID_ARGUMENT;
732 }
733
734 *pCameraInput = cameraInput;
735
736 return CameraErrorCode::SUCCESS;
737 }
738
CreateCameraInput(CameraPosition position,CameraType cameraType)739 sptr<CameraInput> CameraManager::CreateCameraInput(CameraPosition position, CameraType cameraType)
740 {
741 CAMERA_SYNC_TRACE;
742 sptr<CameraInput> cameraInput = nullptr;
743 int ret = CreateCameraInput(position, cameraType, &cameraInput);
744 if (ret != CameraErrorCode::SUCCESS) {
745 MEDIA_ERR_LOG("Failed to CreateCameraInput with error code:%{public}d", ret);
746 return nullptr;
747 }
748
749 return cameraInput;
750 }
751
CreateCameraInput(CameraPosition position,CameraType cameraType,sptr<CameraInput> * pCameraInput)752 int CameraManager::CreateCameraInput(CameraPosition position, CameraType cameraType, sptr<CameraInput> *pCameraInput)
753 {
754 CAMERA_SYNC_TRACE;
755 sptr<CameraInput> cameraInput = nullptr;
756 if (cameraObjList.empty()) {
757 this->GetSupportedCameras();
758 }
759 for (size_t i = 0; i < cameraObjList.size(); i++) {
760 if ((cameraObjList[i]->GetPosition() == position) && (cameraObjList[i]->GetCameraType() == cameraType)) {
761 cameraInput = CreateCameraInput(cameraObjList[i]);
762 break;
763 } else {
764 MEDIA_ERR_LOG("No Camera Device for Camera position:%{public}d, Camera Type:%{public}d",
765 position, cameraType);
766 return CameraErrorCode::SERVICE_FATL_ERROR;
767 }
768 }
769
770 *pCameraInput = cameraInput;
771
772 return CameraErrorCode::SUCCESS;
773 }
774
GetSupportedOutputCapability(sptr<CameraDevice> & camera)775 sptr<CameraOutputCapability> CameraManager::GetSupportedOutputCapability(sptr<CameraDevice>& camera)
776 {
777 sptr<CameraOutputCapability> cameraOutputCapability = nullptr;
778 uint32_t widthOffset = 1;
779 uint32_t heightOffset = 2;
780 CameraFormat format = CAMERA_FORMAT_INVALID;
781 Size size;
782 vector<Profile> photoProfile = {};
783 vector<Profile> previewProfile = {};
784 vector<VideoProfile> videoProfiles = {};
785 vector<MetadataObjectType> objectTypes = {};
786 std::shared_ptr<Camera::CameraMetadata> metadata = camera->GetMetadata();
787 camera_metadata_item_t item;
788 int ret = Camera::FindCameraMetadataItem(metadata->get(),
789 OHOS_ABILITY_STREAM_AVAILABLE_BASIC_CONFIGURATIONS,
790 &item);
791 if ((ret != CAM_META_SUCCESS) || (item.count % UNIT_LENGTH != 0)) {
792 MEDIA_ERR_LOG("Failed to get stream configuration or Invalid stream configuation %{public}d %{public}d",
793 ret, item.count);
794 return nullptr;
795 }
796 cameraOutputCapability = new(std::nothrow) CameraOutputCapability();
797
798 for (uint32_t i = 0; i < item.count; i += UNIT_LENGTH) {
799 auto itr = metaToFwCameraFormat_.find(static_cast<camera_format_t>(item.data.i32[i]));
800 if (itr != metaToFwCameraFormat_.end()) {
801 format = itr->second;
802 } else {
803 format = CAMERA_FORMAT_INVALID;
804 MEDIA_ERR_LOG("format %{public}d is not supported now", item.data.i32[i]);
805 continue;
806 }
807 size.width = item.data.i32[i + widthOffset];
808 size.height = item.data.i32[i + heightOffset];
809 Profile profile = Profile(format, size);
810 if (format == CAMERA_FORMAT_JPEG) {
811 photoProfile.push_back(profile);
812 } else {
813 previewProfile.push_back(profile);
814 camera_metadata_item_t fpsItem;
815 int ret = Camera::FindCameraMetadataItem(metadata->get(), OHOS_ABILITY_FPS_RANGES, &fpsItem);
816 if (ret == CAM_META_SUCCESS) {
817 const uint32_t step = 2;
818 for (uint32_t i = 0; i < (fpsItem.count - 1); i += step) {
819 std::vector<int32_t> fps = {fpsItem.data.i32[i], fpsItem.data.i32[i+1]};
820 VideoProfile vidProfile = VideoProfile(format, size, fps);
821 videoProfiles.push_back(vidProfile);
822 }
823 }
824 }
825 }
826 cameraOutputCapability->SetPhotoProfiles(photoProfile);
827 MEDIA_DEBUG_LOG("SetPhotoProfiles size = %{public}zu", photoProfile.size());
828 cameraOutputCapability->SetPreviewProfiles(previewProfile);
829 MEDIA_DEBUG_LOG("SetPreviewProfiles size = %{public}zu", previewProfile.size());
830 cameraOutputCapability->SetVideoProfiles(videoProfiles);
831 MEDIA_DEBUG_LOG("SetVideoProfiles size = %{public}zu", videoProfiles.size());
832
833 camera_metadata_item_t metadataItem;
834 ret = Camera::FindCameraMetadataItem(metadata->get(), OHOS_STATISTICS_FACE_DETECT_MODE, &metadataItem);
835 if (ret == CAM_META_SUCCESS) {
836 for (uint32_t index = 0; index < metadataItem.count; index++) {
837 if (metadataItem.data.u8[index] == OHOS_CAMERA_FACE_DETECT_MODE_SIMPLE) {
838 objectTypes.push_back(MetadataObjectType::FACE);
839 }
840 }
841 }
842 cameraOutputCapability->SetSupportedMetadataObjectType(objectTypes);
843
844 return cameraOutputCapability;
845 }
SetCameraServiceCallback(sptr<ICameraServiceCallback> & callback)846 void CameraManager::SetCameraServiceCallback(sptr<ICameraServiceCallback>& callback)
847 {
848 int32_t retCode = CAMERA_OK;
849
850 if (serviceProxy_ == nullptr) {
851 MEDIA_ERR_LOG("CameraManager::SetCallback serviceProxy_ is null");
852 return;
853 }
854 retCode = serviceProxy_->SetCallback(callback);
855 if (retCode != CAMERA_OK) {
856 MEDIA_ERR_LOG("CameraManager::Set service Callback failed, retCode: %{public}d", retCode);
857 }
858 return;
859 }
860
GetCameraMetadataFormat(CameraFormat format)861 camera_format_t CameraManager::GetCameraMetadataFormat(CameraFormat format)
862 {
863 camera_format_t metaFormat = OHOS_CAMERA_FORMAT_YCRCB_420_SP;
864
865 auto itr = fwToMetaCameraFormat_.find(format);
866 if (itr != fwToMetaCameraFormat_.end()) {
867 metaFormat = itr->second;
868 }
869
870 return metaFormat;
871 }
872
OnCameraMute(bool muteMode)873 int32_t CameraMuteServiceCallback::OnCameraMute(bool muteMode)
874 {
875 MEDIA_DEBUG_LOG("CameraMuteServiceCallback::OnCameraMute call! muteMode is %{public}d", muteMode);
876 if (camMngr_ == nullptr) {
877 MEDIA_INFO_LOG("CameraMuteServiceCallback::OnCameraMute camMngr_ is nullptr");
878 return CAMERA_OK;
879 }
880 std::vector<shared_ptr<CameraMuteListener>> cameraMuteListenerList = camMngr_->GetCameraMuteListener();
881 if (cameraMuteListenerList.size() > 0) {
882 for (uint32_t i = 0; i < cameraMuteListenerList.size(); ++i) {
883 cameraMuteListenerList[i]->OnCameraMute(muteMode);
884 }
885 } else {
886 MEDIA_INFO_LOG("CameraManager::CameraMuteListener not registered!, Ignore the callback");
887 }
888 return CAMERA_OK;
889 }
890
RegisterCameraMuteListener(std::shared_ptr<CameraMuteListener> listener)891 void CameraManager::RegisterCameraMuteListener(std::shared_ptr<CameraMuteListener> listener)
892 {
893 if (listener == nullptr) {
894 MEDIA_INFO_LOG("CameraManager::RegisterCameraMuteListener(): unregistering the callback");
895 }
896 cameraMuteListenerList.push_back(listener);
897 }
898
SetCameraMuteServiceCallback(sptr<ICameraMuteServiceCallback> & callback)899 void CameraManager::SetCameraMuteServiceCallback(sptr<ICameraMuteServiceCallback>& callback)
900 {
901 int32_t retCode = CAMERA_OK;
902
903 if (serviceProxy_ == nullptr) {
904 MEDIA_ERR_LOG("CameraManager::SetMuteCameraServiceCallback serviceProxy_ is null");
905 return;
906 }
907 retCode = serviceProxy_->SetMuteCallback(callback);
908 if (retCode != CAMERA_OK) {
909 MEDIA_ERR_LOG("CameraManager::Set Mute service Callback failed, retCode: %{public}d", retCode);
910 }
911 return;
912 }
913
GetCameraMuteListener()914 std::vector<shared_ptr<CameraMuteListener>> CameraManager::GetCameraMuteListener()
915 {
916 return cameraMuteListenerList;
917 }
918
IsCameraMuteSupported()919 bool CameraManager::IsCameraMuteSupported()
920 {
921 const uint8_t MUTE_ON = 1;
922 bool result = false;
923 if (cameraObjList.empty()) {
924 this->GetSupportedCameras();
925 }
926 for (size_t i = 0; i < cameraObjList.size(); i++) {
927 std::shared_ptr<Camera::CameraMetadata> metadata = cameraObjList[i]->GetMetadata();
928 camera_metadata_item_t item;
929 int ret = Camera::FindCameraMetadataItem(metadata->get(), OHOS_ABILITY_MUTE_MODES, &item);
930 if (ret == 0) {
931 MEDIA_INFO_LOG("CameraManager::isCameraMuteSupported() OHOS_ABILITY_MUTE_MODES is %{public}d",
932 item.data.u8[0]);
933 result = (item.data.u8[0] == MUTE_ON) ? true : false;
934 } else {
935 MEDIA_ERR_LOG("Failed to get stream configuration or Invalid stream "
936 "configuation OHOS_ABILITY_MUTE_MODES ret = %{public}d", ret);
937 }
938 if (result == true) {
939 break;
940 }
941 }
942 return result;
943 }
944
IsCameraMuted()945 bool CameraManager::IsCameraMuted()
946 {
947 int32_t retCode = CAMERA_OK;
948 bool isMuted = false;
949
950 if (serviceProxy_ == nullptr) {
951 MEDIA_ERR_LOG("CameraManager::IsCameraMuted serviceProxy_ is null");
952 return isMuted;
953 }
954 retCode = serviceProxy_->IsCameraMuted(isMuted);
955 if (retCode != CAMERA_OK) {
956 MEDIA_ERR_LOG("CameraManager::IsCameraMuted failed, retCode: %{public}d", retCode);
957 }
958 return isMuted;
959 }
960
MuteCamera(bool muteMode)961 void CameraManager::MuteCamera(bool muteMode)
962 {
963 std::lock_guard<std::mutex> lock(mutex_);
964 int32_t retCode = CAMERA_OK;
965
966 if (serviceProxy_ == nullptr) {
967 MEDIA_ERR_LOG("CameraManager::MuteCamera serviceProxy_ is null");
968 return;
969 }
970 retCode = serviceProxy_->MuteCamera(muteMode);
971 if (retCode != CAMERA_OK) {
972 MEDIA_ERR_LOG("CameraManager::MuteCamera failed, retCode: %{public}d", retCode);
973 }
974 return;
975 }
976 } // CameraStandard
977 } // OHOS
978