diff --git a/lib/hls/hls_parser.js b/lib/hls/hls_parser.js index 9e0df8da0b..f59bbd8cfa 100644 --- a/lib/hls/hls_parser.js +++ b/lib/hls/hls_parser.js @@ -2267,12 +2267,16 @@ shaka.hls.HlsParser = class { const ContentType = shaka.util.ManifestParserUtils.ContentType; const requestType = shaka.net.NetworkingEngine.RequestType.SEGMENT; + // If you wait long enough, requesting the first segment can fail + // because it has fallen off the left edge of DVR, so to be safer, + // let's request the middle segment. goog.asserts.assert(playlist.segments.length, 'Playlist should have segments!'); - const firstSegmentUri = this.variableSubstitution_( - playlist.segments[0].absoluteUri, variables); + const middleSegmentIdx = Math.trunc((playlist.segments.length - 1) / 2); + const middleSegmentUri = this.variableSubstitution_( + playlist.segments[middleSegmentIdx].absoluteUri, variables); - const parsedUri = new goog.Uri(firstSegmentUri); + const parsedUri = new goog.Uri(middleSegmentUri); const extension = parsedUri.getPath().split('.').pop(); const map = HlsParser.EXTENSION_MAP_BY_CONTENT_TYPE_[contentType]; @@ -2302,7 +2306,7 @@ shaka.hls.HlsParser = class { // If unable to guess mime type, request a segment and try getting it // from the response. const headRequest = shaka.net.NetworkingEngine.makeRequest( - [firstSegmentUri], this.config_.retryParameters); + [middleSegmentUri], this.config_.retryParameters); headRequest.method = 'HEAD'; const response = await this.makeNetworkRequest_(