Skip to content

Commit

Permalink
chore: refactor default live edge crud.
Browse files Browse the repository at this point in the history
  • Loading branch information
cjpillsbury committed Feb 16, 2023
1 parent 69476b3 commit e091e1c
Showing 1 changed file with 13 additions and 31 deletions.
44 changes: 13 additions & 31 deletions src/js/media-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -1030,41 +1030,23 @@ const Delegates = {
return media.currentTime >= media.liveEdgeStart;
}

const streamIsLive = controller.getAttribute(MediaUIAttributes.MEDIA_STREAM_TYPE) === 'live';
const live = getStreamType(controller) === 'live';
// Can't be playing live if it's not a live stream
if (!live) return false;
const seekable = media.seekable;

// If there's no way to seek, assume the media element is keeping it "live"
if (streamIsLive && !seekable) {
return true;
}

// If the slotted media does not provide a `seekable` property or its length is falsey,
// assume the media is not live.
if (!seekable?.length) {
return false;
}
// If the slotted media element is live but does not expose a 'seekable' `TimeRanges` object,
// always assume playing live
if (!seekable) return true;
// If there is an empty `seekable`, assume we are not playing live
if (!seekable.length) return false;

// Default to 10 seconds
// Assuming seekable range already accounts for appropriate buffer room
let liveThreshold = 10;
let liveThresholdAttr = controller.getAttribute('livethreshold');

if (liveThresholdAttr !== null) {
liveThresholdAttr = Number(liveThresholdAttr);

if (!Number.isNaN(liveThresholdAttr)) {
liveThreshold = liveThresholdAttr;
}
}

const currentTime = media.currentTime;
const seekableEnd = seekable.end(seekable.length - 1);

if (currentTime > seekableEnd - liveThreshold) {
return true;
}

return false;
const liveEdgeStartOffset = controller.hasAttribute('livethreshold')
? Number(controller.getAttribute('livethreshold'))
: 10;
const liveEdgeStart = seekable.end(seekable.length - 1) - liveEdgeStartOffset
return media.currentTime >= liveEdgeStart;
},
};

Expand Down

0 comments on commit e091e1c

Please sign in to comment.