提交 98c8253d authored 作者: Iñaki Baz Castillo's avatar Iñaki Baz Castillo

Node: add Server.updateSettings() method.

上级 fbac2062
...@@ -119,6 +119,25 @@ class Server extends EventEmitter ...@@ -119,6 +119,25 @@ class Server extends EventEmitter
this.emit('close', error); this.emit('close', error);
} }
/**
* Update Server settings
* @param {Object} options Object with modified settings
* @return {Promise}
*/
updateSettings(options)
{
logger.debug('updateSettings() [options:%o]', options);
let promises = [];
this._workers.forEach((worker) =>
{
promises.push(worker.updateSettings(options));
});
return Promise.all(promises);
}
createRoom(options) createRoom(options)
{ {
logger.debug('createRoom() [options:%o]', options); logger.debug('createRoom() [options:%o]', options);
......
...@@ -129,6 +129,23 @@ class Worker extends EventEmitter ...@@ -129,6 +129,23 @@ class Worker extends EventEmitter
this.emit('close', error); this.emit('close', error);
} }
updateSettings(options)
{
logger.debug('updateSettings() [options:%o]', options);
return this._channel.request('updateSettings', options)
.then(() =>
{
logger.debug('"updateSettings" request succeeded');
})
.catch((error) =>
{
logger.error('"updateSettings" request failed [status:%s, error:"%s"]', error.status, error.message);
throw error;
});
}
createRoom(options) createRoom(options)
{ {
logger.debug('createRoom() [options:%o]', options); logger.debug('createRoom() [options:%o]', options);
......
...@@ -16,7 +16,8 @@ namespace Channel ...@@ -16,7 +16,8 @@ namespace Channel
public: public:
enum class MethodId enum class MethodId
{ {
createRoom = 1, updateSettings = 1,
createRoom,
createPeer createPeer
}; };
......
...@@ -4,12 +4,16 @@ ...@@ -4,12 +4,16 @@
#include "RTC/Peer.h" #include "RTC/Peer.h"
#include "RTC/RTPPacket.h" #include "RTC/RTPPacket.h"
#include "RTC/RTCPPacket.h" #include "RTC/RTCPPacket.h"
#include <vector> #include <unordered_map>
#include <json/json.h>
namespace RTC namespace RTC
{ {
class Room : public RTC::Peer::Listener class Room : public RTC::Peer::Listener
{ {
public:
static RTC::Room* Factory(Json::Value& data);
public: public:
Room(); Room();
virtual ~Room(); virtual ~Room();
...@@ -23,7 +27,7 @@ namespace RTC ...@@ -23,7 +27,7 @@ namespace RTC
private: private:
// Others. // Others.
typedef std::vector<RTC::Peer*> Peers; typedef std::unordered_map<unsigned int, RTC::Peer*> Peers;
Peers peers; Peers peers;
}; };
} }
......
...@@ -2,9 +2,11 @@ ...@@ -2,9 +2,11 @@
#define MS_SETTINGS_H #define MS_SETTINGS_H
#include "common.h" #include "common.h"
#include "Channel/Request.h"
#include <map> #include <map>
#include <string> #include <string>
class Settings class Settings
{ {
public: public:
...@@ -26,6 +28,7 @@ public: ...@@ -26,6 +28,7 @@ public:
public: public:
static void SetConfiguration(int argc, char* argv[]); static void SetConfiguration(int argc, char* argv[]);
static void PrintConfiguration(); static void PrintConfiguration();
static void HandleUpdateRequest(Channel::Request* request);
private: private:
static void SetDefaultRtcListenIP(int requested_family); static void SetDefaultRtcListenIP(int requested_family);
......
...@@ -13,6 +13,7 @@ namespace Channel ...@@ -13,6 +13,7 @@ namespace Channel
std::unordered_map<std::string, Request::MethodId> Request::string2MethodId = std::unordered_map<std::string, Request::MethodId> Request::string2MethodId =
{ {
{ "updateSettings", Request::MethodId::updateSettings },
{ "createRoom", Request::MethodId::createRoom }, { "createRoom", Request::MethodId::createRoom },
{ "createPeer", Request::MethodId::createPeer } { "createPeer", Request::MethodId::createPeer }
}; };
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
#include "Loop.h" #include "Loop.h"
#include "DepLibUV.h" #include "DepLibUV.h"
#include "Settings.h"
#include "MediaSoupError.h" #include "MediaSoupError.h"
#include "Logger.h" #include "Logger.h"
#include <string> #include <string>
...@@ -104,50 +105,37 @@ void Loop::onChannelRequest(Channel::UnixStreamSocket* channel, Channel::Request ...@@ -104,50 +105,37 @@ void Loop::onChannelRequest(Channel::UnixStreamSocket* channel, Channel::Request
switch (request->methodId) switch (request->methodId)
{ {
case Channel::Request::MethodId::createRoom: case Channel::Request::MethodId::updateSettings:
{ {
MS_DEBUG("'createRoom' method"); MS_DEBUG("'updateSettings' method");
break;
}
case Channel::Request::MethodId::createPeer: Settings::HandleUpdateRequest(request);
{
MS_DEBUG("'createPeer' method");
break; break;
} }
default: case Channel::Request::MethodId::createRoom:
{ {
MS_ABORT("unexpected methodId"); MS_DEBUG("'createRoom' method");
}
}
// TODO: TMP jeje
if (request->id < 25000)
{
request->Accept();
}
else if (request->id < 50000)
{
Json::Value data; Json::Value data;
data["jojojo"] = "🐮🐷🐣😡"; data["jojojo"] = "🐮🐷🐣😡";
request->Accept(data); request->Accept(data);
break;
} }
else if (request->id < 75000)
case Channel::Request::MethodId::createPeer:
{ {
std::string reason = "this is a std::string reason"; MS_DEBUG("'createPeer' method");
request->Reject(400, reason); request->Reject(500, "lalalala failed to create a Peer because yes!");
break;
} }
else
default:
{ {
request->Reject(500, "this is a char* reason"); MS_ABORT("unexpected methodId");
}
} }
} }
......
...@@ -13,6 +13,15 @@ ...@@ -13,6 +13,15 @@
namespace RTC namespace RTC
{ {
/* Class methods. */
RTC::Room* Room::Factory(Json::Value& data)
{
MS_TRACE();
return new Room();
}
/* Instance methods. */ /* Instance methods. */
Room::Room() Room::Room()
...@@ -42,8 +51,12 @@ namespace RTC ...@@ -42,8 +51,12 @@ namespace RTC
MS_TRACE(); MS_TRACE();
// Close all the Peers. // Close all the Peers.
for (auto peer : this->peers) for (auto& kv : this->peers)
{
RTC::Peer* peer = kv.second;
peer->Close(); peer->Close();
}
delete this; delete this;
} }
...@@ -52,54 +65,54 @@ namespace RTC ...@@ -52,54 +65,54 @@ namespace RTC
{ {
MS_TRACE(); MS_TRACE();
int peer_id = *(int*)peer->GetUserData(); // int peer_id = *(int*)peer->GetUserData();
MS_BYTE payload_type = packet->GetPayloadType(); // MS_BYTE payload_type = packet->GetPayloadType();
MS_4BYTES original_ssrc = packet->GetSSRC(); // MS_4BYTES original_ssrc = packet->GetSSRC();
MS_DEBUG("valid RTP packet received from Peer %d [ssrc: %llu | payload: %hu]", // MS_DEBUG("valid RTP packet received from Peer %d [ssrc: %llu | payload: %hu]",
peer_id, (unsigned long long)packet->GetSSRC(), (unsigned short)packet->GetPayloadType()); // peer_id, (unsigned long long)packet->GetSSRC(), (unsigned short)packet->GetPayloadType());
switch (payload_type) // switch (payload_type)
{ // {
case PAYLOAD_TYPE_AUDIO: // case PAYLOAD_TYPE_AUDIO:
case PAYLOAD_TYPE_VIDEO: // case PAYLOAD_TYPE_VIDEO:
break; // break;
default: // default:
MS_ERROR("payload is not OPUS %d nor VP8 %d, packet ignored", PAYLOAD_TYPE_AUDIO, PAYLOAD_TYPE_VIDEO); // MS_ERROR("payload is not OPUS %d nor VP8 %d, packet ignored", PAYLOAD_TYPE_AUDIO, PAYLOAD_TYPE_VIDEO);
return; // return;
} // }
// Deliver to all the peers. // // Deliver to all the peers.
// TODO: but this one? yes (TODO) // // TODO: but this one? yes (TODO)
for (auto dst_peer : this->peers) // for (auto dst_peer : this->peers)
{ // {
// if (dst_peer == peer) // // if (dst_peer == peer)
// continue; // // continue;
int dst_peer_id = *(int*)dst_peer->GetUserData(); // int dst_peer_id = *(int*)dst_peer->GetUserData();
switch (payload_type) // switch (payload_type)
{ // {
case PAYLOAD_TYPE_AUDIO: // case PAYLOAD_TYPE_AUDIO:
packet->SetSSRC((MS_4BYTES)(SSRC_AUDIO_BASE + (peer_id * 10) + dst_peer_id)); // packet->SetSSRC((MS_4BYTES)(SSRC_AUDIO_BASE + (peer_id * 10) + dst_peer_id));
break; // break;
case PAYLOAD_TYPE_VIDEO: // case PAYLOAD_TYPE_VIDEO:
packet->SetSSRC((MS_4BYTES)(SSRC_VIDEO_BASE + (peer_id * 10) + dst_peer_id)); // packet->SetSSRC((MS_4BYTES)(SSRC_VIDEO_BASE + (peer_id * 10) + dst_peer_id));
break; // break;
default: // default:
MS_ABORT("no puede ser!!!"); // MS_ABORT("no puede ser!!!");
return; // return;
} // }
MS_DEBUG("sending RTP packet to Peer %d [ssrc: %llu | payload: %hu | size: %zu]", // MS_DEBUG("sending RTP packet to Peer %d [ssrc: %llu | payload: %hu | size: %zu]",
dst_peer_id, (unsigned long long)packet->GetSSRC(), (unsigned short)packet->GetPayloadType(), packet->GetLength()); // dst_peer_id, (unsigned long long)packet->GetSSRC(), (unsigned short)packet->GetPayloadType(), packet->GetLength());
dst_peer->SendRTPPacket(packet); // dst_peer->SendRTPPacket(packet);
// NOTE: recover the previous SSRC since other peers are going to // // NOTE: recover the previous SSRC since other peers are going to
// send this same RTPPacket! // // send this same RTPPacket!
packet->SetSSRC(original_ssrc); // packet->SetSSRC(original_ssrc);
} // }
} }
void Room::onRTCPPacket(RTC::Peer* peer, RTC::RTCPPacket* packet) void Room::onRTCPPacket(RTC::Peer* peer, RTC::RTCPPacket* packet)
......
...@@ -167,6 +167,35 @@ void Settings::PrintConfiguration() ...@@ -167,6 +167,35 @@ void Settings::PrintConfiguration()
MS_DEBUG("[/configuration]"); MS_DEBUG("[/configuration]");
} }
void Settings::HandleUpdateRequest(Channel::Request* request)
{
MS_TRACE();
try
{
// Update logLevel if requested.
if (request->data["logLevel"].isString())
{
std::string logLevel = request->data["logLevel"].asString();
Settings::SetLogLevel(logLevel);
}
}
catch (const MediaSoupError &error)
{
request->Reject(500, error.what());
return;
}
MS_DEBUG("updated settings:");
// Print configuration.
Settings::PrintConfiguration();
request->Accept();
}
void Settings::SetDefaultRtcListenIP(int requested_family) void Settings::SetDefaultRtcListenIP(int requested_family)
{ {
MS_TRACE(); MS_TRACE();
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论