• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 int32_t AVRouterImpl::Init(IAVSessionServiceListener *servicePtr)
34 {
35     SLOGI("Start init AVRouter");
36     {
37         std::lock_guard lockGuard(servicePtrLock_);
38         servicePtr_ = servicePtr;
39     }
40     castSessionListener_ = std::make_shared<CastSessionListener>(this);
41     hwProvider_ = std::make_shared<HwCastProvider>();
42     if (hwProvider_ != nullptr && hwProvider_->Init() != AVSESSION_ERROR) {
43         SLOGI("init pvd success");
44     } else {
45         hwProvider_ = nullptr;
46         SLOGE("init with null pvd to init");
47         return AVSESSION_ERROR;
48     }
49     providerNumber_ = providerNumberEnableDefault_;
50     std::shared_ptr<AVCastProviderManager> avCastProviderManager = std::make_shared<AVCastProviderManager>();
51     if (avCastProviderManager == nullptr) {
52         SLOGE("init with null manager");
53         return AVSESSION_ERROR;
54     }
55     avCastProviderManager->Init(providerNumber_, hwProvider_);
56     providerManagerMap_[providerNumber_] = avCastProviderManager;
57     if (hwProvider_ != nullptr) {
58         hwProvider_->RegisterCastStateListener(avCastProviderManager);
59     } else {
60         SLOGE("init with null pvd to registerlistener");
61         return AVSESSION_ERROR;
62     }
63     std::lock_guard lockGuard(providerManagerLock_);
64     if (cacheStartDiscovery_) {
65         SLOGI("cacheStartDiscovery check do discovery");
66         StartCastDiscovery(cacheCastDeviceCapability_, cacheDrmSchemes_);
67         cacheStartDiscovery_ = false;
68     }
69     SLOGI("init AVRouter done");
70     return AVSESSION_SUCCESS;
71 }
72 
Release()73 bool AVRouterImpl::Release()
74 {
75     SLOGI("Start Release AVRouter");
76     if (hasSessionAlive_) {
77         SLOGE("has session alive, but continue");
78     }
79     if (hwProvider_ == nullptr) {
80         SLOGE("Start Release AVRouter err for no provider");
81         return false;
82     }
83     std::lock_guard lockGuard(providerManagerLock_);
84 
85     if (hwProvider_ == nullptr) {
86         SLOGE("repeat check for no pvd");
87         return false;
88     }
89     SLOGI("repeat check for pvd alive");
90     hwProvider_->Release();
91     hwProvider_ = nullptr;
92     providerNumber_ = providerNumberDisable_;
93     providerManagerMap_.clear();
94     castHandleToInfoMap_.clear();
95     SLOGD("Release AVRouter done");
96     return false;
97 }
98 
StartDeviceLogging(int32_t fd,uint32_t maxSize)99 int32_t AVRouterImpl::StartDeviceLogging(int32_t fd, uint32_t maxSize)
100 {
101     SLOGI("AVRouterImpl StartDeviceLogging");
102     std::lock_guard lockGuard(providerManagerLock_);
103 
104     if (providerManagerMap_.empty()) {
105         cacheStartDeviceLogging_ = true;
106         return AVSESSION_SUCCESS;
107     }
108     for (const auto& [number, providerManager] : providerManagerMap_) {
109         CHECK_AND_RETURN_RET_LOG(providerManager != nullptr && providerManager->provider_ != nullptr,
110             AVSESSION_ERROR, "provider is nullptr");
111         providerManager->provider_->StartDeviceLogging(fd, maxSize);
112     }
113     return AVSESSION_SUCCESS;
114 }
115 
StopDeviceLogging()116 int32_t AVRouterImpl::StopDeviceLogging()
117 {
118     SLOGI("AVRouterImpl StopDeviceLogging");
119     std::lock_guard lockGuard(providerManagerLock_);
120 
121     if (cacheStartDeviceLogging_) {
122         SLOGI("clear cacheStartDeviceLogging_ when stop discovery");
123         cacheStartDeviceLogging_ = false;
124     }
125     for (const auto& [number, providerManager] : providerManagerMap_) {
126         CHECK_AND_RETURN_RET_LOG(providerManager != nullptr && providerManager->provider_ != nullptr,
127             AVSESSION_ERROR, "provider is nullptr");
128         providerManager->provider_->StopDeviceLogging();
129     }
130     return AVSESSION_SUCCESS;
131 }
132 
StartCastDiscovery(int32_t castDeviceCapability,std::vector<std::string> drmSchemes)133 int32_t AVRouterImpl::StartCastDiscovery(int32_t castDeviceCapability, std::vector<std::string> drmSchemes)
134 {
135     SLOGI("AVRouterImpl StartCastDiscovery");
136     std::lock_guard lockGuard(providerManagerLock_);
137 
138     if (providerManagerMap_.empty()) {
139         SLOGI("set cacheStartDiscovery with no element with cap %{public}d", static_cast<int>(castDeviceCapability));
140         cacheStartDiscovery_ = true;
141         cacheCastDeviceCapability_ = castDeviceCapability;
142         cacheDrmSchemes_ = drmSchemes;
143         return AVSESSION_SUCCESS;
144     }
145     for (const auto& [number, providerManager] : providerManagerMap_) {
146         CHECK_AND_RETURN_RET_LOG(providerManager != nullptr && providerManager->provider_ != nullptr,
147             AVSESSION_ERROR, "provider is nullptr");
148         providerManager->provider_->StartDiscovery(castDeviceCapability, drmSchemes);
149     }
150     return AVSESSION_SUCCESS;
151 }
152 
StopCastDiscovery()153 int32_t AVRouterImpl::StopCastDiscovery()
154 {
155     SLOGI("AVRouterImpl StopCastDiscovery");
156     std::lock_guard lockGuard(providerManagerLock_);
157 
158     if (cacheStartDiscovery_) {
159         SLOGI("clear cacheStartDiscovery when stop discovery");
160         cacheStartDiscovery_ = false;
161     }
162     for (const auto& [number, providerManager] : providerManagerMap_) {
163         CHECK_AND_RETURN_RET_LOG(providerManager != nullptr && providerManager->provider_ != nullptr,
164             AVSESSION_ERROR, "provider is nullptr");
165         providerManager->provider_->StopDiscovery();
166     }
167     return AVSESSION_SUCCESS;
168 }
169 
SetDiscoverable(const bool enable)170 int32_t AVRouterImpl::SetDiscoverable(const bool enable)
171 {
172     SLOGI("AVRouterImpl SetDiscoverable %{public}d", enable);
173 
174     std::lock_guard lockGuard(providerManagerLock_);
175 
176     for (const auto& [number, providerManager] : providerManagerMap_) {
177         CHECK_AND_RETURN_RET_LOG(providerManager != nullptr && providerManager->provider_ != nullptr,
178             AVSESSION_ERROR, "provider is nullptr");
179         providerManager->provider_->SetDiscoverable(enable);
180     }
181 
182     return AVSESSION_SUCCESS;
183 }
184 
OnDeviceAvailable(OutputDeviceInfo & castOutputDeviceInfo)185 int32_t AVRouterImpl::OnDeviceAvailable(OutputDeviceInfo& castOutputDeviceInfo)
186 {
187     SLOGI("AVRouterImpl received OnDeviceAvailable event");
188 
189     std::lock_guard lockGuard(servicePtrLock_);
190     if (servicePtr_ == nullptr) {
191         return ERR_SERVICE_NOT_EXIST;
192     }
193     servicePtr_->NotifyDeviceAvailable(castOutputDeviceInfo);
194     return AVSESSION_SUCCESS;
195 }
196 
OnDeviceLogEvent(const DeviceLogEventCode eventId,const int64_t param)197 int32_t AVRouterImpl::OnDeviceLogEvent(const DeviceLogEventCode eventId, const int64_t param)
198 {
199     SLOGI("AVRouterImpl received OnDeviceLogEvent event");
200 
201     std::lock_guard lockGuard(servicePtrLock_);
202     if (servicePtr_ == nullptr) {
203         return ERR_SERVICE_NOT_EXIST;
204     }
205     servicePtr_->NotifyDeviceLogEvent(eventId, param);
206     return AVSESSION_SUCCESS;
207 }
208 
ReleaseCurrentCastSession()209 void AVRouterImpl::ReleaseCurrentCastSession()
210 {
211     SLOGI("Start ReleaseCurrentCastSession");
212     std::lock_guard lockGuard(servicePtrLock_);
213     servicePtr_->ReleaseCastSession();
214 }
215 
OnCastSessionCreated(const int32_t castId)216 int32_t AVRouterImpl::OnCastSessionCreated(const int32_t castId)
217 {
218     SLOGI("AVRouterImpl On cast session created, cast id is %{public}d", castId);
219 
220     castHandleToInfoMap_.clear();
221     int64_t castHandle = -1;
222     CHECK_AND_RETURN_RET_LOG(providerManagerMap_.find(providerNumberEnableDefault_) !=
223         providerManagerMap_.end(), castHandle, "Can not find corresponding provider");
224     CHECK_AND_RETURN_RET_LOG((providerManagerMap_[providerNumberEnableDefault_] != nullptr) &&
225         (providerManagerMap_[providerNumberEnableDefault_]->provider_ != nullptr),
226         AVSESSION_ERROR, "provider is nullptr");
227     int64_t tempId = 1;
228     // The first 32 bits are providerId, the last 32 bits are castId
229     castHandle = static_cast<int64_t>((static_cast<uint64_t>(tempId) << 32) |
230         static_cast<const uint32_t>(castId));
231     CastHandleInfo castHandleInfo;
232     OutputDeviceInfo outputDeviceInfo;
233     castHandleInfo.outputDeviceInfo_ = outputDeviceInfo;
234     castHandleToInfoMap_[castHandle] = castHandleInfo;
235     {
236         std::lock_guard lockGuard(servicePtrLock_);
237         servicePtr_->CreateSessionByCast(castHandle);
238     }
239     return AVSESSION_SUCCESS;
240 }
241 
OnDeviceOffline(const std::string & deviceId)242 int32_t AVRouterImpl::OnDeviceOffline(const std::string& deviceId)
243 {
244     SLOGI("AVRouterImpl received OnDeviceOffline event");
245 
246     std::lock_guard lockGuard(servicePtrLock_);
247     if (servicePtr_ == nullptr) {
248         return ERR_SERVICE_NOT_EXIST;
249     }
250     servicePtr_->NotifyDeviceOffline(deviceId);
251     return AVSESSION_SUCCESS;
252 }
253 
OnCastServerDied(int32_t providerNumber)254 int32_t AVRouterImpl::OnCastServerDied(int32_t providerNumber)
255 {
256     SLOGI("AVRouterImpl received OnCastServerDied event %{public}d", providerNumber);
257     {
258         std::lock_guard lockGuard(servicePtrLock_);
259         if (servicePtr_ == nullptr) {
260             return ERR_SERVICE_NOT_EXIST;
261         }
262         servicePtr_->setInCast(false);
263     }
264     std::lock_guard lockGuard(providerManagerLock_);
265     hasSessionAlive_ = false;
266     providerNumber_ = providerNumberDisable_;
267     providerManagerMap_.clear();
268     castHandleToInfoMap_.clear();
269     return AVSESSION_SUCCESS;
270 }
271 
GetRemoteController(const int64_t castHandle)272 std::shared_ptr<IAVCastControllerProxy> AVRouterImpl::GetRemoteController(const int64_t castHandle)
273 {
274     SLOGI("AVRouterImpl start get remote controller process");
275 
276     // The first 32 bits are providerId, the last 32 bits are castId
277     int32_t providerNumber = static_cast<int32_t>(static_cast<const uint64_t>(castHandle) >> 32);
278     SLOGD("Get remote controller of provider %{public}d", providerNumber);
279     // The first 32 bits are providerId, the last 32 bits are castId
280     int32_t castId = static_cast<int32_t>((static_cast<const uint64_t>(castHandle) << 32) >> 32);
281     CHECK_AND_RETURN_RET_LOG(providerManagerMap_.find(providerNumber) != providerManagerMap_.end(),
282         nullptr, "Can not find corresponding provider");
283     CHECK_AND_RETURN_RET_LOG(providerManagerMap_[providerNumber] != nullptr &&
284         providerManagerMap_[providerNumber]->provider_ != nullptr, nullptr, "provider is nullptr");
285     for (const auto& [number, castHandleInfo] : castHandleToInfoMap_) {
286         if (number == castHandle && castHandleInfo.avCastControllerProxy_ != nullptr) {
287             return castHandleInfo.avCastControllerProxy_;
288         }
289     }
290 
291     if (castHandleToInfoMap_.find(castHandle) != castHandleToInfoMap_.end()) {
292         castHandleToInfoMap_[castHandle].avCastControllerProxy_ =
293             providerManagerMap_[providerNumber]->provider_->GetRemoteController(castId);
294     }
295     return providerManagerMap_[providerNumber]->provider_->GetRemoteController(castId);
296 }
297 
StartCast(const OutputDeviceInfo & outputDeviceInfo,std::pair<std::string,std::string> & serviceNameStatePair,std::string sessionId)298 int64_t AVRouterImpl::StartCast(const OutputDeviceInfo& outputDeviceInfo,
299     std::pair<std::string, std::string>& serviceNameStatePair, std::string sessionId)
300 {
301     SLOGI("AVRouterImpl start cast process");
302     castserviceNameStatePair_ = serviceNameStatePair;
303     int64_t castHandle = -1;
304     CHECK_AND_RETURN_RET_LOG(providerManagerMap_.find(outputDeviceInfo.deviceInfos_[0].providerId_) !=
305         providerManagerMap_.end(), castHandle, "Can not find corresponding provider");
306     CHECK_AND_RETURN_RET_LOG(providerManagerMap_[outputDeviceInfo.deviceInfos_[0].providerId_] != nullptr
307         && providerManagerMap_[outputDeviceInfo.deviceInfos_[0].providerId_]->provider_ != nullptr,
308         AVSESSION_ERROR, "provider is nullptr");
309     for (const auto& [number, castHandleInfo] : castHandleToInfoMap_) {
310         if (castHandleInfo.sessionId_ != sessionId && castHandleInfo.outputDeviceInfo_.deviceInfos_.size() > 0 &&
311             castHandleInfo.outputDeviceInfo_.deviceInfos_[0].deviceId_ == outputDeviceInfo.deviceInfos_[0].deviceId_) {
312             castHandleToInfoMap_[number].sessionId_ = sessionId;
313             return number;
314         }
315     }
316     int32_t castId = providerManagerMap_[outputDeviceInfo.deviceInfos_[0].
317         providerId_]->provider_->StartCastSession();
318     CHECK_AND_RETURN_RET_LOG(castId != AVSESSION_ERROR, AVSESSION_ERROR, "StartCast failed");
319     int64_t tempId = outputDeviceInfo.deviceInfos_[0].providerId_;
320     // The first 32 bits are providerId, the last 32 bits are castId
321     castHandle = static_cast<int64_t>((static_cast<uint64_t>(tempId) << 32) | static_cast<uint32_t>(castId));
322     hasSessionAlive_ = true;
323 
324     CastHandleInfo castHandleInfo;
325     castHandleInfo.sessionId_ = sessionId;
326     OutputDeviceInfo localDevice;
327     DeviceInfo localInfo;
328     localInfo.castCategory_ = AVCastCategory::CATEGORY_LOCAL;
329     localInfo.deviceId_ = "-1";
330     localInfo.deviceName_ = "LocalDevice";
331     localDevice.deviceInfos_.emplace_back(localInfo);
332     castHandleInfo.outputDeviceInfo_ = localDevice;
333     castHandleToInfoMap_[castHandle] = castHandleInfo;
334     return castHandle;
335 }
336 
AddDevice(const int32_t castId,const OutputDeviceInfo & outputDeviceInfo)337 int32_t AVRouterImpl::AddDevice(const int32_t castId, const OutputDeviceInfo& outputDeviceInfo)
338 {
339     SLOGI("AVRouterImpl AddDevice process");
340 
341     int64_t tempId = outputDeviceInfo.deviceInfos_[0].providerId_;
342     int64_t castHandle = static_cast<int64_t>((static_cast<uint64_t>(tempId) << 32) |
343         static_cast<uint32_t>(castId));
344     for (const auto& [number, castHandleInfo] : castHandleToInfoMap_) {
345         if (castHandle == number && castHandleInfo.outputDeviceInfo_.deviceInfos_.size() > 0 &&
346             castHandleInfo.outputDeviceInfo_.deviceInfos_[0].deviceId_ == outputDeviceInfo.deviceInfos_[0].deviceId_) {
347             return AVSESSION_SUCCESS;
348         }
349     }
350     bool ret = providerManagerMap_[outputDeviceInfo.deviceInfos_[0].providerId_]->provider_->AddCastDevice(castId,
351         outputDeviceInfo.deviceInfos_[0]);
352     SLOGI("AVRouterImpl AddDevice process with ret %{public}d", static_cast<int32_t>(ret));
353     if (ret && castHandleToInfoMap_.find(castHandle) != castHandleToInfoMap_.end()) {
354         castHandleToInfoMap_[castHandle].outputDeviceInfo_ = outputDeviceInfo;
355     }
356     return ret ? AVSESSION_SUCCESS : ERR_DEVICE_CONNECTION_FAILED;
357 }
358 
StopCast(const int64_t castHandle,bool continuePlay)359 int32_t AVRouterImpl::StopCast(const int64_t castHandle, bool continuePlay)
360 {
361     SLOGI("AVRouterImpl stop cast process");
362 
363     int32_t providerNumber = static_cast<int32_t>(static_cast<uint64_t>(castHandle) >> 32);
364     SLOGI("Stop cast, the provider number is %{public}d", providerNumber);
365     CHECK_AND_RETURN_RET_LOG(providerManagerMap_.find(providerNumber) != providerManagerMap_.end(),
366         castHandle, "Can not find corresponding provider");
367     // The first 32 bits are providerId, the last 32 bits are castId
368     int32_t castId = static_cast<int32_t>((static_cast<const uint64_t>(castHandle) << 32) >> 32);
369     SLOGI("Stop cast, the castId is %{public}d", castId);
370     CHECK_AND_RETURN_RET_LOG(castHandleToInfoMap_.find(castHandle) != castHandleToInfoMap_.end(),
371         AVSESSION_ERROR, "Can not find corresponding castHandle");
372     CHECK_AND_RETURN_RET_LOG(providerManagerMap_[providerNumber] != nullptr
373         && providerManagerMap_[providerNumber]->provider_ != nullptr, AVSESSION_ERROR, "provider is nullptr");
374     CHECK_AND_RETURN_RET_LOG(castHandleToInfoMap_[castHandle].outputDeviceInfo_.deviceInfos_.size() > 0,
375         AVSESSION_ERROR, "deviceInfos is empty");
376     providerManagerMap_[providerNumber]->provider_->RemoveCastDevice(castId,
377         castHandleToInfoMap_[castHandle].outputDeviceInfo_.deviceInfos_[0], continuePlay);
378     hasSessionAlive_ = false;
379     SLOGI("AVRouterImpl stop cast process remove device done");
380 
381     if (castHandleToInfoMap_.find(castHandle) != castHandleToInfoMap_.end()) {
382         OutputDeviceInfo localDevice;
383         DeviceInfo localInfo;
384         localInfo.castCategory_ = AVCastCategory::CATEGORY_LOCAL;
385         localInfo.deviceId_ = "-1";
386         localInfo.deviceName_ = "LocalDevice";
387         localDevice.deviceInfos_.emplace_back(localInfo);
388         castHandleToInfoMap_[castHandle].outputDeviceInfo_ = localDevice;
389     }
390     return AVSESSION_SUCCESS;
391 }
392 
StopCastSession(const int64_t castHandle)393 int32_t AVRouterImpl::StopCastSession(const int64_t castHandle)
394 {
395     SLOGI("AVRouterImpl stop cast session");
396 
397     int32_t providerNumber = static_cast<int32_t>(static_cast<uint64_t>(castHandle) >> 32);
398 
399     CHECK_AND_RETURN_RET_LOG(providerManagerMap_.find(providerNumber) != providerManagerMap_.end(),
400         castHandle, "Can not find corresponding provider");
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(castHandleToInfoMap_.find(castHandle) != castHandleToInfoMap_.end(),
404         AVSESSION_ERROR, "Can not find corresponding castHandle");
405     CHECK_AND_RETURN_RET_LOG(providerManagerMap_[providerNumber] != nullptr
406         && providerManagerMap_[providerNumber]->provider_ != nullptr, AVSESSION_ERROR, "provider is nullptr");
407     providerManagerMap_[providerNumber]->provider_->StopCastSession(castId);
408     hasSessionAlive_ = false;
409 
410     for (const auto& [number, castHandleInfo] : castHandleToInfoMap_) {
411         if (number == castHandle) {
412             castHandleToInfoMap_[number].avCastControllerProxy_ = nullptr;
413         }
414     }
415     return AVSESSION_SUCCESS;
416 }
417 
SetServiceAllConnectState(int64_t castHandle,DeviceInfo deviceInfo)418 int32_t AVRouterImpl::SetServiceAllConnectState(int64_t castHandle, DeviceInfo deviceInfo)
419 {
420     int64_t realCastHandle = castHandle == noMirrorCastHandle_ ? GetMirrorCastHandle() : castHandle;
421     if (castHandleToInfoMap_.find(realCastHandle) != castHandleToInfoMap_.end()) {
422         OutputDeviceInfo device;
423         device.deviceInfos_.emplace_back(deviceInfo);
424         castHandleToInfoMap_[realCastHandle].outputDeviceInfo_ = device;
425     }
426     CHECK_AND_RETURN_RET_LOG(providerManagerMap_.find(providerNumberEnableDefault_) != providerManagerMap_.end(),
427         AVSESSION_ERROR, "Can not find corresponding provider");
428     CHECK_AND_RETURN_RET_LOG(providerManagerMap_[providerNumberEnableDefault_] != nullptr
429         && providerManagerMap_[providerNumberEnableDefault_]->provider_ != nullptr,
430         AVSESSION_ERROR, "provider is nullptr");
431     providerManagerMap_[providerNumberEnableDefault_]->provider_->SetStreamState(castHandle, deviceInfo);
432     return AVSESSION_SUCCESS;
433 }
434 
GetRemoteNetWorkId(int64_t castHandle,std::string deviceId,std::string & networkId)435 int32_t AVRouterImpl::GetRemoteNetWorkId(int64_t castHandle, std::string deviceId, std::string &networkId)
436 {
437     int32_t providerNumber = static_cast<int32_t>(static_cast<uint64_t>(castHandle) >> 32);
438     int32_t castId = static_cast<int32_t>((static_cast<uint64_t>(castHandle) << 32) >> 32);
439     CHECK_AND_RETURN_RET_LOG(providerManagerMap_.find(providerNumber) != providerManagerMap_.end(),
440         AVSESSION_ERROR, "Can not find corresponding provider");
441     CHECK_AND_RETURN_RET_LOG(providerManagerMap_[providerNumber] != nullptr
442         && providerManagerMap_[providerNumber]->provider_ != nullptr, AVSESSION_ERROR, "provider is nullptr");
443     providerManagerMap_[providerNumber]->provider_->GetRemoteNetWorkId(castId, deviceId, networkId);
444     return AVSESSION_SUCCESS;
445 }
446 
RegisterCallback(int64_t castHandle,const std::shared_ptr<IAVRouterListener> callback,std::string sessionId,DeviceInfo deviceInfo)447 int32_t AVRouterImpl::RegisterCallback(int64_t castHandle, const std::shared_ptr<IAVRouterListener> callback,
448     std::string sessionId, DeviceInfo deviceInfo)
449 {
450     SLOGI("AVRouterImpl register IAVRouterListener callback to provider");
451     // The first 32 bits are providerId, the last 32 bits are castId
452     int32_t providerNumber = static_cast<int32_t>(static_cast<uint64_t>(castHandle) >> 32);
453     // The first 32 bits are providerId, the last 32 bits are castId
454     int32_t castId = static_cast<int32_t>((static_cast<uint64_t>(castHandle) << 32) >> 32);
455     CHECK_AND_RETURN_RET_LOG(providerManagerMap_.find(providerNumber) != providerManagerMap_.end(),
456         AVSESSION_ERROR, "Can not find corresponding provider");
457     CHECK_AND_RETURN_RET_LOG(providerManagerMap_[providerNumber] != nullptr
458         && providerManagerMap_[providerNumber]->provider_ != nullptr, AVSESSION_ERROR, "provider is nullptr");
459     if (GetMirrorCastHandle() == noMirrorCastHandle_) {
460         for (const auto& [number, castHandleInfo] : castHandleToInfoMap_) {
461             if (number == castHandle && castHandleInfo.outputDeviceInfo_.deviceInfos_.size() > 0 &&
462                 castHandleInfo.avRouterListener_ != nullptr) {
463                 SLOGI("trigger the OnCastStateChange for disconnected/connected avRouterListener");
464                 castHandleInfo.avRouterListener_->OnCastStateChange(disconnectStateFromCast_,
465                     castHandleInfo.outputDeviceInfo_.deviceInfos_[0], false);
466                 castHandleToInfoMap_[castHandle].avRouterListener_ = callback;
467                 callback->OnCastStateChange(connectStateFromCast_,
468                     castHandleInfo.outputDeviceInfo_.deviceInfos_[0], false);
469                 return AVSESSION_SUCCESS;
470             }
471         }
472         providerManagerMap_[providerNumber]->provider_->RegisterCastSessionStateListener(castId, castSessionListener_);
473         if (castHandleToInfoMap_.find(castHandle) != castHandleToInfoMap_.end()) {
474             castHandleToInfoMap_[castHandle].avRouterListener_ = callback;
475             castHandleToInfoMap_[castHandle].sessionId_ = sessionId;
476         }
477     } else {
478         if (castHandleToInfoMap_.find(castHandle) != castHandleToInfoMap_.end() &&
479             castHandleToInfoMap_[castHandle].avRouterListener_ == nullptr) {
480             providerManagerMap_[providerNumber]->provider_->RegisterCastSessionStateListener(castId,
481                 castSessionListener_);
482             OutputDeviceInfo outputDevice;
483             outputDevice.deviceInfos_.emplace_back(deviceInfo);
484             castHandleToInfoMap_[castHandle].outputDeviceInfo_ = outputDevice;
485             castHandleToInfoMap_[castHandle].avRouterListener_ = callback;
486             castHandleToInfoMap_[castHandle].sessionId_ = sessionId;
487         } else {
488             mirrorSessionMap_[sessionId] = callback;
489         }
490         callback->OnCastStateChange(connectStateFromCast_, deviceInfo, false);
491     }
492     SLOGD("AVRouter impl register callback finished");
493     return AVSESSION_SUCCESS;
494 }
495 
UnRegisterCallback(int64_t castHandle,const std::shared_ptr<IAVRouterListener> callback,std::string sessionId)496 int32_t AVRouterImpl::UnRegisterCallback(int64_t castHandle,
497     const std::shared_ptr<IAVRouterListener> callback, std::string sessionId)
498 {
499     SLOGI("AVRouterImpl UnRegisterCallback IAVRouterListener callback to provider");
500     // The first 32 bits are providerId, the last 32 bits are castId
501     int32_t providerNumber = static_cast<uint64_t>(static_cast<uint64_t>(castHandle)) >> 32;
502     // The first 32 bits are providerId, the last 32 bits are castId
503     int32_t castId = static_cast<int32_t>((static_cast<uint64_t>(castHandle) << 32) >> 32);
504     CHECK_AND_RETURN_RET_LOG(providerManagerMap_.find(providerNumber) != providerManagerMap_.end(),
505         AVSESSION_ERROR, "Can not find corresponding provider");
506     CHECK_AND_RETURN_RET_LOG(providerManagerMap_[providerNumber] != nullptr
507         && providerManagerMap_[providerNumber]->provider_ != nullptr, AVSESSION_ERROR, "provider is nullptr");
508     for (const auto& [number, castHandleInfo] : castHandleToInfoMap_) {
509         if (castHandleInfo.sessionId_ == sessionId && number == castHandle) {
510             providerManagerMap_[providerNumber]->provider_->UnRegisterCastSessionStateListener(castId,
511                 castSessionListener_);
512             castHandleToInfoMap_[number].avRouterListener_ = nullptr;
513         }
514     }
515     if (mirrorSessionMap_.find(sessionId) != mirrorSessionMap_.end()) {
516         mirrorSessionMap_.erase(sessionId);
517     }
518     return AVSESSION_SUCCESS;
519 }
520 
GetMirrorCastHandle()521 int64_t AVRouterImpl::GetMirrorCastHandle()
522 {
523     CHECK_AND_RETURN_RET_LOG(providerManagerMap_.find(providerNumberEnableDefault_) != providerManagerMap_.end(),
524         providerNumberEnableDefault_, "Can not find corresponding provider");
525     CHECK_AND_RETURN_RET_LOG(providerManagerMap_[providerNumberEnableDefault_] != nullptr &&
526         providerManagerMap_[providerNumberEnableDefault_]->provider_ != nullptr,
527         AVSESSION_ERROR, "provider is nullptr");
528     return providerManagerMap_[providerNumberEnableDefault_]->provider_->GetMirrorCastHandle();
529 }
530 
OnCastStateChange(int32_t castState,DeviceInfo deviceInfo)531 void AVRouterImpl::OnCastStateChange(int32_t castState, DeviceInfo deviceInfo)
532 {
533     for (const auto& [number, castHandleInfo] : castHandleToInfoMap_) {
534         if (castHandleInfo.avRouterListener_ != nullptr) {
535             SLOGI("trigger the OnCastStateChange for registered avRouterListener");
536             std::shared_ptr<IAVRouterListener> listener = castHandleInfo.avRouterListener_;
537             AVSessionEventHandler::GetInstance().AVSessionPostTask([listener, castState, deviceInfo]() {
538                 listener->OnCastStateChange(castState, deviceInfo, true);
539                 }, "OnCastStateChange", 0);
540             if (castState == disconnectStateFromCast_) {
541                 OutputDeviceInfo localDevice;
542                 DeviceInfo localInfo;
543                 localInfo.castCategory_ = AVCastCategory::CATEGORY_LOCAL;
544                 localInfo.deviceId_ = "-1";
545                 localInfo.deviceName_ = "LocalDevice";
546                 localDevice.deviceInfos_.emplace_back(localInfo);
547                 castHandleToInfoMap_[number].outputDeviceInfo_ = localDevice;
548             }
549         }
550     }
551     if (castState == disconnectStateFromCast_) {
552         servicePtr_->SetIsSupportMirrorToStream(false);
553     }
554 }
555 
OnCastEventRecv(int32_t errorCode,std::string & errorMsg)556 void AVRouterImpl::OnCastEventRecv(int32_t errorCode, std::string& errorMsg)
557 {
558     for (const auto& [number, castHandleInfo] : castHandleToInfoMap_) {
559         if (castHandleInfo.avRouterListener_ != nullptr) {
560             SLOGI("trigger the OnCastEventRecv for registered avRouterListener");
561             castHandleInfo.avRouterListener_->OnCastEventRecv(errorCode, errorMsg);
562         }
563     }
564 }
565 
DisconnectOtherSession(std::string sessionId,DeviceInfo deviceInfo)566 void AVRouterImpl::DisconnectOtherSession(std::string sessionId, DeviceInfo deviceInfo)
567 {
568     for (const auto& [string, avRouterListener] : mirrorSessionMap_) {
569         if (string != sessionId && avRouterListener != nullptr) {
570             avRouterListener->OnCastStateChange(disconnectStateFromCast_, deviceInfo, false);
571         }
572     }
573     for (const auto& [number, castHandleInfo] : castHandleToInfoMap_) {
574         if (castHandleInfo.sessionId_ != sessionId) {
575             if (castHandleInfo.avRouterListener_ != nullptr) {
576                 castHandleInfo.avRouterListener_->OnCastStateChange(disconnectStateFromCast_, deviceInfo, false);
577             }
578             castHandleToInfoMap_[number].sessionId_ = sessionId;
579             castHandleToInfoMap_[number].avRouterListener_ = mirrorSessionMap_[sessionId];
580         }
581     }
582     mirrorSessionMap_.clear();
583 }
584 } // namespace OHOS::AVSession
585