Skip to content

Commit

Permalink
Feature/performance optimizations light (#4590)
Browse files Browse the repository at this point in the history
* Webpack optimizations

* Reset BOLA rule parameters after stream switch

* Resetting additional classes

* WiP: Additional memory optimisations

* Add back adaptation to Representation

* Minor edits
  • Loading branch information
dsilhavy authored Oct 10, 2024
1 parent c070f5b commit 0f76359
Show file tree
Hide file tree
Showing 16 changed files with 140 additions and 40 deletions.
30 changes: 16 additions & 14 deletions build/webpack.base.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,26 @@ const commonConfig = {
target: ['web', 'es5'],
module: {
rules: [
{
test: /\.(js)$/,
loader: 'string-replace-loader',
options: {
search: '__VERSION__',
replace: pkg.version,
}
},
{
test: /\.(js)$/,
use: [
{
loader: `babel-loader`,
options: { presets: ['@babel/env'] }
}
]
}
]
loader: 'string-replace-loader',
options: {
search: '__VERSION__',
replace: pkg.version,
},
},
{
loader: 'babel-loader',
options: {
targets: 'defaults',
presets: ['@babel/preset-env']
},
},
],
},
],
},
//Webpack 5 no longer polyfills Node.js core modules automatically
resolve: {
Expand Down
1 change: 1 addition & 0 deletions src/dash/controllers/RepresentationController.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ function RepresentationController(config) {
}

function resetInitialSettings() {
currentVoRepresentation = null;
voAvailableRepresentations = [];
}

Expand Down
12 changes: 10 additions & 2 deletions src/streaming/FragmentLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,18 +153,26 @@ function FragmentLoader(config) {
}
}

function resetInitialSettings() {
if (urlLoader) {
urlLoader.resetInitialSettings();
}
}

function reset() {
if (urlLoader) {
urlLoader.abort();
urlLoader.reset();
urlLoader = null;
}
}

instance = {
abort,
checkForExistence,
load,
abort,
reset
reset,
resetInitialSettings
};

setup();
Expand Down
5 changes: 3 additions & 2 deletions src/streaming/SourceBufferSink.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ function SourceBufferSink(config) {
return type;
}

function _removeEventListeners() {
function removeEventListeners() {
try {
if (typeof buffer.removeEventListener === 'function') {
buffer.removeEventListener('updateend', _updateEndHandler, false);
Expand Down Expand Up @@ -243,7 +243,7 @@ function SourceBufferSink(config) {
if (buffer) {
try {
callbacks = [];
_removeEventListeners();
removeEventListeners();
isAppendingInProgress = false;
appendQueue = [];
if (!buffer.getClassName || buffer.getClassName() !== 'TextSourceBuffer') {
Expand Down Expand Up @@ -477,6 +477,7 @@ function SourceBufferSink(config) {
initializeForFirstUse,
initializeForStreamSwitch,
remove,
removeEventListeners,
reset,
updateAppendWindow,
updateTimestampOffset,
Expand Down
3 changes: 3 additions & 0 deletions src/streaming/controllers/AbrController.js
Original file line number Diff line number Diff line change
Expand Up @@ -847,8 +847,11 @@ function AbrController() {
if (abandonmentStateDict[streamId]) {
delete abandonmentStateDict[streamId];
}

abrRulesCollection.clearDataForStream(streamId);
}


instance = {
checkPlaybackQuality,
clearDataForStream,
Expand Down
16 changes: 10 additions & 6 deletions src/streaming/controllers/BufferController.js
Original file line number Diff line number Diff line change
Expand Up @@ -1256,12 +1256,16 @@ function BufferController(config) {
if (sourceBufferSink) {
let tmpSourceBufferSinkToReset = sourceBufferSink;
sourceBufferSink = null;
if (!errored && !keepBuffers) {
tmpSourceBufferSinkToReset.abort()
.then(() => {
tmpSourceBufferSinkToReset.reset(keepBuffers);
tmpSourceBufferSinkToReset = null;
});
if (!errored) {
if (!keepBuffers) {
tmpSourceBufferSinkToReset.abort()
.then(() => {
tmpSourceBufferSinkToReset.reset(keepBuffers);
tmpSourceBufferSinkToReset = null;
});
} else {
tmpSourceBufferSinkToReset.removeEventListeners();
}
}
}

Expand Down
1 change: 1 addition & 0 deletions src/streaming/controllers/StreamController.js
Original file line number Diff line number Diff line change
Expand Up @@ -1225,6 +1225,7 @@ function StreamController() {
if (!shouldKeepStream) {
logger.debug(`Removing stream ${stream.getId()}`);
stream.reset(true);
stream = null;
}

return shouldKeepStream;
Expand Down
3 changes: 3 additions & 0 deletions src/streaming/models/FragmentModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,9 @@ function FragmentModel(config) {
function resetInitialSettings() {
executedRequests = [];
loadingRequests = [];
if (fragmentLoader) {
fragmentLoader.resetInitialSettings();
}
}

function reset() {
Expand Down
9 changes: 7 additions & 2 deletions src/streaming/net/FetchLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,10 @@ function FetchLoader() {
}
}

function reset() {

}

/**
* Default throughput calculation
* @param downloadedData
Expand Down Expand Up @@ -473,10 +477,11 @@ function FetchLoader() {
}

instance = {
load,
abort,
calculateDownloadedTime,
load,
reset,
setConfig,
calculateDownloadedTime
};

return instance;
Expand Down
26 changes: 24 additions & 2 deletions src/streaming/net/HTTPLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -680,10 +680,32 @@ function HTTPLoader(cfg) {
httpRequests = [];
}

function resetInitialSettings() {
if (xhrLoader) {
xhrLoader.resetInitialSettings();
}
}

function reset() {
httpRequests = [];
delayedRequests = [];
retryRequests = [];
if (xhrLoader) {
xhrLoader.reset();
}
if (fetchLoader) {
fetchLoader.reset();
}
xhrLoader = null;
fetchLoader = null;
}

instance = {
load,
abort,
setConfig
load,
reset,
resetInitialSettings,
setConfig,
};

setup();
Expand Down
10 changes: 5 additions & 5 deletions src/streaming/net/SchemeLoaderFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,11 @@ function SchemeLoaderFactory() {
setup();

instance = {
getLoader: getLoader,
registerLoader: registerLoader,
unregisterLoader: unregisterLoader,
unregisterAllLoader: unregisterAllLoader,
reset: reset
getLoader,
registerLoader,
unregisterLoader,
unregisterAllLoader,
reset
};

return instance;
Expand Down
27 changes: 24 additions & 3 deletions src/streaming/net/URLLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import SchemeLoaderFactory from '../../streaming/net/SchemeLoaderFactory.js';
* @description Call Offline Loader or Online Loader depending on URL
* @param {Object} cfg - dependencies
* @ignore
*/
*/
function URLLoader(cfg) {

cfg = cfg || {};
Expand Down Expand Up @@ -71,14 +71,35 @@ function URLLoader(cfg) {
loader.abort();
}
}

function resetInitialSettings() {
if (loader && typeof loader.resetInitialSettings === 'function') {
loader.resetInitialSettings();
}
}

function reset() {
if (schemeLoaderFactory) {
schemeLoaderFactory.reset();
schemeLoaderFactory = null;
}
if (loader && typeof loader.reset === 'function') {
loader.reset();
}
loader = null;
}

instance = {
load: load,
abort: abort
abort,
load,
reset,
resetInitialSettings
};

return instance;

}

URLLoader.__dashjs_factory_name = 'URLLoader';

const factory = FactoryMaker.getClassFactory(URLLoader);
Expand Down
23 changes: 19 additions & 4 deletions src/streaming/net/XHRLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ function XHRLoader() {
* @param {CommonMediaResponse} httpResponse
*/
function load(httpRequest, httpResponse) {
xhr = null;
xhr = new XMLHttpRequest();
xhr.open(httpRequest.method, httpRequest.url, true);

Expand All @@ -66,7 +67,7 @@ function XHRLoader() {
xhr.withCredentials = httpRequest.credentials === 'include';
xhr.timeout = httpRequest.timeout;

xhr.onload = function() {
xhr.onload = function () {
httpResponse.url = this.responseURL;
httpResponse.status = this.status;
httpResponse.statusText = this.statusText;
Expand All @@ -85,18 +86,32 @@ function XHRLoader() {
}

function abort() {
xhr.onloadend = xhr.onerror = xhr.onprogress = null; // Ignore events from aborted requests.
xhr.abort();
if (xhr) {
xhr.onloadend = xhr.onerror = xhr.onprogress = xhr.onload = null; // Remove event listeners
xhr.abort();
xhr = null;
}
}

function getXhr() {
return xhr
}

function resetInitialSettings() {
abort();
}

function reset() {
abort();
instance = null;
}

instance = {
load,
abort,
getXhr
getXhr,
reset,
resetInitialSettings
};

return instance;
Expand Down
9 changes: 9 additions & 0 deletions src/streaming/rules/abr/ABRRulesCollection.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,14 @@ function ABRRulesCollection(config) {
eventBus.off(Events.SETTING_UPDATED_ABR_ACTIVE_RULES, _onAbrSettingsActiveRulesUpdated, instance);
}

function clearDataForStream(streamId) {
[qualitySwitchRules, abandonFragmentRules].forEach(rules => {
if (rules && rules.length) {
rules.forEach(rule => rule.clearDataForStream && typeof rule.clearDataForStream === 'function' && rule.clearDataForStream(streamId));
}
});
}

function getQualitySwitchRules() {
return qualitySwitchRules;
}
Expand All @@ -300,6 +308,7 @@ function ABRRulesCollection(config) {
}

instance = {
clearDataForStream,
getAbandonFragmentRules,
getBestPossibleSwitchRequest,
getBolaState,
Expand Down
1 change: 1 addition & 0 deletions src/streaming/rules/abr/BolaRule.js
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,7 @@ function BolaRule(config) {
const mediaType = rulesContext.getMediaType();
const streamId = rulesContext.getStreamInfo().id;
if (!bolaStateDict[streamId]) {
bolaStateDict = {};
bolaStateDict[streamId] = {};
}
let bolaState = bolaStateDict[streamId][mediaType];
Expand Down
4 changes: 4 additions & 0 deletions test/unit/test/streaming/streaming.models.FragmentModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import DashMetricsMock from '../../mocks/DashMetricsMock.js';
import chai from 'chai';
import spies from 'chai-spies';
import sinon from 'sinon';

const expect = chai.expect;

chai.use(spies);
Expand Down Expand Up @@ -62,6 +63,9 @@ describe('FragmentModel', function () {
load: () => {
}, abort: () => {
}, reset: () => {
},
resetInitialSettings: () => {

}
};
const delay = specHelper.getExecutionDelay();
Expand Down

0 comments on commit 0f76359

Please sign in to comment.