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.hpp

144 lines
4.6 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
#ifndef SRS_APP_RTC_QUEUE_HPP
#define SRS_APP_RTC_QUEUE_HPP
2020-04-23 09:08:21 +00:00
#include <srs_core.hpp>
#include <string>
#include <vector>
#include <map>
class SrsRtpPacket2;
2020-04-23 09:08:21 +00:00
class SrsRtpQueue;
2020-05-14 10:33:31 +00:00
class SrsRtpRingBuffer;
2020-04-23 09:08:21 +00:00
// For UDP, the packets sequence may present as bellow:
// [seq1(done)|seq2|seq3 ... seq10|seq11(lost)|seq12|seq13]
// \___(head_sequence_) \ \___(highest_sequence_)
// \___(no received, in nack list)
// * seq1: The packet is done, we already got the entire frame and processed it.
// * seq2,seq3,...,seq10,seq12,seq13: We are processing theses packets, for example, some FU-A or NALUs,
// but not an entire video frame right now.
// * seq10: This packet is lost or not received, we put it in the nack list.
// We store the received packets in ring buffer.
class SrsRtpRingBuffer
2020-04-23 09:08:21 +00:00
{
private:
// Capacity of the ring-buffer.
2020-04-24 08:19:08 +00:00
uint16_t capacity_;
2020-04-23 09:08:21 +00:00
// Ring bufer.
2020-05-14 10:33:31 +00:00
SrsRtpPacket2** queue_;
// Increase one when uint16 flip back, for get_extended_highest_sequence.
uint64_t nn_seq_flip_backs;
// Whether initialized, because we use uint16 so we can't use -1.
bool initialized_;
2020-05-03 11:09:48 +00:00
public:
// The begin iterator for ring buffer.
// For example, when got 1 elems, the begin is 0.
uint16_t begin;
// The end iterator for ring buffer.
// For example, when got 1 elems, the end is 1.
uint16_t end;
public:
2020-05-14 10:33:31 +00:00
SrsRtpRingBuffer(int capacity);
virtual ~SrsRtpRingBuffer();
public:
2020-05-03 11:09:48 +00:00
// Whether the ring buffer is empty.
2020-05-14 10:33:31 +00:00
bool empty();
2020-05-03 11:09:48 +00:00
// Get the count of elems in ring buffer.
2020-05-14 10:33:31 +00:00
int size();
2020-05-03 11:09:48 +00:00
// Move the low position of buffer to seq.
2020-05-14 10:33:31 +00:00
void advance_to(uint16_t seq);
// Free the packet at position.
2020-05-14 10:33:31 +00:00
void set(uint16_t at, SrsRtpPacket2* pkt);
void remove(uint16_t at);
// The highest sequence number, calculate the flip back base.
2020-05-14 10:33:31 +00:00
uint32_t get_extended_highest_sequence();
2020-05-03 11:09:48 +00:00
// Update the sequence, got the nack range by [first, last).
// @return If false, the seq is too old.
2020-05-14 10:33:31 +00:00
bool update(uint16_t seq, uint16_t& nack_first, uint16_t& nack_last);
// Get the packet by seq.
2020-05-14 10:33:31 +00:00
SrsRtpPacket2* at(uint16_t seq);
2020-04-23 09:08:21 +00:00
public:
2020-05-14 10:33:31 +00:00
// TODO: FIXME: Move it?
void notify_nack_list_full();
void notify_drop_seq(uint16_t seq);
2020-04-23 09:08:21 +00:00
};
2020-05-14 10:34:33 +00:00
struct SrsNackOption
{
int max_count;
srs_utime_t max_alive_time;
int64_t first_nack_interval;
int64_t nack_interval;
2020-05-14 23:57:40 +00:00
SrsNackOption();
2020-05-14 10:34:33 +00:00
};
struct SrsRtpNackInfo
{
// Use to control the time of first nack req and the life of seq.
srs_utime_t generate_time_;
// Use to control nack interval.
srs_utime_t pre_req_nack_time_;
// Use to control nack times.
int req_nack_count_;
2020-05-14 23:57:40 +00:00
SrsRtpNackInfo();
2020-05-14 10:34:33 +00:00
};
class SrsRtpNackForReceiver
{
private:
struct SeqComp {
bool operator()(const uint16_t& pre_value, const uint16_t& value) const;
2020-05-14 10:34:33 +00:00
};
private:
// Nack queue, seq order, oldest to newest.
std::map<uint16_t, SrsRtpNackInfo, SeqComp> queue_;
// Max nack count.
size_t max_queue_size_;
SrsRtpRingBuffer* rtp_;
SrsNackOption opts_;
private:
srs_utime_t pre_check_time_;
private:
int rtt_;
public:
SrsRtpNackForReceiver(SrsRtpRingBuffer* rtp, size_t queue_size);
virtual ~SrsRtpNackForReceiver();
public:
void insert(uint16_t first, uint16_t last);
void remove(uint16_t seq);
SrsRtpNackInfo* find(uint16_t seq);
void check_queue_size();
public:
void get_nack_seqs(std::vector<uint16_t>& seqs);
public:
void update_rtt(int rtt);
};
2020-04-23 09:08:21 +00:00
#endif