From 579f7a8a5b6611652c1db9a9dd27c1275bc31ea6 Mon Sep 17 00:00:00 2001 From: winlin Date: Thu, 3 Sep 2020 16:00:12 +0800 Subject: [PATCH] RTC: Refine cid for PLI --- trunk/src/app/srs_app_rtc_conn.cpp | 42 ++++++++++++++++------------ trunk/src/app/srs_app_rtc_conn.hpp | 20 ++++++------- trunk/src/app/srs_app_rtc_source.cpp | 9 +++--- trunk/src/app/srs_app_rtc_source.hpp | 2 ++ 4 files changed, 41 insertions(+), 32 deletions(-) diff --git a/trunk/src/app/srs_app_rtc_conn.cpp b/trunk/src/app/srs_app_rtc_conn.cpp index 3829e1287..d1192b7b3 100644 --- a/trunk/src/app/srs_app_rtc_conn.cpp +++ b/trunk/src/app/srs_app_rtc_conn.cpp @@ -295,9 +295,9 @@ SrsRtcPlayStreamStatistic::~SrsRtcPlayStreamStatistic() { } -SrsRtcPlayStream::SrsRtcPlayStream(SrsRtcConnection* s, SrsContextId parent_cid) +SrsRtcPlayStream::SrsRtcPlayStream(SrsRtcConnection* s, const SrsContextId& cid) { - _parent_cid = parent_cid; + cid_ = cid; trd = new SrsDummyCoroutine(); req_ = NULL; @@ -394,9 +394,9 @@ srs_error_t SrsRtcPlayStream::on_reload_vhost_realtime(string vhost) return on_reload_vhost_play(vhost); } -SrsContextId SrsRtcPlayStream::cid() +const SrsContextId& SrsRtcPlayStream::context_id() { - return trd->cid(); + return cid_; } srs_error_t SrsRtcPlayStream::start() @@ -411,7 +411,7 @@ srs_error_t SrsRtcPlayStream::start() } srs_freep(trd); - trd = new SrsSTCoroutine("rtc_sender", this, _parent_cid); + trd = new SrsSTCoroutine("rtc_sender", this, cid_); if ((err = trd->start()) != srs_success) { return srs_error_wrap(err, "rtc_sender"); @@ -804,11 +804,11 @@ uint32_t SrsRtcPlayStream::get_video_publish_ssrc(uint32_t play_ssrc) return 0; } -SrsRtcPublishStream::SrsRtcPublishStream(SrsRtcConnection* session, SrsContextId parent_cid) +SrsRtcPublishStream::SrsRtcPublishStream(SrsRtcConnection* session, const SrsContextId& cid) { timer_ = new SrsHourGlass(this, 200 * SRS_UTIME_MILLISECONDS); - parent_cid_ = parent_cid; + cid_ = cid; is_started = false; session_ = session; request_keyframe_ = false; @@ -948,6 +948,11 @@ void SrsRtcPublishStream::set_all_tracks_status(bool status) srs_trace("RTC: Init tracks %s ok", merged_log.str().c_str()); } +const SrsContextId& SrsRtcPublishStream::context_id() +{ + return cid_; +} + srs_error_t SrsRtcPublishStream::send_rtcp_rr() { srs_error_t err = srs_success; @@ -1274,10 +1279,10 @@ void SrsRtcPublishStream::request_keyframe(uint32_t ssrc) { uint32_t nn = 0; if (pli_epp->can_print(ssrc, &nn)) { - SrsContextId scid = _srs_context->get_id(); - SrsContextId pcid = session_->context_id(); - srs_trace("RTC: Request PLI ssrc=%u, play=[%d][%s], publish=[%d][%s], count=%u/%u", ssrc, ::getpid(), scid.c_str(), - ::getpid(), pcid.c_str(), nn, pli_epp->nn_count); + // The player(subscriber) cid, which requires PLI. + const SrsContextId& sub_cid = _srs_context->get_id(); + srs_trace("RTC: Need PLI ssrc=%u, play=[%s], publish=[%s], count=%u/%u", ssrc, sub_cid.c_str(), + cid_.c_str(), nn, pli_epp->nn_count); } SrsRtcVideoRecvTrack* video_track = get_video_track(ssrc); @@ -1440,10 +1445,10 @@ ISrsRtcConnectionHijacker::~ISrsRtcConnectionHijacker() { } -SrsRtcConnection::SrsRtcConnection(SrsRtcServer* s, SrsContextId context_id) +SrsRtcConnection::SrsRtcConnection(SrsRtcServer* s, const SrsContextId& cid) { req = NULL; - cid = context_id; + cid_ = cid; stat_ = new SrsRtcConnectionStatistic(); timer_ = new SrsHourGlass(this, 1000 * SRS_UTIME_MILLISECONDS); hijacker_ = NULL; @@ -1548,12 +1553,12 @@ vector SrsRtcConnection::peer_addresses() void SrsRtcConnection::switch_to_context() { - _srs_context->set_id(cid); + _srs_context->set_id(cid_); } -SrsContextId SrsRtcConnection::context_id() +const SrsContextId& SrsRtcConnection::context_id() { - return cid; + return cid_; } srs_error_t SrsRtcConnection::add_publisher(SrsRequest* req, const SrsSdp& remote_sdp, SrsSdp& local_sdp) @@ -2202,7 +2207,7 @@ srs_error_t SrsRtcConnection::send_rtcp_xr_rrtr(uint32_t ssrc) return sendonly_skt->sendto(protected_buf, nb_protected_buf, 0); } -srs_error_t SrsRtcConnection::send_rtcp_fb_pli(uint32_t ssrc) +srs_error_t SrsRtcConnection::send_rtcp_fb_pli(uint32_t ssrc, const SrsContextId& cid_of_subscriber) { srs_error_t err = srs_success; @@ -2216,7 +2221,8 @@ srs_error_t SrsRtcConnection::send_rtcp_fb_pli(uint32_t ssrc) uint32_t nn = 0; if (pli_epp->can_print(ssrc, &nn)) { - srs_trace("RTC: Request PLI ssrc=%u, count=%u/%u, bytes=%uB", ssrc, nn, pli_epp->nn_count, stream.pos()); + srs_trace("RTC: Request PLI ssrc=%u, play=[%s], count=%u/%u, bytes=%uB", ssrc, cid_of_subscriber.c_str(), + nn, pli_epp->nn_count, stream.pos()); } if (_srs_blackhole->blackhole) { diff --git a/trunk/src/app/srs_app_rtc_conn.hpp b/trunk/src/app/srs_app_rtc_conn.hpp index 6940f651a..f616fd752 100644 --- a/trunk/src/app/srs_app_rtc_conn.hpp +++ b/trunk/src/app/srs_app_rtc_conn.hpp @@ -211,7 +211,7 @@ public: class SrsRtcPlayStream : virtual public ISrsCoroutineHandler, virtual public ISrsReloadHandler, virtual public ISrsHourGlass { private: - SrsContextId _parent_cid; + SrsContextId cid_; SrsCoroutine* trd; SrsRtcConnection* session_; private: @@ -235,7 +235,7 @@ private: // The statistic for consumer to send packets to player. SrsRtcPlayStreamStatistic info; public: - SrsRtcPlayStream(SrsRtcConnection* s, SrsContextId parent_cid); + SrsRtcPlayStream(SrsRtcConnection* s, const SrsContextId& cid); virtual ~SrsRtcPlayStream(); public: srs_error_t initialize(SrsRequest* request, std::map sub_relations); @@ -243,8 +243,7 @@ public: public: virtual srs_error_t on_reload_vhost_play(std::string vhost); virtual srs_error_t on_reload_vhost_realtime(std::string vhost); -public: - virtual SrsContextId cid(); + virtual const SrsContextId& context_id(); public: virtual srs_error_t start(); virtual void stop(); @@ -273,7 +272,7 @@ private: class SrsRtcPublishStream : virtual public ISrsHourGlass, virtual public ISrsRtpPacketDecodeHandler, virtual public ISrsRtcPublishStream { private: - SrsContextId parent_cid_; + SrsContextId cid_; SrsHourGlass* timer_; uint64_t nn_audio_frames; private: @@ -300,13 +299,14 @@ private: SrsRtpExtensionTypes extension_types_; bool is_started; public: - SrsRtcPublishStream(SrsRtcConnection* session, SrsContextId parent_cid); + SrsRtcPublishStream(SrsRtcConnection* session, const SrsContextId& cid); virtual ~SrsRtcPublishStream(); public: srs_error_t initialize(SrsRequest* req, SrsRtcStreamDescription* stream_desc); srs_error_t start(); // Directly set the status of track, generally for init to set the default value. void set_all_tracks_status(bool status); + virtual const SrsContextId& context_id(); private: srs_error_t send_rtcp_rr(); srs_error_t send_rtcp_xr_rrtr(); @@ -408,7 +408,7 @@ private: srs_utime_t last_stun_time; private: // For each RTC session, we use a specified cid for debugging logs. - SrsContextId cid; + SrsContextId cid_; // TODO: FIXME: Rename to req_. SrsRequest* req; SrsSdp remote_sdp; @@ -423,7 +423,7 @@ private: // Pithy print for PLI request. SrsErrorPithyPrint* pli_epp; public: - SrsRtcConnection(SrsRtcServer* s, SrsContextId context_id); + SrsRtcConnection(SrsRtcServer* s, const SrsContextId& cid); virtual ~SrsRtcConnection(); public: // TODO: FIXME: save only connection info. @@ -440,7 +440,7 @@ public: std::vector peer_addresses(); public: void switch_to_context(); - SrsContextId context_id(); + const SrsContextId& context_id(); public: srs_error_t add_publisher(SrsRequest* request, const SrsSdp& remote_sdp, SrsSdp& local_sdp); srs_error_t add_player(SrsRequest* request, const SrsSdp& remote_sdp, SrsSdp& local_sdp); @@ -477,7 +477,7 @@ public: void check_send_nacks(SrsRtpNackForReceiver* nack, uint32_t ssrc, uint32_t& sent_nacks); srs_error_t send_rtcp_rr(uint32_t ssrc, SrsRtpRingBuffer* rtp_queue, const uint64_t& last_send_systime, const SrsNtp& last_send_ntp); srs_error_t send_rtcp_xr_rrtr(uint32_t ssrc); - srs_error_t send_rtcp_fb_pli(uint32_t ssrc); + srs_error_t send_rtcp_fb_pli(uint32_t ssrc, const SrsContextId& cid_of_subscriber); public: // Simulate the NACK to drop nn packets. void simulate_nack_drop(int nn); diff --git a/trunk/src/app/srs_app_rtc_source.cpp b/trunk/src/app/srs_app_rtc_source.cpp index 9bbdbc2c7..8f4f319dc 100644 --- a/trunk/src/app/srs_app_rtc_source.cpp +++ b/trunk/src/app/srs_app_rtc_source.cpp @@ -1700,10 +1700,10 @@ srs_error_t SrsRtcRecvTrack::on_nack(SrsRtpPacket2* pkt) // insert check nack list uint16_t nack_first = 0, nack_last = 0; if (!rtp_queue_->update(seq, nack_first, nack_last)) { - srs_warn("too old seq %u, range [%u, %u]", seq, rtp_queue_->begin, rtp_queue_->end); + srs_warn("NACK: too old seq %u, range [%u, %u]", seq, rtp_queue_->begin, rtp_queue_->end); } if (srs_rtp_seq_distance(nack_first, nack_last) > 0) { - srs_trace("update seq=%u, nack range [%u, %u]", seq, nack_first, nack_last); + srs_trace("NACK: update seq=%u, nack range [%u, %u]", seq, nack_first, nack_last); nack_receiver_->insert(nack_first, nack_last); nack_receiver_->check_queue_size(); } @@ -1783,7 +1783,7 @@ srs_error_t SrsRtcVideoRecvTrack::on_rtp(SrsRtcStream* source, SrsRtpPacket2* pk // TODO: FIXME: add coroutine to request key frame. request_key_frame_ = false; - if ((err = session_->send_rtcp_fb_pli(track_desc_->ssrc_)) != srs_success) { + if ((err = session_->send_rtcp_fb_pli(track_desc_->ssrc_, cid_of_subscriber_)) != srs_success) { srs_warn("PLI err %s", srs_error_desc(err).c_str()); srs_freep(err); } @@ -1799,6 +1799,7 @@ srs_error_t SrsRtcVideoRecvTrack::on_rtp(SrsRtcStream* source, SrsRtpPacket2* pk void SrsRtcVideoRecvTrack::request_keyframe() { + cid_of_subscriber_ = _srs_context->get_id(); request_key_frame_ = true; } @@ -1848,7 +1849,7 @@ SrsRtpPacket2* SrsRtcSendTrack::fetch_rtp_packet(uint16_t seq) // Ignore if sequence not match. uint32_t nn = 0; if (nack_epp->can_print(pkt->header.get_ssrc(), &nn)) { - srs_trace("RTC NACK miss seq=%u, require_seq=%u, ssrc=%u, ts=%u, count=%u/%u, %d bytes", seq, pkt->header.get_sequence(), + srs_trace("RTC: NACK miss seq=%u, require_seq=%u, ssrc=%u, ts=%u, count=%u/%u, %d bytes", seq, pkt->header.get_sequence(), pkt->header.get_ssrc(), pkt->header.get_timestamp(), nn, nack_epp->nn_count, pkt->nb_bytes()); } return NULL; diff --git a/trunk/src/app/srs_app_rtc_source.hpp b/trunk/src/app/srs_app_rtc_source.hpp index cd292b767..491501ec0 100644 --- a/trunk/src/app/srs_app_rtc_source.hpp +++ b/trunk/src/app/srs_app_rtc_source.hpp @@ -516,6 +516,8 @@ class SrsRtcVideoRecvTrack : public SrsRtcRecvTrack { private: bool request_key_frame_; + // The player(subscriber) cid, which requires PLI. + SrsContextId cid_of_subscriber_; public: SrsRtcVideoRecvTrack(SrsRtcConnection* session, SrsRtcTrackDescription* stream_descs); virtual ~SrsRtcVideoRecvTrack();