2019-12-05 19:20:12 +08:00
|
|
|
|
/*
|
2020-04-04 20:30:09 +08:00
|
|
|
|
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
|
2019-12-06 11:54:10 +08:00
|
|
|
|
*
|
2021-01-17 18:31:50 +08:00
|
|
|
|
* This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
|
2019-12-06 11:54:10 +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.
|
2019-12-06 11:54:10 +08:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#if defined(ENABLE_RTPPROXY)
|
2019-12-05 19:20:12 +08:00
|
|
|
|
#include "RtpSession.h"
|
|
|
|
|
#include "RtpSelector.h"
|
2020-07-07 10:01:12 +08:00
|
|
|
|
#include "Network/TcpServer.h"
|
2021-04-22 22:02:21 +08:00
|
|
|
|
#include "Rtsp/RtpReceiver.h"
|
2022-02-02 20:34:50 +08:00
|
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
using namespace toolkit;
|
|
|
|
|
|
2019-12-06 11:54:10 +08:00
|
|
|
|
namespace mediakit{
|
2019-12-05 19:20:12 +08:00
|
|
|
|
|
2020-07-07 10:01:12 +08:00
|
|
|
|
const string RtpSession::kStreamID = "stream_id";
|
2021-06-08 14:03:25 +08:00
|
|
|
|
const string RtpSession::kIsUDP = "is_udp";
|
2022-04-16 15:12:49 +08:00
|
|
|
|
const string RtpSession::kSSRC = "ssrc";
|
2020-07-07 10:01:12 +08:00
|
|
|
|
|
2021-06-08 11:29:32 +08:00
|
|
|
|
void RtpSession::attachServer(const Server &server) {
|
|
|
|
|
_stream_id = const_cast<Server &>(server)[kStreamID];
|
2021-06-08 14:03:25 +08:00
|
|
|
|
_is_udp = const_cast<Server &>(server)[kIsUDP];
|
2022-04-16 15:12:49 +08:00
|
|
|
|
_ssrc = const_cast<Server &>(server)[kSSRC];
|
2021-06-08 14:03:25 +08:00
|
|
|
|
|
|
|
|
|
if (_is_udp) {
|
|
|
|
|
//设置udp socket读缓存
|
|
|
|
|
SockUtil::setRecvBuf(getSock()->rawFD(), 4 * 1024 * 1024);
|
|
|
|
|
_statistic_udp = std::make_shared<ObjectStatistic<UdpSession> >();
|
|
|
|
|
} else {
|
|
|
|
|
_statistic_tcp = std::make_shared<ObjectStatistic<TcpSession> >();
|
|
|
|
|
}
|
2020-07-07 10:01:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-08 14:03:25 +08:00
|
|
|
|
RtpSession::RtpSession(const Socket::Ptr &sock) : Session(sock) {
|
2019-12-05 19:20:12 +08:00
|
|
|
|
DebugP(this);
|
2021-06-08 14:03:25 +08:00
|
|
|
|
socklen_t addr_len = sizeof(_addr);
|
2022-05-08 00:26:01 +08:00
|
|
|
|
getpeername(sock->rawFD(), (struct sockaddr *)&_addr, &addr_len);
|
2019-12-05 19:20:12 +08:00
|
|
|
|
}
|
2021-06-08 14:03:25 +08:00
|
|
|
|
|
2019-12-05 19:20:12 +08:00
|
|
|
|
RtpSession::~RtpSession() {
|
|
|
|
|
DebugP(this);
|
2020-02-23 12:16:20 +08:00
|
|
|
|
if(_process){
|
2020-07-07 10:01:12 +08:00
|
|
|
|
RtpSelector::Instance().delProcess(_stream_id,_process.get());
|
2019-12-05 19:20:12 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RtpSession::onRecv(const Buffer::Ptr &data) {
|
2021-11-19 15:26:33 +08:00
|
|
|
|
if (_is_udp) {
|
|
|
|
|
onRtpPacket(data->data(), data->size());
|
|
|
|
|
return;
|
2019-12-05 19:20:12 +08:00
|
|
|
|
}
|
2021-11-19 15:26:33 +08:00
|
|
|
|
RtpSplitter::input(data->data(), data->size());
|
2019-12-05 19:20:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RtpSession::onError(const SockException &err) {
|
2022-07-29 17:57:52 +08:00
|
|
|
|
WarnP(this) << _stream_id << " " << err.what();
|
2019-12-05 19:20:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RtpSession::onManager() {
|
|
|
|
|
if(_process && !_process->alive()){
|
|
|
|
|
shutdown(SockException(Err_timeout, "receive rtp timeout"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(!_process && _ticker.createdTime() > 10 * 1000){
|
|
|
|
|
shutdown(SockException(Err_timeout, "illegal connection"));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-17 18:31:50 +08:00
|
|
|
|
void RtpSession::onRtpPacket(const char *data, size_t len) {
|
2021-06-08 14:03:25 +08:00
|
|
|
|
if (!_is_udp) {
|
|
|
|
|
if (_search_rtp) {
|
|
|
|
|
//搜索上下文期间,数据丢弃
|
|
|
|
|
if (_search_rtp_finished) {
|
|
|
|
|
//下个包开始就是正确的rtp包了
|
|
|
|
|
_search_rtp_finished = false;
|
|
|
|
|
_search_rtp = false;
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-08-11 15:48:15 +08:00
|
|
|
|
GET_CONFIG(uint32_t, rtpMaxSize, Rtp::kRtpMaxSize);
|
|
|
|
|
if (len > 1024 * rtpMaxSize) {
|
2021-06-08 14:03:25 +08:00
|
|
|
|
_search_rtp = true;
|
|
|
|
|
WarnL << "rtp包长度异常(" << len << "),发送端可能缓存溢出并覆盖,开始搜索ssrc以便恢复上下文";
|
|
|
|
|
return;
|
2021-04-22 22:02:21 +08:00
|
|
|
|
}
|
2020-10-18 23:05:24 +08:00
|
|
|
|
}
|
2020-02-23 12:16:20 +08:00
|
|
|
|
if (!_process) {
|
2022-04-16 15:12:49 +08:00
|
|
|
|
//未设置ssrc时,尝试获取ssrc
|
|
|
|
|
if (!_ssrc && !RtpSelector::getSSRC(data, len, _ssrc)) {
|
2020-02-23 12:16:20 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
2020-07-07 10:01:12 +08:00
|
|
|
|
if (_stream_id.empty()) {
|
|
|
|
|
//未指定流id就使用ssrc为流id
|
2021-04-22 22:02:21 +08:00
|
|
|
|
_stream_id = printSSRC(_ssrc);
|
2020-07-07 10:01:12 +08:00
|
|
|
|
}
|
|
|
|
|
//tcp情况下,一个tcp链接只可能是一路流,不需要通过多个ssrc来区分,所以不需要频繁getProcess
|
|
|
|
|
_process = RtpSelector::Instance().getProcess(_stream_id, true);
|
2020-02-28 16:25:14 +08:00
|
|
|
|
_process->setListener(dynamic_pointer_cast<RtpSession>(shared_from_this()));
|
2019-12-05 19:20:12 +08:00
|
|
|
|
}
|
2021-04-22 22:02:21 +08:00
|
|
|
|
try {
|
2022-04-16 15:12:49 +08:00
|
|
|
|
uint32_t rtp_ssrc = 0;
|
|
|
|
|
RtpSelector::getSSRC(data, len, rtp_ssrc);
|
|
|
|
|
if (rtp_ssrc != _ssrc) {
|
|
|
|
|
WarnP(this) << "ssrc不匹配,rtp已丢弃:" << rtp_ssrc << " != " << _ssrc;
|
|
|
|
|
return;
|
|
|
|
|
}
|
2022-05-08 00:26:01 +08:00
|
|
|
|
_process->inputRtp(false, getSock(), data, len, (struct sockaddr *)&_addr);
|
2021-06-25 16:24:44 +08:00
|
|
|
|
} catch (RtpTrack::BadRtpException &ex) {
|
2021-06-08 14:03:25 +08:00
|
|
|
|
if (!_is_udp) {
|
|
|
|
|
WarnL << ex.what() << ",开始搜索ssrc以便恢复上下文";
|
|
|
|
|
_search_rtp = true;
|
|
|
|
|
} else {
|
|
|
|
|
throw;
|
|
|
|
|
}
|
2021-04-22 22:02:21 +08:00
|
|
|
|
} catch (...) {
|
|
|
|
|
throw;
|
|
|
|
|
}
|
2019-12-05 19:20:12 +08:00
|
|
|
|
_ticker.resetTime();
|
|
|
|
|
}
|
2019-12-06 11:54:10 +08:00
|
|
|
|
|
2020-02-28 16:25:14 +08:00
|
|
|
|
bool RtpSession::close(MediaSource &sender, bool force) {
|
|
|
|
|
//此回调在其他线程触发
|
2021-01-17 20:15:08 +08:00
|
|
|
|
if(!_process || (!force && _process->getTotalReaderCount())){
|
2020-02-28 16:25:14 +08:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
string err = StrPrinter << "close media:" << sender.getSchema() << "/" << sender.getVhost() << "/" << sender.getApp() << "/" << sender.getId() << " " << force;
|
|
|
|
|
safeShutdown(SockException(Err_shutdown,err));
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int RtpSession::totalReaderCount(MediaSource &sender) {
|
|
|
|
|
//此回调在其他线程触发
|
2021-01-17 20:15:08 +08:00
|
|
|
|
return _process ? _process->getTotalReaderCount() : sender.totalReaderCount();
|
2020-02-28 16:25:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-22 22:02:21 +08:00
|
|
|
|
static const char *findSSRC(const char *data, ssize_t len, uint32_t ssrc) {
|
|
|
|
|
//rtp前面必须预留两个字节的长度字段
|
|
|
|
|
for (ssize_t i = 2; i <= len - 4; ++i) {
|
|
|
|
|
auto ptr = (const uint8_t *) data + i;
|
|
|
|
|
if (ptr[0] == (ssrc >> 24) && ptr[1] == ((ssrc >> 16) & 0xFF) &&
|
|
|
|
|
ptr[2] == ((ssrc >> 8) & 0xFF) && ptr[3] == (ssrc & 0xFF)) {
|
|
|
|
|
return (const char *) ptr;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//rtp长度到ssrc间的长度固定为10
|
|
|
|
|
static size_t constexpr kSSRCOffset = 2 + 4 + 4;
|
|
|
|
|
|
|
|
|
|
const char *RtpSession::onSearchPacketTail(const char *data, size_t len) {
|
|
|
|
|
if (!_search_rtp) {
|
|
|
|
|
//tcp上下文正常,不用搜索ssrc
|
|
|
|
|
return RtpSplitter::onSearchPacketTail(data, len);
|
|
|
|
|
}
|
|
|
|
|
if (!_process) {
|
|
|
|
|
throw SockException(Err_shutdown, "ssrc未获取到,无法通过ssrc恢复tcp上下文");
|
|
|
|
|
}
|
|
|
|
|
//搜索第一个rtp的ssrc
|
|
|
|
|
auto ssrc_ptr0 = findSSRC(data, len, _ssrc);
|
|
|
|
|
if (!ssrc_ptr0) {
|
|
|
|
|
//未搜索到任意rtp,返回数据不够
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
//这两个字节是第一个rtp的长度字段
|
|
|
|
|
auto rtp_len_ptr = (ssrc_ptr0 - kSSRCOffset);
|
|
|
|
|
auto rtp_len = ((uint8_t *)rtp_len_ptr)[0] << 8 | ((uint8_t *)rtp_len_ptr)[1];
|
|
|
|
|
|
|
|
|
|
//搜索第二个rtp的ssrc
|
|
|
|
|
auto ssrc_ptr1 = findSSRC(ssrc_ptr0 + rtp_len, data + (ssize_t) len - ssrc_ptr0 - rtp_len, _ssrc);
|
|
|
|
|
if (!ssrc_ptr1) {
|
|
|
|
|
//未搜索到第二个rtp,返回数据不够
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//两个ssrc的间隔正好等于rtp的长度(外加rtp长度字段),那么说明找到rtp
|
|
|
|
|
auto ssrc_offset = ssrc_ptr1 - ssrc_ptr0;
|
|
|
|
|
if (ssrc_offset == rtp_len + 2 || ssrc_offset == rtp_len + 4) {
|
|
|
|
|
InfoL << "rtp搜索成功,tcp上下文恢复成功,丢弃的rtp残余数据为:" << rtp_len_ptr - data;
|
|
|
|
|
_search_rtp_finished = true;
|
2021-11-19 11:09:55 +08:00
|
|
|
|
if (rtp_len_ptr == data) {
|
|
|
|
|
//停止搜索rtp,否则会进入死循环
|
|
|
|
|
_search_rtp = false;
|
|
|
|
|
}
|
2021-04-22 22:02:21 +08:00
|
|
|
|
//前面的数据都需要丢弃,这个是rtp的起始
|
|
|
|
|
return rtp_len_ptr;
|
|
|
|
|
}
|
|
|
|
|
//第一个rtp长度不匹配,说明第一个找到的ssrc不是rtp,丢弃之,我们从第二个ssrc所在rtp开始搜索
|
|
|
|
|
return ssrc_ptr1 - kSSRCOffset;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-06 11:54:10 +08:00
|
|
|
|
}//namespace mediakit
|
|
|
|
|
#endif//defined(ENABLE_RTPPROXY)
|