http://www.oschina.net/code/snippet_54371_18805
1. [代码][JavaScript]代码
001 | performance.now = performance.now || performance.webkitNow; |
005 | btn1.disabled = false; |
006 | btn2.disabled = true; |
007 | btn3.disabled = true; |
010 | function trace(text) { |
012 | if (text[text.length - 1] == '\n') { |
013 | text = text.substring(0, text.length - 1); |
015 | console.log((performance.now() / 1000).toFixed(3) + ": " + text); |
018 | function gotStream(stream){ |
019 | trace("Received local stream"); |
020 | vid1.src = webkitURL.createObjectURL(stream); |
021 | localstream = stream; |
022 | btn2.disabled = false; |
026 | trace("Requesting local stream"); |
027 | btn1.disabled = true; |
028 | navigator.webkitGetUserMedia({audio:true, video:true}, |
029 | gotStream, function() {}); |
033 | btn2.disabled = true; |
034 | btn3.disabled = false; |
035 | trace("Starting call"); |
038 | if (!!localstream.videoTracks && !localstream.getVideoTracks) { |
039 | localstream.getVideoTracks = function(){ |
040 | return this.videoTracks; |
043 | if (!!localstream.audioTracks && !localstream.getAudioTracks) { |
044 | localstream.getAudioTracks = function(){ |
045 | return this.audioTracks; |
050 | if (localstream.getVideoTracks().length > 0) |
051 | trace('Using Video device: ' + localstream.getVideoTracks()[0].label); |
052 | if (localstream.getAudioTracks().length > 0) |
053 | trace('Using Audio device: ' + localstream.getAudioTracks()[0].label); |
055 | window.pc1 = new webkitRTCPeerConnection(servers); |
056 | trace("Created local peer connection object pc1"); |
057 | pc1.onicecandidate = iceCallback1; |
058 | window.pc2 = new webkitRTCPeerConnection(servers); |
059 | trace("Created remote peer connection object pc2"); |
060 | pc2.onicecandidate = iceCallback2; |
061 | pc2.onaddstream = gotRemoteStream; |
063 | pc1.addStream(localstream); |
064 | trace("Adding Local Stream to peer connection"); |
066 | pc1.createOffer(gotDescription1); |
069 | function gotDescription1(desc){ |
070 | pc1.setLocalDescription(desc); |
071 | trace("Offer from pc1 \n" + desc.sdp); |
072 | pc2.setRemoteDescription(desc); |
073 | pc2.createAnswer(gotDescription2); |
076 | function gotDescription2(desc){ |
077 | pc2.setLocalDescription(desc); |
078 | trace("Answer from pc2 \n" + desc.sdp); |
079 | pc1.setRemoteDescription(desc); |
083 | trace("Ending call"); |
088 | btn3.disabled = true; |
089 | btn2.disabled = false; |
092 | function gotRemoteStream(e){ |
093 | vid2.src = webkitURL.createObjectURL(e.stream); |
094 | trace("Received remote stream"); |
097 | function iceCallback1(event){ |
098 | if (event.candidate) { |
099 | pc2.addIceCandidate(new RTCIceCandidate(event.candidate)); |
100 | trace("Local ICE candidate: \n" + event.candidate.candidate); |
104 | function iceCallback2(event){ |
105 | if (event.candidate) { |
106 | pc1.addIceCandidate(new RTCIceCandidate(event.candidate)); |
107 | trace("Remote ICE candidate: \n " + event.candidate.candidate); |