开始对接rtsp源

This commit is contained in:
ziyue 2021-04-02 17:56:24 +08:00
parent 7110dc75ae
commit ee072191e0
2 changed files with 61 additions and 42 deletions

View File

@ -61,10 +61,10 @@ void WebRtcTransport::OnDtlsTransportSendData(const RTC::DtlsTransport *dtlsTran
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void WebRtcTransport::onSendSockData(const char *buf, size_t len){ void WebRtcTransport::onSendSockData(const char *buf, size_t len, bool flush){
auto tuple = _ice_server->GetSelectedTuple(); auto tuple = _ice_server->GetSelectedTuple();
assert(tuple); assert(tuple);
onSendSockData(buf, len, (struct sockaddr_in *) tuple); onSendSockData(buf, len, (struct sockaddr_in *) tuple, flush);
} }
string getFingerprint(const string &algorithm_str, const std::shared_ptr<RTC::DtlsTransport> &transport){ string getFingerprint(const string &algorithm_str, const std::shared_ptr<RTC::DtlsTransport> &transport){
@ -85,7 +85,7 @@ void WebRtcTransport::setRemoteDtlsFingerprint(const RtcSession &remote){
_dtls_transport->SetRemoteFingerprint(remote_fingerprint); _dtls_transport->SetRemoteFingerprint(remote_fingerprint);
} }
void WebRtcTransport::onCheckSdp(SdpType type, const RtcSession &sdp) const{ void WebRtcTransport::onCheckSdp(SdpType type, RtcSession &sdp) const{
for (auto &m : sdp.media) { for (auto &m : sdp.media) {
if (m.type != TrackApplication && !m.rtcp_mux) { if (m.type != TrackApplication && !m.rtcp_mux) {
throw std::invalid_argument("只支持rtcp-mux模式"); throw std::invalid_argument("只支持rtcp-mux模式");
@ -150,23 +150,27 @@ void WebRtcTransport::inputSockData(char *buf, size_t len, RTC::TransportTuple *
return; return;
} }
if (is_rtp(buf)) { if (is_rtp(buf)) {
RtpHeader *header = (RtpHeader *) buf; if (_srtp_session_recv->DecryptSrtp((uint8_t *) buf, &len)) {
onRtp(buf, len);
}
return; return;
} }
if (is_rtcp(buf)) { if (is_rtcp(buf)) {
RtcpHeader *header = (RtcpHeader *) buf; if (_srtp_session_recv->DecryptSrtcp((uint8_t *) buf, &len)) {
onRtcp(buf, len);
}
return; return;
} }
} }
void WebRtcTransport::sendRtpPacket(char *buf, size_t len) { void WebRtcTransport::sendRtpPacket(char *buf, size_t len, bool flush) {
const uint8_t *p = (uint8_t *) buf; const uint8_t *p = (uint8_t *) buf;
bool ret = false; bool ret = false;
if (_srtp_session_send) { if (_srtp_session_send) {
ret = _srtp_session_send->EncryptRtp(&p, &len); ret = _srtp_session_send->EncryptRtp(&p, &len);
} }
if (ret) { if (ret) {
onSendSockData((char *) p, len); onSendSockData((char *) p, len, flush);
} }
} }
@ -205,34 +209,38 @@ void WebRtcTransportImp::onStartWebRTC() {
if (!strongSelf) { if (!strongSelf) {
return; return;
} }
size_t i = 0;
pkt->for_each([&](const RtpPacket::Ptr &rtp) { pkt->for_each([&](const RtpPacket::Ptr &rtp) {
if(rtp->type == TrackVideo) { strongSelf->onSendRtp(rtp, ++i == pkt->size());
//目前只支持视频
strongSelf->sendRtpPacket(rtp->data() + RtpPacket::kRtpTcpHeaderSize,
rtp->size() - RtpPacket::kRtpTcpHeaderSize);
}
}); });
}); });
} }
void WebRtcTransportImp::onSendSockData(const char *buf, size_t len, struct sockaddr_in *dst) { void WebRtcTransportImp::onSendRtp(const RtpPacket::Ptr &rtp, bool flush){
InfoL << flush;
}
void WebRtcTransportImp::onCheckSdp(SdpType type, RtcSession &sdp) const{
WebRtcTransport::onCheckSdp(type, sdp);
if (type != SdpType::answer) {
return;
}
for (auto &m : sdp.media) {
if (m.type == TrackApplication) {
continue;
}
m.rtp_ssrc.ssrc = _src->getSsrc(m.type);
m.rtx_ssrc.ssrc = 2 + m.rtp_ssrc.ssrc;
m.rtp_ssrc.cname = "zlmediakit rtc";
m.rtx_ssrc.cname = "zlmediakit rtc";
}
}
void WebRtcTransportImp::onSendSockData(const char *buf, size_t len, struct sockaddr_in *dst, bool flush) {
auto ptr = BufferRaw::create(); auto ptr = BufferRaw::create();
ptr->assign(buf, len); ptr->assign(buf, len);
_socket->send(ptr, (struct sockaddr *)(dst), sizeof(struct sockaddr)); _socket->send(ptr, (struct sockaddr *)(dst), sizeof(struct sockaddr), flush);
}
uint32_t WebRtcTransportImp::getSSRC() const {
return _src->getSsrc(TrackVideo);
}
uint16_t WebRtcTransportImp::getPort() const {
//todo udp端口号应该与外网映射端口相同
return _socket->get_local_port();
}
std::string WebRtcTransportImp::getIP() const {
//todo 替换为外网ip
return SockUtil::get_local_ip();
} }
SdpAttrCandidate::Ptr WebRtcTransportImp::getIceCandidate() const{ SdpAttrCandidate::Ptr WebRtcTransportImp::getIceCandidate() const{
@ -241,12 +249,22 @@ SdpAttrCandidate::Ptr WebRtcTransportImp::getIceCandidate() const{
candidate->component = 1; candidate->component = 1;
candidate->transport = "udp"; candidate->transport = "udp";
candidate->priority = 100; candidate->priority = 100;
candidate->address = getIP(); candidate->address = SockUtil::get_local_ip();
candidate->port = getPort(); candidate->port = _socket->get_local_port();
candidate->type = "host"; candidate->type = "host";
return candidate; return candidate;
} }
void WebRtcTransportImp::onRtp(const char *buf, size_t len) {
RtpHeader *rtp = (RtpHeader *) buf;
// TraceL << (int)rtp->ssrc;
}
void WebRtcTransportImp::onRtcp(const char *buf, size_t len) {
RtcpHeader *rtcp = (RtcpHeader *) buf;
// TraceL << rtcpTypeToStr((RtcpType)rtcp->pt);
}
/////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////

View File

@ -44,7 +44,7 @@ public:
* @param buf rtcp内容 * @param buf rtcp内容
* @param len rtcp长度 * @param len rtcp长度
*/ */
void sendRtpPacket(char *buf, size_t len); void sendRtpPacket(char *buf, size_t len, bool flush);
protected: protected:
//// dtls相关的回调 //// //// dtls相关的回调 ////
@ -71,19 +71,18 @@ protected:
void OnIceServerDisconnected(const RTC::IceServer *iceServer) override; void OnIceServerDisconnected(const RTC::IceServer *iceServer) override;
protected: protected:
virtual uint32_t getSSRC() const = 0;
virtual uint16_t getPort() const = 0;
virtual std::string getIP() const = 0;
virtual void onStartWebRTC() = 0; virtual void onStartWebRTC() = 0;
virtual void onRtcConfigure(RtcConfigure &configure) const {} virtual void onRtcConfigure(RtcConfigure &configure) const {}
virtual void onCheckSdp(SdpType type, const RtcSession &sdp) const; virtual void onCheckSdp(SdpType type, RtcSession &sdp) const;
virtual SdpAttrCandidate::Ptr getIceCandidate() const = 0; virtual SdpAttrCandidate::Ptr getIceCandidate() const = 0;
virtual void onSendSockData(const char *buf, size_t len, struct sockaddr_in *dst) = 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;
virtual void onRtcp(const char *buf, size_t len) = 0;
private: private:
void onSendSockData(const char *buf, size_t len); void onSendSockData(const char *buf, size_t len, bool flush = true);
void setRemoteDtlsFingerprint(const RtcSession &remote); void setRemoteDtlsFingerprint(const RtcSession &remote);
private: private:
@ -115,15 +114,17 @@ public:
protected: protected:
void onStartWebRTC() override; void onStartWebRTC() override;
void onSendSockData(const char *buf, size_t len, struct sockaddr_in *dst) override; void onSendSockData(const char *buf, size_t len, struct sockaddr_in *dst, bool flush = true) override;
uint32_t getSSRC() const override; void onCheckSdp(SdpType type, RtcSession &sdp) const override;
uint16_t getPort() const override;
std::string getIP() const override;
SdpAttrCandidate::Ptr getIceCandidate() 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;
private: private:
WebRtcTransportImp(const EventPoller::Ptr &poller); WebRtcTransportImp(const EventPoller::Ptr &poller);
void onDestory() override; void onDestory() override;
void onSendRtp(const RtpPacket::Ptr &rtp, bool flush);
private: private:
Socket::Ptr _socket; Socket::Ptr _socket;