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 | 1x 1x 1x 46x 46x 62x 46x 46x 69x 69x 69x 69x 69x 69x 69x 69x 69x 1x 68x 67x 68x 66x 68x 66x 68x 66x 62x 62x 62x 62x 62x 69x 69x 69x 69x 69x 1x 2x 2x 1x 1x 1x 3x 2x 1x 1x 1x 91x 90x 1x 1x 91x 15x 76x 76x 76x 76x 76x 76x 1x 1x 1x 1x 69x 69x 1x 1x 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 Events from '../../core/events/Events.js';
import EventBus from '../../core/EventBus.js';
import Constants from '../constants/Constants.js';
const GAP_HANDLER_INTERVAL = 100;
const THRESHOLD_TO_STALLS = 10;
const GAP_JUMP_WAITING_TIME_OFFSET = 0.1;
function GapController() {
const context = this.context;
const eventBus = EventBus(context).getInstance();
let instance,
lastPlaybackTime,
settings,
wallclockTicked,
gapHandlerInterval,
lastGapJumpPosition,
playbackController,
streamController,
videoModel,
jumpTimeoutHandler,
trackSwitchByMediaType,
logger;
function initialize() {
_registerEvents();
}
function setup() {
logger = Debug(context).getInstance().getLogger(instance);
reset();
}
function reset() {
_stopGapHandler();
_unregisterEvents();
resetInitialSettings();
}
function resetInitialSettings() {
gapHandlerInterval = null;
lastGapJumpPosition = NaN;
wallclockTicked = 0;
jumpTimeoutHandler = null;
trackSwitchByMediaType = {};
}
function setConfig(config) {
if (!config) {
return;
}
if (config.settings) {
settings = config.settings;
}
if (config.playbackController) {
playbackController = config.playbackController;
}
if (config.streamController) {
streamController = config.streamController;
}
if (config.videoModel) {
videoModel = config.videoModel;
}
}
function _registerEvents() {
eventBus.on(Events.WALLCLOCK_TIME_UPDATED, _onWallclockTimeUpdated, this);
eventBus.on(Events.INITIAL_STREAM_SWITCH, _onInitialStreamSwitch, this);
eventBus.on(Events.PLAYBACK_SEEKING, _onPlaybackSeeking, this);
eventBus.on(Events.BUFFER_REPLACEMENT_STARTED, _onBufferReplacementStarted, instance);
eventBus.on(Events.TRACK_CHANGE_RENDERED, _onBufferReplacementEnded, instance);
}
function _unregisterEvents() {
eventBus.off(Events.WALLCLOCK_TIME_UPDATED, _onWallclockTimeUpdated, this);
eventBus.off(Events.INITIAL_STREAM_SWITCH, _onInitialStreamSwitch, this);
eventBus.off(Events.PLAYBACK_SEEKING, _onPlaybackSeeking, this);
eventBus.off(Events.BUFFER_REPLACEMENT_STARTED, _onBufferReplacementStarted, instance);
eventBus.off(Events.TRACK_CHANGE_RENDERED, _onBufferReplacementEnded, instance);
}
/**
* Clear scheduled gap jump when seeking
* @private
*/
function _onPlaybackSeeking() {
Iif (jumpTimeoutHandler) {
clearTimeout(jumpTimeoutHandler);
jumpTimeoutHandler = null;
}
}
/**
* If the track was changed in the current active period and the player might aggressively replace segments the buffer will be empty for a short period of time. Avoid gap jumping at that time.
* We wait until the next media fragment of the target type has been appended before activating again
* @param {object} e
* @private
*/
function _onBufferReplacementStarted(e) {
try {
if (e.streamId !== streamController.getActiveStreamInfo().id || (e.mediaType !== Constants.VIDEO && e.mediaType !== Constants.AUDIO)) {
return;
}
if (e.streamId === streamController.getActiveStreamInfo().id) {
trackSwitchByMediaType[e.mediaType] = true;
}
} catch (e) {
logger.error(e);
}
}
/**
* Activate gap jumping again once segment of target type has been appended
* @param {object} e
* @private
*/
function _onBufferReplacementEnded(e) {
if (!e || !e.mediaType) {
return;
}
trackSwitchByMediaType[e.mediaType] = false;
}
/**
* Activate the gap handler after the first stream switch
* @private
*/
function _onInitialStreamSwitch() {
if (!gapHandlerInterval) {
_startGapHandler();
}
}
/**
* Callback handler for when the wallclock time has been updated
* @private
*/
function _onWallclockTimeUpdated(/*e*/) {
if (!_shouldCheckForGaps(settings.get().streaming.gaps.enableSeekFix)) {
return;
}
wallclockTicked++;
Iif (wallclockTicked >= THRESHOLD_TO_STALLS) {
const currentTime = playbackController.getTime();
if (lastPlaybackTime === currentTime) {
_jumpGap(currentTime, true);
} else {
lastPlaybackTime = currentTime;
lastGapJumpPosition = NaN;
}
wallclockTicked = 0;
}
}
/**
* Returns if we are supposed to check for gaps
* @param {boolean} checkSeekingState - Usually we are not checking for gaps in the videolement is in seeking state. If this flag is set to true we check for a potential exceptions of this rule.
* @return {boolean}
* @private
*/
function _shouldCheckForGaps(checkSeekingState = false) {
if (!streamController.getActiveStream()) {
return false;
}
const trackSwitchInProgress = Object.keys(trackSwitchByMediaType).some((key) => {
return trackSwitchByMediaType[key];
});
const shouldIgnoreSeekingState = checkSeekingState ? _shouldIgnoreSeekingState() : false;
return !trackSwitchInProgress && settings.get().streaming.gaps.jumpGaps && streamController.getActiveStreamProcessors().length > 0 && (!playbackController.isSeeking() || shouldIgnoreSeekingState) && !playbackController.isPaused() && !streamController.getIsStreamSwitchInProgress() &&
!streamController.getHasMediaOrInitialisationError();
}
/**
* There are cases in which we never transition out of the seeking state and still need to jump a gap. For instance if the user seeks right before a gap and video element will not transition out of the seeking state.
* For now limit this to period boundaries. In this case the current period is completely buffered and we are right before the end of the period.
* @private
*/
function _shouldIgnoreSeekingState() {
const activeStream = streamController.getActiveStream();
const streamEnd = parseFloat((activeStream.getStartTime() + activeStream.getDuration()).toFixed(5))
return playbackController.getTime() + settings.get().streaming.gaps.threshold >= streamEnd;
}
/**
* Returns the index of the range object that comes after the current time
* @param {object} ranges
* @param {number} currentTime
* @private
* @return {null|number}
*/
function _getNextRangeIndex(ranges, currentTime) {
try {
if (!ranges || (ranges.length <= 1 && currentTime > 0)) {
return NaN;
}
let nextRangeIndex = NaN;
let j = 0;
while (isNaN(nextRangeIndex) && j < ranges.length) {
const rangeEnd = j > 0 ? ranges.end(j - 1) : 0;
if (currentTime < ranges.start(j) && rangeEnd - currentTime < settings.get().streaming.gaps.threshold) {
nextRangeIndex = j;
}
j += 1;
}
return nextRangeIndex;
} catch (e) {
return null;
}
}
/**
* Check if the currentTime exist within the buffered range
* @param {object} ranges
* @param {number} currentTime
* @private
* @return {boolean}
*/
function _isTimeBuffered(ranges, currentTime) {
for (let i = 0, len = ranges.length; i < len; i++) {
if (currentTime >= ranges.start(i) && currentTime <= ranges.end(i)) {
return true;
}
}
return false;
}
/**
* Starts the interval that checks for gaps
* @private
*/
function _startGapHandler() {
try {
if (!gapHandlerInterval) {
logger.debug('Starting the gap controller');
gapHandlerInterval = setInterval(() => {
if (!_shouldCheckForGaps()) {
return;
}
const currentTime = playbackController.getTime();
_jumpGap(currentTime);
}, GAP_HANDLER_INTERVAL);
}
} catch (e) {
}
}
/**
* Clears the gap interval handler
* @private
*/
function _stopGapHandler() {
logger.debug('Stopping the gap controller');
if (gapHandlerInterval) {
clearInterval(gapHandlerInterval);
gapHandlerInterval = null;
}
}
/**
* Jump a gap
* @param {number} currentTime
* @param {boolean} playbackStalled
* @private
*/
function _jumpGap(currentTime, playbackStalled = false) {
const enableStallFix = settings.get().streaming.gaps.enableStallFix;
const stallSeek = settings.get().streaming.gaps.stallSeek;
const smallGapLimit = settings.get().streaming.gaps.smallGapLimit;
const jumpLargeGaps = settings.get().streaming.gaps.jumpLargeGaps;
const ranges = videoModel.getBufferRange();
let nextRangeIndex;
let seekToPosition = NaN;
let jumpToStreamEnd = false;
// Get the range just after current time position
nextRangeIndex = _getNextRangeIndex(ranges, currentTime);
if (!isNaN(nextRangeIndex)) {
const start = ranges.start(nextRangeIndex);
const gap = start - currentTime;
if (gap > 0 && (gap <= smallGapLimit || jumpLargeGaps)) {
seekToPosition = start;
}
}
// Playback has stalled before period end. We seek to the end of the period
const timeToStreamEnd = playbackController.getTimeToStreamEnd();
if (isNaN(seekToPosition) && playbackStalled && isFinite(timeToStreamEnd) && !isNaN(timeToStreamEnd) && timeToStreamEnd < smallGapLimit) {
seekToPosition = parseFloat(playbackController.getStreamEndTime().toFixed(5));
jumpToStreamEnd = true;
}
if (enableStallFix && isNaN(seekToPosition) && playbackStalled && isNaN(nextRangeIndex) && _isTimeBuffered(ranges, currentTime)) {
if (stallSeek === 0) {
logger.warn(`Toggle play pause to break stall`);
videoModel.pause();
videoModel.play();
} else {
logger.warn(`Jumping ${stallSeek}s to break stall`);
seekToPosition = currentTime + stallSeek;
}
}
if (seekToPosition > 0 && lastGapJumpPosition !== seekToPosition && seekToPosition > currentTime && !jumpTimeoutHandler) {
const timeUntilGapEnd = seekToPosition - currentTime;
if (jumpToStreamEnd) {
const nextStream = streamController.getStreamForTime(seekToPosition);
const internalSeek = nextStream && !!nextStream.getPreloaded();
logger.warn(`Jumping to end of stream because of gap from ${currentTime} to ${seekToPosition}. Gap duration: ${timeUntilGapEnd}`);
playbackController.seek(seekToPosition, true, internalSeek);
} else {
const isDynamic = playbackController.getIsDynamic();
const start = nextRangeIndex > 0 ? ranges.end(nextRangeIndex - 1) : currentTime;
const timeToWait = !isDynamic ? 0 : Math.max(0, timeUntilGapEnd - GAP_JUMP_WAITING_TIME_OFFSET) * 1000;
jumpTimeoutHandler = window.setTimeout(() => {
playbackController.seek(seekToPosition, true, true);
const activeStream = streamController.getActiveStream();
if (activeStream) {
logger.warn(`Jumping gap occuring in period ${activeStream.getStreamId()} starting at ${start} and ending at ${seekToPosition}. Jumping by: ${seekToPosition - start}`);
}
jumpTimeoutHandler = null;
}, timeToWait);
}
lastGapJumpPosition = seekToPosition;
}
}
instance = {
reset,
setConfig,
initialize
};
setup();
return instance;
}
GapController.__dashjs_factory_name = 'GapController';
export default FactoryMaker.getSingletonFactory(GapController);
|