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 "hw_cast_provider.h"
24 #include "avsession_sysevent.h"
25
26 static std::shared_ptr<OHOS::AVSession::HwCastProvider> hwProvider_;
27
28 namespace OHOS::AVSession {
AVRouterImpl()29 AVRouterImpl::AVRouterImpl()
30 {
31 SLOGD("AVRouter construct");
32 }
33
Init(IAVSessionServiceListener *servicePtr)34 void AVRouterImpl::Init(IAVSessionServiceListener *servicePtr)
35 {
36 SLOGI("Start init AVRouter");
37 {
38 std::lock_guard lockGuard(servicePtrLock_);
39 servicePtr_ = servicePtr;
40 }
41 hwProvider_ = std::make_shared<HwCastProvider>();
42 if (hwProvider_ != nullptr) {
43 hwProvider_->Init();
44 } else {
45 SLOGE("init with null pvd to init");
46 return;
47 }
48 providerNumber_ = providerNumberEnableDefault_;
49 std::shared_ptr<AVCastProviderManager> avCastProviderManager = std::make_shared<AVCastProviderManager>();
50 if (avCastProviderManager == nullptr) {
51 SLOGE("init with null manager");
52 return;
53 }
54 avCastProviderManager->Init(providerNumber_, hwProvider_);
55 providerManagerMap_[providerNumber_] = avCastProviderManager;
56 if (hwProvider_ != nullptr) {
57 hwProvider_->RegisterCastStateListener(avCastProviderManager);
58 } else {
59 SLOGE("init with null pvd to registerlistener");
60 return;
61 }
62 if (cacheStartDiscovery_) {
63 SLOGI("cacheStartDiscovery check do discovery");
64 std::lock_guard lockGuard(providerManagerLock_);
65 StartCastDiscovery(cacheCastDeviceCapability_, cacheDrmSchemes_);
66 cacheStartDiscovery_ = false;
67 }
68 SLOGI("init AVRouter done");
69 }
70
Release()71 bool AVRouterImpl::Release()
72 {
73 SLOGI("Start Release AVRouter");
74 if (hasSessionAlive_) {
75 SLOGE("has session alive, but continue");
76 }
77 if (hwProvider_ == nullptr || (castServiceNameMapState_["HuaweiCast"] == deviceStateConnection ||
78 castServiceNameMapState_["HuaweiCast-Dual"] == deviceStateConnection)) {
79 SLOGE("Start Release AVRouter err for no provider");
80 return false;
81 }
82 std::lock_ground lockGuard(providerManagerLock_);
83
84 if (hwProvider_ == nullptr) {
85 SLOGE("repeat check for no pvd");
86 return false;
87 }
88 SLOGI("repeat check for pvd alive");
89 hwProvider_->Release();
90 hwProvider_ = nullptr;
91 providerNumber_ = providerNumberDisable_;
92 providerManagerMap_.clear();
93 SLOGD("Release AVRouter done");
94 return false;
95 }
96
StartDeviceLogging(int32_t fd, uint32_t maxSize)97 int32_t AVRouterImpl::StartDeviceLogging(int32_t fd, uint32_t maxSize)
98 {
99 SLOGI("AVRouterImpl StartDeviceLogging");
100 std::lock_guard lockGuard(providerManagerLock_);
101
102 if (providerManagerMap_.empty()) {
103 cacheStartDeviceLogging_ = true;
104 return AVSESSION_SUCCESS;
105 }
106 for (const auto& [number, providerManager] : providerManagerMap_) {
107 CHECK_AND_RETURN_RET_LOG(providerManager != nullptr && providerManager->provider_ != nullptr,
108 AVSESSION_ERROR, "provider is nullptr");
109 providerManager->provider_->StartDeviceLogging(fd, maxSize);
110 }
111 return AVSESSION_SUCCESS;
112 }
113
StopDeviceLogging()114 int32_t AVRouterImpl::StopDeviceLogging()
115 {
116 SLOGI("AVRouterImpl StopDeviceLogging");
117 std::lock_guard lockGuard(providerManagerLock_);
118
119 if (cacheStartDeviceLogging_) {
120 SLOGI("clear cacheStartDeviceLogging_ when stop discovery");
121 cacheStartDeviceLogging_ = false;
122 }
123 for (const auto& [number, providerManager] : providerManagerMap_) {
124 CHECK_AND_RETURN_RET_LOG(providerManager != nullptr && providerManager->provider_ != nullptr,
125 AVSESSION_ERROR, "provider is nullptr");
126 providerManager->provider_->StopDeviceLogging();
127 }
128 return AVSESSION_SUCCESS;
129 }
130
StartCastDiscovery(int32_t castDeviceCapability, std::vector<std::string> drmSchemes)131 int32_t AVRouterImpl::StartCastDiscovery(int32_t castDeviceCapability, std::vector<std::string> drmSchemes)
132 {
133 SLOGI("AVRouterImpl StartCastDiscovery");
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 std::lock_guard lockGuard(providerManagerLock_);
155
156 if (cacheStartDiscovery_) {
157 SLOGI("clear cacheStartDiscovery when stop discovery");
158 cacheStartDiscovery_ = false;
159 }
160 for (const auto& [number, providerManager] : providerManagerMap_) {
161 CHECK_AND_RETURN_RET_LOG(providerManager != nullptr && providerManager->provider_ != nullptr,
162 AVSESSION_ERROR, "provider is nullptr");
163 providerManager->provider_->StopDiscovery();
164 }
165 return AVSESSION_SUCCESS;
166 }
167
SetDiscoverable(const bool enable)168 int32_t AVRouterImpl::SetDiscoverable(const bool enable)
169 {
170 SLOGI("AVRouterImpl SetDiscoverable %{public}d", enable);
171
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
218 int64_t castHandle = -1;
219 CHECK_AND_RETURN_RET_LOG(providerManagerMap_.find(providerNumberEnableDefault_) !=
220 providerManagerMap_.end(), castHandle, "Can not find corresponding provider");
221 CHECK_AND_RETURN_RET_LOG(providerManagerMap_[1] != nullptr &&
222 providerManagerMap_[1]->provider_ ! = nullptr, AVSESSION_ERROR, "provider is nullptr");
223 int64_t tempId = 1;
224 // The first 32 bits are providerId, the last 32 bits are castId
225 castHandle = static_cast<int64_t>((static_cast<uint64_t>(tempId) << 32) |
226 static_cast<const uint32_t>(castId));
227 {
228 std::lock_guard lockGuard(servicePtrLock_);
229 servicePtr_->CreateSessionByCast(castHandle);
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].
289 providerId_]->provider_->StartCastSession();
290 int64_t tempId = outputDeviceInfo.deviceInfos_[0].providerId_;
291 // The first 32 bits are providerId, the last 32 bits are castId
292 castHandle = static_cast<int64_t>((static_cast<uint64_t>(tempId) << 32) | static_cast<uint32_t>(castId));
293 hasSessionAlive_ = true;
294
295 return castHandle;
296 }
297
AddDevice(const int32_t castId, const OutputDeviceInfo& outputDeviceInfo)298 int32_t AVRouterImpl::AddDevice(const int32_t castId, const OutputDeviceInfo& outputDeviceInfo)
299 {
300 SLOGI("AVRouterImpl AddDevice process");
301 bool ret = providerManagerMap_[outputDeviceInfo.deviceInfos_[0].providerId_]->provider_->AddCastDevice(castId,
302 outputDeviceInfo.deviceInfos_[0]);
303 SLOGI("AVRouterImpl AddDevice process with ret %{public}d", static_cast<int32_t>(ret));
304 castHandleToOutputDeviceMap_[castId] = outputDeviceInfo;
305 return ret ? AVSESSION_SUCCESS : ERR_DEVICE_CONNECTION_FAILED;
306 }
307
StopCast(const int64_t castHandle, int32_t removeTimes)308 int32_t AVRouterImpl::StopCast(const int64_t castHandle, int32_t removeTimes)
309 {
310 SLOGI("AVRouterImpl stop cast process");
311
312 int32_t providerNumber = static_cast<int32_t>(static_cast<uint64_t>(castHandle) >> 32);
313 SLOGI("Stop cast, the provider number is %{public}d", providerNumber);
314 CHECK_AND_RETURN_RET_LOG(providerManagerMap_.find(providerNumber) != providerManagerMap_.end(),
315 castHandle, "Can not find corresponding provider");
316 // The first 32 bits are providerId, the last 32 bits are castId
317 int32_t castId = static_cast<int32_t>((static_cast<const uint64_t>(castHandle) << 32) >> 32);
318 SLOGI("Stop cast, the castId is %{public}d, removeTimes is %{public}d", castId, removeTimes);
319 CHECK_AND_RETURN_RET_LOG(castHandleToOutputDeviceMap_.find(castId) != castHandleToOutputDeviceMap_.end(),
320 AVSESSION_ERROR, "Can not find corresponding castId");
321 CHECK_AND_RETURN_RET_LOG(providerManagerMap_[providerNumber] != nullptr
322 && providerManagerMap_[providerNumber]->provider_ != nullptr, AVSESSION_ERROR, "provider is nullptr");
323 CHECK_AND_RETURN_RET_LOG(castId != providerManagerMap_[providerNumber]->provider_->GetMirrorCastId() ||
324 removeTimes == 0, AVSESSION_ERROR, "cannot remove");
325 providerManagerMap_[providerNumber]->provider_->RemoveCastDevice(castId,
326 castHandleToOutputDeviceMap_[castId].deviceInfos_[0]);
327 hasSessionAlive_ = false;
328 SLOGI("AVRouterImpl stop cast process remove device done");
329
330 return AVSESSION_SUCCESS;
331 }
332
StopCastSession(const int64_t castHandle)333 int32_t AVRouterImpl::StopCastSession(const int64_t castHandle)
334 {
335 SLOGI("AVRouterImpl stop cast process");
336
337 int32_t providerNumber = static_cast<int32_t>(static_cast<uint64_t>(castHandle) >> 32);
338
339 CHECK_AND_RETURN_RET_LOG(providerManagerMap_.find(providerNumber) != providerManagerMap_.end(),
340 castHandle, "Can not find corresponding provider");
341 // The first 32 bits are providerId, the last 32 bits are castId
342 int32_t castId = static_cast<int32_t>((static_cast<uint64_t>(castHandle) << 32) >> 32);
343 castHandleToOutputDeviceMap_[castId] = castOutputDeviceInfo_;
344 CHECK_AND_RETURN_RET_LOG(castHandleToOutputDeviceMap_.find(castId) != castHandleToOutputDeviceMap_.end(),
345 AVSESSION_ERROR, "Can not find corresponding castId");
346 CHECK_AND_RETURN_RET_LOG(providerManagerMap_[providerNumber] != nullptr
347 && providerManagerMap_[providerNumber]->provider_ != nullptr, AVSESSION_ERROR, "provider is nullptr");
348 providerManagerMap_[providerNumber]->provider_->StopCastSession(castId);
349 hasSessionAlive_ = false;
350
351 return AVSESSION_SUCCESS;
352 }
353
SetServiceAllConnectState(int64_t castHandle, DeviceInfo deviceInfo)354 int32_t AVRouterImpl::SetServiceAllConnectState(int64_t castHandle, DeviceInfo deviceInfo)
355 {
356 int32_t providerNumber = static_cast<int32_t>(static_cast<uint64_t>(castHandle) >> 32);
357 int32_t castId = static_cast<int32_t>((static_cast<uint64_t>(castHandle) << 32) >> 32);
358 castOutputDeviceInfo_.deviceInfos_.emplace_back(deviceInfo);
359 castHandleToOutputDeviceMap_[castId] = castOutputDeviceInfo_;
360 CHECK_AND_RETURN_RET_LOG(providerManagerMap_.find(providerNumber) != providerManagerMap_.end(),
361 AVSESSION_ERROR, "Can not find corresponding provider");
362 CHECK_AND_RETURN_RET_LOG(providerManagerMap_[providerNumber] != nullptr
363 && providerManagerMap_[providerNumber]->provider_ != nullptr, AVSESSION_ERROR, "provider is nullptr");
364 providerManagerMap_[providerNumber]->provider_->SetStreamState(castId, deviceInfo);
365 return AVSESSION_SUCCESS;
366 }
367
GetRemoteNetWorkId(int64_t castHandle, std::string deviceId, std::string &networkId)368 int32_t AVRouterImpl::GetRemoteNetWorkId(int64_t castHandle, std::string deviceId, std::string &networkId)
369 {
370 int32_t providerNumber = static_cast<int32_t>(static_cast<uint64_t>(castHandle) >> 32);
371 int32_t castId = static_cast<int32_t>((static_cast<uint64_t>(castHandle) << 32) >> 32);
372 CHECK_AND_RETURN_RET_LOG(providerManagerMap_.find(providerNumber) != providerManagerMap_.end(),
373 AVSESSION_ERROR, "Can not find corresponding provider");
374 CHECK_AND_RETURN_RET_LOG(providerManagerMap_[providerNumber] != nullptr
375 && providerManagerMap_[providerNumber]->provider_ != nullptr, AVSESSION_ERROR, "provider is nullptr");
376 providerManagerMap_[providerNumber]->provider_->GetRemoteNetWorkId(castId, deviceId, networkId);
377 return AVSESSION_SUCCESS;
378 }
379
RegisterCallback(int64_t castHandle, const std::shared_ptr<IAVCastSessionStateListener> callback)380 int32_t AVRouterImpl::RegisterCallback(int64_t castHandle, const std::shared_ptr<IAVCastSessionStateListener> callback)
381 {
382 SLOGI("AVRouterImpl register IAVCastSessionStateListener callback to provider");
383 // The first 32 bits are providerId, the last 32 bits are castId
384 int32_t providerNumber = static_cast<int32_t>(static_cast<uint64_t>(castHandle) >> 32);
385 // The first 32 bits are providerId, the last 32 bits are castId
386 int32_t castId = static_cast<int32_t>((static_cast<uint64_t>(castHandle) << 32) >> 32);
387 CHECK_AND_RETURN_RET_LOG(providerManagerMap_.find(providerNumber) != providerManagerMap_.end(),
388 AVSESSION_ERROR, "Can not find corresponding provider");
389 CHECK_AND_RETURN_RET_LOG(providerManagerMap_[providerNumber] != nullptr
390 && providerManagerMap_[providerNumber]->provider_ != nullptr, AVSESSION_ERROR, "provider is nullptr");
391 providerManagerMap_[providerNumber]->provider_->RegisterCastSessionStateListener(castId, callback);
392 SLOGD("AVRouter impl register callback finished");
393 return AVSESSION_SUCCESS;
394 }
395
UnRegisterCallback(int64_t castHandle, const std::shared_ptr<IAVCastSessionStateListener> callback)396 int32_t AVRouterImpl::UnRegisterCallback(int64_t castHandle,
397 const std::shared_ptr<IAVCastSessionStateListener> callback)
398 {
399 SLOGI("AVRouterImpl UnRegisterCallback IAVCastSessionStateListener callback to provider");
400 // The first 32 bits are providerId, the last 32 bits are castId
401 int32_t providerNumber = static_cast<uint64_t>(static_cast<uint64_t>(castHandle)) >> 32;
402 // The first 32 bits are providerId, the last 32 bits are castId
403 int32_t castId = static_cast<int32_t>((static_cast<uint64_t>(castHandle) << 32) >> 32);
404 CHECK_AND_RETURN_RET_LOG(providerManagerMap_.find(providerNumber) != providerManagerMap_.end(),
405 AVSESSION_ERROR, "Can not find corresponding provider");
406 CHECK_AND_RETURN_RET_LOG(providerManagerMap_[providerNumber] != nullptr
407 && providerManagerMap_[providerNumber]->provider_ != nullptr, AVSESSION_ERROR, "provider is nullptr");
408 providerManagerMap_[providerNumber]->provider_->UnRegisterCastSessionStateListener(castId, callback);
409 return AVSESSION_SUCCESS;
410 }
411 } // namespace OHOS::AVSession
412