Skip to content

Commit

Permalink
chore: update segment loader
Browse files Browse the repository at this point in the history
  • Loading branch information
Dzianis Dashkevich committed Nov 13, 2024
1 parent 4d4b751 commit 231b4bb
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 52 deletions.
13 changes: 12 additions & 1 deletion src/playlist-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -1136,7 +1136,18 @@ export class PlaylistController extends videojs.EventTarget {
this.mainSegmentLoader_.load();
});

// don't need to reset audio as it is reset when media changes
if (this.mediaTypes_.AUDIO.activePlaylistLoader) {
this.audioSegmentLoader_.pause();
this.audioSegmentLoader_.resetEverything(() => {
this.audioSegmentLoader_.load();

Check warning on line 1142 in src/playlist-controller.js

View check run for this annotation

Codecov / codecov/patch

src/playlist-controller.js#L1140-L1142

Added lines #L1140 - L1142 were not covered by tests
});
}
if (this.mediaTypes_.SUBTITLES.activePlaylistLoader) {
this.subtitleSegmentLoader_.pause();
this.subtitleSegmentLoader_.resetEverything(() => {
this.subtitleSegmentLoader_.load();

Check warning on line 1148 in src/playlist-controller.js

View check run for this annotation

Codecov / codecov/patch

src/playlist-controller.js#L1146-L1148

Added lines #L1146 - L1148 were not covered by tests
});
}
}

/**
Expand Down
10 changes: 10 additions & 0 deletions src/segment-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -857,6 +857,7 @@ export default class SegmentLoader extends videojs.EventTarget {
if (this.pendingSegment_) {
this.pendingSegment_ = null;
}
this.timelineChangeController_.clearPendingTimelineChange(this.loaderType_);
return;
}

Expand Down Expand Up @@ -1102,6 +1103,15 @@ export default class SegmentLoader extends videojs.EventTarget {
if (!newPlaylist) {
return;
}

if (this.playlist_ &&
this.playlist_.endList &&
newPlaylist.endList &&
this.playlist_.uri === newPlaylist.uri) {
// skip update if both prev and new are vod and have the same URI
return;
}

const oldPlaylist = this.playlist_;
const segmentInfo = this.pendingSegment_;

Expand Down
76 changes: 25 additions & 51 deletions test/segment-loader.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import {
getTroublesomeSegmentDurationMessage,
getSyncSegmentCandidate,
segmentInfoString,
shouldFixBadTimelineChanges,
fixBadTimelineChange
shouldFixBadTimelineChanges
} from '../src/segment-loader';
import mp4probe from 'mux.js/lib/mp4/probe';
import {
Expand Down Expand Up @@ -515,37 +514,6 @@ QUnit.test('shouldFixBadTimelineChange returns false when timelineChangeControll
assert.notOk(shouldFixBadTimelineChanges(timelineChangeController), 'should not fix a timeline change with no timelineChangeController');
});

QUnit.module('fixBadTimelineChange');

QUnit.test('fixBadTimelineChange calls pause, resetEverything and load on a segmentLoader', function(assert) {
let pauseCalls = 0;
let resetEverythingCalls = 0;
let loadCalls = 0;
let mockSegmentLoader = {
pause() {
pauseCalls++;
},
resetEverything() {
resetEverythingCalls++;
},
load() {
loadCalls++;
}
};

fixBadTimelineChange(mockSegmentLoader);
assert.equal(pauseCalls, 1, 'calls pause once');
assert.equal(resetEverythingCalls, 1, 'calls resetEverything once');
assert.equal(loadCalls, 1, 'calls load once');

// early return if undefined. call counts remain the same.
mockSegmentLoader = undefined;
fixBadTimelineChange(mockSegmentLoader);
assert.equal(pauseCalls, 1, 'calls pause once');
assert.equal(resetEverythingCalls, 1, 'calls resetEverything once');
assert.equal(loadCalls, 1, 'calls load once');
});

QUnit.module('safeBackBufferTrimTime');

QUnit.test('uses 30s before playhead when seekable start is 0', function(assert) {
Expand Down Expand Up @@ -1668,17 +1636,11 @@ QUnit.module('SegmentLoader', function(hooks) {
loader = new SegmentLoader(LoaderCommonSettings.call(this, {
loaderType: 'audio'
}), {});
const origPause = loader.pause;
const origLoad = loader.load;
const origResetEverything = loader.resetEverything;
let pauseCalls = 0;
let loadCalls = 0;
let resetEverythingCalls = 0;

loader.pause = () => {
pauseCalls++;
origPause.call(loader);
};
loader.load = () => {
loadCalls++;
origLoad.call(loader);
Expand All @@ -1701,6 +1663,14 @@ QUnit.module('SegmentLoader', function(hooks) {
}
};

let fixBadTimelineChangeCount = 0;

loader.timelineChangeController_.trigger = (type) => {
if (type === 'fixBadTimelineChange') {
fixBadTimelineChangeCount++;
}
};

const playlist = playlistWithDuration(20);

playlist.discontinuityStarts = [1];
Expand All @@ -1716,9 +1686,9 @@ QUnit.module('SegmentLoader', function(hooks) {
loader.playlist(playlist);
loader.load();
this.clock.tick(1);
assert.equal(pauseCalls, 1, '1 pause call expected');
assert.equal(loadCalls, 2, '2 load calls expected');
assert.equal(resetEverythingCalls, 2, '1 load calls expected');
assert.equal(loadCalls, 1, '1 load calls expected');
assert.equal(resetEverythingCalls, 1, '1 load calls expected');
assert.equal(fixBadTimelineChangeCount, 1, '1 fixBadTimelineChangeCount triggered');
});
});

Expand All @@ -1727,17 +1697,11 @@ QUnit.module('SegmentLoader', function(hooks) {
loader = new SegmentLoader(LoaderCommonSettings.call(this, {
loaderType: 'main'
}), {});
const origPause = loader.pause;
const origLoad = loader.load;
const origResetEverything = loader.resetEverything;
let pauseCalls = 0;
let loadCalls = 0;
let resetEverythingCalls = 0;

loader.pause = () => {
pauseCalls++;
origPause.call(loader);
};
loader.load = () => {
loadCalls++;
origLoad.call(loader);
Expand All @@ -1759,6 +1723,15 @@ QUnit.module('SegmentLoader', function(hooks) {
};
}
};

let fixBadTimelineChangeCount = 0;

loader.timelineChangeController_.trigger = (type) => {
if (type === 'fixBadTimelineChange') {
fixBadTimelineChangeCount++;
}
};

this.sourceUpdater_.ready = () => {
return true;
};
Expand All @@ -1783,9 +1756,10 @@ QUnit.module('SegmentLoader', function(hooks) {
loader.playlist(playlist);
loader.load();
this.clock.tick(1);
assert.equal(pauseCalls, 1, '1 pause call expected');
assert.equal(loadCalls, 2, '2 load calls expected');
assert.equal(resetEverythingCalls, 2, '1 load calls expected');
assert.equal(loadCalls, 1, '1 load calls expected');
assert.equal(resetEverythingCalls, 1, '1 load calls expected');
assert.equal(fixBadTimelineChangeCount, 1, '1 fixBadTimelineChangeCount triggered');

});
});

Expand Down

0 comments on commit 231b4bb

Please sign in to comment.