ZLMediaKit/src/Rtsp/RtpReceiver.cpp

116 lines
3.3 KiB
C++
Raw Normal View History

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/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 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) {
2021-02-05 11:28:50 +08:00
onRtpSorted(std::move(packet), index);
2020-10-01 19:02:14 +08:00
});
++index;
}
}
2021-01-31 19:19:24 +08:00
2018-12-14 17:46:12 +08:00
RtpReceiver::~RtpReceiver() {}
2021-01-31 19:19:24 +08:00
bool RtpReceiver::handleOneRtp(int index, TrackType type, int sample_rate, uint8_t *ptr, size_t len) {
if (len < RtpPacket::kRtpHeaderSize) {
WarnL << "rtp包太小:" << len;
2019-08-19 23:08:41 +08:00
return false;
}
2021-01-31 19:19:24 +08:00
if (len > RTP_MAX_SIZE) {
WarnL << "超大的rtp包:" << len << " > " << RTP_MAX_SIZE;
return false;
2020-10-24 23:32:43 +08:00
}
2021-01-31 19:19:24 +08:00
if (!sample_rate) {
2019-06-24 16:56:46 +08:00
//无法把时间戳转换成毫秒
2018-12-14 18:24:27 +08:00
return false;
}
2021-01-31 19:19:24 +08:00
RtpHeader *header = (RtpHeader *) ptr;
if (header->version != RtpPacket::kRtpVersion) {
throw BadRtpException("非法的rtpversion字段非法");
2018-12-14 17:46:12 +08:00
}
2021-01-31 19:19:24 +08:00
if (!header->getPayloadSize(len)) {
2020-12-19 19:47:43 +08:00
//无有效负载的rtp包
2019-04-24 11:40:54 +08:00
return false;
}
2021-01-31 19:19:24 +08:00
//比对缓存ssrc
auto ssrc = ntohl(header->ssrc);
if (!_ssrc[index]) {
2021-04-11 20:35:00 +08:00
//记录并锁定ssrc
2021-01-31 19:19:24 +08:00
_ssrc[index] = ssrc;
2021-04-11 20:35:00 +08:00
_ssrc_alive[index].resetTime();
} else if (_ssrc[index] == ssrc) {
//ssrc匹配正确,刷新计时器
_ssrc_alive[index].resetTime();
} else {
2021-01-31 19:19:24 +08:00
//ssrc错误
2021-06-08 20:05:45 +08:00
if (_ssrc_alive[index].elapsedTime() < 3 * 1000) {
2021-04-11 20:35:00 +08:00
//接受正确ssrc的rtp在10秒内那么我们认为存在多路rtp,忽略掉ssrc不匹配的rtp
2021-04-13 08:28:23 +08:00
WarnL << "ssrc不匹配,rtp已丢弃:" << ssrc << " != " << _ssrc[index];
2021-04-11 20:35:00 +08:00
return false;
}
InfoL << "rtp流ssrc切换:" << _ssrc[index] << " -> " << ssrc;
_ssrc[index] = ssrc;
_ssrc_alive[index].resetTime();
2019-06-24 16:56:46 +08:00
}
2021-02-05 11:28:50 +08:00
auto rtp = RtpPacket::create();
2021-01-31 19:19:24 +08:00
//需要添加4个字节的rtp over tcp头
rtp->setCapacity(RtpPacket::kRtpTcpHeaderSize + len);
rtp->setSize(RtpPacket::kRtpTcpHeaderSize + len);
rtp->sample_rate = sample_rate;
rtp->type = type;
//赋值4个字节的rtp over tcp头
uint8_t *data = (uint8_t *) rtp->data();
data[0] = '$';
data[1] = 2 * type;
data[2] = (len >> 8) & 0xFF;
data[3] = len & 0xFF;
//拷贝rtp
memcpy(&data[4], ptr, len);
onBeforeRtpSorted(rtp, index);
auto seq = rtp->getSeq();
_rtp_sortor[index].sortPacket(seq, std::move(rtp));
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);
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-31 19:19:24 +08:00
size_t RtpReceiver::getJitterSize(int index) const{
return _rtp_sortor[index].getJitterSize();
2019-05-09 13:35:54 +08:00
}
2021-01-31 19:19:24 +08:00
size_t RtpReceiver::getCycleCount(int index) const{
return _rtp_sortor[index].getCycleCount();
2019-05-09 13:35:54 +08:00
}
2021-01-31 19:19:24 +08:00
uint32_t RtpReceiver::getSSRC(int index) const{
return _ssrc[index];
2020-12-27 18:11:10 +08:00
}
2019-05-09 13:35:54 +08:00
2018-12-14 17:46:12 +08:00
}//namespace mediakit