format code use clang-format

This commit is contained in:
xiongguangjie 2022-07-14 22:49:31 +08:00
parent 602b475a3c
commit cffc0743d7
7 changed files with 1071 additions and 1042 deletions

View File

@ -8,11 +8,11 @@
* may be found in the AUTHORS file in the root of the source tree. * may be found in the AUTHORS file in the root of the source tree.
*/ */
#include <stddef.h>
#include <assert.h>
#include "Rtcp.h" #include "Rtcp.h"
#include "Util/logger.h"
#include "RtcpFCI.h" #include "RtcpFCI.h"
#include "Util/logger.h"
#include <assert.h>
#include <stddef.h>
using namespace std; using namespace std;
using namespace toolkit; using namespace toolkit;
@ -21,37 +21,49 @@ namespace mediakit {
const char *rtcpTypeToStr(RtcpType type) { const char *rtcpTypeToStr(RtcpType type) {
switch (type) { switch (type) {
#define SWITCH_CASE(key, value) case RtcpType::key : return #value "(" #key ")"; #define SWITCH_CASE(key, value) \
case RtcpType::key: \
return #value "(" #key ")";
RTCP_PT_MAP(SWITCH_CASE) RTCP_PT_MAP(SWITCH_CASE)
#undef SWITCH_CASE #undef SWITCH_CASE
default: return "unknown rtcp pt"; default:
return "unknown rtcp pt";
} }
} }
const char *sdesTypeToStr(SdesType type) { const char *sdesTypeToStr(SdesType type) {
switch (type) { switch (type) {
#define SWITCH_CASE(key, value) case SdesType::key : return #value "(" #key ")"; #define SWITCH_CASE(key, value) \
case SdesType::key: \
return #value "(" #key ")";
SDES_TYPE_MAP(SWITCH_CASE) SDES_TYPE_MAP(SWITCH_CASE)
#undef SWITCH_CASE #undef SWITCH_CASE
default: return "unknown source description type"; default:
return "unknown source description type";
} }
} }
const char *psfbTypeToStr(PSFBType type) { const char *psfbTypeToStr(PSFBType type) {
switch (type) { switch (type) {
#define SWITCH_CASE(key, value) case PSFBType::key : return #value "(" #key ")"; #define SWITCH_CASE(key, value) \
case PSFBType::key: \
return #value "(" #key ")";
PSFB_TYPE_MAP(SWITCH_CASE) PSFB_TYPE_MAP(SWITCH_CASE)
#undef SWITCH_CASE #undef SWITCH_CASE
default: return "unknown payload-specific fb message fmt type"; default:
return "unknown payload-specific fb message fmt type";
} }
} }
const char *rtpfbTypeToStr(RTPFBType type) { const char *rtpfbTypeToStr(RTPFBType type) {
switch (type) { switch (type) {
#define SWITCH_CASE(key, value) case RTPFBType::key : return #value "(" #key ")"; #define SWITCH_CASE(key, value) \
case RTPFBType::key: \
return #value "(" #key ")";
RTPFB_TYPE_MAP(SWITCH_CASE) RTPFB_TYPE_MAP(SWITCH_CASE)
#undef SWITCH_CASE #undef SWITCH_CASE
default: return "unknown transport layer feedback messages fmt type"; default:
return "unknown transport layer feedback messages fmt type";
} }
} }
@ -140,7 +152,8 @@ string RtcpHeader::dumpString() const {
return rtcp->dumpString(); return rtcp->dumpString();
} }
default: return StrPrinter << dumpHeader() << hexdump((char *)this + sizeof(*this), getSize() - sizeof(*this)); default:
return StrPrinter << dumpHeader() << hexdump((char *)this + sizeof(*this), getSize() - sizeof(*this));
} }
} }
@ -207,7 +220,8 @@ void RtcpHeader::net2Host(size_t len) {
} }
break; break;
} }
default: throw std::runtime_error(StrPrinter << "未处理的rtcp包:" << rtcpTypeToStr((RtcpType) this->pt)); default:
throw std::runtime_error(StrPrinter << "未处理的rtcp包:" << rtcpTypeToStr((RtcpType)this->pt));
} }
} }
@ -237,19 +251,13 @@ vector<RtcpHeader *> RtcpHeader::loadFromBytes(char *data, size_t len) {
class BufferRtcp : public Buffer { class BufferRtcp : public Buffer {
public: public:
BufferRtcp(std::shared_ptr<RtcpHeader> rtcp) { BufferRtcp(std::shared_ptr<RtcpHeader> rtcp) { _rtcp = std::move(rtcp); }
_rtcp = std::move(rtcp);
}
~BufferRtcp() override {} ~BufferRtcp() override {}
char *data() const override { char *data() const override { return (char *)_rtcp.get(); }
return (char *) _rtcp.get();
}
size_t size() const override { size_t size() const override { return _rtcp->getSize(); }
return _rtcp->getSize();
}
private: private:
std::shared_ptr<RtcpHeader> _rtcp; std::shared_ptr<RtcpHeader> _rtcp;
@ -267,9 +275,7 @@ std::shared_ptr<RtcpSR> RtcpSR::create(size_t item_count) {
auto ptr = (RtcpSR *)new char[bytes]; auto ptr = (RtcpSR *)new char[bytes];
setupHeader(ptr, RtcpType::RTCP_SR, item_count, bytes); setupHeader(ptr, RtcpType::RTCP_SR, item_count, bytes);
setupPadding(ptr, bytes - real_size); setupPadding(ptr, bytes - real_size);
return std::shared_ptr<RtcpSR>(ptr, [](RtcpSR *ptr) { return std::shared_ptr<RtcpSR>(ptr, [](RtcpSR *ptr) { delete[] (char *)ptr; });
delete[] (char *) ptr;
});
} }
string RtcpSR::getNtpStamp() const { string RtcpSR::getNtpStamp() const {
@ -324,13 +330,15 @@ string RtcpSR::dumpString() const {
#define CHECK_MIN_SIZE(size, kMinSize) \ #define CHECK_MIN_SIZE(size, kMinSize) \
if (size < kMinSize) { \ if (size < kMinSize) { \
throw std::out_of_range(StrPrinter << rtcpTypeToStr((RtcpType)pt) << " 长度不足:" << size << " < " << kMinSize); \ throw std::out_of_range( \
StrPrinter << rtcpTypeToStr((RtcpType)pt) << " 长度不足:" << size << " < " << kMinSize); \
} }
#define CHECK_REPORT_COUNT(item_count) \ #define CHECK_REPORT_COUNT(item_count) \
/*修正个数防止getItemList时内存越界*/ \ /*修正个数防止getItemList时内存越界*/ \
if (report_count != item_count) { \ if (report_count != item_count) { \
WarnL << rtcpTypeToStr((RtcpType)pt) << " report_count 字段不正确,已修正为:" << (int)report_count << " -> " << item_count; \ WarnL << rtcpTypeToStr((RtcpType)pt) << " report_count 字段不正确,已修正为:" << (int)report_count << " -> " \
<< item_count; \
report_count = item_count; \ report_count = item_count; \
} }
@ -398,9 +406,7 @@ std::shared_ptr<RtcpRR> RtcpRR::create(size_t item_count) {
auto ptr = (RtcpRR *)new char[bytes]; auto ptr = (RtcpRR *)new char[bytes];
setupHeader(ptr, RtcpType::RTCP_RR, item_count, bytes); setupHeader(ptr, RtcpType::RTCP_RR, item_count, bytes);
setupPadding(ptr, bytes - real_size); setupPadding(ptr, bytes - real_size);
return std::shared_ptr<RtcpRR>(ptr, [](RtcpRR *ptr) { return std::shared_ptr<RtcpRR>(ptr, [](RtcpRR *ptr) { delete[] (char *)ptr; });
delete[] (char *) ptr;
});
} }
string RtcpRR::dumpString() const { string RtcpRR::dumpString() const {
@ -486,9 +492,7 @@ std::shared_ptr<RtcpSdes> RtcpSdes::create(const std::vector<string> &item_text)
setupHeader(ptr, RtcpType::RTCP_SDES, item_text.size(), bytes); setupHeader(ptr, RtcpType::RTCP_SDES, item_text.size(), bytes);
setupPadding(ptr, bytes - real_size); setupPadding(ptr, bytes - real_size);
return std::shared_ptr<RtcpSdes>(ptr, [](RtcpSdes *ptr) { return std::shared_ptr<RtcpSdes>(ptr, [](RtcpSdes *ptr) { delete[] (char *)ptr; });
delete[] (char *) ptr;
});
} }
string RtcpSdes::dumpString() const { string RtcpSdes::dumpString() const {
@ -540,9 +544,7 @@ std::shared_ptr<RtcpFB> RtcpFB::create_l(RtcpType type, int fmt, const void *fci
} }
setupHeader(ptr, type, fmt, bytes); setupHeader(ptr, type, fmt, bytes);
setupPadding(ptr, bytes - real_size); setupPadding(ptr, bytes - real_size);
return std::shared_ptr<RtcpFB>((RtcpFB *) ptr, [](RtcpFB *ptr) { return std::shared_ptr<RtcpFB>((RtcpFB *)ptr, [](RtcpFB *ptr) { delete[] (char *)ptr; });
delete[] (char *) ptr;
});
} }
std::shared_ptr<RtcpFB> RtcpFB::create(PSFBType fmt, const void *fci, size_t fci_len) { std::shared_ptr<RtcpFB> RtcpFB::create(PSFBType fmt, const void *fci, size_t fci_len) {
@ -619,7 +621,9 @@ string RtcpFB::dumpString() const {
} }
break; break;
} }
default: /*不可达*/ assert(0); break; default: /*不可达*/
assert(0);
break;
} }
return std::move(printer); return std::move(printer);
} }
@ -652,9 +656,7 @@ std::shared_ptr<RtcpBye> RtcpBye::create(const std::vector<uint32_t> &ssrcs, con
memcpy(reason_len_ptr + 1, reason.data(), *reason_len_ptr); memcpy(reason_len_ptr + 1, reason.data(), *reason_len_ptr);
} }
return std::shared_ptr<RtcpBye>(ptr, [](RtcpBye *ptr) { return std::shared_ptr<RtcpBye>(ptr, [](RtcpBye *ptr) { delete[] (char *)ptr; });
delete[] (char *) ptr;
});
} }
vector<uint32_t *> RtcpBye::getSSRC() { vector<uint32_t *> RtcpBye::getSSRC() {
@ -720,7 +722,9 @@ void RtcpXRRRTR::net2Host(size_t size) {
static const size_t kMinSize = sizeof(RtcpHeader); static const size_t kMinSize = sizeof(RtcpHeader);
CHECK_MIN_SIZE(size, kMinSize); CHECK_MIN_SIZE(size, kMinSize);
if (size != sizeof(RtcpXRRRTR)) { if (size != sizeof(RtcpXRRRTR)) {
throw std::invalid_argument(StrPrinter << "rtcp xr Receiver Reference Time Report Block must is " << sizeof(RtcpXRRRTR)<<" actual size "<<size); throw std::invalid_argument(
StrPrinter << "rtcp xr Receiver Reference Time Report Block must is " << sizeof(RtcpXRRRTR)
<< " actual size " << size);
} }
ssrc = ntohl(ssrc); ssrc = ntohl(ssrc);
block_length = ntohs(block_length); block_length = ntohs(block_length);
@ -728,7 +732,6 @@ void RtcpXRRRTR::net2Host(size_t size) {
ntplsw = ntohl(ntplsw); ntplsw = ntohl(ntplsw);
} }
string RtcpXRDLRRReportItem::dumpString() const { string RtcpXRDLRRReportItem::dumpString() const {
_StrPrinter printer; _StrPrinter printer;
@ -754,7 +757,6 @@ std::vector<RtcpXRDLRRReportItem*> RtcpXRDLRR::getItemList(){
++ptr; ++ptr;
} }
return ret; return ret;
} }
string RtcpXRDLRR::dumpString() const { string RtcpXRDLRR::dumpString() const {
_StrPrinter printer; _StrPrinter printer;
@ -792,9 +794,7 @@ std::shared_ptr<RtcpXRDLRR> RtcpXRDLRR::create(size_t item_count){
auto ptr = (RtcpXRDLRR *)new char[bytes]; auto ptr = (RtcpXRDLRR *)new char[bytes];
setupHeader(ptr, RtcpType::RTCP_XR, 0, bytes); setupHeader(ptr, RtcpType::RTCP_XR, 0, bytes);
setupPadding(ptr, bytes - real_size); setupPadding(ptr, bytes - real_size);
return std::shared_ptr<RtcpXRDLRR>(ptr, [](RtcpXRDLRR *ptr) { return std::shared_ptr<RtcpXRDLRR>(ptr, [](RtcpXRDLRR *ptr) { delete[] (char *)ptr; });
delete[] (char *) ptr;
});
} }
#if 0 #if 0

View File

@ -11,11 +11,11 @@
#ifndef ZLMEDIAKIT_RTCP_H #ifndef ZLMEDIAKIT_RTCP_H
#define ZLMEDIAKIT_RTCP_H #define ZLMEDIAKIT_RTCP_H
#include "Common/macros.h"
#include "Network/Buffer.h"
#include "Util/util.h"
#include <stdint.h> #include <stdint.h>
#include <vector> #include <vector>
#include "Util/util.h"
#include "Network/Buffer.h"
#include "Common/macros.h"
namespace mediakit { namespace mediakit {
@ -222,7 +222,6 @@ public:
void setSize(size_t size); void setSize(size_t size);
protected: protected:
/** /**
* *
* 使net2Host转换成主机字节序后才可使用此函数 * 使net2Host转换成主机字节序后才可使用此函数
@ -712,8 +711,7 @@ private:
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
*/ */
class RtcpXRRRTR : public RtcpHeader class RtcpXRRRTR : public RtcpHeader {
{
public: public:
friend class RtcpHeader; friend class RtcpHeader;
uint32_t ssrc; uint32_t ssrc;
@ -726,6 +724,7 @@ public:
uint32_t ntpmsw; uint32_t ntpmsw;
// ntp timestamp LSW(in picosecond) // ntp timestamp LSW(in picosecond)
uint32_t ntplsw; uint32_t ntplsw;
private: private:
/** /**
* *
@ -759,13 +758,13 @@ private:
: ... : 2 : ... : 2
+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+ +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
*/ */
class RtcpXRDLRRReportItem class RtcpXRDLRRReportItem {
{
public: public:
friend class RtcpXRDLRR; friend class RtcpXRDLRR;
uint32_t ssrc; uint32_t ssrc;
uint32_t lrr; uint32_t lrr;
uint32_t dlrr; uint32_t dlrr;
private: private:
/** /**
* *
@ -780,10 +779,7 @@ private:
void net2Host(); void net2Host();
} PACKED; } PACKED;
class RtcpXRDLRR : public RtcpHeader {
class RtcpXRDLRR : public RtcpHeader
{
public: public:
friend class RtcpHeader; friend class RtcpHeader;
uint32_t ssrc; uint32_t ssrc;
@ -820,7 +816,6 @@ private:
} PACKED; } PACKED;
#if defined(_WIN32) #if defined(_WIN32)
#pragma pack(pop) #pragma pack(pop)
#endif // defined(_WIN32) #endif // defined(_WIN32)

View File

@ -14,7 +14,8 @@ using namespace toolkit;
namespace mediakit { namespace mediakit {
void RtcpContext::onRtp(uint16_t /*seq*/, uint32_t stamp, uint64_t ntp_stamp_ms, uint32_t /*sample_rate*/, size_t bytes) { void RtcpContext::onRtp(
uint16_t /*seq*/, uint32_t stamp, uint64_t ntp_stamp_ms, uint32_t /*sample_rate*/, size_t bytes) {
++_packets; ++_packets;
_bytes += bytes; _bytes += bytes;
_last_rtp_stamp = stamp; _last_rtp_stamp = stamp;
@ -79,7 +80,8 @@ void RtcpContextForSend::onRtcp(RtcpHeader *rtcp) {
case RtcpType::RTCP_XR: { case RtcpType::RTCP_XR: {
auto rtcp_xr = (RtcpXRRRTR *)rtcp; auto rtcp_xr = (RtcpXRRRTR *)rtcp;
if (rtcp_xr->bt == 4) { if (rtcp_xr->bt == 4) {
_xr_xrrtr_recv_last_rr[rtcp_xr->ssrc] = ((rtcp_xr->ntpmsw & 0xFFFF) << 16) | ((rtcp_xr->ntplsw >> 16) & 0xFFFF); _xr_xrrtr_recv_last_rr[rtcp_xr->ssrc]
= ((rtcp_xr->ntpmsw & 0xFFFF) << 16) | ((rtcp_xr->ntplsw >> 16) & 0xFFFF);
_xr_rrtr_recv_sys_stamp[rtcp_xr->ssrc] = getCurrentMillisecond(); _xr_rrtr_recv_sys_stamp[rtcp_xr->ssrc] = getCurrentMillisecond();
} else if (rtcp_xr->bt == 5) { } else if (rtcp_xr->bt == 5) {
TraceL << "for sender not recive dlrr"; TraceL << "for sender not recive dlrr";
@ -88,7 +90,8 @@ void RtcpContextForSend::onRtcp(RtcpHeader *rtcp) {
} }
break; break;
} }
default: break; default:
break;
} }
} }
@ -149,13 +152,15 @@ toolkit::Buffer::Ptr RtcpContextForSend::createRtcpXRDLRR(uint32_t rtcp_ssrc, ui
//////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////
void RtcpContextForRecv::onRtp(uint16_t seq, uint32_t stamp, uint64_t ntp_stamp_ms, uint32_t sample_rate, size_t bytes) { void RtcpContextForRecv::onRtp(
uint16_t seq, uint32_t stamp, uint64_t ntp_stamp_ms, uint32_t sample_rate, size_t bytes) {
{ {
// 接收者才做复杂的统计运算 // 接收者才做复杂的统计运算
auto sys_stamp = getCurrentMillisecond(); auto sys_stamp = getCurrentMillisecond();
if (_last_rtp_sys_stamp) { if (_last_rtp_sys_stamp) {
// 计算时间戳抖动值 // 计算时间戳抖动值
double diff = double((int64_t(sys_stamp) - int64_t(_last_rtp_sys_stamp)) * (sample_rate / double(1000.0)) double diff = double(
(int64_t(sys_stamp) - int64_t(_last_rtp_sys_stamp)) * (sample_rate / double(1000.0))
- (int64_t(stamp) - int64_t(_last_rtp_stamp))); - (int64_t(stamp) - int64_t(_last_rtp_stamp)));
if (diff < 0) { if (diff < 0) {
diff = -diff; diff = -diff;
@ -206,7 +211,8 @@ void RtcpContextForRecv::onRtcp(RtcpHeader *rtcp) {
_last_sr_ntp_sys = getCurrentMillisecond(); _last_sr_ntp_sys = getCurrentMillisecond();
break; break;
} }
default: break; default:
break;
} }
} }

View File

@ -11,9 +11,9 @@
#ifndef ZLMEDIAKIT_RTCPCONTEXT_H #ifndef ZLMEDIAKIT_RTCPCONTEXT_H
#define ZLMEDIAKIT_RTCPCONTEXT_H #define ZLMEDIAKIT_RTCPCONTEXT_H
#include <stdint.h>
#include <stddef.h>
#include "Rtcp.h" #include "Rtcp.h"
#include <stddef.h>
#include <stdint.h>
namespace mediakit { namespace mediakit {
@ -55,7 +55,6 @@ public:
*/ */
virtual toolkit::Buffer::Ptr createRtcpSR(uint32_t rtcp_ssrc); virtual toolkit::Buffer::Ptr createRtcpSR(uint32_t rtcp_ssrc);
/** /**
* @brief xr的dlrr包rtt * @brief xr的dlrr包rtt
* *

View File

@ -465,9 +465,11 @@ FCI_TWCC::TwccPacketStatus FCI_TWCC::getPacketChunkList(size_t total_size) const
string FCI_TWCC::dumpString(size_t total_size) const { string FCI_TWCC::dumpString(size_t total_size) const {
_StrPrinter printer; _StrPrinter printer;
auto map = getPacketChunkList(total_size); auto map = getPacketChunkList(total_size);
printer << "twcc fci, base_seq:" << getBaseSeq() << ", pkt_status_count:" << getPacketCount() << ", ref time:" << getReferenceTime() << ", fb count:" << (int)fb_pkt_count << "\n"; printer << "twcc fci, base_seq:" << getBaseSeq() << ", pkt_status_count:" << getPacketCount()
<< ", ref time:" << getReferenceTime() << ", fb count:" << (int)fb_pkt_count << "\n";
for (auto &pr : map) { for (auto &pr : map) {
printer << "rtp seq:" << pr.first <<", packet status:" << (int)(pr.second.first) << ", delta:" << pr.second.second << "\n"; printer << "rtp seq:" << pr.first << ", packet status:" << (int)(pr.second.first)
<< ", delta:" << pr.second.second << "\n";
} }
return std::move(printer); return std::move(printer);
} }
@ -476,10 +478,14 @@ static void appendDeltaString(string &delta_str, FCI_TWCC::TwccPacketStatus &sta
for (auto it = status.begin(); it != status.end() && count--;) { for (auto it = status.begin(); it != status.end() && count--;) {
switch (it->second.first) { switch (it->second.first) {
// large delta模式先写高字节再写低字节 // large delta模式先写高字节再写低字节
case SymbolStatus::large_delta: delta_str.push_back((it->second.second >> 8) & 0xFF); case SymbolStatus::large_delta:
delta_str.push_back((it->second.second >> 8) & 0xFF);
// small delta模式只写低字节 // small delta模式只写低字节
case SymbolStatus::small_delta: delta_str.push_back(it->second.second & 0xFF); break; case SymbolStatus::small_delta:
default: break; delta_str.push_back(it->second.second & 0xFF);
break;
default:
break;
} }
// 移除已经处理过的数据 // 移除已经处理过的数据
it = status.erase(it); it = status.erase(it);

View File

@ -11,8 +11,8 @@
#ifndef ZLMEDIAKIT_RTCPFCI_H #ifndef ZLMEDIAKIT_RTCPFCI_H
#define ZLMEDIAKIT_RTCPFCI_H #define ZLMEDIAKIT_RTCPFCI_H
#include "Rtcp.h"
#include "Common/config.h" #include "Common/config.h"
#include "Rtcp.h"
namespace mediakit { namespace mediakit {
@ -354,7 +354,8 @@ enum class SymbolStatus : uint8_t{
class FCI_TWCC { class FCI_TWCC {
public: public:
static size_t constexpr kSize = 8; static size_t constexpr kSize = 8;
using TwccPacketStatus = std::map<uint16_t/*rtp ext seq*/, std::pair<SymbolStatus, int16_t/*recv delta,单位为250us*/> >; using TwccPacketStatus
= std::map<uint16_t /*rtp ext seq*/, std::pair<SymbolStatus, int16_t /*recv delta,单位为250us*/>>;
void check(size_t size); void check(size_t size);
std::string dumpString(size_t total_size) const; std::string dumpString(size_t total_size) const;
uint16_t getBaseSeq() const; uint16_t getBaseSeq() const;

View File

@ -9,11 +9,11 @@
*/ */
#include "WebRtcTransport.h" #include "WebRtcTransport.h"
#include <iostream>
#include "RtpExt.h"
#include "Rtcp/Rtcp.h" #include "Rtcp/Rtcp.h"
#include "Rtcp/RtcpFCI.h" #include "Rtcp/RtcpFCI.h"
#include "RtpExt.h"
#include "Rtsp/RtpReceiver.h" #include "Rtsp/RtpReceiver.h"
#include <iostream>
#define RTP_SSRC_OFFSET 1 #define RTP_SSRC_OFFSET 1
#define RTX_SSRC_OFFSET 2 #define RTX_SSRC_OFFSET 2
@ -92,7 +92,8 @@ const string &WebRtcTransport::getIdentifier() const {
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void WebRtcTransport::OnIceServerSendStunPacket(const RTC::IceServer *iceServer, const RTC::StunPacket *packet, RTC::TransportTuple *tuple) { void WebRtcTransport::OnIceServerSendStunPacket(
const RTC::IceServer *iceServer, const RTC::StunPacket *packet, RTC::TransportTuple *tuple) {
sendSockData((char *)packet->GetData(), packet->GetSize(), tuple); sendSockData((char *)packet->GetData(), packet->GetSize(), tuple);
} }
@ -120,16 +121,13 @@ void WebRtcTransport::OnIceServerDisconnected(const RTC::IceServer *iceServer) {
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void WebRtcTransport::OnDtlsTransportConnected( void WebRtcTransport::OnDtlsTransportConnected(
const RTC::DtlsTransport *dtlsTransport, const RTC::DtlsTransport *dtlsTransport, RTC::SrtpSession::CryptoSuite srtpCryptoSuite, uint8_t *srtpLocalKey,
RTC::SrtpSession::CryptoSuite srtpCryptoSuite, size_t srtpLocalKeyLen, uint8_t *srtpRemoteKey, size_t srtpRemoteKeyLen, std::string &remoteCert) {
uint8_t *srtpLocalKey,
size_t srtpLocalKeyLen,
uint8_t *srtpRemoteKey,
size_t srtpRemoteKeyLen,
std::string &remoteCert) {
InfoL; InfoL;
_srtp_session_send = std::make_shared<RTC::SrtpSession>(RTC::SrtpSession::Type::OUTBOUND, srtpCryptoSuite, srtpLocalKey, srtpLocalKeyLen); _srtp_session_send = std::make_shared<RTC::SrtpSession>(
_srtp_session_recv = std::make_shared<RTC::SrtpSession>(RTC::SrtpSession::Type::INBOUND, srtpCryptoSuite, srtpRemoteKey, srtpRemoteKeyLen); RTC::SrtpSession::Type::OUTBOUND, srtpCryptoSuite, srtpLocalKey, srtpLocalKeyLen);
_srtp_session_recv = std::make_shared<RTC::SrtpSession>(
RTC::SrtpSession::Type::INBOUND, srtpCryptoSuite, srtpRemoteKey, srtpRemoteKeyLen);
#ifdef ENABLE_SCTP #ifdef ENABLE_SCTP
_sctp = std::make_shared<RTC::SctpAssociationImp>(getPoller(), this, 128, 128, 262144, true); _sctp = std::make_shared<RTC::SctpAssociationImp>(getPoller(), this, 128, 128, 262144, true);
_sctp->TransportConnected(); _sctp->TransportConnected();
@ -137,7 +135,8 @@ void WebRtcTransport::OnDtlsTransportConnected(
onStartWebRTC(); onStartWebRTC();
} }
void WebRtcTransport::OnDtlsTransportSendData(const RTC::DtlsTransport *dtlsTransport, const uint8_t *data, size_t len) { void WebRtcTransport::OnDtlsTransportSendData(
const RTC::DtlsTransport *dtlsTransport, const uint8_t *data, size_t len) {
sendSockData((char *)data, len, nullptr); sendSockData((char *)data, len, nullptr);
} }
@ -155,7 +154,8 @@ void WebRtcTransport::OnDtlsTransportClosed(const RTC::DtlsTransport *dtlsTransp
onShutdown(SockException(Err_shutdown, "dtls close notify received")); onShutdown(SockException(Err_shutdown, "dtls close notify received"));
} }
void WebRtcTransport::OnDtlsTransportApplicationDataReceived(const RTC::DtlsTransport *dtlsTransport, const uint8_t *data, size_t len) { void WebRtcTransport::OnDtlsTransportApplicationDataReceived(
const RTC::DtlsTransport *dtlsTransport, const uint8_t *data, size_t len) {
#ifdef ENABLE_SCTP #ifdef ENABLE_SCTP
_sctp->ProcessSctpData(data, len); _sctp->ProcessSctpData(data, len);
#else #else
@ -181,12 +181,13 @@ void WebRtcTransport::OnSctpAssociationClosed(RTC::SctpAssociation* sctpAssociat
InfoL << getIdentifier(); InfoL << getIdentifier();
} }
void WebRtcTransport::OnSctpAssociationSendData(RTC::SctpAssociation* sctpAssociation, const uint8_t* data, size_t len) { void WebRtcTransport::OnSctpAssociationSendData(
RTC::SctpAssociation *sctpAssociation, const uint8_t *data, size_t len) {
_dtls_transport->SendApplicationData(data, len); _dtls_transport->SendApplicationData(data, len);
} }
void WebRtcTransport::OnSctpAssociationMessageReceived(RTC::SctpAssociation *sctpAssociation, uint16_t streamId, void WebRtcTransport::OnSctpAssociationMessageReceived(
uint32_t ppid, const uint8_t *msg, size_t len) { RTC::SctpAssociation *sctpAssociation, uint16_t streamId, uint32_t ppid, const uint8_t *msg, size_t len) {
InfoL << getIdentifier() << " " << streamId << " " << ppid << " " << len << " " << string((char *)msg, len); InfoL << getIdentifier() << " " << streamId << " " << ppid << " " << len << " " << string((char *)msg, len);
RTC::SctpStreamParameters params; RTC::SctpStreamParameters params;
params.streamId = streamId; params.streamId = streamId;
@ -234,7 +235,8 @@ string getFingerprint(const string &algorithm_str, const std::shared_ptr<RTC::Dt
void WebRtcTransport::setRemoteDtlsFingerprint(const RtcSession &remote) { void WebRtcTransport::setRemoteDtlsFingerprint(const RtcSession &remote) {
// 设置远端dtls签名 // 设置远端dtls签名
RTC::DtlsTransport::Fingerprint remote_fingerprint; RTC::DtlsTransport::Fingerprint remote_fingerprint;
remote_fingerprint.algorithm = RTC::DtlsTransport::GetFingerprintAlgorithm(_offer_sdp->media[0].fingerprint.algorithm); remote_fingerprint.algorithm
= RTC::DtlsTransport::GetFingerprintAlgorithm(_offer_sdp->media[0].fingerprint.algorithm);
remote_fingerprint.value = _offer_sdp->media[0].fingerprint.hash; remote_fingerprint.value = _offer_sdp->media[0].fingerprint.hash;
_dtls_transport->SetRemoteFingerprint(remote_fingerprint); _dtls_transport->SetRemoteFingerprint(remote_fingerprint);
} }
@ -259,8 +261,8 @@ std::string WebRtcTransport::getAnswerSdp(const string &offer){
fingerprint.algorithm = _offer_sdp->media[0].fingerprint.algorithm; fingerprint.algorithm = _offer_sdp->media[0].fingerprint.algorithm;
fingerprint.hash = getFingerprint(fingerprint.algorithm, _dtls_transport); fingerprint.hash = getFingerprint(fingerprint.algorithm, _dtls_transport);
RtcConfigure configure; RtcConfigure configure;
configure.setDefaultSetting(_ice_server->GetUsernameFragment(), _ice_server->GetPassword(), configure.setDefaultSetting(
RtpDirection::sendrecv, fingerprint); _ice_server->GetUsernameFragment(), _ice_server->GetPassword(), RtpDirection::sendrecv, fingerprint);
onRtcConfigure(configure); onRtcConfigure(configure);
//// 生成answer sdp //// //// 生成answer sdp ////
@ -364,7 +366,9 @@ void WebRtcTransportImp::onCreate(){
weak_ptr<WebRtcTransportImp> weak_self = static_pointer_cast<WebRtcTransportImp>(shared_from_this()); weak_ptr<WebRtcTransportImp> weak_self = static_pointer_cast<WebRtcTransportImp>(shared_from_this());
GET_CONFIG(float, timeoutSec, RTC::kTimeOutSec); GET_CONFIG(float, timeoutSec, RTC::kTimeOutSec);
_timer = std::make_shared<Timer>(timeoutSec / 2, [weak_self]() { _timer = std::make_shared<Timer>(
timeoutSec / 2,
[weak_self]() {
auto strong_self = weak_self.lock(); auto strong_self = weak_self.lock();
if (!strong_self) { if (!strong_self) {
return false; return false;
@ -373,14 +377,14 @@ void WebRtcTransportImp::onCreate(){
strong_self->onShutdown(SockException(Err_timeout, "接受rtp和rtcp超时")); strong_self->onShutdown(SockException(Err_timeout, "接受rtp和rtcp超时"));
} }
return true; return true;
}, getPoller()); },
getPoller());
_twcc_ctx.setOnSendTwccCB([this](uint32_t ssrc, string fci) { _twcc_ctx.setOnSendTwccCB([this](uint32_t ssrc, string fci) { onSendTwcc(ssrc, fci); });
onSendTwcc(ssrc, fci);
});
} }
WebRtcTransportImp::WebRtcTransportImp(const EventPoller::Ptr &poller) : WebRtcTransport(poller) { WebRtcTransportImp::WebRtcTransportImp(const EventPoller::Ptr &poller)
: WebRtcTransport(poller) {
InfoL << getIdentifier(); InfoL << getIdentifier();
} }
@ -455,7 +459,8 @@ void WebRtcTransportImp::onStartWebRTC() {
_ssrc_to_track[track->offer_ssrc_rtx] = track; _ssrc_to_track[track->offer_ssrc_rtx] = track;
// rtp pt --> MediaTrack // rtp pt --> MediaTrack
_pt_to_track.emplace(track->plan_rtp->pt, std::unique_ptr<WrappedMediaTrack>(new WrappedRtpTrack(track, _twcc_ctx, *this))); _pt_to_track.emplace(
track->plan_rtp->pt, std::unique_ptr<WrappedMediaTrack>(new WrappedRtpTrack(track, _twcc_ctx, *this)));
if (track->plan_rtx) { if (track->plan_rtx) {
// rtx pt --> MediaTrack // rtx pt --> MediaTrack
_pt_to_track.emplace(track->plan_rtx->pt, std::unique_ptr<WrappedMediaTrack>(new WrappedRtxTrack(track))); _pt_to_track.emplace(track->plan_rtx->pt, std::unique_ptr<WrappedMediaTrack>(new WrappedRtxTrack(track)));
@ -546,14 +551,19 @@ void WebRtcTransportImp::onCheckAnswer(RtcSession &sdp) {
void WebRtcTransportImp::onCheckSdp(SdpType type, RtcSession &sdp) { void WebRtcTransportImp::onCheckSdp(SdpType type, RtcSession &sdp) {
switch (type) { switch (type) {
case SdpType::answer: onCheckAnswer(sdp); break; case SdpType::answer:
case SdpType::offer: break; onCheckAnswer(sdp);
default: /*不可达*/ assert(0); break; break;
case SdpType::offer:
break;
default: /*不可达*/
assert(0);
break;
} }
} }
SdpAttrCandidate::Ptr makeIceCandidate(std::string ip, uint16_t port, SdpAttrCandidate::Ptr
uint32_t priority = 100, std::string proto = "udp") { makeIceCandidate(std::string ip, uint16_t port, uint32_t priority = 100, std::string proto = "udp") {
auto candidate = std::make_shared<SdpAttrCandidate>(); auto candidate = std::make_shared<SdpAttrCandidate>();
// rtp端口 // rtp端口
candidate->component = 1; candidate->component = 1;
@ -593,19 +603,18 @@ void WebRtcTransportImp::onRtcConfigure(RtcConfigure &configure) const {
} }
} }
/////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////
class RtpChannel : public RtpTrackImp, public std::enable_shared_from_this<RtpChannel> { class RtpChannel
: public RtpTrackImp
, public std::enable_shared_from_this<RtpChannel> {
public: public:
RtpChannel(EventPoller::Ptr poller, RtpTrackImp::OnSorted cb, function<void(const FCI_NACK &nack)> on_nack) { RtpChannel(EventPoller::Ptr poller, RtpTrackImp::OnSorted cb, function<void(const FCI_NACK &nack)> on_nack) {
_poller = std::move(poller); _poller = std::move(poller);
_on_nack = std::move(on_nack); _on_nack = std::move(on_nack);
setOnSorted(std::move(cb)); setOnSorted(std::move(cb));
_nack_ctx.setOnNack([this](const FCI_NACK &nack) { _nack_ctx.setOnNack([this](const FCI_NACK &nack) { onNack(nack); });
onNack(nack);
});
} }
~RtpChannel() override = default; ~RtpChannel() override = default;
@ -770,7 +779,8 @@ void WebRtcTransportImp::onRtcp(const char *buf, size_t len) {
}); });
break; break;
} }
default: break; default:
break;
} }
break; break;
} }
@ -791,7 +801,8 @@ void WebRtcTransportImp::onRtcp(const char *buf, size_t len) {
break; break;
} }
default: break; default:
break;
} }
} }
} }
@ -802,9 +813,9 @@ void WebRtcTransportImp::createRtpChannel(const string &rid, uint32_t ssrc, Medi
// rid --> RtpReceiverImp // rid --> RtpReceiverImp
auto &ref = track.rtp_channel[rid]; auto &ref = track.rtp_channel[rid];
weak_ptr<WebRtcTransportImp> weak_self = dynamic_pointer_cast<WebRtcTransportImp>(shared_from_this()); weak_ptr<WebRtcTransportImp> weak_self = dynamic_pointer_cast<WebRtcTransportImp>(shared_from_this());
ref = std::make_shared<RtpChannel>(getPoller(), [&track, this, rid](RtpPacket::Ptr rtp) mutable { ref = std::make_shared<RtpChannel>(
onSortedRtp(track, rid, std::move(rtp)); getPoller(), [&track, this, rid](RtpPacket::Ptr rtp) mutable { onSortedRtp(track, rid, std::move(rtp)); },
}, [&track, weak_self, ssrc](const FCI_NACK &nack) mutable { [&track, weak_self, ssrc](const FCI_NACK &nack) mutable {
// nack发送可能由定时器异步触发 // nack发送可能由定时器异步触发
auto strong_self = weak_self.lock(); auto strong_self = weak_self.lock();
if (strong_self) { if (strong_self) {
@ -868,7 +879,8 @@ void WrappedRtxTrack::inputRtp(const char *buf, size_t len, uint64_t stamp_ms, R
auto &ref = track->rtp_channel[rid]; auto &ref = track->rtp_channel[rid];
if (!ref) { if (!ref) {
// 再接收到对应的rtp前丢弃rtx包 // 再接收到对应的rtp前丢弃rtx包
WarnL << "unknown rtx rtp, rid:" << rid << ", ssrc:" << ntohl(rtp->ssrc) << ", codec:" << track->plan_rtp->codec << ", seq:" << ntohs(rtp->seq); WarnL << "unknown rtx rtp, rid:" << rid << ", ssrc:" << ntohl(rtp->ssrc) << ", codec:" << track->plan_rtp->codec
<< ", seq:" << ntohs(rtp->seq);
return; return;
} }
@ -935,7 +947,9 @@ void WebRtcTransportImp::onSendRtp(const RtpPacket::Ptr &rtp, bool flush, bool r
} }
if (!rtx) { if (!rtx) {
// 统计rtp发送情况好做sr汇报 // 统计rtp发送情况好做sr汇报
track->rtcp_context_send->onRtp(rtp->getSeq(), rtp->getStamp(), rtp->ntp_stamp, rtp->sample_rate, rtp->size() - RtpPacket::kRtpTcpHeaderSize); track->rtcp_context_send->onRtp(
rtp->getSeq(), rtp->getStamp(), rtp->ntp_stamp, rtp->sample_rate,
rtp->size() - RtpPacket::kRtpTcpHeaderSize);
track->nack_list.pushBack(rtp); track->nack_list.pushBack(rtp);
#if 0 #if 0
//此处模拟发送丢包 //此处模拟发送丢包
@ -1005,8 +1019,9 @@ void WebRtcTransportImp::onShutdown(const SockException &ex){
void WebRtcTransportImp::setSession(Session::Ptr session) { void WebRtcTransportImp::setSession(Session::Ptr session) {
_history_sessions.emplace(session.get(), session); _history_sessions.emplace(session.get(), session);
if (_selected_session) { if (_selected_session) {
InfoL << "rtc network changed: " << _selected_session->get_peer_ip() << ":" << _selected_session->get_peer_port() InfoL << "rtc network changed: " << _selected_session->get_peer_ip() << ":"
<< " -> " << session->get_peer_ip() << ":" << session->get_peer_port() << ", id:" << getIdentifier(); << _selected_session->get_peer_port() << " -> " << session->get_peer_ip() << ":"
<< session->get_peer_port() << ", id:" << getIdentifier();
} }
_selected_session = std::move(session); _selected_session = std::move(session);
unrefSelf(); unrefSelf();
@ -1079,8 +1094,8 @@ void WebRtcPluginManager::registerPlugin(const string &type, Plugin cb) {
_map_creator[type] = std::move(cb); _map_creator[type] = std::move(cb);
} }
void WebRtcPluginManager::getAnswerSdp(Session &sender, const string &type, const string &offer, const WebRtcArgs &args, void WebRtcPluginManager::getAnswerSdp(
const onCreateRtc &cb) { Session &sender, const string &type, const string &offer, const WebRtcArgs &args, const onCreateRtc &cb) {
lock_guard<mutex> lck(_mtx_creator); lock_guard<mutex> lck(_mtx_creator);
auto it = _map_creator.find(type); auto it = _map_creator.find(type);
if (it == _map_creator.end()) { if (it == _map_creator.end()) {
@ -1090,17 +1105,20 @@ void WebRtcPluginManager::getAnswerSdp(Session &sender, const string &type, cons
it->second(sender, offer, args, cb); it->second(sender, offer, args, cb);
} }
#include "WebRtcEchoTest.h"
#include "WebRtcPlayer.h" #include "WebRtcPlayer.h"
#include "WebRtcPusher.h" #include "WebRtcPusher.h"
#include "WebRtcEchoTest.h"
void echo_plugin(Session &sender, const string &offer, const WebRtcArgs &args, const WebRtcPluginManager::onCreateRtc &cb) { void echo_plugin(
Session &sender, const string &offer, const WebRtcArgs &args, const WebRtcPluginManager::onCreateRtc &cb) {
cb(*WebRtcEchoTest::create(EventPollerPool::Instance().getPoller())); cb(*WebRtcEchoTest::create(EventPollerPool::Instance().getPoller()));
} }
void push_plugin(Session &sender, const string &offer_sdp, const WebRtcArgs &args, const WebRtcPluginManager::onCreateRtc &cb) { void push_plugin(
Session &sender, const string &offer_sdp, const WebRtcArgs &args, const WebRtcPluginManager::onCreateRtc &cb) {
MediaInfo info(args["url"]); MediaInfo info(args["url"]);
Broadcast::PublishAuthInvoker invoker = [cb, offer_sdp, info](const string &err, const ProtocolOption &option) mutable { Broadcast::PublishAuthInvoker invoker = [cb, offer_sdp,
info](const string &err, const ProtocolOption &option) mutable {
if (!err.empty()) { if (!err.empty()) {
cb(WebRtcException(SockException(Err_other, err))); cb(WebRtcException(SockException(Err_other, err)));
return; return;
@ -1139,20 +1157,23 @@ void push_plugin(Session &sender, const string &offer_sdp, const WebRtcArgs &arg
push_src_ownership = push_src->getOwnership(); push_src_ownership = push_src->getOwnership();
push_src->setProtocolOption(option); push_src->setProtocolOption(option);
} }
auto rtc = WebRtcPusher::create(EventPollerPool::Instance().getPoller(), push_src, push_src_ownership, info, option); auto rtc
= WebRtcPusher::create(EventPollerPool::Instance().getPoller(), push_src, push_src_ownership, info, option);
push_src->setListener(rtc); push_src->setListener(rtc);
cb(*rtc); cb(*rtc);
}; };
// rtsp推流需要鉴权 // rtsp推流需要鉴权
auto flag = NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastMediaPublish, MediaOriginType::rtc_push, info, invoker, static_cast<SockInfo &>(sender)); auto flag = NoticeCenter::Instance().emitEvent(
Broadcast::kBroadcastMediaPublish, MediaOriginType::rtc_push, info, invoker, static_cast<SockInfo &>(sender));
if (!flag) { if (!flag) {
// 该事件无人监听,默认不鉴权 // 该事件无人监听,默认不鉴权
invoker("", ProtocolOption()); invoker("", ProtocolOption());
} }
} }
void play_plugin(Session &sender, const string &offer_sdp, const WebRtcArgs &args, const WebRtcPluginManager::onCreateRtc &cb) { void play_plugin(
Session &sender, const string &offer_sdp, const WebRtcArgs &args, const WebRtcPluginManager::onCreateRtc &cb) {
MediaInfo info(args["url"]); MediaInfo info(args["url"]);
auto session_ptr = sender.shared_from_this(); auto session_ptr = sender.shared_from_this();
Broadcast::AuthInvoker invoker = [cb, offer_sdp, info, session_ptr](const string &err) mutable { Broadcast::AuthInvoker invoker = [cb, offer_sdp, info, session_ptr](const string &err) mutable {
@ -1177,7 +1198,8 @@ void play_plugin(Session &sender, const string &offer_sdp, const WebRtcArgs &arg
}; };
// 广播通用播放url鉴权事件 // 广播通用播放url鉴权事件
auto flag = NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastMediaPlayed, info, invoker, static_cast<SockInfo &>(sender)); auto flag = NoticeCenter::Instance().emitEvent(
Broadcast::kBroadcastMediaPlayed, info, invoker, static_cast<SockInfo &>(sender));
if (!flag) { if (!flag) {
// 该事件无人监听,默认不鉴权 // 该事件无人监听,默认不鉴权
invoker(""); invoker("");