@observertc/samples-encoder
Version:
ObserveRTC Library for Encoding Samples
268 lines (267 loc) • 8.01 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.convertUint8ToBase64 = exports.uuidToByteArray = exports.stringToBytesArray = exports.BooleanToBooleanEncoder = exports.NumberToNumberEncoder = exports.NumberToBigIntEncoder = exports.StringToNumberEncoder = exports.StringToUint8ArrayEncoder = exports.StringToStringEncoder = exports.OneTimePassStringToUint8ArrayEncoder = exports.OneTimePassUuidToByteArrayEncoder = exports.OneTimePassEncoder = exports.DefaultAttachmentEncoder = exports.DefaultAttachmentEncoderFactory = void 0;
const textEncoder = new TextEncoder();
class DefaultAttachmentEncoderFactory {
createIceCandidatePairAttachmentEncoder() {
return new DefaultAttachmentEncoder();
}
createCodecStatsAttachmentEncoder() {
return new DefaultAttachmentEncoder();
}
createClientSampleAttachmentEncoder() {
return new DefaultAttachmentEncoder();
}
createInboundTrackAttachmentEncoder() {
return new DefaultAttachmentEncoder();
}
createOutboundTrackAttachmentEncoder() {
return new DefaultAttachmentEncoder();
}
createPeerConnectionSampleAttachmentEncoder() {
return new DefaultAttachmentEncoder();
}
createCertificateAttachmentEncoder() {
return new DefaultAttachmentEncoder();
}
createIceCandidateAttachmentEncoder() {
return new DefaultAttachmentEncoder();
}
createIceTransportAttachmentEncoder() {
return new DefaultAttachmentEncoder();
}
createInboundRtpAttachmentEncoder() {
return new DefaultAttachmentEncoder();
}
createOutboundRtpAttachmentEncoder() {
return new DefaultAttachmentEncoder();
}
createDataChannelAttachmentEncoder() {
return new DefaultAttachmentEncoder();
}
createRemoteInboundRtpAttachmentEncoder() {
return new DefaultAttachmentEncoder();
}
createRemoteOutboundRtpAttachmentEncoder() {
return new DefaultAttachmentEncoder();
}
createMediaSourceAttachmentEncoder() {
return new DefaultAttachmentEncoder();
}
createMediaPlayoutAttachmentEncoder() {
return new DefaultAttachmentEncoder();
}
createPeerConnectionTransportAttachmentEncoder() {
return new DefaultAttachmentEncoder();
}
}
exports.DefaultAttachmentEncoderFactory = DefaultAttachmentEncoderFactory;
class DefaultAttachmentEncoder {
constructor() {
}
reset() {
this._actualValueRef = undefined;
}
encode(newValue) {
if (this._actualValueRef && newValue === this._actualValueRef)
return;
if (newValue === undefined)
return;
this._actualValueRef = newValue;
return JSON.stringify(newValue);
}
}
exports.DefaultAttachmentEncoder = DefaultAttachmentEncoder;
class OneTimePassEncoder {
get actualValue() {
return this._value;
}
reset() {
this._value = undefined;
}
encode(newValue) {
if (this._value !== undefined)
return;
this._value = newValue;
return newValue;
}
}
exports.OneTimePassEncoder = OneTimePassEncoder;
class OneTimePassUuidToByteArrayEncoder {
reset() {
this._done = undefined;
}
encode(newValue) {
if (newValue === undefined)
return;
if (this._done)
return;
this._done = true;
return uuidToByteArray(newValue);
}
}
exports.OneTimePassUuidToByteArrayEncoder = OneTimePassUuidToByteArrayEncoder;
class OneTimePassStringToUint8ArrayEncoder {
reset() {
this._done = undefined;
}
encode(newValue) {
if (newValue === undefined)
return;
if (this._done)
return;
this._done = true;
return textEncoder.encode(newValue);
}
}
exports.OneTimePassStringToUint8ArrayEncoder = OneTimePassStringToUint8ArrayEncoder;
class StringToStringEncoder {
get actualValue() {
return this._value;
}
reset() {
this._value = undefined;
}
encode(newValue) {
if (!newValue)
return;
if (newValue === this._value)
return;
this._value = newValue;
return newValue;
}
}
exports.StringToStringEncoder = StringToStringEncoder;
class StringToUint8ArrayEncoder {
get actualValue() {
return this._value;
}
reset() {
this._value = undefined;
}
encode(newValue) {
if (!newValue)
return;
if (newValue === this._value)
return;
this._value = newValue;
return textEncoder.encode(newValue);
}
}
exports.StringToUint8ArrayEncoder = StringToUint8ArrayEncoder;
class StringToNumberEncoder {
get actualValue() {
return this._value;
}
reset() {
this._value = undefined;
}
encode(newValue) {
if (newValue === undefined)
return;
if (newValue === this._value)
return;
this._value = newValue;
return Number(newValue);
}
}
exports.StringToNumberEncoder = StringToNumberEncoder;
class NumberToBigIntEncoder {
get actualValue() {
return this._value;
}
reset() {
this._value = undefined;
}
encode(newValue) {
if (newValue === undefined)
return;
if (newValue === this._value)
return;
this._value = newValue;
return BigInt(newValue);
}
}
exports.NumberToBigIntEncoder = NumberToBigIntEncoder;
class NumberToNumberEncoder {
get actualValue() {
return this._value;
}
reset() {
this._value = undefined;
}
encode(newValue) {
if (newValue === undefined)
return;
if (newValue === this._value)
return;
this._value = newValue;
return newValue;
}
}
exports.NumberToNumberEncoder = NumberToNumberEncoder;
class BooleanToBooleanEncoder {
get actualValue() {
return this._value;
}
reset() {
this._value = undefined;
}
encode(newValue) {
if (!newValue)
return;
if (newValue === this._value)
return;
this._value = newValue;
return newValue;
}
}
exports.BooleanToBooleanEncoder = BooleanToBooleanEncoder;
function stringToBytesArray(str) {
return textEncoder.encode(str);
}
exports.stringToBytesArray = stringToBytesArray;
function uuidToByteArray(uuid) {
const hex = uuid.replace(/[-]/g, '');
const byteArray = new Uint8Array(16);
for (let i = 0, j = 0; i < 32; i += 2, j++) {
byteArray[j] = parseInt(hex.substr(i, 2), 16);
}
return byteArray;
}
exports.uuidToByteArray = uuidToByteArray;
/* Base64 string to array encoding */
function uint6ToB64(nUint6) {
return nUint6 < 26
? nUint6 + 65
: nUint6 < 52
? nUint6 + 71
: nUint6 < 62
? nUint6 - 4
: nUint6 === 62
? 43
: nUint6 === 63
? 47
: 65;
}
function convertUint8ToBase64(aBytes) {
let nMod3 = 2;
let sB64Enc = "";
const nLen = aBytes.length;
let nUint24 = 0;
for (let nIdx = 0; nIdx < nLen; nIdx++) {
nMod3 = nIdx % 3;
// To break your base64 into several 80-character lines, add:
// if (nIdx > 0 && ((nIdx * 4) / 3) % 76 === 0) {
// sB64Enc += "\r\n";
// }
nUint24 |= aBytes[nIdx] << ((16 >>> nMod3) & 24);
if (nMod3 === 2 || aBytes.length - nIdx === 1) {
sB64Enc += String.fromCodePoint(uint6ToB64((nUint24 >>> 18) & 63), uint6ToB64((nUint24 >>> 12) & 63), uint6ToB64((nUint24 >>> 6) & 63), uint6ToB64(nUint24 & 63));
nUint24 = 0;
}
}
return (sB64Enc.substring(0, sB64Enc.length - 2 + nMod3) +
(nMod3 === 2 ? "" : nMod3 === 1 ? "=" : "=="));
}
exports.convertUint8ToBase64 = convertUint8ToBase64;