• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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 "voip_call_connection.h"
17 
18 #include "i_voip_call_manager_callback.h"
19 #include "i_voip_call_manager_service.h"
20 #include "iservice_registry.h"
21 #include "system_ability.h"
22 #include "system_ability_definition.h"
23 #include "telephony_log_wrapper.h"
24 #include "voip_call_manager_proxy.h"
25 
26 namespace OHOS {
27 namespace Telephony {
VoipCallConnection()28 VoipCallConnection::VoipCallConnection()
29     : systemAbilityId_(TELEPHONY_VOIP_CALL_MANAGER_SYS_ABILITY_ID), connectCallManagerState_(false)
30 {}
31 
~VoipCallConnection()32 VoipCallConnection::~VoipCallConnection()
33 {
34     UnInit();
35 }
36 
Init(int32_t systemAbilityId)37 void VoipCallConnection::Init(int32_t systemAbilityId)
38 {
39     std::lock_guard<std::mutex> lock(mutex_);
40     if (connectCallManagerState_) {
41         TELEPHONY_LOGE("Init, connectState is true");
42         return;
43     }
44     systemAbilityId_ = systemAbilityId;
45     TELEPHONY_LOGI("systemAbilityId_ = %{public}d", systemAbilityId);
46     GetCallManagerProxy();
47     statusChangeListener_ = new (std::nothrow) SystemAbilityListener();
48     if (statusChangeListener_ == nullptr) {
49         TELEPHONY_LOGE("Init, failed to create statusChangeListener.");
50         return;
51     }
52     sptr<ISystemAbilityManager> managerPtr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
53     if (managerPtr == nullptr) {
54         TELEPHONY_LOGE("voipconnect managerPtr is null");
55         return;
56     }
57     int32_t ret = managerPtr->SubscribeSystemAbility(systemAbilityId_, statusChangeListener_);
58     if (ret != TELEPHONY_SUCCESS) {
59         TELEPHONY_LOGE("Init, failed to subscribe sa:%{public}d", systemAbilityId_);
60         return;
61     }
62     TELEPHONY_LOGI("subscribe voip call manager successfully!");
63 }
64 
UnInit()65 void VoipCallConnection::UnInit()
66 {
67     std::lock_guard<std::mutex> lock(mutex_);
68     voipCallManagerInterfacePtr_ = nullptr;
69     connectCallManagerState_ = false;
70     if (statusChangeListener_ != nullptr) {
71         auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
72         if (samgrProxy != nullptr) {
73             samgrProxy->UnSubscribeSystemAbility(systemAbilityId_, statusChangeListener_);
74             statusChangeListener_ = nullptr;
75         }
76     }
77     TELEPHONY_LOGI("voip call connection uninit");
78 }
79 
GetCallManagerProxy()80 int32_t VoipCallConnection::GetCallManagerProxy()
81 {
82     TELEPHONY_LOGI("Voipconnect GetCallManagerProxy start");
83     if (voipCallManagerInterfacePtr_ != nullptr) {
84         return TELEPHONY_SUCCESS;
85     }
86     sptr<ISystemAbilityManager> managerPtr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
87     if (managerPtr == nullptr) {
88         TELEPHONY_LOGI("Voipconnect managerPtr is null");
89         return TELEPHONY_ERR_LOCAL_PTR_NULL;
90     }
91     sptr<IVoipCallManagerService> voipCallManagerInterfacePtr = nullptr;
92     sptr<IRemoteObject> iRemoteObjectPtr = managerPtr->GetSystemAbility(systemAbilityId_);
93     if (iRemoteObjectPtr == nullptr) {
94         TELEPHONY_LOGI("Voipconnect iRemoteObjectPtr is null");
95         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
96     }
97     voipCallManagerInterfacePtr = iface_cast<IVoipCallManagerService>(iRemoteObjectPtr);
98     if (!voipCallManagerInterfacePtr) {
99         TELEPHONY_LOGI("Voipconnect GetCallManagerProxy voipCallManagerInterfacePtr is null");
100         return TELEPHONY_ERR_LOCAL_PTR_NULL;
101     }
102 
103     voipCallManagerInterfacePtr_ = voipCallManagerInterfacePtr;
104     connectCallManagerState_ = true;
105     return TELEPHONY_SUCCESS;
106 }
107 
AnswerCall(const VoipCallEventInfo & events,int32_t videoState)108 int32_t VoipCallConnection::AnswerCall(const VoipCallEventInfo &events, int32_t videoState)
109 {
110     std::lock_guard<std::mutex> lock(mutex_);
111     GetCallManagerProxy();
112     if (voipCallManagerInterfacePtr_ == nullptr) {
113         TELEPHONY_LOGI("Voipconnect AnswerCall voipCallManagerInterfacePtr_ is null");
114         return TELEPHONY_ERROR;
115     }
116     return voipCallManagerInterfacePtr_->Answer(events, videoState);
117 }
118 
RejectCall(const VoipCallEventInfo & events)119 int32_t VoipCallConnection::RejectCall(const VoipCallEventInfo &events)
120 {
121     std::lock_guard<std::mutex> lock(mutex_);
122     GetCallManagerProxy();
123     if (voipCallManagerInterfacePtr_ == nullptr) {
124         TELEPHONY_LOGI("Voipconnect RejectCall voipCallManagerInterfacePtr_ is null");
125         return TELEPHONY_ERROR;
126     }
127     return voipCallManagerInterfacePtr_->Reject(events);
128 }
129 
HangUpCall(const VoipCallEventInfo & events)130 int32_t VoipCallConnection::HangUpCall(const VoipCallEventInfo &events)
131 {
132     std::lock_guard<std::mutex> lock(mutex_);
133     GetCallManagerProxy();
134     if (voipCallManagerInterfacePtr_ == nullptr) {
135         TELEPHONY_LOGI("Voipconnect HangUpCall voipCallManagerInterfacePtr_ is null");
136         return TELEPHONY_ERROR;
137     }
138     return voipCallManagerInterfacePtr_->HangUp(events);
139 }
140 
RegisterCallManagerCallBack(const sptr<ICallStatusCallback> & callback)141 int32_t VoipCallConnection::RegisterCallManagerCallBack(const sptr<ICallStatusCallback> &callback)
142 {
143     std::lock_guard<std::mutex> lock(mutex_);
144     GetCallManagerProxy();
145     if (voipCallManagerInterfacePtr_ == nullptr) {
146         TELEPHONY_LOGI("Voipconnect RegisterCallManagerCallBack voipCallManagerInterfacePtr_ is null");
147         return TELEPHONY_ERROR;
148     }
149     return voipCallManagerInterfacePtr_->RegisterCallManagerCallBack(callback);
150 }
151 
UnRegisterCallManagerCallBack()152 int32_t VoipCallConnection::UnRegisterCallManagerCallBack()
153 {
154     int32_t ret;
155     {
156         std::lock_guard<std::mutex> lock(mutex_);
157         GetCallManagerProxy();
158         if (voipCallManagerInterfacePtr_ == nullptr) {
159             TELEPHONY_LOGI("Voipconnect UnRegisterCallManagerCallBack voipCallManagerInterfacePtr_ is null");
160             return TELEPHONY_ERROR;
161         }
162         ret = voipCallManagerInterfacePtr_->UnRegisterCallManagerCallBack();
163     }
164     UnInit();
165     return ret;
166 }
167 
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)168 void VoipCallConnection::SystemAbilityListener::OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId)
169 {
170     auto voipCallConnection = DelayedSingleton<VoipCallConnection>::GetInstance();
171     if (voipCallConnection == nullptr) {
172         TELEPHONY_LOGE("voipCallConnection is nullptr");
173         return;
174     }
175     voipCallConnection->Init(systemAbilityId);
176 }
177 
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)178 void VoipCallConnection::SystemAbilityListener::OnRemoveSystemAbility(
179     int32_t systemAbilityId, const std::string &deviceId)
180 {
181     auto voipCallConnection = DelayedSingleton<VoipCallConnection>::GetInstance();
182     if (voipCallConnection == nullptr) {
183         TELEPHONY_LOGE("voipCallConnection is nullptr");
184         return;
185     }
186     voipCallConnection->ClearVoipCall();
187     voipCallConnection->UnInit();
188 }
189 
ClearVoipCall()190 void VoipCallConnection::ClearVoipCall()
191 {
192     if (!CallObjectManager::HasVoipCallExist()) {
193         TELEPHONY_LOGI("no voip call exist, no need to clear");
194         return;
195     }
196     std::list<sptr<CallBase>> allCallList = CallObjectManager::GetAllCallList();
197     for (auto call : allCallList) {
198         if (call != nullptr && call->GetCallType() == CallType::TYPE_VOIP) {
199             TELEPHONY_LOGI("clearVoipCall callId %{public}d", call->GetCallID());
200             CallObjectManager::DeleteOneCallObject(call);
201         }
202     }
203 }
204 
SendCallUiEvent(std::string voipCallId,const CallAudioEvent & callAudioEvent)205 int32_t VoipCallConnection::SendCallUiEvent(std::string voipCallId, const CallAudioEvent &callAudioEvent)
206 {
207     std::lock_guard<std::mutex> lock(mutex_);
208     GetCallManagerProxy();
209     if (voipCallManagerInterfacePtr_ == nullptr) {
210         TELEPHONY_LOGE("voipCallManagerInterfacePtr_ is nullptr");
211         return TELEPHONY_ERROR;
212     }
213     return voipCallManagerInterfacePtr_->SendCallUiEvent(voipCallId, callAudioEvent);
214 }
215 } // namespace Telephony
216 } // namespace OHOS
217