Skip to content

Commit

Permalink
RTC: Support disable the NACK no-copy, enable copy by default
Browse files Browse the repository at this point in the history
  • Loading branch information
winlinvip committed Feb 28, 2021
1 parent 44aa976 commit c20d8fb
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 8 deletions.
3 changes: 3 additions & 0 deletions trunk/conf/full.conf
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,9 @@ vhost rtc.vhost.srs.com {
# Whether support NACK.
# default: on
enabled on;
# Whether directly use the packet, avoid copy.
# default: off
no_copy off;
}
# For TWCC.
twcc {
Expand Down
22 changes: 22 additions & 0 deletions trunk/src/app/srs_app_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5249,6 +5249,28 @@ bool SrsConfig::get_rtc_nack_enabled(string vhost)
return SRS_CONF_PERFER_TRUE(conf->arg0());
}

bool SrsConfig::get_rtc_nack_no_copy(string vhost)
{
static bool DEFAULT = false;

SrsConfDirective* conf = get_vhost(vhost);
if (!conf) {
return DEFAULT;
}

conf = conf->get("nack");
if (!conf) {
return DEFAULT;
}

conf = conf->get("no_copy");
if (!conf || conf->arg0().empty()) {
return DEFAULT;
}

return SRS_CONF_PERFER_FALSE(conf->arg0());
}

bool SrsConfig::get_rtc_twcc_enabled(string vhost)
{
static bool DEFAULT = true;
Expand Down
1 change: 1 addition & 0 deletions trunk/src/app/srs_app_config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,7 @@ class SrsConfig
std::string get_rtc_dtls_version(std::string vhost);
int get_rtc_drop_for_pt(std::string vhost);
bool get_rtc_nack_enabled(std::string vhost);
bool get_rtc_nack_no_copy(std::string vhost);
bool get_rtc_twcc_enabled(std::string vhost);

// vhost specified section
Expand Down
30 changes: 28 additions & 2 deletions trunk/src/app/srs_app_rtc_conn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,7 @@ SrsRtcPlayStream::SrsRtcPlayStream(SrsRtcConnection* s, const SrsContextId& cid)
realtime = true;

nack_enabled_ = false;
nack_no_copy_ = false;

_srs_config->subscribe(this);
timer_ = new SrsHourGlass("play", this, 1000 * SRS_UTIME_MILLISECONDS);
Expand Down Expand Up @@ -446,7 +447,19 @@ srs_error_t SrsRtcPlayStream::initialize(SrsRequest* req, std::map<uint32_t, Srs

// TODO: FIXME: Support reload.
nack_enabled_ = _srs_config->get_rtc_nack_enabled(req->vhost);
srs_trace("RTC player nack=%d", nack_enabled_);
nack_no_copy_ = _srs_config->get_rtc_nack_no_copy(req->vhost);
srs_trace("RTC player nack=%d, nnc=%d", nack_enabled_, nack_no_copy_);

// Setup tracks.
for (map<uint32_t, SrsRtcAudioSendTrack*>::iterator it = audio_tracks_.begin(); it != audio_tracks_.end(); ++it) {
SrsRtcAudioSendTrack* track = it->second;
track->set_nack_no_copy(nack_no_copy_);
}

for (map<uint32_t, SrsRtcVideoSendTrack*>::iterator it = video_tracks_.begin(); it != video_tracks_.end(); ++it) {
SrsRtcVideoSendTrack* track = it->second;
track->set_nack_no_copy(nack_no_copy_);
}

// Update stat for session.
session_->stat_->nn_subscribers++;
Expand Down Expand Up @@ -849,6 +862,7 @@ SrsRtcPublishStream::SrsRtcPublishStream(SrsRtcConnection* session, const SrsCon
source = NULL;
nn_simulate_nack_drop = 0;
nack_enabled_ = false;
nack_no_copy_ = false;
pt_to_drop_ = 0;

nn_audio_frames = 0;
Expand Down Expand Up @@ -923,10 +937,22 @@ srs_error_t SrsRtcPublishStream::initialize(SrsRequest* r, SrsRtcStreamDescripti
}

nack_enabled_ = _srs_config->get_rtc_nack_enabled(req->vhost);
nack_no_copy_ = _srs_config->get_rtc_nack_no_copy(req->vhost);
pt_to_drop_ = (uint16_t)_srs_config->get_rtc_drop_for_pt(req->vhost);
twcc_enabled_ = _srs_config->get_rtc_twcc_enabled(req->vhost);

srs_trace("RTC publisher nack=%d, pt-drop=%u, twcc=%u/%d", nack_enabled_, pt_to_drop_, twcc_enabled_, twcc_id);
srs_trace("RTC publisher nack=%d, nnc=%d, pt-drop=%u, twcc=%u/%d", nack_enabled_, nack_no_copy_, pt_to_drop_, twcc_enabled_, twcc_id);

// Setup tracks.
for (int i = 0; i < (int)audio_tracks_.size(); i++) {
SrsRtcAudioRecvTrack* track = audio_tracks_.at(i);
track->set_nack_no_copy(nack_no_copy_);
}

for (int i = 0; i < (int)video_tracks_.size(); i++) {
SrsRtcVideoRecvTrack* track = video_tracks_.at(i);
track->set_nack_no_copy(nack_no_copy_);
}

// Update stat for session.
session_->stat_->nn_publishers++;
Expand Down
2 changes: 2 additions & 0 deletions trunk/src/app/srs_app_rtc_conn.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ class SrsRtcPlayStream : virtual public ISrsCoroutineHandler, virtual public ISr
bool realtime;
// Whether enabled nack.
bool nack_enabled_;
bool nack_no_copy_;
private:
// Whether palyer started.
bool is_started;
Expand Down Expand Up @@ -287,6 +288,7 @@ class SrsRtcPublishStream : virtual public ISrsHourGlass, virtual public ISrsRtp
uint16_t pt_to_drop_;
// Whether enabled nack.
bool nack_enabled_;
bool nack_no_copy_;
bool twcc_enabled_;
private:
bool request_keyframe_;
Expand Down
18 changes: 14 additions & 4 deletions trunk/src/app/srs_app_rtc_source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1729,6 +1729,7 @@ SrsRtcRecvTrack::SrsRtcRecvTrack(SrsRtcConnection* session, SrsRtcTrackDescripti
session_ = session;
track_desc_ = track_desc->copy();
statistic_ = new SrsRtcTrackStatistic();
nack_no_copy_ = false;

if (is_audio) {
rtp_queue_ = new SrsRtpRingBuffer(100);
Expand Down Expand Up @@ -1838,8 +1839,12 @@ srs_error_t SrsRtcRecvTrack::on_nack(SrsRtpPacket2** ppkt)

// insert into video_queue and audio_queue
// We directly use the pkt, never copy it, so we should set the pkt to NULL.
rtp_queue_->set(seq, pkt);
*ppkt = NULL;
if (nack_no_copy_) {
rtp_queue_->set(seq, pkt);
*ppkt = NULL;
} else {
rtp_queue_->set(seq, pkt->copy());
}

return err;
}
Expand Down Expand Up @@ -1983,6 +1988,7 @@ SrsRtcSendTrack::SrsRtcSendTrack(SrsRtcConnection* session, SrsRtcTrackDescripti
session_ = session;
track_desc_ = track_desc->copy();
statistic_ = new SrsRtcTrackStatistic();
nack_no_copy_ = false;

if (is_audio) {
rtp_queue_ = new SrsRtpRingBuffer(100);
Expand Down Expand Up @@ -2058,8 +2064,12 @@ srs_error_t SrsRtcSendTrack::on_nack(SrsRtpPacket2** ppkt)

// insert into video_queue and audio_queue
// We directly use the pkt, never copy it, so we should set the pkt to NULL.
rtp_queue_->set(seq, pkt);
*ppkt = NULL;
if (nack_no_copy_) {
rtp_queue_->set(seq, pkt);
*ppkt = NULL;
} else {
rtp_queue_->set(seq, pkt->copy());
}

return err;
}
Expand Down
13 changes: 11 additions & 2 deletions trunk/src/app/srs_app_rtc_source.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -512,18 +512,23 @@ class SrsRtcRecvTrack
protected:
SrsRtcTrackDescription* track_desc_;
SrsRtcTrackStatistic* statistic_;

protected:
SrsRtcConnection* session_;
SrsRtpRingBuffer* rtp_queue_;
SrsRtpNackForReceiver* nack_receiver_;

private:
// By config, whether no copy.
bool nack_no_copy_;
protected:
// send report ntp and received time.
SrsNtp last_sender_report_ntp;
uint64_t last_sender_report_sys_time;
public:
SrsRtcRecvTrack(SrsRtcConnection* session, SrsRtcTrackDescription* stream_descs, bool is_audio);
virtual ~SrsRtcRecvTrack();
public:
// SrsRtcSendTrack::set_nack_no_copy
void set_nack_no_copy(bool v) { nack_no_copy_ = v; }
bool has_ssrc(uint32_t ssrc);
uint32_t get_ssrc();
void update_rtt(int rtt);
Expand Down Expand Up @@ -580,12 +585,16 @@ class SrsRtcSendTrack
// NACK ARQ ring buffer.
SrsRtpRingBuffer* rtp_queue_;
private:
// By config, whether no copy.
bool nack_no_copy_;
// The pithy print for special stage.
SrsErrorPithyPrint* nack_epp;
public:
SrsRtcSendTrack(SrsRtcConnection* session, SrsRtcTrackDescription* track_desc, bool is_audio);
virtual ~SrsRtcSendTrack();
public:
// SrsRtcSendTrack::set_nack_no_copy
void set_nack_no_copy(bool v) { nack_no_copy_ = v; }
bool has_ssrc(uint32_t ssrc);
SrsRtpPacket2* fetch_rtp_packet(uint16_t seq);
bool set_track_status(bool active);
Expand Down

0 comments on commit c20d8fb

Please sign in to comment.