提交 3ba11af5 authored 作者: Iñaki Baz Castillo's avatar Iñaki Baz Castillo

Merge branch 'master' of github.com:versatica/mediasoup

...@@ -4,6 +4,7 @@ const utils = require('./utils'); ...@@ -4,6 +4,7 @@ const utils = require('./utils');
const errors = require('./errors'); const errors = require('./errors');
const ortc = require('./ortc'); const ortc = require('./ortc');
const WebRtcTransport = require('./WebRtcTransport'); const WebRtcTransport = require('./WebRtcTransport');
const PlainRtpTransport = require('./PlainRtpTransport');
const Producer = require('./Producer'); const Producer = require('./Producer');
const Consumer = require('./Consumer'); const Consumer = require('./Consumer');
...@@ -348,9 +349,35 @@ class Peer extends EnhancedEventEmitter ...@@ -348,9 +349,35 @@ class Peer extends EnhancedEventEmitter
direction, direction,
options, options,
dtlsParameters, dtlsParameters,
plainRtpParameters,
appData appData
} = request; } = request;
// Plain RTP transport.
if (plainRtpParameters)
{
plainRtpParameters.preferIPv4 = Boolean(options.preferIPv4);
plainRtpParameters.preferIPv6 = Boolean(options.preferIPv6);
return this._createPlainRtpTransport(
id, direction, plainRtpParameters, appData)
.then((transport) =>
{
const response =
{
plainRtpParameters :
{
ip : transport.localIP,
port : transport.localPort
}
};
return response;
});
}
// WebRTC transport.
else
{
return this._createWebRtcTransport(id, direction, options, appData) return this._createWebRtcTransport(id, direction, options, appData)
.then((transport) => .then((transport) =>
{ {
...@@ -379,6 +406,7 @@ class Peer extends EnhancedEventEmitter ...@@ -379,6 +406,7 @@ class Peer extends EnhancedEventEmitter
return response; return response;
}); });
} }
}
case 'restartTransport': case 'restartTransport':
{ {
...@@ -483,12 +511,14 @@ class Peer extends EnhancedEventEmitter ...@@ -483,12 +511,14 @@ class Peer extends EnhancedEventEmitter
case 'updateTransport': case 'updateTransport':
{ {
const { id, dtlsParameters } = notification; const { id, dtlsParameters, plainRtpParameters } = notification;
const transport = this._transports.get(id); const transport = this._transports.get(id);
if (!transport) if (!transport)
throw new Error('Transport not found'); throw new Error('Transport not found');
if (dtlsParameters)
{
return transport.setRemoteDtlsParameters(dtlsParameters) return transport.setRemoteDtlsParameters(dtlsParameters)
.catch((error) => .catch((error) =>
{ {
...@@ -497,6 +527,19 @@ class Peer extends EnhancedEventEmitter ...@@ -497,6 +527,19 @@ class Peer extends EnhancedEventEmitter
throw error; throw error;
}); });
} }
else if (plainRtpParameters)
{
return transport.setRemoteParameters(plainRtpParameters)
.catch((error) =>
{
transport.close();
throw error;
});
}
break;
}
case 'closeTransport': case 'closeTransport':
{ {
...@@ -829,6 +872,56 @@ class Peer extends EnhancedEventEmitter ...@@ -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) _createProducer(id, kind, transportId, rtpParameters, remotelyPaused, appData)
{ {
logger.debug('_createProducer() [id:%s, kind:%s]', id, kind); logger.debug('_createProducer() [id:%s, kind:%s]', id, kind);
......
...@@ -53,6 +53,16 @@ class PlainRtpTransport extends EnhancedEventEmitter ...@@ -53,6 +53,16 @@ class PlainRtpTransport extends EnhancedEventEmitter
return this._data.tuple; return this._data.tuple;
} }
get localIP()
{
return this._data.localIP;
}
get localPort()
{
return this._data.localPort;
}
/** /**
* Close the PlainRtpTransport. * Close the PlainRtpTransport.
* *
...@@ -115,6 +125,39 @@ class PlainRtpTransport extends EnhancedEventEmitter ...@@ -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. * Enables periodic stats retrieval.
* *
......
...@@ -31,6 +31,7 @@ namespace Channel ...@@ -31,6 +31,7 @@ namespace Channel
TRANSPORT_DUMP, TRANSPORT_DUMP,
TRANSPORT_GET_STATS, TRANSPORT_GET_STATS,
TRANSPORT_SET_REMOTE_DTLS_PARAMETERS, TRANSPORT_SET_REMOTE_DTLS_PARAMETERS,
TRANSPORT_SET_REMOTE_PARAMETERS,
TRANSPORT_SET_MAX_BITRATE, TRANSPORT_SET_MAX_BITRATE,
TRANSPORT_CHANGE_UFRAG_PWD, TRANSPORT_CHANGE_UFRAG_PWD,
TRANSPORT_START_MIRRORING, TRANSPORT_START_MIRRORING,
......
#ifndef MS_RTC_PLAIN_RTPWEBRTC_TRANSPORT_HPP #ifndef MS_RTC_PLAIN_RTP_TRANSPORT_HPP
#define MS_RTC_PLAIN_RTPWEBRTC_TRANSPORT_HPP #define MS_RTC_PLAIN_RTP_TRANSPORT_HPP
#include "common.hpp" #include "common.hpp"
#include "Channel/Notifier.hpp" #include "Channel/Notifier.hpp"
...@@ -17,6 +17,8 @@ namespace RTC ...@@ -17,6 +17,8 @@ namespace RTC
std::string remoteIP; std::string remoteIP;
uint16_t remotePort; uint16_t remotePort;
std::string localIP; std::string localIP;
bool preferIPv4;
bool preferIPv6;
}; };
public: public:
...@@ -32,10 +34,12 @@ namespace RTC ...@@ -32,10 +34,12 @@ namespace RTC
public: public:
Json::Value ToJson() const override; Json::Value ToJson() const override;
Json::Value GetStats() const override; Json::Value GetStats() const override;
void SetRemoteParameters(const std::string& ip, uint16_t port);
void SendRtpPacket(RTC::RtpPacket* packet) override; void SendRtpPacket(RTC::RtpPacket* packet) override;
void SendRtcpPacket(RTC::RTCP::Packet* packet) override; void SendRtcpPacket(RTC::RTCP::Packet* packet) override;
private: private:
void CreateSocket(int addressFamily, const std::string& localIP);
bool IsConnected() const override; bool IsConnected() const override;
void SendRtcpCompoundPacket(RTC::RTCP::CompoundPacket* packet) override; void SendRtcpCompoundPacket(RTC::RTCP::CompoundPacket* packet) override;
......
...@@ -26,6 +26,7 @@ namespace Channel ...@@ -26,6 +26,7 @@ namespace Channel
{ "transport.dump", Request::MethodId::TRANSPORT_DUMP }, { "transport.dump", Request::MethodId::TRANSPORT_DUMP },
{ "transport.getStats", Request::MethodId::TRANSPORT_GET_STATS }, { "transport.getStats", Request::MethodId::TRANSPORT_GET_STATS },
{ "transport.setRemoteDtlsParameters", Request::MethodId::TRANSPORT_SET_REMOTE_DTLS_PARAMETERS }, { "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.setMaxBitrate", Request::MethodId::TRANSPORT_SET_MAX_BITRATE },
{ "transport.changeUfragPwd", Request::MethodId::TRANSPORT_CHANGE_UFRAG_PWD }, { "transport.changeUfragPwd", Request::MethodId::TRANSPORT_CHANGE_UFRAG_PWD },
{ "transport.startMirroring", Request::MethodId::TRANSPORT_START_MIRRORING }, { "transport.startMirroring", Request::MethodId::TRANSPORT_START_MIRRORING },
......
...@@ -23,64 +23,50 @@ namespace RTC ...@@ -23,64 +23,50 @@ namespace RTC
{ {
MS_TRACE(); MS_TRACE();
int err; // Remote IP address is provided.
if (!options.remoteIP.empty())
switch (Utils::IP::GetFamily(options.remoteIP))
{
case AF_INET:
{ {
if (!Settings::configuration.hasIPv4 && options.localIP.empty()) int addressFamily = Utils::IP::GetFamily(options.remoteIP);
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()) if (addressFamily == AF_INET)
this->udpSocket = new RTC::UdpSocket(this, AF_INET); this->CreateSocket(AF_INET, options.localIP);
else if (addressFamily == AF_INET6)
this->CreateSocket(AF_INET6, options.localIP);
else else
this->udpSocket = new RTC::UdpSocket(this, options.localIP); MS_THROW_ERROR("invalid destination IP '%s'", options.remoteIP.c_str());
break; // This may throw.
SetRemoteParameters(options.remoteIP.c_str(), options.remotePort);
} }
// No remote IP address is provided.
else
{
// IPv4 is preferred.
if (options.preferIPv4)
{
if (!Settings::configuration.hasIPv4)
MS_THROW_ERROR("IPv4 disabled");
case AF_INET6: this->CreateSocket(AF_INET, options.localIP);
}
// IPv6 is preferred.
else if (options.preferIPv6)
{ {
if (!Settings::configuration.hasIPv6 && options.localIP.empty()) if (!Settings::configuration.hasIPv6)
MS_THROW_ERROR("IPv6 disabled"); MS_THROW_ERROR("IPv6 disabled");
err = uv_ip6_addr( this->CreateSocket(AF_INET6, options.localIP);
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;
} }
// No IP family preference, try with IPv4 and then IPv6.
default: else if (Settings::configuration.hasIPv4)
{ {
MS_THROW_ERROR("invalid destination IP '%s'", options.remoteIP.c_str()); this->CreateSocket(AF_INET, options.localIP);
}
break; 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() PlainRtpTransport::~PlainRtpTransport()
...@@ -100,6 +86,8 @@ namespace RTC ...@@ -100,6 +86,8 @@ namespace RTC
static const Json::StaticString JsonStringTransportId{ "transportId" }; static const Json::StaticString JsonStringTransportId{ "transportId" };
static const Json::StaticString JsonStringTuple{ "tuple" }; static const Json::StaticString JsonStringTuple{ "tuple" };
static const Json::StaticString JsonStringRtpListener{ "rtpListener" }; static const Json::StaticString JsonStringRtpListener{ "rtpListener" };
static const Json::StaticString JsonStringLocalIP{ "localIP" };
static const Json::StaticString JsonStringLocalPort{ "localPort" };
Json::Value json(Json::objectValue); Json::Value json(Json::objectValue);
...@@ -110,6 +98,10 @@ namespace RTC ...@@ -110,6 +98,10 @@ namespace RTC
if (this->tuple != nullptr) if (this->tuple != nullptr)
json[JsonStringTuple] = this->tuple->ToJson(); json[JsonStringTuple] = this->tuple->ToJson();
// Local IP address and port.
json[JsonStringLocalIP] = this->udpSocket->GetLocalIP();
json[JsonStringLocalPort] = Json::UInt{ this->udpSocket->GetLocalPort() };
// Add rtpListener. // Add rtpListener.
json[JsonStringRtpListener] = this->rtpListener.ToJson(); json[JsonStringRtpListener] = this->rtpListener.ToJson();
...@@ -125,6 +117,58 @@ namespace RTC ...@@ -125,6 +117,58 @@ namespace RTC
return json; 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) void PlainRtpTransport::SendRtpPacket(RTC::RtpPacket* packet)
{ {
MS_TRACE(); MS_TRACE();
...@@ -159,9 +203,17 @@ namespace RTC ...@@ -159,9 +203,17 @@ namespace RTC
this->tuple->Send(data, len); 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 bool PlainRtpTransport::IsConnected() const
{ {
return true; return this->tuple ? true : false;
} }
void PlainRtpTransport::SendRtcpCompoundPacket(RTC::RTCP::CompoundPacket* packet) void PlainRtpTransport::SendRtcpCompoundPacket(RTC::RTCP::CompoundPacket* packet)
......
...@@ -241,6 +241,8 @@ namespace RTC ...@@ -241,6 +241,8 @@ namespace RTC
static const Json::StaticString JsonStringRemoteIP{ "remoteIP" }; static const Json::StaticString JsonStringRemoteIP{ "remoteIP" };
static const Json::StaticString JsonStringRemotePort{ "remotePort" }; static const Json::StaticString JsonStringRemotePort{ "remotePort" };
static const Json::StaticString JsonStringLocalIP{ "localIP" }; static const Json::StaticString JsonStringLocalIP{ "localIP" };
static const Json::StaticString JsonStringPreferIPv4{ "preferIPv4" };
static const Json::StaticString JsonStringPreferIPv6{ "preferIPv6" };
uint32_t transportId; uint32_t transportId;
...@@ -257,27 +259,21 @@ namespace RTC ...@@ -257,27 +259,21 @@ namespace RTC
RTC::PlainRtpTransport::Options options; RTC::PlainRtpTransport::Options options;
if (!request->data[JsonStringRemoteIP].isString()) if (request->data[JsonStringRemoteIP].isString())
{
request->Reject("missing remoteIP");
return;
}
options.remoteIP = request->data[JsonStringRemoteIP].asString(); options.remoteIP = request->data[JsonStringRemoteIP].asString();
if (!request->data[JsonStringRemotePort].isUInt()) if (request->data[JsonStringRemotePort].isUInt())
{
request->Reject("missing remotePort");
return;
}
options.remotePort = request->data[JsonStringRemotePort].asUInt(); options.remotePort = request->data[JsonStringRemotePort].asUInt();
if (request->data[JsonStringLocalIP].isString()) if (request->data[JsonStringLocalIP].isString())
options.localIP = request->data[JsonStringLocalIP].asString(); 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; RTC::PlainRtpTransport* plainRtpTransport;
try try
...@@ -767,6 +763,60 @@ namespace RTC ...@@ -767,6 +763,60 @@ namespace RTC
break; 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: case Channel::Request::MethodId::TRANSPORT_SET_MAX_BITRATE:
{ {
static const Json::StaticString JsonStringBitrate{ "bitrate" }; static const Json::StaticString JsonStringBitrate{ "bitrate" };
......
...@@ -247,6 +247,7 @@ void Worker::OnChannelRequest(Channel::UnixStreamSocket* /*channel*/, Channel::R ...@@ -247,6 +247,7 @@ void Worker::OnChannelRequest(Channel::UnixStreamSocket* /*channel*/, Channel::R
case Channel::Request::MethodId::TRANSPORT_DUMP: case Channel::Request::MethodId::TRANSPORT_DUMP:
case Channel::Request::MethodId::TRANSPORT_GET_STATS: case Channel::Request::MethodId::TRANSPORT_GET_STATS:
case Channel::Request::MethodId::TRANSPORT_SET_REMOTE_DTLS_PARAMETERS: 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_SET_MAX_BITRATE:
case Channel::Request::MethodId::TRANSPORT_CHANGE_UFRAG_PWD: case Channel::Request::MethodId::TRANSPORT_CHANGE_UFRAG_PWD:
case Channel::Request::MethodId::TRANSPORT_START_MIRRORING: case Channel::Request::MethodId::TRANSPORT_START_MIRRORING:
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论