• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development 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  */
15 
16 #ifndef OHOS_SHARING_WFD_SOURCE_SESSION_H
17 #define OHOS_SHARING_WFD_SOURCE_SESSION_H
18 
19 #include <list>
20 #include <memory>
21 #include <thread>
22 #include "agent/session/base_session.h"
23 #include "extend/magic_enum/magic_enum.hpp"
24 #include "network/network_factory.h"
25 #include "protocol/rtsp/include/rtsp_request.h"
26 #include "protocol/rtsp/include/rtsp_response.h"
27 #include "sharing_hisysevent.h"
28 #include "timer.h"
29 #include "utils/utils.h"
30 #include "wfd_message.h"
31 #include "wfd_session_def.h"
32 
33 namespace OHOS {
34 namespace Sharing {
35 
36 class WfdSourceSession : public BaseSession,
37                          public IServerCallback,
38                          public std::enable_shared_from_this<WfdSourceSession> {
39 public:
40     enum class WfdSessionState { M0, M1, M2, M3, M4, M5, M6, M6_SENT, M6_DONE, M7, M7_WAIT, M7_SENT, M7_DONE, M8 };
41 
42     WfdSourceSession();
43     ~WfdSourceSession() override;
44 
45     int32_t HandleEvent(SharingEvent &event) override;
46     void UpdateOperation(SessionStatusMsg::Ptr &statusMsg) override;
47     void UpdateMediaStatus(SessionStatusMsg::Ptr &statusMsg) override;
48 
49 protected:
50     void NotifyProsumerInit(SessionStatusMsg::Ptr &statusMsg);
51     void NotifyAgentSessionStatus(SessionStatusMsg::Ptr &statusMsg);
52 
53 private:
54     class WfdSourceNetworkSession : public INetworkSessionCallback,
55                                     public std::enable_shared_from_this<WfdSourceNetworkSession> {
56     public:
57         WfdSourceNetworkSession(std::weak_ptr<INetworkSession> sessionPtr, std::weak_ptr<WfdSourceSession> serverPtr);
58         ~WfdSourceNetworkSession();
59 
60         void SetKeepAliveTimer();
61         void UnsetKeepAliveTimer();
62 
63         void OnSessionClose(int32_t fd) override;
64         void OnSessionWriteable(int32_t fd) override;
65         void OnSessionException(int32_t fd) override;
66         void OnSessionReadData(int32_t fd, DataBuffer::Ptr buf) override;
67 
68     private:
69         void OnSendKeepAlive() const;
70 
71     private:
72         uint32_t timerId_ = 0;
73         std::weak_ptr<INetworkSession> sessionPtr_;
74         std::weak_ptr<WfdSourceSession> serverPtr_;
75         std::unique_ptr<OHOS::Utils::Timer> timer_ = std::make_unique<OHOS::Utils::Timer>("RtspSourceSessionTimer");
76     };
77 
78     void HandleSessionInit(SharingEvent &event);
79     void HandleProsumerInitState(SharingEvent &event);
80 
81     bool StopWfdSession();
82     bool StartWfdSession();
83 
84     // impl IServerCallback
OnServerClose(int32_t fd)85     void OnServerClose(int32_t fd) override {}
OnServerWriteable(int32_t fd)86     void OnServerWriteable(int32_t fd) override {}
OnServerException(int32_t fd)87     void OnServerException(int32_t fd) override {}
88     void OnAccept(std::weak_ptr<INetworkSession> session) override;
89     void OnServerReadData(int32_t fd, DataBuffer::Ptr buf, INetworkSession::Ptr session = nullptr) override;
90 
91     bool HandleRequest(const RtspRequest &request, INetworkSession::Ptr &session);
92     bool HandleIDRRequest(const RtspRequest &request, int32_t cseq, INetworkSession::Ptr &session);
93     bool HandlePlayRequest(const RtspRequest &request, int32_t cseq, INetworkSession::Ptr &session);
94     bool HandlePauseRequest(const RtspRequest &request, int32_t cseq, INetworkSession::Ptr &session);
95     bool HandleSetupRequest(const RtspRequest &request, int32_t cseq, INetworkSession::Ptr &session);
96     bool HandleOptionRequest(const RtspRequest &request, int32_t cseq, INetworkSession::Ptr &session);
97     bool HandleTeardownRequest(const RtspRequest &request, int32_t cseq, INetworkSession::Ptr &session);
98 
99     bool HandleResponse(const RtspResponse &response, const std::string &message, INetworkSession::Ptr &session);
100     bool HandleM1Response(const RtspResponse &response, const std::string &message, INetworkSession::Ptr &session);
101     bool HandleM3Response(const RtspResponse &response, const std::string &message, INetworkSession::Ptr &session);
102     bool HandleM4Response(const RtspResponse &response, const std::string &message, INetworkSession::Ptr &session);
103     bool HandleM5Response(const RtspResponse &response, const std::string &message, INetworkSession::Ptr &session);
104     bool HandleM7Response(const RtspResponse &response, const std::string &message, INetworkSession::Ptr &session);
105     bool HandleM8Response(const RtspResponse &response, const std::string &message, INetworkSession::Ptr &session);
106 
107     bool SendM1Request(INetworkSession::Ptr &session);
108     bool SendM3Request(INetworkSession::Ptr &session);
109     bool SendM4Request(INetworkSession::Ptr &session);
110     bool SendM5Request(INetworkSession::Ptr &session);
111     bool SendM16Request(INetworkSession::Ptr &session);
112     bool SendM2Response(int32_t cseq, INetworkSession::Ptr &session);     // onOptionsRequest
113     bool SendM6Response(INetworkSession::Ptr &session, int32_t cseq);     // SETUP
114     bool SendM7Response(INetworkSession::Ptr &session, int32_t cseq);     // PLAY
115     bool SendM8Response(INetworkSession::Ptr &session, int32_t cseq);     // TEARDOWN
116     bool SendCommonResponse(int32_t cseq, INetworkSession::Ptr &session); // M4 M5 M8 M16
117 
118     void NotifyServiceError();
119 
120 private:
121     uint32_t sinkAgentId_ = 0;
122 
123     uint16_t cseq_ = 0;
124     uint16_t sinkRtpPort_ = 0;
125     uint16_t sourceRtpPort_ = 0;
126     uint16_t wfdDefaultPort_ = DEFAULT_WFD_CTRLPORT;
127     uint16_t rtspTimeoutCounts_ = MAX_RTSP_TIMEOUT_COUNTS;
128 
129     int32_t rtspServerFd_ = 0;
130     int32_t prosumerState_ = ERR_PROSUMER_INIT;
131     int32_t rtspTimeout_ = RTSP_SESSION_TIMEOUT;
132 
133     std::string sinkIp_;
134     std::string rtspUrl_;
135     std::string sourceIp_;
136     std::string sourceMac_;
137     std::string sessionID_;
138     std::string lastMessage_;
139 
140     WfdAudioCodec wfdAudioCodec_ = {CODEC_DEFAULT, AUDIO_48000_16_2};
141     WfdVideoFormatsInfo wfdVideoFormatsInfo_;
142     AudioFormat audioFormat_ = AUDIO_48000_16_2;
143     VideoFormat videoFormat_ = VIDEO_1920X1080_30;
144     WfdSessionState wfdState_ = WfdSessionState::M0;
145 
146     SharingHiSysEvent::Ptr sysEvent_ = nullptr;
147     NetworkFactory::ServerPtr rtspServerPtr_ = nullptr;
148     std::weak_ptr<INetworkSession> sessionPtr_;
149 };
150 
151 } // namespace Sharing
152 } // namespace OHOS
153 
154 #endif
155