blob: ba1949ec1d3b125693fe7a6822c4754a145906bb [file] [log] [blame]
vikasmarwaha@webrtc.org98fce152013-02-27 23:22:10 +00001<!DOCTYPE html>
2<html>
3<head>
4 <title>PeerConnection PRANSWER Demo</title>
5 <style>
6 video {
7 border:5px solid black;
8 width:320px;
9 height:240px;
10 }
11 </style>
12 </head>
13 <body>
vikasmarwaha@webrtc.orgda0f7082013-03-11 16:58:07 +000014 <video id="vid1" autoplay="true" muted="true"></video>
vikasmarwaha@webrtc.org98fce152013-02-27 23:22:10 +000015 <video id="vid2" autoplay></video>
16 <br>
17 <button id="btn1" onclick="start()">Call</button>
18 <button id="btn15" onclick="accept()">Accept</button>
19 <button id="btn2" onclick="stop()">Hang Up</button>
20 <script>
21//var vid1 = document.getElementById("vid1");
22//var vid2 = document.getElementById("vid2");
23btn1.disabled = true;
24btn2.disabled = true;
25var pc1,pc2;
26var localstream;
27
28function trace(txt) {
29 // This function is used for logging.
30 console.log(txt);
31}
32
33function traceCandidate(kind, cand) {
34 trace("Candidate(" + kind + "): " + cand.label + ": " +
35 cand.toSdp().replace("\n", ""));
36}
37
38function gotStream(stream){
39 trace("Received local stream");
40 vid1.src = webkitURL.createObjectURL(stream);
41 localstream = stream;
42 btn1.disabled = false;
43}
44
45navigator.webkitGetUserMedia({audio:true, video:true}, gotStream, function() {});
46
47function start() {
48 btn1.disabled = true;
49 btn2.disabled = false;
50 trace("Starting Call");
51 if (localstream.videoTracks.length > 0)
52 trace('Using Video device: ' + localstream.videoTracks[0].label); // Prints audio & video device names
53 if (localstream.audioTracks.length > 0)
54 trace('Using Audio device: ' + localstream.audioTracks[0].label);
55
56 pc1 = new webkitPeerConnection00(null,iceCallback1);
57 trace("Created local peer connection object pc1");
58 pc2 = new webkitPeerConnection00(null,iceCallback2);
59 trace("Created remote peer connection object pc2");
60 pc2.onaddstream = gotRemoteStream;
61
62 pc1.addStream(localstream);
63 trace("Adding Local Stream to peer connection");
64 var offer = pc1.createOffer(null);
65 trace("Created offer:\n" + offer.toSdp());
66 pc1.setLocalDescription(pc1.SDP_OFFER, offer);
67 trace("SetLocalDesc1");
68 pc2.setRemoteDescription(pc2.SDP_OFFER, offer);
69 trace("SetRemoteDesc2");
70 var answer = pc2.createAnswer(offer.toSdp(), {has_audio:true, has_video:true});
71 var sdp = answer.toSdp();
72 sdp = sdp.replace(/a=sendrecv/g, "a=inactive");
73 answer = new SessionDescription(sdp);
74 trace("Created answer:\n" + answer.toSdp());
75 pc2.setLocalDescription(pc2.SDP_PRANSWER, answer);
76 trace("SetLocalDesc2");
77 pc1.setRemoteDescription(pc1.SDP_PRANSWER, answer);
78 trace("SetRemoteDesc1");
79 pc1.startIce(); // Start finding local ice candidates. Once it finds candidates it will call icecallback
80 pc2.startIce(); //Starts finding remote ice candidates. Once it finds candidates it will call iceCallback2
81 trace("Start ICE for both local & remote");
82}
83
84function accept() {
85 var sdp = pc1.remoteDescription.toSdp();
86 sdp = sdp.replace(/a=inactive/g, "a=sendrecv");
87 var answer = new SessionDescription(sdp);
88 pc2.setLocalDescription(pc1.SDP_ANSWER, answer);
89 pc1.setRemoteDescription(pc2.SDP_ANSWER, answer);
90 trace("Set final answer:" + sdp);
91}
92
93function stop() {
94 trace("Ending Call" + "\n\n");
95 pc1.close();
96 pc2.close();
97 pc1=null;
98 pc2=null;
99 btn2.disabled = true;
100 btn1.disabled = false;
101}
102
103function gotRemoteStream(e){
104 vid2.src = webkitURL.createObjectURL(e.stream);
105 trace("Received Remote Stream");
106}
107
108function iceCallback1(candidate,bMore){
109 if (candidate) {
110 pc2.processIceMessage(candidate);
111 traceCandidate("local", candidate);
112 }
113}
114
115function iceCallback2(candidate,bMore){
116 if (candidate) {
117 pc1.processIceMessage(candidate);
118 traceCandidate("remote", candidate);
119 }
120}
121 </script>
122 </body>
123</html>
124
125