# All files / src/demux demuxer.js

85.54% Statements 71/83
74.42% Branches 32/43
57.14% Functions 4/7
86.59% Lines 71/82

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

    1x
    1x

    12x
    12x
    12x

    12x
    12x

    12x

    12x
    12x
    12x
    12x
    12x
    12x
    12x
    12x

    12x

    12x
    12x
    9x

    9x
    9x
    9x
    9x
    9x

    9x

    3x

    2x
    2x
    1x
    1x
    1x

    1x
    1x
    1x
    1x

    2x
    2x
    2x
    2x

    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    1x

    2x
    1x

    2x
    2x

    1x

    1x
    1x
    1x

    6x
    6x
    6x

    4x
    4x

    1x
    1x
    1x

    2x
    2x
    2x
    2x
    2x

     import { EventEmitter } from 'eventemitter3';
    import * as work from 'webworkify-webpack';

    import Event from '../events';
    import DemuxerInline from '../demux/demuxer-inline';
    import { logger } from '../utils/logger';
    import { ErrorTypes, ErrorDetails } from '../errors';
    import { getMediaSource } from '../utils/mediasource-helper';
    import { getSelfScope } from '../utils/get-self-scope';

    import { Observer } from '../observer';

    // see https://stackoverflow.com/a/11237259/589493
    const global = getSelfScope(); // safeguard for code that might run both on worker and main thread
    const MediaSource = getMediaSource() || { isTypeSupported: () => false };

    class Demuxer {
      constructor (hls, id) {
        this.hls = hls;
        this.id = id;

        const observer = this.observer = new Observer();
        const config = hls.config;

        const forwardMessage = (ev, data) => {
          data = data || {};
          data.frag = this.frag;
          data.id = this.id;
          hls.trigger(ev, data);
        };

        // forward events to main thread
        observer.on(Event.FRAG_DECRYPTED, forwardMessage);
        observer.on(Event.FRAG_PARSING_INIT_SEGMENT, forwardMessage);
        observer.on(Event.FRAG_PARSING_DATA, forwardMessage);
        observer.on(Event.FRAG_PARSED, forwardMessage);
        observer.on(Event.ERROR, forwardMessage);
        observer.on(Event.FRAG_PARSING_METADATA, forwardMessage);
        observer.on(Event.FRAG_PARSING_USERDATA, forwardMessage);
        observer.on(Event.INIT_PTS_FOUND, forwardMessage);

        const typeSupported = {
          mp4: MediaSource.isTypeSupported('video/mp4'),
          mpeg: MediaSource.isTypeSupported('audio/mpeg'),
          mp3: MediaSource.isTypeSupported('audio/mp4; codecs="mp3"')
        };
        // navigator.vendor is not always available in Web Worker
        // refer to https://developer.mozilla.org/en-US/docs/Web/API/WorkerGlobalScope/navigator
        const vendor = navigator.vendor;
        if (config.enableWorker && (typeof (Worker) !== 'undefined')) {
          logger.log('demuxing in webworker');
          let w;
          try {
            w = this.w = work(require.resolve('../demux/demuxer-worker.js'));
            this.onwmsg = this.onWorkerMessage.bind(this);
            w.addEventListener('message', this.onwmsg);
            w.onerror = function (event) {
              hls.trigger(Event.ERROR, { type: ErrorTypes.OTHER_ERROR, details: ErrorDetails.INTERNAL_EXCEPTION, fatal: true, event: 'demuxerWorker', err: { message: event.message + ' (' + event.filename + ':' + event.lineno + ')' } });
            };
            w.postMessage({ cmd: 'init', typeSupported: typeSupported, vendor: vendor, id: id, config: JSON.stringify(config) });
          } catch (err) {
            logger.warn('Error in worker:', err);
            logger.error('Error while initializing DemuxerWorker, fallback on DemuxerInline');
            if (w) {
              // revoke the Object URL that was used to create demuxer worker, so as not to leak it
              global.URL.revokeObjectURL(w.objectURL);
            }
            this.demuxer = new DemuxerInline(observer, typeSupported, config, vendor);
            this.w = undefined;
          }
        } else {
          this.demuxer = new DemuxerInline(observer, typeSupported, config, vendor);
        }
      }

      destroy () {
        let w = this.w;
        if (w) {
          w.removeEventListener('message', this.onwmsg);
          w.terminate();
          this.w = null;
        } else {
          let demuxer = this.demuxer;
          Eif (demuxer) {
            demuxer.destroy();
            this.demuxer = null;
          }
        }
        const observer = this.observer;
        Eif (observer) {
          observer.removeAllListeners();
          this.observer = null;
        }
      }

      push (data, initSegment, audioCodec, videoCodec, frag, duration, accurateTimeOffset, defaultInitPTS) {
        const w = this.w;
        const timeOffset = Number.isFinite(frag.startPTS) ? frag.startPTS : frag.start;
        const decryptdata = frag.decryptdata;
        const lastFrag = this.frag;
        const discontinuity = !(lastFrag && (frag.cc === lastFrag.cc));
        const trackSwitch = !(lastFrag && (frag.level === lastFrag.level));
        const nextSN = lastFrag && (frag.sn === (lastFrag.sn + 1));
        const contiguous = !trackSwitch && nextSN;
        if (discontinuity) {
          logger.log(`${this.id}:discontinuity detected`);
        }

        if (trackSwitch) {
          logger.log(`${this.id}:switch detected`);
        }

        this.frag = frag;
        if (w) {
          // post fragment payload as transferable objects for ArrayBuffer (no copy)
          w.postMessage({ cmd: 'demux', data, decryptdata, initSegment, audioCodec, videoCodec, timeOffset, discontinuity, trackSwitch, contiguous, duration, accurateTimeOffset, defaultInitPTS }, data instanceof ArrayBuffer ? [data] : []);
        } else {
          let demuxer = this.demuxer;
          Eif (demuxer) {
            demuxer.push(data, decryptdata, initSegment, audioCodec, videoCodec, timeOffset, discontinuity, trackSwitch, contiguous, duration, accurateTimeOffset, defaultInitPTS);
          }
        }
      }

      onWorkerMessage (ev) {
        let data = ev.data,
          hls = this.hls;
        switch (data.event) {
        case 'init':
          // revoke the Object URL that was used to create demuxer worker, so as not to leak it
          global.URL.revokeObjectURL(this.w.objectURL);
          break;
          // special case for FRAG_PARSING_DATA: data1 and data2 are transferable objects
        case Event.FRAG_PARSING_DATA:
          data.data.data1 = new Uint8Array(data.data1);
          Eif (data.data2) {
            data.data.data2 = new Uint8Array(data.data2);
          }

          /* falls through */
        default:
          data.data = data.data || {};
          data.data.frag = this.frag;
          data.data.id = this.id;
          hls.trigger(data.event, data.data);
          break;
        }
      }
    }

    export default Demuxer;