ZLMediaKit/webrtc/WebRtcTransport.h

131 lines
3.8 KiB
C++
Raw Normal View History

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-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;
/// 销毁对象
virtual void onDestory();
2021-03-24 16:52:41 +08:00
2021-03-31 17:15:26 +08:00
std::string getAnswerSdp(const string &offer);
2021-03-24 16:52:41 +08:00
/// 收到udp数据
/// \param buf
/// \param len
/// \param remote_address
2021-03-26 11:07:03 +08:00
void OnInputDataPacket(char *buf, size_t len, RTC::TransportTuple *tuple);
2021-03-24 16:52:41 +08:00
/// 发送rtp
/// \param buf
/// \param len
void WritRtpPacket(char *buf, size_t len);
2021-03-26 11:07:03 +08:00
protected:
// dtls相关的回调
void OnDtlsTransportConnecting(const RTC::DtlsTransport *dtlsTransport) override {};
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;
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:
//ice相关的回调
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:
/// 输出udp数据
/// \param buf
/// \param len
/// \param dst
virtual void onWrite(const char *buf, size_t len, struct sockaddr_in *dst) = 0;
virtual uint32_t getSSRC() const = 0;
virtual uint16_t getPort() const = 0;
virtual std::string getIP() const = 0;
virtual int getPayloadType() const = 0;
2021-03-26 11:07:03 +08:00
virtual void onDtlsConnected() = 0;
2021-03-24 16:52:41 +08:00
private:
2021-03-26 11:07:03 +08:00
void onWrite(const char *buf, size_t len);
2021-03-24 16:52:41 +08:00
private:
2021-03-26 11:07:03 +08:00
std::shared_ptr<RTC::IceServer> ice_server_;
std::shared_ptr<RTC::DtlsTransport> dtls_transport_;
2021-03-24 16:52:41 +08:00
std::shared_ptr<RTC::SrtpSession> srtp_session_;
2021-04-01 10:09:50 +08:00
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
};
#include "Poller/EventPoller.h"
#include "Network/Socket.h"
#include "Rtsp/RtspMediaSource.h"
using namespace toolkit;
using namespace mediakit;
class WebRtcTransportImp : public WebRtcTransport, public std::enable_shared_from_this<WebRtcTransportImp>{
public:
using Ptr = std::shared_ptr<WebRtcTransportImp>;
2021-03-27 10:16:49 +08:00
static Ptr create(const EventPoller::Ptr &poller);
2021-03-24 16:52:41 +08:00
~WebRtcTransportImp() override = default;
void attach(const RtspMediaSource::Ptr &src);
protected:
void onWrite(const char *buf, size_t len, struct sockaddr_in *dst) override;
2021-03-27 22:23:38 +08:00
int getPayloadType() const override;
2021-03-24 16:52:41 +08:00
uint32_t getSSRC() const override;
uint16_t getPort() const override;
std::string getIP() const override;
2021-03-26 11:07:03 +08:00
void onDtlsConnected() override;
2021-03-27 10:16:49 +08:00
WebRtcTransportImp(const EventPoller::Ptr &poller);
void onDestory() override;
2021-03-24 16:52:41 +08:00
private:
Socket::Ptr _socket;
RtspMediaSource::Ptr _src;
RtspMediaSource::RingType::RingReader::Ptr _reader;
};