a57eefd330f388c526a5494fe1420099d5c304ab
[WebKit-https.git] / LayoutTests / webrtc / audio-peer-connection-webaudio.html
1 <!DOCTYPE html>
2 <html>
3 <head>
4     <meta charset="utf-8">
5     <title>Testing local audio capture playback causes "playing" event to fire</title>
6     <script src="../resources/testharness.js"></script>
7     <script src="../resources/testharnessreport.js"></script>
8     <script src ="routines.js"></script>
9     <script>
10     var test = async_test(() => {
11         if (window.internals)
12             internals.useMockRTCPeerConnectionFactory("TwoRealPeerConnections");
13
14         if (window.testRunner)
15             testRunner.setUserMediaPermission(true);
16
17         var heardHum = false;
18         var heardBop = false;
19         var heardBip = false;
20
21         navigator.mediaDevices.getUserMedia({audio: true}).then((stream) => {
22             createConnections((firstConnection) => {
23                 firstConnection.addStream(stream);
24             }, (secondConnection) => {
25                 secondConnection.onaddstream = (streamEvent) => { 
26                     var context = new webkitAudioContext();
27                     var sourceNode = context.createMediaStreamSource(streamEvent.stream);
28                     var analyser = context.createAnalyser();
29                     var gain = context.createGain();
30
31                     analyser.fftSize = 2048;
32                     analyser.smoothingTimeConstant = 0;
33                     analyser.minDecibels = -100;
34                     analyser.maxDecibels = 0;
35                     gain.gain.value = 0;
36
37                     sourceNode.connect(analyser);
38                     analyser.connect(gain);
39                     gain.connect(context.destination);
40
41                     function analyse() {
42                         var freqDomain = new Uint8Array(analyser.frequencyBinCount);
43                         analyser.getByteFrequencyData(freqDomain);
44
45                         var hasFrequency = expectedFrequency => {
46                             var bin = Math.floor(expectedFrequency * analyser.fftSize / context.sampleRate);
47                             return bin < freqDomain.length && freqDomain[bin] >= 150;
48                         };
49
50                         if (!heardHum)
51                             heardHum = hasFrequency(150);
52
53                         if (!heardBip)
54                             heardBip = hasFrequency(1500);
55
56                         if (!heardBop)
57                             heardBop = hasFrequency(500);
58
59                         if (heardHum && heardBip && heardBop)
60                             done();
61                     };
62
63                     var done = () => {
64                         clearTimeout(timeout);
65                         clearInterval(interval);
66
67                         assert_true(heardHum);
68                         assert_true(heardBip);
69                         assert_true(heardBop);
70                         test.done();
71                     };
72
73                     var timeout = setTimeout(done, 3000);
74                     var interval = setInterval(analyse, 1000 / 30);
75                     analyse();
76                 }
77             });
78         });
79     }, "Basic audio playback through a peer connection");
80     </script>
81 </head>
82 <body>
83 </body>
84 </html>