提交 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
let targetId = json.targetId;
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
this.emit(targetId, eventName, json.eventData);
......
......@@ -29,10 +29,6 @@ class Room extends EventEmitter
this._closed = false;
// 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()
......
......@@ -27,7 +27,25 @@ class Transport extends EventEmitter
// Closed flag
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()
......@@ -75,6 +93,10 @@ class Transport extends EventEmitter
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)
{
let internal = this._internal;
......
......@@ -69,6 +69,7 @@ tap.test('transport.close() must succeed', { timeout: 1000 }, (t) =>
{
let server = mediasoup.Server();
t.plan(7);
t.tearDown(() => server.close());
let room = server.Room();
......@@ -79,6 +80,13 @@ tap.test('transport.close() must succeed', { timeout: 1000 }, (t) =>
{
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) =>
{
t.error(error, 'transport should close cleanly');
......@@ -88,7 +96,6 @@ tap.test('transport.close() must succeed', { timeout: 1000 }, (t) =>
{
t.pass('peer.dump() succeeded');
t.equal(Object.keys(data.transports).length, 0, 'peer.dump() should retrieve zero transports');
t.end();
})
.catch((error) => t.fail(`peer.dump() failed: ${error}`));
});
......
......@@ -2,7 +2,8 @@
'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 url = require('url');
......@@ -170,6 +171,20 @@ app.put('/test-transport', function(req)
{
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
transportPromises.push(promise);
}
......
......@@ -39,10 +39,10 @@ private:
// Passed by argument.
Channel::UnixStreamSocket* channel = nullptr;
// Allocated by this.
Channel::Notifier* notifier = nullptr;
SignalsHandler* signalsHandler = nullptr;
// Others.
bool closed = false;
Channel::Notifier* notifier = nullptr;
std::unordered_map<unsigned int, RTC::Room*> rooms;
};
......
......@@ -3,6 +3,7 @@
#include "RTC/Transport.h"
#include "Channel/Request.h"
#include "Channel/Notifier.h"
#include <string>
#include <unordered_map>
#include <json/json.h>
......@@ -20,7 +21,7 @@ namespace RTC
};
public:
Peer(Listener* listener, unsigned int peerId, std::string& peerName);
Peer(Listener* listener, Channel::Notifier* notifier, unsigned int peerId, std::string& peerName);
virtual ~Peer();
void Close();
......@@ -42,6 +43,7 @@ namespace RTC
private:
// Passed by argument.
Listener* listener = nullptr;
Channel::Notifier* notifier = nullptr;
// Others.
std::unordered_map<unsigned int, RTC::Transport*> transports;
};
......
......@@ -3,6 +3,7 @@
#include "RTC/Peer.h"
#include "Channel/Request.h"
#include "Channel/Notifier.h"
#include <unordered_map>
#include <json/json.h>
......@@ -19,7 +20,7 @@ namespace RTC
};
public:
Room(Listener* listener, unsigned int roomId);
Room(Listener* listener, Channel::Notifier* notifier, unsigned int roomId);
virtual ~Room();
void Close();
......@@ -40,6 +41,7 @@ namespace RTC
private:
// Passed by argument.
Listener* listener = nullptr;
Channel::Notifier* notifier = nullptr;
// Others.
std::unordered_map<unsigned int, RTC::Peer*> peers;
};
......
......@@ -10,6 +10,7 @@
#include "RTC/UDPSocket.h"
#include "RTC/TCPServer.h"
#include "RTC/TCPConnection.h"
#include "Channel/Notifier.h"
#include <string>
#include <vector>
#include <list>
......@@ -52,7 +53,7 @@ namespace RTC
static size_t maxSources;
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();
void Close();
......@@ -124,7 +125,7 @@ namespace RTC
private:
// Passed by argument.
Listener* listener = nullptr;
RTC::Transport::IceComponent iceComponent;
Channel::Notifier* notifier = nullptr;
// Allocated by this.
RTC::IceServer* iceServer = nullptr;
std::vector<RTC::UDPSocket*> udpSockets;
......@@ -133,6 +134,7 @@ namespace RTC
// Others.
bool allocated = false;
// Others (ICE).
RTC::Transport::IceComponent iceComponent;
std::vector<IceCandidate> iceLocalCandidates;
std::list<RTC::TransportSource> sources;
RTC::TransportSource* sendingSource = nullptr;
......
......@@ -199,7 +199,7 @@ void Loop::onChannelRequest(Channel::UnixStreamSocket* channel, Channel::Request
try
{
room = new RTC::Room(this, roomId);
room = new RTC::Room(this, this->notifier, roomId);
}
catch (const MediaSoupError &error)
{
......@@ -212,9 +212,6 @@ void Loop::onChannelRequest(Channel::UnixStreamSocket* channel, Channel::Request
MS_DEBUG("Room created [roomId:%u]", roomId);
request->Accept();
// TODO: TESTING
this->notifier->Emit(roomId, "created");
break;
}
......
......@@ -821,7 +821,7 @@ namespace RTC
if (was_connected)
{
MS_DEBUG("DTLS disconnected");
MS_DEBUG("disconnected");
Reset();
......@@ -830,7 +830,7 @@ namespace RTC
}
else
{
MS_DEBUG("DTLS connection failed");
MS_DEBUG("connection failed");
Reset();
......
......@@ -8,10 +8,11 @@ namespace RTC
{
/* 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),
peerName(peerName),
listener(listener)
listener(listener),
notifier(notifier)
{
MS_TRACE();
}
......@@ -118,7 +119,7 @@ namespace RTC
try
{
transport = new RTC::Transport(this, transportId, request->data);
transport = new RTC::Transport(this, this->notifier, transportId, request->data);
}
catch (const MediaSoupError &error)
{
......
......@@ -9,9 +9,10 @@ namespace RTC
{
/* Instance methods. */
Room::Room(Listener* listener, unsigned int roomId) :
Room::Room(Listener* listener, Channel::Notifier* notifier, unsigned int roomId) :
roomId(roomId),
listener(listener)
listener(listener),
notifier(notifier)
{
MS_TRACE();
}
......@@ -128,7 +129,7 @@ namespace RTC
try
{
peer = new RTC::Peer(this, peerId, peerName);
peer = new RTC::Peer(this, this->notifier, peerId, peerName);
}
catch (const MediaSoupError &error)
{
......
......@@ -32,9 +32,10 @@ namespace RTC
/* 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),
listener(listener)
listener(listener),
notifier(notifier)
{
MS_TRACE();
......@@ -496,7 +497,7 @@ namespace RTC
if (this->iceComponent != IceComponent::RTP)
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
......@@ -768,17 +769,29 @@ namespace RTC
{
MS_TRACE();
static const Json::StaticString k_dtlsState("dtlsState");
static const Json::StaticString v_connecting("connecting");
Json::Value eventData;
MS_DEBUG("DTLS 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)
{
MS_TRACE();
static const Json::StaticString k_dtlsState("dtlsState");
static const Json::StaticString v_connected("connected");
Json::Value eventData;
MS_DEBUG("DTLS connected");
this->dtlsState = DtlsTransportState::CONNECTED;
......@@ -787,13 +800,21 @@ namespace RTC
// SetLocalSRTPKey(srtp_profile, srtp_local_key, srtp_local_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)
{
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");
this->dtlsState = DtlsTransportState::CLOSED;
......@@ -801,13 +822,23 @@ namespace RTC
// Call Terminate() so all the servers are closed.
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)
{
MS_TRACE();
static const Json::StaticString k_dtlsState("dtlsState");
static const Json::StaticString v_failed("failed");
Json::Value eventData;
MS_DEBUG("DTLS failed");
this->dtlsState = DtlsTransportState::FAILED;
......@@ -815,13 +846,21 @@ namespace RTC
// Call Terminate() so all the servers are closed.
Terminate();
// TODO: notify 'failed'.
// Notify.
eventData[k_dtlsState] = v_failed;
this->notifier->Emit(this->transportId, "dtlsstatechange", eventData);
}
void Transport::onDTLSClosed(RTC::DTLSTransport* dtlsTransport)
{
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");
this->dtlsState = DtlsTransportState::CLOSED;
......@@ -829,7 +868,12 @@ namespace RTC
// NOTE: This is just called when calling Close() so don't call
// 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)
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论