2 * Copyright (c) 2013 The Chromium Authors. All rights reserved.
3 * Use of this source code is governed by a BSD-style license that can be
4 * found in the LICENSE file.
9 function loadAudioAndAddToPeerConnection(url, peerconnection) {
11 gContext = new AudioContext();
13 var inputSink = gContext.createMediaStreamDestination();
14 peerconnection.addStream(inputSink.stream);
16 loadAudioBuffer_(url, function(voiceSoundBuffer) {
17 if (peerconnection.webAudioBufferSource)
18 throw failTest('Cannot load more than one sound per peerconnection.');
20 peerconnection.inputSink = inputSink;
21 peerconnection.webAudioBufferSource = gContext.createBufferSource();
22 peerconnection.webAudioBufferSource.buffer = voiceSoundBuffer;
23 peerconnection.webAudioBufferSource.connect(inputSink);
24 returnToTest('ok-added');
28 function mixLocalStreamIntoPeerConnection(peerconnection, localStream) {
29 if (!peerconnection.inputSink)
30 throw failTest('Must call loadAudioAndAddToPeerConnection before this.');
31 micInputNode = gContext.createMediaStreamSource(localStream);
32 micInputNode.connect(peerconnection.inputSink);
33 returnToTest('ok-mixed-in');
36 function playPreviouslyLoadedAudioFile(peerconnection) {
37 if (!peerconnection.webAudioBufferSource)
38 throw failTest('Must call loadAudioAndAddToPeerConnection before this.');
39 peerconnection.webAudioBufferSource.start(gContext.currentTime);
43 function loadAudioBuffer_(url, callback) {
44 var request = new XMLHttpRequest();
45 request.open('GET', url, true);
46 request.responseType = 'arraybuffer';
48 request.onload = function() {
49 gContext.decodeAudioData(request.response, function (decodedAudio) {
50 voiceSoundBuffer = decodedAudio;
51 callback(voiceSoundBuffer);