Skip to content
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 HTTPLoader #4403

Merged
merged 13 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions src/streaming/net/FetchLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,6 @@ function FetchLoader() {

httpResponse.data = receivedData.buffer;
}
httpRequest.customData.onload();
httpRequest.customData.onloadend();
}

Expand Down Expand Up @@ -271,8 +270,8 @@ function FetchLoader() {
_read(httpRequest, httpResponse, _processResult);
})
.catch(function (e) {
if (httpRequest.customData.onerror) {
httpRequest.customData.onerror(e);
if (httpRequest.customData.onloadend) {
httpRequest.customData.onloadend(e);
}
});
});
Expand Down
147 changes: 68 additions & 79 deletions src/streaming/net/HTTPLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,19 +120,12 @@ function HTTPLoader(cfg) {
* @private
*/
function _internalLoad(config, remainingAttempts) {

/**
* Fired when a request has completed, whether successfully (after load) or unsuccessfully (after abort or error).
* Fired when a request has completed, whether successfully (after load) or unsuccessfully (after abort, timeout or error).
*/
const _onloadend = function () {
// Remove the request from our list of requests
if (httpRequests.indexOf(httpRequest) !== -1) {
httpRequests.splice(httpRequests.indexOf(httpRequest), 1);
}

if (progressTimeout) {
clearTimeout(progressTimeout);
progressTimeout = null;
}
_onRequestEnd();
};

/**
Expand Down Expand Up @@ -195,30 +188,57 @@ function HTTPLoader(cfg) {
}
};

const _oncomplete = function() {
// Update request timing info
requestObject.startDate = requestStartTime;
requestObject.endDate = new Date();
requestObject.firstByteDate = requestObject.firstByteDate || requestStartTime;
httpResponse.resourceTiming.responseEnd = requestObject.endDate.getTime();

// If enabled the ResourceTimingApi we add the corresponding information to the request object.
// These values are more accurate and can be used by the ThroughputController later
_addResourceTimingValues(httpRequest, httpResponse);
}
/**
* Fired when a request has been aborted, for example because the program called XMLHttpRequest.abort().
*/
const _onabort = function () {
_onRequestEnd(true)
};

/**
* Fired when an XMLHttpRequest transaction completes.
* This includes status codes such as 404. We handle errors in the _onError function.
* Fired when progress is terminated due to preset time expiring.
* @param event
*/
const _onload = function () {
_oncomplete();
const _ontimeout = function (event) {
let timeoutMessage;
// We know how much we already downloaded by looking at the timeout event
if (event.lengthComputable) {
let percentageComplete = (event.loaded / event.total) * 100;
timeoutMessage = 'Request timeout: loaded: ' + event.loaded + ', out of: ' + event.total + ' : ' + percentageComplete.toFixed(3) + '% Completed';
} else {
timeoutMessage = 'Request timeout: non-computable download size';
}
logger.warn(timeoutMessage);

_onRequestEnd();
};

const _onRequestEnd = function (aborted = false) {
// Remove the request from our list of requests
if (httpRequests.indexOf(httpRequest) !== -1) {
httpRequests.splice(httpRequests.indexOf(httpRequest), 1);
}

if (progressTimeout) {
clearTimeout(progressTimeout);
progressTimeout = null;
}

_updateResourceTimingInfo();

_applyResponseInterceptors(httpResponse).then((_httpResponse) => {
httpResponse = _httpResponse;

_addHttpRequestMetric(httpRequest, httpResponse, traces);

// Ignore aborted requests
if (aborted) {
if (config.abort) {
config.abort(requestObject);
}
return;
}

if (requestObject.type === HTTPRequest.MPD_TYPE) {
dashMetrics.addManifestUpdate(requestObject);
eventBus.trigger(Events.MANIFEST_LOADING_FINISHED, { requestObject });
Expand All @@ -233,72 +253,40 @@ function HTTPLoader(cfg) {
config.complete(requestObject, httpResponse.statusText);
}
} else {
_onerror();
// If we get a 404 to a media segment we should check the client clock again and perform a UTC sync in the background.
try {
if (httpResponse.status === 404 && settings.get().streaming.utcSynchronization.enableBackgroundSyncAfterSegmentDownloadError && requestObject.type === HTTPRequest.MEDIA_SEGMENT_TYPE) {
// Only trigger a sync if the loading failed for the first time
const initialNumberOfAttempts = mediaPlayerModel.getRetryAttemptsForType(HTTPRequest.MEDIA_SEGMENT_TYPE);
if (initialNumberOfAttempts === remainingAttempts) {
eventBus.trigger(Events.ATTEMPT_BACKGROUND_SYNC);
}
}
} catch (e) {}

_retriggerRequest();
}
});
};

/**
* Fired when a request has been aborted, for example because the program called XMLHttpRequest.abort().
*/
const _onabort = function () {
_oncomplete();
_addHttpRequestMetric(httpRequest, httpResponse, traces);
if (progressTimeout) {
clearTimeout(progressTimeout);
progressTimeout = null;
}
if (config.abort) {
config.abort(requestObject);
}
};

/**
* Fired when progress is terminated due to preset time expiring.
* @param event
*/
const _ontimeout = function (event) {
_oncomplete();

let timeoutMessage;
// We know how much we already downloaded by looking at the timeout event
if (event.lengthComputable) {
let percentageComplete = (event.loaded / event.total) * 100;
timeoutMessage = 'Request timeout: loaded: ' + event.loaded + ', out of: ' + event.total + ' : ' + percentageComplete.toFixed(3) + '% Completed';
} else {
timeoutMessage = 'Request timeout: non-computable download size';
}
logger.warn(timeoutMessage);
_addHttpRequestMetric(httpRequest, httpResponse, traces);
_retriggerRequest();
};

/**
* Fired when the request encountered an error.
*/
const _onerror = function () {
// If we get a 404 to a media segment we should check the client clock again and perform a UTC sync in the background.
try {
if (httpResponse.status === 404 && settings.get().streaming.utcSynchronization.enableBackgroundSyncAfterSegmentDownloadError && requestObject.type === HTTPRequest.MEDIA_SEGMENT_TYPE) {
// Only trigger a sync if the loading failed for the first time
const initialNumberOfAttempts = mediaPlayerModel.getRetryAttemptsForType(HTTPRequest.MEDIA_SEGMENT_TYPE);
if (initialNumberOfAttempts === remainingAttempts) {
eventBus.trigger(Events.ATTEMPT_BACKGROUND_SYNC);
}
}
} catch (e) {}
const _updateResourceTimingInfo = function() {
requestObject.startDate = requestStartTime;
requestObject.endDate = new Date();
requestObject.firstByteDate = requestObject.firstByteDate || requestStartTime;
httpResponse.resourceTiming.responseEnd = requestObject.endDate.getTime();

_retriggerRequest();
// If enabled the ResourceTimingApi we add the corresponding information to the request object.
// These values are more accurate and can be used by the ThroughputController later
_addResourceTimingValues(httpRequest, httpResponse);
}

const _loadRequest = function(loader, httpRequest, httpResponse) {
return new Promise((resolve) => {
_applyRequestInterceptors(httpRequest).then((_httpRequest) => {
httpRequest = _httpRequest;

httpRequest.customData.onload = _onload;
httpRequest.customData.onloadend = _onloadend;
httpRequest.customData.onerror = _onloadend;
httpRequest.customData.onprogress = _onprogress;
httpRequest.customData.onabort = _onabort;
httpRequest.customData.ontimeout = _ontimeout;
Expand All @@ -309,7 +297,7 @@ function HTTPLoader(cfg) {
});
});
}

/**
* Retriggers the request in case we did not exceed the number of retry attempts
* @private
Expand Down Expand Up @@ -397,7 +385,8 @@ function HTTPLoader(cfg) {
resourceTiming: {
startTime: Date.now(),
encodedBodySize: 0
}
},
status: 0
};

// Adds the ability to delay single fragment loading time to control buffer.
Expand Down
6 changes: 4 additions & 2 deletions src/streaming/net/XHRLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,15 @@ function XHRLoader() {
xhr.withCredentials = httpRequest.credentials === 'include';
xhr.timeout = httpRequest.timeout;

xhr.onload = function(e) {
xhr.onload = function() {
httpResponse.url = this.responseURL;
httpResponse.status = this.status;
httpResponse.statusText = this.statusText;
httpResponse.headers = Utils.parseHttpHeaders(this.getAllResponseHeaders());
httpResponse.data = this.response;
httpRequest.customData.onload(e);
if (httpRequest.customData.onload) {
httpRequest.customData.onload();
}
}
xhr.onloadend = httpRequest.customData.onloadend;
xhr.onerror = httpRequest.customData.onerror;
Expand Down