forked from legastero/stanza
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjingle-demo.html
239 lines (216 loc) · 9.29 KB
/
jingle-demo.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
<html>
<head>
<meta charset="UTF-8" />
<title>Jingle Testing</title>
<!-- Using webrtc-adapter is 1000% recommended -->
<script src="https://github.com/webrtcHacks/adapter/blob/master/release/adapter.js"></script>
<script src="dist/stanza.browser.js"></script>
<style>
video {
width: 320px;
}
#received {
display: none;
}
</style>
</head>
<body>
<div id="settings">
<h1>Connection Settings</h1>
<form id="loginInfo">
<label
>JID: <input id="jid" type="text" name="jid" placeholder="[email protected]"
/></label>
<label>Password: <input id="password" type="password" name="password"/></label>
<label
>WebSocket URL:
<input
id="url"
type="text"
name="url"
value=""
placeholder="ws://localhost:5280/xmpp-websocket/"
/></label>
<input id="connect" type="submit" value="Connect" />
</form>
<h2>My full JID:</h2>
<p id="myJID"></p>
<h2>Start Video Session</h2>
<form id="callInfo">
<label
>Peer JID:
<input id="peer" type="text" name="peer" placeholder="[email protected]"
/></label>
<input id="call" type="submit" value="Call" />
</form>
<h2>Send a file</h2>
<form id="fileInfo">
<label
>Peer JID:
<input
id="filepeer"
type="text"
name="peer"
placeholder="[email protected]"
/></label>
<input type="file" id="files" name="files" />
</form>
<form id="snapshot">
<label
>Peer JID:
<input
id="snappeer"
type="text"
name="peer"
placeholder="[email protected]"
/></label>
<input id="snapshot" type="submit" value="Send snapshot" />
</form>
<a id="received"></a>
</div>
<h2>Local Video</h2>
<video id="localVideo"></video>
<h2>Remote Video</h2>
<video id="remoteVideo"></video>
<script>
var client;
var localMedia = [];
var loginInfo = document.getElementById('loginInfo');
var inlog = console.log.bind(console, '<<in');
var outlog = console.log.bind(console, 'out>>');
loginInfo.onsubmit = function(e) {
if (e.preventDefault) e.preventDefault();
var jid = document.getElementById('jid').value;
var username = jid.slice(0, jid.indexOf('@'));
var server = jid.slice(jid.indexOf('@') + 1);
var url = document.getElementById('url').value;
var wsURL, boshURL, transport;
if (url.indexOf('http') === 0) {
boshURL = url;
transport = 'bosh';
} else if (url.indexOf('ws') === 0) {
wsURL = url;
transport = 'websocket';
}
client = XMPP.createClient({
jid: jid,
password: document.getElementById('password').value,
wsURL: wsURL,
boshURL: boshURL,
transport: transport
});
client.jingle.config.debug = true;
client.on('raw:incoming', function(data) {
inlog(data);
});
client.on('raw:outgoing', function(data) {
outlog(data.toString());
});
client.on('session:started', function() {
client.getRoster(function(err, resp) {
client.sendPresence();
});
document.getElementById('myJID').textContent = client.jid.full;
});
client.jingle.on('peerTrackAdded', function(session, track, stream) {
if (track.kind === 'video') {
const vid = document.getElementById('remoteVideo');
vid.srcObject = stream;
vid.muted = true;
vid.play();
}
});
client.on('jingle:incoming', function(session) {
if (session.addTrack) {
for (const stream of localMedia) {
for (const track of stream.getTracks()) {
session.addTrack(track, stream);
}
}
}
session.accept();
});
client.jingle.on('log', console.log);
client.jingle.on('sentFile', function(session, metadata) {
console.log('sent', metadata);
});
client.jingle.on('receivedFile', function(session, file, metadata) {
//saveAs(file, metadata.name); // -- https://github.com/eligrey/FileSaver.js
var href = document.getElementById('received');
href.href = URL.createObjectURL(file);
href.download = metadata.name;
var text =
'Click to download ' + metadata.name + ' (' + metadata.size + ' bytes)';
href.appendChild(document.createTextNode(text));
href.style.display = 'block';
});
navigator.mediaDevices.getUserMedia({ audio: true, video: true }).then(stream => {
const vid = document.getElementById('localVideo');
vid.srcObject = stream;
vid.muted = true;
vid.play();
localMedia.push(stream);
});
client.connect();
var callInfo = document.getElementById('callInfo');
callInfo.onsubmit = function(e) {
e.preventDefault();
var jid = document.getElementById('peer').value;
var session = client.jingle.createMediaSession(jid);
for (const stream of localMedia) {
for (const track of stream.getTracks()) {
session.addTrack(track, stream);
}
}
session.start();
return false;
};
return false;
};
// file select
function handleFileSelect(evt) {
var file = evt.target.files[0]; // FileList object
console.log('file', file.name, file.size, file.type, file.lastModifiedDate);
var jid = document.getElementById('filepeer').value;
var sess = client.jingle.createFileTransferSession(jid);
sess.start(file);
}
document.getElementById('files').addEventListener('change', handleFileSelect, false);
function takeSnapshot() {
var canvasEl = document.createElement('canvas');
var localVideoEl = document.getElementById('localVideo');
var w = 320; //localVideoEl.videoWidth;
var h = 240; //localVideoEl.videoHeight;
canvasEl.width = w;
canvasEl.height = h;
var context = canvasEl.getContext('2d');
context.fillRect(0, 0, w, h);
context.translate(w / 2, h / 2);
context.scale(-1, 1);
context.translate(w / -2, h / -2);
context.drawImage(localVideoEl, 0, 0, w, h);
// toBlob would be nice...
var url = canvasEl.toDataURL('image/jpg');
var data = url.match(/data:([^;]*);(base64)?,([0-9A-Za-z+/]+)/);
var raw = atob(data[3]);
var arr = new Uint8Array(raw.length);
for (var i = 0; i < raw.length; i++) {
arr[i] = raw.charCodeAt(i);
}
return new Blob([arr], { type: data[1] });
}
var snapshot = document.getElementById('snapshot');
snapshot.onsubmit = function(e) {
e.preventDefault();
var file = takeSnapshot();
file.name = 'snapshot-' + new Date().getTime() + '.png';
file.lastModifiedDate = new Date();
console.log('file', file.name, file.size, file.type, file.lastModifiedDate);
var jid = document.getElementById('snappeer').value;
var sess = client.jingle.createFileTransferSession(jid);
sess.start(file);
return false;
};
</script>
</body>
</html>