diff --git a/api/source/mk_webrtc_private.h b/api/source/mk_webrtc_private.h index 80ec8332..0809ce18 100644 --- a/api/source/mk_webrtc_private.h +++ b/api/source/mk_webrtc_private.h @@ -24,6 +24,8 @@ #include "../webrtc/WebRtcPusher.h" #include "../webrtc/WebRtcTransport.h" +using namespace mediakit; + namespace API { typedef enum { NotFound = -500, //未找到 diff --git a/server/main.cpp b/server/main.cpp index f98e9640..e1b0e8f6 100644 --- a/server/main.cpp +++ b/server/main.cpp @@ -286,7 +286,7 @@ int start_main(int argc,char *argv[]) { } return Socket::createSocket(new_poller, false); }); - uint16_t rtcPort = mINI::Instance()[RTC::kPort]; + uint16_t rtcPort = mINI::Instance()[Rtc::kPort]; #endif//defined(ENABLE_WEBRTC) diff --git a/tests/test_server.cpp b/tests/test_server.cpp index d488e61a..7d073b5e 100644 --- a/tests/test_server.cpp +++ b/tests/test_server.cpp @@ -83,7 +83,7 @@ onceToken token1([](){ } // namespace mediakit -#define REALM "realm_zlmedaikit" +#define REALM "realm_zlmediakit" static map s_mapFlvRecorder; static mutex s_mtxFlvRecorder; diff --git a/webrtc/Nack.cpp b/webrtc/Nack.cpp index 1ac2ff9b..63e615b5 100644 --- a/webrtc/Nack.cpp +++ b/webrtc/Nack.cpp @@ -12,7 +12,8 @@ using namespace std; using namespace toolkit; -using namespace mediakit; + +namespace mediakit { static constexpr uint32_t kMaxNackMS = 5 * 1000; static constexpr uint32_t kRtpCacheCheckInterval = 100; @@ -92,7 +93,6 @@ int64_t NackList::getRtpStamp(uint16_t seq) { return it->second->getStampMS(false); } - //////////////////////////////////////////////////////////////////////////////////////////////// void NackContext::received(uint16_t seq, bool is_rtx) { @@ -101,7 +101,7 @@ void NackContext::received(uint16_t seq, bool is_rtx) { } if (is_rtx || (seq < _last_max_seq && !(seq < 1024 && _last_max_seq > UINT16_MAX - 1024))) { //重传包或 - //seq回退,且非回环,那么这个应该是重传包 + // seq回退,且非回环,那么这个应该是重传包 onRtx(seq); return; } @@ -127,7 +127,7 @@ void NackContext::received(uint16_t seq, bool is_rtx) { _seq.clear(); _last_max_seq = max_seq; } else { - //seq不连续,有丢包 + // seq不连续,有丢包 if (min_seq == _last_max_seq + 1) { //前面部分seq是连续的,未丢包,移除之 eraseFrontSeq(); @@ -135,7 +135,7 @@ void NackContext::received(uint16_t seq, bool is_rtx) { //有丢包,丢包从_last_max_seq开始 auto nack_rtp_count = FCI_NACK::kBitSize; - if (max_seq > nack_rtp_count + _last_max_seq) { + if (max_seq > nack_rtp_count + _last_max_seq) { vector vec; vec.resize(FCI_NACK::kBitSize, false); for (size_t i = 0; i < nack_rtp_count; ++i) { @@ -170,7 +170,7 @@ void NackContext::eraseFrontSeq() { //前面部分seq是连续的,未丢包,移除之 for (auto it = _seq.begin(); it != _seq.end();) { if (*it != _last_max_seq + 1) { - //seq不连续,丢包了 + // seq不连续,丢包了 break; } _last_max_seq = *it; @@ -187,9 +187,9 @@ void NackContext::onRtx(uint16_t seq) { _nack_send_status.erase(it); if (rtt >= 0) { - //rtt不肯小于0 + // rtt不肯小于0 _rtt = rtt; - //InfoL << "rtt:" << rtt; + // InfoL << "rtt:" << rtt; } } @@ -230,7 +230,7 @@ uint64_t NackContext::reSendNack() { //更新nack发送时间戳 it->second.update_stamp = now; if (++(it->second.nack_count) == kNackMaxCount) { - //nack次数太多,移除之 + // nack次数太多,移除之 it = _nack_send_status.erase(it); continue; } @@ -269,3 +269,5 @@ uint64_t NackContext::reSendNack() { //重传间隔不得低于5ms return max(_rtt, 5); } + +} // namespace mediakit diff --git a/webrtc/Nack.h b/webrtc/Nack.h index 761c618d..2a02f311 100644 --- a/webrtc/Nack.h +++ b/webrtc/Nack.h @@ -14,38 +14,39 @@ #include "Rtsp/Rtsp.h" #include "Rtcp/RtcpFCI.h" +namespace mediakit { class NackList { public: NackList() = default; ~NackList() = default; - void pushBack(mediakit::RtpPacket::Ptr rtp); - void forEach(const mediakit::FCI_NACK &nack, const std::function &cb); + void pushBack(RtpPacket::Ptr rtp); + void forEach(const FCI_NACK &nack, const std::function &cb); private: void popFront(); uint32_t getCacheMS(); int64_t getRtpStamp(uint16_t seq); - mediakit::RtpPacket::Ptr *getRtp(uint16_t seq); + RtpPacket::Ptr *getRtp(uint16_t seq); private: uint32_t _cache_ms_check = 0; std::deque _nack_cache_seq; - std::unordered_map _nack_cache_pkt; + std::unordered_map _nack_cache_pkt; }; class NackContext { public: using Ptr = std::shared_ptr; - using onNack = std::function; + using onNack = std::function; //最大保留的rtp丢包状态个数 static constexpr auto kNackMaxSize = 2048; - //rtp丢包状态最长保留时间 + // rtp丢包状态最长保留时间 static constexpr auto kNackMaxMS = 3 * 1000; - //nack最多请求重传10次 + // nack最多请求重传10次 static constexpr auto kNackMaxCount = 10; - //nack重传频率,rtt的倍数 + // nack重传频率,rtt的倍数 static constexpr auto kNackIntervalRatio = 1.0f; NackContext() = default; @@ -57,8 +58,8 @@ public: private: void eraseFrontSeq(); - void doNack(const mediakit::FCI_NACK &nack, bool record_nack); - void recordNack(const mediakit::FCI_NACK &nack); + void doNack(const FCI_NACK &nack, bool record_nack); + void recordNack(const FCI_NACK &nack); void onRtx(uint16_t seq); private: @@ -67,12 +68,14 @@ private: std::set _seq; uint16_t _last_max_seq = 0; - struct NackStatus{ + struct NackStatus { uint64_t first_stamp; uint64_t update_stamp; int nack_count = 0; }; - std::map _nack_send_status; + std::map _nack_send_status; }; +} // namespace mediakit + #endif //ZLMEDIAKIT_NACK_H diff --git a/webrtc/RtpExt.cpp b/webrtc/RtpExt.cpp index b9568584..e1424ea7 100644 --- a/webrtc/RtpExt.cpp +++ b/webrtc/RtpExt.cpp @@ -17,7 +17,8 @@ using namespace std; using namespace toolkit; -using namespace mediakit; + +namespace mediakit { //https://tools.ietf.org/html/draft-holmer-rmcat-transport-wide-cc-extensions-01 //https://tools.ietf.org/html/rfc5285 @@ -644,3 +645,4 @@ void RtpExtContext::onGetRtp(uint8_t pt, uint32_t ssrc, const string &rid){ } } +}// namespace mediakit \ No newline at end of file diff --git a/webrtc/RtpExt.h b/webrtc/RtpExt.h index 89581c88..6345b394 100644 --- a/webrtc/RtpExt.h +++ b/webrtc/RtpExt.h @@ -17,6 +17,7 @@ #include "Common/macros.h" #include "Rtsp/Rtsp.h" +namespace mediakit { #define RTP_EXT_MAP(XX) \ XX(ssrc_audio_level, "urn:ietf:params:rtp-hdrext:ssrc-audio-level") \ @@ -53,7 +54,7 @@ public: friend class RtpExtContext; ~RtpExt() = default; - static std::map getExtValue(const mediakit::RtpHeader *header); + static std::map getExtValue(const RtpHeader *header); static RtpExtType getExtType(const std::string &url); static const std::string& getExtUrl(RtpExtType type); static const char *getExtName(RtpExtType type); @@ -122,7 +123,7 @@ public: void setOnGetRtp(OnGetRtp cb); std::string getRid(uint32_t ssrc) const; void setRid(uint32_t ssrc, const std::string &rid); - RtpExt changeRtpExtId(const mediakit::RtpHeader *header, bool is_recv, std::string *rid_ptr = nullptr, RtpExtType type = RtpExtType::padding); + RtpExt changeRtpExtId(const RtpHeader *header, bool is_recv, std::string *rid_ptr = nullptr, RtpExtType type = RtpExtType::padding); private: void onGetRtp(uint8_t pt, uint32_t ssrc, const std::string &rid); @@ -137,4 +138,5 @@ private: std::unordered_map _ssrc_to_rid; }; +} //namespace mediakit #endif //ZLMEDIAKIT_RTPEXT_H diff --git a/webrtc/Sdp.cpp b/webrtc/Sdp.cpp index b8eb3304..2a6f5093 100644 --- a/webrtc/Sdp.cpp +++ b/webrtc/Sdp.cpp @@ -14,9 +14,10 @@ using namespace std; using namespace toolkit; -using namespace mediakit; -namespace RTC { +namespace mediakit { + +namespace Rtc { #define RTC_FIELD "rtc." const string kPreferredCodecA = RTC_FIELD"preferredCodecA"; const string kPreferredCodecV = RTC_FIELD"preferredCodecV"; @@ -1404,7 +1405,7 @@ void RtcConfigure::RtcTrackConfigure::setDefaultSetting(TrackType type){ switch (type) { case TrackAudio: { //此处调整偏好的编码格式优先级 - GET_CONFIG_FUNC(vector, s_preferred_codec, RTC::kPreferredCodecA, toCodecArray); + GET_CONFIG_FUNC(vector, s_preferred_codec, Rtc::kPreferredCodecA, toCodecArray); CHECK(!s_preferred_codec.empty(), "rtc音频偏好codec不能为空"); preferred_codec = s_preferred_codec; @@ -1423,7 +1424,7 @@ void RtcConfigure::RtcTrackConfigure::setDefaultSetting(TrackType type){ } case TrackVideo: { //此处调整偏好的编码格式优先级 - GET_CONFIG_FUNC(vector, s_preferred_codec, RTC::kPreferredCodecV, toCodecArray); + GET_CONFIG_FUNC(vector, s_preferred_codec, Rtc::kPreferredCodecV, toCodecArray); CHECK(!s_preferred_codec.empty(), "rtc视频偏好codec不能为空"); preferred_codec = s_preferred_codec; @@ -1811,3 +1812,5 @@ void RtcConfigure::onSelectPlan(RtcCodecPlan &plan, CodecId codec) const { plan.fmtp[kMode] = mode.empty() ? "0" : mode; } } + +} // namespace mediakit \ No newline at end of file diff --git a/webrtc/Sdp.h b/webrtc/Sdp.h index 240a6a07..564362a4 100644 --- a/webrtc/Sdp.h +++ b/webrtc/Sdp.h @@ -18,6 +18,8 @@ #include "Extension/Frame.h" #include "Common/Parser.h" +namespace mediakit { + //https://datatracker.ietf.org/doc/rfc4566/?include_text=1 //https://blog.csdn.net/aggresss/article/details/109850434 //https://aggresss.blog.csdn.net/article/details/106436703 @@ -189,7 +191,7 @@ class SdpMedia : public SdpItem { public: // 5.14. Media Descriptions ("m=") // m= ... - mediakit::TrackType type; + TrackType type; uint16_t port; //RTP/AVP:应用场景为视频/音频的 RTP 协议。参考 RFC 3551 //RTP/SAVP:应用场景为视频/音频的 SRTP 协议。参考 RFC 3711 @@ -374,7 +376,7 @@ class SdpAttrFmtp : public SdpItem { public: //fmtp:96 level-asymmetry-allowed=1;packetization-mode=0;profile-level-id=42e01f uint8_t pt; - std::map fmtp; + std::map fmtp; void parse(const std::string &str) override; std::string toString() const override; const char* getKey() const override { return "fmtp";} @@ -600,7 +602,7 @@ public: uint32_t channel = 0; //rtcp反馈 std::set rtcp_fb; - std::map fmtp; + std::map fmtp; std::string getFmtp(const char *key) const; }; @@ -608,7 +610,7 @@ public: //rtc 媒体描述 class RtcMedia{ public: - mediakit::TrackType type{mediakit::TrackType::TrackInvalid}; + TrackType type{TrackType::TrackInvalid}; std::string mid; uint16_t port{0}; SdpConnection addr; @@ -675,8 +677,8 @@ public: void checkValid() const; std::string toString() const; std::string toRtspSdp() const; - const RtcMedia *getMedia(mediakit::TrackType type) const; - bool supportRtcpFb(const std::string &name, mediakit::TrackType type = mediakit::TrackType::TrackVideo) const; + const RtcMedia *getMedia(TrackType type) const; + bool supportRtcpFb(const std::string &name, TrackType type = TrackType::TrackVideo) const; bool supportSimulcast() const; bool isOnlyDatachannel() const; @@ -706,10 +708,10 @@ public: std::set rtcp_fb; std::map extmap; - std::vector preferred_codec; + std::vector preferred_codec; std::vector candidate; - void setDefaultSetting(mediakit::TrackType type); + void setDefaultSetting(TrackType type); void enableTWCC(bool enable = true); void enableREMB(bool enable = true); }; @@ -719,19 +721,19 @@ public: RtcTrackConfigure application; void setDefaultSetting(std::string ice_ufrag, std::string ice_pwd, RtpDirection direction, const SdpAttrFingerprint &fingerprint); - void addCandidate(const SdpAttrCandidate &candidate, mediakit::TrackType type = mediakit::TrackInvalid); + void addCandidate(const SdpAttrCandidate &candidate, TrackType type = TrackInvalid); std::shared_ptr createAnswer(const RtcSession &offer) const; void setPlayRtspInfo(const std::string &sdp); - void enableTWCC(bool enable = true, mediakit::TrackType type = mediakit::TrackInvalid); - void enableREMB(bool enable = true, mediakit::TrackType type = mediakit::TrackInvalid); + void enableTWCC(bool enable = true, TrackType type = TrackInvalid); + void enableREMB(bool enable = true, TrackType type = TrackInvalid); private: void matchMedia(const std::shared_ptr &ret, const RtcMedia &media) const; - bool onCheckCodecProfile(const RtcCodecPlan &plan, mediakit::CodecId codec) const; - void onSelectPlan(RtcCodecPlan &plan, mediakit::CodecId codec) const; + bool onCheckCodecProfile(const RtcCodecPlan &plan, CodecId codec) const; + void onSelectPlan(RtcCodecPlan &plan, CodecId codec) const; private: RtcCodecPlan::Ptr _rtsp_video_plan; @@ -748,5 +750,6 @@ private: ~SdpConst() = delete; }; +}// namespace mediakit #endif //ZLMEDIAKIT_SDP_H diff --git a/webrtc/TwccContext.cpp b/webrtc/TwccContext.cpp index ed565186..bbdba654 100644 --- a/webrtc/TwccContext.cpp +++ b/webrtc/TwccContext.cpp @@ -11,7 +11,7 @@ #include "TwccContext.h" #include "Rtcp/RtcpFCI.h" -using namespace mediakit; +namespace mediakit { enum class ExtSeqStatus : int { normal = 0, @@ -121,3 +121,5 @@ void TwccContext::clearStatus() { void TwccContext::setOnSendTwccCB(TwccContext::onSendTwccCB cb) { _cb = std::move(cb); } + +}// namespace mediakit \ No newline at end of file diff --git a/webrtc/TwccContext.h b/webrtc/TwccContext.h index 26b10fcd..597d6d61 100644 --- a/webrtc/TwccContext.h +++ b/webrtc/TwccContext.h @@ -16,6 +16,8 @@ #include #include "Util/TimeTicker.h" +namespace mediakit { + class TwccContext { public: using onSendTwccCB = std::function; @@ -44,5 +46,5 @@ private: onSendTwccCB _cb; }; - +}// namespace mediakit #endif //ZLMEDIAKIT_TWCCCONTEXT_H diff --git a/webrtc/WebRtcEchoTest.cpp b/webrtc/WebRtcEchoTest.cpp index 1566ce53..f55ea198 100644 --- a/webrtc/WebRtcEchoTest.cpp +++ b/webrtc/WebRtcEchoTest.cpp @@ -10,6 +10,8 @@ #include "WebRtcEchoTest.h" +namespace mediakit { + WebRtcEchoTest::Ptr WebRtcEchoTest::create(const EventPoller::Ptr &poller) { WebRtcEchoTest::Ptr ret(new WebRtcEchoTest(poller), [](WebRtcEchoTest *ptr) { ptr->onDestory(); @@ -48,4 +50,6 @@ void WebRtcEchoTest::onCheckSdp(SdpType type, RtcSession &sdp) { } } } -} \ No newline at end of file +} + +}// namespace mediakit \ No newline at end of file diff --git a/webrtc/WebRtcEchoTest.h b/webrtc/WebRtcEchoTest.h index cdc5d92c..0ab74e7b 100644 --- a/webrtc/WebRtcEchoTest.h +++ b/webrtc/WebRtcEchoTest.h @@ -13,6 +13,8 @@ #include "WebRtcTransport.h" +namespace mediakit { + class WebRtcEchoTest : public WebRtcTransportImp { public: using Ptr = std::shared_ptr; @@ -26,7 +28,7 @@ protected: void onRtp(const char *buf, size_t len, uint64_t stamp_ms) override; void onRtcp(const char *buf, size_t len) override; - void onRecvRtp(MediaTrack &track, const std::string &rid, mediakit::RtpPacket::Ptr rtp) override {}; + void onRecvRtp(MediaTrack &track, const std::string &rid, RtpPacket::Ptr rtp) override {}; void onBeforeEncryptRtp(const char *buf, int &len, void *ctx) override {}; void onBeforeEncryptRtcp(const char *buf, int &len, void *ctx) override {}; @@ -34,4 +36,5 @@ private: WebRtcEchoTest(const EventPoller::Ptr &poller); }; +}// namespace mediakit #endif //ZLMEDIAKIT_WEBRTCECHOTEST_H diff --git a/webrtc/WebRtcPlayer.cpp b/webrtc/WebRtcPlayer.cpp index 74f216d9..c2882319 100644 --- a/webrtc/WebRtcPlayer.cpp +++ b/webrtc/WebRtcPlayer.cpp @@ -11,7 +11,8 @@ #include "WebRtcPlayer.h" using namespace std; -using namespace mediakit; + +namespace mediakit { WebRtcPlayer::Ptr WebRtcPlayer::create(const EventPoller::Ptr &poller, const RtspMediaSource::Ptr &src, @@ -88,3 +89,5 @@ void WebRtcPlayer::onRtcConfigure(RtcConfigure &configure) const { configure.audio.direction = configure.video.direction = RtpDirection::sendonly; configure.setPlayRtspInfo(_play_src->getSdp()); } + +}// namespace mediakit \ No newline at end of file diff --git a/webrtc/WebRtcPlayer.h b/webrtc/WebRtcPlayer.h index e2487c67..c3a31b14 100644 --- a/webrtc/WebRtcPlayer.h +++ b/webrtc/WebRtcPlayer.h @@ -13,30 +13,32 @@ #include "WebRtcTransport.h" +namespace mediakit { + class WebRtcPlayer : public WebRtcTransportImp { public: using Ptr = std::shared_ptr; ~WebRtcPlayer() override = default; - static Ptr create(const EventPoller::Ptr &poller, const mediakit::RtspMediaSource::Ptr &src, const mediakit::MediaInfo &info); + static Ptr create(const EventPoller::Ptr &poller, const RtspMediaSource::Ptr &src, const MediaInfo &info); protected: ///////WebRtcTransportImp override/////// void onStartWebRTC() override; void onDestory() override; void onRtcConfigure(RtcConfigure &configure) const override; - void onRecvRtp(MediaTrack &track, const std::string &rid, mediakit::RtpPacket::Ptr rtp) override {}; + void onRecvRtp(MediaTrack &track, const std::string &rid, RtpPacket::Ptr rtp) override {}; private: - WebRtcPlayer(const EventPoller::Ptr &poller, const mediakit::RtspMediaSource::Ptr &src, const mediakit::MediaInfo &info); + WebRtcPlayer(const EventPoller::Ptr &poller, const RtspMediaSource::Ptr &src, const MediaInfo &info); private: //媒体相关元数据 - mediakit::MediaInfo _media_info; + MediaInfo _media_info; //播放的rtsp源 - mediakit::RtspMediaSource::Ptr _play_src; + RtspMediaSource::Ptr _play_src; //播放rtsp源的reader对象 - mediakit::RtspMediaSource::RingType::RingReader::Ptr _reader; + RtspMediaSource::RingType::RingReader::Ptr _reader; }; - -#endif //ZLMEDIAKIT_WEBRTCPLAYER_H +}// namespace mediakit +#endif // ZLMEDIAKIT_WEBRTCPLAYER_H diff --git a/webrtc/WebRtcPusher.cpp b/webrtc/WebRtcPusher.cpp index ebb7b6d8..33622ca8 100644 --- a/webrtc/WebRtcPusher.cpp +++ b/webrtc/WebRtcPusher.cpp @@ -11,13 +11,14 @@ #include "WebRtcPusher.h" using namespace std; -using namespace mediakit; + +namespace mediakit { WebRtcPusher::Ptr WebRtcPusher::create(const EventPoller::Ptr &poller, const RtspMediaSourceImp::Ptr &src, const std::shared_ptr &ownership, const MediaInfo &info, - const mediakit::ProtocolOption &option) { + const ProtocolOption &option) { WebRtcPusher::Ptr ret(new WebRtcPusher(poller, src, ownership, info, option), [](WebRtcPusher *ptr) { ptr->onDestory(); delete ptr; @@ -30,7 +31,7 @@ WebRtcPusher::WebRtcPusher(const EventPoller::Ptr &poller, const RtspMediaSourceImp::Ptr &src, const std::shared_ptr &ownership, const MediaInfo &info, - const mediakit::ProtocolOption &option) : WebRtcTransportImp(poller) { + const ProtocolOption &option) : WebRtcTransportImp(poller) { _media_info = info; _push_src = src; _push_src_ownership = ownership; @@ -142,7 +143,7 @@ void WebRtcPusher::onRtcConfigure(RtcConfigure &configure) const { configure.audio.direction = configure.video.direction = RtpDirection::recvonly; } -float WebRtcPusher::getLossRate(MediaSource &sender,mediakit::TrackType type){ +float WebRtcPusher::getLossRate(MediaSource &sender,TrackType type){ return WebRtcTransportImp::getLossRate(type); } @@ -155,4 +156,6 @@ void WebRtcPusher::OnDtlsTransportClosed(const RTC::DtlsTransport *dtlsTransport void WebRtcPusher::onRtcpBye(){ _push_src = nullptr; WebRtcTransportImp::onRtcpBye(); -} \ No newline at end of file +} + +}// namespace mediakit \ No newline at end of file diff --git a/webrtc/WebRtcPusher.h b/webrtc/WebRtcPusher.h index 41122bc1..9c71420f 100644 --- a/webrtc/WebRtcPusher.h +++ b/webrtc/WebRtcPusher.h @@ -13,19 +13,21 @@ #include "WebRtcTransport.h" -class WebRtcPusher : public WebRtcTransportImp, public mediakit::MediaSourceEvent { +namespace mediakit { + +class WebRtcPusher : public WebRtcTransportImp, public MediaSourceEvent { public: using Ptr = std::shared_ptr; ~WebRtcPusher() override = default; - static Ptr create(const EventPoller::Ptr &poller, const mediakit::RtspMediaSourceImp::Ptr &src, - const std::shared_ptr &ownership, const mediakit::MediaInfo &info, const mediakit::ProtocolOption &option); + static Ptr create(const EventPoller::Ptr &poller, const RtspMediaSourceImp::Ptr &src, + const std::shared_ptr &ownership, const MediaInfo &info, const ProtocolOption &option); protected: ///////WebRtcTransportImp override/////// void onStartWebRTC() override; void onDestory() override; void onRtcConfigure(RtcConfigure &configure) const override; - void onRecvRtp(MediaTrack &track, const std::string &rid, mediakit::RtpPacket::Ptr rtp) override; + void onRecvRtp(MediaTrack &track, const std::string &rid, RtpPacket::Ptr rtp) override; void onRtcpBye() override; //// dtls相关的回调 //// void OnDtlsTransportClosed(const RTC::DtlsTransport *dtlsTransport) override; @@ -33,35 +35,36 @@ protected: protected: ///////MediaSourceEvent override/////// // 关闭 - bool close(mediakit::MediaSource &sender) override; + bool close(MediaSource &sender) override; // 播放总人数 - int totalReaderCount(mediakit::MediaSource &sender) override; + int totalReaderCount(MediaSource &sender) override; // 获取媒体源类型 - mediakit::MediaOriginType getOriginType(mediakit::MediaSource &sender) const override; + MediaOriginType getOriginType(MediaSource &sender) const override; // 获取媒体源url或者文件路径 - std::string getOriginUrl(mediakit::MediaSource &sender) const override; + std::string getOriginUrl(MediaSource &sender) const override; // 获取媒体源客户端相关信息 - std::shared_ptr getOriginSock(mediakit::MediaSource &sender) const override; + std::shared_ptr getOriginSock(MediaSource &sender) const override; // 获取丢包率 - float getLossRate(mediakit::MediaSource &sender,mediakit::TrackType type) override; + float getLossRate(MediaSource &sender,TrackType type) override; private: - WebRtcPusher(const EventPoller::Ptr &poller, const mediakit::RtspMediaSourceImp::Ptr &src, - const std::shared_ptr &ownership, const mediakit::MediaInfo &info, const mediakit::ProtocolOption &option); + WebRtcPusher(const EventPoller::Ptr &poller, const RtspMediaSourceImp::Ptr &src, + const std::shared_ptr &ownership, const MediaInfo &info, const ProtocolOption &option); private: bool _simulcast = false; //断连续推延时 uint32_t _continue_push_ms = 0; //媒体相关元数据 - mediakit::MediaInfo _media_info; + MediaInfo _media_info; //推流的rtsp源 - mediakit::RtspMediaSourceImp::Ptr _push_src; + RtspMediaSourceImp::Ptr _push_src; //推流所有权 std::shared_ptr _push_src_ownership; //推流的rtsp源,支持simulcast - std::unordered_map _push_src_sim; + std::unordered_map _push_src_sim; std::unordered_map > _push_src_sim_ownership; }; +}// namespace mediakit #endif //ZLMEDIAKIT_WEBRTCPUSHER_H diff --git a/webrtc/WebRtcSession.cpp b/webrtc/WebRtcSession.cpp index a87a835b..f8a37300 100644 --- a/webrtc/WebRtcSession.cpp +++ b/webrtc/WebRtcSession.cpp @@ -12,7 +12,8 @@ #include "Util/util.h" using namespace std; -using namespace mediakit; + +namespace mediakit { static string getUserName(const Buffer::Ptr &buffer) { auto buf = buffer->data(); @@ -85,7 +86,7 @@ void WebRtcSession::onError(const SockException &err) { } void WebRtcSession::onManager() { - GET_CONFIG(float, timeoutSec, RTC::kTimeOutSec); + GET_CONFIG(float, timeoutSec, Rtc::kTimeOutSec); if (!_transport && _ticker.createdTime() > timeoutSec * 1000) { shutdown(SockException(Err_timeout, "illegal webrtc connection")); return; @@ -96,5 +97,6 @@ void WebRtcSession::onManager() { } } +}// namespace mediakit diff --git a/webrtc/WebRtcSession.h b/webrtc/WebRtcSession.h index 266407e4..655ca7fa 100644 --- a/webrtc/WebRtcSession.h +++ b/webrtc/WebRtcSession.h @@ -16,6 +16,8 @@ #include "IceServer.hpp" #include "WebRtcTransport.h" +namespace mediakit { + class WebRtcSession : public UdpSession { public: WebRtcSession(const Socket::Ptr &sock); @@ -35,5 +37,6 @@ private: std::shared_ptr _transport; }; +}// namespace mediakit #endif //ZLMEDIAKIT_WEBRTCSESSION_H diff --git a/webrtc/WebRtcTransport.cpp b/webrtc/WebRtcTransport.cpp index 51d2491f..8cb5092f 100644 --- a/webrtc/WebRtcTransport.cpp +++ b/webrtc/WebRtcTransport.cpp @@ -8,15 +8,18 @@ * may be found in the AUTHORS file in the root of the source tree. */ -#include "WebRtcTransport.h" -#include "Rtcp/Rtcp.h" -#include "Rtcp/RtcpFCI.h" -#include "RtpExt.h" -#include "Rtsp/RtpReceiver.h" - +#include #include -#include +#include "RtpExt.h" +#include "Rtcp/Rtcp.h" +#include "Rtcp/RtcpFCI.h" +#include "Rtsp/RtpReceiver.h" +#include "WebRtcTransport.h" + +#include "WebRtcEchoTest.h" +#include "WebRtcPlayer.h" +#include "WebRtcPusher.h" #define RTP_SSRC_OFFSET 1 #define RTX_SSRC_OFFSET 2 @@ -26,10 +29,11 @@ #define RTP_MSID RTP_MSLABEL " " RTP_LABEL using namespace std; -using namespace mediakit; + +namespace mediakit { // RTC配置项目 -namespace RTC { +namespace Rtc { #define RTC_FIELD "rtc." // rtp和rtcp接受超时时间 const string kTimeOutSec = RTC_FIELD "timeoutSec"; @@ -246,7 +250,7 @@ void WebRtcTransport::setRemoteDtlsFingerprint(const RtcSession &remote) { void WebRtcTransport::onRtcConfigure(RtcConfigure &configure) const { // 开启remb后关闭twcc,因为开启twcc后remb无效 - GET_CONFIG(size_t, remb_bit_rate, RTC::kRembBitRate); + GET_CONFIG(size_t, remb_bit_rate, Rtc::kRembBitRate); configure.enableTWCC(!remb_bit_rate); } @@ -368,7 +372,7 @@ void WebRtcTransportImp::onCreate() { registerSelf(); weak_ptr weak_self = static_pointer_cast(shared_from_this()); - GET_CONFIG(float, timeoutSec, RTC::kTimeOutSec); + GET_CONFIG(float, timeoutSec, Rtc::kTimeOutSec); _timer = std::make_shared( timeoutSec / 2, [weak_self]() { @@ -511,7 +515,7 @@ void WebRtcTransportImp::onStartWebRTC() { void WebRtcTransportImp::onCheckAnswer(RtcSession &sdp) { // 修改answer sdp的ip、端口信息 - GET_CONFIG_FUNC(std::vector, extern_ips, RTC::kExternIP, [](string str) { + GET_CONFIG_FUNC(std::vector, extern_ips, Rtc::kExternIP, [](string str) { std::vector ret; if (str.length()) { ret = split(str, ","); @@ -525,7 +529,7 @@ void WebRtcTransportImp::onCheckAnswer(RtcSession &sdp) { m.rtcp_addr.reset(); m.rtcp_addr.address = m.addr.address; - GET_CONFIG(uint16_t, local_port, RTC::kPort); + GET_CONFIG(uint16_t, local_port, Rtc::kPort); m.rtcp_addr.port = local_port; m.port = m.rtcp_addr.port; sdp.origin.address = m.addr.address; @@ -592,9 +596,9 @@ makeIceCandidate(std::string ip, uint16_t port, uint32_t priority = 100, std::st void WebRtcTransportImp::onRtcConfigure(RtcConfigure &configure) const { WebRtcTransport::onRtcConfigure(configure); - GET_CONFIG(uint16_t, local_port, RTC::kPort); + GET_CONFIG(uint16_t, local_port, Rtc::kPort); // 添加接收端口candidate信息 - GET_CONFIG_FUNC(std::vector, extern_ips, RTC::kExternIP, [](string str) { + GET_CONFIG_FUNC(std::vector, extern_ips, Rtc::kExternIP, [](string str) { std::vector ret; if (str.length()) { ret = split(str, ","); @@ -698,7 +702,7 @@ std::shared_ptr MediaTrack::getRtpChannel(uint32_t ssrc) const { return it_chn->second; } -float WebRtcTransportImp::getLossRate(mediakit::TrackType type) { +float WebRtcTransportImp::getLossRate(TrackType type) { for (auto &pr : _ssrc_to_track) { auto ssrc = pr.first; auto &track = pr.second; @@ -942,7 +946,7 @@ void WebRtcTransportImp::onSortedRtp(MediaTrack &track, const string &rid, RtpPa sendRtcpPli(rtp->getSSRC()); // 开启remb,则发送remb包调节比特率 - GET_CONFIG(size_t, remb_bit_rate, RTC::kRembBitRate); + GET_CONFIG(size_t, remb_bit_rate, Rtc::kRembBitRate); if (remb_bit_rate && _answer_sdp->supportRtcpFb(SdpConst::kRembRtcpFb)) { sendRtcpRemb(rtp->getSSRC(), remb_bit_rate); } @@ -1121,10 +1125,6 @@ void WebRtcPluginManager::getAnswerSdp( it->second(sender, offer, args, cb); } -#include "WebRtcEchoTest.h" -#include "WebRtcPlayer.h" -#include "WebRtcPusher.h" - void echo_plugin( Session &sender, const string &offer, const WebRtcArgs &args, const WebRtcPluginManager::onCreateRtc &cb) { cb(*WebRtcEchoTest::create(EventPollerPool::Instance().getPoller())); @@ -1227,3 +1227,5 @@ static onceToken s_rtc_auto_register([]() { WebRtcPluginManager::Instance().registerPlugin("push", push_plugin); WebRtcPluginManager::Instance().registerPlugin("play", play_plugin); }); + +}// namespace mediakit \ No newline at end of file diff --git a/webrtc/WebRtcTransport.h b/webrtc/WebRtcTransport.h index ece9a2b2..eb03b271 100644 --- a/webrtc/WebRtcTransport.h +++ b/webrtc/WebRtcTransport.h @@ -27,8 +27,10 @@ #include "TwccContext.h" #include "SctpAssociation.hpp" +namespace mediakit { + //RTC配置项目 -namespace RTC { +namespace Rtc { extern const std::string kPort; extern const std::string kTimeOutSec; }//namespace RTC @@ -200,7 +202,7 @@ public: //for send rtp NackList nack_list; - mediakit::RtcpContext::Ptr rtcp_context_send; + RtcpContext::Ptr rtcp_context_send; //for recv rtp std::unordered_map > rtp_channel; @@ -211,13 +213,13 @@ struct WrappedMediaTrack { MediaTrack::Ptr track; explicit WrappedMediaTrack(MediaTrack::Ptr ptr): track(ptr) {} virtual ~WrappedMediaTrack() {} - virtual void inputRtp(const char *buf, size_t len, uint64_t stamp_ms, mediakit::RtpHeader *rtp) = 0; + virtual void inputRtp(const char *buf, size_t len, uint64_t stamp_ms, RtpHeader *rtp) = 0; }; struct WrappedRtxTrack: public WrappedMediaTrack { explicit WrappedRtxTrack(MediaTrack::Ptr ptr) : WrappedMediaTrack(std::move(ptr)) {} - void inputRtp(const char *buf, size_t len, uint64_t stamp_ms, mediakit::RtpHeader *rtp) override; + void inputRtp(const char *buf, size_t len, uint64_t stamp_ms, RtpHeader *rtp) override; }; class WebRtcTransportImp; @@ -229,7 +231,7 @@ struct WrappedRtpTrack : public WrappedMediaTrack { , _transport(t) {} TwccContext& _twcc_ctx; WebRtcTransportImp& _transport; - void inputRtp(const char *buf, size_t len, uint64_t stamp_ms, mediakit::RtpHeader *rtp) override; + void inputRtp(const char *buf, size_t len, uint64_t stamp_ms, RtpHeader *rtp) override; }; class WebRtcTransportImp : public WebRtcTransport { @@ -243,7 +245,7 @@ public: uint64_t getDuration() const; bool canSendRtp() const; bool canRecvRtp() const; - void onSendRtp(const mediakit::RtpPacket::Ptr &rtp, bool flush, bool rtx = false); + void onSendRtp(const RtpPacket::Ptr &rtp, bool flush, bool rtx = false); void createRtpChannel(const std::string &rid, uint32_t ssrc, MediaTrack &track); @@ -262,14 +264,14 @@ protected: void onCreate() override; void onDestory() override; void onShutdown(const SockException &ex) override; - virtual void onRecvRtp(MediaTrack &track, const std::string &rid, mediakit::RtpPacket::Ptr rtp) = 0; + virtual void onRecvRtp(MediaTrack &track, const std::string &rid, RtpPacket::Ptr rtp) = 0; void updateTicker(); - float getLossRate(mediakit::TrackType type); + float getLossRate(TrackType type); void onRtcpBye() override; private: - void onSortedRtp(MediaTrack &track, const std::string &rid, mediakit::RtpPacket::Ptr rtp); - void onSendNack(MediaTrack &track, const mediakit::FCI_NACK &nack, uint32_t ssrc); + void onSortedRtp(MediaTrack &track, const std::string &rid, RtpPacket::Ptr rtp); + void onSendNack(MediaTrack &track, const FCI_NACK &nack, uint32_t ssrc); void onSendTwcc(uint32_t ssrc, const std::string &twcc_fci); void registerSelf(); @@ -343,4 +345,6 @@ private: private: mutable std::mutex _mtx_creator; std::unordered_map _map_creator; -}; \ No newline at end of file +}; + +}// namespace mediakit \ No newline at end of file diff --git a/webrtc/readme.md b/webrtc/readme.md index b94b2de4..3ac82dee 100644 --- a/webrtc/readme.md +++ b/webrtc/readme.md @@ -15,7 +15,10 @@ - srtp相关功能: - SrtpSession.cpp - SrtpSession.hpp - + +- datachannel相关功能: + - SctpAssociation.cpp + - SctpAssociation.hpp 以上源码有一定的修改和裁剪,感谢MediaSoup开源项目及作者, 用户在使用本项目的同时,应该同时遵循MediaSoup的开源协议。