提交 22ba582c authored 作者: Iñaki Baz Castillo's avatar Iñaki Baz Castillo

Transport: add "icestatechange" and "iceselectedtuplechange" events.

上级 c306d7cc
......@@ -34,15 +34,25 @@ class Transport extends EventEmitter
// - .type
// - .protocol
// - .tcpType
// - .dtlsLocalParameters
// - .fingerprints
// - .sha-1
// - .sha-224
// - .sha-256
// - .sha-384
// - .sha-512
// - .role
// - .dtlsState
// - .iceSelectedTuple
// - .local
// - .ip
// - .port
// - .protocol
// - .remote
// - .ip
// - .port
// - .protocol
// - .iceState
// - .dtlsLocalParameters
// - .role
// - .fingerprints
// - .sha-1
// - .sha-224
// - .sha-256
// - .sha-384
// - .sha-512
// - .dtlsState
this._data = data;
// Channel instance
......@@ -56,6 +66,22 @@ class Transport extends EventEmitter
{
switch (event)
{
case 'iceselectedtuplechange':
{
this._data.iceSelectedTuple = data.iceSelectedTuple;
// Emit it to the app
this.emit(event, data);
break;
}
case 'icestatechange':
{
this._data.iceState = data.iceState;
// Emit it to the app
this.emit(event, data);
break;
}
case 'dtlsstatechange':
{
this._data.dtlsState = data.dtlsState;
......@@ -90,6 +116,16 @@ class Transport extends EventEmitter
return this._data.iceLocalCandidates;
}
get iceSelectedTuple()
{
return this._data.iceSelectedTuple;
}
get iceState()
{
return this._data.iceState;
}
get dtlsLocalParameters()
{
return this._data.dtlsLocalParameters;
......@@ -120,6 +156,10 @@ class Transport extends EventEmitter
// We won't receive events anymore, so be nice and manually update
// some fields and emit events before firing 'close'
this._data.iceState = 'closed';
this.emit('icestatechange', { iceState: 'closed' });
if (this._data.dtlsState !== 'closed')
{
this._data.dtlsState = 'closed';
......
......@@ -18,6 +18,8 @@ tap.test('transport.createAssociatedTransport() must succeed', { timeout: 1000 }
{
t.pass('peer.createTransport() succeeded');
t.equal(transport.iceComponent, 'RTP', 'transport must have "RTP" `iceComponent`');
t.notOk(transport.iceSelectedTuple, 'transport must not have `iceSelectedTuple`');
t.equal(transport.iceState, 'new', 'transport must have "new" `iceState`');
t.equal(transport.dtlsState, 'new', 'transport must have "new" `dtlsState`');
transport.iceLocalCandidates.forEach((candidate) =>
......@@ -69,7 +71,7 @@ tap.test('transport.close() must succeed', { timeout: 1000 }, (t) =>
{
let server = mediasoup.Server();
t.plan(7);
t.plan(10);
t.tearDown(() => server.close());
let room = server.Room();
......@@ -80,6 +82,13 @@ tap.test('transport.close() must succeed', { timeout: 1000 }, (t) =>
{
t.pass('peer.createTransport() succeeded');
transport.on('icestatechange', (data) =>
{
t.pass('event "icestatechange" fired');
t.equal(data.iceState, 'closed', 'event `data.iceState` must be "closed"');
t.equal(transport.iceState, 'closed', '`transport.iceState` must be "closed"');
});
transport.on('dtlsstatechange', (data) =>
{
t.pass('event "dtlsstatechange" fired');
......
......@@ -169,7 +169,7 @@ app.put('/test-transport', function(req)
// Create transports promises
for (let i = 0; i < numTransports; i++)
{
let promise = mediaPeer.createTransport({ udp: true, tcp: true });
let promise = mediaPeer.createTransport({ udp: true, tcp: false });
// Set transport event listeners
promise.then((transport) =>
......@@ -179,6 +179,16 @@ app.put('/test-transport', function(req)
debug('transport "close" event');
});
transport.on('iceselectedtuplechange', (data) =>
{
debug('transport "iceselectedtuplechange" event [data.iceSelectedTuple:%o, transport.iceSelectedTuple:%o]', data.iceSelectedTuple, transport.iceSelectedTuple);
});
transport.on('icestatechange', (data) =>
{
debug('transport "icestatechange" event [data.iceState:%s, transport.iceState:%s]', data.iceState, transport.iceState);
});
transport.on('dtlsstatechange', (data) =>
{
debug('transport "dtlsstatechange" event [data.dtlsState:%s, transport.dtlsState:%s]', data.dtlsState, transport.dtlsState);
......
......@@ -5,13 +5,14 @@
#include "RTC/UDPSocket.h"
#include "RTC/TCPConnection.h"
#include "Utils.h"
#include <json/json.h>
namespace RTC
{
class TransportTuple
{
public:
enum class Type
enum class Protocol
{
UDP = 1,
TCP
......@@ -22,12 +23,13 @@ namespace RTC
TransportTuple(RTC::TCPConnection* tcpConnection);
void Close();
Json::Value toJson();
void Dump();
void StoreUdpRemoteAddress();
bool IsUDP();
bool IsTCP();
bool Compare(TransportTuple* tuple);
void Send(const MS_BYTE* data, size_t len);
Protocol GetProtocol();
const struct sockaddr* GetLocalAddress();
const struct sockaddr* GetRemoteAddress();
private:
......@@ -37,7 +39,7 @@ namespace RTC
sockaddr_storage udpRemoteAddrStorage;
RTC::TCPConnection* tcpConnection = nullptr;
// Others.
Type type;
Protocol protocol;
};
/* Inline methods. */
......@@ -45,7 +47,7 @@ namespace RTC
inline
void TransportTuple::Close()
{
if (this->type == Type::TCP)
if (this->protocol == Protocol::TCP)
this->tcpConnection->Close();
}
......@@ -53,13 +55,13 @@ namespace RTC
TransportTuple::TransportTuple(RTC::UDPSocket* udpSocket, const struct sockaddr* udpRemoteAddr) :
udpSocket(udpSocket),
udpRemoteAddr((struct sockaddr*)udpRemoteAddr),
type(Type::UDP)
protocol(Protocol::UDP)
{}
inline
TransportTuple::TransportTuple(RTC::TCPConnection* tcpConnection) :
tcpConnection(tcpConnection),
type(Type::TCP)
protocol(Protocol::TCP)
{}
inline
......@@ -72,25 +74,19 @@ namespace RTC
}
inline
bool TransportTuple::IsUDP()
TransportTuple::Protocol TransportTuple::GetProtocol()
{
return (this->type == Type::UDP);
}
inline
bool TransportTuple::IsTCP()
{
return (this->type == Type::TCP);
return this->protocol;
}
inline
bool TransportTuple::Compare(TransportTuple* tuple)
{
if (this->type == Type::UDP && tuple->IsUDP())
if (this->protocol == Protocol::UDP && tuple->GetProtocol() == Protocol::UDP)
{
return (this->udpSocket == tuple->udpSocket && Utils::IP::CompareAddresses(this->udpRemoteAddr, tuple->GetRemoteAddress()));
}
else if (this->type == Type::TCP && tuple->IsTCP())
else if (this->protocol == Protocol::TCP && tuple->GetProtocol() == Protocol::TCP)
{
return (this->tcpConnection == tuple->tcpConnection);
}
......@@ -103,16 +99,25 @@ namespace RTC
inline
void TransportTuple::Send(const MS_BYTE* data, size_t len)
{
if (this->type == Type::UDP)
if (this->protocol == Protocol::UDP)
this->udpSocket->Send(data, len, this->udpRemoteAddr);
else
this->tcpConnection->Send(data, len);
}
inline
const struct sockaddr* TransportTuple::GetLocalAddress()
{
if (this->protocol == Protocol::UDP)
return this->udpSocket->GetLocalAddress();
else
return this->tcpConnection->GetLocalAddress();
}
inline
const struct sockaddr* TransportTuple::GetRemoteAddress()
{
if (this->type == Type::UDP)
if (this->protocol == Protocol::UDP)
return (const struct sockaddr*)this->udpRemoteAddr;
else
return this->tcpConnection->GetPeerAddress();
......
......@@ -27,12 +27,13 @@ public:
TCPConnection(size_t bufferSize);
virtual ~TCPConnection();
void Setup(Listener* listener, const std::string &localIP, MS_PORT localPort);
void Setup(Listener* listener, struct sockaddr_storage* localAddr, const std::string &localIP, MS_PORT localPort);
uv_tcp_t* GetUvHandle();
void Start();
void Write(const MS_BYTE* data, size_t len);
void Write(const MS_BYTE* data1, size_t len1, const MS_BYTE* data2, size_t len2);
void Write(const std::string &data);
const struct sockaddr* GetLocalAddress();
const std::string& GetLocalIP();
MS_PORT GetLocalPort();
const struct sockaddr* GetPeerAddress();
......@@ -62,6 +63,7 @@ private:
// Passed by argument.
Listener* listener = nullptr;
// Others.
struct sockaddr_storage* localAddr = nullptr;
bool isClosing = false;
bool isClosedByPeer = false;
bool hasError = false;
......@@ -94,6 +96,12 @@ void TCPConnection::Write(const std::string &data)
Write((const MS_BYTE*)data.c_str(), data.size());
}
inline
const struct sockaddr* TCPConnection::GetLocalAddress()
{
return (const struct sockaddr*)this->localAddr;
}
inline
const std::string& TCPConnection::GetLocalIP()
{
......
......@@ -204,15 +204,8 @@ namespace RTC
MS_ASSERT(stored_tuple, "cannot force the selected tuple if the given tuple was not already a valid tuple");
// If the given tuple was already the selected tuple do nothing.
if (stored_tuple == this->selectedTuple)
return;
// Mark it as selected tuple.
SetSelectedTuple(stored_tuple);
// Notify the listener.
this->listener->onICESelectedTuple(this, stored_tuple);
}
void IceServer::HandleTuple(RTC::TransportTuple* tuple, bool has_use_candidate)
......@@ -243,7 +236,6 @@ namespace RTC
this->state = IceState::CONNECTED;
// Notify the listener.
this->listener->onICESelectedTuple(this, stored_tuple);
this->listener->onICEConnected(this);
}
else
......@@ -260,7 +252,6 @@ namespace RTC
this->state = IceState::COMPLETED;
// Notify the listener.
this->listener->onICESelectedTuple(this, stored_tuple);
this->listener->onICECompleted(this);
}
......@@ -298,7 +289,6 @@ namespace RTC
this->state = IceState::COMPLETED;
// Notify the listener.
this->listener->onICESelectedTuple(this, stored_tuple);
this->listener->onICECompleted(this);
}
......@@ -329,9 +319,6 @@ namespace RTC
// Mark it as selected tuple.
SetSelectedTuple(stored_tuple);
// Notify the listener.
this->listener->onICESelectedTuple(this, stored_tuple);
}
break;
......@@ -351,7 +338,7 @@ namespace RTC
// If it is UDP then we must store the remote address (until now it is
// just a pointer that will be freed soon).
if (stored_tuple->IsUDP())
if (stored_tuple->GetProtocol() == TransportTuple::Protocol::UDP)
stored_tuple->StoreUdpRemoteAddress();
// Return the address of the inserted tuple.
......@@ -389,7 +376,14 @@ namespace RTC
{
MS_TRACE();
// If already the selected tuple do nothing.
if (stored_tuple == this->selectedTuple)
return;
this->selectedTuple = stored_tuple;
// Notify the listener.
this->listener->onICESelectedTuple(this, this->selectedTuple);
}
// TODO
......
......@@ -249,6 +249,11 @@ namespace RTC
static const Json::StaticString k_usernameFragment("usernameFragment");
static const Json::StaticString k_password("password");
static const Json::StaticString k_iceLocalCandidates("iceLocalCandidates");
static const Json::StaticString k_iceSelectedTuple("iceSelectedTuple");
static const Json::StaticString k_iceState("iceState");
static const Json::StaticString v_new("new");
static const Json::StaticString v_connected("connected");
static const Json::StaticString v_completed("completed");
static const Json::StaticString k_dtlsLocalParameters("dtlsLocalParameters");
static const Json::StaticString k_fingerprints("fingerprints");
static const Json::StaticString k_role("role");
......@@ -256,9 +261,7 @@ namespace RTC
static const Json::StaticString v_client("client");
static const Json::StaticString v_server("server");
static const Json::StaticString k_dtlsState("dtlsState");
static const Json::StaticString v_new("new");
static const Json::StaticString v_connecting("connecting");
static const Json::StaticString v_connected("connected");
static const Json::StaticString v_closed("closed");
static const Json::StaticString v_failed("failed");
......@@ -281,6 +284,24 @@ namespace RTC
data[k_iceLocalCandidates].append(iceCandidate.toJson());
}
// Add `iceSelectedTuple`.
if (this->selectedTuple)
data[k_iceSelectedTuple] = this->selectedTuple->toJson();
// Add `iceState`.
switch (this->iceServer->GetState())
{
case RTC::IceServer::IceState::NEW:
data[k_iceState] = v_new;
break;
case RTC::IceServer::IceState::CONNECTED:
data[k_iceState] = v_connected;
break;
case RTC::IceServer::IceState::COMPLETED:
data[k_iceState] = v_completed;
break;
}
// Add `dtlsLocalParameters.fingerprints`.
data[k_dtlsLocalParameters][k_fingerprints] = RTC::DTLSTransport::GetLocalFingerprints();
......@@ -754,11 +775,13 @@ namespace RTC
{
MS_TRACE();
static const Json::StaticString k_iceSelectedTuple("iceSelectedTuple");
Json::Value eventData;
if (!IsAlive())
return;
this->selectedTuple = tuple;
/*
* RFC 5245 section 11.2 "Receiving Media":
*
......@@ -766,22 +789,48 @@ namespace RTC
* on any candidates provided for that component.
*/
this->selectedTuple = tuple;
// If ready, run the DTLS handler.
MayRunDTLSTransport();
// Notify.
eventData[k_iceSelectedTuple] = tuple->toJson();
// Notify.
this->notifier->Emit(this->transportId, "iceselectedtuplechange", eventData);
}
void Transport::onICEConnected(RTC::IceServer* iceServer)
{
MS_TRACE();
static const Json::StaticString k_iceState("iceState");
static const Json::StaticString v_connected("connected");
Json::Value eventData;
MS_DEBUG("ICE connected");
// Notify.
eventData[k_iceState] = v_connected;
this->notifier->Emit(this->transportId, "icestatechange", eventData);
}
void Transport::onICECompleted(RTC::IceServer* iceServer)
{
MS_TRACE();
static const Json::StaticString k_iceState("iceState");
static const Json::StaticString v_completed("completed");
Json::Value eventData;
MS_DEBUG("ICE completed");
// Notify.
eventData[k_iceState] = v_completed;
this->notifier->Emit(this->transportId, "icestatechange", eventData);
}
void Transport::onDTLSConnecting(RTC::DTLSTransport* dtlsTransport)
......
......@@ -2,12 +2,54 @@
#include "RTC/TransportTuple.h"
#include "Logger.h"
#include "Utils.h"
#include <string>
namespace RTC
{
/* Instance methods. */
Json::Value TransportTuple::toJson()
{
MS_TRACE();
static const Json::StaticString k_local("local");
static const Json::StaticString k_remote("remote");
static const Json::StaticString k_ip("ip");
static const Json::StaticString k_port("port");
static const Json::StaticString k_protocol("protocol");
static const Json::StaticString v_udp("udp");
static const Json::StaticString v_tcp("tcp");
Json::Value data;
Json::Value localTuple;
Json::Value remoteTuple;
int ip_family;
std::string ip;
MS_PORT port;
Utils::IP::GetAddressInfo(this->GetLocalAddress(), &ip_family, ip, &port);
localTuple[k_ip] = ip;
localTuple[k_port] = port;
if (this->GetProtocol() == RTC::TransportTuple::Protocol::UDP)
localTuple[k_protocol] = v_udp;
else
localTuple[k_protocol] = v_tcp;
Utils::IP::GetAddressInfo(this->GetRemoteAddress(), &ip_family, ip, &port);
remoteTuple[k_ip] = ip;
remoteTuple[k_port] = port;
if (this->GetProtocol() == RTC::TransportTuple::Protocol::UDP)
remoteTuple[k_protocol] = v_udp;
else
remoteTuple[k_protocol] = v_tcp;
data[k_local] = localTuple;
data[k_remote] = remoteTuple;
return data;
}
void TransportTuple::Dump()
{
MS_TRACE();
......@@ -15,9 +57,9 @@ namespace RTC
if (!Logger::HasDebugLevel())
return;
switch (this->type)
switch (this->protocol)
{
case Type::UDP:
case Protocol::UDP:
{
int remote_family;
std::string remote_ip;
......@@ -31,7 +73,7 @@ namespace RTC
break;
}
case Type::TCP:
case Protocol::TCP:
{
MS_DEBUG("[TCP | local: %s : %u | remote: %s : %u]",
this->tcpConnection->GetLocalIP().c_str(), (unsigned int)this->tcpConnection->GetLocalPort(),
......
......@@ -78,7 +78,7 @@ TCPConnection::~TCPConnection()
delete[] this->buffer;
}
void TCPConnection::Setup(Listener* listener, const std::string &localIP, MS_PORT localPort)
void TCPConnection::Setup(Listener* listener, struct sockaddr_storage* localAddr, const std::string &localIP, MS_PORT localPort)
{
MS_TRACE();
......@@ -97,6 +97,7 @@ void TCPConnection::Setup(Listener* listener, const std::string &localIP, MS_POR
this->listener = listener;
// Set the local address.
this->localAddr = localAddr;
this->localIP = localIP;
this->localPort = localPort;
}
......
......@@ -228,7 +228,7 @@ void TCPServer::onUvConnection(int status)
try
{
connection->Setup(this, this->localIP, this->localPort);
connection->Setup(this, &(this->localAddr), this->localIP, this->localPort);
}
catch (const MediaSoupError &error)
{
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论