rtp.js
Version:
RTP stack for Node.js and browser written in TypeScript
77 lines (76 loc) • 2.21 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Packet = exports.RTP_VERSION = void 0;
const Serializable_1 = require("./Serializable");
const helpers_1 = require("../utils/helpers");
const bitOps_1 = require("../utils/bitOps");
exports.RTP_VERSION = 2;
/**
* Parent class of all RTP and RTCP packets.
*/
class Packet extends Serializable_1.Serializable {
// Number of bytes of padding.
padding = 0;
constructor(view) {
super(view);
}
/**
* Base RTCP packet dump.
*
* @remarks
* - Read the info dump type of each RTCP packet instead.
*/
dump() {
return {
...super.dump(),
padding: this.getPadding(),
};
}
/**
* Get the padding (in bytes) at the end of the packet.
*/
getPadding() {
return this.padding;
}
/**
* Pad the packet total length to 4 bytes. To achieve it, this method may add
* or remove bytes of padding.
*
* @remarks
* - Serialization maybe needed after calling this method.
*/
padTo4Bytes() {
const previousPacketLength = this.getByteLength();
const packetLength = (0, helpers_1.padTo4Bytes)(previousPacketLength - this.padding);
const padding = this.padding + packetLength - previousPacketLength;
if (padding === this.padding) {
return;
}
this.setPadding(padding);
this.setSerializationNeeded(true);
}
setVersion() {
(0, bitOps_1.writeBitsInDataView)({
view: this.view,
pos: 0,
mask: 0b11000000,
value: exports.RTP_VERSION,
});
}
hasPaddingBit() {
return (0, bitOps_1.readBitInDataView)({ view: this.view, pos: 0, bit: 5 });
}
setPaddingBit(flag) {
(0, bitOps_1.writeBitInDataView)({ view: this.view, pos: 0, bit: 5, flag });
}
setPadding(padding) {
if (padding === this.padding) {
return;
}
this.padding = padding;
// Update padding bit.
this.setPaddingBit(Boolean(this.padding));
this.setSerializationNeeded(true);
}
}
exports.Packet = Packet;