• 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 "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 "hw_cast_provider.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     SLOGI("init AVRouter done");
62 }
63 
Release()64 bool AVRouterImpl::Release()
65 {
66     SLOGI("Start Release AVRouter");
67     if (hasSessionAlive_) {
68         SLOGE("has session alive, return");
69         return true;
70     }
71     if (hwProvider_ == nullptr) {
72         SLOGE("Start Release AVRouter err for no provider");
73         return false;
74     }
75     std::lock_ground lockGuard(providerManagerLock_);
76 
77     if (hwProvider_ == nullptr) {
78         SLOGE("repeat check for no pvd");
79         return false;
80     }
81     SLOGI("repeat check for pvd alive");
82     hwProvider_->Release();
83     hwProvider_ = nullptr;
84     providerNumber_ = providerNumberDisable_;
85     providerManagerMap_.clear();
86     SLOGD("Release AVRouter done");
87     return false;
88 }
89 
StartCastDiscovery(int32_t castDeviceCapability)90 int32_t AVRouterImpl::StartCastDiscovery(int32_t castDeviceCapability)
91 {
92     SLOGI("AVRouterImpl StartCastDiscovery");
93 
94     std::lock_guard lockGuard(providerManagerLock_);
95 
96     for (const auto& [number, providerManager] : providerManagerMap_) {
97         CHECK_AND_RETURN_RET_LOG(providerManager != nullptr && providerManager->provider_ != nullptr,
98             AVSESSION_ERROR, "provider is nullptr");
99         providerManager->provider_->StartDiscovery(castDeviceCapability);
100     }
101     return AVSESSION_SUCCESS;
102 }
103 
StopCastDiscovery()104 int32_t AVRouterImpl::StopCastDiscovery()
105 {
106     SLOGI("AVRouterImpl StopCastDiscovery");
107 
108     std::lock_guard lockGuard(providerManagerLock_);
109 
110     for (const auto& [number, providerManager] : providerManagerMap_) {
111         CHECK_AND_RETURN_RET_LOG(providerManager != nullptr && providerManager->provider_ != nullptr,
112             AVSESSION_ERROR, "provider is nullptr");
113         providerManager->provider_->StopDiscovery();
114     }
115     return AVSESSION_SUCCESS;
116 }
117 
SetDiscoverable(const bool enable)118 int32_t AVRouterImpl::SetDiscoverable(const bool enable)
119 {
120     SLOGI("AVRouterImpl SetDiscoverable %{public}d", enable);
121 
122     std::lock_guard lockGuard(providerManagerLock_);
123 
124     for (const auto& [number, providerManager] : providerManagerMap_) {
125         CHECK_AND_RETURN_RET_LOG(providerManager != nullptr && providerManager->provider_ != nullptr,
126             AVSESSION_ERROR, "provider is nullptr");
127         providerManager->provider_->SetDiscoverable(enable);
128     }
129 
130     return AVSESSION_SUCCESS;
131 }
132 
OnDeviceAvailable(OutputDeviceInfo & castOutputDeviceInfo)133 int32_t AVRouterImpl::OnDeviceAvailable(OutputDeviceInfo& castOutputDeviceInfo)
134 {
135     SLOGI("AVRouterImpl received OnDeviceAvailable event");
136 
137     std::lock_guard lockGuard(servicePtrLock_);
138     if (servicePtr_ == nullptr) {
139         return ERR_SERVICE_NOT_EXIST;
140     }
141     servicePtr_->NotifyDeviceAvailable(castOutputDeviceInfo);
142     return AVSESSION_SUCCESS;
143 }
144 
ReleaseCurrentCastSession()145 void AVRouterImpl::ReleaseCurrentCastSession()
146 {
147     SLOGI("Start ReleaseCurrentCastSession");
148     std::lock_guard lockGuard(servicePtrLock_);
149     servicePtr_->ReleaseCastSession();
150 }
151 
OnCastSessionCreated(const int32_t castId)152 int32_t AVRouterImpl::OnCastSessionCreated(const int32_t castId)
153 {
154     SLOGI("AVRouterImpl On cast session created, cast id is %{public}d", castId);
155 
156     int64_t castHandle = -1;
157     CHECK_AND_RETURN_RET_LOG(providerManagerMap_.find(providerNumberEnableDefault_) !=
158         providerManagerMap_.end(), castHandle, "Can not find corresponding provider");
159     CHECK_AND_RETURN_RET_LOG(providerManagerMap_[1] != nullptr &&
160         providerManagerMap_[1]->provider_ ! = nullptr, AVSESSION_ERROR, "provider is nullptr");
161     int64_t tempId = 1;
162     castHandle = (tempId << 32) | castId; // The first 32 bits are providerId, the last 32 bits are castId
163     {
164         std::lock_guard lockGuard(servicePtrLock_);
165         servicePtr_->CreateSessionByCast(castHandle);
166     }
167     OutputDeviceInfo outputDeviceInfo;
168     castHandleToOutputDeviceMap_[castId] = outputDeviceInfo;
169     return AVSESSION_SUCCESS;
170 }
171 
OnDeviceOffline(const std::string & deviceId)172 int32_t AVRouterImpl::OnDeviceOffline(const std::string& deviceId)
173 {
174     SLOGI("AVRouterImpl received OnDeviceOffline event");
175 
176     std::lock_guard lockGuard(servicePtrLock_);
177     if (servicePtr_ == nullptr) {
178         return ERR_SERVICE_NOT_EXIST;
179     }
180     servicePtr_->NotifyDeviceOffline(deviceId);
181     return AVSESSION_SUCCESS;
182 }
183 
OnCastServerDied(int32_t providerNumber)184 int32_t AVRouterImpl::OnCastServerDied(int32_t providerNumber)
185 {
186     SLOGI("AVRouterImpl received OnCastServerDied event");
187     hasSessionAlive_ = false;
188     std::lock_guard lockGuard(providerManagerLock_);
189     if (providerManagerMap_.find(providerNumber) != providerManagerMap_.end()) {
190         providerManagerMap_.erase(providerNumber);
191     } else {
192         return AVSESSION_ERROR;
193     }
194     return AVSESSION_SUCCESS;
195 }
196 
GetRemoteController(const int64_t castHandle)197 std::shared_ptr<IAVCastControllerProxy> AVRouterImpl::GetRemoteController(const int64_t castHandle)
198 {
199     SLOGI("AVRouterImpl start get remote controller process");
200 
201     // The first 32 bits are providerId, the last 32 bits are castId
202     int32_t providerNumber = castHandle >> 32;
203     SLOGD("Get remote controller of provider %{public}d", providerNumber);
204     // The first 32 bits are providerId, the last 32 bits are castId
205     int32_t castId = static_cast<int32_t>((castHandle << 32) >> 32);
206     CHECK_AND_RETURN_RET_LOG(providerManagerMap_.find(providerNumber) != providerManagerMap_.end(),
207         nullptr, "Can not find corresponding provider");
208     CHECK_AND_RETURN_RET_LOG(providerManagerMap_[providerNumber] != nullptr &&
209         providerManagerMap_[providerNumber]->provider_ != nullptr, nullptr, "provider is nullptr");
210     return providerManagerMap_[providerNumber]->provider_->GetRemoteController(castId);
211 }
212 
StartCast(const OutputDeviceInfo & outputDeviceInfo)213 int64_t AVRouterImpl::StartCast(const OutputDeviceInfo& outputDeviceInfo)
214 {
215     SLOGI("AVRouterImpl start cast process");
216 
217     int64_t castHandle = -1;
218     CHECK_AND_RETURN_RET_LOG(providerManagerMap_.find(outputDeviceInfo.deviceInfos_[0].providerId_) !=
219         providerManagerMap_.end(), castHandle, "Can not find corresponding provider");
220     CHECK_AND_RETURN_RET_LOG(providerManagerMap_[outputDeviceInfo.deviceInfos_[0].providerId_] != nullptr
221         && providerManagerMap_[outputDeviceInfo.deviceInfos_[0].providerId_]->provider_ != nullptr,
222         AVSESSION_ERROR, "provider is nullptr");
223     int32_t castId = providerManagerMap_[outputDeviceInfo.deviceInfos_[0].providerId_]->provider_->StartCastSession();
224     int64_t tempId = outputDeviceInfo.deviceInfos_[0].providerId_;
225     castHandle = (tempId << 32) | castId; // The first 32 bits are providerId, the last 32 bits are castId
226     hasSessionAlive_ = true;
227 
228     return castHandle;
229 }
230 
AddDevice(const int32_t castId,const OutputDeviceInfo & outputDeviceInfo)231 int32_t AVRouterImpl::AddDevice(const int32_t castId, const OutputDeviceInfo& outputDeviceInfo)
232 {
233     SLOGI("AVRouterImpl AddDevice process");
234     bool ret = providerManagerMap_[outputDeviceInfo.deviceInfos_[0].providerId_]->provider_->AddCastDevice(castId,
235         outputDeviceInfo.deviceInfos_[0]);
236     SLOGI("AVRouterImpl AddDevice process with ret %{public}d", static_cast<int32_t>(ret));
237     castHandleToOutputDeviceMap_[castId] = outputDeviceInfo;
238     return ret ? AVSESSION_SUCCESS : ERR_DEVICE_CONNECTION_FAILED;
239 }
240 
StopCast(const int64_t castHandle)241 int32_t AVRouterImpl::StopCast(const int64_t castHandle)
242 {
243     SLOGI("AVRouterImpl stop cast process");
244 
245     int32_t providerNumber = static_cast<int32_t>(castHandle >> 32);
246     SLOGI("Stop cast, the provider number is %{public}d", providerNumber);
247     CHECK_AND_RETURN_RET_LOG(providerManagerMap_.find(providerNumber) != providerManagerMap_.end(),
248         castHandle, "Can not find corresponding provider");
249     // The first 32 bits are providerId, the last 32 bits are castId
250     int32_t castId = static_cast<int32_t>((castHandle << 32) >> 32);
251     SLOGI("Stop cast, the castId is %{public}d", castId);
252     CHECK_AND_RETURN_RET_LOG(castHandleToOutputDeviceMap_.find(castId) != castHandleToOutputDeviceMap_.end(),
253         AVSESSION_ERROR, "Can not find corresponding castId");
254     CHECK_AND_RETURN_RET_LOG(providerManagerMap_[providerNumber] != nullptr
255         && providerManagerMap_[providerNumber]->provider_ != nullptr, AVSESSION_ERROR, "provider is nullptr");
256     providerManagerMap_[providerNumber]->provider_->RemoveCastDevice(castId,
257         castHandleToOutputDeviceMap_[castId].deviceInfos_[0]);
258     hasSessionAlive_ = false;
259     SLOGI("AVRouterImpl stop cast process remove device done");
260 
261     return AVSESSION_SUCCESS;
262 }
263 
StopCastSession(const int64_t castHandle)264 int32_t AVRouterImpl::StopCastSession(const int64_t castHandle)
265 {
266     SLOGI("AVRouterImpl stop cast process");
267 
268     int32_t providerNumber = static_cast<int32_t>(castHandle >> 32);
269 
270     CHECK_AND_RETURN_RET_LOG(providerManagerMap_.find(providerNumber) != providerManagerMap_.end(),
271         castHandle, "Can not find corresponding provider");
272     // The first 32 bits are providerId, the last 32 bits are castId
273     int32_t castId = static_cast<int32_t>((castHandle << 32) >> 32);
274     CHECK_AND_RETURN_RET_LOG(castHandleToOutputDeviceMap_.find(castId) != castHandleToOutputDeviceMap_.end(),
275         AVSESSION_ERROR, "Can not find corresponding castId");
276     CHECK_AND_RETURN_RET_LOG(providerManagerMap_[providerNumber] != nullptr
277         && providerManagerMap_[providerNumber]->provider_ != nullptr, AVSESSION_ERROR, "provider is nullptr");
278     providerManagerMap_[providerNumber]->provider_->StopCastSession(castId);
279     hasSessionAlive_ = false;
280 
281     return AVSESSION_SUCCESS;
282 }
283 
RegisterCallback(int64_t castHandle,const std::shared_ptr<IAVCastSessionStateListener> callback)284 int32_t AVRouterImpl::RegisterCallback(int64_t castHandle, const std::shared_ptr<IAVCastSessionStateListener> callback)
285 {
286     SLOGI("AVRouterImpl register IAVCastSessionStateListener callback to provider");
287     // The first 32 bits are providerId, the last 32 bits are castId
288     int32_t providerNumber = castHandle >> 32;
289     // The first 32 bits are providerId, the last 32 bits are castId
290     int32_t castId = static_cast<int32_t>((castHandle << 32) >> 32);
291     CHECK_AND_RETURN_RET_LOG(providerManagerMap_.find(providerNumber) != providerManagerMap_.end(),
292         AVSESSION_ERROR, "Can not find corresponding provider");
293     CHECK_AND_RETURN_RET_LOG(providerManagerMap_[providerNumber] != nullptr
294         && providerManagerMap_[providerNumber]->provider_ != nullptr, AVSESSION_ERROR, "provider is nullptr");
295     providerManagerMap_[providerNumber]->provider_->RegisterCastSessionStateListener(castId, callback);
296     SLOGD("AVRouter impl register callback finished");
297     return AVSESSION_SUCCESS;
298 }
299 
UnRegisterCallback(int64_t castHandle,const std::shared_ptr<IAVCastSessionStateListener> callback)300 int32_t AVRouterImpl::UnRegisterCallback(int64_t castHandle,
301     const std::shared_ptr<IAVCastSessionStateListener> callback)
302 {
303     SLOGI("AVRouterImpl UnRegisterCallback IAVCastSessionStateListener callback to provider");
304     // The first 32 bits are providerId, the last 32 bits are castId
305     int32_t providerNumber = castHandle >> 32;
306     // The first 32 bits are providerId, the last 32 bits are castId
307     int32_t castId = static_cast<int32_t>((castHandle << 32) >> 32);
308     CHECK_AND_RETURN_RET_LOG(providerManagerMap_.find(providerNumber) != providerManagerMap_.end(),
309         AVSESSION_ERROR, "Can not find corresponding provider");
310     CHECK_AND_RETURN_RET_LOG(providerManagerMap_[providerNumber] != nullptr
311         && providerManagerMap_[providerNumber]->provider_ != nullptr, AVSESSION_ERROR, "provider is nullptr");
312     providerManagerMap_[providerNumber]->provider_->UnRegisterCastSessionStateListener(castId, callback);
313     return AVSESSION_SUCCESS;
314 }
315 } // namespace OHOS::AVSession
316