提交 5d7c2b7b authored 作者: Iñaki Baz Castillo's avatar Iñaki Baz Castillo

Work in RtpReceiver.

上级 70bf6184
...@@ -47,7 +47,7 @@ class Peer extends EventEmitter ...@@ -47,7 +47,7 @@ class Peer extends EventEmitter
this._pc.oniceconnectionstatechange = () => this._pc.oniceconnectionstatechange = () =>
{ {
if ([ 'closed', 'failed' ].indexOf(this._pc.iceConnectionState) !== -1) if ([ 'closed', 'disconnected', 'failed' ].indexOf(this._pc.iceConnectionState) !== -1)
this.closeLocalStream(); this.closeLocalStream();
}; };
...@@ -152,7 +152,7 @@ class Peer extends EventEmitter ...@@ -152,7 +152,7 @@ class Peer extends EventEmitter
// errback // errback
(error) => { throw new Error(error); }, (error) => { throw new Error(error); },
// options // options
{ offerToReceiveAudio: true, offerToReceiveVideo: true } { offerToReceiveAudio: 2, offerToReceiveVideo: 2 }
); );
function sendRequest() function sendRequest()
......
...@@ -33,6 +33,7 @@ namespace RTC ...@@ -33,6 +33,7 @@ namespace RTC
private: private:
RTC::Transport* GetTransportFromRequest(Channel::Request* request, uint32_t* transportId = nullptr); RTC::Transport* GetTransportFromRequest(Channel::Request* request, uint32_t* transportId = nullptr);
RTC::Transport* GetRtcpTransportFromRequest(Channel::Request* request, uint32_t* rtcpTransportId = nullptr);
RTC::RtpReceiver* GetRtpReceiverFromRequest(Channel::Request* request, uint32_t* rtpReceiverId = nullptr); RTC::RtpReceiver* GetRtpReceiverFromRequest(Channel::Request* request, uint32_t* rtpReceiverId = nullptr);
/* Pure virtual methods inherited from RTC::Transport::Listener. */ /* Pure virtual methods inherited from RTC::Transport::Listener. */
......
#ifndef MS_RTC_RTP_LISTENER_H
#define MS_RTC_RTP_LISTENER_H
#include "common.h"
#include "RTC/RtpReceiver.h"
#include "RTC/RTPPacket.h"
#include <unordered_map>
namespace RTC
{
class RtpListener
{
public:
RtpListener();
virtual ~RtpListener();
void Close();
void AddRtpReceiver(RTC::RtpReceiver* rtpReceiver);
RTC::RtpReceiver* GetRtpReceiver(RTC::RTPPacket* rtpPacket);
private:
// Others.
// - Map of rtpReceiverId / RtpReceiver pairs.
std::unordered_map<uint32_t, RTC::RtpReceiver*> rtpReceivers;
// - Table of SSRC / RtpReceiver pairs.
std::unordered_map<MS_4BYTES, RTC::RtpReceiver*> ssrcTable;
// - Table of MID RTP header extension / RtpReceiver pairs.
// std::unordered_map<MS_2BYTES, RTC::RtpReceiver*> midTable;
// - Table of RTP payload type / RtpReceiver pairs.
std::unordered_map<MS_BYTE, RTC::RtpReceiver*> ptTable;
};
}
#endif
...@@ -40,7 +40,7 @@ namespace RTC ...@@ -40,7 +40,7 @@ namespace RTC
void AddCodec(Json::Value& data); void AddCodec(Json::Value& data);
void AddEncoding(Json::Value& data); void AddEncoding(Json::Value& data);
private: public:
// TODO: not sure if 1 or 2 bytes or what... // TODO: not sure if 1 or 2 bytes or what...
std::string muxId; std::string muxId;
std::vector<CodecParameters> codecs; std::vector<CodecParameters> codecs;
......
...@@ -13,10 +13,23 @@ namespace RTC ...@@ -13,10 +13,23 @@ namespace RTC
class RtpReceiver class RtpReceiver
{ {
public: public:
/**
* RTC::Peer is the Listener.
*/
class Listener class Listener
{ {
public: public:
virtual void onRtpReceiverClosed(RTC::RtpReceiver* rtpReceiver) = 0; virtual void onRtpReceiverClosed(RtpReceiver* rtpReceiver) = 0;
};
/**
* RTC::Transport is the RtpListener.
*/
class RtpListener
{
public:
virtual void onRtpListenerParameters(RtpReceiver* rtpReceiver, RTC::RtpParameters* rtpParameters) = 0;
virtual void onRtpReceiverClosed(RtpReceiver* rtpReceiver) = 0;
}; };
public: public:
...@@ -26,6 +39,9 @@ namespace RTC ...@@ -26,6 +39,9 @@ namespace RTC
void Close(); void Close();
Json::Value toJson(); Json::Value toJson();
void HandleRequest(Channel::Request* request); void HandleRequest(Channel::Request* request);
void SetRtpListener(RtpListener* rtpListener);
void SetRtpListenerForRtcp(RtpListener* rtcpListener);
void RemoveRtpListener(RtpListener* rtpListener);
public: public:
// Passed by argument. // Passed by argument.
...@@ -35,9 +51,35 @@ namespace RTC ...@@ -35,9 +51,35 @@ namespace RTC
// Passed by argument. // Passed by argument.
Listener* listener = nullptr; Listener* listener = nullptr;
Channel::Notifier* notifier = nullptr; Channel::Notifier* notifier = nullptr;
RtpListener* rtpListener = nullptr;
RtpListener* rtcpListener = nullptr;
// Allocated by this. // Allocated by this.
RTC::RtpParameters* rtpParameters = nullptr; RTC::RtpParameters* rtpParameters = nullptr;
}; };
/* Inline methods. */
inline
void RtpReceiver::SetRtpListener(RtpListener* rtpListener)
{
this->rtpListener = rtpListener;
}
inline
void RtpReceiver::SetRtpListenerForRtcp(RtpListener* rtcpListener)
{
this->rtcpListener = rtcpListener;
}
inline
void RtpReceiver::RemoveRtpListener(RtpListener* rtpListener)
{
if (this->rtpListener == rtpListener)
this->rtpListener = nullptr;
if (this->rtcpListener == rtpListener)
this->rtcpListener = nullptr;
}
} }
#endif #endif
...@@ -13,11 +13,11 @@ ...@@ -13,11 +13,11 @@
#include "RTC/RTPPacket.h" #include "RTC/RTPPacket.h"
#include "RTC/RTCPPacket.h" #include "RTC/RTCPPacket.h"
#include "RTC/RtpReceiver.h" #include "RTC/RtpReceiver.h"
#include "RTC/RtpListener.h"
#include "Channel/Request.h" #include "Channel/Request.h"
#include "Channel/Notifier.h" #include "Channel/Notifier.h"
#include <string> #include <string>
#include <vector> #include <vector>
#include <unordered_map>
#include <json/json.h> #include <json/json.h>
namespace RTC namespace RTC
...@@ -27,7 +27,8 @@ namespace RTC ...@@ -27,7 +27,8 @@ namespace RTC
public RTC::TCPServer::Listener, public RTC::TCPServer::Listener,
public RTC::TCPConnection::Listener, public RTC::TCPConnection::Listener,
public RTC::ICEServer::Listener, public RTC::ICEServer::Listener,
public RTC::DTLSTransport::Listener public RTC::DTLSTransport::Listener,
public RTC::RtpReceiver::RtpListener
{ {
public: public:
class Listener class Listener
...@@ -36,6 +37,17 @@ namespace RTC ...@@ -36,6 +37,17 @@ namespace RTC
virtual void onTransportClosed(RTC::Transport* transport) = 0; virtual void onTransportClosed(RTC::Transport* transport) = 0;
}; };
private:
struct RtpListener
{
// - Table of SSRC / RtpReceiver pairs.
std::unordered_map<uint32_t, RTC::RtpReceiver*> ssrcTable;
// - Table of MID RTP header extension / RtpReceiver pairs.
// std::unordered_map<MS_2BYTES, RTC::RtpReceiver*> midTable;
// - Table of RTP payload type / RtpReceiver pairs.
std::unordered_map<uint8_t, RTC::RtpReceiver*> ptTable;
};
public: public:
Transport(Listener* listener, Channel::Notifier* notifier, uint32_t transportId, Json::Value& data, Transport* rtpTransport = nullptr); Transport(Listener* listener, Channel::Notifier* notifier, uint32_t transportId, Json::Value& data, Transport* rtpTransport = nullptr);
virtual ~Transport(); virtual ~Transport();
...@@ -46,10 +58,10 @@ namespace RTC ...@@ -46,10 +58,10 @@ namespace RTC
std::string& GetIceUsernameFragment(); std::string& GetIceUsernameFragment();
std::string& GetIcePassword(); std::string& GetIcePassword();
Transport* CreateAssociatedTransport(uint32_t transportId); Transport* CreateAssociatedTransport(uint32_t transportId);
void AddRtpReceiver(RTC::RtpReceiver* rtpReceiver);
private: private:
void MayRunDTLSTransport(); void MayRunDTLSTransport();
void RemoveRtpReceiverFromRtpListener(RTC::RtpReceiver* rtpReceiver);
/* Private methods to unify UDP and TCP behavior. */ /* Private methods to unify UDP and TCP behavior. */
private: private:
...@@ -88,6 +100,10 @@ namespace RTC ...@@ -88,6 +100,10 @@ namespace RTC
virtual void onOutgoingDTLSData(RTC::DTLSTransport* dtlsTransport, const MS_BYTE* data, size_t len) override; virtual void onOutgoingDTLSData(RTC::DTLSTransport* dtlsTransport, const MS_BYTE* data, size_t len) override;
virtual void onDTLSApplicationData(RTC::DTLSTransport* dtlsTransport, const MS_BYTE* data, size_t len) override; virtual void onDTLSApplicationData(RTC::DTLSTransport* dtlsTransport, const MS_BYTE* data, size_t len) override;
/* Pure virtual methods inherited from RTC::RtpReceiver::RtpListener. */
virtual void onRtpListenerParameters(RTC::RtpReceiver* rtpReceiver, RTC::RtpParameters* rtpParameters) override;
virtual void onRtpReceiverClosed(RTC::RtpReceiver* rtpReceiver) override;
public: public:
// Passed by argument. // Passed by argument.
uint32_t transportId; uint32_t transportId;
...@@ -108,7 +124,6 @@ namespace RTC ...@@ -108,7 +124,6 @@ namespace RTC
std::vector<RTC::UDPSocket*> udpSockets; std::vector<RTC::UDPSocket*> udpSockets;
std::vector<RTC::TCPServer*> tcpServers; std::vector<RTC::TCPServer*> tcpServers;
RTC::DTLSTransport* dtlsTransport = nullptr; RTC::DTLSTransport* dtlsTransport = nullptr;
RTC::RtpListener* rtpListener = nullptr;
// Others. // Others.
bool allocated = false; bool allocated = false;
// Others (ICE). // Others (ICE).
...@@ -117,6 +132,8 @@ namespace RTC ...@@ -117,6 +132,8 @@ namespace RTC
// Others (DTLS). // Others (DTLS).
bool remoteDtlsParametersGiven = false; bool remoteDtlsParametersGiven = false;
RTC::DTLSTransport::Role dtlsLocalRole = RTC::DTLSTransport::Role::AUTO; RTC::DTLSTransport::Role dtlsLocalRole = RTC::DTLSTransport::Role::AUTO;
// Others (RTP listener).
RtpListener rtpListener;
}; };
/* Inline methods. */ /* Inline methods. */
......
...@@ -93,16 +93,16 @@ RTC::Room* Loop::GetRoomFromRequest(Channel::Request* request, uint32_t* roomId) ...@@ -93,16 +93,16 @@ RTC::Room* Loop::GetRoomFromRequest(Channel::Request* request, uint32_t* roomId)
static const Json::StaticString k_roomId("roomId"); static const Json::StaticString k_roomId("roomId");
auto jsonRoomId = request->internal[k_roomId]; auto json_roomId = request->internal[k_roomId];
if (!jsonRoomId.isUInt()) if (!json_roomId.isUInt())
MS_THROW_ERROR("Request has not numeric `internal.roomId`"); MS_THROW_ERROR("Request has not numeric `internal.roomId`");
// If given, fill roomId. // If given, fill roomId.
if (roomId) if (roomId)
*roomId = jsonRoomId.asUInt(); *roomId = json_roomId.asUInt();
auto it = this->rooms.find(jsonRoomId.asUInt()); auto it = this->rooms.find(json_roomId.asUInt());
if (it != this->rooms.end()) if (it != this->rooms.end())
{ {
RTC::Room* room = it->second; RTC::Room* room = it->second;
......
...@@ -39,36 +39,36 @@ namespace RTC ...@@ -39,36 +39,36 @@ namespace RTC
static const Json::StaticString k_tcpType("tcpType"); static const Json::StaticString k_tcpType("tcpType");
static const Json::StaticString v_passive("passive"); static const Json::StaticString v_passive("passive");
Json::Value data; Json::Value json;
data[k_foundation] = this->foundation; json[k_foundation] = this->foundation;
data[k_priority] = this->priority; json[k_priority] = this->priority;
data[k_ip] = this->ip; json[k_ip] = this->ip;
data[k_port] = this->port; json[k_port] = this->port;
switch (this->type) switch (this->type)
{ {
case CandidateType::HOST: case CandidateType::HOST:
data[k_type] = v_host; json[k_type] = v_host;
break; break;
} }
switch (this->protocol) switch (this->protocol)
{ {
case Protocol::UDP: case Protocol::UDP:
data[k_protocol] = v_udp; json[k_protocol] = v_udp;
break; break;
case Protocol::TCP: case Protocol::TCP:
data[k_protocol] = v_tcp; json[k_protocol] = v_tcp;
switch (this->tcpType) switch (this->tcpType)
{ {
case TcpCandidateType::PASSIVE: case TcpCandidateType::PASSIVE:
data[k_tcpType] = v_passive; json[k_tcpType] = v_passive;
break; break;
} }
break; break;
} }
return data; return json;
} }
} }
...@@ -26,18 +26,6 @@ namespace RTC ...@@ -26,18 +26,6 @@ namespace RTC
{ {
MS_TRACE(); MS_TRACE();
// Close all the Transports.
// NOTE: Upon Transport closure the onTransportClosed() 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->transports.begin(); it != this->transports.end();)
{
RTC::Transport* transport = it->second;
it = this->transports.erase(it);
transport->Close();
}
// Close all the RtpReceivers. // Close all the RtpReceivers.
for (auto it = this->rtpReceivers.begin(); it != this->rtpReceivers.end();) for (auto it = this->rtpReceivers.begin(); it != this->rtpReceivers.end();)
{ {
...@@ -47,6 +35,17 @@ namespace RTC ...@@ -47,6 +35,17 @@ namespace RTC
rtpReceiver->Close(); rtpReceiver->Close();
} }
// Close all the Transports.
// NOTE: It is critical to close Transports after RtpReceivers because
// RtcReceiver.Close() fires an event in the Transport.
for (auto it = this->transports.begin(); it != this->transports.end();)
{
RTC::Transport* transport = it->second;
it = this->transports.erase(it);
transport->Close();
}
// Notify the listener. // Notify the listener.
this->listener->onPeerClosed(this); this->listener->onPeerClosed(this);
...@@ -160,6 +159,7 @@ namespace RTC ...@@ -160,6 +159,7 @@ namespace RTC
case Channel::Request::MethodId::peer_createAssociatedTransport: case Channel::Request::MethodId::peer_createAssociatedTransport:
{ {
RTC::Transport* transport; RTC::Transport* transport;
RTC::Transport* rtpTransport;
uint32_t transportId; uint32_t transportId;
try try
...@@ -182,9 +182,9 @@ namespace RTC ...@@ -182,9 +182,9 @@ namespace RTC
static const Json::StaticString k_rtpTransportId("rtpTransportId"); static const Json::StaticString k_rtpTransportId("rtpTransportId");
auto jsonRtpTransportId = request->internal[k_rtpTransportId]; auto json_rtpTransportId = request->internal[k_rtpTransportId];
if (!jsonRtpTransportId.isUInt()) if (!json_rtpTransportId.isUInt())
{ {
MS_ERROR("Request has not numeric `internal.rtpTransportId`"); MS_ERROR("Request has not numeric `internal.rtpTransportId`");
...@@ -192,7 +192,7 @@ namespace RTC ...@@ -192,7 +192,7 @@ namespace RTC
return; return;
} }
auto it = this->transports.find(jsonRtpTransportId.asUInt()); auto it = this->transports.find(json_rtpTransportId.asUInt());
if (it == this->transports.end()) if (it == this->transports.end())
{ {
MS_ERROR("RTP Transport does not exist"); MS_ERROR("RTP Transport does not exist");
...@@ -201,7 +201,7 @@ namespace RTC ...@@ -201,7 +201,7 @@ namespace RTC
return; return;
} }
RTC::Transport* rtpTransport = it->second; rtpTransport = it->second;
try try
{ {
...@@ -227,11 +227,9 @@ namespace RTC ...@@ -227,11 +227,9 @@ namespace RTC
case Channel::Request::MethodId::peer_createRtpReceiver: case Channel::Request::MethodId::peer_createRtpReceiver:
{ {
RTC::RtpReceiver* rtpReceiver; RTC::RtpReceiver* rtpReceiver;
uint32_t rtpReceiverId;
uint32_t transportId;
uint32_t rtcpTransportId;
RTC::Transport* transport = nullptr; RTC::Transport* transport = nullptr;
RTC::Transport* rtcpTransport = nullptr; RTC::Transport* rtcpTransport = nullptr;
uint32_t rtpReceiverId;
try try
{ {
...@@ -251,22 +249,17 @@ namespace RTC ...@@ -251,22 +249,17 @@ namespace RTC
return; return;
} }
static const Json::StaticString k_transportId("transportId"); try
auto jsonTransportId = request->internal[k_transportId];
if (!jsonTransportId.isUInt())
{ {
MS_ERROR("Request has not numeric `internal.transportId`"); transport = GetTransportFromRequest(request);
}
request->Reject("Request has not numeric `internal.transportId`"); catch (const MediaSoupError &error)
{
request->Reject(error.what());
return; return;
} }
transportId = jsonTransportId.asUInt(); if (!transport)
auto it = this->transports.find(transportId);
if (it == this->transports.end())
{ {
MS_ERROR("Transport does not exist"); MS_ERROR("Transport does not exist");
...@@ -274,24 +267,17 @@ namespace RTC ...@@ -274,24 +267,17 @@ namespace RTC
return; return;
} }
transport = it->second; try
static const Json::StaticString k_rtcpTransportId("rtcpTransportId");
auto jsonRtcpTransportId = request->internal[k_rtcpTransportId];
if (!jsonRtcpTransportId.isUInt())
{ {
MS_ERROR("Request has not numeric `internal.rtcpTransportId`"); rtcpTransport = GetRtcpTransportFromRequest(request);
}
request->Reject("Request has not numeric `internal.rtcpTransportId`"); catch (const MediaSoupError &error)
{
request->Reject(error.what());
return; return;
} }
rtcpTransportId = jsonRtcpTransportId.asUInt(); if (!rtcpTransport)
it = this->transports.find(rtcpTransportId);
if (it == this->transports.end())
{ {
MS_ERROR("RTCP Transport does not exist"); MS_ERROR("RTCP Transport does not exist");
...@@ -299,8 +285,6 @@ namespace RTC ...@@ -299,8 +285,6 @@ namespace RTC
return; return;
} }
rtcpTransport = it->second;
// Create a RtpReceiver instance. // Create a RtpReceiver instance.
rtpReceiver = new RTC::RtpReceiver(this, this->notifier, rtpReceiverId); rtpReceiver = new RTC::RtpReceiver(this, this->notifier, rtpReceiverId);
...@@ -308,10 +292,10 @@ namespace RTC ...@@ -308,10 +292,10 @@ namespace RTC
MS_DEBUG("RtpReceiver created [rtpReceiverId:%" PRIu32 "]", rtpReceiverId); MS_DEBUG("RtpReceiver created [rtpReceiverId:%" PRIu32 "]", rtpReceiverId);
// Provide the Transports with the RtpReceiver. // Set the transports as RTP and RTCP listeners.
transport->AddRtpReceiver(rtpReceiver); rtpReceiver->SetRtpListener(transport);
if (rtcpTransportId != transportId) if (rtcpTransport != transport)
rtcpTransport->AddRtpReceiver(rtpReceiver); rtpReceiver->SetRtpListenerForRtcp(rtcpTransport);
request->Accept(); request->Accept();
...@@ -391,15 +375,15 @@ namespace RTC ...@@ -391,15 +375,15 @@ namespace RTC
static const Json::StaticString k_transportId("transportId"); static const Json::StaticString k_transportId("transportId");
auto jsonTransportId = request->internal[k_transportId]; auto json_transportId = request->internal[k_transportId];
if (!jsonTransportId.isUInt()) if (!json_transportId.isUInt())
MS_THROW_ERROR("Request has not numeric `internal.transportId`"); MS_THROW_ERROR("Request has not numeric `internal.transportId`");
if (transportId) if (transportId)
*transportId = jsonTransportId.asUInt(); *transportId = json_transportId.asUInt();
auto it = this->transports.find(jsonTransportId.asUInt()); auto it = this->transports.find(json_transportId.asUInt());
if (it != this->transports.end()) if (it != this->transports.end())
{ {
RTC::Transport* transport = it->second; RTC::Transport* transport = it->second;
...@@ -412,21 +396,48 @@ namespace RTC ...@@ -412,21 +396,48 @@ namespace RTC
} }
} }
RTC::Transport* Peer::GetRtcpTransportFromRequest(Channel::Request* request, uint32_t* rtcpTransportId)
{
MS_TRACE();
static const Json::StaticString k_rtcpTransportId("rtcpTransportId");
auto json_rtcpTransportId = request->internal[k_rtcpTransportId];
if (!json_rtcpTransportId.isUInt())
MS_THROW_ERROR("Request has not numeric `internal.rtcpTransportId`");
if (rtcpTransportId)
*rtcpTransportId = json_rtcpTransportId.asUInt();
auto it = this->transports.find(json_rtcpTransportId.asUInt());
if (it != this->transports.end())
{
RTC::Transport* rtcpTransport = it->second;
return rtcpTransport;
}
else
{
return nullptr;
}
}
RTC::RtpReceiver* Peer::GetRtpReceiverFromRequest(Channel::Request* request, uint32_t* rtpReceiverId) RTC::RtpReceiver* Peer::GetRtpReceiverFromRequest(Channel::Request* request, uint32_t* rtpReceiverId)
{ {
MS_TRACE(); MS_TRACE();
static const Json::StaticString k_rtpReceiverId("rtpReceiverId"); static const Json::StaticString k_rtpReceiverId("rtpReceiverId");
auto jsonRtpReceiverId = request->internal[k_rtpReceiverId]; auto json_rtpReceiverId = request->internal[k_rtpReceiverId];
if (!jsonRtpReceiverId.isUInt()) if (!json_rtpReceiverId.isUInt())
MS_THROW_ERROR("Request has not numeric `internal.rtpReceiverId`"); MS_THROW_ERROR("Request has not numeric `internal.rtpReceiverId`");
if (rtpReceiverId) if (rtpReceiverId)
*rtpReceiverId = jsonRtpReceiverId.asUInt(); *rtpReceiverId = json_rtpReceiverId.asUInt();
auto it = this->rtpReceivers.find(jsonRtpReceiverId.asUInt()); auto it = this->rtpReceivers.find(json_rtpReceiverId.asUInt());
if (it != this->rtpReceivers.end()) if (it != this->rtpReceivers.end())
{ {
RTC::RtpReceiver* rtpReceiver = it->second; RTC::RtpReceiver* rtpReceiver = it->second;
...@@ -443,6 +454,15 @@ namespace RTC ...@@ -443,6 +454,15 @@ namespace RTC
{ {
MS_TRACE(); MS_TRACE();
// If a Transport is closed we need to inform all the RtpReceivers of the
// Peer since a RtpReceiver has one or two listeners which are Transports.
for (auto& kv : this->rtpReceivers)
{
RTC::RtpReceiver* rtpReceiver = kv.second;
rtpReceiver->RemoveRtpListener(transport);
}
this->transports.erase(transport->transportId); this->transports.erase(transport->transportId);
} }
......
...@@ -197,16 +197,16 @@ namespace RTC ...@@ -197,16 +197,16 @@ namespace RTC
static const Json::StaticString k_peerId("peerId"); static const Json::StaticString k_peerId("peerId");
auto jsonPeerId = request->internal[k_peerId]; auto json_peerId = request->internal[k_peerId];
if (!jsonPeerId.isUInt()) if (!json_peerId.isUInt())
MS_THROW_ERROR("Request has not numeric .peerId field"); MS_THROW_ERROR("Request has not numeric .peerId field");
// If given, fill peerId. // If given, fill peerId.
if (peerId) if (peerId)
*peerId = jsonPeerId.asUInt(); *peerId = json_peerId.asUInt();
auto it = this->peers.find(jsonPeerId.asUInt()); auto it = this->peers.find(json_peerId.asUInt());
if (it != this->peers.end()) if (it != this->peers.end())
{ {
RTC::Peer* peer = it->second; RTC::Peer* peer = it->second;
......
#define MS_CLASS "RTC::RtpListener"
#include "RTC/RtpListener.h"
#include "Logger.h"
namespace RTC
{
/* Instance methods. */
RtpListener::RtpListener()
{
MS_TRACE();
}
RtpListener::~RtpListener()
{
MS_TRACE();
}
void RtpListener::Close()
{
MS_TRACE();
delete this;
}
void RtpListener::AddRtpReceiver(RTC::RtpReceiver* rtpReceiver)
{
MS_TRACE();
auto it = this->rtpReceivers.find(rtpReceiver->rtpReceiverId);
// TODO: not sure, may be it is legal
MS_ASSERT(it == this->rtpReceivers.end(), "given RtpReceiver already exists");
this->rtpReceivers[rtpReceiver->rtpReceiverId] = rtpReceiver;
}
RTC::RtpReceiver* RtpListener::GetRtpReceiver(RTC::RTPPacket* rtpPacket)
{
MS_TRACE();
// TODO: yeah
return nullptr;
}
}
...@@ -27,7 +27,7 @@ namespace RTC ...@@ -27,7 +27,7 @@ namespace RTC
{ {
auto json_codecs = data[k_codecs]; auto json_codecs = data[k_codecs];
for (int i = 0; i < json_codecs.size(); i++) for (Json::UInt i = 0; i < json_codecs.size(); i++)
AddCodec(json_codecs[i]); AddCodec(json_codecs[i]);
} }
...@@ -35,7 +35,7 @@ namespace RTC ...@@ -35,7 +35,7 @@ namespace RTC
{ {
auto json_encodings = data[k_encodings]; auto json_encodings = data[k_encodings];
for (int i = 0; i < json_encodings.size(); i++) for (Json::UInt i = 0; i < json_encodings.size(); i++)
AddEncoding(json_encodings[i]); AddEncoding(json_encodings[i]);
} }
} }
...@@ -57,13 +57,13 @@ namespace RTC ...@@ -57,13 +57,13 @@ namespace RTC
static const Json::StaticString k_encodings("encodings"); static const Json::StaticString k_encodings("encodings");
static const Json::StaticString k_ssrc("ssrc"); static const Json::StaticString k_ssrc("ssrc");
Json::Value data; Json::Value json;
// Add `muxId`. // Add `muxId`.
data[k_muxId] = this->muxId; json[k_muxId] = this->muxId;
// Add `codecs`. // Add `codecs`.
data[k_codecs] = Json::arrayValue; json[k_codecs] = Json::arrayValue;
for (auto it = this->codecs.begin(); it != this->codecs.end(); ++it) for (auto it = this->codecs.begin(); it != this->codecs.end(); ++it)
{ {
CodecParameters* codec = std::addressof(*it); CodecParameters* codec = std::addressof(*it);
...@@ -76,11 +76,11 @@ namespace RTC ...@@ -76,11 +76,11 @@ namespace RTC
if (codec->clockRate) if (codec->clockRate)
json_codec[k_clockRate] = (Json::UInt)codec->clockRate; json_codec[k_clockRate] = (Json::UInt)codec->clockRate;
data[k_codecs].append(json_codec); json[k_codecs].append(json_codec);
} }
// Add `encodings`. // Add `encodings`.
data[k_encodings] = Json::arrayValue; json[k_encodings] = Json::arrayValue;
for (auto it = this->encodings.begin(); it != this->encodings.end(); ++it) for (auto it = this->encodings.begin(); it != this->encodings.end(); ++it)
{ {
EncodingParameters* encoding = std::addressof(*it); EncodingParameters* encoding = std::addressof(*it);
...@@ -88,10 +88,10 @@ namespace RTC ...@@ -88,10 +88,10 @@ namespace RTC
json_encoding[k_ssrc] = (Json::UInt)encoding->ssrc; json_encoding[k_ssrc] = (Json::UInt)encoding->ssrc;
data[k_encodings].append(json_encoding); json[k_encodings].append(json_encoding);
} }
return data; return json;
} }
void RtpParameters::AddCodec(Json::Value& data) void RtpParameters::AddCodec(Json::Value& data)
...@@ -112,6 +112,9 @@ namespace RTC ...@@ -112,6 +112,9 @@ namespace RTC
codec.name = data[k_name].asString(); codec.name = data[k_name].asString();
if (codec.name.empty())
MS_THROW_ERROR("empty `name`");
if (!data[k_payloadType].isUInt()) if (!data[k_payloadType].isUInt())
MS_THROW_ERROR("missing `payloadType`"); MS_THROW_ERROR("missing `payloadType`");
......
...@@ -31,8 +31,12 @@ namespace RTC ...@@ -31,8 +31,12 @@ namespace RTC
// Notify. // Notify.
this->notifier->Emit(this->rtpReceiverId, "close"); this->notifier->Emit(this->rtpReceiverId, "close");
// Notify the listener. // Notify the listener and also the rtpListener and rtcpListener.
this->listener->onRtpReceiverClosed(this); this->listener->onRtpReceiverClosed(this);
if (this->rtpListener)
this->rtpListener->onRtpReceiverClosed(this);
if (this->rtcpListener)
this->rtcpListener->onRtpReceiverClosed(this);
delete this; delete this;
} }
...@@ -43,14 +47,14 @@ namespace RTC ...@@ -43,14 +47,14 @@ namespace RTC
static const Json::StaticString k_rtpParameters("rtpParameters"); static const Json::StaticString k_rtpParameters("rtpParameters");
Json::Value data(Json::objectValue); Json::Value json;
// TODO
if (this->rtpParameters) if (this->rtpParameters)
data[k_rtpParameters] = this->rtpParameters->toJson(); json[k_rtpParameters] = this->rtpParameters->toJson();
else
json[k_rtpParameters] = Json::nullValue;
return data; return json;
} }
void RtpReceiver::HandleRequest(Channel::Request* request) void RtpReceiver::HandleRequest(Channel::Request* request)
...@@ -82,12 +86,8 @@ namespace RTC ...@@ -82,12 +86,8 @@ namespace RTC
case Channel::Request::MethodId::rtpReceiver_receive: case Channel::Request::MethodId::rtpReceiver_receive:
{ {
// If rtpParameteres was already set, delete it. // Keep a reference to the previous rtpParameters.
if (this->rtpParameters) auto previousRtpParameters = this->rtpParameters;
{
delete this->rtpParameters;
this->rtpParameters = nullptr;
}
try try
{ {
...@@ -99,8 +99,17 @@ namespace RTC ...@@ -99,8 +99,17 @@ namespace RTC
return; return;
} }
// Free the previous rtpParameters.
delete previousRtpParameters;
request->Accept(); request->Accept();
// Notify the rtpListener and rtcpListener.
if (this->rtpListener)
this->rtpListener->onRtpListenerParameters(this, this->rtpParameters);
if (this->rtcpListener)
this->rtcpListener->onRtpListenerParameters(this, this->rtpParameters);
break; break;
} }
......
...@@ -213,9 +213,6 @@ namespace RTC ...@@ -213,9 +213,6 @@ namespace RTC
// Create a DTLS agent. // Create a DTLS agent.
this->dtlsTransport = new RTC::DTLSTransport(this); this->dtlsTransport = new RTC::DTLSTransport(this);
// Create a RtpListener.
this->rtpListener = new RTC::RtpListener();
// Hack to avoid that Close() above attempts to delete this. // Hack to avoid that Close() above attempts to delete this.
this->allocated = true; this->allocated = true;
} }
...@@ -229,9 +226,6 @@ namespace RTC ...@@ -229,9 +226,6 @@ namespace RTC
{ {
MS_TRACE(); MS_TRACE();
if (this->rtpListener)
this->rtpListener->Close();
// if (this->srtpRecvSession) // if (this->srtpRecvSession)
// this->srtpRecvSession->Close(); // this->srtpRecvSession->Close();
...@@ -297,64 +291,68 @@ namespace RTC ...@@ -297,64 +291,68 @@ namespace RTC
static const Json::StaticString v_connecting("connecting"); static const Json::StaticString v_connecting("connecting");
static const Json::StaticString v_closed("closed"); static const Json::StaticString v_closed("closed");
static const Json::StaticString v_failed("failed"); static const Json::StaticString v_failed("failed");
static const Json::StaticString k_rtpListener("rtpListener");
static const Json::StaticString k_ssrcTable("ssrcTable");
// static const Json::StaticString k_midTable("midTable"); // TODO
static const Json::StaticString k_ptTable("ptTable");
Json::Value data; Json::Value json;
// Add `iceRole` (we are always "controlled"). // Add `iceRole` (we are always "controlled").
data[k_iceRole] = v_controlled; json[k_iceRole] = v_controlled;
// Add `iceComponent`. // Add `iceComponent`.
if (this->iceServer->GetComponent() == ICEServer::IceComponent::RTP) if (this->iceServer->GetComponent() == ICEServer::IceComponent::RTP)
data[k_iceComponent] = v_RTP; json[k_iceComponent] = v_RTP;
else else
data[k_iceComponent] = v_RTCP; json[k_iceComponent] = v_RTCP;
// Add `iceLocalParameters`. // Add `iceLocalParameters`.
data[k_iceLocalParameters][k_usernameFragment] = this->iceServer->GetUsernameFragment(); json[k_iceLocalParameters][k_usernameFragment] = this->iceServer->GetUsernameFragment();
data[k_iceLocalParameters][k_password] = this->iceServer->GetPassword(); json[k_iceLocalParameters][k_password] = this->iceServer->GetPassword();
// Add `iceLocalCandidates`. // Add `iceLocalCandidates`.
data[k_iceLocalCandidates] = Json::arrayValue; json[k_iceLocalCandidates] = Json::arrayValue;
for (auto iceCandidate : this->iceLocalCandidates) for (auto iceCandidate : this->iceLocalCandidates)
{ {
data[k_iceLocalCandidates].append(iceCandidate.toJson()); json[k_iceLocalCandidates].append(iceCandidate.toJson());
} }
// Add `iceSelectedTuple`. // Add `iceSelectedTuple`.
if (this->selectedTuple) if (this->selectedTuple)
data[k_iceSelectedTuple] = this->selectedTuple->toJson(); json[k_iceSelectedTuple] = this->selectedTuple->toJson();
// Add `iceState`. // Add `iceState`.
switch (this->iceServer->GetState()) switch (this->iceServer->GetState())
{ {
case RTC::ICEServer::IceState::NEW: case RTC::ICEServer::IceState::NEW:
data[k_iceState] = v_new; json[k_iceState] = v_new;
break; break;
case RTC::ICEServer::IceState::CONNECTED: case RTC::ICEServer::IceState::CONNECTED:
data[k_iceState] = v_connected; json[k_iceState] = v_connected;
break; break;
case RTC::ICEServer::IceState::COMPLETED: case RTC::ICEServer::IceState::COMPLETED:
data[k_iceState] = v_completed; json[k_iceState] = v_completed;
break; break;
case RTC::ICEServer::IceState::DISCONNECTED: case RTC::ICEServer::IceState::DISCONNECTED:
data[k_iceState] = v_disconnected; json[k_iceState] = v_disconnected;
break; break;
} }
// Add `dtlsLocalParameters.fingerprints`. // Add `dtlsLocalParameters.fingerprints`.
data[k_dtlsLocalParameters][k_fingerprints] = RTC::DTLSTransport::GetLocalFingerprints(); json[k_dtlsLocalParameters][k_fingerprints] = RTC::DTLSTransport::GetLocalFingerprints();
// Add `dtlsLocalParameters.role`. // Add `dtlsLocalParameters.role`.
switch (this->dtlsLocalRole) switch (this->dtlsLocalRole)
{ {
case RTC::DTLSTransport::Role::AUTO: case RTC::DTLSTransport::Role::AUTO:
data[k_dtlsLocalParameters][k_role] = v_auto; json[k_dtlsLocalParameters][k_role] = v_auto;
break; break;
case RTC::DTLSTransport::Role::CLIENT: case RTC::DTLSTransport::Role::CLIENT:
data[k_dtlsLocalParameters][k_role] = v_client; json[k_dtlsLocalParameters][k_role] = v_client;
break; break;
case RTC::DTLSTransport::Role::SERVER: case RTC::DTLSTransport::Role::SERVER:
data[k_dtlsLocalParameters][k_role] = v_server; json[k_dtlsLocalParameters][k_role] = v_server;
break; break;
default: default:
MS_ABORT("invalid local DTLS role"); MS_ABORT("invalid local DTLS role");
...@@ -364,23 +362,49 @@ namespace RTC ...@@ -364,23 +362,49 @@ namespace RTC
switch (this->dtlsTransport->GetState()) switch (this->dtlsTransport->GetState())
{ {
case DTLSTransport::DtlsState::NEW: case DTLSTransport::DtlsState::NEW:
data[k_dtlsState] = v_new; json[k_dtlsState] = v_new;
break; break;
case DTLSTransport::DtlsState::CONNECTING: case DTLSTransport::DtlsState::CONNECTING:
data[k_dtlsState] = v_connecting; json[k_dtlsState] = v_connecting;
break; break;
case DTLSTransport::DtlsState::CONNECTED: case DTLSTransport::DtlsState::CONNECTED:
data[k_dtlsState] = v_connected; json[k_dtlsState] = v_connected;
break; break;
case DTLSTransport::DtlsState::FAILED: case DTLSTransport::DtlsState::FAILED:
data[k_dtlsState] = v_failed; json[k_dtlsState] = v_failed;
break; break;
case DTLSTransport::DtlsState::CLOSED: case DTLSTransport::DtlsState::CLOSED:
data[k_dtlsState] = v_closed; json[k_dtlsState] = v_closed;
break; break;
} }
return data; // Add `rtpListener.ssrcTable`.
Json::Value json_ssrcTable(Json::objectValue);
for (auto& kv : this->rtpListener.ssrcTable)
{
auto ssrc = kv.first;
auto rtpReceiver = kv.second;
json_ssrcTable[std::to_string(ssrc)] = std::to_string(rtpReceiver->rtpReceiverId);
}
json[k_rtpListener][k_ssrcTable] = json_ssrcTable;
// TODO: Add `midTable`.
// Add `rtpListener.ptTable`.
Json::Value json_ptTable(Json::objectValue);
for (auto& kv : this->rtpListener.ptTable)
{
auto payloadType = kv.first;
auto rtpReceiver = kv.second;
json_ptTable[std::to_string(payloadType)] = std::to_string(rtpReceiver->rtpReceiverId);
}
json[k_rtpListener][k_ptTable] = json_ptTable;
return json;
} }
void Transport::HandleRequest(Channel::Request* request) void Transport::HandleRequest(Channel::Request* request)
...@@ -533,15 +557,6 @@ namespace RTC ...@@ -533,15 +557,6 @@ namespace RTC
return new RTC::Transport(this->listener, this->notifier, transportId, nullData, this); return new RTC::Transport(this->listener, this->notifier, transportId, nullData, this);
} }
void Transport::AddRtpReceiver(RTC::RtpReceiver* rtpReceiver)
{
MS_TRACE();
// TODO: somebody should ensure that it is not a duplicated RtpReceiver.
this->rtpListener->AddRtpReceiver(rtpReceiver);
}
inline inline
void Transport::MayRunDTLSTransport() void Transport::MayRunDTLSTransport()
{ {
...@@ -597,6 +612,30 @@ namespace RTC ...@@ -597,6 +612,30 @@ namespace RTC
} }
} }
inline
void Transport::RemoveRtpReceiverFromRtpListener(RTC::RtpReceiver* rtpReceiver)
{
MS_TRACE();
for (auto it = this->rtpListener.ssrcTable.begin(); it != this->rtpListener.ssrcTable.end();)
{
if (it->second == rtpReceiver)
it = this->rtpListener.ssrcTable.erase(it);
else
it++;
}
// TODO: midTable
for (auto it = this->rtpListener.ptTable.begin(); it != this->rtpListener.ptTable.end();)
{
if (it->second == rtpReceiver)
it = this->rtpListener.ptTable.erase(it);
else
it++;
}
}
inline inline
void Transport::onPacketRecv(RTC::TransportTuple* tuple, const MS_BYTE* data, size_t len) void Transport::onPacketRecv(RTC::TransportTuple* tuple, const MS_BYTE* data, size_t len)
{ {
...@@ -946,4 +985,35 @@ namespace RTC ...@@ -946,4 +985,35 @@ namespace RTC
// TMP // TMP
MS_DEBUG("data: %s", std::string((char*)data, len).c_str()); MS_DEBUG("data: %s", std::string((char*)data, len).c_str());
} }
void Transport::onRtpListenerParameters(RTC::RtpReceiver* rtpReceiver, RTC::RtpParameters* rtpParameters)
{
MS_TRACE();
// First remove from the rtpListener all the entries pointing to the given rtpReceiver.
RemoveRtpReceiverFromRtpListener(rtpReceiver);
// Add entries into rtpListener.ssrcTable.
for (auto& encoding : rtpParameters->encodings)
{
if (encoding.ssrc)
this->rtpListener.ssrcTable[encoding.ssrc] = rtpReceiver;
}
// TODO: // Add entries into rtpListener.midTable.
// Add entries into rtpListener.ptTable.
for (auto& codec : rtpParameters->codecs)
{
this->rtpListener.ptTable[codec.payloadType] = rtpReceiver;
}
}
void Transport::onRtpReceiverClosed(RTC::RtpReceiver* rtpReceiver)
{
MS_TRACE();
// Remove from the rtpListener all the entries pointing to the given rtpReceiver.
RemoveRtpReceiverFromRtpListener(rtpReceiver);
}
} }
...@@ -21,35 +21,36 @@ namespace RTC ...@@ -21,35 +21,36 @@ namespace RTC
static const Json::StaticString v_udp("udp"); static const Json::StaticString v_udp("udp");
static const Json::StaticString v_tcp("tcp"); static const Json::StaticString v_tcp("tcp");
Json::Value data; Json::Value json;
Json::Value localTuple; Json::Value json_local;
Json::Value remoteTuple; Json::Value json_remote;
int ip_family; int ip_family;
std::string ip; std::string ip;
MS_PORT port; MS_PORT port;
Utils::IP::GetAddressInfo(this->GetLocalAddress(), &ip_family, ip, &port); Utils::IP::GetAddressInfo(this->GetLocalAddress(), &ip_family, ip, &port);
localTuple[k_ip] = ip; json_local[k_ip] = ip;
localTuple[k_port] = port; json_local[k_port] = port;
if (this->GetProtocol() == RTC::TransportTuple::Protocol::UDP) if (this->GetProtocol() == RTC::TransportTuple::Protocol::UDP)
localTuple[k_protocol] = v_udp; json_local[k_protocol] = v_udp;
else else
localTuple[k_protocol] = v_tcp; json_local[k_protocol] = v_tcp;
Utils::IP::GetAddressInfo(this->GetRemoteAddress(), &ip_family, ip, &port); Utils::IP::GetAddressInfo(this->GetRemoteAddress(), &ip_family, ip, &port);
remoteTuple[k_ip] = ip; json_remote[k_ip] = ip;
remoteTuple[k_port] = port; json_remote[k_port] = port;
if (this->GetProtocol() == RTC::TransportTuple::Protocol::UDP) if (this->GetProtocol() == RTC::TransportTuple::Protocol::UDP)
remoteTuple[k_protocol] = v_udp; json_remote[k_protocol] = v_udp;
else else
remoteTuple[k_protocol] = v_tcp; json_remote[k_protocol] = v_tcp;
data[k_local] = localTuple; json[k_local] = json_local;
data[k_remote] = remoteTuple; json[k_remote] = json_remote;
return data; return json;
} }
// TODO: TMP
void TransportTuple::Dump() void TransportTuple::Dump()
{ {
MS_TRACE(); MS_TRACE();
......
...@@ -177,14 +177,14 @@ void Settings::HandleRequest(Channel::Request* request) ...@@ -177,14 +177,14 @@ void Settings::HandleRequest(Channel::Request* request)
{ {
static const Json::StaticString k_logLevel("logLevel"); static const Json::StaticString k_logLevel("logLevel");
Json::Value jsonLogLevel = request->data[k_logLevel]; Json::Value json_logLevel = request->data[k_logLevel];
try try
{ {
// Update logLevel if requested. // Update logLevel if requested.
if (jsonLogLevel.isString()) if (json_logLevel.isString())
{ {
std::string logLevel = jsonLogLevel.asString(); std::string logLevel = json_logLevel.asString();
Settings::SetLogLevel(logLevel); Settings::SetLogLevel(logLevel);
} }
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论