Commit 9b066b28 authored by Moritz Langenstein's avatar Moritz Langenstein
Browse files

(ml5717) Reduce message size using MessagePack and pako

parent 545380d3
...@@ -22,6 +22,14 @@ var _webrtcsupport = require('./webrtcsupport'); ...@@ -22,6 +22,14 @@ var _webrtcsupport = require('./webrtcsupport');
var _webrtcsupport2 = _interopRequireDefault(_webrtcsupport); var _webrtcsupport2 = _interopRequireDefault(_webrtcsupport);
var _whatThePack = require('what-the-pack');
var _whatThePack2 = _interopRequireDefault(_whatThePack);
var _pako = require('pako');
var _pako2 = _interopRequireDefault(_pako);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
...@@ -30,6 +38,10 @@ function _possibleConstructorReturn(self, call) { if (!self) { throw new Referen ...@@ -30,6 +38,10 @@ function _possibleConstructorReturn(self, call) { if (!self) { throw new Referen
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
var _MessagePack$initiali = _whatThePack2.default.initialize(Math.pow(2, 22)),
encode = _MessagePack$initiali.encode,
decode = _MessagePack$initiali.decode;
function isAllTracksEnded(stream) { function isAllTracksEnded(stream) {
var isAllTracksEnded = true; var isAllTracksEnded = true;
stream.getTracks().forEach(function (t) { stream.getTracks().forEach(function (t) {
...@@ -38,6 +50,11 @@ function isAllTracksEnded(stream) { ...@@ -38,6 +50,11 @@ function isAllTracksEnded(stream) {
return isAllTracksEnded; return isAllTracksEnded;
} }
var protoSend = RTCDataChannel.prototype.send;
RTCDataChannel.prototype.send = function (data) {
protoSend.apply(this, [_pako2.default.deflate(encode(data))]);
};
var Peer = function (_WildEmitter) { var Peer = function (_WildEmitter) {
_inherits(Peer, _WildEmitter); _inherits(Peer, _WildEmitter);
...@@ -203,7 +220,7 @@ var Peer = function (_WildEmitter) { ...@@ -203,7 +220,7 @@ var Peer = function (_WildEmitter) {
this.logger.log('sending via datachannel', channel, messageType, message); this.logger.log('sending via datachannel', channel, messageType, message);
var dc = this.getDataChannel(channel); var dc = this.getDataChannel(channel);
if (dc.readyState !== 'open') return false; if (dc.readyState !== 'open') return false;
dc.send(JSON.stringify(message)); dc.send(message);
return true; return true;
} }
...@@ -213,10 +230,11 @@ var Peer = function (_WildEmitter) { ...@@ -213,10 +230,11 @@ var Peer = function (_WildEmitter) {
key: '_observeDataChannel', key: '_observeDataChannel',
value: function _observeDataChannel(channel, peer) { value: function _observeDataChannel(channel, peer) {
var self = this; var self = this;
channel.binaryType = 'arraybuffer';
channel.onclose = this.emit.bind(this, 'channelClose', channel, peer); channel.onclose = this.emit.bind(this, 'channelClose', channel, peer);
channel.onerror = this.emit.bind(this, 'channelError', channel, peer); channel.onerror = this.emit.bind(this, 'channelError', channel, peer);
channel.onmessage = function (event) { channel.onmessage = function (event) {
self.emit('channelMessage', self, channel.label, JSON.parse(event.data), channel, event); self.emit('channelMessage', self, channel.label, decode(_whatThePack2.default.Buffer.from(_pako2.default.inflate(event.data))), channel, event);
}; };
channel.onopen = this.emit.bind(this, 'channelOpen', channel, peer); channel.onopen = this.emit.bind(this, 'channelOpen', channel, peer);
} }
...@@ -335,10 +353,10 @@ var Peer = function (_WildEmitter) { ...@@ -335,10 +353,10 @@ var Peer = function (_WildEmitter) {
}); });
// override onopen // override onopen
dc.onopen = function () { dc.onopen = function () {
dc.send(JSON.stringify({ dc.send({
size: file.size, size: file.size,
name: file.name name: file.name
})); });
sender.send(file, dc); sender.send(file, dc);
}; };
// override onclose // override onclose
......
...@@ -96,7 +96,7 @@ var WebRTC = function (_LocalMedia) { ...@@ -96,7 +96,7 @@ var WebRTC = function (_LocalMedia) {
if (peer.enableDataChannels) { if (peer.enableDataChannels) {
var dc = peer.getDataChannel('liowebrtc'); var dc = peer.getDataChannel('liowebrtc');
if (dc.readyState !== 'open') return; if (dc.readyState !== 'open') return;
dc.sendDirectlyToAll(JSON.stringify({ type: 'speaking' })); dc.sendDirectlyToAll({ type: 'speaking' });
} }
}); });
} }
...@@ -107,7 +107,7 @@ var WebRTC = function (_LocalMedia) { ...@@ -107,7 +107,7 @@ var WebRTC = function (_LocalMedia) {
if (peer.enableDataChannels) { if (peer.enableDataChannels) {
var dc = peer.getDataChannel('liowebrtc'); var dc = peer.getDataChannel('liowebrtc');
if (dc.readyState !== 'open') return; if (dc.readyState !== 'open') return;
dc.sendDirectlyToAll(JSON.stringify({ type: 'stoppedSpeaking' })); dc.sendDirectlyToAll({ type: 'stoppedSpeaking' });
} }
}); });
} }
...@@ -118,7 +118,7 @@ var WebRTC = function (_LocalMedia) { ...@@ -118,7 +118,7 @@ var WebRTC = function (_LocalMedia) {
if (peer.enableDataChannels) { if (peer.enableDataChannels) {
var dc = peer.getDataChannel('liowebrtc'); var dc = peer.getDataChannel('liowebrtc');
if (dc.readyState !== 'open') return; if (dc.readyState !== 'open') return;
dc.sendDirectlyToAll(JSON.stringify({ type: 'payload', volume: volume })); dc.sendDirectlyToAll({ type: 'payload', volume: volume });
} }
}); });
} }
......
...@@ -2,6 +2,10 @@ import PeerConnection from 'rtcpeerconnection'; ...@@ -2,6 +2,10 @@ import PeerConnection from 'rtcpeerconnection';
import WildEmitter from 'wildemitter'; import WildEmitter from 'wildemitter';
import FileTransfer from 'filetransfer'; import FileTransfer from 'filetransfer';
import webrtcSupport from './webrtcsupport'; import webrtcSupport from './webrtcsupport';
import MessagePack from 'what-the-pack';
import pako from 'pako';
const { encode, decode } = MessagePack.initialize(2**22);
function isAllTracksEnded(stream) { function isAllTracksEnded(stream) {
let isAllTracksEnded = true; let isAllTracksEnded = true;
...@@ -11,6 +15,11 @@ function isAllTracksEnded(stream) { ...@@ -11,6 +15,11 @@ function isAllTracksEnded(stream) {
return isAllTracksEnded; return isAllTracksEnded;
} }
const protoSend = RTCDataChannel.prototype.send;
RTCDataChannel.prototype.send = function (data) {
protoSend.apply(this, [pako.deflate(encode(data))])
};
class Peer extends WildEmitter { class Peer extends WildEmitter {
constructor(options) { constructor(options) {
super(); super();
...@@ -159,17 +168,18 @@ class Peer extends WildEmitter { ...@@ -159,17 +168,18 @@ class Peer extends WildEmitter {
this.logger.log('sending via datachannel', channel, messageType, message); this.logger.log('sending via datachannel', channel, messageType, message);
const dc = this.getDataChannel(channel); const dc = this.getDataChannel(channel);
if (dc.readyState !== 'open') return false; if (dc.readyState !== 'open') return false;
dc.send(JSON.stringify(message)); dc.send(message);
return true; return true;
} }
// Internal method registering handlers for a data channel and emitting events on the peer // Internal method registering handlers for a data channel and emitting events on the peer
_observeDataChannel(channel, peer) { _observeDataChannel(channel, peer) {
const self = this; const self = this;
channel.binaryType = 'arraybuffer';
channel.onclose = this.emit.bind(this, 'channelClose', channel, peer); channel.onclose = this.emit.bind(this, 'channelClose', channel, peer);
channel.onerror = this.emit.bind(this, 'channelError', channel, peer); channel.onerror = this.emit.bind(this, 'channelError', channel, peer);
channel.onmessage = (event) => { channel.onmessage = (event) => {
self.emit('channelMessage', self, channel.label, JSON.parse(event.data), channel, event); self.emit('channelMessage', self, channel.label, decode(MessagePack.Buffer.from(pako.inflate(event.data))), channel, event);
}; };
channel.onopen = this.emit.bind(this, 'channelOpen', channel, peer); channel.onopen = this.emit.bind(this, 'channelOpen', channel, peer);
} }
...@@ -272,10 +282,10 @@ class Peer extends WildEmitter { ...@@ -272,10 +282,10 @@ class Peer extends WildEmitter {
}); });
// override onopen // override onopen
dc.onopen = () => { dc.onopen = () => {
dc.send(JSON.stringify({ dc.send({
size: file.size, size: file.size,
name: file.name, name: file.name,
})); });
sender.send(file, dc); sender.send(file, dc);
}; };
// override onclose // override onclose
......
...@@ -61,7 +61,7 @@ class WebRTC extends LocalMedia { ...@@ -61,7 +61,7 @@ class WebRTC extends LocalMedia {
if (peer.enableDataChannels) { if (peer.enableDataChannels) {
const dc = peer.getDataChannel('liowebrtc'); const dc = peer.getDataChannel('liowebrtc');
if (dc.readyState !== 'open') return; if (dc.readyState !== 'open') return;
dc.sendDirectlyToAll(JSON.stringify({ type: 'speaking' })); dc.sendDirectlyToAll({ type: 'speaking' });
} }
}); });
} }
...@@ -72,7 +72,7 @@ class WebRTC extends LocalMedia { ...@@ -72,7 +72,7 @@ class WebRTC extends LocalMedia {
if (peer.enableDataChannels) { if (peer.enableDataChannels) {
const dc = peer.getDataChannel('liowebrtc'); const dc = peer.getDataChannel('liowebrtc');
if (dc.readyState !== 'open') return; if (dc.readyState !== 'open') return;
dc.sendDirectlyToAll(JSON.stringify({ type: 'stoppedSpeaking' })); dc.sendDirectlyToAll({ type: 'stoppedSpeaking' });
} }
}); });
} }
...@@ -83,7 +83,7 @@ class WebRTC extends LocalMedia { ...@@ -83,7 +83,7 @@ class WebRTC extends LocalMedia {
if (peer.enableDataChannels) { if (peer.enableDataChannels) {
const dc = peer.getDataChannel('liowebrtc'); const dc = peer.getDataChannel('liowebrtc');
if (dc.readyState !== 'open') return; if (dc.readyState !== 'open') return;
dc.sendDirectlyToAll(JSON.stringify({ type: 'payload', volume })); dc.sendDirectlyToAll({ type: 'payload', volume });
} }
}); });
} }
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment