2022-06-18 14:34:58 +08:00
|
|
|
|
/*
|
2023-12-09 16:23:51 +08:00
|
|
|
|
* Copyright (c) 2016-present The ZLMediaKit project authors. All Rights Reserved.
|
2021-04-09 20:42:36 +08:00
|
|
|
|
*
|
2023-12-09 16:23:51 +08:00
|
|
|
|
* This file is part of ZLMediaKit(https://github.com/ZLMediaKit/ZLMediaKit).
|
2021-04-09 20:42:36 +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
|
2021-04-09 20:42:36 +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.
|
|
|
|
|
*/
|
|
|
|
|
|
2022-09-18 21:03:05 +08:00
|
|
|
|
#include <iostream>
|
|
|
|
|
#include <srtp2/srtp.h>
|
2023-12-09 16:23:51 +08:00
|
|
|
|
#include "Util/base64.h"
|
|
|
|
|
#include "Network/sockutil.h"
|
2022-11-29 11:07:13 +08:00
|
|
|
|
#include "Common/config.h"
|
2024-06-21 20:21:45 +08:00
|
|
|
|
#include "Nack.h"
|
2022-09-18 21:03:05 +08:00
|
|
|
|
#include "RtpExt.h"
|
2021-03-24 16:52:41 +08:00
|
|
|
|
#include "Rtcp/Rtcp.h"
|
2021-04-28 15:41:36 +08:00
|
|
|
|
#include "Rtcp/RtcpFCI.h"
|
2022-11-29 11:07:13 +08:00
|
|
|
|
#include "Rtcp/RtcpContext.h"
|
2023-02-28 22:23:30 +08:00
|
|
|
|
#include "Rtsp/Rtsp.h"
|
2021-04-02 23:01:58 +08:00
|
|
|
|
#include "Rtsp/RtpReceiver.h"
|
2022-09-18 21:03:05 +08:00
|
|
|
|
#include "WebRtcTransport.h"
|
2022-07-26 00:04:03 +08:00
|
|
|
|
|
2022-09-18 21:03:05 +08:00
|
|
|
|
#include "WebRtcEchoTest.h"
|
|
|
|
|
#include "WebRtcPlayer.h"
|
|
|
|
|
#include "WebRtcPusher.h"
|
2023-05-11 20:47:40 +08:00
|
|
|
|
#include "Rtsp/RtspMediaSourceImp.h"
|
|
|
|
|
|
2021-10-15 16:27:17 +08:00
|
|
|
|
#define RTP_SSRC_OFFSET 1
|
2021-04-02 23:01:58 +08:00
|
|
|
|
#define RTX_SSRC_OFFSET 2
|
|
|
|
|
#define RTP_CNAME "zlmediakit-rtp"
|
2021-05-08 15:01:14 +08:00
|
|
|
|
#define RTP_LABEL "zlmediakit-label"
|
|
|
|
|
#define RTP_MSLABEL "zlmediakit-mslabel"
|
2021-04-02 23:01:58 +08:00
|
|
|
|
|
2022-02-02 20:34:50 +08:00
|
|
|
|
using namespace std;
|
2022-09-18 21:03:05 +08:00
|
|
|
|
|
|
|
|
|
namespace mediakit {
|
2022-02-02 20:34:50 +08:00
|
|
|
|
|
2022-07-14 22:49:31 +08:00
|
|
|
|
// RTC配置项目
|
2022-09-18 21:03:05 +08:00
|
|
|
|
namespace Rtc {
|
2021-04-07 17:51:06 +08:00
|
|
|
|
#define RTC_FIELD "rtc."
|
2022-07-14 22:49:31 +08:00
|
|
|
|
// rtp和rtcp接受超时时间
|
|
|
|
|
const string kTimeOutSec = RTC_FIELD "timeoutSec";
|
|
|
|
|
// 服务器外网ip
|
|
|
|
|
const string kExternIP = RTC_FIELD "externIP";
|
|
|
|
|
// 设置remb比特率,非0时关闭twcc并开启remb。该设置在rtc推流时有效,可以控制推流画质
|
|
|
|
|
const string kRembBitRate = RTC_FIELD "rembBitRate";
|
|
|
|
|
// webrtc单端口udp服务器
|
|
|
|
|
const string kPort = RTC_FIELD "port";
|
2022-11-19 01:51:53 +08:00
|
|
|
|
const string kTcpPort = RTC_FIELD "tcpPort";
|
|
|
|
|
|
2023-12-09 16:23:51 +08:00
|
|
|
|
// 比特率设置
|
|
|
|
|
const string kStartBitrate = RTC_FIELD "start_bitrate";
|
|
|
|
|
const string kMaxBitrate = RTC_FIELD "max_bitrate";
|
|
|
|
|
const string kMinBitrate = RTC_FIELD "min_bitrate";
|
|
|
|
|
|
2024-03-02 16:52:51 +08:00
|
|
|
|
// 数据通道设置
|
|
|
|
|
const string kDataChannelEcho = RTC_FIELD "datachannel_echo";
|
|
|
|
|
|
2021-04-07 17:51:06 +08:00
|
|
|
|
static onceToken token([]() {
|
|
|
|
|
mINI::Instance()[kTimeOutSec] = 15;
|
|
|
|
|
mINI::Instance()[kExternIP] = "";
|
2021-04-28 15:41:36 +08:00
|
|
|
|
mINI::Instance()[kRembBitRate] = 0;
|
2023-12-29 10:53:47 +08:00
|
|
|
|
mINI::Instance()[kPort] = 8000;
|
|
|
|
|
mINI::Instance()[kTcpPort] = 8000;
|
2023-12-09 16:23:51 +08:00
|
|
|
|
|
|
|
|
|
mINI::Instance()[kStartBitrate] = 0;
|
|
|
|
|
mINI::Instance()[kMaxBitrate] = 0;
|
|
|
|
|
mINI::Instance()[kMinBitrate] = 0;
|
2024-03-02 16:52:51 +08:00
|
|
|
|
|
|
|
|
|
mINI::Instance()[kDataChannelEcho] = true;
|
2021-04-07 17:51:06 +08:00
|
|
|
|
});
|
|
|
|
|
|
2022-07-14 22:49:31 +08:00
|
|
|
|
} // namespace RTC
|
2021-04-07 17:51:06 +08:00
|
|
|
|
|
2022-07-14 22:49:31 +08:00
|
|
|
|
static atomic<uint64_t> s_key { 0 };
|
2021-11-28 21:43:21 +08:00
|
|
|
|
|
2022-07-09 21:44:57 +08:00
|
|
|
|
static void translateIPFromEnv(std::vector<std::string> &v) {
|
|
|
|
|
for (auto iter = v.begin(); iter != v.end();) {
|
|
|
|
|
if (start_with(*iter, "$")) {
|
|
|
|
|
auto ip = toolkit::getEnv(*iter);
|
|
|
|
|
if (ip.empty()) {
|
|
|
|
|
iter = v.erase(iter);
|
|
|
|
|
} else {
|
|
|
|
|
*iter++ = ip;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
++iter;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-09 16:23:51 +08:00
|
|
|
|
static std::string getServerPrefix() {
|
|
|
|
|
//stun_user_name格式: base64(ip+udp_port+tcp_port) + _ + number
|
|
|
|
|
//其中ip为二进制char[4], udp_port/tcp_port为大端 uint16.
|
|
|
|
|
//number为自增长数,确保短时间内唯一
|
|
|
|
|
GET_CONFIG(uint16_t, udp_port, Rtc::kPort);
|
|
|
|
|
GET_CONFIG(uint16_t, tcp_port, Rtc::kTcpPort);
|
|
|
|
|
char buf[8];
|
|
|
|
|
auto host = SockUtil::get_local_ip();
|
|
|
|
|
auto addr = SockUtil::make_sockaddr(host.data(), udp_port);
|
|
|
|
|
//拷贝ipv4地址
|
|
|
|
|
memcpy(buf, &(reinterpret_cast<sockaddr_in *>(&addr)->sin_addr), 4);
|
|
|
|
|
//拷贝udp端口
|
|
|
|
|
memcpy(buf + 4, &(reinterpret_cast<sockaddr_in *>(&addr)->sin_port), 2);
|
|
|
|
|
//tcp端口转大端模式
|
|
|
|
|
addr = SockUtil::make_sockaddr(host.data(), tcp_port);
|
|
|
|
|
//拷贝tcp端口
|
|
|
|
|
memcpy(buf + 6, &(reinterpret_cast<sockaddr_in *>(&addr)->sin_port), 2);
|
|
|
|
|
auto ret = encodeBase64(string(buf, 8)) + '_';
|
|
|
|
|
InfoL << "MediaServer(" << host << ":" << udp_port << ":" << tcp_port << ") prefix: " << ret;
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-03 11:18:21 +08:00
|
|
|
|
const char* sockTypeStr(Session* session) {
|
|
|
|
|
if (session) {
|
|
|
|
|
switch (session->getSock()->sockType()) {
|
|
|
|
|
case SockNum::Sock_TCP: return "tcp";
|
|
|
|
|
case SockNum::Sock_UDP: return "udp";
|
|
|
|
|
default: break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return "unknown";
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-27 10:16:49 +08:00
|
|
|
|
WebRtcTransport::WebRtcTransport(const EventPoller::Ptr &poller) {
|
2021-04-04 23:20:10 +08:00
|
|
|
|
_poller = poller;
|
2023-12-09 16:23:51 +08:00
|
|
|
|
static auto prefix = getServerPrefix();
|
|
|
|
|
_identifier = prefix + to_string(++s_key);
|
2021-11-27 21:30:46 +08:00
|
|
|
|
_packet_pool.setSize(64);
|
2021-03-24 16:52:41 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-07-14 22:49:31 +08:00
|
|
|
|
void WebRtcTransport::onCreate() {
|
2021-09-08 18:00:55 +08:00
|
|
|
|
_dtls_transport = std::make_shared<RTC::DtlsTransport>(_poller, this);
|
2021-10-15 17:12:39 +08:00
|
|
|
|
_ice_server = std::make_shared<RTC::IceServer>(this, _identifier, makeRandStr(24));
|
2021-04-07 17:21:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-07-14 22:49:31 +08:00
|
|
|
|
void WebRtcTransport::onDestory() {
|
2022-05-04 22:15:21 +08:00
|
|
|
|
#ifdef ENABLE_SCTP
|
|
|
|
|
_sctp = nullptr;
|
|
|
|
|
#endif
|
2021-04-02 17:08:11 +08:00
|
|
|
|
_dtls_transport = nullptr;
|
|
|
|
|
_ice_server = nullptr;
|
2021-03-27 09:38:13 +08:00
|
|
|
|
}
|
2021-03-24 16:52:41 +08:00
|
|
|
|
|
2022-07-14 22:49:31 +08:00
|
|
|
|
const EventPoller::Ptr &WebRtcTransport::getPoller() const {
|
2021-04-04 23:20:10 +08:00
|
|
|
|
return _poller;
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-15 17:12:39 +08:00
|
|
|
|
const string &WebRtcTransport::getIdentifier() const {
|
|
|
|
|
return _identifier;
|
2021-09-10 22:31:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-28 22:06:46 +08:00
|
|
|
|
const std::string& WebRtcTransport::deleteRandStr() const {
|
|
|
|
|
if (_delete_rand_str.empty()) {
|
|
|
|
|
_delete_rand_str = makeRandStr(32);
|
|
|
|
|
}
|
|
|
|
|
return _delete_rand_str;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-26 11:07:03 +08:00
|
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
2022-07-14 22:49:31 +08:00
|
|
|
|
void WebRtcTransport::OnIceServerSendStunPacket(
|
|
|
|
|
const RTC::IceServer *iceServer, const RTC::StunPacket *packet, RTC::TransportTuple *tuple) {
|
|
|
|
|
sendSockData((char *)packet->GetData(), packet->GetSize(), tuple);
|
2021-03-26 11:07:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-03-03 11:18:21 +08:00
|
|
|
|
void WebRtcTransportImp::OnIceServerSelectedTuple(const RTC::IceServer *iceServer, RTC::TransportTuple *tuple) {
|
|
|
|
|
InfoL << getIdentifier() << " select tuple " << sockTypeStr(tuple) << " " << tuple->get_peer_ip() << ":" << tuple->get_peer_port();
|
|
|
|
|
tuple->setSendFlushFlag(false);
|
|
|
|
|
unrefSelf();
|
2021-03-26 11:07:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WebRtcTransport::OnIceServerConnected(const RTC::IceServer *iceServer) {
|
2023-03-03 11:18:21 +08:00
|
|
|
|
InfoL << getIdentifier();
|
2021-04-01 14:16:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WebRtcTransport::OnIceServerCompleted(const RTC::IceServer *iceServer) {
|
2023-03-03 11:18:21 +08:00
|
|
|
|
InfoL << getIdentifier();
|
2021-04-01 10:09:50 +08:00
|
|
|
|
if (_answer_sdp->media[0].role == DtlsRole::passive) {
|
2021-04-02 17:08:11 +08:00
|
|
|
|
_dtls_transport->Run(RTC::DtlsTransport::Role::SERVER);
|
2021-04-01 10:09:50 +08:00
|
|
|
|
} else {
|
2021-04-02 17:08:11 +08:00
|
|
|
|
_dtls_transport->Run(RTC::DtlsTransport::Role::CLIENT);
|
2021-04-01 10:09:50 +08:00
|
|
|
|
}
|
2021-03-26 11:07:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WebRtcTransport::OnIceServerDisconnected(const RTC::IceServer *iceServer) {
|
2023-03-03 11:18:21 +08:00
|
|
|
|
InfoL << getIdentifier();
|
2021-03-26 11:07:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
void WebRtcTransport::OnDtlsTransportConnected(
|
2022-07-14 22:49:31 +08:00
|
|
|
|
const RTC::DtlsTransport *dtlsTransport, RTC::SrtpSession::CryptoSuite srtpCryptoSuite, uint8_t *srtpLocalKey,
|
|
|
|
|
size_t srtpLocalKeyLen, uint8_t *srtpRemoteKey, size_t srtpRemoteKeyLen, std::string &remoteCert) {
|
2023-03-03 11:18:21 +08:00
|
|
|
|
InfoL << getIdentifier();
|
2022-07-14 22:49:31 +08:00
|
|
|
|
_srtp_session_send = std::make_shared<RTC::SrtpSession>(
|
|
|
|
|
RTC::SrtpSession::Type::OUTBOUND, srtpCryptoSuite, srtpLocalKey, srtpLocalKeyLen);
|
|
|
|
|
_srtp_session_recv = std::make_shared<RTC::SrtpSession>(
|
|
|
|
|
RTC::SrtpSession::Type::INBOUND, srtpCryptoSuite, srtpRemoteKey, srtpRemoteKeyLen);
|
2022-05-04 22:15:21 +08:00
|
|
|
|
#ifdef ENABLE_SCTP
|
|
|
|
|
_sctp = std::make_shared<RTC::SctpAssociationImp>(getPoller(), this, 128, 128, 262144, true);
|
|
|
|
|
_sctp->TransportConnected();
|
|
|
|
|
#endif
|
2021-04-02 17:08:11 +08:00
|
|
|
|
onStartWebRTC();
|
2021-03-26 11:07:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-01-24 15:14:23 +08:00
|
|
|
|
#pragma pack(push, 1)
|
|
|
|
|
struct DtlsHeader {
|
|
|
|
|
uint8_t content_type;
|
|
|
|
|
uint16_t dtls_version;
|
|
|
|
|
uint16_t epoch;
|
|
|
|
|
uint8_t seq[6];
|
|
|
|
|
uint16_t length;
|
|
|
|
|
uint8_t payload[1];
|
|
|
|
|
};
|
|
|
|
|
#pragma pack(pop)
|
|
|
|
|
|
2022-07-14 22:49:31 +08:00
|
|
|
|
void WebRtcTransport::OnDtlsTransportSendData(
|
|
|
|
|
const RTC::DtlsTransport *dtlsTransport, const uint8_t *data, size_t len) {
|
2024-01-24 15:14:23 +08:00
|
|
|
|
size_t offset = 0;
|
|
|
|
|
while(offset < len) {
|
|
|
|
|
auto *header = reinterpret_cast<const DtlsHeader *>(data + offset);
|
|
|
|
|
auto length = ntohs(header->length) + offsetof(DtlsHeader, payload);
|
|
|
|
|
sendSockData((char *)data + offset, length, nullptr);
|
|
|
|
|
offset += length;
|
|
|
|
|
}
|
2021-03-26 11:07:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-07 17:51:06 +08:00
|
|
|
|
void WebRtcTransport::OnDtlsTransportConnecting(const RTC::DtlsTransport *dtlsTransport) {
|
2023-03-03 11:18:21 +08:00
|
|
|
|
InfoL << getIdentifier();
|
2021-04-07 17:51:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WebRtcTransport::OnDtlsTransportFailed(const RTC::DtlsTransport *dtlsTransport) {
|
2023-03-03 11:18:21 +08:00
|
|
|
|
InfoL << getIdentifier();
|
2021-04-07 17:51:06 +08:00
|
|
|
|
onShutdown(SockException(Err_shutdown, "dtls transport failed"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WebRtcTransport::OnDtlsTransportClosed(const RTC::DtlsTransport *dtlsTransport) {
|
2023-03-03 11:18:21 +08:00
|
|
|
|
InfoL << getIdentifier();
|
2021-04-07 17:51:06 +08:00
|
|
|
|
onShutdown(SockException(Err_shutdown, "dtls close notify received"));
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-14 22:49:31 +08:00
|
|
|
|
void WebRtcTransport::OnDtlsTransportApplicationDataReceived(
|
|
|
|
|
const RTC::DtlsTransport *dtlsTransport, const uint8_t *data, size_t len) {
|
2022-05-04 22:15:21 +08:00
|
|
|
|
#ifdef ENABLE_SCTP
|
|
|
|
|
_sctp->ProcessSctpData(data, len);
|
|
|
|
|
#else
|
2021-04-07 17:51:06 +08:00
|
|
|
|
InfoL << hexdump(data, len);
|
2022-05-04 22:15:21 +08:00
|
|
|
|
#endif
|
2021-04-07 17:51:06 +08:00
|
|
|
|
}
|
2022-05-04 22:15:21 +08:00
|
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
#ifdef ENABLE_SCTP
|
2022-07-14 22:49:31 +08:00
|
|
|
|
void WebRtcTransport::OnSctpAssociationConnecting(RTC::SctpAssociation *sctpAssociation) {
|
2023-03-03 11:18:21 +08:00
|
|
|
|
TraceL << getIdentifier();
|
2024-03-02 16:52:51 +08:00
|
|
|
|
try {
|
|
|
|
|
NOTICE_EMIT(BroadcastRtcSctpConnectArgs, Broadcast::kBroadcastRtcSctpConnecting, *this);
|
|
|
|
|
} catch (std::exception &ex) {
|
|
|
|
|
WarnL << "Exception occurred: " << ex.what();
|
|
|
|
|
}
|
2022-05-04 22:15:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-07-14 22:49:31 +08:00
|
|
|
|
void WebRtcTransport::OnSctpAssociationConnected(RTC::SctpAssociation *sctpAssociation) {
|
2022-05-04 22:15:21 +08:00
|
|
|
|
InfoL << getIdentifier();
|
2024-03-02 16:52:51 +08:00
|
|
|
|
try {
|
|
|
|
|
NOTICE_EMIT(BroadcastRtcSctpConnectArgs, Broadcast::kBroadcastRtcSctpConnected, *this);
|
|
|
|
|
} catch (std::exception &ex) {
|
|
|
|
|
WarnL << "Exception occurred: " << ex.what();
|
|
|
|
|
}
|
2022-05-04 22:15:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-07-14 22:49:31 +08:00
|
|
|
|
void WebRtcTransport::OnSctpAssociationFailed(RTC::SctpAssociation *sctpAssociation) {
|
2022-05-04 22:15:21 +08:00
|
|
|
|
WarnL << getIdentifier();
|
2024-03-02 16:52:51 +08:00
|
|
|
|
try {
|
|
|
|
|
NOTICE_EMIT(BroadcastRtcSctpConnectArgs, Broadcast::kBroadcastRtcSctpFailed, *this);
|
|
|
|
|
} catch (std::exception &ex) {
|
|
|
|
|
WarnL << "Exception occurred: " << ex.what();
|
|
|
|
|
}
|
2022-05-04 22:15:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-07-14 22:49:31 +08:00
|
|
|
|
void WebRtcTransport::OnSctpAssociationClosed(RTC::SctpAssociation *sctpAssociation) {
|
2022-05-04 22:15:21 +08:00
|
|
|
|
InfoL << getIdentifier();
|
2024-03-02 16:52:51 +08:00
|
|
|
|
try {
|
|
|
|
|
NOTICE_EMIT(BroadcastRtcSctpConnectArgs, Broadcast::kBroadcastRtcSctpClosed, *this);
|
|
|
|
|
} catch (std::exception &ex) {
|
|
|
|
|
WarnL << "Exception occurred: " << ex.what();
|
|
|
|
|
}
|
2022-05-04 22:15:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-07-14 22:49:31 +08:00
|
|
|
|
void WebRtcTransport::OnSctpAssociationSendData(
|
|
|
|
|
RTC::SctpAssociation *sctpAssociation, const uint8_t *data, size_t len) {
|
2024-03-02 16:52:51 +08:00
|
|
|
|
try {
|
|
|
|
|
NOTICE_EMIT(BroadcastRtcSctpSendArgs, Broadcast::kBroadcastRtcSctpSend, *this, data, len);
|
|
|
|
|
} catch (std::exception &ex) {
|
|
|
|
|
WarnL << "Exception occurred: " << ex.what();
|
|
|
|
|
}
|
2022-05-04 22:15:21 +08:00
|
|
|
|
_dtls_transport->SendApplicationData(data, len);
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-14 22:49:31 +08:00
|
|
|
|
void WebRtcTransport::OnSctpAssociationMessageReceived(
|
|
|
|
|
RTC::SctpAssociation *sctpAssociation, uint16_t streamId, uint32_t ppid, const uint8_t *msg, size_t len) {
|
2022-05-04 22:15:21 +08:00
|
|
|
|
InfoL << getIdentifier() << " " << streamId << " " << ppid << " " << len << " " << string((char *)msg, len);
|
|
|
|
|
RTC::SctpStreamParameters params;
|
|
|
|
|
params.streamId = streamId;
|
2024-03-02 16:52:51 +08:00
|
|
|
|
|
|
|
|
|
GET_CONFIG(bool, datachannel_echo, Rtc::kDataChannelEcho);
|
|
|
|
|
if (datachannel_echo) {
|
|
|
|
|
// 回显数据
|
|
|
|
|
_sctp->SendSctpMessage(params, ppid, msg, len);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
NOTICE_EMIT(BroadcastRtcSctpReceivedArgs, Broadcast::kBroadcastRtcSctpReceived, *this, streamId, ppid, msg, len);
|
|
|
|
|
} catch (std::exception &ex) {
|
|
|
|
|
WarnL << "Exception occurred: " << ex.what();
|
|
|
|
|
}
|
2022-05-04 22:15:21 +08:00
|
|
|
|
}
|
|
|
|
|
#endif
|
2023-09-02 12:53:56 +08:00
|
|
|
|
|
|
|
|
|
void WebRtcTransport::sendDatachannel(uint16_t streamId, uint32_t ppid, const char *msg, size_t len) {
|
|
|
|
|
#ifdef ENABLE_SCTP
|
|
|
|
|
if (_sctp) {
|
|
|
|
|
RTC::SctpStreamParameters params;
|
|
|
|
|
params.streamId = streamId;
|
|
|
|
|
_sctp->SendSctpMessage(params, ppid, (uint8_t *)msg, len);
|
|
|
|
|
}
|
|
|
|
|
#else
|
|
|
|
|
WarnL << "WebRTC datachannel disabled!";
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-26 11:07:03 +08:00
|
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
2022-07-14 22:49:31 +08:00
|
|
|
|
void WebRtcTransport::sendSockData(const char *buf, size_t len, RTC::TransportTuple *tuple) {
|
2022-01-06 14:30:44 +08:00
|
|
|
|
auto pkt = _packet_pool.obtain2();
|
2021-11-27 21:30:46 +08:00
|
|
|
|
pkt->assign(buf, len);
|
|
|
|
|
onSendSockData(std::move(pkt), true, tuple ? tuple : _ice_server->GetSelectedTuple());
|
2021-03-26 11:07:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-03-03 11:18:21 +08:00
|
|
|
|
Session::Ptr WebRtcTransport::getSession() const {
|
2023-10-25 17:50:29 +08:00
|
|
|
|
auto tuple = _ice_server ? _ice_server->GetSelectedTuple(true) : nullptr;
|
2023-04-28 22:03:16 +08:00
|
|
|
|
return tuple ? static_pointer_cast<Session>(tuple->shared_from_this()) : nullptr;
|
2021-04-07 20:33:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-28 15:41:36 +08:00
|
|
|
|
void WebRtcTransport::sendRtcpRemb(uint32_t ssrc, size_t bit_rate) {
|
2022-07-14 22:49:31 +08:00
|
|
|
|
auto remb = FCI_REMB::create({ ssrc }, (uint32_t)bit_rate);
|
2021-04-28 15:41:36 +08:00
|
|
|
|
auto fb = RtcpFB::create(PSFBType::RTCP_PSFB_REMB, remb.data(), remb.size());
|
|
|
|
|
fb->ssrc = htonl(0);
|
|
|
|
|
fb->ssrc_media = htonl(ssrc);
|
2022-07-14 22:49:31 +08:00
|
|
|
|
sendRtcpPacket((char *)fb.get(), fb->getSize(), true);
|
2021-04-28 15:41:36 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WebRtcTransport::sendRtcpPli(uint32_t ssrc) {
|
|
|
|
|
auto pli = RtcpFB::create(PSFBType::RTCP_PSFB_PLI);
|
|
|
|
|
pli->ssrc = htonl(0);
|
|
|
|
|
pli->ssrc_media = htonl(ssrc);
|
2022-07-14 22:49:31 +08:00
|
|
|
|
sendRtcpPacket((char *)pli.get(), pli->getSize(), true);
|
2021-04-28 15:41:36 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-07-14 22:49:31 +08:00
|
|
|
|
string getFingerprint(const string &algorithm_str, const std::shared_ptr<RTC::DtlsTransport> &transport) {
|
2021-04-01 10:09:50 +08:00
|
|
|
|
auto algorithm = RTC::DtlsTransport::GetFingerprintAlgorithm(algorithm_str);
|
|
|
|
|
for (auto &finger_prints : transport->GetLocalFingerprints()) {
|
|
|
|
|
if (finger_prints.algorithm == algorithm) {
|
|
|
|
|
return finger_prints.value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
throw std::invalid_argument(StrPrinter << "不支持的加密算法:" << algorithm_str);
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-14 22:49:31 +08:00
|
|
|
|
void WebRtcTransport::setRemoteDtlsFingerprint(const RtcSession &remote) {
|
|
|
|
|
// 设置远端dtls签名
|
2021-04-02 17:08:11 +08:00
|
|
|
|
RTC::DtlsTransport::Fingerprint remote_fingerprint;
|
2022-07-14 22:49:31 +08:00
|
|
|
|
remote_fingerprint.algorithm
|
|
|
|
|
= RTC::DtlsTransport::GetFingerprintAlgorithm(_offer_sdp->media[0].fingerprint.algorithm);
|
2021-04-02 17:08:11 +08:00
|
|
|
|
remote_fingerprint.value = _offer_sdp->media[0].fingerprint.hash;
|
|
|
|
|
_dtls_transport->SetRemoteFingerprint(remote_fingerprint);
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-28 15:41:36 +08:00
|
|
|
|
void WebRtcTransport::onRtcConfigure(RtcConfigure &configure) const {
|
2024-03-23 22:46:30 +08:00
|
|
|
|
SdpAttrFingerprint fingerprint;
|
|
|
|
|
fingerprint.algorithm = _offer_sdp->media[0].fingerprint.algorithm;
|
|
|
|
|
fingerprint.hash = getFingerprint(fingerprint.algorithm, _dtls_transport);
|
|
|
|
|
configure.setDefaultSetting(
|
|
|
|
|
_ice_server->GetUsernameFragment(), _ice_server->GetPassword(), RtpDirection::sendrecv, fingerprint);
|
|
|
|
|
|
2022-07-14 22:49:31 +08:00
|
|
|
|
// 开启remb后关闭twcc,因为开启twcc后remb无效
|
2022-09-18 21:03:05 +08:00
|
|
|
|
GET_CONFIG(size_t, remb_bit_rate, Rtc::kRembBitRate);
|
2021-05-07 18:34:04 +08:00
|
|
|
|
configure.enableTWCC(!remb_bit_rate);
|
2021-04-28 15:41:36 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-12-09 16:23:51 +08:00
|
|
|
|
static void setSdpBitrate(RtcSession &sdp) {
|
|
|
|
|
GET_CONFIG(size_t, max_bitrate, Rtc::kMaxBitrate);
|
|
|
|
|
GET_CONFIG(size_t, min_bitrate, Rtc::kMinBitrate);
|
|
|
|
|
GET_CONFIG(size_t, start_bitrate, Rtc::kStartBitrate);
|
|
|
|
|
|
|
|
|
|
auto m = (RtcMedia *)(sdp.getMedia(TrackType::TrackVideo));
|
|
|
|
|
if (m) {
|
|
|
|
|
auto &plan = m->plan[0];
|
|
|
|
|
if (max_bitrate) plan.fmtp["x-google-max-bitrate"] = std::to_string(max_bitrate);
|
|
|
|
|
if (min_bitrate) plan.fmtp["x-google-min-bitrate"] = std::to_string(min_bitrate);
|
|
|
|
|
if (start_bitrate) plan.fmtp["x-google-start-bitrate"] = std::to_string(start_bitrate);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-14 22:49:31 +08:00
|
|
|
|
std::string WebRtcTransport::getAnswerSdp(const string &offer) {
|
2021-04-07 20:41:17 +08:00
|
|
|
|
try {
|
|
|
|
|
//// 解析offer sdp ////
|
|
|
|
|
_offer_sdp = std::make_shared<RtcSession>();
|
|
|
|
|
_offer_sdp->loadFrom(offer);
|
2021-10-12 17:32:22 +08:00
|
|
|
|
onCheckSdp(SdpType::offer, *_offer_sdp);
|
2021-10-16 16:46:05 +08:00
|
|
|
|
_offer_sdp->checkValid();
|
2021-04-07 20:41:17 +08:00
|
|
|
|
setRemoteDtlsFingerprint(*_offer_sdp);
|
|
|
|
|
|
|
|
|
|
//// sdp 配置 ////
|
|
|
|
|
RtcConfigure configure;
|
|
|
|
|
onRtcConfigure(configure);
|
|
|
|
|
|
|
|
|
|
//// 生成answer sdp ////
|
|
|
|
|
_answer_sdp = configure.createAnswer(*_offer_sdp);
|
2021-10-12 17:04:21 +08:00
|
|
|
|
onCheckSdp(SdpType::answer, *_answer_sdp);
|
2023-12-09 16:23:51 +08:00
|
|
|
|
setSdpBitrate(*_answer_sdp);
|
2021-10-16 16:46:05 +08:00
|
|
|
|
_answer_sdp->checkValid();
|
2021-04-07 20:41:17 +08:00
|
|
|
|
return _answer_sdp->toString();
|
|
|
|
|
} catch (exception &ex) {
|
|
|
|
|
onShutdown(SockException(Err_shutdown, ex.what()));
|
|
|
|
|
throw;
|
|
|
|
|
}
|
2021-03-31 17:15:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-28 22:23:30 +08:00
|
|
|
|
static bool isDtls(char *buf) {
|
2021-03-24 16:52:41 +08:00
|
|
|
|
return ((*buf > 19) && (*buf < 64));
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-13 16:07:27 +08:00
|
|
|
|
void WebRtcTransport::inputSockData(char *buf, int len, RTC::TransportTuple *tuple) {
|
2022-07-14 22:49:31 +08:00
|
|
|
|
if (RTC::StunPacket::IsStun((const uint8_t *)buf, len)) {
|
|
|
|
|
std::unique_ptr<RTC::StunPacket> packet(RTC::StunPacket::Parse((const uint8_t *)buf, len));
|
2021-08-19 19:14:58 +08:00
|
|
|
|
if (!packet) {
|
2023-06-10 11:04:52 +08:00
|
|
|
|
WarnL << "parse stun error";
|
2021-03-24 16:52:41 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
2021-08-19 19:14:58 +08:00
|
|
|
|
_ice_server->ProcessStunPacket(packet.get(), tuple);
|
2021-03-24 16:52:41 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
2023-02-28 22:23:30 +08:00
|
|
|
|
if (isDtls(buf)) {
|
2022-07-14 22:49:31 +08:00
|
|
|
|
_dtls_transport->ProcessDtlsData((uint8_t *)buf, len);
|
2021-03-24 16:52:41 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
2023-02-28 22:23:30 +08:00
|
|
|
|
if (isRtp(buf, len)) {
|
2021-09-24 11:29:04 +08:00
|
|
|
|
if (!_srtp_session_recv) {
|
2024-03-23 22:46:30 +08:00
|
|
|
|
WarnL << "received rtp packet when dtls not completed from:" << tuple->get_peer_ip();
|
2021-09-24 11:29:04 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
2022-07-14 22:49:31 +08:00
|
|
|
|
if (_srtp_session_recv->DecryptSrtp((uint8_t *)buf, &len)) {
|
2021-10-07 15:49:33 +08:00
|
|
|
|
onRtp(buf, len, _ticker.createdTime());
|
2021-04-02 17:56:24 +08:00
|
|
|
|
}
|
2021-04-01 14:16:42 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
2023-02-28 22:23:30 +08:00
|
|
|
|
if (isRtcp(buf, len)) {
|
2021-09-24 11:29:04 +08:00
|
|
|
|
if (!_srtp_session_recv) {
|
2024-03-23 22:46:30 +08:00
|
|
|
|
WarnL << "received rtcp packet when dtls not completed from:" << tuple->get_peer_ip();
|
2021-09-24 11:29:04 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
2022-07-14 22:49:31 +08:00
|
|
|
|
if (_srtp_session_recv->DecryptSrtcp((uint8_t *)buf, &len)) {
|
2021-04-02 17:56:24 +08:00
|
|
|
|
onRtcp(buf, len);
|
|
|
|
|
}
|
2021-04-01 14:16:42 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
2021-03-24 16:52:41 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-13 16:07:27 +08:00
|
|
|
|
void WebRtcTransport::sendRtpPacket(const char *buf, int len, bool flush, void *ctx) {
|
2021-04-02 17:08:11 +08:00
|
|
|
|
if (_srtp_session_send) {
|
2022-01-06 14:30:44 +08:00
|
|
|
|
auto pkt = _packet_pool.obtain2();
|
2022-07-14 22:49:31 +08:00
|
|
|
|
// 预留rtx加入的两个字节
|
|
|
|
|
pkt->setCapacity((size_t)len + SRTP_MAX_TRAILER_LEN + 2);
|
2022-12-14 15:08:59 +08:00
|
|
|
|
memcpy(pkt->data(), buf, len);
|
2021-11-27 21:30:46 +08:00
|
|
|
|
onBeforeEncryptRtp(pkt->data(), len, ctx);
|
|
|
|
|
if (_srtp_session_send->EncryptRtp(reinterpret_cast<uint8_t *>(pkt->data()), &len)) {
|
|
|
|
|
pkt->setSize(len);
|
|
|
|
|
onSendSockData(std::move(pkt), flush);
|
2021-05-08 21:16:51 +08:00
|
|
|
|
}
|
2021-03-24 16:52:41 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-27 21:30:46 +08:00
|
|
|
|
void WebRtcTransport::sendRtcpPacket(const char *buf, int len, bool flush, void *ctx) {
|
2021-04-02 23:12:37 +08:00
|
|
|
|
if (_srtp_session_send) {
|
2022-01-06 14:30:44 +08:00
|
|
|
|
auto pkt = _packet_pool.obtain2();
|
2022-07-14 22:49:31 +08:00
|
|
|
|
// 预留rtx加入的两个字节
|
|
|
|
|
pkt->setCapacity((size_t)len + SRTP_MAX_TRAILER_LEN + 2);
|
2022-12-15 19:17:09 +08:00
|
|
|
|
memcpy(pkt->data(), buf, len);
|
2021-11-27 21:30:46 +08:00
|
|
|
|
onBeforeEncryptRtcp(pkt->data(), len, ctx);
|
|
|
|
|
if (_srtp_session_send->EncryptRtcp(reinterpret_cast<uint8_t *>(pkt->data()), &len)) {
|
|
|
|
|
pkt->setSize(len);
|
|
|
|
|
onSendSockData(std::move(pkt), flush);
|
2021-05-08 21:16:51 +08:00
|
|
|
|
}
|
2021-04-02 23:12:37 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-24 16:52:41 +08:00
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
2022-07-14 22:49:31 +08:00
|
|
|
|
void WebRtcTransportImp::onCreate() {
|
2021-04-07 17:21:59 +08:00
|
|
|
|
WebRtcTransport::onCreate();
|
2021-09-10 22:31:44 +08:00
|
|
|
|
registerSelf();
|
|
|
|
|
|
2021-09-08 18:00:55 +08:00
|
|
|
|
weak_ptr<WebRtcTransportImp> weak_self = static_pointer_cast<WebRtcTransportImp>(shared_from_this());
|
2022-09-18 21:03:05 +08:00
|
|
|
|
GET_CONFIG(float, timeoutSec, Rtc::kTimeOutSec);
|
2022-07-14 22:49:31 +08:00
|
|
|
|
_timer = std::make_shared<Timer>(
|
|
|
|
|
timeoutSec / 2,
|
|
|
|
|
[weak_self]() {
|
|
|
|
|
auto strong_self = weak_self.lock();
|
|
|
|
|
if (!strong_self) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (strong_self->_alive_ticker.elapsedTime() > timeoutSec * 1000) {
|
2022-08-20 10:28:19 +08:00
|
|
|
|
strong_self->onShutdown(SockException(Err_timeout, "接受rtp/rtcp/datachannel超时"));
|
2022-07-14 22:49:31 +08:00
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
},
|
|
|
|
|
getPoller());
|
2021-10-07 14:22:12 +08:00
|
|
|
|
|
2022-07-14 22:49:31 +08:00
|
|
|
|
_twcc_ctx.setOnSendTwccCB([this](uint32_t ssrc, string fci) { onSendTwcc(ssrc, fci); });
|
2021-03-24 16:52:41 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-08-20 10:28:19 +08:00
|
|
|
|
void WebRtcTransportImp::OnDtlsTransportApplicationDataReceived(const RTC::DtlsTransport *dtlsTransport, const uint8_t *data, size_t len) {
|
|
|
|
|
WebRtcTransport::OnDtlsTransportApplicationDataReceived(dtlsTransport, data, len);
|
|
|
|
|
#ifdef ENABLE_SCTP
|
|
|
|
|
if (_answer_sdp->isOnlyDatachannel()) {
|
|
|
|
|
_alive_ticker.resetTime();
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-23 22:46:30 +08:00
|
|
|
|
WebRtcTransportImp::WebRtcTransportImp(const EventPoller::Ptr &poller) : WebRtcTransport(poller) {
|
2021-10-15 17:12:39 +08:00
|
|
|
|
InfoL << getIdentifier();
|
2021-04-07 17:51:06 +08:00
|
|
|
|
}
|
2021-04-07 17:21:59 +08:00
|
|
|
|
|
2021-04-07 17:51:06 +08:00
|
|
|
|
WebRtcTransportImp::~WebRtcTransportImp() {
|
2021-10-15 17:12:39 +08:00
|
|
|
|
InfoL << getIdentifier();
|
2021-04-07 17:21:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-27 10:16:49 +08:00
|
|
|
|
void WebRtcTransportImp::onDestory() {
|
|
|
|
|
WebRtcTransport::onDestory();
|
2021-09-10 22:31:44 +08:00
|
|
|
|
unregisterSelf();
|
2021-03-24 16:52:41 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-11-27 21:30:46 +08:00
|
|
|
|
void WebRtcTransportImp::onSendSockData(Buffer::Ptr buf, bool flush, RTC::TransportTuple *tuple) {
|
2023-03-03 11:18:21 +08:00
|
|
|
|
if (tuple == nullptr) {
|
|
|
|
|
tuple = _ice_server->GetSelectedTuple();
|
|
|
|
|
if (!tuple) {
|
|
|
|
|
WarnL << "send data failed:" << buf->size();
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-09-10 22:31:44 +08:00
|
|
|
|
}
|
2022-11-18 22:52:57 +08:00
|
|
|
|
|
2022-07-14 22:49:31 +08:00
|
|
|
|
// 一次性发送一帧的rtp数据,提高网络io性能
|
2023-03-03 11:18:21 +08:00
|
|
|
|
if (tuple->getSock()->sockType() == SockNum::Sock_TCP) {
|
2022-11-18 22:52:57 +08:00
|
|
|
|
// 增加tcp两字节头
|
|
|
|
|
auto len = buf->size();
|
|
|
|
|
char tcp_len[2] = { 0 };
|
|
|
|
|
tcp_len[0] = (len >> 8) & 0xff;
|
|
|
|
|
tcp_len[1] = len & 0xff;
|
2023-03-03 11:18:21 +08:00
|
|
|
|
tuple->SockSender::send(tcp_len, 2);
|
2022-11-18 22:52:57 +08:00
|
|
|
|
}
|
2023-03-03 11:18:21 +08:00
|
|
|
|
tuple->send(std::move(buf));
|
2022-11-18 22:52:57 +08:00
|
|
|
|
|
|
|
|
|
if (flush) {
|
2023-03-03 11:18:21 +08:00
|
|
|
|
tuple->flushAll();
|
2022-11-18 22:52:57 +08:00
|
|
|
|
}
|
2021-04-03 08:32:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-03 08:45:18 +08:00
|
|
|
|
///////////////////////////////////////////////////////////////////
|
|
|
|
|
|
2022-07-14 22:49:31 +08:00
|
|
|
|
bool WebRtcTransportImp::canSendRtp() const {
|
2021-10-12 15:34:36 +08:00
|
|
|
|
for (auto &m : _answer_sdp->media) {
|
2021-07-10 20:36:32 +08:00
|
|
|
|
if (m.direction == RtpDirection::sendrecv || m.direction == RtpDirection::sendonly) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
2021-04-03 08:45:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-07-14 22:49:31 +08:00
|
|
|
|
bool WebRtcTransportImp::canRecvRtp() const {
|
2021-10-12 15:34:36 +08:00
|
|
|
|
for (auto &m : _answer_sdp->media) {
|
2021-07-10 20:36:32 +08:00
|
|
|
|
if (m.direction == RtpDirection::sendrecv || m.direction == RtpDirection::recvonly) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
2021-04-03 08:45:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-02 17:08:11 +08:00
|
|
|
|
void WebRtcTransportImp::onStartWebRTC() {
|
2022-07-14 22:49:31 +08:00
|
|
|
|
// 获取ssrc和pt相关信息,届时收到rtp和rtcp时分别可以根据pt和ssrc找到相关的信息
|
2021-10-12 15:34:36 +08:00
|
|
|
|
for (auto &m_answer : _answer_sdp->media) {
|
2022-04-03 17:12:23 +08:00
|
|
|
|
if (m_answer.type == TrackApplication) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2021-10-12 15:34:36 +08:00
|
|
|
|
auto m_offer = _offer_sdp->getMedia(m_answer.type);
|
2021-06-25 17:17:48 +08:00
|
|
|
|
auto track = std::make_shared<MediaTrack>();
|
2021-05-16 16:12:10 +08:00
|
|
|
|
|
2021-06-25 17:17:48 +08:00
|
|
|
|
track->media = &m_answer;
|
|
|
|
|
track->answer_ssrc_rtp = m_answer.getRtpSSRC();
|
|
|
|
|
track->answer_ssrc_rtx = m_answer.getRtxSSRC();
|
|
|
|
|
track->offer_ssrc_rtp = m_offer->getRtpSSRC();
|
|
|
|
|
track->offer_ssrc_rtx = m_offer->getRtxSSRC();
|
2022-04-03 17:12:23 +08:00
|
|
|
|
track->plan_rtp = &m_answer.plan[0];
|
2021-06-25 17:17:48 +08:00
|
|
|
|
track->plan_rtx = m_answer.getRelatedRtxPlan(track->plan_rtp->pt);
|
2021-10-13 15:06:13 +08:00
|
|
|
|
track->rtcp_context_send = std::make_shared<RtcpContextForSend>();
|
2021-05-16 16:12:10 +08:00
|
|
|
|
|
2022-07-14 22:49:31 +08:00
|
|
|
|
// rtp track type --> MediaTrack
|
2021-10-20 16:58:59 +08:00
|
|
|
|
if (m_answer.direction == RtpDirection::sendonly || m_answer.direction == RtpDirection::sendrecv) {
|
2022-07-14 22:49:31 +08:00
|
|
|
|
// 该类型的track 才支持发送
|
2021-10-20 16:58:59 +08:00
|
|
|
|
_type_to_track[m_answer.type] = track;
|
|
|
|
|
}
|
2022-07-14 22:49:31 +08:00
|
|
|
|
// send ssrc --> MediaTrack
|
2021-06-25 17:17:48 +08:00
|
|
|
|
_ssrc_to_track[track->answer_ssrc_rtp] = track;
|
|
|
|
|
_ssrc_to_track[track->answer_ssrc_rtx] = track;
|
2021-05-16 16:12:10 +08:00
|
|
|
|
|
2022-07-14 22:49:31 +08:00
|
|
|
|
// recv ssrc --> MediaTrack
|
2021-06-25 17:17:48 +08:00
|
|
|
|
_ssrc_to_track[track->offer_ssrc_rtp] = track;
|
|
|
|
|
_ssrc_to_track[track->offer_ssrc_rtx] = track;
|
2021-05-16 16:12:10 +08:00
|
|
|
|
|
2022-07-14 22:49:31 +08:00
|
|
|
|
// rtp pt --> MediaTrack
|
|
|
|
|
_pt_to_track.emplace(
|
|
|
|
|
track->plan_rtp->pt, std::unique_ptr<WrappedMediaTrack>(new WrappedRtpTrack(track, _twcc_ctx, *this)));
|
2021-06-25 17:17:48 +08:00
|
|
|
|
if (track->plan_rtx) {
|
2022-07-14 22:49:31 +08:00
|
|
|
|
// rtx pt --> MediaTrack
|
2021-11-19 17:03:54 +08:00
|
|
|
|
_pt_to_track.emplace(track->plan_rtx->pt, std::unique_ptr<WrappedMediaTrack>(new WrappedRtxTrack(track)));
|
2021-04-02 23:01:58 +08:00
|
|
|
|
}
|
2022-07-14 22:49:31 +08:00
|
|
|
|
// 记录rtp ext类型与id的关系,方便接收或发送rtp时修改rtp ext id
|
2023-08-12 15:24:20 +08:00
|
|
|
|
track->rtp_ext_ctx = std::make_shared<RtpExtContext>(m_answer);
|
2022-04-03 17:12:23 +08:00
|
|
|
|
weak_ptr<MediaTrack> weak_track = track;
|
|
|
|
|
track->rtp_ext_ctx->setOnGetRtp([this, weak_track](uint8_t pt, uint32_t ssrc, const string &rid) {
|
2022-07-14 22:49:31 +08:00
|
|
|
|
// ssrc --> MediaTrack
|
2022-04-03 17:12:23 +08:00
|
|
|
|
auto track = weak_track.lock();
|
|
|
|
|
assert(track);
|
|
|
|
|
_ssrc_to_track[ssrc] = std::move(track);
|
2022-07-14 22:49:31 +08:00
|
|
|
|
InfoL << "get rtp, pt:" << (int)pt << ", ssrc:" << ssrc << ", rid:" << rid;
|
2022-04-03 17:12:23 +08:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
size_t index = 0;
|
|
|
|
|
for (auto &ssrc : m_offer->rtp_ssrc_sim) {
|
2022-07-14 22:49:31 +08:00
|
|
|
|
// 记录ssrc对应的MediaTrack
|
2022-04-03 17:12:23 +08:00
|
|
|
|
_ssrc_to_track[ssrc.ssrc] = track;
|
|
|
|
|
if (m_offer->rtp_rids.size() > index) {
|
2022-07-14 22:49:31 +08:00
|
|
|
|
// 支持firefox的simulcast, 提前映射好ssrc和rid的关系
|
2022-04-03 17:12:23 +08:00
|
|
|
|
track->rtp_ext_ctx->setRid(ssrc.ssrc, m_offer->rtp_rids[index]);
|
2022-05-18 11:26:57 +08:00
|
|
|
|
} else {
|
|
|
|
|
// SDP munging没有rid, 它通过group-ssrc:SIM给出ssrc列表;
|
|
|
|
|
// 系统又要有rid,这里手工生成rid,并为其绑定ssrc
|
|
|
|
|
std::string rid = "r" + std::to_string(index);
|
|
|
|
|
track->rtp_ext_ctx->setRid(ssrc.ssrc, rid);
|
2022-07-09 21:44:57 +08:00
|
|
|
|
if (ssrc.rtx_ssrc) {
|
2022-05-18 11:26:57 +08:00
|
|
|
|
track->rtp_ext_ctx->setRid(ssrc.rtx_ssrc, rid);
|
2022-07-09 21:44:57 +08:00
|
|
|
|
}
|
2021-06-25 18:08:21 +08:00
|
|
|
|
}
|
2022-04-03 17:12:23 +08:00
|
|
|
|
++index;
|
2021-05-08 10:49:09 +08:00
|
|
|
|
}
|
2021-04-02 23:01:58 +08:00
|
|
|
|
}
|
2021-04-02 20:35:43 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-10-12 15:53:30 +08:00
|
|
|
|
void WebRtcTransportImp::onCheckAnswer(RtcSession &sdp) {
|
2022-07-14 22:49:31 +08:00
|
|
|
|
// 修改answer sdp的ip、端口信息
|
2022-09-18 21:03:05 +08:00
|
|
|
|
GET_CONFIG_FUNC(std::vector<std::string>, extern_ips, Rtc::kExternIP, [](string str) {
|
2022-05-12 15:21:09 +08:00
|
|
|
|
std::vector<std::string> ret;
|
2022-07-09 21:44:57 +08:00
|
|
|
|
if (str.length()) {
|
2022-05-12 15:21:09 +08:00
|
|
|
|
ret = split(str, ",");
|
2022-07-09 21:44:57 +08:00
|
|
|
|
}
|
|
|
|
|
translateIPFromEnv(ret);
|
2022-05-12 15:21:09 +08:00
|
|
|
|
return ret;
|
|
|
|
|
});
|
2021-04-27 01:16:01 +08:00
|
|
|
|
for (auto &m : sdp.media) {
|
|
|
|
|
m.addr.reset();
|
2024-03-23 22:46:30 +08:00
|
|
|
|
m.addr.address = extern_ips.empty() ? _local_ip.empty() ? SockUtil::get_local_ip() : _local_ip : extern_ips[0];
|
2021-04-27 01:16:01 +08:00
|
|
|
|
m.rtcp_addr.reset();
|
|
|
|
|
m.rtcp_addr.address = m.addr.address;
|
2021-09-08 18:00:55 +08:00
|
|
|
|
|
2022-11-19 09:52:10 +08:00
|
|
|
|
GET_CONFIG(uint16_t, udp_port, Rtc::kPort);
|
|
|
|
|
GET_CONFIG(uint16_t, tcp_port, Rtc::kTcpPort);
|
2023-08-29 11:22:30 +08:00
|
|
|
|
m.port = m.port ? (udp_port ? udp_port : tcp_port) : 0;
|
|
|
|
|
if (m.type != TrackApplication) {
|
|
|
|
|
m.rtcp_addr.port = m.port;
|
|
|
|
|
}
|
2021-04-27 01:16:01 +08:00
|
|
|
|
sdp.origin.address = m.addr.address;
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-12 20:51:51 +08:00
|
|
|
|
if (!canSendRtp()) {
|
2022-07-14 22:49:31 +08:00
|
|
|
|
// 设置我们发送的rtp的ssrc
|
2021-04-02 17:56:24 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
2021-04-02 20:35:43 +08:00
|
|
|
|
|
2021-04-02 17:56:24 +08:00
|
|
|
|
for (auto &m : sdp.media) {
|
|
|
|
|
if (m.type == TrackApplication) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2021-10-16 10:52:28 +08:00
|
|
|
|
if (!m.rtp_rtx_ssrc.empty()) {
|
2022-07-14 22:49:31 +08:00
|
|
|
|
// 已经生成了ssrc
|
2021-10-16 10:52:28 +08:00
|
|
|
|
continue;
|
|
|
|
|
}
|
2022-07-14 22:49:31 +08:00
|
|
|
|
// 添加answer sdp的ssrc信息
|
2021-05-08 18:39:35 +08:00
|
|
|
|
m.rtp_rtx_ssrc.emplace_back();
|
2021-10-14 15:10:55 +08:00
|
|
|
|
auto &ssrc = m.rtp_rtx_ssrc.back();
|
2022-07-14 22:49:31 +08:00
|
|
|
|
// 发送的ssrc我们随便定义,因为在发送rtp时会修改为此值
|
2021-10-15 16:27:17 +08:00
|
|
|
|
ssrc.ssrc = m.type + RTP_SSRC_OFFSET;
|
2021-10-14 15:10:55 +08:00
|
|
|
|
ssrc.cname = RTP_CNAME;
|
2024-03-10 21:34:03 +08:00
|
|
|
|
ssrc.label = std::string(RTP_LABEL) + '-' + m.mid;
|
2021-10-14 15:10:55 +08:00
|
|
|
|
ssrc.mslabel = RTP_MSLABEL;
|
2024-03-10 21:34:03 +08:00
|
|
|
|
ssrc.msid = ssrc.mslabel + ' ' + ssrc.label;
|
2021-05-08 15:01:14 +08:00
|
|
|
|
|
|
|
|
|
if (m.getRelatedRtxPlan(m.plan[0].pt)) {
|
2022-07-14 22:49:31 +08:00
|
|
|
|
// rtx ssrc
|
2021-10-14 15:10:55 +08:00
|
|
|
|
ssrc.rtx_ssrc = ssrc.ssrc + RTX_SSRC_OFFSET;
|
2021-04-02 23:01:58 +08:00
|
|
|
|
}
|
2021-04-02 17:56:24 +08:00
|
|
|
|
}
|
2021-03-24 16:52:41 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-10-12 17:04:21 +08:00
|
|
|
|
void WebRtcTransportImp::onCheckSdp(SdpType type, RtcSession &sdp) {
|
|
|
|
|
switch (type) {
|
2022-07-14 22:49:31 +08:00
|
|
|
|
case SdpType::answer:
|
|
|
|
|
onCheckAnswer(sdp);
|
|
|
|
|
break;
|
|
|
|
|
case SdpType::offer:
|
|
|
|
|
break;
|
|
|
|
|
default: /*不可达*/
|
|
|
|
|
assert(0);
|
|
|
|
|
break;
|
2021-10-12 17:04:21 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-14 22:49:31 +08:00
|
|
|
|
SdpAttrCandidate::Ptr
|
|
|
|
|
makeIceCandidate(std::string ip, uint16_t port, uint32_t priority = 100, std::string proto = "udp") {
|
2021-04-02 17:08:11 +08:00
|
|
|
|
auto candidate = std::make_shared<SdpAttrCandidate>();
|
2022-07-14 22:49:31 +08:00
|
|
|
|
// rtp端口
|
2021-04-02 17:08:11 +08:00
|
|
|
|
candidate->component = 1;
|
2022-05-12 15:21:09 +08:00
|
|
|
|
candidate->transport = proto;
|
|
|
|
|
candidate->foundation = proto + "candidate";
|
2022-07-14 22:49:31 +08:00
|
|
|
|
// 优先级,单candidate时随便
|
2022-05-12 15:21:09 +08:00
|
|
|
|
candidate->priority = priority;
|
2023-12-09 16:23:51 +08:00
|
|
|
|
candidate->address = std::move(ip);
|
2022-05-12 15:21:09 +08:00
|
|
|
|
candidate->port = port;
|
2021-04-02 17:08:11 +08:00
|
|
|
|
candidate->type = "host";
|
2022-11-18 22:52:57 +08:00
|
|
|
|
if (proto == "tcp") {
|
|
|
|
|
candidate->type += " tcptype passive";
|
|
|
|
|
}
|
2021-04-02 17:08:11 +08:00
|
|
|
|
return candidate;
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-12 15:21:09 +08:00
|
|
|
|
void WebRtcTransportImp::onRtcConfigure(RtcConfigure &configure) const {
|
|
|
|
|
WebRtcTransport::onRtcConfigure(configure);
|
2023-12-09 16:23:51 +08:00
|
|
|
|
if (!_cands.empty()) {
|
|
|
|
|
for (auto &cand : _cands) {
|
|
|
|
|
configure.addCandidate(cand);
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
2022-07-14 22:49:31 +08:00
|
|
|
|
|
2022-11-19 09:46:04 +08:00
|
|
|
|
GET_CONFIG(uint16_t, local_udp_port, Rtc::kPort);
|
2022-11-19 01:51:53 +08:00
|
|
|
|
GET_CONFIG(uint16_t, local_tcp_port, Rtc::kTcpPort);
|
2022-07-14 22:49:31 +08:00
|
|
|
|
// 添加接收端口candidate信息
|
2022-09-18 21:03:05 +08:00
|
|
|
|
GET_CONFIG_FUNC(std::vector<std::string>, extern_ips, Rtc::kExternIP, [](string str) {
|
2022-05-12 15:21:09 +08:00
|
|
|
|
std::vector<std::string> ret;
|
2022-07-09 21:44:57 +08:00
|
|
|
|
if (str.length()) {
|
2022-05-12 15:21:09 +08:00
|
|
|
|
ret = split(str, ",");
|
2022-07-09 21:44:57 +08:00
|
|
|
|
}
|
|
|
|
|
translateIPFromEnv(ret);
|
2022-05-12 15:21:09 +08:00
|
|
|
|
return ret;
|
|
|
|
|
});
|
|
|
|
|
if (extern_ips.empty()) {
|
2024-03-23 22:46:30 +08:00
|
|
|
|
std::string local_ip = _local_ip.empty() ? SockUtil::get_local_ip() : _local_ip;
|
2022-11-19 09:46:04 +08:00
|
|
|
|
if (local_udp_port) { configure.addCandidate(*makeIceCandidate(local_ip, local_udp_port, 120, "udp")); }
|
2022-11-30 18:24:35 +08:00
|
|
|
|
if (local_tcp_port) { configure.addCandidate(*makeIceCandidate(local_ip, local_tcp_port, _preferred_tcp ? 125 : 115, "tcp")); }
|
2022-07-09 21:44:57 +08:00
|
|
|
|
} else {
|
2022-05-12 15:21:09 +08:00
|
|
|
|
const uint32_t delta = 10;
|
|
|
|
|
uint32_t priority = 100 + delta * extern_ips.size();
|
|
|
|
|
for (auto ip : extern_ips) {
|
2022-11-27 12:43:16 +08:00
|
|
|
|
if (local_udp_port) { configure.addCandidate(*makeIceCandidate(ip, local_udp_port, priority, "udp")); }
|
2022-11-30 18:24:35 +08:00
|
|
|
|
if (local_tcp_port) { configure.addCandidate(*makeIceCandidate(ip, local_tcp_port, priority - (_preferred_tcp ? -5 : 5), "tcp")); }
|
2022-05-12 15:21:09 +08:00
|
|
|
|
priority -= delta;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-23 22:46:30 +08:00
|
|
|
|
void WebRtcTransportImp::setPreferredTcp(bool flag) {
|
|
|
|
|
_preferred_tcp = flag;
|
2023-12-09 16:23:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-03-23 22:46:30 +08:00
|
|
|
|
void WebRtcTransportImp::setLocalIp(std::string local_ip) {
|
|
|
|
|
_local_ip = std::move(local_ip);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WebRtcTransportImp::setIceCandidate(vector<SdpAttrCandidate> cands) {
|
|
|
|
|
_cands = std::move(cands);
|
2024-01-26 10:26:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-03 08:45:18 +08:00
|
|
|
|
///////////////////////////////////////////////////////////////////
|
|
|
|
|
|
2023-03-11 10:17:31 +08:00
|
|
|
|
class RtpChannel : public RtpTrackImp, public std::enable_shared_from_this<RtpChannel> {
|
2021-06-25 14:37:11 +08:00
|
|
|
|
public:
|
2021-07-27 20:37:43 +08:00
|
|
|
|
RtpChannel(EventPoller::Ptr poller, RtpTrackImp::OnSorted cb, function<void(const FCI_NACK &nack)> on_nack) {
|
|
|
|
|
_poller = std::move(poller);
|
|
|
|
|
_on_nack = std::move(on_nack);
|
2021-06-25 16:24:44 +08:00
|
|
|
|
setOnSorted(std::move(cb));
|
2023-03-11 10:17:31 +08:00
|
|
|
|
//设置jitter buffer参数
|
2024-05-12 18:15:11 +08:00
|
|
|
|
GET_CONFIG(uint32_t, nack_maxms, Rtc::kNackMaxMS);
|
2024-06-21 20:21:45 +08:00
|
|
|
|
GET_CONFIG(uint32_t, nack_max_rtp, Rtc::kNackMaxSize);
|
|
|
|
|
RtpTrackImp::setParams(nack_max_rtp, nack_maxms, nack_max_rtp / 2);
|
2022-07-14 22:49:31 +08:00
|
|
|
|
_nack_ctx.setOnNack([this](const FCI_NACK &nack) { onNack(nack); });
|
2021-04-02 23:01:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-07-14 21:39:30 +08:00
|
|
|
|
RtpPacket::Ptr inputRtp(TrackType type, int sample_rate, uint8_t *ptr, size_t len, bool is_rtx) {
|
|
|
|
|
auto rtp = RtpTrack::inputRtp(type, sample_rate, ptr, len);
|
2021-07-27 20:37:43 +08:00
|
|
|
|
if (!rtp) {
|
|
|
|
|
return rtp;
|
|
|
|
|
}
|
|
|
|
|
auto seq = rtp->getSeq();
|
|
|
|
|
_nack_ctx.received(seq, is_rtx);
|
|
|
|
|
if (!is_rtx) {
|
2022-07-14 22:49:31 +08:00
|
|
|
|
// 统计rtp接受情况,便于生成nack rtcp包
|
2021-07-14 21:39:30 +08:00
|
|
|
|
_rtcp_context.onRtp(seq, rtp->getStamp(), rtp->ntp_stamp, sample_rate, len);
|
2021-06-25 14:37:11 +08:00
|
|
|
|
}
|
2021-07-14 21:39:30 +08:00
|
|
|
|
return rtp;
|
2021-04-02 23:01:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-25 14:59:27 +08:00
|
|
|
|
Buffer::Ptr createRtcpRR(RtcpHeader *sr, uint32_t ssrc) {
|
2021-06-25 16:24:44 +08:00
|
|
|
|
_rtcp_context.onRtcp(sr);
|
2021-06-25 17:10:27 +08:00
|
|
|
|
return _rtcp_context.createRtcpRR(ssrc, getSSRC());
|
2021-04-02 23:01:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-09-03 15:53:01 +08:00
|
|
|
|
float getLossRate() {
|
2022-07-14 22:49:31 +08:00
|
|
|
|
auto expected = _rtcp_context.getExpectedPacketsInterval();
|
2022-06-11 14:23:43 +08:00
|
|
|
|
if (!expected) {
|
2022-09-03 15:53:01 +08:00
|
|
|
|
return -1;
|
2022-06-11 12:56:03 +08:00
|
|
|
|
}
|
2023-05-02 20:40:40 +08:00
|
|
|
|
return _rtcp_context.getLostInterval() * 100 / expected;
|
2021-07-27 20:37:43 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
2022-07-14 22:49:31 +08:00
|
|
|
|
void starNackTimer() {
|
2021-07-27 20:37:43 +08:00
|
|
|
|
if (_delay_task) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
weak_ptr<RtpChannel> weak_self = shared_from_this();
|
|
|
|
|
_delay_task = _poller->doDelayTask(10, [weak_self]() -> uint64_t {
|
|
|
|
|
auto strong_self = weak_self.lock();
|
|
|
|
|
if (!strong_self) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
auto ret = strong_self->_nack_ctx.reSendNack();
|
|
|
|
|
if (!ret) {
|
|
|
|
|
strong_self->_delay_task = nullptr;
|
|
|
|
|
}
|
|
|
|
|
return ret;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void onNack(const FCI_NACK &nack) {
|
|
|
|
|
_on_nack(nack);
|
|
|
|
|
starNackTimer();
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-02 23:01:58 +08:00
|
|
|
|
private:
|
2021-06-25 16:24:44 +08:00
|
|
|
|
NackContext _nack_ctx;
|
2021-10-13 15:06:13 +08:00
|
|
|
|
RtcpContextForRecv _rtcp_context;
|
2021-07-27 20:37:43 +08:00
|
|
|
|
EventPoller::Ptr _poller;
|
2022-02-02 20:34:50 +08:00
|
|
|
|
EventPoller::DelayTask::Ptr _delay_task;
|
2021-07-27 20:37:43 +08:00
|
|
|
|
function<void(const FCI_NACK &nack)> _on_nack;
|
2021-04-02 23:01:58 +08:00
|
|
|
|
};
|
|
|
|
|
|
2022-07-14 22:49:31 +08:00
|
|
|
|
std::shared_ptr<RtpChannel> MediaTrack::getRtpChannel(uint32_t ssrc) const {
|
2021-06-25 15:31:13 +08:00
|
|
|
|
auto it_chn = rtp_channel.find(rtp_ext_ctx->getRid(ssrc));
|
2021-06-25 14:37:11 +08:00
|
|
|
|
if (it_chn == rtp_channel.end()) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
return it_chn->second;
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-18 21:03:05 +08:00
|
|
|
|
float WebRtcTransportImp::getLossRate(TrackType type) {
|
2022-06-11 12:56:03 +08:00
|
|
|
|
for (auto &pr : _ssrc_to_track) {
|
|
|
|
|
auto ssrc = pr.first;
|
|
|
|
|
auto &track = pr.second;
|
|
|
|
|
auto rtp_chn = track->getRtpChannel(ssrc);
|
|
|
|
|
if (rtp_chn) {
|
|
|
|
|
if (track->media && type == track->media->type) {
|
2022-06-11 12:31:06 +08:00
|
|
|
|
return rtp_chn->getLossRate();
|
|
|
|
|
}
|
2022-06-11 12:56:03 +08:00
|
|
|
|
}
|
2022-06-11 12:31:06 +08:00
|
|
|
|
}
|
2022-06-11 12:56:03 +08:00
|
|
|
|
return -1;
|
2022-06-11 12:31:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-02 17:56:24 +08:00
|
|
|
|
void WebRtcTransportImp::onRtcp(const char *buf, size_t len) {
|
2021-04-07 18:35:38 +08:00
|
|
|
|
_bytes_usage += len;
|
2022-07-14 22:49:31 +08:00
|
|
|
|
auto rtcps = RtcpHeader::loadFromBytes((char *)buf, len);
|
2021-04-03 00:04:52 +08:00
|
|
|
|
for (auto rtcp : rtcps) {
|
2022-07-14 22:49:31 +08:00
|
|
|
|
switch ((RtcpType)rtcp->pt) {
|
|
|
|
|
case RtcpType::RTCP_SR: {
|
2022-09-09 23:04:28 +08:00
|
|
|
|
_alive_ticker.resetTime();
|
2022-07-14 22:49:31 +08:00
|
|
|
|
// 对方汇报rtp发送情况
|
|
|
|
|
RtcpSR *sr = (RtcpSR *)rtcp;
|
|
|
|
|
auto it = _ssrc_to_track.find(sr->ssrc);
|
|
|
|
|
if (it != _ssrc_to_track.end()) {
|
|
|
|
|
auto &track = it->second;
|
|
|
|
|
auto rtp_chn = track->getRtpChannel(sr->ssrc);
|
|
|
|
|
if (!rtp_chn) {
|
|
|
|
|
WarnL << "未识别的sr rtcp包:" << rtcp->dumpString();
|
|
|
|
|
} else {
|
|
|
|
|
// 设置rtp时间戳与ntp时间戳的对应关系
|
|
|
|
|
rtp_chn->setNtpStamp(sr->rtpts, sr->getNtpUnixStampMS());
|
|
|
|
|
auto rr = rtp_chn->createRtcpRR(sr, track->answer_ssrc_rtp);
|
|
|
|
|
sendRtcpPacket(rr->data(), rr->size(), true);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
WarnL << "未识别的sr rtcp包:" << rtcp->dumpString();
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case RtcpType::RTCP_RR: {
|
|
|
|
|
_alive_ticker.resetTime();
|
|
|
|
|
// 对方汇报rtp接收情况
|
|
|
|
|
RtcpRR *rr = (RtcpRR *)rtcp;
|
|
|
|
|
for (auto item : rr->getItemList()) {
|
|
|
|
|
auto it = _ssrc_to_track.find(item->ssrc);
|
2021-06-25 14:59:27 +08:00
|
|
|
|
if (it != _ssrc_to_track.end()) {
|
2021-06-25 17:17:48 +08:00
|
|
|
|
auto &track = it->second;
|
2022-07-14 22:49:31 +08:00
|
|
|
|
track->rtcp_context_send->onRtcp(rtcp);
|
|
|
|
|
auto sr = track->rtcp_context_send->createRtcpSR(track->answer_ssrc_rtp);
|
|
|
|
|
sendRtcpPacket(sr->data(), sr->size(), true);
|
2021-05-08 10:49:09 +08:00
|
|
|
|
} else {
|
2022-07-14 22:49:31 +08:00
|
|
|
|
WarnL << "未识别的rr rtcp包:" << rtcp->dumpString();
|
2021-04-03 00:04:52 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-07-14 22:49:31 +08:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case RtcpType::RTCP_BYE: {
|
|
|
|
|
// 对方汇报停止发送rtp
|
|
|
|
|
RtcpBye *bye = (RtcpBye *)rtcp;
|
|
|
|
|
for (auto ssrc : bye->getSSRC()) {
|
|
|
|
|
auto it = _ssrc_to_track.find(*ssrc);
|
|
|
|
|
if (it == _ssrc_to_track.end()) {
|
|
|
|
|
WarnL << "未识别的bye rtcp包:" << rtcp->dumpString();
|
|
|
|
|
continue;
|
2021-04-03 00:04:52 +08:00
|
|
|
|
}
|
2022-07-14 22:49:31 +08:00
|
|
|
|
_ssrc_to_track.erase(it);
|
|
|
|
|
}
|
2022-09-07 19:10:06 +08:00
|
|
|
|
onRtcpBye();
|
2022-11-28 17:52:37 +08:00
|
|
|
|
// bye 会在 sender audio track mute 时出现, 因此不能作为 shutdown 的依据
|
2022-07-14 22:49:31 +08:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case RtcpType::RTCP_PSFB:
|
|
|
|
|
case RtcpType::RTCP_RTPFB: {
|
|
|
|
|
if ((RtcpType)rtcp->pt == RtcpType::RTCP_PSFB) {
|
2021-04-03 00:04:52 +08:00
|
|
|
|
break;
|
|
|
|
|
}
|
2022-07-14 22:49:31 +08:00
|
|
|
|
// RTPFB
|
|
|
|
|
switch ((RTPFBType)rtcp->report_count) {
|
|
|
|
|
case RTPFBType::RTCP_RTPFB_NACK: {
|
|
|
|
|
RtcpFB *fb = (RtcpFB *)rtcp;
|
|
|
|
|
auto it = _ssrc_to_track.find(fb->ssrc_media);
|
|
|
|
|
if (it == _ssrc_to_track.end()) {
|
|
|
|
|
WarnL << "未识别的 rtcp包:" << rtcp->dumpString();
|
|
|
|
|
return;
|
2021-04-07 17:21:59 +08:00
|
|
|
|
}
|
2022-07-14 22:49:31 +08:00
|
|
|
|
auto &track = it->second;
|
|
|
|
|
auto &fci = fb->getFci<FCI_NACK>();
|
|
|
|
|
track->nack_list.forEach(fci, [&](const RtpPacket::Ptr &rtp) {
|
|
|
|
|
// rtp重传
|
|
|
|
|
onSendRtp(rtp, true, true);
|
|
|
|
|
});
|
2021-04-03 08:45:18 +08:00
|
|
|
|
break;
|
|
|
|
|
}
|
2022-07-14 22:49:31 +08:00
|
|
|
|
default:
|
2021-04-22 17:34:26 +08:00
|
|
|
|
break;
|
|
|
|
|
}
|
2022-07-14 22:49:31 +08:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case RtcpType::RTCP_XR: {
|
|
|
|
|
RtcpXRRRTR *xr = (RtcpXRRRTR *)rtcp;
|
|
|
|
|
if (xr->bt != 4) {
|
2022-07-12 19:26:17 +08:00
|
|
|
|
break;
|
|
|
|
|
}
|
2022-07-14 22:49:31 +08:00
|
|
|
|
auto it = _ssrc_to_track.find(xr->ssrc);
|
|
|
|
|
if (it == _ssrc_to_track.end()) {
|
|
|
|
|
WarnL << "未识别的 rtcp包:" << rtcp->dumpString();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
auto &track = it->second;
|
|
|
|
|
track->rtcp_context_send->onRtcp(rtcp);
|
|
|
|
|
auto xrdlrr = track->rtcp_context_send->createRtcpXRDLRR(track->answer_ssrc_rtp, track->answer_ssrc_rtp);
|
|
|
|
|
sendRtcpPacket(xrdlrr->data(), xrdlrr->size(), true);
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
break;
|
2021-04-03 00:04:52 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-11 15:48:01 +08:00
|
|
|
|
///////////////////////////////////////////////////////////////////
|
|
|
|
|
|
2021-09-07 16:33:57 +08:00
|
|
|
|
void WebRtcTransportImp::createRtpChannel(const string &rid, uint32_t ssrc, MediaTrack &track) {
|
2022-07-14 22:49:31 +08:00
|
|
|
|
// rid --> RtpReceiverImp
|
2021-09-07 16:33:57 +08:00
|
|
|
|
auto &ref = track.rtp_channel[rid];
|
2023-04-28 22:04:38 +08:00
|
|
|
|
weak_ptr<WebRtcTransportImp> weak_self = static_pointer_cast<WebRtcTransportImp>(shared_from_this());
|
2022-07-14 22:49:31 +08:00
|
|
|
|
ref = std::make_shared<RtpChannel>(
|
|
|
|
|
getPoller(), [&track, this, rid](RtpPacket::Ptr rtp) mutable { onSortedRtp(track, rid, std::move(rtp)); },
|
|
|
|
|
[&track, weak_self, ssrc](const FCI_NACK &nack) mutable {
|
|
|
|
|
// nack发送可能由定时器异步触发
|
|
|
|
|
auto strong_self = weak_self.lock();
|
|
|
|
|
if (strong_self) {
|
|
|
|
|
strong_self->onSendNack(track, nack, ssrc);
|
|
|
|
|
}
|
|
|
|
|
});
|
2021-09-07 16:33:57 +08:00
|
|
|
|
InfoL << "create rtp receiver of ssrc:" << ssrc << ", rid:" << rid << ", codec:" << track.plan_rtp->codec;
|
2021-06-24 17:54:16 +08:00
|
|
|
|
}
|
2021-06-23 21:44:04 +08:00
|
|
|
|
|
2021-10-16 10:52:28 +08:00
|
|
|
|
void WebRtcTransportImp::updateTicker() {
|
|
|
|
|
_alive_ticker.resetTime();
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-07 15:49:33 +08:00
|
|
|
|
void WebRtcTransportImp::onRtp(const char *buf, size_t len, uint64_t stamp_ms) {
|
2021-06-24 17:54:16 +08:00
|
|
|
|
_bytes_usage += len;
|
|
|
|
|
_alive_ticker.resetTime();
|
|
|
|
|
|
2022-07-14 22:49:31 +08:00
|
|
|
|
RtpHeader *rtp = (RtpHeader *)buf;
|
|
|
|
|
// 根据接收到的rtp的pt信息,找到该流的信息
|
2021-06-25 14:59:27 +08:00
|
|
|
|
auto it = _pt_to_track.find(rtp->pt);
|
|
|
|
|
if (it == _pt_to_track.end()) {
|
2021-06-24 17:54:16 +08:00
|
|
|
|
WarnL << "unknown rtp pt:" << (int)rtp->pt;
|
2021-04-03 08:45:18 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
2021-11-18 18:41:23 +08:00
|
|
|
|
it->second->inputRtp(buf, len, stamp_ms, rtp);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WrappedRtpTrack::inputRtp(const char *buf, size_t len, uint64_t stamp_ms, RtpHeader *rtp) {
|
2022-02-27 00:33:39 +08:00
|
|
|
|
#if 0
|
|
|
|
|
auto seq = ntohs(rtp->seq);
|
|
|
|
|
if (track->media->type == TrackVideo && seq % 100 == 0) {
|
|
|
|
|
//此处模拟接受丢包
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2022-07-14 22:49:31 +08:00
|
|
|
|
|
2021-06-24 17:54:16 +08:00
|
|
|
|
auto ssrc = ntohl(rtp->ssrc);
|
|
|
|
|
|
2022-07-14 22:49:31 +08:00
|
|
|
|
// 修改ext id至统一
|
2021-06-24 17:54:16 +08:00
|
|
|
|
string rid;
|
2021-10-06 22:17:38 +08:00
|
|
|
|
auto twcc_ext = track->rtp_ext_ctx->changeRtpExtId(rtp, true, &rid, RtpExtType::transport_cc);
|
2021-11-18 18:41:23 +08:00
|
|
|
|
|
|
|
|
|
if (twcc_ext) {
|
2021-10-07 15:49:33 +08:00
|
|
|
|
_twcc_ctx.onRtp(ssrc, twcc_ext.getTransportCCSeq(), stamp_ms);
|
2021-10-06 22:17:38 +08:00
|
|
|
|
}
|
2021-06-24 17:54:16 +08:00
|
|
|
|
|
2021-06-25 17:17:48 +08:00
|
|
|
|
auto &ref = track->rtp_channel[rid];
|
2021-06-24 17:54:16 +08:00
|
|
|
|
if (!ref) {
|
2021-11-18 18:41:23 +08:00
|
|
|
|
_transport.createRtpChannel(rid, ssrc, *track);
|
2021-06-24 17:54:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-07-14 22:49:31 +08:00
|
|
|
|
// 解析并排序rtp
|
|
|
|
|
ref->inputRtp(track->media->type, track->plan_rtp->sample_rate, (uint8_t *)buf, len, false);
|
2021-11-18 18:41:23 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WrappedRtxTrack::inputRtp(const char *buf, size_t len, uint64_t stamp_ms, RtpHeader *rtp) {
|
2022-07-14 22:49:31 +08:00
|
|
|
|
// 修改ext id至统一
|
2021-11-18 18:41:23 +08:00
|
|
|
|
string rid;
|
|
|
|
|
track->rtp_ext_ctx->changeRtpExtId(rtp, true, &rid, RtpExtType::transport_cc);
|
|
|
|
|
|
|
|
|
|
auto &ref = track->rtp_channel[rid];
|
|
|
|
|
if (!ref) {
|
2022-07-14 22:49:31 +08:00
|
|
|
|
// 再接收到对应的rtp前,丢弃rtx包
|
|
|
|
|
WarnL << "unknown rtx rtp, rid:" << rid << ", ssrc:" << ntohl(rtp->ssrc) << ", codec:" << track->plan_rtp->codec
|
|
|
|
|
<< ", seq:" << ntohs(rtp->seq);
|
2021-05-11 15:48:01 +08:00
|
|
|
|
return;
|
2021-05-11 11:18:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-07-14 22:49:31 +08:00
|
|
|
|
// 这里是rtx重传包
|
|
|
|
|
// https://datatracker.ietf.org/doc/html/rfc4588#section-4
|
2021-05-11 15:48:01 +08:00
|
|
|
|
auto payload = rtp->getPayloadData();
|
|
|
|
|
auto size = rtp->getPayloadSize(len);
|
|
|
|
|
if (size < 2) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-06-23 21:44:04 +08:00
|
|
|
|
|
2022-07-14 22:49:31 +08:00
|
|
|
|
// 前两个字节是原始的rtp的seq
|
2021-05-11 15:48:01 +08:00
|
|
|
|
auto origin_seq = payload[0] << 8 | payload[1];
|
2021-11-18 18:41:23 +08:00
|
|
|
|
// rtx 转换为 rtp
|
2021-06-25 17:17:48 +08:00
|
|
|
|
rtp->pt = track->plan_rtp->pt;
|
2021-06-25 14:37:11 +08:00
|
|
|
|
rtp->seq = htons(origin_seq);
|
2021-06-25 17:10:27 +08:00
|
|
|
|
rtp->ssrc = htonl(ref->getSSRC());
|
2021-06-25 14:37:11 +08:00
|
|
|
|
|
2022-07-14 22:49:31 +08:00
|
|
|
|
memmove((uint8_t *)buf + 2, buf, payload - (uint8_t *)buf);
|
2021-05-11 15:48:01 +08:00
|
|
|
|
buf += 2;
|
|
|
|
|
len -= 2;
|
2022-07-14 22:49:31 +08:00
|
|
|
|
ref->inputRtp(track->media->type, track->plan_rtp->sample_rate, (uint8_t *)buf, len, true);
|
2021-04-03 08:45:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-25 17:17:48 +08:00
|
|
|
|
void WebRtcTransportImp::onSendNack(MediaTrack &track, const FCI_NACK &nack, uint32_t ssrc) {
|
2021-05-11 12:12:28 +08:00
|
|
|
|
auto rtcp = RtcpFB::create(RTPFBType::RTCP_RTPFB_NACK, &nack, FCI_NACK::kSize);
|
2022-02-26 22:33:00 +08:00
|
|
|
|
rtcp->ssrc = htonl(track.answer_ssrc_rtp);
|
2021-06-23 21:44:04 +08:00
|
|
|
|
rtcp->ssrc_media = htonl(ssrc);
|
2022-07-14 22:49:31 +08:00
|
|
|
|
sendRtcpPacket((char *)rtcp.get(), rtcp->getSize(), true);
|
2021-05-11 12:12:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-10-07 14:22:12 +08:00
|
|
|
|
void WebRtcTransportImp::onSendTwcc(uint32_t ssrc, const string &twcc_fci) {
|
|
|
|
|
auto rtcp = RtcpFB::create(RTPFBType::RTCP_RTPFB_TWCC, twcc_fci.data(), twcc_fci.size());
|
2022-02-26 22:33:00 +08:00
|
|
|
|
rtcp->ssrc = htonl(0);
|
2021-10-07 14:22:12 +08:00
|
|
|
|
rtcp->ssrc_media = htonl(ssrc);
|
2022-07-14 22:49:31 +08:00
|
|
|
|
sendRtcpPacket((char *)rtcp.get(), rtcp->getSize(), true);
|
2021-10-07 14:22:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-03 08:45:18 +08:00
|
|
|
|
///////////////////////////////////////////////////////////////////
|
2021-04-03 08:32:20 +08:00
|
|
|
|
|
2021-06-25 17:17:48 +08:00
|
|
|
|
void WebRtcTransportImp::onSortedRtp(MediaTrack &track, const string &rid, RtpPacket::Ptr rtp) {
|
|
|
|
|
if (track.media->type == TrackVideo && _pli_ticker.elapsedTime() > 2000) {
|
2022-07-14 22:49:31 +08:00
|
|
|
|
// 定期发送pli请求关键帧,方便非rtc等协议
|
2021-04-02 23:12:37 +08:00
|
|
|
|
_pli_ticker.resetTime();
|
2021-05-08 10:49:09 +08:00
|
|
|
|
sendRtcpPli(rtp->getSSRC());
|
2021-04-30 14:49:06 +08:00
|
|
|
|
|
2022-07-14 22:49:31 +08:00
|
|
|
|
// 开启remb,则发送remb包调节比特率
|
2022-09-18 21:03:05 +08:00
|
|
|
|
GET_CONFIG(size_t, remb_bit_rate, Rtc::kRembBitRate);
|
2021-10-12 15:34:36 +08:00
|
|
|
|
if (remb_bit_rate && _answer_sdp->supportRtcpFb(SdpConst::kRembRtcpFb)) {
|
2021-05-08 10:49:09 +08:00
|
|
|
|
sendRtcpRemb(rtp->getSSRC(), remb_bit_rate);
|
2021-04-30 14:49:06 +08:00
|
|
|
|
}
|
2021-04-02 23:12:37 +08:00
|
|
|
|
}
|
2021-05-07 18:34:04 +08:00
|
|
|
|
|
2021-10-15 16:27:17 +08:00
|
|
|
|
onRecvRtp(track, rid, std::move(rtp));
|
2021-05-08 10:49:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-11 15:48:01 +08:00
|
|
|
|
///////////////////////////////////////////////////////////////////
|
2021-04-03 08:32:20 +08:00
|
|
|
|
|
2021-10-15 16:27:17 +08:00
|
|
|
|
void WebRtcTransportImp::onSendRtp(const RtpPacket::Ptr &rtp, bool flush, bool rtx) {
|
2021-06-25 17:17:48 +08:00
|
|
|
|
auto &track = _type_to_track[rtp->type];
|
|
|
|
|
if (!track) {
|
2022-07-14 22:49:31 +08:00
|
|
|
|
// 忽略,对方不支持该编码类型
|
2021-04-03 08:32:20 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
2021-05-11 00:54:33 +08:00
|
|
|
|
if (!rtx) {
|
2022-07-14 22:49:31 +08:00
|
|
|
|
// 统计rtp发送情况,好做sr汇报
|
|
|
|
|
track->rtcp_context_send->onRtp(
|
|
|
|
|
rtp->getSeq(), rtp->getStamp(), rtp->ntp_stamp, rtp->sample_rate,
|
|
|
|
|
rtp->size() - RtpPacket::kRtpTcpHeaderSize);
|
2022-02-27 02:07:25 +08:00
|
|
|
|
track->nack_list.pushBack(rtp);
|
2021-05-12 20:53:37 +08:00
|
|
|
|
#if 0
|
2021-05-11 11:18:55 +08:00
|
|
|
|
//此处模拟发送丢包
|
2021-05-16 16:12:10 +08:00
|
|
|
|
if (rtp->type == TrackVideo && rtp->getSeq() % 100 == 0) {
|
2021-05-11 11:18:55 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2021-05-11 00:54:33 +08:00
|
|
|
|
} else {
|
2022-07-14 22:49:31 +08:00
|
|
|
|
// 发送rtx重传包
|
|
|
|
|
// TraceL << "send rtx rtp:" << rtp->getSeq();
|
2021-05-11 00:54:33 +08:00
|
|
|
|
}
|
2022-07-14 22:49:31 +08:00
|
|
|
|
pair<bool /*rtx*/, MediaTrack *> ctx { rtx, track.get() };
|
2021-05-12 20:51:51 +08:00
|
|
|
|
sendRtpPacket(rtp->data() + RtpPacket::kRtpTcpHeaderSize, rtp->size() - RtpPacket::kRtpTcpHeaderSize, flush, &ctx);
|
2021-04-07 18:35:38 +08:00
|
|
|
|
_bytes_usage += rtp->size() - RtpPacket::kRtpTcpHeaderSize;
|
2021-04-03 08:32:20 +08:00
|
|
|
|
}
|
2021-04-07 17:21:59 +08:00
|
|
|
|
|
2021-08-13 16:07:27 +08:00
|
|
|
|
void WebRtcTransportImp::onBeforeEncryptRtp(const char *buf, int &len, void *ctx) {
|
2022-07-14 22:49:31 +08:00
|
|
|
|
auto pr = (pair<bool /*rtx*/, MediaTrack *> *)ctx;
|
|
|
|
|
auto header = (RtpHeader *)buf;
|
2021-05-08 21:16:51 +08:00
|
|
|
|
|
2021-05-12 20:51:51 +08:00
|
|
|
|
if (!pr->first || !pr->second->plan_rtx) {
|
2022-07-14 22:49:31 +08:00
|
|
|
|
// 普通的rtp,或者不支持rtx, 修改目标pt和ssrc
|
2021-06-25 15:31:13 +08:00
|
|
|
|
pr->second->rtp_ext_ctx->changeRtpExtId(header, false);
|
2021-05-12 20:51:51 +08:00
|
|
|
|
header->pt = pr->second->plan_rtp->pt;
|
2021-05-16 16:12:10 +08:00
|
|
|
|
header->ssrc = htonl(pr->second->answer_ssrc_rtp);
|
2021-05-12 20:51:51 +08:00
|
|
|
|
} else {
|
2022-07-14 22:49:31 +08:00
|
|
|
|
// 重传的rtp, rtx
|
2021-06-25 15:31:13 +08:00
|
|
|
|
pr->second->rtp_ext_ctx->changeRtpExtId(header, false);
|
2021-05-12 20:51:51 +08:00
|
|
|
|
header->pt = pr->second->plan_rtx->pt;
|
2021-05-16 16:12:10 +08:00
|
|
|
|
if (pr->second->answer_ssrc_rtx) {
|
2022-07-14 22:49:31 +08:00
|
|
|
|
// 有rtx单独的ssrc,有些情况下,浏览器支持rtx,但是未指定rtx单独的ssrc
|
2021-05-16 16:12:10 +08:00
|
|
|
|
header->ssrc = htonl(pr->second->answer_ssrc_rtx);
|
2021-05-16 18:06:34 +08:00
|
|
|
|
} else {
|
2022-07-14 22:49:31 +08:00
|
|
|
|
// 未单独指定rtx的ssrc,那么使用rtp的ssrc
|
2021-05-16 18:06:34 +08:00
|
|
|
|
header->ssrc = htonl(pr->second->answer_ssrc_rtp);
|
2021-05-12 20:51:51 +08:00
|
|
|
|
}
|
2021-05-11 00:54:33 +08:00
|
|
|
|
|
2021-05-12 20:51:51 +08:00
|
|
|
|
auto origin_seq = ntohs(header->seq);
|
2022-07-14 22:49:31 +08:00
|
|
|
|
// seq跟原来的不一样
|
2021-12-30 15:28:02 +08:00
|
|
|
|
header->seq = htons(_rtx_seq[pr->second->media->type]);
|
|
|
|
|
++_rtx_seq[pr->second->media->type];
|
|
|
|
|
|
2021-05-12 20:51:51 +08:00
|
|
|
|
auto payload = header->getPayloadData();
|
|
|
|
|
auto payload_size = header->getPayloadSize(len);
|
|
|
|
|
if (payload_size) {
|
2022-07-14 22:49:31 +08:00
|
|
|
|
// rtp负载后移两个字节,这两个字节用于存放osn
|
|
|
|
|
// https://datatracker.ietf.org/doc/html/rfc4588#section-4
|
2021-05-12 20:51:51 +08:00
|
|
|
|
memmove(payload + 2, payload, payload_size);
|
|
|
|
|
}
|
|
|
|
|
payload[0] = origin_seq >> 8;
|
|
|
|
|
payload[1] = origin_seq & 0xFF;
|
|
|
|
|
len += 2;
|
|
|
|
|
}
|
2021-05-11 00:54:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-28 22:06:46 +08:00
|
|
|
|
void WebRtcTransportImp::safeShutdown(const SockException &ex) {
|
|
|
|
|
std::weak_ptr<WebRtcTransportImp> weak_self = static_pointer_cast<WebRtcTransportImp>(shared_from_this());
|
|
|
|
|
getPoller()->async([ex, weak_self]() {
|
|
|
|
|
if (auto strong_self = weak_self.lock()) {
|
|
|
|
|
strong_self->onShutdown(ex);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-14 22:49:31 +08:00
|
|
|
|
void WebRtcTransportImp::onShutdown(const SockException &ex) {
|
2023-04-23 00:10:18 +08:00
|
|
|
|
WarnL << ex;
|
2021-09-10 22:31:44 +08:00
|
|
|
|
unrefSelf();
|
2023-03-03 11:18:21 +08:00
|
|
|
|
for (auto &tuple : _ice_server->GetTuples()) {
|
|
|
|
|
tuple->shutdown(ex);
|
2021-09-10 22:31:44 +08:00
|
|
|
|
}
|
2021-04-07 17:21:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-03-03 11:18:21 +08:00
|
|
|
|
void WebRtcTransportImp::removeTuple(RTC::TransportTuple *tuple) {
|
|
|
|
|
InfoL << getIdentifier() << " remove tuple " << tuple->get_peer_ip() << ":" << tuple->get_peer_port();
|
|
|
|
|
this->_ice_server->RemoveTuple(tuple);
|
2021-04-07 18:18:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-07-14 22:49:31 +08:00
|
|
|
|
uint64_t WebRtcTransportImp::getBytesUsage() const {
|
2021-10-15 16:27:17 +08:00
|
|
|
|
return _bytes_usage;
|
2021-04-07 18:18:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-07-14 22:49:31 +08:00
|
|
|
|
uint64_t WebRtcTransportImp::getDuration() const {
|
2021-10-15 16:27:17 +08:00
|
|
|
|
return _alive_ticker.createdTime() / 1000;
|
2021-09-10 22:31:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-09-07 19:10:06 +08:00
|
|
|
|
void WebRtcTransportImp::onRtcpBye(){}
|
|
|
|
|
|
2021-10-15 16:27:17 +08:00
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////////
|
2021-09-10 22:31:44 +08:00
|
|
|
|
|
|
|
|
|
void WebRtcTransportImp::registerSelf() {
|
2022-07-14 22:49:31 +08:00
|
|
|
|
_self = static_pointer_cast<WebRtcTransportImp>(shared_from_this());
|
2021-10-16 10:25:23 +08:00
|
|
|
|
WebRtcTransportManager::Instance().addItem(getIdentifier(), _self);
|
2021-04-07 20:33:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-09-10 22:31:44 +08:00
|
|
|
|
void WebRtcTransportImp::unrefSelf() {
|
|
|
|
|
_self = nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WebRtcTransportImp::unregisterSelf() {
|
|
|
|
|
unrefSelf();
|
2021-10-16 10:25:23 +08:00
|
|
|
|
WebRtcTransportManager::Instance().removeItem(getIdentifier());
|
2021-09-10 22:31:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-10-16 10:25:23 +08:00
|
|
|
|
WebRtcTransportManager &WebRtcTransportManager::Instance() {
|
2021-10-15 18:56:49 +08:00
|
|
|
|
static WebRtcTransportManager s_instance;
|
|
|
|
|
return s_instance;
|
2021-10-13 14:15:29 +08:00
|
|
|
|
}
|
2021-10-16 10:25:23 +08:00
|
|
|
|
|
2021-10-19 15:23:12 +08:00
|
|
|
|
void WebRtcTransportManager::addItem(const string &key, const WebRtcTransportImp::Ptr &ptr) {
|
2021-10-15 18:56:49 +08:00
|
|
|
|
lock_guard<mutex> lck(_mtx);
|
|
|
|
|
_map[key] = ptr;
|
|
|
|
|
}
|
2021-10-16 10:25:23 +08:00
|
|
|
|
|
2021-10-15 18:56:49 +08:00
|
|
|
|
WebRtcTransportImp::Ptr WebRtcTransportManager::getItem(const string &key) {
|
|
|
|
|
if (key.empty()) {
|
|
|
|
|
return nullptr;
|
2021-09-10 22:31:44 +08:00
|
|
|
|
}
|
2021-10-15 18:56:49 +08:00
|
|
|
|
lock_guard<mutex> lck(_mtx);
|
|
|
|
|
auto it = _map.find(key);
|
|
|
|
|
if (it == _map.end()) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
return it->second.lock();
|
|
|
|
|
}
|
2021-10-16 10:25:23 +08:00
|
|
|
|
|
2021-10-19 15:23:12 +08:00
|
|
|
|
void WebRtcTransportManager::removeItem(const string &key) {
|
2021-10-15 18:56:49 +08:00
|
|
|
|
lock_guard<mutex> lck(_mtx);
|
|
|
|
|
_map.erase(key);
|
2021-10-13 14:15:29 +08:00
|
|
|
|
}
|
2021-10-19 15:23:12 +08:00
|
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
WebRtcPluginManager &WebRtcPluginManager::Instance() {
|
|
|
|
|
static WebRtcPluginManager s_instance;
|
|
|
|
|
return s_instance;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WebRtcPluginManager::registerPlugin(const string &type, Plugin cb) {
|
|
|
|
|
lock_guard<mutex> lck(_mtx_creator);
|
|
|
|
|
_map_creator[type] = std::move(cb);
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-26 10:26:30 +08:00
|
|
|
|
|
2023-12-09 16:23:51 +08:00
|
|
|
|
void WebRtcPluginManager::setListener(Listener cb) {
|
2021-10-19 15:23:12 +08:00
|
|
|
|
lock_guard<mutex> lck(_mtx_creator);
|
2023-12-09 16:23:51 +08:00
|
|
|
|
_listener = std::move(cb);
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-23 22:46:30 +08:00
|
|
|
|
void WebRtcPluginManager::negotiateSdp(Session &sender, const string &type, const WebRtcArgs &args, const onCreateWebRtc &cb_in) {
|
|
|
|
|
onCreateWebRtc cb;
|
2023-12-09 16:23:51 +08:00
|
|
|
|
lock_guard<mutex> lck(_mtx_creator);
|
|
|
|
|
if (_listener) {
|
|
|
|
|
auto listener = _listener;
|
|
|
|
|
auto args_ptr = args.shared_from_this();
|
|
|
|
|
auto sender_ptr = static_pointer_cast<Session>(sender.shared_from_this());
|
|
|
|
|
cb = [listener, sender_ptr, type, args_ptr, cb_in](const WebRtcInterface &rtc) {
|
|
|
|
|
listener(*sender_ptr, type, *args_ptr, rtc);
|
|
|
|
|
cb_in(rtc);
|
|
|
|
|
};
|
|
|
|
|
} else {
|
|
|
|
|
cb = cb_in;
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-19 15:23:12 +08:00
|
|
|
|
auto it = _map_creator.find(type);
|
|
|
|
|
if (it == _map_creator.end()) {
|
2024-03-23 22:46:30 +08:00
|
|
|
|
cb_in(WebRtcException(SockException(Err_other, "the type can not supported")));
|
2021-10-19 15:23:12 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
2022-10-06 12:33:48 +08:00
|
|
|
|
it->second(sender, args, cb);
|
2021-10-19 15:23:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-03-23 22:46:30 +08:00
|
|
|
|
void echo_plugin(Session &sender, const WebRtcArgs &args, const onCreateWebRtc &cb) {
|
2021-10-19 15:23:12 +08:00
|
|
|
|
cb(*WebRtcEchoTest::create(EventPollerPool::Instance().getPoller()));
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-23 22:46:30 +08:00
|
|
|
|
void push_plugin(Session &sender, const WebRtcArgs &args, const onCreateWebRtc &cb) {
|
2021-10-19 15:23:12 +08:00
|
|
|
|
MediaInfo info(args["url"]);
|
2024-03-23 22:46:30 +08:00
|
|
|
|
Broadcast::PublishAuthInvoker invoker = [cb, info](const string &err, const ProtocolOption &option) mutable {
|
2021-10-19 15:23:12 +08:00
|
|
|
|
if (!err.empty()) {
|
|
|
|
|
cb(WebRtcException(SockException(Err_other, err)));
|
|
|
|
|
return;
|
|
|
|
|
}
|
2022-01-10 17:43:28 +08:00
|
|
|
|
|
|
|
|
|
RtspMediaSourceImp::Ptr push_src;
|
|
|
|
|
std::shared_ptr<void> push_src_ownership;
|
2023-05-25 16:23:24 +08:00
|
|
|
|
auto src = MediaSource::find(RTSP_SCHEMA, info.vhost, info.app, info.stream);
|
2022-01-10 17:43:28 +08:00
|
|
|
|
auto push_failed = (bool)src;
|
|
|
|
|
|
|
|
|
|
while (src) {
|
2022-07-14 22:49:31 +08:00
|
|
|
|
// 尝试断连后继续推流
|
2022-01-10 17:43:28 +08:00
|
|
|
|
auto rtsp_src = dynamic_pointer_cast<RtspMediaSourceImp>(src);
|
|
|
|
|
if (!rtsp_src) {
|
2022-07-14 22:49:31 +08:00
|
|
|
|
// 源不是rtsp推流产生的
|
2022-01-10 17:43:28 +08:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
auto ownership = rtsp_src->getOwnership();
|
|
|
|
|
if (!ownership) {
|
2022-07-14 22:49:31 +08:00
|
|
|
|
// 获取推流源所有权失败
|
2022-01-10 17:43:28 +08:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
push_src = std::move(rtsp_src);
|
|
|
|
|
push_src_ownership = std::move(ownership);
|
|
|
|
|
push_failed = false;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (push_failed) {
|
2021-10-19 15:23:12 +08:00
|
|
|
|
cb(WebRtcException(SockException(Err_other, "already publishing")));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-10 17:43:28 +08:00
|
|
|
|
if (!push_src) {
|
2023-05-25 16:23:24 +08:00
|
|
|
|
push_src = std::make_shared<RtspMediaSourceImp>(info);
|
2022-01-10 17:43:28 +08:00
|
|
|
|
push_src_ownership = push_src->getOwnership();
|
2022-03-12 13:19:21 +08:00
|
|
|
|
push_src->setProtocolOption(option);
|
2022-01-10 17:43:28 +08:00
|
|
|
|
}
|
2024-03-23 22:46:30 +08:00
|
|
|
|
auto rtc = WebRtcPusher::create(EventPollerPool::Instance().getPoller(), push_src, push_src_ownership, info, option);
|
2021-10-19 15:23:12 +08:00
|
|
|
|
push_src->setListener(rtc);
|
|
|
|
|
cb(*rtc);
|
|
|
|
|
};
|
|
|
|
|
|
2022-07-14 22:49:31 +08:00
|
|
|
|
// rtsp推流需要鉴权
|
2023-09-02 10:52:07 +08:00
|
|
|
|
auto flag = NOTICE_EMIT(BroadcastMediaPublishArgs, Broadcast::kBroadcastMediaPublish, MediaOriginType::rtc_push, info, invoker, sender);
|
2021-10-19 15:23:12 +08:00
|
|
|
|
if (!flag) {
|
2022-07-14 22:49:31 +08:00
|
|
|
|
// 该事件无人监听,默认不鉴权
|
2022-03-12 13:19:21 +08:00
|
|
|
|
invoker("", ProtocolOption());
|
2021-10-19 15:23:12 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-23 22:46:30 +08:00
|
|
|
|
void play_plugin(Session &sender, const WebRtcArgs &args, const onCreateWebRtc &cb) {
|
2021-10-19 15:23:12 +08:00
|
|
|
|
MediaInfo info(args["url"]);
|
2023-04-28 22:03:16 +08:00
|
|
|
|
auto session_ptr = static_pointer_cast<Session>(sender.shared_from_this());
|
2024-03-23 22:46:30 +08:00
|
|
|
|
Broadcast::AuthInvoker invoker = [cb, info, session_ptr](const string &err) mutable {
|
2021-10-19 15:23:12 +08:00
|
|
|
|
if (!err.empty()) {
|
|
|
|
|
cb(WebRtcException(SockException(Err_other, err)));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-14 22:49:31 +08:00
|
|
|
|
// webrtc播放的是rtsp的源
|
2023-05-25 16:23:24 +08:00
|
|
|
|
info.schema = RTSP_SCHEMA;
|
2021-10-19 15:23:12 +08:00
|
|
|
|
MediaSource::findAsync(info, session_ptr, [=](const MediaSource::Ptr &src_in) mutable {
|
|
|
|
|
auto src = dynamic_pointer_cast<RtspMediaSource>(src_in);
|
|
|
|
|
if (!src) {
|
|
|
|
|
cb(WebRtcException(SockException(Err_other, "stream not found")));
|
|
|
|
|
return;
|
|
|
|
|
}
|
2022-07-14 22:49:31 +08:00
|
|
|
|
// 还原成rtc,目的是为了hook时识别哪种播放协议
|
2023-12-02 16:31:40 +08:00
|
|
|
|
info.schema = "rtc";
|
2024-03-23 22:46:30 +08:00
|
|
|
|
auto rtc = WebRtcPlayer::create(EventPollerPool::Instance().getPoller(), src, info);
|
2021-10-19 15:23:12 +08:00
|
|
|
|
cb(*rtc);
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2022-07-14 22:49:31 +08:00
|
|
|
|
// 广播通用播放url鉴权事件
|
2023-09-02 10:52:07 +08:00
|
|
|
|
auto flag = NOTICE_EMIT(BroadcastMediaPlayedArgs, Broadcast::kBroadcastMediaPlayed, info, invoker, sender);
|
2021-10-19 15:23:12 +08:00
|
|
|
|
if (!flag) {
|
2022-07-14 22:49:31 +08:00
|
|
|
|
// 该事件无人监听,默认不鉴权
|
2021-10-19 15:23:12 +08:00
|
|
|
|
invoker("");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-23 22:46:30 +08:00
|
|
|
|
static void setWebRtcArgs(const WebRtcArgs &args, WebRtcInterface &rtc) {
|
2023-12-09 16:23:51 +08:00
|
|
|
|
{
|
2024-03-23 22:46:30 +08:00
|
|
|
|
static auto is_vaild_ip = [](const std::string &ip) -> bool {
|
|
|
|
|
int a, b, c, d;
|
|
|
|
|
return sscanf(ip.c_str(), "%d.%d.%d.%d", &a, &b, &c, &d) == 4;
|
|
|
|
|
};
|
|
|
|
|
std::string host = args["Host"];
|
|
|
|
|
if (!host.empty()) {
|
|
|
|
|
auto local_ip = host.substr(0, host.find(':'));
|
|
|
|
|
if (!is_vaild_ip(local_ip) || local_ip == "127.0.0.1") {
|
|
|
|
|
local_ip = "";
|
|
|
|
|
}
|
|
|
|
|
rtc.setLocalIp(std::move(local_ip));
|
2023-12-09 16:23:51 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2024-03-23 22:46:30 +08:00
|
|
|
|
|
|
|
|
|
bool preferred_tcp = args["preferred_tcp"];
|
2023-12-09 16:23:51 +08:00
|
|
|
|
{
|
2024-03-23 22:46:30 +08:00
|
|
|
|
rtc.setPreferredTcp(preferred_tcp);
|
2023-12-09 16:23:51 +08:00
|
|
|
|
}
|
2024-03-23 22:46:30 +08:00
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
vector<SdpAttrCandidate> cands;
|
|
|
|
|
{
|
|
|
|
|
auto cand_str = trim(args["cand_udp"]);
|
|
|
|
|
auto ip_port = toolkit::split(cand_str, ":");
|
|
|
|
|
if (ip_port.size() == 2) {
|
|
|
|
|
// udp优先
|
|
|
|
|
auto ice_cand = makeIceCandidate(ip_port[0], atoi(ip_port[1].data()), preferred_tcp ? 100 : 120, "udp");
|
|
|
|
|
cands.emplace_back(std::move(*ice_cand));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
{
|
|
|
|
|
auto cand_str = trim(args["cand_tcp"]);
|
|
|
|
|
auto ip_port = toolkit::split(cand_str, ":");
|
|
|
|
|
if (ip_port.size() == 2) {
|
|
|
|
|
// tcp模式
|
|
|
|
|
auto ice_cand = makeIceCandidate(ip_port[0], atoi(ip_port[1].data()), preferred_tcp ? 120 : 100, "tcp");
|
|
|
|
|
cands.emplace_back(std::move(*ice_cand));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!cands.empty()) {
|
|
|
|
|
// udp优先
|
|
|
|
|
rtc.setIceCandidate(std::move(cands));
|
|
|
|
|
}
|
2023-12-09 16:23:51 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-14 22:49:31 +08:00
|
|
|
|
static onceToken s_rtc_auto_register([]() {
|
2024-03-23 22:46:30 +08:00
|
|
|
|
#if !defined (NDEBUG)
|
|
|
|
|
// debug模式才开启echo插件
|
2021-10-19 15:23:12 +08:00
|
|
|
|
WebRtcPluginManager::Instance().registerPlugin("echo", echo_plugin);
|
2024-03-23 22:46:30 +08:00
|
|
|
|
#endif
|
2021-10-19 15:23:12 +08:00
|
|
|
|
WebRtcPluginManager::Instance().registerPlugin("push", push_plugin);
|
|
|
|
|
WebRtcPluginManager::Instance().registerPlugin("play", play_plugin);
|
2023-12-09 16:23:51 +08:00
|
|
|
|
WebRtcPluginManager::Instance().setListener([](Session &sender, const std::string &type, const WebRtcArgs &args, const WebRtcInterface &rtc) {
|
2024-03-23 22:46:30 +08:00
|
|
|
|
setWebRtcArgs(args, const_cast<WebRtcInterface&>(rtc));
|
2023-12-09 16:23:51 +08:00
|
|
|
|
});
|
2021-10-19 15:23:12 +08:00
|
|
|
|
});
|
2022-09-18 21:03:05 +08:00
|
|
|
|
|
2023-12-29 10:53:47 +08:00
|
|
|
|
}// namespace mediakit
|