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
23namespace OHOS {
24namespace CastEngine {
25namespace CastEngineService {
26using CastSessionRtsp::IRtspController;
27using CastSessionRtsp::IRtspListener;
28using CastSessionRtsp::ParamInfo;
29using CastSessionEnums::MessageId;
30
31class CastSessionImpl::ChannelManagerListenerImpl : public IChannelManagerListener {
32public:
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
42private:
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
51class CastSessionImpl::RtspListenerImpl : public IRtspListener {
52public:
53    explicit RtspListenerImpl(sptr<CastSessionImpl> session) : session_(session) {}
54
55    void OnSetup(const ParamInfo &param, int mediaPort, int remoteControlPort, const std::string &deviceId) override;
56    bool OnPlay(const ParamInfo &param, 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 &param) 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 &param) override;
69    void ProcessStreamMode(const ParamInfo &param, const std::string &deviceId) override;
70
71private:
72    wptr<CastSessionImpl> session_;
73};
74
75class CastSessionImpl::ConnectManagerListenerImpl : public IConnectManagerSessionListener {
76public:
77    explicit ConnectManagerListenerImpl(sptr<CastSessionImpl> session) : session_(session) {}
78
79    void NotifySessionEvent(const std::string &deviceId, int result) override;
80
81private:
82    wptr<CastSessionImpl> session_;
83};
84
85class CastSessionImpl::CastStreamListenerImpl : public ICastStreamListener {
86public:
87    explicit CastStreamListenerImpl(sptr<CastSessionImpl> session) : session_(session) {}
88    ~CastStreamListenerImpl() override;
89    bool SendActionToPeers(int action, const std::string &param) override;
90    void OnRenderReady(bool isReady) override;
91    void OnEvent(EventId eventId, const std::string &data) override;
92
93private:
94    wptr<CastSessionImpl> session_;
95};
96
97class CastSessionImpl::BaseState : public State {
98public:
99    BaseState(SessionState stateId, sptr<CastSessionImpl> session, std::shared_ptr<State> parentState = nullptr)
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
107protected:
108    wptr<CastSessionImpl> session_;
109    SessionState stateId_;
110    DISALLOW_EVIL_CONSTRUCTORS(BaseState);
111
112private:
113    void Enter() override {}
114};
115
116class CastSessionImpl::DefaultState : public BaseState {
117public:
118    DefaultState(SessionState stateId, sptr<CastSessionImpl> session, std::shared_ptr<State> parentState = nullptr)
119        : BaseState(stateId, session, parentState) {};
120
121protected:
122    void Enter() override;
123    void Exit() override;
124    bool HandleMessage(const Message &msg) override;
125    DISALLOW_EVIL_CONSTRUCTORS(DefaultState);
126};
127
128class CastSessionImpl::DisconnectedState : public BaseState {
129public:
130    DisconnectedState(SessionState stateId, sptr<CastSessionImpl> session, std::shared_ptr<State> parentState = nullptr)
131        : BaseState(stateId, session, parentState) {};
132
133protected:
134    void Enter() override;
135    void Exit() override;
136    bool HandleMessage(const Message &msg) override;
137    DISALLOW_EVIL_CONSTRUCTORS(DisconnectedState);
138};
139
140class CastSessionImpl::AuthingState : public BaseState {
141public:
142    AuthingState(SessionState stateId, sptr<CastSessionImpl> session, std::shared_ptr<State> parentState = nullptr)
143        : BaseState(stateId, session, parentState) {};
144
145protected:
146    void Enter() override;
147    void Exit() override;
148    bool HandleMessage(const Message &msg) override;
149    DISALLOW_EVIL_CONSTRUCTORS(AuthingState);
150};
151
152class CastSessionImpl::ConnectingState : public BaseState {
153public:
154    ConnectingState(SessionState stateId, sptr<CastSessionImpl> session, std::shared_ptr<State> parentState = nullptr)
155        : BaseState(stateId, session, parentState) {};
156
157protected:
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
170class CastSessionImpl::ConnectedState : public BaseState {
171public:
172    ConnectedState(SessionState stateId, sptr<CastSessionImpl> session, std::shared_ptr<State> parentState = nullptr)
173        : BaseState(stateId, session, parentState) {};
174
175protected:
176    void Enter() override;
177    void Exit() override;
178    bool HandleMessage(const Message &msg) override;
179    DISALLOW_EVIL_CONSTRUCTORS(ConnectedState);
180};
181
182class CastSessionImpl::PausedState : public BaseState {
183public:
184    explicit PausedState(SessionState stateId, sptr<CastSessionImpl> session,
185        std::shared_ptr<State> parentState = nullptr)
186        : BaseState(stateId, session, parentState) {};
187
188protected:
189    void Enter() override;
190    void Exit() override;
191    bool HandleMessage(const Message &msg) override;
192    DISALLOW_EVIL_CONSTRUCTORS(PausedState);
193};
194
195class CastSessionImpl::PlayingState : public BaseState {
196public:
197    explicit PlayingState(SessionState stateId, sptr<CastSessionImpl> session,
198        std::shared_ptr<State> parentState = nullptr)
199        : BaseState(stateId, session, parentState) {};
200
201protected:
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
209class CastSessionImpl::StreamState : public BaseState {
210public:
211    explicit StreamState(SessionState stateId, sptr<CastSessionImpl> session,
212        std::shared_ptr<State> parentState = nullptr)
213        : BaseState(stateId, session, parentState) {};
214
215protected:
216    void Enter() override;
217    void Exit() override;
218    bool HandleMessage(const Message &msg) override;
219    DISALLOW_EVIL_CONSTRUCTORS(StreamState);
220};
221
222class CastSessionImpl::DisconnectingState : public BaseState {
223public:
224    explicit DisconnectingState(SessionState stateId, sptr<CastSessionImpl> session,
225        std::shared_ptr<State> parentState = nullptr)
226        : BaseState(stateId, session, parentState) {};
227
228protected:
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