summaryrefslogtreecommitdiffstats
path: root/media/test/data/eme_player_js/media_source_utils.js
blob: 8888582c72a61b7949f4ca2cc13cca124e9c06e5 (plain)
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
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// MediaSourceUtils provides basic functionality to load content using MSE API.
var MediaSourceUtils = new function() {
}

MediaSourceUtils.loadMediaSourceFromTestConfig = function(testConfig,
                                                          appendCallbackFn) {
  return this.loadMediaSource(testConfig.mediaFile,
                              testConfig.mediaType,
                              appendCallbackFn);
};

MediaSourceUtils.loadMediaSource = function(mediaFiles,
                                            mediaTypes,
                                            appendCallbackFn) {
  if (!mediaFiles || !mediaTypes)
    Utils.failTest('Missing parameters in loadMediaSource().');

  var mediaFiles = Utils.convertToArray(mediaFiles);
  var mediaTypes = Utils.convertToArray(mediaTypes);
  var totalAppended = 0;
  function onSourceOpen(e) {
    Utils.timeLog('onSourceOpen', e);
    // We can load multiple media files using the same media type. However, if
    // more than one media type is used, we expect to have a media type entry
    // for each corresponding media file.
    var srcBuffer = null;
    for (var i = 0; i < mediaFiles.length; i++) {
      if (i == 0 || mediaFiles.length == mediaTypes.length) {
        Utils.timeLog('Creating a source buffer for type ' + mediaTypes[i]);
        try {
          srcBuffer = mediaSource.addSourceBuffer(mediaTypes[i]);
        } catch (e) {
          Utils.failTest('Exception adding source buffer: ' + e.message);
          return;
        }
      }
      doAppend(mediaFiles[i], srcBuffer);
    }
  }

  function doAppend(mediaFile, srcBuffer) {
    var xhr = new XMLHttpRequest();
    xhr.open('GET', mediaFile);
    xhr.responseType = 'arraybuffer';
    xhr.addEventListener('load', function(e) {
      var onUpdateEnd = function(e) {
        Utils.timeLog('End of appending buffer from ' + mediaFile);
        srcBuffer.removeEventListener('updateend', onUpdateEnd);
        totalAppended++;
        if (totalAppended == mediaFiles.length) {
          if (appendCallbackFn)
            appendCallbackFn(mediaSource);
          else
            mediaSource.endOfStream();
        }
      };
      srcBuffer.addEventListener('updateend', onUpdateEnd);
      srcBuffer.appendBuffer(new Uint8Array(e.target.response));
    });
    xhr.send();
  }

  var mediaSource = new MediaSource();
  mediaSource.addEventListener('sourceopen', onSourceOpen);
  return mediaSource;
};