Skip to content

Commit 2ad24b2

Browse files
committedMay 8, 2021
Refine shared fast timer. 4.0.105
·
v6.0.48v4.0-b0
1 parent f370259 commit 2ad24b2

File tree

5 files changed

+186
-122
lines changed

5 files changed

+186
-122
lines changed
 

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ The ports used by SRS:
176176

177177
## V4 changes
178178

179+
* v4.0, 2021-05-08, Refine shared fast timer. 4.0.105
179180
* v4.0, 2021-05-08, Refine global or thread-local variables initialize. 4.0.104
180181
* v4.0, 2021-05-07, RTC: Support circuit breaker. 4.0.103
181182
* v4.0, 2021-05-07, RTC: Refine play stream find track. 4.0.102

‎trunk/src/app/srs_app_rtc_conn.cpp

Lines changed: 126 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -392,8 +392,6 @@ SrsRtcPlayStream::SrsRtcPlayStream(SrsRtcConnection* s, const SrsContextId& cid)
392392

393393
SrsRtcPlayStream::~SrsRtcPlayStream()
394394
{
395-
_srs_hybrid->timer1s()->unsubscribe(this);
396-
397395
// TODO: FIXME: Should not do callback in de-constructor?
398396
if (_srs_rtc_hijacker) {
399397
_srs_rtc_hijacker->on_stop_play(session_, this, req_);
@@ -534,10 +532,6 @@ srs_error_t SrsRtcPlayStream::start()
534532
return srs_error_wrap(err, "rtc_sender");
535533
}
536534

537-
// The timer for play, process TWCC in the future.
538-
// @see SrsRtcPlayStream::on_timer()
539-
_srs_hybrid->timer1s()->subscribe(this);
540-
541535
if ((err = pli_worker_->start()) != srs_success) {
542536
return srs_error_wrap(err, "start pli worker");
543537
}
@@ -722,17 +716,6 @@ void SrsRtcPlayStream::set_all_tracks_status(bool status)
722716
srs_trace("RTC: Init tracks %s ok", merged_log.str().c_str());
723717
}
724718

725-
srs_error_t SrsRtcPlayStream::on_timer(srs_utime_t interval)
726-
{
727-
srs_error_t err = srs_success;
728-
729-
if (!is_started) {
730-
return err;
731-
}
732-
733-
return err;
734-
}
735-
736719
srs_error_t SrsRtcPlayStream::on_rtcp(SrsRtcpCommon* rtcp)
737720
{
738721
if(SrsRtcpType_rr == rtcp->type()) {
@@ -893,6 +876,85 @@ srs_error_t SrsRtcPlayStream::do_request_keyframe(uint32_t ssrc, SrsContextId ci
893876
return err;
894877
}
895878

879+
SrsRtcPublishRtcpTimer::SrsRtcPublishRtcpTimer(SrsRtcPublishStream* p) : p_(p)
880+
{
881+
_srs_hybrid->timer1s()->subscribe(this);
882+
}
883+
884+
SrsRtcPublishRtcpTimer::~SrsRtcPublishRtcpTimer()
885+
{
886+
_srs_hybrid->timer1s()->unsubscribe(this);
887+
}
888+
889+
srs_error_t SrsRtcPublishRtcpTimer::on_timer(srs_utime_t interval)
890+
{
891+
srs_error_t err = srs_success;
892+
893+
++_srs_pps_pub->sugar;
894+
895+
if (!p_->is_started) {
896+
return err;
897+
}
898+
899+
// For RR and RRTR.
900+
++_srs_pps_rr->sugar;
901+
902+
if ((err = p_->send_rtcp_rr()) != srs_success) {
903+
srs_warn("RR err %s", srs_error_desc(err).c_str());
904+
srs_freep(err);
905+
}
906+
907+
if ((err = p_->send_rtcp_xr_rrtr()) != srs_success) {
908+
srs_warn("XR err %s", srs_error_desc(err).c_str());
909+
srs_freep(err);
910+
}
911+
912+
return err;
913+
}
914+
915+
SrsRtcPublishTwccTimer::SrsRtcPublishTwccTimer(SrsRtcPublishStream* p) : p_(p)
916+
{
917+
_srs_hybrid->timer100ms()->subscribe(this);
918+
}
919+
920+
SrsRtcPublishTwccTimer::~SrsRtcPublishTwccTimer()
921+
{
922+
_srs_hybrid->timer100ms()->unsubscribe(this);
923+
}
924+
925+
srs_error_t SrsRtcPublishTwccTimer::on_timer(srs_utime_t interval)
926+
{
927+
srs_error_t err = srs_success;
928+
929+
++_srs_pps_pub->sugar;
930+
931+
if (!p_->is_started) {
932+
return err;
933+
}
934+
935+
// For TWCC feedback.
936+
if (!p_->twcc_enabled_) {
937+
return err;
938+
}
939+
940+
++_srs_pps_twcc->sugar;
941+
942+
// If circuit-breaker is dropping packet, disable TWCC.
943+
if (_srs_circuit_breaker->hybrid_critical_water_level()) {
944+
++_srs_pps_snack4->sugar;
945+
return err;
946+
}
947+
948+
// We should not depends on the received packet,
949+
// instead we should send feedback every Nms.
950+
if ((err = p_->send_periodic_twcc()) != srs_success) {
951+
srs_warn("TWCC err %s", srs_error_desc(err).c_str());
952+
srs_freep(err);
953+
}
954+
955+
return err;
956+
}
957+
896958
SrsRtcPublishStream::SrsRtcPublishStream(SrsRtcConnection* session, const SrsContextId& cid)
897959
{
898960
cid_ = cid;
@@ -916,11 +978,15 @@ SrsRtcPublishStream::SrsRtcPublishStream(SrsRtcConnection* session, const SrsCon
916978

917979
pli_worker_ = new SrsRtcPLIWorker(this);
918980
last_time_send_twcc_ = 0;
981+
982+
timer_rtcp_ = new SrsRtcPublishRtcpTimer(this);
983+
timer_twcc_ = new SrsRtcPublishTwccTimer(this);
919984
}
920985

921986
SrsRtcPublishStream::~SrsRtcPublishStream()
922987
{
923-
_srs_hybrid->timer100ms()->unsubscribe(this);
988+
srs_freep(timer_rtcp_);
989+
srs_freep(timer_twcc_);
924990

925991
// TODO: FIXME: Should remove and delete source.
926992
if (source) {
@@ -1056,10 +1122,6 @@ srs_error_t SrsRtcPublishStream::start()
10561122
return err;
10571123
}
10581124

1059-
// For publisher timer, such as TWCC and RR.
1060-
// @see SrsRtcPublishStream::on_timer()
1061-
_srs_hybrid->timer100ms()->subscribe(this);
1062-
10631125
if ((err = source->on_publish()) != srs_success) {
10641126
return srs_error_wrap(err, "on publish");
10651127
}
@@ -1529,52 +1591,6 @@ srs_error_t SrsRtcPublishStream::do_request_keyframe(uint32_t ssrc, SrsContextId
15291591
return err;
15301592
}
15311593

1532-
srs_error_t SrsRtcPublishStream::on_timer(srs_utime_t interval)
1533-
{
1534-
srs_error_t err = srs_success;
1535-
1536-
++_srs_pps_pub->sugar;
1537-
1538-
if (!is_started) {
1539-
return err;
1540-
}
1541-
1542-
// For RR and RRTR.
1543-
if (true) {
1544-
++_srs_pps_rr->sugar;
1545-
1546-
if ((err = send_rtcp_rr()) != srs_success) {
1547-
srs_warn("RR err %s", srs_error_desc(err).c_str());
1548-
srs_freep(err);
1549-
}
1550-
1551-
if ((err = send_rtcp_xr_rrtr()) != srs_success) {
1552-
srs_warn("XR err %s", srs_error_desc(err).c_str());
1553-
srs_freep(err);
1554-
}
1555-
}
1556-
1557-
// For TWCC feedback.
1558-
if (twcc_enabled_) {
1559-
++_srs_pps_twcc->sugar;
1560-
1561-
// If circuit-breaker is dropping packet, disable TWCC.
1562-
if (_srs_circuit_breaker->hybrid_critical_water_level()) {
1563-
++_srs_pps_snack4->sugar;
1564-
return err;
1565-
}
1566-
1567-
// We should not depends on the received packet,
1568-
// instead we should send feedback every Nms.
1569-
if ((err = send_periodic_twcc()) != srs_success) {
1570-
srs_warn("TWCC err %s", srs_error_desc(err).c_str());
1571-
srs_freep(err);
1572-
}
1573-
}
1574-
1575-
return err;
1576-
}
1577-
15781594
void SrsRtcPublishStream::simulate_nack_drop(int nn)
15791595
{
15801596
nn_simulate_nack_drop = nn;
@@ -1692,6 +1708,45 @@ ISrsRtcConnectionHijacker::~ISrsRtcConnectionHijacker()
16921708
{
16931709
}
16941710

1711+
SrsRtcConnectionNackTimer::SrsRtcConnectionNackTimer(SrsRtcConnection* p) : p_(p)
1712+
{
1713+
_srs_hybrid->timer20ms()->subscribe(this);
1714+
}
1715+
1716+
SrsRtcConnectionNackTimer::~SrsRtcConnectionNackTimer()
1717+
{
1718+
_srs_hybrid->timer20ms()->unsubscribe(this);
1719+
}
1720+
1721+
srs_error_t SrsRtcConnectionNackTimer::on_timer(srs_utime_t interval)
1722+
{
1723+
srs_error_t err = srs_success;
1724+
1725+
if (!p_->nack_enabled_) {
1726+
return err;
1727+
}
1728+
1729+
++_srs_pps_conn->sugar;
1730+
1731+
// If circuit-breaker is enabled, disable nack.
1732+
if (_srs_circuit_breaker->hybrid_critical_water_level()) {
1733+
++_srs_pps_snack4->sugar;
1734+
return err;
1735+
}
1736+
1737+
std::map<std::string, SrsRtcPublishStream*>::iterator it;
1738+
for (it = p_->publishers_.begin(); it != p_->publishers_.end(); it++) {
1739+
SrsRtcPublishStream* publisher = it->second;
1740+
1741+
if ((err = publisher->check_send_nacks()) != srs_success) {
1742+
srs_warn("ignore nack err %s", srs_error_desc(err).c_str());
1743+
srs_freep(err);
1744+
}
1745+
}
1746+
1747+
return err;
1748+
}
1749+
16951750
SrsRtcConnection::SrsRtcConnection(SrsRtcServer* s, const SrsContextId& cid)
16961751
{
16971752
req = NULL;
@@ -1719,16 +1774,17 @@ SrsRtcConnection::SrsRtcConnection(SrsRtcServer* s, const SrsContextId& cid)
17191774
pli_epp = new SrsErrorPithyPrint();
17201775

17211776
nack_enabled_ = false;
1777+
timer_nack_ = new SrsRtcConnectionNackTimer(this);
17221778

17231779
_srs_rtc_manager->subscribe(this);
17241780
}
17251781

17261782
SrsRtcConnection::~SrsRtcConnection()
17271783
{
1728-
_srs_hybrid->timer20ms()->unsubscribe(this);
1729-
17301784
_srs_rtc_manager->unsubscribe(this);
17311785

1786+
srs_freep(timer_nack_);
1787+
17321788
// Cleanup publishers.
17331789
for(map<string, SrsRtcPublishStream*>::iterator it = publishers_.begin(); it != publishers_.end(); ++it) {
17341790
SrsRtcPublishStream* publisher = it->second;
@@ -1972,10 +2028,6 @@ srs_error_t SrsRtcConnection::initialize(SrsRequest* r, bool dtls, bool srtp, st
19722028
return srs_error_wrap(err, "init");
19732029
}
19742030

1975-
// The RTC connection start a timer, handle nacks.
1976-
// @see SrsRtcConnection::on_timer()
1977-
_srs_hybrid->timer20ms()->subscribe(this);
1978-
19792031
// TODO: FIXME: Support reload.
19802032
session_timeout = _srs_config->get_rtc_stun_timeout(req->vhost);
19812033
last_stun_time = srs_get_system_time();
@@ -2351,35 +2403,6 @@ void SrsRtcConnection::update_sendonly_socket(SrsUdpMuxSocket* skt)
23512403
sendonly_skt = addr_cache;
23522404
}
23532405

2354-
srs_error_t SrsRtcConnection::on_timer(srs_utime_t interval)
2355-
{
2356-
srs_error_t err = srs_success;
2357-
2358-
if (!nack_enabled_) {
2359-
return err;
2360-
}
2361-
2362-
++_srs_pps_conn->sugar;
2363-
2364-
// If circuit-breaker is enabled, disable nack.
2365-
if (_srs_circuit_breaker->hybrid_critical_water_level()) {
2366-
++_srs_pps_snack4->sugar;
2367-
return err;
2368-
}
2369-
2370-
std::map<std::string, SrsRtcPublishStream*>::iterator it;
2371-
for (it = publishers_.begin(); it != publishers_.end(); it++) {
2372-
SrsRtcPublishStream* publisher = it->second;
2373-
2374-
if ((err = publisher->check_send_nacks()) != srs_success) {
2375-
srs_warn("ignore nack err %s", srs_error_desc(err).c_str());
2376-
srs_freep(err);
2377-
}
2378-
}
2379-
2380-
return err;
2381-
}
2382-
23832406
srs_error_t SrsRtcConnection::send_rtcp(char *data, int nb_data)
23842407
{
23852408
srs_error_t err = srs_success;

0 commit comments

Comments
 (0)
Please sign in to comment.