diff --git a/webrtc/Sdp.cpp b/webrtc/Sdp.cpp index e96c660c..bc573e59 100644 --- a/webrtc/Sdp.cpp +++ b/webrtc/Sdp.cpp @@ -4,7 +4,9 @@ #include "Sdp.h" #include "Common/Parser.h" +#include "Rtsp/Rtsp.h" #include +using namespace mediakit; using onCreateSdpItem = function; static map sdpItemCreator; @@ -596,11 +598,11 @@ void SdpAttrFmtp::parse(const string &str) { auto vec = split(str.substr(pos + 1), ";"); for (auto &item : vec) { trim(item); - auto pr_vec = split(item, "="); - if (pr_vec.size() != 2) { + auto pos = item.find('='); + if(pos == string::npos){ SDP_THROW(); } - arr.emplace_back(std::make_pair(pr_vec[0], pr_vec[1])); + arr.emplace_back(std::make_pair(item.substr(0, pos), item.substr(pos + 1))); } if (arr.empty()) { SDP_THROW(); @@ -746,7 +748,7 @@ string SdpAttrCandidate::toString() const { return SdpItem::toString(); } -void RtcSession::loadFrom(const string &str) { +void RtcSession::loadFrom(const string &str, bool check) { RtcSessionSdp sdp; sdp.parse(str); @@ -887,12 +889,17 @@ void RtcSession::loadFrom(const string &str) { auto &plan = rtc_media.plan.back(); auto rtpmap_it = rtpmap_map.find(pt); if (rtpmap_it == rtpmap_map.end()) { - throw std::invalid_argument(StrPrinter << "该pt不存在相对于的a=rtpmap:" << pt); + plan.pt = pt; + plan.codec = RtpPayload::getCodecId(pt); + plan.sample_rate = RtpPayload::getClockRate(pt); + plan.channel = RtpPayload::getAudioChannel(pt); + } else { + plan.pt = rtpmap_it->second.pt; + plan.codec = rtpmap_it->second.codec; + plan.sample_rate = rtpmap_it->second.sample_rate; + plan.channel = rtpmap_it->second.channel; } - plan.pt = rtpmap_it->second.pt; - plan.codec = rtpmap_it->second.codec; - plan.sample_rate = rtpmap_it->second.sample_rate; - plan.channel = rtpmap_it->second.channel; + auto fmtp_it = fmtp_map.find(pt); if (fmtp_it != fmtp_map.end()) { plan.fmtp = fmtp_it->second.arr; @@ -905,7 +912,9 @@ void RtcSession::loadFrom(const string &str) { } group = sdp.getItemClass('a', "group"); - checkValid(); + if (check) { + checkValid(); + } } std::shared_ptr wrapSdpAttr(SdpItem::Ptr item){ diff --git a/webrtc/Sdp.h b/webrtc/Sdp.h index fa3b193c..c6720618 100644 --- a/webrtc/Sdp.h +++ b/webrtc/Sdp.h @@ -649,7 +649,7 @@ public: vector media; SdpAttrGroup group; - void loadFrom(const string &sdp); + void loadFrom(const string &sdp, bool check = true); void checkValid() const; string toString() const; RtcMedia *getMedia(TrackType type); diff --git a/webrtc/WebRtcTransport.cpp b/webrtc/WebRtcTransport.cpp index 8cecdfaf..43fbebd9 100644 --- a/webrtc/WebRtcTransport.cpp +++ b/webrtc/WebRtcTransport.cpp @@ -109,7 +109,6 @@ std::string WebRtcTransport::getAnswerSdp(const string &offer){ fingerprint.hash = getFingerprint(fingerprint.algorithm, _dtls_transport); RtcConfigure configure; configure.setDefaultSetting(_ice_server->GetUsernameFragment(), _ice_server->GetPassword(), RtpDirection::recvonly, fingerprint); - configure.addCandidate(*getIceCandidate()); onRtcConfigure(configure); //// 生成answer sdp //// @@ -217,6 +216,7 @@ void WebRtcTransportImp::onStartWebRTC() { } void WebRtcTransportImp::onSendRtp(const RtpPacket::Ptr &rtp, bool flush){ + //需要修改pt InfoL << flush; } @@ -237,6 +237,36 @@ void WebRtcTransportImp::onCheckSdp(SdpType type, RtcSession &sdp) const{ } } +void WebRtcTransportImp::onRtcConfigure(RtcConfigure &configure) const { + WebRtcTransport::onRtcConfigure(configure); + + RtcSession sdp; + sdp.loadFrom(_src->getSdp(), false); + + configure.audio.enable = false; + configure.video.enable = false; + + for (auto &m : sdp.media) { + switch (m.type) { + case TrackVideo: { + configure.video.enable = true; + configure.video.preferred_codec = {getCodecId(m.plan[0].codec)}; + break; + } + case TrackAudio: { + configure.audio.enable = true; + configure.audio.preferred_codec = {getCodecId(m.plan[0].codec)}; + break; + } + default: + break; + } + } + + configure.addCandidate(*getIceCandidate()); +} + + void WebRtcTransportImp::onSendSockData(const char *buf, size_t len, struct sockaddr_in *dst, bool flush) { auto ptr = BufferRaw::create(); ptr->assign(buf, len); diff --git a/webrtc/WebRtcTransport.h b/webrtc/WebRtcTransport.h index a337bf89..271d1bbf 100644 --- a/webrtc/WebRtcTransport.h +++ b/webrtc/WebRtcTransport.h @@ -74,8 +74,6 @@ protected: virtual void onStartWebRTC() = 0; virtual void onRtcConfigure(RtcConfigure &configure) const {} virtual void onCheckSdp(SdpType type, RtcSession &sdp) const; - - virtual SdpAttrCandidate::Ptr getIceCandidate() const = 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) = 0; @@ -116,8 +114,8 @@ 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) const override; + void onRtcConfigure(RtcConfigure &configure) const override; - SdpAttrCandidate::Ptr getIceCandidate() const override; void onRtp(const char *buf, size_t len) override; void onRtcp(const char *buf, size_t len) override; @@ -125,6 +123,7 @@ private: WebRtcTransportImp(const EventPoller::Ptr &poller); void onDestory() override; void onSendRtp(const RtpPacket::Ptr &rtp, bool flush); + SdpAttrCandidate::Ptr getIceCandidate() const; private: Socket::Ptr _socket;