phoglund@webrtc.org | 7d74bdb | 2012-11-28 13:03:17 +0000 | [diff] [blame] | 1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> |
| 2 | |
| 3 | <!-- |
| 4 | To quickly iterate when developing this test, make sure you select |
| 5 | 'Always allow this site to use this webcam' option in the dropdown menu of |
| 6 | Chrome when it's requesting access to your webcam. |
| 7 | Notice that this requires the site you're browsing to use HTTPS. |
| 8 | |
| 9 | Without that, the test might timeout before you have had the chance to accept |
| 10 | access to the webcam. |
| 11 | --> |
| 12 | |
| 13 | <html> |
| 14 | <head> |
| 15 | <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> |
| 16 | <title>PeerConnection Connection Test</title> |
| 17 | |
| 18 | <script src="https://w3c-test.org/resources/testharness.js"></script> |
| 19 | |
| 20 | <script type="text/javascript"> |
| 21 | var test = async_test('Can set up a basic WebRTC call.', {timeout: 5000}); |
| 22 | var gFirstConnection = null; |
| 23 | var gSecondConnection = null; |
| 24 | |
phoglund@webrtc.org | 7d74bdb | 2012-11-28 13:03:17 +0000 | [diff] [blame] | 25 | function getUserMediaOkCallback(localStream) { |
| 26 | gFirstConnection = new webkitRTCPeerConnection(null, null); |
| 27 | gFirstConnection.onicecandidate = onIceCandidateToFirst; |
| 28 | gFirstConnection.addStream(localStream); |
| 29 | gFirstConnection.createOffer(onOfferCreated); |
phoglund@webrtc.org | 53034fb | 2012-11-30 11:59:20 +0000 | [diff] [blame^] | 30 | |
| 31 | var videoTag = document.getElementById('local-view'); |
| 32 | videoTag.src = webkitURL.createObjectURL(localStream); |
phoglund@webrtc.org | 7d74bdb | 2012-11-28 13:03:17 +0000 | [diff] [blame] | 33 | }; |
| 34 | |
| 35 | var onOfferCreated = test.step_func(function(offer) { |
| 36 | gFirstConnection.setLocalDescription(offer); |
| 37 | |
| 38 | // This would normally go across the application's signaling solution. |
| 39 | // In our case, the "signaling" is to call this function. |
| 40 | receiveCall(offer.sdp); |
| 41 | }); |
| 42 | |
phoglund@webrtc.org | 53034fb | 2012-11-30 11:59:20 +0000 | [diff] [blame^] | 43 | function receiveCall(offerSdp) { |
phoglund@webrtc.org | 7d74bdb | 2012-11-28 13:03:17 +0000 | [diff] [blame] | 44 | gSecondConnection = new webkitRTCPeerConnection(null, null); |
| 45 | gSecondConnection.onicecandidate = onIceCandidateToSecond; |
| 46 | gSecondConnection.onaddstream = onRemoteStream; |
| 47 | |
| 48 | var parsedOffer = new RTCSessionDescription({ type: 'offer', |
| 49 | sdp: offerSdp }); |
| 50 | gSecondConnection.setRemoteDescription(parsedOffer); |
| 51 | |
phoglund@webrtc.org | 53034fb | 2012-11-30 11:59:20 +0000 | [diff] [blame^] | 52 | gSecondConnection.createAnswer(onAnswerCreated, |
| 53 | failed('createAnswer')); |
| 54 | }; |
phoglund@webrtc.org | 7d74bdb | 2012-11-28 13:03:17 +0000 | [diff] [blame] | 55 | |
| 56 | var onAnswerCreated = test.step_func(function(answer) { |
| 57 | gSecondConnection.setLocalDescription(answer); |
| 58 | |
| 59 | // Similarly, this would go over the application's signaling solution. |
| 60 | handleAnswer(answer.sdp); |
| 61 | }); |
| 62 | |
phoglund@webrtc.org | 53034fb | 2012-11-30 11:59:20 +0000 | [diff] [blame^] | 63 | function handleAnswer(answerSdp) { |
phoglund@webrtc.org | 7d74bdb | 2012-11-28 13:03:17 +0000 | [diff] [blame] | 64 | var parsedAnswer = new RTCSessionDescription({ type: 'answer', |
| 65 | sdp: answerSdp }); |
| 66 | gFirstConnection.setRemoteDescription(parsedAnswer); |
phoglund@webrtc.org | 7d74bdb | 2012-11-28 13:03:17 +0000 | [diff] [blame] | 67 | |
phoglund@webrtc.org | 53034fb | 2012-11-30 11:59:20 +0000 | [diff] [blame^] | 68 | // Call negotiated: done. |
| 69 | test.done(); |
| 70 | }; |
| 71 | |
| 72 | // Note: the ice candidate handlers are special. We can not wrap them in test |
| 73 | // steps since that seems to cause some kind of starvation that prevents the |
| 74 | // call of being set up. Unfortunately we cannot report errors in here. |
| 75 | var onIceCandidateToFirst = function(event) { |
phoglund@webrtc.org | 7d74bdb | 2012-11-28 13:03:17 +0000 | [diff] [blame] | 76 | // If event.candidate is null = no more candidates. |
| 77 | if (event.candidate) { |
| 78 | var candidate = new RTCIceCandidate(event.candidate); |
| 79 | gSecondConnection.addIceCandidate(candidate); |
| 80 | } |
phoglund@webrtc.org | 53034fb | 2012-11-30 11:59:20 +0000 | [diff] [blame^] | 81 | }; |
phoglund@webrtc.org | 7d74bdb | 2012-11-28 13:03:17 +0000 | [diff] [blame] | 82 | |
phoglund@webrtc.org | 53034fb | 2012-11-30 11:59:20 +0000 | [diff] [blame^] | 83 | var onIceCandidateToSecond = function(event) { |
phoglund@webrtc.org | 7d74bdb | 2012-11-28 13:03:17 +0000 | [diff] [blame] | 84 | if (event.candidate) { |
| 85 | var candidate = new RTCIceCandidate(event.candidate); |
| 86 | gFirstConnection.addIceCandidate(candidate); |
| 87 | } |
phoglund@webrtc.org | 53034fb | 2012-11-30 11:59:20 +0000 | [diff] [blame^] | 88 | }; |
| 89 | |
| 90 | var onRemoteStream = test.step_func(function(event) { |
| 91 | var videoTag = document.getElementById('remote-view'); |
| 92 | videoTag.src = webkitURL.createObjectURL(event.stream); |
phoglund@webrtc.org | 7d74bdb | 2012-11-28 13:03:17 +0000 | [diff] [blame] | 93 | }); |
| 94 | |
phoglund@webrtc.org | 53034fb | 2012-11-30 11:59:20 +0000 | [diff] [blame^] | 95 | // Returns a suitable error callback. |
| 96 | function failed(function_name) { |
| 97 | return test.step_func(function() { |
| 98 | assert_unreached('WebRTC called error callback for ' + function_name); |
| 99 | }); |
| 100 | } |
phoglund@webrtc.org | 7d74bdb | 2012-11-28 13:03:17 +0000 | [diff] [blame] | 101 | |
phoglund@webrtc.org | 53034fb | 2012-11-30 11:59:20 +0000 | [diff] [blame^] | 102 | // This function starts the test. |
phoglund@webrtc.org | 7d74bdb | 2012-11-28 13:03:17 +0000 | [diff] [blame] | 103 | test.step(function() { |
| 104 | navigator.webkitGetUserMedia({ video: true, audio: true }, |
| 105 | getUserMediaOkCallback, |
phoglund@webrtc.org | 53034fb | 2012-11-30 11:59:20 +0000 | [diff] [blame^] | 106 | failed('getUserMedia')); |
phoglund@webrtc.org | 7d74bdb | 2012-11-28 13:03:17 +0000 | [diff] [blame] | 107 | }); |
| 108 | </script> |
| 109 | </head> |
| 110 | |
| 111 | <body> |
phoglund@webrtc.org | 7d74bdb | 2012-11-28 13:03:17 +0000 | [diff] [blame] | 112 | |
phoglund@webrtc.org | 53034fb | 2012-11-30 11:59:20 +0000 | [diff] [blame^] | 113 | <div> |
| 114 | <video width="320" height="240" id="remote-view" autoplay="autoplay"></video> |
| 115 | <video width="320" height="240" id="local-view" autoplay="autoplay"></video> |
| 116 | </div> |
phoglund@webrtc.org | 7d74bdb | 2012-11-28 13:03:17 +0000 | [diff] [blame] | 117 | <div id="log"></div> |
| 118 | </body> |
phoglund@webrtc.org | 53034fb | 2012-11-30 11:59:20 +0000 | [diff] [blame^] | 119 | </html> |