Skip to content

Commit

Permalink
Do not reset when crossing a plain boundary from a plain boundary and…
Browse files Browse the repository at this point in the history
… migrated to encrypted flag
  • Loading branch information
matvp91 committed Feb 27, 2025
1 parent de4dce9 commit 389cce2
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 30 deletions.
8 changes: 6 additions & 2 deletions lib/dash/dash_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -2216,6 +2216,8 @@ shaka.dash.DashParser = class {
TXml.findChildren(node, 'SupplementalProperty');
const essentialPropertyElements =
TXml.findChildren(node, 'EssentialProperty');
const contentProtectionElements =
TXml.findChildren(node, 'ContentProtection');

let representationUrlParams = null;
let urlParamsElement = essentialPropertyElements.find((element) => {
Expand Down Expand Up @@ -2248,6 +2250,10 @@ shaka.dash.DashParser = class {
contentType == ContentType.APPLICATION;
const isImage = contentType == ContentType.IMAGE;

if (contentProtectionElements.length) {
context.adaptationSet.encrypted = true;
}

try {
/** @type {shaka.extern.aesKey|undefined} */
let aesKey = undefined;
Expand Down Expand Up @@ -2340,8 +2346,6 @@ shaka.dash.DashParser = class {
throw error;
}

const contentProtectionElements =
TXml.findChildren(node, 'ContentProtection');
const keyId = shaka.dash.ContentProtection.parseFromRepresentation(
contentProtectionElements, contentProtection,
this.config_.ignoreDrmInfo,
Expand Down
55 changes: 27 additions & 28 deletions lib/media/streaming_engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -1543,19 +1543,30 @@ shaka.media.StreamingEngine = class {
return updateIntervalSeconds;
}

if (mediaState.segmentPrefetch && mediaState.segmentIterator &&
!this.audioPrefetchMap_.has(mediaState.stream)) {
mediaState.segmentPrefetch.evict(reference.startTime);
mediaState.segmentPrefetch.prefetchSegmentsByTime(reference.startTime);
}

const CrossBoundaryStrategy = shaka.config.CrossBoundaryStrategy;
if (this.config_.crossBoundaryStrategy !== CrossBoundaryStrategy.KEEP &&
mediaState.lastInitSegmentReference &&
mediaState.lastInitSegmentReference.startBoundary !==
reference.initSegmentReference.startBoundary) {
// If seeked across a boundary, reset MediaSource.
if (mediaState.seeked && !mediaState.clearingBuffer) {
let resetMediaSource = mediaState.seeked;

// Some devices can play plain data when initialized with an encrypted
// init segment. We can keep the MediaSource in this case.
if (this.config_.crossBoundaryStrategy ===
CrossBoundaryStrategy.RESET_TO_ENCRYPTED) {
if (mediaState.lastInitSegmentReference.encrypted) {
shaka.log.debug(logPrefix, 'stream is encrypted, ' +
'discard crossBoundaryStrategy');
this.config_.crossBoundaryStrategy = CrossBoundaryStrategy.KEEP;
} else if (!reference.initSegmentReference.encrypted) {
// The last segment is not encrypted and we're crossing
// a plain boundary. Do not reset MSE.
resetMediaSource = false;
}
}

if (resetMediaSource) {
shaka.log.debug(logPrefix, 'reset mediaSource',
'from=', mediaState.lastInitSegmentReference,
'to=', reference.initSegmentReference);
Expand All @@ -1565,30 +1576,18 @@ shaka.media.StreamingEngine = class {
this.playerInterface_.onEvent(new shaka.util.FakeEvent(eventName));
});
}
// Some devices can play plain data when initialized with an encrypted
// init segment. We can keep the MediaSource in this case.
if (this.config_.crossBoundaryStrategy ===
CrossBoundaryStrategy.RESET_TO_ENCRYPTED) {
const matchedStreams = mediaState.stream.matchedStreams;
if (matchedStreams) {
const matchedStream = matchedStreams.find((stream) => {
const references = stream.segmentIndex.references;
const lastReference = references[references.length - 1];
return lastReference && presentationTime < lastReference.endTime &&
presentationTime >= references[0].startTime;
});

if (matchedStream && matchedStream.encrypted) {
shaka.log.debug(logPrefix, 'stream is encrypted, ' +
'discard crossBoundaryStrategy', matchedStream);
this.config_.crossBoundaryStrategy =
CrossBoundaryStrategy.KEEP;
}
}
}

// Return null as we do not watch to fetch and append segments outside
// of the current boundary.
return null;
}

if (mediaState.segmentPrefetch && mediaState.segmentIterator &&
!this.audioPrefetchMap_.has(mediaState.stream)) {
mediaState.segmentPrefetch.evict(reference.startTime);
mediaState.segmentPrefetch.prefetchSegmentsByTime(reference.startTime);
}

const p = this.fetchAndAppend_(mediaState, presentationTime, reference,
adaptation);
p.catch(() => {}); // TODO(#1993): Handle asynchronous errors.
Expand Down

0 comments on commit 389cce2

Please sign in to comment.