Skip to content

Commit

Permalink
poc-ll-hls
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonocasey committed Feb 2, 2021
1 parent a34b4da commit 767fe7e
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/media-segment-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -920,7 +920,7 @@ export const mediaSegmentRequest = ({
}

const segmentRequestOptions = videojs.mergeOptions(xhrOptions, {
uri: segment.resolvedUri,
uri: segment.part && segment.part.resolvedUri || segment.resolvedUri,
responseType: 'arraybuffer',
headers: segmentXhrHeaders(segment)
});
Expand Down
29 changes: 27 additions & 2 deletions src/playlist-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export const updateSegments = (original, update, offset) => {
};

export const resolveSegmentUris = (segment, baseUri) => {
if (!segment.resolvedUri) {
if (!segment.resolvedUri && segment.uri) {
segment.resolvedUri = resolveUrl(baseUri, segment.uri);
}
if (segment.key && !segment.key.resolvedUri) {
Expand All @@ -55,6 +55,17 @@ export const resolveSegmentUris = (segment, baseUri) => {
if (segment.map && !segment.map.resolvedUri) {
segment.map.resolvedUri = resolveUrl(baseUri, segment.map.uri);
}
if (segment.parts && segment.parts.length && !segment.parts[0].resolvedUri) {
segment.parts.forEach((p) => {
p.resolvedUri = resolveUrl(baseUri, p.URI);
});
}

if (segment.preloadHints && segment.preloadHints.length && !segment.preloadHints[0].resolvedUri) {
segment.preloadHints.forEach((p) => {
p.resolvedUri = resolveUrl(baseUri, p.URI);
});
}
};

/**
Expand Down Expand Up @@ -91,8 +102,14 @@ export const updateMaster = (master, media) => {

// if the update could overlap existing segment information, merge the two segment lists
if (playlist.segments) {
const original = playlist.segments.slice();

// also merge the pending segment in with the new playlist update.
if (playlist.preloadSegment) {
original.push(playlist.preloadSegment);
}
mergedPlaylist.segments = updateSegments(
playlist.segments,
original,
media.segments,
media.mediaSequence - playlist.mediaSequence
);
Expand All @@ -103,6 +120,10 @@ export const updateMaster = (master, media) => {
resolveSegmentUris(segment, mergedPlaylist.resolvedUri);
});

if (mergedPlaylist.preloadSegment) {
resolveSegmentUris(mergedPlaylist.preloadSegment, mergedPlaylist.resolvedUri);
}

// TODO Right now in the playlists array there are two references to each playlist, one
// that is referenced by index, and one by URI. The index reference may no longer be
// necessary.
Expand Down Expand Up @@ -604,6 +625,10 @@ export default class PlaylistLoader extends EventTarget {
resolveSegmentUris(segment, playlist.resolvedUri);
});
}

if (playlist.preloadSegment) {
resolveSegmentUris(playlist.preloadSegment, playlist.resolvedUri);
}
});
this.trigger('loadedplaylist');
if (!this.request) {
Expand Down
29 changes: 22 additions & 7 deletions src/segment-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,7 @@ export default class SegmentLoader extends videojs.EventTarget {
this.roundTrip = NaN;
this.resetStats_();
this.mediaIndex = null;
this.partIndex = null;

// private settings
this.hasPlayed_ = settings.hasPlayed;
Expand Down Expand Up @@ -1057,6 +1058,7 @@ export default class SegmentLoader extends videojs.EventTarget {
segmentTransmuxer.reset(this.transmuxer_);
}
this.mediaIndex = null;
this.partIndex = null;
this.syncPoint_ = null;
this.isPendingTimestampOffset_ = false;
this.callQueue_ = [];
Expand Down Expand Up @@ -1192,7 +1194,8 @@ export default class SegmentLoader extends videojs.EventTarget {
this.mediaIndex,
this.hasPlayed_(),
this.currentTime_(),
this.syncPoint_
this.syncPoint_,
this.partIndex
);

if (!segmentInfo) {
Expand Down Expand Up @@ -1254,7 +1257,7 @@ export default class SegmentLoader extends videojs.EventTarget {
* @param {Object} syncPoint - a segment info object that describes the
* @return {Object} a segment request object that describes the segment to load
*/
checkBuffer_(buffered, playlist, currentMediaIndex, hasPlayed, currentTime, syncPoint) {
checkBuffer_(buffered, playlist, currentMediaIndex, hasPlayed, currentTime, syncPoint, currentPartIndex) {
let lastBufferedEnd = 0;

if (buffered.length) {
Expand All @@ -1279,6 +1282,7 @@ export default class SegmentLoader extends videojs.EventTarget {
return null;
}

let nextPartIndex = null;
let nextMediaIndex = null;
let startOfSegment;
let isSyncRequest = false;
Expand All @@ -1299,8 +1303,14 @@ export default class SegmentLoader extends videojs.EventTarget {
} else {
startOfSegment = lastBufferedEnd;
}
nextPartIndex = typeof currentPartIndex === 'number' ? currentPartIndex + 1 : 0;

nextMediaIndex = currentMediaIndex + 1;
if (!segment.parts || !segment.parts.length || !segment.parts[nextPartIndex]) {
nextMediaIndex = currentMediaIndex + 1;
nextPartIndex = 0;
} else {
nextMediaIndex = currentMediaIndex;
}

// There is a sync-point but the lack of a mediaIndex indicates that
// we need to make a good conservative guess about which segment to
Expand Down Expand Up @@ -1329,7 +1339,7 @@ export default class SegmentLoader extends videojs.EventTarget {
startOfSegment = mediaSourceInfo.startTime;
}

const segmentInfo = this.generateSegmentInfo_(playlist, nextMediaIndex, startOfSegment, isSyncRequest);
const segmentInfo = this.generateSegmentInfo_(playlist, nextMediaIndex, startOfSegment, isSyncRequest, nextPartIndex);

if (!segmentInfo) {
return;
Expand Down Expand Up @@ -1385,12 +1395,13 @@ export default class SegmentLoader extends videojs.EventTarget {
return Math.max(playlist.segments.length - 1, 0);
}

generateSegmentInfo_(playlist, mediaIndex, startOfSegment, isSyncRequest) {
generateSegmentInfo_(playlist, mediaIndex, startOfSegment, isSyncRequest, partIndex) {
if (mediaIndex < 0 || mediaIndex >= playlist.segments.length) {
return null;
}

const segment = playlist.segments[mediaIndex];
const part = segment.parts && segment.parts.length && segment.parts[partIndex];
const audioBuffered = this.sourceUpdater_.audioBuffered();
const videoBuffered = this.sourceUpdater_.videoBuffered();
let audioAppendStart;
Expand All @@ -1416,9 +1427,10 @@ export default class SegmentLoader extends videojs.EventTarget {
return {
requestId: 'segment-loader-' + Math.random(),
// resolve the segment URL relative to the playlist
uri: segment.resolvedUri,
uri: part && part.resolvedUri || segment.resolvedUri,
// the segment's mediaIndex at the time it was requested
mediaIndex,
partIndex,
// whether or not to update the SegmentLoader's state with this
// segment's mediaIndex
isSyncRequest,
Expand All @@ -1438,6 +1450,7 @@ export default class SegmentLoader extends videojs.EventTarget {
duration: segment.duration,
// retain the segment in case the playlist updates while doing an async process
segment,
part,
byteLength: 0,
transmuxer: this.transmuxer_,
audioAppendStart,
Expand Down Expand Up @@ -2257,7 +2270,8 @@ export default class SegmentLoader extends videojs.EventTarget {
requestId: segmentInfo.requestId,
transmuxer: segmentInfo.transmuxer,
audioAppendStart: segmentInfo.audioAppendStart,
gopsToAlignWith: segmentInfo.gopsToAlignWith
gopsToAlignWith: segmentInfo.gopsToAlignWith,
part: segmentInfo.part
};

const previousSegment = segmentInfo.playlist.segments[segmentInfo.mediaIndex - 1];
Expand Down Expand Up @@ -2780,6 +2794,7 @@ export default class SegmentLoader extends videojs.EventTarget {
this.trigger('progress');

this.mediaIndex = segmentInfo.mediaIndex;
this.partIndex = segmentInfo.partIndex;

// any time an update finishes and the last segment is in the
// buffer, end the stream. this ensures the "ended" event will
Expand Down

0 comments on commit 767fe7e

Please sign in to comment.