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

worker: Improve binary notifications

上级 3886e49b
......@@ -132,7 +132,7 @@ class Channel extends EventEmitter
// Reset
this._lastBinaryNotification = null;
// Emit
this.emit(msg.targetId, msg.event, nsPayload);
this.emit(msg.targetId, msg.event, msg.data, nsPayload);
}
// Remove the read payload from the recvBuffer
......@@ -192,7 +192,6 @@ class Channel extends EventEmitter
let request = { id, method, internal, data };
let ns = netstring.nsWrite(JSON.stringify(request));
let promise;
if (Buffer.byteLength(ns) > NS_MAX_SIZE)
return Promise.reject(new Error('request too big'));
......@@ -207,7 +206,7 @@ class Channel extends EventEmitter
return Promise.reject(error);
}
promise = new Promise((pResolve, pReject) =>
return new Promise((pResolve, pReject) =>
{
let sent =
{
......@@ -247,8 +246,6 @@ class Channel extends EventEmitter
// Add sent stuff to the Map
this._pendingSent.set(id, sent);
});
return promise;
}
_processMessage(msg)
......@@ -281,7 +278,7 @@ class Channel extends EventEmitter
{
// If a binary notification, keep this message until the binary
// data arrives
if (msg.data && msg.data.binary)
if (msg.binary)
{
this._lastBinaryNotification = msg;
......
......@@ -37,7 +37,7 @@ class RtpReceiver extends EventEmitter
this._closed = false;
// Subscribe to notifications
this._channel.on(this._internal.rtpReceiverId, (event, data) =>
this._channel.on(this._internal.rtpReceiverId, (event, data, buffer) =>
{
switch (event)
{
......@@ -49,7 +49,7 @@ class RtpReceiver extends EventEmitter
case 'rtp':
{
this.emit('rtp', data);
this.emit('rtp', buffer);
break;
}
......
......@@ -18,7 +18,7 @@ namespace Channel
void Emit(uint32_t targetId, std::string event);
void Emit(uint32_t targetId, std::string event, Json::Value& data);
void Emit(const uint8_t* data, size_t len);
void EmitWithBinary(uint32_t targetId, std::string event, Json::Value& data, const uint8_t* binary_data, size_t binary_len);
public:
// Passed by argument.
......
......@@ -68,6 +68,9 @@ namespace RTC
DTMF
};
private:
static std::unordered_map<std::string, RtpCodecParameters::Subtype> string2Subtype;
public:
RtpCodecParameters(RTC::RtpKind kind, Json::Value& data);
virtual ~RtpCodecParameters();
......
......@@ -58,10 +58,23 @@ namespace Channel
this->channel->Send(json);
}
void Notifier::Emit(const uint8_t* data, size_t len)
void Notifier::EmitWithBinary(uint32_t targetId, std::string event, Json::Value& data, const uint8_t* binary_data, size_t binary_len)
{
MS_TRACE();
this->channel->SendBinary(data, len);
static const Json::StaticString k_targetId("targetId");
static const Json::StaticString k_event("event");
static const Json::StaticString k_data("data");
static const Json::StaticString k_binary("binary");
Json::Value json(Json::objectValue);
json[k_targetId] = (Json::UInt)targetId;
json[k_event] = event;
json[k_data] = data;
json[k_binary] = true;
this->channel->Send(json);
this->channel->SendBinary(binary_data, binary_len);
}
}
......@@ -6,7 +6,9 @@
namespace RTC
{
static std::unordered_map<std::string, RtpCodecParameters::Subtype> string2Subtype =
/* Class variables. */
std::unordered_map<std::string, RtpCodecParameters::Subtype> RtpCodecParameters::string2Subtype =
{
{ "rtx", RtpCodecParameters::Subtype::RTX },
{ "RTX", RtpCodecParameters::Subtype::RTX },
......@@ -50,15 +52,15 @@ namespace RTC
else if (kind == RTC::RtpKind::VIDEO && this->name.compare(0, 6, "video/") == 0)
this->type = RTC::RtpKind::VIDEO;
else
MS_THROW_ERROR("invalid `RtpCodecParameters.name` MIME type [name:%s", this->name.c_str());
MS_THROW_ERROR("invalid `RtpCodecParameters.name` MIME type [name:%s]", this->name.c_str());
// Get name MIME subtype.
std::string subtype = this->name.substr(6);
if (string2Subtype.find(subtype) == string2Subtype.end())
if (RtpCodecParameters::string2Subtype.find(subtype) == RtpCodecParameters::string2Subtype.end())
this->subtype = Subtype::MEDIA;
else
this->subtype = string2Subtype[subtype];
this->subtype = RtpCodecParameters::string2Subtype[subtype];
// `payloadType` is mandatory.
if (!data[k_payloadType].isUInt())
......
......@@ -195,16 +195,13 @@ namespace RTC
// Send a JSON event followed by binary data (the RTP packet).
static const Json::StaticString k_class("class");
static const Json::StaticString k_binary("binary");
Json::Value event_data(Json::objectValue);
// Notify binary event.
event_data[k_class] = "RtpReceiver";
event_data[k_binary] = true;
this->notifier->Emit(this->rtpReceiverId, "rtp", event_data);
this->notifier->Emit(packet->GetRaw(), packet->GetLength());
this->notifier->EmitWithBinary(this->rtpReceiverId, "rtp", event_data, packet->GetRaw(), packet->GetLength());
}
}
}
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论