mirror of
https://gitee.com/openharmony/castengine_cast_framework
synced 2024-11-27 00:21:04 +00:00
!101 新需求:完善CastSessionManagerService和ConnectionManager类
Merge pull request !101 from LongestDistance/master
This commit is contained in:
commit
7bcfd324af
@ -45,6 +45,7 @@ public:
|
||||
|
||||
void OnStart() override;
|
||||
void OnStop() override;
|
||||
void OnActive(const SystemAbilityOnDemandReason& activeReason) override;
|
||||
|
||||
int32_t RegisterListener(sptr<ICastServiceListenerImpl> listener) override;
|
||||
int32_t UnregisterListener() override;
|
||||
@ -85,6 +86,8 @@ private:
|
||||
uid_t uid_;
|
||||
};
|
||||
|
||||
bool CheckAndWaitDevieManagerServiceInit();
|
||||
|
||||
pid_t myPid_;
|
||||
std::shared_mutex mutex_;
|
||||
std::map<pid_t, std::pair<sptr<ICastServiceListenerImpl>, uid_t>> listeners_;
|
||||
@ -95,6 +98,7 @@ private:
|
||||
std::atomic<int> sessionIndex_{ 0 };
|
||||
std::unordered_map<pid_t, sptr<IRemoteObject::DeathRecipient>> deathRecipientMap_;
|
||||
std::atomic<bool> hasServer_{ false };
|
||||
std::atomic<bool> isUnloading_{ false };
|
||||
|
||||
void AddClientDeathRecipientLocked(pid_t pid, uid_t uid, sptr<ICastServiceListenerImpl> listener);
|
||||
void RemoveClientDeathRecipientLocked(pid_t pid, sptr<ICastServiceListenerImpl> listener);
|
||||
@ -109,10 +113,10 @@ class ConnectionManagerListener : public IConnectionManagerListener {
|
||||
public:
|
||||
ConnectionManagerListener(sptr<CastSessionManagerService> service) : service_(service) {}
|
||||
int NotifySessionIsReady() override;
|
||||
void ReportSessionCreate(int castSessionId) override;
|
||||
bool NotifyRemoteDeviceIsReady(int castSessionId, const CastInnerRemoteDevice &device) override;
|
||||
void NotifyDeviceIsOffline(const std::string &deviceId) override;
|
||||
void GrabDevice(int32_t sessionId) override;
|
||||
void OnEvent(const std::string &deviceId, ReasonCode currentEventCode) override;
|
||||
int32_t GetSessionProtocolType(int sessionId, ProtocolType &protocolType) override;
|
||||
int32_t SetSessionProtocolType(int sessionId, ProtocolType protocolType) override;
|
||||
|
||||
|
@ -30,6 +30,7 @@
|
||||
#include "cast_engine_errors.h"
|
||||
#include "cast_engine_log.h"
|
||||
#include "cast_session_impl.h"
|
||||
#include "cast_session_impl_class.h"
|
||||
#include "connection_manager.h"
|
||||
#include "discovery_manager.h"
|
||||
#include "softbus_error_code.h"
|
||||
@ -141,6 +142,10 @@ bool SetupSessionServer()
|
||||
}
|
||||
}
|
||||
|
||||
namespace {
|
||||
constexpr int DEVICE_MANAGER_SA_ID = 4802;
|
||||
} // namespace
|
||||
|
||||
CastSessionManagerService::CastSessionManagerService(int32_t saId, bool runOnCreate) : SystemAbility(saId, runOnCreate)
|
||||
{
|
||||
CLOGD("construction in");
|
||||
@ -186,6 +191,12 @@ void CastSessionManagerService::OnStop()
|
||||
RemoveSessionServer(PKG_NAME, SessionServer::SESSION_NAME);
|
||||
}
|
||||
|
||||
void CastSessionManagerService::OnActive(const SystemAbilityOnDemandReason& activeReason)
|
||||
{
|
||||
CLOGI("OnActive in, reasonValue:%{public}s", activeReason.GetValue().c_str());
|
||||
isUnloading_.store(false);
|
||||
}
|
||||
|
||||
namespace {
|
||||
using namespace OHOS::DistributedHardware;
|
||||
constexpr int AV_SESSION_UID = 6700;
|
||||
@ -238,12 +249,27 @@ int ConnectionManagerListener::NotifySessionIsReady()
|
||||
return INVALID_ID;
|
||||
}
|
||||
|
||||
service->ReportSessionCreate(session);
|
||||
std::string sessionId{};
|
||||
session->GetSessionId(sessionId);
|
||||
return Utils::StringToInt((sessionId));
|
||||
}
|
||||
|
||||
void ConnectionManagerListener::ReportSessionCreate(int castSessionId)
|
||||
{
|
||||
auto service = service_.promote();
|
||||
if (!service) {
|
||||
CLOGE("service is null");
|
||||
return;
|
||||
}
|
||||
|
||||
sptr<ICastSessionImpl> castSession;
|
||||
if (service->GetCastSession(std::to_string(castSessionId), castSession) == CAST_ENGINE_SUCCESS) {
|
||||
service->ReportSessionCreate(castSession);
|
||||
return;
|
||||
}
|
||||
CLOGE("Invalid castSessionId: %d.", castSessionId);
|
||||
}
|
||||
|
||||
bool ConnectionManagerListener::NotifyRemoteDeviceIsReady(int castSessionId, const CastInnerRemoteDevice &device)
|
||||
{
|
||||
auto service = service_.promote();
|
||||
@ -251,16 +277,20 @@ bool ConnectionManagerListener::NotifyRemoteDeviceIsReady(int castSessionId, con
|
||||
CLOGE("service is null");
|
||||
return false;
|
||||
}
|
||||
|
||||
sptr<ICastSessionImpl> session;
|
||||
service->GetCastSession(std::to_string(castSessionId), session);
|
||||
if (session == nullptr) {
|
||||
CLOGE("Session is null when consultation data comes!");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!session->AddDevice(device)) {
|
||||
CLOGE("Session addDevice fail");
|
||||
return false;
|
||||
}
|
||||
|
||||
ConnectionManager::GetInstance().NotifyConnectStage(device, ConnectStageResult::CONNECT_START);
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -275,29 +305,6 @@ void ConnectionManagerListener::NotifyDeviceIsOffline(const std::string &deviceI
|
||||
CLOGD("OnDeviceOffline out");
|
||||
}
|
||||
|
||||
void ConnectionManagerListener::OnEvent(const std::string &deviceId, ReasonCode currentEventCode)
|
||||
{
|
||||
auto service = service_.promote();
|
||||
if (!service) {
|
||||
CLOGE("service is null");
|
||||
return;
|
||||
}
|
||||
|
||||
int sessionId = CastDeviceDataManager::GetInstance().GetSessionIdByDeviceId(deviceId);
|
||||
if (sessionId == INVALID_ID) {
|
||||
CLOGE("The obtained sessionId is null");
|
||||
return;
|
||||
}
|
||||
|
||||
sptr<ICastSessionImpl> session;
|
||||
service->GetCastSession(std::to_string(sessionId), session);
|
||||
if (!session) {
|
||||
CLOGE("The session is null. Failed to obtain the session.");
|
||||
return;
|
||||
}
|
||||
session->OnSessionEvent(deviceId, currentEventCode);
|
||||
}
|
||||
|
||||
void ConnectionManagerListener::GrabDevice(int32_t sessionId)
|
||||
{
|
||||
auto service = service_.promote();
|
||||
@ -372,6 +379,12 @@ int32_t CastSessionManagerService::RegisterListener(sptr<ICastServiceListenerImp
|
||||
}
|
||||
|
||||
if (needInitMore) {
|
||||
if (!CheckAndWaitDevieManagerServiceInit()) {
|
||||
CLOGE("Wait DM SA load timeout");
|
||||
ReleaseLocked();
|
||||
return CAST_ENGINE_ERROR;
|
||||
}
|
||||
|
||||
DiscoveryManager::GetInstance().Init(std::make_shared<DiscoveryManagerListener>(this));
|
||||
ConnectionManager::GetInstance().Init(std::make_shared<ConnectionManagerListener>(this));
|
||||
sessionMap_.clear();
|
||||
@ -519,6 +532,7 @@ bool CastSessionManagerService::DestroyCastSession(int32_t sessionId)
|
||||
sessionMap_.erase(it);
|
||||
}
|
||||
|
||||
ConnectionManager::GetInstance().RemoveSessionListener(sessionId);
|
||||
ConnectionManager::GetInstance().UpdateGrabState(false, -1);
|
||||
session->Stop();
|
||||
CLOGD("Session refcount is %d, session count:%zu", session->GetSptrRefCount(), sessionMap_.size());
|
||||
@ -744,6 +758,26 @@ void CastSessionManagerService::ReportDeviceOffline(const std::string &deviceId)
|
||||
}
|
||||
}
|
||||
|
||||
bool CastSessionManagerService::CheckAndWaitDevieManagerServiceInit()
|
||||
{
|
||||
auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
|
||||
if (samgr == nullptr) {
|
||||
CLOGE("get samgr failed");
|
||||
return false;
|
||||
}
|
||||
constexpr int32_t sleepTimeMs = 50;
|
||||
constexpr int32_t retryTimes = 200;
|
||||
int32_t retryTime = 0;
|
||||
|
||||
while (samgr->CheckSystemAbility(DEVICE_MANAGER_SA_ID) == nullptr && (retryTime < retryTimes)) {
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(sleepTimeMs));
|
||||
retryTime++;
|
||||
}
|
||||
CLOGI("retryTime:%{public}d", retryTime);
|
||||
|
||||
return retryTime < retryTimes;
|
||||
}
|
||||
|
||||
void CastSessionManagerService::CastEngineClientDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &object)
|
||||
{
|
||||
CLOGI("Client died, need release resources, client pid_: %{public}d", pid_);
|
||||
|
@ -83,6 +83,7 @@ public:
|
||||
void DisconnectDevice(const std::string &deviceId);
|
||||
|
||||
bool UpdateDeviceState(const std::string &deviceId, RemoteDeviceState state);
|
||||
void UpdateGrabState(bool changeState, int32_t sessionId);
|
||||
|
||||
int GetProtocolType() const;
|
||||
void SetProtocolType(int protocols);
|
||||
@ -91,9 +92,9 @@ public:
|
||||
void NotifySessionIsReady(int transportId);
|
||||
void NotifyDeviceIsOffline(const std::string &deviceId);
|
||||
bool NotifyConnectStage(const CastInnerRemoteDevice &device, int result, int32_t reasonCode = REASON_DEFAULT);
|
||||
void ReportErrorByListener(const std::string &deviceId, ReasonCode currentEventCode);
|
||||
void UpdateGrabState(bool changeState, int32_t sessionId);
|
||||
void SetSessionListener(std::shared_ptr<IConnectManagerSessionListener> listener);
|
||||
|
||||
void AddSessionListener(int castSessionId, std::shared_ptr<IConnectManagerSessionListener> listener);
|
||||
void RemoveSessionListener(int castSessionId);
|
||||
|
||||
int32_t GetSessionProtocolType(int sessionId, ProtocolType &protocolType);
|
||||
int32_t SetSessionProtocolType(int sessionId, ProtocolType protocolType);
|
||||
@ -127,14 +128,16 @@ private:
|
||||
std::unique_ptr<CastInnerRemoteDevice> GetRemoteFromJsonData(const std::string &Data);
|
||||
|
||||
int GetRTSPPort();
|
||||
std::shared_ptr<IConnectManagerSessionListener> GetSessionListener(int castSessionId);
|
||||
void SetListener(std::shared_ptr<IConnectionManagerListener> listener);
|
||||
std::shared_ptr<IConnectionManagerListener> GetListener();
|
||||
bool HasListener();
|
||||
void ResetListener();
|
||||
|
||||
std::mutex mutex_;
|
||||
int protocolType_ = 0;
|
||||
std::shared_ptr<IConnectionManagerListener> listener_;
|
||||
std::shared_ptr<IConnectManagerSessionListener> sessionListener_;
|
||||
std::map<int, std::shared_ptr<IConnectManagerSessionListener>> sessionListeners_;
|
||||
std::map<int, int> transIdToCastSessionIdMap_;
|
||||
bool isDiscoverable_{ false };
|
||||
DeviceGrabState grabState_{ DeviceGrabState::NO_GRAB };
|
||||
|
@ -16,8 +16,8 @@
|
||||
* Create: 2023-11-08
|
||||
*/
|
||||
|
||||
#ifndef LIBCASTENGINE_CONNECTMANAGER_LISTENER_H
|
||||
#define LIBCASTENGINE_CONNECTMANAGER_LISTENER_H
|
||||
#ifndef CONNECTION_MANAGER_LISTENER_H
|
||||
#define CONNECTION_MANAGER_LISTENER_H
|
||||
|
||||
#include "cast_engine_common.h"
|
||||
|
||||
@ -31,9 +31,9 @@ public:
|
||||
virtual ~IConnectionManagerListener() = default;
|
||||
|
||||
virtual int NotifySessionIsReady() = 0;
|
||||
virtual void ReportSessionCreate(int castSessionId) = 0;
|
||||
virtual void NotifyDeviceIsOffline(const std::string &deviceId) = 0;
|
||||
virtual bool NotifyRemoteDeviceIsReady(int castSessionId, const CastInnerRemoteDevice &device) = 0;
|
||||
virtual void OnEvent(const std::string &deviceId, ReasonCode currentEventCode) = 0;
|
||||
virtual void GrabDevice(int32_t sessionId) = 0;
|
||||
virtual int32_t GetSessionProtocolType(int sessionId, ProtocolType &protocolType) = 0;
|
||||
virtual int32_t SetSessionProtocolType(int sessionId, ProtocolType protocolType) = 0;
|
||||
@ -49,4 +49,4 @@ public:
|
||||
} // namespace CastEngine
|
||||
} // namespace OHOS
|
||||
|
||||
#endif // LIBCASTENGINE_CONNECTMANAGER_LISTENER_H
|
||||
#endif // CONNECTION_MANAGER_LISTENER_H
|
@ -119,8 +119,9 @@ private:
|
||||
void UpdateDeviceState();
|
||||
|
||||
std::mutex mutex_;
|
||||
int protocolType_ = 0;
|
||||
|
||||
int32_t uid_{ 0 };
|
||||
int protocolType_ = 0;
|
||||
std::shared_ptr<IDiscoveryManagerListener> listener_;
|
||||
std::shared_ptr<EventRunner> eventRunner_;
|
||||
std::shared_ptr<DiscoveryEventHandler> eventHandler_;
|
||||
|
@ -864,6 +864,37 @@ void ConnectionManager::UpdateGrabState(bool changeState, int32_t sessionId)
|
||||
grabState_ = DeviceGrabState::NO_GRAB;
|
||||
}
|
||||
|
||||
void ConnectionManager::AddSessionListener(int castSessionId, std::shared_ptr<IConnectManagerSessionListener> listener)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
sessionListeners_[castSessionId] = listener;
|
||||
}
|
||||
|
||||
void ConnectionManager::RemoveSessionListener(int castSessionId)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
if (castSessionId == INVALID_ID) {
|
||||
sessionListeners_.clear();
|
||||
return;
|
||||
}
|
||||
auto it = sessionListeners_.find(castSessionId);
|
||||
if (it == sessionListeners_.end()) {
|
||||
CLOGE("Cast session listener(%{public}d) has gone.", castSessionId);
|
||||
return;
|
||||
}
|
||||
sessionListeners_.erase(it);
|
||||
}
|
||||
|
||||
std::shared_ptr<IConnectManagerSessionListener> ConnectionManager::GetSessionListener(int castSessionId)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
auto it = sessionListeners_.find(castSessionId);
|
||||
if (it == sessionListeners_.end()) {
|
||||
CLOGE("Cast session listener(%{public}d) has gone.", castSessionId);
|
||||
return nullptr;
|
||||
}
|
||||
return it->second;
|
||||
}
|
||||
void ConnectionManager::SetListener(std::shared_ptr<IConnectionManagerListener> listener)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
@ -879,7 +910,7 @@ bool ConnectionManager::HasListener()
|
||||
void ConnectionManager::ResetListener()
|
||||
{
|
||||
SetListener(nullptr);
|
||||
SetSessionListener(nullptr);
|
||||
RemoveSessionListener(INVALID_ID);
|
||||
}
|
||||
|
||||
bool ConnectionManager::UpdateDeviceState(const std::string &deviceId, RemoteDeviceState state)
|
||||
@ -888,15 +919,6 @@ bool ConnectionManager::UpdateDeviceState(const std::string &deviceId, RemoteDev
|
||||
return CastDeviceDataManager::GetInstance().SetDeviceState(deviceId, state);
|
||||
}
|
||||
|
||||
void ConnectionManager::ReportErrorByListener(const std::string &deviceId, ReasonCode currentEventCode)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
if (!listener_) {
|
||||
return;
|
||||
}
|
||||
listener_->OnEvent(deviceId, currentEventCode);
|
||||
}
|
||||
|
||||
int32_t ConnectionManager::GetSessionProtocolType(int sessionId, ProtocolType &protocolType)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
@ -925,10 +947,23 @@ void ConnectionManager::NotifyDeviceIsOffline(const std::string &deviceId)
|
||||
listener_->NotifyDeviceIsOffline(deviceId);
|
||||
}
|
||||
|
||||
void ConnectionManager::SetSessionListener(std::shared_ptr<IConnectManagerSessionListener> listener)
|
||||
bool ConnectionManager::NotifyConnectStage(const CastInnerRemoteDevice &device, int result, int32_t reasonCode)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
sessionListener_ = listener;
|
||||
CLOGI("result %{public}d, reasonCode %{public}d", result, reasonCode);
|
||||
|
||||
auto sessionListener = GetSessionListener(device.localCastSessionId);
|
||||
if (sessionListener == nullptr) {
|
||||
CLOGE("sessionListener is NULL");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (result == ConnectStageResult::AUTH_FAILED || result == ConnectStageResult::CONNECT_FAIL ||
|
||||
result == ConnectStageResult::DISCONNECT_START) {
|
||||
UpdateDeviceState(device.deviceId, RemoteDeviceState::FOUND);
|
||||
}
|
||||
|
||||
sessionListener->NotifyConnectStage(device.deviceId, result, reasonCode);
|
||||
return true;
|
||||
}
|
||||
|
||||
void CastBindTargetCallback::OnBindResult(const PeerTargetId &targetId, int32_t result, int32_t status,
|
||||
|
@ -544,7 +544,7 @@ bool CastSessionImpl::Init()
|
||||
rtspControl_ = IRtspController::GetInstance(rtspListener_, property_.protocolType, property_.endType);
|
||||
|
||||
connectManagerListener_ = std::make_shared<ConnectManagerListenerImpl>(this);
|
||||
ConnectionManager::GetInstance().SetSessionListener(connectManagerListener_);
|
||||
ConnectionManager::GetInstance().AddSessionListener(sessionId_, connectManagerListener_);
|
||||
|
||||
defaultState_ = std::make_shared<DefaultState>(SessionState::DEFAULT, this, nullptr);
|
||||
disconnectedState_ = std::make_shared<DisconnectedState>(SessionState::DISCONNECTED, this, defaultState_);
|
||||
|
Loading…
Reference in New Issue
Block a user