1 /* 2 * Copyright (c) 2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 * Description: Cast session implement class. 15 * Author: zhangge 16 * Create: 2022-07-19 17 */ 18 19 #ifndef CAST_SESSION_IMPL_H 20 #define CAST_SESSION_IMPL_H 21 #include "cast_session_impl_class.h" 22 23 namespace OHOS { 24 namespace CastEngine { 25 namespace CastEngineService { 26 using CastSessionRtsp::IRtspController; 27 using CastSessionRtsp::IRtspListener; 28 using CastSessionRtsp::ParamInfo; 29 using CastSessionEnums::MessageId; 30 31 class CastSessionImpl::ChannelManagerListenerImpl : public IChannelManagerListener { 32 public: ChannelManagerListenerImpl(sptr<CastSessionImpl> session)33 explicit ChannelManagerListenerImpl(sptr<CastSessionImpl> session) : session_(session) {} 34 35 void OnChannelCreated(std::shared_ptr<Channel> channel) override; 36 void OnChannelOpenFailed(ChannelRequest &channelRequest, int errorCode) override; 37 void OnChannelRemoved(std::shared_ptr<Channel> channel) override; 38 void OnChannelError(std::shared_ptr<Channel> channel, int errorCode) override; 39 bool IsMediaChannelReady(); 40 void SetMediaChannel(ModuleType moduleType); 41 42 private: 43 const static unsigned int UNCONNECTED_STATE = 0; 44 const static unsigned int VIDEO_CHANNEL_CONNECTED = 1; 45 const static unsigned int AUDIO_CHANNEL_CONNECTED = 2; 46 47 wptr<CastSessionImpl> session_; 48 unsigned int mediaChannelState_{ UNCONNECTED_STATE }; 49 }; 50 51 class CastSessionImpl::RtspListenerImpl : public IRtspListener { 52 public: RtspListenerImpl(sptr<CastSessionImpl> session)53 explicit RtspListenerImpl(sptr<CastSessionImpl> session) : session_(session) {} 54 55 void OnSetup(const ParamInfo ¶m, int mediaPort, int remoteControlPort, const std::string &deviceId) override; 56 bool OnPlay(const ParamInfo ¶m, int port, const std::string &deviceId) override; 57 bool OnPause() override; 58 void OnTearDown() override; 59 void OnError(int errCode) override; 60 bool OnPlayerReady(const ParamInfo &clientParam, const std::string &deviceId, int readyFlag) override; 61 62 void NotifyTrigger(int trigger) override; 63 void NotifyEventChange(int moduleId, int event, const std::string ¶m) override; 64 void NotifyModuleCustomParamsNegotiation(const std::string &mediaParams, 65 const std::string &controllerParams) override; 66 bool NotifyEvent(int event) override; 67 68 int StartMediaVtp(const ParamInfo ¶m) override; 69 void ProcessStreamMode(const ParamInfo ¶m, const std::string &deviceId) override; 70 71 private: 72 wptr<CastSessionImpl> session_; 73 }; 74 75 class CastSessionImpl::ConnectManagerListenerImpl : public IConnectManagerSessionListener { 76 public: ConnectManagerListenerImpl(sptr<CastSessionImpl> session)77 explicit ConnectManagerListenerImpl(sptr<CastSessionImpl> session) : session_(session) {} 78 79 void NotifySessionEvent(const std::string &deviceId, int result) override; 80 81 private: 82 wptr<CastSessionImpl> session_; 83 }; 84 85 class CastSessionImpl::CastStreamListenerImpl : public ICastStreamListener { 86 public: CastStreamListenerImpl(sptr<CastSessionImpl> session)87 explicit CastStreamListenerImpl(sptr<CastSessionImpl> session) : session_(session) {} 88 ~CastStreamListenerImpl() override; 89 bool SendActionToPeers(int action, const std::string ¶m) override; 90 void OnRenderReady(bool isReady) override; 91 void OnEvent(EventId eventId, const std::string &data) override; 92 93 private: 94 wptr<CastSessionImpl> session_; 95 }; 96 97 class CastSessionImpl::BaseState : public State { 98 public: 99 BaseState(SessionState stateId, sptr<CastSessionImpl> session, std::shared_ptr<State> parentState = nullptr) State(parentState)100 : State(parentState), session_(session), stateId_(stateId) {}; 101 102 void Enter(SessionState state); 103 void Exit() override; 104 bool HandleMessage(const Message &msg) override; 105 SessionState GetStateId() const; 106 107 protected: 108 wptr<CastSessionImpl> session_; 109 SessionState stateId_; 110 DISALLOW_EVIL_CONSTRUCTORS(BaseState); 111 112 private: Enter()113 void Enter() override {} 114 }; 115 116 class CastSessionImpl::DefaultState : public BaseState { 117 public: 118 DefaultState(SessionState stateId, sptr<CastSessionImpl> session, std::shared_ptr<State> parentState = nullptr) BaseState(stateId,session,parentState)119 : BaseState(stateId, session, parentState) {}; 120 121 protected: 122 void Enter() override; 123 void Exit() override; 124 bool HandleMessage(const Message &msg) override; 125 DISALLOW_EVIL_CONSTRUCTORS(DefaultState); 126 }; 127 128 class CastSessionImpl::DisconnectedState : public BaseState { 129 public: 130 DisconnectedState(SessionState stateId, sptr<CastSessionImpl> session, std::shared_ptr<State> parentState = nullptr) BaseState(stateId,session,parentState)131 : BaseState(stateId, session, parentState) {}; 132 133 protected: 134 void Enter() override; 135 void Exit() override; 136 bool HandleMessage(const Message &msg) override; 137 DISALLOW_EVIL_CONSTRUCTORS(DisconnectedState); 138 }; 139 140 class CastSessionImpl::AuthingState : public BaseState { 141 public: 142 AuthingState(SessionState stateId, sptr<CastSessionImpl> session, std::shared_ptr<State> parentState = nullptr) BaseState(stateId,session,parentState)143 : BaseState(stateId, session, parentState) {}; 144 145 protected: 146 void Enter() override; 147 void Exit() override; 148 bool HandleMessage(const Message &msg) override; 149 DISALLOW_EVIL_CONSTRUCTORS(AuthingState); 150 }; 151 152 class CastSessionImpl::ConnectingState : public BaseState { 153 public: 154 ConnectingState(SessionState stateId, sptr<CastSessionImpl> session, std::shared_ptr<State> parentState = nullptr) BaseState(stateId,session,parentState)155 : BaseState(stateId, session, parentState) {}; 156 157 protected: 158 void Enter() override; 159 void Exit() override; 160 bool HandleMessage(const Message &msg) override; 161 void HandleSetupMessage(const Message &msg, sptr<CastSessionImpl> session); 162 void HandleSetupSuccessMessage(const Message &msg, const MessageId &msgId, sptr<CastSessionImpl> session); 163 void HandleDisconnectMessage(const Message &msg, sptr<CastSessionImpl> session); 164 void HandleErrorMessage(const Message &msg, sptr<CastSessionImpl> session); 165 void HandleRenderReadyMessage(const Message &msg, sptr<CastSessionImpl> session); 166 void HandleConnectMessage(const Message &msg, const MessageId &msgId, sptr<CastSessionImpl> session); 167 DISALLOW_EVIL_CONSTRUCTORS(ConnectingState); 168 }; 169 170 class CastSessionImpl::ConnectedState : public BaseState { 171 public: 172 ConnectedState(SessionState stateId, sptr<CastSessionImpl> session, std::shared_ptr<State> parentState = nullptr) BaseState(stateId,session,parentState)173 : BaseState(stateId, session, parentState) {}; 174 175 protected: 176 void Enter() override; 177 void Exit() override; 178 bool HandleMessage(const Message &msg) override; 179 DISALLOW_EVIL_CONSTRUCTORS(ConnectedState); 180 }; 181 182 class CastSessionImpl::PausedState : public BaseState { 183 public: 184 explicit PausedState(SessionState stateId, sptr<CastSessionImpl> session, 185 std::shared_ptr<State> parentState = nullptr) BaseState(stateId,session,parentState)186 : BaseState(stateId, session, parentState) {}; 187 188 protected: 189 void Enter() override; 190 void Exit() override; 191 bool HandleMessage(const Message &msg) override; 192 DISALLOW_EVIL_CONSTRUCTORS(PausedState); 193 }; 194 195 class CastSessionImpl::PlayingState : public BaseState { 196 public: 197 explicit PlayingState(SessionState stateId, sptr<CastSessionImpl> session, 198 std::shared_ptr<State> parentState = nullptr) BaseState(stateId,session,parentState)199 : BaseState(stateId, session, parentState) {}; 200 201 protected: 202 void Enter() override; 203 void Exit() override; 204 bool HandleMessage(const Message &msg) override; 205 DISALLOW_EVIL_CONSTRUCTORS(PlayingState); 206 bool RecvActionEventFromPeers(const Message &msg); 207 }; 208 209 class CastSessionImpl::StreamState : public BaseState { 210 public: 211 explicit StreamState(SessionState stateId, sptr<CastSessionImpl> session, 212 std::shared_ptr<State> parentState = nullptr) BaseState(stateId,session,parentState)213 : BaseState(stateId, session, parentState) {}; 214 215 protected: 216 void Enter() override; 217 void Exit() override; 218 bool HandleMessage(const Message &msg) override; 219 DISALLOW_EVIL_CONSTRUCTORS(StreamState); 220 }; 221 222 class CastSessionImpl::DisconnectingState : public BaseState { 223 public: 224 explicit DisconnectingState(SessionState stateId, sptr<CastSessionImpl> session, 225 std::shared_ptr<State> parentState = nullptr) BaseState(stateId,session,parentState)226 : BaseState(stateId, session, parentState) {}; 227 228 protected: 229 void Enter() override; 230 void Exit() override; 231 bool HandleMessage(const Message &msg) override; 232 DISALLOW_EVIL_CONSTRUCTORS(DisconnectingState); 233 }; 234 } // namespace CastEngineService 235 } // namespace CastEngine 236 } // namespace OHOS 237 238 #endif 239