summaryrefslogtreecommitdiffstats
path: root/content/test/data/media/player.html
blob: e954cf8e938d53707a9df3b71a9db74433f010a9 (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
<html>
<body onload="RunTest();">
<div id="player_container"></div>
</body>

<script type="text/javascript">
// <audio> or <video> player element.
var player;

// Listen for |event| from |element|, set document.title = |event| upon event.
function InstallTitleEventHandler(element, event) {
  element.addEventListener(event, function(e) {
    document.title = event.toUpperCase();
  }, false);
}

function Failed() {
  document.title = 'FAILED';
  return false;
}

function SeekTestStep(e) {
  player.removeEventListener('ended', SeekTestStep, false);

  // Test completes on the next ended event.
  InstallTitleEventHandler(player, 'ended');

  player.currentTime = 0.9 * player.duration;
  player.play();
}

function SeekTestTimeoutSetup() {
  if (player.currentTime < 2)
    return;

  player.removeEventListener('timeupdate', SeekTestTimeoutSetup, false);
  SeekTestStep();
}

// Uses URL query parameters to create an audio or video element using a given
// source.  URL must be of the form "player.html?[tag]=[media_url]".  Plays the
// media and waits for X seconds of playback or the ended event, at which point
// the test seeks near the end of the file and resumes playback.  Test completes
// when the second ended event occurs or an error event occurs at any time.
function RunTest() {
  var url_parts = window.location.href.split('?');
  if (url_parts.length != 2)
    return Failed();

  var query_parts = url_parts[1].split('=');
  if (query_parts.length != 2)
    return Failed();

  var tag = query_parts[0];
  var media_url = query_parts[1];
  if (tag != 'audio' && tag != 'video')
    return Failed();

  // Create player and insert into DOM.
  player = document.createElement(tag);
  player.controls = true;
  document.getElementById('player_container').appendChild(player);

  // Transition to the seek test after X seconds of playback or when the ended
  // event occurs, whichever happens first.
  player.addEventListener('ended', SeekTestStep, false);
  player.addEventListener('timeupdate', SeekTestTimeoutSetup, false);

  // Ensure we percolate up any error events.
  InstallTitleEventHandler(player, 'error');

  // Starts the player.
  player.src = media_url;
  player.play();
}
</script>
</html>