1 /*
2 * Copyright (c) 2024 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 "collaboration_manager.h"
17
18 namespace OHOS::AVSession {
GetInstance()19 CollaborationManager& CollaborationManager::GetInstance()
20 {
21 static CollaborationManager collaborationManager;
22 return collaborationManager;
23 }
24
CollaborationManager()25 CollaborationManager::CollaborationManager()
26 {
27 localHardwareList_ = {
28 .hardWareType = ServiceCollaborationManagerHardwareType::SCM_UNKNOWN_TYPE,
29 .canShare = false
30 };
31 remoteHardwareList_[0] = {
32 .hardWareType = ServiceCollaborationManagerHardwareType::SCM_DISPLAY,
33 .canShare = false
34 };
35 remoteHardwareList_[1] = {
36 .hardWareType = ServiceCollaborationManagerHardwareType::SCM_SPEAKER,
37 .canShare = false
38 };
39 communicationRequest_ = {
40 .minBandwidth = 80 * 1024 * 1024,
41 .maxLatency = 5000,
42 .minLatency = 500,
43 .maxWaitTime = 60000,
44 .dataType = dataType_.c_str()
45 };
46 }
47
~CollaborationManager()48 CollaborationManager::~CollaborationManager()
49 {
50 SLOGI("enter ~CollaborationManager");
51 delete resourceRequest_;
52 resourceRequest_ = nullptr;
53 }
54
SendRejectStateToStopCast(const std::function<void (const int32_t code)> & callback)55 void CollaborationManager::SendRejectStateToStopCast(const std::function<
56 void(const int32_t code)>& callback)
57 {
58 sendRejectStateToStopCast_ = callback;
59 }
60
OnStop(const char * peerNetworkId)61 __attribute__((no_sanitize("cfi")))static int32_t OnStop(const char* peerNetworkId)
62 {
63 SLOGE("Onstop to stop cast");
64 CollaborationManager::GetInstance().sendRejectStateToStopCast_(
65 ServiceCollaborationManagerResultCode::ONSTOP);
66 return AVSESSION_SUCCESS;
67 }
68
ApplyResult(int32_t errorcode,int32_t result,const char * reason)69 __attribute__((no_sanitize("cfi")))static int32_t ApplyResult(int32_t errorcode,
70 int32_t result, const char* reason)
71 {
72 if (result == ServiceCollaborationManagerResultCode::REJECT) {
73 SLOGE("return connect reject and reson:%{public}s", reason);
74 }
75 CollaborationManager::GetInstance().sendRejectStateToStopCast_(result);
76 return AVSESSION_SUCCESS;
77 }
78
79 static ServiceCollaborationManager_Callback serviceCollaborationCallback {
80 .OnStop = OnStop,
81 .ApplyResult = ApplyResult
82 };
83
ReadCollaborationManagerSo()84 __attribute__((no_sanitize("cfi"))) int32_t CollaborationManager::ReadCollaborationManagerSo()
85 {
86 SLOGI("enter ReadCollaborationManagerSo");
87 void *collaborationManagerExport = pluginLib_.LoadSymbol("ServiceCollaborationManager_Export");
88 if (collaborationManagerExport == nullptr) {
89 SLOGE("load libcfwk_allconnect_client.z.so failed");
90 return AVSESSION_ERROR;
91 }
92 collaborationManagerExportFun_ = (reinterpret_cast<CollaborationManagerExportFunType>(
93 collaborationManagerExport));
94 (*collaborationManagerExportFun_)(&exportapi_);
95 return AVSESSION_SUCCESS;
96 }
97
RegisterLifecycleCallback()98 int32_t CollaborationManager::RegisterLifecycleCallback()
99 {
100 SLOGI("enter RegisterLifecycleCallback");
101 if (exportapi_.ServiceCollaborationManager_RegisterLifecycleCallback == nullptr) {
102 SLOGE("RegisterLifecycleCallback function sptr nullptr");
103 return AVSESSION_ERROR;
104 }
105 if (exportapi_.ServiceCollaborationManager_RegisterLifecycleCallback(serviceName_.c_str(),
106 &serviceCollaborationCallback)) {
107 return AVSESSION_ERROR;
108 }
109 return AVSESSION_SUCCESS;
110 }
111
UnRegisterLifecycleCallback()112 int32_t CollaborationManager::UnRegisterLifecycleCallback()
113 {
114 SLOGI("enter UnRegisterLifecycleCallback");
115 if (exportapi_.ServiceCollaborationManager_UnRegisterLifecycleCallback == nullptr) {
116 SLOGE("UnRegisterLifecycleCallback function sptr nullptr");
117 return AVSESSION_ERROR;
118 }
119 if (exportapi_.ServiceCollaborationManager_UnRegisterLifecycleCallback(serviceName_.c_str())) {
120 return AVSESSION_ERROR;
121 }
122 return AVSESSION_SUCCESS;
123 }
124
PublishServiceState(const char * peerNetworkId,ServiceCollaborationManagerBussinessStatus state)125 int32_t CollaborationManager::PublishServiceState(const char* peerNetworkId,
126 ServiceCollaborationManagerBussinessStatus state)
127 {
128 SLOGI("enter PublishServiceState");
129 if (exportapi_.ServiceCollaborationManager_PublishServiceState == nullptr) {
130 SLOGE("PublishServiceState function sptr nullptr");
131 return AVSESSION_ERROR;
132 }
133 if (exportapi_.ServiceCollaborationManager_PublishServiceState(peerNetworkId,
134 serviceName_.c_str(), "NULL", state)) {
135 return AVSESSION_ERROR;
136 }
137 return AVSESSION_SUCCESS;
138 }
139
ApplyAdvancedResource(const char * peerNetworkId)140 int32_t CollaborationManager::ApplyAdvancedResource(const char* peerNetworkId)
141 {
142 SLOGI("enter ApplyAdvancedResource");
143 if (exportapi_.ServiceCollaborationManager_ApplyAdvancedResource == nullptr) {
144 SLOGE("ApplyAdvancedResource function sptr nullptr");
145 return AVSESSION_ERROR;
146 }
147 if (resourceRequest_ == nullptr) {
148 SLOGE("resourceRequest_ is nullptr");
149 return AVSESSION_ERROR;
150 }
151 resourceRequest_->localHardwareListSize = localHardwareListSize_;
152 resourceRequest_->localHardwareList = &localHardwareList_;
153 resourceRequest_->remoteHardwareListSize = remoteHardwareListSize_;
154 resourceRequest_->remoteHardwareList = remoteHardwareList_;
155 resourceRequest_->communicationRequest = &communicationRequest_;
156 if (exportapi_.ServiceCollaborationManager_ApplyAdvancedResource(peerNetworkId,
157 serviceName_.c_str(), resourceRequest_, &serviceCollaborationCallback)) {
158 return AVSESSION_ERROR;
159 }
160 return AVSESSION_SUCCESS;
161 }
162 } // namespace OHOS::AVSession