All files / dash.js/src/dash/utils SegmentsUtils.js

93.93% Statements 62/66
82.75% Branches 24/29
100% Functions 11/11
93.93% Lines 62/66

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                                                                        53x       53x 11x   42x         11x 11x   11x   11x       11x 11x 11x 11x                   11x 11x 11x 11x   11x       42x 42x   42x   42x   42x                         53x   53x                   53x   53x       53x 53x 53x               53x 53x       53x     53x 3x     50x 5x                 5x 5x   5x     45x       90x 20x     70x                           33x           33x             33x       33x 33x   33x   33x                                 33x 5x     28x                                 20x 20x 20x 20x 20x   20x                                 20x       20x       50x                  
/**
 * 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 {processUriTemplate as cmlProcessUriTemplate} from '@svta/common-media-library/dash/processUriTemplate.js';
import FullSegment from './../vo/FullSegment.js';
import PartialSegment from '../vo/PartialSegment.js';
 
function _getSegment(data) {
    Iif (!data) {
        return null;
    }
 
    if (data.totalNumberOfPartialSegments && !isNaN(data.totalNumberOfPartialSegments) && data.totalNumberOfPartialSegments > 0 && !isNaN(data.subNumberOfPartialSegmentToRequest)) {
        return _getPartialSegment(data);
    } else {
        return _getFullSegment(data);
    }
}
 
function _getPartialSegment(data) {
    const subNumberOfPartialSegmentToRequest = data.subNumberOfPartialSegmentToRequest;
    const partialSegment = _createSinglePartialSegment(data, subNumberOfPartialSegmentToRequest)
 
    _addTimeBasedInformation(partialSegment, data);
 
    return partialSegment;
}
 
function _createSinglePartialSegment(data, subNumberOfPartialSegmentToRequest) {
    const partialSegment = new PartialSegment();
    const partialSegmentDurationInSeconds = data.segmentDurationInSeconds / data.totalNumberOfPartialSegments;
    const { representation, timelineConverter, isDynamic, index, totalNumberOfPartialSegments } = data;
    const inputData = {
        representation,
        segmentDurationInSeconds: partialSegmentDurationInSeconds,
        presentationStartTime: data.presentationStartTime + subNumberOfPartialSegmentToRequest * partialSegmentDurationInSeconds,
        presentationEndTime: data.presentationStartTime + ((subNumberOfPartialSegmentToRequest + 1) * partialSegmentDurationInSeconds),
        timelineConverter,
        isDynamic,
        mediaTimeInSeconds: data.mediaTimeInSeconds + subNumberOfPartialSegmentToRequest * partialSegmentDurationInSeconds,
        index
    }
    const segmentData = _getCommonSegmentData(inputData);
    partialSegment.assignAttributes(segmentData);
    partialSegment.replacementSubNumber = subNumberOfPartialSegmentToRequest;
    partialSegment.totalNumberOfPartialSegments = totalNumberOfPartialSegments;
 
    return partialSegment;
}
 
function _getFullSegment(data) {
    const fullSegment = new FullSegment();
    const segmentData = _getCommonSegmentData(data);
 
    fullSegment.assignAttributes(segmentData);
 
    _addTimeBasedInformation(fullSegment, data);
 
    return fullSegment;
}
 
function _getCommonSegmentData(data) {
    const {
        representation,
        segmentDurationInSeconds,
        presentationStartTime,
        presentationEndTime,
        mediaTimeInSeconds,
        timelineConverter,
        isDynamic,
        index,
    } = data;
 
    const segmentData = {
        availabilityEndTime: timelineConverter.calcAvailabilityEndTimeFromPresentationTime(presentationEndTime + segmentDurationInSeconds, representation, isDynamic),
        availabilityStartTime: timelineConverter.calcAvailabilityStartTimeFromPresentationTime(presentationEndTime, representation, isDynamic),
        duration: segmentDurationInSeconds,
        index: index,
        mediaStartTime: mediaTimeInSeconds,
        presentationStartTime: presentationStartTime,
        replacementNumber: representation.startNumber + index,
        representation: representation,
    }
    segmentData.wallStartTime = timelineConverter.calcWallTimeForSegment(segmentData, isDynamic);
 
    return segmentData
}
 
function _addTimeBasedInformation(segment, data) {
    const { tManifest, mediaTime, mediaUrl, mediaRange } = data;
    segment.replacementTime = tManifest ? tManifest : mediaTime;
    segment.media = processUriTemplate(
        mediaUrl,
        segment.representation.id,
        segment.replacementNumber,
        segment.replacementSubNumber,
        segment.representation.bandwidth,
        segment.replacementTime,
    );
    segment.mediaRange = mediaRange;
    segment.mediaUrl = mediaUrl;
}
 
function _isSegmentAvailable(timelineConverter, representation, segment, isDynamic) {
    const voPeriod = representation.adaptation.period;
 
    // Avoid requesting segments for which the start time overlaps the period boundary
    if (isFinite(voPeriod.duration) && voPeriod.start + voPeriod.duration <= segment.presentationStartTime) {
        return false
    }
 
    if (isDynamic) {
        Iif (representation.availabilityTimeOffset === 'INF') {
            return true;
        }
 
        // For dynamic manifests we check if the presentation start time + duration is included in the availability window
        // SAST = Period@start + seg@presentationStartTime + seg@duration
        // ASAST = SAST - ATO
        // SAET = SAST + TSBD + seg@duration
        // refTime serves as an anchor time to compare the availability time of the segments against.
        const refTime = timelineConverter.getClientReferenceTime();
        const isAvailable = segment.availabilityStartTime.getTime() <= refTime && (!isFinite(segment.availabilityEndTime) || segment.availabilityEndTime.getTime() >= refTime);
 
        return isAvailable
    }
 
    return true;
}
 
function processUriTemplate(url, representationId, number, subNumber, bandwidth, time) {
    if (!url) {
        return url;
    }
 
    return cmlProcessUriTemplate(url, representationId, number, subNumber, bandwidth, time);
}
 
function getIndexBasedSegment(data) {
    const {
        index,
        isDynamic,
        mediaRange,
        mediaTime,
        mediaUrl,
        representation,
        subNumberOfPartialSegmentToRequest,
        timelineConverter,
        totalNumberOfPartialSegments
    } = data;
    let segmentDurationInSeconds,
        presentationStartTime,
        presentationEndTime;
 
 
    segmentDurationInSeconds = representation.segmentDuration;
 
    /*
     * From spec - If neither @duration attribute nor SegmentTimeline element is present, then the Representation
     * shall contain exactly one Media Segment. The MPD start time is 0 and the MPD duration is obtained
     * in the same way as for the last Media Segment in the Representation.
     */
    Iif (isNaN(segmentDurationInSeconds)) {
        segmentDurationInSeconds = representation.adaptation.period.duration;
    }
 
    presentationStartTime = parseFloat((representation.adaptation.period.start + (index * segmentDurationInSeconds)).toFixed(5));
    presentationEndTime = parseFloat((presentationStartTime + segmentDurationInSeconds).toFixed(5));
 
    const mediaTimeInSeconds = timelineConverter.calcMediaTimeFromPresentationTime(presentationStartTime, representation);
 
    const segment = _getSegment(
        {
            index,
            isDynamic,
            mediaRange,
            mediaTime,
            mediaTimeInSeconds,
            mediaUrl,
            presentationEndTime,
            presentationStartTime,
            representation,
            segmentDurationInSeconds,
            subNumberOfPartialSegmentToRequest,
            timelineConverter,
            totalNumberOfPartialSegments
        });
 
    if (!_isSegmentAvailable(timelineConverter, representation, segment, isDynamic)) {
        return null;
    }
 
    return segment;
}
 
function getTimeBasedSegment(data) {
    const {
        durationInTimescale,
        fTimescale,
        index,
        subNumberOfPartialSegmentToRequest,
        isDynamic,
        mediaRange,
        mediaTime,
        mediaUrl,
        totalNumberOfPartialSegments,
        representation,
        tManifest,
        timelineConverter,
    } = data;
    const mediaTimeInSeconds = mediaTime / fTimescale;
    const segmentDurationInSeconds = durationInTimescale / fTimescale;
    let presentationStartTime = timelineConverter.calcPresentationTimeFromMediaTime(mediaTimeInSeconds, representation);
    let presentationEndTime = presentationStartTime + segmentDurationInSeconds;
 
    let segment = _getSegment({
        representation,
        segmentDurationInSeconds,
        presentationStartTime,
        presentationEndTime,
        mediaTimeInSeconds,
        timelineConverter,
        isDynamic,
        index,
        totalNumberOfPartialSegments,
        subNumberOfPartialSegmentToRequest,
        mediaUrl,
        mediaRange,
        tManifest,
        mediaTime
    });
 
    Iif (!_isSegmentAvailable(timelineConverter, representation, segment, isDynamic)) {
        return null;
    }
 
    return segment;
}
 
function getTotalNumberOfPartialSegments(element) {
    return element.k;
}
 
export {
    getIndexBasedSegment,
    getTimeBasedSegment,
    getTotalNumberOfPartialSegments,
    processUriTemplate,
};