All files / dash.js/src/streaming PreBufferSink.js

90% Statements 45/50
94.44% Branches 17/18
77.77% Functions 14/18
89.36% Lines 42/47

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                                                                                    8x         8x 8x     8x       1x 1x 1x       28x 26x 26x 50x   26x   2x     28x 28x         28x       21x 9x                 1x   1x 4x 4x 2x   2x         1x   2x     2x       1x   2x       1x                                         9x 9x 1x 1x     9x   9x       21x             8x                       8x   8x     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 Debug from '../core/Debug.js';
import FactoryMaker from '../core/FactoryMaker.js';
 
/**
 * This is a sink that is used to temporarily hold onto media chunks before a video element is added.
 * The discharge() function is used to get the chunks out of the PreBuffer for adding to a real SourceBuffer.
 *
 * @class PreBufferSink
 * @ignore
 * @implements FragmentSink
 */
function PreBufferSink(onAppendedCallback) {
    const context = this.context;
 
    let instance,
        logger,
        outstandingInit;
    let chunks = [];
    let onAppended = onAppendedCallback;
 
    function setup() {
        logger = Debug(context).getInstance().getLogger(instance);
    }
 
    function reset() {
        chunks = [];
        outstandingInit = null;
        onAppended = null;
    }
 
    function append(chunk) {
        if (chunk.segmentType !== 'InitializationSegment') { //Init segments are stored in the initCache.
            chunks.push(chunk);
            chunks.sort(function (a, b) {
                return a.start - b.start;
            });
            outstandingInit = null;
        } else {//We need to hold an init chunk for when a corresponding media segment is being downloaded when the discharge happens.
            outstandingInit = chunk;
        }
 
        logger.debug('PreBufferSink appended chunk s: ' + chunk.start + '; e: ' + chunk.end);
        Iif (onAppended) {
            onAppended({
                chunk: chunk
            });
        }
        return Promise.resolve();
    }
 
    function remove(start, end) {
        chunks = chunks.filter(a => !((isNaN(end) || a.start < end) && (isNaN(start) || a.end > start))); //The opposite of the getChunks predicate.
        return Promise.resolve();
    }
 
    //Nothing async, nothing to abort.
    function abort() {
        return Promise.resolve();
    }
 
    function getAllBufferRanges() {
        let ranges = [];
 
        for (let i = 0; i < chunks.length; i++) {
            let chunk = chunks[i];
            if (ranges.length === 0 || chunk.start > ranges[ranges.length - 1].end) {
                ranges.push({start: chunk.start, end: chunk.end});
            } else {
                ranges[ranges.length - 1].end = chunk.end;
            }
        }
 
        //Implements TimeRanges interface. So acts just like sourceBuffer.buffered.
        const timeranges = {
            start: function (n) {
                return ranges[n].start;
            },
            end: function (n) {
                return ranges[n].end;
            }
        };
 
        Object.defineProperty(timeranges, 'length', {
            get: function () {
                return ranges.length;
            }
        });
 
        return timeranges;
    }
 
    function updateTimestampOffset() {
        return Promise.resolve();
    }
 
    function getBuffer() {
        return this;
    }
 
    /**
     * Return the all chunks in the buffer the lie between times start and end.
     * Because a chunk cannot be split, this returns the full chunk if any part of its time lies in the requested range.
     * Chunks are removed from the buffer when they are discharged.
     * @function PreBufferSink#discharge
     * @param {?Number} start The start time from which to discharge from the buffer. If NaN, it is regarded as unbounded.
     * @param {?Number} end The end time from which to discharge from the buffer. If NaN, it is regarded as unbounded.
     * @returns {Array} The set of chunks from the buffer within the time ranges.
     */
    function discharge(start, end) {
        const result = getChunksAt(start, end);
        if (outstandingInit) {
            result.push(outstandingInit);
            outstandingInit = null;
        }
 
        remove(start, end);
 
        return result;
    }
 
    function getChunksAt(start, end) {
        return chunks.filter(a => ((isNaN(end) || a.start < end) && (isNaN(start) || a.end > start)));
    }
 
    function waitForUpdateEnd(callback) {
        callback();
    }
 
    instance = {
        getAllBufferRanges,
        append,
        remove,
        abort,
        discharge,
        reset,
        updateTimestampOffset,
        waitForUpdateEnd,
        getBuffer
    };
 
    setup();
 
    return instance;
}
 
PreBufferSink.__dashjs_factory_name = 'PreBufferSink';
const factory = FactoryMaker.getClassFactory(PreBufferSink);
export default factory;