summaryrefslogtreecommitdiffstats
path: root/dom/browser-element/mochitest/file_browserElement_AudioChannelMutedByDefault.html
blob: ea4f3bde1228fcb4c9ccf30e0192949330d79c9d (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
<html>
<body>
<script>
var audio = new Audio("audio.ogg");
var context = new AudioContext();
var node = context.createMediaElementSource(audio);
var sp = context.createScriptProcessor(2048, 1);
node.connect(sp);
var expectedSamplesCount;
var nonzeroSamplesCount = 0;
var isStarted = false;

function ok(aVal, aMsg) {
  alert((!!aVal ? "OK" : "KO") + ", " + aMsg);
}

function finish() {
  audio.onended = null;
  audio.pause();
  alert("DONE");
}

function processSamples(e) {
  var buf = e.inputBuffer.getChannelData(0);
  for (var i = 0; i < buf.length; ++i) {
    if (buf[i] != 0) {
      if (!isStarted) {
        isStarted = true;
        ok(true, "Start process audio sample.");
      }
      nonzeroSamplesCount++;
    }
  }

  if (nonzeroSamplesCount >= expectedSamplesCount) {
    finish();
  }
}

audio.oncanplaythrough = function() {
  var testDuration = audio.duration > 1.0 ? 1.0 : audio.duration * 0.5;
  expectedSamplesCount = Math.floor(testDuration * context.sampleRate);
  sp.onaudioprocess = processSamples;
};

function runCommands()
{
  switch(location.hash) {
    case '#play':
      ok(true, "Audio starts playing.")
      audio.play();
      audio.onended = () => {
        audio.onended = null;
        ok(false, "Audio shouldn't go ended in this test!")
      };
      break;
    default :
      ok(false, "Undefined command!");
  }
}

window.addEventListener('hashchange', runCommands);
</script>
</body>
</html>