完善rtsp sdp匹配

This commit is contained in:
ziyue 2021-04-02 18:28:01 +08:00
parent ee072191e0
commit 9a2f2cbf2e
4 changed files with 53 additions and 15 deletions

View File

@ -4,7 +4,9 @@
#include "Sdp.h"
#include "Common/Parser.h"
#include "Rtsp/Rtsp.h"
#include <inttypes.h>
using namespace mediakit;
using onCreateSdpItem = function<SdpItem::Ptr(const string &key, const string &value)>;
static map<string, onCreateSdpItem, StrCaseCompare> 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;
}
auto fmtp_it = fmtp_map.find(pt);
if (fmtp_it != fmtp_map.end()) {
plan.fmtp = fmtp_it->second.arr;
@ -905,8 +912,10 @@ void RtcSession::loadFrom(const string &str) {
}
group = sdp.getItemClass<SdpAttrGroup>('a', "group");
if (check) {
checkValid();
}
}
std::shared_ptr<SdpItem> wrapSdpAttr(SdpItem::Ptr item){
auto ret = std::make_shared<SdpAttr>();

View File

@ -649,7 +649,7 @@ public:
vector<RtcMedia> 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);

View File

@ -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);

View File

@ -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;