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 #include "dcamera_source_handler_ipc.h" 17 18 #include <cstdint> 19 #include <new> 20 21 #include "if_system_ability_manager.h" 22 #include "iservice_registry.h" 23 #include "dcamera_source_handler.h" 24 #include "distributed_camera_constants.h" 25 #include "distributed_hardware_log.h" 26 #include "iremote_broker.h" 27 28 namespace OHOS { 29 namespace DistributedHardware { DCameraSourceHandlerIpc()30DCameraSourceHandlerIpc::DCameraSourceHandlerIpc() : isInit_(false) 31 { 32 DHLOGI("DCameraSourceHandlerIpc Create"); 33 } 34 ~DCameraSourceHandlerIpc()35DCameraSourceHandlerIpc::~DCameraSourceHandlerIpc() 36 { 37 DHLOGI("DCameraSourceHandlerIpc Delete"); 38 UnInit(); 39 } 40 41 IMPLEMENT_SINGLE_INSTANCE(DCameraSourceHandlerIpc); 42 Init()43void DCameraSourceHandlerIpc::Init() 44 { 45 std::lock_guard<std::mutex> autoLock(initCamSrvLock_); 46 DHLOGI("Start"); 47 if (isInit_) { 48 DHLOGI("DCameraSourceHandlerIpc has already init"); 49 return; 50 } 51 sourceLocalRecipient_ = sptr<SourceLocalRecipient>(new SourceLocalRecipient()); 52 isInit_ = true; 53 DHLOGI("End"); 54 } 55 UnInit()56void DCameraSourceHandlerIpc::UnInit() 57 { 58 std::lock_guard<std::mutex> autoLock(initCamSrvLock_); 59 DHLOGI("Start"); 60 if (!isInit_) { 61 DHLOGI("DCameraSourceHandlerIpc has already UnInit"); 62 return; 63 } 64 DeleteSourceLocalCamSrv(); 65 DHLOGI("DCameraSourceHandlerIpc Start free recipient"); 66 sourceLocalRecipient_ = nullptr; 67 isInit_ = false; 68 DHLOGI("End"); 69 } 70 GetSourceLocalCamSrv()71sptr<IDistributedCameraSource> DCameraSourceHandlerIpc::GetSourceLocalCamSrv() 72 { 73 { 74 std::lock_guard<std::mutex> autoLock(sourceLocalCamSrvLock_); 75 if (localSource_ != nullptr) { 76 DHLOGI("DCameraSourceHandlerIpc GetSourceLocalCamSrv from cache"); 77 return localSource_; 78 } 79 } 80 DHLOGI("Start"); 81 sptr<ISystemAbilityManager> sm = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); 82 if (sm == nullptr) { 83 DHLOGE("GetSystemAbilityManager failed"); 84 return nullptr; 85 } 86 87 sptr<IRemoteObject> object = sm->GetSystemAbility(DISTRIBUTED_HARDWARE_CAMERA_SOURCE_SA_ID); 88 if (object == nullptr) { 89 DHLOGE("GetSystemAbility failed"); 90 return nullptr; 91 } 92 int32_t ret = object->AddDeathRecipient(sourceLocalRecipient_); 93 sptr<IDistributedCameraSource> localSource = iface_cast<IDistributedCameraSource>(object); 94 if (localSource == nullptr) { 95 DHLOGI("GetSourceLocalCamSrv failed, localSource is null ret: %{public}d", ret); 96 return nullptr; 97 } 98 { 99 std::lock_guard<std::mutex> autoLock(sourceLocalCamSrvLock_); 100 if (localSource_ != nullptr) { 101 localSource_->AsObject()->RemoveDeathRecipient(sourceLocalRecipient_); 102 } 103 localSource_ = localSource; 104 } 105 DHLOGI("success, AddDeathRecipient ret: %{public}d", ret); 106 return localSource; 107 } 108 DeleteSourceLocalCamSrv()109void DCameraSourceHandlerIpc::DeleteSourceLocalCamSrv() 110 { 111 DHLOGI("start"); 112 std::lock_guard<std::mutex> autoLock(sourceLocalCamSrvLock_); 113 if (localSource_ != nullptr) { 114 localSource_->AsObject()->RemoveDeathRecipient(sourceLocalRecipient_); 115 } 116 localSource_ = nullptr; 117 DHLOGI("end"); 118 } 119 OnRemoteDied(const wptr<IRemoteObject> & remote)120void DCameraSourceHandlerIpc::SourceLocalRecipient::OnRemoteDied(const wptr<IRemoteObject>& remote) 121 { 122 DHLOGI("SourceLocalRecipient OnRemoteDied received died notify!"); 123 DCameraSourceHandlerIpc::GetInstance().OnSourceLocalCamSrvDied(remote); 124 } 125 OnSourceLocalCamSrvDied(const wptr<IRemoteObject> & remote)126void DCameraSourceHandlerIpc::OnSourceLocalCamSrvDied(const wptr<IRemoteObject>& remote) 127 { 128 DHLOGI("OnSourceLocalCamSrvDied delete diedRemoted"); 129 std::lock_guard<std::mutex> autoLock(sourceLocalCamSrvLock_); 130 if (localSource_ == nullptr) { 131 DHLOGE("localSource is null."); 132 return; 133 } 134 sptr<IRemoteObject> diedRemoted = remote.promote(); 135 if (diedRemoted == nullptr) { 136 DHLOGE("OnSourceLocalCamSrvDied promote failed!"); 137 return; 138 } 139 if (localSource_->AsObject() != diedRemoted) { 140 DHLOGI("OnSourceLocalCamSrvDied not found remote object."); 141 return; 142 } 143 DHLOGI("OnSourceLocalCamSrvDied Clear"); 144 localSource_->AsObject()->RemoveDeathRecipient(sourceLocalRecipient_); 145 localSource_ = nullptr; 146 DCameraSourceHandler::GetInstance().SetSAState(); 147 } 148 } // namespace DistributedHardware 149 } // namespace OHOS 150