!108 调整service下的类

Merge pull request !108 from LongestDistance/master
This commit is contained in:
openharmony_ci 2024-11-18 02:48:49 +00:00 committed by Gitee
commit d61a0a68cc
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
18 changed files with 505 additions and 78 deletions

View File

@ -41,13 +41,16 @@ enum class SessionState : uint8_t {
DISCONNECTING,
STREAM,
AUTHING,
MIRROR_TO_STREAM,
STREAM_TO_MIRROR,
SESSION_STATE_MAX,
};
const std::array<std::string, static_cast<size_t>(SessionState::SESSION_STATE_MAX)> SESSION_STATE_STRING = {
"DEFAULT", "DISCONNECTED", "CONNECTING", "CONNECTED", "PLAYING", "PAUSED", "DISCONNECTING", "STREAM",
"AUTHING"
"AUTHING", "MIRROR_TO_STREAM", "STREAM_TO_MIRROR"
};
const std::string SUPPORT_MIRROR_TO_STREAM = "SUPPORT_MIRROR_TO_STREAM";
} // namespace CastEngineService
} // namespace CastEngine
} // namespace OHOS

View File

@ -37,7 +37,7 @@ public:
void OnChannelRemoved(std::shared_ptr<Channel> channel) override;
void OnChannelError(std::shared_ptr<Channel> channel, int errorCode) override;
bool IsMediaChannelReady();
void SetMediaChannel(ModuleType moduleType);
bool SetAndCheckMediaChannel(ModuleType moduleType, const CastInnerRemoteDevice &remote);
private:
const static unsigned int UNCONNECTED_STATE = 0;
@ -87,6 +87,8 @@ public:
explicit CastStreamListenerImpl(sptr<CastSessionImpl> session) : session_(session) {}
~CastStreamListenerImpl() override;
bool SendActionToPeers(int action, const std::string &param) override;
bool TransferToStreamMode() override;
bool DisconnectSession(std::string deviceId) override;
void OnRenderReady(bool isReady) override;
void OnEvent(EventId eventId, const std::string &data) override;
@ -231,6 +233,34 @@ protected:
bool HandleMessage(const Message &msg) override;
DISALLOW_EVIL_CONSTRUCTORS(DisconnectingState);
};
class CastSessionImpl::MirrorToStreamState : public BaseState {
public:
explicit MirrorToStreamState(SessionState stateId, sptr<CastSessionImpl> session,
std::shared_ptr<State> parentState = nullptr)
: BaseState(stateId, session, parentState) {};
protected:
void Enter() override;
void Exit() override;
bool HandleMessage(const Message &msg) override;
void HandleRenderReadyMessage(const Message &msg, sptr<CastSessionImpl> session);
DISALLOW_EVIL_CONSTRUCTORS(MirrorToStreamState);
};
class CastSessionImpl::StreamToMirrorState : public BaseState {
public:
explicit StreamToMirrorState(SessionState stateId, sptr<CastSessionImpl> session,
std::shared_ptr<State> parentState = nullptr)
: BaseState(stateId, session, parentState) {};
protected:
void Enter() override;
void Exit() override;
bool HandleMessage(const Message &msg) override;
DISALLOW_EVIL_CONSTRUCTORS(StreamToMirrorState);
};
} // namespace CastEngineService
} // namespace CastEngine
} // namespace OHOS

View File

@ -84,6 +84,8 @@ public:
int32_t GetSessionProtocolType(ProtocolType &protocolType) override;
void SetSessionProtocolType(ProtocolType protocolType) override;
uint8_t GetSessionState() override;
int32_t GetRemoteDeviceInfo(std::string deviceId, CastRemoteDevice &remoteDevice) override;
int32_t Play(const std::string &deviceId);
int32_t Pause(const std::string &deviceId);
@ -105,9 +107,12 @@ private:
};
enum class CastSessionRemoteEventId : uint8_t {
SWITCH_TO_UI = 1,
SWITCH_TO_MIRROR,
MIRROR_TO_UI = 1,
UI_TO_MIRROR,
READY_TO_PLAYING,
MIRROR_TO_STREAM,
STREAM_TO_MIRROR,
RESPONSE_MODE_SWITCH,
};
class ChannelManagerListenerImpl;
class RtspListenerImpl;
@ -122,13 +127,20 @@ private:
class PausedState;
class PlayingState;
class DisconnectingState;
class MirrorToStreamState;
class StreamToMirrorState;
class StreamState;
static constexpr int MODULE_ID_RTSP = 1000;
static constexpr int MODULE_ID_MEDIA = 1001;
static constexpr int MODULE_ID_RC = 1002;
static constexpr int MODULE_ID_DLNA = 1004;
static constexpr int MODULE_ID_CHANNEL = 1005;
static constexpr int MODULE_ID_CAST_PROJECTION = 1006;
static constexpr int MODULE_ID_CAST_STREAM = 1009;
static constexpr int MODULE_ID_CAST_SESSION = 2000;
static constexpr int MODULE_ID_UI = 2001;
static constexpr int MODULE_ID_MEDIA_NEW = 2002;
// COOPERATION/HICAR use it to colse media channel, don't modify it.
static constexpr double CAST_VERSION = 1.0;
static constexpr int TIMEOUT_CONNECT = 30 * 1000;
@ -156,9 +168,6 @@ private:
void UpdateRemoteDeviceInfoFromCastDeviceDataManager(const std::string &deviceId);
void ChangeDeviceState(DeviceState state, const std::string &deviceId,
int32_t reasonCode = REASON_DEFAULT);
void ChangeDeviceStateLocked(
DeviceState state, const std::string &deviceId, int32_t reasonCode = REASON_DEFAULT);
void ReportDeviceStateInfo(DeviceState state, const std::string &deviceId, const ReasonCode eventCode);
void ChangeDeviceStateInner(DeviceState state, const std::string &deviceId,
int32_t reasonCode = REASON_DEFAULT);
void OnEvent(EventId eventId, const std::string &data);
@ -198,14 +207,24 @@ private:
std::shared_ptr<ICastStreamManager> StreamManagerGetter();
sptr<IMirrorPlayerImpl> MirrorPlayerGetter();
void ProcessRtspEvent(int moduleId, int event, const std::string &param);
void ProcessRtspEventInner(int moduleId, int event, const std::string &param);
void OtherAddDevice(const CastInnerRemoteDevice &remoteDevice);
void SendConsultData(const std::string &deviceId, int port);
int32_t ConnectDlnaDevice(CastInnerRemoteDevice remoteDeviceInfo);
bool IsDlnaDevice(CastInnerRemoteDevice remoteDeviceInfo);
void MirrorRcvVideoFrame();
bool ProcessSetCastMode(const Message &msg);
void UpdateScreenInfo(uint64_t screenId, uint16_t width, uint16_t height);
void UpdateDefaultDisplayRotationInfo(int rotation, uint16_t width, uint16_t height);
bool ProcessMirrorToStream();
bool PauseMirrorForStream();
bool PlayAfterSwitchToStream();
bool ProcessStreamToMirror();
bool PlayMirrorForChange();
bool IsStreamMode();
std::string GetPlayerControllerCapability();
bool IsSink();
@ -213,7 +232,9 @@ private:
void SendCastRenderReadyOption(int isReady);
void OnEventInner(sptr<CastSessionImpl> session, EventId eventId, const std::string &jsonParam);
void WaitSinkSetProperty();
void SetWifiScene(unsigned int scene);
void SetMirrorToStreamState(bool state);
int32_t RemoveDeviceInner(const std::string &deviceId);
std::shared_ptr<DefaultState> defaultState_;
std::shared_ptr<DisconnectedState> disconnectedState_;
std::shared_ptr<AuthingState> authingState_;
@ -225,6 +246,8 @@ private:
bool isMirrorPlaying_ { false };
std::shared_ptr<StreamState> streamState_;
std::shared_ptr<MirrorToStreamState> mirrorToStreamState_;
std::shared_ptr<StreamToMirrorState> streamToMirrorState_;
static std::atomic<int> idCount_;
@ -236,8 +259,10 @@ private:
int rtspPort_{ -1 };
std::list<CastRemoteDeviceInfo> remoteDeviceList_;
CastLocalDevice localDevice_;
uint32_t dlnaDeviceId_{ static_cast<uint32_t>(INVALID_ID) };
CastSessionProperty property_;
ParamInfo rtspParamInfo_;
bool mirrorToStream_ { false };
SessionState sessionState_{ SessionState::DISCONNECTED };
std::shared_ptr<ChannelManager> channelManager_;
@ -245,6 +270,7 @@ private:
std::shared_ptr<IRtspController> rtspControl_;
std::shared_ptr<RtspListenerImpl> rtspListener_;
std::shared_ptr<ConnectManagerListenerImpl> connectManagerListener_;
sptr<IStreamPlayerIpc> streamPlayer_;
std::mutex mutex_;
std::mutex mirrorMutex_;

View File

@ -47,6 +47,7 @@ private:
int32_t DoNotifyEvent(MessageParcel &data, MessageParcel &reply);
int32_t DoSetCastMode(MessageParcel &data, MessageParcel &reply);
int32_t DoRelease(MessageParcel &data, MessageParcel &reply);
int32_t DoGetRemoteDeviceInfo(MessageParcel &data, MessageParcel &reply);
};
} // namespace CastEngineService
} // namespace CastEngine

View File

@ -32,6 +32,7 @@
#include "mirror_player_impl.h"
#include "permission.h"
#include "cast_engine_dfx.h"
#include "wifi_hid2d.h"
#include "bundle_mgr_client.h"
namespace OHOS {
@ -45,6 +46,7 @@ using CastSessionRtsp::VtpType;
namespace {
constexpr int SESSION_KEY_LENGTH = 16;
constexpr int AV_SESSION_UID = 6700;
void ProcessConnectWriteWrap(const std::string& funcName,
ProtocolType protocolType, int sceneType, const std::string& puid)
@ -133,9 +135,6 @@ void CastSessionImpl::SetServiceCallbackForRelease(const std::function<void(int)
int32_t CastSessionImpl::RegisterListener(sptr<ICastSessionListenerImpl> listener)
{
CLOGD("Start to register session listener");
if (!Permission::CheckPidPermission()) {
return ERR_NO_PERMISSION;
}
std::unique_lock<std::mutex> lock(mutex_);
listeners_[IPCSkeleton::GetCallingPid()] = listener;
cond_.notify_all();
@ -179,24 +178,62 @@ int32_t CastSessionImpl::AddDevice(const CastRemoteDevice &remoteDevice)
auto remote = CastDeviceDataManager::GetInstance().GetDeviceByDeviceId(remoteDevice.deviceId);
if (remote == std::nullopt) {
CLOGE("device is not found");
return CAST_ENGINE_ERROR;
}
if (IsDlnaDevice(*remote)) {
return ConnectDlnaDevice(*remote);
}
if (CastDeviceDataManager::GetInstance().IsDeviceConnected(remoteDevice.deviceId)) {
CLOGE("device(%{public}s) has been connected, do not grab", Utils::Mask(remoteDevice.deviceId).c_str());
return CAST_ENGINE_ERROR;
}
remote->sessionId = sessionId_;
remote->subDeviceType = remoteDevice.subDeviceType;
if (!AddDevice(*remote) || !ConnectionManager::GetInstance().ConnectDevice(*remote, property_.protocolType)) {
CLOGE("AddDevice or ConnectDevice fail");
return CAST_ENGINE_ERROR;
}
CLOGI("AddDevice out.");
return CAST_ENGINE_SUCCESS;
}
bool CastSessionImpl::IsDlnaDevice(CastInnerRemoteDevice remoteDeviceInfo)
{
CLOGI("IsDlnaDevice in, protocolType:%{public}d, capabilityInfo:%{public}u",
property_.protocolType, remoteDeviceInfo.capabilityInfo);
if ((remoteDeviceInfo.capabilityInfo & static_cast<uint32_t>(ProtocolType::CAST_PLUS_STREAM)) != 0) {
CLOGI("Priority Stream");
return false;
}
if (((remoteDeviceInfo.capabilityInfo & static_cast<uint32_t>(ProtocolType::DLNA)) != 0) &&
(property_.protocolType == ProtocolType::CAST_PLUS_STREAM || property_.protocolType == ProtocolType::DLNA)) {
return true;
}
return false;
}
int32_t CastSessionImpl::ConnectDlnaDevice(CastInnerRemoteDevice remoteDeviceInfo)
{
CLOGI("ConnectDlnaDevice in");
if (!AddRemoteDevice(CastRemoteDeviceInfo { remoteDeviceInfo, DeviceState::STREAM })) {
CLOGE("AddRemoteDevice fail");
return CAST_ENGINE_ERROR;
}
std::lock_guard<std::mutex> lock(mutex_);
dlnaDeviceId_ = remoteDeviceInfo.dlnaDeviceId;
CLOGI("dlnaDeviceId is %u, deviceId is %s", dlnaDeviceId_, Utils::Mask(remoteDeviceInfo.deviceId).c_str());
for (const auto &[pid, listener] : listeners_) {
listener->OnDeviceState(DeviceStateInfo { DeviceState::STREAM, remoteDeviceInfo.deviceId});
}
return CAST_ENGINE_SUCCESS;
}
bool CastSessionImpl::AddDevice(const CastInnerRemoteDevice &remoteDevice)
{
if (!Permission::CheckPidPermission()) {
return false;
}
CLOGI("In: session state: %{public}s", SESSION_STATE_STRING[static_cast<int>(sessionState_)].c_str());
{
std::unique_lock<std::mutex> lock(mutex_);
@ -267,23 +304,30 @@ void CastSessionImpl::WaitSinkSetProperty()
int32_t CastSessionImpl::RemoveDevice(const std::string &deviceId)
{
CLOGI("In: session state: %{public}s", SESSION_STATE_STRING[static_cast<int>(sessionState_)].c_str());
if (!Permission::CheckPidPermission()) {
return ERR_NO_PERMISSION;
CLOGI("In: session state: %{public}s, deviceId:%{public}s",
SESSION_STATE_STRING[static_cast<int>(sessionState_)].c_str(), Utils::Mask(deviceId).c_str());
std::lock_guard<std::mutex> lock(mutex_);
return RemoveDeviceInner(deviceId);
}
int32_t CastSessionImpl::RemoveDeviceInner(const std::string &deviceId)
{
CLOGI("RemoveDeviceInner in");
if (IPCSkeleton::GetCallingUid() == AV_SESSION_UID && mirrorToStream_) {
CLOGI("send message to mirror");
SendCastMessage(Message(MessageId::MSG_SWITCH_TO_MIRROR, deviceId));
return CAST_ENGINE_SUCCESS;
}
auto anonymousID = GetAnonymousDeviceID(deviceId);
auto sceneType = GetBIZSceneType(static_cast<int>(property_.protocolType));
HiSysEventWriteWrap(__func__, {
{"BIZ_SCENE", sceneType},
{"BIZ_STATE", static_cast<int32_t>(BIZStateType::BIZ_STATE_BEGIN)},
{"BIZ_STAGE", static_cast<int32_t>(BIZSceneStage::START_DISCONNECT)},
{"STAGE_RES", static_cast<int32_t>(StageResType::STAGE_RES_SUCCESS)},
{"ERROR_CODE", CAST_RADAR_SUCCESS}}, {
{"TO_CALL_PKG", DEVICE_MANAGER_NAME},
{"LOCAL_SESS_NAME", ""},
{"PEER_SESS_NAME", ""},
{"PEER_UDID", anonymousID}});
if ((property_.protocolType == ProtocolType::DLNA ||
dlnaDeviceId_ != static_cast<uint32_t>(INVALID_ID)) && streamPlayer_) {
streamPlayer_->Stop();
for (const auto &[pid, listener] : listeners_) {
listener->OnDeviceState(DeviceStateInfo { DeviceState::DISCONNECTED, deviceId});
}
}
SendCastMessage(Message(MessageId::MSG_DISCONNECT, deviceId));
return CAST_ENGINE_SUCCESS;
@ -306,10 +350,9 @@ bool CastSessionImpl::ReleaseSessionResources(pid_t pid, uid_t uid)
}
for (auto &deviceInfo : remoteDeviceList_) {
RemoveDevice(deviceInfo.remoteDevice.deviceId);
RemoveDeviceInner(deviceInfo.remoteDevice.deviceId);
}
Stop();
return true;
}
@ -375,18 +418,21 @@ bool CastSessionImpl::DestroyStreamPlayer()
int32_t CastSessionImpl::Release()
{
{
std::lock_guard<std::mutex> lock(mutex_);
for (auto &deviceInfo : remoteDeviceList_) {
RemoveDevice(deviceInfo.remoteDevice.deviceId);
std::thread([this] {
Utils::SetThreadName("SessionsRelease");
{
std::lock_guard<std::mutex> lock(mutex_);
for (auto &deviceInfo : remoteDeviceList_) {
RemoveDeviceInner(deviceInfo.remoteDevice.deviceId);
}
}
}
std::lock_guard<std::mutex> lock(serviceCallbackMutex_);
if (!serviceCallback_) {
CLOGE("serviceCallback is null");
return CAST_ENGINE_ERROR;
}
serviceCallback_(sessionId_);
std::lock_guard<std::mutex> lock(serviceCallbackMutex_);
if (!serviceCallback_) {
CLOGE("serviceCallback is null");
return;
}
serviceCallback_(sessionId_);
}).detach();
return CAST_ENGINE_SUCCESS;
}
@ -438,6 +484,14 @@ int32_t CastSessionImpl::GetDeviceState(const std::string &deviceId, DeviceState
return CAST_ENGINE_SUCCESS;
}
uint8_t CastSessionImpl::GetSessionState()
{
std::unique_lock<std::mutex> lock(mutex_);
CLOGE("GetSessionState in, session state: %{public}s",
SESSION_STATE_STRING[static_cast<int>(sessionState_)].c_str());
return static_cast<uint8_t>(sessionState_);
}
int32_t CastSessionImpl::SetSessionProperty(const CastSessionProperty &property)
{
if (!Permission::CheckPidPermission()) {
@ -555,6 +609,8 @@ bool CastSessionImpl::Init()
pausedState_ = std::make_shared<PausedState>(SessionState::PAUSED, this, connectedState_);
playingState_ = std::make_shared<PlayingState>(SessionState::PLAYING, this, connectedState_);
streamState_ = std::make_shared<StreamState>(SessionState::STREAM, this, connectedState_);
mirrorToStreamState_ = std::make_shared<MirrorToStreamState>(SessionState::MIRROR_TO_STREAM, this, connectedState_);
streamToMirrorState_ = std::make_shared<StreamToMirrorState>(SessionState::STREAM_TO_MIRROR, this, connectedState_);
TransferTo(disconnectedState_);
return true;
@ -739,10 +795,16 @@ std::optional<int> CastSessionImpl::SetupMedia(const CastInnerRemoteDevice &remo
return std::nullopt;
}
void CastSessionImpl::SetWifiScene(unsigned int scene)
{
CLOGI("SetWifiScene %{public}u end", scene);
}
bool CastSessionImpl::ProcessSetUp(const Message &msg)
{
CLOGD("Media ports: %d, rc ports: %d id %s", msg.arg1_, msg.arg2_, msg.strArg_.c_str());
SetWifiScene(1);
auto deviceInfo = FindRemoteDevice(msg.strArg_);
if (deviceInfo == nullptr) {
CLOGE("Remote device is null");
@ -806,6 +868,44 @@ bool CastSessionImpl::ProcessPauseReq(const Message &msg)
return true;
}
bool CastSessionImpl::ProcessMirrorToStream()
{
CLOGD("In");
SendEventChange(MODULE_ID_CAST_SESSION, static_cast<int>(CastSessionRemoteEventId::MIRROR_TO_STREAM), "");
PauseMirrorForStream();
return true;
}
bool CastSessionImpl::ProcessStreamToMirror()
{
CLOGI("In");
PlayMirrorForChange();
SendEventChange(MODULE_ID_CAST_SESSION, static_cast<int>(CastSessionRemoteEventId::STREAM_TO_MIRROR), "");
return true;
}
bool CastSessionImpl::PauseMirrorForStream()
{
CLOGD("In");
return true;
}
bool CastSessionImpl::PlayMirrorForChange()
{
CLOGD("In");
return true;
}
bool CastSessionImpl::PlayAfterSwitchToStream()
{
auto streamManager = StreamManagerGetter();
if (!streamManager) {
CLOGI("streamManager is null");
return false;
}
return streamManager->PlayAfterSwitchToStream();
}
void CastSessionImpl::MirrorRcvVideoFrame()
{
}
@ -838,6 +938,7 @@ bool CastSessionImpl::ProcessDisconnect(const Message &msg)
{"PEER_UDID", ""}});
CLOGD("In");
SetWifiScene(0);
rtspControl_->Action(ActionType::TEARDOWN);
channelManager_->DestroyAllChannels();
return true;
@ -850,9 +951,12 @@ bool CastSessionImpl::ProcessError(const Message &msg)
std::lock_guard<std::mutex> lock(mutex_);
auto &devices = remoteDeviceList_;
for (auto it = devices.begin(); it != devices.end();) {
ChangeDeviceStateLocked(DeviceState::DISCONNECTED, it->remoteDevice.deviceId, msg.arg1_);
ConnectionManager::GetInstance().DisconnectDevice(it->remoteDevice.deviceId);
devices.erase(it++);
ChangeDeviceStateInner(DeviceState::DISCONNECTED, it->remoteDevice.deviceId, msg.arg1_);
if (property_.protocolType == ProtocolType::CAST_PLUS_MIRROR ||
property_.protocolType == ProtocolType::CAST_PLUS_STREAM) {
ConnectionManager::GetInstance().DisconnectDevice(it->remoteDevice.deviceId);
}
it = devices.erase(it);
}
return result;
@ -1036,13 +1140,13 @@ bool CastSessionImpl::IsAllowTransferState(SessionState desiredState) const
return true;
}
void CastSessionImpl::ChangeDeviceState(DeviceState state, const std::string &deviceId, int32_t eventCode)
void CastSessionImpl::ChangeDeviceState(DeviceState state, const std::string &deviceId, int32_t reasonCode)
{
std::lock_guard<std::mutex> lock(mutex_);
ChangeDeviceStateLocked(state, deviceId, eventCode);
ChangeDeviceStateInner(state, deviceId, reasonCode);
}
void CastSessionImpl::ChangeDeviceStateLocked(DeviceState state, const std::string &deviceId, int32_t eventCode)
void CastSessionImpl::ChangeDeviceStateInner(DeviceState state, const std::string &deviceId, int32_t reasonCode)
{
auto deviceInfo = FindRemoteDeviceLocked(deviceId);
if (!deviceInfo) {
@ -1050,32 +1154,20 @@ void CastSessionImpl::ChangeDeviceStateLocked(DeviceState state, const std::stri
return;
}
CLOGD("New state:%{public}s, old state:%{public}s, device id:%s, eventCode:%{public}d",
CLOGI("New state:%{public}s, old state:%{public}s, device id:%s, reasonCode:%{public}d",
DEVICE_STATE_STRING[static_cast<int>(state)].c_str(),
DEVICE_STATE_STRING[static_cast<int>(deviceInfo->deviceState)].c_str(),
deviceId.c_str(),
static_cast<ReasonCode>(eventCode));
if (state == deviceInfo->deviceState) {
reasonCode);
// Allow multiple reporting in authing state
if (state != DeviceState::AUTHING && state == deviceInfo->deviceState) {
return;
}
UpdateRemoteDeviceStateLocked(deviceId, state);
for (const auto &[pid, listener] : listeners_) {
listener->OnDeviceState(DeviceStateInfo { state, deviceId, static_cast<ReasonCode>(eventCode) });
}
}
void CastSessionImpl::ReportDeviceStateInfo(DeviceState state, const std::string &deviceId, const ReasonCode eventCode)
{
CLOGI("ReportDeviceStateInfo in.");
auto deviceInfo = FindRemoteDeviceLocked(deviceId);
if (!deviceInfo) {
CLOGE("does not exist this device, deviceId = %s.", deviceId.c_str());
return;
}
for (const auto &[pid, listener] : listeners_) {
listener->OnDeviceState(DeviceStateInfo { state, deviceId, eventCode });
listener->OnDeviceState(DeviceStateInfo{ state, deviceId, static_cast<ReasonCode>(reasonCode) });
}
}
@ -1087,6 +1179,7 @@ void CastSessionImpl::OnEvent(EventId eventId, const std::string &data)
CLOGE("OnEvent failed because listeners_ is empty!");
return;
}
for (const auto &[pid, listener] : listeners_) {
listener->OnEvent(eventId, data);
}
@ -1111,6 +1204,29 @@ void CastSessionImpl::ProcessRtspEvent(int moduleId, int event, const std::strin
default:
break;
}
ProcessRtspEventInner(moduleId, event, param);
}
void CastSessionImpl::ProcessRtspEventInner(int moduleId, int event, const std::string &param)
{
switch (moduleId) {
case MODULE_ID_CAST_SESSION:
if (event == static_cast<int>(CastSessionRemoteEventId::MIRROR_TO_STREAM)) {
SendCastMessage(Message(MessageId::MSG_SWITCH_TO_STREAM, param));
} else if (event == static_cast<int>(CastSessionRemoteEventId::STREAM_TO_MIRROR)) {
SendCastMessage(Message(MessageId::MSG_SWITCH_TO_MIRROR, param));
} else if (event == static_cast<int>(CastSessionRemoteEventId::RESPONSE_MODE_SWITCH) &&
!param.compare("success")) {
SendCastMessage(Message(MessageId::MSG_PEER_RENDER_READY, param));
} else if (event == static_cast<int>(CastSessionRemoteEventId::RESPONSE_MODE_SWITCH) &&
!param.compare("fail")) {
SendCastMessage(Message(MessageId::MSG_PEER_RENDER_FAIL, param));
}
break;
default:
break;
}
}
bool CastSessionImpl::IsSupportFeature(const std::set<int> &featureSet, int supportFeature)
@ -1249,6 +1365,37 @@ void CastSessionImpl::OnRemoteCtrlEvent(int eventType, const uint8_t *data, uint
listener->OnRemoteCtrlEvent(eventType, data, len);
}
}
void CastSessionImpl::SetMirrorToStreamState(bool state)
{
std::unique_lock<std::mutex> lock(mutex_);
CLOGI("set success %d", state);
mirrorToStream_ = state;
}
int32_t CastSessionImpl::GetRemoteDeviceInfo(std::string deviceId, CastRemoteDevice &remoteDevice)
{
CLOGI("GetRemoteDeviceInfo in");
auto device = CastDeviceDataManager::GetInstance().GetDeviceByDeviceId(deviceId);
if (device == std::nullopt) {
CLOGI("The device is not found. deviceId: %{public}s", Utils::Mask(deviceId).c_str());
return CAST_ENGINE_ERROR;
}
auto networkId = CastDeviceDataManager::GetInstance().GetDeviceNetworkId(deviceId);
remoteDevice = CastRemoteDevice {
device->deviceId, device->deviceName,
device->deviceType, device->subDeviceType,
device->ipAddress, device->channelType,
device->capability, device->capabilityInfo,
*networkId, "", 0, nullptr, device->isLeagacy,
device->sessionId, device->drmCapabilities,
};
return CAST_ENGINE_SUCCESS;
}
} // namespace CastEngineService
} // namespace CastEngine
} // namespace OHOS

View File

@ -49,6 +49,7 @@ CastSessionImplStub::CastSessionImplStub()
FILL_SINGLE_STUB_TASK(NOTIFY_EVENT, &CastSessionImplStub::DoNotifyEvent);
FILL_SINGLE_STUB_TASK(SET_CAST_MODE, &CastSessionImplStub::DoSetCastMode);
FILL_SINGLE_STUB_TASK(RELEASE, &CastSessionImplStub::DoRelease);
FILL_SINGLE_STUB_TASK(GET_REMOTE_DEVICE_INFO, &CastSessionImplStub::DoGetRemoteDeviceInfo);
}
int32_t CastSessionImplStub::DoRegisterListenerTask(MessageParcel &data, MessageParcel &reply)
@ -343,6 +344,32 @@ int32_t CastSessionImplStub::DoNotifyEvent(MessageParcel &data, MessageParcel &r
NotifyEvent(eventId, param);
return ERR_NONE;
}
int32_t CastSessionImplStub::DoGetRemoteDeviceInfo(MessageParcel &data, MessageParcel &reply)
{
if (!Permission::CheckMirrorPermission()) {
return ERR_NO_PERMISSION;
}
if (!Permission::CheckPidPermission()) {
return CAST_ENGINE_ERROR;
}
CastRemoteDevice remoteDevice;
int32_t ret = GetRemoteDeviceInfo(data.ReadString(), remoteDevice);
if (!reply.WriteInt32(ret)) {
CLOGE("Failed to write ret:%d", ret);
return IPC_STUB_WRITE_PARCEL_ERR;
}
if (!WriteCastRemoteDevice(reply, remoteDevice)) {
CLOGE("Failed to write remote device");
return IPC_STUB_WRITE_PARCEL_ERR;
}
return ERR_NONE;
}
} // namespace CastEngineService
} // namespace CastEngine
} // namespace OHOS

View File

@ -27,6 +27,21 @@ namespace CastEngineService {
DEFINE_CAST_ENGINE_LABEL("Cast-Session-Listeners");
namespace {
void AudioAndVideoWriteWrap(const std::string& funcName,
ProtocolType protocolType, ModuleType moduleType,
int sessionID)
{
auto localSessName = SoftBusWrapper::GetSoftBusMySessionName(sessionID);
auto peerSessName = SoftBusWrapper::GetSoftBusPeerSessionName(sessionID);
}
void RemoteControlWriteWrap(const std::string& funcName,
ProtocolType protocolType,
int sessionID)
{
auto localSessName = SoftBusWrapper::GetSoftBusMySessionName(sessionID);
auto peerSessName = SoftBusWrapper::GetSoftBusPeerSessionName(sessionID);
}
void RTSPWriteWrap(const std::string& funcName,
ProtocolType protocolType,
@ -190,6 +205,35 @@ bool CastSessionImpl::CastStreamListenerImpl::SendActionToPeers(int action, cons
return session->SendCastMessage(Message(MessageId::MSG_STREAM_SEND_ACTION_EVENT_TO_PEERS, action, param));
}
bool CastSessionImpl::CastStreamListenerImpl::TransferToStreamMode()
{
auto session = session_.promote();
if (!session) {
CLOGE("session is nullptr");
return false;
}
auto curSessionState = session->GetSessionState();
if (curSessionState != static_cast<uint8_t>(SessionState::STREAM)) {
return session->SendCastMessage(Message(MessageId::MSG_SWITCH_TO_STREAM));
}
return false;
}
bool CastSessionImpl::CastStreamListenerImpl::DisconnectSession(std::string deviceId)
{
auto session = session_.promote();
if (!session) {
CLOGE("session is nullptr");
return false;
}
session->ChangeDeviceState(DeviceState::DISCONNECTED, deviceId);
return true;
}
CastSessionImpl::CastStreamListenerImpl::~CastStreamListenerImpl()
{
CLOGD("~CastStreamListenerImpl");
@ -292,6 +336,20 @@ void CastSessionImpl::ChannelManagerListenerImpl::OnChannelCreated(std::shared_p
auto streamManager = session->StreamManagerGetter();
ModuleType moduleType = channel->GetRequest().moduleType;
switch (moduleType) {
case ModuleType::AUDIO:
case ModuleType::VIDEO:
AudioAndVideoWriteWrap(__func__, session->property_.protocolType, moduleType, sessionID);
if (SetAndCheckMediaChannel(moduleType, deviceInfo->remoteDevice)) {
session->SendCastMessage(Message(MessageId::MSG_SETUP_SUCCESS, MODULE_ID_MEDIA, remoteDeviceId));
}
break;
case ModuleType::REMOTE_CONTROL:
CLOGI("REMOTE_CONTROL channel created.");
RemoteControlWriteWrap(__func__, session->property_.protocolType, sessionID);
session->SendCastMessage(Message(MessageId::MSG_SETUP_SUCCESS, MODULE_ID_RC, remoteDeviceId));
break;
case ModuleType::RTSP:
session->rtspControl_->AddChannel(channel, deviceInfo->remoteDevice);
@ -369,17 +427,33 @@ void CastSessionImpl::ChannelManagerListenerImpl::OnChannelRemoved(std::shared_p
return;
}
void CastSessionImpl::ChannelManagerListenerImpl::SetMediaChannel(ModuleType moduleType)
bool CastSessionImpl::ChannelManagerListenerImpl::SetAndCheckMediaChannel(ModuleType moduleType,
const CastInnerRemoteDevice &remote)
{
auto session = session_.promote();
if (session == nullptr) {
CLOGE("Session is null");
return false;
}
std::unique_lock<std::mutex> lock(session->mutex_);
if (moduleType == ModuleType::AUDIO) {
mediaChannelState_ |= AUDIO_CHANNEL_CONNECTED;
return;
}
if (moduleType == ModuleType::VIDEO) {
mediaChannelState_ |= VIDEO_CHANNEL_CONNECTED;
}
CLOGI("SetMediaChannel mediaChannelState_ is %d, moduleType is %d", mediaChannelState_, moduleType);
CLOGI("SetAndCheckMediaChannel mediaChannelState_ is %{public}d, moduleType is %{public}d, protocolType %{public}d",
mediaChannelState_, moduleType, session->property_.protocolType);
if (session->property_.protocolType == ProtocolType::CAST_PLUS_MIRROR ||
session->property_.protocolType == ProtocolType::CAST_PLUS_STREAM ||
(session->property_.protocolType == ProtocolType::COOPERATION && !remote.isLeagacy)) {
return mediaChannelState_ == (VIDEO_CHANNEL_CONNECTED | AUDIO_CHANNEL_CONNECTED);
}
CLOGI("Only Cast Video.");
return mediaChannelState_ == VIDEO_CHANNEL_CONNECTED;
}
bool CastSessionImpl::ChannelManagerListenerImpl::IsMediaChannelReady()

View File

@ -415,9 +415,10 @@ bool CastSessionImpl::ConnectedState::HandleMessage(const Message &msg)
return true;
case MessageId::MSG_DISCONNECT:
session->ProcessDisconnect(msg);
session->TransferTo(session->disconnectingState_);
session->ChangeDeviceState(DeviceState::DISCONNECTED, deviceId);
session->RemoveRemoteDevice(deviceId);
session->TransferTo(session->disconnectingState_);
session->SetMirrorToStreamState(false);
return true;
case MessageId::MSG_ERROR:
session->ProcessError(msg);
@ -475,6 +476,14 @@ bool CastSessionImpl::StreamState::HandleMessage(const Message &msg)
case MessageId::MSG_PEER_RENDER_READY:
session->SendCastRenderReadyOption(msg.arg1_);
break;
case MessageId::MSG_SWITCH_TO_MIRROR:
if (!session->IsSink()) {
session->ProcessStreamToMirror();
session->ChangeDeviceState(DeviceState::STREAM_TO_MIRROR, session->GetCurrentRemoteDeviceId());
session->TransferTo(session->streamToMirrorState_);
session->SetMirrorToStreamState(false);
}
break;
default:
CLOGW("unsupported msg: %s, in stream state", MESSAGE_ID_STRING[msgId].c_str());
return false;
@ -564,6 +573,13 @@ bool CastSessionImpl::PlayingState::HandleMessage(const Message &msg)
case MessageId::MSG_MIRROR_SEND_ACTION_EVENT_TO_PEERS:
session->SendEventChange(MODULE_ID_MEDIA, msg.arg1_, param);
break;
case MessageId::MSG_SWITCH_TO_STREAM:
if (!session->IsSink()) {
session->ProcessMirrorToStream();
session->ChangeDeviceState(DeviceState::MIRROR_TO_STREAM, session->GetCurrentRemoteDeviceId());
session->TransferTo(session->mirrorToStreamState_);
}
break;
default:
CLOGW("unsupported msg: %s, in playing state", MESSAGE_ID_STRING[msgId].c_str());
return false;
@ -571,6 +587,46 @@ bool CastSessionImpl::PlayingState::HandleMessage(const Message &msg)
return true;
}
void CastSessionImpl::StreamToMirrorState::Enter()
{
BaseState::Enter(SessionState::STREAM_TO_MIRROR);
}
void CastSessionImpl::StreamToMirrorState::Exit()
{
BaseState::Exit();
}
bool CastSessionImpl::StreamToMirrorState::HandleMessage(const Message &msg)
{
auto session = session_.promote();
if (!session) {
CLOGE("Session is invalid");
return false;
}
BaseState::HandleMessage(msg);
MessageId msgId = static_cast<MessageId>(msg.what_);
std::string deviceId = msg.strArg_;
switch (msgId) {
case MessageId::MSG_PEER_RENDER_READY:
CLOGI("MSG_PEER_RENDER_READY in");
session->ProcessStreamToMirror();
session->ChangeDeviceState(DeviceState::PLAYING, session->GetCurrentRemoteDeviceId());
session->TransferTo(session->playingState_);
session->SetMirrorToStreamState(false);
break;
case MessageId::MSG_PEER_RENDER_FAIL:
CLOGI("MSG_PEER_RENDER_FAIL in");
session->ChangeDeviceState(DeviceState::STREAM, session->GetCurrentRemoteDeviceId());
session->TransferTo(session->streamState_);
break;
default:
CLOGW("unsupported msg: %{public}s, in StreamToMirror state", MESSAGE_ID_STRING[msgId].c_str());
return false;
}
return true;
}
void CastSessionImpl::DisconnectingState::Enter()
{
auto session = session_.promote();

View File

@ -33,6 +33,7 @@ constexpr int32_t AUTO_POSITION_UPDATE_INTERVAL = 100;
constexpr int32_t AUTO_POSITION_SYNC_INTERVAL = 10000;
constexpr int32_t POSITION_LAG_MINIMUM = 50;
constexpr int32_t POSITION_LAG_MAXIMUM = 150;
constexpr int32_t DEFAULT_MAX_VOLUME = 14;
const std::string PLAYER_ERROR = "PLAYER_ERROR";
#define RETURN_IF_PARSE_WRONG(value, data, key, ret, jsonType) \

View File

@ -71,6 +71,9 @@ public:
StreamCapability GetAvailableCapability() override;
PlaybackSpeed GetPlaySpeed() override;
bool IsDoubleFrame() override;
bool TransferToStreamMode(const MediaInfo &mediaInfo) override;
bool DisconnectSession(std::string deviceId) override;
bool PlayAfterSwitchToStream() override;
private:
bool ProcessActionPlayerStatusChanged(const json &data);
@ -113,6 +116,7 @@ private:
std::atomic<bool> isSeeking_{ false };
std::atomic<bool> isNewResourceLoaded_{ false };
std::atomic<bool> isDoubleFrame_{ false };
MediaInfo mediaInfo_{};
StreamCapability availableCapability_{};
};
} // namespace CastEngineService

View File

@ -54,6 +54,7 @@ public:
bool NotifyPeerCreateChannel() override;
void OnEvent(EventId eventId, const std::string &data) override;
void OnRenderReady(bool isReady) override;
bool PlayAfterSwitchToStream() override;
private:
bool ProcessActionLoad(const json &data);

View File

@ -28,6 +28,8 @@ class ICastStreamListener {
public:
virtual ~ICastStreamListener() = default;
virtual bool SendActionToPeers(int action, const std::string &param) = 0;
virtual bool TransferToStreamMode() = 0;
virtual bool DisconnectSession(std::string deviceId) = 0;
virtual void OnRenderReady(bool isReady) = 0;
virtual void OnEvent(EventId eventId, const std::string &data) = 0;
};

View File

@ -48,6 +48,7 @@ public:
void RemoveChannel(std::shared_ptr<Channel> channel);
std::string GetStreamPlayerCapability();
std::string HandleCustomNegotiationParams(const std::string &playerParams);
virtual bool PlayAfterSwitchToStream() = 0;
static constexpr int MODULE_EVENT_ID_CONTROL_EVENT = 100;
static constexpr int MODULE_EVENT_ID_CALLBACK_EVENT = 101;
@ -166,7 +167,7 @@ protected:
std::mutex dataMutex_;
std::mutex listenerMutex_;
int currentVolume_{ CAST_STREAM_INT_INVALID };
int maxVolume_{ CAST_STREAM_INT_INVALID };
int maxVolume_{ DEFAULT_MAX_VOLUME };
bool isMute_ = false;
};
} // namespace CastEngineService

View File

@ -57,6 +57,8 @@ public:
virtual StreamCapability GetAvailableCapability() = 0;
virtual PlaybackSpeed GetPlaySpeed() = 0;
virtual bool IsDoubleFrame() = 0;
virtual bool TransferToStreamMode(const MediaInfo &mediaInfo) = 0;
virtual bool DisconnectSession(std::string deviceId) = 0;
};
} // namespace CastEngineService
} // namespace CastEngine

View File

@ -361,6 +361,44 @@ void CastStreamManagerClient::OnEvent(EventId eventId, const std::string &data)
streamListener_->OnEvent(eventId, data);
}
bool CastStreamManagerClient::TransferToStreamMode(const MediaInfo &mediaInfo)
{
{
std::lock_guard<std::mutex> lock(dataMutex_);
mediaInfo_ = mediaInfo;
}
CLOGI("TransferToStreamMode in");
if (!streamListener_) {
CLOGE("streamListener_ is nullptr");
return false;
}
return streamListener_->TransferToStreamMode();
}
bool CastStreamManagerClient::DisconnectSession(std::string deviceId)
{
CLOGI("DisconnectSession in");
if (!streamListener_) {
CLOGE("streamListener_ is nullptr");
return false;
}
return streamListener_->DisconnectSession(deviceId);
}
bool CastStreamManagerClient::PlayAfterSwitchToStream()
{
std::lock_guard<std::mutex> lock(dataMutex_);
if (!player_) {
CLOGE("player_ is nullptr");
return false;
}
player_->Play(mediaInfo_);
return true;
}
sptr<IStreamPlayerListenerImpl> CastStreamManagerClient::PlayerListenerGetter()
{
std::lock_guard<std::mutex> lock(listenerMutex_);

View File

@ -219,6 +219,12 @@ void CastStreamManagerServer::OnRenderReady(bool isReady)
streamListener_->OnRenderReady(isReady);
}
bool CastStreamManagerServer::PlayAfterSwitchToStream()
{
CLOGE("Don't support PlayAfterSwitchToStream in CastStreamManagerServer");
return false;
}
std::shared_ptr<CastStreamPlayerManager> CastStreamManagerServer::PlayerGetter()
{
std::lock_guard<std::mutex> lock(dataMutex_);

View File

@ -1379,15 +1379,15 @@ bool CastStreamPlayer::SetVideoSurface(sptr<Surface> surface)
CastStreamVolumeCallback::~CastStreamVolumeCallback()
{
CLOGD("~CastStreamVolumeCallback in");
CLOGI("~CastStreamVolumeCallback in");
}
void CastStreamVolumeCallback::SetMaxVolume(int maxVolume)
{
CLOGD("SetMaxVolume in");
CLOGI("SetMaxVolume in");
std::lock_guard<std::mutex> lock(mutex_);
maxVolume_ = maxVolume;
CLOGD("SetMaxVolume out");
CLOGI("SetMaxVolume out");
}
void CastStreamVolumeCallback::OnVolumeKeyEvent(AudioStandard::VolumeEvent volumeEvent)

View File

@ -105,11 +105,17 @@ int32_t RemotePlayerController::Load(const MediaInfo &mediaInfo)
fileChannelServer_->ClearAllLocalFileInfo();
fileChannelServer_->AddLocalFileInfo(mediaInfoToPlay);
}
std::shared_ptr<ICastStreamManagerClient> targetCallback = callback_.lock();
if (!targetCallback) {
CLOGE("ICastStreamManagerClient is null");
return CAST_ENGINE_ERROR;
}
if (targetCallback->TransferToStreamMode(mediaInfoToPlay)) {
return CAST_ENGINE_SUCCESS;
}
if (targetCallback->IsDoubleFrame()) {
if (mediaInfo.mediaUrl == "http:") {
CLOGD("No need for double frame to load");
@ -125,6 +131,7 @@ int32_t RemotePlayerController::Load(const MediaInfo &mediaInfo)
return CAST_ENGINE_ERROR;
}
}
return CAST_ENGINE_SUCCESS;
}
@ -595,11 +602,12 @@ int32_t RemotePlayerController::GetPlaySpeed(PlaybackSpeed &playbackSpeed)
{
CLOGI("GetPlaySpeed in");
std::shared_ptr<ICastStreamManagerClient> targetCallback = callback_.lock();
playbackSpeed = PlaybackSpeed::SPEED_FORWARD_1_00_X;
if (!targetCallback) {
CLOGE("ICastStreamManagerClient is null");
playbackSpeed = PlaybackSpeed::SPEED_FORWARD_1_00_X;
return CAST_ENGINE_ERROR;
}
playbackSpeed = targetCallback->GetPlaySpeed();
return CAST_ENGINE_SUCCESS;
}