2018-12-14 17:46:12 +08:00
|
|
|
|
/*
|
2020-04-04 20:30:09 +08:00
|
|
|
|
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
|
2018-12-14 17:46:12 +08:00
|
|
|
|
*
|
2021-01-17 18:31:50 +08:00
|
|
|
|
* This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
|
2018-12-14 17:46:12 +08:00
|
|
|
|
*
|
2020-04-04 20:30:09 +08:00
|
|
|
|
* Use of this source code is governed by MIT license that can be found in the
|
|
|
|
|
|
* LICENSE file in the root of the source tree. All contributing project authors
|
|
|
|
|
|
* may be found in the AUTHORS file in the root of the source tree.
|
2018-12-14 17:46:12 +08:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef ZLMEDIAKIT_RTPRECEIVER_H
|
|
|
|
|
|
#define ZLMEDIAKIT_RTPRECEIVER_H
|
|
|
|
|
|
|
2018-12-14 18:13:05 +08:00
|
|
|
|
#include <map>
|
2018-12-14 17:46:12 +08:00
|
|
|
|
#include <string>
|
|
|
|
|
|
#include <memory>
|
2019-06-28 17:30:13 +08:00
|
|
|
|
#include "RtpCodec.h"
|
2018-12-14 17:46:12 +08:00
|
|
|
|
#include "RtspMediaSource.h"
|
2021-07-12 21:18:22 +08:00
|
|
|
|
#include "Common/Stamp.h"
|
2018-12-14 17:46:12 +08:00
|
|
|
|
using namespace std;
|
|
|
|
|
|
using namespace toolkit;
|
|
|
|
|
|
|
|
|
|
|
|
namespace mediakit {
|
|
|
|
|
|
|
2021-05-11 11:18:55 +08:00
|
|
|
|
template<typename T, typename SEQ = uint16_t, size_t kMax = 1024, size_t kMin = 32>
|
2020-10-01 19:02:14 +08:00
|
|
|
|
class PacketSortor {
|
|
|
|
|
|
public:
|
|
|
|
|
|
PacketSortor() = default;
|
|
|
|
|
|
~PacketSortor() = default;
|
|
|
|
|
|
|
2020-10-01 21:33:07 +08:00
|
|
|
|
void setOnSort(function<void(SEQ seq, T &packet)> cb) {
|
2020-10-01 19:02:14 +08:00
|
|
|
|
_cb = std::move(cb);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 清空状态
|
|
|
|
|
|
*/
|
|
|
|
|
|
void clear() {
|
|
|
|
|
|
_seq_cycle_count = 0;
|
2021-07-17 19:58:29 +08:00
|
|
|
|
_pkt_sort_cache_map.clear();
|
2020-10-01 21:33:07 +08:00
|
|
|
|
_next_seq_out = 0;
|
|
|
|
|
|
_max_sort_size = kMin;
|
2020-10-01 19:02:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 获取排序缓存长度
|
|
|
|
|
|
*/
|
2021-01-17 18:31:50 +08:00
|
|
|
|
size_t getJitterSize() const{
|
2021-07-17 19:58:29 +08:00
|
|
|
|
return _pkt_sort_cache_map.size();
|
2020-10-01 19:02:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 获取seq回环次数
|
|
|
|
|
|
*/
|
2021-01-17 18:31:50 +08:00
|
|
|
|
size_t getCycleCount() const{
|
2020-10-01 19:02:14 +08:00
|
|
|
|
return _seq_cycle_count;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 输入并排序
|
|
|
|
|
|
* @param seq 序列号
|
|
|
|
|
|
* @param packet 包负载
|
|
|
|
|
|
*/
|
2020-10-01 21:33:07 +08:00
|
|
|
|
void sortPacket(SEQ seq, T packet) {
|
2020-10-17 14:46:59 +08:00
|
|
|
|
if (seq < _next_seq_out) {
|
2021-02-04 18:09:06 +08:00
|
|
|
|
if (_next_seq_out < seq + kMax) {
|
2020-10-17 14:46:59 +08:00
|
|
|
|
//过滤seq回退包(回环包除外)
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2021-07-19 10:31:24 +08:00
|
|
|
|
} else if (_next_seq_out && seq - _next_seq_out > ((std::numeric_limits<SEQ>::max)() >> 1)) {
|
2020-10-17 14:46:59 +08:00
|
|
|
|
//过滤seq跳变非常大的包(防止回环时乱序时收到非常大的seq)
|
|
|
|
|
|
return;
|
2020-10-01 19:02:14 +08:00
|
|
|
|
}
|
2020-10-17 14:46:59 +08:00
|
|
|
|
|
2020-10-01 21:33:07 +08:00
|
|
|
|
//放入排序缓存
|
2021-07-17 19:58:29 +08:00
|
|
|
|
_pkt_sort_cache_map.emplace(seq, std::move(packet));
|
2020-10-01 21:33:07 +08:00
|
|
|
|
//尝试输出排序后的包
|
|
|
|
|
|
tryPopPacket();
|
2020-10-01 19:02:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-10-17 14:46:59 +08:00
|
|
|
|
void flush(){
|
|
|
|
|
|
//清空缓存
|
2021-07-17 19:58:29 +08:00
|
|
|
|
while (!_pkt_sort_cache_map.empty()) {
|
|
|
|
|
|
popIterator(_pkt_sort_cache_map.begin());
|
2020-10-17 14:46:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-10-01 19:02:14 +08:00
|
|
|
|
private:
|
|
|
|
|
|
void popPacket() {
|
2021-07-17 19:58:29 +08:00
|
|
|
|
auto it = _pkt_sort_cache_map.begin();
|
2020-10-17 14:46:59 +08:00
|
|
|
|
if (it->first >= _next_seq_out) {
|
|
|
|
|
|
//过滤回跳包
|
|
|
|
|
|
popIterator(it);
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (_next_seq_out - it->first > (0xFFFF >> 1)) {
|
|
|
|
|
|
//产生回环了
|
2021-07-17 19:58:29 +08:00
|
|
|
|
if (_pkt_sort_cache_map.size() < 2 * kMin) {
|
2020-10-17 14:46:59 +08:00
|
|
|
|
//等足够多的数据后才处理回环, 因为后面还可能出现大的SEQ
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
++_seq_cycle_count;
|
|
|
|
|
|
//找到大的SEQ并清空掉,然后从小的SEQ重新开始排序
|
2021-07-17 19:58:29 +08:00
|
|
|
|
auto hit = _pkt_sort_cache_map.upper_bound((SEQ) (_next_seq_out - _pkt_sort_cache_map.size()));
|
|
|
|
|
|
while (hit != _pkt_sort_cache_map.end()) {
|
2020-10-17 14:46:59 +08:00
|
|
|
|
//回环前,清空剩余的大的SEQ的数据
|
|
|
|
|
|
_cb(hit->first, hit->second);
|
2021-07-17 19:58:29 +08:00
|
|
|
|
hit = _pkt_sort_cache_map.erase(hit);
|
2020-10-17 14:46:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
//下一个回环的数据
|
2021-07-17 19:58:29 +08:00
|
|
|
|
popIterator(_pkt_sort_cache_map.begin());
|
2020-10-17 14:46:59 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
//删除回跳的数据包
|
2021-07-17 19:58:29 +08:00
|
|
|
|
_pkt_sort_cache_map.erase(it);
|
2020-10-17 14:46:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void popIterator(typename map<SEQ, T>::iterator it) {
|
2021-01-17 10:25:00 +08:00
|
|
|
|
auto seq = it->first;
|
|
|
|
|
|
auto data = std::move(it->second);
|
2021-07-17 19:58:29 +08:00
|
|
|
|
_pkt_sort_cache_map.erase(it);
|
2021-01-17 10:25:00 +08:00
|
|
|
|
_next_seq_out = seq + 1;
|
|
|
|
|
|
_cb(seq, data);
|
2020-10-01 19:02:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-10-01 21:33:07 +08:00
|
|
|
|
void tryPopPacket() {
|
2020-10-17 14:46:59 +08:00
|
|
|
|
int count = 0;
|
2021-07-17 19:58:29 +08:00
|
|
|
|
while ((!_pkt_sort_cache_map.empty() && _pkt_sort_cache_map.begin()->first == _next_seq_out)) {
|
2020-10-01 21:33:07 +08:00
|
|
|
|
//找到下个包,直接输出
|
|
|
|
|
|
popPacket();
|
2020-10-17 14:46:59 +08:00
|
|
|
|
++count;
|
2020-10-01 21:33:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-10-17 14:46:59 +08:00
|
|
|
|
if (count) {
|
2020-10-01 21:33:07 +08:00
|
|
|
|
setSortSize();
|
2021-07-17 19:58:29 +08:00
|
|
|
|
} else if (_pkt_sort_cache_map.size() > _max_sort_size) {
|
2020-10-01 21:33:07 +08:00
|
|
|
|
//排序缓存溢出,不再继续排序
|
|
|
|
|
|
popPacket();
|
|
|
|
|
|
setSortSize();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void setSortSize() {
|
2021-07-17 19:58:29 +08:00
|
|
|
|
_max_sort_size = kMin + _pkt_sort_cache_map.size();
|
2020-10-01 21:33:07 +08:00
|
|
|
|
if (_max_sort_size > kMax) {
|
|
|
|
|
|
_max_sort_size = kMax;
|
|
|
|
|
|
}
|
2020-10-01 19:02:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private:
|
2020-10-01 21:33:07 +08:00
|
|
|
|
//下次应该输出的SEQ
|
|
|
|
|
|
SEQ _next_seq_out = 0;
|
2020-10-01 19:02:14 +08:00
|
|
|
|
//seq回环次数计数
|
2021-01-17 18:31:50 +08:00
|
|
|
|
size_t _seq_cycle_count = 0;
|
2020-10-01 19:02:14 +08:00
|
|
|
|
//排序缓存长度
|
2021-01-17 18:31:50 +08:00
|
|
|
|
size_t _max_sort_size = kMin;
|
2021-07-17 19:58:29 +08:00
|
|
|
|
//pkt排序缓存,根据seq排序
|
|
|
|
|
|
map<SEQ, T> _pkt_sort_cache_map;
|
2020-10-01 19:02:14 +08:00
|
|
|
|
//回调
|
2020-10-01 21:33:07 +08:00
|
|
|
|
function<void(SEQ seq, T &packet)> _cb;
|
2020-10-01 19:02:14 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
2021-06-25 16:24:44 +08:00
|
|
|
|
class RtpTrack : private PacketSortor<RtpPacket::Ptr>{
|
2018-12-14 17:46:12 +08:00
|
|
|
|
public:
|
2021-04-22 22:02:21 +08:00
|
|
|
|
class BadRtpException : public invalid_argument {
|
|
|
|
|
|
public:
|
|
|
|
|
|
template<typename Type>
|
|
|
|
|
|
BadRtpException(Type &&type) : invalid_argument(std::forward<Type>(type)) {}
|
|
|
|
|
|
~BadRtpException() = default;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2021-06-25 16:24:44 +08:00
|
|
|
|
RtpTrack();
|
|
|
|
|
|
virtual ~RtpTrack() = default;
|
|
|
|
|
|
|
|
|
|
|
|
void clear();
|
|
|
|
|
|
uint32_t getSSRC() const;
|
2021-07-14 21:39:30 +08:00
|
|
|
|
RtpPacket::Ptr inputRtp(TrackType type, int sample_rate, uint8_t *ptr, size_t len);
|
2021-09-02 21:17:59 +08:00
|
|
|
|
void setNtpStamp(uint32_t rtp_stamp, uint64_t ntp_stamp_ms);
|
2021-06-25 16:24:44 +08:00
|
|
|
|
|
2020-10-01 19:02:14 +08:00
|
|
|
|
protected:
|
2021-06-25 16:24:44 +08:00
|
|
|
|
virtual void onRtpSorted(RtpPacket::Ptr rtp) {}
|
|
|
|
|
|
virtual void onBeforeRtpSorted(const RtpPacket::Ptr &rtp) {}
|
|
|
|
|
|
|
|
|
|
|
|
private:
|
2021-08-21 18:52:52 +08:00
|
|
|
|
bool _disable_ntp = false;
|
2021-06-25 16:24:44 +08:00
|
|
|
|
uint32_t _ssrc = 0;
|
|
|
|
|
|
Ticker _ssrc_alive;
|
2021-07-12 21:18:22 +08:00
|
|
|
|
NtpStamp _ntp_stamp;
|
2021-06-25 16:24:44 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class RtpTrackImp : public RtpTrack{
|
|
|
|
|
|
public:
|
|
|
|
|
|
using OnSorted = function<void(RtpPacket::Ptr)>;
|
|
|
|
|
|
using BeforeSorted = function<void(const RtpPacket::Ptr &)>;
|
|
|
|
|
|
|
|
|
|
|
|
RtpTrackImp() = default;
|
|
|
|
|
|
~RtpTrackImp() override = default;
|
|
|
|
|
|
|
|
|
|
|
|
void setOnSorted(OnSorted cb);
|
|
|
|
|
|
void setBeforeSorted(BeforeSorted cb);
|
|
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
void onRtpSorted(RtpPacket::Ptr rtp) override;
|
|
|
|
|
|
void onBeforeRtpSorted(const RtpPacket::Ptr &rtp) override;
|
|
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
OnSorted _on_sorted;
|
|
|
|
|
|
BeforeSorted _on_before_sorted;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
template<int kCount = 2>
|
|
|
|
|
|
class RtpMultiReceiver {
|
|
|
|
|
|
public:
|
|
|
|
|
|
RtpMultiReceiver() {
|
|
|
|
|
|
int index = 0;
|
|
|
|
|
|
for (auto &track : _track) {
|
|
|
|
|
|
track.setOnSorted([this, index](RtpPacket::Ptr rtp) {
|
|
|
|
|
|
onRtpSorted(std::move(rtp), index);
|
|
|
|
|
|
});
|
|
|
|
|
|
track.setBeforeSorted([this, index](const RtpPacket::Ptr &rtp) {
|
|
|
|
|
|
onBeforeRtpSorted(rtp, index);
|
|
|
|
|
|
});
|
|
|
|
|
|
++index;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
virtual ~RtpMultiReceiver() = default;
|
|
|
|
|
|
|
2018-12-14 17:46:12 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 输入数据指针生成并排序rtp包
|
2021-01-31 19:19:24 +08:00
|
|
|
|
* @param index track下标索引
|
2020-07-08 23:23:11 +08:00
|
|
|
|
* @param type track类型
|
|
|
|
|
|
* @param samplerate rtp时间戳基准时钟,视频为90000,音频为采样率
|
2021-01-31 19:19:24 +08:00
|
|
|
|
* @param ptr rtp数据指针
|
|
|
|
|
|
* @param len rtp数据指针长度
|
2018-12-14 17:46:12 +08:00
|
|
|
|
* @return 解析成功返回true
|
|
|
|
|
|
*/
|
2021-06-25 16:24:44 +08:00
|
|
|
|
bool handleOneRtp(int index, TrackType type, int sample_rate, uint8_t *ptr, size_t len){
|
2021-07-14 21:39:30 +08:00
|
|
|
|
return _track[index].inputRtp(type, sample_rate, ptr, len).operator bool();
|
2021-06-25 16:24:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-07-12 21:18:22 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 设置ntp时间戳,在收到rtcp sender report时设置
|
2021-08-21 18:52:52 +08:00
|
|
|
|
* 如果rtp_stamp/sample_rate/ntp_stamp_ms都为0,那么采用rtp时间戳为ntp时间戳
|
2021-07-12 21:18:22 +08:00
|
|
|
|
* @param index track下标索引
|
|
|
|
|
|
* @param rtp_stamp rtp时间戳
|
|
|
|
|
|
* @param ntp_stamp_ms ntp时间戳
|
|
|
|
|
|
*/
|
2021-09-02 21:17:59 +08:00
|
|
|
|
void setNtpStamp(int index, uint32_t rtp_stamp, uint64_t ntp_stamp_ms){
|
|
|
|
|
|
_track[index].setNtpStamp(rtp_stamp, ntp_stamp_ms);
|
2021-07-12 21:18:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-06-25 16:24:44 +08:00
|
|
|
|
void clear() {
|
|
|
|
|
|
for (auto &track : _track) {
|
|
|
|
|
|
track.clear();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
size_t getJitterSize(int index) const {
|
|
|
|
|
|
return _track[index].getJitterSize();
|
|
|
|
|
|
}
|
2018-12-14 17:46:12 +08:00
|
|
|
|
|
2021-06-25 16:24:44 +08:00
|
|
|
|
size_t getCycleCount(int index) const {
|
|
|
|
|
|
return _track[index].getCycleCount();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
uint32_t getSSRC(int index) const {
|
|
|
|
|
|
return _track[index].getSSRC();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected:
|
2018-12-14 17:46:12 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* rtp数据包排序后输出
|
2019-06-24 17:01:15 +08:00
|
|
|
|
* @param rtp rtp数据包
|
|
|
|
|
|
* @param track_index track索引
|
2018-12-14 17:46:12 +08:00
|
|
|
|
*/
|
2021-06-25 16:24:44 +08:00
|
|
|
|
virtual void onRtpSorted(RtpPacket::Ptr rtp, int index) {}
|
2020-10-01 19:02:14 +08:00
|
|
|
|
|
2021-01-31 19:18:17 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 解析出rtp但还未排序
|
|
|
|
|
|
* @param rtp rtp数据包
|
|
|
|
|
|
* @param track_index track索引
|
|
|
|
|
|
*/
|
2021-06-25 16:24:44 +08:00
|
|
|
|
virtual void onBeforeRtpSorted(const RtpPacket::Ptr &rtp, int index) {}
|
2020-07-08 23:23:11 +08:00
|
|
|
|
|
2019-06-24 16:56:46 +08:00
|
|
|
|
private:
|
2021-06-25 16:24:44 +08:00
|
|
|
|
RtpTrackImp _track[kCount];
|
2018-12-14 17:46:12 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
2021-06-25 16:24:44 +08:00
|
|
|
|
using RtpReceiver = RtpMultiReceiver<2>;
|
|
|
|
|
|
|
2018-12-14 17:46:12 +08:00
|
|
|
|
}//namespace mediakit
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#endif //ZLMEDIAKIT_RTPRECEIVER_H
|