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

Work in RtpReceiver.

上级 70bf6184
......@@ -47,7 +47,7 @@ class Peer extends EventEmitter
this._pc.oniceconnectionstatechange = () =>
{
if ([ 'closed', 'failed' ].indexOf(this._pc.iceConnectionState) !== -1)
if ([ 'closed', 'disconnected', 'failed' ].indexOf(this._pc.iceConnectionState) !== -1)
this.closeLocalStream();
};
......@@ -152,7 +152,7 @@ class Peer extends EventEmitter
// errback
(error) => { throw new Error(error); },
// options
{ offerToReceiveAudio: true, offerToReceiveVideo: true }
{ offerToReceiveAudio: 2, offerToReceiveVideo: 2 }
);
function sendRequest()
......
......@@ -33,6 +33,7 @@ namespace RTC
private:
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);
/* 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
void AddCodec(Json::Value& data);
void AddEncoding(Json::Value& data);
private:
public:
// TODO: not sure if 1 or 2 bytes or what...
std::string muxId;
std::vector<CodecParameters> codecs;
......
......@@ -13,10 +13,23 @@ namespace RTC
class RtpReceiver
{
public:
/**
* RTC::Peer is the Listener.
*/
class Listener
{
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:
......@@ -26,6 +39,9 @@ namespace RTC
void Close();
Json::Value toJson();
void HandleRequest(Channel::Request* request);
void SetRtpListener(RtpListener* rtpListener);
void SetRtpListenerForRtcp(RtpListener* rtcpListener);
void RemoveRtpListener(RtpListener* rtpListener);
public:
// Passed by argument.
......@@ -35,9 +51,35 @@ namespace RTC
// Passed by argument.
Listener* listener = nullptr;
Channel::Notifier* notifier = nullptr;
RtpListener* rtpListener = nullptr;
RtpListener* rtcpListener = nullptr;
// Allocated by this.
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
......@@ -13,11 +13,11 @@
#include "RTC/RTPPacket.h"
#include "RTC/RTCPPacket.h"
#include "RTC/RtpReceiver.h"
#include "RTC/RtpListener.h"
#include "Channel/Request.h"
#include "Channel/Notifier.h"
#include <string>
#include <vector>
#include <unordered_map>
#include <json/json.h>
namespace RTC
......@@ -27,7 +27,8 @@ namespace RTC
public RTC::TCPServer::Listener,
public RTC::TCPConnection::Listener,
public RTC::ICEServer::Listener,
public RTC::DTLSTransport::Listener
public RTC::DTLSTransport::Listener,
public RTC::RtpReceiver::RtpListener
{
public:
class Listener
......@@ -36,6 +37,17 @@ namespace RTC
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:
Transport(Listener* listener, Channel::Notifier* notifier, uint32_t transportId, Json::Value& data, Transport* rtpTransport = nullptr);
virtual ~Transport();
......@@ -46,10 +58,10 @@ namespace RTC
std::string& GetIceUsernameFragment();
std::string& GetIcePassword();
Transport* CreateAssociatedTransport(uint32_t transportId);
void AddRtpReceiver(RTC::RtpReceiver* rtpReceiver);
private:
void MayRunDTLSTransport();
void RemoveRtpReceiverFromRtpListener(RTC::RtpReceiver* rtpReceiver);
/* Private methods to unify UDP and TCP behavior. */
private:
......@@ -88,6 +100,10 @@ namespace RTC
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;
/* 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:
// Passed by argument.
uint32_t transportId;
......@@ -108,7 +124,6 @@ namespace RTC
std::vector<RTC::UDPSocket*> udpSockets;
std::vector<RTC::TCPServer*> tcpServers;
RTC::DTLSTransport* dtlsTransport = nullptr;
RTC::RtpListener* rtpListener = nullptr;
// Others.
bool allocated = false;
// Others (ICE).
......@@ -117,6 +132,8 @@ namespace RTC
// Others (DTLS).
bool remoteDtlsParametersGiven = false;
RTC::DTLSTransport::Role dtlsLocalRole = RTC::DTLSTransport::Role::AUTO;
// Others (RTP listener).
RtpListener rtpListener;
};
/* Inline methods. */
......
......@@ -93,16 +93,16 @@ RTC::Room* Loop::GetRoomFromRequest(Channel::Request* request, uint32_t* 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`");
// If given, fill 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())
{
RTC::Room* room = it->second;
......
......@@ -39,36 +39,36 @@ namespace RTC
static const Json::StaticString k_tcpType("tcpType");
static const Json::StaticString v_passive("passive");
Json::Value data;
Json::Value json;
data[k_foundation] = this->foundation;
data[k_priority] = this->priority;
data[k_ip] = this->ip;
data[k_port] = this->port;
json[k_foundation] = this->foundation;
json[k_priority] = this->priority;
json[k_ip] = this->ip;
json[k_port] = this->port;
switch (this->type)
{
case CandidateType::HOST:
data[k_type] = v_host;
json[k_type] = v_host;
break;
}
switch (this->protocol)
{
case Protocol::UDP:
data[k_protocol] = v_udp;
json[k_protocol] = v_udp;
break;
case Protocol::TCP:
data[k_protocol] = v_tcp;
json[k_protocol] = v_tcp;
switch (this->tcpType)
{
case TcpCandidateType::PASSIVE:
data[k_tcpType] = v_passive;
json[k_tcpType] = v_passive;
break;
}
break;
}
return data;
return json;
}
}
......@@ -26,18 +26,6 @@ namespace RTC
{
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.
for (auto it = this->rtpReceivers.begin(); it != this->rtpReceivers.end();)
{
......@@ -47,6 +35,17 @@ namespace RTC
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.
this->listener->onPeerClosed(this);
......@@ -160,6 +159,7 @@ namespace RTC
case Channel::Request::MethodId::peer_createAssociatedTransport:
{
RTC::Transport* transport;
RTC::Transport* rtpTransport;
uint32_t transportId;
try
......@@ -182,9 +182,9 @@ namespace RTC
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`");
......@@ -192,7 +192,7 @@ namespace RTC
return;
}
auto it = this->transports.find(jsonRtpTransportId.asUInt());
auto it = this->transports.find(json_rtpTransportId.asUInt());
if (it == this->transports.end())
{
MS_ERROR("RTP Transport does not exist");
......@@ -201,7 +201,7 @@ namespace RTC
return;
}
RTC::Transport* rtpTransport = it->second;
rtpTransport = it->second;
try
{
......@@ -227,11 +227,9 @@ namespace RTC
case Channel::Request::MethodId::peer_createRtpReceiver:
{
RTC::RtpReceiver* rtpReceiver;
uint32_t rtpReceiverId;
uint32_t transportId;
uint32_t rtcpTransportId;
RTC::Transport* transport = nullptr;
RTC::Transport* rtcpTransport = nullptr;
uint32_t rtpReceiverId;
try
{
......@@ -251,22 +249,17 @@ namespace RTC
return;
}
static const Json::StaticString k_transportId("transportId");
auto jsonTransportId = request->internal[k_transportId];
if (!jsonTransportId.isUInt())
try
{
MS_ERROR("Request has not numeric `internal.transportId`");
request->Reject("Request has not numeric `internal.transportId`");
transport = GetTransportFromRequest(request);
}
catch (const MediaSoupError &error)
{
request->Reject(error.what());
return;
}
transportId = jsonTransportId.asUInt();
auto it = this->transports.find(transportId);
if (it == this->transports.end())
if (!transport)
{
MS_ERROR("Transport does not exist");
......@@ -274,24 +267,17 @@ namespace RTC
return;
}
transport = it->second;
static const Json::StaticString k_rtcpTransportId("rtcpTransportId");
auto jsonRtcpTransportId = request->internal[k_rtcpTransportId];
if (!jsonRtcpTransportId.isUInt())
try
{
MS_ERROR("Request has not numeric `internal.rtcpTransportId`");
request->Reject("Request has not numeric `internal.rtcpTransportId`");
rtcpTransport = GetRtcpTransportFromRequest(request);
}
catch (const MediaSoupError &error)
{
request->Reject(error.what());
return;
}
rtcpTransportId = jsonRtcpTransportId.asUInt();
it = this->transports.find(rtcpTransportId);
if (it == this->transports.end())
if (!rtcpTransport)
{
MS_ERROR("RTCP Transport does not exist");
......@@ -299,8 +285,6 @@ namespace RTC
return;
}
rtcpTransport = it->second;
// Create a RtpReceiver instance.
rtpReceiver = new RTC::RtpReceiver(this, this->notifier, rtpReceiverId);
......@@ -308,10 +292,10 @@ namespace RTC
MS_DEBUG("RtpReceiver created [rtpReceiverId:%" PRIu32 "]", rtpReceiverId);
// Provide the Transports with the RtpReceiver.
transport->AddRtpReceiver(rtpReceiver);
if (rtcpTransportId != transportId)
rtcpTransport->AddRtpReceiver(rtpReceiver);
// Set the transports as RTP and RTCP listeners.
rtpReceiver->SetRtpListener(transport);
if (rtcpTransport != transport)
rtpReceiver->SetRtpListenerForRtcp(rtcpTransport);
request->Accept();
......@@ -391,15 +375,15 @@ namespace RTC
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`");
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())
{
RTC::Transport* transport = it->second;
......@@ -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)
{
MS_TRACE();
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`");
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())
{
RTC::RtpReceiver* rtpReceiver = it->second;
......@@ -443,6 +454,15 @@ namespace RTC
{
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);
}
......
......@@ -197,16 +197,16 @@ namespace RTC
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");
// If given, fill 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())
{
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
{
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]);
}
......@@ -35,7 +35,7 @@ namespace RTC
{
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]);
}
}
......@@ -57,13 +57,13 @@ namespace RTC
static const Json::StaticString k_encodings("encodings");
static const Json::StaticString k_ssrc("ssrc");
Json::Value data;
Json::Value json;
// Add `muxId`.
data[k_muxId] = this->muxId;
json[k_muxId] = this->muxId;
// Add `codecs`.
data[k_codecs] = Json::arrayValue;
json[k_codecs] = Json::arrayValue;
for (auto it = this->codecs.begin(); it != this->codecs.end(); ++it)
{
CodecParameters* codec = std::addressof(*it);
......@@ -76,11 +76,11 @@ namespace RTC
if (codec->clockRate)
json_codec[k_clockRate] = (Json::UInt)codec->clockRate;
data[k_codecs].append(json_codec);
json[k_codecs].append(json_codec);
}
// Add `encodings`.
data[k_encodings] = Json::arrayValue;
json[k_encodings] = Json::arrayValue;
for (auto it = this->encodings.begin(); it != this->encodings.end(); ++it)
{
EncodingParameters* encoding = std::addressof(*it);
......@@ -88,10 +88,10 @@ namespace RTC
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)
......@@ -112,6 +112,9 @@ namespace RTC
codec.name = data[k_name].asString();
if (codec.name.empty())
MS_THROW_ERROR("empty `name`");
if (!data[k_payloadType].isUInt())
MS_THROW_ERROR("missing `payloadType`");
......
......@@ -31,8 +31,12 @@ namespace RTC
// Notify.
this->notifier->Emit(this->rtpReceiverId, "close");
// Notify the listener.
// Notify the listener and also the rtpListener and rtcpListener.
this->listener->onRtpReceiverClosed(this);
if (this->rtpListener)
this->rtpListener->onRtpReceiverClosed(this);
if (this->rtcpListener)
this->rtcpListener->onRtpReceiverClosed(this);
delete this;
}
......@@ -43,14 +47,14 @@ namespace RTC
static const Json::StaticString k_rtpParameters("rtpParameters");
Json::Value data(Json::objectValue);
// TODO
Json::Value json;
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)
......@@ -82,12 +86,8 @@ namespace RTC
case Channel::Request::MethodId::rtpReceiver_receive:
{
// If rtpParameteres was already set, delete it.
if (this->rtpParameters)
{
delete this->rtpParameters;
this->rtpParameters = nullptr;
}
// Keep a reference to the previous rtpParameters.
auto previousRtpParameters = this->rtpParameters;
try
{
......@@ -99,8 +99,17 @@ namespace RTC
return;
}
// Free the previous rtpParameters.
delete previousRtpParameters;
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;
}
......
......@@ -213,9 +213,6 @@ namespace RTC
// Create a DTLS agent.
this->dtlsTransport = new RTC::DTLSTransport(this);
// Create a RtpListener.
this->rtpListener = new RTC::RtpListener();
// Hack to avoid that Close() above attempts to delete this.
this->allocated = true;
}
......@@ -229,9 +226,6 @@ namespace RTC
{
MS_TRACE();
if (this->rtpListener)
this->rtpListener->Close();
// if (this->srtpRecvSession)
// this->srtpRecvSession->Close();
......@@ -297,64 +291,68 @@ namespace RTC
static const Json::StaticString v_connecting("connecting");
static const Json::StaticString v_closed("closed");
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").
data[k_iceRole] = v_controlled;
json[k_iceRole] = v_controlled;
// Add `iceComponent`.
if (this->iceServer->GetComponent() == ICEServer::IceComponent::RTP)
data[k_iceComponent] = v_RTP;
json[k_iceComponent] = v_RTP;
else
data[k_iceComponent] = v_RTCP;
json[k_iceComponent] = v_RTCP;
// Add `iceLocalParameters`.
data[k_iceLocalParameters][k_usernameFragment] = this->iceServer->GetUsernameFragment();
data[k_iceLocalParameters][k_password] = this->iceServer->GetPassword();
json[k_iceLocalParameters][k_usernameFragment] = this->iceServer->GetUsernameFragment();
json[k_iceLocalParameters][k_password] = this->iceServer->GetPassword();
// Add `iceLocalCandidates`.
data[k_iceLocalCandidates] = Json::arrayValue;
json[k_iceLocalCandidates] = Json::arrayValue;
for (auto iceCandidate : this->iceLocalCandidates)
{
data[k_iceLocalCandidates].append(iceCandidate.toJson());
json[k_iceLocalCandidates].append(iceCandidate.toJson());
}
// Add `iceSelectedTuple`.
if (this->selectedTuple)
data[k_iceSelectedTuple] = this->selectedTuple->toJson();
json[k_iceSelectedTuple] = this->selectedTuple->toJson();
// Add `iceState`.
switch (this->iceServer->GetState())
{
case RTC::ICEServer::IceState::NEW:
data[k_iceState] = v_new;
json[k_iceState] = v_new;
break;
case RTC::ICEServer::IceState::CONNECTED:
data[k_iceState] = v_connected;
json[k_iceState] = v_connected;
break;
case RTC::ICEServer::IceState::COMPLETED:
data[k_iceState] = v_completed;
json[k_iceState] = v_completed;
break;
case RTC::ICEServer::IceState::DISCONNECTED:
data[k_iceState] = v_disconnected;
json[k_iceState] = v_disconnected;
break;
}
// Add `dtlsLocalParameters.fingerprints`.
data[k_dtlsLocalParameters][k_fingerprints] = RTC::DTLSTransport::GetLocalFingerprints();
json[k_dtlsLocalParameters][k_fingerprints] = RTC::DTLSTransport::GetLocalFingerprints();
// Add `dtlsLocalParameters.role`.
switch (this->dtlsLocalRole)
{
case RTC::DTLSTransport::Role::AUTO:
data[k_dtlsLocalParameters][k_role] = v_auto;
json[k_dtlsLocalParameters][k_role] = v_auto;
break;
case RTC::DTLSTransport::Role::CLIENT:
data[k_dtlsLocalParameters][k_role] = v_client;
json[k_dtlsLocalParameters][k_role] = v_client;
break;
case RTC::DTLSTransport::Role::SERVER:
data[k_dtlsLocalParameters][k_role] = v_server;
json[k_dtlsLocalParameters][k_role] = v_server;
break;
default:
MS_ABORT("invalid local DTLS role");
......@@ -364,23 +362,49 @@ namespace RTC
switch (this->dtlsTransport->GetState())
{
case DTLSTransport::DtlsState::NEW:
data[k_dtlsState] = v_new;
json[k_dtlsState] = v_new;
break;
case DTLSTransport::DtlsState::CONNECTING:
data[k_dtlsState] = v_connecting;
json[k_dtlsState] = v_connecting;
break;
case DTLSTransport::DtlsState::CONNECTED:
data[k_dtlsState] = v_connected;
json[k_dtlsState] = v_connected;
break;
case DTLSTransport::DtlsState::FAILED:
data[k_dtlsState] = v_failed;
json[k_dtlsState] = v_failed;
break;
case DTLSTransport::DtlsState::CLOSED:
data[k_dtlsState] = v_closed;
json[k_dtlsState] = v_closed;
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)
......@@ -533,15 +557,6 @@ namespace RTC
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
void Transport::MayRunDTLSTransport()
{
......@@ -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
void Transport::onPacketRecv(RTC::TransportTuple* tuple, const MS_BYTE* data, size_t len)
{
......@@ -946,4 +985,35 @@ namespace RTC
// TMP
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
static const Json::StaticString v_udp("udp");
static const Json::StaticString v_tcp("tcp");
Json::Value data;
Json::Value localTuple;
Json::Value remoteTuple;
Json::Value json;
Json::Value json_local;
Json::Value json_remote;
int ip_family;
std::string ip;
MS_PORT port;
Utils::IP::GetAddressInfo(this->GetLocalAddress(), &ip_family, ip, &port);
localTuple[k_ip] = ip;
localTuple[k_port] = port;
json_local[k_ip] = ip;
json_local[k_port] = port;
if (this->GetProtocol() == RTC::TransportTuple::Protocol::UDP)
localTuple[k_protocol] = v_udp;
json_local[k_protocol] = v_udp;
else
localTuple[k_protocol] = v_tcp;
json_local[k_protocol] = v_tcp;
Utils::IP::GetAddressInfo(this->GetRemoteAddress(), &ip_family, ip, &port);
remoteTuple[k_ip] = ip;
remoteTuple[k_port] = port;
json_remote[k_ip] = ip;
json_remote[k_port] = port;
if (this->GetProtocol() == RTC::TransportTuple::Protocol::UDP)
remoteTuple[k_protocol] = v_udp;
json_remote[k_protocol] = v_udp;
else
remoteTuple[k_protocol] = v_tcp;
json_remote[k_protocol] = v_tcp;
data[k_local] = localTuple;
data[k_remote] = remoteTuple;
json[k_local] = json_local;
json[k_remote] = json_remote;
return data;
return json;
}
// TODO: TMP
void TransportTuple::Dump()
{
MS_TRACE();
......
......@@ -177,14 +177,14 @@ void Settings::HandleRequest(Channel::Request* request)
{
static const Json::StaticString k_logLevel("logLevel");
Json::Value jsonLogLevel = request->data[k_logLevel];
Json::Value json_logLevel = request->data[k_logLevel];
try
{
// 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);
}
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论