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 <iostream> 20 #include <refbase.h> 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 37 namespace OHOS { 38 namespace CameraStandard { 39 enum CameraDeviceStatus { 40 CAMERA_DEVICE_STATUS_UNAVAILABLE = 0, 41 CAMERA_DEVICE_STATUS_AVAILABLE 42 }; 43 44 enum FlashlightStatus { 45 FLASHLIGHT_STATUS_OFF = 0, 46 FLASHLIGHT_STATUS_ON, 47 FLASHLIGHT_STATUS_UNAVAILABLE 48 }; 49 50 struct CameraStatusInfo { 51 sptr<CameraInfo> cameraInfo; 52 sptr<CameraDevice> cameraDevice; 53 CameraStatus cameraStatus; ~CameraStatusInfoCameraStatusInfo54 ~CameraStatusInfo() 55 { 56 cameraInfo = nullptr; 57 cameraDevice = nullptr; 58 } 59 }; 60 61 class CameraManagerCallback { 62 public: 63 CameraManagerCallback() = default; 64 virtual ~CameraManagerCallback() = default; 65 virtual void OnCameraStatusChanged(const CameraStatusInfo &cameraStatusInfo) const = 0; 66 virtual void OnFlashlightStatusChanged(const std::string &cameraID, const FlashStatus flashStatus) const = 0; 67 }; 68 69 class CameraMuteListener { 70 public: 71 CameraMuteListener() = default; 72 virtual ~CameraMuteListener() = default; 73 virtual void OnCameraMute(bool muteMode) const = 0; 74 }; 75 76 class CameraManager; 77 class CameraMuteServiceCallback : public HCameraMuteServiceCallbackStub { 78 public: 79 sptr<CameraManager> camMngr_ = nullptr; CameraMuteServiceCallback()80 CameraMuteServiceCallback() : camMngr_(nullptr) { 81 } 82 CameraMuteServiceCallback(const sptr<CameraManager> & cameraManager)83 explicit CameraMuteServiceCallback(const sptr<CameraManager>& cameraManager) : camMngr_(cameraManager) { 84 } 85 ~CameraMuteServiceCallback()86 ~CameraMuteServiceCallback() 87 { 88 camMngr_ = nullptr; 89 } 90 91 int32_t OnCameraMute(bool muteMode) override; 92 }; 93 94 class CameraManager : public RefBase { 95 public: 96 ~CameraManager(); 97 /** 98 * @brief Get camera manager instance. 99 * 100 * @return Returns pointer to camera manager instance. 101 */ 102 static sptr<CameraManager> &GetInstance(); 103 104 /** 105 * @brief Get all available cameras. 106 * 107 * @return Returns vector of cameraDevice of available camera. 108 */ 109 std::vector<sptr<CameraDevice>> GetSupportedCameras(); 110 111 /** 112 * @brief Get output capaility of the given camera. 113 * 114 * @param Camera device for which capability need to be fetched. 115 * @return Returns vector of cameraDevice of available camera. 116 */ 117 sptr<CameraOutputCapability> GetSupportedOutputCapability(sptr<CameraDevice>& camera); 118 119 /** 120 * @brief Create camera input instance with provided camera position and type. 121 * 122 * @param The cameraDevice for which input has to be created. 123 * @return Returns pointer to camera input instance. 124 */ 125 sptr<CameraInput> CreateCameraInput(CameraPosition position, CameraType cameraType); 126 127 /** 128 * @brief Create camera input instance with provided camera position and type. 129 * 130 * @param The cameraDevice for which input has to be created. 131 * @param Returns pointer to camera input instance. 132 * @return Returns error code. 133 */ 134 int CreateCameraInput(CameraPosition position, CameraType cameraType, sptr<CameraInput> *pCameraInput); 135 136 /** 137 * @brief Create camera input instance. 138 * 139 * @param The cameraDevice for which input has to be created. 140 * @return Returns pointer to camera input instance. 141 */ 142 sptr<CameraInput> CreateCameraInput(sptr<CameraDevice> &camera); 143 144 /** 145 * @brief Create camera input instance. 146 * 147 * @param The cameraDevice for which input has to be created. 148 * @param Returns pointer to camera input instance. 149 * @return Returns error code. 150 */ 151 int CreateCameraInput(sptr<CameraDevice> &camera, sptr<CameraInput> *pCameraInput); 152 153 /** 154 * @brief Get all available cameras. 155 * 156 * @return Returns vector of cameraInfo of available camera. 157 */ 158 [[deprecated]] std::vector<sptr<CameraInfo>> GetCameras(); 159 160 /** 161 * @brief Create camera input instance. 162 * 163 * @param The cameraInfo for which input has to be created. 164 * @return Returns pointer to camera input instance. 165 */ 166 [[deprecated]] sptr<CameraInput> CreateCameraInput(sptr<CameraInfo> &camera); 167 168 /** 169 * @brief Create capture session. 170 * 171 * @return Returns pointer to capture session. 172 */ 173 sptr<CaptureSession> CreateCaptureSession(); 174 175 /** 176 * @brief Create capture session. 177 * 178 * @param Returns pointer to capture session. 179 * @return Returns error code. 180 */ 181 int CreateCaptureSession(sptr<CaptureSession> *pCaptureSession); 182 183 /** 184 * @brief Create photo output instance using surface. 185 * 186 * @param The surface to be used for photo output. 187 * @return Returns pointer to photo output instance. 188 */ 189 sptr<PhotoOutput> CreatePhotoOutput(Profile &profile, sptr<Surface> &surface); 190 191 /** 192 * @brief Create photo output instance using surface. 193 * 194 * @param The surface to be used for photo output. 195 * @param Returns pointer to photo output instance. 196 * @return Returns error code. 197 */ 198 int CreatePhotoOutput(Profile &profile, sptr<Surface> &surface, sptr<PhotoOutput> *pPhotoOutput); 199 200 /** 201 * @brief Create photo output instance using surface. 202 * 203 * @param The surface to be used for photo output. 204 * @return Returns pointer to photo output instance. 205 */ 206 [[deprecated]] sptr<PhotoOutput> CreatePhotoOutput(sptr<Surface> &surface); 207 208 /** 209 * @brief Create photo output instance using IBufferProducer. 210 * 211 * @param The IBufferProducer to be used for photo output. 212 * @param The format to be used for photo capture. 213 * @return Returns pointer to photo output instance. 214 */ 215 [[deprecated]] sptr<PhotoOutput> CreatePhotoOutput(const sptr<OHOS::IBufferProducer> &producer, int32_t format); 216 217 /** 218 * @brief Create video output instance using surface. 219 * 220 * @param The surface to be used for video output. 221 * @return Returns pointer to video output instance. 222 */ 223 sptr<VideoOutput> CreateVideoOutput(VideoProfile &profile, sptr<Surface> &surface); 224 225 /** 226 * @brief Create video output instance using surface. 227 * 228 * @param The surface to be used for video output. 229 * @param Returns pointer to video output instance. 230 * @return Returns error code. 231 */ 232 int CreateVideoOutput(VideoProfile &profile, sptr<Surface> &surface, sptr<VideoOutput> *pVideoOutput); 233 234 /** 235 * @brief Create video output instance using surface. 236 * 237 * @param The surface to be used for video output. 238 * @return Returns pointer to video output instance. 239 */ 240 [[deprecated]] sptr<VideoOutput> CreateVideoOutput(sptr<Surface> &surface); 241 242 /** 243 * @brief Create video output instance using IBufferProducer. 244 * 245 * @param The IBufferProducer to be used for video output. 246 * @param The format to be used for video capture. 247 * @return Returns pointer to video output instance. 248 */ 249 [[deprecated]] sptr<VideoOutput> CreateVideoOutput(const sptr<OHOS::IBufferProducer> &producer, int32_t format); 250 251 /** 252 * @brief Create preview output instance using surface. 253 * 254 * @param The surface to be used for preview. 255 * @return Returns pointer to preview output instance. 256 */ 257 sptr<PreviewOutput> CreatePreviewOutput(Profile &profile, sptr<Surface> surface); 258 259 /** 260 * @brief Create preview output instance using surface. 261 * 262 * @param The surface to be used for preview. 263 * @param Returns pointer to camera preview output instance. 264 * @return Returns error code. 265 */ 266 int CreatePreviewOutput(Profile &profile, sptr<Surface> surface, sptr<PreviewOutput> *pPreviewOutput); 267 268 /** 269 * @brief Create preview output instance using surface. 270 * 271 * @param The surface to be used for preview. 272 * @return Returns pointer to preview output instance. 273 */ 274 [[deprecated]] sptr<PreviewOutput> CreatePreviewOutput(sptr<Surface> surface); 275 276 /** 277 * @brief Create preview output instance using IBufferProducer. 278 * 279 * @param The IBufferProducer to be used for preview output. 280 * @param The format to be used for preview. 281 * @return Returns pointer to video preview instance. 282 */ 283 [[deprecated]] sptr<PreviewOutput> CreatePreviewOutput(const sptr<OHOS::IBufferProducer> &producer, 284 int32_t format); 285 286 /** 287 * @brief Create preview output instance using surface. 288 * 289 * @param The surface to be used for preview. 290 * @return Returns pointer to preview output instance. 291 */ 292 sptr<PreviewOutput> CreateDeferredPreviewOutput(Profile &profile); 293 294 /** 295 * @brief Create preview output instance using surface. 296 * 297 * @param The surface to be used for preview. 298 * @param Returns pointer to preview output instance. 299 * @return Returns error code. 300 */ 301 int CreateDeferredPreviewOutput(Profile &profile, sptr<PreviewOutput> *pPreviewOutput); 302 303 /** 304 * @brief Create preview output instance using surface 305 * with custom width and height. 306 * 307 * @param The surface to be used for preview. 308 * @param preview width. 309 * @param preview height. 310 * @return Returns pointer to preview output instance. 311 */ 312 [[deprecated]] sptr<PreviewOutput> CreateCustomPreviewOutput(sptr<Surface> surface, int32_t width, int32_t height); 313 314 /** 315 * @brief Create preview output instance using IBufferProducer 316 * with custom width and height. 317 * 318 * @param The IBufferProducer to be used for preview output. 319 * @param The format to be used for preview. 320 * @param preview width. 321 * @param preview height. 322 * @return Returns pointer to preview output instance. 323 */ 324 [[deprecated]] sptr<PreviewOutput> CreateCustomPreviewOutput(const sptr<OHOS::IBufferProducer> &producer, 325 int32_t format, int32_t width, int32_t height); 326 327 /** 328 * @brief Create metadata output instance. 329 * 330 * @return Returns pointer to metadata output instance. 331 */ 332 sptr<MetadataOutput> CreateMetadataOutput(); 333 334 /** 335 * @brief Create metadata output instance. 336 * 337 * @param Returns pointer to metadata output instance. 338 * @return Returns error code. 339 */ 340 int CreateMetadataOutput(sptr<MetadataOutput> *pMetadataOutput); 341 342 /** 343 * @brief Set camera manager callback. 344 * 345 * @param CameraManagerCallback pointer. 346 */ 347 void SetCallback(std::shared_ptr<CameraManagerCallback> callback); 348 349 /** 350 * @brief Get the application callback. 351 * 352 * @return CameraManagerCallback pointer is set by application. 353 */ 354 std::shared_ptr<CameraManagerCallback> GetApplicationCallback(); 355 356 /** 357 * @brief Get cameraDevice of specific camera id. 358 * 359 * @param std::string camera id. 360 * @return Returns pointer to cameraDevice of given Id if found else return nullptr. 361 */ 362 sptr<CameraDevice> GetCameraDeviceFromId(std::string cameraId); 363 364 /** 365 * @brief Get cameraInfo of specific camera id. 366 * 367 * @param std::string camera id. 368 * @return Returns pointer to cameraInfo of given Id if found else return nullptr. 369 */ 370 [[deprecated]] sptr<CameraInfo> GetCameraInfo(std::string cameraId); 371 372 /** 373 * @brief Get the support of camera mute mode. 374 * 375 * @return Returns true is supported, false is not supported. 376 */ 377 bool IsCameraMuteSupported(); 378 379 /** 380 * @brief Get camera mute mode. 381 * 382 * @return Returns true is in mute, else is not in mute. 383 */ 384 bool IsCameraMuted(); 385 386 /** 387 * @brief Mute the camera 388 * 389 * @return. 390 */ 391 void MuteCamera(bool muteMode); 392 393 /** 394 * @brief register camera mute listener 395 * 396 * @param CameraMuteListener listener object. 397 * @return. 398 */ 399 void RegisterCameraMuteListener(std::shared_ptr<CameraMuteListener> listener); 400 401 /** 402 * @brief get the camera mute listener 403 * 404 * @return CameraMuteListener point.. 405 */ 406 std::vector<std::shared_ptr<CameraMuteListener>> GetCameraMuteListener(); 407 408 static const std::string surfaceFormat; 409 410 protected: CameraManager(sptr<ICameraService> serviceProxy)411 explicit CameraManager(sptr<ICameraService> serviceProxy) : serviceProxy_(serviceProxy) {} 412 413 private: 414 CameraManager(); 415 void Init(); 416 void SetCameraServiceCallback(sptr<ICameraServiceCallback>& callback); 417 void SetCameraMuteServiceCallback(sptr<ICameraMuteServiceCallback>& callback); 418 int32_t CreateListenerObject(); 419 void CameraServerDied(pid_t pid); 420 void ChooseDeFaultCameras(std::vector<sptr<CameraDevice>>& supportedCameras); 421 static const std::unordered_map<camera_format_t, CameraFormat> metaToFwCameraFormat_; 422 static const std::unordered_map<CameraFormat, camera_format_t> fwToMetaCameraFormat_; 423 424 std::mutex mutex_; 425 int CreateCameraDevice(std::string cameraId, sptr<ICameraDeviceService> *pICameraDeviceService); 426 camera_format_t GetCameraMetadataFormat(CameraFormat format); 427 sptr<ICameraService> serviceProxy_; 428 sptr<CameraListenerStub> listenerStub_ = nullptr; 429 sptr<CameraDeathRecipient> deathRecipient_ = nullptr; 430 static sptr<CameraManager> cameraManager_; 431 sptr<ICameraServiceCallback> cameraSvcCallback_; 432 std::shared_ptr<CameraManagerCallback> cameraMngrCallback_; 433 434 sptr<ICameraMuteServiceCallback> cameraMuteSvcCallback_; 435 std::vector<std::shared_ptr<CameraMuteListener>> cameraMuteListenerList; 436 std::vector<sptr<CameraDevice>> cameraObjList; 437 std::vector<sptr<CameraInfo>> dcameraObjList; 438 }; 439 } // namespace CameraStandard 440 } // namespace OHOS 441 #endif // OHOS_CAMERA_CAMERA_MANAGER_H 442