blob: d4425d619b8539793e762c6a3a7c459014072e00 [file] [log] [blame]
vikasmarwaha@webrtc.org98fce152013-02-27 23:22:10 +00001<!DOCTYPE html>
2<html>
3<head>
vikasmarwaha@webrtc.org4c44fe02013-04-08 21:23:58 +00004<title>PeerConnection PRANSWER Demo</title>
5<!-- Load the polyfill to switch-hit between Chrome and Firefox -->
6<script src="../../base/adapter.js"></script>
7<style>
8video {
9 border:5px solid black;
10 width:320px;
11 height:240px;
12}
13</style>
14</head>
15<body>
16<video id="vid1" autoplay="true" muted="true"></video>
17<video id="vid2" autoplay></video>
18<br>
19<button id="btn1" onclick="start()">Call</button>
20<button id="btn2" onclick="accept()">Accept</button>
21<button id="btn3" onclick="stop()">Hang Up</button>
22<script>
vikasmarwaha@webrtc.org98fce152013-02-27 23:22:10 +000023//var vid1 = document.getElementById("vid1");
24//var vid2 = document.getElementById("vid2");
25btn1.disabled = true;
26btn2.disabled = true;
vikasmarwaha@webrtc.org4c44fe02013-04-08 21:23:58 +000027btn3.disabled = true;
vikasmarwaha@webrtc.org98fce152013-02-27 23:22:10 +000028var pc1,pc2;
29var localstream;
vikasmarwaha@webrtc.org4c44fe02013-04-08 21:23:58 +000030var sdpConstraints = {'mandatory': {
31 'OfferToReceiveAudio':true,
32 'OfferToReceiveVideo':true }};
33
34function gotStream(stream) {
vikasmarwaha@webrtc.org98fce152013-02-27 23:22:10 +000035 trace("Received local stream");
vikasmarwaha@webrtc.org4c44fe02013-04-08 21:23:58 +000036 // Call the polyfill wrapper to attach the media stream to this element.
37 attachMediaStream(vid1, stream);
vikasmarwaha@webrtc.org98fce152013-02-27 23:22:10 +000038 localstream = stream;
39 btn1.disabled = false;
40}
41
vikasmarwaha@webrtc.org4c44fe02013-04-08 21:23:58 +000042getUserMedia({audio:true, video:true}, gotStream, function() {});
vikasmarwaha@webrtc.org98fce152013-02-27 23:22:10 +000043
44function start() {
45 btn1.disabled = true;
46 btn2.disabled = false;
vikasmarwaha@webrtc.org4c44fe02013-04-08 21:23:58 +000047 btn3.disabled = false;
vikasmarwaha@webrtc.org98fce152013-02-27 23:22:10 +000048 trace("Starting Call");
vikasmarwaha@webrtc.org4c44fe02013-04-08 21:23:58 +000049 videoTracks = localstream.getVideoTracks();
50 audioTracks = localstream.getAudioTracks();
51 if (videoTracks.length > 0)
52 trace('Using Video device: ' + videoTracks[0].label);
53 if (audioTracks.length > 0)
54 trace('Using Audio device: ' + audioTracks[0].label);
vikasmarwaha@webrtc.org98fce152013-02-27 23:22:10 +000055
vikasmarwaha@webrtc.org4c44fe02013-04-08 21:23:58 +000056 var servers = null;
57 pc1 = new RTCPeerConnection(servers);
58 trace("Created local peer connection object pc1");
59 pc1.onicecandidate = iceCallback1;
60 pc2 = new RTCPeerConnection(servers);
vikasmarwaha@webrtc.org98fce152013-02-27 23:22:10 +000061 trace("Created remote peer connection object pc2");
vikasmarwaha@webrtc.org4c44fe02013-04-08 21:23:58 +000062 pc2.onicecandidate = iceCallback2;
vikasmarwaha@webrtc.org98fce152013-02-27 23:22:10 +000063 pc2.onaddstream = gotRemoteStream;
64
65 pc1.addStream(localstream);
66 trace("Adding Local Stream to peer connection");
vikasmarwaha@webrtc.org4c44fe02013-04-08 21:23:58 +000067
68 pc1.createOffer(gotDescription1);
69
70}
71
72function gotDescription1(desc) {
73 pc1.setLocalDescription(desc);
74 trace("Offer from pc1 \n" + desc.sdp);
75 pc2.setRemoteDescription(desc);
76 // Since the "remote" side has no media stream we need
77 // to pass in the right constraints in order for it to
78 // accept the incoming offer of audio and video.
79 pc2.createAnswer(gotDescription2, null, sdpConstraints);
80}
81
82function gotDescription2(desc) {
83 // Provisional answer, set a=inactive & set sdp type to pranswer.
84 desc.sdp = desc.sdp.replace(/a=recvonly/g, "a=inactive");
85 desc.type = "pranswer";
86 pc2.setLocalDescription(desc);
87 trace("Pranswer from pc2 \n" + desc.sdp);
88 pc1.setRemoteDescription(desc);
89}
90
91function gotDescription3(desc) {
92 // Final answer, setting a=recvonly & sdp type to answer.
93 desc.sdp = desc.sdp.replace(/a=inactive/g, "a=recvonly");
94 desc.type = "answer";
95 pc2.setLocalDescription(desc);
96 trace("Answer from pc2 \n" + desc.sdp);
97 pc1.setRemoteDescription(desc);
vikasmarwaha@webrtc.org98fce152013-02-27 23:22:10 +000098}
99
100function accept() {
vikasmarwaha@webrtc.org4c44fe02013-04-08 21:23:58 +0000101 pc2.createAnswer(gotDescription3, null, sdpConstraints);
102 btn2.disabled = true;
103 btn1.disabled = false;
vikasmarwaha@webrtc.org98fce152013-02-27 23:22:10 +0000104}
105
106function stop() {
107 trace("Ending Call" + "\n\n");
108 pc1.close();
109 pc2.close();
110 pc1=null;
111 pc2=null;
112 btn2.disabled = true;
113 btn1.disabled = false;
vikasmarwaha@webrtc.org4c44fe02013-04-08 21:23:58 +0000114 btn3.disabled = true;
vikasmarwaha@webrtc.org98fce152013-02-27 23:22:10 +0000115}
116
vikasmarwaha@webrtc.org4c44fe02013-04-08 21:23:58 +0000117function gotRemoteStream(e) {
vikasmarwaha@webrtc.org98fce152013-02-27 23:22:10 +0000118 vid2.src = webkitURL.createObjectURL(e.stream);
vikasmarwaha@webrtc.org4c44fe02013-04-08 21:23:58 +0000119 trace("Received remote stream");
vikasmarwaha@webrtc.org98fce152013-02-27 23:22:10 +0000120}
121
vikasmarwaha@webrtc.org4c44fe02013-04-08 21:23:58 +0000122function iceCallback1(event) {
123 if (event.candidate) {
124 pc2.addIceCandidate(new RTCIceCandidate(event.candidate));
125 trace("Local ICE candidate: \n" + event.candidate.candidate);
vikasmarwaha@webrtc.org98fce152013-02-27 23:22:10 +0000126 }
127}
128
vikasmarwaha@webrtc.org4c44fe02013-04-08 21:23:58 +0000129function iceCallback2(event) {
130 if (event.candidate) {
131 pc1.addIceCandidate(new RTCIceCandidate(event.candidate));
132 trace("Remote ICE candidate: \n " + event.candidate.candidate);
vikasmarwaha@webrtc.org98fce152013-02-27 23:22:10 +0000133 }
vikasmarwaha@webrtc.org4c44fe02013-04-08 21:23:58 +0000134}
vikasmarwaha@webrtc.org98fce152013-02-27 23:22:10 +0000135 </script>
136 </body>
137</html>
138