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

Start a big refactoring

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