-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpeerconnection-multi.html
282 lines (250 loc) · 9.76 KB
/
peerconnection-multi.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<!--
Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
Use of this source code is governed by a BSD-style license
that can be found in the LICENSE file in the root of the source
tree. An additional intellectual property rights grant can be found
in the file PATENTS. All contributing project authors may
be found in the AUTHORS file in the root of the source tree.
-->
<html>
<head>
<title>WebRTC Multi-PeerConnection Test</title>
<script type="text/javascript">
// This file can create an arbitrary number of peer connection calls, each
// with an arbitrary number of auto-echoing data channels. It can run with
// two separate cameras.
// Our two local video / audio streams.
var gLocalStream1 = null;
var gLocalStream2 = null;
// The number of remote view windows (2x number of calls).
var gNumRemoteViews = 0;
// Maps connection id -> { connection1, connection2 }.
var gAllConnections = [];
var gNumConnections = 0;
// Maps data channel id -> sending channel.
// Note: there can be many data channels per connection id.
var gSendingDataChannels = [];
var gTotalNumSendChannels = 0;
function startTest() {
navigator.webkitGetUserMedia(
{video: true, audio: true},
function(localStream) {
gLocalStream1 = localStream;
play(localStream, 'local-view-1');
},
getUserMediaFailedCallback);
navigator.webkitGetUserMedia(
{video: true, audio: true},
function(localStream) {
gLocalStream2 = localStream;
play(localStream, 'local-view-2');
},
getUserMediaFailedCallback);
}
function playStreamInNewRemoteView(stream, peerNumber) {
console.log('Remote stream to connection ' + peerNumber +
': ' + stream.label);
gNumRemoteViews++;
var viewName = 'remote-view-' + gNumRemoteViews;
addRemoteView(viewName, peerNumber);
play(stream, viewName);
}
function addRemoteView(elementName, peerNumber) {
var remoteViews = $('remote-views-' + peerNumber);
remoteViews.innerHTML +=
'<tr><td><video width="320" height="240" id="' + elementName + '" ' +
'autoplay="autoplay"></video></td></tr>';
}
function play(stream, videoElement) {
var streamUrl = URL.createObjectURL(stream);
$(videoElement).src = streamUrl;
}
function getUserMediaFailedCallback(error) {
console.log('getUserMedia request failed with code ' + error.code);
}
function call() {
connection1 = new webkitRTCPeerConnection(null,
{optional:[{RtpDataChannels: true}]});
connection1.addStream(gLocalStream1);
connection2 = new webkitRTCPeerConnection(
null, {optional:[{RtpDataChannels: true}]});
connection2.addStream(gLocalStream2);
connection2.onicecandidate = function(event) {
if (event.candidate) {
var candidate = new RTCIceCandidate(event.candidate);
connection1.addIceCandidate(candidate);
}
};
connection1.onicecandidate = function(event) {
if (event.candidate) {
console.log('Ice candidate: ' + event.candidate);
var candidate = new RTCIceCandidate(event.candidate);
connection2.addIceCandidate(candidate);
}
};
connection1.onaddstream = function(event) {
playStreamInNewRemoteView(event.stream, 1);
//addDataChannelAnchor(connection1, connection2);
};
connection2.onaddstream = function(event) {
playStreamInNewRemoteView(event.stream, 2);
};
// TODO(phoglund): hack to work around
// https://code.google.com/p/webrtc/issues/detail?id=1203. When it is fixed,
// uncomment the negotiate call, remove addDataChannel and uncomment in
// connection1.onaddstream. Also remove the notice at the top of the HTML!
// negotiate(connection1, connection2);
addDataChannelAnchor(connection1, connection2);
}
function negotiate(connection1, connection2) {
connection1.createOffer(function(offer) {
connection1.setLocalDescription(offer);
connection2.setRemoteDescription(offer);
connection2.createAnswer(function(answer) {
console.log('Created answer ' + answer);
connection2.setLocalDescription(answer);
connection1.setRemoteDescription(answer);
});
});
}
function addDataChannelAnchor(connection1, connection2) {
var connectionId = gNumConnections++;
gAllConnections[connectionId] = { connection1: connection1,
connection2: connection2 };
addOneAnchor(1, connectionId);
addOneAnchor(2, connectionId);
}
function makeDataChannelAnchorName(peerId, connectionId) {
return 'data-channels-peer' + peerId + '-' + connectionId;
}
// This adds a target table we'll add our input fields to later.
function addOneAnchor(peerId, connectionId) {
var newButtonId = 'add-data-channel-' + connectionId;
var remoteViewContainer = 'remote-views-' + peerId;
$(remoteViewContainer).innerHTML +=
'<tr><td><button id="' + newButtonId + '" ' +
'onclick="addDataChannel(' + connectionId + ')">' +
' Add Echoing Data Channel</button></td></tr>';
var anchorName = makeDataChannelAnchorName(peerId, connectionId);
$(remoteViewContainer).innerHTML +=
'<tr><td><table id="' + anchorName + '"></table></td></tr>';
}
// Called by clicking Add Echoing Data Channel.
function addDataChannel(connectionId) {
var dataChannelId = gTotalNumSendChannels++;
var peer1SinkId = addDataChannelSink(1, connectionId, dataChannelId);
var peer2SinkId = addDataChannelSink(2, connectionId, dataChannelId);
var connections = gAllConnections[connectionId];
configureChannels(connections.connection1, connections.connection2,
peer1SinkId, peer2SinkId, dataChannelId);
// Add the field the user types in, and a
// dummy field so everything lines up nicely.
addDataChannelSource(1, connectionId, dataChannelId);
addDisabledInputField(2, connectionId, '(the above is echoed)');
negotiate(connections.connection1, connections.connection2);
}
function configureChannels(connection1, connection2, targetFor1, targetFor2,
dataChannelId) {
// Label the channel so we know where to send the data later in dispatch.
sendChannel = connection1.createDataChannel(
targetFor2, { reliable : false });
sendChannel.onmessage = function(messageEvent) {
$(targetFor1).value = messageEvent.data;
}
gSendingDataChannels[dataChannelId] = sendChannel;
connection2.ondatachannel = function(event) {
// The channel got created by a message from a sending channel: hook this
// new receiver channel up to dispatch and then echo any messages.
event.channel.onmessage = dispatchAndEchoDataMessage;
}
}
function addDataChannelSink(peerNumber, connectionId, dataChannelId) {
var sinkId = 'data-sink-peer' + peerNumber + '-' + dataChannelId;
var anchor = $(makeDataChannelAnchorName(peerNumber, connectionId));
anchor.innerHTML +=
'<tr><td><input type="text" id="' + sinkId + '" disabled/></td></tr>';
return sinkId;
}
function addDataChannelSource(peerNumber, connectionId, dataChannelId) {
var sourceId = 'data-source-peer' + peerNumber + '-' + dataChannelId;
var anchor = $(makeDataChannelAnchorName(peerNumber, connectionId));
anchor.innerHTML +=
'<tr><td><input type="text" id="' + sourceId + '"' +
' onchange="userWroteSomethingIn(\'' + sourceId + '\', ' +
dataChannelId + ');"/></td></tr>';
}
function userWroteSomethingIn(sourceId, dataChannelId) {
var source = $(sourceId);
var dataChannel = gSendingDataChannels[dataChannelId];
dataChannel.send(source.value);
}
function addDisabledInputField(peerNumber, connectionId, text) {
var anchor = $(makeDataChannelAnchorName(peerNumber, connectionId));
anchor.innerHTML +=
'<tr><td><input type="text" value="' + text + '" disabled/></td></tr>';
}
function dispatchAndEchoDataMessage(messageEvent) {
// Since we labeled the channel earlier, we know to which input element
// we should send the data.
var dataChannel = messageEvent.currentTarget;
var targetInput = $(dataChannel.label);
targetInput.value = messageEvent.data;
dataChannel.send('echo: ' + messageEvent.data);
}
window.onload = function() {
startTest();
}
$ = function(id) {
return document.getElementById(id);
};
</script>
</head>
<body>
<table border="0">
<tr>
<td colspan="2">
Notes:
<ul>
<li>Due to https://code.google.com/p/webrtc/issues/detail?id=1203,
you must create a data channel to actually get a call negotiated. Add
one call at a time and click "add echoing data channel" for each and
you'll be fine.</li>
<li>For unknown reasons, adding a new data channel will clear the
input field contents for all other channels on the same call. This is
not the data channel's fault though.</li>
</ul>
</td>
</tr>
<tr>
<td>Local Preview for Peer 1</td>
<td>Local Preview for Peer 2</td>
</tr>
<tr>
<td><video width="320" height="240" id="local-view-1"
autoplay="autoplay"></video></td>
<td><video width="320" height="240" id="local-view-2"
autoplay="autoplay"></video></td>
</tr>
<tr>
<td><button id="add-call" onclick="call();">Add Call</button></td>
</tr>
<tr>
<td>
<table id="remote-views-1">
<tr>
<td>Remote (Incoming to Peer 1)</td>
</tr>
</table>
</td>
<td>
<table id="remote-views-2">
<tr>
<td>Remote (Incoming to Peer 2)</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>