2021-03-27 09:13:10 +08:00
|
|
|
|
#include "WebRtcTransport.h"
|
2021-03-24 16:52:41 +08:00
|
|
|
|
#include <iostream>
|
|
|
|
|
#include "Rtcp/Rtcp.h"
|
2021-04-02 23:01:58 +08:00
|
|
|
|
#include "Rtsp/RtpReceiver.h"
|
|
|
|
|
#define RTX_SSRC_OFFSET 2
|
|
|
|
|
#define RTP_CNAME "zlmediakit-rtp"
|
|
|
|
|
#define RTX_CNAME "zlmediakit-rtx"
|
|
|
|
|
|
2021-03-24 16:52:41 +08:00
|
|
|
|
|
2021-03-27 10:16:49 +08:00
|
|
|
|
WebRtcTransport::WebRtcTransport(const EventPoller::Ptr &poller) {
|
2021-04-02 17:08:11 +08:00
|
|
|
|
_dtls_transport = std::make_shared<RTC::DtlsTransport>(poller, this);
|
|
|
|
|
_ice_server = std::make_shared<RTC::IceServer>(this, makeRandStr(4), makeRandStr(24));
|
2021-03-24 16:52:41 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-27 10:16:49 +08:00
|
|
|
|
void WebRtcTransport::onDestory(){
|
2021-04-02 17:08:11 +08:00
|
|
|
|
_dtls_transport = nullptr;
|
|
|
|
|
_ice_server = nullptr;
|
2021-03-27 09:38:13 +08:00
|
|
|
|
}
|
2021-03-24 16:52:41 +08:00
|
|
|
|
|
2021-03-26 11:07:03 +08:00
|
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
void WebRtcTransport::OnIceServerSendStunPacket(const RTC::IceServer *iceServer, const RTC::StunPacket *packet, RTC::TransportTuple *tuple) {
|
2021-04-02 17:08:11 +08:00
|
|
|
|
onSendSockData((char *) packet->GetData(), packet->GetSize(), (struct sockaddr_in *) tuple);
|
2021-03-26 11:07:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WebRtcTransport::OnIceServerSelectedTuple(const RTC::IceServer *iceServer, RTC::TransportTuple *tuple) {
|
|
|
|
|
InfoL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WebRtcTransport::OnIceServerConnected(const RTC::IceServer *iceServer) {
|
|
|
|
|
InfoL;
|
2021-04-01 14:16:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WebRtcTransport::OnIceServerCompleted(const RTC::IceServer *iceServer) {
|
|
|
|
|
InfoL;
|
2021-04-01 10:09:50 +08:00
|
|
|
|
if (_answer_sdp->media[0].role == DtlsRole::passive) {
|
2021-04-02 17:08:11 +08:00
|
|
|
|
_dtls_transport->Run(RTC::DtlsTransport::Role::SERVER);
|
2021-04-01 10:09:50 +08:00
|
|
|
|
} else {
|
2021-04-02 17:08:11 +08:00
|
|
|
|
_dtls_transport->Run(RTC::DtlsTransport::Role::CLIENT);
|
2021-04-01 10:09:50 +08:00
|
|
|
|
}
|
2021-03-26 11:07:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WebRtcTransport::OnIceServerDisconnected(const RTC::IceServer *iceServer) {
|
|
|
|
|
InfoL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
void WebRtcTransport::OnDtlsTransportConnected(
|
|
|
|
|
const RTC::DtlsTransport *dtlsTransport,
|
|
|
|
|
RTC::SrtpSession::CryptoSuite srtpCryptoSuite,
|
|
|
|
|
uint8_t *srtpLocalKey,
|
|
|
|
|
size_t srtpLocalKeyLen,
|
|
|
|
|
uint8_t *srtpRemoteKey,
|
|
|
|
|
size_t srtpRemoteKeyLen,
|
|
|
|
|
std::string &remoteCert) {
|
|
|
|
|
InfoL;
|
2021-04-02 17:08:11 +08:00
|
|
|
|
_srtp_session_send = std::make_shared<RTC::SrtpSession>(RTC::SrtpSession::Type::OUTBOUND, srtpCryptoSuite, srtpLocalKey, srtpLocalKeyLen);
|
2021-04-02 23:01:58 +08:00
|
|
|
|
_srtp_session_recv = std::make_shared<RTC::SrtpSession>(RTC::SrtpSession::Type::INBOUND, srtpCryptoSuite, srtpRemoteKey, srtpRemoteKeyLen);
|
2021-04-02 17:08:11 +08:00
|
|
|
|
onStartWebRTC();
|
2021-03-26 11:07:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WebRtcTransport::OnDtlsTransportSendData(const RTC::DtlsTransport *dtlsTransport, const uint8_t *data, size_t len) {
|
2021-04-02 17:08:11 +08:00
|
|
|
|
onSendSockData((char *)data, len);
|
2021-03-26 11:07:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
2021-04-02 17:56:24 +08:00
|
|
|
|
void WebRtcTransport::onSendSockData(const char *buf, size_t len, bool flush){
|
2021-04-02 17:08:11 +08:00
|
|
|
|
auto tuple = _ice_server->GetSelectedTuple();
|
2021-03-26 11:07:03 +08:00
|
|
|
|
assert(tuple);
|
2021-04-02 17:56:24 +08:00
|
|
|
|
onSendSockData(buf, len, (struct sockaddr_in *) tuple, flush);
|
2021-03-26 11:07:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-02 20:35:43 +08:00
|
|
|
|
const RtcSession& WebRtcTransport::getSdp(SdpType type) const{
|
|
|
|
|
switch (type) {
|
|
|
|
|
case SdpType::offer: return *_offer_sdp;
|
|
|
|
|
case SdpType::answer: return *_answer_sdp;
|
|
|
|
|
default: throw std::invalid_argument("不识别的sdp类型");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-01 10:09:50 +08:00
|
|
|
|
string getFingerprint(const string &algorithm_str, const std::shared_ptr<RTC::DtlsTransport> &transport){
|
|
|
|
|
auto algorithm = RTC::DtlsTransport::GetFingerprintAlgorithm(algorithm_str);
|
|
|
|
|
for (auto &finger_prints : transport->GetLocalFingerprints()) {
|
|
|
|
|
if (finger_prints.algorithm == algorithm) {
|
|
|
|
|
return finger_prints.value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
throw std::invalid_argument(StrPrinter << "不支持的加密算法:" << algorithm_str);
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-02 17:08:11 +08:00
|
|
|
|
void WebRtcTransport::setRemoteDtlsFingerprint(const RtcSession &remote){
|
|
|
|
|
//设置远端dtls签名
|
|
|
|
|
RTC::DtlsTransport::Fingerprint remote_fingerprint;
|
|
|
|
|
remote_fingerprint.algorithm = RTC::DtlsTransport::GetFingerprintAlgorithm(_offer_sdp->media[0].fingerprint.algorithm);
|
|
|
|
|
remote_fingerprint.value = _offer_sdp->media[0].fingerprint.hash;
|
|
|
|
|
_dtls_transport->SetRemoteFingerprint(remote_fingerprint);
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-02 17:56:24 +08:00
|
|
|
|
void WebRtcTransport::onCheckSdp(SdpType type, RtcSession &sdp) const{
|
2021-04-02 17:08:11 +08:00
|
|
|
|
for (auto &m : sdp.media) {
|
|
|
|
|
if (m.type != TrackApplication && !m.rtcp_mux) {
|
|
|
|
|
throw std::invalid_argument("只支持rtcp-mux模式");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (sdp.group.mids.empty()) {
|
|
|
|
|
throw std::invalid_argument("只支持group BUNDLE模式");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-31 17:15:26 +08:00
|
|
|
|
std::string WebRtcTransport::getAnswerSdp(const string &offer){
|
2021-04-02 17:08:11 +08:00
|
|
|
|
//// 解析offer sdp ////
|
2021-03-31 17:15:26 +08:00
|
|
|
|
_offer_sdp = std::make_shared<RtcSession>();
|
|
|
|
|
_offer_sdp->loadFrom(offer);
|
2021-04-02 17:08:11 +08:00
|
|
|
|
onCheckSdp(SdpType::offer, *_offer_sdp);
|
|
|
|
|
setRemoteDtlsFingerprint(*_offer_sdp);
|
2021-03-31 17:15:26 +08:00
|
|
|
|
|
2021-04-02 17:08:11 +08:00
|
|
|
|
//// sdp 配置 ////
|
2021-04-01 10:09:50 +08:00
|
|
|
|
SdpAttrFingerprint fingerprint;
|
|
|
|
|
fingerprint.algorithm = _offer_sdp->media[0].fingerprint.algorithm;
|
2021-04-02 17:08:11 +08:00
|
|
|
|
fingerprint.hash = getFingerprint(fingerprint.algorithm, _dtls_transport);
|
2021-03-31 17:15:26 +08:00
|
|
|
|
RtcConfigure configure;
|
2021-04-02 20:35:43 +08:00
|
|
|
|
configure.setDefaultSetting(_ice_server->GetUsernameFragment(), _ice_server->GetPassword(), RtpDirection::sendrecv, fingerprint);
|
2021-04-02 17:08:11 +08:00
|
|
|
|
onRtcConfigure(configure);
|
2021-04-02 16:23:40 +08:00
|
|
|
|
|
2021-04-02 17:08:11 +08:00
|
|
|
|
//// 生成answer sdp ////
|
2021-03-31 17:15:26 +08:00
|
|
|
|
_answer_sdp = configure.createAnswer(*_offer_sdp);
|
2021-04-02 17:08:11 +08:00
|
|
|
|
onCheckSdp(SdpType::answer, *_answer_sdp);
|
2021-04-01 11:59:35 +08:00
|
|
|
|
|
2021-04-01 10:09:50 +08:00
|
|
|
|
auto str = _answer_sdp->toString();
|
2021-04-02 17:08:11 +08:00
|
|
|
|
TraceL << "\r\n" << str;
|
2021-04-01 10:09:50 +08:00
|
|
|
|
return str;
|
2021-03-31 17:15:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-24 16:52:41 +08:00
|
|
|
|
bool is_dtls(char *buf) {
|
|
|
|
|
return ((*buf > 19) && (*buf < 64));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool is_rtp(char *buf) {
|
|
|
|
|
RtpHeader *header = (RtpHeader *) buf;
|
|
|
|
|
return ((header->pt < 64) || (header->pt >= 96));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool is_rtcp(char *buf) {
|
|
|
|
|
RtpHeader *header = (RtpHeader *) buf;
|
|
|
|
|
return ((header->pt >= 64) && (header->pt < 96));
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-02 17:08:11 +08:00
|
|
|
|
void WebRtcTransport::inputSockData(char *buf, size_t len, RTC::TransportTuple *tuple) {
|
2021-03-24 16:52:41 +08:00
|
|
|
|
if (RTC::StunPacket::IsStun((const uint8_t *) buf, len)) {
|
|
|
|
|
RTC::StunPacket *packet = RTC::StunPacket::Parse((const uint8_t *) buf, len);
|
|
|
|
|
if (packet == nullptr) {
|
|
|
|
|
WarnL << "parse stun error" << std::endl;
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-04-02 17:08:11 +08:00
|
|
|
|
_ice_server->ProcessStunPacket(packet, tuple);
|
2021-03-24 16:52:41 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
2021-03-26 11:07:03 +08:00
|
|
|
|
if (is_dtls(buf)) {
|
2021-04-02 17:08:11 +08:00
|
|
|
|
_dtls_transport->ProcessDtlsData((uint8_t *) buf, len);
|
2021-03-24 16:52:41 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
2021-04-01 14:16:42 +08:00
|
|
|
|
if (is_rtp(buf)) {
|
2021-04-02 17:56:24 +08:00
|
|
|
|
if (_srtp_session_recv->DecryptSrtp((uint8_t *) buf, &len)) {
|
|
|
|
|
onRtp(buf, len);
|
|
|
|
|
}
|
2021-04-01 14:16:42 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (is_rtcp(buf)) {
|
2021-04-02 17:56:24 +08:00
|
|
|
|
if (_srtp_session_recv->DecryptSrtcp((uint8_t *) buf, &len)) {
|
|
|
|
|
onRtcp(buf, len);
|
|
|
|
|
}
|
2021-04-01 14:16:42 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
2021-03-24 16:52:41 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-02 17:56:24 +08:00
|
|
|
|
void WebRtcTransport::sendRtpPacket(char *buf, size_t len, bool flush) {
|
2021-03-24 16:52:41 +08:00
|
|
|
|
const uint8_t *p = (uint8_t *) buf;
|
|
|
|
|
bool ret = false;
|
2021-04-02 17:08:11 +08:00
|
|
|
|
if (_srtp_session_send) {
|
|
|
|
|
ret = _srtp_session_send->EncryptRtp(&p, &len);
|
2021-03-24 16:52:41 +08:00
|
|
|
|
}
|
|
|
|
|
if (ret) {
|
2021-04-02 17:56:24 +08:00
|
|
|
|
onSendSockData((char *) p, len, flush);
|
2021-03-24 16:52:41 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-02 23:12:37 +08:00
|
|
|
|
void WebRtcTransport::sendRtcpPacket(char *buf, size_t len, bool flush){
|
|
|
|
|
const uint8_t *p = (uint8_t *) buf;
|
|
|
|
|
bool ret = false;
|
|
|
|
|
if (_srtp_session_send) {
|
|
|
|
|
ret = _srtp_session_send->EncryptRtcp(&p, &len);
|
|
|
|
|
}
|
|
|
|
|
if (ret) {
|
|
|
|
|
onSendSockData((char *) p, len, flush);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-24 16:52:41 +08:00
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////
|
2021-03-27 10:16:49 +08:00
|
|
|
|
WebRtcTransportImp::Ptr WebRtcTransportImp::create(const EventPoller::Ptr &poller){
|
|
|
|
|
WebRtcTransportImp::Ptr ret(new WebRtcTransportImp(poller), [](WebRtcTransportImp *ptr){
|
|
|
|
|
ptr->onDestory();
|
|
|
|
|
delete ptr;
|
|
|
|
|
});
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
2021-03-24 16:52:41 +08:00
|
|
|
|
|
2021-03-27 10:16:49 +08:00
|
|
|
|
WebRtcTransportImp::WebRtcTransportImp(const EventPoller::Ptr &poller) : WebRtcTransport(poller) {
|
2021-03-24 16:52:41 +08:00
|
|
|
|
_socket = Socket::createSocket(poller, false);
|
|
|
|
|
//随机端口,绑定全部网卡
|
|
|
|
|
_socket->bindUdpSock(0);
|
2021-03-26 11:07:03 +08:00
|
|
|
|
_socket->setOnRead([this](const Buffer::Ptr &buf, struct sockaddr *addr, int addr_len) mutable {
|
2021-04-02 17:08:11 +08:00
|
|
|
|
inputSockData(buf->data(), buf->size(), addr);
|
2021-03-24 16:52:41 +08:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-27 10:16:49 +08:00
|
|
|
|
void WebRtcTransportImp::onDestory() {
|
|
|
|
|
WebRtcTransport::onDestory();
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-24 16:52:41 +08:00
|
|
|
|
void WebRtcTransportImp::attach(const RtspMediaSource::Ptr &src) {
|
|
|
|
|
assert(src);
|
|
|
|
|
_src = src;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-02 17:08:11 +08:00
|
|
|
|
void WebRtcTransportImp::onStartWebRTC() {
|
2021-04-02 23:01:58 +08:00
|
|
|
|
if (canRecvRtp()) {
|
|
|
|
|
_push_src = std::make_shared<RtspMediaSourceImp>(DEFAULT_VHOST, "live", "push");
|
|
|
|
|
auto rtsp_sdp = getSdp(SdpType::answer).toRtspSdp();
|
|
|
|
|
_push_src->setSdp(rtsp_sdp);
|
|
|
|
|
|
|
|
|
|
for (auto &m : getSdp(SdpType::offer).media) {
|
|
|
|
|
for (auto &plan : m.plan) {
|
|
|
|
|
auto hit_pan = getSdp(SdpType::answer).getMedia(m.type)->getPlan(plan.pt);
|
|
|
|
|
if (!hit_pan) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
auto &ref = _rtp_receiver[plan.pt];
|
|
|
|
|
ref.plan = &plan;
|
|
|
|
|
ref.media = &m;
|
|
|
|
|
ref.is_common_rtp = getCodecId(plan.codec) != CodecInvalid;
|
|
|
|
|
ref.receiver = std::make_shared<RtpReceiverImp>([&ref, this](RtpPacket::Ptr rtp) {
|
|
|
|
|
onSortedRtp(ref, std::move(rtp));
|
|
|
|
|
}, [ref, this](const RtpPacket::Ptr &rtp) {
|
|
|
|
|
onBeforeSortedRtp(ref, rtp);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-04-02 20:35:43 +08:00
|
|
|
|
if (!canSendRtp()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-03-24 16:52:41 +08:00
|
|
|
|
_reader = _src->getRing()->attach(_socket->getPoller(), true);
|
|
|
|
|
weak_ptr<WebRtcTransportImp> weak_self = shared_from_this();
|
|
|
|
|
_reader->setReadCB([weak_self](const RtspMediaSource::RingDataType &pkt){
|
|
|
|
|
auto strongSelf = weak_self.lock();
|
|
|
|
|
if (!strongSelf) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-04-02 17:56:24 +08:00
|
|
|
|
size_t i = 0;
|
2021-03-24 16:52:41 +08:00
|
|
|
|
pkt->for_each([&](const RtpPacket::Ptr &rtp) {
|
2021-04-02 17:56:24 +08:00
|
|
|
|
strongSelf->onSendRtp(rtp, ++i == pkt->size());
|
2021-03-24 16:52:41 +08:00
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-02 17:56:24 +08:00
|
|
|
|
void WebRtcTransportImp::onSendRtp(const RtpPacket::Ptr &rtp, bool flush){
|
2021-04-02 21:08:40 +08:00
|
|
|
|
if (!_send_rtp_pt[rtp->type]) {
|
|
|
|
|
//忽略,对方不支持该编码类型
|
|
|
|
|
return;
|
2021-04-02 20:35:43 +08:00
|
|
|
|
}
|
2021-04-02 21:08:40 +08:00
|
|
|
|
auto tmp = rtp->getHeader()->pt;
|
|
|
|
|
//设置pt
|
|
|
|
|
rtp->getHeader()->pt = _send_rtp_pt[rtp->type];
|
|
|
|
|
sendRtpPacket(rtp->data() + RtpPacket::kRtpTcpHeaderSize, rtp->size() - RtpPacket::kRtpTcpHeaderSize, flush);
|
|
|
|
|
//还原pt
|
|
|
|
|
rtp->getHeader()->pt = tmp;
|
2021-04-02 20:35:43 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool WebRtcTransportImp::canSendRtp() const{
|
|
|
|
|
auto &sdp = getSdp(SdpType::answer);
|
|
|
|
|
return sdp.media[0].direction == RtpDirection::sendrecv || sdp.media[0].direction == RtpDirection::sendonly;
|
2021-03-24 16:52:41 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-02 23:01:58 +08:00
|
|
|
|
bool WebRtcTransportImp::canRecvRtp() const{
|
|
|
|
|
auto &sdp = getSdp(SdpType::answer);
|
|
|
|
|
return sdp.media[0].direction == RtpDirection::sendrecv || sdp.media[0].direction == RtpDirection::recvonly;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-02 17:56:24 +08:00
|
|
|
|
void WebRtcTransportImp::onCheckSdp(SdpType type, RtcSession &sdp) const{
|
|
|
|
|
WebRtcTransport::onCheckSdp(type, sdp);
|
2021-04-02 20:35:43 +08:00
|
|
|
|
if (type != SdpType::answer || !canSendRtp()) {
|
2021-04-02 17:56:24 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
2021-04-02 20:35:43 +08:00
|
|
|
|
|
2021-04-02 17:56:24 +08:00
|
|
|
|
for (auto &m : sdp.media) {
|
|
|
|
|
if (m.type == TrackApplication) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
m.rtp_ssrc.ssrc = _src->getSsrc(m.type);
|
2021-04-02 23:01:58 +08:00
|
|
|
|
m.rtp_ssrc.cname = RTP_CNAME;
|
|
|
|
|
//todo 先屏蔽rtx,因为chrome报错
|
|
|
|
|
if (false && m.getRelatedRtxPlan(m.plan[0].pt)) {
|
|
|
|
|
m.rtx_ssrc.ssrc = RTX_SSRC_OFFSET + m.rtp_ssrc.ssrc;
|
|
|
|
|
m.rtx_ssrc.cname = RTX_CNAME;
|
|
|
|
|
}
|
2021-04-02 21:08:40 +08:00
|
|
|
|
auto rtsp_media = _rtsp_send_sdp.getMedia(m.type);
|
|
|
|
|
if (rtsp_media && getCodecId(rtsp_media->plan[0].codec) == getCodecId(m.plan[0].codec)) {
|
|
|
|
|
_send_rtp_pt[m.type] = m.plan[0].pt;
|
|
|
|
|
}
|
2021-04-02 17:56:24 +08:00
|
|
|
|
}
|
2021-03-24 16:52:41 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-02 18:28:01 +08:00
|
|
|
|
void WebRtcTransportImp::onRtcConfigure(RtcConfigure &configure) const {
|
|
|
|
|
WebRtcTransport::onRtcConfigure(configure);
|
2021-04-02 21:08:40 +08:00
|
|
|
|
_rtsp_send_sdp.loadFrom(_src->getSdp(), false);
|
2021-04-02 18:28:01 +08:00
|
|
|
|
|
|
|
|
|
configure.audio.enable = false;
|
|
|
|
|
configure.video.enable = false;
|
|
|
|
|
|
2021-04-02 21:08:40 +08:00
|
|
|
|
for (auto &m : _rtsp_send_sdp.media) {
|
2021-04-02 18:28:01 +08:00
|
|
|
|
switch (m.type) {
|
|
|
|
|
case TrackVideo: {
|
|
|
|
|
configure.video.enable = true;
|
2021-04-02 18:36:33 +08:00
|
|
|
|
configure.video.preferred_codec.insert(configure.video.preferred_codec.begin(), getCodecId(m.plan[0].codec));
|
2021-04-02 18:28:01 +08:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case TrackAudio: {
|
|
|
|
|
configure.audio.enable = true;
|
2021-04-02 18:36:33 +08:00
|
|
|
|
configure.audio.preferred_codec.insert(configure.audio.preferred_codec.begin(),getCodecId(m.plan[0].codec));
|
2021-04-02 18:28:01 +08:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
configure.addCandidate(*getIceCandidate());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-04-02 17:56:24 +08:00
|
|
|
|
void WebRtcTransportImp::onSendSockData(const char *buf, size_t len, struct sockaddr_in *dst, bool flush) {
|
|
|
|
|
auto ptr = BufferRaw::create();
|
|
|
|
|
ptr->assign(buf, len);
|
|
|
|
|
_socket->send(ptr, (struct sockaddr *)(dst), sizeof(struct sockaddr), flush);
|
2021-03-24 16:52:41 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-02 17:08:11 +08:00
|
|
|
|
SdpAttrCandidate::Ptr WebRtcTransportImp::getIceCandidate() const{
|
|
|
|
|
auto candidate = std::make_shared<SdpAttrCandidate>();
|
|
|
|
|
candidate->foundation = "udpcandidate";
|
|
|
|
|
candidate->component = 1;
|
|
|
|
|
candidate->transport = "udp";
|
|
|
|
|
candidate->priority = 100;
|
2021-04-02 17:56:24 +08:00
|
|
|
|
candidate->address = SockUtil::get_local_ip();
|
|
|
|
|
candidate->port = _socket->get_local_port();
|
2021-04-02 17:08:11 +08:00
|
|
|
|
candidate->type = "host";
|
|
|
|
|
return candidate;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-02 23:01:58 +08:00
|
|
|
|
class RtpReceiverImp : public RtpReceiver {
|
|
|
|
|
public:
|
|
|
|
|
RtpReceiverImp( function<void(RtpPacket::Ptr rtp)> cb, function<void(const RtpPacket::Ptr &rtp)> cb_before = nullptr){
|
|
|
|
|
_on_sort = std::move(cb);
|
|
|
|
|
_on_before_sort = std::move(cb_before);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~RtpReceiverImp() override = default;
|
|
|
|
|
|
|
|
|
|
bool inputRtp(TrackType type, int samplerate, uint8_t *ptr, size_t len){
|
|
|
|
|
return handleOneRtp((int) type, type, samplerate, ptr, len);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
void onRtpSorted(RtpPacket::Ptr rtp, int track_index) override {
|
|
|
|
|
_on_sort(std::move(rtp));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void onBeforeRtpSorted(const RtpPacket::Ptr &rtp, int track_index) override {
|
|
|
|
|
if (_on_before_sort) {
|
|
|
|
|
_on_before_sort(rtp);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
function<void(RtpPacket::Ptr rtp)> _on_sort;
|
|
|
|
|
function<void(const RtpPacket::Ptr &rtp)> _on_before_sort;
|
|
|
|
|
};
|
|
|
|
|
|
2021-04-02 17:56:24 +08:00
|
|
|
|
void WebRtcTransportImp::onRtp(const char *buf, size_t len) {
|
|
|
|
|
RtpHeader *rtp = (RtpHeader *) buf;
|
2021-04-02 23:01:58 +08:00
|
|
|
|
auto it = _rtp_receiver.find(rtp->pt);
|
|
|
|
|
if (it == _rtp_receiver.end()) {
|
|
|
|
|
WarnL;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
auto &info = it->second;
|
|
|
|
|
info.receiver->inputRtp(info.media->type, info.plan->sample_rate, (uint8_t *) buf, len);
|
2021-04-02 17:56:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WebRtcTransportImp::onRtcp(const char *buf, size_t len) {
|
|
|
|
|
RtcpHeader *rtcp = (RtcpHeader *) buf;
|
2021-04-02 23:12:37 +08:00
|
|
|
|
//todo rtcp相关
|
2021-04-02 17:56:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-02 23:01:58 +08:00
|
|
|
|
void WebRtcTransportImp::onSortedRtp(const RtpPayloadInfo &info, RtpPacket::Ptr rtp) {
|
|
|
|
|
if(!info.is_common_rtp){
|
|
|
|
|
WarnL;
|
|
|
|
|
}
|
2021-04-02 23:12:37 +08:00
|
|
|
|
if (_pli_ticker.elapsedTime() > 2000) {
|
|
|
|
|
//todo 发送pli
|
|
|
|
|
_pli_ticker.resetTime();
|
|
|
|
|
}
|
|
|
|
|
_push_src->onWrite(std::move(rtp), false);
|
2021-04-02 23:01:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WebRtcTransportImp::onBeforeSortedRtp(const RtpPayloadInfo &info, const RtpPacket::Ptr &rtp) {
|
2021-04-02 23:12:37 +08:00
|
|
|
|
//todo rtcp相关
|
2021-04-02 23:01:58 +08:00
|
|
|
|
}
|
2021-03-24 16:52:41 +08:00
|
|
|
|
///////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|