180922886Sopenharmony_ci/* 280922886Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd. 380922886Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 480922886Sopenharmony_ci * you may not use this file except in compliance with the License. 580922886Sopenharmony_ci * You may obtain a copy of the License at 680922886Sopenharmony_ci * 780922886Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 880922886Sopenharmony_ci * 980922886Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 1080922886Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 1180922886Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1280922886Sopenharmony_ci * See the License for the specific language governing permissions and 1380922886Sopenharmony_ci * limitations under the License. 1480922886Sopenharmony_ci */ 1580922886Sopenharmony_ci 1680922886Sopenharmony_ci#include "hw_cast_provider.h" 1780922886Sopenharmony_ci#include <thread> 1880922886Sopenharmony_ci#include "cast_session_manager.h" 1980922886Sopenharmony_ci#include "hw_cast_stream_player.h" 2080922886Sopenharmony_ci#include "avsession_log.h" 2180922886Sopenharmony_ci#include "avsession_errors.h" 2280922886Sopenharmony_ci#include "avsession_radar.h" 2380922886Sopenharmony_ci 2480922886Sopenharmony_ciusing namespace OHOS::CastEngine::CastEngineClient; 2580922886Sopenharmony_ciusing namespace OHOS::CastEngine; 2680922886Sopenharmony_ci 2780922886Sopenharmony_cinamespace OHOS::AVSession { 2880922886Sopenharmony_ciconst uint32_t UNTRUSTED_DEVICE = 0; 2980922886Sopenharmony_ciconst uint32_t TRUSTED_DEVICE = 1; 3080922886Sopenharmony_ci 3180922886Sopenharmony_ciHwCastProvider::~HwCastProvider() 3280922886Sopenharmony_ci{ 3380922886Sopenharmony_ci SLOGI("destruct the HwCastProvider"); 3480922886Sopenharmony_ci Release(); 3580922886Sopenharmony_ci} 3680922886Sopenharmony_ci 3780922886Sopenharmony_civoid HwCastProvider::Init() 3880922886Sopenharmony_ci{ 3980922886Sopenharmony_ci SLOGI("Init the HwCastProvider"); 4080922886Sopenharmony_ci CastSessionManager::GetInstance().RegisterListener(shared_from_this()); 4180922886Sopenharmony_ci} 4280922886Sopenharmony_ci 4380922886Sopenharmony_ciint32_t HwCastProvider::StartDeviceLogging(int32_t fd, uint32_t maxSize) 4480922886Sopenharmony_ci{ 4580922886Sopenharmony_ci SLOGI("start StartDeviceLogging, fd is %{public}d and maxSize is %{public}d", fd, maxSize); 4680922886Sopenharmony_ci return CastSessionManager::GetInstance().StartDeviceLogging(fd, maxSize); 4780922886Sopenharmony_ci} 4880922886Sopenharmony_ci 4980922886Sopenharmony_ciint32_t HwCastProvider::StopDeviceLogging() 5080922886Sopenharmony_ci{ 5180922886Sopenharmony_ci SLOGI("StopDeviceLogging"); 5280922886Sopenharmony_ci return CastSessionManager::GetInstance().StartDeviceLogging(-1, 0); 5380922886Sopenharmony_ci} 5480922886Sopenharmony_ci 5580922886Sopenharmony_cibool HwCastProvider::StartDiscovery(int castCapability, std::vector<std::string> drmSchemes) 5680922886Sopenharmony_ci{ 5780922886Sopenharmony_ci SLOGI("start discovery and the castCapability is %{public}d", castCapability); 5880922886Sopenharmony_ci AVSessionRadarInfo info("HwCastProvider::StartDiscovery"); 5980922886Sopenharmony_ci AVSessionRadar::GetInstance().StartCastDiscoveryBegin(info); 6080922886Sopenharmony_ci auto ret = CastSessionManager::GetInstance().StartDiscovery(castCapability, drmSchemes); 6180922886Sopenharmony_ci if (ret != 0) { 6280922886Sopenharmony_ci info.errorCode_ = ret; 6380922886Sopenharmony_ci AVSessionRadar::GetInstance().FailToStartCastDiscovery(info); 6480922886Sopenharmony_ci } else { 6580922886Sopenharmony_ci AVSessionRadar::GetInstance().StartCastDiscoveryEnd(info); 6680922886Sopenharmony_ci } 6780922886Sopenharmony_ci return ret; 6880922886Sopenharmony_ci} 6980922886Sopenharmony_ci 7080922886Sopenharmony_civoid HwCastProvider::StopDiscovery() 7180922886Sopenharmony_ci{ 7280922886Sopenharmony_ci SLOGI("stop discovery"); 7380922886Sopenharmony_ci AVSessionRadarInfo info("HwCastProvider::StopDiscovery"); 7480922886Sopenharmony_ci AVSessionRadar::GetInstance().StopCastDiscoveryBegin(info); 7580922886Sopenharmony_ci auto ret = CastSessionManager::GetInstance().StopDiscovery(); 7680922886Sopenharmony_ci if (ret != 0) { 7780922886Sopenharmony_ci info.errorCode_ = ret; 7880922886Sopenharmony_ci AVSessionRadar::GetInstance().FailToStopCastDiscovery(info); 7980922886Sopenharmony_ci } else { 8080922886Sopenharmony_ci AVSessionRadar::GetInstance().StopCastDiscoveryEnd(info); 8180922886Sopenharmony_ci } 8280922886Sopenharmony_ci} 8380922886Sopenharmony_ci 8480922886Sopenharmony_ciint32_t HwCastProvider::SetDiscoverable(const bool enable) 8580922886Sopenharmony_ci{ 8680922886Sopenharmony_ci SLOGI("SetDiscoverable in %{public}d", static_cast<int32_t>(enable)); 8780922886Sopenharmony_ci return CastSessionManager::GetInstance().SetDiscoverable(enable); 8880922886Sopenharmony_ci} 8980922886Sopenharmony_ci 9080922886Sopenharmony_civoid HwCastProvider::Release() 9180922886Sopenharmony_ci{ 9280922886Sopenharmony_ci SLOGI("Release the HwCastProvider"); 9380922886Sopenharmony_ci { 9480922886Sopenharmony_ci std::lock_guard lockGuard(mutexLock_); 9580922886Sopenharmony_ci hwCastProviderSessionMap_.clear(); 9680922886Sopenharmony_ci avCastControllerMap_.clear(); 9780922886Sopenharmony_ci castStateListenerList_.clear(); 9880922886Sopenharmony_ci castFlag_.clear(); 9980922886Sopenharmony_ci } 10080922886Sopenharmony_ci if (!isRelease_) { 10180922886Sopenharmony_ci SLOGI("release in with check pass"); 10280922886Sopenharmony_ci isRelease_ = true; 10380922886Sopenharmony_ci } else { 10480922886Sopenharmony_ci SLOGW("already in release, check return"); 10580922886Sopenharmony_ci return; 10680922886Sopenharmony_ci } 10780922886Sopenharmony_ci CastSessionManager::GetInstance().UnregisterListener(); 10880922886Sopenharmony_ci CastSessionManager::GetInstance().Release(); 10980922886Sopenharmony_ci SLOGD("Release done"); 11080922886Sopenharmony_ci} 11180922886Sopenharmony_ci 11280922886Sopenharmony_ciint HwCastProvider::StartCastSession() 11380922886Sopenharmony_ci{ 11480922886Sopenharmony_ci SLOGI("StartCastSession begin"); 11580922886Sopenharmony_ci CastSessionProperty property = {CastEngine::ProtocolType::CAST_PLUS_STREAM, CastEngine::EndType::CAST_SOURCE}; 11680922886Sopenharmony_ci std::shared_ptr<ICastSession> castSession = nullptr; 11780922886Sopenharmony_ci int ret = CastSessionManager::GetInstance().CreateCastSession(property, castSession); 11880922886Sopenharmony_ci if (ret != AVSESSION_SUCCESS) { 11980922886Sopenharmony_ci AVSessionRadarInfo info("HwCastProvider::StartCastSession"); 12080922886Sopenharmony_ci info.errorCode_ = ret; 12180922886Sopenharmony_ci AVSessionRadar::GetInstance().FailToStartCast(info); 12280922886Sopenharmony_ci } 12380922886Sopenharmony_ci int castId; 12480922886Sopenharmony_ci { 12580922886Sopenharmony_ci SLOGI("StartCastSession pre check lock"); 12680922886Sopenharmony_ci std::lock_guard lockGuard(mutexLock_); 12780922886Sopenharmony_ci SLOGI("StartCastSession check lock"); 12880922886Sopenharmony_ci std::vector<bool>::iterator iter = find(castFlag_.begin(), castFlag_.end(), false); 12980922886Sopenharmony_ci if (iter == castFlag_.end()) { 13080922886Sopenharmony_ci SLOGE("StartCastSession faileded"); 13180922886Sopenharmony_ci return AVSESSION_ERROR; 13280922886Sopenharmony_ci } 13380922886Sopenharmony_ci *iter = true; 13480922886Sopenharmony_ci castId = iter - castFlag_.begin(); 13580922886Sopenharmony_ci auto hwCastProviderSession = std::make_shared<HwCastProviderSession>(castSession); 13680922886Sopenharmony_ci if (hwCastProviderSession) { 13780922886Sopenharmony_ci hwCastProviderSession->Init(); 13880922886Sopenharmony_ci } 13980922886Sopenharmony_ci hwCastProviderSessionMap_[castId] = hwCastProviderSession; 14080922886Sopenharmony_ci } 14180922886Sopenharmony_ci SLOGI("StartCastSession successed and return the castId is %{public}d", castId); 14280922886Sopenharmony_ci 14380922886Sopenharmony_ci return castId; 14480922886Sopenharmony_ci} 14580922886Sopenharmony_ci} 14680922886Sopenharmony_ci 14780922886Sopenharmony_civoid HwCastProvider::StopCastSession(int castId) 14880922886Sopenharmony_ci{ 14980922886Sopenharmony_ci SLOGI("StopCastSession begin with %{public}d", castId); 15080922886Sopenharmony_ci std::lock_guard lockGuard(mutexLock_); 15180922886Sopenharmony_ci SLOGI("StopCastSession check lock"); 15280922886Sopenharmony_ci 15380922886Sopenharmony_ci auto hwCastStreamPlayer = avCastControllerMap_[castId]; 15480922886Sopenharmony_ci if (hwCastStreamPlayer) { 15580922886Sopenharmony_ci hwCastStreamPlayer->Release(); 15680922886Sopenharmony_ci } 15780922886Sopenharmony_ci 15880922886Sopenharmony_ci if (hwCastProviderSessionMap_.find(castId) == hwCastProviderSessionMap_.end()) { 15980922886Sopenharmony_ci SLOGE("no need to release castSession for castId %{public}d is not exit in hwCastProviderSessionMap_", castId); 16080922886Sopenharmony_ci return; 16180922886Sopenharmony_ci } 16280922886Sopenharmony_ci auto hwCastProviderSession = hwCastProviderSessionMap_[castId]; 16380922886Sopenharmony_ci if (hwCastProviderSession && castId != mirrorCastId) { 16480922886Sopenharmony_ci hwCastProviderSession->Release(); 16580922886Sopenharmony_ci } 16680922886Sopenharmony_ci if (castId != mirrorCastId) { 16780922886Sopenharmony_ci hwCastProviderSessionMap_.erase(castId); 16880922886Sopenharmony_ci castFlag_[castId] = false; 16980922886Sopenharmony_ci } 17080922886Sopenharmony_ci avCastControllerMap_.erase(castId); 17180922886Sopenharmony_ci} 17280922886Sopenharmony_ci 17380922886Sopenharmony_cibool HwCastProvider::AddCastDevice(int castId, DeviceInfo deviceInfo) 17480922886Sopenharmony_ci{ 17580922886Sopenharmony_ci SLOGI("AddCastDevice with config castSession and corresonding castId is %{public}d", castId); 17680922886Sopenharmony_ci std::lock_guard lockGuard(mutexLock_); 17780922886Sopenharmony_ci SLOGI("add device check lock done"); 17880922886Sopenharmony_ci 17980922886Sopenharmony_ci if (hwCastProviderSessionMap_.find(castId) == hwCastProviderSessionMap_.end()) { 18080922886Sopenharmony_ci SLOGE("the castId corresonding to castSession is not exist"); 18180922886Sopenharmony_ci return false; 18280922886Sopenharmony_ci } 18380922886Sopenharmony_ci auto hwCastProviderSession = hwCastProviderSessionMap_[castId]; 18480922886Sopenharmony_ci if (!hwCastProviderSession) { 18580922886Sopenharmony_ci SLOGE("the castId corresonding to castSession is nullptr"); 18680922886Sopenharmony_ci return false; 18780922886Sopenharmony_ci } 18880922886Sopenharmony_ci 18980922886Sopenharmony_ci return hwCastProviderSession->AddDevice(deviceInfo.deviceId_); 19080922886Sopenharmony_ci} 19180922886Sopenharmony_ci 19280922886Sopenharmony_cibool HwCastProvider::RemoveCastDevice(int castId, DeviceInfo deviceInfo) 19380922886Sopenharmony_ci{ 19480922886Sopenharmony_ci SLOGI("RemoveCastDevice with config castSession and corresonding castId is %{public}d", castId); 19580922886Sopenharmony_ci std::lock_guard lockGuard(mutexLock_); 19680922886Sopenharmony_ci SLOGI("remove device check lock"); 19780922886Sopenharmony_ci if (hwCastProviderSessionMap_.find(castId) == hwCastProviderSessionMap_.end()) { 19880922886Sopenharmony_ci SLOGE("the castId corresonding to castSession is not exist"); 19980922886Sopenharmony_ci return false; 20080922886Sopenharmony_ci } 20180922886Sopenharmony_ci auto hwCastProviderSession = hwCastProviderSessionMap_[castId]; 20280922886Sopenharmony_ci if (!hwCastProviderSession) { 20380922886Sopenharmony_ci SLOGE("the castId corresonding to castSession is nullptr"); 20480922886Sopenharmony_ci return false; 20580922886Sopenharmony_ci } 20680922886Sopenharmony_ci 20780922886Sopenharmony_ci return hwCastProviderSession->RemoveDevice(deviceInfo.deviceId_); 20880922886Sopenharmony_ci} 20980922886Sopenharmony_ci 21080922886Sopenharmony_cibool HwCastProvider::RegisterCastStateListener(std::shared_ptr<IAVCastStateListener> listener) 21180922886Sopenharmony_ci{ 21280922886Sopenharmony_ci SLOGI("RegisterCastStateListener in"); 21380922886Sopenharmony_ci std::lock_guard lockGuard(mutexLock_); 21480922886Sopenharmony_ci SLOGI("RegisterCastStateListener in pass lock"); 21580922886Sopenharmony_ci if (listener == nullptr) { 21680922886Sopenharmony_ci SLOGE("RegisterCastStateListener the listener is nullptr"); 21780922886Sopenharmony_ci return false; 21880922886Sopenharmony_ci } 21980922886Sopenharmony_ci if (find(castStateListenerList_.begin(), castStateListenerList_.end(), listener) != castStateListenerList_.end()) { 22080922886Sopenharmony_ci SLOGE("RegisterCastStateListener the listener is already be registered"); 22180922886Sopenharmony_ci return false; 22280922886Sopenharmony_ci } 22380922886Sopenharmony_ci SLOGI("RegisterCastStateListener successed, and save it in the castStateListenerList_"); 22480922886Sopenharmony_ci castStateListenerList_.emplace_back(listener); 22580922886Sopenharmony_ci 22680922886Sopenharmony_ci return true; 22780922886Sopenharmony_ci} 22880922886Sopenharmony_ci 22980922886Sopenharmony_cibool HwCastProvider::UnRegisterCastStateListener(std::shared_ptr<IAVCastStateListener> listener) 23080922886Sopenharmony_ci{ 23180922886Sopenharmony_ci SLOGI("UnRegisterCastStateListener in"); 23280922886Sopenharmony_ci std::lock_guard lockGuard(mutexLock_); 23380922886Sopenharmony_ci SLOGI("UnRegisterCastStateListener in pass lock"); 23480922886Sopenharmony_ci if (listener == nullptr) { 23580922886Sopenharmony_ci SLOGE("UnRegisterCastStateListener the listener is nullptr"); 23680922886Sopenharmony_ci return false; 23780922886Sopenharmony_ci } 23880922886Sopenharmony_ci for (auto iter = castStateListenerList_.begin(); iter != castStateListenerList_.end();) { 23980922886Sopenharmony_ci if (*iter == listener) { 24080922886Sopenharmony_ci castStateListenerList_.erase(iter); 24180922886Sopenharmony_ci SLOGI("UnRegisterCastStateListener successed, and erase it from castStateListenerList_"); 24280922886Sopenharmony_ci return true; 24380922886Sopenharmony_ci } else { 24480922886Sopenharmony_ci ++iter; 24580922886Sopenharmony_ci } 24680922886Sopenharmony_ci } 24780922886Sopenharmony_ci SLOGE("listener is not found in castStateListenerList_, so UnRegisterCastStateListener failed"); 24880922886Sopenharmony_ci 24980922886Sopenharmony_ci return false; 25080922886Sopenharmony_ci} 25180922886Sopenharmony_ci 25280922886Sopenharmony_cistd::shared_ptr<IAVCastControllerProxy> HwCastProvider::GetRemoteController(int castId) 25380922886Sopenharmony_ci{ 25480922886Sopenharmony_ci SLOGI("get remote controller with castId %{public}d", static_cast<int32_t>(castId)); 25580922886Sopenharmony_ci std::lock_guard lockGuard(mutexLock_); 25680922886Sopenharmony_ci SLOGI("get remote controller check lock with castId %{public}d", static_cast<int32_t>(castId)); 25780922886Sopenharmony_ci if (avCastControllerMap_.find(castId) != avCastControllerMap_.end()) { 25880922886Sopenharmony_ci SLOGI("the castId corresonding to streamPlayer is already exist"); 25980922886Sopenharmony_ci return avCastControllerMap_[castId]; 26080922886Sopenharmony_ci } 26180922886Sopenharmony_ci if (hwCastProviderSessionMap_.find(castId) == hwCastProviderSessionMap_.end()) { 26280922886Sopenharmony_ci SLOGE("No castSession corresonding to castId exists"); 26380922886Sopenharmony_ci return nullptr; 26480922886Sopenharmony_ci } 26580922886Sopenharmony_ci auto hwCastProviderSession = hwCastProviderSessionMap_[castId]; 26680922886Sopenharmony_ci if (hwCastProviderSession == nullptr) { 26780922886Sopenharmony_ci SLOGE("castSession corresonding to castId is nullptr"); 26880922886Sopenharmony_ci return nullptr; 26980922886Sopenharmony_ci } 27080922886Sopenharmony_ci std::shared_ptr<IStreamPlayer> streamPlayer = hwCastProviderSession->CreateStreamPlayer(); 27180922886Sopenharmony_ci std::shared_ptr<HwCastStreamPlayer> hwCastStreamPlayer = std::make_shared<HwCastStreamPlayer>(streamPlayer); 27280922886Sopenharmony_ci if (!hwCastStreamPlayer) { 27380922886Sopenharmony_ci SLOGE("the created hwCastStreamPlayer is nullptr"); 27480922886Sopenharmony_ci return nullptr; 27580922886Sopenharmony_ci } 27680922886Sopenharmony_ci hwCastStreamPlayer->Init(); 27780922886Sopenharmony_ci avCastControllerMap_[castId] = hwCastStreamPlayer; 27880922886Sopenharmony_ci SLOGI("Create streamPlayer finished"); 27980922886Sopenharmony_ci return hwCastStreamPlayer; 28080922886Sopenharmony_ci} 28180922886Sopenharmony_ci 28280922886Sopenharmony_cibool HwCastProvider::SetStreamState(int32_t castId, DeviceInfo deviceInfo) 28380922886Sopenharmony_ci{ 28480922886Sopenharmony_ci if (hwCastProviderSessionMap_.find(castId) == hwCastProviderSessionMap_.end()) { 28580922886Sopenharmony_ci SLOGE("SetStreamState failed for the castSession corresponding to castId is not exit"); 28680922886Sopenharmony_ci return false; 28780922886Sopenharmony_ci } 28880922886Sopenharmony_ci auto hwCastProviderSession = hwCastProviderSessionMap_[castId]; 28980922886Sopenharmony_ci if (hwCastProviderSession == nullptr) { 29080922886Sopenharmony_ci SLOGE("SetStreamState failed for the hwCastProviderSession is nullptr"); 29180922886Sopenharmony_ci return false; 29280922886Sopenharmony_ci } 29380922886Sopenharmony_ci mirrorCastId = castId; 29480922886Sopenharmony_ci SLOGI("mirrorCastId is %{public}d", mirrorCastId); 29580922886Sopenharmony_ci return hwCastProviderSession->SetStreamState(deviceInfo); 29680922886Sopenharmony_ci} 29780922886Sopenharmony_ci 29880922886Sopenharmony_cibool HwCastProvider::GetRemoteNetWorkId(int32_t castId, std::string deviceId, std::string &networkId) 29980922886Sopenharmony_ci{ 30080922886Sopenharmony_ci SLOGI("enter GetRemoteNetWorkId"); 30180922886Sopenharmony_ci if (hwCastProviderSessionMap_.find(castId) == hwCastProviderSessionMap_.end()) { 30280922886Sopenharmony_ci SLOGE("GetRemoteNetWorkId failed for the castSession corresponding to castId is not exit"); 30380922886Sopenharmony_ci return false; 30480922886Sopenharmony_ci } 30580922886Sopenharmony_ci auto hwCastProviderSession = hwCastProviderSessionMap_[castId]; 30680922886Sopenharmony_ci if (hwCastProviderSession == nullptr) { 30780922886Sopenharmony_ci SLOGE("GetRemoteNetWorkId failed for the hwCastProviderSession is nullptr"); 30880922886Sopenharmony_ci return false; 30980922886Sopenharmony_ci } 31080922886Sopenharmony_ci return hwCastProviderSession->GetRemoteNetWorkId(deviceId, networkId); 31180922886Sopenharmony_ci} 31280922886Sopenharmony_ci 31380922886Sopenharmony_ciint HwCastProvider::GetMirrorCastId() 31480922886Sopenharmony_ci{ 31580922886Sopenharmony_ci return mirrorCastId; 31680922886Sopenharmony_ci} 31780922886Sopenharmony_ci 31880922886Sopenharmony_cibool HwCastProvider::RegisterCastSessionStateListener(int castId, 31980922886Sopenharmony_ci std::shared_ptr<IAVCastSessionStateListener> listener) 32080922886Sopenharmony_ci{ 32180922886Sopenharmony_ci SLOGD("RegisterCastSessionStateListener for castId %{public}d", castId); 32280922886Sopenharmony_ci if (listener == nullptr) { 32380922886Sopenharmony_ci SLOGE("RegisterCastSessionStateListener failed for the listener is nullptr"); 32480922886Sopenharmony_ci return false; 32580922886Sopenharmony_ci } 32680922886Sopenharmony_ci std::lock_guard lockGuard(mutexLock_); 32780922886Sopenharmony_ci SLOGI("register castsession state listener check lock with castId %{public}d", static_cast<int32_t>(castId)); 32880922886Sopenharmony_ci if (hwCastProviderSessionMap_.find(castId) == hwCastProviderSessionMap_.end()) { 32980922886Sopenharmony_ci SLOGE("RegisterCastSessionStateListener failed for the castSession corresponding to castId is not exit"); 33080922886Sopenharmony_ci return false; 33180922886Sopenharmony_ci } 33280922886Sopenharmony_ci auto hwCastProviderSession = hwCastProviderSessionMap_[castId]; 33380922886Sopenharmony_ci if (hwCastProviderSession == nullptr) { 33480922886Sopenharmony_ci SLOGE("RegisterCastSessionStateListener failed for the hwCastProviderSession is nullptr"); 33580922886Sopenharmony_ci return false; 33680922886Sopenharmony_ci } 33780922886Sopenharmony_ci 33880922886Sopenharmony_ci return hwCastProviderSession->RegisterCastSessionStateListener(listener); 33980922886Sopenharmony_ci} 34080922886Sopenharmony_ci 34180922886Sopenharmony_cibool HwCastProvider::UnRegisterCastSessionStateListener(int castId, 34280922886Sopenharmony_ci std::shared_ptr<IAVCastSessionStateListener> listener) 34380922886Sopenharmony_ci{ 34480922886Sopenharmony_ci if (listener == nullptr) { 34580922886Sopenharmony_ci SLOGE("UnRegisterCastSessionStateListener failed for the listener is nullptr"); 34680922886Sopenharmony_ci return false; 34780922886Sopenharmony_ci } 34880922886Sopenharmony_ci std::lock_guard lockGuard(mutexLock_); 34980922886Sopenharmony_ci SLOGI("unregister castsession state listener check lock with castId %{public}d", static_cast<int32_t>(castId)); 35080922886Sopenharmony_ci if (hwCastProviderSessionMap_.find(castId) == hwCastProviderSessionMap_.end()) { 35180922886Sopenharmony_ci SLOGE("UnRegisterCastSessionStateListener failed for the castSession corresponding to castId is not exit"); 35280922886Sopenharmony_ci return false; 35380922886Sopenharmony_ci } 35480922886Sopenharmony_ci auto hwCastProviderSession = hwCastProviderSessionMap_[castId]; 35580922886Sopenharmony_ci if (hwCastProviderSession == nullptr) { 35680922886Sopenharmony_ci SLOGE("UnRegisterCastSessionStateListener failed for the hwCastProviderSession is nullptr"); 35780922886Sopenharmony_ci return false; 35880922886Sopenharmony_ci } 35980922886Sopenharmony_ci 36080922886Sopenharmony_ci return hwCastProviderSession->UnRegisterCastSessionStateListener(listener); 36180922886Sopenharmony_ci} 36280922886Sopenharmony_ci 36380922886Sopenharmony_ci 36480922886Sopenharmony_civoid HwCastProvider::OnDeviceFound(const std::vector<CastRemoteDevice> &deviceList) 36580922886Sopenharmony_ci{ 36680922886Sopenharmony_ci std::vector<DeviceInfo> deviceInfoList; 36780922886Sopenharmony_ci if (deviceList.empty()) { 36880922886Sopenharmony_ci SLOGW("recv empty deviceList, return"); 36980922886Sopenharmony_ci return; 37080922886Sopenharmony_ci } 37180922886Sopenharmony_ci SLOGI("get deviceList size %{public}zu", deviceList.size()); 37280922886Sopenharmony_ci for (CastRemoteDevice castRemoteDevice : deviceList) { 37380922886Sopenharmony_ci SLOGI("get devices with deviceName %{public}s", castRemoteDevice.deviceName.c_str()); 37480922886Sopenharmony_ci DeviceInfo deviceInfo; 37580922886Sopenharmony_ci deviceInfo.castCategory_ = AVCastCategory::CATEGORY_REMOTE; 37680922886Sopenharmony_ci deviceInfo.deviceId_ = castRemoteDevice.deviceId; 37780922886Sopenharmony_ci deviceInfo.deviceName_ = castRemoteDevice.deviceName; 37880922886Sopenharmony_ci deviceInfo.deviceType_ = static_cast<int>(castRemoteDevice.deviceType); 37980922886Sopenharmony_ci deviceInfo.ipAddress_ = castRemoteDevice.ipAddress; 38080922886Sopenharmony_ci deviceInfo.networkId_ = castRemoteDevice.networkId; 38180922886Sopenharmony_ci deviceInfo.manufacturer_ = castRemoteDevice.manufacturerName; 38280922886Sopenharmony_ci deviceInfo.modelName_ = castRemoteDevice.modelName; 38380922886Sopenharmony_ci deviceInfo.supportedProtocols_ = castPlusTypeToAVSessionType_ [castRemoteDevice.capability]; 38480922886Sopenharmony_ci deviceInfo.authenticationStatus_ = static_cast<int>(castRemoteDevice.subDeviceType) == 0 ? 38580922886Sopenharmony_ci TRUSTED_DEVICE : UNTRUSTED_DEVICE; 38680922886Sopenharmony_ci deviceInfo.supportedDrmCapabilities_ = castRemoteDevice.drmCapabilities; 38780922886Sopenharmony_ci deviceInfo.isLegacy_ = castRemoteDevice.isLeagacy; 38880922886Sopenharmony_ci deviceInfo.mediumTypes_ = static_cast<int32_t>(castRemoteDevice.mediumTypes); 38980922886Sopenharmony_ci deviceInfoList.emplace_back(deviceInfo); 39080922886Sopenharmony_ci } 39180922886Sopenharmony_ci for (auto listener : castStateListenerList_) { 39280922886Sopenharmony_ci if (listener != nullptr) { 39380922886Sopenharmony_ci SLOGI("trigger the OnDeviceAvailable for registered listeners"); 39480922886Sopenharmony_ci listener->OnDeviceAvailable(deviceInfoList); 39580922886Sopenharmony_ci } 39680922886Sopenharmony_ci } 39780922886Sopenharmony_ci} 39880922886Sopenharmony_ci 39980922886Sopenharmony_civoid HwCastProvider::OnLogEvent(const int32_t eventId, const int64_t param) 40080922886Sopenharmony_ci{ 40180922886Sopenharmony_ci SLOGI("eventId is %{public}d, param is %{public}ld", eventId, param); 40280922886Sopenharmony_ci std::lock_guard lockGuard(mutexLock_); 40380922886Sopenharmony_ci for (auto listener : castStateListenerList_) { 40480922886Sopenharmony_ci if (listener != nullptr) { 40580922886Sopenharmony_ci SLOGI("trigger the OnDeviceLogEvent for registered listeners"); 40680922886Sopenharmony_ci if (eventId == DeviceLogEventCode::DEVICE_LOG_FULL) { 40780922886Sopenharmony_ci listener->OnDeviceLogEvent(DeviceLogEventCode::DEVICE_LOG_FULL, param); 40880922886Sopenharmony_ci } else { 40980922886Sopenharmony_ci listener->OnDeviceLogEvent(DeviceLogEventCode::DEVICE_LOG_EXCEPTION, param); 41080922886Sopenharmony_ci } 41180922886Sopenharmony_ci } 41280922886Sopenharmony_ci } 41380922886Sopenharmony_ci} 41480922886Sopenharmony_ci 41580922886Sopenharmony_civoid HwCastProvider::OnDeviceOffline(const std::string& deviceId) 41680922886Sopenharmony_ci{ 41780922886Sopenharmony_ci SLOGI("Received on device offline event"); 41880922886Sopenharmony_ci for (auto listener : castStateListenerList_) { 41980922886Sopenharmony_ci if (listener != nullptr) { 42080922886Sopenharmony_ci SLOGI("trigger the OnDeviceOffline for registered listeners"); 42180922886Sopenharmony_ci listener->OnDeviceOffline(deviceId); 42280922886Sopenharmony_ci } 42380922886Sopenharmony_ci } 42480922886Sopenharmony_ci} 42580922886Sopenharmony_ci 42680922886Sopenharmony_civoid HwCastProvider::OnSessionCreated(const std::shared_ptr<CastEngine::ICastSession> &castSession) 42780922886Sopenharmony_ci{ 42880922886Sopenharmony_ci SLOGI("Cast provider received session create event"); 42980922886Sopenharmony_ci std::thread([this, castSession]() { 43080922886Sopenharmony_ci SLOGI("Cast pvd received session create event and create task thread"); 43180922886Sopenharmony_ci for (auto listener : castStateListenerList_) { 43280922886Sopenharmony_ci listener->OnSessionNeedDestroy(); 43380922886Sopenharmony_ci SLOGI("Cast pvd received session create event and session destroy check done"); 43480922886Sopenharmony_ci } 43580922886Sopenharmony_ci int32_t castId; 43680922886Sopenharmony_ci { 43780922886Sopenharmony_ci std::lock_guard lockGuard(mutexLock_); 43880922886Sopenharmony_ci std::vector<bool>::iterator iter = find(castFlag_.begin(), castFlag_.end(), false); 43980922886Sopenharmony_ci if (iter == castFlag_.end()) { 44080922886Sopenharmony_ci SLOGE("Do not trigger callback due to the castFlag_ used up."); 44180922886Sopenharmony_ci return; 44280922886Sopenharmony_ci } 44380922886Sopenharmony_ci *iter = true; 44480922886Sopenharmony_ci castId = iter - castFlag_.begin(); 44580922886Sopenharmony_ci SLOGI("Cast task thread to find flag"); 44680922886Sopenharmony_ci } 44780922886Sopenharmony_ci auto hwCastProviderSession = std::make_shared<HwCastProviderSession>(castSession); 44880922886Sopenharmony_ci hwCastProviderSession->Init(); 44980922886Sopenharmony_ci { 45080922886Sopenharmony_ci std::lock_guard lockGuard(mutexLock_); 45180922886Sopenharmony_ci hwCastProviderSessionMap_[castId] = hwCastProviderSession; 45280922886Sopenharmony_ci SLOGI("Cast task thread to create player"); 45380922886Sopenharmony_ci std::shared_ptr<IStreamPlayer> streamPlayer = hwCastProviderSession->CreateStreamPlayer(); 45480922886Sopenharmony_ci std::shared_ptr<HwCastStreamPlayer> hwCastStreamPlayer = std::make_shared<HwCastStreamPlayer>(streamPlayer); 45580922886Sopenharmony_ci hwCastStreamPlayer->Init(); 45680922886Sopenharmony_ci avCastControllerMap_[castId] = hwCastStreamPlayer; 45780922886Sopenharmony_ci } 45880922886Sopenharmony_ci SLOGI("Create streamPlayer finished %{public}d", castId); 45980922886Sopenharmony_ci for (auto listener : castStateListenerList_) { 46080922886Sopenharmony_ci listener->OnSessionCreated(castId); 46180922886Sopenharmony_ci } 46280922886Sopenharmony_ci SLOGI("do session create notify finished %{public}d", castId); 46380922886Sopenharmony_ci }).detach(); 46480922886Sopenharmony_ci} 46580922886Sopenharmony_ci 46680922886Sopenharmony_civoid HwCastProvider::OnServiceDied() 46780922886Sopenharmony_ci{ 46880922886Sopenharmony_ci for (auto listener : castStateListenerList_) { 46980922886Sopenharmony_ci if (listener != nullptr) { 47080922886Sopenharmony_ci SLOGI("trigger the OnServiceDied for registered listeners"); 47180922886Sopenharmony_ci listener->OnCastServerDied(); 47280922886Sopenharmony_ci } 47380922886Sopenharmony_ci } 47480922886Sopenharmony_ci} 47580922886Sopenharmony_ci} // namespace OHOS::AVSession 476