diff --git a/common/BUILD.gn b/common/BUILD.gn index e3ec2a222bd2fa0e271a3a808e79ecdfb81c1ba2..880aafd0122a51e4634a28bbfcb6eb878b3e50fd 100644 --- a/common/BUILD.gn +++ b/common/BUILD.gn @@ -32,6 +32,7 @@ ohos_shared_library("distributed_camera_utils") { sources = [ "src/utils/data_buffer.cpp", + "src/utils/dcamera_sa_process_state.cpp", "src/utils/dcamera_utils_tools.cpp", ] diff --git a/common/include/utils/dcamera_sa_process_state.h b/common/include/utils/dcamera_sa_process_state.h new file mode 100644 index 0000000000000000000000000000000000000000..6d479d007121b36c3b5225e36d020333949bae56 --- /dev/null +++ b/common/include/utils/dcamera_sa_process_state.h @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef DCAMERA_SA_PROCESS_STATE_H +#define DCAMERA_SA_PROCESS_STATE_H + +namespace OHOS { +namespace DistributedHardware { +void SetSinkProcessExit(); +void SetSourceProcessExit(); +} // namespace DistributedHardware +} // namespace OHOS +#endif // DCAMERA_SA_PROCESS_STATE_H diff --git a/common/src/utils/dcamera_sa_process_state.cpp b/common/src/utils/dcamera_sa_process_state.cpp new file mode 100644 index 0000000000000000000000000000000000000000..00df00182be7efc3d1c4b104d445fb83d1c142e5 --- /dev/null +++ b/common/src/utils/dcamera_sa_process_state.cpp @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "dcamera_sa_process_state.h" + +#include + +#include "distributed_hardware_log.h" + +namespace OHOS { +namespace DistributedHardware { +typedef enum { + DCAMERA_SA_EXIT_STATE_START = 0, + DCAMERA_SA_EXIT_STATE_STOP = 1 +} DCameraSAState; + +DCameraSAState g_sinkSAState = DCAMERA_SA_EXIT_STATE_START; +DCameraSAState g_sourceSAState = DCAMERA_SA_EXIT_STATE_START; +std::mutex g_saProcessState; + +void SetSinkProcessExit() +{ + DHLOGI("set sink process exit."); + std::lock_guard autoLock(g_saProcessState); + g_sinkSAState = DCAMERA_SA_EXIT_STATE_STOP; + DHLOGI("g_sourceSAState = %d g_sinkSAState = %d", g_sourceSAState, g_sinkSAState); + if (g_sourceSAState == DCAMERA_SA_EXIT_STATE_START || g_sinkSAState == DCAMERA_SA_EXIT_STATE_START) { + return; + } + DHLOGI("exit sa process success."); + exit(0); +} + +void SetSourceProcessExit() +{ + DHLOGI("set sources process exit."); + std::lock_guard autoLock(g_saProcessState); + g_sourceSAState = DCAMERA_SA_EXIT_STATE_STOP; + DHLOGI("g_sourceSAState = %d g_sinkSAState = %d", g_sourceSAState, g_sinkSAState); + if (g_sourceSAState == DCAMERA_SA_EXIT_STATE_START || g_sinkSAState == DCAMERA_SA_EXIT_STATE_START) { + return; + } + DHLOGI("exit sa process success."); + exit(0); +} +} // namespace DistributedHardware +} // namespace OHOS \ No newline at end of file diff --git a/services/cameraservice/sinkservice/src/distributedcamera/distributed_camera_sink_service.cpp b/services/cameraservice/sinkservice/src/distributedcamera/distributed_camera_sink_service.cpp index a968898c91be6ab10c92be2c393b8f8943212007..41ea35c4f6688d4c5a64ebdd15add4352fd14261 100644 --- a/services/cameraservice/sinkservice/src/distributedcamera/distributed_camera_sink_service.cpp +++ b/services/cameraservice/sinkservice/src/distributedcamera/distributed_camera_sink_service.cpp @@ -27,6 +27,7 @@ #include "dcamera_sink_service_ipc.h" #include "distributed_camera_errno.h" #include "distributed_hardware_log.h" +#include "dcamera_sa_process_state.h" namespace OHOS { namespace DistributedHardware { @@ -116,8 +117,8 @@ int32_t DistributedCameraSinkService::ReleaseSink() } } camerasMap_.clear(); - DHLOGI("exit sink sa process."); - exit(0); + DHLOGI("check sink sa state."); + SetSinkProcessExit(); return DCAMERA_OK; } diff --git a/services/cameraservice/sourceservice/include/distributedcamera/dcamera_service_state_listener.h b/services/cameraservice/sourceservice/include/distributedcamera/dcamera_service_state_listener.h index 967bd17a3122957e488d62f8cc86509ee168e4d9..be75ad8698448fdac321ff0f33bf81797547da9c 100644 --- a/services/cameraservice/sourceservice/include/distributedcamera/dcamera_service_state_listener.h +++ b/services/cameraservice/sourceservice/include/distributedcamera/dcamera_service_state_listener.h @@ -16,6 +16,8 @@ #ifndef OHOS_DCAMERA_SERVICE_STATE_LISTENER_H #define OHOS_DCAMERA_SERVICE_STATE_LISTENER_H +#include + #include "icamera_state_listener.h" #include "idcamera_source_callback.h" @@ -23,16 +25,17 @@ namespace OHOS { namespace DistributedHardware { class DCameraServiceStateListener : public ICameraStateListener { public: - explicit DCameraServiceStateListener(sptr callback); + explicit DCameraServiceStateListener(); ~DCameraServiceStateListener(); int32_t OnRegisterNotify(const std::string& devId, const std::string& dhId, const std::string& reqId, int32_t status, std::string& data) override; int32_t OnUnregisterNotify(const std::string& devId, const std::string& dhId, const std::string& reqId, int32_t status, std::string& data) override; - + void SetCallback(sptr callback) override; private: sptr callbackProxy_; + std::mutex proxyMutex_; }; } // namespace DistributedHardware } // namespace OHOS diff --git a/services/cameraservice/sourceservice/include/distributedcameramgr/dcamerainterface/icamera_state_listener.h b/services/cameraservice/sourceservice/include/distributedcameramgr/dcamerainterface/icamera_state_listener.h index 9ed09406f5496fec9b33bda0917dd702b69291b0..08c8ceaa3560c893727d246bf4b2f862a6f098cf 100644 --- a/services/cameraservice/sourceservice/include/distributedcameramgr/dcamerainterface/icamera_state_listener.h +++ b/services/cameraservice/sourceservice/include/distributedcameramgr/dcamerainterface/icamera_state_listener.h @@ -19,6 +19,8 @@ #include #include +#include "idcamera_source_callback.h" + namespace OHOS { namespace DistributedHardware { class ICameraStateListener { @@ -28,6 +30,7 @@ public: int32_t status, std::string& data) = 0; virtual int32_t OnUnregisterNotify(const std::string& devId, const std::string& dhId, const std::string& reqId, int32_t status, std::string& data) = 0; + virtual void SetCallback(sptr callback) = 0; }; } // namespace DistributedHardware } // namespace OHOS diff --git a/services/cameraservice/sourceservice/src/distributedcamera/dcamera_service_state_listener.cpp b/services/cameraservice/sourceservice/src/distributedcamera/dcamera_service_state_listener.cpp index 49dc8f9940703bc263be4ceeb686ca7d2f470a30..36cdd886b7761ec976d8e1b8e2c96cc11aae6d15 100644 --- a/services/cameraservice/sourceservice/src/distributedcamera/dcamera_service_state_listener.cpp +++ b/services/cameraservice/sourceservice/src/distributedcamera/dcamera_service_state_listener.cpp @@ -25,8 +25,7 @@ namespace OHOS { namespace DistributedHardware { -DCameraServiceStateListener::DCameraServiceStateListener(sptr callback) - : callbackProxy_(callback) +DCameraServiceStateListener::DCameraServiceStateListener() { DHLOGI("DCameraServiceStateListener Create"); } @@ -37,11 +36,19 @@ DCameraServiceStateListener::~DCameraServiceStateListener() callbackProxy_ = nullptr; } +void DCameraServiceStateListener::SetCallback(sptr callback) +{ + DHLOGI("DCameraServiceStateListener SetCallback"); + std::lock_guard autoLock(proxyMutex_); + callbackProxy_ = callback; +} + int32_t DCameraServiceStateListener::OnRegisterNotify(const std::string& devId, const std::string& dhId, const std::string& reqId, int32_t status, std::string& data) { DHLOGI("DCameraServiceStateListener OnRegisterNotify devId: %s, dhId: %s, status: %d", GetAnonyString(devId).c_str(), GetAnonyString(dhId).c_str(), status); + std::lock_guard autoLock(proxyMutex_); if (callbackProxy_ == nullptr) { DHLOGE("DCameraServiceStateListener OnRegisterNotify callbackProxy_ is nullptr"); return DCAMERA_BAD_VALUE; @@ -73,6 +80,7 @@ int32_t DCameraServiceStateListener::OnUnregisterNotify(const std::string& devId { DHLOGI("DCameraServiceStateListener OnUnregisterNotify devId: %s, dhId: %s, status: %d", GetAnonyString(devId).c_str(), GetAnonyString(dhId).c_str(), status); + std::lock_guard autoLock(proxyMutex_); if (callbackProxy_ == nullptr) { DHLOGE("DCameraServiceStateListener OnUnregisterNotify callbackProxy_ is nullptr"); return DCAMERA_BAD_VALUE; diff --git a/services/cameraservice/sourceservice/src/distributedcamera/distributed_camera_source_service.cpp b/services/cameraservice/sourceservice/src/distributedcamera/distributed_camera_source_service.cpp index 3da2ed457f7bb489ff758a3c352b600d4af2de32..2aceb73940d33355b99b73a6c5e92125f12e03e1 100644 --- a/services/cameraservice/sourceservice/src/distributedcamera/distributed_camera_source_service.cpp +++ b/services/cameraservice/sourceservice/src/distributedcamera/distributed_camera_source_service.cpp @@ -27,6 +27,7 @@ #include "dcamera_source_service_ipc.h" #include "distributed_camera_errno.h" #include "distributed_hardware_log.h" +#include "dcamera_sa_process_state.h" namespace OHOS { namespace DistributedHardware { @@ -67,6 +68,7 @@ bool DistributedCameraSourceService::Init() } registerToService_ = true; } + listener_ = std::make_shared(); DHLOGI("DistributedCameraSourceService init success"); return true; } @@ -76,6 +78,7 @@ void DistributedCameraSourceService::OnStop() DHLOGI("DistributedCameraSourceService OnStop service"); state_ = DCameraServiceState::DCAMERA_SRV_STATE_NOT_START; registerToService_ = false; + listener_ = nullptr; DCameraSourceServiceIpc::GetInstance().UnInit(); } @@ -89,7 +92,7 @@ int32_t DistributedCameraSourceService::InitSource(const std::string& params, return ret; } sourceVer_ = params; - listener_ = std::make_shared(callback); + listener_->SetCallback(callback); return DCAMERA_OK; } @@ -101,9 +104,8 @@ int32_t DistributedCameraSourceService::ReleaseSource() DHLOGE("DistributedCameraSourceService ReleaseSource UnLoadHDF failed, ret: %d", ret); return ret; } - listener_ = nullptr; - DHLOGI("exit source sa process."); - exit(0); + DHLOGI("check source sa state."); + SetSourceProcessExit(); return DCAMERA_OK; } diff --git a/services/cameraservice/sourceservice/test/unittest/common/distributedcameramgr/mock_dcamera_source_state_listener.h b/services/cameraservice/sourceservice/test/unittest/common/distributedcameramgr/mock_dcamera_source_state_listener.h index 958b40cc7a7549be63499a72171bda493feb22d4..593cf5a2e16a7d9b35409550b3ac7c38cce0c713 100644 --- a/services/cameraservice/sourceservice/test/unittest/common/distributedcameramgr/mock_dcamera_source_state_listener.h +++ b/services/cameraservice/sourceservice/test/unittest/common/distributedcameramgr/mock_dcamera_source_state_listener.h @@ -38,6 +38,10 @@ public: { return DCAMERA_OK; } + + void SetCallback(sptr callback) + { + } }; } // namespace DistributedHardware } // namespace OHOS