2021-03-24 16:52:41 +08:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
2021-03-27 09:13:10 +08:00
|
|
|
#include "DtlsTransport.hpp"
|
|
|
|
#include "IceServer.hpp"
|
|
|
|
#include "SrtpSession.hpp"
|
|
|
|
#include "StunPacket.hpp"
|
2021-03-31 17:15:26 +08:00
|
|
|
#include "Sdp.h"
|
2021-04-02 17:08:11 +08:00
|
|
|
#include "Poller/EventPoller.h"
|
|
|
|
#include "Network/Socket.h"
|
2021-04-02 23:01:58 +08:00
|
|
|
#include "Rtsp/RtspMediaSourceImp.h"
|
2021-04-03 00:04:52 +08:00
|
|
|
#include "Rtcp/RtcpContext.h"
|
2021-04-02 17:08:11 +08:00
|
|
|
using namespace toolkit;
|
|
|
|
using namespace mediakit;
|
2021-03-24 16:52:41 +08:00
|
|
|
|
2021-03-26 11:07:03 +08:00
|
|
|
class WebRtcTransport : public RTC::DtlsTransport::Listener, public RTC::IceServer::Listener {
|
2021-03-24 16:52:41 +08:00
|
|
|
public:
|
|
|
|
using Ptr = std::shared_ptr<WebRtcTransport>;
|
2021-03-27 10:16:49 +08:00
|
|
|
WebRtcTransport(const EventPoller::Ptr &poller);
|
|
|
|
~WebRtcTransport() override = default;
|
|
|
|
|
2021-04-02 17:08:11 +08:00
|
|
|
/**
|
|
|
|
* 消费对象
|
|
|
|
*/
|
2021-03-27 10:16:49 +08:00
|
|
|
virtual void onDestory();
|
2021-03-24 16:52:41 +08:00
|
|
|
|
2021-04-02 17:08:11 +08:00
|
|
|
/**
|
|
|
|
* 创建webrtc answer sdp
|
|
|
|
* @param offer offer sdp
|
|
|
|
* @return answer sdp
|
|
|
|
*/
|
2021-03-31 17:15:26 +08:00
|
|
|
std::string getAnswerSdp(const string &offer);
|
|
|
|
|
2021-04-02 17:08:11 +08:00
|
|
|
/**
|
|
|
|
* socket收到udp数据
|
|
|
|
* @param buf 数据指针
|
|
|
|
* @param len 数据长度
|
|
|
|
* @param tuple 数据来源
|
|
|
|
*/
|
|
|
|
void inputSockData(char *buf, size_t len, RTC::TransportTuple *tuple);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 发送rtp
|
|
|
|
* @param buf rtcp内容
|
|
|
|
* @param len rtcp长度
|
|
|
|
*/
|
2021-04-02 17:56:24 +08:00
|
|
|
void sendRtpPacket(char *buf, size_t len, bool flush);
|
2021-04-02 23:12:37 +08:00
|
|
|
void sendRtcpPacket(char *buf, size_t len, bool flush);
|
2021-03-24 16:52:41 +08:00
|
|
|
|
2021-03-26 11:07:03 +08:00
|
|
|
protected:
|
2021-04-02 17:08:11 +08:00
|
|
|
//// dtls相关的回调 ////
|
2021-03-26 11:07:03 +08:00
|
|
|
void OnDtlsTransportConnecting(const RTC::DtlsTransport *dtlsTransport) override {};
|
2021-04-02 17:08:11 +08:00
|
|
|
void OnDtlsTransportConnected(const RTC::DtlsTransport *dtlsTransport,
|
|
|
|
RTC::SrtpSession::CryptoSuite srtpCryptoSuite,
|
|
|
|
uint8_t *srtpLocalKey,
|
|
|
|
size_t srtpLocalKeyLen,
|
|
|
|
uint8_t *srtpRemoteKey,
|
|
|
|
size_t srtpRemoteKeyLen,
|
|
|
|
std::string &remoteCert) override;
|
2021-03-26 11:07:03 +08:00
|
|
|
|
|
|
|
void OnDtlsTransportFailed(const RTC::DtlsTransport *dtlsTransport) override {};
|
|
|
|
void OnDtlsTransportClosed(const RTC::DtlsTransport *dtlsTransport) override {};
|
|
|
|
void OnDtlsTransportSendData(const RTC::DtlsTransport *dtlsTransport, const uint8_t *data, size_t len) override;
|
|
|
|
void OnDtlsTransportApplicationDataReceived(const RTC::DtlsTransport *dtlsTransport, const uint8_t *data, size_t len) override {};
|
|
|
|
|
|
|
|
protected:
|
2021-04-02 17:08:11 +08:00
|
|
|
//// ice相关的回调 ///
|
2021-03-26 11:07:03 +08:00
|
|
|
void OnIceServerSendStunPacket(const RTC::IceServer *iceServer, const RTC::StunPacket *packet, RTC::TransportTuple *tuple) override;
|
|
|
|
void OnIceServerSelectedTuple(const RTC::IceServer *iceServer, RTC::TransportTuple *tuple) override;
|
|
|
|
void OnIceServerConnected(const RTC::IceServer *iceServer) override;
|
|
|
|
void OnIceServerCompleted(const RTC::IceServer *iceServer) override;
|
|
|
|
void OnIceServerDisconnected(const RTC::IceServer *iceServer) override;
|
|
|
|
|
2021-03-24 16:52:41 +08:00
|
|
|
protected:
|
2021-04-02 17:08:11 +08:00
|
|
|
virtual void onStartWebRTC() = 0;
|
|
|
|
virtual void onRtcConfigure(RtcConfigure &configure) const {}
|
2021-04-02 17:56:24 +08:00
|
|
|
virtual void onCheckSdp(SdpType type, RtcSession &sdp) const;
|
|
|
|
virtual void onSendSockData(const char *buf, size_t len, struct sockaddr_in *dst, bool flush = true) = 0;
|
|
|
|
|
|
|
|
virtual void onRtp(const char *buf, size_t len) = 0;
|
|
|
|
virtual void onRtcp(const char *buf, size_t len) = 0;
|
2021-03-24 16:52:41 +08:00
|
|
|
|
2021-04-02 20:35:43 +08:00
|
|
|
protected:
|
|
|
|
const RtcSession& getSdp(SdpType type) const;
|
|
|
|
|
2021-03-24 16:52:41 +08:00
|
|
|
private:
|
2021-04-02 17:56:24 +08:00
|
|
|
void onSendSockData(const char *buf, size_t len, bool flush = true);
|
2021-04-02 17:08:11 +08:00
|
|
|
void setRemoteDtlsFingerprint(const RtcSession &remote);
|
2021-03-24 16:52:41 +08:00
|
|
|
|
|
|
|
private:
|
2021-04-02 17:08:11 +08:00
|
|
|
std::shared_ptr<RTC::IceServer> _ice_server;
|
|
|
|
std::shared_ptr<RTC::DtlsTransport> _dtls_transport;
|
|
|
|
std::shared_ptr<RTC::SrtpSession> _srtp_session_send;
|
|
|
|
std::shared_ptr<RTC::SrtpSession> _srtp_session_recv;
|
2021-03-31 17:15:26 +08:00
|
|
|
RtcSession::Ptr _offer_sdp;
|
|
|
|
RtcSession::Ptr _answer_sdp;
|
2021-03-24 16:52:41 +08:00
|
|
|
};
|
|
|
|
|
2021-04-02 23:01:58 +08:00
|
|
|
class RtpReceiverImp;
|
|
|
|
|
2021-03-24 16:52:41 +08:00
|
|
|
class WebRtcTransportImp : public WebRtcTransport, public std::enable_shared_from_this<WebRtcTransportImp>{
|
|
|
|
public:
|
|
|
|
using Ptr = std::shared_ptr<WebRtcTransportImp>;
|
2021-04-02 17:08:11 +08:00
|
|
|
~WebRtcTransportImp() override = default;
|
2021-03-24 16:52:41 +08:00
|
|
|
|
2021-04-02 17:08:11 +08:00
|
|
|
/**
|
|
|
|
* 创建WebRTC对象
|
|
|
|
* @param poller 改对象需要绑定的线程
|
|
|
|
* @return 对象
|
|
|
|
*/
|
2021-03-27 10:16:49 +08:00
|
|
|
static Ptr create(const EventPoller::Ptr &poller);
|
2021-03-24 16:52:41 +08:00
|
|
|
|
2021-04-02 17:08:11 +08:00
|
|
|
/**
|
|
|
|
* 绑定rtsp媒体源
|
|
|
|
* @param src 媒体源
|
|
|
|
*/
|
2021-03-24 16:52:41 +08:00
|
|
|
void attach(const RtspMediaSource::Ptr &src);
|
|
|
|
|
|
|
|
protected:
|
2021-04-02 17:08:11 +08:00
|
|
|
void onStartWebRTC() override;
|
2021-04-02 17:56:24 +08:00
|
|
|
void onSendSockData(const char *buf, size_t len, struct sockaddr_in *dst, bool flush = true) override;
|
|
|
|
void onCheckSdp(SdpType type, RtcSession &sdp) const override;
|
2021-04-02 18:28:01 +08:00
|
|
|
void onRtcConfigure(RtcConfigure &configure) const override;
|
2021-04-02 17:56:24 +08:00
|
|
|
|
|
|
|
void onRtp(const char *buf, size_t len) override;
|
|
|
|
void onRtcp(const char *buf, size_t len) override;
|
2021-04-02 17:08:11 +08:00
|
|
|
|
|
|
|
private:
|
2021-03-27 10:16:49 +08:00
|
|
|
WebRtcTransportImp(const EventPoller::Ptr &poller);
|
|
|
|
void onDestory() override;
|
2021-04-02 17:56:24 +08:00
|
|
|
void onSendRtp(const RtpPacket::Ptr &rtp, bool flush);
|
2021-04-02 18:28:01 +08:00
|
|
|
SdpAttrCandidate::Ptr getIceCandidate() const;
|
2021-04-02 20:35:43 +08:00
|
|
|
bool canSendRtp() const;
|
2021-04-02 23:01:58 +08:00
|
|
|
bool canRecvRtp() const;
|
|
|
|
|
|
|
|
class RtpPayloadInfo {
|
|
|
|
public:
|
|
|
|
bool is_common_rtp;
|
|
|
|
const RtcCodecPlan *plan;
|
|
|
|
const RtcMedia *media;
|
|
|
|
std::shared_ptr<RtpReceiverImp> receiver;
|
2021-04-03 00:04:52 +08:00
|
|
|
RtcpContext::Ptr rtcp_context_recv;
|
|
|
|
RtcpContext::Ptr rtcp_context_send;
|
2021-04-02 23:01:58 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
void onSortedRtp(const RtpPayloadInfo &info,RtpPacket::Ptr rtp);
|
|
|
|
void onBeforeSortedRtp(const RtpPayloadInfo &info,const RtpPacket::Ptr &rtp);
|
2021-03-24 16:52:41 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
Socket::Ptr _socket;
|
|
|
|
RtspMediaSource::Ptr _src;
|
|
|
|
RtspMediaSource::RingType::RingReader::Ptr _reader;
|
2021-04-02 20:35:43 +08:00
|
|
|
RtcSession _answer_sdp;
|
2021-04-02 21:08:40 +08:00
|
|
|
mutable RtcSession _rtsp_send_sdp;
|
|
|
|
mutable uint8_t _send_rtp_pt[2] = {0, 0};
|
2021-04-02 23:01:58 +08:00
|
|
|
RtspMediaSourceImp::Ptr _push_src;
|
|
|
|
unordered_map<uint8_t, RtpPayloadInfo> _rtp_receiver;
|
2021-04-03 00:04:52 +08:00
|
|
|
unordered_map<uint32_t, RtpPayloadInfo*> _ssrc_info;
|
2021-04-02 23:12:37 +08:00
|
|
|
Ticker _pli_ticker;
|
2021-03-24 16:52:41 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|