Best JavaScript code snippet using wpt
mp4iso.ts
Source:mp4iso.ts
...20 var DEFAULT_OP_COLOR: number[] = [0, 0, 0];21 function concatArrays<T>(arg0: T[], ...args: T[][]): T[] {22 return Array.prototype.concat.apply(arg0, args);23 }24 function writeInt32(data: Uint8Array, offset: number, value: number) {25 data[offset] = (value >> 24) & 255;26 data[offset + 1] = (value >> 16) & 255;27 data[offset + 2] = (value >> 8) & 255;28 data[offset + 3] = value & 255;29 }30 function decodeInt32(s: string): number {31 return (s.charCodeAt(0) << 24) | (s.charCodeAt(1) << 16) |32 (s.charCodeAt(2) << 8) | s.charCodeAt(3);33 }34 function encodeDate(d: number): number {35 return ((d - START_DATE) / 1000) | 0;36 }37 function encodeFloat_16_16(f: number): number {38 return (f * 0x10000) | 0;39 }40 function encodeFloat_2_30(f: number): number {41 return (f * 0x40000000) | 0;42 }43 function encodeFloat_8_8(f: number): number {44 return (f * 0x100) | 0;45 }46 function encodeLang(s: string): number {47 return ((s.charCodeAt(0) & 0x1F) << 10) | ((s.charCodeAt(1) & 0x1F) << 5) | (s.charCodeAt(2) & 0x1F);48 }49 export class Box {50 public offset: number;51 public size: number;52 public boxtype: string;53 public userType: Uint8Array;54 public constructor(boxtype: string, extendedType?: Uint8Array) {55 this.boxtype = boxtype;56 if (boxtype === 'uuid') {57 this.userType = extendedType;58 }59 }60 /**61 * @param offset Position where writing will start in the output array62 * @returns {number} Size of the written data63 */64 public layout(offset: number): number {65 this.offset = offset;66 var size = 8;67 if (this.userType) {68 size += 16;69 }70 this.size = size;71 return size;72 }73 /**74 * @param data Output array75 * @returns {number} Amount of written bytes by this Box and its children only.76 */77 public write(data: Uint8Array): number {78 writeInt32(data, this.offset, this.size);79 writeInt32(data, this.offset + 4, decodeInt32(this.boxtype));80 if (!this.userType) {81 return 8;82 }83 data.set(this.userType, this.offset + 8);84 return 24;85 }86 public toUint8Array(): Uint8Array {87 var size = this.layout(0);88 var data = new Uint8Array(size);89 this.write(data);90 return data;91 }92 }93 export class FullBox extends Box {94 public version: number;95 public flags: number;96 constructor(boxtype: string, version: number = 0, flags: number = 0) {97 super(boxtype);98 this.version = version;99 this.flags = flags;100 }101 public layout(offset: number): number {102 this.size = super.layout(offset) + 4;103 return this.size;104 }105 public write(data: Uint8Array): number {106 var offset = super.write(data);107 writeInt32(data, this.offset + offset, (this.version << 24) | this.flags);108 return offset + 4;109 }110 }111 export class FileTypeBox extends Box {112 public majorBrand: string;113 public minorVersion: number;114 public compatibleBrands: string[];115 constructor(majorBrand: string, minorVersion: number, compatibleBrands: string[]) {116 super('ftype');117 this.majorBrand = majorBrand;118 this.minorVersion = minorVersion;119 this.compatibleBrands = compatibleBrands;120 }121 public layout(offset: number): number {122 this.size = super.layout(offset) + 4 * (2 + this.compatibleBrands.length);123 return this.size;124 }125 public write(data: Uint8Array): number {126 var offset = super.write(data);127 writeInt32(data, this.offset + offset, decodeInt32(this.majorBrand));128 writeInt32(data, this.offset + offset + 4, this.minorVersion);129 offset += 8;130 this.compatibleBrands.forEach((brand: string) => {131 writeInt32(data, this.offset + offset, decodeInt32(brand));132 offset += 4;133 }, this);134 return offset;135 }136 }137 export class BoxContainerBox extends Box {138 public children: Box[];139 constructor(type: string, children: Box[]) {140 super(type);141 this.children = children;142 }143 public layout(offset: number): number {144 var size = super.layout(offset);145 this.children.forEach((child) => {146 if (!child) {147 return; // skipping undefined148 }149 size += child.layout(offset + size);150 });151 return (this.size = size);152 }153 public write(data: Uint8Array): number {154 var offset = super.write(data);155 this.children.forEach((child) => {156 if (!child) {157 return; // skipping undefined158 }159 offset += child.write(data);160 });161 return offset;162 }163 }164 export class MovieBox extends BoxContainerBox {165 constructor(public header: MovieHeaderBox,166 public tracks: Box[],167 public extendsBox: MovieExtendsBox,168 public userData: Box) {169 super('moov', concatArrays<Box>([header], tracks, [extendsBox, userData]));170 }171 }172 export class MovieHeaderBox extends FullBox {173 constructor(public timescale: number,174 public duration: number,175 public nextTrackId: number,176 public rate: number = 1.0,177 public volume: number = 1.0,178 public matrix: number[] = DEFAULT_MOVIE_MATRIX,179 public creationTime: number = START_DATE,180 public modificationTime: number = START_DATE) {181 super('mvhd', 0, 0);182 }183 public layout(offset: number): number {184 this.size = super.layout(offset) + 16 + 4 + 2 + 2 + 8 + 36 + 24 + 4;185 return this.size;186 }187 public write(data: Uint8Array): number {188 var offset = super.write(data);189 // Only version 0190 writeInt32(data, this.offset + offset, encodeDate(this.creationTime));191 writeInt32(data, this.offset + offset + 4, encodeDate(this.modificationTime));192 writeInt32(data, this.offset + offset + 8, this.timescale);193 writeInt32(data, this.offset + offset + 12, this.duration);194 offset += 16;195 writeInt32(data, this.offset + offset, encodeFloat_16_16(this.rate));196 writeInt32(data, this.offset + offset + 4, encodeFloat_8_8(this.volume) << 16);197 writeInt32(data, this.offset + offset + 8, 0);198 writeInt32(data, this.offset + offset + 12, 0);199 offset += 16;200 writeInt32(data, this.offset + offset, encodeFloat_16_16(this.matrix[0]));201 writeInt32(data, this.offset + offset + 4, encodeFloat_16_16(this.matrix[1]));202 writeInt32(data, this.offset + offset + 8, encodeFloat_16_16(this.matrix[2]));203 writeInt32(data, this.offset + offset + 12, encodeFloat_16_16(this.matrix[3]));204 writeInt32(data, this.offset + offset + 16, encodeFloat_16_16(this.matrix[4]));205 writeInt32(data, this.offset + offset + 20, encodeFloat_16_16(this.matrix[5]));206 writeInt32(data, this.offset + offset + 24, encodeFloat_2_30(this.matrix[6]));207 writeInt32(data, this.offset + offset + 28, encodeFloat_2_30(this.matrix[7]));208 writeInt32(data, this.offset + offset + 32, encodeFloat_2_30(this.matrix[8]));209 offset += 36;210 writeInt32(data, this.offset + offset, 0);211 writeInt32(data, this.offset + offset + 4, 0);212 writeInt32(data, this.offset + offset + 8, 0);213 writeInt32(data, this.offset + offset + 12, 0);214 writeInt32(data, this.offset + offset + 16, 0);215 writeInt32(data, this.offset + offset + 20, 0);216 offset += 24;217 writeInt32(data, this.offset + offset, this.nextTrackId);218 offset += 4;219 return offset;220 }221 }222 export const enum TrackHeaderFlags {223 TRACK_ENABLED = 0x000001,224 TRACK_IN_MOVIE = 0x000002,225 TRACK_IN_PREVIEW = 0x000004,226 }227 export class TrackHeaderBox extends FullBox {228 constructor(flags: number,229 public trackId: number,230 public duration: number,231 public width: number,232 public height: number,233 public volume: number,234 public alternateGroup: number = 0,235 public layer: number = 0,236 public matrix: number[] = DEFAULT_MOVIE_MATRIX,237 public creationTime: number = START_DATE,238 public modificationTime: number = START_DATE) {239 super('tkhd', 0, flags);240 }241 public layout(offset: number): number {242 this.size = super.layout(offset) + 20 + 8 + 6 + 2 + 36 + 8;243 return this.size;244 }245 public write(data: Uint8Array): number {246 var offset = super.write(data);247 // Only version 0248 writeInt32(data, this.offset + offset, encodeDate(this.creationTime));249 writeInt32(data, this.offset + offset + 4, encodeDate(this.modificationTime));250 writeInt32(data, this.offset + offset + 8, this.trackId);251 writeInt32(data, this.offset + offset + 12, 0);252 writeInt32(data, this.offset + offset + 16, this.duration);253 offset += 20;254 writeInt32(data, this.offset + offset, 0);255 writeInt32(data, this.offset + offset + 4, 0);256 writeInt32(data, this.offset + offset + 8, (this.layer << 16) | this.alternateGroup);257 writeInt32(data, this.offset + offset + 12, encodeFloat_8_8(this.volume) << 16);258 offset += 16;259 writeInt32(data, this.offset + offset, encodeFloat_16_16(this.matrix[0]));260 writeInt32(data, this.offset + offset + 4, encodeFloat_16_16(this.matrix[1]));261 writeInt32(data, this.offset + offset + 8, encodeFloat_16_16(this.matrix[2]));262 writeInt32(data, this.offset + offset + 12, encodeFloat_16_16(this.matrix[3]));263 writeInt32(data, this.offset + offset + 16, encodeFloat_16_16(this.matrix[4]));264 writeInt32(data, this.offset + offset + 20, encodeFloat_16_16(this.matrix[5]));265 writeInt32(data, this.offset + offset + 24, encodeFloat_2_30(this.matrix[6]));266 writeInt32(data, this.offset + offset + 28, encodeFloat_2_30(this.matrix[7]));267 writeInt32(data, this.offset + offset + 32, encodeFloat_2_30(this.matrix[8]));268 offset += 36;269 writeInt32(data, this.offset + offset, encodeFloat_16_16(this.width));270 writeInt32(data, this.offset + offset + 4, encodeFloat_16_16(this.height));271 offset += 8;272 return offset;273 }274 }275 export class MediaHeaderBox extends FullBox {276 constructor(public timescale: number,277 public duration: number,278 public language: string = 'unk',279 public creationTime: number = START_DATE,280 public modificationTime: number = START_DATE) {281 super('mdhd', 0, 0);282 }283 public layout(offset: number): number {284 this.size = super.layout(offset) + 16 + 4;285 return this.size;286 }287 public write(data: Uint8Array): number {288 var offset = super.write(data);289 // Only version 0290 writeInt32(data, this.offset + offset, encodeDate(this.creationTime));291 writeInt32(data, this.offset + offset + 4, encodeDate(this.modificationTime));292 writeInt32(data, this.offset + offset + 8, this.timescale);293 writeInt32(data, this.offset + offset + 12, this.duration);294 writeInt32(data, this.offset + offset + 16, encodeLang(this.language) << 16);295 return offset + 20;296 }297 }298 export class HandlerBox extends FullBox {299 private _encodedName: Uint8Array;300 constructor(public handlerType: string,301 public name: string) {302 super('hdlr', 0, 0);303 this._encodedName = utf8decode(this.name);304 }305 public layout(offset: number): number {306 this.size = super.layout(offset) + 8 + 12 + (this._encodedName.length + 1);307 return this.size;308 }309 public write(data: Uint8Array): number {310 var offset = super.write(data);311 writeInt32(data, this.offset + offset, 0);312 writeInt32(data, this.offset + offset + 4, decodeInt32(this.handlerType));313 writeInt32(data, this.offset + offset + 8, 0);314 writeInt32(data, this.offset + offset + 12, 0);315 writeInt32(data, this.offset + offset + 16, 0);316 offset += 20;317 data.set(this._encodedName, this.offset + offset);318 data[this.offset + offset + this._encodedName.length] = 0;319 offset += this._encodedName.length + 1;320 return offset;321 }322 }323 export class SoundMediaHeaderBox extends FullBox {324 constructor(public balance: number = 0.0) {325 super('smhd', 0, 0);326 }327 public layout(offset: number): number {328 this.size = super.layout(offset) + 4;329 return this.size;330 }331 public write(data: Uint8Array): number {332 var offset = super.write(data);333 writeInt32(data, this.offset + offset, encodeFloat_8_8(this.balance) << 16);334 return offset + 4;335 }336 }337 export class VideoMediaHeaderBox extends FullBox {338 constructor(public graphicsMode: number = 0,339 public opColor: number[] = DEFAULT_OP_COLOR) {340 super('vmhd', 0, 0);341 }342 public layout(offset: number): number {343 this.size = super.layout(offset) + 8;344 return this.size;345 }346 public write(data: Uint8Array): number {347 var offset = super.write(data);348 writeInt32(data, this.offset + offset, (this.graphicsMode << 16) | this.opColor[0]);349 writeInt32(data, this.offset + offset + 4, (this.opColor[1] << 16) | this.opColor[2]);350 return offset + 8;351 }352 }353 export var SELF_CONTAINED_DATA_REFERENCE_FLAG = 0x000001;354 export class DataEntryUrlBox extends FullBox {355 private _encodedLocation: Uint8Array;356 constructor(flags: number,357 public location: string = null) {358 super('url ', 0, flags);359 if (!(flags & SELF_CONTAINED_DATA_REFERENCE_FLAG)) {360 this._encodedLocation = utf8decode(location);361 }362 }363 public layout(offset: number): number {364 var size = super.layout(offset);365 if (this._encodedLocation) {366 size += this._encodedLocation.length + 1;367 }368 return (this.size = size);369 }370 public write(data: Uint8Array): number {371 var offset = super.write(data);372 if (this._encodedLocation) {373 data.set(this._encodedLocation, this.offset + offset);374 data[this.offset + offset + this._encodedLocation.length] = 0;375 offset += this._encodedLocation.length;376 }377 return offset;378 }379 }380 export class DataReferenceBox extends FullBox {381 constructor(public entries: Box[]) {382 super('dref', 0, 0);383 }384 public layout(offset: number): number {385 var size = super.layout(offset) + 4;386 this.entries.forEach((entry) => {387 size += entry.layout(offset + size);388 });389 return (this.size = size);390 }391 public write(data: Uint8Array): number {392 var offset = super.write(data);393 writeInt32(data, this.offset + offset, this.entries.length);394 this.entries.forEach((entry) => {395 offset += entry.write(data);396 });397 return offset;398 }399 }400 export class DataInformationBox extends BoxContainerBox {401 constructor(public dataReference: Box) {402 super('dinf', [dataReference]);403 }404 }405 export class SampleDescriptionBox extends FullBox {406 constructor(public entries: Box[]) {407 super('stsd', 0, 0);408 }409 public layout(offset: number): number {410 var size = super.layout(offset);411 size += 4;412 this.entries.forEach((entry) => {413 size += entry.layout(offset + size);414 });415 return (this.size = size);416 }417 public write(data: Uint8Array): number {418 var offset = super.write(data);419 writeInt32(data, this.offset + offset, this.entries.length);420 offset += 4;421 this.entries.forEach((entry) => {422 offset += entry.write(data);423 });424 return offset;425 }426 }427 export class SampleTableBox extends BoxContainerBox {428 constructor(public sampleDescriptions: SampleDescriptionBox,429 public timeToSample: Box,430 public sampleToChunk: Box,431 public sampleSizes: Box, // optional?432 public chunkOffset: Box) {433 super('stbl', [sampleDescriptions, timeToSample, sampleToChunk, sampleSizes, chunkOffset]);434 }435 }436 export class MediaInformationBox extends BoxContainerBox {437 constructor(public header: Box, // SoundMediaHeaderBox|VideoMediaHeaderBox438 public info: DataInformationBox,439 public sampleTable: SampleTableBox) {440 super('minf', [header, info, sampleTable]);441 }442 }443 export class MediaBox extends BoxContainerBox {444 constructor(public header: MediaHeaderBox,445 public handler: HandlerBox,446 public info: MediaInformationBox) {447 super('mdia', [header, handler, info]);448 }449 }450 export class TrackBox extends BoxContainerBox {451 constructor(public header: TrackHeaderBox,452 public media: Box) {453 super('trak', [header, media]);454 }455 }456 export class TrackExtendsBox extends FullBox {457 constructor(public trackId: number,458 public defaultSampleDescriptionIndex: number,459 public defaultSampleDuration: number,460 public defaultSampleSize: number,461 public defaultSampleFlags: number) {462 super('trex', 0, 0);463 }464 public layout(offset: number): number {465 this.size = super.layout(offset) + 20;466 return this.size;467 }468 public write(data: Uint8Array): number {469 var offset = super.write(data);470 writeInt32(data, this.offset + offset, this.trackId);471 writeInt32(data, this.offset + offset + 4, this.defaultSampleDescriptionIndex);472 writeInt32(data, this.offset + offset + 8, this.defaultSampleDuration);473 writeInt32(data, this.offset + offset + 12, this.defaultSampleSize);474 writeInt32(data, this.offset + offset + 16, this.defaultSampleFlags);475 return offset + 20;476 }477 }478 export class MovieExtendsBox extends BoxContainerBox {479 constructor(public header: Box,480 public tracDefaults: TrackExtendsBox[],481 public levels: Box) {482 super('mvex', concatArrays<Box>([header], tracDefaults, [levels]));483 }484 }485 export class MetaBox extends FullBox {486 constructor(public handler: Box,487 public otherBoxes: Box[]) {488 super('meta', 0, 0);489 }490 public layout(offset: number): number {491 var size = super.layout(offset);492 size += this.handler.layout(offset + size);493 this.otherBoxes.forEach((box) => {494 size += box.layout(offset + size);495 });496 return (this.size = size);497 }498 public write(data: Uint8Array): number {499 var offset = super.write(data);500 offset += this.handler.write(data);501 this.otherBoxes.forEach((box) => {502 offset += box.write(data);503 });504 return offset;505 }506 }507 export class MovieFragmentHeaderBox extends FullBox {508 constructor(public sequenceNumber: number) {509 super('mfhd', 0, 0);510 }511 public layout(offset: number): number {512 this.size = super.layout(offset) + 4;513 return this.size;514 }515 public write(data: Uint8Array): number {516 var offset = super.write(data);517 writeInt32(data, this.offset + offset, this.sequenceNumber);518 return offset + 4;519 }520 }521 export const enum TrackFragmentFlags {522 BASE_DATA_OFFSET_PRESENT = 0x000001,523 SAMPLE_DESCRIPTION_INDEX_PRESENT = 0x000002,524 DEFAULT_SAMPLE_DURATION_PRESENT = 0x000008,525 DEFAULT_SAMPLE_SIZE_PRESENT = 0x0000010,526 DEFAULT_SAMPLE_FLAGS_PRESENT = 0x000020,527 }528 export class TrackFragmentHeaderBox extends FullBox {529 constructor(flags: number,530 public trackId: number,531 public baseDataOffset: number,532 public sampleDescriptionIndex: number,533 public defaultSampleDuration: number,534 public defaultSampleSize: number,535 public defaultSampleFlags: number) {536 super('tfhd', 0, flags);537 }538 public layout(offset: number): number {539 var size = super.layout(offset) + 4;540 var flags = this.flags;541 if (!!(flags & TrackFragmentFlags.BASE_DATA_OFFSET_PRESENT)) {542 size += 8;543 }544 if (!!(flags & TrackFragmentFlags.SAMPLE_DESCRIPTION_INDEX_PRESENT)) {545 size += 4;546 }547 if (!!(flags & TrackFragmentFlags.DEFAULT_SAMPLE_DURATION_PRESENT)) {548 size += 4;549 }550 if (!!(flags & TrackFragmentFlags.DEFAULT_SAMPLE_SIZE_PRESENT)) {551 size += 4;552 }553 if (!!(flags & TrackFragmentFlags.DEFAULT_SAMPLE_FLAGS_PRESENT)) {554 size += 4;555 }556 return (this.size = size);557 }558 public write(data: Uint8Array): number {559 var offset = super.write(data);560 var flags = this.flags;561 writeInt32(data, this.offset + offset, this.trackId);562 offset += 4;563 if (!!(flags & TrackFragmentFlags.BASE_DATA_OFFSET_PRESENT)) {564 writeInt32(data, this.offset + offset, 0);565 writeInt32(data, this.offset + offset + 4, this.baseDataOffset);566 offset += 8;567 }568 if (!!(flags & TrackFragmentFlags.SAMPLE_DESCRIPTION_INDEX_PRESENT)) {569 writeInt32(data, this.offset + offset, this.sampleDescriptionIndex);570 offset += 4;571 }572 if (!!(flags & TrackFragmentFlags.DEFAULT_SAMPLE_DURATION_PRESENT)) {573 writeInt32(data, this.offset + offset, this.defaultSampleDuration);574 offset += 4;575 }576 if (!!(flags & TrackFragmentFlags.DEFAULT_SAMPLE_SIZE_PRESENT)) {577 writeInt32(data, this.offset + offset, this.defaultSampleSize);578 offset += 4;579 }580 if (!!(flags & TrackFragmentFlags.DEFAULT_SAMPLE_FLAGS_PRESENT)) {581 writeInt32(data, this.offset + offset, this.defaultSampleFlags);582 offset += 4;583 }584 return offset;585 }586 }587 export class TrackFragmentBaseMediaDecodeTimeBox extends FullBox {588 constructor(public baseMediaDecodeTime: number) {589 super('tfdt', 0, 0);590 }591 public layout(offset: number): number {592 this.size = super.layout(offset) + 4;593 return this.size;594 }595 public write(data: Uint8Array): number {596 var offset = super.write(data);597 writeInt32(data, this.offset + offset, this.baseMediaDecodeTime);598 return offset + 4;599 }600 }601 export class TrackFragmentBox extends BoxContainerBox {602 constructor(public header: TrackFragmentHeaderBox,603 public decodeTime: TrackFragmentBaseMediaDecodeTimeBox, // move after run?604 public run: TrackRunBox) {605 super('traf', [header, decodeTime, run]);606 }607 }608 export const enum SampleFlags {609 IS_LEADING_MASK = 0x0C000000,610 SAMPLE_DEPENDS_ON_MASK = 0x03000000,611 SAMPLE_DEPENDS_ON_OTHER = 0x01000000,612 SAMPLE_DEPENDS_ON_NO_OTHERS = 0x02000000,613 SAMPLE_IS_DEPENDED_ON_MASK = 0x00C00000,614 SAMPLE_HAS_REDUNDANCY_MASK = 0x00300000,615 SAMPLE_PADDING_VALUE_MASK = 0x000E0000,616 SAMPLE_IS_NOT_SYNC = 0x00010000,617 SAMPLE_DEGRADATION_PRIORITY_MASK = 0x0000FFFF,618 }619 export const enum TrackRunFlags {620 DATA_OFFSET_PRESENT = 0x000001,621 FIRST_SAMPLE_FLAGS_PRESENT = 0x000004,622 SAMPLE_DURATION_PRESENT = 0x000100,623 SAMPLE_SIZE_PRESENT = 0x000200,624 SAMPLE_FLAGS_PRESENT = 0x000400,625 SAMPLE_COMPOSITION_TIME_OFFSET = 0x000800,626 }627 export interface TrackRunSample {628 duration?: number;629 size?: number;630 flags?: number;631 compositionTimeOffset?: number;632 }633 export class TrackRunBox extends FullBox {634 constructor(flags: number,635 public samples: TrackRunSample[],636 public dataOffset?: number,637 public firstSampleFlags?: number) {638 super('trun', 1, flags);639 }640 public layout(offset: number): number {641 var size = super.layout(offset) + 4;642 var samplesCount = this.samples.length;643 var flags = this.flags;644 if (!!(flags & TrackRunFlags.DATA_OFFSET_PRESENT)) {645 size += 4;646 }647 if (!!(flags & TrackRunFlags.FIRST_SAMPLE_FLAGS_PRESENT)) {648 size += 4;649 }650 if (!!(flags & TrackRunFlags.SAMPLE_DURATION_PRESENT)) {651 size += 4 * samplesCount;652 }653 if (!!(flags & TrackRunFlags.SAMPLE_SIZE_PRESENT)) {654 size += 4 * samplesCount;655 }656 if (!!(flags & TrackRunFlags.SAMPLE_FLAGS_PRESENT)) {657 size += 4 * samplesCount;658 }659 if (!!(flags & TrackRunFlags.SAMPLE_COMPOSITION_TIME_OFFSET)) {660 size += 4 * samplesCount;661 }662 return (this.size = size);663 }664 public write(data: Uint8Array): number {665 var offset = super.write(data);666 var samplesCount = this.samples.length;667 var flags = this.flags;668 writeInt32(data, this.offset + offset, samplesCount);669 offset += 4;670 if (!!(flags & TrackRunFlags.DATA_OFFSET_PRESENT)) {671 writeInt32(data, this.offset + offset, this.dataOffset);672 offset += 4;673 }674 if (!!(flags & TrackRunFlags.FIRST_SAMPLE_FLAGS_PRESENT)) {675 writeInt32(data, this.offset + offset, this.firstSampleFlags);676 offset += 4;677 }678 for (var i = 0; i < samplesCount; i++) {679 var sample = this.samples[i];680 if (!!(flags & TrackRunFlags.SAMPLE_DURATION_PRESENT)) {681 writeInt32(data, this.offset + offset, sample.duration);682 offset += 4;683 }684 if (!!(flags & TrackRunFlags.SAMPLE_SIZE_PRESENT)) {685 writeInt32(data, this.offset + offset, sample.size);686 offset += 4;687 }688 if (!!(flags & TrackRunFlags.SAMPLE_FLAGS_PRESENT)) {689 writeInt32(data, this.offset + offset, sample.flags);690 offset += 4;691 }692 if (!!(flags & TrackRunFlags.SAMPLE_COMPOSITION_TIME_OFFSET)) {693 writeInt32(data, this.offset + offset, sample.compositionTimeOffset);694 offset += 4;695 }696 }697 return offset;698 }699 }700 export class MovieFragmentBox extends BoxContainerBox {701 constructor(public header: MovieFragmentHeaderBox,702 public trafs: TrackFragmentBox[]) {703 super('moof', concatArrays<Box>([header], trafs));704 }705 }706 export class MediaDataBox extends Box {707 constructor(public chunks: Uint8Array[]) {708 super('mdat');709 }710 public layout(offset: number): number {711 var size = super.layout(offset);712 this.chunks.forEach((chunk) => { size += chunk.length; });713 return (this.size = size);714 }715 public write(data: Uint8Array): number {716 var offset = super.write(data);717 this.chunks.forEach((chunk) => {718 data.set(chunk, this.offset + offset);719 offset += chunk.length;720 }, this);721 return offset;722 }723 }724 export class SampleEntry extends Box {725 constructor(format: string,726 public dataReferenceIndex: number) {727 super(format);728 }729 public layout(offset: number): number {730 this.size = super.layout(offset) + 8;731 return this.size;732 }733 public write(data: Uint8Array): number {734 var offset = super.write(data);735 writeInt32(data, this.offset + offset, 0);736 writeInt32(data, this.offset + offset + 4, this.dataReferenceIndex);737 return offset + 8;738 }739 }740 export class AudioSampleEntry extends SampleEntry {741 constructor(codingName: string,742 dataReferenceIndex: number,743 public channelCount: number = 2,744 public sampleSize: number = 16,745 public sampleRate: number = 44100,746 public otherBoxes: Box[] = null) {747 super(codingName, dataReferenceIndex);748 }749 public layout(offset: number): number {750 var size = super.layout(offset) + 20;751 this.otherBoxes && this.otherBoxes.forEach((box) => {752 size += box.layout(offset + size);753 });754 return (this.size = size);755 }756 public write(data: Uint8Array): number {757 var offset = super.write(data);758 writeInt32(data, this.offset + offset, 0);759 writeInt32(data, this.offset + offset + 4, 0);760 writeInt32(data, this.offset + offset + 8, (this.channelCount << 16) | this.sampleSize);761 writeInt32(data, this.offset + offset + 12, 0);762 writeInt32(data, this.offset + offset + 16, (this.sampleRate << 16));763 offset += 20;764 this.otherBoxes && this.otherBoxes.forEach((box) => {765 offset += box.write(data);766 });767 return offset;768 }769 }770 export var COLOR_NO_ALPHA_VIDEO_SAMPLE_DEPTH = 0x0018;771 export class VideoSampleEntry extends SampleEntry {772 constructor(codingName: string,773 dataReferenceIndex: number,774 public width: number,775 public height: number,776 public compressorName: string = '',777 public horizResolution: number = 72,778 public vertResolution: number = 72,779 public frameCount: number = 1,780 public depth: number = COLOR_NO_ALPHA_VIDEO_SAMPLE_DEPTH,781 public otherBoxes: Box[] = null) {782 super(codingName, dataReferenceIndex);783 if (compressorName.length > 31) {784 throw new Error('invalid compressor name');785 }786 }787 public layout(offset: number): number {788 var size = super.layout(offset) + 16 + 12 + 4 + 2 + 32 + 2 + 2;789 this.otherBoxes && this.otherBoxes.forEach((box) => {790 size += box.layout(offset + size);791 });792 return (this.size = size);793 }794 public write(data: Uint8Array): number {795 var offset = super.write(data);796 writeInt32(data, this.offset + offset, 0);797 writeInt32(data, this.offset + offset + 4, 0);798 writeInt32(data, this.offset + offset + 8, 0);799 writeInt32(data, this.offset + offset + 12, 0);800 offset += 16;801 writeInt32(data, this.offset + offset, (this.width << 16) | this.height);802 writeInt32(data, this.offset + offset + 4, encodeFloat_16_16(this.horizResolution));803 writeInt32(data, this.offset + offset + 8, encodeFloat_16_16(this.vertResolution));804 offset += 12;805 writeInt32(data, this.offset + offset, 0);806 writeInt32(data, this.offset + offset + 4, (this.frameCount << 16));807 offset += 6; // weird offset808 data[this.offset + offset] = this.compressorName.length;809 for (var i = 0; i < 31; i++) {810 data[this.offset + offset + i + 1] = i < this.compressorName.length ? (this.compressorName.charCodeAt(i) & 127) : 0;811 }812 offset += 32;813 writeInt32(data, this.offset + offset, (this.depth << 16) | 0xFFFF);814 offset += 4;815 this.otherBoxes && this.otherBoxes.forEach((box) => {816 offset += box.write(data);817 });818 return offset;819 }820 }821 export class RawTag extends Box {822 public data: Uint8Array;823 constructor(type: string, data: Uint8Array) {824 super(type);825 this.data = data;826 }827 public layout(offset: number): number {...
request.js
Source:request.js
...139 docsSize; // OP_REPLY Documents140 // Header and op_reply fields141 let header = Buffer.alloc(4 + 4 + 4 + 4 + 4 + 8 + 4 + 4);142 // Write total size143 writeInt32(header, 0, totalSize);144 // Write requestId145 writeInt32(header, 4, this.requestId);146 // Write responseId147 writeInt32(header, 8, this.responseTo);148 // Write opcode149 writeInt32(header, 12, this.opCode);150 // Write responseflags151 writeInt32(header, 16, this.responseFlags);152 // Write cursorId153 writeInt64(header, 20, this.cursorId);154 // Write startingFrom155 writeInt32(header, 28, this.startingFrom);156 // Write startingFrom157 writeInt32(header, 32, this.numberReturned);158 // Add header to the list of buffers159 buffers.push(header);160 // Add docs to list of buffers161 buffers = buffers.concat(docs);162 // Return all the buffers163 return Buffer.concat(buffers);164};165CompressedResponse.prototype.toBin = function () {166 let buffers = [];167 // Serialize all the docs168 let docs = this.uncompressedResponse.documents.map(function (x) {169 return BSON.serialize(x);170 });171 // Document total size172 let uncompressedSize = 4 + 8 + 4 + 4; // OP_REPLY Header size173 docs.forEach(function (x) {174 uncompressedSize = uncompressedSize + x.length;175 });176 let dataToBeCompressedHeader = Buffer.alloc(20);177 let dataToBeCompressedBody = Buffer.concat(docs);178 // Write response flags179 writeInt32(dataToBeCompressedHeader, 0, this.uncompressedResponse.responseFlags);180 writeInt64(dataToBeCompressedHeader, 4, this.uncompressedResponse.cursorId);181 writeInt32(dataToBeCompressedHeader, 12, this.uncompressedResponse.startingFrom);182 writeInt32(dataToBeCompressedHeader, 16, this.uncompressedResponse.numberReturned);183 let dataToBeCompressed = Buffer.concat([dataToBeCompressedHeader, dataToBeCompressedBody]);184 // Compress the data185 let compressedData;186 switch (this.compressorID) {187 case compressorIDs.snappy:188 compressedData = snappy.compressSync(dataToBeCompressed);189 break;190 case compressorIDs.zlib:191 compressedData = zlib.deflateSync(dataToBeCompressed);192 break;193 default:194 compressedData = dataToBeCompressed;195 }196 // Calculate total size197 let totalSize =198 4 +199 4 +200 4 +201 4 + // Header size202 4 +203 4 +204 1 + // OP_COMPRESSED fields205 compressedData.length; // OP_REPLY fields206 // Header and op_reply fields207 let header = Buffer.alloc(totalSize - compressedData.length);208 // Write total size209 writeInt32(header, 0, totalSize);210 // Write requestId211 writeInt32(header, 4, this.requestId);212 // Write responseId213 writeInt32(header, 8, this.responseTo);214 // Write opcode215 writeInt32(header, 12, this.opCode);216 // Write original opcode`217 writeInt32(header, 16, this.originalOpCode);218 // Write uncompressed message size219 writeInt64(header, 20, Long.fromNumber(uncompressedSize));220 // Write compressorID221 header[24] = this.compressorID & 0xff;222 // Add header to the list of buffers223 buffers.push(header);224 // Add docs to list of buffers225 buffers = buffers.concat(compressedData);226 return Buffer.concat(buffers);227};228const writeInt32 = function (buffer, index, value) {229 buffer[index] = value & 0xff;230 buffer[index + 1] = (value >> 8) & 0xff;231 buffer[index + 2] = (value >> 16) & 0xff;...
cube.js
Source:cube.js
...46 vertexList.writeFloat32(-0.4) 47 vertexList.writeFloat32(-0.4) 48 vertexList.writeFloat32(-0.4)4950 indexList.writeInt32(0) 51 indexList.writeInt32(1) 52 indexList.writeInt32(2) 53 indexList.writeInt32(3)54 indexList.writeInt32(0) 55 indexList.writeInt32(3) 56 indexList.writeInt32(4) 57 indexList.writeInt32(5)58 indexList.writeInt32(4) 59 indexList.writeInt32(5) 60 indexList.writeInt32(6) 61 indexList.writeInt32(7)62 indexList.writeInt32(1) 63 indexList.writeInt32(2) 64 indexList.writeInt32(7) 65 indexList.writeInt32(6)66 indexList.writeInt32(0) 67 indexList.writeInt32(1) 68 indexList.writeInt32(6) 69 indexList.writeInt32(5)70 indexList.writeInt32(2) 71 indexList.writeInt32(3) 72 indexList.writeInt32(4) 73 indexList.writeInt32(7)7475 colorList.writeFloat32(1) 76 colorList.writeFloat32(0) 77 colorList.writeFloat32(0)78 colorList.writeFloat32(0) 79 colorList.writeFloat32(1) 80 colorList.writeFloat32(0)81 colorList.writeFloat32(0) 82 colorList.writeFloat32(0) 83 colorList.writeFloat32(1)84 colorList.writeFloat32(1) 85 colorList.writeFloat32(1) 86 colorList.writeFloat32(0)87 colorList.writeFloat32(1)
...
Using AI Code Generation
1wpt.writeInt32(1, 2);2wpt.writeInt32(2, 4);3wpt.writeInt32(3, 8);4wpt.writeInt32(4, 16);5wpt.writeInt32(5, 32);6wpt.writeInt32(6, 64);7wpt.writeInt32(7, 128);8wpt.writeInt32(8, 256);9wpt.writeInt32(9, 512);10wpt.writeInt32(10, 1024);11wpt.writeInt32(11, 2048);12wpt.writeInt32(12, 4096);13wpt.writeInt32(13, 8192);14wpt.writeInt32(14, 16384);15wpt.writeInt32(15, 32768);16wpt.writeInt32(16, 65536);17wpt.writeInt32(17, 131072);18wpt.writeInt32(18, 262144);19wpt.writeInt32(19, 524288);20wpt.writeInt32(20, 1048576);21wpt.writeInt32(21, 2097152);22wpt.writeInt32(22, 4194304);23wpt.writeInt32(23, 8388608);24wpt.writeInt32(24, 16777216);25wpt.writeInt32(25, 33554432);26wpt.writeInt32(26, 67108864);27wpt.writeInt32(27, 134217728);28wpt.writeInt32(28, 268435456);29wpt.writeInt32(29, 536870912);30wpt.writeInt32(30, 1073741824);31wpt.writeInt32(31, 2147483648);32wpt.writeInt32(32, 4294967296);33wpt.writeInt32(33, 8589934592);34wpt.writeInt32(34, 17179869184);35wpt.writeInt32(35, 34359738368);36wpt.writeInt32(36, 68719476736);37wpt.writeInt32(37, 137438953472);38wpt.writeInt32(38, 274877906944);39wpt.writeInt32(39, 549755813888);40wpt.writeInt32(40, 1099511627776);41wpt.writeInt32(41, 2199023255552);42wpt.writeInt32(42, 4398046511104);43wpt.writeInt32(43
Using AI Code Generation
1wpt.writeInt32(0, 0x12345678);2wpt.writeInt32LE(0, 0x12345678);3wpt.writeInt32BE(0, 0x12345678);4wpt.writeFloat(0, 0x12345678);5wpt.writeFloatLE(0, 0x12345678);6wpt.writeFloatBE(0, 0x12345678);7wpt.writeDouble(0, 0x12345678);8wpt.writeDoubleLE(0, 0x12345678);9wpt.writeDoubleBE(0, 0x12345678);10wpt.write("test", 0, 4, "utf8");11wpt.write("test", 0, 4, "utf16le");12wpt.write("test", 0, 4, "ucs2");13wpt.write("test", 0, 4, "base64");14wpt.write("test", 0, 4, "ascii");15wpt.write("test", 0, 4, "binary");16wpt.write("test", 0, 4, "hex");17wpt.fill(0xff, 0, 4);18wpt.copy(wpt, 0, 0, 4);19wpt.slice(0, 4);20wpt.toString("utf8", 0, 4);
Using AI Code Generation
1var wpt = require('wpt');2var writeInt32 = wpt.writeInt32;3var wpt = require('wpt');4var readInt32 = wpt.readInt32;5var wpt = require('wpt');6var writeInt32LE = wpt.writeInt32LE;7var wpt = require('wpt');8var readInt32LE = wpt.readInt32LE;9var wpt = require('wpt');10var writeInt32BE = wpt.writeInt32BE;11var wpt = require('wpt');12var readInt32BE = wpt.readInt32BE;13var wpt = require('wpt');14var writeUInt32 = wpt.writeUInt32;15var wpt = require('wpt');16var readUInt32 = wpt.readUInt32;17var wpt = require('wpt');18var writeUInt32LE = wpt.writeUInt32LE;19var wpt = require('wpt');20var readUInt32LE = wpt.readUInt32LE;21var wpt = require('wpt');22var writeUInt32BE = wpt.writeUInt32BE;23var wpt = require('wpt');24var readUInt32BE = wpt.readUInt32BE;25var wpt = require('wpt');26var writeInt64 = wpt.writeInt64;27var wpt = require('wpt');28var readInt64 = wpt.readInt64;29var wpt = require('wpt');30var writeInt64LE = wpt.writeInt64LE;
Using AI Code Generation
1var wpt = new Wpt();2wpt.writeInt32(5);3wpt.writeInt32(6);4wpt.writeInt32(7);5wpt.writeInt32(8);6wpt.writeInt32(9);7wpt.writeInt32(10);8wpt.writeInt32(11);9wpt.writeInt32(12);10wpt.writeInt32(13);11wpt.writeInt32(14);12wpt.writeInt32(15);13wpt.writeInt32(16);14wpt.writeInt32(17);15wpt.writeInt32(18);16wpt.writeInt32(19);17wpt.writeInt32(20);18wpt.writeInt32(21);19wpt.writeInt32(22);20wpt.writeInt32(23);21wpt.writeInt32(24);22wpt.writeInt32(25);23wpt.writeInt32(26);24wpt.writeInt32(27);25wpt.writeInt32(28);26wpt.writeInt32(29);27wpt.writeInt32(30);28wpt.writeInt32(31);29wpt.writeInt32(32);30wpt.writeInt32(33);31wpt.writeInt32(34);32wpt.writeInt32(35);33wpt.writeInt32(36);34wpt.writeInt32(37);35wpt.writeInt32(38);36wpt.writeInt32(39);37wpt.writeInt32(40);38wpt.writeInt32(41);39wpt.writeInt32(42);40wpt.writeInt32(43);41wpt.writeInt32(44);42wpt.writeInt32(45);43wpt.writeInt32(46);44wpt.writeInt32(47);45wpt.writeInt32(48);46wpt.writeInt32(49);47wpt.writeInt32(50);48wpt.writeInt32(51);49wpt.writeInt32(52);50wpt.writeInt32(53);51wpt.writeInt32(54);52wpt.writeInt32(55);53wpt.writeInt32(56);54wpt.writeInt32(57);55wpt.writeInt32(58);56wpt.writeInt32(59);57wpt.writeInt32(60);58wpt.writeInt32(61);59wpt.writeInt32(62);60wpt.writeInt32(63);61wpt.writeInt32(64);62wpt.writeInt32(65);63wpt.writeInt32(66);64wpt.writeInt32(67);65wpt.writeInt32(68);66wpt.writeInt32(69);67wpt.writeInt32(70);68wpt.writeInt32(71);69wpt.writeInt32(72);70wpt.writeInt32(73);71wpt.writeInt32(74
Using AI Code Generation
1wpt.writeInt32(0x12345678);2wpt.writeInt32(0x12345678, true);3wpt.writeInt32LE(0x12345678);4wpt.writeInt32LE(0x12345678, true);5wpt.writeInt32BE(0x12345678);6wpt.writeInt32BE(0x12345678, true);7wpt.writeUInt32(0x12345678);8wpt.writeUInt32(0x12345678, true);9wpt.writeUInt32LE(0x12345678);10wpt.writeUInt32LE(0x12345678, true);11wpt.writeUInt32BE(0x12345678);12wpt.writeUInt32BE(0x12345678, true);13wpt.writeFloat(0.12345678);14wpt.writeFloat(0.12345678, true);15wpt.writeFloatLE(0.12345678);16wpt.writeFloatLE(0.12345678, true);17wpt.writeFloatBE(0.12345678);18wpt.writeFloatBE(0.12345678, true);19wpt.writeDouble(0.123456789123456789);20wpt.writeDouble(0.123456789123456789, true);21wpt.writeDoubleLE(0.123456789123456789);22wpt.writeDoubleLE(0.123456789123456789, true);23wpt.writeDoubleBE(0.123456789123456789);24wpt.writeDoubleBE(0.123456789123456789, true);25wpt.write('wpt');
Using AI Code Generation
1var wptools = require('wp-tools');2var fs = require('fs');3var array = new Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);4var buffer = new Buffer(40);5var writer = new wptools.Writer(buffer);6writer.writeInt32(array, 0, 10);7console.log(buffer);8var wptools = require('wp-tools');9var fs = require('fs');10var array = new Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);11var buffer = new Buffer(40);12var writer = new wptools.Writer(buffer);13writer.writeInt32(array, 0, 10);14console.log(buffer);15var wptools = require('wp-tools');16var fs = require('fs');17var array = new Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);18var buffer = new Buffer(40);19var writer = new wptools.Writer(buffer);20writer.writeInt32(array, 0, 10);21console.log(buffer);
Using AI Code Generation
1var wptools = require('wptools');2var wp = new wptools();3var buffer = new Buffer(4);4wp.writeInt32(buffer, 0, 5);5console.log(buffer);6var wptools = require('wptools');7var wp = new wptools();8var buffer = new Buffer(4);9wp.writeInt32(buffer, 0, 5);10console.log(buffer);11var wptools = require('wptools');12var wp = new wptools();13var buffer = new Buffer(4);14wp.writeInt32(buffer, 0, 5);15console.log(buffer);16var wptools = require('wptools');17var wp = new wptools();18var buffer = new Buffer(4);19wp.writeInt32(buffer, 0, 5);20console.log(buffer);21var wptools = require('wptools');22var wp = new wptools();23var buffer = new Buffer(4);24wp.writeInt32(buffer, 0, 5);25console.log(buffer);26var wptools = require('wptools');27var wp = new wptools();28var buffer = new Buffer(4);29wp.writeInt32(buffer, 0, 5);30console.log(buffer);31var wptools = require('wptools');32var wp = new wptools();33var buffer = new Buffer(4);34wp.writeInt32(buffer, 0, 5);35console.log(buffer);
Using AI Code Generation
1var wptools = require("wptools");2var wp = new wptools();3wp.writeInt32(1, 0x12345678);4console.log(wp.buffer);5var wptools = require("wptools");6var wp = new wptools();7wp.writeInt32LE(1, 0x12345678);8console.log(wp.buffer);9var wptools = require("wptools");10var wp = new wptools();11wp.writeInt32BE(1, 0x12345678);12console.log(wp.buffer);13var wptools = require("wptools");14var wp = new wptools();15wp.writeInt64(1, 0x1234567812345678);16console.log(wp.buffer);17var wptools = require("wptools");18var wp = new wptools();19wp.writeInt64LE(1, 0x1234567812345678);20console.log(wp.buffer);21var wptools = require("wptools");22var wp = new wptools();23wp.writeInt64BE(1, 0x1234567812345678);24console.log(wp.buffer);25var wptools = require("wptools");26var wp = new wptools();27wp.writeFloat(1, 0.12345678);28console.log(wp.buffer);
Using AI Code Generation
1var wptools = require('wptools');2var wp = new wptools();3var wpInt = new wp.writeInt32(0x12345678);4var wpInt2 = new wp.writeInt32(0x87654321);5var wpInt3 = new wp.writeInt32(0x00000000);6var wpInt4 = new wp.writeInt32(0xFFFFFFFF);7var wpInt5 = new wp.writeInt32(0x00000001);8var wpInt6 = new wp.writeInt32(0x00000002);9var wpInt7 = new wp.writeInt32(0x00000003);10var wpInt8 = new wp.writeInt32(0x00000004);11var wpInt9 = new wp.writeInt32(0x00000005);12var wpInt10 = new wp.writeInt32(0x00000006);13var wpInt11 = new wp.writeInt32(0x00000007);14var wpInt12 = new wp.writeInt32(0x00000008);15var wpInt13 = new wp.writeInt32(0x00000009);16var wpInt14 = new wp.writeInt32(0x0000000A);17var wpInt15 = new wp.writeInt32(0x0000000B);18var wpInt16 = new wp.writeInt32(0x0000000C);19var wpInt17 = new wp.writeInt32(0x0000000D);20var wpInt18 = new wp.writeInt32(0x0000000E);21var wpInt19 = new wp.writeInt32(0x0000000F);22var wpInt20 = new wp.writeInt32(0x00000010);23var wpInt21 = new wp.writeInt32(0x00000011);24var wpInt22 = new wp.writeInt32(0x00000012);25var wpInt23 = new wp.writeInt32(0x00000013);26var wpInt24 = new wp.writeInt32(0x00000014);27var wpInt25 = new wp.writeInt32(0x00000015);28var wpInt26 = new wp.writeInt32(0x00000016);29var wpInt27 = new wp.writeInt32(0x00000017);30var wpInt28 = new wp.writeInt32(0x00000018);31var wpInt29 = new wp.writeInt32(0x00000019
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!