webrtc/samples/js/demos/html/pc1-audio.html

111 lines
3.0 KiB
HTML
Raw Normal View History

<!DOCTYPE html>
<html>
<head>
<title>PeerConnection Audio Only Demo 1</title>
<!-- Load the polyfill to switch-hit between Chrome and Firefox -->
<script src="../../base/adapter.js"></script>
<style>
button {
font: 18px sans-serif;
padding: 8px;
}
</style>
</head>
<body>
<h2>Local-Audio</h2>
<audio id="audio1" autoplay="autoplay" controls="controls" muted="true"></audio>
<h2>Remote-Audio</h2>
<audio id="audio2" autoplay="autoplay" controls="controls"></audio>
<br><br>
<button id="btn1" onclick="call()">Call</button>
<button id="btn2" onclick="hangup()">Hang Up</button>
<br><br>
<script>
btn1.disabled = false;
btn2.disabled = true;
var pc1,pc2;
var localstream;
var sdpConstraints = {'mandatory': {
'OfferToReceiveAudio':true,
'OfferToReceiveVideo':false }};
function gotStream(stream){
trace("Received local stream");
// Call the polyfill wrapper to attach the media stream to this element.
localstream = stream;
audioTracks = localstream.getAudioTracks();
if (audioTracks.length > 0)
trace('Using Audio device: ' + audioTracks[0].label);
pc1.addStream(localstream);
trace("Adding Local Stream to peer connection");
pc1.createOffer(gotDescription1);
}
function call() {
btn1.disabled = true;
btn2.disabled = false;
trace("Starting call");
var servers = null;
var pc_constraints = {"optional": []};
pc1 = new RTCPeerConnection(servers,pc_constraints);
trace("Created local peer connection object pc1");
pc1.onicecandidate = iceCallback1;
pc2 = new RTCPeerConnection(servers,pc_constraints);
trace("Created remote peer connection object pc2");
pc2.onicecandidate = iceCallback2;
pc2.onaddstream = gotRemoteStream;
trace("Requesting local stream");
// Call into getUserMedia via the polyfill (adapter.js).
getUserMedia({audio:true, video:false},
gotStream, function() {});
}
function gotDescription1(desc){
pc1.setLocalDescription(desc);
trace("Offer from pc1 \n" + desc.sdp);
pc2.setRemoteDescription(desc);
// Since the "remote" side has no media stream we need
// to pass in the right constraints in order for it to
// accept the incoming offer of audio.
pc2.createAnswer(gotDescription2, null, sdpConstraints);
}
function gotDescription2(desc){
pc2.setLocalDescription(desc);
trace("Answer from pc2 \n" + desc.sdp);
pc1.setRemoteDescription(desc);
}
function hangup() {
trace("Ending call");
pc1.close();
pc2.close();
pc1 = null;
pc2 = null;
btn2.disabled = true;
btn1.disabled = false;
}
function gotRemoteStream(e){
audio2.src = webkitURL.createObjectURL(e.stream);
trace("Received remote stream");
}
function iceCallback1(event){
if (event.candidate) {
pc2.addIceCandidate(new RTCIceCandidate(event.candidate));
trace("Local ICE candidate: \n" + event.candidate.candidate);
}
}
function iceCallback2(event){
if (event.candidate) {
pc1.addIceCandidate(new RTCIceCandidate(event.candidate));
trace("Remote ICE candidate: \n " + event.candidate.candidate);
}
}
</script>
</body>
</html>