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 | 1x 1x 1x 1x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 15x 17x 2x 2x 2x 2x 2x 2x 7x 7x 7x 6x 6x 7x 1x 1x 1x 7x 6x 6x 6x 6x 1x 6x 6x 6x 6x 2x 2x 2x 2x 6x 3x 3x 1x 1x 2x 6x 6x 6x 9x 9x 9x 7x 4x 4x 4x 4x 3x 4x 5x 5x 5x 1x 1x 1x 1x 1x 1x 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 FragmentRequest from '../vo/FragmentRequest.js';
const FRAGMENT_MODEL_LOADING = 'loading';
const FRAGMENT_MODEL_EXECUTED = 'executed';
const FRAGMENT_MODEL_CANCELED = 'canceled';
const FRAGMENT_MODEL_FAILED = 'failed';
function FragmentModel(config) {
config = config || {};
const eventBus = config.eventBus;
const events = config.events;
const dashMetrics = config.dashMetrics;
const fragmentLoader = config.fragmentLoader;
const debug = config.debug;
const streamInfo = config.streamInfo;
const type = config.type;
let instance,
logger,
executedRequests,
loadingRequests;
function setup() {
logger = debug.getLogger(instance);
resetInitialSettings();
eventBus.on(events.LOADING_COMPLETED, onLoadingCompleted, instance);
eventBus.on(events.LOADING_DATA_PROGRESS, onLoadingInProgress, instance);
eventBus.on(events.LOADING_ABANDONED, onLoadingAborted, instance);
}
function getStreamId() {
return streamInfo.id;
}
function getType() {
return type;
}
function isFragmentLoaded(request) {
const isEqualComplete = function (req1, req2) {
return ((req1.action === FragmentRequest.ACTION_COMPLETE) && (req1.action === req2.action));
};
const isEqualMedia = function (req1, req2) {
return !isNaN(req1.index) && (req1.startTime === req2.startTime) && (req1.adaptationIndex === req2.adaptationIndex) && (req1.type === req2.type);
};
const isEqualInit = function (req1, req2) {
return req1.representation.id === req2.representation.id;
};
const check = function (requests) {
let isLoaded = false;
requests.some(req => {
if (isEqualMedia(request, req) || isEqualInit(request, req) || isEqualComplete(request, req)) {
isLoaded = true;
return isLoaded;
}
});
return isLoaded;
};
if (!request) {
return false;
}
return check(executedRequests);
}
function isFragmentLoadedOrPending(request) {
let isLoaded = false;
let i = 0;
let req;
// First, check if the fragment has already been loaded
isLoaded = isFragmentLoaded(request);
// Then, check if the fragment is about to be loeaded
if (!isLoaded) {
for (i = 0; i < loadingRequests.length; i++) {
req = loadingRequests[i];
if ((request.url === req.url) && (request.startTime === req.startTime)) {
isLoaded = true;
}
}
}
return isLoaded;
}
/**
*
* Gets an array of {@link FragmentRequest} objects
*
* @param {Object} filter The object with properties by which the method filters the requests to be returned.
* the only mandatory property is state, which must be a value from
* other properties should match the properties of {@link FragmentRequest}. E.g.:
* getRequests({state: FragmentModel.FRAGMENT_MODEL_EXECUTED, quality: 0})
*
* @returns {Array}
* @memberof FragmentModel#
*/
function getRequests(filter) {
const states = filter ? filter.state instanceof Array ? filter.state : [filter.state] : [];
let filteredRequests = [];
states.forEach(state => {
const requests = getRequestsForState(state);
filteredRequests = filteredRequests.concat(filterRequests(requests, filter));
});
return filteredRequests;
}
function getRequestThreshold(req) {
return isNaN(req.duration) ? 0.25 : Math.min(req.duration / 8, 0.5);
}
function removeExecutedRequestsBeforeTime(time) {
executedRequests = executedRequests.filter(req => {
const threshold = getRequestThreshold(req);
return isNaN(req.startTime) || (time !== undefined ? req.startTime >= time - threshold : false);
});
}
function removeExecutedRequestsAfterTime(time) {
executedRequests = executedRequests.filter(req => {
return isNaN(req.startTime) || (time !== undefined ? req.startTime < time : false);
});
}
function removeExecutedRequestsInTimeRange(start, end) {
if (end <= start + 0.5) {
return;
}
executedRequests = executedRequests.filter(req => {
const threshold = getRequestThreshold(req);
return (isNaN(req.startTime) || req.startTime >= (end - threshold)) ||
(isNaN(req.duration) || (req.startTime + req.duration) <= (start + threshold));
});
}
// Remove requests that are not "represented" by any of buffered ranges
function syncExecutedRequestsWithBufferedRange(bufferedRanges, streamDuration) {
if (!bufferedRanges || bufferedRanges.length === 0) {
removeExecutedRequestsBeforeTime();
return;
}
let start = 0;
for (let i = 0, ln = bufferedRanges.length; i < ln; i++) {
removeExecutedRequestsInTimeRange(start, bufferedRanges.start(i));
start = bufferedRanges.end(i);
}
if (streamDuration > 0) {
removeExecutedRequestsInTimeRange(start, streamDuration);
}
}
function abortRequests() {
logger.debug('abort requests');
fragmentLoader.abort();
loadingRequests = [];
}
function executeRequest(request) {
switch (request.action) {
case FragmentRequest.ACTION_DOWNLOAD:
addSchedulingInfoMetrics(request, FRAGMENT_MODEL_LOADING);
loadingRequests.push(request);
loadCurrentFragment(request);
break;
default:
logger.warn('Unknown request action.');
}
}
function loadCurrentFragment(request) {
eventBus.trigger(events.FRAGMENT_LOADING_STARTED,
{ request: request },
{ streamId: streamInfo.id, mediaType: type }
);
fragmentLoader.load(request);
}
function getRequestForTime(arr, time, threshold) {
// loop through the executed requests and pick the one for which the playback interval matches the given time
const lastIdx = arr.length - 1;
for (let i = lastIdx; i >= 0; i--) {
const req = arr[i];
const start = req.startTime;
const end = start + req.duration;
threshold = !isNaN(threshold) ? threshold : getRequestThreshold(req);
if ((!isNaN(start) && !isNaN(end) && ((time + threshold) >= start) && ((time - threshold) < end)) || (isNaN(start) && isNaN(time))) {
return req;
}
}
return null;
}
function filterRequests(arr, filter) {
// for time use a specific filtration function
Iif (filter.hasOwnProperty('time')) {
return [getRequestForTime(arr, filter.time, filter.threshold)];
}
return arr.filter(request => {
for (const prop in filter) {
if (prop === 'state') {
continue;
}
if (filter.hasOwnProperty(prop) && request[prop] != filter[prop]) {
return false;
}
}
return true;
});
}
function getRequestsForState(state) {
let requests;
switch (state) {
case FRAGMENT_MODEL_LOADING:
requests = loadingRequests;
break;
case FRAGMENT_MODEL_EXECUTED:
requests = executedRequests;
break;
default:
requests = [];
}
return requests;
}
function addSchedulingInfoMetrics(request, state) {
dashMetrics.addSchedulingInfo(request, state);
dashMetrics.addRequestsQueue(request.mediaType, loadingRequests, executedRequests);
}
function onLoadingCompleted(e) {
if (e.sender !== fragmentLoader) {
return;
}
loadingRequests.splice(loadingRequests.indexOf(e.request), 1);
if (e.response && !e.error) {
executedRequests.push(e.request);
}
addSchedulingInfoMetrics(e.request, e.error ? FRAGMENT_MODEL_FAILED : FRAGMENT_MODEL_EXECUTED);
eventBus.trigger(events.FRAGMENT_LOADING_COMPLETED,
{
request: e.request,
response: e.response,
error: e.error,
sender: this
},
{ streamId: streamInfo.id, mediaType: type }
);
}
function onLoadingInProgress(e) {
if (e.sender !== fragmentLoader) {
return;
}
eventBus.trigger(events.FRAGMENT_LOADING_PROGRESS,
{
request: e.request,
response: e.response,
error: e.error,
sender: this
},
{ streamId: streamInfo.id, mediaType: type }
);
}
function onLoadingAborted(e) {
if (e.sender !== fragmentLoader) {
return;
}
eventBus.trigger(events.FRAGMENT_LOADING_ABANDONED,
{ request: e.request },
{ streamId: streamInfo.id, mediaType: type }
);
}
function resetInitialSettings() {
executedRequests = [];
loadingRequests = [];
if (fragmentLoader) {
fragmentLoader.resetInitialSettings();
}
}
function reset() {
eventBus.off(events.LOADING_COMPLETED, onLoadingCompleted, this);
eventBus.off(events.LOADING_DATA_PROGRESS, onLoadingInProgress, this);
eventBus.off(events.LOADING_ABANDONED, onLoadingAborted, this);
if (fragmentLoader) {
fragmentLoader.reset();
}
resetInitialSettings();
}
instance = {
abortRequests,
executeRequest,
getRequests,
getStreamId,
getType,
isFragmentLoaded,
isFragmentLoadedOrPending,
removeExecutedRequestsAfterTime,
removeExecutedRequestsBeforeTime,
reset,
resetInitialSettings,
syncExecutedRequestsWithBufferedRange,
};
setup();
return instance;
}
FragmentModel.__dashjs_factory_name = 'FragmentModel';
const factory = FactoryMaker.getClassFactory(FragmentModel);
factory.FRAGMENT_MODEL_LOADING = FRAGMENT_MODEL_LOADING;
factory.FRAGMENT_MODEL_EXECUTED = FRAGMENT_MODEL_EXECUTED;
factory.FRAGMENT_MODEL_CANCELED = FRAGMENT_MODEL_CANCELED;
factory.FRAGMENT_MODEL_FAILED = FRAGMENT_MODEL_FAILED;
FactoryMaker.updateClassFactory(FragmentModel.__dashjs_factory_name, factory);
export default factory;
|