| <!DOCTYPE html> |
| <html> |
| <head> |
| <title>PeerConnection PRANSWER Demo</title> |
| <style> |
| video { |
| border:5px solid black; |
| width:320px; |
| height:240px; |
| } |
| </style> |
| </head> |
| <body> |
| <video id="vid1" autoplay="true" muted="true"></video> |
| <video id="vid2" autoplay></video> |
| <br> |
| <button id="btn1" onclick="start()">Call</button> |
| <button id="btn15" onclick="accept()">Accept</button> |
| <button id="btn2" onclick="stop()">Hang Up</button> |
| <script> |
| //var vid1 = document.getElementById("vid1"); |
| //var vid2 = document.getElementById("vid2"); |
| btn1.disabled = true; |
| btn2.disabled = true; |
| var pc1,pc2; |
| var localstream; |
| |
| function trace(txt) { |
| // This function is used for logging. |
| console.log(txt); |
| } |
| |
| function traceCandidate(kind, cand) { |
| trace("Candidate(" + kind + "): " + cand.label + ": " + |
| cand.toSdp().replace("\n", "")); |
| } |
| |
| function gotStream(stream){ |
| trace("Received local stream"); |
| vid1.src = webkitURL.createObjectURL(stream); |
| localstream = stream; |
| btn1.disabled = false; |
| } |
| |
| navigator.webkitGetUserMedia({audio:true, video:true}, gotStream, function() {}); |
| |
| function start() { |
| btn1.disabled = true; |
| btn2.disabled = false; |
| trace("Starting Call"); |
| if (localstream.videoTracks.length > 0) |
| trace('Using Video device: ' + localstream.videoTracks[0].label); // Prints audio & video device names |
| if (localstream.audioTracks.length > 0) |
| trace('Using Audio device: ' + localstream.audioTracks[0].label); |
| |
| pc1 = new webkitPeerConnection00(null,iceCallback1); |
| trace("Created local peer connection object pc1"); |
| pc2 = new webkitPeerConnection00(null,iceCallback2); |
| trace("Created remote peer connection object pc2"); |
| pc2.onaddstream = gotRemoteStream; |
| |
| pc1.addStream(localstream); |
| trace("Adding Local Stream to peer connection"); |
| var offer = pc1.createOffer(null); |
| trace("Created offer:\n" + offer.toSdp()); |
| pc1.setLocalDescription(pc1.SDP_OFFER, offer); |
| trace("SetLocalDesc1"); |
| pc2.setRemoteDescription(pc2.SDP_OFFER, offer); |
| trace("SetRemoteDesc2"); |
| var answer = pc2.createAnswer(offer.toSdp(), {has_audio:true, has_video:true}); |
| var sdp = answer.toSdp(); |
| sdp = sdp.replace(/a=sendrecv/g, "a=inactive"); |
| answer = new SessionDescription(sdp); |
| trace("Created answer:\n" + answer.toSdp()); |
| pc2.setLocalDescription(pc2.SDP_PRANSWER, answer); |
| trace("SetLocalDesc2"); |
| pc1.setRemoteDescription(pc1.SDP_PRANSWER, answer); |
| trace("SetRemoteDesc1"); |
| pc1.startIce(); // Start finding local ice candidates. Once it finds candidates it will call icecallback |
| pc2.startIce(); //Starts finding remote ice candidates. Once it finds candidates it will call iceCallback2 |
| trace("Start ICE for both local & remote"); |
| } |
| |
| function accept() { |
| var sdp = pc1.remoteDescription.toSdp(); |
| sdp = sdp.replace(/a=inactive/g, "a=sendrecv"); |
| var answer = new SessionDescription(sdp); |
| pc2.setLocalDescription(pc1.SDP_ANSWER, answer); |
| pc1.setRemoteDescription(pc2.SDP_ANSWER, answer); |
| trace("Set final answer:" + sdp); |
| } |
| |
| function stop() { |
| trace("Ending Call" + "\n\n"); |
| pc1.close(); |
| pc2.close(); |
| pc1=null; |
| pc2=null; |
| btn2.disabled = true; |
| btn1.disabled = false; |
| } |
| |
| function gotRemoteStream(e){ |
| vid2.src = webkitURL.createObjectURL(e.stream); |
| trace("Received Remote Stream"); |
| } |
| |
| function iceCallback1(candidate,bMore){ |
| if (candidate) { |
| pc2.processIceMessage(candidate); |
| traceCandidate("local", candidate); |
| } |
| } |
| |
| function iceCallback2(candidate,bMore){ |
| if (candidate) { |
| pc1.processIceMessage(candidate); |
| traceCandidate("remote", candidate); |
| } |
| } |
| </script> |
| </body> |
| </html> |
| |
| |