Skip to content

Commit

Permalink
update for pr review
Browse files Browse the repository at this point in the history
  • Loading branch information
runner365 committed Aug 8, 2023
1 parent 07345e8 commit db122a7
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 52 deletions.
35 changes: 12 additions & 23 deletions trunk/src/app/srs_app_rtc_conn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -450,9 +450,7 @@ SrsRtcPlayStream::~SrsRtcPlayStream()

_srs_config->unsubscribe(this);

if (timer_rtcp_) {
srs_freep(timer_rtcp_);
}
srs_freep(timer_rtcp_);
srs_freep(nack_epp);
srs_freep(pli_worker_);
srs_freep(trd_);
Expand Down Expand Up @@ -686,20 +684,17 @@ srs_error_t SrsRtcPlayStream::cycle()
}
}

srs_error_t SrsRtcPlayStream::send_rtcp_sr(int64_t now_ms) {
srs_error_t SrsRtcPlayStream::send_rtcp_sr(srs_utime_t now_ms)
{
srs_error_t err = srs_success;
for(std::map<uint32_t, SrsRtcVideoSendTrack*>::iterator iter = video_tracks_.begin();
iter != video_tracks_.end();
iter++) {
for(std::map<uint32_t, SrsRtcVideoSendTrack*>::iterator iter = video_tracks_.begin(); iter != video_tracks_.end(); iter++) {
SrsRtcVideoSendTrack* track = iter->second;
if ((err = track->send_rtcp_sr(now_ms)) != srs_success) {
return srs_error_wrap(err, "video send rtcp sr error track=%s", track->get_track_id().c_str());
}
}

for(std::map<uint32_t, SrsRtcAudioSendTrack*>::iterator iter = audio_tracks_.begin();
iter != audio_tracks_.end();
iter++) {
for(std::map<uint32_t, SrsRtcAudioSendTrack*>::iterator iter = audio_tracks_.begin(); iter != audio_tracks_.end(); iter++) {
SrsRtcAudioSendTrack* track = iter->second;
if ((err = track->send_rtcp_sr(now_ms)) != srs_success) {
return srs_error_wrap(err, "audiosend rtcp sr error track=%s", track->get_track_id().c_str());
Expand Down Expand Up @@ -807,7 +802,7 @@ void SrsRtcPlayStream::set_all_tracks_status(bool status)
srs_error_t SrsRtcPlayStream::on_rtcp(SrsRtcpCommon* rtcp)
{
if(SrsRtcpType_rr == rtcp->type()) {
int64_t now_ms = srs_update_system_time()/1000;
srs_utime_t now_ms = srs_update_system_time()/1000;
SrsRtcpRR* rr = dynamic_cast<SrsRtcpRR*>(rtcp);
return on_rtcp_rr(rr, now_ms);
} else if(SrsRtcpType_rtpfb == rtcp->type()) {
Expand All @@ -828,27 +823,21 @@ srs_error_t SrsRtcPlayStream::on_rtcp(SrsRtcpCommon* rtcp)
}
}

srs_error_t SrsRtcPlayStream::on_rtcp_rr(SrsRtcpRR* rtcp, int64_t now_ms)
srs_error_t SrsRtcPlayStream::on_rtcp_rr(SrsRtcpRR* rtcp, srs_utime_t now_ms)
{
srs_error_t err = srs_success;

for(std::vector<SrsRtcpRB>::iterator iter = rtcp->rr_blocks_.begin();
iter != rtcp->rr_blocks_.end();
iter++) {

for(std::vector<SrsRtcpRB>::iterator iter = rtcp->rr_blocks_.begin(); iter != rtcp->rr_blocks_.end(); iter++) {
SrsRtcpRB& rb = *iter;
uint32_t ssrc = rb.ssrc;

for(std::map<uint32_t, SrsRtcAudioSendTrack*>::iterator audio_iter = audio_tracks_.begin();
audio_iter != audio_tracks_.end();
audio_iter++) {
for(std::map<uint32_t, SrsRtcAudioSendTrack*>::iterator audio_iter = audio_tracks_.begin(); audio_iter != audio_tracks_.end(); audio_iter++) {
if(ssrc == audio_iter->second->track_desc_->ssrc_) {
return audio_iter->second->handle_rtcp_rr(rb, now_ms);
}
}

for(std::map<uint32_t, SrsRtcVideoSendTrack*>::iterator video_iter = video_tracks_.begin();
video_iter != video_tracks_.end();
video_iter++) {
for(std::map<uint32_t, SrsRtcVideoSendTrack*>::iterator video_iter = video_tracks_.begin(); video_iter != video_tracks_.end(); video_iter++) {
if(ssrc == video_iter->second->track_desc_->ssrc_) {
return video_iter->second->handle_rtcp_rr(rb, now_ms);
}
Expand Down Expand Up @@ -996,7 +985,7 @@ srs_error_t SrsRtcPlayRtcpTimer::on_timer(srs_utime_t interval)
return err;
}

int64_t now_ms = srs_update_system_time();
srs_utime_t now_ms = srs_update_system_time();
if ((err = p_->send_rtcp_sr(now_ms)) != srs_success) {
srs_warn("RR err %s", srs_error_desc(err).c_str());
srs_freep(err);
Expand Down
2 changes: 1 addition & 1 deletion trunk/src/app/srs_app_rtc_conn.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ friend class SrsRtcPlayRtcpTimer;
public:
virtual srs_error_t cycle();
public:
srs_error_t send_rtcp_sr(int64_t now_ms);
srs_error_t send_rtcp_sr(srs_utime_t now_ms);
private:
srs_error_t send_packet(SrsRtpPacket*& pkt);
public:
Expand Down
38 changes: 21 additions & 17 deletions trunk/src/app/srs_app_rtc_source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2645,6 +2645,14 @@ SrsRtcSendTrack::SrsRtcSendTrack(SrsRtcConnection* session, SrsRtcTrackDescripti
lost_rate_ = 0.0;
rtt_ = 0.0;
avg_rtt_ = 10.0;

send_bytes_ = 0;
send_count_ = 0;
last_rtp_pkt_ts_ = 0;
last_rtp_ms_ = 0;
last_sr_ = 0;
last_ms_ = 0;

}

SrsRtcSendTrack::~SrsRtcSendTrack()
Expand All @@ -2661,7 +2669,7 @@ bool SrsRtcSendTrack::has_ssrc(uint32_t ssrc)
return track_desc_->has_ssrc(ssrc);
}

SrsRtpPacket* SrsRtcSendTrack::fetch_rtp_packet(uint16_t seq, int64_t now_ms)
SrsRtpPacket* SrsRtcSendTrack::fetch_rtp_packet(uint16_t seq, srs_utime_t now_ms)
{
SrsRtpPacket* pkt = rtp_queue_->at(seq);
const int RESEND_MAX = 20;
Expand All @@ -2679,15 +2687,14 @@ SrsRtpPacket* SrsRtcSendTrack::fetch_rtp_packet(uint16_t seq, int64_t now_ms)
pkt->resend_ms_ = now_ms;
pkt->resend_count_++;
} else {
int64_t diff_t = now_ms - pkt->resend_ms_;
int64_t interval = (int64_t)avg_rtt_;
srs_utime_t diff_t = now_ms - pkt->resend_ms_;
srs_utime_t interval = (srs_utime_t)avg_rtt_;
interval = (interval > 10) ? (interval - 10) : interval;//for resend interval Residual
if (diff_t < (int64_t)interval) {
if (diff_t < interval) {
return NULL;
}
if (pkt->resend_count_ > RESEND_MAX) {
srs_warn("the rtp packet(seq=%d) resend count(%d) is too many", seq, pkt->resend_count_);
return NULL;
}
pkt->resend_ms_ = now_ms;
pkt->resend_count_++;
Expand Down Expand Up @@ -2736,19 +2743,19 @@ void SrsRtcSendTrack::rebuild_packet(SrsRtpPacket* pkt)
srs_info("RTC: Correct %s seq=%u/%u, ts=%u/%u", track_desc_->type_.c_str(), seq, pkt->header.get_sequence(), ts, pkt->header.get_timestamp());
}

srs_error_t SrsRtcSendTrack::send_rtcp_sr(int64_t now_ms) {
srs_error_t SrsRtcSendTrack::send_rtcp_sr(srs_utime_t now_ms) {
srs_error_t err = srs_success;
SrsRtcpSR* sr = new SrsRtcpSR();
SrsAutoFree(SrsRtcpSR, sr);

uint32_t ssrc = track_desc_->ssrc_;

last_sr_ntp_ = SrsNtp::from_time_ms(now_ms);
int64_t current_sr = ((last_sr_ntp_.ntp_second_ & 0xffff) << 16) | (last_sr_ntp_.ntp_fractions_ & 0xffff);
int64_t diff_ms = now_ms - last_rtp_ms_;
int64_t diff_ts = diff_ms * track_desc_->media_->sample_ / 1000;
int64_t video_rtp_ts = last_rtp_pkt_ts_ + diff_ts;
srs_utime_t diff_ms = now_ms - last_rtp_ms_;
srs_utime_t diff_ts = diff_ms * track_desc_->media_->sample_ / 1000;
srs_utime_t video_rtp_ts = last_rtp_pkt_ts_ + diff_ts;

//srs_trace("send rtcp sr ssrc:%u, current_sr:%ld, last_sr:%ld, diff:%ld", ssrc, current_sr, last_sr_, current_sr - last_sr_);
//srs_trace("send rtcp sr ssrc:%u, current ms:%ld, last ms:%ld, diff:%ld", ssrc, now_ms, last_ms_, now_ms - last_ms_);
last_sr_ = current_sr;
last_ms_ = now_ms;
sr->set_ssrc(ssrc);
Expand All @@ -2757,12 +2764,9 @@ srs_error_t SrsRtcSendTrack::send_rtcp_sr(int64_t now_ms) {
sr->set_rtp_send_packets(send_count_);
sr->set_rtp_send_bytes(send_bytes_);

char data[1500];
char data[kRtcpPacketSize];
SrsBuffer buffer(data, sr->nb_bytes());
sr->encode(&buffer);
delete sr;
sr = NULL;

session_->send_rtcp(buffer.data(), buffer.size());

return err;
Expand All @@ -2775,7 +2779,7 @@ void SrsRtcSendTrack::update_rtp_static(int64_t len, uint32_t rtp_ts) {
last_rtp_ms_ = srs_update_system_time() / 1000;//ms
}

srs_error_t SrsRtcSendTrack::handle_rtcp_rr(const SrsRtcpRB& rb, int64_t now_ms) {
srs_error_t SrsRtcSendTrack::handle_rtcp_rr(const SrsRtcpRB& rb, srs_utime_t now_ms) {
jitter_ = rb.jitter;
lost_rate_ = rb.fraction_lost / 256.0;
lost_total_ = rb.lost_packets;
Expand Down Expand Up @@ -2825,7 +2829,7 @@ srs_error_t SrsRtcSendTrack::on_nack(SrsRtpPacket** ppkt)
srs_error_t SrsRtcSendTrack::on_recv_nack(const vector<uint16_t>& lost_seqs)
{
srs_error_t err = srs_success;
int64_t now_ms = srs_update_system_time();
srs_utime_t now_ms = srs_update_system_time();

++_srs_pps_rnack2->sugar;

Expand Down
18 changes: 9 additions & 9 deletions trunk/src/app/srs_app_rtc_source.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -700,7 +700,7 @@ class SrsRtcSendTrack
// SrsRtcSendTrack::set_nack_no_copy
void set_nack_no_copy(bool v) { nack_no_copy_ = v; }
bool has_ssrc(uint32_t ssrc);
SrsRtpPacket* fetch_rtp_packet(uint16_t seq, int64_t now_ms);
SrsRtpPacket* fetch_rtp_packet(uint16_t seq, srs_utime_t now_ms);
bool set_track_status(bool active);
bool get_track_status();
std::string get_track_id();
Expand All @@ -715,17 +715,17 @@ class SrsRtcSendTrack
virtual srs_error_t on_rtcp(SrsRtpPacket* pkt) = 0;
virtual srs_error_t on_recv_nack(const std::vector<uint16_t>& lost_seqs);
public:
srs_error_t send_rtcp_sr(int64_t now_ms);
srs_error_t send_rtcp_sr(srs_utime_t now_ms);
void update_rtp_static(int64_t len, uint32_t rtp_ts);
public:
srs_error_t handle_rtcp_rr(const SrsRtcpRB& rb, int64_t now_ms);
srs_error_t handle_rtcp_rr(const SrsRtcpRB& rb, srs_utime_t now_ms);
protected:
int64_t send_bytes_ = 0;
int64_t send_count_ = 0;
int64_t last_rtp_pkt_ts_ = 0;
int64_t last_rtp_ms_ = 0;
int64_t last_sr_ = 0;//for debug
int64_t last_ms_ = 0;//for debug
int64_t send_bytes_;
int64_t send_count_;
int64_t last_rtp_pkt_ts_;
srs_utime_t last_rtp_ms_;
srs_utime_t last_sr_;
srs_utime_t last_ms_;
SrsNtp last_sr_ntp_;
};

Expand Down
2 changes: 1 addition & 1 deletion trunk/src/kernel/srs_kernel_rtc_rtp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ class SrsRtpPacket
//Rtp resend for rtcp nack
public:
int resend_count_;
int64_t resend_ms_;
srs_utime_t resend_ms_;
// Fast cache for performance.
private:
// The cached payload size for packet.
Expand Down
2 changes: 1 addition & 1 deletion trunk/src/utest/srs_utest_rtc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,7 @@ VOID TEST(KernelRTCTest, NACKFetchRTPPacket)
track->rtp_queue_->set(pkt->header.get_sequence(), pkt);
}

int64_t now_ms = srs_update_system_time();
srs_utime_t now_ms = srs_update_system_time();
// If sequence not match, packet not found.
if (true) {
SrsRtpPacket* pkt = track->fetch_rtp_packet(10, now_ms);
Expand Down

0 comments on commit db122a7

Please sign in to comment.