Skip to content

Commit

Permalink
Merge pull request #2391 from video-dev/remove-slice-pollyfill
Browse files Browse the repository at this point in the history
remove slice pollyfill
  • Loading branch information
robwalch authored Oct 8, 2019
2 parents 08d8800 + 635a459 commit fb06e1d
Show file tree
Hide file tree
Showing 10 changed files with 23 additions and 21 deletions.
4 changes: 3 additions & 1 deletion src/crypt/aes-decryptor.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { sliceUint8 } from '../utils/typed-array';

// PKCS7
export function removePadding (buffer) {
const outputBytes = buffer.byteLength;
const paddingBytes = outputBytes && (new DataView(buffer)).getUint8(outputBytes - 1);
if (paddingBytes) {
return buffer.slice(0, outputBytes - paddingBytes);
return sliceUint8(buffer, 0, outputBytes - paddingBytes);
} else {
return buffer;
}
Expand Down
7 changes: 4 additions & 3 deletions src/crypt/decrypter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import FastAESKey from './fast-aes-key';
import AESDecryptor, { removePadding } from './aes-decryptor';
import { logger } from '../utils/logger';
import { appendUint8Array } from '../utils/mp4-tools';
import { sliceUint8 } from '../utils/typed-array';

const CHUNK_SIZE = 16; // 16 bytes, 128 bits

Expand Down Expand Up @@ -95,7 +96,7 @@ export default class Decrypter {
const result = currentResult;

this.currentResult = softwareDecrypter.decrypt(currentChunk.buffer, 0, iv);
this.currentIV = currentChunk.slice(-16).buffer;
this.currentIV = sliceUint8(currentChunk, -16).buffer;

if (!result) {
return null;
Expand Down Expand Up @@ -134,8 +135,8 @@ export default class Decrypter {
let currentChunk = data;
const splitPoint = data.length - (data.length % CHUNK_SIZE);
if (splitPoint !== data.length) {
currentChunk = data.slice(0, splitPoint);
this.remainderData = data.slice(splitPoint);
currentChunk = sliceUint8(data, 0, splitPoint);
this.remainderData = sliceUint8(data, splitPoint);
}
return currentChunk;
}
Expand Down
3 changes: 2 additions & 1 deletion src/demux/base-audio-demuxer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import ID3 from '../demux/id3';
import { DemuxerResult, Demuxer, DemuxedTrack, DemuxedAudioTrack } from '../types/demuxer';
import { dummyTrack } from './dummy-demuxed-track';
import { appendUint8Array } from '../utils/mp4-tools';
import { sliceUint8 } from '../utils/typed-array';

class BaseAudioDemuxer implements Demuxer {
protected _audioTrack!: DemuxedAudioTrack;
Expand Down Expand Up @@ -82,7 +83,7 @@ class BaseAudioDemuxer implements Demuxer {
offset++;
}
if (offset === length && lastDataIndex !== length) {
const partialData = data.slice(lastDataIndex);
const partialData = sliceUint8(data, lastDataIndex);
this.cachedData = appendUint8Array(this.cachedData, partialData);
}
}
Expand Down
2 changes: 0 additions & 2 deletions src/demux/transmuxer-worker.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import '../polyfills/runtime-polyfills';

import Transmuxer, { isPromise } from '../demux/transmuxer';
import Event from '../events';
import { enableLogs } from '../utils/logger';
Expand Down
3 changes: 2 additions & 1 deletion src/demux/tsdemuxer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { logger } from '../utils/logger';
import { ErrorTypes, ErrorDetails } from '../errors';
import { DemuxedAvcTrack, DemuxedAudioTrack, DemuxedTrack, Demuxer, DemuxerResult } from '../types/demuxer';
import { appendUint8Array } from '../utils/mp4-tools';
import { sliceUint8 } from '../utils/typed-array';

// We are using fixed track IDs for driving the MP4 remuxer
// instead of following the TS PIDs.
Expand Down Expand Up @@ -208,7 +209,7 @@ class TSDemuxer implements Demuxer {
};
}
len -= (len + syncOffset) % 188;
this.remainderData = data.slice(len);
this.remainderData = sliceUint8(data, len);

// loop through TS packets
for (start = syncOffset; start < len; start += 188) {
Expand Down
1 change: 0 additions & 1 deletion src/hls.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import './polyfills/runtime-polyfills';
import * as URLToolkit from 'url-toolkit';

import {
Expand Down
8 changes: 0 additions & 8 deletions src/polyfills/runtime-polyfills.ts

This file was deleted.

5 changes: 3 additions & 2 deletions src/utils/chunker.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { appendUint8Array } from './mp4-tools';
import { sliceUint8 } from './typed-array';

export default class Chunker {
private chunkSize: number;
Expand Down Expand Up @@ -28,10 +29,10 @@ export default class Chunker {
let offset = 0;
const len = temp.length;
while (offset < (len - chunkSize)) {
result.push(temp.slice(offset, offset + chunkSize));
result.push(sliceUint8(temp, offset, offset + chunkSize));
offset += chunkSize;
}
this.cache = temp.slice(offset);
this.cache = sliceUint8(temp, offset);
} else {
result.push(temp);
}
Expand Down
6 changes: 4 additions & 2 deletions src/utils/mp4-tools.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { sliceUint8 } from './typed-array';

// Todo: Optimize by using loops instead of array methods
const UINT32_MAX = Math.pow(2, 32) - 1;

Expand Down Expand Up @@ -445,8 +447,8 @@ export function segmentValidRange (data: Uint8Array): SegmentedRange {
}
const last = moofs[moofs.length - 1];
// Offset by 8 bytes; findBox offsets the start by as much
segmentedRange.valid = data.slice(0, last.start - 8);
segmentedRange.remainder = data.slice(last.start - 8);
segmentedRange.valid = sliceUint8(data, 0, last.start - 8);
segmentedRange.remainder = sliceUint8(data, last.start - 8);
return segmentedRange;
}

Expand Down
5 changes: 5 additions & 0 deletions src/utils/typed-array.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export function sliceUint8 (array: Uint8Array, start?: number, end?: number): Uint8Array {
return Uint8Array.prototype.slice
? array.slice(start, end)
: new Uint8Array(Array.prototype.slice.call(array, start, end));
}

0 comments on commit fb06e1d

Please sign in to comment.