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