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
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "Common/config.h"
|
|
|
|
|
#include "RtpReceiver.h"
|
|
|
|
|
|
2019-06-24 16:56:46 +08:00
|
|
|
|
#define AV_RB16(x) \
|
2018-12-14 17:46:12 +08:00
|
|
|
|
((((const uint8_t*)(x))[0] << 8) | \
|
|
|
|
|
((const uint8_t*)(x))[1])
|
|
|
|
|
|
2019-06-24 16:56:46 +08:00
|
|
|
|
#define RTP_MAX_SIZE (10 * 1024)
|
2018-12-14 17:46:12 +08:00
|
|
|
|
|
|
|
|
|
namespace mediakit {
|
|
|
|
|
|
2020-10-01 19:02:14 +08:00
|
|
|
|
RtpReceiver::RtpReceiver() {
|
|
|
|
|
int index = 0;
|
|
|
|
|
for (auto &sortor : _rtp_sortor) {
|
2020-10-01 21:33:07 +08:00
|
|
|
|
sortor.setOnSort([this, index](uint16_t seq, RtpPacket::Ptr &packet) {
|
2020-10-01 19:02:14 +08:00
|
|
|
|
onRtpSorted(packet, index);
|
|
|
|
|
});
|
|
|
|
|
++index;
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-12-14 17:46:12 +08:00
|
|
|
|
RtpReceiver::~RtpReceiver() {}
|
|
|
|
|
|
2021-01-17 18:31:50 +08:00
|
|
|
|
bool RtpReceiver::handleOneRtp(int track_index, TrackType type, int samplerate, unsigned char *rtp_raw_ptr, size_t rtp_raw_len) {
|
2020-10-01 21:33:07 +08:00
|
|
|
|
if (rtp_raw_len < 12) {
|
2019-08-20 09:22:13 +08:00
|
|
|
|
WarnL << "rtp包太小:" << rtp_raw_len;
|
2019-08-19 23:08:41 +08:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2019-08-20 09:22:13 +08:00
|
|
|
|
|
2020-10-24 23:32:43 +08:00
|
|
|
|
uint32_t version = rtp_raw_ptr[0] >> 6;
|
2019-08-20 09:22:13 +08:00
|
|
|
|
uint8_t padding = 0;
|
2020-10-24 23:32:43 +08:00
|
|
|
|
uint8_t ext = rtp_raw_ptr[0] & 0x10;
|
|
|
|
|
uint8_t csrc = rtp_raw_ptr[0] & 0x0f;
|
|
|
|
|
|
2020-05-08 21:57:56 +08:00
|
|
|
|
if (rtp_raw_ptr[0] & 0x20) {
|
2019-08-20 09:22:13 +08:00
|
|
|
|
//获取padding大小
|
|
|
|
|
padding = rtp_raw_ptr[rtp_raw_len - 1];
|
|
|
|
|
//移除padding flag
|
2020-05-08 21:57:56 +08:00
|
|
|
|
rtp_raw_ptr[0] &= ~0x20;
|
2019-08-20 09:22:13 +08:00
|
|
|
|
//移除padding字节
|
|
|
|
|
rtp_raw_len -= padding;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-24 23:32:43 +08:00
|
|
|
|
if (version != 2) {
|
|
|
|
|
throw std::invalid_argument("非法的rtp,version != 2");
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-24 16:56:46 +08:00
|
|
|
|
auto rtp_ptr = _rtp_pool.obtain();
|
|
|
|
|
auto &rtp = *rtp_ptr;
|
2018-12-14 17:46:12 +08:00
|
|
|
|
|
2020-07-08 23:23:11 +08:00
|
|
|
|
rtp.type = type;
|
|
|
|
|
rtp.interleaved = 2 * type;
|
2019-06-24 16:56:46 +08:00
|
|
|
|
rtp.mark = rtp_raw_ptr[1] >> 7;
|
|
|
|
|
rtp.PT = rtp_raw_ptr[1] & 0x7F;
|
2019-08-20 09:22:13 +08:00
|
|
|
|
|
|
|
|
|
//序列号,内存对齐
|
|
|
|
|
memcpy(&rtp.sequence, rtp_raw_ptr + 2, 2);
|
2019-06-24 16:56:46 +08:00
|
|
|
|
rtp.sequence = ntohs(rtp.sequence);
|
2019-08-20 09:22:13 +08:00
|
|
|
|
|
|
|
|
|
//时间戳,内存对齐
|
|
|
|
|
memcpy(&rtp.timeStamp, rtp_raw_ptr + 4, 4);
|
|
|
|
|
rtp.timeStamp = ntohl(rtp.timeStamp);
|
2018-12-14 18:24:27 +08:00
|
|
|
|
|
2020-10-01 21:33:07 +08:00
|
|
|
|
if (!samplerate) {
|
2019-06-24 16:56:46 +08:00
|
|
|
|
//无法把时间戳转换成毫秒
|
2018-12-14 18:24:27 +08:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2018-12-14 17:46:12 +08:00
|
|
|
|
//时间戳转换成毫秒
|
2020-07-08 23:23:11 +08:00
|
|
|
|
rtp.timeStamp = rtp.timeStamp * 1000LL / samplerate;
|
2019-08-20 09:22:13 +08:00
|
|
|
|
|
|
|
|
|
//ssrc,内存对齐
|
|
|
|
|
memcpy(&rtp.ssrc, rtp_raw_ptr + 8, 4);
|
2019-06-24 16:56:46 +08:00
|
|
|
|
rtp.ssrc = ntohl(rtp.ssrc);
|
|
|
|
|
|
2020-07-08 23:23:11 +08:00
|
|
|
|
if (_ssrc[track_index] != rtp.ssrc) {
|
|
|
|
|
if (_ssrc[track_index] == 0) {
|
2019-06-24 16:56:46 +08:00
|
|
|
|
//保存SSRC至track对象
|
2020-07-08 23:23:11 +08:00
|
|
|
|
_ssrc[track_index] = rtp.ssrc;
|
2020-10-01 21:33:07 +08:00
|
|
|
|
} else {
|
2019-06-24 16:56:46 +08:00
|
|
|
|
//ssrc错误
|
2020-07-08 23:23:11 +08:00
|
|
|
|
WarnL << "ssrc错误:" << rtp.ssrc << " != " << _ssrc[track_index];
|
2019-06-24 16:56:46 +08:00
|
|
|
|
if (_ssrc_err_count[track_index]++ > 10) {
|
|
|
|
|
//ssrc切换后清除老数据
|
2020-07-08 23:23:11 +08:00
|
|
|
|
WarnL << "ssrc更换:" << _ssrc[track_index] << " -> " << rtp.ssrc;
|
2020-10-01 19:02:14 +08:00
|
|
|
|
_rtp_sortor[track_index].clear();
|
2020-07-08 23:23:11 +08:00
|
|
|
|
_ssrc[track_index] = rtp.ssrc;
|
2019-06-24 16:56:46 +08:00
|
|
|
|
}
|
|
|
|
|
return false;
|
2018-12-14 17:46:12 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-06-24 16:56:46 +08:00
|
|
|
|
|
|
|
|
|
//ssrc匹配正确,不匹配计数清零
|
|
|
|
|
_ssrc_err_count[track_index] = 0;
|
2018-12-14 17:46:12 +08:00
|
|
|
|
|
2020-12-06 21:08:16 +08:00
|
|
|
|
//rtp 12个固定字节头
|
|
|
|
|
rtp.offset = 12;
|
|
|
|
|
//rtp有csrc
|
2020-10-01 21:33:07 +08:00
|
|
|
|
rtp.offset += 4 * csrc;
|
2020-12-06 21:08:16 +08:00
|
|
|
|
if (ext) {
|
|
|
|
|
//rtp有ext
|
|
|
|
|
uint16_t reserved = AV_RB16(rtp_raw_ptr + rtp.offset);
|
|
|
|
|
uint16_t extlen = AV_RB16(rtp_raw_ptr + rtp.offset + 2) << 2;
|
|
|
|
|
rtp.offset += extlen + 4;
|
2018-12-14 17:46:12 +08:00
|
|
|
|
}
|
2019-04-24 11:40:54 +08:00
|
|
|
|
|
2020-12-06 21:08:16 +08:00
|
|
|
|
if (rtp_raw_len <= rtp.offset) {
|
2020-12-19 19:47:43 +08:00
|
|
|
|
//无有效负载的rtp包
|
2019-04-24 11:40:54 +08:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-01 21:33:07 +08:00
|
|
|
|
if (rtp_raw_len > RTP_MAX_SIZE) {
|
2019-08-20 09:22:13 +08:00
|
|
|
|
WarnL << "超大的rtp包:" << rtp_raw_len << " > " << RTP_MAX_SIZE;
|
2019-06-24 16:56:46 +08:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//设置rtp负载长度
|
2019-08-20 09:22:13 +08:00
|
|
|
|
rtp.setCapacity(rtp_raw_len + 4);
|
|
|
|
|
rtp.setSize(rtp_raw_len + 4);
|
2020-10-01 21:33:07 +08:00
|
|
|
|
uint8_t *payload_ptr = (uint8_t *) rtp.data();
|
2019-06-24 16:07:44 +08:00
|
|
|
|
payload_ptr[0] = '$';
|
2019-06-24 16:56:46 +08:00
|
|
|
|
payload_ptr[1] = rtp.interleaved;
|
2021-01-17 18:31:50 +08:00
|
|
|
|
payload_ptr[2] = (rtp_raw_len >> 8) & 0xFF;
|
|
|
|
|
payload_ptr[3] = rtp_raw_len & 0xFF;
|
2020-12-06 21:08:16 +08:00
|
|
|
|
//添加rtp over tcp前4个字节的偏移量
|
|
|
|
|
rtp.offset += 4;
|
2019-06-24 16:07:44 +08:00
|
|
|
|
//拷贝rtp负载
|
2019-06-24 16:56:46 +08:00
|
|
|
|
memcpy(payload_ptr + 4, rtp_raw_ptr, rtp_raw_len);
|
|
|
|
|
//排序rtp
|
2020-10-24 23:28:56 +08:00
|
|
|
|
auto seq = rtp_ptr->sequence;
|
|
|
|
|
_rtp_sortor[track_index].sortPacket(seq, std::move(rtp_ptr));
|
2019-06-24 16:56:46 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
2018-12-14 17:46:12 +08:00
|
|
|
|
|
|
|
|
|
void RtpReceiver::clear() {
|
2020-07-08 23:23:11 +08:00
|
|
|
|
CLEAR_ARR(_ssrc);
|
|
|
|
|
CLEAR_ARR(_ssrc_err_count);
|
2020-10-01 19:02:14 +08:00
|
|
|
|
for (auto &sortor : _rtp_sortor) {
|
|
|
|
|
sortor.clear();
|
|
|
|
|
}
|
2018-12-14 17:46:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-01-17 18:31:50 +08:00
|
|
|
|
void RtpReceiver::setPoolSize(size_t size) {
|
2019-06-24 16:56:46 +08:00
|
|
|
|
_rtp_pool.setSize(size);
|
2018-12-14 17:46:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-01-17 18:31:50 +08:00
|
|
|
|
size_t RtpReceiver::getJitterSize(int track_index) const{
|
2020-10-01 19:02:14 +08:00
|
|
|
|
return _rtp_sortor[track_index].getJitterSize();
|
2019-05-09 13:35:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-01-17 18:31:50 +08:00
|
|
|
|
size_t RtpReceiver::getCycleCount(int track_index) const{
|
2020-10-01 19:02:14 +08:00
|
|
|
|
return _rtp_sortor[track_index].getCycleCount();
|
2019-05-09 13:35:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-12-27 18:11:10 +08:00
|
|
|
|
uint32_t RtpReceiver::getSSRC(int track_index) const{
|
|
|
|
|
return _ssrc[track_index];
|
|
|
|
|
}
|
2019-05-09 13:35:54 +08:00
|
|
|
|
|
2018-12-14 17:46:12 +08:00
|
|
|
|
}//namespace mediakit
|