All files / dash.js/src/streaming/controllers CatchupController.js

88.8% Statements 119/134
75.6% Branches 62/82
100% Functions 22/22
88.8% Lines 119/134

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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435                                                                                46x 46x                           68x 68x       74x 1x     73x 72x     73x 71x     73x 71x     73x 71x     73x 71x         68x 68x 68x 68x 68x 68x 68x 68x 68x       28x 28x 28x 28x 28x 28x 28x 28x 28x       46x   46x       28x 28x 28x       74x 74x 74x         1x                   2x 1x       1x 1x 1x 1x                     4x 1x     3x               1x 1x                 10x             6x                   6x       6x   6x 6x 6x 6x     6x 6x   1x 1x 1x         5x 5x   5x   2x 2x     3x         5x     5x 5x 5x                       12x 12x   12x               6x 6x       6x   6x 2x 2x   2x   4x                             11x   11x                 4x 4x   4x                           2x 2x   2x                                       3x 3x                                                                               2x   1x 1x 1x       1x 1x   1x       1x 1x     1x   1x 1x       1x 1x     1x     2x       71x     46x           46x   46x     1x    
/**
 * The copyright in this software is being made available under the BSD License,
 * included below. This software may be subject to other third party and contributor
 * rights, including patent rights, and no such rights are granted under this license.
 *
 * Copyright (c) 2013, Dash Industry Forum.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *  * Redistributions of source code must retain the above copyright notice, this
 *  list of conditions and the following disclaimer.
 *  * Redistributions in binary form must reproduce the above copyright notice,
 *  this list of conditions and the following disclaimer in the documentation and/or
 *  other materials provided with the distribution.
 *  * Neither the name of Dash Industry Forum nor the names of its
 *  contributors may be used to endorse or promote products derived from this software
 *  without specific prior written permission.
 *
 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS AND ANY
 *  EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 *  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 *  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 *  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 *  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 *  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 *  POSSIBILITY OF SUCH DAMAGE.
 */
import FactoryMaker from '../../core/FactoryMaker.js';
import Debug from '../../core/Debug.js';
import EventBus from '../../core/EventBus.js';
import Constants from '../constants/Constants.js';
import MediaPlayerEvents from '../MediaPlayerEvents.js';
import Events from '../../core/events/Events.js';
import MetricsConstants from '../constants/MetricsConstants.js';
import Utils from '../../core/Utils.js';
 
function CatchupController() {
    const context = this.context;
    const eventBus = EventBus(context).getInstance();
 
    let instance,
        isCatchupSeekInProgress,
        isSafari,
        videoModel,
        settings,
        streamController,
        playbackController,
        mediaPlayerModel,
        playbackStalled,
        logger;
 
    function initialize() {
        _registerEvents();
        _checkPlaybackRates();
    }
 
    function setConfig(config) {
        if (!config) {
            return;
        }
 
        if (config.settings) {
            settings = config.settings;
        }
 
        if (config.videoModel) {
            videoModel = config.videoModel;
        }
 
        if (config.streamController) {
            streamController = config.streamController;
        }
 
        if (config.playbackController) {
            playbackController = config.playbackController;
        }
 
        if (config.mediaPlayerModel) {
            mediaPlayerModel = config.mediaPlayerModel;
        }
    }
 
    function _registerEvents() {
        eventBus.on(MediaPlayerEvents.BUFFER_LEVEL_UPDATED, _onBufferLevelUpdated, instance);
        eventBus.on(MediaPlayerEvents.BUFFER_LEVEL_STATE_CHANGED, _onBufferLevelStateChanged, instance);
        eventBus.on(MediaPlayerEvents.PLAYBACK_PROGRESS, _onPlaybackProgression, instance);
        eventBus.on(MediaPlayerEvents.PLAYBACK_TIME_UPDATED, _onPlaybackProgression, instance);
        eventBus.on(MediaPlayerEvents.PLAYBACK_SEEKED, _onPlaybackSeeked, instance);
        eventBus.on(Events.SETTING_UPDATED_CATCHUP_ENABLED, _onCatchupSettingUpdated, instance);
        eventBus.on(Events.SETTING_UPDATED_PLAYBACK_RATE_MIN, _checkPlaybackRates, instance);
        eventBus.on(Events.SETTING_UPDATED_PLAYBACK_RATE_MAX, _checkPlaybackRates, instance);
        eventBus.on(MediaPlayerEvents.STREAM_INITIALIZED, _checkPlaybackRates, instance);
    }
 
    function _unregisterEvents() {
        eventBus.off(MediaPlayerEvents.BUFFER_LEVEL_UPDATED, _onBufferLevelUpdated, instance);
        eventBus.off(MediaPlayerEvents.BUFFER_LEVEL_STATE_CHANGED, _onBufferLevelStateChanged, instance);
        eventBus.off(MediaPlayerEvents.PLAYBACK_PROGRESS, _onPlaybackProgression, instance);
        eventBus.off(MediaPlayerEvents.PLAYBACK_TIME_UPDATED, _onPlaybackProgression, instance);
        eventBus.off(MediaPlayerEvents.PLAYBACK_SEEKED, _onPlaybackSeeked, instance);
        eventBus.off(Events.SETTING_UPDATED_CATCHUP_ENABLED, _onCatchupSettingUpdated, instance);
        eventBus.off(Events.SETTING_UPDATED_PLAYBACK_RATE_MIN, _checkPlaybackRates, instance);
        eventBus.off(Events.SETTING_UPDATED_PLAYBACK_RATE_MAX, _checkPlaybackRates, instance);
        eventBus.off(MediaPlayerEvents.STREAM_INITIALIZED, _checkPlaybackRates, instance);
    }
 
    function setup() {
        logger = Debug(context).getInstance().getLogger(instance);
 
        _resetInitialSettings();
    }
 
    function reset() {
        _unregisterEvents();
        _resetInitialSettings();
        videoModel.setPlaybackRate(1.0, true);
    }
 
    function _resetInitialSettings() {
        isCatchupSeekInProgress = false;
        const ua = Utils.parseUserAgent();
        isSafari = ua && ua.browser && ua.browser.name && ua.browser.name.toLowerCase() === 'safari';
    }
 
 
    function _onPlaybackSeeked() {
        isCatchupSeekInProgress = false;
    }
 
    /**
     * When the buffer level updated we check if we can remove the stalled state
     * @param {object} e
     * @private
     */
    function _onBufferLevelUpdated(e) {
        // do not stop when getting an event from Stream that is not active
        if (e.streamId !== streamController.getActiveStreamInfo().id || !playbackStalled) {
            return;
        }
 
        // we remove the stalled state once we reach a certain buffer level
        const liveDelay = playbackController.getLiveDelay();
        const bufferLevel = playbackController.getBufferLevel();
        if (bufferLevel > liveDelay / 2) {
            playbackStalled = false;
        }
    }
 
    /**
     * When the buffer state changed to BUFFER_EMPTY we update the stalled state
     * @param {object} e
     * @private
     */
    function _onBufferLevelStateChanged(e) {
        // do not stop when getting an event from Stream that is not active
        if (e.streamId !== streamController.getActiveStreamInfo().id) {
            return;
        }
 
        playbackStalled = e.state === MetricsConstants.BUFFER_EMPTY;
    }
 
    /**
     * If the catchup mode is disabled in the settings we reset playback rate to 1.0
     * @private
     */
    function _onCatchupSettingUpdated() {
        if (!mediaPlayerModel.getCatchupModeEnabled()) {
            videoModel.setPlaybackRate(1.0);
        }
    }
 
    /**
     * While playback is progressing we check if we need to start or stop the catchup mechanism to reach the target latency
     * @private
     */
    function _onPlaybackProgression() {
        if (
            playbackController.getIsDynamic() &&
            mediaPlayerModel.getCatchupModeEnabled() &&
            ((mediaPlayerModel.getCatchupPlaybackRates().max > 0) || (mediaPlayerModel.getCatchupPlaybackRates().min < 0)) &&
            !playbackController.isPaused() &&
            !playbackController.isSeeking() && _shouldStartCatchUp()
        ) {
            _startPlaybackCatchUp();
        }
    }
 
    /**
     * Apply catchup mode. We either seek back to the target live edge or increase the playback rate.
     */
    function _startPlaybackCatchUp() {
 
        // we are seeking dont do anything for now
        Iif (isCatchupSeekInProgress) {
            return;
        }
 
        if (videoModel) {
            let newRate;
            const currentPlaybackRate = videoModel.getPlaybackRate();
            const liveCatchupPlaybackRates = mediaPlayerModel.getCatchupPlaybackRates();
            const bufferLevel = playbackController.getBufferLevel();
            const deltaLatency = _getLatencyDrift();
 
            // we reached the maxDrift. Do a seek
            const maxDrift = mediaPlayerModel.getCatchupMaxDrift();
            if (!isNaN(maxDrift) && maxDrift > 0 &&
                deltaLatency > maxDrift) {
                logger.info('[CatchupController]: Low Latency catchup mechanism. Latency too high, doing a seek to live point');
                isCatchupSeekInProgress = true;
                playbackController.seekToCurrentLive(true, false);
            }
 
            // try to reach the target latency by adjusting the playback rate
            else {
                const currentLiveLatency = playbackController.getCurrentLiveLatency();
                const targetLiveDelay = playbackController.getLiveDelay();
 
                if (_getCatchupMode() === Constants.LIVE_CATCHUP_MODE_LOLP) {
                    // Custom playback control: Based on buffer level
                    const playbackBufferMin = settings.get().streaming.liveCatchup.playbackBufferMin;
                    newRate = _calculateNewPlaybackRateLolP(liveCatchupPlaybackRates, currentLiveLatency, targetLiveDelay, playbackBufferMin, bufferLevel);
                } else {
                    // Default playback control: Based on target and current latency
                    newRate = _calculateNewPlaybackRateDefault(liveCatchupPlaybackRates, currentLiveLatency, targetLiveDelay, bufferLevel);
                }
 
                // We adjust the min change linear, depending on the maximum catchup rate. Default is 0.02 for rate 0.5.
                // For Safari we stick to a fixed value because of  https://bugs.webkit.org/show_bug.cgi?id=208142
                const minPlaybackRateChange = isSafari ? 0.25 : 0.02 / (0.5 / liveCatchupPlaybackRates.max);
 
                // Obtain newRate and apply to video model.  Don't change playbackrate for small variations (don't overload element with playbackrate changes)
                if (newRate && Math.abs(currentPlaybackRate - newRate) >= minPlaybackRateChange || newRate == 1.0) { // non-null
                    logger.debug(`[CatchupController]: Setting playback rate to ${newRate}`);
                    videoModel.setPlaybackRate(newRate);
                }
            }
        }
    }
 
    /**
     * Calculates the drift between the current latency and the target latency
     * @return {number}
     * @private
     */
    function _getLatencyDrift() {
        const currentLiveLatency = playbackController.getCurrentLiveLatency();
        const targetLiveDelay = playbackController.getLiveDelay();
 
        return currentLiveLatency - targetLiveDelay;
    }
 
    /**
     * Checks whether the catchup mechanism should be enabled. We use different subfunctions here depending on the catchup mode.
     * @return {boolean}
     */
    function _shouldStartCatchUp() {
        try {
            Iif (!playbackController.getTime() > 0 || isCatchupSeekInProgress) {
                return false;
            }
 
            const catchupMode = _getCatchupMode();
 
            if (catchupMode === Constants.LIVE_CATCHUP_MODE_LOLP) {
                const currentBuffer = playbackController.getBufferLevel();
                const playbackBufferMin = settings.get().streaming.liveCatchup.playbackBufferMin;
 
                return _lolpNeedToCatchUpCustom(currentBuffer, playbackBufferMin);
            } else {
                return _defaultNeedToCatchUp();
            }
 
        } catch (e) {
            return false;
        }
    }
 
 
    /**
     * Returns the mode for live playback catchup.
     * @return {String}
     * @private
     */
    function _getCatchupMode() {
        const playbackBufferMin = settings.get().streaming.liveCatchup.playbackBufferMin;
 
        return settings.get().streaming.liveCatchup.mode === Constants.LIVE_CATCHUP_MODE_LOLP && playbackBufferMin !== null && !isNaN(playbackBufferMin) ? Constants.LIVE_CATCHUP_MODE_LOLP : Constants.LIVE_CATCHUP_MODE_DEFAULT;
    }
 
    /**
     * Default algorithm to determine if catchup mode should be enabled
     * @return {boolean}
     * @private
     */
    function _defaultNeedToCatchUp() {
        try {
            const latencyDrift = Math.abs(_getLatencyDrift());
 
            return latencyDrift > 0;
        } catch (e) {
            return false;
        }
    }
 
    /**
     * LoL+ logic to determine if catchup mode should be enabled
     * @param {number} currentBuffer
     * @param {number} playbackBufferMin
     * @return {boolean}
     * @private
     */
    function _lolpNeedToCatchUpCustom(currentBuffer, playbackBufferMin) {
        try {
            const latencyDrift = Math.abs(_getLatencyDrift());
 
            return latencyDrift > 0 || currentBuffer < playbackBufferMin;
        } catch (e) {
            return false;
        }
    }
 
    /**
     * Default algorithm to calculate the new playback rate
     * @param {object} liveCatchUpPlaybackRates
     * @param {number} liveCatchUpPlaybackRates.min - minimum playback rate decrease limit
     * @param {number} liveCatchUpPlaybackRates.max - maximum playback rate increase limit
     * @param {number} currentLiveLatency
     * @param {number} liveDelay
     * @param {number} bufferLevel
     * @param {number} currentPlaybackRate
     * @return {number}
     * @private
     */
    function _calculateNewPlaybackRateDefault(liveCatchUpPlaybackRates, currentLiveLatency, liveDelay, bufferLevel) {
        // if we recently ran into an empty buffer we wait for the buffer to recover before applying a new rate
        if (playbackStalled) {
            return 1.0;
        }
 
        const deltaLatency = currentLiveLatency - liveDelay;
        const cpr = (deltaLatency < 0) ? Math.abs(liveCatchUpPlaybackRates.min) : liveCatchUpPlaybackRates.max;
        const d = deltaLatency * 5;
 
        // Playback rate must be between (1 - cpr) - (1 + cpr)
        // ex: if cpr is 0.5, it can have values between 0.5 - 1.5
        const s = (cpr * 2) / (1 + Math.pow(Math.E, -d));
        let newRate = (1 - cpr) + s;
        // take into account situations in which there are buffer stalls,
        // in which increasing playbackRate to reach target latency will
        // just cause more and more stall situations
        if (playbackController.getPlaybackStalled()) {
            if (bufferLevel <= liveDelay / 2 && deltaLatency > 0) {
                newRate = 1.0;
            }
        }
 
        return newRate;
    }
 
    /**
     * Lol+ algorithm to calculate the new playback rate
     * @param {object} liveCatchUpPlaybackRates
     * @param {number} liveCatchUpPlaybackRates.min - minimum playback rate decrease limit
     * @param {number} liveCatchUpPlaybackRates.max - maximum playback rate increase limit
     * @param {number} currentLiveLatency
     * @param {number} liveDelay
     * @param {number} playbackBufferMin
     * @param {number} bufferLevel
     * @param {number} currentPlaybackRate
     * @return {number}
     * @private
     */
    function _calculateNewPlaybackRateLolP(liveCatchUpPlaybackRates, currentLiveLatency, liveDelay, playbackBufferMin, bufferLevel) {
        let newRate;
 
        // Hybrid: Buffer-based
        if (bufferLevel < playbackBufferMin) {
            // Buffer in danger, slow down
            const cpr = Math.abs(liveCatchUpPlaybackRates.min); // Absolute value as negative delta value will be used.
            const deltaBuffer = bufferLevel - playbackBufferMin; // -ve value
            const d = deltaBuffer * 5;
 
            // Playback rate must be between (1 - cpr) - (1 + cpr)
            // ex: if cpr is 0.5, it can have values between 0.5 - 1.5
            const s = (cpr * 2) / (1 + Math.pow(Math.E, -d));
            newRate = (1 - cpr) + s;
 
            logger.debug('[LoL+ playback control_buffer-based] bufferLevel: ' + bufferLevel + ', newRate: ' + newRate);
        } else {
            // Hybrid: Latency-based
            // Check if latency is within range of target latency
            const minDifference = 0.02;
            Iif (Math.abs(currentLiveLatency - liveDelay) <= (minDifference * liveDelay)) {
                newRate = 1;
            } else {
                const deltaLatency = currentLiveLatency - liveDelay;
                // Buffer is safe, vary playback rate based on latency
                const cpr = (deltaLatency < 0) ? Math.abs(liveCatchUpPlaybackRates.min) : liveCatchUpPlaybackRates.max;
                const d = deltaLatency * 5;
 
                // Playback rate must be between (1 - cpr) - (1 + cpr)
                // ex: if cpr is 0.5, it can have values between 0.5 - 1.5
                const s = (cpr * 2) / (1 + Math.pow(Math.E, -d));
                newRate = (1 - cpr) + s;
            }
 
            logger.debug('[LoL+ playback control_latency-based] latency: ' + currentLiveLatency + ', newRate: ' + newRate);
        }
 
        return newRate
    }
 
    function _checkPlaybackRates() {
        mediaPlayerModel.getCatchupPlaybackRates(true);
    }
 
    instance = {
        reset,
        setConfig,
        initialize
    };
 
    setup();
 
    return instance;
}
 
CatchupController.__dashjs_factory_name = 'CatchupController';
export default FactoryMaker.getSingletonFactory(CatchupController);