1 /*
2 * Copyright (c) 2023-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 "avrouter_impl.h"
17 #include "ipc_skeleton.h"
18 #include "avsession_errors.h"
19 #include "avsession_log.h"
20 #include "avsession_trace.h"
21 #include "permission_checker.h"
22 #include "avcast_provider_manager.h"
23 #include "avsession_sysevent.h"
24
25 static std::shared_ptr<OHOS::AVSession::HwCastProvider> hwProvider_;
26
27 namespace OHOS::AVSession {
AVRouterImpl()28 AVRouterImpl::AVRouterImpl()
29 {
30 SLOGD("AVRouter construct");
31 }
32
Init(IAVSessionServiceListener * servicePtr)33 void AVRouterImpl::Init(IAVSessionServiceListener *servicePtr)
34 {
35 SLOGI("Start init AVRouter");
36 {
37 std::lock_guard lockGuard(servicePtrLock_);
38 servicePtr_ = servicePtr;
39 }
40 hwProvider_ = std::make_shared<HwCastProvider>();
41 if (hwProvider_ != nullptr) {
42 hwProvider_->Init();
43 } else {
44 SLOGE("init with null pvd to init");
45 return;
46 }
47 providerNumber_ = providerNumberEnableDefault_;
48 std::shared_ptr<AVCastProviderManager> avCastProviderManager = std::make_shared<AVCastProviderManager>();
49 if (avCastProviderManager == nullptr) {
50 SLOGE("init with null manager");
51 return;
52 }
53 avCastProviderManager->Init(providerNumber_, hwProvider_);
54 providerManagerMap_[providerNumber_] = avCastProviderManager;
55 if (hwProvider_ != nullptr) {
56 hwProvider_->RegisterCastStateListener(avCastProviderManager);
57 } else {
58 SLOGE("init with null pvd to registerlistener");
59 return;
60 }
61 if (cacheStartDiscovery_) {
62 SLOGI("cacheStartDiscovery check do discovery");
63 std::lock_guard lockGuard(providerManagerLock_);
64 StartCastDiscovery(cacheCastDeviceCapability_, cacheDrmSchemes_);
65 cacheStartDiscovery_ = false;
66 }
67 SLOGI("init AVRouter done");
68 }
69
Release()70 bool AVRouterImpl::Release()
71 {
72 SLOGI("Start Release AVRouter");
73 if (hasSessionAlive_) {
74 SLOGE("has session alive, but continue");
75 }
76 if (hwProvider_ == nullptr || (castServiceNameMapState_["HuaweiCast"] == deviceStateConnection ||
77 castServiceNameMapState_["HuaweiCast-Dual"] == deviceStateConnection)) {
78 SLOGE("Start Release AVRouter err for no provider");
79 return false;
80 }
81 std::lock_guard lockGuard(providerManagerLock_);
82
83 if (hwProvider_ == nullptr) {
84 SLOGE("repeat check for no pvd");
85 return false;
86 }
87 SLOGI("repeat check for pvd alive");
88 hwProvider_->Release();
89 hwProvider_ = nullptr;
90 providerNumber_ = providerNumberDisable_;
91 providerManagerMap_.clear();
92 SLOGD("Release AVRouter done");
93 return false;
94 }
95
StartDeviceLogging(int32_t fd,uint32_t maxSize)96 int32_t AVRouterImpl::StartDeviceLogging(int32_t fd, uint32_t maxSize)
97 {
98 SLOGI("AVRouterImpl StartDeviceLogging");
99 std::lock_guard lockGuard(providerManagerLock_);
100
101 if (providerManagerMap_.empty()) {
102 cacheStartDeviceLogging_ = true;
103 return AVSESSION_SUCCESS;
104 }
105 for (const auto& [number, providerManager] : providerManagerMap_) {
106 CHECK_AND_RETURN_RET_LOG(providerManager != nullptr && providerManager->provider_ != nullptr,
107 AVSESSION_ERROR, "provider is nullptr");
108 providerManager->provider_->StartDeviceLogging(fd, maxSize);
109 }
110 return AVSESSION_SUCCESS;
111 }
112
StopDeviceLogging()113 int32_t AVRouterImpl::StopDeviceLogging()
114 {
115 SLOGI("AVRouterImpl StopDeviceLogging");
116 std::lock_guard lockGuard(providerManagerLock_);
117
118 if (cacheStartDeviceLogging_) {
119 SLOGI("clear cacheStartDeviceLogging_ when stop discovery");
120 cacheStartDeviceLogging_ = false;
121 }
122 for (const auto& [number, providerManager] : providerManagerMap_) {
123 CHECK_AND_RETURN_RET_LOG(providerManager != nullptr && providerManager->provider_ != nullptr,
124 AVSESSION_ERROR, "provider is nullptr");
125 providerManager->provider_->StopDeviceLogging();
126 }
127 return AVSESSION_SUCCESS;
128 }
129
StartCastDiscovery(int32_t castDeviceCapability,std::vector<std::string> drmSchemes)130 int32_t AVRouterImpl::StartCastDiscovery(int32_t castDeviceCapability, std::vector<std::string> drmSchemes)
131 {
132 SLOGI("AVRouterImpl StartCastDiscovery");
133
134 std::lock_guard lockGuard(providerManagerLock_);
135
136 if (providerManagerMap_.empty()) {
137 SLOGI("set cacheStartDiscovery with no element with cap %{public}d", static_cast<int>(castDeviceCapability));
138 cacheStartDiscovery_ = true;
139 cacheCastDeviceCapability_ = castDeviceCapability;
140 cacheDrmSchemes_ = drmSchemes;
141 return AVSESSION_SUCCESS;
142 }
143 for (const auto& [number, providerManager] : providerManagerMap_) {
144 CHECK_AND_RETURN_RET_LOG(providerManager != nullptr && providerManager->provider_ != nullptr,
145 AVSESSION_ERROR, "provider is nullptr");
146 providerManager->provider_->StartDiscovery(castDeviceCapability, drmSchemes);
147 }
148 return AVSESSION_SUCCESS;
149 }
150
StopCastDiscovery()151 int32_t AVRouterImpl::StopCastDiscovery()
152 {
153 SLOGI("AVRouterImpl StopCastDiscovery");
154
155 std::lock_guard lockGuard(providerManagerLock_);
156
157 if (cacheStartDiscovery_) {
158 SLOGI("clear cacheStartDiscovery when stop discovery");
159 cacheStartDiscovery_ = false;
160 }
161 for (const auto& [number, providerManager] : providerManagerMap_) {
162 CHECK_AND_RETURN_RET_LOG(providerManager != nullptr && providerManager->provider_ != nullptr,
163 AVSESSION_ERROR, "provider is nullptr");
164 providerManager->provider_->StopDiscovery();
165 }
166 return AVSESSION_SUCCESS;
167 }
168
SetDiscoverable(const bool enable)169 int32_t AVRouterImpl::SetDiscoverable(const bool enable)
170 {
171 SLOGI("AVRouterImpl SetDiscoverable %{public}d", enable);
172 std::lock_guard lockGuard(providerManagerLock_);
173
174 for (const auto& [number, providerManager] : providerManagerMap_) {
175 CHECK_AND_RETURN_RET_LOG(providerManager != nullptr && providerManager->provider_ != nullptr,
176 AVSESSION_ERROR, "provider is nullptr");
177 providerManager->provider_->SetDiscoverable(enable);
178 }
179
180 return AVSESSION_SUCCESS;
181 }
182
OnDeviceAvailable(OutputDeviceInfo & castOutputDeviceInfo)183 int32_t AVRouterImpl::OnDeviceAvailable(OutputDeviceInfo& castOutputDeviceInfo)
184 {
185 SLOGI("AVRouterImpl received OnDeviceAvailable event");
186
187 std::lock_guard lockGuard(servicePtrLock_);
188 if (servicePtr_ == nullptr) {
189 return ERR_SERVICE_NOT_EXIST;
190 }
191 servicePtr_->NotifyDeviceAvailable(castOutputDeviceInfo);
192 return AVSESSION_SUCCESS;
193 }
194
OnDeviceLogEvent(const DeviceLogEventCode eventId,const int64_t param)195 int32_t AVRouterImpl::OnDeviceLogEvent(const DeviceLogEventCode eventId, const int64_t param)
196 {
197 SLOGI("AVRouterImpl received OnDeviceLogEvent event");
198
199 std::lock_guard lockGuard(servicePtrLock_);
200 if (servicePtr_ == nullptr) {
201 return ERR_SERVICE_NOT_EXIST;
202 }
203 servicePtr_->NotifyDeviceLogEvent(eventId, param);
204 return AVSESSION_SUCCESS;
205 }
206
ReleaseCurrentCastSession()207 void AVRouterImpl::ReleaseCurrentCastSession()
208 {
209 SLOGI("Start ReleaseCurrentCastSession");
210 std::lock_guard lockGuard(servicePtrLock_);
211 servicePtr_->ReleaseCastSession();
212 }
213
OnCastSessionCreated(const int32_t castId)214 int32_t AVRouterImpl::OnCastSessionCreated(const int32_t castId)
215 {
216 SLOGI("AVRouterImpl On cast session created, cast id is %{public}d", castId);
217 int64_t castHandle = -1;
218 CHECK_AND_RETURN_RET_LOG(providerManagerMap_.find(providerNumberEnableDefault_) !=
219 providerManagerMap_.end(), castHandle, "Can not find corresponding provider");
220 CHECK_AND_RETURN_RET_LOG(providerManagerMap_[1] != nullptr
221 && providerManagerMap_[1]->provider_ != nullptr, AVSESSION_ERROR, "provider is nullptr");
222 int64_t tempId = 1;
223 // The first 32 bits are providerId, the last 32 bits are castId
224 castHandle = static_cast<int64_t>((static_cast<uint64_t>(tempId) << 32) |
225 static_cast<const uint32_t>(castId));
226 {
227 std::lock_guard lockGuard(servicePtrLock_);
228 servicePtr_->CreateSessionByCast(castHandle);
229 }
230
231 OutputDeviceInfo outputDeviceInfo;
232 castHandleToOutputDeviceMap_[castId] = outputDeviceInfo;
233 return AVSESSION_SUCCESS;
234 }
235
OnDeviceOffline(const std::string & deviceId)236 int32_t AVRouterImpl::OnDeviceOffline(const std::string& deviceId)
237 {
238 SLOGI("AVRouterImpl received OnDeviceOffline event");
239
240 std::lock_guard lockGuard(servicePtrLock_);
241 if (servicePtr_ == nullptr) {
242 return ERR_SERVICE_NOT_EXIST;
243 }
244 servicePtr_->NotifyDeviceOffline(deviceId);
245 return AVSESSION_SUCCESS;
246 }
247
OnCastServerDied(int32_t providerNumber)248 int32_t AVRouterImpl::OnCastServerDied(int32_t providerNumber)
249 {
250 SLOGI("AVRouterImpl received OnCastServerDied event");
251 hasSessionAlive_ = false;
252 std::lock_guard lockGuard(providerManagerLock_);
253 if (providerManagerMap_.find(providerNumber) != providerManagerMap_.end()) {
254 providerManagerMap_.erase(providerNumber);
255 } else {
256 return AVSESSION_ERROR;
257 }
258 return AVSESSION_SUCCESS;
259 }
260
GetRemoteController(const int64_t castHandle)261 std::shared_ptr<IAVCastControllerProxy> AVRouterImpl::GetRemoteController(const int64_t castHandle)
262 {
263 SLOGI("AVRouterImpl start get remote controller process");
264
265 // The first 32 bits are providerId, the last 32 bits are castId
266 int32_t providerNumber = static_cast<int32_t>(static_cast<const uint64_t>(castHandle) >> 32);
267 SLOGD("Get remote controller of provider %{public}d", providerNumber);
268 // The first 32 bits are providerId, the last 32 bits are castId
269 int32_t castId = static_cast<int32_t>((static_cast<const uint64_t>(castHandle) << 32) >> 32);
270 CHECK_AND_RETURN_RET_LOG(providerManagerMap_.find(providerNumber) != providerManagerMap_.end(),
271 nullptr, "Can not find corresponding provider");
272 CHECK_AND_RETURN_RET_LOG(providerManagerMap_[providerNumber] != nullptr &&
273 providerManagerMap_[providerNumber]->provider_ != nullptr, nullptr, "provider is nullptr");
274 return providerManagerMap_[providerNumber]->provider_->GetRemoteController(castId);
275 }
276
StartCast(const OutputDeviceInfo & outputDeviceInfo,std::map<std::string,std::string> & serviceNameMapState)277 int64_t AVRouterImpl::StartCast(const OutputDeviceInfo& outputDeviceInfo,
278 std::map<std::string, std::string>& serviceNameMapState)
279 {
280 SLOGI("AVRouterImpl start cast process");
281 castServiceNameMapState_ = serviceNameMapState;
282 int64_t castHandle = -1;
283 CHECK_AND_RETURN_RET_LOG(providerManagerMap_.find(outputDeviceInfo.deviceInfos_[0].providerId_) !=
284 providerManagerMap_.end(), castHandle, "Can not find corresponding provider");
285 CHECK_AND_RETURN_RET_LOG(providerManagerMap_[outputDeviceInfo.deviceInfos_[0].providerId_] != nullptr
286 && providerManagerMap_[outputDeviceInfo.deviceInfos_[0].providerId_]->provider_ != nullptr,
287 AVSESSION_ERROR, "provider is nullptr");
288 int32_t castId = providerManagerMap_[outputDeviceInfo.deviceInfos_[0].providerId_]->provider_->StartCastSession();
289 int64_t tempId = outputDeviceInfo.deviceInfos_[0].providerId_;
290 // The first 32 bits are providerId, the last 32 bits are castId
291 castHandle = static_cast<int64_t>((static_cast<uint64_t>(tempId) << 32) | static_cast<uint32_t>(castId));
292 hasSessionAlive_ = true;
293
294 return castHandle;
295 }
296
AddDevice(const int32_t castId,const OutputDeviceInfo & outputDeviceInfo)297 int32_t AVRouterImpl::AddDevice(const int32_t castId, const OutputDeviceInfo& outputDeviceInfo)
298 {
299 SLOGI("AVRouterImpl AddDevice process");
300 bool ret = providerManagerMap_[outputDeviceInfo.deviceInfos_[0].providerId_]->provider_->AddCastDevice(castId,
301 outputDeviceInfo.deviceInfos_[0]);
302 SLOGI("AVRouterImpl AddDevice process with ret %{public}d", static_cast<int32_t>(ret));
303 castHandleToOutputDeviceMap_[castId] = outputDeviceInfo;
304 return ret ? AVSESSION_SUCCESS : ERR_DEVICE_CONNECTION_FAILED;
305 }
306
StopCast(const int64_t castHandle,int32_t removeTimes)307 int32_t AVRouterImpl::StopCast(const int64_t castHandle, int32_t removeTimes)
308 {
309 SLOGI("AVRouterImpl stop cast process");
310
311 int32_t providerNumber = static_cast<int32_t>(static_cast<uint64_t>(castHandle) >> 32);
312 SLOGI("Stop cast, the provider number is %{public}d", providerNumber);
313 CHECK_AND_RETURN_RET_LOG(providerManagerMap_.find(providerNumber) != providerManagerMap_.end(),
314 castHandle, "Can not find corresponding provider");
315 // The first 32 bits are providerId, the last 32 bits are castId
316 int32_t castId = static_cast<int32_t>((static_cast<const uint64_t>(castHandle) << 32) >> 32);
317 SLOGI("Stop cast, the castId is %{public}d, removeTimes is %{public}d", castId, removeTimes);
318 CHECK_AND_RETURN_RET_LOG(castHandleToOutputDeviceMap_.find(castId) != castHandleToOutputDeviceMap_.end(),
319 AVSESSION_ERROR, "Can not find corresponding castId");
320 CHECK_AND_RETURN_RET_LOG(providerManagerMap_[providerNumber] != nullptr
321 && providerManagerMap_[providerNumber]->provider_ != nullptr, AVSESSION_ERROR, "provider is nullptr");
322 CHECK_AND_RETURN_RET_LOG(castId != providerManagerMap_[providerNumber]->provider_->GetMirrorCastId() ||
323 removeTimes == 0, AVSESSION_ERROR, "cannot remove");
324 providerManagerMap_[providerNumber]->provider_->RemoveCastDevice(castId,
325 castHandleToOutputDeviceMap_[castId].deviceInfos_[0]);
326 hasSessionAlive_ = false;
327 SLOGI("AVRouterImpl stop cast process remove device done");
328
329 return AVSESSION_SUCCESS;
330 }
331
StopCastSession(const int64_t castHandle)332 int32_t AVRouterImpl::StopCastSession(const int64_t castHandle)
333 {
334 SLOGI("AVRouterImpl stop cast process");
335
336 int32_t providerNumber = static_cast<int32_t>(static_cast<uint64_t>(castHandle) >> 32);
337
338 CHECK_AND_RETURN_RET_LOG(providerManagerMap_.find(providerNumber) != providerManagerMap_.end(),
339 castHandle, "Can not find corresponding provider");
340 // The first 32 bits are providerId, the last 32 bits are castId
341 int32_t castId = static_cast<int32_t>((static_cast<uint64_t>(castHandle) << 32) >> 32);
342 castHandleToOutputDeviceMap_[castId] = castOutputDeviceInfo_;
343 CHECK_AND_RETURN_RET_LOG(castHandleToOutputDeviceMap_.find(castId) != castHandleToOutputDeviceMap_.end(),
344 AVSESSION_ERROR, "Can not find corresponding castId");
345 CHECK_AND_RETURN_RET_LOG(providerManagerMap_[providerNumber] != nullptr
346 && providerManagerMap_[providerNumber]->provider_ != nullptr, AVSESSION_ERROR, "provider is nullptr");
347 providerManagerMap_[providerNumber]->provider_->StopCastSession(castId);
348 hasSessionAlive_ = false;
349
350 return AVSESSION_SUCCESS;
351 }
352
SetServiceAllConnectState(int64_t castHandle,DeviceInfo deviceInfo)353 int32_t AVRouterImpl::SetServiceAllConnectState(int64_t castHandle, DeviceInfo deviceInfo)
354 {
355 int32_t providerNumber = static_cast<int32_t>(static_cast<uint64_t>(castHandle) >> 32);
356 int32_t castId = static_cast<int32_t>((static_cast<uint64_t>(castHandle) << 32) >> 32);
357 castOutputDeviceInfo_.deviceInfos_.emplace_back(deviceInfo);
358 castHandleToOutputDeviceMap_[castId] = castOutputDeviceInfo_;
359 CHECK_AND_RETURN_RET_LOG(providerManagerMap_.find(providerNumber) != providerManagerMap_.end(),
360 AVSESSION_ERROR, "Can not find corresponding provider");
361 CHECK_AND_RETURN_RET_LOG(providerManagerMap_[providerNumber] != nullptr
362 && providerManagerMap_[providerNumber]->provider_ != nullptr, AVSESSION_ERROR, "provider is nullptr");
363 providerManagerMap_[providerNumber]->provider_->SetStreamState(castId, deviceInfo);
364 return AVSESSION_SUCCESS;
365 }
366
GetRemoteNetWorkId(int64_t castHandle,std::string deviceId,std::string & networkId)367 int32_t AVRouterImpl::GetRemoteNetWorkId(int64_t castHandle, std::string deviceId, std::string &networkId)
368 {
369 int32_t providerNumber = static_cast<int32_t>(static_cast<uint64_t>(castHandle) >> 32);
370 int32_t castId = static_cast<int32_t>((static_cast<uint64_t>(castHandle) << 32) >> 32);
371 CHECK_AND_RETURN_RET_LOG(providerManagerMap_.find(providerNumber) != providerManagerMap_.end(),
372 AVSESSION_ERROR, "Can not find corresponding provider");
373 CHECK_AND_RETURN_RET_LOG(providerManagerMap_[providerNumber] != nullptr
374 && providerManagerMap_[providerNumber]->provider_ != nullptr, AVSESSION_ERROR, "provider is nullptr");
375 providerManagerMap_[providerNumber]->provider_->GetRemoteNetWorkId(castId, deviceId, networkId);
376 return AVSESSION_SUCCESS;
377 }
378
RegisterCallback(int64_t castHandle,const std::shared_ptr<IAVCastSessionStateListener> callback)379 int32_t AVRouterImpl::RegisterCallback(int64_t castHandle, const std::shared_ptr<IAVCastSessionStateListener> callback)
380 {
381 SLOGI("AVRouterImpl register IAVCastSessionStateListener callback to provider");
382 // The first 32 bits are providerId, the last 32 bits are castId
383 int32_t providerNumber = static_cast<int32_t>(static_cast<uint64_t>(castHandle) >> 32);
384 // The first 32 bits are providerId, the last 32 bits are castId
385 int32_t castId = static_cast<int32_t>((static_cast<uint64_t>(castHandle) << 32) >> 32);
386 CHECK_AND_RETURN_RET_LOG(providerManagerMap_.find(providerNumber) != providerManagerMap_.end(),
387 AVSESSION_ERROR, "Can not find corresponding provider");
388 CHECK_AND_RETURN_RET_LOG(providerManagerMap_[providerNumber] != nullptr
389 && providerManagerMap_[providerNumber]->provider_ != nullptr, AVSESSION_ERROR, "provider is nullptr");
390 providerManagerMap_[providerNumber]->provider_->RegisterCastSessionStateListener(castId, callback);
391 SLOGD("AVRouter impl register callback finished");
392 return AVSESSION_SUCCESS;
393 }
394
UnRegisterCallback(int64_t castHandle,const std::shared_ptr<IAVCastSessionStateListener> callback)395 int32_t AVRouterImpl::UnRegisterCallback(int64_t castHandle,
396 const std::shared_ptr<IAVCastSessionStateListener> callback)
397 {
398 SLOGI("AVRouterImpl UnRegisterCallback IAVCastSessionStateListener callback to provider");
399 // The first 32 bits are providerId, the last 32 bits are castId
400 int32_t providerNumber = static_cast<uint64_t>(static_cast<uint64_t>(castHandle)) >> 32;
401 // The first 32 bits are providerId, the last 32 bits are castId
402 int32_t castId = static_cast<int32_t>((static_cast<uint64_t>(castHandle) << 32) >> 32);
403 CHECK_AND_RETURN_RET_LOG(providerManagerMap_.find(providerNumber) != providerManagerMap_.end(),
404 AVSESSION_ERROR, "Can not find corresponding provider");
405 CHECK_AND_RETURN_RET_LOG(providerManagerMap_[providerNumber] != nullptr
406 && providerManagerMap_[providerNumber]->provider_ != nullptr, AVSESSION_ERROR, "provider is nullptr");
407 providerManagerMap_[providerNumber]->provider_->UnRegisterCastSessionStateListener(castId, callback);
408 return AVSESSION_SUCCESS;
409 }
410 } // namespace OHOS::AVSession
411