UNPKG

@bengreenier/krtp

Version:

Node implementation of rdp protocol. RFC 3550

82 lines (81 loc) 3.12 kB
"use strict"; /* * +=============================================== * | Author: Parham Alvani (parham.alvani@gmail.com) * | * | Creation Date: 03-06-2017 * | * | File Name: Control.ts * +=============================================== */ Object.defineProperty(exports, "__esModule", { value: true }); /** * ControlSR implements sender report message of real-time control protocol. * The sender report is sent periodically by the active senders in a conference to report transmission * and reception statistics for all RTP packets sent during the interval. */ class ControlSR { constructor(packetCount, octetCount, ssrc, timestamp, ntpTimestamp = Date.now()) { this.rc = 0; // Reception report count this.packetCount = packetCount; this.octetCount = octetCount; this.ssrc = ssrc; this.timestamp = timestamp; this.ntpTimestamp = ntpTimestamp; } static deserialize(buff) { // header // buff[0] = (V << 6 | P << 5 | RC) if ((buff[0] & (0xc0 >> 6)) !== 2) { throw new Error("Invalid RTCP packet"); } // buff[1] = PT if (buff[1] !== 200) { throw new Error("Invalid RTCP packet"); } // buff[2, 3] = length const length = (buff.readUInt16BE(2) + 1) * 4; if (buff.length !== length) { throw new Error("Invalid RTCP packet"); } // buff[4, 5, 6, 7] = SSRC const ssrc = buff.readUInt32BE(4); // sender info // buff[8, 9, 10, 11] = ntpTS // buff[12, 13, 14, 15] = ntpTS const ntpTimestamp = buff.readUInt32BE(8) * 1000 + buff.readUInt32BE(12); // buff[16, 17, 18, 19] = TS const timestamp = buff.readUInt32BE(16); // buff[20, 21, 22, 23] = packetCount const packetCount = buff.readUInt32BE(20); // buff[24, 25, 26, 27] = octetCount const octetCount = buff.readUInt32BE(24); return new ControlSR(packetCount, octetCount, ssrc, timestamp, ntpTimestamp); } serialize() { const buff = Buffer.alloc(8 + 20); // header // buff[0] = (V << 6 | P << 5 | RC) buff[0] = (2 << 6) | (0 << 5) | this.rc; // buff[1] = PT // Sender Report Packet Type buff[1] = 200; // buff[2, 3] = length buff.writeUInt16BE(((buff.length / 4) | 0) - 1, 2); // buff[4, 5, 6, 7] = SSRC buff.writeUInt32BE(this.ssrc, 4); // sender info // buff[8, 9, 10, 11] = ntpTS buff.writeUInt32BE((this.ntpTimestamp / 1000) | 0, 8); // buff[12, 13, 14, 15] = ntpTS buff.writeUInt32BE(this.ntpTimestamp % 1000, 12); // buff[16, 17, 18, 19] = TS buff.writeUInt32BE(this.timestamp, 16); // buff[20, 21, 22, 23] = packetCount buff.writeUInt32BE(this.packetCount, 20); // buff[24, 25, 26, 27] = octetCount buff.writeUInt32BE(this.octetCount, 24); return buff; } } exports.ControlSR = ControlSR;