1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-02-15 04:42:04 +00:00
srs/trunk/src/app/srs_app_rtc_queue.cpp

293 lines
7.9 KiB
C++
Raw Normal View History

2020-04-23 09:08:21 +00:00
/**
* The MIT License (MIT)
*
* Copyright (c) 2013-2020 John
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
2020-05-11 04:07:55 +00:00
#include <srs_app_rtc_queue.hpp>
2020-04-23 09:08:21 +00:00
#include <string.h>
#include <unistd.h>
#include <sstream>
using namespace std;
#include <srs_kernel_error.hpp>
2020-05-11 04:07:55 +00:00
#include <srs_kernel_rtc_rtp.hpp>
2020-04-23 09:08:21 +00:00
#include <srs_kernel_utility.hpp>
#include <srs_app_utility.hpp>
2020-05-14 10:33:31 +00:00
SrsRtpRingBuffer::SrsRtpRingBuffer(int capacity)
{
2020-05-14 10:33:31 +00:00
nn_seq_flip_backs = 0;
begin = end = 0;
capacity_ = (uint16_t)capacity;
initialized_ = false;
2020-04-23 09:08:21 +00:00
2020-05-14 10:33:31 +00:00
queue_ = new SrsRtpPacket2*[capacity_];
memset(queue_, 0, sizeof(SrsRtpPacket2*) * capacity);
2020-04-23 09:08:21 +00:00
}
2020-05-14 10:33:31 +00:00
SrsRtpRingBuffer::~SrsRtpRingBuffer()
2020-04-23 09:08:21 +00:00
{
2020-05-14 10:33:31 +00:00
srs_freepa(queue_);
2020-04-23 09:08:21 +00:00
}
2020-05-14 10:33:31 +00:00
bool SrsRtpRingBuffer::empty()
{
2020-05-14 10:33:31 +00:00
return begin == end;
}
2020-05-14 10:33:31 +00:00
int SrsRtpRingBuffer::size()
{
2020-05-14 10:33:31 +00:00
int size = srs_rtp_seq_distance(begin, end);
srs_assert(size >= 0);
return size;
}
2020-05-14 10:33:31 +00:00
void SrsRtpRingBuffer::advance_to(uint16_t seq)
{
2020-05-14 10:33:31 +00:00
begin = seq;
2020-04-23 09:08:21 +00:00
}
2020-05-14 10:33:31 +00:00
void SrsRtpRingBuffer::set(uint16_t at, SrsRtpPacket2* pkt)
2020-04-23 09:08:21 +00:00
{
2020-05-14 10:33:31 +00:00
SrsRtpPacket2* p = queue_[at % capacity_];
2020-05-04 12:42:30 +00:00
2020-05-14 10:33:31 +00:00
if (p) {
srs_freep(p);
2020-04-23 09:08:21 +00:00
}
2020-05-14 10:33:31 +00:00
queue_[at % capacity_] = pkt;
2020-05-08 08:25:09 +00:00
}
2020-05-14 10:33:31 +00:00
void SrsRtpRingBuffer::remove(uint16_t at)
2020-05-02 12:57:36 +00:00
{
2020-05-14 10:33:31 +00:00
set(at, NULL);
2020-05-02 12:57:36 +00:00
}
2020-05-14 10:33:31 +00:00
uint32_t SrsRtpRingBuffer::get_extended_highest_sequence()
2020-05-03 09:57:07 +00:00
{
2020-05-14 10:33:31 +00:00
return nn_seq_flip_backs * 65536 + end - 1;
2020-05-03 09:57:07 +00:00
}
2020-05-14 10:33:31 +00:00
bool SrsRtpRingBuffer::update(uint16_t seq, uint16_t& nack_first, uint16_t& nack_last)
2020-05-03 09:57:07 +00:00
{
2020-05-14 10:33:31 +00:00
if (!initialized_) {
initialized_ = true;
begin = seq;
end = seq + 1;
2020-05-03 02:15:54 +00:00
return true;
}
2020-05-14 10:33:31 +00:00
// Normal sequence, seq follows high_.
if (srs_rtp_seq_distance(end, seq) >= 0) {
2020-05-17 03:16:00 +00:00
//TODO: FIXME: if diff_upper > limit_max_size clear?
// int16_t diff_upper = srs_rtp_seq_distance(end, seq)
// notify_nack_list_full()
2020-05-14 10:33:31 +00:00
nack_first = end;
nack_last = seq;
2020-05-03 02:15:54 +00:00
2020-05-14 10:33:31 +00:00
// When distance(seq,high_)>0 and seq<high_, seq must flip back,
// for example, high_=65535, seq=1, distance(65535,1)>0 and 1<65535.
// TODO: FIXME: The first flip may be dropped.
if (seq < end) {
++nn_seq_flip_backs;
}
2020-05-14 10:33:31 +00:00
end = seq + 1;
2020-05-17 03:16:00 +00:00
// TODO: FIXME: check whether is neccessary?
// srs_rtp_seq_distance(begin, end) > max_size
// advance_to(), srs_rtp_seq_distance(begin, end) < max_size;
2020-05-14 10:33:31 +00:00
return true;
2020-05-02 12:57:36 +00:00
}
2020-05-14 10:33:31 +00:00
// Out-of-order sequence, seq before low_.
if (srs_rtp_seq_distance(seq, begin) > 0) {
2020-05-17 03:16:00 +00:00
nack_first = seq;
nack_last = begin;
begin = seq;
2020-05-17 13:45:50 +00:00
// TODO: FIXME: Maybe should support startup drop.
2020-05-17 03:16:00 +00:00
return true;
2020-05-14 10:33:31 +00:00
// When startup, we may receive packets in chaos order.
// Because we don't know the ISN(initiazlie sequence number), the first packet
// we received maybe no the first packet client sent.
// @remark We only log a warning, because it seems ok for publisher.
2020-05-17 03:16:00 +00:00
//return false;
2020-05-02 12:57:36 +00:00
}
2020-05-14 10:33:31 +00:00
return true;
}
2020-05-14 10:33:31 +00:00
SrsRtpPacket2* SrsRtpRingBuffer::at(uint16_t seq) {
return queue_[seq % capacity_];
2020-04-23 09:08:21 +00:00
}
2020-05-02 12:57:36 +00:00
2020-05-14 10:33:31 +00:00
void SrsRtpRingBuffer::notify_nack_list_full()
2020-05-03 09:57:07 +00:00
{
while(begin <= end) {
remove(begin);
++begin;
}
begin = end = 0;
initialized_ = false;
2020-05-03 09:57:07 +00:00
}
2020-05-14 10:33:31 +00:00
void SrsRtpRingBuffer::notify_drop_seq(uint16_t seq)
2020-05-03 09:57:07 +00:00
{
remove(seq);
advance_to(seq+1);
2020-05-03 09:57:07 +00:00
}
2020-05-14 23:57:40 +00:00
SrsNackOption::SrsNackOption()
{
2020-05-17 12:20:37 +00:00
max_count = 15;
2020-09-09 06:26:42 +00:00
max_alive_time = 1000 * SRS_UTIME_MILLISECONDS;
2020-05-14 23:57:40 +00:00
first_nack_interval = 10 * SRS_UTIME_MILLISECONDS;
2020-05-17 12:20:37 +00:00
nack_interval = 50 * SRS_UTIME_MILLISECONDS;
2020-09-09 06:26:42 +00:00
max_nack_interval = 500 * SRS_UTIME_MILLISECONDS;
min_nack_interval = 20 * SRS_UTIME_MILLISECONDS;
nack_check_interval = 3 * SRS_UTIME_MILLISECONDS;
2020-05-17 13:45:50 +00:00
//TODO: FIXME: audio and video using diff nack strategy
2020-05-17 03:35:43 +00:00
// video:
// max_alive_time = 1 * SRS_UTIME_SECONDS
// max_count = 15;
// nack_interval = 50 * SRS_UTIME_MILLISECONDS
//
// audio:
// DefaultRequestNackDelay = 30; //ms
// DefaultLostPacketLifeTime = 600; //ms
// FirstRequestInterval = 50;//ms
2020-05-14 23:57:40 +00:00
}
2020-05-14 10:34:33 +00:00
SrsRtpNackInfo::SrsRtpNackInfo()
{
generate_time_ = srs_update_system_time();
pre_req_nack_time_ = 0;
req_nack_count_ = 0;
}
SrsRtpNackForReceiver::SrsRtpNackForReceiver(SrsRtpRingBuffer* rtp, size_t queue_size)
{
max_queue_size_ = queue_size;
rtp_ = rtp;
pre_check_time_ = 0;
2020-08-21 13:14:18 +00:00
rtt_ = 0;
2020-05-14 10:34:33 +00:00
srs_info("max_queue_size=%u, nack opt: max_count=%d, max_alive_time=%us, first_nack_interval=%" PRId64 ", nack_interval=%" PRId64,
max_queue_size_, opts_.max_count, opts_.max_alive_time, opts.first_nack_interval, opts_.nack_interval);
}
SrsRtpNackForReceiver::~SrsRtpNackForReceiver()
{
}
void SrsRtpNackForReceiver::insert(uint16_t first, uint16_t last)
{
for (uint16_t s = first; s != last; ++s) {
queue_[s] = SrsRtpNackInfo();
}
}
void SrsRtpNackForReceiver::remove(uint16_t seq)
{
queue_.erase(seq);
}
SrsRtpNackInfo* SrsRtpNackForReceiver::find(uint16_t seq)
{
std::map<uint16_t, SrsRtpNackInfo>::iterator iter = queue_.find(seq);
if (iter == queue_.end()) {
return NULL;
}
return &(iter->second);
}
void SrsRtpNackForReceiver::check_queue_size()
{
if (queue_.size() >= max_queue_size_) {
rtp_->notify_nack_list_full();
queue_.clear();
2020-05-14 10:34:33 +00:00
}
}
2020-09-09 06:07:13 +00:00
void SrsRtpNackForReceiver::get_nack_seqs(SrsRtcpNack& seqs, uint32_t& timeout_nacks)
2020-05-14 10:34:33 +00:00
{
2020-05-17 13:45:50 +00:00
// TODO: FIXME: Use packet as tick count, not clock.
2020-05-17 12:35:53 +00:00
srs_utime_t now = srs_update_system_time();
2020-05-17 13:45:50 +00:00
2020-05-14 10:34:33 +00:00
srs_utime_t interval = now - pre_check_time_;
2020-09-09 06:26:42 +00:00
if (interval < opts_.nack_check_interval) {
2020-05-14 10:34:33 +00:00
return;
}
pre_check_time_ = now;
std::map<uint16_t, SrsRtpNackInfo>::iterator iter = queue_.begin();
while (iter != queue_.end()) {
const uint16_t& seq = iter->first;
SrsRtpNackInfo& nack_info = iter->second;
int alive_time = now - nack_info.generate_time_;
if (alive_time > opts_.max_alive_time || nack_info.req_nack_count_ > opts_.max_count) {
2020-09-09 06:07:13 +00:00
++timeout_nacks;
2020-05-14 10:34:33 +00:00
rtp_->notify_drop_seq(seq);
queue_.erase(iter++);
2020-05-14 10:34:33 +00:00
continue;
}
// TODO:Statistics unorder packet.
if (now - nack_info.generate_time_ < opts_.first_nack_interval) {
break;
}
2020-09-09 06:26:42 +00:00
srs_utime_t nack_interval = srs_max(opts_.min_nack_interval, opts_.nack_interval / 3);
if(opts_.nack_interval < 50 * SRS_UTIME_MILLISECONDS){
nack_interval = srs_max(opts_.min_nack_interval, opts_.nack_interval);
}
if (now - nack_info.pre_req_nack_time_ >= nack_interval ) {
2020-05-14 10:34:33 +00:00
++nack_info.req_nack_count_;
nack_info.pre_req_nack_time_ = now;
2020-09-02 03:30:20 +00:00
seqs.add_lost_sn(seq);
2020-05-14 10:34:33 +00:00
}
++iter;
}
}
void SrsRtpNackForReceiver::update_rtt(int rtt)
{
rtt_ = rtt * SRS_UTIME_MILLISECONDS;
2020-09-09 06:26:42 +00:00
if (rtt_ > opts_.nack_interval) {
opts_.nack_interval = opts_.nack_interval * 0.8 + rtt_ * 0.2;
} else {
opts_.nack_interval = rtt_;
}
opts_.nack_interval = srs_min(opts_.nack_interval, opts_.max_nack_interval);
2020-05-14 10:34:33 +00:00
}