提交 81ab544b authored 作者: Iñaki Baz Castillo's avatar Iñaki Baz Castillo

Habemus transport.on('dtlsstatechange') events.

上级 100c7ab1
...@@ -264,7 +264,7 @@ class Channel extends EventEmitter ...@@ -264,7 +264,7 @@ class Channel extends EventEmitter
let targetId = json.targetId; let targetId = json.targetId;
let eventName = json.eventName; let eventName = json.eventName;
logger.debug('received Notification [targetId:%s, eventName:%s]', targetId, eventName); logger.debug('received Notification [targetId:%s, event:%s]', targetId, eventName);
// Emit it // Emit it
this.emit(targetId, eventName, json.eventData); this.emit(targetId, eventName, json.eventData);
......
...@@ -29,10 +29,6 @@ class Room extends EventEmitter ...@@ -29,10 +29,6 @@ class Room extends EventEmitter
this._closed = false; this._closed = false;
// TODO: subscribe to channel notifications from the worker // TODO: subscribe to channel notifications from the worker
this._channel.on(this._internal.roomId, (eventName, eventData) =>
{
logger.debug('----- room %s received event "%s" [data:%o]', this._internal.roomId, eventName, eventData);
});
} }
get closed() get closed()
......
...@@ -27,7 +27,25 @@ class Transport extends EventEmitter ...@@ -27,7 +27,25 @@ class Transport extends EventEmitter
// Closed flag // Closed flag
this._closed = false; this._closed = false;
// TODO: subscribe to channel notifications from the worker // Subscribe to notifications
this._channel.on(this._internal.transportId, (event, data) =>
{
switch (event)
{
case 'dtlsstatechange':
{
this._data.dtlsState = data.dtlsState;
// Emit it to the app
this.emit(event, data);
break;
}
default:
logger.error('ignoring unknown event "%s"', event);
}
});
} }
get closed() get closed()
...@@ -75,6 +93,10 @@ class Transport extends EventEmitter ...@@ -75,6 +93,10 @@ class Transport extends EventEmitter
this._closed = true; this._closed = true;
// Remove notification subscriptions (do it in the next iteration to
// ensure the user receives closure events)
setImmediate(() => this._channel.removeAllListeners(this._internal.transportId));
if (!dontSendChannel) if (!dontSendChannel)
{ {
let internal = this._internal; let internal = this._internal;
......
...@@ -69,6 +69,7 @@ tap.test('transport.close() must succeed', { timeout: 1000 }, (t) => ...@@ -69,6 +69,7 @@ tap.test('transport.close() must succeed', { timeout: 1000 }, (t) =>
{ {
let server = mediasoup.Server(); let server = mediasoup.Server();
t.plan(7);
t.tearDown(() => server.close()); t.tearDown(() => server.close());
let room = server.Room(); let room = server.Room();
...@@ -79,6 +80,13 @@ tap.test('transport.close() must succeed', { timeout: 1000 }, (t) => ...@@ -79,6 +80,13 @@ tap.test('transport.close() must succeed', { timeout: 1000 }, (t) =>
{ {
t.pass('peer.createTransport() succeeded'); t.pass('peer.createTransport() succeeded');
transport.on('dtlsstatechange', (data) =>
{
t.pass('event "dtlsstatechange" fired');
t.equal(data.dtlsState, 'closed', 'event `data.dtlsState` must be "closed"');
t.equal(transport.dtlsState, 'closed', '`transport.dtlsState` must be "closed"');
});
transport.on('close', (error) => transport.on('close', (error) =>
{ {
t.error(error, 'transport should close cleanly'); t.error(error, 'transport should close cleanly');
...@@ -88,7 +96,6 @@ tap.test('transport.close() must succeed', { timeout: 1000 }, (t) => ...@@ -88,7 +96,6 @@ tap.test('transport.close() must succeed', { timeout: 1000 }, (t) =>
{ {
t.pass('peer.dump() succeeded'); t.pass('peer.dump() succeeded');
t.equal(Object.keys(data.transports).length, 0, 'peer.dump() should retrieve zero transports'); t.equal(Object.keys(data.transports).length, 0, 'peer.dump() should retrieve zero transports');
t.end();
}) })
.catch((error) => t.fail(`peer.dump() failed: ${error}`)); .catch((error) => t.fail(`peer.dump() failed: ${error}`));
}); });
......
...@@ -2,7 +2,8 @@ ...@@ -2,7 +2,8 @@
'use strict'; 'use strict';
process.env.DEBUG = '*ERROR* *WARN* mediasoup* mediasoup-tester*'; // process.env.DEBUG = '*ERROR* *WARN* mediasoup* mediasoup-tester*';
process.env.DEBUG = '*ERROR* *WARN* mediasoup* mediasoup-tester* -mediasoup:mediasoup-worker*';
const http = require('http'); const http = require('http');
const url = require('url'); const url = require('url');
...@@ -170,6 +171,20 @@ app.put('/test-transport', function(req) ...@@ -170,6 +171,20 @@ app.put('/test-transport', function(req)
{ {
let promise = mediaPeer.createTransport({ udp: true, tcp: false }); let promise = mediaPeer.createTransport({ udp: true, tcp: false });
// Set transport event listeners
promise.then((transport) =>
{
transport.on('close', () =>
{
debug('transport "close" event');
});
transport.on('dtlsstatechange', (data) =>
{
debug('transport "dtlsstatechange" event [data.dtlsState:%s, transport.dtlsState:%s]', data.dtlsState, transport.dtlsState);
});
})
// Add the transport promise to the array of promises // Add the transport promise to the array of promises
transportPromises.push(promise); transportPromises.push(promise);
} }
......
...@@ -39,10 +39,10 @@ private: ...@@ -39,10 +39,10 @@ private:
// Passed by argument. // Passed by argument.
Channel::UnixStreamSocket* channel = nullptr; Channel::UnixStreamSocket* channel = nullptr;
// Allocated by this. // Allocated by this.
Channel::Notifier* notifier = nullptr;
SignalsHandler* signalsHandler = nullptr; SignalsHandler* signalsHandler = nullptr;
// Others. // Others.
bool closed = false; bool closed = false;
Channel::Notifier* notifier = nullptr;
std::unordered_map<unsigned int, RTC::Room*> rooms; std::unordered_map<unsigned int, RTC::Room*> rooms;
}; };
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
#include "RTC/Transport.h" #include "RTC/Transport.h"
#include "Channel/Request.h" #include "Channel/Request.h"
#include "Channel/Notifier.h"
#include <string> #include <string>
#include <unordered_map> #include <unordered_map>
#include <json/json.h> #include <json/json.h>
...@@ -20,7 +21,7 @@ namespace RTC ...@@ -20,7 +21,7 @@ namespace RTC
}; };
public: public:
Peer(Listener* listener, unsigned int peerId, std::string& peerName); Peer(Listener* listener, Channel::Notifier* notifier, unsigned int peerId, std::string& peerName);
virtual ~Peer(); virtual ~Peer();
void Close(); void Close();
...@@ -42,6 +43,7 @@ namespace RTC ...@@ -42,6 +43,7 @@ namespace RTC
private: private:
// Passed by argument. // Passed by argument.
Listener* listener = nullptr; Listener* listener = nullptr;
Channel::Notifier* notifier = nullptr;
// Others. // Others.
std::unordered_map<unsigned int, RTC::Transport*> transports; std::unordered_map<unsigned int, RTC::Transport*> transports;
}; };
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
#include "RTC/Peer.h" #include "RTC/Peer.h"
#include "Channel/Request.h" #include "Channel/Request.h"
#include "Channel/Notifier.h"
#include <unordered_map> #include <unordered_map>
#include <json/json.h> #include <json/json.h>
...@@ -19,7 +20,7 @@ namespace RTC ...@@ -19,7 +20,7 @@ namespace RTC
}; };
public: public:
Room(Listener* listener, unsigned int roomId); Room(Listener* listener, Channel::Notifier* notifier, unsigned int roomId);
virtual ~Room(); virtual ~Room();
void Close(); void Close();
...@@ -40,6 +41,7 @@ namespace RTC ...@@ -40,6 +41,7 @@ namespace RTC
private: private:
// Passed by argument. // Passed by argument.
Listener* listener = nullptr; Listener* listener = nullptr;
Channel::Notifier* notifier = nullptr;
// Others. // Others.
std::unordered_map<unsigned int, RTC::Peer*> peers; std::unordered_map<unsigned int, RTC::Peer*> peers;
}; };
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
#include "RTC/UDPSocket.h" #include "RTC/UDPSocket.h"
#include "RTC/TCPServer.h" #include "RTC/TCPServer.h"
#include "RTC/TCPConnection.h" #include "RTC/TCPConnection.h"
#include "Channel/Notifier.h"
#include <string> #include <string>
#include <vector> #include <vector>
#include <list> #include <list>
...@@ -52,7 +53,7 @@ namespace RTC ...@@ -52,7 +53,7 @@ namespace RTC
static size_t maxSources; static size_t maxSources;
public: public:
Transport(Listener* listener, unsigned int transportId, Json::Value& data, Transport* rtpTransport = nullptr); Transport(Listener* listener, Channel::Notifier* notifier, unsigned int transportId, Json::Value& data, Transport* rtpTransport = nullptr);
virtual ~Transport(); virtual ~Transport();
void Close(); void Close();
...@@ -124,7 +125,7 @@ namespace RTC ...@@ -124,7 +125,7 @@ namespace RTC
private: private:
// Passed by argument. // Passed by argument.
Listener* listener = nullptr; Listener* listener = nullptr;
RTC::Transport::IceComponent iceComponent; Channel::Notifier* notifier = nullptr;
// Allocated by this. // Allocated by this.
RTC::IceServer* iceServer = nullptr; RTC::IceServer* iceServer = nullptr;
std::vector<RTC::UDPSocket*> udpSockets; std::vector<RTC::UDPSocket*> udpSockets;
...@@ -133,6 +134,7 @@ namespace RTC ...@@ -133,6 +134,7 @@ namespace RTC
// Others. // Others.
bool allocated = false; bool allocated = false;
// Others (ICE). // Others (ICE).
RTC::Transport::IceComponent iceComponent;
std::vector<IceCandidate> iceLocalCandidates; std::vector<IceCandidate> iceLocalCandidates;
std::list<RTC::TransportSource> sources; std::list<RTC::TransportSource> sources;
RTC::TransportSource* sendingSource = nullptr; RTC::TransportSource* sendingSource = nullptr;
......
...@@ -199,7 +199,7 @@ void Loop::onChannelRequest(Channel::UnixStreamSocket* channel, Channel::Request ...@@ -199,7 +199,7 @@ void Loop::onChannelRequest(Channel::UnixStreamSocket* channel, Channel::Request
try try
{ {
room = new RTC::Room(this, roomId); room = new RTC::Room(this, this->notifier, roomId);
} }
catch (const MediaSoupError &error) catch (const MediaSoupError &error)
{ {
...@@ -212,9 +212,6 @@ void Loop::onChannelRequest(Channel::UnixStreamSocket* channel, Channel::Request ...@@ -212,9 +212,6 @@ void Loop::onChannelRequest(Channel::UnixStreamSocket* channel, Channel::Request
MS_DEBUG("Room created [roomId:%u]", roomId); MS_DEBUG("Room created [roomId:%u]", roomId);
request->Accept(); request->Accept();
// TODO: TESTING
this->notifier->Emit(roomId, "created");
break; break;
} }
......
...@@ -821,7 +821,7 @@ namespace RTC ...@@ -821,7 +821,7 @@ namespace RTC
if (was_connected) if (was_connected)
{ {
MS_DEBUG("DTLS disconnected"); MS_DEBUG("disconnected");
Reset(); Reset();
...@@ -830,7 +830,7 @@ namespace RTC ...@@ -830,7 +830,7 @@ namespace RTC
} }
else else
{ {
MS_DEBUG("DTLS connection failed"); MS_DEBUG("connection failed");
Reset(); Reset();
......
...@@ -8,10 +8,11 @@ namespace RTC ...@@ -8,10 +8,11 @@ namespace RTC
{ {
/* Instance methods. */ /* Instance methods. */
Peer::Peer(Listener* listener, unsigned int peerId, std::string& peerName) : Peer::Peer(Listener* listener, Channel::Notifier* notifier, unsigned int peerId, std::string& peerName) :
peerId(peerId), peerId(peerId),
peerName(peerName), peerName(peerName),
listener(listener) listener(listener),
notifier(notifier)
{ {
MS_TRACE(); MS_TRACE();
} }
...@@ -118,7 +119,7 @@ namespace RTC ...@@ -118,7 +119,7 @@ namespace RTC
try try
{ {
transport = new RTC::Transport(this, transportId, request->data); transport = new RTC::Transport(this, this->notifier, transportId, request->data);
} }
catch (const MediaSoupError &error) catch (const MediaSoupError &error)
{ {
......
...@@ -9,9 +9,10 @@ namespace RTC ...@@ -9,9 +9,10 @@ namespace RTC
{ {
/* Instance methods. */ /* Instance methods. */
Room::Room(Listener* listener, unsigned int roomId) : Room::Room(Listener* listener, Channel::Notifier* notifier, unsigned int roomId) :
roomId(roomId), roomId(roomId),
listener(listener) listener(listener),
notifier(notifier)
{ {
MS_TRACE(); MS_TRACE();
} }
...@@ -128,7 +129,7 @@ namespace RTC ...@@ -128,7 +129,7 @@ namespace RTC
try try
{ {
peer = new RTC::Peer(this, peerId, peerName); peer = new RTC::Peer(this, this->notifier, peerId, peerName);
} }
catch (const MediaSoupError &error) catch (const MediaSoupError &error)
{ {
......
...@@ -32,9 +32,10 @@ namespace RTC ...@@ -32,9 +32,10 @@ namespace RTC
/* Instance methods. */ /* Instance methods. */
Transport::Transport(Listener* listener, unsigned int transportId, Json::Value& data, Transport* rtpTransport) : Transport::Transport(Listener* listener, Channel::Notifier* notifier, unsigned int transportId, Json::Value& data, Transport* rtpTransport) :
transportId(transportId), transportId(transportId),
listener(listener) listener(listener),
notifier(notifier)
{ {
MS_TRACE(); MS_TRACE();
...@@ -496,7 +497,7 @@ namespace RTC ...@@ -496,7 +497,7 @@ namespace RTC
if (this->iceComponent != IceComponent::RTP) if (this->iceComponent != IceComponent::RTP)
MS_THROW_ERROR("cannot call CreateAssociatedTransport() on a RTCP Transport"); MS_THROW_ERROR("cannot call CreateAssociatedTransport() on a RTCP Transport");
return new RTC::Transport(this->listener, transportId, nullData, this); return new RTC::Transport(this->listener, this->notifier, transportId, nullData, this);
} }
inline inline
...@@ -768,17 +769,29 @@ namespace RTC ...@@ -768,17 +769,29 @@ namespace RTC
{ {
MS_TRACE(); MS_TRACE();
static const Json::StaticString k_dtlsState("dtlsState");
static const Json::StaticString v_connecting("connecting");
Json::Value eventData;
MS_DEBUG("DTLS connecting"); MS_DEBUG("DTLS connecting");
this->dtlsState = DtlsTransportState::CONNECTING; this->dtlsState = DtlsTransportState::CONNECTING;
// TODO: notify 'connecting'. // Notify.
eventData[k_dtlsState] = v_connecting;
this->notifier->Emit(this->transportId, "dtlsstatechange", eventData);
} }
void Transport::onDTLSConnected(RTC::DTLSTransport* dtlsTransport, RTC::SRTPSession::SRTPProfile srtp_profile, MS_BYTE* srtp_local_key, size_t srtp_local_key_len, MS_BYTE* srtp_remote_key, size_t srtp_remote_key_len) void Transport::onDTLSConnected(RTC::DTLSTransport* dtlsTransport, RTC::SRTPSession::SRTPProfile srtp_profile, MS_BYTE* srtp_local_key, size_t srtp_local_key_len, MS_BYTE* srtp_remote_key, size_t srtp_remote_key_len)
{ {
MS_TRACE(); MS_TRACE();
static const Json::StaticString k_dtlsState("dtlsState");
static const Json::StaticString v_connected("connected");
Json::Value eventData;
MS_DEBUG("DTLS connected"); MS_DEBUG("DTLS connected");
this->dtlsState = DtlsTransportState::CONNECTED; this->dtlsState = DtlsTransportState::CONNECTED;
...@@ -787,13 +800,21 @@ namespace RTC ...@@ -787,13 +800,21 @@ namespace RTC
// SetLocalSRTPKey(srtp_profile, srtp_local_key, srtp_local_key_len); // SetLocalSRTPKey(srtp_profile, srtp_local_key, srtp_local_key_len);
// SetRemoteSRTPKey(srtp_profile, srtp_remote_key, srtp_remote_key_len); // SetRemoteSRTPKey(srtp_profile, srtp_remote_key, srtp_remote_key_len);
// TODO: notify 'connected'. // Notify.
eventData[k_dtlsState] = v_connected;
this->notifier->Emit(this->transportId, "dtlsstatechange", eventData);
} }
void Transport::onDTLSDisconnected(RTC::DTLSTransport* dtlsTransport) void Transport::onDTLSDisconnected(RTC::DTLSTransport* dtlsTransport)
{ {
MS_TRACE(); MS_TRACE();
static const Json::StaticString k_dtlsState("dtlsState");
static const Json::StaticString v_closed("closed");
DtlsTransportState previousDtlsState = this->dtlsState;
Json::Value eventData;
MS_DEBUG("DTLS remotely disconnected"); MS_DEBUG("DTLS remotely disconnected");
this->dtlsState = DtlsTransportState::CLOSED; this->dtlsState = DtlsTransportState::CLOSED;
...@@ -801,13 +822,23 @@ namespace RTC ...@@ -801,13 +822,23 @@ namespace RTC
// Call Terminate() so all the servers are closed. // Call Terminate() so all the servers are closed.
Terminate(); Terminate();
// TODO: notify 'closed' if not already 'closed'. // Notify.
if (previousDtlsState != DtlsTransportState::CLOSED)
{
eventData[k_dtlsState] = v_closed;
this->notifier->Emit(this->transportId, "dtlsstatechange", eventData);
}
} }
void Transport::onDTLSFailed(RTC::DTLSTransport* dtlsTransport) void Transport::onDTLSFailed(RTC::DTLSTransport* dtlsTransport)
{ {
MS_TRACE(); MS_TRACE();
static const Json::StaticString k_dtlsState("dtlsState");
static const Json::StaticString v_failed("failed");
Json::Value eventData;
MS_DEBUG("DTLS failed"); MS_DEBUG("DTLS failed");
this->dtlsState = DtlsTransportState::FAILED; this->dtlsState = DtlsTransportState::FAILED;
...@@ -815,13 +846,21 @@ namespace RTC ...@@ -815,13 +846,21 @@ namespace RTC
// Call Terminate() so all the servers are closed. // Call Terminate() so all the servers are closed.
Terminate(); Terminate();
// TODO: notify 'failed'. // Notify.
eventData[k_dtlsState] = v_failed;
this->notifier->Emit(this->transportId, "dtlsstatechange", eventData);
} }
void Transport::onDTLSClosed(RTC::DTLSTransport* dtlsTransport) void Transport::onDTLSClosed(RTC::DTLSTransport* dtlsTransport)
{ {
MS_TRACE(); MS_TRACE();
static const Json::StaticString k_dtlsState("dtlsState");
static const Json::StaticString v_closed("closed");
DtlsTransportState previousDtlsState = this->dtlsState;
Json::Value eventData;
MS_DEBUG("DTLS closed"); MS_DEBUG("DTLS closed");
this->dtlsState = DtlsTransportState::CLOSED; this->dtlsState = DtlsTransportState::CLOSED;
...@@ -829,7 +868,12 @@ namespace RTC ...@@ -829,7 +868,12 @@ namespace RTC
// NOTE: This is just called when calling Close() so don't call // NOTE: This is just called when calling Close() so don't call
// Terminate() here (not needed). // Terminate() here (not needed).
// TODO: notify 'closed' if not already 'closed'. // Notify.
if (previousDtlsState != DtlsTransportState::CLOSED)
{
eventData[k_dtlsState] = v_closed;
this->notifier->Emit(this->transportId, "dtlsstatechange", eventData);
}
} }
void Transport::onOutgoingDTLSData(RTC::DTLSTransport* dtlsTransport, const MS_BYTE* data, size_t len) void Transport::onOutgoingDTLSData(RTC::DTLSTransport* dtlsTransport, const MS_BYTE* data, size_t len)
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论