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 #ifndef OHOS_CAMERA_CAMERA_MANAGER_H 17 #define OHOS_CAMERA_CAMERA_MANAGER_H 18 19 #include <cstdint> 20 #include <iostream> 21 #include <memory> 22 #include <mutex> 23 #include <refbase.h> 24 #include <thread> 25 #include <unordered_map> 26 #include <utility> 27 #include <vector> 28 #include <unordered_map> 29 30 #include "camera_stream_info_parse.h" 31 #include "deferred_proc_session/deferred_photo_proc_session.h" 32 #include "deferred_proc_session/deferred_video_proc_session.h" 33 #include "hcamera_listener_stub.h" 34 #include "hcamera_service_callback_stub.h" 35 #include "hcamera_service_proxy.h" 36 #include "icamera_device_service.h" 37 #include "icamera_service_callback.h" 38 #include "input/camera_death_recipient.h" 39 #include "input/camera_device.h" 40 #include "input/camera_info.h" 41 #include "input/camera_input.h" 42 #include "istream_common.h" 43 #include "istream_repeat.h" 44 #include "output/camera_output_capability.h" 45 #include "output/depth_data_output.h" 46 #include "output/metadata_output.h" 47 #include "output/photo_output.h" 48 #include "output/preview_output.h" 49 #include "output/video_output.h" 50 #include "safe_map.h" 51 #include "color_space_info_parse.h" 52 53 namespace OHOS { 54 namespace CameraStandard { 55 enum CameraDeviceStatus { 56 CAMERA_DEVICE_STATUS_UNAVAILABLE = 0, 57 CAMERA_DEVICE_STATUS_AVAILABLE 58 }; 59 60 enum FlashlightStatus { 61 FLASHLIGHT_STATUS_OFF = 0, 62 FLASHLIGHT_STATUS_ON, 63 FLASHLIGHT_STATUS_UNAVAILABLE 64 }; 65 66 enum TorchMode { 67 TORCH_MODE_OFF = 0, 68 TORCH_MODE_ON, 69 TORCH_MODE_AUTO 70 }; 71 72 struct CameraStatusInfo { 73 sptr<CameraInfo> cameraInfo; 74 sptr<CameraDevice> cameraDevice; 75 CameraStatus cameraStatus; 76 std::string bundleName; 77 }; 78 79 struct TorchStatusInfo { 80 bool isTorchAvailable; 81 bool isTorchActive; 82 float torchLevel; 83 }; 84 85 struct FoldStatusInfo { 86 FoldStatus foldStatus; 87 std::vector<sptr<CameraDevice>> supportedCameras; 88 }; 89 90 typedef enum OutputCapStreamType { 91 PREVIEW = 0, 92 VIDEO_STREAM = 1, 93 STILL_CAPTURE = 2, 94 POST_VIEW = 3, 95 ANALYZE = 4, 96 CUSTOM = 5, 97 DEPTH = 6 98 } OutputCapStreamType; 99 100 class CameraManagerCallback { 101 public: 102 CameraManagerCallback() = default; 103 virtual ~CameraManagerCallback() = default; 104 virtual void OnCameraStatusChanged(const CameraStatusInfo &cameraStatusInfo) const = 0; 105 virtual void OnFlashlightStatusChanged(const std::string &cameraID, const FlashStatus flashStatus) const = 0; 106 }; 107 108 class CameraMuteListener { 109 public: 110 CameraMuteListener() = default; 111 virtual ~CameraMuteListener() = default; 112 virtual void OnCameraMute(bool muteMode) const = 0; 113 }; 114 115 class TorchListener { 116 public: 117 TorchListener() = default; 118 virtual ~TorchListener() = default; 119 virtual void OnTorchStatusChange(const TorchStatusInfo &torchStatusInfo) const = 0; 120 }; 121 122 class FoldListener { 123 public: 124 FoldListener() = default; 125 virtual ~FoldListener() = default; 126 virtual void OnFoldStatusChanged(const FoldStatusInfo &foldStatusInfo) const = 0; 127 }; 128 129 class TorchServiceListenerManager; 130 class CameraStatusListenerManager; 131 class CameraMuteListenerManager; 132 class FoldStatusListenerManager; 133 class CameraManager : public RefBase { 134 public: 135 virtual ~CameraManager(); 136 /** 137 * @brief Get camera manager instance. 138 * 139 * @return Returns pointer to camera manager instance. 140 */ 141 static sptr<CameraManager>& GetInstance(); 142 143 /** 144 * @brief Get all available cameras. 145 * 146 * @return Returns vector of cameraDevice of available camera. 147 */ 148 std::vector<sptr<CameraDevice>> GetSupportedCameras(); 149 150 /** 151 * @brief Get support modes. 152 * 153 * @return Returns array the mode of current CameraDevice. 154 */ 155 std::vector<SceneMode> GetSupportedModes(sptr<CameraDevice>& camera); 156 157 /** 158 * @brief Get extend output capaility of the mode of the given camera from cameraDevice. 159 * 160 * @param Camera device for which extend capability need to be fetched. 161 * @return Returns vector the ability of the mode of cameraDevice of available camera from cameraDevice. 162 */ 163 sptr<CameraOutputCapability> GetSupportedOutputCapability(sptr<CameraDevice>& camera, int32_t modeName = 0); 164 165 /** 166 * @brief Create camera input instance with provided camera position and type. 167 * 168 * @param The cameraDevice for which input has to be created. 169 * @return Returns pointer to camera input instance. 170 */ 171 172 sptr<CameraInput> CreateCameraInput(CameraPosition position, CameraType cameraType); 173 174 /** 175 * @brief Create camera input instance with provided camera position and type. 176 * 177 * @param The cameraDevice for which input has to be created. 178 * @param Returns pointer to camera input instance. 179 * @return Returns error code. 180 */ 181 int CreateCameraInput(CameraPosition position, CameraType cameraType, sptr<CameraInput>* pCameraInput); 182 183 /** 184 * @brief Create camera input instance. 185 * 186 * @param The cameraDevice for which input has to be created. 187 * @return Returns pointer to camera input instance. 188 */ 189 sptr<CameraInput> CreateCameraInput(sptr<CameraDevice>& camera); 190 191 /** 192 * @brief Create camera input instance. 193 * 194 * @param The cameraDevice for which input has to be created. 195 * @param Returns pointer to camera input instance. 196 * @return Returns error code. 197 */ 198 int CreateCameraInput(sptr<CameraDevice>& camera, sptr<CameraInput>* pCameraInput); 199 200 /** 201 * @brief Get all available cameras. 202 * 203 * @return Returns vector of cameraInfo of available camera. 204 */ 205 [[deprecated]] std::vector<sptr<CameraInfo>> GetCameras(); 206 207 /** 208 * @brief Create camera input instance. 209 * 210 * @param The cameraInfo for which input has to be created. 211 * @return Returns pointer to camera input instance. 212 */ 213 [[deprecated]] sptr<CameraInput> CreateCameraInput(sptr<CameraInfo>& camera); 214 215 /** 216 * @brief Create capture session. 217 * 218 * @return Returns pointer to capture session. 219 */ 220 sptr<CaptureSession> CreateCaptureSession(); 221 222 /** 223 * @brief Create capture session. 224 * 225 * @return Returns pointer to capture session. 226 */ 227 sptr<CaptureSession> CreateCaptureSession(SceneMode mode); 228 229 /** 230 * @brief Create capture session. 231 * 232 * @param pCaptureSession pointer to capture session. 233 * @param mode Target mode 234 * @return Returns error code. 235 */ 236 int32_t CreateCaptureSession(sptr<CaptureSession>& pCaptureSession, SceneMode mode); 237 238 /** 239 * @brief Create capture session. 240 * 241 * @param pCaptureSession pointer to capture session. 242 * @return Returns error code. 243 */ 244 int32_t CreateCaptureSession(sptr<CaptureSession>* pCaptureSession); 245 246 /** 247 * @brief Create photo output instance using surface. 248 * 249 * @param The surface to be used for photo output. 250 * @return Returns pointer to photo output instance. 251 */ 252 sptr<PhotoOutput> CreatePhotoOutput(Profile& profile, sptr<IBufferProducer>& surface); 253 254 /** 255 * @brief Create deferred photo processing session. 256 * 257 * @return Returns pointer to capture session. 258 */ 259 static sptr<DeferredPhotoProcSession> CreateDeferredPhotoProcessingSession(int userId, 260 std::shared_ptr<IDeferredPhotoProcSessionCallback> callback); 261 262 /** 263 * @brief Create deferred photo processing session. 264 * 265 * @param Returns pointer to capture session. 266 * @return Returns error code. 267 */ 268 static int CreateDeferredPhotoProcessingSession(int userId, 269 std::shared_ptr<IDeferredPhotoProcSessionCallback> callback, 270 sptr<DeferredPhotoProcSession> *pDeferredPhotoProcSession); 271 272 /** 273 * @brief Create deferred video processing session. 274 * 275 * @return Returns pointer to capture session. 276 */ 277 static sptr<DeferredVideoProcSession> CreateDeferredVideoProcessingSession(int userId, 278 std::shared_ptr<IDeferredVideoProcSessionCallback> callback); 279 280 /** 281 * @brief Create deferred video processing session. 282 * 283 * @param Returns pointer to capture session. 284 * @return Returns error code. 285 */ 286 static int CreateDeferredVideoProcessingSession(int userId, 287 std::shared_ptr<IDeferredVideoProcSessionCallback> callback, 288 sptr<DeferredVideoProcSession> *pDeferredVideoProcSession); 289 290 /** 291 * @brief Create photo output instance. 292 * 293 * @param profile photo profile. 294 * @param surface photo buffer surface. 295 * @param pPhotoOutput pointer to photo output instance. 296 * @return Returns error code. 297 */ 298 int CreatePhotoOutput(Profile& profile, sptr<IBufferProducer>& surface, sptr<PhotoOutput>* pPhotoOutput); 299 300 /** 301 * @brief Create photo output instance. 302 * 303 * @param profile photo profile. 304 * @param surface photo buffer surface. 305 * @param pPhotoOutput pointer to photo output instance. 306 * @return Returns error code. 307 */ 308 int CreatePhotoOutput(Profile &profile, sptr<IBufferProducer> &surfaceProducer, sptr<PhotoOutput> *pPhotoOutput, 309 sptr<Surface> photoSurface); 310 311 /** 312 * @brief Create photo output instance without profile. 313 * 314 * @param surface photo buffer surface. 315 * @param pPhotoOutput pointer to photo output instance. 316 * @return Returns error code. 317 */ 318 int CreatePhotoOutputWithoutProfile(sptr<IBufferProducer> surface, sptr<PhotoOutput>* pPhotoOutput); 319 320 /** 321 * @brief Create photo output instance without profile. 322 * 323 * @param surface photo buffer surface. 324 * @param pPhotoOutput pointer to photo output instance. 325 * @return Returns error code. 326 */ 327 int CreatePhotoOutputWithoutProfile(sptr<IBufferProducer> surface, 328 sptr<PhotoOutput>* pPhotoOutput, sptr<Surface> photoSurface); 329 330 /** 331 * @brief Create photo output instance using surface. 332 * 333 * @param The surface to be used for photo output. 334 * @return Returns pointer to photo output instance. 335 */ 336 [[deprecated]] sptr<PhotoOutput> CreatePhotoOutput(sptr<IBufferProducer>& surface); 337 338 /** 339 * @brief Create photo output instance using IBufferProducer. 340 * 341 * @param The IBufferProducer to be used for photo output. 342 * @param The format to be used for photo capture. 343 * @return Returns pointer to photo output instance. 344 */ 345 [[deprecated]] sptr<PhotoOutput> CreatePhotoOutput(const sptr<OHOS::IBufferProducer>& producer, int32_t format); 346 347 /** 348 * @brief Create video output instance using surface. 349 * 350 * @param The surface to be used for video output. 351 * @return Returns pointer to video output instance. 352 */ 353 sptr<VideoOutput> CreateVideoOutput(VideoProfile& profile, sptr<Surface>& surface); 354 355 /** 356 * @brief Create video output instance using surface. 357 * 358 * @param The surface to be used for video output. 359 * @param Returns pointer to video output instance. 360 * @return Returns error code. 361 */ 362 int CreateVideoOutput(VideoProfile& profile, sptr<Surface>& surface, sptr<VideoOutput>* pVideoOutput); 363 364 /** 365 * @brief Create video output instance without profile. 366 * 367 * @param surface video buffer surface. 368 * @param pVideoOutput pointer to video output instance. 369 * @return Returns error code. 370 */ 371 int CreateVideoOutputWithoutProfile(sptr<Surface> surface, sptr<VideoOutput>* pVideoOutput); 372 373 /** 374 * @brief Create video output instance using surface. 375 * 376 * @param The surface to be used for video output. 377 * @return Returns pointer to video output instance. 378 */ 379 [[deprecated]] sptr<VideoOutput> CreateVideoOutput(sptr<Surface>& surface); 380 381 /** 382 * @brief Create video output instance using IBufferProducer. 383 * 384 * @param The IBufferProducer to be used for video output. 385 * @param The format to be used for video capture. 386 * @return Returns pointer to video output instance. 387 */ 388 [[deprecated]] sptr<VideoOutput> CreateVideoOutput(const sptr<OHOS::IBufferProducer>& producer, int32_t format); 389 390 /** 391 * @brief Create preview output instance using surface. 392 * 393 * @param The surface to be used for preview. 394 * @return Returns pointer to preview output instance. 395 */ 396 sptr<PreviewOutput> CreatePreviewOutput(Profile& profile, sptr<Surface> surface); 397 398 /** 399 * @brief Create preview output instance. 400 * 401 * @param profile preview profile. 402 * @param surface preview buffer surface. 403 * @param pPhotoOutput pointer to photo preview instance. 404 * @return Returns error code. 405 */ 406 int CreatePreviewOutput(Profile& profile, sptr<Surface> surface, sptr<PreviewOutput>* pPreviewOutput); 407 408 /** 409 * @brief Create preview output instance without profile. 410 * 411 * @param surface preview buffer surface. 412 * @param pPhotoOutput pointer to photo preview instance. 413 * @return Returns error code. 414 */ 415 int CreatePreviewOutputWithoutProfile(sptr<Surface> surface, sptr<PreviewOutput>* pPreviewOutput); 416 417 /** 418 * @brief Create preview output instance using surface. 419 * 420 * @param The surface to be used for preview. 421 * @return Returns pointer to preview output instance. 422 */ 423 [[deprecated]] sptr<PreviewOutput> CreatePreviewOutput(sptr<Surface> surface); 424 425 /** 426 * @brief Create preview output instance using IBufferProducer. 427 * 428 * @param The IBufferProducer to be used for preview output. 429 * @param The format to be used for preview. 430 * @return Returns pointer to video preview instance. 431 */ 432 [[deprecated]] sptr<PreviewOutput> CreatePreviewOutput(const sptr<OHOS::IBufferProducer>& producer, int32_t format); 433 434 /** 435 * @brief Create preview output instance using surface. 436 * 437 * @param The surface to be used for preview. 438 * @return Returns pointer to preview output instance. 439 */ 440 sptr<PreviewOutput> CreateDeferredPreviewOutput(Profile& profile); 441 442 /** 443 * @brief Create preview output instance using surface. 444 * 445 * @param The surface to be used for preview. 446 * @param Returns pointer to preview output instance. 447 * @return Returns error code. 448 */ 449 int CreateDeferredPreviewOutput(Profile& profile, sptr<PreviewOutput>* pPreviewOutput); 450 451 /** 452 * @brief Create preview output instance using surface 453 * with custom width and height. 454 * 455 * @param The surface to be used for preview. 456 * @param preview width. 457 * @param preview height. 458 * @return Returns pointer to preview output instance. 459 */ 460 [[deprecated]] sptr<PreviewOutput> CreateCustomPreviewOutput(sptr<Surface> surface, int32_t width, int32_t height); 461 462 /** 463 * @brief Create preview output instance using IBufferProducer 464 * with custom width and height. 465 * 466 * @param The IBufferProducer to be used for preview output. 467 * @param The format to be used for preview. 468 * @param preview width. 469 * @param preview height. 470 * @return Returns pointer to preview output instance. 471 */ 472 [[deprecated]] sptr<PreviewOutput> CreateCustomPreviewOutput( 473 const sptr<OHOS::IBufferProducer>& producer, int32_t format, int32_t width, int32_t height); 474 475 /** 476 * @brief Create metadata output instance. 477 * 478 * @return Returns pointer to metadata output instance. 479 */ 480 sptr<MetadataOutput> CreateMetadataOutput(); 481 482 /** 483 * @brief Create metadata output instance. 484 * 485 * @param Returns pointer to metadata output instance. 486 * @return Returns error code. 487 */ 488 int CreateMetadataOutput(sptr<MetadataOutput>& pMetadataOutput); 489 490 /** 491 * @brief Create depth output instance. 492 * 493 * @param depthProfile depth profile. 494 * @param surface depth data buffer surface. 495 * @return pointer to depth data output instance. 496 */ 497 sptr<DepthDataOutput> CreateDepthDataOutput(DepthProfile& depthProfile, sptr<IBufferProducer> &surface); 498 499 /** 500 * @brief Create depth output instance. 501 * 502 * @param depthProfile depth profile. 503 * @param surface depth data buffer surface. 504 * @param pDepthDataOutput pointer to depth data output instance. 505 * @return Returns error code. 506 */ 507 int CreateDepthDataOutput(DepthProfile& depthProfile, sptr<IBufferProducer> &surface, 508 sptr<DepthDataOutput>* pDepthDataOutput); 509 510 /** 511 * @brief Create metadata output instance. 512 * 513 * @param Returns pointer to metadata output instance. 514 * @return Returns error code. 515 */ 516 int CreateMetadataOutput(sptr<MetadataOutput>& pMetadataOutput, 517 std::vector<MetadataObjectType> metadataObjectTypes); 518 519 /** 520 * @brief Register camera status listener. 521 * 522 * @param listener CameraManagerCallback pointer. 523 */ 524 void SetCallback(std::shared_ptr<CameraManagerCallback> listener); 525 526 /** 527 * @brief Register camera status listener. 528 * 529 * @param listener CameraManagerCallback pointer. 530 */ 531 void RegisterCameraStatusCallback(std::shared_ptr<CameraManagerCallback> listener); 532 533 /** 534 * @brief Unregister camera status listener. 535 * 536 * @param listener CameraManagerCallback pointer. 537 */ 538 void UnregisterCameraStatusCallback(std::shared_ptr<CameraManagerCallback> listener); 539 540 /** 541 * @brief Get the camera status listener manager. 542 * 543 * @return CameraStatusListenerManager pointer is set by application. 544 */ 545 sptr<CameraStatusListenerManager> GetCameraStatusListenerManager(); 546 547 /** 548 * @brief Get cameraDevice of specific camera id. 549 * 550 * @param std::string camera id. 551 * @return Returns pointer to cameraDevice of given Id if found else return nullptr. 552 */ 553 sptr<CameraDevice> GetCameraDeviceFromId(std::string cameraId); 554 555 /** 556 * @brief Get cameraInfo of specific camera id. 557 * 558 * @param std::string camera id. 559 * @return Returns pointer to cameraInfo of given Id if found else return nullptr. 560 */ 561 [[deprecated]] sptr<CameraInfo> GetCameraInfo(std::string cameraId); 562 563 /** 564 * @brief Get the support of camera mute mode. 565 * 566 * @return Returns true is supported, false is not supported. 567 */ 568 bool IsCameraMuteSupported(); 569 570 /** 571 * @brief Get camera mute mode. 572 * 573 * @return Returns true is in mute, else is not in mute. 574 */ 575 bool IsCameraMuted(); 576 577 /** 578 * @brief Mute the camera 579 * 580 * @return. 581 */ 582 void MuteCamera(bool muteMode); 583 584 /** 585 * @brief Mute the camera, and the mute mode can be persisting; 586 * 587 * @param PolicyType policyType. 588 * @param bool muteMode. 589 * @return. 590 */ 591 int32_t MuteCameraPersist(PolicyType policyType, bool muteMode); 592 593 /** 594 * @brief Register camera mute listener 595 * 596 * @param listener The mute listener object. 597 * 598 */ 599 void RegisterCameraMuteListener(std::shared_ptr<CameraMuteListener> listener); 600 601 /** 602 * @brief Unregister camera mute listener 603 * 604 * @param listener The mute listener object. 605 * @return. 606 */ 607 void UnregisterCameraMuteListener(std::shared_ptr<CameraMuteListener> listener); 608 609 /** 610 * @brief Get the camera mute listener manager. 611 * 612 * @return CameraMuteListenerManager point. 613 */ 614 sptr<CameraMuteListenerManager> GetCameraMuteListenerManager(); 615 616 /** 617 * @brief prelaunch the camera 618 * 619 * @return Server error code. 620 */ 621 int32_t PrelaunchCamera(); 622 623 /** 624 * @brief Pre-switch camera 625 * 626 * @return Server error code. 627 */ 628 int32_t PreSwitchCamera(const std::string cameraId); 629 630 /** 631 * @brief set prelaunch config 632 * 633 * @return. 634 */ 635 int32_t SetPrelaunchConfig(std::string cameraId, RestoreParamTypeOhos restoreParamType, int activeTime, 636 EffectParam effectParam); 637 638 /** 639 * @brief Get the support of camera pre launch mode. 640 * 641 * @return Returns true is supported, false is not supported. 642 */ 643 bool IsPrelaunchSupported(sptr<CameraDevice> camera); 644 645 /** 646 * @brief Register torch listener 647 * 648 * @param TorchListener listener object. 649 * @return. 650 */ 651 void RegisterTorchListener(std::shared_ptr<TorchListener> listener); 652 653 /** 654 * @brief Unregister torch listener 655 * 656 * @param TorchListener listener object. 657 * @return. 658 */ 659 void UnregisterTorchListener(std::shared_ptr<TorchListener> listener); 660 661 /** 662 * @brief Get the TorchServiceListenerManager. 663 * 664 * @return TorchServiceListenerManager. 665 */ 666 sptr<TorchServiceListenerManager> GetTorchServiceListenerManager(); 667 668 /** 669 * @brief Register fold status listener 670 * 671 * @param listener The fold listener object. 672 * @return. 673 */ 674 void RegisterFoldListener(std::shared_ptr<FoldListener> listener); 675 676 /** 677 * @brief Unregister fold status listener 678 * 679 * @param listener The fold listener object. 680 * @return. 681 */ 682 void UnregisterFoldListener(std::shared_ptr<FoldListener> listener); 683 684 /** 685 * @brief Get the FoldStatusListenerManager. 686 * 687 * @return FoldStatusListenerManager. 688 */ 689 sptr<FoldStatusListenerManager> GetFoldStatusListenerManager(); 690 691 /** 692 * @brief check device if support torch 693 * 694 * @return Returns true is supported, false is not supported. 695 */ 696 bool IsTorchSupported(); 697 698 /** 699 * @brief check mode if device can support 700 * 701 * @return Returns true is supported, false is not supported. 702 */ 703 bool IsTorchModeSupported(TorchMode mode); 704 705 /** 706 * @brief get current torchmode 707 * 708 * @return Returns current torchmode 709 */ 710 TorchMode GetTorchMode(); 711 712 /** 713 * @brief set torch mode 714 * 715 * @return. 716 */ 717 int32_t SetTorchMode(TorchMode mode); 718 719 /** 720 * @brief update torch mode 721 * 722 */ 723 void UpdateTorchMode(TorchMode mode); 724 725 /** 726 * @brief set cameramanager null 727 * 728 */ 729 void SetCameraManagerNull(); 730 731 int32_t RequireMemorySize(int32_t memSize); 732 733 int32_t CreatePreviewOutputStream( 734 sptr<IStreamRepeat>& streamPtr, Profile& profile, const sptr<OHOS::IBufferProducer>& producer); 735 736 int32_t CreateVideoOutputStream( 737 sptr<IStreamRepeat>& streamPtr, Profile& profile, const sptr<OHOS::IBufferProducer>& producer); 738 739 int32_t CreatePhotoOutputStream( 740 sptr<IStreamCapture>& streamPtr, Profile& profile, const sptr<OHOS::IBufferProducer>& producer); 741 /** 742 * @brief clear remote stub obj. 743 * 744 */ 745 int32_t DestroyStubObj(); 746 747 static const std::string surfaceFormat; 748 749 void OnCameraServerAlive(); 750 751 virtual bool GetIsFoldable(); 752 753 virtual FoldStatus GetFoldStatus(); 754 ClearCameraDeviceListCache()755 inline void ClearCameraDeviceListCache() 756 { 757 std::lock_guard<std::mutex> lock(cameraDeviceListMutex_); 758 cameraDeviceList_.clear(); 759 } 760 ClearCameraDeviceAbilitySupportMap()761 inline void ClearCameraDeviceAbilitySupportMap() 762 { 763 std::lock_guard<std::mutex> lock(cameraDeviceAbilitySupportMapMutex_); 764 cameraDeviceAbilitySupportMap_.clear(); 765 } 766 RemoveCameraDeviceFromCache(const std::string & cameraId)767 inline void RemoveCameraDeviceFromCache(const std::string& cameraId) 768 { 769 std::lock_guard<std::mutex> lock(cameraDeviceListMutex_); 770 cameraDeviceList_.erase(std::remove_if(cameraDeviceList_.begin(), cameraDeviceList_.end(), 771 [&cameraId](const auto& cameraDevice) { return cameraDevice->GetID() == cameraId; }), 772 cameraDeviceList_.end()); 773 } 774 775 void GetCameraOutputStatus(int32_t pid, int32_t &status); 776 int CreateCameraDevice(std::string cameraId, sptr<ICameraDeviceService> *pICameraDeviceService); GetServiceProxy()777 inline sptr<ICameraService> GetServiceProxy() 778 { 779 std::lock_guard<std::mutex> lock(serviceProxyMutex_); 780 return serviceProxyPrivate_; 781 } 782 std::vector<sptr<CameraDevice>> GetSupportedCamerasWithFoldStatus(); 783 784 struct ProfilesWrapper { 785 std::vector<Profile> photoProfiles = {}; 786 std::vector<Profile> previewProfiles = {}; 787 std::vector<VideoProfile> vidProfiles = {}; 788 }; 789 790 void GetCameraConcurrentInfos(std::vector<sptr<CameraDevice>> cameraDeviceArrray, 791 std::vector<bool> cameraConcurrentType, std::vector<std::vector<SceneMode>> &modes, 792 std::vector<std::vector<sptr<CameraOutputCapability>>> &outputCapabilities); 793 void GetMetadataInfos(camera_metadata_item_t item, 794 std::vector<SceneMode> &modeofThis, std::vector<sptr<CameraOutputCapability>> &outputCapabilitiesofThis, 795 shared_ptr<OHOS::Camera::CameraMetadata> &cameraAbility); 796 void SetCameraOutputCapabilityofthis(sptr<CameraOutputCapability> &cameraOutputCapability, 797 ProfilesWrapper &profilesWrapper, int32_t modeName, 798 shared_ptr<OHOS::Camera::CameraMetadata> &cameraAbility); 799 bool GetConcurrentType(std::vector<sptr<CameraDevice>> cameraDeviceArrray, 800 std::vector<bool> &cameraConcurrentType); 801 bool CheckConcurrentExecution(std::vector<sptr<CameraDevice>> cameraDeviceArrray); 802 bool CheckCameraConcurrentId(std::unordered_map<std::string, int32_t> &idmap, 803 std::vector<std::string> &cameraIdv); 804 void ParsingCameraConcurrentLimted(camera_metadata_item_t &item, 805 std::vector<SceneMode> &mode, std::vector<sptr<CameraOutputCapability>> &outputCapabilitiesofThis, 806 shared_ptr<OHOS::Camera::CameraMetadata> &cameraAbility, sptr<CameraDevice>cameraDevNow); 807 void GetMetadataInfosfordouble(camera_metadata_item_t &item, double* originInfo, uint32_t i, 808 std::vector<SceneMode> &modeofThis, std::vector<sptr<CameraOutputCapability>> &outputCapabilitiesofThis, 809 shared_ptr<OHOS::Camera::CameraMetadata> &cameraAbility); 810 void GetSpecInfofordouble(double* originInfo, uint32_t start, uint32_t end, ProfileLevelInfo &modeInfo); 811 void GetStreamInfofordouble(double* originInfo, uint32_t start, uint32_t end, SpecInfo &specInfo); 812 void GetDetailInfofordouble(double* originInfo, uint32_t start, uint32_t end, StreamInfo &streamInfo); 813 void GetAbilityStructofConcurrentLimted(std::vector<int32_t> &vec, double* originInfo, int length); 814 void GetAbilityStructofConcurrentLimtedfloat(std::vector<float> &vec, double* originInfo, int length); 815 std::vector<dmDeviceInfo> GetDmDeviceInfo(); 816 CameraConcurrentLimtedCapability limtedCapabilitySave_; 817 std::unordered_map<std::string, CameraConcurrentLimtedCapability> cameraConLimCapMap_; 818 void FindConcurrentLimtedEnd(double* originInfo, int32_t i, int32_t count, int32_t &countl); 819 friend int CameraInput::Open(int32_t cameraConcurrentType); 820 std::string GetFoldScreenType(); 821 822 protected: 823 // Only for UT CameraManager(sptr<ICameraService> serviceProxy)824 explicit CameraManager(sptr<ICameraService> serviceProxy) : serviceProxyPrivate_(serviceProxy) 825 { 826 // Construct method add mutex lock is not necessary. Ignore g_instanceMutex. 827 CameraManager::g_cameraManager = this; 828 } 829 830 private: 831 832 enum CameraAbilitySupportCacheKey { CAMERA_ABILITY_SUPPORT_TORCH, CAMERA_ABILITY_SUPPORT_MUTE }; 833 834 explicit CameraManager(); 835 void InitCameraManager(); 836 int32_t SetCameraServiceCallback(sptr<ICameraServiceCallback>& callback); 837 int32_t UnSetCameraServiceCallback(); 838 839 int32_t SetCameraMuteServiceCallback(sptr<ICameraMuteServiceCallback>& callback); 840 int32_t UnSetCameraMuteServiceCallback(); 841 842 int32_t SetTorchServiceCallback(sptr<ITorchServiceCallback>& callback); 843 int32_t UnSetTorchServiceCallback(); 844 845 int32_t SetFoldServiceCallback(sptr<IFoldServiceCallback>& callback); 846 int32_t UnSetFoldServiceCallback(); 847 848 int32_t CreateMetadataOutputInternal(sptr<MetadataOutput>& pMetadataOutput, 849 const std::vector<MetadataObjectType>& metadataObjectTypes = {}); 850 851 sptr<CaptureSession> CreateCaptureSessionImpl(SceneMode mode, sptr<ICaptureSession> session); 852 int32_t CreateListenerObject(); 853 void CameraServerDied(pid_t pid); 854 int32_t AddServiceProxyDeathRecipient(); 855 void RemoveServiceProxyDeathRecipient(); 856 sptr<CameraOutputCapability> ParseSupportedOutputCapability(sptr<CameraDevice>& camera, int32_t modeName = 0, 857 std::shared_ptr<OHOS::Camera::CameraMetadata> cameraAbility = nullptr); 858 void ParseProfileLevel( 859 ProfilesWrapper& profilesWrapper, const int32_t modeName, const camera_metadata_item_t& item); 860 void CreateProfileLevel4StreamType(ProfilesWrapper& profilesWrapper, int32_t specId, StreamInfo& streamInfo); 861 void CreateProfile4StreamType(ProfilesWrapper& profilesWrapper, OutputCapStreamType streamType, uint32_t modeIndex, 862 uint32_t streamIndex, ExtendInfo extendInfo); 863 static const std::unordered_map<camera_format_t, CameraFormat> metaToFwCameraFormat_; 864 static const std::unordered_map<CameraFormat, camera_format_t> fwToMetaCameraFormat_; 865 static const std::unordered_map<DepthDataAccuracyType, DepthDataAccuracy> metaToFwDepthDataAccuracy_; 866 void ParseExtendCapability( 867 ProfilesWrapper& profilesWrapper, const int32_t modeName, const camera_metadata_item_t& item); 868 void ParseBasicCapability(ProfilesWrapper& profilesWrapper, std::shared_ptr<OHOS::Camera::CameraMetadata> metadata, 869 const camera_metadata_item_t& item); 870 void CreateDepthProfile4StreamType(OutputCapStreamType streamType, uint32_t modeIndex, 871 uint32_t streamIndex, ExtendInfo extendInfo); 872 void CreateProfile4StreamType(OutputCapStreamType streamType, uint32_t modeIndex, 873 uint32_t streamIndex, ExtendInfo extendInfo); 874 void ParseExtendCapability(const int32_t modeName, const camera_metadata_item_t& item); 875 void ParseBasicCapability( 876 std::shared_ptr<OHOS::Camera::CameraMetadata> metadata, const camera_metadata_item_t& item); 877 void ParseDepthCapability(const int32_t modeName, const camera_metadata_item_t& item); 878 void AlignVideoFpsProfile(std::vector<sptr<CameraDevice>>& cameraObjList); 879 void SetProfile(sptr<CameraDevice>& cameraObj, std::shared_ptr<OHOS::Camera::CameraMetadata> metadata); 880 SceneMode GetFallbackConfigMode(SceneMode profileMode, ProfilesWrapper& profilesWrapper); 881 void ParseCapability(ProfilesWrapper& profilesWrapper, sptr<CameraDevice>& camera, const int32_t modeName, 882 camera_metadata_item_t& item, std::shared_ptr<OHOS::Camera::CameraMetadata> metadata); 883 camera_format_t GetCameraMetadataFormat(CameraFormat format); 884 885 dmDeviceInfo GetDmDeviceInfo(const std::string& cameraId, const std::vector<dmDeviceInfo>& dmDeviceInfoList); 886 int32_t SetTorchLevel(float level); 887 int32_t ValidCreateOutputStream(Profile& profile, const sptr<OHOS::IBufferProducer>& producer); 888 int32_t SubscribeSystemAbility(); 889 int32_t UnSubscribeSystemAbility(); 890 void ReportEvent(const string& cameraId); 891 int32_t RefreshServiceProxy(); 892 std::vector<sptr<CameraDevice>> GetCameraDeviceListFromServer(); 893 bool IsSystemApp(); 894 vector<CameraFormat> GetSupportPhotoFormat(const int32_t modeName, 895 std::shared_ptr<OHOS::Camera::CameraMetadata> metadata); 896 void FillSupportPhotoFormats(std::vector<Profile>& profiles); 897 bool CheckWhiteList(); SetServiceProxy(sptr<ICameraService> proxy)898 inline void SetServiceProxy(sptr<ICameraService> proxy) 899 { 900 std::lock_guard<std::mutex> lock(serviceProxyMutex_); 901 serviceProxyPrivate_ = proxy; 902 } 903 GetCameraDeviceList()904 inline std::vector<sptr<CameraDevice>> GetCameraDeviceList() 905 { 906 std::lock_guard<std::mutex> lock(cameraDeviceListMutex_); 907 if (cameraDeviceList_.empty()) { 908 cameraDeviceList_ = GetCameraDeviceListFromServer(); 909 } 910 return cameraDeviceList_; 911 } 912 IsCameraDeviceListCached()913 inline bool IsCameraDeviceListCached() 914 { 915 std::lock_guard<std::mutex> lock(cameraDeviceListMutex_); 916 return !cameraDeviceList_.empty(); 917 } 918 CacheCameraDeviceAbilitySupportValue(CameraAbilitySupportCacheKey key,bool value)919 inline void CacheCameraDeviceAbilitySupportValue(CameraAbilitySupportCacheKey key, bool value) 920 { 921 std::lock_guard<std::mutex> lock(cameraDeviceAbilitySupportMapMutex_); 922 cameraDeviceAbilitySupportMap_[key] = value; 923 } 924 GetCameraDeviceAbilitySupportValue(CameraAbilitySupportCacheKey key,bool & value)925 inline bool GetCameraDeviceAbilitySupportValue(CameraAbilitySupportCacheKey key, bool& value) 926 { 927 std::lock_guard<std::mutex> lock(cameraDeviceAbilitySupportMapMutex_); 928 auto it = cameraDeviceAbilitySupportMap_.find(key); 929 if (it == cameraDeviceAbilitySupportMap_.end()) { 930 return false; 931 } 932 value = it->second; 933 return true; 934 } 935 936 std::mutex cameraDeviceListMutex_; 937 std::vector<sptr<CameraDevice>> cameraDeviceList_ = {}; 938 939 std::mutex cameraDeviceAbilitySupportMapMutex_; 940 std::unordered_map<CameraAbilitySupportCacheKey, bool> cameraDeviceAbilitySupportMap_; 941 942 std::mutex serviceProxyMutex_; 943 sptr<ICameraService> serviceProxyPrivate_; 944 std::mutex deathRecipientMutex_; 945 sptr<CameraDeathRecipient> deathRecipient_ = nullptr; 946 947 static sptr<CameraManager> g_cameraManager; 948 static std::mutex g_instanceMutex; 949 950 sptr<CameraMuteListenerManager> cameraMuteListenerManager_ = sptr<CameraMuteListenerManager>::MakeSptr(); 951 sptr<TorchServiceListenerManager> torchServiceListenerManager_ = sptr<TorchServiceListenerManager>::MakeSptr(); 952 sptr<CameraStatusListenerManager> cameraStatusListenerManager_ = sptr<CameraStatusListenerManager>::MakeSptr(); 953 sptr<FoldStatusListenerManager> foldStatusListenerManager_ = sptr<FoldStatusListenerManager>::MakeSptr(); 954 955 std::map<std::string, dmDeviceInfo> distributedCamInfoAndId_; 956 957 std::map<std::string, std::vector<Profile>> modePhotoProfiles_ = {}; 958 std::map<std::string, std::vector<Profile>> modePreviewProfiles_ = {}; 959 std::vector<DepthProfile> depthProfiles_ = {}; 960 961 std::vector<CameraFormat> photoFormats_ = {}; 962 sptr<CameraInput> cameraInput_; 963 TorchMode torchMode_ = TorchMode::TORCH_MODE_OFF; 964 std::mutex saListenerMuxtex_; 965 sptr<IRemoteBroker> saListener_ = nullptr; 966 std::string foldScreenType_; 967 bool isSystemApp_ = false; 968 }; 969 970 class CameraManagerGetter { 971 public: 972 void SetCameraManager(wptr<CameraManager> cameraManager); 973 sptr<CameraManager> GetCameraManager(); 974 975 private: 976 wptr<CameraManager> cameraManager_ = nullptr; 977 }; 978 979 class CameraStatusListenerManager : public CameraManagerGetter, 980 public HCameraServiceCallbackStub, 981 public CameraListenerManager<CameraManagerCallback> { 982 public: 983 int32_t OnCameraStatusChanged( 984 const std::string& cameraId, const CameraStatus status, const std::string& bundleName) override; 985 int32_t OnFlashlightStatusChanged(const std::string& cameraId, const FlashStatus status) override; 986 GetCachedCameraStatus()987 inline std::vector<std::shared_ptr<CameraStatusInfo>> GetCachedCameraStatus() 988 { 989 std::lock_guard<std::mutex> lock(cachedCameraStatusMutex_); 990 std::vector<std::shared_ptr<CameraStatusInfo>> infoList = {}; 991 for (auto& it : cachedCameraStatus_) { 992 if (it.second != nullptr) { 993 infoList.emplace_back(it.second); 994 } 995 } 996 return infoList; 997 } GetCachedFlashStatus()998 inline std::vector<std::pair<std::string, FlashStatus>> GetCachedFlashStatus() 999 { 1000 std::lock_guard<std::mutex> lock(cachedFlashStatusMutex_); 1001 std::vector<std::pair<std::string, FlashStatus>> statusList = {}; 1002 for (auto& it : cachedFlashStatus_) { 1003 statusList.emplace_back(it); 1004 } 1005 return statusList; 1006 } 1007 1008 private: CacheCameraStatus(const std::string & cameraId,std::shared_ptr<CameraStatusInfo> statusInfo)1009 inline void CacheCameraStatus(const std::string& cameraId, std::shared_ptr<CameraStatusInfo> statusInfo) 1010 { 1011 std::lock_guard<std::mutex> lock(cachedCameraStatusMutex_); 1012 cachedCameraStatus_[cameraId] = statusInfo; 1013 } 1014 CacheFlashStatus(const std::string & cameraId,const FlashStatus status)1015 inline void CacheFlashStatus(const std::string& cameraId, const FlashStatus status) 1016 { 1017 std::lock_guard<std::mutex> lock(cachedFlashStatusMutex_); 1018 cachedFlashStatus_[cameraId] = status; 1019 } 1020 1021 std::mutex cachedCameraStatusMutex_; 1022 unordered_map<string, std::shared_ptr<CameraStatusInfo>> cachedCameraStatus_; 1023 std::mutex cachedFlashStatusMutex_; 1024 unordered_map<string, FlashStatus> cachedFlashStatus_; 1025 }; 1026 1027 class TorchServiceListenerManager : public CameraManagerGetter, 1028 public HTorchServiceCallbackStub, 1029 public CameraListenerManager<TorchListener> { 1030 public: 1031 int32_t OnTorchStatusChange(const TorchStatus status) override; 1032 GetCachedTorchStatus()1033 inline TorchStatusInfo GetCachedTorchStatus() 1034 { 1035 return cachedTorchStatus_; 1036 } 1037 1038 private: 1039 TorchStatusInfo cachedTorchStatus_ = {}; 1040 }; 1041 1042 class CameraMuteListenerManager : public CameraManagerGetter, 1043 public HCameraMuteServiceCallbackStub, 1044 public CameraListenerManager<CameraMuteListener> { 1045 public: 1046 int32_t OnCameraMute(bool muteMode) override; 1047 }; 1048 1049 class FoldStatusListenerManager : public CameraManagerGetter, 1050 public HFoldServiceCallbackStub, 1051 public CameraListenerManager<FoldListener> { 1052 public: 1053 int32_t OnFoldStatusChanged(const FoldStatus status) override; 1054 }; 1055 } // namespace CameraStandard 1056 } // namespace OHOS 1057 #endif // OHOS_CAMERA_CAMERA_MANAGER_H 1058