Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 22 additions & 19 deletions src/AbstractMessage.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,30 @@
/* eslint-disable no-restricted-syntax */
// A Protocol message interface
function AbstractMessage() {}
class AbstractMessage {
constructor() {
this.isMessage = true;
this.isValid = true;
this.buffer = Buffer.alloc(0);
}

AbstractMessage.prototype.mixin = function copyFrom(data) {
for (const k in data) {
// eslint-disable-next-line no-prototype-builtins
if (data.hasOwnProperty(k)) {
this[k] = data[k];
mixin(data) {
for (const k in data) {
// eslint-disable-next-line no-prototype-builtins
if (data.hasOwnProperty(k)) {
this[k] = data[k];
}
}
return this;
}
}
return this;
};

AbstractMessage.prototype.parseBuffer = function parseBuffer(buffer) {
this.buffer = buffer;
return this;
};
parseBuffer(buffer) {
this.buffer = buffer;
return this;
}

AbstractMessage.prototype.generateBuffer = function generateBuffer() {
return this;
};
AbstractMessage.prototype.isMessage = true;
AbstractMessage.prototype.isValid = true;
AbstractMessage.prototype.buffer = Buffer.alloc(0);
generateBuffer() {
return this;
}
}

module.exports = AbstractMessage;
181 changes: 89 additions & 92 deletions src/ControlMessage.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
/* eslint-disable no-bitwise */
const util = require('util');

const AbstractMessage = require('./AbstractMessage');

const byteToCommand = {
Expand Down Expand Up @@ -30,102 +28,101 @@ const flags = {
start: 0xFFFF,
};

function ControlMessage() {
AbstractMessage.apply(this);
}
class ControlMessage extends AbstractMessage {
constructor() {
super()
this.name = '';
this.isValid = true;
this.start = flags.start;
this.version = 2;
}

util.inherits(ControlMessage, AbstractMessage);
parseBuffer(buffer, ...args) {
super.parseBuffer(...args);
this.start = buffer.readUInt16BE(0);
if (this.start !== flags.start) {
this.isValid = false;
return this;
}
this.command = byteToCommand[buffer.readUInt16BE(2)];

ControlMessage.prototype.name = '';
ControlMessage.prototype.isValid = true;
ControlMessage.prototype.start = flags.start;
ControlMessage.prototype.version = 2;
switch (this.command) {
case 'invitation':
case 'invitation_accepted':
case 'invitation_rejected':
case 'end':
this.version = buffer.readUInt32BE(4);
this.token = buffer.readUInt32BE(8);
this.ssrc = buffer.readUInt32BE(12);
this.name = buffer.toString('utf-8', 16);
break;
case 'synchronization':
this.ssrc = buffer.readUInt32BE(4, 8);
this.count = buffer.readUInt8(8);
// eslint-disable-next-line no-bitwise
this.padding = (buffer.readUInt8(9) << 0xF0) + buffer.readUInt16BE(10);
this.timestamp1 = buffer.slice(12, 20);
this.timestamp2 = buffer.slice(20, 28);
this.timestamp3 = buffer.slice(28, 36);
break;
case 'receiver_feedback':
this.ssrc = buffer.readUInt32BE(4, 8);
this.sequenceNumber = buffer.readUInt16BE(8);
break;
default:
break;
}
return this;
}

ControlMessage.prototype.parseBuffer = function parseBuffer(buffer, ...args) {
AbstractMessage.prototype.parseBuffer.apply(this, args);
this.start = buffer.readUInt16BE(0);
if (this.start !== flags.start) {
this.isValid = false;
return this;
}
this.command = byteToCommand[buffer.readUInt16BE(2)];
generateBuffer() {
let buffer;
const commandByte = commandToByte[this.command];

switch (this.command) {
case 'invitation':
case 'invitation_accepted':
case 'invitation_rejected':
case 'end':
this.version = buffer.readUInt32BE(4);
this.token = buffer.readUInt32BE(8);
this.ssrc = buffer.readUInt32BE(12);
this.name = buffer.toString('utf-8', 16);
break;
case 'synchronization':
this.ssrc = buffer.readUInt32BE(4, 8);
this.count = buffer.readUInt8(8);
// eslint-disable-next-line no-bitwise
this.padding = (buffer.readUInt8(9) << 0xF0) + buffer.readUInt16BE(10);
this.timestamp1 = buffer.slice(12, 20);
this.timestamp2 = buffer.slice(20, 28);
this.timestamp3 = buffer.slice(28, 36);
break;
case 'receiver_feedback':
this.ssrc = buffer.readUInt32BE(4, 8);
this.sequenceNumber = buffer.readUInt16BE(8);
break;
default:
break;
}
return this;
};
switch (this.command) {
case 'invitation':
case 'invitation_accepted':
case 'invitation_rejected':
case 'end':
this.name = this.name || '';
buffer = Buffer.alloc(17 + Buffer.byteLength(this.name, 'utf8'));
buffer.writeUInt16BE(this.start, 0);
buffer.writeUInt16BE(commandByte, 2);
buffer.writeUInt32BE(this.version, 4);
buffer.writeUInt32BE(this.token, 8);
buffer.writeUInt32BE(this.ssrc, 12);
buffer.write(this.name, 16);
if (this.command !== 'end') {
buffer.writeUInt8(0, buffer.length - 1);
}
break;
case 'synchronization':
buffer = Buffer.alloc(36);
buffer.writeUInt16BE(this.start, 0);
buffer.writeUInt16BE(commandByte, 2);
buffer.writeUInt32BE(this.ssrc, 4);
buffer.writeUInt8(this.count, 8);
buffer.writeUInt8(this.padding >>> 0xF0, 9);
buffer.writeUInt16BE(this.padding & 0x00FFFF, 10);

ControlMessage.prototype.generateBuffer = function generateBuffer() {
let buffer;
const commandByte = commandToByte[this.command];
this.timestamp1.copy(buffer, 12);
this.timestamp2.copy(buffer, 20);
this.timestamp3.copy(buffer, 28);

switch (this.command) {
case 'invitation':
case 'invitation_accepted':
case 'invitation_rejected':
case 'end':
this.name = this.name || '';
buffer = Buffer.alloc(17 + Buffer.byteLength(this.name, 'utf8'));
buffer.writeUInt16BE(this.start, 0);
buffer.writeUInt16BE(commandByte, 2);
buffer.writeUInt32BE(this.version, 4);
buffer.writeUInt32BE(this.token, 8);
buffer.writeUInt32BE(this.ssrc, 12);
buffer.write(this.name, 16);
if (this.command !== 'end') {
buffer.writeUInt8(0, buffer.length - 1);
break;
case 'receiver_feedback':
buffer = Buffer.alloc(12);
buffer.writeUInt16BE(this.start, 0);
buffer.writeUInt16BE(commandByte, 2);
buffer.writeUInt32BE(this.ssrc, 4);
buffer.writeUInt16BE(this.sequenceNumber, 8);
break;
default:
break;
}
break;
case 'synchronization':
buffer = Buffer.alloc(36);
buffer.writeUInt16BE(this.start, 0);
buffer.writeUInt16BE(commandByte, 2);
buffer.writeUInt32BE(this.ssrc, 4);
buffer.writeUInt8(this.count, 8);
buffer.writeUInt8(this.padding >>> 0xF0, 9);
buffer.writeUInt16BE(this.padding & 0x00FFFF, 10);

this.timestamp1.copy(buffer, 12);
this.timestamp2.copy(buffer, 20);
this.timestamp3.copy(buffer, 28);

break;
case 'receiver_feedback':
buffer = Buffer.alloc(12);
buffer.writeUInt16BE(this.start, 0);
buffer.writeUInt16BE(commandByte, 2);
buffer.writeUInt32BE(this.ssrc, 4);
buffer.writeUInt16BE(this.sequenceNumber, 8);
break;
default:
break;
}
this.buffer = buffer;
return this;
};
this.buffer = buffer;
return this;
}
}

module.exports = ControlMessage;
Loading