提交 8da707fd authored 作者: Iñaki Baz Castillo's avatar Iñaki Baz Castillo

Lot of work...

上级 56fe7f4c
...@@ -3,6 +3,9 @@ ...@@ -3,6 +3,9 @@
const EventEmitter = require('events').EventEmitter; const EventEmitter = require('events').EventEmitter;
const logger = require('./logger')('Peer'); const logger = require('./logger')('Peer');
const utils = require('./utils');
const errors = require('./errors');
const Transport = require('./Transport');
class Peer extends EventEmitter class Peer extends EventEmitter
{ {
...@@ -21,6 +24,9 @@ class Peer extends EventEmitter ...@@ -21,6 +24,9 @@ class Peer extends EventEmitter
// Channel instance // Channel instance
this._channel = channel; this._channel = channel;
// Set of Transport instances
this._transports = new Set();
// Closed flag // Closed flag
this._closed = false; this._closed = false;
...@@ -39,12 +45,15 @@ class Peer extends EventEmitter ...@@ -39,12 +45,15 @@ class Peer extends EventEmitter
this._closed = true; this._closed = true;
// Close every Transport
this._transports.forEach((transport) => transport.close(undefined, true));
if (!dontSendChannel) if (!dontSendChannel)
{ {
let data = let data =
{ {
roomId: this._roomId, roomId : this._roomId,
peerId: this._peerId peerId : this._peerId
}; };
// Send Channel request // Send Channel request
...@@ -72,8 +81,8 @@ class Peer extends EventEmitter ...@@ -72,8 +81,8 @@ class Peer extends EventEmitter
let data = let data =
{ {
roomId: this._roomId, roomId : this._roomId,
peerId: this._peerId peerId : this._peerId
}; };
return this._channel.request('dumpPeer', data) return this._channel.request('dumpPeer', data)
...@@ -90,6 +99,50 @@ class Peer extends EventEmitter ...@@ -90,6 +99,50 @@ class Peer extends EventEmitter
throw error; throw error;
}); });
} }
/**
* Create a Transport
* @param {Object} options Transport options
* @param {Boolean} options.udp Listen UDP (default true)
* @param {Boolean} options.tcp Listen TCP (default true)
* @return {Promise}
*/
createTransport(options)
{
logger.debug('createTransport() [options:%o]', options);
if (this._closed)
return Promise.reject(errors.Closed('peer closed'));
options = utils.cloneObject(options);
let transportId = utils.randomNumber();
options.roomId = this._roomId;
options.peerId = this._peerId;
options.transportId = transportId;
return this._channel.request('createTransport', options)
.then((data) =>
{
logger.debug('"createTransport" request succeeded');
// Create a Transport instance
var transport = new Transport(options, data, this._channel);
// Store the Transport instance and remove it when closed
this._transports.add(transport);
transport.on('close', () => this._transports.delete(transport));
return transport;
})
.catch((error) =>
{
logger.error('"createTransport" request failed [status:%s, error:"%s"]', error.status, error.message);
throw error;
});
}
} }
module.exports = Peer; module.exports = Peer;
...@@ -45,14 +45,14 @@ class Room extends EventEmitter ...@@ -45,14 +45,14 @@ class Room extends EventEmitter
// Close every Peer // Close every Peer
for (let peer of this._peers.values()) for (let peer of this._peers.values())
{ {
peer.close(null, true); peer.close(undefined, true);
} }
if (!dontSendChannel) if (!dontSendChannel)
{ {
let data = let data =
{ {
roomId: this._roomId roomId : this._roomId
}; };
// Send Channel request // Send Channel request
...@@ -80,7 +80,7 @@ class Room extends EventEmitter ...@@ -80,7 +80,7 @@ class Room extends EventEmitter
let data = let data =
{ {
roomId: this._roomId roomId : this._roomId
}; };
return this._channel.request('dumpRoom', data) return this._channel.request('dumpRoom', data)
......
...@@ -124,7 +124,7 @@ class Server extends EventEmitter ...@@ -124,7 +124,7 @@ class Server extends EventEmitter
{ {
let json = let json =
{ {
workers: {} workers : {}
}; };
for (let data of datas) for (let data of datas)
......
'use strict';
const EventEmitter = require('events').EventEmitter;
const logger = require('./logger')('Transport');
class Transport extends EventEmitter
{
constructor(options, data, channel)
{
logger.debug('constructor() [options:%o, data:%o]', options, data);
super();
// Room identificator
this._roomId = options.roomId;
// Peer identificator
this._peerId = options.peerId;
// Transport identificator
this._transportId = options.transportId;
// Transport data provided by the worker
this._data = data;
// Channel instance
this._channel = channel;
// Closed flag
this._closed = false;
// TODO: subscribe to channel notifications from the worker
}
/**
* Close the Transport
*/
close(error, dontSendChannel)
{
logger.debug('close() [error:%s]', error);
if (this._closed)
return;
this._closed = true;
if (!dontSendChannel)
{
let data =
{
roomId : this._roomId,
peerId : this._peerId,
transportId : this._transportId
};
// Send Channel request
this._channel.request('closeTransport', data)
.then(() =>
{
logger.debug('"closeTransport" request succeeded');
})
.catch((error) =>
{
logger.error('"closeTransport" request failed [status:%s, error:"%s"]', error.status, error.message);
});
}
this.emit('close', error);
}
/**
* Dump the Transport
* @return {Promise}
*/
dump()
{
logger.debug('dump()');
let data =
{
roomId : this._roomId,
peerId : this._peerId,
transportId : this._transportId
};
return this._channel.request('dumpTransport', data)
.then((data) =>
{
logger.debug('"dumpTransport" request succeeded');
return data;
})
.catch((error) =>
{
logger.error('"dumpTransport" request failed [status:%s, error:"%s"]', error.status, error.message);
throw error;
});
}
}
module.exports = Transport;
...@@ -116,7 +116,7 @@ class Worker extends EventEmitter ...@@ -116,7 +116,7 @@ class Worker extends EventEmitter
logger.debug('close() [error:%s]', error); logger.debug('close() [error:%s]', error);
// Close every Room // Close every Room
this._rooms.forEach((room) => room.close(null, true)); this._rooms.forEach((room) => room.close(undefined, true));
// Kill mediasoup-worker process (let it some time so previous // Kill mediasoup-worker process (let it some time so previous
// requests are properly sent and replied) // requests are properly sent and replied)
......
...@@ -22,7 +22,7 @@ module.exports = ...@@ -22,7 +22,7 @@ module.exports =
cloneObject: (obj) => cloneObject: (obj) =>
{ {
if (!obj || typeof obj !== 'object') if (!obj || typeof obj !== 'object' || Array.isArray(obj))
return {}; return {};
else else
return clone(obj, false); return clone(obj, false);
......
...@@ -23,7 +23,10 @@ namespace Channel ...@@ -23,7 +23,10 @@ namespace Channel
dumpRoom, dumpRoom,
createPeer, createPeer,
closePeer, closePeer,
dumpPeer dumpPeer,
createTransport,
closeTransport,
dumpTransport
}; };
private: private:
......
...@@ -17,7 +17,7 @@ public: ...@@ -17,7 +17,7 @@ public:
~Loop(); ~Loop();
private: private:
RTC::Room* GetRoomFromRequest(Channel::Request* request, unsigned int* roomId); RTC::Room* GetRoomFromRequest(Channel::Request* request, unsigned int* roomId = nullptr);
void Close(); void Close();
/* Methods inherited from SignalsHandler::Listener. */ /* Methods inherited from SignalsHandler::Listener. */
......
#ifndef MS_RTC_DTLS_HANDLER_H #ifndef MS_RTC_DTLS_AGENT_H
#define MS_RTC_DTLS_HANDLER_H #define MS_RTC_DTLS_AGENT_H
#include "common.h" #include "common.h"
#include "handles/Timer.h" #include "handles/Timer.h"
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
namespace RTC namespace RTC
{ {
class DTLSHandler : public Timer::Listener class DTLSAgent : public Timer::Listener
{ {
private: private:
struct SrtpProfileMapEntry struct SrtpProfileMapEntry
...@@ -30,14 +30,14 @@ namespace RTC ...@@ -30,14 +30,14 @@ namespace RTC
public: public:
// NOTE: The caller MUST NOT call Reset() or Close() during the // NOTE: The caller MUST NOT call Reset() or Close() during the
// onOutgoingDTLSData() callback. // onOutgoingDTLSData() callback.
virtual void onOutgoingDTLSData(DTLSHandler* dtlsHandler, const MS_BYTE* data, size_t len) = 0; virtual void onOutgoingDTLSData(DTLSAgent* dtlsAgent, const MS_BYTE* data, size_t len) = 0;
// NOTE: The caller MUST NOT call any method during the onDTLSConnected, // NOTE: The caller MUST NOT call any method during the onDTLSConnected,
// onDTLSDisconnected or onDTLSFailed callbacks. // onDTLSDisconnected or onDTLSFailed callbacks.
virtual void onDTLSConnected(DTLSHandler* dtlsHandler) = 0; virtual void onDTLSConnected(DTLSAgent* dtlsAgent) = 0;
virtual void onDTLSDisconnected(DTLSHandler* dtlsHandler) = 0; virtual void onDTLSDisconnected(DTLSAgent* dtlsAgent) = 0;
virtual void onDTLSFailed(DTLSHandler* dtlsHandler) = 0; virtual void onDTLSFailed(DTLSAgent* dtlsAgent) = 0;
virtual void onSRTPKeyMaterial(DTLSHandler* dtlsHandler, RTC::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) = 0; virtual void onSRTPKeyMaterial(DTLSAgent* dtlsAgent, RTC::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) = 0;
virtual void onDTLSApplicationData(DTLSHandler* dtlsHandler, const MS_BYTE* data, size_t len) = 0; virtual void onDTLSApplicationData(DTLSAgent* dtlsAgent, const MS_BYTE* data, size_t len) = 0;
}; };
public: public:
...@@ -63,8 +63,8 @@ namespace RTC ...@@ -63,8 +63,8 @@ namespace RTC
static MS_BYTE sslReadBuffer[]; static MS_BYTE sslReadBuffer[];
public: public:
DTLSHandler(Listener* listener); DTLSAgent(Listener* listener);
virtual ~DTLSHandler(); virtual ~DTLSAgent();
void Run(RTC::DTLSRole role); void Run(RTC::DTLSRole role);
void SetRemoteFingerprint(RTC::FingerprintHash hash, std::string& fingerprint); void SetRemoteFingerprint(RTC::FingerprintHash hash, std::string& fingerprint);
...@@ -117,7 +117,7 @@ namespace RTC ...@@ -117,7 +117,7 @@ namespace RTC
/* Inline static methods. */ /* Inline static methods. */
inline inline
bool DTLSHandler::IsDTLS(const MS_BYTE* data, size_t len) bool DTLSAgent::IsDTLS(const MS_BYTE* data, size_t len)
{ {
return ( return (
// Minimum DTLS record length is 13 bytes. // Minimum DTLS record length is 13 bytes.
...@@ -130,13 +130,13 @@ namespace RTC ...@@ -130,13 +130,13 @@ namespace RTC
/* Inline instance methods. */ /* Inline instance methods. */
inline inline
bool DTLSHandler::IsConnected() bool DTLSAgent::IsConnected()
{ {
return this->isConnected; return this->isConnected;
} }
inline inline
bool DTLSHandler::IsRunning() bool DTLSAgent::IsRunning()
{ {
return this->isRunning; return this->isRunning;
} }
......
...@@ -25,33 +25,33 @@ namespace RTC ...@@ -25,33 +25,33 @@ namespace RTC
}; };
public: public:
ICEServer(Listener* listener); ICEServer(Listener* listener, std::string& usernameFragment, std::string password);
void ProcessSTUNMessage(RTC::STUNMessage* msg, RTC::TransportSource* source);
std::string& GetLocalUsername();
std::string& GetLocalPassword();
void Close(); void Close();
void ProcessSTUNMessage(RTC::STUNMessage* msg, RTC::TransportSource* source);
std::string& GetUsernameFragment();
std::string& GetPassword();
private: private:
// Passed by argument. // Passed by argument.
Listener* listener = nullptr; Listener* listener = nullptr;
// Others. // Others.
std::string localUsername; std::string usernameFragment;
std::string localPassword; std::string password;
}; };
/* Inline methods. */ /* Inline methods. */
inline inline
std::string& ICEServer::GetLocalUsername() std::string& ICEServer::GetUsernameFragment()
{ {
return this->localUsername; return this->usernameFragment;
} }
inline inline
std::string& ICEServer::GetLocalPassword() std::string& ICEServer::GetPassword()
{ {
return this->localPassword; return this->password;
} }
} }
......
#ifndef MS_RTC_ICE_TRANSPORT_H
#define MS_RTC_ICE_TRANSPORT_H
#include "common.h"
#include "RTC/IceCandidate.h"
#include "RTC/ICEServer.h"
#include "RTC/STUNMessage.h"
#include "RTC/TransportSource.h"
#include "RTC/UDPSocket.h"
#include "RTC/TCPServer.h"
#include "RTC/TCPConnection.h"
#include <string>
#include <vector>
#include <json/json.h>
namespace RTC
{
class ICETransport :
public RTC::ICEServer::Listener,
public RTC::UDPSocket::Listener,
public RTC::TCPServer::Listener,
public RTC::TCPConnection::Reader
{
public:
class Listener
{
public:
// TODO
};
public:
enum class IceComponent
{
RTP = 1,
RTCP = 2
};
public:
typedef struct IceParameters
{
std::string usernameFragment;
std::string password;
} IceParameters;
public:
ICETransport(Listener* listener, Json::Value& data);
virtual ~ICETransport();
void Close();
Json::Value toJson();
IceComponent GetIceComponent(); // TODO: needed?
IceParameters* GetIceLocalParameters(); // TODO: needed?
std::vector<IceCandidate>& GetIceLocalCandidates();
// ICETransport* CreateAssociatedGatherer(); // TODO: let's see.
/* Pure virtual methods inherited from RTC::ICEServer::Listener. */
public:
virtual void onOutgoingSTUNMessage(RTC::ICEServer* iceServer, RTC::STUNMessage* msg, RTC::TransportSource* source) override;
virtual void onICEValidPair(RTC::ICEServer* iceServer, RTC::TransportSource* source, bool has_use_candidate) override;
/* Private methods to unify UDP and TCP behavior. */
private:
void onSTUNDataRecv(RTC::TransportSource* source, const MS_BYTE* data, size_t len);
void onDTLSDataRecv(RTC::TransportSource* source, const MS_BYTE* data, size_t len);
void onRTPDataRecv(RTC::TransportSource* source, const MS_BYTE* data, size_t len);
void onRTCPDataRecv(RTC::TransportSource* source, const MS_BYTE* data, size_t len);
/* Pure virtual methods inherited from RTC::UDPSocket::Listener. */
public:
virtual void onSTUNDataRecv(RTC::UDPSocket *socket, const MS_BYTE* data, size_t len, const struct sockaddr* remote_addr) override;
virtual void onDTLSDataRecv(RTC::UDPSocket *socket, const MS_BYTE* data, size_t len, const struct sockaddr* remote_addr) override;
virtual void onRTPDataRecv(RTC::UDPSocket *socket, const MS_BYTE* data, size_t len, const struct sockaddr* remote_addr) override;
virtual void onRTCPDataRecv(RTC::UDPSocket *socket, const MS_BYTE* data, size_t len, const struct sockaddr* remote_addr) override;
/* Pure virtual methods inherited from RTC::TCPServer::Listener. */
public:
virtual void onRTCTCPConnectionClosed(RTC::TCPServer* tcpServer, RTC::TCPConnection* connection, bool is_closed_by_peer) override;
/* Pure virtual methods inherited from RTC::TCPConnection::Listener. */
public:
virtual void onSTUNDataRecv(RTC::TCPConnection *connection, const MS_BYTE* data, size_t len) override;
virtual void onDTLSDataRecv(RTC::TCPConnection *connection, const MS_BYTE* data, size_t len) override;
virtual void onRTPDataRecv(RTC::TCPConnection *connection, const MS_BYTE* data, size_t len) override;
virtual void onRTCPDataRecv(RTC::TCPConnection *connection, const MS_BYTE* data, size_t len) override;
private:
// Passed by argument.
Listener* listener = nullptr;
// Allocated by this.
RTC::ICEServer* iceServer = nullptr;
std::vector<RTC::UDPSocket*> udpSockets;
std::vector<RTC::TCPServer*> tcpServers;
// Others.
IceComponent iceComponent = IceComponent::RTP;
IceParameters iceLocalParameters;
std::vector<IceCandidate> iceLocalCandidates;
};
/* Inline methods. */
inline
ICETransport::IceParameters* ICETransport::GetIceLocalParameters()
{
return &(this->iceLocalParameters);
}
}
#endif
#ifndef MS_RTC_ICE_CANDIDATE_H
#define MS_RTC_ICE_CANDIDATE_H
#include "common.h"
#include "RTC/UDPSocket.h"
#include "RTC/TCPServer.h"
#include <string>
#include <json/json.h>
namespace RTC
{
class IceCandidate
{
public:
enum class IceComponent
{
RTP = 1,
RTCP = 2
};
public:
enum class IceProtocol
{
UDP = 1,
TCP
};
public:
enum class IceCandidateType
{
HOST = 1
};
public:
enum class IceTcpCandidateType
{
PASSIVE = 1
};
public:
IceCandidate(RTC::UDPSocket* udpSocket);
IceCandidate(RTC::TCPServer* tcpServer);
Json::Value toJson();
private:
// Others.
std::string foundation;
unsigned long priority;
std::string ip;
IceProtocol protocol;
MS_PORT port;
IceCandidateType type;
IceTcpCandidateType tcpType;
};
/* Inline methods. */
inline
IceCandidate::IceCandidate(RTC::UDPSocket* udpSocket) :
foundation("udpcandidate"),
ip(udpSocket->GetLocalIP()),
protocol(IceProtocol::UDP),
port(udpSocket->GetLocalPort()),
type(IceCandidateType::HOST)
{}
inline
IceCandidate::IceCandidate(RTC::TCPServer* tcpServer) :
foundation("tcpcandidate"),
ip(tcpServer->GetLocalIP()),
protocol(IceProtocol::TCP),
port(tcpServer->GetLocalPort()),
type(IceCandidateType::HOST),
tcpType(IceTcpCandidateType::PASSIVE)
{}
inline
Json::Value IceCandidate::toJson()
{
Json::Value data;
data["foundation"] = this->foundation;
data["priority"] = (unsigned int)this->priority;
data["ip"] = this->ip;
data["port"] = this->port;
if (this->protocol == IceProtocol::UDP)
data["protocol"] = "udp";
else
data["protocol"] = "tcp";
if (this->type == IceCandidateType::HOST)
data["type"] = "host";
if (this->protocol == IceProtocol::TCP)
{
if (this->tcpType == IceTcpCandidateType::PASSIVE)
data["tcpType"] = "passive";
}
return data;
}
}
#endif
#ifndef MS_RTC_PEER_H #ifndef MS_RTC_PEER_H
#define MS_RTC_PEER_H #define MS_RTC_PEER_H
#include "RTC/Transport.h" #include "RTC/ICETransport.h"
#include "RTC/RTPPacket.h" #include "RTC/RTPPacket.h"
#include "RTC/RTCPPacket.h" #include "RTC/RTCPPacket.h"
#include "Channel/Request.h"
#include <string> #include <string>
#include <json/json.h> #include <json/json.h>
namespace RTC namespace RTC
{ {
class Peer : public RTC::Transport::Listener class Peer :
public RTC::ICETransport::Listener
{ {
public: public:
class Listener class Listener
{ {
public: public:
virtual void onRTPPacket(RTC::Peer* peer, RTC::RTPPacket* packet) = 0; virtual void onPeerClosed(RTC::Peer* peer) = 0;
virtual void onRTCPPacket(RTC::Peer* peer, RTC::RTCPPacket* packet) = 0;
}; };
public: public:
Peer(Listener* listener, std::string& peerId, Json::Value& data); Peer(Listener* listener, std::string& peerId, Json::Value& data);
virtual ~Peer(); virtual ~Peer();
void SendRTPPacket(RTC::RTPPacket* packet);
void SendRTCPPacket(RTC::RTCPPacket* packet);
Json::Value Dump();
void Close(); void Close();
Json::Value Dump();
void HandleRequest(Channel::Request* request);
/* Pure virtual methods inherited from RTC::Transport::Listener. */ /* Pure virtual methods inherited from RTC::ICETransport::Listener. */
public: public:
virtual void onRTPPacket(RTC::Transport* transport, RTC::RTPPacket* packet) override; // TODO
virtual void onRTCPPacket(RTC::Transport* transport, RTC::RTCPPacket* packet) override;
public: public:
// Passed by argument. // Passed by argument.
...@@ -41,7 +40,8 @@ namespace RTC ...@@ -41,7 +40,8 @@ namespace RTC
// Passed by argument. // Passed by argument.
Listener* listener = nullptr; Listener* listener = nullptr;
// Others. // Others.
RTC::Transport* transport = nullptr; // TODO: it must be a unordered_map.
RTC::ICETransport* iceTransport = nullptr;
}; };
} }
......
...@@ -2,8 +2,6 @@ ...@@ -2,8 +2,6 @@
#define MS_RTC_ROOM_H #define MS_RTC_ROOM_H
#include "RTC/Peer.h" #include "RTC/Peer.h"
#include "RTC/RTPPacket.h"
#include "RTC/RTCPPacket.h"
#include "Channel/Request.h" #include "Channel/Request.h"
#include <string> #include <string>
#include <unordered_map> #include <unordered_map>
...@@ -17,19 +15,16 @@ namespace RTC ...@@ -17,19 +15,16 @@ namespace RTC
Room(unsigned int roomId, Json::Value& data); Room(unsigned int roomId, Json::Value& data);
virtual ~Room(); virtual ~Room();
void HandleCreatePeerRequest(Channel::Request* request);
void HandleClosePeerRequest(Channel::Request* request);
void HandleDumpPeerRequest(Channel::Request* request);
Json::Value Dump();
void Close(); void Close();
Json::Value Dump();
void HandleRequest(Channel::Request* request);
private: private:
RTC::Peer* GetPeerFromRequest(Channel::Request* request, std::string* peerId); RTC::Peer* GetPeerFromRequest(Channel::Request* request, std::string* peerId = nullptr);
/* Pure virtual methods inherited from RTC::Peer::Listener. */ /* Pure virtual methods inherited from RTC::Peer::Listener. */
public: public:
virtual void onRTPPacket(RTC::Peer* peer, RTC::RTPPacket* packet) override; virtual void onPeerClosed(RTC::Peer* peer) override;
virtual void onRTCPPacket(RTC::Peer* peer, RTC::RTCPPacket* packet) override;
public: public:
// Passed by argument. // Passed by argument.
......
#ifndef MS_RTC_STREAM_H
#define MS_RTC_STREAM_H
#include "common.h"
#include "RTC/RTPPacket.h"
#include "RTC/RTCPPacket.h"
#include <unordered_set>
namespace RTC
{
class Stream
{
public:
Stream();
~Stream();
void Close();
private:
MS_4BYTES ssrc = 0;
std::unordered_set<MS_BYTE> payloads;
};
}
#endif
...@@ -21,8 +21,8 @@ namespace RTC ...@@ -21,8 +21,8 @@ namespace RTC
public: public:
static void ClassInit(); static void ClassInit();
static RTC::TCPServer* New(int address_family); static RTC::TCPServer* Factory(Listener* listener, RTC::TCPConnection::Reader* reader, int address_family);
static void NewPair(int address_family, RTC::TCPServer* servers[]); static void PairFactory(Listener* listener, RTC::TCPConnection::Reader* reader, int address_family, RTC::TCPServer* servers[]);
private: private:
static void RandomizePort(int address_family, uv_tcp_t* uvHandles[], bool pair); static void RandomizePort(int address_family, uv_tcp_t* uvHandles[], bool pair);
...@@ -37,9 +37,7 @@ namespace RTC ...@@ -37,9 +37,7 @@ namespace RTC
static AvailablePorts availableIPv6Ports; static AvailablePorts availableIPv6Ports;
public: public:
TCPServer(uv_tcp_t* uvHandle); TCPServer(Listener* listener, RTC::TCPConnection::Reader* reader, uv_tcp_t* uvHandle);
void SetListeners(Listener* listener, RTC::TCPConnection::Reader* reader);
/* Pure virtual methods inherited from ::TCPServer. */ /* Pure virtual methods inherited from ::TCPServer. */
public: public:
...@@ -53,15 +51,6 @@ namespace RTC ...@@ -53,15 +51,6 @@ namespace RTC
Listener* listener = nullptr; Listener* listener = nullptr;
RTC::TCPConnection::Reader* reader = nullptr; RTC::TCPConnection::Reader* reader = nullptr;
}; };
/* Inline methods. */
inline
void TCPServer::SetListeners(Listener* listener, RTC::TCPConnection::Reader* reader)
{
this->listener = listener;
this->reader = reader;
}
} }
#endif #endif
差异被折叠。
...@@ -23,8 +23,8 @@ namespace RTC ...@@ -23,8 +23,8 @@ namespace RTC
public: public:
static void ClassInit(); static void ClassInit();
static RTC::UDPSocket* New(int address_family); static RTC::UDPSocket* Factory(Listener* listener, int address_family);
static void NewPair(int address_family, RTC::UDPSocket* sockets[]); static void PairFactory(Listener* listener, int address_family, RTC::UDPSocket* sockets[]);
private: private:
static void RandomizePort(int address_family, uv_udp_t* uvHandles[], bool pair); static void RandomizePort(int address_family, uv_udp_t* uvHandles[], bool pair);
...@@ -39,9 +39,7 @@ namespace RTC ...@@ -39,9 +39,7 @@ namespace RTC
static AvailablePorts availableIPv6Ports; static AvailablePorts availableIPv6Ports;
public: public:
UDPSocket(uv_udp_t* uvHandle); UDPSocket(Listener* listener, uv_udp_t* uvHandle);
void SetListener(Listener* listener);
/* Pure virtual methods inherited from ::UDPSocket. */ /* Pure virtual methods inherited from ::UDPSocket. */
public: public:
...@@ -52,14 +50,6 @@ namespace RTC ...@@ -52,14 +50,6 @@ namespace RTC
// Passed by argument. // Passed by argument.
Listener* listener = nullptr; Listener* listener = nullptr;
}; };
/* Inline methods. */
inline
void UDPSocket::SetListener(Listener* listener)
{
this->listener = listener;
}
} }
#endif #endif
...@@ -21,7 +21,10 @@ namespace Channel ...@@ -21,7 +21,10 @@ namespace Channel
{ "dumpRoom", Request::MethodId::dumpRoom }, { "dumpRoom", Request::MethodId::dumpRoom },
{ "createPeer", Request::MethodId::createPeer }, { "createPeer", Request::MethodId::createPeer },
{ "closePeer", Request::MethodId::closePeer }, { "closePeer", Request::MethodId::closePeer },
{ "dumpPeer", Request::MethodId::dumpPeer } { "dumpPeer", Request::MethodId::dumpPeer },
{ "createTransport", Request::MethodId::createTransport },
{ "closeTransport", Request::MethodId::closeTransport },
{ "dumpTransport", Request::MethodId::dumpTransport }
}; };
/* Instance methods. */ /* Instance methods. */
...@@ -32,21 +35,27 @@ namespace Channel ...@@ -32,21 +35,27 @@ namespace Channel
MS_TRACE(); MS_TRACE();
if (json["id"].isUInt()) if (json["id"].isUInt())
id = json["id"].asUInt(); this->id = json["id"].asUInt();
else else
MS_THROW_ERROR("json has no numeric .id field"); MS_THROW_ERROR("json has no numeric .id field");
if (json["method"].isString()) if (json["method"].isString())
method = json["method"].asString(); this->method = json["method"].asString();
else else
MS_THROW_ERROR("json has no string .method field"); MS_THROW_ERROR("json has no string .method field");
auto it = Request::string2MethodId.find(method); auto it = Request::string2MethodId.find(method);
if (it != Request::string2MethodId.end()) if (it != Request::string2MethodId.end())
{
this->methodId = it->second; this->methodId = it->second;
}
else else
MS_THROW_ERROR("unknwon .method '%s'", method.c_str()); {
Reject(405, "method not allowed");
MS_THROW_ERROR("unknown .method '%s'", method.c_str());
}
if (json["data"].isObject()) if (json["data"].isObject())
this->data = json["data"]; this->data = json["data"];
......
...@@ -40,7 +40,7 @@ Loop::~Loop() ...@@ -40,7 +40,7 @@ Loop::~Loop()
MS_TRACE(); MS_TRACE();
} }
RTC::Room* Loop::GetRoomFromRequest(Channel::Request* request, unsigned int* roomId = nullptr) RTC::Room* Loop::GetRoomFromRequest(Channel::Request* request, unsigned int* roomId)
{ {
MS_TRACE(); MS_TRACE();
...@@ -133,12 +133,12 @@ void Loop::onChannelRequest(Channel::UnixStreamSocket* channel, Channel::Request ...@@ -133,12 +133,12 @@ void Loop::onChannelRequest(Channel::UnixStreamSocket* channel, Channel::Request
{ {
MS_TRACE(); MS_TRACE();
MS_DEBUG("'%s' request", request->method.c_str());
switch (request->methodId) switch (request->methodId)
{ {
case Channel::Request::MethodId::dumpWorker: case Channel::Request::MethodId::dumpWorker:
{ {
MS_DEBUG("'dumpWorker' method");
Json::Value json(Json::objectValue); Json::Value json(Json::objectValue);
Json::Value jsonWorker(Json::objectValue); Json::Value jsonWorker(Json::objectValue);
Json::Value jsonRooms(Json::objectValue); Json::Value jsonRooms(Json::objectValue);
...@@ -160,16 +160,12 @@ void Loop::onChannelRequest(Channel::UnixStreamSocket* channel, Channel::Request ...@@ -160,16 +160,12 @@ void Loop::onChannelRequest(Channel::UnixStreamSocket* channel, Channel::Request
case Channel::Request::MethodId::updateSettings: case Channel::Request::MethodId::updateSettings:
{ {
MS_DEBUG("'updateSettings' method");
Settings::HandleUpdateRequest(request); Settings::HandleUpdateRequest(request);
break; break;
} }
case Channel::Request::MethodId::createRoom: case Channel::Request::MethodId::createRoom:
{ {
MS_DEBUG("'createRoom' method");
RTC::Room* room; RTC::Room* room;
unsigned int roomId; unsigned int roomId;
...@@ -211,8 +207,6 @@ void Loop::onChannelRequest(Channel::UnixStreamSocket* channel, Channel::Request ...@@ -211,8 +207,6 @@ void Loop::onChannelRequest(Channel::UnixStreamSocket* channel, Channel::Request
case Channel::Request::MethodId::closeRoom: case Channel::Request::MethodId::closeRoom:
{ {
MS_DEBUG("'closeRoom' method");
RTC::Room* room; RTC::Room* room;
unsigned int roomId; unsigned int roomId;
...@@ -247,14 +241,11 @@ void Loop::onChannelRequest(Channel::UnixStreamSocket* channel, Channel::Request ...@@ -247,14 +241,11 @@ void Loop::onChannelRequest(Channel::UnixStreamSocket* channel, Channel::Request
case Channel::Request::MethodId::dumpRoom: case Channel::Request::MethodId::dumpRoom:
{ {
MS_DEBUG("'dumpRoom' method");
RTC::Room* room; RTC::Room* room;
unsigned int roomId;
try try
{ {
room = GetRoomFromRequest(request, &roomId); room = GetRoomFromRequest(request);
} }
catch (const MediaSoupError &error) catch (const MediaSoupError &error)
{ {
...@@ -278,67 +269,12 @@ void Loop::onChannelRequest(Channel::UnixStreamSocket* channel, Channel::Request ...@@ -278,67 +269,12 @@ void Loop::onChannelRequest(Channel::UnixStreamSocket* channel, Channel::Request
} }
case Channel::Request::MethodId::createPeer: case Channel::Request::MethodId::createPeer:
{
MS_DEBUG("'createPeer' method");
RTC::Room* room;
try
{
room = GetRoomFromRequest(request);
}
catch (const MediaSoupError &error)
{
request->Reject(500, error.what());
return;
}
if (!room)
{
MS_ERROR("Room does not exist");
request->Reject(500, "Room does not exist");
return;
}
room->HandleCreatePeerRequest(request);
break;
}
case Channel::Request::MethodId::closePeer: case Channel::Request::MethodId::closePeer:
{
MS_DEBUG("'closePeer' method");
RTC::Room* room;
try
{
room = GetRoomFromRequest(request);
}
catch (const MediaSoupError &error)
{
request->Reject(500, error.what());
return;
}
if (!room)
{
MS_ERROR("Room does not exist");
request->Reject(500, "Room does not exist");
return;
}
room->HandleClosePeerRequest(request);
break;
}
case Channel::Request::MethodId::dumpPeer: case Channel::Request::MethodId::dumpPeer:
case Channel::Request::MethodId::createTransport:
case Channel::Request::MethodId::closeTransport:
case Channel::Request::MethodId::dumpTransport:
{ {
MS_DEBUG("'dumpPeer' method");
RTC::Room* room; RTC::Room* room;
try try
...@@ -359,14 +295,14 @@ void Loop::onChannelRequest(Channel::UnixStreamSocket* channel, Channel::Request ...@@ -359,14 +295,14 @@ void Loop::onChannelRequest(Channel::UnixStreamSocket* channel, Channel::Request
return; return;
} }
room->HandleDumpPeerRequest(request); room->HandleRequest(request);
break; break;
} }
default: default:
{ {
MS_ABORT("unexpected methodId"); MS_ABORT("unknown method");
} }
} }
} }
......
#define MS_CLASS "RTC::ICEServer" #define MS_CLASS "RTC::ICEServer"
#include "RTC/ICEServer.h" #include "RTC/ICEServer.h"
#include "Utils.h"
#include "Logger.h" #include "Logger.h"
namespace RTC namespace RTC
{ {
/* Instance methods. */ /* Instance methods. */
ICEServer::ICEServer(Listener* listener) : ICEServer::ICEServer(Listener* listener, std::string& usernameFragment, std::string password) :
listener(listener) listener(listener),
usernameFragment(usernameFragment),
password(password)
{ {
MS_TRACE(); MS_TRACE();
char local_username[16]; MS_DEBUG("[usernameFragment:%s, password:%s]", this->usernameFragment.c_str(), this->password.c_str());
char local_password[16]; }
this->localUsername = std::string(Utils::Crypto::GetRandomHexString(local_username, 16), 16);
this->localPassword = std::string(Utils::Crypto::GetRandomHexString(local_password, 32), 32);
MS_DEBUG("local username: %s | local password: %s", this->localUsername.c_str(), this->localPassword.c_str()); void ICEServer::Close()
{
MS_TRACE();
// TODO: TMP for testing delete this;
this->localUsername = "7D5B3AA55DC2CE83";
this->localPassword = "75C96DDDFC38D194FEDF75986CF962A2D56F3B65F1F7";
} }
void ICEServer::ProcessSTUNMessage(RTC::STUNMessage* msg, RTC::TransportSource* source) void ICEServer::ProcessSTUNMessage(RTC::STUNMessage* msg, RTC::TransportSource* source)
...@@ -91,7 +89,7 @@ namespace RTC ...@@ -91,7 +89,7 @@ namespace RTC
} }
// Check authentication. // Check authentication.
switch (msg->CheckAuthentication(this->localUsername, this->localPassword)) switch (msg->CheckAuthentication(this->usernameFragment, this->password))
{ {
case RTC::STUNMessage::Authentication::OK: case RTC::STUNMessage::Authentication::OK:
break; break;
...@@ -146,7 +144,7 @@ namespace RTC ...@@ -146,7 +144,7 @@ namespace RTC
response->SetXorMappedAddress(source->GetRemoteAddress()); response->SetXorMappedAddress(source->GetRemoteAddress());
// Authenticate the response. // Authenticate the response.
response->Authenticate(this->localPassword); response->Authenticate(this->password);
// Send back. // Send back.
response->Serialize(); response->Serialize();
...@@ -178,11 +176,4 @@ namespace RTC ...@@ -178,11 +176,4 @@ namespace RTC
} }
} }
} }
void ICEServer::Close()
{
MS_TRACE();
delete this;
}
} }
#define MS_CLASS "RTC::Transport"
#include "RTC/ICETransport.h"
#include "Settings.h"
#include "Utils.h"
#include "MediaSoupError.h"
#include "Logger.h"
namespace RTC
{
/* Instance methods. */
ICETransport::ICETransport(Listener* listener, Json::Value& data) :
listener(listener)
{
MS_TRACE();
// Check options.
static const Json::StaticString udp("udp");
static const Json::StaticString tcp("tcp");
bool option_udp = true;
bool option_tcp = true;
if (data[udp].isBool())
option_udp = data[udp].asBool();
if (data[tcp].isBool())
option_tcp = data[tcp].asBool();
MS_DEBUG("[udp:%s, tcp:%s]",
option_udp ? "true" : "false", option_tcp ? "true" : "false");
// Create an ICEServer.
char _username_fragment[16];
char _password[16];
std::string usernameFragment = std::string(Utils::Crypto::GetRandomHexString(_username_fragment, 16), 16);
std::string password = std::string(Utils::Crypto::GetRandomHexString(_password, 32), 32);
this->iceServer = new RTC::ICEServer(this, usernameFragment, password);
// Fill IceParameters.
this->iceLocalParameters.usernameFragment = this->iceServer->GetUsernameFragment();
this->iceLocalParameters.password = this->iceServer->GetPassword();
// Open a IPv4 UDP socket.
if (option_udp && Settings::configuration.hasIPv4)
{
try
{
RTC::UDPSocket* udpSocket = RTC::UDPSocket::Factory(this, AF_INET);
RTC::IceCandidate iceCandidate(udpSocket);
this->udpSockets.push_back(udpSocket);
this->iceLocalCandidates.push_back(iceCandidate);
}
catch (const MediaSoupError &error)
{
MS_ERROR("error adding IPv4 UDP socket: %s", error.what());
}
}
// Open a IPv6 UDP socket.
if (option_udp && Settings::configuration.hasIPv6)
{
try
{
RTC::UDPSocket* udpSocket = RTC::UDPSocket::Factory(this, AF_INET6);
RTC::IceCandidate iceCandidate(udpSocket);
this->udpSockets.push_back(udpSocket);
this->iceLocalCandidates.push_back(iceCandidate);
}
catch (const MediaSoupError &error)
{
MS_ERROR("error adding IPv6 UDP socket: %s", error.what());
}
}
// Open a IPv4 TCP server.
if (option_tcp && Settings::configuration.hasIPv4)
{
try
{
RTC::TCPServer* tcpServer = RTC::TCPServer::Factory(this, this, AF_INET);
RTC::IceCandidate iceCandidate(tcpServer);
this->tcpServers.push_back(tcpServer);
this->iceLocalCandidates.push_back(iceCandidate);
}
catch (const MediaSoupError &error)
{
MS_ERROR("error adding IPv4 TCP server: %s", error.what());
}
}
// Open a IPv6 TCP server.
if (option_tcp && Settings::configuration.hasIPv6)
{
try
{
RTC::TCPServer* tcpServer = RTC::TCPServer::Factory(this, this, AF_INET6);
RTC::IceCandidate iceCandidate(tcpServer);
this->tcpServers.push_back(tcpServer);
this->iceLocalCandidates.push_back(iceCandidate);
}
catch (const MediaSoupError &error)
{
MS_ERROR("error adding IPv6 TCP server: %s", error.what());
}
}
}
ICETransport::~ICETransport()
{
MS_TRACE();
}
void ICETransport::Close()
{
MS_TRACE();
if (this->iceServer)
this->iceServer->Close();
// if (this->dtlsAgent)
// this->dtlsAgent->Close();
// if (this->srtpRecvSession)
// this->srtpRecvSession->Close();
// if (this->srtpSendSession)
// this->srtpSendSession->Close();
for (auto socket : this->udpSockets)
socket->Close();
for (auto server : this->tcpServers)
server->Close();
delete this;
}
Json::Value ICETransport::toJson()
{
MS_TRACE();
static const Json::StaticString iceLocalParameters("iceLocalParameters");
static const Json::StaticString iceCandidates("iceCandidates");
Json::Value data;
if (this->iceComponent == ICETransport::IceComponent::RTP)
data["iceComponent"] = "RTP";
else
data["iceComponent"] = "RTCP";
data[iceLocalParameters]["usernameFragment"] = this->iceLocalParameters.usernameFragment;
data[iceLocalParameters]["password"] = this->iceLocalParameters.password;
data[iceCandidates] = Json::arrayValue;
for (auto iceCandidate : this->iceLocalCandidates)
{
data[iceCandidates].append(iceCandidate.toJson());
}
return data;
}
void ICETransport::onOutgoingSTUNMessage(RTC::ICEServer* iceServer, RTC::STUNMessage* msg, RTC::TransportSource* source)
{
MS_TRACE();
// TODO
}
void ICETransport::onICEValidPair(RTC::ICEServer* iceServer, RTC::TransportSource* source, bool has_use_candidate)
{
MS_TRACE();
// TODO
}
inline
void ICETransport::onSTUNDataRecv(RTC::TransportSource* source, const MS_BYTE* data, size_t len)
{
MS_TRACE();
// TODO
}
inline
void ICETransport::onDTLSDataRecv(RTC::TransportSource* source, const MS_BYTE* data, size_t len)
{
MS_TRACE();
// TODO
}
inline
void ICETransport::onRTPDataRecv(RTC::TransportSource* source, const MS_BYTE* data, size_t len)
{
MS_TRACE();
// TODO
}
inline
void ICETransport::onRTCPDataRecv(RTC::TransportSource* source, const MS_BYTE* data, size_t len)
{
MS_TRACE();
// TODO
}
void ICETransport::onSTUNDataRecv(RTC::UDPSocket *socket, const MS_BYTE* data, size_t len, const struct sockaddr* remote_addr)
{
MS_TRACE();
RTC::TransportSource source(socket, remote_addr);
onSTUNDataRecv(&source, data, len);
}
void ICETransport::onDTLSDataRecv(RTC::UDPSocket *socket, const MS_BYTE* data, size_t len, const struct sockaddr* remote_addr)
{
MS_TRACE();
RTC::TransportSource source(socket, remote_addr);
onDTLSDataRecv(&source, data, len);
}
void ICETransport::onRTPDataRecv(RTC::UDPSocket *socket, const MS_BYTE* data, size_t len, const struct sockaddr* remote_addr)
{
MS_TRACE();
RTC::TransportSource source(socket, remote_addr);
onRTPDataRecv(&source, data, len);
}
void ICETransport::onRTCPDataRecv(RTC::UDPSocket *socket, const MS_BYTE* data, size_t len, const struct sockaddr* remote_addr)
{
MS_TRACE();
RTC::TransportSource source(socket, remote_addr);
onRTCPDataRecv(&source, data, len);
}
void ICETransport::onRTCTCPConnectionClosed(RTC::TCPServer* tcpServer, RTC::TCPConnection* connection, bool is_closed_by_peer)
{
MS_TRACE();
// RTC::TransportSource source(connection);
// if (RemoveSource(&source))
// {
// MS_DEBUG("valid source %s:", is_closed_by_peer ? "closed by peer" : "locally closed");
// source.Dump();
// }
}
void ICETransport::onSTUNDataRecv(RTC::TCPConnection *connection, const MS_BYTE* data, size_t len)
{
MS_TRACE();
RTC::TransportSource source(connection);
onSTUNDataRecv(&source, data, len);
}
void ICETransport::onDTLSDataRecv(RTC::TCPConnection *connection, const MS_BYTE* data, size_t len)
{
MS_TRACE();
RTC::TransportSource source(connection);
onDTLSDataRecv(&source, data, len);
}
void ICETransport::onRTPDataRecv(RTC::TCPConnection *connection, const MS_BYTE* data, size_t len)
{
MS_TRACE();
RTC::TransportSource source(connection);
onRTPDataRecv(&source, data, len);
}
void ICETransport::onRTCPDataRecv(RTC::TCPConnection *connection, const MS_BYTE* data, size_t len)
{
MS_TRACE();
RTC::TransportSource source(connection);
onRTCPDataRecv(&source, data, len);
}
}
...@@ -13,8 +13,8 @@ namespace RTC ...@@ -13,8 +13,8 @@ namespace RTC
/* Instance methods. */ /* Instance methods. */
Peer::Peer(Listener* listener, std::string& peerId, Json::Value& data) : Peer::Peer(Listener* listener, std::string& peerId, Json::Value& data) :
listener(listener), peerId(peerId),
peerId(peerId) listener(listener)
{ {
MS_TRACE(); MS_TRACE();
...@@ -26,24 +26,17 @@ namespace RTC ...@@ -26,24 +26,17 @@ namespace RTC
MS_TRACE(); MS_TRACE();
} }
void Peer::SendRTPPacket(RTC::RTPPacket* packet) void Peer::Close()
{ {
MS_TRACE(); MS_TRACE();
// TMP // TMP
if (this->iceTransport)
this->iceTransport->Close();
if (this->transport->IsReadyForMedia()) this->listener->onPeerClosed(this);
this->transport->SendRTPPacket(packet);
}
void Peer::SendRTCPPacket(RTC::RTCPPacket* packet)
{
MS_TRACE();
// TMP
if (this->transport->IsReadyForMedia()) delete this;
this->transport->SendRTCPPacket(packet);
} }
Json::Value Peer::Dump() Json::Value Peer::Dump()
...@@ -52,42 +45,50 @@ namespace RTC ...@@ -52,42 +45,50 @@ namespace RTC
Json::Value json(Json::objectValue); Json::Value json(Json::objectValue);
// TODO: TMP // TODO
json["foo"] = "bar";
return json; return json;
} }
void Peer::Close() void Peer::HandleRequest(Channel::Request* request)
{ {
MS_TRACE(); MS_TRACE();
// TMP switch (request->methodId)
if (this->transport) {
this->transport->Close(); case Channel::Request::MethodId::createTransport:
{
delete this; try
{
this->iceTransport = new RTC::ICETransport(this, request->data);
} }
catch (const MediaSoupError &error)
void Peer::onRTPPacket(RTC::Transport* transport, RTC::RTPPacket* packet)
{ {
MS_TRACE(); request->Reject(500, error.what());
return;
}
// MS_DEBUG("RTP packet received"); // TODO: TMP auto data = this->iceTransport->toJson();
// TMP request->Accept(data);
this->listener->onRTPPacket(this, packet); break;
} }
void Peer::onRTCPPacket(RTC::Transport* transport, RTC::RTCPPacket* packet) case Channel::Request::MethodId::closeTransport:
case Channel::Request::MethodId::dumpTransport:
{ {
MS_TRACE(); // TODO
// MS_DEBUG("RTCP packet received"); // TODO: TMP request->Accept();
// TMP break;
}
this->listener->onRTCPPacket(this, packet); default:
{
MS_ABORT("unknown method");
}
}
} }
} }
...@@ -4,13 +4,6 @@ ...@@ -4,13 +4,6 @@
#include "MediaSoupError.h" #include "MediaSoupError.h"
#include "Logger.h" #include "Logger.h"
// TMP
#define NUM_PEERS 4
#define PAYLOAD_TYPE_AUDIO 111 // OPUS
#define PAYLOAD_TYPE_VIDEO 100 // VP8
#define SSRC_AUDIO_BASE 100000
#define SSRC_VIDEO_BASE 200000
namespace RTC namespace RTC
{ {
/* Instance methods. */ /* Instance methods. */
...@@ -20,7 +13,7 @@ namespace RTC ...@@ -20,7 +13,7 @@ namespace RTC
{ {
MS_TRACE(); MS_TRACE();
// TODO: do something wit data and throw if incorrect. // TODO: do something with data and throw if incorrect.
} }
Room::~Room() Room::~Room()
...@@ -28,10 +21,52 @@ namespace RTC ...@@ -28,10 +21,52 @@ namespace RTC
MS_TRACE(); MS_TRACE();
} }
void Room::HandleCreatePeerRequest(Channel::Request* request) void Room::Close()
{
MS_TRACE();
// Close all the Peers.
// NOTE: Upon Peer closure the onPeerClosed() method is called which
// removes it from the map, so this is the safe way to iterate the mao
// and remove elements.
for (auto it = this->peers.begin(); it != this->peers.end();)
{
auto peer = it->second;
it = this->peers.erase(it);
peer->Close();
}
delete this;
}
Json::Value Room::Dump()
{ {
MS_TRACE(); MS_TRACE();
Json::Value json(Json::objectValue);
Json::Value jsonPeers(Json::objectValue);
for (auto& kv : this->peers)
{
auto peer = kv.second;
jsonPeers[peer->peerId] = peer->Dump();
}
json["peers"] = jsonPeers;
return json;
}
void Room::HandleRequest(Channel::Request* request)
{
MS_TRACE();
switch (request->methodId)
{
case Channel::Request::MethodId::createPeer:
{
RTC::Peer* peer; RTC::Peer* peer;
std::string peerId; std::string peerId;
...@@ -67,12 +102,12 @@ namespace RTC ...@@ -67,12 +102,12 @@ namespace RTC
MS_DEBUG("Peer created [peerId:%s]", peerId.c_str()); MS_DEBUG("Peer created [peerId:%s]", peerId.c_str());
request->Accept(); request->Accept();
break;
} }
void Room::HandleClosePeerRequest(Channel::Request* request) case Channel::Request::MethodId::closePeer:
{ {
MS_TRACE();
RTC::Peer* peer; RTC::Peer* peer;
std::string peerId; std::string peerId;
...@@ -95,24 +130,18 @@ namespace RTC ...@@ -95,24 +130,18 @@ namespace RTC
} }
peer->Close(); peer->Close();
// TODO: Instead of this, the Peer should fire an onPeerClosed() here.
this->peers.erase(peerId);
MS_DEBUG("Peer closed [peerId:%s]", peerId.c_str());
request->Accept(); request->Accept();
break;
} }
void Room::HandleDumpPeerRequest(Channel::Request* request) case Channel::Request::MethodId::dumpPeer:
{ {
MS_TRACE();
RTC::Peer* peer; RTC::Peer* peer;
std::string peerId;
try try
{ {
peer = GetPeerFromRequest(request, &peerId); peer = GetPeerFromRequest(request);
} }
catch (const MediaSoupError &error) catch (const MediaSoupError &error)
{ {
...@@ -131,43 +160,47 @@ namespace RTC ...@@ -131,43 +160,47 @@ namespace RTC
Json::Value jsonPeer = peer->Dump(); Json::Value jsonPeer = peer->Dump();
request->Accept(jsonPeer); request->Accept(jsonPeer);
break;
} }
Json::Value Room::Dump() case Channel::Request::MethodId::createTransport:
case Channel::Request::MethodId::closeTransport:
case Channel::Request::MethodId::dumpTransport:
{ {
MS_TRACE(); RTC::Peer* peer;
Json::Value json(Json::objectValue); try
Json::Value jsonPeers(Json::objectValue); {
peer = GetPeerFromRequest(request);
}
catch (const MediaSoupError &error)
{
request->Reject(500, error.what());
return;
}
for (auto& kv : this->peers) if (!peer)
{ {
auto peer = kv.second; MS_ERROR("Peer does not exist");
jsonPeers[peer->peerId] = peer->Dump(); request->Reject(500, "Peer does not exist");
return;
} }
json["peers"] = jsonPeers; peer->HandleRequest(request);
return json; break;
} }
void Room::Close() default:
{ {
MS_TRACE(); MS_ABORT("unknown method");
}
// Close all the Peers.
for (auto& kv : this->peers)
{
RTC::Peer* peer = kv.second;
peer->Close();
} }
delete this;
} }
RTC::Peer* Room::GetPeerFromRequest(Channel::Request* request, std::string* peerId = nullptr) RTC::Peer* Room::GetPeerFromRequest(Channel::Request* request, std::string* peerId)
{ {
MS_TRACE(); MS_TRACE();
...@@ -194,70 +227,10 @@ namespace RTC ...@@ -194,70 +227,10 @@ namespace RTC
} }
} }
void Room::onRTPPacket(RTC::Peer* peer, RTC::RTPPacket* packet) void Room::onPeerClosed(RTC::Peer* peer)
{
MS_TRACE();
// int peer_id = *(int*)peer->GetUserData();
// MS_BYTE payload_type = packet->GetPayloadType();
// MS_4BYTES original_ssrc = packet->GetSSRC();
// MS_DEBUG("valid RTP packet received from Peer %d [ssrc: %llu | payload: %hu]",
// peer_id, (unsigned long long)packet->GetSSRC(), (unsigned short)packet->GetPayloadType());
// switch (payload_type)
// {
// case PAYLOAD_TYPE_AUDIO:
// case PAYLOAD_TYPE_VIDEO:
// break;
// default:
// MS_ERROR("payload is not OPUS %d nor VP8 %d, packet ignored", PAYLOAD_TYPE_AUDIO, PAYLOAD_TYPE_VIDEO);
// return;
// }
// // Deliver to all the peers.
// // TODO: but this one? yes (TODO)
// for (auto dst_peer : this->peers)
// {
// // if (dst_peer == peer)
// // continue;
// int dst_peer_id = *(int*)dst_peer->GetUserData();
// switch (payload_type)
// {
// case PAYLOAD_TYPE_AUDIO:
// packet->SetSSRC((MS_4BYTES)(SSRC_AUDIO_BASE + (peer_id * 10) + dst_peer_id));
// break;
// case PAYLOAD_TYPE_VIDEO:
// packet->SetSSRC((MS_4BYTES)(SSRC_VIDEO_BASE + (peer_id * 10) + dst_peer_id));
// break;
// default:
// MS_ABORT("no puede ser!!!");
// return;
// }
// MS_DEBUG("sending RTP packet to Peer %d [ssrc: %llu | payload: %hu | size: %zu]",
// dst_peer_id, (unsigned long long)packet->GetSSRC(), (unsigned short)packet->GetPayloadType(), packet->GetLength());
// dst_peer->SendRTPPacket(packet);
// // NOTE: recover the previous SSRC since other peers are going to
// // send this same RTPPacket!
// packet->SetSSRC(original_ssrc);
// }
}
void Room::onRTCPPacket(RTC::Peer* peer, RTC::RTCPPacket* packet)
{ {
MS_TRACE(); MS_TRACE();
// TMP this->peers.erase(peer->peerId);
// Ignore RTCP packets.
// for (auto dst_peer : this->peers) {
// dst_peer->SendRTCPPacket(packet);
// }
} }
} }
#define MS_CLASS "RTC::Stream"
#include "RTC/Stream.h"
#include "Logger.h"
namespace RTC
{
/* Instance methods. */
Stream::Stream()
{
MS_TRACE();
}
Stream::~Stream()
{
MS_TRACE();
}
void Stream::Close()
{
MS_TRACE();
delete this;
}
}
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
#include "RTC/TCPConnection.h" #include "RTC/TCPConnection.h"
#include "RTC/STUNMessage.h" #include "RTC/STUNMessage.h"
#include "RTC/DTLSHandler.h" #include "RTC/DTLSAgent.h"
#include "RTC/RTPPacket.h" #include "RTC/RTPPacket.h"
#include "RTC/RTCPPacket.h" #include "RTC/RTCPPacket.h"
#include "Utils.h" #include "Utils.h"
...@@ -84,7 +84,7 @@ namespace RTC ...@@ -84,7 +84,7 @@ namespace RTC
this->reader->onRTPDataRecv(this, packet, packet_len); this->reader->onRTPDataRecv(this, packet, packet_len);
} }
// Check if it's DTLS. // Check if it's DTLS.
else if (DTLSHandler::IsDTLS(packet, packet_len)) else if (DTLSAgent::IsDTLS(packet, packet_len))
{ {
this->reader->onDTLSDataRecv(this, packet, packet_len); this->reader->onDTLSDataRecv(this, packet, packet_len);
} }
......
...@@ -3,7 +3,6 @@ ...@@ -3,7 +3,6 @@
#include "RTC/TCPServer.h" #include "RTC/TCPServer.h"
#include "RTC/STUNMessage.h" #include "RTC/STUNMessage.h"
#include "RTC/RTPPacket.h" #include "RTC/RTPPacket.h"
#include "RTC/DTLSHandler.h"
#include "Settings.h" #include "Settings.h"
#include "Utils.h" #include "Utils.h"
#include "DepLibUV.h" #include "DepLibUV.h"
...@@ -67,7 +66,7 @@ namespace RTC ...@@ -67,7 +66,7 @@ namespace RTC
while (i++ != RTC::TCPServer::maxPort); while (i++ != RTC::TCPServer::maxPort);
} }
RTC::TCPServer* TCPServer::New(int address_family) RTC::TCPServer* TCPServer::Factory(Listener* listener, RTC::TCPConnection::Reader* reader, int address_family)
{ {
MS_TRACE(); MS_TRACE();
...@@ -77,10 +76,10 @@ namespace RTC ...@@ -77,10 +76,10 @@ namespace RTC
// or there are no available ports. // or there are no available ports.
RandomizePort(address_family, uvHandles, false); RandomizePort(address_family, uvHandles, false);
return new RTC::TCPServer(uvHandles[0]); return new RTC::TCPServer(listener, reader, uvHandles[0]);
} }
void TCPServer::NewPair(int address_family, RTC::TCPServer* servers[]) void TCPServer::PairFactory(Listener* listener, RTC::TCPConnection::Reader* reader, int address_family, RTC::TCPServer* servers[])
{ {
MS_TRACE(); MS_TRACE();
...@@ -90,8 +89,8 @@ namespace RTC ...@@ -90,8 +89,8 @@ namespace RTC
// or there are no available ports. // or there are no available ports.
RandomizePort(address_family, uvHandles, true); RandomizePort(address_family, uvHandles, true);
servers[0] = new RTC::TCPServer(uvHandles[0]); servers[0] = new RTC::TCPServer(listener, reader, uvHandles[0]);
servers[1] = new RTC::TCPServer(uvHandles[1]); servers[1] = new RTC::TCPServer(listener, reader, uvHandles[1]);
} }
void TCPServer::RandomizePort(int address_family, uv_tcp_t* uvHandles[], bool pair) void TCPServer::RandomizePort(int address_family, uv_tcp_t* uvHandles[], bool pair)
...@@ -322,8 +321,10 @@ namespace RTC ...@@ -322,8 +321,10 @@ namespace RTC
/* Instance methods. */ /* Instance methods. */
TCPServer::TCPServer(uv_tcp_t* uvHandle) : TCPServer::TCPServer(Listener* listener, RTC::TCPConnection::Reader* reader, uv_tcp_t* uvHandle) :
::TCPServer::TCPServer(uvHandle, 256) ::TCPServer::TCPServer(uvHandle, 256),
listener(listener),
reader(reader)
{ {
MS_TRACE(); MS_TRACE();
} }
......
差异被折叠。
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
#include "RTC/UDPSocket.h" #include "RTC/UDPSocket.h"
#include "RTC/STUNMessage.h" #include "RTC/STUNMessage.h"
#include "RTC/DTLSHandler.h" #include "RTC/DTLSAgent.h"
#include "RTC/RTPPacket.h" #include "RTC/RTPPacket.h"
#include "RTC/RTCPPacket.h" #include "RTC/RTCPPacket.h"
#include "Settings.h" #include "Settings.h"
...@@ -67,7 +67,7 @@ namespace RTC ...@@ -67,7 +67,7 @@ namespace RTC
while (i++ != RTC::UDPSocket::maxPort); while (i++ != RTC::UDPSocket::maxPort);
} }
RTC::UDPSocket* UDPSocket::New(int address_family) RTC::UDPSocket* UDPSocket::Factory(Listener* listener, int address_family)
{ {
MS_TRACE(); MS_TRACE();
...@@ -77,10 +77,10 @@ namespace RTC ...@@ -77,10 +77,10 @@ namespace RTC
// or there are no available ports. // or there are no available ports.
RandomizePort(address_family, uvHandles, false); RandomizePort(address_family, uvHandles, false);
return new RTC::UDPSocket(uvHandles[0]); return new RTC::UDPSocket(listener, uvHandles[0]);
} }
void UDPSocket::NewPair(int address_family, RTC::UDPSocket* sockets[]) void UDPSocket::PairFactory(Listener* listener, int address_family, RTC::UDPSocket* sockets[])
{ {
MS_TRACE(); MS_TRACE();
...@@ -90,8 +90,8 @@ namespace RTC ...@@ -90,8 +90,8 @@ namespace RTC
// or there are no available ports. // or there are no available ports.
RandomizePort(address_family, uvHandles, true); RandomizePort(address_family, uvHandles, true);
sockets[0] = new RTC::UDPSocket(uvHandles[0]); sockets[0] = new RTC::UDPSocket(listener, uvHandles[0]);
sockets[1] = new RTC::UDPSocket(uvHandles[1]); sockets[1] = new RTC::UDPSocket(listener, uvHandles[1]);
} }
void UDPSocket::RandomizePort(int address_family, uv_udp_t* uvHandles[], bool pair) void UDPSocket::RandomizePort(int address_family, uv_udp_t* uvHandles[], bool pair)
...@@ -320,8 +320,9 @@ namespace RTC ...@@ -320,8 +320,9 @@ namespace RTC
/* Instance methods. */ /* Instance methods. */
UDPSocket::UDPSocket(uv_udp_t* uvHandle) : UDPSocket::UDPSocket(Listener* listener, uv_udp_t* uvHandle) :
::UDPSocket::UDPSocket(uvHandle) ::UDPSocket::UDPSocket(uvHandle),
listener(listener)
{ {
MS_TRACE(); MS_TRACE();
} }
...@@ -367,7 +368,7 @@ namespace RTC ...@@ -367,7 +368,7 @@ namespace RTC
} }
// Check if it's DTLS. // Check if it's DTLS.
if (DTLSHandler::IsDTLS(data, len)) if (DTLSAgent::IsDTLS(data, len))
{ {
this->listener->onDTLSDataRecv(this, data, len, addr); this->listener->onDTLSDataRecv(this, data, len, addr);
return; return;
......
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
#include "Utils.h" #include "Utils.h"
#include "RTC/UDPSocket.h" #include "RTC/UDPSocket.h"
#include "RTC/TCPServer.h" #include "RTC/TCPServer.h"
#include "RTC/DTLSHandler.h" #include "RTC/DTLSAgent.h"
#include "RTC/SRTPSession.h" #include "RTC/SRTPSession.h"
#include "Loop.h" #include "Loop.h"
#include "MediaSoupError.h" #include "MediaSoupError.h"
...@@ -91,7 +91,7 @@ void init() ...@@ -91,7 +91,7 @@ void init()
DepUsrSCTP::ClassInit(); DepUsrSCTP::ClassInit();
RTC::UDPSocket::ClassInit(); RTC::UDPSocket::ClassInit();
RTC::TCPServer::ClassInit(); RTC::TCPServer::ClassInit();
RTC::DTLSHandler::ClassInit(); RTC::DTLSAgent::ClassInit();
RTC::SRTPSession::ClassInit(); RTC::SRTPSession::ClassInit();
Utils::Crypto::ClassInit(); Utils::Crypto::ClassInit();
} }
...@@ -137,6 +137,6 @@ void destroy() ...@@ -137,6 +137,6 @@ void destroy()
DepOpenSSL::ClassDestroy(); DepOpenSSL::ClassDestroy();
DepLibSRTP::ClassDestroy(); DepLibSRTP::ClassDestroy();
DepUsrSCTP::ClassDestroy(); DepUsrSCTP::ClassDestroy();
RTC::DTLSHandler::ClassDestroy(); RTC::DTLSAgent::ClassDestroy();
Utils::Crypto::ClassDestroy(); Utils::Crypto::ClassDestroy();
} }
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论