Skip to content

Commit

Permalink
Merge pull request video-dev#3755 from softworkz/fetchloaderfix
Browse files Browse the repository at this point in the history
Fix Video Glitches with Fetch Loader
  • Loading branch information
robwalch authored Apr 13, 2021
2 parents 92bca82 + 1404009 commit b14d783
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 12 deletions.
7 changes: 6 additions & 1 deletion src/demux/transmuxer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,12 @@ export default class Transmuxer {
chunkMeta: ChunkMetadata
): TransmuxerResult {
const { audioTrack, avcTrack, id3Track, textTrack } = (this
.demuxer as Demuxer).demux(data, timeOffset, false);
.demuxer as Demuxer).demux(
data,
timeOffset,
false,
!this.config.progressive
);
const remuxResult = this.remuxer!.remux(
audioTrack,
avcTrack,
Expand Down
13 changes: 11 additions & 2 deletions src/demux/tsdemuxer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,11 @@ class TSDemuxer implements Demuxer {
id3Track,
textTrack: this._txtTrack,
};
this.extractRemainingSamples(demuxResult);

if (flush) {
this.extractRemainingSamples(demuxResult);
}

return demuxResult;
}

Expand Down Expand Up @@ -482,7 +486,12 @@ class TSDemuxer implements Demuxer {
keyData: KeyData,
timeOffset: number
): Promise<DemuxerResult> {
const demuxResult = this.demux(data, timeOffset, true);
const demuxResult = this.demux(
data,
timeOffset,
true,
!this.config.progressive
);
const sampleAes = (this.sampleAes = new SampleAesDecrypter(
this.observer,
this.config,
Expand Down
3 changes: 2 additions & 1 deletion src/types/demuxer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ export interface Demuxer {
demux(
data: Uint8Array,
timeOffset: number,
isSampleAes?: boolean
isSampleAes?: boolean,
flush?: boolean
): DemuxerResult;
demuxSampleAes(
data: Uint8Array,
Expand Down
18 changes: 10 additions & 8 deletions src/utils/fetch-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ class FetchLoader implements Loader<LoaderContext> {
stats.total = parseInt(response.headers.get('Content-Length') || '0');

if (onProgress && Number.isFinite(config.highWaterMark)) {
this.loadProgressively(
return this.loadProgressively(
response,
stats,
context,
Expand Down Expand Up @@ -175,19 +175,20 @@ class FetchLoader implements Loader<LoaderContext> {
context: LoaderContext,
highWaterMark: number = 0,
onProgress: LoaderOnProgress<LoaderContext>
) {
): Promise<ArrayBuffer> {
const chunkCache = new ChunkCache();
const reader = (response.clone().body as ReadableStream).getReader();
const reader = (response.body as ReadableStream).getReader();

const pump = () => {
reader
const pump = (): Promise<ArrayBuffer> => {
return reader
.read()
.then((data) => {
if (data.done) {
if (chunkCache.dataLength) {
onProgress(stats, context, chunkCache.flush(), response);
}
return;

return Promise.resolve(new ArrayBuffer(0));
}
const chunk: Uint8Array = data.value;
const len = chunk.length;
Expand All @@ -205,14 +206,15 @@ class FetchLoader implements Loader<LoaderContext> {
// just emit the progress event
onProgress(stats, context, chunk, response);
}
pump();
return pump();
})
.catch(() => {
/* aborted */
return Promise.reject();
});
};

pump();
return pump();
}
}

Expand Down

0 comments on commit b14d783

Please sign in to comment.