1 /* 2 * Copyright (c) 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 "dscreen_sink_handler.h" 17 18 #include "if_system_ability_manager.h" 19 #include "iservice_registry.h" 20 21 #include "dscreen_constants.h" 22 #include "dscreen_errcode.h" 23 #include "dscreen_log.h" 24 25 namespace OHOS { 26 namespace DistributedHardware { 27 IMPLEMENT_SINGLE_INSTANCE(DScreenSinkHandler); 28 DScreenSinkHandler()29DScreenSinkHandler::DScreenSinkHandler() 30 { 31 DHLOGI("DScreenSinkHandler construct."); 32 std::lock_guard<std::mutex> lock(mutex_); 33 if (!sinkSvrRecipient_) { 34 sinkSvrRecipient_ = new DScreenSinkSvrRecipient(); 35 } 36 } 37 ~DScreenSinkHandler()38DScreenSinkHandler::~DScreenSinkHandler() 39 { 40 DHLOGI("~DScreenSinkHandler."); 41 } 42 InitSink(const std::string & params)43int32_t DScreenSinkHandler::InitSink(const std::string ¶ms) 44 { 45 DHLOGD("InitSink"); 46 std::lock_guard<std::mutex> lock(mutex_); 47 48 if (!dScreenSinkProxy_) { 49 sptr<ISystemAbilityManager> samgr = 50 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); 51 if (!samgr) { 52 DHLOGE("Failed to get system ability mgr."); 53 return ERR_DH_SCREEN_SA_GET_SAMGR_FAIL; 54 } 55 sptr<IRemoteObject> remoteObject = samgr->GetSystemAbility(DISTRIBUTED_HARDWARE_SCREEN_SINK_SA_ID); 56 if (!remoteObject) { 57 DHLOGE("Failed to get dscreen sink service."); 58 return ERR_DH_SCREEN_SA_GET_SINKSERVICE_FAIL; 59 } 60 61 remoteObject->AddDeathRecipient(sinkSvrRecipient_); 62 dScreenSinkProxy_ = iface_cast<IDScreenSink>(remoteObject); 63 if ((!dScreenSinkProxy_) || (!dScreenSinkProxy_->AsObject())) { 64 DHLOGE("Failed to get dscreen sink proxy."); 65 return ERR_DH_SCREEN_SA_GET_SINKPROXY_FAIL; 66 } 67 } 68 int32_t ret = dScreenSinkProxy_->InitSink(params); 69 return ret; 70 } 71 ReleaseSink()72int32_t DScreenSinkHandler::ReleaseSink() 73 { 74 DHLOGD("ReleaseSink"); 75 std::lock_guard<std::mutex> lock(mutex_); 76 if (!dScreenSinkProxy_) { 77 DHLOGE("screen sink proxy not init."); 78 return ERR_DH_SCREEN_SA_SINKPROXY_NOT_INIT; 79 } 80 81 int32_t ret = dScreenSinkProxy_->ReleaseSink(); 82 return ret; 83 } 84 SubscribeLocalHardware(const std::string & dhId,const std::string & param)85int32_t DScreenSinkHandler::SubscribeLocalHardware(const std::string &dhId, const std::string ¶m) 86 { 87 DHLOGD("SubscribeLocalHardware"); 88 std::lock_guard<std::mutex> lock(mutex_); 89 if (!dScreenSinkProxy_) { 90 DHLOGE("screen sink proxy not init."); 91 return ERR_DH_SCREEN_SA_SINKPROXY_NOT_INIT; 92 } 93 int32_t ret = dScreenSinkProxy_->SubscribeLocalHardware(dhId, param); 94 return ret; 95 } 96 UnsubscribeLocalHardware(const std::string & dhId)97int32_t DScreenSinkHandler::UnsubscribeLocalHardware(const std::string &dhId) 98 { 99 DHLOGD("UnsubscribeLocalHardware"); 100 std::lock_guard<std::mutex> lock(mutex_); 101 if (!dScreenSinkProxy_) { 102 DHLOGE("screen sink proxy not init."); 103 return ERR_DH_SCREEN_SA_SINKPROXY_NOT_INIT; 104 } 105 int32_t ret = dScreenSinkProxy_->UnsubscribeLocalHardware(dhId); 106 return ret; 107 } 108 OnRemoteDied(const wptr<IRemoteObject> & remote)109void DScreenSinkHandler::DScreenSinkSvrRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote) 110 { 111 DHLOGI("DScreenSinkSvrRecipient OnRemoteDied."); 112 DScreenSinkHandler::GetInstance().OnRemoteSinkSvrDied(remote); 113 } 114 OnRemoteSinkSvrDied(const wptr<IRemoteObject> & remote)115void DScreenSinkHandler::OnRemoteSinkSvrDied(const wptr<IRemoteObject> &remote) 116 { 117 DHLOGI("OnRemoteSinkSvrDied"); 118 sptr<IRemoteObject> remoteObject = remote.promote(); 119 if (!remoteObject) { 120 DHLOGE("OnRemoteDied remote promoted failed"); 121 return; 122 } 123 std::lock_guard<std::mutex> lock(mutex_); 124 if (dScreenSinkProxy_ != nullptr) { 125 dScreenSinkProxy_->AsObject()->RemoveDeathRecipient(sinkSvrRecipient_); 126 dScreenSinkProxy_ = nullptr; 127 } 128 } 129 GetSinkHardwareHandler()130IDistributedHardwareSink *GetSinkHardwareHandler() 131 { 132 DHLOGD("GetSinkHardwareHandler"); 133 return &DScreenSinkHandler::GetInstance(); 134 } 135 } // namespace DistributedHardware 136 } // namespace OHOS