完善answer sdp代码

This commit is contained in:
ziyue 2021-04-01 10:09:50 +08:00
parent 356a8d1596
commit 868b379a12
5 changed files with 140 additions and 49 deletions

View File

@ -1105,6 +1105,15 @@ void RtcSession::checkValid() const{
} }
} }
RtcMedia *RtcSession::getMedia(TrackType type){
for(auto &m : media){
if(m.type == type){
return &m;
}
}
return nullptr;
}
void RtcConfigure::RtcTrackConfigure::setDefaultSetting(TrackType type){ void RtcConfigure::RtcTrackConfigure::setDefaultSetting(TrackType type){
enable = true; enable = true;
rtcp_mux = true; rtcp_mux = true;
@ -1119,13 +1128,13 @@ void RtcConfigure::RtcTrackConfigure::setDefaultSetting(TrackType type){
ice_trickle = true; ice_trickle = true;
ice_renomination = false; ice_renomination = false;
switch (type) { switch (type) {
case TrackVideo: { case TrackAudio: {
preferred_codec = {CodecAAC, CodecOpus, CodecG711U, CodecG711A}; preferred_codec = {CodecAAC, CodecOpus, CodecG711U, CodecG711A};
rtcp_fb = {"transport-cc"}; rtcp_fb = {"transport-cc"};
extmap = {"1 urn:ietf:params:rtp-hdrext:ssrc-audio-level"}; extmap = {"1 urn:ietf:params:rtp-hdrext:ssrc-audio-level"};
break; break;
} }
case TrackAudio: { case TrackVideo: {
preferred_codec = {CodecH264, CodecH265}; preferred_codec = {CodecH264, CodecH265};
rtcp_fb = {"nack", "ccm fir", "nack pli", "goog-remb", "transport-cc"}; rtcp_fb = {"nack", "ccm fir", "nack pli", "goog-remb", "transport-cc"};
extmap = {"2 urn:ietf:params:rtp-hdrext:toffset", extmap = {"2 urn:ietf:params:rtp-hdrext:toffset",
@ -1152,7 +1161,7 @@ void RtcConfigure::setDefaultSetting(string ice_ufrag,
application.setDefaultSetting(TrackApplication); application.setDefaultSetting(TrackApplication);
video.ice_ufrag = audio.ice_ufrag = application.ice_ufrag = ice_ufrag; video.ice_ufrag = audio.ice_ufrag = application.ice_ufrag = ice_ufrag;
video.ice_pwd = audio.ice_pwd = application.ice_pwd = ice_ufrag; video.ice_pwd = audio.ice_pwd = application.ice_pwd = ice_pwd;
video.direction = audio.direction = application.direction = direction; video.direction = audio.direction = application.direction = direction;
video.fingerprint = audio.fingerprint = application.fingerprint = fingerprint; video.fingerprint = audio.fingerprint = application.fingerprint = fingerprint;
application.enable = false; application.enable = false;
@ -1217,17 +1226,16 @@ void RtcConfigure::matchMedia(shared_ptr<RtcSession> &ret, TrackType type, const
return; return;
} }
for (auto &codec : configure.preferred_codec) { for (auto &codec : configure.preferred_codec) {
for (auto &media : medias) { for (auto &offer_media : medias) {
if (media.type != type) { if (offer_media.type != type) {
continue; continue;
} }
if (media.ice_lite && configure.ice_lite) { if (offer_media.ice_lite && configure.ice_lite) {
WarnL << "offer sdp开启了ice_lite模式但是answer sdp配置为不支持"; WarnL << "offer sdp开启了ice_lite模式但是answer sdp配置为不支持";
continue; continue;
} }
const RtcCodecPlan *offer_plan_ptr = nullptr;
const RtcCodecPlan *plan_ptr = nullptr; for (auto &plan : offer_media.plan) {
for (auto &plan : media.plan) {
if (getCodecId(plan.codec) != codec) { if (getCodecId(plan.codec) != codec) {
continue; continue;
} }
@ -1235,24 +1243,27 @@ void RtcConfigure::matchMedia(shared_ptr<RtcSession> &ret, TrackType type, const
if (!onMatchCodecPlan(plan, codec)) { if (!onMatchCodecPlan(plan, codec)) {
continue; continue;
} }
plan_ptr = &plan; //找到中意的codec
offer_plan_ptr = &plan;
break;
} }
if (!plan_ptr) { if (!offer_plan_ptr) {
//offer中该媒体的所有的codec都不支持
continue; continue;
} }
RtcMedia answer_media; RtcMedia answer_media;
answer_media.type = media.type; answer_media.type = offer_media.type;
answer_media.mid = media.type; answer_media.mid = offer_media.mid;
answer_media.proto = media.proto; answer_media.proto = offer_media.proto;
answer_media.rtcp_mux = media.rtcp_mux && configure.rtcp_mux; answer_media.rtcp_mux = offer_media.rtcp_mux && configure.rtcp_mux;
answer_media.rtcp_rsize = media.rtcp_rsize && configure.rtcp_rsize; answer_media.rtcp_rsize = offer_media.rtcp_rsize && configure.rtcp_rsize;
answer_media.ice_trickle = media.ice_trickle && configure.ice_trickle; answer_media.ice_trickle = offer_media.ice_trickle && configure.ice_trickle;
answer_media.ice_renomination = media.ice_renomination && configure.ice_renomination; answer_media.ice_renomination = offer_media.ice_renomination && configure.ice_renomination;
switch (media.role) { answer_media.ice_ufrag = configure.ice_ufrag;
case DtlsRole::actpass : { answer_media.ice_pwd = configure.ice_pwd;
answer_media.role = DtlsRole::passive; answer_media.fingerprint = configure.fingerprint;
break; switch (offer_media.role) {
} case DtlsRole::actpass :
case DtlsRole::active : { case DtlsRole::active : {
answer_media.role = DtlsRole::passive; answer_media.role = DtlsRole::passive;
break; break;
@ -1264,7 +1275,7 @@ void RtcConfigure::matchMedia(shared_ptr<RtcSession> &ret, TrackType type, const
default: continue; default: continue;
} }
switch (media.direction) { switch (offer_media.direction) {
case RtpDirection::sendonly : { case RtpDirection::sendonly : {
if (configure.direction != RtpDirection::recvonly && if (configure.direction != RtpDirection::recvonly &&
configure.direction != RtpDirection::sendrecv) { configure.direction != RtpDirection::sendrecv) {
@ -1290,9 +1301,59 @@ void RtcConfigure::matchMedia(shared_ptr<RtcSession> &ret, TrackType type, const
} }
default: continue; default: continue;
} }
answer_media.plan.emplace_back(*plan_ptr); answer_media.plan.emplace_back(*offer_plan_ptr);
if (configure.support_red || configure.support_rtx || configure.support_ulpfec) {
for (auto &plan : offer_media.plan) {
if (!strcasecmp(plan.codec.data(), "rtx")) {
if (configure.support_rtx && atoi(plan.getFmtp("apt").data()) == offer_plan_ptr->pt) {
answer_media.plan.emplace_back(plan);
}
continue;
}
if (!strcasecmp(plan.codec.data(), "red")) {
if (configure.support_red) {
answer_media.plan.emplace_back(plan);
}
continue;
}
if (!strcasecmp(plan.codec.data(), "ulpfec")) {
if (configure.support_ulpfec) {
answer_media.plan.emplace_back(plan);
}
continue;
}
}
}
//这是我们支持的扩展
unordered_set<string> extmap_set;
for (auto &ext : configure.extmap) {
SdpAttrExtmap ext_cfg;
ext_cfg.parse(ext);
extmap_set.emplace(ext_cfg.ext);
}
//对方和我方都支持的扩展,那么我们都支持
for (auto &ext : offer_media.extmap) {
if (extmap_set.find(ext.ext) != extmap_set.end()) {
answer_media.extmap.emplace_back(ext);
}
}
//我们支持的rtcp类型
unordered_set<string> rtcp_fb_set;
for (auto &fp : configure.rtcp_fb) {
rtcp_fb_set.emplace(fp);
}
vector<string> offer_rtcp_fb;
for (auto &fp : offer_plan_ptr->rtcp_fb) {
if (rtcp_fb_set.find(fp) != rtcp_fb_set.end()) {
//对方该rtcp被我们支持
offer_rtcp_fb.emplace_back(fp);
}
}
answer_media.plan[0].rtcp_fb.swap(offer_rtcp_fb);
ret->media.emplace_back(answer_media); ret->media.emplace_back(answer_media);
return;
} }
} }
} }

View File

@ -644,6 +644,7 @@ public:
void loadFrom(const string &sdp); void loadFrom(const string &sdp);
void checkValid() const; void checkValid() const;
string toString() const; string toString() const;
RtcMedia *getMedia(TrackType type);
}; };
class RtcConfigure { class RtcConfigure {

View File

@ -24,7 +24,11 @@ void WebRtcTransport::OnIceServerSelectedTuple(const RTC::IceServer *iceServer,
void WebRtcTransport::OnIceServerConnected(const RTC::IceServer *iceServer) { void WebRtcTransport::OnIceServerConnected(const RTC::IceServer *iceServer) {
InfoL; InfoL;
dtls_transport_->Run(RTC::DtlsTransport::Role::SERVER); if (_answer_sdp->media[0].role == DtlsRole::passive) {
dtls_transport_->Run(RTC::DtlsTransport::Role::SERVER);
} else {
dtls_transport_->Run(RTC::DtlsTransport::Role::CLIENT);
}
} }
void WebRtcTransport::OnIceServerCompleted(const RTC::IceServer *iceServer) { void WebRtcTransport::OnIceServerCompleted(const RTC::IceServer *iceServer) {
@ -47,6 +51,7 @@ void WebRtcTransport::OnDtlsTransportConnected(
std::string &remoteCert) { std::string &remoteCert) {
InfoL; InfoL;
srtp_session_ = std::make_shared<RTC::SrtpSession>(RTC::SrtpSession::Type::OUTBOUND, srtpCryptoSuite, srtpLocalKey, srtpLocalKeyLen); srtp_session_ = std::make_shared<RTC::SrtpSession>(RTC::SrtpSession::Type::OUTBOUND, srtpCryptoSuite, srtpLocalKey, srtpLocalKeyLen);
srtp_session_recv_ = std::make_shared<RTC::SrtpSession>(RTC::SrtpSession::Type::OUTBOUND, srtpCryptoSuite, srtpRemoteKey, srtpRemoteKeyLen);
onDtlsConnected(); onDtlsConnected();
} }
@ -62,14 +67,45 @@ void WebRtcTransport::onWrite(const char *buf, size_t len){
onWrite(buf, len, (struct sockaddr_in *)tuple); onWrite(buf, len, (struct sockaddr_in *)tuple);
} }
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);
}
std::string WebRtcTransport::getAnswerSdp(const string &offer){ std::string WebRtcTransport::getAnswerSdp(const string &offer){
InfoL << offer; InfoL << offer;
_offer_sdp = std::make_shared<RtcSession>(); _offer_sdp = std::make_shared<RtcSession>();
_offer_sdp->loadFrom(offer); _offer_sdp->loadFrom(offer);
SdpAttrFingerprint fingerprint;
fingerprint.algorithm = _offer_sdp->media[0].fingerprint.algorithm;
fingerprint.hash = getFingerprint(fingerprint.algorithm, dtls_transport_);
RtcConfigure configure; RtcConfigure configure;
configure.setDefaultSetting(ice_server_->GetUsernameFragment(), ice_server_->GetPassword(), RtpDirection::recvonly, fingerprint);
_answer_sdp = configure.createAnswer(*_offer_sdp); _answer_sdp = configure.createAnswer(*_offer_sdp);
return _answer_sdp->toString();
//设置远端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模式");
}
auto str = _answer_sdp->toString();
InfoL << str;
return str;
} }
std::string WebRtcTransport::getOfferSdp() { std::string WebRtcTransport::getOfferSdp() {
@ -78,14 +114,6 @@ std::string WebRtcTransport::getOfferSdp() {
remote_fingerprint.value = ""; remote_fingerprint.value = "";
dtls_transport_->SetRemoteFingerprint(remote_fingerprint); dtls_transport_->SetRemoteFingerprint(remote_fingerprint);
string finger_print_sha256;
auto finger_prints = dtls_transport_->GetLocalFingerprints();
for (size_t i = 0; i < finger_prints.size(); i++) {
if (finger_prints[i].algorithm == RTC::DtlsTransport::FingerprintAlgorithm::SHA256) {
finger_print_sha256 = finger_prints[i].value;
}
}
char sdp[1024 * 10] = {0}; char sdp[1024 * 10] = {0};
auto ssrc = getSSRC(); auto ssrc = getSSRC();
auto ip = getIP(); auto ip = getIP();
@ -118,7 +146,7 @@ std::string WebRtcTransport::getOfferSdp() {
"a=candidate:%s 1 udp %u %s %u typ %s\r\n", "a=candidate:%s 1 udp %u %s %u typ %s\r\n",
ip.c_str(), port, pt, ip.c_str(), ip.c_str(), port, pt, ip.c_str(),
ice_server_->GetUsernameFragment().c_str(),ice_server_->GetPassword().c_str(), ice_server_->GetUsernameFragment().c_str(),ice_server_->GetPassword().c_str(),
finger_print_sha256.c_str(), pt, ssrc, ssrc, ssrc, ssrc, "4", ssrc, ip.c_str(), port, "host"); "", pt, ssrc, ssrc, ssrc, ssrc, "4", ssrc, ip.c_str(), port, "host");
return sdp; return sdp;
} }
@ -137,6 +165,16 @@ bool is_rtcp(char *buf) {
} }
void WebRtcTransport::OnInputDataPacket(char *buf, size_t len, RTC::TransportTuple *tuple) { void WebRtcTransport::OnInputDataPacket(char *buf, size_t len, RTC::TransportTuple *tuple) {
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;
}
if (RTC::StunPacket::IsStun((const uint8_t *) buf, len)) { if (RTC::StunPacket::IsStun((const uint8_t *) buf, len)) {
RTC::StunPacket *packet = RTC::StunPacket::Parse((const uint8_t *) buf, len); RTC::StunPacket *packet = RTC::StunPacket::Parse((const uint8_t *) buf, len);
if (packet == nullptr) { if (packet == nullptr) {
@ -147,17 +185,7 @@ void WebRtcTransport::OnInputDataPacket(char *buf, size_t len, RTC::TransportTup
return; return;
} }
if (is_dtls(buf)) { if (is_dtls(buf)) {
dtls_transport_->ProcessDtlsData((uint8_t *)buf, len); dtls_transport_->ProcessDtlsData((uint8_t *) buf, len);
return;
}
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; return;
} }
} }

View File

@ -77,6 +77,7 @@ private:
std::shared_ptr<RTC::IceServer> ice_server_; std::shared_ptr<RTC::IceServer> ice_server_;
std::shared_ptr<RTC::DtlsTransport> dtls_transport_; std::shared_ptr<RTC::DtlsTransport> dtls_transport_;
std::shared_ptr<RTC::SrtpSession> srtp_session_; std::shared_ptr<RTC::SrtpSession> srtp_session_;
std::shared_ptr<RTC::SrtpSession> srtp_session_recv_;
RtcSession::Ptr _offer_sdp; RtcSession::Ptr _offer_sdp;
RtcSession::Ptr _answer_sdp; RtcSession::Ptr _answer_sdp;
}; };

View File

@ -7357,7 +7357,7 @@ var ZLMRTCClient = (function (exports) {
'Content-Type': 'text/plain;charset=utf-8' 'Content-Type': 'text/plain;charset=utf-8'
} }
}).then(response => { }).then(response => {
let ret = JSON.parse(response.data); let ret = response.data;
if (ret.code != 0) { if (ret.code != 0) {
// mean failed for offer/anwser exchange // mean failed for offer/anwser exchange