OLD | NEW |
| (Empty) |
1 /** | |
2 * Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
3 * Use of this source code is governed by a BSD-style license that can be | |
4 * found in the LICENSE file. | |
5 */ | |
6 | |
7 // TODO(phoglund): merge into message_handling.js. | |
8 | |
9 /** @private */ | |
10 var gHasSeenCryptoInSdp = 'no-crypto-seen'; | |
11 | |
12 // Public interface towards the other javascript files, such as | |
13 // message_handling.js. The contract for these functions is described in | |
14 // message_handling.js. | |
15 | |
16 function receiveOffer(peerConnection, offer, constraints, callback) { | |
17 var sessionDescription = new RTCSessionDescription(offer); | |
18 peerConnection.setRemoteDescription( | |
19 sessionDescription, | |
20 function() { success_('setRemoteDescription'); }, | |
21 function() { failure_('setRemoteDescription'); }); | |
22 | |
23 peerConnection.createAnswer( | |
24 function(answer) { | |
25 success_('createAnswer'); | |
26 setLocalDescription(peerConnection, answer); | |
27 callback(answer); | |
28 }, | |
29 function() { failure_('createAnswer'); }, | |
30 constraints); | |
31 } | |
32 | |
33 function receiveAnswer(peerConnection, answer, callback) { | |
34 var sessionDescription = new RTCSessionDescription(answer); | |
35 peerConnection.setRemoteDescription( | |
36 sessionDescription, | |
37 function() { | |
38 success_('setRemoteDescription'); | |
39 callback(); | |
40 }, | |
41 function() { failure_('setRemoteDescription'); }); | |
42 } | |
43 | |
44 function createPeerConnection(stun_server, useRtpDataChannels) { | |
45 servers = {iceServers: [{url: 'stun:' + stun_server}]}; | |
46 try { | |
47 var constraints = { optional: [{ RtpDataChannels: useRtpDataChannels }]}; | |
48 peerConnection = new RTCPeerConnection(servers, constraints); | |
49 } catch (exception) { | |
50 throw failTest('Failed to create peer connection: ' + exception); | |
51 } | |
52 peerConnection.onaddstream = addStreamCallback_; | |
53 peerConnection.onremovestream = removeStreamCallback_; | |
54 peerConnection.onicecandidate = iceCallback_; | |
55 return peerConnection; | |
56 } | |
57 | |
58 function createOffer(peerConnection, constraints, callback) { | |
59 peerConnection.createOffer( | |
60 function(localDescription) { | |
61 success_('createOffer'); | |
62 setLocalDescription(peerConnection, localDescription); | |
63 callback(localDescription); | |
64 }, | |
65 function() { failure_('createOffer'); }, | |
66 constraints); | |
67 } | |
68 | |
69 function hasSeenCryptoInSdp() { | |
70 returnToTest(gHasSeenCryptoInSdp); | |
71 } | |
72 | |
73 // Internals. | |
74 /** @private */ | |
75 function success_(method) { | |
76 debug(method + '(): success.'); | |
77 } | |
78 | |
79 /** @private */ | |
80 function failure_(method, error) { | |
81 throw failTest(method + '() failed: ' + error); | |
82 } | |
83 | |
84 /** @private */ | |
85 function iceCallback_(event) { | |
86 if (event.candidate) | |
87 sendIceCandidate(event.candidate); | |
88 } | |
89 | |
90 /** @private */ | |
91 function setLocalDescription(peerConnection, sessionDescription) { | |
92 if (sessionDescription.sdp.search('a=crypto') != -1 || | |
93 sessionDescription.sdp.search('a=fingerprint') != -1) | |
94 gHasSeenCryptoInSdp = 'crypto-seen'; | |
95 | |
96 peerConnection.setLocalDescription( | |
97 sessionDescription, | |
98 function() { success_('setLocalDescription'); }, | |
99 function() { failure_('setLocalDescription'); }); | |
100 } | |
101 | |
102 /** @private */ | |
103 function addStreamCallback_(event) { | |
104 debug('Receiving remote stream...'); | |
105 var videoTag = document.getElementById('remote-view'); | |
106 attachMediaStream(videoTag, event.stream); | |
107 } | |
108 | |
109 /** @private */ | |
110 function removeStreamCallback_(event) { | |
111 debug('Call ended.'); | |
112 document.getElementById('remote-view').src = ''; | |
113 } | |
OLD | NEW |