2018-12-14 17:46:12 +08:00
|
|
|
|
/*
|
2023-12-09 16:23:51 +08:00
|
|
|
|
* Copyright (c) 2016-present The ZLMediaKit project authors. All Rights Reserved.
|
2018-12-14 17:46:12 +08:00
|
|
|
|
*
|
2023-12-09 16:23:51 +08:00
|
|
|
|
* This file is part of ZLMediaKit(https://github.com/ZLMediaKit/ZLMediaKit).
|
2018-12-14 17:46:12 +08:00
|
|
|
|
*
|
2023-12-09 16:23:51 +08:00
|
|
|
|
* Use of this source code is governed by MIT-like license that can be found in the
|
2020-04-04 20:30:09 +08:00
|
|
|
|
* 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"
|
|
|
|
|
|
|
|
|
|
namespace mediakit {
|
|
|
|
|
|
2021-06-25 16:24:44 +08:00
|
|
|
|
RtpTrack::RtpTrack() {
|
2023-03-05 23:24:55 +08:00
|
|
|
|
setOnSort([this](uint16_t seq, RtpPacket::Ptr packet) {
|
2022-05-28 10:34:54 +08:00
|
|
|
|
onRtpSorted(std::move(packet));
|
2021-06-25 16:24:44 +08:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint32_t RtpTrack::getSSRC() const {
|
|
|
|
|
return _ssrc;
|
2020-10-01 19:02:14 +08:00
|
|
|
|
}
|
2021-01-31 19:19:24 +08:00
|
|
|
|
|
2021-06-25 16:24:44 +08:00
|
|
|
|
void RtpTrack::clear() {
|
|
|
|
|
_ssrc = 0;
|
|
|
|
|
_ssrc_alive.resetTime();
|
|
|
|
|
PacketSortor<RtpPacket::Ptr>::clear();
|
|
|
|
|
}
|
2018-12-14 17:46:12 +08:00
|
|
|
|
|
2021-07-14 21:39:30 +08:00
|
|
|
|
RtpPacket::Ptr RtpTrack::inputRtp(TrackType type, int sample_rate, uint8_t *ptr, size_t len) {
|
2021-01-31 19:19:24 +08:00
|
|
|
|
if (len < RtpPacket::kRtpHeaderSize) {
|
2022-05-28 10:34:54 +08:00
|
|
|
|
throw BadRtpException("rtp size less than 12");
|
2019-08-19 23:08:41 +08:00
|
|
|
|
}
|
2021-08-11 15:48:15 +08:00
|
|
|
|
GET_CONFIG(uint32_t, rtpMaxSize, Rtp::kRtpMaxSize);
|
|
|
|
|
if (len > 1024 * rtpMaxSize) {
|
|
|
|
|
WarnL << "超大的rtp包:" << len << " > " << 1024 * rtpMaxSize;
|
2021-07-14 21:39:30 +08:00
|
|
|
|
return nullptr;
|
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
|
|
|
|
//无法把时间戳转换成毫秒
|
2021-07-14 21:39:30 +08:00
|
|
|
|
return nullptr;
|
2018-12-14 18:24:27 +08:00
|
|
|
|
}
|
2021-01-31 19:19:24 +08:00
|
|
|
|
RtpHeader *header = (RtpHeader *) ptr;
|
|
|
|
|
if (header->version != RtpPacket::kRtpVersion) {
|
2022-05-28 10:34:54 +08:00
|
|
|
|
throw BadRtpException("invalid rtp version");
|
2018-12-14 17:46:12 +08:00
|
|
|
|
}
|
2022-05-28 10:34:54 +08:00
|
|
|
|
if (header->getPayloadSize(len) < 0) {
|
|
|
|
|
//rtp有效负载小于0,非法
|
|
|
|
|
throw BadRtpException("invalid rtp payload size");
|
2019-04-24 11:40:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-01-31 19:19:24 +08:00
|
|
|
|
//比对缓存ssrc
|
|
|
|
|
auto ssrc = ntohl(header->ssrc);
|
|
|
|
|
|
2022-02-22 16:53:05 +08:00
|
|
|
|
if (_pt == 0xFF) {
|
|
|
|
|
_pt = header->pt;
|
|
|
|
|
} else if (header->pt != _pt) {
|
2022-05-28 10:34:54 +08:00
|
|
|
|
//TraceL << "rtp pt mismatch:" << (int) header->pt << " !=" << (int) _pt;
|
2022-02-22 16:53:05 +08:00
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-25 16:24:44 +08:00
|
|
|
|
if (!_ssrc) {
|
2021-04-11 20:35:00 +08:00
|
|
|
|
//记录并锁定ssrc
|
2021-06-25 16:24:44 +08:00
|
|
|
|
_ssrc = ssrc;
|
|
|
|
|
_ssrc_alive.resetTime();
|
|
|
|
|
} else if (_ssrc == ssrc) {
|
2021-04-11 20:35:00 +08:00
|
|
|
|
//ssrc匹配正确,刷新计时器
|
2021-06-25 16:24:44 +08:00
|
|
|
|
_ssrc_alive.resetTime();
|
2021-04-11 20:35:00 +08:00
|
|
|
|
} else {
|
2021-01-31 19:19:24 +08:00
|
|
|
|
//ssrc错误
|
2021-06-25 16:24:44 +08:00
|
|
|
|
if (_ssrc_alive.elapsedTime() < 3 * 1000) {
|
2023-07-09 10:25:10 +08:00
|
|
|
|
//接收正确ssrc的rtp在10秒内,那么我们认为存在多路rtp,忽略掉ssrc不匹配的rtp
|
2022-05-28 10:34:54 +08:00
|
|
|
|
WarnL << "ssrc mismatch, rtp dropped:" << ssrc << " != " << _ssrc;
|
2021-07-14 21:39:30 +08:00
|
|
|
|
return nullptr;
|
2021-04-11 20:35:00 +08:00
|
|
|
|
}
|
2022-05-28 10:34:54 +08:00
|
|
|
|
InfoL << "rtp ssrc changed:" << _ssrc << " -> " << ssrc;
|
2021-06-25 16:24:44 +08:00
|
|
|
|
_ssrc = ssrc;
|
|
|
|
|
_ssrc_alive.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);
|
2021-08-21 18:52:52 +08:00
|
|
|
|
if (_disable_ntp) {
|
|
|
|
|
//不支持ntp时间戳,例如国标推流,那么直接使用rtp时间戳
|
|
|
|
|
rtp->ntp_stamp = rtp->getStamp() * uint64_t(1000) / sample_rate;
|
|
|
|
|
} else {
|
|
|
|
|
//设置ntp时间戳
|
2021-10-13 15:52:12 +08:00
|
|
|
|
rtp->ntp_stamp = _ntp_stamp.getNtpStamp(rtp->getStamp(), sample_rate);
|
2021-08-21 18:52:52 +08:00
|
|
|
|
}
|
2021-06-25 16:24:44 +08:00
|
|
|
|
onBeforeRtpSorted(rtp);
|
2021-07-14 21:39:30 +08:00
|
|
|
|
sortPacket(rtp->getSeq(), rtp);
|
|
|
|
|
return rtp;
|
2019-06-24 16:56:46 +08:00
|
|
|
|
}
|
2018-12-14 17:46:12 +08:00
|
|
|
|
|
2021-09-02 21:17:59 +08:00
|
|
|
|
void RtpTrack::setNtpStamp(uint32_t rtp_stamp, uint64_t ntp_stamp_ms) {
|
|
|
|
|
_disable_ntp = rtp_stamp == 0 && ntp_stamp_ms == 0;
|
|
|
|
|
if (!_disable_ntp) {
|
|
|
|
|
_ntp_stamp.setNtpStamp(rtp_stamp, ntp_stamp_ms);
|
2021-08-22 15:13:20 +08:00
|
|
|
|
}
|
2021-07-12 21:18:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-03-06 20:43:07 +08:00
|
|
|
|
void RtpTrack::setPayloadType(uint8_t pt) {
|
2022-07-17 00:26:07 +08:00
|
|
|
|
_pt = pt;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-25 16:24:44 +08:00
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
void RtpTrackImp::setOnSorted(OnSorted cb) {
|
|
|
|
|
_on_sorted = std::move(cb);
|
2018-12-14 17:46:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-25 16:24:44 +08:00
|
|
|
|
void RtpTrackImp::setBeforeSorted(BeforeSorted cb) {
|
|
|
|
|
_on_before_sorted = std::move(cb);
|
2019-05-09 13:35:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-25 16:24:44 +08:00
|
|
|
|
void RtpTrackImp::onRtpSorted(RtpPacket::Ptr rtp) {
|
|
|
|
|
if (_on_sorted) {
|
|
|
|
|
_on_sorted(std::move(rtp));
|
|
|
|
|
}
|
2019-05-09 13:35:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-25 16:24:44 +08:00
|
|
|
|
void RtpTrackImp::onBeforeRtpSorted(const RtpPacket::Ptr &rtp) {
|
|
|
|
|
if (_on_before_sorted) {
|
|
|
|
|
_on_before_sorted(rtp);
|
|
|
|
|
}
|
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
|