@observertc/samples-decoder
Version:
ObserveRTC Library for Decoding Samples
267 lines (266 loc) • 8.46 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.convertUint8ToBase64 = exports.uuidByteArrayToString = exports.bytesArrayToString = exports.BooleanToBooleanDecoder = exports.NumberToNumberDecoder = exports.BigIntToNumberDecoder = exports.NumberToStringDecoder = exports.Uint8ArrayToStringDecoder = exports.StringToStringDecoder = exports.OneTimePassByteArrayToStringDecoder = exports.OneTimePassUuidByteArrayToStringDecoder = exports.OneTimePassDecoder = exports.DefaultAttachmentDecoder = exports.DefaultAttachmentDecoderFactory = exports.logger = void 0;
const textDecoder = new TextDecoder();
exports.logger = {
debug: (...args) => {
// console.debug(...args);
},
error: (...args) => {
// console.error(...args);
},
warn: (...args) => {
console.warn(...args);
},
info: (...args) => {
console.info(...args);
},
};
class DefaultAttachmentDecoderFactory {
createIceCandidatePairAttachmentDecoder() {
return new DefaultAttachmentDecoder();
}
createCodecStatsAttachmentDecoder() {
return new DefaultAttachmentDecoder();
}
createClientSampleAttachmentDecoder() {
return new DefaultAttachmentDecoder();
}
createInboundTrackAttachmentDecoder() {
return new DefaultAttachmentDecoder();
}
createOutboundTrackAttachmentDecoder() {
return new DefaultAttachmentDecoder();
}
createPeerConnectionSampleAttachmentDecoder() {
return new DefaultAttachmentDecoder();
}
createCertificateAttachmentDecoder() {
return new DefaultAttachmentDecoder();
}
createIceCandidateAttachmentDecoder() {
return new DefaultAttachmentDecoder();
}
createIceTransportAttachmentDecoder() {
return new DefaultAttachmentDecoder();
}
createInboundRtpAttachmentDecoder() {
return new DefaultAttachmentDecoder();
}
createOutboundRtpAttachmentDecoder() {
return new DefaultAttachmentDecoder();
}
createDataChannelAttachmentDecoder() {
return new DefaultAttachmentDecoder();
}
createRemoteInboundRtpAttachmentDecoder() {
return new DefaultAttachmentDecoder();
}
createRemoteOutboundRtpAttachmentDecoder() {
return new DefaultAttachmentDecoder();
}
createMediaSourceAttachmentDecoder() {
return new DefaultAttachmentDecoder();
}
createMediaPlayoutAttachmentDecoder() {
return new DefaultAttachmentDecoder();
}
createPeerConnectionTransportAttachmentDecoder() {
return new DefaultAttachmentDecoder();
}
}
exports.DefaultAttachmentDecoderFactory = DefaultAttachmentDecoderFactory;
class DefaultAttachmentDecoder {
get actualValue() {
return this._actual?.data;
}
set actualValue(value) {
if (!value)
return;
this._actual = {
value: JSON.stringify(value),
data: value,
};
}
constructor() {
}
reset() {
this._actual = undefined;
}
decode(newValue) {
if (!newValue)
return this._actual?.data;
if (this._actual && newValue === this._actual.value)
return this._actual.data;
if (newValue === undefined)
return;
this._actual = {
value: newValue,
data: JSON.parse(newValue),
};
return this._actual.data;
}
}
exports.DefaultAttachmentDecoder = DefaultAttachmentDecoder;
class OneTimePassDecoder {
reset() {
this.actualValue = undefined;
}
decode(newValue) {
if (this.actualValue !== undefined)
return;
this.actualValue = newValue;
return newValue;
}
}
exports.OneTimePassDecoder = OneTimePassDecoder;
class OneTimePassUuidByteArrayToStringDecoder {
reset() {
this.actualValue = undefined;
}
decode(newValue) {
if (newValue === undefined || this.actualValue)
return this.actualValue;
this.actualValue = uuidByteArrayToString(newValue);
return this.actualValue;
}
}
exports.OneTimePassUuidByteArrayToStringDecoder = OneTimePassUuidByteArrayToStringDecoder;
class OneTimePassByteArrayToStringDecoder {
reset() {
this.actualValue = undefined;
}
decode(newValue) {
if (newValue === undefined || this.actualValue)
return this.actualValue;
this.actualValue = textDecoder.decode(newValue);
return this.actualValue;
}
}
exports.OneTimePassByteArrayToStringDecoder = OneTimePassByteArrayToStringDecoder;
class StringToStringDecoder {
reset() {
this.actualValue = undefined;
}
decode(newValue) {
if (newValue === undefined)
return this.actualValue;
this.actualValue = newValue;
return newValue;
}
}
exports.StringToStringDecoder = StringToStringDecoder;
class Uint8ArrayToStringDecoder {
reset() {
this.actualValue = undefined;
}
decode(newValue) {
if (!newValue)
return this.actualValue;
this.actualValue = textDecoder.decode(newValue);
return this.actualValue;
}
}
exports.Uint8ArrayToStringDecoder = Uint8ArrayToStringDecoder;
class NumberToStringDecoder {
reset() {
this.actualValue = undefined;
}
decode(newValue) {
if (newValue === undefined)
return this.actualValue;
this.actualValue = `${newValue}`;
return this.actualValue;
}
}
exports.NumberToStringDecoder = NumberToStringDecoder;
class BigIntToNumberDecoder {
reset() {
this.actualValue = undefined;
}
decode(newValue) {
if (newValue === undefined)
return this.actualValue;
this.actualValue = Number(newValue);
return this.actualValue;
}
}
exports.BigIntToNumberDecoder = BigIntToNumberDecoder;
class NumberToNumberDecoder {
reset() {
this.actualValue = undefined;
}
decode(newValue) {
if (newValue === undefined)
return this.actualValue;
this.actualValue = newValue;
return newValue;
}
}
exports.NumberToNumberDecoder = NumberToNumberDecoder;
class BooleanToBooleanDecoder {
reset() {
this.actualValue = undefined;
}
decode(newValue) {
if (!newValue)
return this.actualValue;
this.actualValue = newValue;
return newValue;
}
}
exports.BooleanToBooleanDecoder = BooleanToBooleanDecoder;
function bytesArrayToString(bytes) {
return textDecoder.decode(bytes);
}
exports.bytesArrayToString = bytesArrayToString;
function uuidByteArrayToString(byteArray) {
if (byteArray.length !== 16) {
throw new Error('Invalid byte array length for UUID conversion');
}
const toHex = (n) => n.toString(16).padStart(2, '0');
const parts = [
byteArray.subarray(0, 4),
byteArray.subarray(4, 6),
byteArray.subarray(6, 8),
byteArray.subarray(8, 10),
byteArray.subarray(10, 16),
];
return parts.map((part) => Array.from(part).map(toHex).join('')).join('-');
}
exports.uuidByteArrayToString = uuidByteArrayToString;
/* 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;