UNPKG

ebml-stream

Version:
72 lines (71 loc) 2.38 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const EbmlDataTag_1 = require("./EbmlDataTag"); const BlockLacing_1 = require("../enums/BlockLacing"); const tools_1 = require("../../tools"); const EbmlTagId_1 = require("../enums/EbmlTagId"); const EbmlElementType_1 = require("../enums/EbmlElementType"); class Block extends EbmlDataTag_1.EbmlDataTag { constructor(subTypeId) { super(subTypeId || EbmlTagId_1.EbmlTagId.Block, EbmlElementType_1.EbmlElementType.Binary); } writeTrackBuffer() { return tools_1.Tools.writeVint(this.track); } writeValueBuffer() { let value = Buffer.alloc(2); value.writeInt16BE(this.value, 0); return value; } writeFlagsBuffer() { let flags = 0x00; if (this.invisible) { flags |= 0x10; } switch (this.lacing) { case BlockLacing_1.BlockLacing.None: break; case BlockLacing_1.BlockLacing.Xiph: flags |= 0x04; break; case BlockLacing_1.BlockLacing.EBML: flags |= 0x08; break; case BlockLacing_1.BlockLacing.FixedSize: flags |= 0x0c; break; } return Buffer.of(flags); } encodeContent() { return Buffer.concat([ this.writeTrackBuffer(), this.writeValueBuffer(), this.writeFlagsBuffer(), this.payload ]); } parseContent(data) { const track = tools_1.Tools.readVint(data); this.track = track.value; this.value = tools_1.Tools.readSigned(data.subarray(track.length, track.length + 2)); let flags = data[track.length + 2]; this.invisible = Boolean(flags & 0x10); switch (flags & 0x0c) { case 0x00: this.lacing = BlockLacing_1.BlockLacing.None; break; case 0x04: this.lacing = BlockLacing_1.BlockLacing.Xiph; break; case 0x08: this.lacing = BlockLacing_1.BlockLacing.EBML; break; case 0x0c: this.lacing = BlockLacing_1.BlockLacing.FixedSize; break; } this.payload = data.slice(track.length + 3); } } exports.Block = Block;