From 0f4289a584208bb371532922c808fbf170a2c4b8 Mon Sep 17 00:00:00 2001 From: Johnny Date: Tue, 12 Oct 2021 15:53:30 +0800 Subject: [PATCH] =?UTF-8?q?Refine(webrtc/sdp):=20=E5=87=BD=E6=95=B0=20WebR?= =?UTF-8?q?tcTransport::onCheckSdp()=20=E6=8B=86=E5=88=86=E4=B8=BAWebRtcTr?= =?UTF-8?q?ansport::onCheckAnswer()=20=E5=92=8C=20RtcSession::checkSdp()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- webrtc/Sdp.cpp | 10 ++++++++++ webrtc/Sdp.h | 1 + webrtc/WebRtcTransport.cpp | 27 +++++---------------------- webrtc/WebRtcTransport.h | 4 ++-- 4 files changed, 18 insertions(+), 24 deletions(-) diff --git a/webrtc/Sdp.cpp b/webrtc/Sdp.cpp index d73f9bc1..51e1c173 100644 --- a/webrtc/Sdp.cpp +++ b/webrtc/Sdp.cpp @@ -1381,6 +1381,16 @@ bool RtcSession::supportSimulcast() const { } return false; } +void RtcSession::checkSdp() const { + for (auto &m : media) { + if (m.type != TrackApplication && !m.rtcp_mux) { + throw std::invalid_argument("只支持rtcp-mux模式"); + } + } + if (group.mids.empty()) { + throw std::invalid_argument("只支持group BUNDLE模式"); + } +} string const SdpConst::kTWCCRtcpFb = "transport-cc"; string const SdpConst::kRembRtcpFb = "goog-remb"; diff --git a/webrtc/Sdp.h b/webrtc/Sdp.h index 999df44d..d8c6c0ae 100644 --- a/webrtc/Sdp.h +++ b/webrtc/Sdp.h @@ -667,6 +667,7 @@ public: void loadFrom(const string &sdp, bool check = true); void checkValid() const; //offer sdp,如果指定了发送rtp,那么应该指定ssrc + void checkSdp() const; void checkValidSSRC() const; string toString() const; string toRtspSdp() const; diff --git a/webrtc/WebRtcTransport.cpp b/webrtc/WebRtcTransport.cpp index 94dc5a81..50acad51 100644 --- a/webrtc/WebRtcTransport.cpp +++ b/webrtc/WebRtcTransport.cpp @@ -174,19 +174,6 @@ void WebRtcTransport::setRemoteDtlsFingerprint(const RtcSession &remote){ _dtls_transport->SetRemoteFingerprint(remote_fingerprint); } -void WebRtcTransport::onCheckSdp(SdpType type, RtcSession &sdp){ - 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模式"); - } - if (type == SdpType::offer) { - sdp.checkValidSSRC(); - } -} void WebRtcTransport::onRtcConfigure(RtcConfigure &configure) const { //开启remb后关闭twcc,因为开启twcc后remb无效 @@ -199,7 +186,8 @@ std::string WebRtcTransport::getAnswerSdp(const string &offer){ //// 解析offer sdp //// _offer_sdp = std::make_shared(); _offer_sdp->loadFrom(offer); - onCheckSdp(SdpType::offer, *_offer_sdp); + _offer_sdp->checkSdp(); + _offer_sdp->checkValidSSRC(); setRemoteDtlsFingerprint(*_offer_sdp); //// sdp 配置 //// @@ -213,7 +201,8 @@ std::string WebRtcTransport::getAnswerSdp(const string &offer){ //// 生成answer sdp //// _answer_sdp = configure.createAnswer(*_offer_sdp); - onCheckSdp(SdpType::answer, *_answer_sdp); + _answer_sdp->checkSdp(); + onCheckAnswer(*_answer_sdp); return _answer_sdp->toString(); } catch (exception &ex) { onShutdown(SockException(Err_shutdown, ex.what())); @@ -520,13 +509,7 @@ void WebRtcTransportImp::onStartWebRTC() { _play_src = nullptr; } -void WebRtcTransportImp::onCheckSdp(SdpType type, RtcSession &sdp){ - WebRtcTransport::onCheckSdp(type, sdp); - if (type != SdpType::answer) { - //我们只修改answer sdp - return; - } - +void WebRtcTransportImp::onCheckAnswer(RtcSession &sdp) { //修改answer sdp的ip、端口信息 GET_CONFIG(string, extern_ip, RTC::kExternIP); for (auto &m : sdp.media) { diff --git a/webrtc/WebRtcTransport.h b/webrtc/WebRtcTransport.h index b6aa5c4a..081ff196 100644 --- a/webrtc/WebRtcTransport.h +++ b/webrtc/WebRtcTransport.h @@ -106,7 +106,7 @@ protected: protected: virtual void onStartWebRTC() = 0; virtual void onRtcConfigure(RtcConfigure &configure) const; - virtual void onCheckSdp(SdpType type, RtcSession &sdp); + virtual void onCheckAnswer(RtcSession &sdp) = 0; 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, uint64_t stamp_ms) = 0; @@ -184,7 +184,7 @@ public: protected: void onStartWebRTC() override; void onSendSockData(const char *buf, size_t len, struct sockaddr_in *dst, bool flush = true) override; - void onCheckSdp(SdpType type, RtcSession &sdp) override; + void onCheckAnswer(RtcSession &sdp) override; void onRtcConfigure(RtcConfigure &configure) const override; void onRtp(const char *buf, size_t len, uint64_t stamp_ms) override;