-
Notifications
You must be signed in to change notification settings - Fork 425
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
fix: more accurate segment choices and logging #1127
Changes from 13 commits
a4a45df
d2d4aab
127e075
6ddcf4c
fa97a95
eae4f41
17dda63
ad088c5
8ebbc50
c6f36e4
2bd3f53
dd29591
a971ff0
504f434
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -32,30 +32,33 @@ import {getKnownPartCount} from './playlist.js'; | |||||||||
* generate a syncPoint. This function returns a good candidate index | ||||||||||
* for that process. | ||||||||||
* | ||||||||||
* @param {Object} playlist - the playlist object to look for a | ||||||||||
* @param {Array} segments - the segments array from a playlist. | ||||||||||
* @return {number} An index of a segment from the playlist to load | ||||||||||
*/ | ||||||||||
export const getSyncSegmentCandidate = function(currentTimeline, {segments = []} = {}) { | ||||||||||
// if we don't currently have a real timeline yet. | ||||||||||
if (currentTimeline === -1) { | ||||||||||
return 0; | ||||||||||
} | ||||||||||
export const getSyncSegmentCandidate = function(currentTimeline, segments, targetTime) { | ||||||||||
segments = segments || []; | ||||||||||
const timelineSegments = []; | ||||||||||
let time = 0; | ||||||||||
|
||||||||||
for (let i = 0; i < segments.length; i++) { | ||||||||||
const segment = segments[i]; | ||||||||||
|
||||||||||
if (currentTimeline === segment.timeline) { | ||||||||||
timelineSegments.push(i); | ||||||||||
time += segment.duration; | ||||||||||
|
||||||||||
const segmentIndexArray = segments.reduce((acc, s, i) => { | ||||||||||
if (s.timeline === currentTimeline) { | ||||||||||
acc.push(i); | ||||||||||
if (time > targetTime) { | ||||||||||
return i; | ||||||||||
} | ||||||||||
} | ||||||||||
return acc; | ||||||||||
}, []); | ||||||||||
} | ||||||||||
|
||||||||||
if (segmentIndexArray.length) { | ||||||||||
// TODO: why do we do this? Basically we choose index 0 if | ||||||||||
// segmentIndexArray.length is 1 and index = 1 if segmentIndexArray.length | ||||||||||
// is greater then 1 | ||||||||||
return segmentIndexArray[Math.min(segmentIndexArray.length - 1, 1)]; | ||||||||||
if (timelineSegments.length === 0) { | ||||||||||
return 0; | ||||||||||
} | ||||||||||
|
||||||||||
return Math.max(segments.length - 1, 0); | ||||||||||
// default to the last timeline segment | ||||||||||
return timelineSegments[timelineSegments.length - 1]; | ||||||||||
}; | ||||||||||
|
||||||||||
// In the event of a quota exceeded error, keep at least one second of back buffer. This | ||||||||||
|
@@ -137,6 +140,7 @@ const segmentInfoString = (segmentInfo) => { | |||||||||
startOfSegment, | ||||||||||
duration, | ||||||||||
segment, | ||||||||||
part, | ||||||||||
playlist: { | ||||||||||
mediaSequence: seq, | ||||||||||
id, | ||||||||||
|
@@ -155,15 +159,15 @@ const segmentInfoString = (segmentInfo) => { | |||||||||
} else if (segmentInfo.isSyncRequest) { | ||||||||||
selection = 'getSyncSegmentCandidate (isSyncRequest)'; | ||||||||||
} | ||||||||||
const {start, end} = segment; | ||||||||||
|
||||||||||
const hasPartIndex = typeof partIndex === 'number'; | ||||||||||
const name = segmentInfo.segment.uri ? 'segment' : 'pre-segment'; | ||||||||||
const totalParts = hasPartIndex ? getKnownPartCount({preloadSegment: segment}) - 1 : 0; | ||||||||||
|
||||||||||
return `${name} [${index}/${segmentLen}]` + | ||||||||||
return `${name} [${seq + index}/${seq + segmentLen}]` + | ||||||||||
(hasPartIndex ? ` part [${partIndex}/${totalParts}]` : '') + | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this take into account the 0 based indexing vs total. Maybe we can just say `[index 0, 3 total] There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should keep the name |
||||||||||
` mediaSequenceNumber [${seq}/${seq + segmentLen}]` + | ||||||||||
` start/end [${start} => ${end}]` + | ||||||||||
` segment start/end [${segment.start} => ${segment.end}]` + | ||||||||||
(hasPartIndex ? ` part start/end [${part.start} => ${part.end}]` : '') + | ||||||||||
` startOfSegment [${startOfSegment}]` + | ||||||||||
` duration [${duration}]` + | ||||||||||
` timeline [${timeline}]` + | ||||||||||
|
@@ -1381,7 +1385,7 @@ export default class SegmentLoader extends videojs.EventTarget { | |||||||||
}; | ||||||||||
|
||||||||||
if (next.isSyncRequest) { | ||||||||||
next.mediaIndex = getSyncSegmentCandidate(this.currentTimeline_, this.playlist_); | ||||||||||
next.mediaIndex = getSyncSegmentCandidate(this.currentTimeline_, segments, bufferedEnd); | ||||||||||
} else if (this.mediaIndex !== null) { | ||||||||||
const segment = segments[this.mediaIndex]; | ||||||||||
const partIndex = typeof this.partIndex === 'number' ? this.partIndex : -1; | ||||||||||
|
@@ -2030,6 +2034,31 @@ export default class SegmentLoader extends videojs.EventTarget { | |||||||||
// as we use the start of the segment to offset the best guess (playlist provided) | ||||||||||
// timestamp offset. | ||||||||||
this.updateSourceBufferTimestampOffset_(segmentInfo); | ||||||||||
|
||||||||||
// if this is a sync request we need to determine whether it should | ||||||||||
// be appended or not. | ||||||||||
if (segmentInfo.isSyncRequest) { | ||||||||||
// first save/update our timing info for this segment. | ||||||||||
// this is what allows us to choose an accurate segment | ||||||||||
// and the main reason we make a sync request. | ||||||||||
this.updateTimingInfoEnd_(segmentInfo); | ||||||||||
this.syncController_.saveSegmentTimingInfo({ | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We may have to be careful here, since we're losing some of our timing logic, which may lead to inaccurate next requests: http-streaming/src/segment-loader.js Lines 2860 to 2863 in 1c7a63b
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we call this function within this block? From a brief glance it seems like it only estimates the segment end point using start/duration unless we have an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should probably call it. On that note we may also want to consider adding a property to the segment whether the last time we requested that segment it was a sync request (and clear it if it's requested on a non sync request). It may help us for debugging, particularly now that it's no longer being appended. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. called |
||||||||||
segmentInfo, | ||||||||||
shouldSaveTimelineMapping: this.loaderType_ === 'main' | ||||||||||
}); | ||||||||||
|
||||||||||
const next = this.chooseNextRequest_(); | ||||||||||
|
||||||||||
// If the sync request isn't the segment that would be requested next | ||||||||||
// after taking into account its timing info, do not append it. | ||||||||||
if (next.mediaIndex !== segmentInfo.mediaIndex || next.partIndex !== segmentInfo.partIndex) { | ||||||||||
this.logger_('sync segment was incorrect, not appending'); | ||||||||||
return; | ||||||||||
} | ||||||||||
// otherwise append it like any other segment as our guess was correct. | ||||||||||
this.logger_('sync segment was correct, appending'); | ||||||||||
} | ||||||||||
|
||||||||||
// Save some state so that in the future anything waiting on first append (and/or | ||||||||||
// timestamp offset(s)) can process immediately. While the extra state isn't optimal, | ||||||||||
// we need some notion of whether the timestamp offset or other relevant information | ||||||||||
|
@@ -2884,8 +2913,6 @@ export default class SegmentLoader extends videojs.EventTarget { | |||||||||
}); | ||||||||||
} | ||||||||||
|
||||||||||
this.logger_(`Appended ${segmentInfoString(segmentInfo)}`); | ||||||||||
|
||||||||||
const segmentDurationMessage = | ||||||||||
getTroublesomeSegmentDurationMessage(segmentInfo, this.sourceType_); | ||||||||||
|
||||||||||
|
@@ -2903,9 +2930,18 @@ export default class SegmentLoader extends videojs.EventTarget { | |||||||||
|
||||||||||
if (segmentInfo.isSyncRequest) { | ||||||||||
this.trigger('syncinfoupdate'); | ||||||||||
return; | ||||||||||
// if the sync request was not appended | ||||||||||
// then it was not the correct segment. | ||||||||||
// throw it away and use the data it gave us | ||||||||||
// to get the correct one. | ||||||||||
if (!segmentInfo.hasAppendedData_) { | ||||||||||
this.logger_(`Throwing away un-appended sync request ${segmentInfoString(segmentInfo)}`); | ||||||||||
return; | ||||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
this.logger_(`Appended ${segmentInfoString(segmentInfo)}`); | ||||||||||
|
||||||||||
this.addSegmentMetadataCue_(segmentInfo); | ||||||||||
this.fetchAtBuffer_ = true; | ||||||||||
if (this.currentTimeline_ !== segmentInfo.timeline) { | ||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -870,7 +870,7 @@ export const LoaderCommonFactory = ({ | |
}); | ||
|
||
QUnit.test('drops partIndex if playlist update drops parts', function(assert) { | ||
assert.timeout(100000000000000000000); | ||
loader.duration_ = () => Infinity; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ie 11 has the duration as NaN here, so sync-controller gets a sync point of |
||
return setupMediaSource(loader.mediaSource_, loader.sourceUpdater_).then(() => { | ||
loader.playlist(playlistWithDuration(50, { | ||
mediaSequence: 0, | ||
|
@@ -899,8 +899,8 @@ export const LoaderCommonFactory = ({ | |
|
||
assert.equal( | ||
this.requests[0].url, | ||
'1.ts', | ||
'requested mediaIndex 1 only' | ||
'0.ts', | ||
'requested mediaIndex 0 only' | ||
); | ||
}); | ||
}); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we keep
TIME_FUDGE_FACTOR
at all?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
question: What is
TIME_FUDGE_FACTOR
used for here? Is using it causing timing issues down the line?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this may be the wrong place for us to use time fudging. We should probably only ever fudge time in one place: when we are trying to determine the next segment to request and are not simply walking the playlist (mediaIndex++). In that case, all requests to get media info/segment info should be as precise as possible given the current segment information, then, once the final segment is determined, we should decide whether we want to fudge the time at all to conservatively request a segment (i.e., whether to request back 1 segment, or, as an alternative, get media info for time with a fudge factor included).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this change basically encompasses what you are saying. We only use
getMediaIndexForTime
when we are not incrementing mediaIndex/partIndex. This change makes it so that cumulatively we can only be off byTIME_FUDGE_FACTOR
rather than every single part/segments duration being incremented or decremented byTIME_FUDGE_FACTOR
. For the sake of time I think that we might want to add a TODO here to see if we should even use TIME_FUDGE_FACTOR at all. I lean towards not using it, but it will require a bit more testing and could easily enough be an isolated change.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 a TODO would be good for now. This definitely is better than before.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably worth making an issue/a note in our major/refactoring epic
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added