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
|
|
|
|
*
|
|
|
|
|
|
* This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
|
|
|
|
|
|
*
|
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"
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
using namespace toolkit;
|
|
|
|
|
|
|
|
|
|
|
|
namespace mediakit {
|
|
|
|
|
|
|
2020-10-01 19:02:14 +08:00
|
|
|
|
template<typename T, typename SEQ = uint16_t>
|
|
|
|
|
|
class PacketSortor {
|
|
|
|
|
|
public:
|
|
|
|
|
|
PacketSortor() = default;
|
|
|
|
|
|
~PacketSortor() = default;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 设置参数
|
|
|
|
|
|
* @param max_sort_size 最大排序缓存长度
|
|
|
|
|
|
* @param clear_sort_size seq连续次数超过该值后,清空并关闭排序缓存
|
|
|
|
|
|
*/
|
|
|
|
|
|
void setup(uint32_t max_sort_size, uint32_t clear_sort_size) {
|
|
|
|
|
|
_max_sort_size = max_sort_size;
|
|
|
|
|
|
_clear_sort_size = clear_sort_size;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void setOnSort(function<void(SEQ seq, const T &packet)> cb){
|
|
|
|
|
|
_cb = std::move(cb);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 清空状态
|
|
|
|
|
|
*/
|
|
|
|
|
|
void clear() {
|
|
|
|
|
|
_last_seq = 0;
|
|
|
|
|
|
_seq_ok_count = 0;
|
|
|
|
|
|
_sort_started = 0;
|
|
|
|
|
|
_seq_cycle_count = 0;
|
|
|
|
|
|
_rtp_sort_cache_map.clear();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 获取排序缓存长度
|
|
|
|
|
|
*/
|
|
|
|
|
|
int getJitterSize(){
|
|
|
|
|
|
return _rtp_sort_cache_map.size();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 获取seq回环次数
|
|
|
|
|
|
*/
|
|
|
|
|
|
int getCycleCount(){
|
|
|
|
|
|
return _seq_cycle_count;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 输入并排序
|
|
|
|
|
|
* @param seq 序列号
|
|
|
|
|
|
* @param packet 包负载
|
|
|
|
|
|
*/
|
|
|
|
|
|
void sortPacket(SEQ seq, const T &packet){
|
|
|
|
|
|
if (seq != _last_seq + 1 && _last_seq != 0) {
|
|
|
|
|
|
//包乱序或丢包
|
|
|
|
|
|
_seq_ok_count = 0;
|
|
|
|
|
|
_sort_started = true;
|
|
|
|
|
|
if (_last_seq > seq && _last_seq - seq > 0xFF) {
|
|
|
|
|
|
//sequence回环,清空所有排序缓存
|
|
|
|
|
|
while (_rtp_sort_cache_map.size()) {
|
|
|
|
|
|
popPacket();
|
|
|
|
|
|
}
|
|
|
|
|
|
++_seq_cycle_count;
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
//正确序列的包
|
|
|
|
|
|
_seq_ok_count++;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
_last_seq = seq;
|
|
|
|
|
|
|
|
|
|
|
|
//开始排序缓存
|
|
|
|
|
|
if (_sort_started) {
|
|
|
|
|
|
_rtp_sort_cache_map.emplace(seq, packet);
|
|
|
|
|
|
if (_seq_ok_count >= _clear_sort_size) {
|
|
|
|
|
|
//网络环境改善,需要清空排序缓存
|
|
|
|
|
|
_seq_ok_count = 0;
|
|
|
|
|
|
_sort_started = false;
|
|
|
|
|
|
while (_rtp_sort_cache_map.size()) {
|
|
|
|
|
|
popPacket();
|
|
|
|
|
|
}
|
|
|
|
|
|
} else if (_rtp_sort_cache_map.size() >= _max_sort_size) {
|
|
|
|
|
|
//排序缓存溢出
|
|
|
|
|
|
popPacket();
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
//正确序列
|
|
|
|
|
|
onPacketSorted(seq, packet);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
void popPacket() {
|
|
|
|
|
|
auto it = _rtp_sort_cache_map.begin();
|
|
|
|
|
|
onPacketSorted(it->first, it->second);
|
|
|
|
|
|
_rtp_sort_cache_map.erase(it);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void onPacketSorted(SEQ seq, const T &packet) {
|
|
|
|
|
|
_cb(seq, packet);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
//是否开始seq排序
|
|
|
|
|
|
bool _sort_started = false;
|
|
|
|
|
|
//上次seq
|
|
|
|
|
|
SEQ _last_seq = 0;
|
|
|
|
|
|
//seq连续次数计数
|
|
|
|
|
|
uint32_t _seq_ok_count = 0;
|
|
|
|
|
|
//seq回环次数计数
|
|
|
|
|
|
uint32_t _seq_cycle_count = 0;
|
|
|
|
|
|
//排序缓存长度
|
|
|
|
|
|
uint32_t _max_sort_size;
|
|
|
|
|
|
//seq连续次数超过该值后,清空并关闭排序缓存
|
|
|
|
|
|
uint32_t _clear_sort_size;
|
|
|
|
|
|
//rtp排序缓存,根据seq排序
|
|
|
|
|
|
map<SEQ, T> _rtp_sort_cache_map;
|
|
|
|
|
|
//回调
|
|
|
|
|
|
function<void(SEQ seq, const T &packet)> _cb;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2018-12-14 17:46:12 +08:00
|
|
|
|
class RtpReceiver {
|
|
|
|
|
|
public:
|
|
|
|
|
|
RtpReceiver();
|
|
|
|
|
|
virtual ~RtpReceiver();
|
|
|
|
|
|
|
2020-10-01 19:02:14 +08:00
|
|
|
|
protected:
|
2018-12-14 17:46:12 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 输入数据指针生成并排序rtp包
|
2019-06-24 16:56:46 +08:00
|
|
|
|
* @param track_index track下标索引
|
2020-07-08 23:23:11 +08:00
|
|
|
|
* @param type track类型
|
|
|
|
|
|
* @param samplerate rtp时间戳基准时钟,视频为90000,音频为采样率
|
2019-06-24 16:56:46 +08:00
|
|
|
|
* @param rtp_raw_ptr rtp数据指针
|
|
|
|
|
|
* @param rtp_raw_len rtp数据指针长度
|
2018-12-14 17:46:12 +08:00
|
|
|
|
* @return 解析成功返回true
|
|
|
|
|
|
*/
|
2020-07-08 23:23:11 +08:00
|
|
|
|
bool handleOneRtp(int track_index, TrackType type, int samplerate, unsigned char *rtp_raw_ptr, unsigned int rtp_raw_len);
|
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
|
|
|
|
*/
|
2019-06-24 17:01:15 +08:00
|
|
|
|
virtual void onRtpSorted(const RtpPacket::Ptr &rtp, int track_index){}
|
2020-10-01 19:02:14 +08:00
|
|
|
|
|
2018-12-14 17:46:12 +08:00
|
|
|
|
void clear();
|
|
|
|
|
|
void setPoolSize(int size);
|
2019-06-24 17:01:15 +08:00
|
|
|
|
int getJitterSize(int track_index);
|
|
|
|
|
|
int getCycleCount(int track_index);
|
2020-07-08 23:23:11 +08:00
|
|
|
|
|
2018-12-14 17:46:12 +08:00
|
|
|
|
private:
|
2019-06-24 16:56:46 +08:00
|
|
|
|
void sortRtp(const RtpPacket::Ptr &rtp , int track_index);
|
2020-07-08 23:23:11 +08:00
|
|
|
|
|
2019-06-24 16:56:46 +08:00
|
|
|
|
private:
|
2020-07-08 23:23:11 +08:00
|
|
|
|
uint32_t _ssrc[2] = { 0, 0 };
|
2019-06-24 16:56:46 +08:00
|
|
|
|
//ssrc不匹配计数
|
|
|
|
|
|
uint32_t _ssrc_err_count[2] = { 0, 0 };
|
|
|
|
|
|
//rtp排序缓存,根据seq排序
|
2020-10-01 19:02:14 +08:00
|
|
|
|
PacketSortor<RtpPacket::Ptr> _rtp_sortor[2];
|
2019-06-24 16:56:46 +08:00
|
|
|
|
//rtp循环池
|
|
|
|
|
|
RtspMediaSource::PoolType _rtp_pool;
|
2018-12-14 17:46:12 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
}//namespace mediakit
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#endif //ZLMEDIAKIT_RTPRECEIVER_H
|