• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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: gaoshuai
16  * Create: 2023-11
17  */
18 
19 #ifndef CAST_SESSION_IMPL_CLASS_H
20 #define CAST_SESSION_IMPL_CLASS_H
21 
22 #include <array>
23 #include <condition_variable>
24 #include <mutex>
25 #include <memory>
26 #include <optional>
27 #include <set>
28 #include <utility>
29 
30 #include "cast_engine_common.h"
31 #include "cast_session_common.h"
32 #include "cast_session_enums.h"
33 #include "cast_session_impl_stub.h"
34 #include "connection_manager_listener.h"
35 #include "channel.h"
36 #include "channel_info.h"
37 #include "channel_manager.h"
38 #include "channel_manager_listener.h"
39 #include "channel_request.h"
40 #include "message.h"
41 #include "i_rtsp_controller.h"
42 #include "oh_remote_control_event.h"
43 #include "rtsp_listener.h"
44 #include "rtsp_param_info.h"
45 #include "state_machine.h"
46 #include "i_cast_stream_listener.h"
47 #include "i_cast_stream_manager.h"
48 
49 namespace OHOS {
50 namespace CastEngine {
51 namespace CastEngineService {
52 using CastSessionRtsp::IRtspController;
53 using CastSessionRtsp::IRtspListener;
54 using CastSessionRtsp::ParamInfo;
55 using CastSessionEnums::MessageId;
56 
57 class CastSessionImpl : public StateMachine,
58     public CastSessionImplStub,
59     public std::enable_shared_from_this<CastSessionImpl> {
60 public:
61     CastSessionImpl(const CastSessionProperty &property, const CastLocalDevice &localDevice);
62     ~CastSessionImpl() override;
63     void SetServiceCallbackForRelease(const std::function<void(int)> &callback);
64     int32_t RegisterListener(sptr<ICastSessionListenerImpl> listener) override;
65     int32_t UnregisterListener() override;
66     int32_t AddDevice(const CastRemoteDevice &remoteDevice) override;
67     bool AddDevice(const CastInnerRemoteDevice &remoteDevice) override;
68     int32_t RemoveDevice(const std::string &deviceId) override;
69     int32_t StartAuth(const AuthInfo &authInfo) override;
70     int32_t GetSessionId(std::string &sessionId) override;
71     int32_t GetDeviceState(const std::string &deviceId, DeviceState &deviceState) override;
72     int32_t SetSessionProperty(const CastSessionProperty &property) override;
73     bool ReleaseSessionResources(pid_t pid) override;
74     int32_t CreateMirrorPlayer(sptr<IMirrorPlayerImpl> &mirrorPlayer) override;
75     int32_t CreateStreamPlayer(sptr<IStreamPlayerIpc> &streamPlayer) override;
76     std::shared_ptr<ICastStreamManager> CreateStreamPlayerManager();
77     bool DestroyMirrorPlayer();
78     bool DestroyStreamPlayer();
79     void Stop() override;
80     int32_t Release() override;
81     bool Init();
82     int32_t NotifyEvent(EventId eventId, std::string &jsonParam) override;
83     int32_t SetCastMode(CastMode mode, std::string &jsonParam) override;
84 
85     int32_t GetSessionProtocolType(ProtocolType &protocolType) override;
86     void SetSessionProtocolType(ProtocolType protocolType) override;
87 
88     int32_t Play(const std::string &deviceId);
89     int32_t Pause(const std::string &deviceId);
90     int32_t SetSurface(sptr<IBufferProducer> producer);
91     int32_t DeliverInputEvent(const OHRemoteControlEvent &event);
92     int32_t InjectEvent(const OHRemoteControlEvent &event);
93     int32_t GetDisplayId(std::string &displayId);
94     int32_t ResizeVirtualScreen(uint32_t width, uint32_t height);
95 
96 private:
97 
98     static const std::array<std::string, static_cast<size_t>(MessageId::MSG_ID_MAX)> MESSAGE_ID_STRING;
99 
100     enum class ModuleState : uint8_t {
101         IDLE,
102         STARTING,
103         START_SUCCESS,
104         START_FAILED
105     };
106 
107     enum class CastSessionRemoteEventId : uint8_t {
108         SWITCH_TO_UI = 1,
109         SWITCH_TO_MIRROR,
110         READY_TO_PLAYING,
111     };
112     class ChannelManagerListenerImpl;
113     class RtspListenerImpl;
114     class ConnectManagerListenerImpl;
115     class CastStreamListenerImpl;
116     class BaseState;
117     class DefaultState;
118     class DisconnectedState;
119     class AuthingState;
120     class ConnectingState;
121     class ConnectedState;
122     class PausedState;
123     class PlayingState;
124     class DisconnectingState;
125     class StreamState;
126 
127     static constexpr int MODULE_ID_MEDIA = 1001;
128     static constexpr int MODULE_ID_RC = 1002;
129     static constexpr int MODULE_ID_CAST_STREAM = 1009;
130     static constexpr int MODULE_ID_CAST_SESSION = 2000;
131     static constexpr int MODULE_ID_UI = 2001;
132     static constexpr double CAST_VERSION = 1.1;
133     static constexpr int TIMEOUT_CONNECT = 60 * 1000;
134     static constexpr int INVALID_PORT = -1;
135     static constexpr int UNUSED_VALUE = -1;
136     static constexpr int NO_DELAY = 0;
137     static constexpr int UNNEEDED_PORT = -2;
138     static constexpr unsigned int SOCKET_PORT_BITS = 16;
139     static constexpr unsigned int SOCKET_PORT_MASK = 0xFFFF;
140     static constexpr int ERR_CODE = -1;
141     static constexpr int ID_INC_STEP = 1;
142     static constexpr int RENDER_READY = 1;
143     static constexpr int NOT_RENDER_READY = 0;
144     static constexpr int MAX_SESSION_ID = 0xFFFFFFF;
145     static constexpr int MAX_PORT = 65535;
146     static constexpr int MIN_PORT = 1024;
147 
148     void SetLocalDevice(const CastLocalDevice &localDevice);
149     std::shared_ptr<CastRemoteDeviceInfo> FindRemoteDevice(const std::string &deviceId);
150     std::shared_ptr<CastRemoteDeviceInfo> FindRemoteDeviceLocked(const std::string &deviceId);
151     void UpdateRemoteDeviceStateLocked(const std::string &deviceId, DeviceState state);
152     void UpdateRemoteDeviceSessionId(const std::string &deviceId, int sessionId);
153     void UpdateRemoteDeviceInfoFromCastDeviceDataManager(const std::string &deviceId);
154     void ChangeDeviceState(DeviceState state, const std::string &deviceId,
155         const EventCode eventCode = EventCode::DEFAULT_EVENT);
156     void ChangeDeviceStateLocked(
157         DeviceState state, const std::string &deviceId, const EventCode eventCode = EventCode::DEFAULT_EVENT);
158     void ReportDeviceStateInfo(DeviceState state, const std::string &deviceId, const EventCode eventCode);
159     void OnSessionEvent(const std::string& deviceId, const EventCode eventCode) override;
160     void OnEvent(EventId eventId, const std::string &data);
161     void OnRemoteCtrlEvent(int eventType, const uint8_t *data, uint32_t len);
162     bool TransferTo(std::shared_ptr<BaseState> state);
163     bool IsAllowTransferState(SessionState desiredState) const;
164     std::string GetCurrentRemoteDeviceId();
165     int ProcessConnect(const Message &msg);
166     bool ProcessSetUp(const Message &msg);
167     bool ProcessSetUpSuccess(const Message &msg);
168     bool ProcessPlay(const Message &msg);
169     bool ProcessPlayReq(const Message &msg);
170     bool ProcessPause(const Message &msg);
171     bool ProcessPauseReq(const Message &msg);
172     bool ProcessDisconnect(const Message &msg);
173     bool ProcessError(const Message &msg);
174     bool ProcessTriggerReq(const Message &msg);
175     bool ProcessUpdateVideoSize(const Message &msg);
176     bool ProcessStateEvent(MessageId msgId, const Message &msg);
177     bool IsSupportFeature(const std::set<int> &featureSet, int supportFeature);
178     bool IsConnected() const;
179     bool SendEventChange(int moduleId, int event, const std::string &param);
180     void RemoveRemoteDevice(const std::string &deviceId);
181     bool AddRemoteDevice(const CastRemoteDeviceInfo &remoteDeviceInfo);
182     std::shared_ptr<ChannelRequest> BuildChannelRequest(const std::string &remoteDeviceId, bool isSupportVtp,
183         ModuleType moduleType);
184     int SetupRemoteControl(const CastInnerRemoteDevice &remote);
185     bool IsVtpUsed(ChannelType type);
186     bool IsChannelClient(ChannelType type);
187     bool IsChannelNeeded(ChannelType type);
188     std::pair<int, int> GetMediaPort(ChannelType type, int port);
189     std::optional<int> SetupMedia(const CastInnerRemoteDevice &remote, ChannelType type, int ports);
190     void InitRtspParamInfo(std::shared_ptr<CastRemoteDeviceInfo> remoteDeviceInfo);
191     std::shared_ptr<ICastStreamManager> StreamManagerGetter();
192     sptr<IMirrorPlayerImpl> MirrorPlayerGetter();
193     void ProcessRtspEvent(int moduleId, int event, const std::string &param);
194     void OtherAddDevice(const CastInnerRemoteDevice &remoteDevice);
195     void SendConsultData(const std::string &deviceId, int port);
196 
197     void MirrorRcvVideoFrame();
198     bool ProcessSetCastMode(const Message &msg);
199     void UpdateScreenInfo(uint64_t screenId, uint16_t width, uint16_t height);
200     void UpdateDefaultDisplayRotationInfo(int rotation, uint16_t width, uint16_t height);
201 
202     bool IsStreamMode();
203     std::string GetPlayerControllerCapability();
204     bool IsSink();
205     int CreateStreamChannel();
206     void SendCastRenderReadyOption(int isReady);
207     void OnEventInner(sptr<CastSessionImpl> session, EventId eventId, const std::string &jsonParam);
208     void WaitSinkSetProperty();
209 
210     std::shared_ptr<DefaultState> defaultState_;
211     std::shared_ptr<DisconnectedState> disconnectedState_;
212     std::shared_ptr<AuthingState> authingState_;
213     std::shared_ptr<ConnectingState> connectingState_;
214     std::shared_ptr<ConnectedState> connectedState_;
215     std::shared_ptr<PausedState> pausedState_;
216     std::shared_ptr<PlayingState> playingState_;
217     std::shared_ptr<DisconnectingState> disconnectingState_;
218 
219     bool isMirrorPlaying_ { false };
220     std::shared_ptr<StreamState> streamState_;
221 
222     static std::atomic<int> idCount_;
223 
224     std::function<void(int)> serviceCallback_;
225     std::map<pid_t, sptr<ICastSessionListenerImpl>> listeners_;
226     sptr<IMirrorPlayerImpl> mirrorPlayer_;
227     std::shared_ptr<ICastStreamManager> streamManager_;
228     int sessionId_{ -1 };
229     int rtspPort_{ -1 };
230     std::list<CastRemoteDeviceInfo> remoteDeviceList_;
231     CastLocalDevice localDevice_;
232     CastSessionProperty property_;
233     ParamInfo rtspParamInfo_;
234     SessionState sessionState_{ SessionState::DISCONNECTED };
235 
236     std::shared_ptr<ChannelManager> channelManager_;
237     std::shared_ptr<ChannelManagerListenerImpl> channelManagerListener_;
238     std::shared_ptr<IRtspController> rtspControl_;
239     std::shared_ptr<RtspListenerImpl> rtspListener_;
240     std::shared_ptr<ConnectManagerListenerImpl> connectManagerListener_;
241 
242     std::mutex mutex_;
243     std::mutex mirrorMutex_;
244     std::mutex streamMutex_;
245     std::mutex serviceCallbackMutex_;
246     std::condition_variable cond_;
247     std::condition_variable setProperty_;
248     std::mutex dataTransMutex_;
249     CastMode castMode_ = CastMode::MIRROR_CAST;
250     ModuleState mediaState_{ ModuleState::IDLE };
251     ModuleState remoteCtlState_{ ModuleState::IDLE };
252 
253     using StateProcessor = bool (CastSessionImpl::*)(const Message &msg);
254     std::array<StateProcessor, static_cast<size_t>(MessageId::MSG_ID_MAX)> stateProcessor_{
255         nullptr,                                  // MSG_CONNECT
256         &CastSessionImpl::ProcessSetUp,           // MSG_SETUP
257         &CastSessionImpl::ProcessSetUpSuccess,    // MSG_SETUP_SUCCESS
258         nullptr,                                  // MSG_SETUP_FAILED
259         nullptr,                                  // MSG_SETUP_DONE
260         &CastSessionImpl::ProcessPlay,            // MSG_PLAY
261         &CastSessionImpl::ProcessPause,           // MSG_PAUSE
262         &CastSessionImpl::ProcessPlayReq,         // MSG_PLAY_REQ
263         &CastSessionImpl::ProcessPauseReq,        // MSG_PAUSE_REQ
264         &CastSessionImpl::ProcessDisconnect,      // MSG_DISCONNECT
265         &CastSessionImpl::ProcessDisconnect,      // MSG_CONNECT_TIMEOUT
266         &CastSessionImpl::ProcessTriggerReq,      // MSG_PROCESS_TRIGGER_REQ
267         &CastSessionImpl::ProcessUpdateVideoSize, // MSG_UPDATE_VIDEO_SIZE
268         nullptr,                                  // MSG_STREAM_RECV_ACTION_EVENT_FROM_PEERS
269         nullptr,                                  // MSG_STREAM_SEND_ACTION_EVENT_TO_PEERS
270         nullptr,                                  // MSG_PEER_RENDER_READY
271         &CastSessionImpl::ProcessError,           // MSG_ERROR
272     };
273 };
274 } // namespace CastEngineService
275 } // namespace CastEngine
276 } // namespace OHOS
277 
278 #endif
279