提交 2642f782 authored 作者: Iñaki Baz Castillo's avatar Iñaki Baz Castillo

Start a big refactoring

上级 c2c3efdf
# BUGS
* `CREATE_PEER` is emitting `newconsumer` but does not emit `newpeer`. Who must do that?
* Move the RTCP timer from Peer to Transport.
* worker `Producer` / `Consumer` may need various listeners, so `Add/RemoveListener(listener)` is required.
* `then()` of Promises is async so if we create an instance (Peer, Producer, Transport, etc) within the `channel.request().then(....)` and an event happens for that instance immediately, such an event may be lost.
- The solution is creating the instance before calling `channel.request()`.
......
......@@ -19,16 +19,13 @@ namespace Channel
{
WORKER_DUMP = 1,
WORKER_UPDATE_SETTINGS,
WORKER_CREATE_ROOM,
ROOM_CLOSE,
ROOM_DUMP,
ROOM_CREATE_PEER,
ROOM_SET_AUDIO_LEVELS_EVENT,
PEER_CLOSE,
PEER_DUMP,
PEER_SET_CAPABILITIES,
PEER_CREATE_TRANSPORT,
PEER_CREATE_PRODUCER,
WORKER_CREATE_ROUTER,
ROUTER_CLOSE,
ROUTER_DUMP,
ROUTER_CREATE_TRANSPORT,
ROUTER_CREATE_PRODUCER,
ROUTER_CREATE_CONSUMER,
ROUTER_SET_AUDIO_LEVELS_EVENT,
TRANSPORT_CLOSE,
TRANSPORT_DUMP,
TRANSPORT_SET_REMOTE_DTLS_PARAMETERS,
......@@ -41,6 +38,7 @@ namespace Channel
PRODUCER_RESUME,
PRODUCER_SET_RTP_RAW_EVENT,
PRODUCER_SET_RTP_OBJECT_EVENT,
CONSUMER_CLOSE,
CONSUMER_DUMP,
CONSUMER_ENABLE,
CONSUMER_PAUSE,
......
......@@ -43,7 +43,7 @@
*
* Logs if the current source file defines the MS_LOG_DEV macro.
* Example:
* MS_DEBUG_DEV("Room closed [roomId:%" PRIu32 "]", roomId);
* MS_DEBUG_DEV("Producer closed [producerId:%" PRIu32 "]", producerId);
*
* MS_DUMP(...)
*
......
......@@ -5,13 +5,13 @@
#include "Channel/Notifier.hpp"
#include "Channel/Request.hpp"
#include "Channel/UnixStreamSocket.hpp"
#include "RTC/Room.hpp"
#include "RTC/Router.hpp"
#include "handles/SignalsHandler.hpp"
#include <unordered_map>
class Loop : public SignalsHandler::Listener,
public Channel::UnixStreamSocket::Listener,
public RTC::Room::Listener
public RTC::Router::Listener
{
public:
explicit Loop(Channel::UnixStreamSocket* channel);
......@@ -19,7 +19,7 @@ public:
private:
void Close();
RTC::Room* GetRoomFromRequest(Channel::Request* request, uint32_t* roomId = nullptr);
RTC::Router* GetRouterFromRequest(Channel::Request* request, uint32_t* routerId = nullptr);
/* Methods inherited from SignalsHandler::Listener. */
public:
......@@ -30,9 +30,9 @@ public:
void OnChannelRequest(Channel::UnixStreamSocket* channel, Channel::Request* request) override;
void OnChannelUnixStreamSocketRemotelyClosed(Channel::UnixStreamSocket* channel) override;
/* Methods inherited from RTC::Room::Listener. */
/* Methods inherited from RTC::Router::Listener. */
public:
void OnRoomClosed(RTC::Room* room) override;
void OnRouterClosed(RTC::Router* router) override;
private:
// Passed by argument.
......@@ -42,7 +42,7 @@ private:
SignalsHandler* signalsHandler{ nullptr };
// Others.
bool closed{ false };
std::unordered_map<uint32_t, RTC::Room*> rooms;
std::unordered_map<uint32_t, RTC::Router*> routers;
};
#endif
......@@ -86,7 +86,7 @@ namespace RTC
/* Pure virtual methods inherited from RTC::Producer::Listener. */
public:
void OnProducerClosed(const RTC::Producer* producer) override;
void OnProducerClosed(RTC::Producer* producer) override;
void OnProducerRtpParameters(RTC::Producer* producer) override;
void OnProducerPaused(RTC::Producer* producer) override;
void OnProducerResumed(RTC::Producer* producer) override;
......
......@@ -29,7 +29,7 @@ namespace RTC
class Listener
{
public:
virtual void OnProducerClosed(const RTC::Producer* producer) = 0;
virtual void OnProducerClosed(RTC::Producer* producer) = 0;
virtual void OnProducerRtpParameters(RTC::Producer* producer) = 0;
virtual void OnProducerPaused(RTC::Producer* producer) = 0;
virtual void OnProducerResumed(RTC::Producer* producer) = 0;
......
#ifndef MS_RTC_ROOM_HPP
#define MS_RTC_ROOM_HPP
#ifndef MS_RTC_ROUTER_HPP
#define MS_RTC_ROUTER_HPP
#include "common.hpp"
#include "Channel/Notifier.hpp"
#include "Channel/Request.hpp"
#include "RTC/Consumer.hpp"
#include "RTC/Peer.hpp"
#include "RTC/Transport.hpp"
#include "RTC/Producer.hpp"
#include "RTC/RTCP/Feedback.hpp"
#include "RTC/RTCP/ReceiverReport.hpp"
#include "RTC/RTCP/Sdes.hpp"
#include "RTC/RTCP/SenderReport.hpp"
#include "RTC/RtpDictionaries.hpp"
#include "RTC/Consumer.hpp"
#include "RTC/RtpPacket.hpp"
// TODO: Let's see.
// #include "RTC/RTCP/Feedback.hpp"
// #include "RTC/RTCP/ReceiverReport.hpp"
// #include "RTC/RTCP/Sdes.hpp"
// #include "RTC/RTCP/SenderReport.hpp"
#include "handles/Timer.hpp"
#include <json/json.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
namespace RTC
{
class Room : public RTC::Peer::Listener, public Timer::Listener
class Router : public RTC::Transport::Listener,
public RTC::Producer::Listener,
public RTC::Consumer::Listener,
public Timer::Listener
{
public:
class Listener
{
public:
virtual void OnRoomClosed(RTC::Room* room) = 0;
virtual void OnRouterClosed(RTC::Router* router) = 0;
};
private:
struct AudioLevelContainer
{
int16_t numdBovs{ 0 };
int16_t sumdBovs{ 0 };
};
public:
Room(Listener* listener, Channel::Notifier* notifier, uint32_t roomId);
Router(Listener* listener, Channel::Notifier* notifier, uint32_t routerId);
private:
virtual ~Room();
virtual ~Router();
public:
void Destroy();
......@@ -42,28 +51,24 @@ namespace RTC
void HandleRequest(Channel::Request* request);
private:
RTC::Peer* GetPeerFromRequest(Channel::Request* request, uint32_t* peerId = nullptr) const;
void AddConsumerForProducer(RTC::Peer* consumerPeer, const RTC::Producer* producer);
RTC::Transport* GetTransportFromRequest(Channel::Request* request, uint32_t* transportId = nullptr) const;
RTC::Producer* GetProducerFromRequest(Channel::Request* request, uint32_t* producerId = nullptr) const;
RTC::Consumer* GetConsumerFromRequest(Channel::Request* request, uint32_t* consumerId = nullptr) const;
/* Pure virtual methods inherited from RTC::Transport::Listener. */
public:
void OnTransportClosed(RTC::Transport* transport) override;
// TODO: RTCP listener to route RTCP feedback from Consumer to its Producer.
/* Pure virtual methods inherited from RTC::Producer::Listener. */
public:
void OnProducerClosed(RTC::Producer* producer) override;
void OnProducerRtpPacket(RTC::Producer* producer, RTC::RtpPacket* packet) override;
/* Pure virtual methods inherited from RTC::Peer::Listener. */
/* Pure virtual methods inherited from RTC::Consumer::Listener. */
public:
void OnPeerClosed(const RTC::Peer* peer) override;
void OnPeerProducerClosed(const RTC::Peer* peer, const RTC::Producer* producer) override;
void OnPeerProducerRtpParameters(const RTC::Peer* peer, RTC::Producer* producer) override;
void OnPeerProducerPaused(const RTC::Peer* peer, const RTC::Producer* producer) override;
void OnPeerProducerResumed(const RTC::Peer* peer, const RTC::Producer* producer) override;
void OnPeerProducerRtpPacket(
const RTC::Peer* peer, RTC::Producer* producer, RTC::RtpPacket* packet) override;
void OnPeerProducerRtcpSenderReport(
const RTC::Peer* peer, RTC::Producer* producer, RTC::RTCP::SenderReport* report) override;
void OnPeerConsumerClosed(const RTC::Peer* peer, RTC::Consumer* consumer) override;
void OnPeerConsumerRtcpReceiverReport(
const RTC::Peer* peer, RTC::Consumer* consumer, RTC::RTCP::ReceiverReport* report) override;
void OnPeerConsumerRtcpFeedback(
const RTC::Peer* peer, RTC::Consumer* consumer, RTC::RTCP::FeedbackPsPacket* packet) override;
void OnPeerConsumerRtcpFeedback(
const RTC::Peer* peer, RTC::Consumer* consumer, RTC::RTCP::FeedbackRtpPacket* packet) override;
void OnPeerConsumerFullFrameRequired(RTC::Peer* peer, RTC::Consumer* consumer) override;
void OnConsumerClosed(RTC::Consumer* consumer) override;
void OnConsumerFullFrameRequired(RTC::Consumer* consumer) override;
/* Pure virtual methods inherited from Timer::Listener. */
public:
......@@ -71,7 +76,7 @@ namespace RTC
public:
// Passed by argument.
uint32_t roomId{ 0 };
uint32_t routerId{ 0 };
private:
// Passed by argument.
......@@ -80,10 +85,12 @@ namespace RTC
// Allocated by this.
Timer* audioLevelsTimer{ nullptr };
// Others.
std::unordered_map<uint32_t, RTC::Peer*> peers;
std::unordered_map<uint32_t, RTC::Transport*> transports;
std::unordered_map<uint32_t, RTC::Producer*> producers;
std::unordered_map<uint32_t, RTC::Consumer*> consumers;
std::unordered_map<const RTC::Producer*, std::unordered_set<RTC::Consumer*>> mapProducerConsumers;
std::unordered_map<const RTC::Consumer*, const RTC::Producer*> mapConsumerProducer;
std::unordered_map<RTC::Producer*, std::vector<int8_t>> mapProducerAudioLevels;
std::unordered_map<RTC::Producer*, struct AudioLevelContainer> mapProducerAudioLevelContainer;
bool audioLevelsEventEnabled{ false };
};
} // namespace RTC
......
......@@ -26,9 +26,8 @@
'src/RTC/IceCandidate.cpp',
'src/RTC/IceServer.cpp',
'src/RTC/NackGenerator.cpp',
'src/RTC/Peer.cpp',
'src/RTC/Producer.cpp',
'src/RTC/Room.cpp',
'src/RTC/Router.cpp',
'src/RTC/RtpListener.cpp',
'src/RTC/RtpPacket.cpp',
'src/RTC/RtpStream.cpp',
......@@ -112,9 +111,8 @@
'include/RTC/IceServer.hpp',
'include/RTC/NackGenerator.hpp',
'include/RTC/Parameters.hpp',
'include/RTC/Peer.hpp',
'include/RTC/Producer.hpp',
'include/RTC/Room.hpp',
'include/RTC/Router.hpp',
'include/RTC/RtpDictionaries.hpp',
'include/RTC/RtpListener.hpp',
'include/RTC/RtpPacket.hpp',
......
......@@ -14,16 +14,13 @@ namespace Channel
{
{ "worker.dump", Request::MethodId::WORKER_DUMP },
{ "worker.updateSettings", Request::MethodId::WORKER_UPDATE_SETTINGS },
{ "worker.createRoom", Request::MethodId::WORKER_CREATE_ROOM },
{ "room.close", Request::MethodId::ROOM_CLOSE },
{ "room.dump", Request::MethodId::ROOM_DUMP },
{ "room.createPeer", Request::MethodId::ROOM_CREATE_PEER },
{ "room.setAudioLevelsEvent", Request::MethodId::ROOM_SET_AUDIO_LEVELS_EVENT },
{ "peer.close", Request::MethodId::PEER_CLOSE },
{ "peer.dump", Request::MethodId::PEER_DUMP },
{ "peer.setCapabilities", Request::MethodId::PEER_SET_CAPABILITIES },
{ "peer.createTransport", Request::MethodId::PEER_CREATE_TRANSPORT },
{ "peer.createProducer", Request::MethodId::PEER_CREATE_PRODUCER },
{ "worker.createRouter", Request::MethodId::WORKER_CREATE_ROTER },
{ "router.close", Request::MethodId::ROUTER_CLOSE },
{ "router.dump", Request::MethodId::ROUTER_DUMP },
{ "router.createTransport", Request::MethodId::ROUTER_CREATE_TRANSPORT },
{ "router.createProducer", Request::MethodId::ROUTER_CREATE_PRODUCER },
{ "router.createConsumer", Request::MethodId::ROUTER_CREATE_CONSUMER },
{ "router.setAudioLevelsEvent", Request::MethodId::ROUTER_SET_AUDIO_LEVELS_EVENT },
{ "transport.close", Request::MethodId::TRANSPORT_CLOSE },
{ "transport.dump", Request::MethodId::TRANSPORT_DUMP },
{ "transport.setRemoteDtlsParameters", Request::MethodId::TRANSPORT_SET_REMOTE_DTLS_PARAMETERS },
......@@ -36,6 +33,7 @@ namespace Channel
{ "producer.resume" , Request::MethodId::PRODUCER_RESUME },
{ "producer.setRtpRawEvent", Request::MethodId::PRODUCER_SET_RTP_RAW_EVENT },
{ "producer.setRtpObjectEvent", Request::MethodId::PRODUCER_SET_RTP_OBJECT_EVENT },
{ "consumer.close", Request::MethodId::CONSUMER_CLOSE },
{ "consumer.dump", Request::MethodId::CONSUMER_DUMP },
{ "consumer.enable", Request::MethodId::CONSUMER_ENABLE },
{ "consumer.pause", Request::MethodId::CONSUMER_PAUSE },
......
......@@ -58,16 +58,16 @@ void Loop::Close()
if (this->signalsHandler != nullptr)
this->signalsHandler->Destroy();
// Close all the Rooms.
// NOTE: Upon Room closure the onRoomClosed() method is called which
// Close all the Routers.
// NOTE: Upon Router closure the onRouterClosed() method is called, which
// removes it from the map, so this is the safe way to iterate the map
// and remove elements.
for (auto it = this->rooms.begin(); it != this->rooms.end();)
for (auto it = this->routers.begin(); it != this->routers.end();)
{
RTC::Room* room = it->second;
RTC::Router* router = it->second;
it = this->rooms.erase(it);
room->Destroy();
it = this->routers.erase(it);
router->Destroy();
}
// Delete the Notifier.
......@@ -78,27 +78,27 @@ void Loop::Close()
this->channel->Destroy();
}
RTC::Room* Loop::GetRoomFromRequest(Channel::Request* request, uint32_t* roomId)
RTC::Router* Loop::GetRouterFromRequest(Channel::Request* request, uint32_t* routerId)
{
MS_TRACE();
static const Json::StaticString JsonStringRoomId{ "roomId" };
static const Json::StaticString JsonStringRouterId{ "routerId" };
auto jsonRoomId = request->internal[JsonStringRoomId];
auto jsonRouterId = request->internal[JsonStringRouterId];
if (!jsonRoomId.isUInt())
MS_THROW_ERROR("Request has not numeric internal.roomId");
if (!jsonRouterId.isUInt())
MS_THROW_ERROR("Request has not numeric internal.routerId");
// If given, fill roomId.
if (roomId != nullptr)
*roomId = jsonRoomId.asUInt();
// If given, fill routerId.
if (routerId != nullptr)
*routerId = jsonRouterId.asUInt();
auto it = this->rooms.find(jsonRoomId.asUInt());
if (it != this->rooms.end())
auto it = this->routers.find(jsonRouterId.asUInt());
if (it != this->routers.end())
{
RTC::Room* room = it->second;
RTC::Router* router = it->second;
return room;
return router;
}
return nullptr;
......@@ -136,21 +136,21 @@ void Loop::OnChannelRequest(Channel::UnixStreamSocket* /*channel*/, Channel::Req
case Channel::Request::MethodId::WORKER_DUMP:
{
static const Json::StaticString JsonStringWorkerId{ "workerId" };
static const Json::StaticString JsonStringRooms{ "rooms" };
static const Json::StaticString JsonStringRouters{ "routers" };
Json::Value json(Json::objectValue);
Json::Value jsonRooms(Json::arrayValue);
Json::Value jsonRouters(Json::arrayValue);
json[JsonStringWorkerId] = Logger::id;
for (auto& kv : this->rooms)
for (auto& kv : this->routers)
{
auto room = kv.second;
auto router = kv.second;
jsonRooms.append(room->ToJson());
jsonRouters.append(router->ToJson());
}
json[JsonStringRooms] = jsonRooms;
json[JsonStringRouters] = jsonRouters;
request->Accept(json);
......@@ -164,14 +164,14 @@ void Loop::OnChannelRequest(Channel::UnixStreamSocket* /*channel*/, Channel::Req
break;
}
case Channel::Request::MethodId::WORKER_CREATE_ROOM:
case Channel::Request::MethodId::WORKER_CREATE_ROUTER:
{
RTC::Room* room;
uint32_t roomId;
RTC::Router* router;
uint32_t routerId;
try
{
room = GetRoomFromRequest(request, &roomId);
router = GetRouterFromRequest(request, &routerId);
}
catch (const MediaSoupError& error)
{
......@@ -180,16 +180,16 @@ void Loop::OnChannelRequest(Channel::UnixStreamSocket* /*channel*/, Channel::Req
return;
}
if (room != nullptr)
if (router != nullptr)
{
request->Reject("Room already exists");
request->Reject("Router already exists");
return;
}
try
{
room = new RTC::Room(this, this->notifier, roomId);
router = new RTC::Router(this, this->notifier, routerId);
}
catch (const MediaSoupError& error)
{
......@@ -198,9 +198,9 @@ void Loop::OnChannelRequest(Channel::UnixStreamSocket* /*channel*/, Channel::Req
return;
}
this->rooms[roomId] = room;
this->routers[routerId] = router;
MS_DEBUG_DEV("Room created [roomId:%" PRIu32 "]", roomId);
MS_DEBUG_DEV("Router created [routerId:%" PRIu32 "]", routerId);
Json::Value data(Json::objectValue);
......@@ -209,14 +209,12 @@ void Loop::OnChannelRequest(Channel::UnixStreamSocket* /*channel*/, Channel::Req
break;
}
case Channel::Request::MethodId::ROOM_CLOSE:
case Channel::Request::MethodId::ROOM_DUMP:
case Channel::Request::MethodId::ROOM_CREATE_PEER:
case Channel::Request::MethodId::ROOM_SET_AUDIO_LEVELS_EVENT:
case Channel::Request::MethodId::PEER_CLOSE:
case Channel::Request::MethodId::PEER_DUMP:
case Channel::Request::MethodId::PEER_CREATE_TRANSPORT:
case Channel::Request::MethodId::PEER_CREATE_PRODUCER:
case Channel::Request::MethodId::ROUTER_CLOSE:
case Channel::Request::MethodId::ROUTER_DUMP:
case Channel::Request::MethodId::ROUTER_CREATE_TRANSPORT:
case Channel::Request::MethodId::ROUTER_CREATE_PRODUCER:
case Channel::Request::MethodId::ROUTER_CREATE_CONSUMER:
case Channel::Request::MethodId::ROUTER_SET_AUDIO_LEVELS_EVENT:
case Channel::Request::MethodId::TRANSPORT_CLOSE:
case Channel::Request::MethodId::TRANSPORT_DUMP:
case Channel::Request::MethodId::TRANSPORT_SET_REMOTE_DTLS_PARAMETERS:
......@@ -229,16 +227,17 @@ void Loop::OnChannelRequest(Channel::UnixStreamSocket* /*channel*/, Channel::Req
case Channel::Request::MethodId::PRODUCER_RESUME:
case Channel::Request::MethodId::PRODUCER_SET_RTP_RAW_EVENT:
case Channel::Request::MethodId::PRODUCER_SET_RTP_OBJECT_EVENT:
case Channel::Request::MethodId::CONSUMER_CLOSE:
case Channel::Request::MethodId::CONSUMER_DUMP:
case Channel::Request::MethodId::CONSUMER_ENABLE:
case Channel::Request::MethodId::CONSUMER_PAUSE:
case Channel::Request::MethodId::CONSUMER_RESUME:
{
RTC::Room* room;
RTC::Router* router;
try
{
room = GetRoomFromRequest(request);
router = GetRouterFromRequest(request);
}
catch (const MediaSoupError& error)
{
......@@ -247,14 +246,14 @@ void Loop::OnChannelRequest(Channel::UnixStreamSocket* /*channel*/, Channel::Req
return;
}
if (room == nullptr)
if (router == nullptr)
{
request->Reject("Room does not exist");
request->Reject("Router does not exist");
return;
}
room->HandleRequest(request);
router->HandleRequest(request);
break;
}
......@@ -272,19 +271,20 @@ void Loop::OnChannelUnixStreamSocketRemotelyClosed(Channel::UnixStreamSocket* /*
{
MS_TRACE_STD();
// When mediasoup Node process ends it sends a SIGTERM to us so we close this
// When mediasoup Node process ends, it sends a SIGTERM to us so we close this
// pipe and then exit.
// If the pipe is remotely closed it means that mediasoup Node process
// abruptly died (SIGKILL?) so we must die.
MS_ERROR_STD("Channel remotely closed, killing myself");
this->channel = nullptr;
Close();
}
void Loop::OnRoomClosed(RTC::Room* room)
void Loop::OnRouterClosed(RTC::Router* router)
{
MS_TRACE();
this->rooms.erase(room->roomId);
this->routers.erase(router->routerId);
}
......@@ -29,15 +29,15 @@ namespace RTC
Peer::~Peer()
{
MS_TRACE();
// Destroy the RTCP timer.
this->timer->Destroy();
}
void Peer::Destroy()
{
MS_TRACE();
// Destroy the RTCP timer.
this->timer->Destroy();
// Close all the Producers.
for (auto it = this->producers.begin(); it != this->producers.end();)
{
......
......@@ -345,7 +345,7 @@ namespace RTC
this->producerId, "rtpobject", packet->GetPayload(), packet->GetPayloadLength(), eventData);
}
// Apply the Producer RTP mapping before dispatching the packet to the Room.
// Apply the Producer RTP mapping before dispatching the packet to the Router.
ApplyRtpMapping(packet);
// Notify the listener.
......
#define MS_CLASS "RTC::Room"
#define MS_CLASS "RTC::Router"
// #define MS_LOG_DEV
#include "RTC/Room.hpp"
#include "RTC/Router.hpp"
#include "Logger.hpp"
#include "MediaSoupError.hpp"
#include "Utils.hpp"
#include <cmath> // std::lround()
#include <set>
#include <string>
#include <vector>
namespace RTC
{
......@@ -18,8 +17,8 @@ namespace RTC
/* Instance methods. */
Room::Room(Listener* listener, Channel::Notifier* notifier, uint32_t roomId)
: roomId(roomId), listener(listener), notifier(notifier)
Router::Router(Listener* listener, Channel::Notifier* notifier, uint32_t routerId)
: routerId(routerId), listener(listener), notifier(notifier)
{
MS_TRACE();
......@@ -27,67 +26,104 @@ namespace RTC
this->audioLevelsTimer = new Timer(this);
}
Room::~Room()
Router::~Router()
{
MS_TRACE();
}
void Room::Destroy()
void Router::Destroy()
{
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 map
// and remove elements.
for (auto it = this->peers.begin(); it != this->peers.end();)
// Close all the Producers.
for (auto it = this->producers.begin(); it != this->producers.end();)
{
auto* peer = it->second;
auto* producer = it->second;
it = this->peers.erase(it);
peer->Destroy();
it = this->producers.erase(it);
producer->Destroy();
}
// Close all the Consumers.
for (auto it = this->consumers.begin(); it != this->consumers.end();)
{
auto* consumer = it->second;
it = this->consumers.erase(it);
consumer->Destroy();
}
// TODO: Is this warning still true?
// Close all the Transports.
// NOTE: It is critical to close Transports after Producers/Consumers
// because Producer.Destroy() fires an event in the Transport.
for (auto it = this->transports.begin(); it != this->transports.end();)
{
auto* transport = it->second;
it = this->transports.erase(it);
transport->Destroy();
}
// Close the audio level timer.
this->audioLevelsTimer->Destroy();
// Notify.
this->notifier->Emit(this->roomId, "close");
// Notify the listener.
this->listener->OnRoomClosed(this);
this->listener->OnRouterClosed(this);
delete this;
}
Json::Value Room::ToJson() const
Json::Value Router::ToJson() const
{
MS_TRACE();
static const Json::StaticString JsonStringRoomId{ "roomId" };
static const Json::StaticString JsonStringPeers{ "peers" };
static const Json::StaticString JsonStringRouterId{ "routerId" };
static const Json::StaticString JsonStringTransports{ "transports" };
static const Json::StaticString JsonStringProducers{ "producers" };
static const Json::StaticString JsonStringConsumers{ "consumers" };
static const Json::StaticString JsonStringMapProducerConsumers{ "mapProducerConsumers" };
static const Json::StaticString JsonStringMapConsumerProducer{ "mapConsumerProducer" };
static const Json::StaticString JsonStringAudioLevelsEventEnabled{ "audioLevelsEventEnabled" };
Json::Value json(Json::objectValue);
Json::Value jsonPeers(Json::arrayValue);
Json::Value jsonTransports(Json::arrayValue);
Json::Value jsonProducers(Json::arrayValue);
Json::Value jsonConsumers(Json::arrayValue);
Json::Value jsonMapProducerConsumers(Json::objectValue);
Json::Value jsonMapConsumerProducer(Json::objectValue);
// Add `roomId`.
json[JsonStringRoomId] = Json::UInt{ this->roomId };
// Add routerId.
json[JsonStringRouterId] = Json::UInt{ this->routerId };
// Add transports.
for (auto& kv : this->transports)
{
auto* transport = kv.second;
jsonTransports.append(transport->ToJson());
}
json[JsonStringTransports] = jsonTransports;
// Add `peers`.
for (auto& kv : this->peers)
// Add producers.
for (auto& kv : this->producers)
{
auto* peer = kv.second;
auto* producer = kv.second;
jsonPeers.append(peer->ToJson());
jsonProducers.append(producer->ToJson());
}
json[JsonStringPeers] = jsonPeers;
json[JsonStringProducers] = jsonProducers;
// Add consumers.
for (auto& kv : this->consumers)
{
auto* consumer = kv.second;
// Add `mapProducerConsumers`.
jsonConsumers.append(consumer->ToJson());
}
json[JsonStringConsumers] = jsonConsumers;
// Add mapProducerConsumers.
for (auto& kv : this->mapProducerConsumers)
{
auto producer = kv.first;
......@@ -103,7 +139,7 @@ namespace RTC
}
json[JsonStringMapProducerConsumers] = jsonMapProducerConsumers;
// Add `mapConsumerProducer`.
// Add mapConsumerProducer.
for (auto& kv : this->mapConsumerProducer)
{
auto consumer = kv.first;
......@@ -119,28 +155,28 @@ namespace RTC
return json;
}
void Room::HandleRequest(Channel::Request* request)
void Router::HandleRequest(Channel::Request* request)
{
MS_TRACE();
switch (request->methodId)
{
case Channel::Request::MethodId::ROOM_CLOSE:
case Channel::Request::MethodId::ROUTER_CLOSE:
{
#ifdef MS_LOG_DEV
uint32_t roomId = this->roomId;
uint32_t routerId = this->routerId;
#endif
Destroy();
MS_DEBUG_DEV("Room closed [roomId:%" PRIu32 "]", roomId);
MS_DEBUG_DEV("Router closed [routerId:%" PRIu32 "]", routerId);
request->Accept();
break;
}
case Channel::Request::MethodId::ROOM_DUMP:
case Channel::Request::MethodId::ROUTER_DUMP:
{
auto json = ToJson();
......@@ -149,17 +185,14 @@ namespace RTC
break;
}
case Channel::Request::MethodId::ROOM_CREATE_PEER:
case Channel::Request::MethodId::ROUTER_CREATE_TRANSPORT:
{
static const Json::StaticString JsonStringPeerName{ "peerName" };
RTC::Peer* peer;
uint32_t peerId;
std::string peerName;
RTC::Transport* transport;
uint32_t transportId;
try
{
peer = GetPeerFromRequest(request, &peerId);
transport = GetTransportFromRequest(request, &transportId);
}
catch (const MediaSoupError& error)
{
......@@ -168,25 +201,16 @@ namespace RTC
return;
}
if (peer != nullptr)
{
request->Reject("Peer already exists");
return;
}
if (!request->internal[JsonStringPeerName].isString())
if (transport != nullptr)
{
request->Reject("Request has not string internal.peerName");
request->Reject("Transport already exists");
return;
}
peerName = request->internal[JsonStringPeerName].asString();
try
{
peer = new RTC::Peer(this, this->notifier, peerId, peerName);
transport = new RTC::Transport(this, this->notifier, transportId, request->data);
}
catch (const MediaSoupError& error)
{
......@@ -195,38 +219,18 @@ namespace RTC
return;
}
// Store the new Peer.
this->peers[peerId] = peer;
this->transports[transportId] = transport;
MS_DEBUG_DEV("Peer created [peerId:%u, peerName:'%s']", peerId, peerName.c_str());
MS_DEBUG_DEV("Transport created [transportId:%" PRIu32 "]", transportId);
request->Accept();
auto data = transport->ToJson();
for (auto& kv : this->peers)
{
auto* otherPeer = kv.second;
// Skip new Peer.
if (otherPeer == peer)
continue;
// Get all the ready Producers of the others Peers in the Room and create
// Consumers for this new Peer.
for (auto producer : otherPeer->GetProducers())
{
// Skip if the Producer has not RTP parameters.
if (producer->GetParameters() == nullptr)
continue;
AddConsumerForProducer(peer, producer);
}
}
request->Accept(data);
break;
}
case Channel::Request::MethodId::ROOM_SET_AUDIO_LEVELS_EVENT:
case Channel::Request::MethodId::ROUTER_SET_AUDIO_LEVELS_EVENT:
{
static const Json::StaticString JsonStringEnabled{ "enabled" };
......@@ -237,31 +241,27 @@ namespace RTC
return;
}
bool audioLevelsEventEnabled = request->data[JsonStringEnabled].asBool();
bool enabled = request->data[JsonStringEnabled].asBool();
if (audioLevelsEventEnabled == this->audioLevelsEventEnabled)
if (enabled == this->audioLevelsEventEnabled)
return;
this->audioLevelsEventEnabled = enabled;
// Clear map of audio levels.
this->mapProducerAudioLevels.clear();
this->mapProducerAudioLevelContainer.clear();
// Start or stop audio levels periodic timer.
if (audioLevelsEventEnabled)
if (enabled)
this->audioLevelsTimer->Start(AudioLevelsInterval, AudioLevelsInterval);
else
this->audioLevelsTimer->Stop();
this->audioLevelsEventEnabled = audioLevelsEventEnabled;
request->Accept();
break;
}
case Channel::Request::MethodId::PEER_CLOSE:
case Channel::Request::MethodId::PEER_DUMP:
case Channel::Request::MethodId::PEER_CREATE_TRANSPORT:
case Channel::Request::MethodId::PEER_CREATE_PRODUCER:
case Channel::Request::MethodId::TRANSPORT_CLOSE:
case Channel::Request::MethodId::TRANSPORT_DUMP:
case Channel::Request::MethodId::TRANSPORT_SET_REMOTE_DTLS_PARAMETERS:
......@@ -274,6 +274,7 @@ namespace RTC
case Channel::Request::MethodId::PRODUCER_RESUME:
case Channel::Request::MethodId::PRODUCER_SET_RTP_RAW_EVENT:
case Channel::Request::MethodId::PRODUCER_SET_RTP_OBJECT_EVENT:
case Channel::Request::MethodId::CONSUMER_CLOSE:
case Channel::Request::MethodId::CONSUMER_DUMP:
case Channel::Request::MethodId::CONSUMER_ENABLE:
case Channel::Request::MethodId::CONSUMER_PAUSE:
......@@ -313,131 +314,122 @@ namespace RTC
}
}
RTC::Peer* Room::GetPeerFromRequest(Channel::Request* request, uint32_t* peerId) const
RTC::Transport* Router::GetTransportFromRequest(Channel::Request* request, uint32_t* transportId) const
{
MS_TRACE();
static const Json::StaticString JsonStringPeerId{ "peerId" };
static const Json::StaticString JsonStringTransportId{ "transportId" };
auto jsonPeerId = request->internal[JsonStringPeerId];
auto jsonTransportId = request->internal[JsonStringTransportId];
if (!jsonPeerId.isUInt())
MS_THROW_ERROR("Request has not numeric .peerId field");
if (!jsonTransportId.isUInt())
MS_THROW_ERROR("Request has not numeric internal.transportId");
// If given, fill peerId.
if (peerId != nullptr)
*peerId = jsonPeerId.asUInt();
if (transportId != nullptr)
*transportId = jsonTransportId.asUInt();
auto it = this->peers.find(jsonPeerId.asUInt());
if (it != this->peers.end())
auto it = this->transports.find(*transportId);
if (it != this->transports.end())
{
auto* peer = it->second;
auto* transport = it->second;
return peer;
return transport;
}
return nullptr;
}
inline void Room::AddConsumerForProducer(RTC::Peer* consumerPeer, const RTC::Producer* producer)
RTC::Producer* Router::GetProducerFromRequest(Channel::Request* request, uint32_t* producerId) const
{
MS_TRACE();
MS_ASSERT(producer->GetParameters(), "Producer has no parameters");
static const Json::StaticString JsonStringProducerId{ "producerId" };
auto jsonProducerId = request->internal[JsonStringProducerId];
uint32_t consumerId = Utils::Crypto::GetRandomUInt(10000000, 99999999);
auto consumer = new RTC::Consumer(consumerPeer, this->notifier, consumerId, producer->kind);
if (!jsonProducerId.isUInt())
MS_THROW_ERROR("Request has not numeric internal.producerId");
// Store into the maps.
this->mapProducerConsumers[producer].insert(consumer);
this->mapConsumerProducer[consumer] = producer;
if (producerId != nullptr)
*producerId = jsonProducerId.asUInt();
// TODO: NO
auto rtpParameters = producer->GetParameters();
// TODO: Maybe, not sure
auto associatedProducerId = producer->producerId;
auto it = this->producers.find(*producerId);
if (it != this->producers.end())
{
auto* producer = it->second;
// TODO: Check producer->IsPaused() and, if so, set consumer->SetSourcePaused(true).
return producer;
}
// Attach the Consumer to the peer.
consumerPeer->AddConsumer(consumer, rtpParameters, associatedProducerId);
return nullptr;
}
void Room::OnPeerClosed(const RTC::Peer* peer)
RTC::Consumer* Router::GetConsumerFromRequest(Channel::Request* request, uint32_t* consumerId) const
{
MS_TRACE();
this->peers.erase(peer->peerId);
}
static const Json::StaticString JsonStringConsumerId{ "consumerId" };
void Room::OnPeerProducerClosed(const RTC::Peer* /*peer*/, const RTC::Producer* producer)
{
MS_TRACE();
auto jsonConsumerId = request->internal[JsonStringConsumerId];
// If the Producer is in the map, iterate the map and close all the
// Consumers associated to the closed Producer.
if (this->mapProducerConsumers.find(producer) != this->mapProducerConsumers.end())
{
// Make a copy of the set of Consumers given that Destroy() will be called
// in all of them, producing onPeerConsumerClosed() that will remove it
// from the map.
auto consumers = this->mapProducerConsumers[producer];
if (!jsonConsumerId.isUInt())
MS_THROW_ERROR("Request has not numeric internal.consumerId");
// Safely iterate the copy of the set.
for (auto& consumer : consumers)
{
consumer->Destroy();
}
if (consumerId != nullptr)
*consumerId = jsonConsumerId.asUInt();
// Finally delete the Producer entry in the map.
this->mapProducerConsumers.erase(producer);
auto it = this->consumers.find(*consumerId);
if (it != this->consumers.end())
{
auto* consumer = it->second;
return consumer;
}
return nullptr;
}
void Room::OnPeerProducerRtpParameters(const RTC::Peer* peer, RTC::Producer* producer)
void Router::OnTransportClosed(RTC::Transport* transport)
{
MS_TRACE();
// Iterate all the peers but this one and create a Consumer associated to this
// Producer for each Peer.
// TODO: How to tell all the Producers/Consumers using this Transport.
// Probably using multiple listeners (or not).
MS_ASSERT(producer->GetParameters(), "Producer has no parameters");
MS_ASSERT(
this->mapProducerConsumers.find(producer) == this->mapProducerConsumers.end(),
"Producer already in mapProducerConsumers");
this->transports.erase(transport->transportId);
}
// Ensure the entry will exist even with an empty array.
this->mapProducerConsumers[producer];
void Router::OnProducerClosed(RTC::Producer* producer)
{
MS_TRACE();
for (auto& kv : this->peers)
{
auto* consumerPeer = kv.second;
// TODO: How to tell the Transport using this Producer.
// Skip producing Peer.
if (consumerPeer == peer)
continue;
this->producers.erase(producer->producerId);
AddConsumerForProducer(consumerPeer, producer);
}
}
// If the Producer is not in the map, do nothing.
if (this->mapProducerConsumers.find(producer) == this->mapProducerConsumers.end())
return;
void Room::OnPeerProducerPaused(const RTC::Peer* peer, const RTC::Producer* producer)
{
MS_TRACE();
// Iterate the map and close all the Consumers associated to it.
auto& consumers = this->mapProducerConsumers[producer];
// TODO
}
for (auto it = this->consumers.begin(); it != this->consumers.end();)
{
auto* consumer = it->second;
void Room::OnPeerProducerResumed(const RTC::Peer* peer, const RTC::Producer* producer)
{
MS_TRACE();
it = consumers.erase(it);
consumer->Destroy();
}
// Finally delete the Producer entry in the map.
this->mapProducerConsumers.erase(producer);
// TODO
// Also delete it from the map of audio levels.
this->mapProducerAudioLevelContainer.erase(producer);
}
void Room::OnPeerProducerRtpPacket(
const RTC::Peer* /*peer*/, RTC::Producer* producer, RTC::RtpPacket* packet)
void Router::OnProducerRtpPacket(RTC::Producer* producer, RTC::RtpPacket* packet)
{
MS_TRACE();
......@@ -451,6 +443,8 @@ namespace RTC
// from which it was received.
for (auto& consumer : consumers)
{
// TODO: just if enabled?
consumer->SendRtpPacket(packet);
}
......@@ -463,28 +457,21 @@ namespace RTC
if (packet->ReadAudioLevel(&volume, &voice))
{
int8_t dBov = volume * -1;
auto& audioLevelContainer = this->mapProducerAudioLevelContainer[producer];
this->mapProducerAudioLevels[producer].push_back(dBov);
audioLevelContainer.numdBovs++;
audioLevelContainer.sumdBovs += dBov;
}
}
}
void Room::OnPeerProducerRtcpSenderReport(
const RTC::Peer* /*peer*/, RTC::Producer* producer, RTC::RTCP::SenderReport* report)
void Router::OnConsumerClosed(RTC::Consumer* consumer)
{
MS_TRACE();
// Producer needs the sender report in order to generate it's receiver report.
producer->ReceiveRtcpSenderReport(report);
// TODO: How to tell the Transport using this Consumer.
MS_ASSERT(
this->mapProducerConsumers.find(producer) != this->mapProducerConsumers.end(),
"Producer not present in the map");
}
void Room::OnPeerConsumerClosed(const RTC::Peer* /*peer*/, RTC::Consumer* consumer)
{
MS_TRACE();
this->consumers.erase(consumer->consumerId);
for (auto& kv : this->mapProducerConsumers)
{
......@@ -493,50 +480,11 @@ namespace RTC
consumers.erase(consumer);
}
// Finally delete the Consumer entry in the map.
this->mapConsumerProducer.erase(consumer);
}
void Room::OnPeerConsumerRtcpReceiverReport(
const RTC::Peer* /*peer*/, RTC::Consumer* consumer, RTC::RTCP::ReceiverReport* report)
{
MS_TRACE();
MS_ASSERT(
this->mapConsumerProducer.find(consumer) != this->mapConsumerProducer.end(),
"Consumer not present in the map");
consumer->ReceiveRtcpReceiverReport(report);
}
void Room::OnPeerConsumerRtcpFeedback(
const RTC::Peer* /*peer*/, RTC::Consumer* consumer, RTC::RTCP::FeedbackPsPacket* packet)
{
MS_TRACE();
MS_ASSERT(
this->mapConsumerProducer.find(consumer) != this->mapConsumerProducer.end(),
"Consumer not present in the map");
auto& producer = this->mapConsumerProducer[consumer];
producer->ReceiveRtcpFeedback(packet);
}
void Room::OnPeerConsumerRtcpFeedback(
const RTC::Peer* /*peer*/, RTC::Consumer* consumer, RTC::RTCP::FeedbackRtpPacket* packet)
{
MS_TRACE();
MS_ASSERT(
this->mapConsumerProducer.find(consumer) != this->mapConsumerProducer.end(),
"Consumer not present in the map");
auto& producer = this->mapConsumerProducer[consumer];
producer->ReceiveRtcpFeedback(packet);
}
void Room::OnPeerConsumerFullFrameRequired(RTC::Peer* /*peer*/, RTC::Consumer* consumer)
void Peer::OnConsumerFullFrameRequired(RTC::Consumer* consumer)
{
MS_TRACE();
......@@ -549,7 +497,7 @@ namespace RTC
producer->RequestFullFrame();
}
inline void Room::OnTimer(Timer* timer)
inline void Router::OnTimer(Timer* timer)
{
MS_TRACE();
......@@ -558,50 +506,36 @@ namespace RTC
// Audio levels timer.
if (timer == this->audioLevelsTimer)
{
std::unordered_map<RTC::Producer*, int8_t> mapProducerAudioLevel;
Json::Value eventData(Json::objectValue);
for (auto& kv : this->mapProducerAudioLevels)
eventData[JsonStringEntries] = Json::arrayValue;
for (auto& kv : this->mapProducerAudioLevelContainer)
{
auto producer = kv.first;
auto& dBovs = kv.second;
auto producer = kv.first;
auto& audioLevelContainer = kv.second;
auto numdBovs = audioLevelContainer.numdBovs;
auto sumdBovs = audioLevelContainer.sumdBovs;
int8_t avgdBov{ -127 };
if (!dBovs.empty())
{
int16_t sumdBovs{ 0 };
if (numdBovs > 0)
avgdBov = static_cast<int8_t>(std::lround(sumdBovs / numdBovs));
else
avgdBov = -127;
for (auto& dBov : dBovs)
{
sumdBovs += dBov;
}
Json::Value entry(Json::arrayValue);
avgdBov = static_cast<int8_t>(std::lround(sumdBovs / static_cast<int16_t>(dBovs.size())));
}
entry.append(Json::UInt{ producer.producerId });
entry.append(Json::Int{ avgdBov });
mapProducerAudioLevel[producer] = avgdBov;
eventData[JsonStringEntries].append(entry);
}
// Clear map.
this->mapProducerAudioLevels.clear();
this->mapProducerAudioLevelContainer.clear();
// Emit event.
Json::Value eventData(Json::objectValue);
eventData[JsonStringEntries] = Json::arrayValue;
for (auto& kv : mapProducerAudioLevel)
{
auto& producerId = kv.first->producerId;
auto& audioLevel = kv.second;
Json::Value entry(Json::arrayValue);
entry.append(Json::UInt{ producerId });
entry.append(Json::Int{ audioLevel });
eventData[JsonStringEntries].append(entry);
}
this->notifier->Emit(this->roomId, "audiolevels", eventData);
this->notifier->Emit(this->routerId, "audiolevels", eventData);
}
}
} // namespace RTC
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论