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_SINK_SESSION_H 17 #define OHOS_SHARING_WFD_SINK_SESSION_H 18 19 #include <cstdint> 20 #include <future> 21 #include <list> 22 #include <memory> 23 #include <thread> 24 #include "agent/session/base_session.h" 25 #include "network/network_factory.h" 26 #include "protocol/rtsp/include/rtsp_request.h" 27 #include "protocol/rtsp/include/rtsp_response.h" 28 #include "utils/timeout_timer.h" 29 #include "wfd_session_def.h" 30 31 namespace OHOS { 32 namespace Sharing { 33 34 class WfdSinkSession : public BaseSession, 35 public IClientCallback, 36 public std::enable_shared_from_this<WfdSinkSession> { 37 public: 38 WfdSinkSession(); 39 ~WfdSinkSession() override; 40 41 void UpdateOperation(SessionStatusMsg::Ptr &statusMsg) override; 42 void UpdateMediaStatus(SessionStatusMsg::Ptr &statusMsg) override; 43 44 int32_t HandleEvent(SharingEvent &event) override; 45 46 protected: 47 void NotifyProsumerInit(SessionStatusMsg::Ptr &statusMsg); 48 void NotifyAgentSessionStatus(SessionStatusMsg::Ptr &statusMsg); 49 50 private: 51 // impl IClientCallback 52 void OnClientClose(int32_t fd) override; OnClientException(int32_t fd)53 void OnClientException(int32_t fd) override {} OnClientWriteable(int32_t fd)54 void OnClientWriteable(int32_t fd) override {} OnClientConnect(bool isSuccess)55 void OnClientConnect(bool isSuccess) override {} 56 void OnClientReadData(int32_t fd, DataBuffer::Ptr buf) override; 57 58 bool StartWfdSession(); 59 bool StopWfdSession(); 60 61 void HandleSessionInit(SharingEvent &event); 62 void HandleProsumerInitState(SharingEvent &event); 63 64 void NotifySessionInterrupted(); 65 void NotifyServiceError(SharingErrorCode errorCode = ERR_INTERACTION_FAILURE); 66 67 bool HandleM4Request(const std::string &message); 68 bool HandleTriggerMethod(int32_t cseq, const std::string &method); 69 void HandleRequest(const RtspRequest &request, const std::string &message); 70 void HandleSetParamRequest(const RtspRequest &request, const std::string &message); 71 72 void HandleM2Response(const RtspResponse &response, const std::string &message); 73 void HandleM6Response(const RtspResponse &response, const std::string &message); 74 void HandleM7Response(const RtspResponse &response, const std::string &message); 75 void HandleM8Response(const RtspResponse &response, const std::string &message); 76 void HandleCommonResponse(const RtspResponse &response, const std::string &message); 77 78 bool SendM2Request(); // M2/OPTIONS 79 bool SendM6Request(); // M6/SETUP 80 bool SendM7Request(); // M7/PLAY 81 bool SendM8Request(); // M8/TEARDOWN 82 bool SendIDRRequest(); // M13/SET_PARAMETER wfd-idr-request 83 bool SendM1Response(int32_t cseq); // M1/OPTIONS 84 bool SendCommonResponse(int32_t cseq); // M4, M5/SET_PARAMETER Triger, M8, M16/GET_PARAMETER keep-alive 85 bool SendM3Response(int32_t cseq, std::list<std::string> ¶ms); 86 87 private: 88 enum class WfdSessionState { INIT, READY, PLAYING, STOPPING }; 89 90 bool connected_ = false; 91 92 uint16_t localRtpPort_ = 0; 93 uint16_t remoteRtspPort_ = 0; 94 95 int32_t cseq_ = 0; 96 int32_t keepAliveTimeout_ = 0; 97 98 std::string rtspUrl_; 99 std::string remoteMac_; 100 std::string rtspSession_; 101 std::string lastMessage_; 102 std::string remoteRtspIp_; 103 104 std::unique_ptr<TimeoutTimer> timeoutTimer_ = nullptr; 105 std::unique_ptr<TimeoutTimer> keepAliveTimer_ = nullptr; 106 std::map<int, std::function<void(const RtspResponse &response, const std::string &message)>> responseHandlers_; 107 108 AudioFormat audioFormat_ = AUDIO_48000_16_2; 109 VideoFormat videoFormat_ = VIDEO_1920X1080_30; 110 NetworkFactory::ClientPtr rtspClient_ = nullptr; 111 WfdSessionState wfdState_ = WfdSessionState::INIT; 112 }; 113 114 } // namespace Sharing 115 } // namespace OHOS 116 #endif 117