提交 1db95262 authored 作者: Iñaki Baz Castillo's avatar Iñaki Baz Castillo

More work in RtpSender & RtpReceiver

上级 75d9c4e6
......@@ -11,7 +11,7 @@ const RtpSender = require('./RtpSender');
class Peer extends EventEmitter
{
constructor(internal, channel)
constructor(internal, channel, extra)
{
logger.debug('constructor() [internal:%o]', internal);
......@@ -23,6 +23,10 @@ class Peer extends EventEmitter
// - .peerId
this._internal = internal;
// Store internal extra stuff
// - .getPeer()
this._extra = extra;
// Channel instance
this._channel = channel;
......@@ -39,13 +43,53 @@ class Peer extends EventEmitter
this._closed = false;
// Subscribe to notifications
this._channel.on(this._internal.peerId, (event) =>
this._channel.on(this._internal.peerId, (event, data) =>
{
switch (event)
{
case 'close':
{
this.close(undefined, true);
break;
}
case 'newrtpsender':
{
let receiverPeer = this._extra.getPeer(data.peerName);
if (!receiverPeer)
{
logger.error('"newrtpsender" event for a non existing receiver Peer [peerName:"%s"]', data.peerName);
return;
}
// senderPeer.RtpSender(data.sender.rtpSenderId, data.sender.rtpParameters, receiverPeer);
let internal =
{
roomId : this._internal.roomId,
peerId : this._internal.peerId,
rtpSenderId : data.rtpSenderId,
transportId : null
};
let data2 =
{
rtpParameters : data.rtpParameters
};
// Create a RtpSender instance
let rtpSender = new RtpSender(internal, data2, this._channel);
// Store the RtpSender instance and remove it when closed
this._rtpSenders.add(rtpSender);
rtpSender.on('close', () => this._rtpSenders.delete(rtpSender));
this.emit('newrtpsender', rtpSender, receiverPeer);
break;
}
default:
logger.error('ignoring unknown event "%s"', event);
......
......@@ -26,45 +26,19 @@ class Room extends EventEmitter
// Map of Peer instances indexed by `peerName`
this._peers = new Map();
// Map of Peer instances indexed by `peerId`
this._peersById = new Map();
// Closed flag
this._closed = false;
// Subscribe to notifications
this._channel.on(this._internal.roomId, (event, data) =>
this._channel.on(this._internal.roomId, (event) =>
{
switch (event)
{
case 'newrtpsender':
{
let senderPeer = this._peersById.get(data.sender.peerId);
if (!senderPeer)
{
logger.error('"newrtpsender" event for a non existing sender Peer [peerId:%s]', data.sender.peerId);
return;
}
let receiverPeer = this._peersById.get(data.receiver.peerId);
if (!receiverPeer)
{
logger.error('"newrtpsender" event for a non existing receiver Peer [peerId:%s]', data.receiver.peerId);
return;
}
// Request the sender Peerto create a new RtpSender
senderPeer.RtpSender(data.sender.rtpSenderId, data.sender.rtpParameters, receiverPeer);
break;
}
case 'close':
{
this.close(undefined, true);
break;
}
default:
logger.error('ignoring unknown event "%s"', event);
......@@ -171,17 +145,19 @@ class Room extends EventEmitter
peerId : peerId,
peerName : peerName
};
let extra =
{
getPeer : this.getPeer.bind(this)
};
// Create a Peer instance
let peer = new Peer(internal, this._channel);
let peer = new Peer(internal, this._channel, extra);
// Store the Peer instance and remove it when closed
this._peers.set(peerName, peer);
this._peersById.set(peerId, peer);
peer.on('close', () =>
{
this._peers.delete(peerName);
this._peersById.delete(peerId);
});
this._channel.request('room.createPeer', internal)
......
......@@ -37,8 +37,10 @@ class RtpReceiver extends EventEmitter
switch (event)
{
case 'close':
{
this.close(undefined, true);
break;
}
default:
logger.error('ignoring unknown event "%s"', event);
......
......@@ -37,14 +37,18 @@ class RtpSender extends EventEmitter
{
switch (event)
{
case 'close':
{
this.close();
break;
}
case 'updateparameters':
{
this._data.rtpParameters = data.rtpParameters;
this.emit('updateparameters', data.rtpParameters);
break;
case 'close':
this.close();
break;
}
default:
logger.error('ignoring unknown event "%s"', event);
......
......@@ -65,6 +65,12 @@ class Transport extends EventEmitter
{
switch (event)
{
case 'close':
{
this.close(undefined, true);
break;
}
case 'iceselectedtuplechange':
{
this._data.iceSelectedTuple = data.iceSelectedTuple;
......@@ -89,10 +95,6 @@ class Transport extends EventEmitter
break;
}
case 'close':
this.close(undefined, true);
break;
default:
logger.error('ignoring unknown event "%s"', event);
}
......
......@@ -37,7 +37,12 @@ namespace RTC
Json::Value toJson();
void HandleRequest(Channel::Request* request);
std::vector<RTC::RtpReceiver*> GetRtpReceivers();
void AddRtpSender(RTC::RtpSender* rtpSender);
/**
* Add a new RtpSender to the Peer.
* @param rtpSender Instance of RtpSender.
* @param peerName Name of the receiver Peer.
*/
void AddRtpSender(RTC::RtpSender* rtpSender, std::string& peerName);
private:
RTC::Transport* GetTransportFromRequest(Channel::Request* request, uint32_t* transportId = nullptr);
......
......@@ -37,6 +37,7 @@ namespace RTC
RTC::RtpListener* GetRtpListener();
void RemoveRtpListener(RTC::RtpListener* rtpListener);
RTC::RtpParameters* GetRtpParameters();
void NotifyParameters();
public:
// Passed by argument.
......
......@@ -73,6 +73,7 @@ namespace RTC
{
MS_TRACE();
static const Json::StaticString k_peerId("peerId");
static const Json::StaticString k_peerName("peerName");
static const Json::StaticString k_transports("transports");
static const Json::StaticString k_rtpReceivers("rtpReceivers");
......@@ -83,6 +84,8 @@ namespace RTC
Json::Value json_rtpReceivers(Json::objectValue);
Json::Value json_rtpSenders(Json::objectValue);
json[k_peerId] = (Json::UInt)this->peerId;
json[k_peerName] = this->peerName;
for (auto& kv : this->transports)
......@@ -338,15 +341,31 @@ namespace RTC
}
}
void Peer::AddRtpSender(RTC::RtpSender* rtpSender)
void Peer::AddRtpSender(RTC::RtpSender* rtpSender, std::string& peerName)
{
MS_TRACE();
// TODO: let's see
static const Json::StaticString k_rtpSenderId("rtpSenderId");
static const Json::StaticString k_rtpParameters("rtpParameters");
static const Json::StaticString k_peerName("peerName");
MS_ASSERT(this->rtpSenders.find(rtpSender->rtpSenderId) == this->rtpSenders.end(),
"given RtpSender already exists in this Peer");
MS_ASSERT(rtpSender->GetRtpParameters(), "given RtpSender does not have RtpParameters");
// Store it.
this->rtpSenders[rtpSender->rtpSenderId] = rtpSender;
// Notify.
Json::Value event_data(Json::objectValue);
event_data[k_rtpSenderId] = (Json::UInt)rtpSender->rtpSenderId;
event_data[k_rtpParameters] = rtpSender->GetRtpParameters()->toJson();
event_data[k_peerName] = peerName;
this->notifier->Emit(this->peerId, "newrtpsender", event_data);
}
RTC::Transport* Peer::GetTransportFromRequest(Channel::Request* request, uint32_t* transportId)
......
......@@ -57,11 +57,14 @@ namespace RTC
{
MS_TRACE();
static const Json::StaticString k_roomId("roomId");
static const Json::StaticString k_peers("peers");
Json::Value json(Json::objectValue);
Json::Value json_peers(Json::objectValue);
json[k_roomId] = (Json::UInt)this->roomId;
for (auto& kv : this->peers)
{
RTC::Peer* peer = kv.second;
......@@ -146,14 +149,24 @@ namespace RTC
return;
}
// Get all the RtpReceivers of the others Peers in the Room and create
// RtpSenders for this new Peer.
// Store the new Peer.
this->peers[peerId] = peer;
MS_DEBUG("Peer created [peerId:%u, peerName:'%s']", peerId, peerName.c_str());
request->Accept();
// Get all the ready RtpReceivers of the others Peers in the Room and
// create RtpSenders for this new Peer.
for (auto& kv : this->peers)
{
RTC::Peer* other_peer = kv.second;
RTC::Peer* receiver_peer = kv.second;
for (auto rtpReceiver : other_peer->GetRtpReceivers())
for (auto rtpReceiver : receiver_peer->GetRtpReceivers())
{
// Skip if the RtpReceiver has not parameters.
if (!rtpReceiver->GetRtpParameters())
continue;
uint32_t rtpSenderId = Utils::Crypto::GetRandomUInt(10000000, 99999999);
RTC::RtpSender* rtpSender = new RTC::RtpSender(peer, this->notifier, rtpSenderId);
......@@ -163,16 +176,10 @@ namespace RTC
rtpSender->Send(rtpSenderParameters);
// Attach the RtpSender to peer.
peer->AddRtpSender(rtpSender);
peer->AddRtpSender(rtpSender, receiver_peer->peerName);
}
}
// Store the new Peer.
this->peers[peerId] = peer;
MS_DEBUG("Peer created [peerId:%u, peerName:'%s']", peerId, peerName.c_str());
request->Accept();
break;
}
......@@ -261,12 +268,6 @@ namespace RTC
{
MS_TRACE();
static const Json::StaticString k_sender("sender");
static const Json::StaticString k_receiver("receiver");
static const Json::StaticString k_peerId("peerId");
static const Json::StaticString k_rtpSenderId("rtpSenderId");
static const Json::StaticString k_rtpParameters("rtpParameters");
MS_ASSERT(rtpReceiver->GetRtpParameters(), "rtpReceiver->GetRtpParameters() returns no RtpParameters");
// The RtpReceiver may already be set previously.
......@@ -278,15 +279,15 @@ namespace RTC
{
for (auto& kv : this->peers)
{
RTC::Peer* other_peer = kv.second;
RTC::Peer* sender_peer = kv.second;
// Skip receiver peer.
if (other_peer == peer)
if (sender_peer == peer)
continue;
// Create a RtpSender for the other Peer.
uint32_t rtpSenderId = Utils::Crypto::GetRandomUInt(10000000, 99999999);
RTC::RtpSender* rtpSender = new RTC::RtpSender(other_peer, this->notifier, rtpSenderId);
RTC::RtpSender* rtpSender = new RTC::RtpSender(sender_peer, this->notifier, rtpSenderId);
// Store into the map.
this->mapReceiverSenders[rtpReceiver].insert(rtpSender);
......@@ -297,25 +298,8 @@ namespace RTC
// Assign cloned parameters to the RtpSender.
rtpSender->Send(rtpSenderParameters);
// Attach the RtpSender to other_peer.
other_peer->AddRtpSender(rtpSender);
// Notify.
Json::Value event_data(Json::objectValue);
Json::Value event_sender(Json::objectValue);
Json::Value event_receiver(Json::objectValue);
event_sender[k_peerId] = other_peer->peerId;
event_sender[k_rtpSenderId] = rtpSender->rtpSenderId;
event_sender[k_rtpParameters] = rtpSender->GetRtpParameters()->toJson();
event_receiver[k_peerId] = peer->peerId;
event_data[k_sender] = event_sender;
event_data[k_receiver] = event_receiver;
this->notifier->Emit(this->roomId, "newrtpsender", event_data);
// Attach the RtpSender to sender_peer.
sender_peer->AddRtpSender(rtpSender, peer->peerName);
}
}
// If this is not a new RtpReceiver let's retrieve its updated parameters
......@@ -330,11 +314,8 @@ namespace RTC
// Assign cloned parameters to the RtpSender.
rtpSender->Send(rtpSenderParameters);
// Notify.
Json::Value event_data = rtpSender->GetRtpParameters()->toJson();
this->notifier->Emit(rtpSender->rtpSenderId, "updateparameters", event_data);
// Notify updated parameters.
rtpSender->NotifyParameters();
}
}
}
......
......@@ -47,10 +47,13 @@ namespace RTC
MS_TRACE();
static Json::Value null_data(Json::nullValue);
static const Json::StaticString k_rtpReceiverId("rtpReceiverId");
static const Json::StaticString k_rtpParameters("rtpParameters");
Json::Value json(Json::objectValue);
json[k_rtpReceiverId] = (Json::UInt)this->rtpReceiverId;
if (this->rtpParameters)
json[k_rtpParameters] = this->rtpParameters->toJson();
else
......
......@@ -47,10 +47,13 @@ namespace RTC
MS_TRACE();
static Json::Value null_data(Json::nullValue);
static const Json::StaticString k_rtpSenderId("rtpSenderId");
static const Json::StaticString k_rtpParameters("rtpParameters");
Json::Value json(Json::objectValue);
json[k_rtpSenderId] = (Json::UInt)this->rtpSenderId;
if (this->rtpParameters)
json[k_rtpParameters] = this->rtpParameters->toJson();
else
......@@ -93,4 +96,15 @@ namespace RTC
this->rtpParameters = rtpParameters;
}
void RtpSender::NotifyParameters()
{
MS_TRACE();
MS_ASSERT(this->rtpParameters, "no RtpParameters set");
Json::Value event_data = this->rtpParameters->toJson();
this->notifier->Emit(this->rtpSenderId, "updateparameters", event_data);
}
}
......@@ -250,6 +250,7 @@ namespace RTC
{
MS_TRACE();
static const Json::StaticString k_transportId("transportId");
static const Json::StaticString k_iceRole("iceRole");
static const Json::StaticString v_controlled("controlled");
static const Json::StaticString k_iceLocalParameters("iceLocalParameters");
......@@ -276,6 +277,8 @@ namespace RTC
Json::Value json(Json::objectValue);
json[k_transportId] = (Json::UInt)this->transportId;
// Add `iceRole` (we are always "controlled").
json[k_iceRole] = v_controlled;
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论