All files / dash.js/src/streaming/rules/abr SwitchHistoryRule.js

100% Statements 30/30
66.66% Branches 12/18
100% Functions 2/2
100% Lines 29/29

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63            55x 55x       2x 2x   2x 1x     1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x   1x 1x 1x 1x         1x         1x     55x           55x       1x    
import FactoryMaker from '../../../core/FactoryMaker.js';
import SwitchRequest from '../SwitchRequest.js';
import Settings from '../../../core/Settings.js';
 
function SwitchHistoryRule() {
 
    const context = this.context;
    const settings = Settings(context).getInstance();
    let instance;
 
    function getSwitchRequest(rulesContext) {
        const switchRequest = SwitchRequest(context).create();
        switchRequest.rule = this.getClassName();
 
        if (!rulesContext) {
            return switchRequest;
        }
 
        const streamId = rulesContext.getStreamInfo().id;
        const mediaType = rulesContext.getMediaType();
        const switchRequestHistory = rulesContext ? rulesContext.getSwitchRequestHistory() : null;
        const switchRequests = switchRequestHistory ? switchRequestHistory.getSwitchRequests(streamId, mediaType) : {};
        const abrController = rulesContext.getAbrController();
        const mediaInfo = rulesContext.getMediaInfo();
        const representations = abrController.getPossibleVoRepresentationsFilteredBySettings(mediaInfo, true);
        let drops = 0;
        let noDrops = 0;
 
        for (let i = 0; i < representations.length; i++) {
            const currentPossibleRepresentation = representations[i];
            if (currentPossibleRepresentation && switchRequests[currentPossibleRepresentation.id]) {
                drops += switchRequests[currentPossibleRepresentation.id].drops;
                noDrops += switchRequests[currentPossibleRepresentation.id].noDrops;
 
                if (drops + noDrops >= settings.get().streaming.abr.rules.switchHistoryRule.parameters.sampleSize && (drops / noDrops > settings.get().streaming.abr.rules.switchHistoryRule.parameters.switchPercentageThreshold)) {
                    switchRequest.representation = (i > 0 && switchRequests[currentPossibleRepresentation.id].drops > 0) ? representations[i - 1] : currentPossibleRepresentation;
                    switchRequest.priority = settings.get().streaming.abr.rules.switchHistoryRule.priority;
                    switchRequest.reason = {
                        drops: drops,
                        noDrops: noDrops,
                        message: `[SwitchHistoryRule]: Switch to index: ${switchRequest.representation.absoluteIndex} samples: ${(drops + noDrops)} drops:  ${drops}`
                    };
                    break;
                }
            }
        }
 
        return switchRequest;
    }
 
    instance = {
        getSwitchRequest
    };
 
 
 
    return instance;
}
 
 
SwitchHistoryRule.__dashjs_factory_name = 'SwitchHistoryRule';
export default FactoryMaker.getClassFactory(SwitchHistoryRule);