summaryrefslogtreecommitdiffstats
path: root/dom/media/mediasource/test/mediasource.js
blob: 71d8d4ef9f3dbd02dd3d9033f747baea601e9f32 (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
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
// Helpers for Media Source Extensions tests

let gMSETestPrefs = [
  ["media.mediasource.enabled", true],
  ["media.audio-max-decode-error", 0],
  ["media.video-max-decode-error", 0],
];

// Called before runWithMSE() to set the prefs before running MSE tests.
function addMSEPrefs(...prefs) {
  gMSETestPrefs = gMSETestPrefs.concat(prefs);
}

async function runWithMSE(testFunction) {
  await once(window, "load");
  await SpecialPowers.pushPrefEnv({ set: gMSETestPrefs });

  const ms = new MediaSource();

  const el = document.createElement("video");
  el.src = URL.createObjectURL(ms);
  el.preload = "auto";

  document.body.appendChild(el);
  SimpleTest.registerCleanupFunction(() => {
    el.remove();
    el.removeAttribute("src");
    el.load();
  });
  try {
    await testFunction(ms, el);
  } catch (e) {
    ok(false, `${testFunction.name} failed with error ${e.name}`);
    throw e;
  }
}

async function fetchWithXHR(uri) {
  return new Promise(resolve => {
    const xhr = new XMLHttpRequest();
    xhr.open("GET", uri, true);
    xhr.responseType = "arraybuffer";
    xhr.addEventListener("load", function () {
      is(
        xhr.status,
        200,
        "fetchWithXHR load uri='" + uri + "' status=" + xhr.status
      );
      resolve(xhr.response);
    });
    xhr.send();
  });
}

function range(start, end) {
  const rv = [];
  for (let i = start; i < end; ++i) {
    rv.push(i);
  }
  return rv;
}

function must_throw(f, msg, error = true) {
  try {
    f();
    ok(!error, msg);
  } catch (e) {
    ok(error, msg);
    if (error === true) {
      ok(
        false,
        `Please provide name of expected error! Got ${e.name}: ${e.message}.`
      );
    } else if (e.name != error) {
      throw e;
    }
  }
}

async function must_reject(f, msg, error = true) {
  try {
    await f();
    ok(!error, msg);
  } catch (e) {
    ok(error, msg);
    if (error === true) {
      ok(
        false,
        `Please provide name of expected error! Got ${e.name}: ${e.message}.`
      );
    } else if (e.name != error) {
      throw e;
    }
  }
}

const wait = ms => new Promise(resolve => setTimeout(resolve, ms));

const must_not_throw = (f, msg) => must_throw(f, msg, false);
const must_not_reject = (f, msg) => must_reject(f, msg, false);

async function once(target, name) {
  return new Promise(r => target.addEventListener(name, r, { once: true }));
}

function timeRangeToString(r) {
  let str = "TimeRanges: ";
  for (let i = 0; i < r.length; i++) {
    str += "[" + r.start(i) + ", " + r.end(i) + ")";
  }
  return str;
}

async function loadSegment(sb, typedArrayOrArrayBuffer) {
  const typedArray =
    typedArrayOrArrayBuffer instanceof ArrayBuffer
      ? new Uint8Array(typedArrayOrArrayBuffer)
      : typedArrayOrArrayBuffer;
  info(
    `Loading buffer: [${typedArray.byteOffset}, ${
      typedArray.byteOffset + typedArray.byteLength
    })`
  );
  const beforeBuffered = timeRangeToString(sb.buffered);
  const p = once(sb, "update");
  sb.appendBuffer(typedArray);
  await p;
  const afterBuffered = timeRangeToString(sb.buffered);
  info(
    `SourceBuffer buffered ranges grew from ${beforeBuffered} to ${afterBuffered}`
  );
}

async function fetchAndLoad(sb, prefix, chunks, suffix) {
  // Fetch the buffers in parallel.
  const buffers = await Promise.all(
    chunks.map(c => fetchWithXHR(prefix + c + suffix))
  );

  // Load them in series, as required per spec.
  for (const buffer of buffers) {
    await loadSegment(sb, buffer);
  }
}

function loadSegmentAsync(sb, typedArrayOrArrayBuffer) {
  const typedArray =
    typedArrayOrArrayBuffer instanceof ArrayBuffer
      ? new Uint8Array(typedArrayOrArrayBuffer)
      : typedArrayOrArrayBuffer;
  info(
    `Loading buffer2: [${typedArray.byteOffset}, ${
      typedArray.byteOffset + typedArray.byteLength
    })`
  );
  const beforeBuffered = timeRangeToString(sb.buffered);
  return sb.appendBufferAsync(typedArray).then(() => {
    const afterBuffered = timeRangeToString(sb.buffered);
    info(
      `SourceBuffer buffered ranges grew from ${beforeBuffered} to ${afterBuffered}`
    );
  });
}

function fetchAndLoadAsync(sb, prefix, chunks, suffix) {
  // Fetch the buffers in parallel.
  const buffers = {};
  const fetches = [];
  for (const chunk of chunks) {
    fetches.push(
      fetchWithXHR(prefix + chunk + suffix).then(
        ((c, x) => (buffers[c] = x)).bind(null, chunk)
      )
    );
  }

  // Load them in series, as required per spec.
  return Promise.all(fetches).then(function () {
    let rv = Promise.resolve();
    for (const chunk of chunks) {
      rv = rv.then(loadSegmentAsync.bind(null, sb, buffers[chunk]));
    }
    return rv;
  });
}

// Register timeout function to dump debugging logs.
SimpleTest.registerTimeoutFunction(async function () {
  for (const v of document.getElementsByTagName("video")) {
    console.log(await SpecialPowers.wrap(v).mozRequestDebugInfo());
  }
  for (const a of document.getElementsByTagName("audio")) {
    console.log(await SpecialPowers.wrap(a).mozRequestDebugInfo());
  }
});

async function waitUntilTime(target, targetTime) {
  await new Promise(resolve => {
    target.addEventListener("waiting", function onwaiting() {
      info("Got a waiting event at " + target.currentTime);
      if (target.currentTime >= targetTime) {
        target.removeEventListener("waiting", onwaiting);
        resolve();
      }
    });
  });
  ok(true, "Reached target time of: " + targetTime);
}

// Log events for debugging.

function logEvents(el) {
  [
    "suspend",
    "play",
    "canplay",
    "canplaythrough",
    "loadstart",
    "loadedmetadata",
    "loadeddata",
    "playing",
    "ended",
    "error",
    "stalled",
    "emptied",
    "abort",
    "waiting",
    "pause",
    "durationchange",
    "seeking",
    "seeked",
  ].forEach(type =>
    el.addEventListener(type, e => info(`got ${e.type} event`))
  );
}