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_CAPTURE_SESSION_H 17 #define OHOS_CAMERA_CAPTURE_SESSION_H 18 19 #include <iostream> 20 #include <set> 21 #include <vector> 22 #include "camera_error_code.h" 23 #include "input/capture_input.h" 24 #include "output/capture_output.h" 25 #include "icamera_util.h" 26 #include "icapture_session.h" 27 #include "icapture_session_callback.h" 28 29 namespace OHOS { 30 namespace CameraStandard { 31 enum ExposureMode { 32 EXPOSURE_MODE_UNSUPPORTED = -1, 33 EXPOSURE_MODE_LOCKED = 0, 34 EXPOSURE_MODE_AUTO, 35 EXPOSURE_MODE_CONTINUOUS_AUTO 36 }; 37 38 enum FlashMode { 39 FLASH_MODE_CLOSE = 0, 40 FLASH_MODE_OPEN, 41 FLASH_MODE_AUTO, 42 FLASH_MODE_ALWAYS_OPEN, 43 }; 44 45 enum FocusMode { 46 FOCUS_MODE_MANUAL = 0, 47 FOCUS_MODE_CONTINUOUS_AUTO, 48 FOCUS_MODE_AUTO, 49 FOCUS_MODE_LOCKED, 50 }; 51 52 enum FocusState { 53 FOCUS_STATE_SCAN = 0, 54 FOCUS_STATE_FOCUSED, 55 FOCUS_STATE_UNFOCUSED 56 }; 57 58 enum ExposureState { 59 EXPOSURE_STATE_SCAN = 0, 60 EXPOSURE_STATE_CONVERGED 61 }; 62 63 typedef struct { 64 float x; 65 float y; 66 }Point; 67 68 class SessionCallback { 69 public: 70 SessionCallback() = default; 71 virtual ~SessionCallback() = default; 72 /** 73 * @brief Called when error occured during capture session callback. 74 * 75 * @param errorCode Indicates a {@link ErrorCode} which will give information for capture session callback error. 76 */ 77 virtual void OnError(int32_t errorCode) = 0; 78 }; 79 80 class ExposureCallback { 81 public: 82 enum ExposureState { 83 SCAN = 0, 84 CONVERGED, 85 }; 86 ExposureCallback() = default; 87 virtual ~ExposureCallback() = default; 88 virtual void OnExposureState(ExposureState state) = 0; 89 }; 90 91 class FocusCallback { 92 public: 93 enum FocusState { 94 SCAN = 0, 95 FOCUSED, 96 UNFOCUSED 97 }; 98 FocusCallback() = default; 99 virtual ~FocusCallback() = default; 100 virtual void OnFocusState(FocusState state) = 0; 101 }; 102 103 enum VideoStabilizationMode { 104 OFF = 0, 105 LOW, 106 MIDDLE, 107 HIGH, 108 AUTO 109 }; 110 111 class CaptureOutput; 112 class CaptureSession : public RefBase { 113 public: 114 sptr<CaptureInput> inputDevice_; 115 explicit CaptureSession(sptr<ICaptureSession> &captureSession); 116 ~CaptureSession(); 117 118 /** 119 * @brief Begin the capture session config. 120 */ 121 int32_t BeginConfig(); 122 123 /** 124 * @brief Commit the capture session config. 125 */ 126 int32_t CommitConfig(); 127 128 /** 129 * @brief Determine if the given Input can be added to session. 130 * 131 * @param CaptureInput to be added to session. 132 */ 133 int32_t CanAddInput(sptr<CaptureInput> &input); 134 135 /** 136 * @brief Add CaptureInput for the capture session. 137 * 138 * @param CaptureInput to be added to session. 139 */ 140 int32_t AddInput(sptr<CaptureInput> &input); 141 142 /** 143 * @brief Determine if the given Ouput can be added to session. 144 * 145 * @param CaptureOutput to be added to session. 146 */ 147 int32_t CanAddOutput(sptr<CaptureOutput> &output); 148 149 /** 150 * @brief Add CaptureOutput for the capture session. 151 * 152 * @param CaptureOutput to be added to session. 153 */ 154 int32_t AddOutput(sptr<CaptureOutput> &output); 155 156 /** 157 * @brief Remove CaptureInput for the capture session. 158 * 159 * @param CaptureInput to be removed from session. 160 */ 161 int32_t RemoveInput(sptr<CaptureInput> &input); 162 163 /** 164 * @brief Remove CaptureOutput for the capture session. 165 * 166 * @param CaptureOutput to be removed from session. 167 */ 168 int32_t RemoveOutput(sptr<CaptureOutput> &output); 169 170 /** 171 * @brief Starts session and preview. 172 */ 173 int32_t Start(); 174 175 /** 176 * @brief Stop session and preview.. 177 */ 178 int32_t Stop(); 179 180 /** 181 * @brief Set the session callback for the capture session. 182 * 183 * @param SessionCallback pointer to be triggered. 184 */ 185 void SetCallback(std::shared_ptr<SessionCallback> callback); 186 187 /** 188 * @brief Get the application callback information. 189 * 190 * @return Returns the pointer to SessionCallback set by application. 191 */ 192 std::shared_ptr<SessionCallback> GetApplicationCallback(); 193 194 /** 195 * @brief Releases CaptureSession instance. 196 * @return Returns errCode. 197 */ 198 int32_t Release(); 199 200 /** 201 * @brief create new device control setting. 202 */ 203 void LockForControl(); 204 205 /** 206 * @brief submit device control setting. 207 * 208 * @return Returns CAMERA_OK is success. 209 */ 210 int32_t UnlockForControl(); 211 212 /** 213 * @brief Get the supported video sabilization modes. 214 * 215 * @return Returns vector of CameraVideoStabilizationMode supported stabilization modes. 216 */ 217 std::vector<VideoStabilizationMode> GetSupportedStabilizationMode(); 218 219 /** 220 * @brief Get the supported video sabilization modes. 221 * @param vector of CameraVideoStabilizationMode supported stabilization modes. 222 * @return Returns errCode. 223 */ 224 int32_t GetSupportedStabilizationMode(std::vector<VideoStabilizationMode> &modes); 225 226 /** 227 * @brief Query whether given stabilization mode supported. 228 * 229 * @param VideoStabilizationMode stabilization mode to query. 230 * @return True is supported false otherwise. 231 */ 232 bool IsVideoStabilizationModeSupported(VideoStabilizationMode stabilizationMode); 233 234 /** 235 * @brief Query whether given stabilization mode supported. 236 * 237 * @param VideoStabilizationMode stabilization mode to query. 238 * @param bool True is supported false otherwise. 239 * @return errCode. 240 */ 241 int32_t IsVideoStabilizationModeSupported(VideoStabilizationMode stabilizationMode, bool &isSupported); 242 243 /** 244 * @brief Get the current Video Stabilizaion mode. 245 * 246 * @return Returns current Video Stabilizaion mode. 247 */ 248 VideoStabilizationMode GetActiveVideoStabilizationMode(); 249 250 /** 251 * @brief Get the current Video Stabilizaion mode. 252 * @param current Video Stabilizaion mode. 253 * @return errCode 254 */ 255 int32_t GetActiveVideoStabilizationMode(VideoStabilizationMode &mode); 256 257 /** 258 * @brief Set the Video Stabilizaion mode. 259 * @param VideoStabilizationMode stabilization mode to set. 260 * @return errCode 261 */ 262 int32_t SetVideoStabilizationMode(VideoStabilizationMode stabilizationMode); 263 264 /** 265 * @brief Get the supported exposure modes. 266 * 267 * @return Returns vector of ExposureMode supported exposure modes. 268 */ 269 std::vector<ExposureMode> GetSupportedExposureModes(); 270 271 /** 272 * @brief Get the supported exposure modes. 273 * @param vector of ExposureMode supported exposure modes. 274 * @return errCode. 275 */ 276 int32_t GetSupportedExposureModes(std::vector<ExposureMode> &exposureModes); 277 278 /** 279 * @brief Query whether given exposure mode supported. 280 * 281 * @param ExposureMode exposure mode to query. 282 * @return True is supported false otherwise. 283 */ 284 bool IsExposureModeSupported(ExposureMode exposureMode); 285 286 /** 287 * @brief Query whether given exposure mode supported. 288 * 289 * @param ExposureMode exposure mode to query. 290 * @param bool True is supported false otherwise. 291 * @return errCode. 292 */ 293 int32_t IsExposureModeSupported(ExposureMode exposureMode, bool &isSupported); 294 295 /** 296 * @brief Set exposure mode. 297 * @param ExposureMode exposure mode to be set. 298 * @return errCode 299 */ 300 int32_t SetExposureMode(ExposureMode exposureMode); 301 302 /** 303 * @brief Get the current exposure mode. 304 * 305 * @return Returns current exposure mode. 306 */ 307 ExposureMode GetExposureMode(); 308 309 /** 310 * @brief Get the current exposure mode. 311 * @param ExposureMode current exposure mode. 312 * @return errCode. 313 */ 314 int32_t GetExposureMode(ExposureMode &exposureMode); 315 316 /** 317 * @brief Set the centre point of exposure area. 318 * @param Point which specifies the area to expose. 319 * @return errCode 320 */ 321 int32_t SetMeteringPoint(Point exposurePoint); 322 323 /** 324 * @brief Get centre point of exposure area. 325 * 326 * @return Returns current exposure point. 327 */ 328 Point GetMeteringPoint(); 329 330 /** 331 * @brief Get centre point of exposure area. 332 * @param Point current exposure point. 333 * @return errCode 334 */ 335 int32_t GetMeteringPoint(Point &exposurePoint); 336 337 /** 338 * @brief Get exposure compensation range. 339 * 340 * @return Returns supported exposure compensation range. 341 */ 342 std::vector<int32_t> GetExposureBiasRange(); 343 344 /** 345 * @brief Get exposure compensation range. 346 * @param vector of exposure bias range. 347 * @return errCode. 348 */ 349 int32_t GetExposureBiasRange(std::vector<int32_t> &exposureBiasRange); 350 351 /** 352 * @brief Set exposure compensation value. 353 * @param exposure compensation value to be set. 354 * @return errCode. 355 */ 356 int32_t SetExposureBias(int32_t exposureBias); 357 358 /** 359 * @brief Get exposure compensation value. 360 * 361 * @return Returns current exposure compensation value. 362 */ 363 int32_t GetExposureValue(); 364 365 /** 366 * @brief Get exposure compensation value. 367 * @param exposure current exposure compensation value . 368 * @return Returns errCode. 369 */ 370 int32_t GetExposureValue(int32_t &exposure); 371 372 /** 373 * @brief Set the exposure callback. 374 * which will be called when there is exposure state change. 375 * 376 * @param The ExposureCallback pointer. 377 */ 378 void SetExposureCallback(std::shared_ptr<ExposureCallback> exposureCallback); 379 380 /** 381 * @brief This function is called when there is exposure state change 382 * and process the exposure state callback. 383 * 384 * @param result metadata got from callback from service layer. 385 */ 386 void ProcessAutoExposureUpdates(const std::shared_ptr<OHOS::Camera::CameraMetadata> &result); 387 388 /** 389 * @brief Get the supported Focus modes. 390 * 391 * @return Returns vector of FocusMode supported exposure modes. 392 */ 393 std::vector<FocusMode> GetSupportedFocusModes(); 394 395 /** 396 * @brief Get the supported Focus modes. 397 * @param vector of FocusMode supported. 398 * @return Returns errCode. 399 */ 400 int32_t GetSupportedFocusModes(std::vector<FocusMode> &modes); 401 402 /** 403 * @brief Query whether given focus mode supported. 404 * 405 * @param camera_focus_mode_enum_t focus mode to query. 406 * @return True is supported false otherwise. 407 */ 408 bool IsFocusModeSupported(FocusMode focusMode); 409 410 /** 411 * @brief Query whether given focus mode supported. 412 * 413 * @param camera_focus_mode_enum_t focus mode to query. 414 * @param bool True is supported false otherwise. 415 * @return Returns errCode. 416 */ 417 int32_t IsFocusModeSupported(FocusMode focusMode, bool &isSupported); 418 419 /** 420 * @brief Set Focus mode. 421 * 422 * @param FocusMode focus mode to be set. 423 * @return Returns errCode. 424 */ 425 int32_t SetFocusMode(FocusMode focusMode); 426 427 /** 428 * @brief Get the current focus mode. 429 * 430 * @return Returns current focus mode. 431 */ 432 FocusMode GetFocusMode(); 433 434 /** 435 * @brief Get the current focus mode. 436 * @param FocusMode current focus mode. 437 * @return Returns errCode. 438 */ 439 int32_t GetFocusMode(FocusMode &focusMode); 440 441 /** 442 * @brief Set the focus callback. 443 * which will be called when there is focus state change. 444 * 445 * @param The ExposureCallback pointer. 446 */ 447 void SetFocusCallback(std::shared_ptr<FocusCallback> focusCallback); 448 449 /** 450 * @brief This function is called when there is focus state change 451 * and process the focus state callback. 452 * 453 * @param result metadata got from callback from service layer. 454 */ 455 void ProcessAutoFocusUpdates(const std::shared_ptr<OHOS::Camera::CameraMetadata> &result); 456 457 /** 458 * @brief Set the Focus area. 459 * 460 * @param Point which specifies the area to focus. 461 * @return Returns errCode. 462 */ 463 int32_t SetFocusPoint(Point focusPoint); 464 465 /** 466 * @brief Get centre point of focus area. 467 * 468 * @return Returns current focus point. 469 */ 470 Point GetFocusPoint(); 471 472 /** 473 * @brief Get centre point of focus area. 474 * @param Point current focus point. 475 * @return Returns errCode. 476 */ 477 int32_t GetFocusPoint(Point &focusPoint); 478 479 /** 480 * @brief Get focal length. 481 * 482 * @return Returns focal length value. 483 */ 484 float GetFocalLength(); 485 486 /** 487 * @brief Get focal length. 488 * @param focalLength current focal length compensation value . 489 * @return Returns errCode. 490 */ 491 int32_t GetFocalLength(float &focalLength); 492 493 /** 494 * @brief Get the supported Focus modes. 495 * 496 * @return Returns vector of camera_focus_mode_enum_t supported exposure modes. 497 */ 498 std::vector<FlashMode> GetSupportedFlashModes(); 499 500 /** 501 * @brief Get the supported Focus modes. 502 * @param vector of camera_focus_mode_enum_t supported exposure modes. 503 * @return Returns errCode. 504 */ 505 int32_t GetSupportedFlashModes(std::vector<FlashMode> &flashModes); 506 507 /** 508 * @brief Check whether camera has flash. 509 */ 510 bool HasFlash(); 511 512 /** 513 * @brief Check whether camera has flash. 514 * @param bool True is has flash false otherwise. 515 * @return Returns errCode. 516 */ 517 int32_t HasFlash(bool &hasFlash); 518 519 /** 520 * @brief Query whether given flash mode supported. 521 * 522 * @param camera_flash_mode_enum_t flash mode to query. 523 * @return True if supported false otherwise. 524 */ 525 bool IsFlashModeSupported(FlashMode flashMode); 526 527 /** 528 * @brief Query whether given flash mode supported. 529 * 530 * @param camera_flash_mode_enum_t flash mode to query. 531 * @param bool True if supported false otherwise. 532 * @return errCode. 533 */ 534 int32_t IsFlashModeSupported(FlashMode flashMode, bool &isSupported); 535 536 /** 537 * @brief Get the current flash mode. 538 * 539 * @return Returns current flash mode. 540 */ 541 FlashMode GetFlashMode(); 542 543 /** 544 * @brief Get the current flash mode. 545 * @param current flash mode. 546 * @return Returns errCode. 547 */ 548 int32_t GetFlashMode(FlashMode &flashMode); 549 550 /** 551 * @brief Set flash mode. 552 * 553 * @param camera_flash_mode_enum_t flash mode to be set. 554 * @return Returns errCode. 555 */ 556 int32_t SetFlashMode(FlashMode flashMode); 557 558 /** 559 * @brief Get the supported Zoom Ratio range. 560 * 561 * @return Returns vector<float> of supported Zoom ratio range. 562 */ 563 std::vector<float> GetZoomRatioRange(); 564 565 /** 566 * @brief Get the supported Zoom Ratio range. 567 * 568 * @param vector<float> of supported Zoom ratio range. 569 * @return Returns errCode. 570 */ 571 int32_t GetZoomRatioRange(std::vector<float> &zoomRatioRange); 572 573 /** 574 * @brief Get the current Zoom Ratio. 575 * 576 * @return Returns current Zoom Ratio. 577 */ 578 float GetZoomRatio(); 579 580 /** 581 * @brief Get the current Zoom Ratio. 582 * @param zoomRatio current Zoom Ratio. 583 * @return Returns errCode. 584 */ 585 int32_t GetZoomRatio(float &zoomRatio); 586 587 /** 588 * @brief Set Zoom ratio. 589 * 590 * @param Zoom ratio to be set. 591 * @return Returns errCode. 592 */ 593 int32_t SetZoomRatio(float zoomRatio); 594 595 /** 596 * @brief Set Metadata Object types. 597 * 598 * @param set of camera_face_detect_mode_t types. 599 */ 600 void SetCaptureMetadataObjectTypes(std::set<camera_face_detect_mode_t> metadataObjectTypes); 601 602 /** 603 * @brief Get whether or not commit config. 604 * 605 * @return Returns whether or not commit config. 606 */ 607 bool IsSessionCommited(); 608 609 /** 610 * @brief Get whether or not commit config. 611 * 612 * @return Returns whether or not commit config. 613 */ 614 bool IsSessionConfiged(); 615 private: 616 std::mutex changeMetaMutex_; 617 std::shared_ptr<OHOS::Camera::CameraMetadata> changedMetadata_; 618 sptr<ICaptureSession> captureSession_; 619 std::shared_ptr<SessionCallback> appCallback_; 620 sptr<ICaptureSessionCallback> captureSessionCallback_; 621 std::shared_ptr<ExposureCallback> exposureCallback_; 622 std::shared_ptr<FocusCallback> focusCallback_; 623 static const std::unordered_map<camera_focus_state_t, FocusCallback::FocusState> metaToFwFocusState_; 624 static const std::unordered_map<camera_exposure_state_t, ExposureCallback::ExposureState> metaToFwExposureState_; 625 static const std::unordered_map<camera_exposure_mode_enum_t, ExposureMode> metaToFwExposureMode_; 626 static const std::unordered_map<ExposureMode, camera_exposure_mode_enum_t> fwToMetaExposureMode_; 627 static const std::unordered_map<camera_focus_mode_enum_t, FocusMode> metaToFwFocusMode_; 628 static const std::unordered_map<FocusMode, camera_focus_mode_enum_t> fwToMetaFocusMode_; 629 static const std::unordered_map<camera_flash_mode_enum_t, FlashMode> metaToFwFlashMode_; 630 static const std::unordered_map<FlashMode, camera_flash_mode_enum_t> fwToMetaFlashMode_; 631 static const std::unordered_map<CameraVideoStabilizationMode, VideoStabilizationMode> metaToFwVideoStabModes_; 632 static const std::unordered_map<VideoStabilizationMode, CameraVideoStabilizationMode> fwToMetaVideoStabModes_; 633 634 int32_t SetCropRegion(float zoomRatio); 635 int32_t StartFocus(FocusMode focusMode); 636 int32_t UpdateSetting(std::shared_ptr<OHOS::Camera::CameraMetadata> changedMetadata); 637 void SetFrameRateRange(const std::vector<int32_t>& frameRateRange); 638 Point CoordinateTransform(Point point); 639 }; 640 } // namespace CameraStandard 641 } // namespace OHOS 642 #endif // OHOS_CAMERA_CAPTURE_SESSION_H 643