Skip to content

fix(HLS): Fix preload initial variant #8835

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
161 changes: 101 additions & 60 deletions lib/media/preload_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -430,13 +430,18 @@ shaka.media.PreloadManager = class extends shaka.util.FakeEventTarget {
await this.parseManifestInner_();
this.throwIfDestroyed_();

await this.chooseInitialVariant_();
this.throwIfDestroyed_();

if (!shaka.drm.DrmUtils.isMediaKeysPolyfilled('webkit')) {
await this.initializeDrm();
this.throwIfDestroyed_();
}

await this.chooseInitialVariantAndPrefetchInner_();
this.throwIfDestroyed_();
if (this.allowPrefetch_) {
await this.prefetchInner_();
this.throwIfDestroyed_();
}

// We don't need the drm keys to load completely for the initial variant
// to be chosen, but we won't mark the load as a success until it has
Expand Down Expand Up @@ -552,19 +557,6 @@ shaka.media.PreloadManager = class extends shaka.util.FakeEventTarget {
if (!this.manifest_) {
this.manifest_ = await this.parser_.start(
this.assetUri_, this.manifestPlayerInterface_);

if (this.manifest_.variants.length == 1) {
const createSegmentIndexPromises = [];
const variant = this.manifest_.variants[0];
for (const stream of [variant.video, variant.audio]) {
if (stream && !stream.segmentIndex) {
createSegmentIndexPromises.push(stream.createSegmentIndex());
}
}
if (createSegmentIndexPromises.length > 0) {
await Promise.all(createSegmentIndexPromises);
}
}
}

this.manifestPromise_.resolve();
Expand All @@ -590,6 +582,16 @@ shaka.media.PreloadManager = class extends shaka.util.FakeEventTarget {
// audio-video.
shaka.media.PreloadManager.filterForAVVariants_(this.manifest_);

const tracksChangedInitial = this.manifestFilterer_.applyRestrictions(
this.manifest_);
if (tracksChangedInitial) {
const event = this.makeEvent_(
shaka.util.FakeEvent.EventName.TracksChanged);
await Promise.resolve();
this.throwIfDestroyed_();
this.dispatchEvent(event);
}

const now = Date.now() / 1000;
const delta = now - startTime;
this.stats_.setManifestTime(delta);
Expand All @@ -614,23 +616,10 @@ shaka.media.PreloadManager = class extends shaka.util.FakeEventTarget {

this.drmEngine_.configure(this.config_.drm, () => this.isPreload_);

const tracksChangedInitial = this.manifestFilterer_.applyRestrictions(
this.manifest_);
if (tracksChangedInitial) {
const event = this.makeEvent_(
shaka.util.FakeEvent.EventName.TracksChanged);
await Promise.resolve();
this.throwIfDestroyed_();
this.dispatchEvent(event);
}

const playableVariants = shaka.util.StreamUtils.getPlayableVariants(
this.manifest_.variants);
let isLive = true;
// In HLS we need to parse the media playlist to know if it is Live or not.
// So at this point we don't know yet. By default we assume it is Live.
if (this.manifest_ && this.manifest_.presentationTimeline &&
this.manifest_.type != shaka.media.ManifestParser.HLS) {
if (this.manifest_ && this.manifest_.presentationTimeline) {
isLive = this.manifest_.presentationTimeline.isLive();
}
await this.drmEngine_.initForPlayback(
Expand Down Expand Up @@ -679,13 +668,13 @@ shaka.media.PreloadManager = class extends shaka.util.FakeEventTarget {
}

/**
* Performs a final filtering of the manifest, and chooses the initial
* variant. Also prefetches segments.
* Performs a filtering of the manifest, and chooses the initial
* variant.
*
* @return {!Promise}
* @private
*/
async chooseInitialVariantAndPrefetchInner_() {
async chooseInitialVariant_() {
goog.asserts.assert(
this.manifest_, 'The manifest should already be parsed.');

Expand Down Expand Up @@ -724,40 +713,92 @@ shaka.media.PreloadManager = class extends shaka.util.FakeEventTarget {
this.abrManager_.configure(this.config_.abr);
}

if (this.allowPrefetch_) {
const isLive = this.manifest_.presentationTimeline.isLive();
// Prefetch segments for the predicted first variant.
// We start these here, but don't wait for them; it's okay to start the
// full load process while the segments are being prefetched.
const playableVariants = shaka.util.StreamUtils.getPlayableVariants(
this.manifest_.variants);
const adaptationSet = this.currentAdaptationSetCriteria_.create(
playableVariants);
// Guess what the first variant will be, based on a SimpleAbrManager.
this.abrManager_.configure(this.config_.abr);
this.abrManager_.setVariants(Array.from(adaptationSet.values()));
const variant = this.abrManager_.chooseVariant();
if (variant) {
const promises = [];
this.prefetchedVariant_ = variant;
if (variant.video) {
promises.push(this.prefetchStream_(variant.video, isLive));
}
if (variant.audio) {
promises.push(this.prefetchStream_(variant.audio, isLive));
}
const textStream = this.chooseTextStream_();
if (textStream && shaka.util.StreamUtils.shouldInitiallyShowText(
variant.audio, textStream, this.config_)) {
promises.push(this.prefetchStream_(textStream, isLive));
this.prefetchedTextStream_ = textStream;
const variant = this.configureAbrManagerAndChooseVariant_();
if (variant &&
this.shouldCreateSegmentIndexBeforeDrmEngineInitialization_()) {
const createSegmentIndexPromises = [];
for (const stream of [variant.video, variant.audio]) {
if (stream && !stream.segmentIndex) {
createSegmentIndexPromises.push(stream.createSegmentIndex());
}
}
if (createSegmentIndexPromises.length > 0) {
await Promise.all(createSegmentIndexPromises);
}
}
}

await Promise.all(promises);
/**
* @return {boolean}
* @private
*/
shouldCreateSegmentIndexBeforeDrmEngineInitialization_() {
goog.asserts.assert(
this.manifest_, 'The manifest should already be parsed.');

// If we only have one variant, it is useful to preload it, because it will
// be the only one we can use.
if (this.manifest_.variants.length == 1) {
return true;
}

// In HLS, DRM information is usually included in the media playlist, so we
// need to download the media playlist to get the real information.
if (this.manifest_.type == shaka.media.ManifestParser.HLS) {
return true;
}

return false;
}

/**
* Prefetches segments.
*
* @return {!Promise}
* @private
*/
async prefetchInner_() {
const variant = this.configureAbrManagerAndChooseVariant_();
if (variant) {
const isLive = this.manifest_.presentationTimeline.isLive();
const promises = [];
this.prefetchedVariant_ = variant;
if (variant.video) {
promises.push(this.prefetchStream_(variant.video, isLive));
}
if (variant.audio) {
promises.push(this.prefetchStream_(variant.audio, isLive));
}
const textStream = this.chooseTextStream_();
if (textStream && shaka.util.StreamUtils.shouldInitiallyShowText(
variant.audio, textStream, this.config_)) {
promises.push(this.prefetchStream_(textStream, isLive));
this.prefetchedTextStream_ = textStream;
}

await Promise.all(promises);
}
}

/**
* @return {shaka.extern.Variant}
* @private
*/
configureAbrManagerAndChooseVariant_() {
goog.asserts.assert(this.currentAdaptationSetCriteria_,
'Must have an AdaptationSetCriteria');

const playableVariants = shaka.util.StreamUtils.getPlayableVariants(
this.manifest_.variants);
const adaptationSet = this.currentAdaptationSetCriteria_.create(
playableVariants);
// Guess what the first variant will be, based on a SimpleAbrManager.
this.abrManager_.configure(this.config_.abr);
this.abrManager_.setVariants(Array.from(adaptationSet.values()));

return this.abrManager_.chooseVariant();
}

/**
* @return {?shaka.extern.Stream}
* @private
Expand Down
3 changes: 2 additions & 1 deletion test/player_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -4157,7 +4157,8 @@ describe('Player', () => {

await player.load(fakeManifestUri, 0, fakeMimeType);
expect(abrManager.setVariants).toHaveBeenCalled();
const variants = abrManager.setVariants.calls.argsFor(0)[0];
const lastCallIndex = abrManager.setVariants.calls.count() - 1;
const variants = abrManager.setVariants.calls.argsFor(lastCallIndex)[0];
// We've already chosen codecs, so only 3 tracks should remain.
expect(variants.length).toBe(3);
// They should be the low-bandwidth ones.
Expand Down