diff --git a/frameworks/native/camera/src/input/camera_input.cpp b/frameworks/native/camera/src/input/camera_input.cpp index c923e5eee85b8b307e56a10ab31dc36a7dc3928d..6e2dc89dfd87fc5a30c1433159092f0196f7f31a 100644 --- a/frameworks/native/camera/src/input/camera_input.cpp +++ b/frameworks/native/camera/src/input/camera_input.cpp @@ -153,6 +153,7 @@ void CameraInput::SetErrorCallback(std::shared_ptr errorCallback) if (errorCallback == nullptr) { MEDIA_ERR_LOG("SetErrorCallback: Unregistering error callback"); } + std::lock_guard lock(cameraInputCallbackMutex_); errorCallback_ = errorCallback; return; } @@ -169,6 +170,7 @@ sptr CameraInput::GetCameraDevice() std::shared_ptr CameraInput::GetErrorCallback() { + std::lock_guard lock(cameraInputCallbackMutex_); return errorCallback_; } diff --git a/frameworks/native/camera/src/input/camera_manager.cpp b/frameworks/native/camera/src/input/camera_manager.cpp index 9c0e2fe947e0e172689f304fb9ad17412e353142..cb5a5775fb2eb0cf9667f707da1fbb2d5940090b 100644 --- a/frameworks/native/camera/src/input/camera_manager.cpp +++ b/frameworks/native/camera/src/input/camera_manager.cpp @@ -60,7 +60,6 @@ CameraManager::~CameraManager() deathRecipient_ = nullptr; cameraSvcCallback_ = nullptr; cameraMuteSvcCallback_ = nullptr; - cameraMngrCallback_ = nullptr; for (unsigned int i = 0; i < cameraMuteListenerList.size(); i++) { if (cameraMuteListenerList[i]) { cameraMuteListenerList[i] = nullptr; @@ -589,11 +588,13 @@ void CameraManager::SetCallback(std::shared_ptr callback) if (callback == nullptr) { MEDIA_INFO_LOG("CameraManager::SetCallback(): Application unregistering the callback"); } + std::lock_guard lock(cameraMngrCallbackMutex_); cameraMngrCallback_ = callback; } std::shared_ptr CameraManager::GetApplicationCallback() { + std::lock_guard lock(cameraMngrCallbackMutex_); MEDIA_INFO_LOG("CameraManager::GetApplicationCallback callback! isExist = %{public}d", cameraMngrCallback_ != nullptr); return cameraMngrCallback_; diff --git a/frameworks/native/camera/src/output/metadata_output.cpp b/frameworks/native/camera/src/output/metadata_output.cpp index b147424732bab62b1fd8e12db0cc2c120f858b29..c32d1e0ca4c5c0f400604f8976425d1ca6dd136c 100644 --- a/frameworks/native/camera/src/output/metadata_output.cpp +++ b/frameworks/native/camera/src/output/metadata_output.cpp @@ -60,8 +60,20 @@ MetadataOutput::~MetadataOutput() } surface_ = nullptr; } - appObjectCallback_ = nullptr; - appStateCallback_ = nullptr; +} + +std::shared_ptr MetadataOutput::GetAppObjectCallback() +{ + MEDIA_DEBUG_LOG("CameraDeviceServiceCallback::GetAppObjectCallback"); + std::lock_guard lock(metadataCallbackMutex_); + return appObjectCallback_; +} + +std::shared_ptr MetadataOutput::GetAppStateCallback() +{ + MEDIA_DEBUG_LOG("CameraDeviceServiceCallback::GetAppStateCallback"); + std::lock_guard lock(metadataCallbackMutex_); + return appStateCallback_; } std::vector MetadataOutput::GetSupportedMetadataObjectTypes() @@ -107,11 +119,13 @@ void MetadataOutput::SetCapturingMetadataObjectTypes(std::vector metadataObjectCallback) { + std::lock_guard lock(metadataCallbackMutex_); appObjectCallback_ = metadataObjectCallback; } void MetadataOutput::SetCallback(std::shared_ptr metadataStateCallback) { + std::lock_guard lock(metadataCallbackMutex_); appStateCallback_ = metadataStateCallback; } @@ -163,8 +177,11 @@ int32_t MetadataOutput::Release() } surface_ = nullptr; } - appObjectCallback_ = nullptr; - appStateCallback_ = nullptr; + { + std::lock_guard lock(metadataCallbackMutex_); + appObjectCallback_ = nullptr; + appStateCallback_ = nullptr; + } CaptureOutput::Release(); return ServiceToCameraError(errCode); } @@ -220,7 +237,7 @@ int32_t MetadataObjectListener::ProcessMetadataBuffer(void* buffer, int64_t time } } } - std::shared_ptr appObjectCallback = metadata_->appObjectCallback_; + std::shared_ptr appObjectCallback = metadata_->GetAppObjectCallback(); if (!metaObjects.empty() && appObjectCallback) { appObjectCallback->OnMetadataObjectsAvailable(metaObjects); } @@ -249,7 +266,7 @@ void MetadataObjectListener::OnBufferAvailable() } int32_t ret = ProcessMetadataBuffer(buffer->GetVirAddr(), timestamp); if (ret) { - std::shared_ptr appStateCallback = metadata_->appStateCallback_; + std::shared_ptr appStateCallback = metadata_->GetAppStateCallback(); if (appStateCallback) { appStateCallback->OnError(ret); } diff --git a/frameworks/native/camera/src/output/photo_output.cpp b/frameworks/native/camera/src/output/photo_output.cpp index 47d796e833832cbc5952fa4618c6905430a94bbd..4b513c43e81f7b611ceea666e87edc5bcc0d5f4a 100644 --- a/frameworks/native/camera/src/output/photo_output.cpp +++ b/frameworks/native/camera/src/output/photo_output.cpp @@ -227,12 +227,12 @@ PhotoOutput::PhotoOutput(sptr &streamCapture) PhotoOutput::~PhotoOutput() { cameraSvcCallback_ = nullptr; - appCallback_ = nullptr; defaultCaptureSetting_ = nullptr; } void PhotoOutput::SetCallback(std::shared_ptr callback) { + std::lock_guard lock(photoCallbackMutex_); appCallback_ = callback; if (appCallback_ != nullptr) { if (cameraSvcCallback_ == nullptr) { @@ -260,6 +260,7 @@ void PhotoOutput::SetCallback(std::shared_ptr callback) std::shared_ptr PhotoOutput::GetApplicationCallback() { + std::lock_guard lock(photoCallbackMutex_); return appCallback_; } @@ -361,7 +362,10 @@ int32_t PhotoOutput::Release() MEDIA_ERR_LOG("PhotoOutput Failed to release!, errCode: %{public}d", errCode); } cameraSvcCallback_ = nullptr; - appCallback_ = nullptr; + { + std::lock_guard lock(photoCallbackMutex_); + appCallback_ = nullptr; + } defaultCaptureSetting_ = nullptr; CaptureOutput::Release(); return ServiceToCameraError(errCode); diff --git a/frameworks/native/camera/src/output/preview_output.cpp b/frameworks/native/camera/src/output/preview_output.cpp index 28fde1c88bdb3993bde5aec6904050aca74f25b0..67df2f7466a0594cc4482a693ad71a2bc552cb9a 100644 --- a/frameworks/native/camera/src/output/preview_output.cpp +++ b/frameworks/native/camera/src/output/preview_output.cpp @@ -27,7 +27,6 @@ PreviewOutput::PreviewOutput(sptr &streamRepeat) PreviewOutput::~PreviewOutput() { svcCallback_ = nullptr; - appCallback_ = nullptr; } int32_t PreviewOutput::Release() @@ -48,7 +47,10 @@ int32_t PreviewOutput::Release() MEDIA_ERR_LOG("PreviewOutput::Release() itemStream is nullptr"); } svcCallback_ = nullptr; - appCallback_ = nullptr; + { + std::lock_guard lock(previewCallbackMutex_); + appCallback_ = nullptr; + } CaptureOutput::Release(); return ServiceToCameraError(errCode); } @@ -161,6 +163,7 @@ int32_t PreviewOutput::Stop() void PreviewOutput::SetCallback(std::shared_ptr callback) { + std::lock_guard lock(previewCallbackMutex_); appCallback_ = callback; if (appCallback_ != nullptr) { if (svcCallback_ == nullptr) { @@ -193,6 +196,7 @@ void PreviewOutput::SetCallback(std::shared_ptr callback) std::shared_ptr PreviewOutput::GetApplicationCallback() { + std::lock_guard lock(previewCallbackMutex_); return appCallback_; } } // CameraStandard diff --git a/frameworks/native/camera/src/output/video_output.cpp b/frameworks/native/camera/src/output/video_output.cpp index a963113b881430987e0513936e70b4deaef5a2eb..302438bf43c5c85451297e52e580eb25b3db732d 100644 --- a/frameworks/native/camera/src/output/video_output.cpp +++ b/frameworks/native/camera/src/output/video_output.cpp @@ -29,7 +29,6 @@ VideoOutput::VideoOutput(sptr &streamRepeat) VideoOutput::~VideoOutput() { svcCallback_ = nullptr; - appCallback_ = nullptr; } class HStreamRepeatCallbackImpl : public HStreamRepeatCallbackStub { @@ -81,6 +80,7 @@ public: void VideoOutput::SetCallback(std::shared_ptr callback) { + std::lock_guard lock(videoCallbackMutex_); appCallback_ = callback; if (appCallback_ != nullptr) { if (svcCallback_ == nullptr) { @@ -208,13 +208,17 @@ int32_t VideoOutput::Release() MEDIA_ERR_LOG("Failed to release VideoOutput!, errCode: %{public}d", errCode); } svcCallback_ = nullptr; - appCallback_ = nullptr; + { + std::lock_guard lock(videoCallbackMutex_); + appCallback_ = nullptr; + } CaptureOutput::Release(); return ServiceToCameraError(errCode); } std::shared_ptr VideoOutput::GetApplicationCallback() { + std::lock_guard lock(videoCallbackMutex_); return appCallback_; } diff --git a/frameworks/native/camera/src/session/capture_session.cpp b/frameworks/native/camera/src/session/capture_session.cpp index 562ac76852e69a90c90af7602a557add89d08e76..c148b768d09c97a2ae1d24b9e717e314db6e930f 100644 --- a/frameworks/native/camera/src/session/capture_session.cpp +++ b/frameworks/native/camera/src/session/capture_session.cpp @@ -137,10 +137,7 @@ CaptureSession::~CaptureSession() inputDevice_ = nullptr; captureSession_ = nullptr; changedMetadata_ = nullptr; - appCallback_ = nullptr; captureSessionCallback_ = nullptr; - exposureCallback_ = nullptr; - focusCallback_ = nullptr; } int32_t CaptureSession::BeginConfig() @@ -347,9 +344,12 @@ int32_t CaptureSession::Release() captureSession_ = nullptr; captureSessionCallback_ = nullptr; changedMetadata_ = nullptr; - appCallback_ = nullptr; - exposureCallback_ = nullptr; - focusCallback_ = nullptr; + { + std::lock_guard lock(sessionCallbackMutex_); + appCallback_ = nullptr; + exposureCallback_ = nullptr; + focusCallback_ = nullptr; + } return ServiceToCameraError(errCode); } @@ -359,7 +359,7 @@ void CaptureSession::SetCallback(std::shared_ptr callback) MEDIA_ERR_LOG("CaptureSession::SetCallback: Unregistering application callback!"); } int32_t errorCode = CAMERA_OK; - + std::lock_guard lock(sessionCallbackMutex_); appCallback_ = callback; if (appCallback_ != nullptr && captureSession_ != nullptr) { if (captureSessionCallback_ == nullptr) { @@ -382,6 +382,7 @@ void CaptureSession::SetCallback(std::shared_ptr callback) std::shared_ptr CaptureSession::GetApplicationCallback() { + std::lock_guard lock(sessionCallbackMutex_); return appCallback_; } @@ -998,6 +999,7 @@ int32_t CaptureSession::GetExposureValue(int32_t &exposureValue) void CaptureSession::SetExposureCallback(std::shared_ptr exposureCallback) { + std::lock_guard lock(sessionCallbackMutex_); exposureCallback_ = exposureCallback; } @@ -1014,6 +1016,7 @@ void CaptureSession::ProcessAutoExposureUpdates(const std::shared_ptr lock(sessionCallbackMutex_); if (exposureCallback_ != nullptr) { auto itr = metaToFwExposureState_.find(static_cast(item.data.u8[0])); if (itr != metaToFwExposureState_.end()) { @@ -1080,6 +1083,7 @@ int32_t CaptureSession::GetSupportedFocusModes(std::vector &supported void CaptureSession::SetFocusCallback(std::shared_ptr focusCallback) { + std::lock_guard lock(sessionCallbackMutex_); focusCallback_ = focusCallback; return; } @@ -1411,6 +1415,7 @@ void CaptureSession::ProcessAutoFocusUpdates(const std::shared_ptr lock(sessionCallbackMutex_); if (focusCallback_ != nullptr) { auto itr = metaToFwFocusState_.find(static_cast(item.data.u8[0])); if (itr != metaToFwFocusState_.end()) { diff --git a/interfaces/inner_api/native/camera/include/input/camera_input.h b/interfaces/inner_api/native/camera/include/input/camera_input.h index d2a1f8dbf64ce5d3d81db1d00c3c345958fadbf0..b2709bcac90b8303c955769545a6cf1190a5d6b7 100644 --- a/interfaces/inner_api/native/camera/include/input/camera_input.h +++ b/interfaces/inner_api/native/camera/include/input/camera_input.h @@ -292,6 +292,7 @@ public: int32_t SetCameraSettings(std::string setting); private: + std::mutex cameraInputCallbackMutex_; sptr cameraObj_; sptr deviceObj_; std::shared_ptr errorCallback_; diff --git a/interfaces/inner_api/native/camera/include/input/camera_manager.h b/interfaces/inner_api/native/camera/include/input/camera_manager.h index a48153a48dba966a2cb1ee05b72e8cf0761583c1..1dc31b44a9d833c522ef108cbbd9e42e0c9c28ed 100644 --- a/interfaces/inner_api/native/camera/include/input/camera_manager.h +++ b/interfaces/inner_api/native/camera/include/input/camera_manager.h @@ -422,6 +422,7 @@ private: static const std::unordered_map fwToMetaCameraFormat_; std::mutex mutex_; + std::mutex cameraMngrCallbackMutex_; int CreateCameraDevice(std::string cameraId, sptr *pICameraDeviceService); camera_format_t GetCameraMetadataFormat(CameraFormat format); sptr serviceProxy_; diff --git a/interfaces/inner_api/native/camera/include/output/metadata_output.h b/interfaces/inner_api/native/camera/include/output/metadata_output.h index 1648d30d7fbc87dcb798acd723aeab0a2fed4f87..2cf779c5145afb029c976cc1467cf5a2e787b2a7 100644 --- a/interfaces/inner_api/native/camera/include/output/metadata_output.h +++ b/interfaces/inner_api/native/camera/include/output/metadata_output.h @@ -123,8 +123,12 @@ public: */ int32_t Release() override; + std::shared_ptr GetAppObjectCallback(); + std::shared_ptr GetAppStateCallback(); + friend class MetadataObjectListener; private: + std::mutex metadataCallbackMutex_; sptr surface_; std::shared_ptr appObjectCallback_; std::shared_ptr appStateCallback_; diff --git a/interfaces/inner_api/native/camera/include/output/photo_output.h b/interfaces/inner_api/native/camera/include/output/photo_output.h index 8c31deb4a65dcf9007c931f1f8ee1bcd89bca6fa..d5a2368d50596d549eb2801459bb4321be1bb44b 100644 --- a/interfaces/inner_api/native/camera/include/output/photo_output.h +++ b/interfaces/inner_api/native/camera/include/output/photo_output.h @@ -256,6 +256,7 @@ public: std::shared_ptr GetDefaultCaptureSetting(); private: + std::mutex photoCallbackMutex_; std::shared_ptr appCallback_; sptr cameraSvcCallback_; std::shared_ptr defaultCaptureSetting_; diff --git a/interfaces/inner_api/native/camera/include/output/preview_output.h b/interfaces/inner_api/native/camera/include/output/preview_output.h index e4d760b5024aabcefe14c24f557057d785a96809..fdc9f14120162faea1d6c88416d6e25a957fd4a1 100644 --- a/interfaces/inner_api/native/camera/include/output/preview_output.h +++ b/interfaces/inner_api/native/camera/include/output/preview_output.h @@ -89,6 +89,7 @@ public: std::shared_ptr GetApplicationCallback(); private: + std::mutex previewCallbackMutex_; std::shared_ptr appCallback_; sptr svcCallback_; }; diff --git a/interfaces/inner_api/native/camera/include/output/video_output.h b/interfaces/inner_api/native/camera/include/output/video_output.h index 5d409c03288a974cd94cbbf86b7603fd86cba613..c7642dfc850cd85e11d98fb97ffb622887c69ef8 100644 --- a/interfaces/inner_api/native/camera/include/output/video_output.h +++ b/interfaces/inner_api/native/camera/include/output/video_output.h @@ -112,6 +112,7 @@ public: void SetFrameRateRange(int32_t minFrameRate, int32_t maxFrameRate); private: + std::mutex videoCallbackMutex_; std::shared_ptr appCallback_; sptr svcCallback_; std::vector videoFrameRateRange_; diff --git a/interfaces/inner_api/native/camera/include/session/capture_session.h b/interfaces/inner_api/native/camera/include/session/capture_session.h index e8d4b7919a5247fd6aaa85baef4ae98b87693efa..4af3ccdee9b0cfdf276f52d31a32e213bbba77f3 100644 --- a/interfaces/inner_api/native/camera/include/session/capture_session.h +++ b/interfaces/inner_api/native/camera/include/session/capture_session.h @@ -613,6 +613,7 @@ public: */ bool IsSessionConfiged(); private: + std::mutex sessionCallbackMutex_; std::mutex changeMetaMutex_; std::shared_ptr changedMetadata_; sptr captureSession_; diff --git a/services/camera_service/include/hcapture_session.h b/services/camera_service/include/hcapture_session.h index c990ad2762ea6c19e2e763bd3b384c8514e63fc0..7451eb4dee5e48cccaa67994f87625d028ed8bf6 100644 --- a/services/camera_service/include/hcapture_session.h +++ b/services/camera_service/include/hcapture_session.h @@ -115,6 +115,7 @@ private: sptr sessionCallback_; int32_t streamId_ = STREAMID_BEGIN; std::mutex streamsLock_; + std::mutex sessionCbMutex_; pid_t pid_; int32_t uid_; uint32_t callerToken_; diff --git a/services/camera_service/src/hcamera_device.cpp b/services/camera_service/src/hcamera_device.cpp index 3be039aae15b48d9c517857ff30d42972ab9b165..fdce00f79cb65de2008ccb962a7edb209b1a9b56 100644 --- a/services/camera_service/src/hcamera_device.cpp +++ b/services/camera_service/src/hcamera_device.cpp @@ -42,8 +42,6 @@ HCameraDevice::~HCameraDevice() cameraHostManager_ = nullptr; } deviceHDICallback_ = nullptr; - std::lock_guard lock(deviceSvcCbMutex_); - deviceSvcCallback_ = nullptr; } std::string HCameraDevice::GetCameraId() @@ -493,9 +491,11 @@ int32_t HCameraDevice::OnCameraStatus(const std::string& cameraId, CameraStatus int32_t HCameraDevice::OnResult(const uint64_t timestamp, const std::shared_ptr &result) { - std::lock_guard lock(deviceSvcCbMutex_); - if (deviceSvcCallback_ != nullptr) { - deviceSvcCallback_->OnResult(timestamp, result); + { + std::lock_guard lock(deviceSvcCbMutex_); + if (deviceSvcCallback_ != nullptr) { + deviceSvcCallback_->OnResult(timestamp, result); + } } camera_metadata_item_t item; common_metadata_header_t* metadata = result->get(); diff --git a/services/camera_service/src/hcapture_session.cpp b/services/camera_service/src/hcapture_session.cpp index d97948c0b8ed22496f6d59ac9c1eb50c7319c548..267fb315b21579b35cade8d83d30fc372be7da06 100644 --- a/services/camera_service/src/hcapture_session.cpp +++ b/services/camera_service/src/hcapture_session.cpp @@ -745,7 +745,10 @@ int32_t HCaptureSession::Release(pid_t pid) } tempStreams_.clear(); ClearCaptureSession(pid); - sessionCallback_ = nullptr; + { + std::lock_guard lock(sessionCbMutex_); + sessionCallback_ = nullptr; + } cameraHostManager_ = nullptr; return CAMERA_OK; } @@ -802,7 +805,7 @@ int32_t HCaptureSession::SetCallback(sptr &callback) MEDIA_ERR_LOG("HCaptureSession::SetCallback callback is null"); return CAMERA_INVALID_ARG; } - + std::lock_guard lock(sessionCbMutex_); sessionCallback_ = callback; return CAMERA_OK; }