4 <title>PeerConnection Demo 1</title>
5 <!-- Load the polyfill to switch-hit between Chrome and Firefox -->
6 <script src="../../base/adapter.js"></script>
9 border:5px solid black;
14 font: 18px sans-serif;
18 font-family: monospace;
26 <video id="vid1" autoplay="true" muted="true"></video>
27 <video id="vid2" autoplay></video>
29 <button id="btn1" onclick="start()">Start</button>
30 <button id="btn2" onclick="call()">Call</button>
31 <button id="btn3" onclick="hangup()">Hang Up</button>
33 <xtextarea id="ta1"></textarea>
34 <xtextarea id="ta2"></textarea>
36 //var vid1 = document.getElementById("vid1");
37 //var vid2 = document.getElementById("vid2");
38 btn1.disabled = false;
43 var sdpConstraints = {'mandatory': {
44 'OfferToReceiveAudio':true,
45 'OfferToReceiveVideo':true }};
47 function gotStream(stream){
48 trace("Received local stream");
49 // Call the polyfill wrapper to attach the media stream to this element.
50 attachMediaStream(vid1, stream);
52 btn2.disabled = false;
56 trace("Requesting local stream");
58 // Call into getUserMedia via the polyfill (adapter.js).
59 getUserMedia({audio:true, video:true},
60 gotStream, function() {});
65 btn3.disabled = false;
66 trace("Starting call");
67 videoTracks = localstream.getVideoTracks();
68 audioTracks = localstream.getAudioTracks();
69 if (videoTracks.length > 0)
70 trace('Using Video device: ' + videoTracks[0].label);
71 if (audioTracks.length > 0)
72 trace('Using Audio device: ' + audioTracks[0].label);
74 pc1 = new RTCPeerConnection(servers);
75 trace("Created local peer connection object pc1");
76 pc1.onicecandidate = iceCallback1;
77 pc2 = new RTCPeerConnection(servers);
78 trace("Created remote peer connection object pc2");
79 pc2.onicecandidate = iceCallback2;
80 pc2.onaddstream = gotRemoteStream;
82 pc1.addStream(localstream);
83 trace("Adding Local Stream to peer connection");
85 pc1.createOffer(gotDescription1, onCreateSessionDescriptionError);
88 function onCreateSessionDescriptionError(error) {
89 trace('Failed to create session description: ' + error.toString());
92 function gotDescription1(desc){
93 pc1.setLocalDescription(desc);
94 trace("Offer from pc1 \n" + desc.sdp);
95 pc2.setRemoteDescription(desc);
96 // Since the "remote" side has no media stream we need
97 // to pass in the right constraints in order for it to
98 // accept the incoming offer of audio and video.
99 pc2.createAnswer(gotDescription2, onCreateSessionDescriptionError,
103 function gotDescription2(desc){
104 pc2.setLocalDescription(desc);
105 trace("Answer from pc2 \n" + desc.sdp);
106 pc1.setRemoteDescription(desc);
110 trace("Ending call");
115 btn3.disabled = true;
116 btn2.disabled = false;
119 function gotRemoteStream(e){
120 // Call the polyfill wrapper to attach the media stream to this element.
121 attachMediaStream(vid2, e.stream);
122 trace("Received remote stream");
125 function iceCallback1(event){
126 if (event.candidate) {
127 pc2.addIceCandidate(new RTCIceCandidate(event.candidate),
128 onAddIceCandidateSuccess, onAddIceCandidateError);
129 trace("Local ICE candidate: \n" + event.candidate.candidate);
133 function iceCallback2(event){
134 if (event.candidate) {
135 pc1.addIceCandidate(new RTCIceCandidate(event.candidate),
136 onAddIceCandidateSuccess, onAddIceCandidateError);
137 trace("Remote ICE candidate: \n " + event.candidate.candidate);
141 function onAddIceCandidateSuccess() {
142 trace("AddIceCandidate success.");
145 function onAddIceCandidateError(error) {
146 trace("Failed to add Ice Candidate: " + error.toString());