ZLMediaKit/webrtc/WebRtcTransport.cpp

256 lines
8.5 KiB
C++
Raw Normal View History

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-03-27 10:16:49 +08:00
WebRtcTransport::WebRtcTransport(const EventPoller::Ptr &poller) {
dtls_transport_ = std::make_shared<RTC::DtlsTransport>(poller, this);
2021-03-26 11:07:03 +08:00
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-03-27 09:38:13 +08:00
dtls_transport_ = nullptr;
ice_server_ = nullptr;
}
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) {
onWrite((char *)packet->GetData(), packet->GetSize(), (struct sockaddr_in *)tuple);
}
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) {
dtls_transport_->Run(RTC::DtlsTransport::Role::SERVER);
} else {
dtls_transport_->Run(RTC::DtlsTransport::Role::CLIENT);
}
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;
srtp_session_ = std::make_shared<RTC::SrtpSession>(RTC::SrtpSession::Type::OUTBOUND, srtpCryptoSuite, srtpLocalKey, srtpLocalKeyLen);
2021-04-01 10:09:50 +08:00
srtp_session_recv_ = std::make_shared<RTC::SrtpSession>(RTC::SrtpSession::Type::OUTBOUND, srtpCryptoSuite, srtpRemoteKey, srtpRemoteKeyLen);
2021-03-26 11:07:03 +08:00
onDtlsConnected();
}
void WebRtcTransport::OnDtlsTransportSendData(const RTC::DtlsTransport *dtlsTransport, const uint8_t *data, size_t len) {
onWrite((char *)data, len);
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void WebRtcTransport::onWrite(const char *buf, size_t len){
auto tuple = ice_server_->GetSelectedTuple();
assert(tuple);
onWrite(buf, len, (struct sockaddr_in *)tuple);
}
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-03-31 17:15:26 +08:00
std::string WebRtcTransport::getAnswerSdp(const string &offer){
2021-04-01 10:35:31 +08:00
// InfoL << offer;
2021-03-31 17:15:26 +08:00
_offer_sdp = std::make_shared<RtcSession>();
_offer_sdp->loadFrom(offer);
2021-04-01 10:09:50 +08:00
SdpAttrFingerprint fingerprint;
fingerprint.algorithm = _offer_sdp->media[0].fingerprint.algorithm;
fingerprint.hash = getFingerprint(fingerprint.algorithm, dtls_transport_);
2021-03-31 17:15:26 +08:00
RtcConfigure configure;
2021-04-01 10:09:50 +08:00
configure.setDefaultSetting(ice_server_->GetUsernameFragment(), ice_server_->GetPassword(), RtpDirection::recvonly, fingerprint);
2021-03-31 17:15:26 +08:00
_answer_sdp = configure.createAnswer(*_offer_sdp);
2021-04-01 10:09:50 +08:00
//设置远端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);
if (!_offer_sdp->group.mids.empty()) {
for (auto &m : _answer_sdp->media) {
_answer_sdp->group.mids.emplace_back(m.mid);
}
} else {
throw std::invalid_argument("支持group BUNDLE模式");
}
2021-04-01 11:59:35 +08:00
SdpAttrCandidate candidate;
candidate.foundation = "udpcandidate";
candidate.component = 1;
candidate.transport = "udp";
candidate.priority = getSSRC();
candidate.address = getIP();
candidate.port = getPort();
candidate.type = "host";
for (auto &m :_answer_sdp->media) {
m.candidate.emplace_back(candidate);
m.port = getPort();
m.rtcp_addr.address = getIP();
m.rtcp_addr.port = getPort();
m.addr.address = getIP();
}
_answer_sdp->connection.address = getIP();
2021-04-01 10:09:50 +08:00
auto str = _answer_sdp->toString();
2021-04-01 11:42:40 +08:00
InfoL << "\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-03-26 11:07:03 +08:00
void WebRtcTransport::OnInputDataPacket(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-03-26 11:07:03 +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-01 10:09:50 +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)) {
RtpHeader *header = (RtpHeader *) buf;
InfoL << "rtp:" << header->dumpString(len);
return;
}
if (is_rtcp(buf)) {
RtcpHeader *header = (RtcpHeader *) buf;
// InfoL << "rtcp:" << header->dumpString();
return;
}
2021-03-24 16:52:41 +08:00
}
void WebRtcTransport::WritRtpPacket(char *buf, size_t len) {
const uint8_t *p = (uint8_t *) buf;
bool ret = false;
if (srtp_session_) {
ret = srtp_session_->EncryptRtp(&p, &len);
}
if (ret) {
2021-03-26 11:07:03 +08:00
onWrite((char *) p, len);
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 {
OnInputDataPacket(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-03-26 11:07:03 +08:00
void WebRtcTransportImp::onDtlsConnected() {
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;
}
pkt->for_each([&](const RtpPacket::Ptr &rtp) {
if(rtp->type == TrackVideo) {
//目前只支持视频
strongSelf->WritRtpPacket(rtp->data() + RtpPacket::kRtpTcpHeaderSize,
rtp->size() - RtpPacket::kRtpTcpHeaderSize);
}
});
});
}
void WebRtcTransportImp::onWrite(const char *buf, size_t len, struct sockaddr_in *dst) {
auto ptr = BufferRaw::create();
ptr->assign(buf, len);
_socket->send(ptr, (struct sockaddr *)(dst), sizeof(struct sockaddr));
}
uint32_t WebRtcTransportImp::getSSRC() const {
return _src->getSsrc(TrackVideo);
}
int WebRtcTransportImp::getPayloadType() const{
auto sdp = SdpParser(_src->getSdp());
auto track = sdp.getTrack(TrackVideo);
assert(track);
return track ? track->_pt : 0;
}
uint16_t WebRtcTransportImp::getPort() const {
//todo udp端口号应该与外网映射端口相同
return _socket->get_local_port();
}
std::string WebRtcTransportImp::getIP() const {
//todo 替换为外网ip
return SockUtil::get_local_ip();
}
///////////////////////////////////////////////////////////////////