diff --git a/interfaces/inner_kits/native_cpp/camera_sink/BUILD.gn b/interfaces/inner_kits/native_cpp/camera_sink/BUILD.gn index ea37996..33d359b 100644 --- a/interfaces/inner_kits/native_cpp/camera_sink/BUILD.gn +++ b/interfaces/inner_kits/native_cpp/camera_sink/BUILD.gn @@ -33,6 +33,7 @@ ohos_shared_library("distributed_camera_sink_sdk") { sources = [ "src/dcamera_sink_handler.cpp", "src/dcamera_sink_handler_ipc.cpp", + "src/dcamera_sink_load_callback.cpp", "src/distributed_camera_sink_proxy.cpp", ] diff --git a/interfaces/inner_kits/native_cpp/camera_sink/include/dcamera_sink_handler.h b/interfaces/inner_kits/native_cpp/camera_sink/include/dcamera_sink_handler.h index 52b95b6..e982972 100644 --- a/interfaces/inner_kits/native_cpp/camera_sink/include/dcamera_sink_handler.h +++ b/interfaces/inner_kits/native_cpp/camera_sink/include/dcamera_sink_handler.h @@ -16,6 +16,9 @@ #ifndef OHOS_DCAMERA_SINK_HANDLER_H #define OHOS_DCAMERA_SINK_HANDLER_H +#include +#include + #include "idistributed_hardware_sink.h" #include "single_instance.h" @@ -28,10 +31,19 @@ public: int32_t ReleaseSink() override; int32_t SubscribeLocalHardware(const std::string& dhId, const std::string& parameters) override; int32_t UnsubscribeLocalHardware(const std::string& dhId) override; - + void FinishStartSA(const std::string ¶ms); + void FinishStartSAFailed(int32_t systemAbilityId); private: + typedef enum { + DCAMERA_SA_STATE_STOP = 0, + DCAMERA_SA_STATE_START = 1, + } DCameraSAState; DCameraSinkHandler() = default; ~DCameraSinkHandler(); +private: + std::condition_variable producerCon_; + std::mutex producerMutex_; + DCameraSAState state_ = DCAMERA_SA_STATE_STOP; }; #ifdef __cplusplus diff --git a/interfaces/inner_kits/native_cpp/camera_sink/include/dcamera_sink_load_callback.h b/interfaces/inner_kits/native_cpp/camera_sink/include/dcamera_sink_load_callback.h new file mode 100644 index 0000000..8230e09 --- /dev/null +++ b/interfaces/inner_kits/native_cpp/camera_sink/include/dcamera_sink_load_callback.h @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2022 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 OHOS_DCAMERA_SINk_LOAD_CALLBACK_H +#define OHOS_DCAMERA_SINk_LOAD_CALLBACK_H + +#include "system_ability_load_callback_stub.h" + +namespace OHOS { +namespace DistributedHardware { +class DCameraSinkLoadCallback : public SystemAbilityLoadCallbackStub { +public: + + explicit DCameraSinkLoadCallback(const std::string params); + void OnLoadSystemAbilitySuccess(int32_t systemAbilityId, const sptr& remoteObject); + void OnLoadSystemAbilityFail(int32_t systemAbilityId); +private: + std::string params; +}; +} +} +#endif \ No newline at end of file diff --git a/interfaces/inner_kits/native_cpp/camera_sink/src/dcamera_sink_handler.cpp b/interfaces/inner_kits/native_cpp/camera_sink/src/dcamera_sink_handler.cpp index 4ddea60..00a849e 100644 --- a/interfaces/inner_kits/native_cpp/camera_sink/src/dcamera_sink_handler.cpp +++ b/interfaces/inner_kits/native_cpp/camera_sink/src/dcamera_sink_handler.cpp @@ -14,11 +14,15 @@ */ #include "dcamera_sink_handler.h" +#include "dcamera_sink_load_callback.h" #include "anonymous_string.h" #include "dcamera_sink_handler_ipc.h" +#include "distributed_camera_constants.h" #include "distributed_camera_errno.h" #include "distributed_hardware_log.h" +#include "if_system_ability_manager.h" +#include "iservice_registry.h" namespace OHOS { namespace DistributedHardware { @@ -32,13 +36,50 @@ DCameraSinkHandler::~DCameraSinkHandler() int32_t DCameraSinkHandler::InitSink(const std::string& params) { DHLOGI("DCameraSinkHandler::InitSink"); + sptr loadCallback = new DCameraSinkLoadCallback(params); + sptr sm = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (sm == nullptr) { + DHLOGE("GetSourceLocalDHMS GetSystemAbilityManager failed"); + return DCAMERA_INIT_ERR; + } + int32_t ret = sm->LoadSystemAbility(DISTRIBUTED_HARDWARE_CAMERA_SINK_SA_ID, loadCallback); + if (ret != DCAMERA_OK) { + DHLOGE("systemAbilityId: %d load filed,result code: %d.", DISTRIBUTED_HARDWARE_CAMERA_SINK_SA_ID, ret); + return DCAMERA_INIT_ERR; + } + uint32_t interval = 1; + std::unique_lock lock(producerMutex_); + producerCon_.wait_for(lock, std::chrono::minutes(interval), [this] { + return (this->state_ == DCAMERA_SA_STATE_START); + }); + if (state_ == DCAMERA_SA_STATE_STOP) { + DHLOGE("SinkSA Start failed!"); + return DCAMERA_INIT_ERR; + } + DHLOGI("DCameraSinkHandler InitSink end, result: %d", ret); + return DCAMERA_OK; +} + +void DCameraSinkHandler::FinishStartSA(const std::string ¶ms) +{ DCameraSinkHandlerIpc::GetInstance().Init(); sptr dCameraSinkSrv = DCameraSinkHandlerIpc::GetInstance().GetSinkLocalDHMS(); if (dCameraSinkSrv == nullptr) { DHLOGE("DCameraSinkHandler::InitSink get Service failed"); - return DCAMERA_INIT_ERR; + return; } - return dCameraSinkSrv->InitSink(params); + dCameraSinkSrv->InitSink(params); + std::unique_lock lock(producerMutex_); + state_ = DCAMERA_SA_STATE_START; + producerCon_.notify_one(); +} + +void DCameraSinkHandler::FinishStartSAFailed(int32_t systemAbilityId) +{ + DHLOGI("SinkSA Start failed, systemAbilityId: %d.", systemAbilityId); + std::unique_lock lock(producerMutex_); + state_ = DCAMERA_SA_STATE_STOP; + producerCon_.notify_one(); } int32_t DCameraSinkHandler::ReleaseSink() diff --git a/interfaces/inner_kits/native_cpp/camera_sink/src/dcamera_sink_load_callback.cpp b/interfaces/inner_kits/native_cpp/camera_sink/src/dcamera_sink_load_callback.cpp new file mode 100644 index 0000000..998af41 --- /dev/null +++ b/interfaces/inner_kits/native_cpp/camera_sink/src/dcamera_sink_load_callback.cpp @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2022 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_sink_load_callback.h" +#include "dcamera_sink_handler.h" +#include "distributed_hardware_log.h" +#include "distributed_camera_constants.h" + +namespace OHOS { +namespace DistributedHardware { +DCameraSinkLoadCallback::DCameraSinkLoadCallback(const std::string params) : params(params) {} +void DCameraSinkLoadCallback::OnLoadSystemAbilitySuccess(int32_t systemAbilityId, + const sptr& remoteObject) +{ + DHLOGI("OnLoadSystemAbilitySuccess start systemAbilityId: %d.", systemAbilityId); + if (systemAbilityId != DISTRIBUTED_HARDWARE_CAMERA_SINK_SA_ID) { + DHLOGE("start aystemabilityId is not sinkSAId!"); + return; + } + DHLOGE("OnLoadSystemAbilitySuccess systemAbilityId: %d, IRmoteObject result: %s", + systemAbilityId, (remoteObject != nullptr) ? "true" : "false"); + if (remoteObject == nullptr) { + DHLOGE("remoteObject is null."); + return; + } + DCameraSinkHandler::GetInstance().FinishStartSA(params); +} + +void DCameraSinkLoadCallback::OnLoadSystemAbilityFail(int32_t systemAbilityId) +{ + DHLOGE("OnLoadSystemAbilityFail systemAbilityId: %d.", systemAbilityId); + if (systemAbilityId != DISTRIBUTED_HARDWARE_CAMERA_SINK_SA_ID) { + DHLOGE("start aystemabilityId is not sinkSAId!"); + return; + } + DCameraSinkHandler::GetInstance().FinishStartSAFailed(systemAbilityId); +} +} +} \ No newline at end of file diff --git a/interfaces/inner_kits/native_cpp/camera_source/BUILD.gn b/interfaces/inner_kits/native_cpp/camera_source/BUILD.gn index 50cc17a..011442d 100644 --- a/interfaces/inner_kits/native_cpp/camera_source/BUILD.gn +++ b/interfaces/inner_kits/native_cpp/camera_source/BUILD.gn @@ -36,6 +36,7 @@ ohos_shared_library("distributed_camera_source_sdk") { "src/callback/dcamera_source_callback_stub.cpp", "src/dcamera_source_handler.cpp", "src/dcamera_source_handler_ipc.cpp", + "src/dcamera_source_load_callback.cpp", "src/distributed_camera_source_proxy.cpp", ] diff --git a/interfaces/inner_kits/native_cpp/camera_source/include/dcamera_source_handler.h b/interfaces/inner_kits/native_cpp/camera_source/include/dcamera_source_handler.h index a1f47f6..0278578 100644 --- a/interfaces/inner_kits/native_cpp/camera_source/include/dcamera_source_handler.h +++ b/interfaces/inner_kits/native_cpp/camera_source/include/dcamera_source_handler.h @@ -17,6 +17,7 @@ #define OHOS_DCAMERA_SOURCE_HANDLER_H #include +#include #include "iremote_proxy.h" #include "iremote_broker.h" @@ -39,14 +40,22 @@ public: std::shared_ptr callback) override; int32_t ConfigDistributedHardware(const std::string& devId, const std::string& dhId, const std::string& key, const std::string& value) override; - + void FinishStartSA(const std::string ¶ms); + void FinishStartSAFailed(int32_t systemAbilityId); private: + typedef enum { + DCAMERA_SA_STATE_STOP = 0, + DCAMERA_SA_STATE_START = 1, + } DCameraSAState; DCameraSourceHandler() = default; ~DCameraSourceHandler(); private: std::mutex optLock_; sptr callback_; + std::condition_variable producerCon_; + std::mutex producerMutex_; + DCameraSAState state_ = DCAMERA_SA_STATE_STOP; }; #ifdef __cplusplus diff --git a/interfaces/inner_kits/native_cpp/camera_source/include/dcamera_source_load_callback.h b/interfaces/inner_kits/native_cpp/camera_source/include/dcamera_source_load_callback.h new file mode 100644 index 0000000..05f4d34 --- /dev/null +++ b/interfaces/inner_kits/native_cpp/camera_source/include/dcamera_source_load_callback.h @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2022 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 OHOS_DCAMERA_SOURCE_LOAD_CALLBACK_H +#define OHOS_DCAMERA_SOURCE_LOAD_CALLBACK_H + +#include "system_ability_load_callback_stub.h" + +namespace OHOS { +namespace DistributedHardware { +class DCameraSourceLoadCallback : public SystemAbilityLoadCallbackStub { +public: + + explicit DCameraSourceLoadCallback(const std::string params); + void OnLoadSystemAbilitySuccess(int32_t systemAbilityId, const sptr& remoteObject); + void OnLoadSystemAbilityFail(int32_t systemAbilityId); +private: + std::string params; +}; +} +} +#endif \ No newline at end of file diff --git a/interfaces/inner_kits/native_cpp/camera_source/src/dcamera_source_handler.cpp b/interfaces/inner_kits/native_cpp/camera_source/src/dcamera_source_handler.cpp index ddee107..244683c 100644 --- a/interfaces/inner_kits/native_cpp/camera_source/src/dcamera_source_handler.cpp +++ b/interfaces/inner_kits/native_cpp/camera_source/src/dcamera_source_handler.cpp @@ -15,10 +15,14 @@ #include "dcamera_source_handler.h" #include "dcamera_source_callback.h" +#include "dcamera_source_load_callback.h" +#include "if_system_ability_manager.h" +#include "iservice_registry.h" #include "anonymous_string.h" #include "dcamera_source_handler_ipc.h" #include "dh_utils_tool.h" +#include "distributed_camera_constants.h" #include "distributed_camera_errno.h" #include "distributed_hardware_log.h" @@ -34,21 +38,56 @@ DCameraSourceHandler::~DCameraSourceHandler() int32_t DCameraSourceHandler::InitSource(const std::string& params) { DHLOGI("DCameraSourceHandler InitSource Start"); + sptr loadCallback = new DCameraSourceLoadCallback(params); + sptr sm = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (sm == nullptr) { + DHLOGE("GetSourceLocalDHMS GetSystemAbilityManager failed"); + return DCAMERA_INIT_ERR; + } + int32_t ret = sm->LoadSystemAbility(DISTRIBUTED_HARDWARE_CAMERA_SOURCE_SA_ID, loadCallback); + if (ret != DCAMERA_OK) { + DHLOGE("systemAbilityId: %d load filed,result code: %d.", DISTRIBUTED_HARDWARE_CAMERA_SINK_SA_ID, ret); + return DCAMERA_INIT_ERR; + } + uint32_t interval = 1; + std::unique_lock lock(producerMutex_); + producerCon_.wait_for(lock, std::chrono::minutes(interval), [this] { + return (this->state_ == DCAMERA_SA_STATE_START); + }); + if (state_ == DCAMERA_SA_STATE_STOP) { + DHLOGE("SourceSA Start failed!"); + return DCAMERA_INIT_ERR; + } + DHLOGI("DCameraSourceHandler InitSource end, result: %d", ret); + return DCAMERA_OK; +} + +void DCameraSourceHandler::FinishStartSA(const std::string ¶ms) +{ DCameraSourceHandlerIpc::GetInstance().Init(); sptr dCameraSourceSrv = DCameraSourceHandlerIpc::GetInstance().GetSourceLocalDHMS(); if (dCameraSourceSrv == nullptr) { DHLOGE("DCameraSourceHandler InitSource get Service failed"); - return DCAMERA_INIT_ERR; + return; } callback_ = new DCameraSourceCallback(); if (callback_ == nullptr) { DHLOGE("DCameraSourceHandler InitSource init callback failed"); - return DCAMERA_INIT_ERR; + return; } - int32_t ret = dCameraSourceSrv->InitSource(params, callback_); - DHLOGI("DCameraSourceHandler InitSource end, ret: %d", ret); - return ret; + dCameraSourceSrv->InitSource(params, callback_); + std::unique_lock lock(producerMutex_); + state_ = DCAMERA_SA_STATE_START; + producerCon_.notify_one(); +} + +void DCameraSourceHandler::FinishStartSAFailed(int32_t systemAbilityId) +{ + DHLOGE("SourceSA Start failed, systemAbilityId: %d.", systemAbilityId); + std::unique_lock lock(producerMutex_); + state_ = DCAMERA_SA_STATE_STOP; + producerCon_.notify_one(); } int32_t DCameraSourceHandler::ReleaseSource() diff --git a/interfaces/inner_kits/native_cpp/camera_source/src/dcamera_source_load_callback.cpp b/interfaces/inner_kits/native_cpp/camera_source/src/dcamera_source_load_callback.cpp new file mode 100644 index 0000000..fe344ce --- /dev/null +++ b/interfaces/inner_kits/native_cpp/camera_source/src/dcamera_source_load_callback.cpp @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2022 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_source_load_callback.h" +#include "dcamera_source_handler.h" +#include "distributed_hardware_log.h" +#include "distributed_camera_constants.h" + +namespace OHOS { +namespace DistributedHardware { +DCameraSourceLoadCallback::DCameraSourceLoadCallback(const std::string params) : params(params) {} +void DCameraSourceLoadCallback::OnLoadSystemAbilitySuccess(int32_t systemAbilityId, + const sptr& remoteObject) +{ + DHLOGI("OnLoadSystemAbilitySuccess start systemAbilityId: %d.", systemAbilityId); + if (systemAbilityId != DISTRIBUTED_HARDWARE_CAMERA_SOURCE_SA_ID) { + DHLOGE("start systemabilityId is not sourceSAId!"); + return; + } + DHLOGE("OnLoadSystemAbilitySuccess systemAbilityId: %d, IRmoteObject result: %s", + systemAbilityId, (remoteObject != nullptr) ? "true" : "false"); + if (remoteObject == nullptr) { + DHLOGE("remoteObject is null."); + return; + } + DCameraSourceHandler::GetInstance().FinishStartSA(params); +} + +void DCameraSourceLoadCallback::OnLoadSystemAbilityFail(int32_t systemAbilityId) +{ + DHLOGE("OnLoadSystemAbilityFail systemAbilityId: %d.", systemAbilityId); + if (systemAbilityId != DISTRIBUTED_HARDWARE_CAMERA_SOURCE_SA_ID) { + DHLOGE("start systemabilityId is not sourceSAId!"); + return; + } + DCameraSourceHandler::GetInstance().FinishStartSAFailed(systemAbilityId); +} +} +} \ No newline at end of file diff --git a/sa_profile/4803.xml b/sa_profile/4803.xml index d5e5bfa..fb50166 100644 --- a/sa_profile/4803.xml +++ b/sa_profile/4803.xml @@ -14,13 +14,13 @@ * limitations under the License. --> - dhardware + dcamerasrc 4803 libdistributed_camera_source.z.so - true + false true 1 diff --git a/sa_profile/4804.xml b/sa_profile/4804.xml index f0e7057..a827ae5 100644 --- a/sa_profile/4804.xml +++ b/sa_profile/4804.xml @@ -14,13 +14,13 @@ * limitations under the License. --> - dhardware + dcamerasink 4804 libdistributed_camera_sink.z.so - true + false true 1 diff --git a/services/cameraservice/sinkservice/BUILD.gn b/services/cameraservice/sinkservice/BUILD.gn index 7a27e23..c6f00f0 100644 --- a/services/cameraservice/sinkservice/BUILD.gn +++ b/services/cameraservice/sinkservice/BUILD.gn @@ -108,6 +108,7 @@ ohos_shared_library("distributed_camera_sink") { "${services_path}/data_process:distributed_camera_data_process", "//third_party/jsoncpp:jsoncpp", "//utils/native/base:utils", + ":dcamerasink.cfg", ] defines = [ @@ -127,4 +128,11 @@ ohos_shared_library("distributed_camera_sink") { subsystem_name = "distributedhardware" part_name = "distributed_camera" + } +ohos_prebuilt_etc("dcamerasink.cfg") { + relative_install_dir = "init" + source = "dcamerasink.cfg" + part_name = "distributed_camera" + subsystem_name = "distributedhardware" +} \ No newline at end of file diff --git a/services/cameraservice/sinkservice/dcamerasink.cfg b/services/cameraservice/sinkservice/dcamerasink.cfg new file mode 100644 index 0000000..ff208b8 --- /dev/null +++ b/services/cameraservice/sinkservice/dcamerasink.cfg @@ -0,0 +1,9 @@ +{ + "services" : [{ + "name" : "dcamerasink", + "path" : ["/system/bin/sa_main","/system/profile/dcamerasink.xml"], + "uid" : "system", + "gid" : ["system"], + "ondemand" : true + }] +} \ No newline at end of file diff --git a/services/cameraservice/sourceservice/BUILD.gn b/services/cameraservice/sourceservice/BUILD.gn index 8f53c80..f2042b3 100644 --- a/services/cameraservice/sourceservice/BUILD.gn +++ b/services/cameraservice/sourceservice/BUILD.gn @@ -93,6 +93,7 @@ ohos_shared_library("distributed_camera_source") { "${services_path}/data_process:distributed_camera_data_process", "//third_party/jsoncpp:jsoncpp", "//utils/native/base:utils", + ":dcamerasrc.cfg", ] defines = [ @@ -112,4 +113,11 @@ ohos_shared_library("distributed_camera_source") { subsystem_name = "distributedhardware" part_name = "distributed_camera" + } +ohos_prebuilt_etc("dcamerasrc.cfg") { + relative_install_dir = "init" + source = "dcamerasrc.cfg" + part_name = "distributed_camera" + subsystem_name = "distributedhardware" +} \ No newline at end of file diff --git a/services/cameraservice/sourceservice/dcamerasrc.cfg b/services/cameraservice/sourceservice/dcamerasrc.cfg new file mode 100644 index 0000000..74a1198 --- /dev/null +++ b/services/cameraservice/sourceservice/dcamerasrc.cfg @@ -0,0 +1,9 @@ +{ + "services" : [{ + "name" : "dcamerasrc", + "path" : ["/system/bin/sa_main","/system/profile/dcamerasrc.xml"], + "uid" : "system", + "gid" : ["system"], + "ondemand" : true + }] +} \ No newline at end of file