提交 6cff9368 authored 作者: José Luis Millán's avatar José Luis Millán 提交者: Iñaki Baz Castillo

Plain RTP transport (#223)

上级 64248387
......@@ -4,6 +4,7 @@ const utils = require('./utils');
const errors = require('./errors');
const ortc = require('./ortc');
const WebRtcTransport = require('./WebRtcTransport');
const PlainRtpTransport = require('./PlainRtpTransport');
const Producer = require('./Producer');
const Consumer = require('./Consumer');
......@@ -348,36 +349,63 @@ class Peer extends EnhancedEventEmitter
direction,
options,
dtlsParameters,
plainRtpParameters,
appData
} = request;
return this._createWebRtcTransport(id, direction, options, appData)
.then((transport) =>
{
// If no client's DTLS parameters are given, we are done.
if (!dtlsParameters)
return transport;
// Plain RTP transport.
if (plainRtpParameters)
{
plainRtpParameters.preferIPv4 = Boolean(options.preferIPv4);
plainRtpParameters.preferIPv6 = Boolean(options.preferIPv6);
return transport.setRemoteDtlsParameters(dtlsParameters)
.then(() => transport)
.catch((error) =>
return this._createPlainRtpTransport(
id, direction, plainRtpParameters, appData)
.then((transport) =>
{
const response =
{
transport.remoteClose();
throw error;
});
})
.then((transport) =>
{
const response =
plainRtpParameters :
{
ip : transport.localIP,
port : transport.localPort
}
};
return response;
});
}
// WebRTC transport.
else
{
return this._createWebRtcTransport(id, direction, options, appData)
.then((transport) =>
{
iceParameters : transport.iceLocalParameters,
iceCandidates : transport.iceLocalCandidates,
dtlsParameters : transport.dtlsLocalParameters
};
return response;
});
// If no client's DTLS parameters are given, we are done.
if (!dtlsParameters)
return transport;
return transport.setRemoteDtlsParameters(dtlsParameters)
.then(() => transport)
.catch((error) =>
{
transport.remoteClose();
throw error;
});
})
.then((transport) =>
{
const response =
{
iceParameters : transport.iceLocalParameters,
iceCandidates : transport.iceLocalCandidates,
dtlsParameters : transport.dtlsLocalParameters
};
return response;
});
}
}
case 'restartTransport':
......@@ -483,19 +511,34 @@ class Peer extends EnhancedEventEmitter
case 'updateTransport':
{
const { id, dtlsParameters } = notification;
const { id, dtlsParameters, plainRtpParameters } = notification;
const transport = this._transports.get(id);
if (!transport)
throw new Error('Transport not found');
return transport.setRemoteDtlsParameters(dtlsParameters)
.catch((error) =>
{
transport.close();
if (dtlsParameters)
{
return transport.setRemoteDtlsParameters(dtlsParameters)
.catch((error) =>
{
transport.close();
throw error;
});
}
else if (plainRtpParameters)
{
return transport.setRemoteParameters(plainRtpParameters)
.catch((error) =>
{
transport.close();
throw error;
});
throw error;
});
}
break;
}
case 'closeTransport':
......@@ -829,6 +872,56 @@ class Peer extends EnhancedEventEmitter
});
}
_createPlainRtpTransport(id, direction, options, appData)
{
logger.debug('_createPlainRtpTransport() [id:%s, direction:%s]', id, direction);
const internal =
{
routerId : this._internal.routerId,
transportId : id || utils.randomNumber()
};
return this._channel.request('router.createPlainRtpTransport', internal,
{
remoteIP : options.ip,
remotePort : options.port,
preferIPv4 : options.preferIPv4,
preferIPv6 : options.preferIPv6
})
.then((data) =>
{
logger.debug('"router.createPlainRtpTransport" request succeeded');
data = Object.assign(data, { direction });
// Create a PlainRtpTransport.
const transport = new PlainRtpTransport(internal, data, this._channel);
transport.appData = appData;
// Store the PlainRtpTransport and remove it when closed.
this._transports.set(transport.id, transport);
transport.on('@close', () => this._transports.delete(transport.id));
transport.on('@notify', (method, data2) =>
{
this._sendNotification(method, data2);
});
this.safeEmit('newtransport', transport);
return transport;
})
.catch((error) =>
{
logger.error(
'"router.createPlainRtpTransport" request failed: %s', String(error));
throw error;
});
}
_createProducer(id, kind, transportId, rtpParameters, remotelyPaused, appData)
{
logger.debug('_createProducer() [id:%s, kind:%s]', id, kind);
......
......@@ -53,6 +53,16 @@ class PlainRtpTransport extends EnhancedEventEmitter
return this._data.tuple;
}
get localIP()
{
return this._data.localIP;
}
get localPort()
{
return this._data.localPort;
}
/**
* Close the PlainRtpTransport.
*
......@@ -115,6 +125,39 @@ class PlainRtpTransport extends EnhancedEventEmitter
});
}
/**
* Provide the remote parameters.
*
* @private
*
* @param {Object} parameters - Remote parameters.
*
* @return {Promise} Resolves to this.
*/
setRemoteParameters(parameters)
{
logger.debug('setRemoteParameters()');
if (this._closed)
return Promise.reject(new errors.InvalidStateError('PlainRtpTransport closed'));
return this._channel.request(
'transport.setRemoteParameters', this._internal, parameters)
.then(() =>
{
logger.debug('"transport.setRemoteParameters" request succeeded');
return this;
})
.catch((error) =>
{
logger.error(
'"transport.setRemoteParameters" request failed: %s', String(error));
throw error;
});
}
/**
* Enables periodic stats retrieval.
*
......
......@@ -31,6 +31,7 @@ namespace Channel
TRANSPORT_DUMP,
TRANSPORT_GET_STATS,
TRANSPORT_SET_REMOTE_DTLS_PARAMETERS,
TRANSPORT_SET_REMOTE_PARAMETERS,
TRANSPORT_SET_MAX_BITRATE,
TRANSPORT_CHANGE_UFRAG_PWD,
TRANSPORT_START_MIRRORING,
......
#ifndef MS_RTC_PLAIN_RTPWEBRTC_TRANSPORT_HPP
#define MS_RTC_PLAIN_RTPWEBRTC_TRANSPORT_HPP
#ifndef MS_RTC_PLAIN_RTP_TRANSPORT_HPP
#define MS_RTC_PLAIN_RTP_TRANSPORT_HPP
#include "common.hpp"
#include "Channel/Notifier.hpp"
......@@ -17,6 +17,8 @@ namespace RTC
std::string remoteIP;
uint16_t remotePort;
std::string localIP;
bool preferIPv4;
bool preferIPv6;
};
public:
......@@ -32,10 +34,12 @@ namespace RTC
public:
Json::Value ToJson() const override;
Json::Value GetStats() const override;
void SetRemoteParameters(const std::string& ip, uint16_t port);
void SendRtpPacket(RTC::RtpPacket* packet) override;
void SendRtcpPacket(RTC::RTCP::Packet* packet) override;
private:
void CreateSocket(int addressFamily, const std::string& localIP);
bool IsConnected() const override;
void SendRtcpCompoundPacket(RTC::RTCP::CompoundPacket* packet) override;
......
......@@ -26,6 +26,7 @@ namespace Channel
{ "transport.dump", Request::MethodId::TRANSPORT_DUMP },
{ "transport.getStats", Request::MethodId::TRANSPORT_GET_STATS },
{ "transport.setRemoteDtlsParameters", Request::MethodId::TRANSPORT_SET_REMOTE_DTLS_PARAMETERS },
{ "transport.setRemoteParameters", Request::MethodId::TRANSPORT_SET_REMOTE_PARAMETERS },
{ "transport.setMaxBitrate", Request::MethodId::TRANSPORT_SET_MAX_BITRATE },
{ "transport.changeUfragPwd", Request::MethodId::TRANSPORT_CHANGE_UFRAG_PWD },
{ "transport.startMirroring", Request::MethodId::TRANSPORT_START_MIRRORING },
......
......@@ -23,64 +23,50 @@ namespace RTC
{
MS_TRACE();
int err;
// Remote IP address is provided.
if (!options.remoteIP.empty())
{
int addressFamily = Utils::IP::GetFamily(options.remoteIP);
if (addressFamily == AF_INET)
this->CreateSocket(AF_INET, options.localIP);
else if (addressFamily == AF_INET6)
this->CreateSocket(AF_INET6, options.localIP);
else
MS_THROW_ERROR("invalid destination IP '%s'", options.remoteIP.c_str());
switch (Utils::IP::GetFamily(options.remoteIP))
// This may throw.
SetRemoteParameters(options.remoteIP.c_str(), options.remotePort);
}
// No remote IP address is provided.
else
{
case AF_INET:
// IPv4 is preferred.
if (options.preferIPv4)
{
if (!Settings::configuration.hasIPv4 && options.localIP.empty())
if (!Settings::configuration.hasIPv4)
MS_THROW_ERROR("IPv4 disabled");
err = uv_ip4_addr(
options.remoteIP.c_str(),
static_cast<int>(options.remotePort),
reinterpret_cast<struct sockaddr_in*>(&this->remoteAddrStorage));
if (err != 0)
MS_ABORT("uv_ipv4_addr() failed: %s", uv_strerror(err));
if (options.localIP.empty())
this->udpSocket = new RTC::UdpSocket(this, AF_INET);
else
this->udpSocket = new RTC::UdpSocket(this, options.localIP);
break;
this->CreateSocket(AF_INET, options.localIP);
}
case AF_INET6:
// IPv6 is preferred.
else if (options.preferIPv6)
{
if (!Settings::configuration.hasIPv6 && options.localIP.empty())
if (!Settings::configuration.hasIPv6)
MS_THROW_ERROR("IPv6 disabled");
err = uv_ip6_addr(
options.remoteIP.c_str(),
static_cast<int>(options.remotePort),
reinterpret_cast<struct sockaddr_in6*>(&this->remoteAddrStorage));
if (err != 0)
MS_ABORT("uv_ipv6_addr() failed: %s", uv_strerror(err));
if (options.localIP.empty())
this->udpSocket = new RTC::UdpSocket(this, AF_INET6);
else
this->udpSocket = new RTC::UdpSocket(this, options.localIP);
break;
this->CreateSocket(AF_INET6, options.localIP);
}
default:
// No IP family preference, try with IPv4 and then IPv6.
else if (Settings::configuration.hasIPv4)
{
MS_THROW_ERROR("invalid destination IP '%s'", options.remoteIP.c_str());
break;
this->CreateSocket(AF_INET, options.localIP);
}
else
{
this->CreateSocket(AF_INET6, options.localIP);
}
}
// Create a single tuple.
this->tuple = new RTC::TransportTuple(
this->udpSocket, reinterpret_cast<struct sockaddr*>(&this->remoteAddrStorage));
// Start the RTCP timer.
this->rtcpTimer->Start(static_cast<uint64_t>(RTC::RTCP::MaxVideoIntervalMs / 2));
}
PlainRtpTransport::~PlainRtpTransport()
......@@ -100,6 +86,8 @@ namespace RTC
static const Json::StaticString JsonStringTransportId{ "transportId" };
static const Json::StaticString JsonStringTuple{ "tuple" };
static const Json::StaticString JsonStringRtpListener{ "rtpListener" };
static const Json::StaticString JsonStringLocalIP{ "localIP" };
static const Json::StaticString JsonStringLocalPort{ "localPort" };
Json::Value json(Json::objectValue);
......@@ -110,6 +98,10 @@ namespace RTC
if (this->tuple != nullptr)
json[JsonStringTuple] = this->tuple->ToJson();
// Local IP address and port.
json[JsonStringLocalIP] = this->udpSocket->GetLocalIP();
json[JsonStringLocalPort] = Json::UInt{ this->udpSocket->GetLocalPort() };
// Add rtpListener.
json[JsonStringRtpListener] = this->rtpListener.ToJson();
......@@ -125,6 +117,58 @@ namespace RTC
return json;
}
void PlainRtpTransport::SetRemoteParameters(const std::string& ip, const uint16_t port)
{
int err;
switch (Utils::IP::GetFamily(ip))
{
case AF_INET:
{
if (!Settings::configuration.hasIPv4)
MS_THROW_ERROR("IPv4 disabled");
err = uv_ip4_addr(
ip.c_str(),
static_cast<int>(port),
reinterpret_cast<struct sockaddr_in*>(&this->remoteAddrStorage));
if (err != 0)
MS_ABORT("uv_ipv4_addr() failed: %s", uv_strerror(err));
break;
}
case AF_INET6:
{
if (!Settings::configuration.hasIPv6)
MS_THROW_ERROR("IPv6 disabled");
err = uv_ip6_addr(
ip.c_str(),
static_cast<int>(port),
reinterpret_cast<struct sockaddr_in6*>(&this->remoteAddrStorage));
if (err != 0)
MS_ABORT("uv_ipv6_addr() failed: %s", uv_strerror(err));
break;
}
default:
{
MS_THROW_ERROR("invalid destination IP '%s'", ip.c_str());
break;
}
}
// Create a single tuple.
this->tuple = new RTC::TransportTuple(
this->udpSocket, reinterpret_cast<struct sockaddr*>(&this->remoteAddrStorage));
// Start the RTCP timer.
this->rtcpTimer->Start(static_cast<uint64_t>(RTC::RTCP::MaxVideoIntervalMs / 2));
}
void PlainRtpTransport::SendRtpPacket(RTC::RtpPacket* packet)
{
MS_TRACE();
......@@ -159,9 +203,17 @@ namespace RTC
this->tuple->Send(data, len);
}
void PlainRtpTransport::CreateSocket(int addressFamily, const std::string& localIP)
{
if (localIP.empty())
this->udpSocket = new RTC::UdpSocket(this, addressFamily);
else
this->udpSocket = new RTC::UdpSocket(this, localIP);
}
bool PlainRtpTransport::IsConnected() const
{
return true;
return this->tuple ? true : false;
}
void PlainRtpTransport::SendRtcpCompoundPacket(RTC::RTCP::CompoundPacket* packet)
......
......@@ -241,6 +241,8 @@ namespace RTC
static const Json::StaticString JsonStringRemoteIP{ "remoteIP" };
static const Json::StaticString JsonStringRemotePort{ "remotePort" };
static const Json::StaticString JsonStringLocalIP{ "localIP" };
static const Json::StaticString JsonStringPreferIPv4{ "preferIPv4" };
static const Json::StaticString JsonStringPreferIPv6{ "preferIPv6" };
uint32_t transportId;
......@@ -257,27 +259,21 @@ namespace RTC
RTC::PlainRtpTransport::Options options;
if (!request->data[JsonStringRemoteIP].isString())
{
request->Reject("missing remoteIP");
return;
}
options.remoteIP = request->data[JsonStringRemoteIP].asString();
if (!request->data[JsonStringRemotePort].isUInt())
{
request->Reject("missing remotePort");
return;
}
if (request->data[JsonStringRemoteIP].isString())
options.remoteIP = request->data[JsonStringRemoteIP].asString();
options.remotePort = request->data[JsonStringRemotePort].asUInt();
if (request->data[JsonStringRemotePort].isUInt())
options.remotePort = request->data[JsonStringRemotePort].asUInt();
if (request->data[JsonStringLocalIP].isString())
options.localIP = request->data[JsonStringLocalIP].asString();
if (request->data[JsonStringPreferIPv4].isBool())
options.preferIPv4 = request->data[JsonStringPreferIPv4].asBool();
if (request->data[JsonStringPreferIPv6].isBool())
options.preferIPv6 = request->data[JsonStringPreferIPv6].asBool();
RTC::PlainRtpTransport* plainRtpTransport;
try
......@@ -767,6 +763,60 @@ namespace RTC
break;
}
case Channel::Request::MethodId::TRANSPORT_SET_REMOTE_PARAMETERS:
{
static const Json::StaticString JsonStringIP{ "ip" };
static const Json::StaticString JsonStringPort{ "port" };
RTC::Transport* transport;
try
{
transport = GetTransportFromRequest(request);
}
catch (const MediaSoupError& error)
{
request->Reject(error.what());
return;
}
if (!request->data[JsonStringIP].isString())
{
request->Reject("missing data.ip");
return;
}
if (!request->data[JsonStringPort].isUInt())
{
request->Reject("missing data.port");
return;
}
auto ip = std::string{ request->data[JsonStringIP].asString() };
auto port = uint32_t{ request->data[JsonStringPort].asUInt() };
try
{
auto* plainRtpTransport = dynamic_cast<RTC::PlainRtpTransport*>(transport);
// This may throw.
plainRtpTransport->SetRemoteParameters(ip, port);
}
catch (const MediaSoupError& error)
{
request->Reject(error.what());
return;
}
request->Accept();
break;
}
case Channel::Request::MethodId::TRANSPORT_SET_MAX_BITRATE:
{
static const Json::StaticString JsonStringBitrate{ "bitrate" };
......
......@@ -247,6 +247,7 @@ void Worker::OnChannelRequest(Channel::UnixStreamSocket* /*channel*/, Channel::R
case Channel::Request::MethodId::TRANSPORT_DUMP:
case Channel::Request::MethodId::TRANSPORT_GET_STATS:
case Channel::Request::MethodId::TRANSPORT_SET_REMOTE_DTLS_PARAMETERS:
case Channel::Request::MethodId::TRANSPORT_SET_REMOTE_PARAMETERS:
case Channel::Request::MethodId::TRANSPORT_SET_MAX_BITRATE:
case Channel::Request::MethodId::TRANSPORT_CHANGE_UFRAG_PWD:
case Channel::Request::MethodId::TRANSPORT_START_MIRRORING:
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论