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 "hw_cast_provider.h"
17 #include <thread>
18 #include "cast_session_manager.h"
19 #include "hw_cast_stream_player.h"
20 #include "avsession_log.h"
21 #include "avsession_errors.h"
22 #include "avsession_event_handler.h"
23 #include "avsession_radar.h"
24
25 using namespace OHOS::CastEngine::CastEngineClient;
26 using namespace OHOS::CastEngine;
27
28 namespace OHOS::AVSession {
29 const uint32_t UNTRUSTED_DEVICE = 0;
30 const uint32_t TRUSTED_DEVICE = 1;
31
HwCastProvider()32 HwCastProvider::HwCastProvider()
33 {
34 SLOGD("pre construct the HwCastProvider");
35 std::lock_guard lockGuard(mutexLock_);
36 SLOGI("construct the HwCastProvider");
37 }
38
~HwCastProvider()39 HwCastProvider::~HwCastProvider()
40 {
41 SLOGD("pre destruct the HwCastProvider");
42 std::lock_guard lockGuard(mutexLock_);
43 SLOGI("destruct the HwCastProvider");
44 }
45
Init()46 int32_t HwCastProvider::Init()
47 {
48 SLOGD("pre init the HwCastProvider");
49 std::lock_guard lockGuard(mutexLock_);
50 int32_t ret = CastSessionManager::GetInstance().RegisterListener(shared_from_this());
51 SLOGI("Init the HwCastProvider %{public}d", ret);
52 return ret;
53 }
54
StartDeviceLogging(int32_t fd,uint32_t maxSize)55 int32_t HwCastProvider::StartDeviceLogging(int32_t fd, uint32_t maxSize)
56 {
57 SLOGI("start StartDeviceLogging, fd is %{public}d and maxSize is %{public}d", fd, maxSize);
58 return CastSessionManager::GetInstance().StartDeviceLogging(fd, maxSize);
59 }
60
StopDeviceLogging()61 int32_t HwCastProvider::StopDeviceLogging()
62 {
63 SLOGI("StopDeviceLogging");
64 return CastSessionManager::GetInstance().StartDeviceLogging(-1, 0);
65 }
66
StartDiscovery(int castCapability,std::vector<std::string> drmSchemes)67 bool HwCastProvider::StartDiscovery(int castCapability, std::vector<std::string> drmSchemes)
68 {
69 SLOGI("start discovery and the castCapability is %{public}d", castCapability);
70 castCapability = GetCastProtocolType(castCapability);
71 AVSessionRadarInfo info("HwCastProvider::StartDiscovery");
72 AVSessionRadar::GetInstance().StartCastDiscoveryBegin(info);
73 auto ret = CastSessionManager::GetInstance().StartDiscovery(castCapability, drmSchemes);
74 if (ret != 0) {
75 info.errorCode_ = ret;
76 AVSessionRadar::GetInstance().FailToStartCastDiscovery(info);
77 } else {
78 AVSessionRadar::GetInstance().StartCastDiscoveryEnd(info);
79 }
80 return ret;
81 }
82
StopDiscovery()83 void HwCastProvider::StopDiscovery()
84 {
85 SLOGI("stop discovery");
86 AVSessionRadarInfo info("HwCastProvider::StopDiscovery");
87 AVSessionRadar::GetInstance().StopCastDiscoveryBegin(info);
88 auto ret = CastSessionManager::GetInstance().StopDiscovery();
89 if (ret != 0) {
90 info.errorCode_ = ret;
91 AVSessionRadar::GetInstance().FailToStopCastDiscovery(info);
92 } else {
93 AVSessionRadar::GetInstance().StopCastDiscoveryEnd(info);
94 }
95 }
96
SetDiscoverable(const bool enable)97 int32_t HwCastProvider::SetDiscoverable(const bool enable)
98 {
99 SLOGI("SetDiscoverable in %{public}d", static_cast<int32_t>(enable));
100 return AVSESSION_SUCCESS;
101 }
102
Release()103 void HwCastProvider::Release()
104 {
105 SLOGI("cast provider release");
106 {
107 std::lock_guard lockGuard(mutexLock_);
108 hwCastProviderSessionMap_.clear();
109 avCastControllerMap_.clear();
110 castStateListenerList_.clear();
111 castFlag_.clear();
112 }
113 if (!isRelease_) {
114 SLOGI("release in with check pass");
115 isRelease_ = true;
116 } else {
117 SLOGW("already in release, check return");
118 return;
119 }
120 CastSessionManager::GetInstance().UnregisterListener();
121 SLOGD("provider release done");
122 }
123
StartCastSession(bool isHiStream)124 int HwCastProvider::StartCastSession(bool isHiStream)
125 {
126 SLOGI("StartCastSession begin");
127 CastSessionProperty property = {
128 isHiStream ? CastEngine::ProtocolType::CAST_PLUS_AUDIO : CastEngine::ProtocolType::CAST_PLUS_STREAM,
129 CastEngine::EndType::CAST_SOURCE};
130 std::shared_ptr<ICastSession> castSession = nullptr;
131 int ret = CastSessionManager::GetInstance().CreateCastSession(property, castSession);
132 if (ret != AVSESSION_SUCCESS) {
133 AVSessionRadarInfo info("HwCastProvider::StartCastSession");
134 info.errorCode_ = ret;
135 AVSessionRadar::GetInstance().FailToStartCast(info);
136 SLOGI("StartCastSession failed and return the ret is %{public}d", ret);
137 return AVSESSION_ERROR;
138 }
139 int castId;
140 {
141 SLOGI("StartCastSession pre check lock");
142 std::lock_guard lockGuard(mutexLock_);
143 SLOGI("StartCastSession check lock");
144 std::vector<bool>::iterator iter = find(castFlag_.begin(), castFlag_.end(), false);
145 if (iter == castFlag_.end()) {
146 SLOGE("StartCastSession failed");
147 return AVSESSION_ERROR;
148 }
149 *iter = true;
150 castId = iter - castFlag_.begin();
151 auto hwCastProviderSession = std::make_shared<HwCastProviderSession>(castSession);
152 if (hwCastProviderSession) {
153 if (hwCastProviderSession->Init() != AVSESSION_ERROR) {
154 SLOGI("CastSession init successed");
155 } else {
156 hwCastProviderSession->Release();
157 return AVSESSION_ERROR;
158 }
159 if (isHiStream) {
160 hwCastProviderSession->SetProtocolType(CastEngine::ProtocolType::CAST_PLUS_AUDIO);
161 }
162 }
163 hwCastProviderSessionMap_[castId] = hwCastProviderSession;
164 }
165 SLOGI("StartCastSession successed and return the castId is %{public}d", castId);
166
167 return castId;
168 }
StopCastSession(int castId)169 void HwCastProvider::StopCastSession(int castId)
170 {
171 SLOGI("StopCastSession begin");
172 std::lock_guard lockGuard(mutexLock_);
173 SLOGI("StopCastSession check lock");
174 auto hwCastStreamPlayer = avCastControllerMap_[castId];
175 if (hwCastStreamPlayer) {
176 hwCastStreamPlayer->Release();
177 }
178 avCastControllerMap_.erase(castId);
179 int32_t mirrorCastId = static_cast<int32_t>((static_cast<uint64_t>(mirrorCastHandle) << 32) >> 32);
180 if (castId == mirrorCastId) {
181 return;
182 }
183 if (hwCastProviderSessionMap_.find(castId) == hwCastProviderSessionMap_.end()) {
184 SLOGE("no need to release castSession for castId %{public}d is not exit in hwCastProviderSessionMap_", castId);
185 return;
186 }
187 auto hwCastProviderSession = hwCastProviderSessionMap_[castId];
188 if (hwCastProviderSession) {
189 hwCastProviderSession->Release();
190 }
191 hwCastProviderSessionMap_.erase(castId);
192 castFlag_[castId] = false;
193 }
194
AddCastDevice(int castId,DeviceInfo deviceInfo,uint32_t spid)195 bool HwCastProvider::AddCastDevice(int castId, DeviceInfo deviceInfo, uint32_t spid)
196 {
197 SLOGI("AddCastDevice with config castSession and corresonding castId is %{public}d", castId);
198 std::lock_guard lockGuard(mutexLock_);
199 SLOGI("add device check lock done");
200
201 if (hwCastProviderSessionMap_.find(castId) == hwCastProviderSessionMap_.end()) {
202 SLOGE("the castId corresonding to castSession is not exist");
203 return false;
204 }
205 auto hwCastProviderSession = hwCastProviderSessionMap_[castId];
206 if (!hwCastProviderSession) {
207 SLOGE("the castId corresonding to castSession is nullptr");
208 return false;
209 }
210
211 return hwCastProviderSession->AddDevice(deviceInfo.deviceId_, spid);
212 }
213
RemoveCastDevice(int castId,DeviceInfo deviceInfo,bool continuePlay)214 bool HwCastProvider::RemoveCastDevice(int castId, DeviceInfo deviceInfo, bool continuePlay)
215 {
216 SLOGI("RemoveCastDevice with config castSession and corresonding castId is %{public}d", castId);
217 std::lock_guard lockGuard(mutexLock_);
218 SLOGI("remove device check lock");
219 if (hwCastProviderSessionMap_.find(castId) == hwCastProviderSessionMap_.end()) {
220 SLOGE("the castId corresonding to castSession is not exist");
221 return false;
222 }
223 auto hwCastProviderSession = hwCastProviderSessionMap_[castId];
224 if (!hwCastProviderSession) {
225 SLOGE("the castId corresonding to castSession is nullptr");
226 return false;
227 }
228
229 return hwCastProviderSession->RemoveDevice(deviceInfo.deviceId_, continuePlay);
230 }
231
RegisterCastStateListener(std::shared_ptr<IAVCastStateListener> listener)232 bool HwCastProvider::RegisterCastStateListener(std::shared_ptr<IAVCastStateListener> listener)
233 {
234 SLOGI("RegisterCastStateListener in");
235 std::lock_guard lockGuard(mutexLock_);
236 SLOGI("RegisterCastStateListener in pass lock");
237 if (listener == nullptr) {
238 SLOGE("RegisterCastStateListener the listener is nullptr");
239 return false;
240 }
241 if (find(castStateListenerList_.begin(), castStateListenerList_.end(), listener) != castStateListenerList_.end()) {
242 SLOGE("RegisterCastStateListener the listener is already be registered");
243 return false;
244 }
245 SLOGI("RegisterCastStateListener successed, and save it in the castStateListenerList_");
246 castStateListenerList_.emplace_back(listener);
247
248 return true;
249 }
250
UnRegisterCastStateListener(std::shared_ptr<IAVCastStateListener> listener)251 bool HwCastProvider::UnRegisterCastStateListener(std::shared_ptr<IAVCastStateListener> listener)
252 {
253 SLOGI("UnRegisterCastStateListener in");
254 std::lock_guard lockGuard(mutexLock_);
255 SLOGI("UnRegisterCastStateListener in pass lock");
256 if (listener == nullptr) {
257 SLOGE("UnRegisterCastStateListener the listener is nullptr");
258 return false;
259 }
260 for (auto iter = castStateListenerList_.begin(); iter != castStateListenerList_.end();) {
261 if (*iter == listener) {
262 castStateListenerList_.erase(iter);
263 SLOGI("UnRegisterCastStateListener successed, and erase it from castStateListenerList_");
264 return true;
265 } else {
266 ++iter;
267 }
268 }
269 SLOGE("listener is not found in castStateListenerList_, so UnRegisterCastStateListener failed");
270
271 return false;
272 }
273
GetRemoteController(int castId)274 std::shared_ptr<IAVCastControllerProxy> HwCastProvider::GetRemoteController(int castId)
275 {
276 SLOGI("get remote controller with castId %{public}d", static_cast<int32_t>(castId));
277 std::lock_guard lockGuard(mutexLock_);
278 SLOGI("get remote controller check lock with castId %{public}d", static_cast<int32_t>(castId));
279 if (avCastControllerMap_.find(castId) != avCastControllerMap_.end()) {
280 SLOGI("the castId corresonding to streamPlayer is already exist");
281 return avCastControllerMap_[castId];
282 }
283 if (hwCastProviderSessionMap_.find(castId) == hwCastProviderSessionMap_.end()) {
284 SLOGE("No castSession corresonding to castId exists");
285 return nullptr;
286 }
287 auto hwCastProviderSession = hwCastProviderSessionMap_[castId];
288 if (hwCastProviderSession == nullptr) {
289 SLOGE("castSession corresonding to castId is nullptr");
290 return nullptr;
291 }
292 std::shared_ptr<IStreamPlayer> streamPlayer = hwCastProviderSession->CreateStreamPlayer();
293 std::shared_ptr<HwCastStreamPlayer> hwCastStreamPlayer = std::make_shared<HwCastStreamPlayer>(streamPlayer);
294 if (!hwCastStreamPlayer) {
295 SLOGE("the created hwCastStreamPlayer is nullptr");
296 return nullptr;
297 }
298 if (hwCastStreamPlayer->Init() == AVSESSION_ERROR) {
299 SLOGE("hwCastStreamPlayer init failed");
300 return nullptr;
301 }
302 avCastControllerMap_[castId] = hwCastStreamPlayer;
303 SLOGI("Create streamPlayer finished");
304 return hwCastStreamPlayer;
305 }
306
SetStreamState(int64_t castHandle,DeviceInfo deviceInfo)307 bool HwCastProvider::SetStreamState(int64_t castHandle, DeviceInfo deviceInfo)
308 {
309 int32_t castId = static_cast<int32_t>((static_cast<uint64_t>(castHandle) << 32) >> 32);
310 mirrorCastHandle = castHandle;
311 SLOGI("mirrorCastHandle is %{public}lld", static_cast<long long>(mirrorCastHandle));
312 if (hwCastProviderSessionMap_.find(castId) == hwCastProviderSessionMap_.end()) {
313 SLOGE("SetStreamState failed for the castSession corresponding to castId is not exit");
314 return false;
315 }
316 auto hwCastProviderSession = hwCastProviderSessionMap_[castId];
317 if (hwCastProviderSession == nullptr) {
318 SLOGE("SetStreamState failed for the hwCastProviderSession is nullptr");
319 return false;
320 }
321 return hwCastProviderSession->SetStreamState(deviceInfo);
322 }
323
GetRemoteNetWorkId(int32_t castId,std::string deviceId,std::string & networkId)324 bool HwCastProvider::GetRemoteNetWorkId(int32_t castId, std::string deviceId, std::string &networkId)
325 {
326 SLOGI("enter GetRemoteNetWorkId");
327 if (hwCastProviderSessionMap_.find(castId) == hwCastProviderSessionMap_.end()) {
328 SLOGE("GetRemoteNetWorkId failed for the castSession corresponding to castId is not exit");
329 return false;
330 }
331 auto hwCastProviderSession = hwCastProviderSessionMap_[castId];
332 if (hwCastProviderSession == nullptr) {
333 SLOGE("GetRemoteNetWorkId failed for the hwCastProviderSession is nullptr");
334 return false;
335 }
336 return hwCastProviderSession->GetRemoteNetWorkId(deviceId, networkId);
337 }
338
GetRemoteDrmCapabilities(int32_t castId,std::string deviceId,std::vector<std::string> & drmCapabilities)339 bool HwCastProvider::GetRemoteDrmCapabilities(int32_t castId, std::string deviceId,
340 std::vector<std::string> &drmCapabilities)
341 {
342 SLOGI("enter GetRemoteDrmCapabilities");
343 if (hwCastProviderSessionMap_.find(castId) == hwCastProviderSessionMap_.end()) {
344 SLOGE("GetRemoteDrmCapabilities failed for the castSession corresponding to castId is not exit");
345 return false;
346 }
347 auto hwCastProviderSession = hwCastProviderSessionMap_[castId];
348 if (hwCastProviderSession == nullptr) {
349 SLOGE("GetRemoteDrmCapabilities failed for the hwCastProviderSession is nullptr");
350 return false;
351 }
352 return hwCastProviderSession->GetRemoteDrmCapabilities(deviceId, drmCapabilities);
353 }
354
GetMirrorCastHandle()355 int64_t HwCastProvider::GetMirrorCastHandle()
356 {
357 return mirrorCastHandle;
358 }
359
RegisterCastSessionStateListener(int castId,std::shared_ptr<IAVCastSessionStateListener> listener)360 bool HwCastProvider::RegisterCastSessionStateListener(int castId,
361 std::shared_ptr<IAVCastSessionStateListener> listener)
362 {
363 SLOGD("RegisterCastSessionStateListener for castId %{public}d", castId);
364 if (listener == nullptr) {
365 SLOGE("RegisterCastSessionStateListener failed for the listener is nullptr");
366 return false;
367 }
368 std::lock_guard lockGuard(mutexLock_);
369 SLOGI("register castsession state listener check lock with castId %{public}d", static_cast<int32_t>(castId));
370 if (hwCastProviderSessionMap_.find(castId) == hwCastProviderSessionMap_.end()) {
371 SLOGE("RegisterCastSessionStateListener failed for the castSession corresponding to castId is not exit");
372 return false;
373 }
374 auto hwCastProviderSession = hwCastProviderSessionMap_[castId];
375 if (hwCastProviderSession == nullptr) {
376 SLOGE("RegisterCastSessionStateListener failed for the hwCastProviderSession is nullptr");
377 return false;
378 }
379
380 return hwCastProviderSession->RegisterCastSessionStateListener(listener);
381 }
382
UnRegisterCastSessionStateListener(int castId,std::shared_ptr<IAVCastSessionStateListener> listener)383 bool HwCastProvider::UnRegisterCastSessionStateListener(int castId,
384 std::shared_ptr<IAVCastSessionStateListener> listener)
385 {
386 if (listener == nullptr) {
387 SLOGE("UnRegisterCastSessionStateListener failed for the listener is nullptr");
388 return false;
389 }
390 std::lock_guard lockGuard(mutexLock_);
391 SLOGI("unregister castsession state listener check lock with castId %{public}d", static_cast<int32_t>(castId));
392 if (hwCastProviderSessionMap_.find(castId) == hwCastProviderSessionMap_.end()) {
393 SLOGE("UnRegisterCastSessionStateListener failed for the castSession corresponding to castId is not exit");
394 return false;
395 }
396 auto hwCastProviderSession = hwCastProviderSessionMap_[castId];
397 if (hwCastProviderSession == nullptr) {
398 SLOGE("UnRegisterCastSessionStateListener failed for the hwCastProviderSession is nullptr");
399 return false;
400 }
401
402 return hwCastProviderSession->UnRegisterCastSessionStateListener(listener);
403 }
404
ParsePullClients(const std::string & str)405 std::vector<uint32_t> HwCastProvider::ParsePullClients(const std::string& str)
406 {
407 std::vector<uint32_t> ret;
408
409 cJSON* array = cJSON_Parse(str.c_str());
410 if (array == nullptr) {
411 return ret;
412 }
413 if (cJSON_IsInvalid(array) || !cJSON_IsArray(array)) {
414 SLOGE("pullClients is invalid");
415 cJSON_Delete(array);
416 return ret;
417 }
418 cJSON* item;
419 cJSON_ArrayForEach(item, array) {
420 CHECK_AND_CONTINUE(item != nullptr && !cJSON_IsInvalid(item) &&
421 cJSON_IsNumber(item));
422 ret.push_back(static_cast<uint32_t>(item->valueint));
423 }
424 cJSON_Delete(array);
425 return ret;
426 }
427
OnDeviceFound(const std::vector<CastRemoteDevice> & deviceList)428 void HwCastProvider::OnDeviceFound(const std::vector<CastRemoteDevice> &deviceList)
429 {
430 std::vector<DeviceInfo> deviceInfoList;
431 if (deviceList.empty()) {
432 SLOGW("recv empty deviceList, return");
433 return;
434 }
435 SLOGI("get deviceList size %{public}zu", deviceList.size());
436 for (const CastRemoteDevice& castRemoteDevice : deviceList) {
437 SLOGI("get devices with deviceName %{public}s", castRemoteDevice.deviceName.c_str());
438 DeviceInfo deviceInfo;
439 deviceInfo.castCategory_ = AVCastCategory::CATEGORY_REMOTE;
440 deviceInfo.deviceId_ = castRemoteDevice.deviceId;
441 deviceInfo.deviceName_ = castRemoteDevice.deviceName;
442 deviceInfo.deviceType_ = static_cast<int>(castRemoteDevice.deviceType);
443 deviceInfo.ipAddress_ = castRemoteDevice.ipAddress;
444 deviceInfo.networkId_ = castRemoteDevice.networkId;
445 deviceInfo.manufacturer_ = castRemoteDevice.manufacturerName;
446 deviceInfo.modelName_ = castRemoteDevice.modelName;
447 deviceInfo.supportedProtocols_ = GetProtocolType(castRemoteDevice.protocolCapabilities);
448 deviceInfo.authenticationStatus_ = castRemoteDevice.isTrushed ? TRUSTED_DEVICE : UNTRUSTED_DEVICE;
449 deviceInfo.supportedDrmCapabilities_ = castRemoteDevice.drmCapabilities;
450 deviceInfo.isLegacy_ = castRemoteDevice.isLeagacy;
451 deviceInfo.mediumTypes_ = static_cast<int32_t>(castRemoteDevice.mediumTypes);
452 SLOGI("castRemoteDevice.streamCapability %{public}s", castRemoteDevice.streamCapability.c_str());
453 deviceInfo.supportedPullClients_ = ParsePullClients(castRemoteDevice.streamCapability);
454 deviceInfoList.emplace_back(deviceInfo);
455 }
456 for (auto listener : castStateListenerList_) {
457 if (listener != nullptr) {
458 SLOGI("trigger the OnDeviceAvailable for registered listeners");
459 listener->OnDeviceAvailable(deviceInfoList);
460 }
461 }
462 }
463
OnLogEvent(const int32_t eventId,const int64_t param)464 void HwCastProvider::OnLogEvent(const int32_t eventId, const int64_t param)
465 {
466 SLOGI("eventId is %{public}d, param is %{public}" PRId64, eventId, param);
467 std::lock_guard lockGuard(mutexLock_);
468 for (auto listener : castStateListenerList_) {
469 if (listener != nullptr) {
470 SLOGI("trigger the OnDeviceLogEvent for registered listeners");
471 if (eventId == DeviceLogEventCode::DEVICE_LOG_FULL) {
472 listener->OnDeviceLogEvent(DeviceLogEventCode::DEVICE_LOG_FULL, param);
473 } else {
474 listener->OnDeviceLogEvent(DeviceLogEventCode::DEVICE_LOG_EXCEPTION, param);
475 }
476 }
477 }
478 }
479
OnDeviceOffline(const std::string & deviceId)480 void HwCastProvider::OnDeviceOffline(const std::string& deviceId)
481 {
482 SLOGI("Received on device offline event");
483 for (auto listener : castStateListenerList_) {
484 if (listener != nullptr) {
485 SLOGI("trigger the OnDeviceOffline for registered listeners");
486 listener->OnDeviceOffline(deviceId);
487 }
488 }
489 }
490
OnSessionCreated(const std::shared_ptr<CastEngine::ICastSession> & castSession)491 void HwCastProvider::OnSessionCreated(const std::shared_ptr<CastEngine::ICastSession> &castSession)
492 {
493 SLOGI("Cast provider received session create event");
494 AVSessionEventHandler::GetInstance().AVSessionPostTask([this, castSession]() {
495 SLOGI("Cast pvd received session create event and create task thread");
496 for (auto listener : castStateListenerList_) {
497 listener->OnSessionNeedDestroy();
498 SLOGI("Cast pvd received session create event and session destroy check done");
499 }
500 int32_t castId;
501 {
502 std::lock_guard lockGuard(mutexLock_);
503 std::vector<bool>::iterator iter = find(castFlag_.begin(), castFlag_.end(), false);
504 if (iter == castFlag_.end()) {
505 SLOGE("Do not trigger callback due to the castFlag_ used up.");
506 return;
507 }
508 *iter = true;
509 castId = iter - castFlag_.begin();
510 SLOGI("Cast task thread to find flag");
511 }
512 auto hwCastProviderSession = std::make_shared<HwCastProviderSession>(castSession);
513 hwCastProviderSession->Init();
514 {
515 std::lock_guard lockGuard(mutexLock_);
516 hwCastProviderSessionMap_[castId] = hwCastProviderSession;
517 SLOGI("Cast task thread to create player");
518 std::shared_ptr<IStreamPlayer> streamPlayer = hwCastProviderSession->CreateStreamPlayer();
519 std::shared_ptr<HwCastStreamPlayer> hwCastStreamPlayer = std::make_shared<HwCastStreamPlayer>(streamPlayer);
520 hwCastStreamPlayer->Init();
521 avCastControllerMap_[castId] = hwCastStreamPlayer;
522 }
523 SLOGI("Create streamPlayer finished %{public}d", castId);
524 for (auto listener : castStateListenerList_) {
525 listener->OnSessionCreated(castId);
526 }
527 SLOGI("do session create notify finished %{public}d", castId);
528 }, "OnSessionCreated", 0);
529 }
530
OnServiceDied()531 void HwCastProvider::OnServiceDied()
532 {
533 for (auto listener : castStateListenerList_) {
534 if (listener != nullptr) {
535 SLOGI("trigger the OnServiceDied for registered listeners");
536 listener->OnCastServerDied();
537 }
538 }
539 }
540
GetProtocolType(uint32_t castProtocolType)541 int32_t HwCastProvider::GetProtocolType(uint32_t castProtocolType)
542 {
543 int32_t protocolType = (castProtocolType & ProtocolType::TYPE_CAST_PLUS_STREAM) |
544 (castProtocolType & ProtocolType::TYPE_DLNA) |
545 ((castProtocolType & static_cast<int>(CastEngine::ProtocolType::CAST_PLUS_AUDIO)) ?
546 ProtocolType::TYPE_CAST_PLUS_AUDIO : 0);
547 return protocolType;
548 }
549
GetCastProtocolType(int castCapability)550 int HwCastProvider::GetCastProtocolType(int castCapability)
551 {
552 uint32_t capability = static_cast<uint32_t>(castCapability);
553 int castProtocolType = static_cast<int>((capability & ProtocolType::TYPE_CAST_PLUS_MIRROR ?
554 static_cast<uint32_t>(CastEngine::ProtocolType::CAST_PLUS_MIRROR) : 0) |
555 (capability & ProtocolType::TYPE_CAST_PLUS_STREAM ?
556 static_cast<uint32_t>(CastEngine::ProtocolType::CAST_PLUS_STREAM) : 0) |
557 (capability & ProtocolType::TYPE_DLNA ? static_cast<uint32_t>(CastEngine::ProtocolType::DLNA) : 0) |
558 (capability & ProtocolType::TYPE_CAST_PLUS_AUDIO ?
559 static_cast<uint32_t>(CastEngine::ProtocolType::CAST_PLUS_AUDIO) : 0));
560 return castProtocolType;
561 }
562 } // namespace OHOS::AVSession
563