diff --git a/src/streaming/models/ThroughputModel.js b/src/streaming/models/ThroughputModel.js index 02bc06d8d5..b4d79039ad 100644 --- a/src/streaming/models/ThroughputModel.js +++ b/src/streaming/models/ThroughputModel.js @@ -185,20 +185,14 @@ function ThroughputModel(config) { * @private */ function _calculateThroughputValuesForXhr(httpRequest, latencyInMs) { - let resourceTimingValues = null; let downloadedBytes = NaN; let downloadTimeInMs = NaN; let deriveThroughputViaResourceTimingApi = false; - - if (settings.get().streaming.abr.throughput.useResourceTimingApi) { - resourceTimingValues = _deriveDownloadValuesFromResourceTimingApi(httpRequest) - } - - // Calculate the throughput using the ResourceTimingAPI if we got useful values - if (resourceTimingValues && !isNaN(resourceTimingValues.downloadedBytes) && !isNaN(resourceTimingValues.downloadTimeInMs)) { - downloadTimeInMs = resourceTimingValues.downloadTimeInMs; - downloadedBytes = resourceTimingValues.downloadedBytes; + // Calculate the throughput using the ResourceTimingAPI if available + if (settings.get().streaming.abr.throughput.useResourceTimingApi && httpRequest._resourceTimingValues) { + downloadedBytes = httpRequest._resourceTimingValues.transferSize; + downloadTimeInMs = httpRequest._resourceTimingValues.responseEnd - httpRequest._resourceTimingValues.responseStart; deriveThroughputViaResourceTimingApi = true; } @@ -231,36 +225,6 @@ function ThroughputModel(config) { }; } - /** - * Calculate the downloaded bytes and the download times using the resource timing API - * @param httpRequest - * @returns {{downloadTimeInMs: (*|number|NaN), downloadedBytes: (*|number|NaN)}} - * @private - */ - function _deriveDownloadValuesFromResourceTimingApi(httpRequest) { - let downloadedBytes = NaN; - let downloadTimeInMs = NaN; - - if (_areResourceTimingValuesUsable(httpRequest)) { - downloadedBytes = httpRequest._resourceTimingValues.transferSize; - downloadTimeInMs = httpRequest._resourceTimingValues.responseEnd - httpRequest._resourceTimingValues.responseStart; - } - - return { downloadedBytes, downloadTimeInMs } - } - - /** - * Checks if we got useful ResourceTimingAPI values - * @param httpRequest - * @param {boolean} ignoreTransferSize - * @returns {null|*|boolean} - * @private - */ - function _areResourceTimingValuesUsable(httpRequest, ignoreTransferSize = false) { - return settings.get().streaming.abr.throughput.useResourceTimingApi && httpRequest._resourceTimingValues && !isNaN(httpRequest._resourceTimingValues.responseStart) && httpRequest._resourceTimingValues.responseStart > 0 - && !isNaN(httpRequest._resourceTimingValues.responseEnd) && httpRequest._resourceTimingValues.responseEnd > 0 && ((!isNaN(httpRequest._resourceTimingValues.transferSize) && httpRequest._resourceTimingValues.transferSize > 0) || ignoreTransferSize) - } - /** * Return the current estimated bandwidth based on NetworkInformation.downlink if the API is available * @returns {*|number} @@ -285,7 +249,7 @@ function ThroughputModel(config) { */ function _isCachedResponse(mediaType, cacheReferenceTime, httpRequest) { - if (_areResourceTimingValuesUsable(httpRequest, true)) { + if (settings.get().streaming.abr.throughput.useResourceTimingApi && httpRequest._resourceTimingValues) { return httpRequest._resourceTimingValues.transferSize === 0 && httpRequest._resourceTimingValues.decodedBodySize > 0 } diff --git a/src/streaming/net/HTTPLoader.js b/src/streaming/net/HTTPLoader.js index 1a9b64c518..08e6ada949 100644 --- a/src/streaming/net/HTTPLoader.js +++ b/src/streaming/net/HTTPLoader.js @@ -490,16 +490,34 @@ function HTTPLoader(cfg) { i += 1; } + // Check if PerformanceResourceTiming values are usable + // Note: to allow seeing cross-origin timing information, the Timing-Allow-Origin HTTP response header needs to be set + // See https://developer.mozilla.org/en-US/docs/Web/API/PerformanceResourceTiming#cross-origin_timing_information + if (!_areResourceTimingValuesUsable(resource)) { + return; + } + httpRequest.customData.request.resourceTimingValues = resource; - // Use Resource Timing info when available for CommonMediaResponse - if (resource) { - httpResponse.resourceTiming.startTime = resource.startTime; - httpResponse.resourceTiming.encodedBodySize = resource.encodedBodySize; - httpResponse.resourceTiming.responseStart = resource.startTime; - httpResponse.resourceTiming.responseEnd = resource.responseEnd; - httpResponse.resourceTiming.duration = resource.duration; - } + // Update CommonMediaResponse Resource Timing info + httpResponse.resourceTiming.startTime = resource.startTime; + httpResponse.resourceTiming.encodedBodySize = resource.encodedBodySize; + httpResponse.resourceTiming.responseStart = resource.startTime; + httpResponse.resourceTiming.responseEnd = resource.responseEnd; + httpResponse.resourceTiming.duration = resource.duration; + } + + /** + * Checks if we got usable ResourceTimingAPI values + * @param httpRequest + * @returns {boolean} + * @private + */ + function _areResourceTimingValuesUsable(resource) { + return resource && + !isNaN(resource.responseStart) && resource.responseStart > 0 && + !isNaN(resource.responseEnd) && resource.responseEnd > 0 && + !isNaN(resource.transferSize) && resource.transferSize > 0 } /**