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) {
|
2024-09-19 14:53:50 +08:00
|
|
|
|
// 无法把时间戳转换成毫秒 [AUTO-TRANSLATED:ec2d97b6]
|
|
|
|
|
|
// Unable to convert timestamp to milliseconds
|
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) {
|
2024-09-19 14:53:50 +08:00
|
|
|
|
// rtp有效负载小于0,非法 [AUTO-TRANSLATED:07eb3ec3]
|
|
|
|
|
|
// RTP payload is less than 0, illegal
|
2022-05-28 10:34:54 +08:00
|
|
|
|
throw BadRtpException("invalid rtp payload size");
|
2019-04-24 11:40:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-09-19 14:53:50 +08:00
|
|
|
|
// 比对缓存ssrc [AUTO-TRANSLATED:206cb66f]
|
|
|
|
|
|
// Compare cache ssrc
|
2021-01-31 19:19:24 +08:00
|
|
|
|
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) {
|
2024-09-19 14:53:50 +08:00
|
|
|
|
// 记录并锁定ssrc [AUTO-TRANSLATED:29452029]
|
|
|
|
|
|
// Record and lock ssrc
|
2021-06-25 16:24:44 +08:00
|
|
|
|
_ssrc = ssrc;
|
|
|
|
|
|
_ssrc_alive.resetTime();
|
|
|
|
|
|
} else if (_ssrc == ssrc) {
|
2024-09-19 14:53:50 +08:00
|
|
|
|
// ssrc匹配正确,刷新计时器 [AUTO-TRANSLATED:266518e6]
|
|
|
|
|
|
// SSRC matches correctly, refresh timer
|
2021-06-25 16:24:44 +08:00
|
|
|
|
_ssrc_alive.resetTime();
|
2021-04-11 20:35:00 +08:00
|
|
|
|
} else {
|
2024-09-19 14:53:50 +08:00
|
|
|
|
// ssrc错误 [AUTO-TRANSLATED:b967d497]
|
|
|
|
|
|
// SSRC error
|
2021-06-25 16:24:44 +08:00
|
|
|
|
if (_ssrc_alive.elapsedTime() < 3 * 1000) {
|
2024-09-19 14:53:50 +08:00
|
|
|
|
// 接收正确ssrc的rtp在10秒内,那么我们认为存在多路rtp,忽略掉ssrc不匹配的rtp [AUTO-TRANSLATED:2f98c2b5]
|
|
|
|
|
|
// If the RTP with the correct SSRC is received within 10 seconds, we consider it to be multi-path RTP, and ignore the RTP with mismatched SSRC
|
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();
|
2024-09-19 14:53:50 +08:00
|
|
|
|
// 需要添加4个字节的rtp over tcp头 [AUTO-TRANSLATED:a37d639b]
|
|
|
|
|
|
// Need to add 4 bytes of RTP over TCP header
|
2021-01-31 19:19:24 +08:00
|
|
|
|
rtp->setCapacity(RtpPacket::kRtpTcpHeaderSize + len);
|
|
|
|
|
|
rtp->setSize(RtpPacket::kRtpTcpHeaderSize + len);
|
|
|
|
|
|
rtp->sample_rate = sample_rate;
|
|
|
|
|
|
rtp->type = type;
|
|
|
|
|
|
|
2024-09-19 14:53:50 +08:00
|
|
|
|
// 赋值4个字节的rtp over tcp头 [AUTO-TRANSLATED:eeb990a9]
|
|
|
|
|
|
// Assign 4 bytes of RTP over TCP header
|
2021-01-31 19:19:24 +08:00
|
|
|
|
uint8_t *data = (uint8_t *) rtp->data();
|
|
|
|
|
|
data[0] = '$';
|
|
|
|
|
|
data[1] = 2 * type;
|
|
|
|
|
|
data[2] = (len >> 8) & 0xFF;
|
|
|
|
|
|
data[3] = len & 0xFF;
|
2024-09-19 14:53:50 +08:00
|
|
|
|
// 拷贝rtp [AUTO-TRANSLATED:3a2466c2]
|
|
|
|
|
|
// Copy RTP
|
2021-01-31 19:19:24 +08:00
|
|
|
|
memcpy(&data[4], ptr, len);
|
2021-08-21 18:52:52 +08:00
|
|
|
|
if (_disable_ntp) {
|
2024-09-19 14:53:50 +08:00
|
|
|
|
// 不支持ntp时间戳,例如国标推流,那么直接使用rtp时间戳 [AUTO-TRANSLATED:20085979]
|
|
|
|
|
|
// Does not support NTP timestamp, such as national standard streaming, so directly use RTP timestamp
|
2021-08-21 18:52:52 +08:00
|
|
|
|
rtp->ntp_stamp = rtp->getStamp() * uint64_t(1000) / sample_rate;
|
|
|
|
|
|
} else {
|
2024-09-19 14:53:50 +08:00
|
|
|
|
// 设置ntp时间戳 [AUTO-TRANSLATED:5e60d5cf]
|
|
|
|
|
|
// Set NTP timestamp
|
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
|