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