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 <refbase.h> 20 #include <iostream> 21 #include <vector> 22 #include "input/camera_input.h" 23 #include "input/camera_info.h" 24 #include "input/camera_device.h" 25 #include "hcamera_service_proxy.h" 26 #include "icamera_device_service.h" 27 #include "session/capture_session.h" 28 #include "output/camera_output_capability.h" 29 #include "output/metadata_output.h" 30 #include "output/photo_output.h" 31 #include "output/video_output.h" 32 #include "output/preview_output.h" 33 #include "hcamera_listener_stub.h" 34 #include "input/camera_death_recipient.h" 35 #include "hcamera_service_callback_stub.h" 36 #include "camera_stream_info_parse.h" 37 38 namespace OHOS { 39 namespace CameraStandard { 40 enum CameraDeviceStatus { 41 CAMERA_DEVICE_STATUS_UNAVAILABLE = 0, 42 CAMERA_DEVICE_STATUS_AVAILABLE 43 }; 44 45 enum FlashlightStatus { 46 FLASHLIGHT_STATUS_OFF = 0, 47 FLASHLIGHT_STATUS_ON, 48 FLASHLIGHT_STATUS_UNAVAILABLE 49 }; 50 51 struct CameraStatusInfo { 52 sptr<CameraInfo> cameraInfo; 53 sptr<CameraDevice> cameraDevice; 54 CameraStatus cameraStatus; ~CameraStatusInfoCameraStatusInfo55 ~CameraStatusInfo() 56 { 57 cameraInfo = nullptr; 58 cameraDevice = nullptr; 59 } 60 }; 61 62 typedef enum OutputCapStreamType { 63 PREVIEW = 0, 64 VIDEO = 1, 65 STILL_CAPTURE = 2, 66 POST_VIEW = 3, 67 ANALYZE = 4, 68 CUSTOM = 5 69 } OutputCapStreamType; 70 71 class CameraManagerCallback { 72 public: 73 CameraManagerCallback() = default; 74 virtual ~CameraManagerCallback() = default; 75 virtual void OnCameraStatusChanged(const CameraStatusInfo &cameraStatusInfo) const = 0; 76 virtual void OnFlashlightStatusChanged(const std::string &cameraID, const FlashStatus flashStatus) const = 0; 77 }; 78 79 class CameraMuteListener { 80 public: 81 CameraMuteListener() = default; 82 virtual ~CameraMuteListener() = default; 83 virtual void OnCameraMute(bool muteMode) const = 0; 84 }; 85 86 class CameraManager; 87 class CameraMuteServiceCallback : public HCameraMuteServiceCallbackStub { 88 public: 89 sptr<CameraManager> camMngr_ = nullptr; CameraMuteServiceCallback()90 CameraMuteServiceCallback() : camMngr_(nullptr) { 91 } 92 CameraMuteServiceCallback(const sptr<CameraManager> & cameraManager)93 explicit CameraMuteServiceCallback(const sptr<CameraManager>& cameraManager) : camMngr_(cameraManager) { 94 } 95 ~CameraMuteServiceCallback()96 ~CameraMuteServiceCallback() 97 { 98 camMngr_ = nullptr; 99 } 100 101 int32_t OnCameraMute(bool muteMode) override; 102 }; 103 104 class CameraStatusServiceCallback : public HCameraServiceCallbackStub { 105 public: 106 sptr<CameraManager> camMngr_ = nullptr; CameraStatusServiceCallback()107 CameraStatusServiceCallback() : camMngr_(nullptr) { 108 } 109 CameraStatusServiceCallback(const sptr<CameraManager> & cameraManager)110 explicit CameraStatusServiceCallback(const sptr<CameraManager>& cameraManager) : camMngr_(cameraManager) { 111 } 112 ~CameraStatusServiceCallback()113 ~CameraStatusServiceCallback() 114 { 115 camMngr_ = nullptr; 116 } 117 118 int32_t OnCameraStatusChanged(const std::string& cameraId, const CameraStatus status) override; 119 120 int32_t OnFlashlightStatusChanged(const std::string& cameraId, const FlashStatus status) override; 121 }; 122 123 class CameraManager : public RefBase { 124 public: 125 ~CameraManager(); 126 /** 127 * @brief Get camera manager instance. 128 * 129 * @return Returns pointer to camera manager instance. 130 */ 131 static sptr<CameraManager> &GetInstance(); 132 133 /** 134 * @brief Get all available cameras. 135 * 136 * @return Returns vector of cameraDevice of available camera. 137 */ 138 std::vector<sptr<CameraDevice>> GetSupportedCameras(); 139 140 /** 141 * @brief Get extend output capaility of the mode of the given camera. 142 * 143 * @param Camera device for which extend capability need to be fetched. 144 * @return Returns vector the ability of the mode of cameraDevice of available camera. 145 */ 146 sptr<CameraOutputCapability> GetSupportedOutputCapability(sptr<CameraDevice>& camera, int32_t modeName = 0); 147 148 /** 149 * @brief Create camera input instance with provided camera position and type. 150 * 151 * @param The cameraDevice for which input has to be created. 152 * @return Returns pointer to camera input instance. 153 */ 154 155 sptr<CameraInput> CreateCameraInput(CameraPosition position, CameraType cameraType); 156 157 /** 158 * @brief Create camera input instance with provided camera position and type. 159 * 160 * @param The cameraDevice for which input has to be created. 161 * @param Returns pointer to camera input instance. 162 * @return Returns error code. 163 */ 164 int CreateCameraInput(CameraPosition position, CameraType cameraType, sptr<CameraInput> *pCameraInput); 165 166 /** 167 * @brief Create camera input instance. 168 * 169 * @param The cameraDevice for which input has to be created. 170 * @return Returns pointer to camera input instance. 171 */ 172 sptr<CameraInput> CreateCameraInput(sptr<CameraDevice> &camera); 173 174 /** 175 * @brief Create camera input instance. 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(sptr<CameraDevice> &camera, sptr<CameraInput> *pCameraInput); 182 183 /** 184 * @brief Get all available cameras. 185 * 186 * @return Returns vector of cameraInfo of available camera. 187 */ 188 [[deprecated]] std::vector<sptr<CameraInfo>> GetCameras(); 189 190 /** 191 * @brief Create camera input instance. 192 * 193 * @param The cameraInfo for which input has to be created. 194 * @return Returns pointer to camera input instance. 195 */ 196 [[deprecated]] sptr<CameraInput> CreateCameraInput(sptr<CameraInfo> &camera); 197 198 /** 199 * @brief Create capture session. 200 * 201 * @return Returns pointer to capture session. 202 */ 203 sptr<CaptureSession> CreateCaptureSession(); 204 205 /** 206 * @brief Create capture session. 207 * 208 * @param Returns pointer to capture session. 209 * @return Returns error code. 210 */ 211 int CreateCaptureSession(sptr<CaptureSession> *pCaptureSession); 212 213 /** 214 * @brief Create photo output instance using surface. 215 * 216 * @param The surface to be used for photo output. 217 * @return Returns pointer to photo output instance. 218 */ 219 sptr<PhotoOutput> CreatePhotoOutput(Profile &profile, sptr<IBufferProducer> &surface); 220 221 /** 222 * @brief Create photo output instance using surface. 223 * 224 * @param The surface to be used for photo output. 225 * @param Returns pointer to photo output instance. 226 * @return Returns error code. 227 */ 228 int CreatePhotoOutput(Profile &profile, sptr<IBufferProducer> &surface, sptr<PhotoOutput> *pPhotoOutput); 229 230 /** 231 * @brief Create photo output instance using surface. 232 * 233 * @param The surface to be used for photo output. 234 * @return Returns pointer to photo output instance. 235 */ 236 [[deprecated]] sptr<PhotoOutput> CreatePhotoOutput(sptr<IBufferProducer> &surface); 237 238 /** 239 * @brief Create photo output instance using IBufferProducer. 240 * 241 * @param The IBufferProducer to be used for photo output. 242 * @param The format to be used for photo capture. 243 * @return Returns pointer to photo output instance. 244 */ 245 [[deprecated]] sptr<PhotoOutput> CreatePhotoOutput(const sptr<OHOS::IBufferProducer> &producer, int32_t format); 246 247 /** 248 * @brief Create video output instance using surface. 249 * 250 * @param The surface to be used for video output. 251 * @return Returns pointer to video output instance. 252 */ 253 sptr<VideoOutput> CreateVideoOutput(VideoProfile &profile, sptr<Surface> &surface); 254 255 /** 256 * @brief Create video output instance using surface. 257 * 258 * @param The surface to be used for video output. 259 * @param Returns pointer to video output instance. 260 * @return Returns error code. 261 */ 262 int CreateVideoOutput(VideoProfile &profile, sptr<Surface> &surface, sptr<VideoOutput> *pVideoOutput); 263 264 /** 265 * @brief Create video output instance using surface. 266 * 267 * @param The surface to be used for video output. 268 * @return Returns pointer to video output instance. 269 */ 270 [[deprecated]] sptr<VideoOutput> CreateVideoOutput(sptr<Surface> &surface); 271 272 /** 273 * @brief Create video output instance using IBufferProducer. 274 * 275 * @param The IBufferProducer to be used for video output. 276 * @param The format to be used for video capture. 277 * @return Returns pointer to video output instance. 278 */ 279 [[deprecated]] sptr<VideoOutput> CreateVideoOutput(const sptr<OHOS::IBufferProducer> &producer, int32_t format); 280 281 /** 282 * @brief Create preview output instance using surface. 283 * 284 * @param The surface to be used for preview. 285 * @return Returns pointer to preview output instance. 286 */ 287 sptr<PreviewOutput> CreatePreviewOutput(Profile &profile, sptr<Surface> surface); 288 289 /** 290 * @brief Create preview output instance using surface. 291 * 292 * @param The surface to be used for preview. 293 * @param Returns pointer to camera preview output instance. 294 * @return Returns error code. 295 */ 296 int CreatePreviewOutput(Profile &profile, sptr<Surface> surface, sptr<PreviewOutput> *pPreviewOutput); 297 298 /** 299 * @brief Create preview output instance using surface. 300 * 301 * @param The surface to be used for preview. 302 * @return Returns pointer to preview output instance. 303 */ 304 [[deprecated]] sptr<PreviewOutput> CreatePreviewOutput(sptr<Surface> surface); 305 306 /** 307 * @brief Create preview output instance using IBufferProducer. 308 * 309 * @param The IBufferProducer to be used for preview output. 310 * @param The format to be used for preview. 311 * @return Returns pointer to video preview instance. 312 */ 313 [[deprecated]] sptr<PreviewOutput> CreatePreviewOutput(const sptr<OHOS::IBufferProducer> &producer, 314 int32_t format); 315 316 /** 317 * @brief Create preview output instance using surface. 318 * 319 * @param The surface to be used for preview. 320 * @return Returns pointer to preview output instance. 321 */ 322 sptr<PreviewOutput> CreateDeferredPreviewOutput(Profile &profile); 323 324 /** 325 * @brief Create preview output instance using surface. 326 * 327 * @param The surface to be used for preview. 328 * @param Returns pointer to preview output instance. 329 * @return Returns error code. 330 */ 331 int CreateDeferredPreviewOutput(Profile &profile, sptr<PreviewOutput> *pPreviewOutput); 332 333 /** 334 * @brief Create preview output instance using surface 335 * with custom width and height. 336 * 337 * @param The surface to be used for preview. 338 * @param preview width. 339 * @param preview height. 340 * @return Returns pointer to preview output instance. 341 */ 342 [[deprecated]] sptr<PreviewOutput> CreateCustomPreviewOutput(sptr<Surface> surface, int32_t width, int32_t height); 343 344 /** 345 * @brief Create preview output instance using IBufferProducer 346 * with custom width and height. 347 * 348 * @param The IBufferProducer to be used for preview output. 349 * @param The format to be used for preview. 350 * @param preview width. 351 * @param preview height. 352 * @return Returns pointer to preview output instance. 353 */ 354 [[deprecated]] sptr<PreviewOutput> CreateCustomPreviewOutput(const sptr<OHOS::IBufferProducer> &producer, 355 int32_t format, int32_t width, int32_t height); 356 357 /** 358 * @brief Create metadata output instance. 359 * 360 * @return Returns pointer to metadata output instance. 361 */ 362 sptr<MetadataOutput> CreateMetadataOutput(); 363 364 /** 365 * @brief Create metadata output instance. 366 * 367 * @param Returns pointer to metadata output instance. 368 * @return Returns error code. 369 */ 370 int CreateMetadataOutput(sptr<MetadataOutput> *pMetadataOutput); 371 372 /** 373 * @brief Set camera manager callback. 374 * 375 * @param CameraManagerCallback pointer. 376 */ 377 void SetCallback(std::shared_ptr<CameraManagerCallback> callback); 378 379 /** 380 * @brief Get the application callback. 381 * 382 * @return CameraManagerCallback pointer is set by application. 383 */ 384 std::shared_ptr<CameraManagerCallback> GetApplicationCallback(); 385 386 /** 387 * @brief Get cameraDevice of specific camera id. 388 * 389 * @param std::string camera id. 390 * @return Returns pointer to cameraDevice of given Id if found else return nullptr. 391 */ 392 sptr<CameraDevice> GetCameraDeviceFromId(std::string cameraId); 393 394 /** 395 * @brief Get cameraInfo of specific camera id. 396 * 397 * @param std::string camera id. 398 * @return Returns pointer to cameraInfo of given Id if found else return nullptr. 399 */ 400 [[deprecated]] sptr<CameraInfo> GetCameraInfo(std::string cameraId); 401 402 /** 403 * @brief Get the support of camera mute mode. 404 * 405 * @return Returns true is supported, false is not supported. 406 */ 407 bool IsCameraMuteSupported(); 408 409 /** 410 * @brief Get camera mute mode. 411 * 412 * @return Returns true is in mute, else is not in mute. 413 */ 414 bool IsCameraMuted(); 415 416 /** 417 * @brief Mute the camera 418 * 419 * @return. 420 */ 421 void MuteCamera(bool muteMode); 422 423 /** 424 * @brief register camera mute listener 425 * 426 * @param CameraMuteListener listener object. 427 * @return. 428 */ 429 void RegisterCameraMuteListener(std::shared_ptr<CameraMuteListener> listener); 430 431 /** 432 * @brief get the camera mute listener 433 * 434 * @return CameraMuteListener point.. 435 */ 436 std::vector<std::shared_ptr<CameraMuteListener>> GetCameraMuteListener(); 437 438 /** 439 * @brief prelaunch the camera 440 * 441 * @return. 442 */ 443 int32_t PrelaunchCamera(); 444 445 /** 446 * @brief set prelaunch config 447 * 448 * @return. 449 */ 450 int32_t SetPrelaunchConfig(std::string cameraId); 451 452 /** 453 * @brief Get the support of camera pre launch mode. 454 * 455 * @return Returns true is supported, false is not supported. 456 */ 457 bool IsPrelaunchSupported(sptr<CameraDevice> camera); 458 459 static const std::string surfaceFormat; 460 461 protected: CameraManager(sptr<ICameraService> serviceProxy)462 explicit CameraManager(sptr<ICameraService> serviceProxy) : serviceProxy_(serviceProxy) {} 463 464 private: 465 CameraManager(); 466 void Init(); 467 void SetCameraServiceCallback(sptr<ICameraServiceCallback>& callback); 468 void SetCameraMuteServiceCallback(sptr<ICameraMuteServiceCallback>& callback); 469 int32_t CreateListenerObject(); 470 void CameraServerDied(pid_t pid); 471 void ChooseDeFaultCameras(std::vector<sptr<CameraDevice>>& supportedCameras); 472 473 void CreateProfile4StreamType(OutputCapStreamType streamType, uint32_t modeIndex, 474 uint32_t streamIndex, ExtendInfo extendInfo); 475 static const std::unordered_map<camera_format_t, CameraFormat> metaToFwCameraFormat_; 476 static const std::unordered_map<CameraFormat, camera_format_t> fwToMetaCameraFormat_; 477 void ParseExtendCapability(sptr<CameraOutputCapability> cameraOutputCapability, 478 const int32_t modeName, const camera_metadata_item_t &item); 479 void ParseBasicCapability(sptr<CameraOutputCapability> cameraOutputCapability, 480 std::shared_ptr<OHOS::Camera::CameraMetadata> metadata, const camera_metadata_item_t &item); 481 482 std::mutex mutex_; 483 std::mutex cameraMngrCbMutex_; 484 std::mutex vectorMutex_; 485 int CreateCameraDevice(std::string cameraId, sptr<ICameraDeviceService> *pICameraDeviceService); 486 camera_format_t GetCameraMetadataFormat(CameraFormat format); 487 bool GetDmDeviceInfo(); 488 bool isDistributeCamera(std::string cameraId, dmDeviceInfo& deviceInfo); 489 sptr<ICameraService> serviceProxy_; 490 sptr<CameraListenerStub> listenerStub_ = nullptr; 491 sptr<CameraDeathRecipient> deathRecipient_ = nullptr; 492 static sptr<CameraManager> cameraManager_; 493 sptr<ICameraServiceCallback> cameraSvcCallback_; 494 std::shared_ptr<CameraManagerCallback> cameraMngrCallback_; 495 496 sptr<ICameraMuteServiceCallback> cameraMuteSvcCallback_; 497 std::vector<std::shared_ptr<CameraMuteListener>> cameraMuteListenerList; 498 std::vector<sptr<CameraDevice>> cameraObjList; 499 std::vector<sptr<CameraInfo>> dcameraObjList; 500 std::vector<dmDeviceInfo> distributedCamInfo_; 501 std::map<std::string, dmDeviceInfo> distributedCamInfoAndId_; 502 class DeviceInitCallBack; 503 std::vector<Profile> photoProfiles_ = {}; 504 std::vector<Profile> previewProfiles_ = {}; 505 std::vector<VideoProfile> vidProfiles_ = {}; 506 }; 507 } // namespace CameraStandard 508 } // namespace OHOS 509 #endif // OHOS_CAMERA_CAMERA_MANAGER_H 510