|
|
@@ -0,0 +1,858 @@
|
|
|
1
|
+/* jshint -W117 */
|
|
|
2
|
+// Jingle stuff
|
|
|
3
|
+function JingleSession(me, sid, connection) {
|
|
|
4
|
+ this.me = me;
|
|
|
5
|
+ this.sid = sid;
|
|
|
6
|
+ this.connection = connection;
|
|
|
7
|
+ this.initiator = null;
|
|
|
8
|
+ this.responder = null;
|
|
|
9
|
+ this.isInitiator = null;
|
|
|
10
|
+ this.peerjid = null;
|
|
|
11
|
+ this.state = null;
|
|
|
12
|
+ this.peerconnection = null;
|
|
|
13
|
+ this.remoteStream = null;
|
|
|
14
|
+ this.localSDP = null;
|
|
|
15
|
+ this.remoteSDP = null;
|
|
|
16
|
+ this.localStreams = [];
|
|
|
17
|
+ this.relayedStreams = [];
|
|
|
18
|
+ this.remoteStreams = [];
|
|
|
19
|
+ this.startTime = null;
|
|
|
20
|
+ this.stopTime = null;
|
|
|
21
|
+ this.media_constraints = null;
|
|
|
22
|
+ this.pc_constraints = null;
|
|
|
23
|
+ this.ice_config = {};
|
|
|
24
|
+ this.drip_container = [];
|
|
|
25
|
+
|
|
|
26
|
+ this.usetrickle = true;
|
|
|
27
|
+ this.usepranswer = false; // early transport warmup -- mind you, this might fail. depends on webrtc issue 1718
|
|
|
28
|
+ this.usedrip = false; // dripping is sending trickle candidates not one-by-one
|
|
|
29
|
+
|
|
|
30
|
+ this.hadstuncandidate = false;
|
|
|
31
|
+ this.hadturncandidate = false;
|
|
|
32
|
+ this.lasticecandidate = false;
|
|
|
33
|
+
|
|
|
34
|
+ this.statsinterval = null;
|
|
|
35
|
+
|
|
|
36
|
+ this.reason = null;
|
|
|
37
|
+
|
|
|
38
|
+ this.addssrc = [];
|
|
|
39
|
+ this.removessrc = [];
|
|
|
40
|
+ this.pendingop = null;
|
|
|
41
|
+
|
|
|
42
|
+ this.wait = true;
|
|
|
43
|
+}
|
|
|
44
|
+
|
|
|
45
|
+JingleSession.prototype.initiate = function (peerjid, isInitiator) {
|
|
|
46
|
+ var self = this;
|
|
|
47
|
+ if (this.state !== null) {
|
|
|
48
|
+ console.error('attempt to initiate on session ' + this.sid +
|
|
|
49
|
+ 'in state ' + this.state);
|
|
|
50
|
+ return;
|
|
|
51
|
+ }
|
|
|
52
|
+ this.isInitiator = isInitiator;
|
|
|
53
|
+ this.state = 'pending';
|
|
|
54
|
+ this.initiator = isInitiator ? this.me : peerjid;
|
|
|
55
|
+ this.responder = !isInitiator ? this.me : peerjid;
|
|
|
56
|
+ this.peerjid = peerjid;
|
|
|
57
|
+ //console.log('create PeerConnection ' + JSON.stringify(this.ice_config));
|
|
|
58
|
+ try {
|
|
|
59
|
+ this.peerconnection = new RTCPeerconnection(this.ice_config,
|
|
|
60
|
+ this.pc_constraints);
|
|
|
61
|
+ } catch (e) {
|
|
|
62
|
+ console.error('Failed to create PeerConnection, exception: ',
|
|
|
63
|
+ e.message);
|
|
|
64
|
+ console.error(e);
|
|
|
65
|
+ return;
|
|
|
66
|
+ }
|
|
|
67
|
+ this.hadstuncandidate = false;
|
|
|
68
|
+ this.hadturncandidate = false;
|
|
|
69
|
+ this.lasticecandidate = false;
|
|
|
70
|
+ this.peerconnection.onicecandidate = function (event) {
|
|
|
71
|
+ self.sendIceCandidate(event.candidate);
|
|
|
72
|
+ };
|
|
|
73
|
+ this.peerconnection.onaddstream = function (event) {
|
|
|
74
|
+ self.remoteStream = event.stream;
|
|
|
75
|
+ self.remoteStreams.push(event.stream);
|
|
|
76
|
+ $(document).trigger('remotestreamadded.jingle', [event, self.sid]);
|
|
|
77
|
+ };
|
|
|
78
|
+ this.peerconnection.onremovestream = function (event) {
|
|
|
79
|
+ self.remoteStream = null;
|
|
|
80
|
+ // FIXME: remove from this.remoteStreams
|
|
|
81
|
+ $(document).trigger('remotestreamremoved.jingle', [event, self.sid]);
|
|
|
82
|
+ };
|
|
|
83
|
+ this.peerconnection.onsignalingstatechange = function (event) {
|
|
|
84
|
+ if (!(self && self.peerconnection)) return;
|
|
|
85
|
+ };
|
|
|
86
|
+ this.peerconnection.oniceconnectionstatechange = function (event) {
|
|
|
87
|
+ if (!(self && self.peerconnection)) return;
|
|
|
88
|
+ switch (self.peerconnection.iceConnectionState) {
|
|
|
89
|
+ case 'connected':
|
|
|
90
|
+ this.startTime = new Date();
|
|
|
91
|
+ break;
|
|
|
92
|
+ case 'disconnected':
|
|
|
93
|
+ this.stopTime = new Date();
|
|
|
94
|
+ break;
|
|
|
95
|
+ }
|
|
|
96
|
+ $(document).trigger('iceconnectionstatechange.jingle', [self.sid, self]);
|
|
|
97
|
+ };
|
|
|
98
|
+ // add any local and relayed stream
|
|
|
99
|
+ this.localStreams.forEach(function(stream) {
|
|
|
100
|
+ self.peerconnection.addStream(stream);
|
|
|
101
|
+ });
|
|
|
102
|
+ this.relayedStreams.forEach(function(stream) {
|
|
|
103
|
+ self.peerconnection.addStream(stream);
|
|
|
104
|
+ });
|
|
|
105
|
+};
|
|
|
106
|
+
|
|
|
107
|
+JingleSession.prototype.accept = function () {
|
|
|
108
|
+ var self = this;
|
|
|
109
|
+ this.state = 'active';
|
|
|
110
|
+
|
|
|
111
|
+ var pranswer = this.peerconnection.localDescription;
|
|
|
112
|
+ if (!pranswer || pranswer.type != 'pranswer') {
|
|
|
113
|
+ return;
|
|
|
114
|
+ }
|
|
|
115
|
+ console.log('going from pranswer to answer');
|
|
|
116
|
+ if (this.usetrickle) {
|
|
|
117
|
+ // remove candidates already sent from session-accept
|
|
|
118
|
+ var lines = SDPUtil.find_lines(pranswer.sdp, 'a=candidate:');
|
|
|
119
|
+ for (var i = 0; i < lines.length; i++) {
|
|
|
120
|
+ pranswer.sdp = pranswer.sdp.replace(lines[i] + '\r\n', '');
|
|
|
121
|
+ }
|
|
|
122
|
+ }
|
|
|
123
|
+ while (SDPUtil.find_line(pranswer.sdp, 'a=inactive')) {
|
|
|
124
|
+ // FIXME: change any inactive to sendrecv or whatever they were originally
|
|
|
125
|
+ pranswer.sdp = pranswer.sdp.replace('a=inactive', 'a=sendrecv');
|
|
|
126
|
+ }
|
|
|
127
|
+ var prsdp = new SDP(pranswer.sdp);
|
|
|
128
|
+ var accept = $iq({to: this.peerjid,
|
|
|
129
|
+ type: 'set'})
|
|
|
130
|
+ .c('jingle', {xmlns: 'urn:xmpp:jingle:1',
|
|
|
131
|
+ action: 'session-accept',
|
|
|
132
|
+ initiator: this.initiator,
|
|
|
133
|
+ responder: this.responder,
|
|
|
134
|
+ sid: this.sid });
|
|
|
135
|
+ prsdp.toJingle(accept, this.initiator == this.me ? 'initiator' : 'responder');
|
|
|
136
|
+ this.connection.sendIQ(accept,
|
|
|
137
|
+ function () {
|
|
|
138
|
+ var ack = {};
|
|
|
139
|
+ ack.source = 'answer';
|
|
|
140
|
+ $(document).trigger('ack.jingle', [self.sid, ack]);
|
|
|
141
|
+ },
|
|
|
142
|
+ function (stanza) {
|
|
|
143
|
+ var error = ($(stanza).find('error').length) ? {
|
|
|
144
|
+ code: $(stanza).find('error').attr('code'),
|
|
|
145
|
+ reason: $(stanza).find('error :first')[0].tagName,
|
|
|
146
|
+ }:{};
|
|
|
147
|
+ error.source = 'answer';
|
|
|
148
|
+ $(document).trigger('error.jingle', [self.sid, error]);
|
|
|
149
|
+ },
|
|
|
150
|
+ 10000);
|
|
|
151
|
+
|
|
|
152
|
+ var sdp = this.peerconnection.localDescription.sdp;
|
|
|
153
|
+ while (SDPUtil.find_line(sdp, 'a=inactive')) {
|
|
|
154
|
+ // FIXME: change any inactive to sendrecv or whatever they were originally
|
|
|
155
|
+ sdp = sdp.replace('a=inactive', 'a=sendrecv');
|
|
|
156
|
+ }
|
|
|
157
|
+ this.peerconnection.setLocalDescription(new RTCSessionDescription({type: 'answer', sdp: sdp}),
|
|
|
158
|
+ function () {
|
|
|
159
|
+ //console.log('setLocalDescription success');
|
|
|
160
|
+ $(document).trigger('setLocalDescription.jingle', [self.sid]);
|
|
|
161
|
+ },
|
|
|
162
|
+ function (e) {
|
|
|
163
|
+ console.error('setLocalDescription failed', e);
|
|
|
164
|
+ }
|
|
|
165
|
+ );
|
|
|
166
|
+};
|
|
|
167
|
+
|
|
|
168
|
+JingleSession.prototype.terminate = function (reason) {
|
|
|
169
|
+ this.state = 'ended';
|
|
|
170
|
+ this.reason = reason;
|
|
|
171
|
+ this.peerconnection.close();
|
|
|
172
|
+ if (this.statsinterval !== null) {
|
|
|
173
|
+ window.clearInterval(this.statsinterval);
|
|
|
174
|
+ this.statsinterval = null;
|
|
|
175
|
+ }
|
|
|
176
|
+};
|
|
|
177
|
+
|
|
|
178
|
+JingleSession.prototype.active = function () {
|
|
|
179
|
+ return this.state == 'active';
|
|
|
180
|
+};
|
|
|
181
|
+
|
|
|
182
|
+JingleSession.prototype.sendIceCandidate = function (candidate) {
|
|
|
183
|
+ var self = this;
|
|
|
184
|
+ if (candidate && !this.lasticecandidate) {
|
|
|
185
|
+ var ice = SDPUtil.iceparams(this.localSDP.media[candidate.sdpMLineIndex], this.localSDP.session);
|
|
|
186
|
+ var jcand = SDPUtil.candidateToJingle(candidate.candidate);
|
|
|
187
|
+ if (!(ice && jcand)) {
|
|
|
188
|
+ console.error('failed to get ice && jcand');
|
|
|
189
|
+ return;
|
|
|
190
|
+ }
|
|
|
191
|
+ ice.xmlns = 'urn:xmpp:jingle:transports:ice-udp:1';
|
|
|
192
|
+
|
|
|
193
|
+ if (jcand.type === 'srflx') {
|
|
|
194
|
+ this.hadstuncandidate = true;
|
|
|
195
|
+ } else if (jcand.type === 'relay') {
|
|
|
196
|
+ this.hadturncandidate = true;
|
|
|
197
|
+ }
|
|
|
198
|
+
|
|
|
199
|
+ if (this.usetrickle) {
|
|
|
200
|
+ if (this.usedrip) {
|
|
|
201
|
+ if (this.drip_container.length === 0) {
|
|
|
202
|
+ // start 20ms callout
|
|
|
203
|
+ window.setTimeout(function () {
|
|
|
204
|
+ if (self.drip_container.length === 0) return;
|
|
|
205
|
+ self.sendIceCandidates(self.drip_container);
|
|
|
206
|
+ self.drip_container = [];
|
|
|
207
|
+ }, 20);
|
|
|
208
|
+
|
|
|
209
|
+ }
|
|
|
210
|
+ this.drip_container.push(event.candidate);
|
|
|
211
|
+ return;
|
|
|
212
|
+ } else {
|
|
|
213
|
+ self.sendIceCandidate([event.candidate]);
|
|
|
214
|
+ }
|
|
|
215
|
+ }
|
|
|
216
|
+ } else {
|
|
|
217
|
+ //console.log('sendIceCandidate: last candidate.');
|
|
|
218
|
+ if (!this.usetrickle) {
|
|
|
219
|
+ //console.log('should send full offer now...');
|
|
|
220
|
+ var init = $iq({to: this.peerjid,
|
|
|
221
|
+ type: 'set'})
|
|
|
222
|
+ .c('jingle', {xmlns: 'urn:xmpp:jingle:1',
|
|
|
223
|
+ action: this.peerconnection.localDescription.type == 'offer' ? 'session-initiate' : 'session-accept',
|
|
|
224
|
+ initiator: this.initiator,
|
|
|
225
|
+ sid: this.sid});
|
|
|
226
|
+ this.localSDP = new SDP(this.peerconnection.localDescription.sdp);
|
|
|
227
|
+ this.localSDP.toJingle(init, this.initiator == this.me ? 'initiator' : 'responder');
|
|
|
228
|
+ this.connection.sendIQ(init,
|
|
|
229
|
+ function () {
|
|
|
230
|
+ //console.log('session initiate ack');
|
|
|
231
|
+ var ack = {};
|
|
|
232
|
+ ack.source = 'offer';
|
|
|
233
|
+ $(document).trigger('ack.jingle', [self.sid, ack]);
|
|
|
234
|
+ },
|
|
|
235
|
+ function (stanza) {
|
|
|
236
|
+ self.state = 'error';
|
|
|
237
|
+ self.peerconnection.close();
|
|
|
238
|
+ var error = ($(stanza).find('error').length) ? {
|
|
|
239
|
+ code: $(stanza).find('error').attr('code'),
|
|
|
240
|
+ reason: $(stanza).find('error :first')[0].tagName,
|
|
|
241
|
+ }:{};
|
|
|
242
|
+ error.source = 'offer';
|
|
|
243
|
+ $(document).trigger('error.jingle', [self.sid, error]);
|
|
|
244
|
+ },
|
|
|
245
|
+ 10000);
|
|
|
246
|
+ }
|
|
|
247
|
+ this.lasticecandidate = true;
|
|
|
248
|
+ console.log('Have we encountered any srflx candidates? ' + this.hadstuncandidate);
|
|
|
249
|
+ console.log('Have we encountered any relay candidates? ' + this.hadturncandidate);
|
|
|
250
|
+
|
|
|
251
|
+ if (!(this.hadstuncandidate || this.hadturncandidate) && this.peerconnection.signalingState != 'closed') {
|
|
|
252
|
+ $(document).trigger('nostuncandidates.jingle', [this.sid]);
|
|
|
253
|
+ }
|
|
|
254
|
+ }
|
|
|
255
|
+};
|
|
|
256
|
+
|
|
|
257
|
+JingleSession.prototype.sendIceCandidates = function (candidates) {
|
|
|
258
|
+ console.log('sendIceCandidates', candidates);
|
|
|
259
|
+ var cand = $iq({to: this.peerjid, type: 'set'})
|
|
|
260
|
+ .c('jingle', {xmlns: 'urn:xmpp:jingle:1',
|
|
|
261
|
+ action: 'transport-info',
|
|
|
262
|
+ initiator: this.initiator,
|
|
|
263
|
+ sid: this.sid});
|
|
|
264
|
+ for (var mid = 0; mid < this.localSDP.media.length; mid++) {
|
|
|
265
|
+ var cands = candidates.filter(function (el) { return el.sdpMLineIndex == mid; });
|
|
|
266
|
+ if (cands.length > 0) {
|
|
|
267
|
+ var ice = SDPUtil.iceparams(this.localSDP.media[mid], this.localSDP.session);
|
|
|
268
|
+ ice.xmlns = 'urn:xmpp:jingle:transports:ice-udp:1';
|
|
|
269
|
+ cand.c('content', {creator: this.initiator == this.me ? 'initiator' : 'responder',
|
|
|
270
|
+ name: cands[0].sdpMid
|
|
|
271
|
+ }).c('transport', ice);
|
|
|
272
|
+ for (var i = 0; i < cands.length; i++) {
|
|
|
273
|
+ cand.c('candidate', SDPUtil.candidateToJingle(cands[i].candidate)).up();
|
|
|
274
|
+ }
|
|
|
275
|
+ // add fingerprint
|
|
|
276
|
+ if (SDPUtil.find_line(this.localSDP.media[mid], 'a=fingerprint:', this.localSDP.session)) {
|
|
|
277
|
+ var tmp = SDPUtil.parse_fingerprint(SDPUtil.find_line(this.localSDP.media[mid], 'a=fingerprint:', this.localSDP.session));
|
|
|
278
|
+ tmp.required = true;
|
|
|
279
|
+ cand.c('fingerprint').t(tmp.fingerprint);
|
|
|
280
|
+ delete tmp.fingerprint;
|
|
|
281
|
+ cand.attrs(tmp);
|
|
|
282
|
+ cand.up();
|
|
|
283
|
+ }
|
|
|
284
|
+ cand.up(); // transport
|
|
|
285
|
+ cand.up(); // content
|
|
|
286
|
+ }
|
|
|
287
|
+ }
|
|
|
288
|
+ // might merge last-candidate notification into this, but it is called alot later. See webrtc issue #2340
|
|
|
289
|
+ //console.log('was this the last candidate', this.lasticecandidate);
|
|
|
290
|
+ this.connection.sendIQ(cand,
|
|
|
291
|
+ function () {
|
|
|
292
|
+ var ack = {};
|
|
|
293
|
+ ack.source = 'transportinfo';
|
|
|
294
|
+ $(document).trigger('ack.jingle', [this.sid, ack]);
|
|
|
295
|
+ },
|
|
|
296
|
+ function (stanza) {
|
|
|
297
|
+ var error = ($(stanza).find('error').length) ? {
|
|
|
298
|
+ code: $(stanza).find('error').attr('code'),
|
|
|
299
|
+ reason: $(stanza).find('error :first')[0].tagName,
|
|
|
300
|
+ }:{};
|
|
|
301
|
+ error.source = 'transportinfo';
|
|
|
302
|
+ $(document).trigger('error.jingle', [this.sid, error]);
|
|
|
303
|
+ },
|
|
|
304
|
+ 10000);
|
|
|
305
|
+};
|
|
|
306
|
+
|
|
|
307
|
+
|
|
|
308
|
+JingleSession.prototype.sendOffer = function () {
|
|
|
309
|
+ //console.log('sendOffer...');
|
|
|
310
|
+ var self = this;
|
|
|
311
|
+ this.peerconnection.createOffer(function (sdp) {
|
|
|
312
|
+ self.createdOffer(sdp);
|
|
|
313
|
+ },
|
|
|
314
|
+ function (e) {
|
|
|
315
|
+ console.error('createOffer failed', e);
|
|
|
316
|
+ },
|
|
|
317
|
+ this.media_constraints
|
|
|
318
|
+ );
|
|
|
319
|
+};
|
|
|
320
|
+
|
|
|
321
|
+JingleSession.prototype.createdOffer = function (sdp) {
|
|
|
322
|
+ //console.log('createdOffer', sdp);
|
|
|
323
|
+ var self = this;
|
|
|
324
|
+ this.localSDP = new SDP(sdp.sdp);
|
|
|
325
|
+ //this.localSDP.mangle();
|
|
|
326
|
+ if (this.usetrickle) {
|
|
|
327
|
+ var init = $iq({to: this.peerjid,
|
|
|
328
|
+ type: 'set'})
|
|
|
329
|
+ .c('jingle', {xmlns: 'urn:xmpp:jingle:1',
|
|
|
330
|
+ action: 'session-initiate',
|
|
|
331
|
+ initiator: this.initiator,
|
|
|
332
|
+ sid: this.sid});
|
|
|
333
|
+ this.localSDP.toJingle(init, this.initiator == this.me ? 'initiator' : 'responder');
|
|
|
334
|
+ this.connection.sendIQ(init,
|
|
|
335
|
+ function () {
|
|
|
336
|
+ var ack = {};
|
|
|
337
|
+ ack.source = 'offer';
|
|
|
338
|
+ $(document).trigger('ack.jingle', [self.sid, ack]);
|
|
|
339
|
+ },
|
|
|
340
|
+ function (stanza) {
|
|
|
341
|
+ self.state = 'error';
|
|
|
342
|
+ self.peerconnection.close();
|
|
|
343
|
+ var error = ($(stanza).find('error').length) ? {
|
|
|
344
|
+ code: $(stanza).find('error').attr('code'),
|
|
|
345
|
+ reason: $(stanza).find('error :first')[0].tagName,
|
|
|
346
|
+ }:{};
|
|
|
347
|
+ error.source = 'offer';
|
|
|
348
|
+ $(document).trigger('error.jingle', [self.sid, error]);
|
|
|
349
|
+ },
|
|
|
350
|
+ 10000);
|
|
|
351
|
+ }
|
|
|
352
|
+ sdp.sdp = this.localSDP.raw;
|
|
|
353
|
+ this.peerconnection.setLocalDescription(sdp,
|
|
|
354
|
+ function () {
|
|
|
355
|
+ $(document).trigger('setLocalDescription.jingle', [self.sid]);
|
|
|
356
|
+ //console.log('setLocalDescription success');
|
|
|
357
|
+ },
|
|
|
358
|
+ function (e) {
|
|
|
359
|
+ console.error('setLocalDescription failed', e);
|
|
|
360
|
+ }
|
|
|
361
|
+ );
|
|
|
362
|
+ var cands = SDPUtil.find_lines(this.localSDP.raw, 'a=candidate:');
|
|
|
363
|
+ for (var i = 0; i < cands.length; i++) {
|
|
|
364
|
+ var cand = SDPUtil.parse_icecandidate(cands[i]);
|
|
|
365
|
+ if (cand.type == 'srflx') {
|
|
|
366
|
+ this.hadstuncandidate = true;
|
|
|
367
|
+ } else if (cand.type == 'relay') {
|
|
|
368
|
+ this.hadturncandidate = true;
|
|
|
369
|
+ }
|
|
|
370
|
+ }
|
|
|
371
|
+};
|
|
|
372
|
+
|
|
|
373
|
+JingleSession.prototype.setRemoteDescription = function (elem, desctype) {
|
|
|
374
|
+ //console.log('setting remote description... ', desctype);
|
|
|
375
|
+ this.remoteSDP = new SDP('');
|
|
|
376
|
+ this.remoteSDP.fromJingle(elem);
|
|
|
377
|
+ if (this.peerconnection.remoteDescription !== null) {
|
|
|
378
|
+ console.log('setRemoteDescription when remote description is not null, should be pranswer', this.peerconnection.remoteDescription);
|
|
|
379
|
+ if (this.peerconnection.remoteDescription.type == 'pranswer') {
|
|
|
380
|
+ var pranswer = new SDP(this.peerconnection.remoteDescription.sdp);
|
|
|
381
|
+ for (var i = 0; i < pranswer.media.length; i++) {
|
|
|
382
|
+ // make sure we have ice ufrag and pwd
|
|
|
383
|
+ if (!SDPUtil.find_line(this.remoteSDP.media[i], 'a=ice-ufrag:', this.remoteSDP.session)) {
|
|
|
384
|
+ if (SDPUtil.find_line(pranswer.media[i], 'a=ice-ufrag:', pranswer.session)) {
|
|
|
385
|
+ this.remoteSDP.media[i] += SDPUtil.find_line(pranswer.media[i], 'a=ice-ufrag:', pranswer.session) + '\r\n';
|
|
|
386
|
+ } else {
|
|
|
387
|
+ console.warn('no ice ufrag?');
|
|
|
388
|
+ }
|
|
|
389
|
+ if (SDPUtil.find_line(pranswer.media[i], 'a=ice-pwd:', pranswer.session)) {
|
|
|
390
|
+ this.remoteSDP.media[i] += SDPUtil.find_line(pranswer.media[i], 'a=ice-pwd:', pranswer.session) + '\r\n';
|
|
|
391
|
+ } else {
|
|
|
392
|
+ console.warn('no ice pwd?');
|
|
|
393
|
+ }
|
|
|
394
|
+ }
|
|
|
395
|
+ // copy over candidates
|
|
|
396
|
+ var lines = SDPUtil.find_lines(pranswer.media[i], 'a=candidate:');
|
|
|
397
|
+ for (var j = 0; j < lines.length; j++) {
|
|
|
398
|
+ this.remoteSDP.media[i] += lines[j] + '\r\n';
|
|
|
399
|
+ }
|
|
|
400
|
+ }
|
|
|
401
|
+ this.remoteSDP.raw = this.remoteSDP.session + this.remoteSDP.media.join('');
|
|
|
402
|
+ }
|
|
|
403
|
+ }
|
|
|
404
|
+ var remotedesc = new RTCSessionDescription({type: desctype, sdp: this.remoteSDP.raw});
|
|
|
405
|
+
|
|
|
406
|
+ this.peerconnection.setRemoteDescription(remotedesc,
|
|
|
407
|
+ function () {
|
|
|
408
|
+ //console.log('setRemoteDescription success');
|
|
|
409
|
+ },
|
|
|
410
|
+ function (e) {
|
|
|
411
|
+ console.error('setRemoteDescription error', e);
|
|
|
412
|
+ }
|
|
|
413
|
+ );
|
|
|
414
|
+};
|
|
|
415
|
+
|
|
|
416
|
+JingleSession.prototype.addIceCandidate = function (elem) {
|
|
|
417
|
+ var self = this;
|
|
|
418
|
+ if (this.peerconnection.signalingState == 'closed') {
|
|
|
419
|
+ return;
|
|
|
420
|
+ }
|
|
|
421
|
+ if (!this.peerconnection.remoteDescription && this.peerconnection.signalingState == 'have-local-offer') {
|
|
|
422
|
+ console.log('trickle ice candidate arriving before session accept...');
|
|
|
423
|
+ // create a PRANSWER for setRemoteDescription
|
|
|
424
|
+ if (!this.remoteSDP) {
|
|
|
425
|
+ var cobbled = 'v=0\r\n' +
|
|
|
426
|
+ 'o=- ' + '1923518516' + ' 2 IN IP4 0.0.0.0\r\n' +// FIXME
|
|
|
427
|
+ 's=-\r\n' +
|
|
|
428
|
+ 't=0 0\r\n';
|
|
|
429
|
+ // first, take some things from the local description
|
|
|
430
|
+ for (var i = 0; i < this.localSDP.media.length; i++) {
|
|
|
431
|
+ cobbled += SDPUtil.find_line(this.localSDP.media[i], 'm=') + '\r\n';
|
|
|
432
|
+ cobbled += SDPUtil.find_lines(this.localSDP.media[i], 'a=rtpmap:').join('\r\n') + '\r\n';
|
|
|
433
|
+ if (SDPUtil.find_line(this.localSDP.media[i], 'a=mid:')) {
|
|
|
434
|
+ cobbled += SDPUtil.find_line(this.localSDP.media[i], 'a=mid:') + '\r\n';
|
|
|
435
|
+ }
|
|
|
436
|
+ cobbled += 'a=inactive\r\n';
|
|
|
437
|
+ }
|
|
|
438
|
+ this.remoteSDP = new SDP(cobbled);
|
|
|
439
|
+ }
|
|
|
440
|
+ // then add things like ice and dtls from remote candidate
|
|
|
441
|
+ elem.each(function () {
|
|
|
442
|
+ for (var i = 0; i < self.remoteSDP.media.length; i++) {
|
|
|
443
|
+ if (SDPUtil.find_line(self.remoteSDP.media[i], 'a=mid:' + $(this).attr('name')) ||
|
|
|
444
|
+ self.remoteSDP.media[i].indexOf('m=' + $(this).attr('name')) === 0) {
|
|
|
445
|
+ if (!SDPUtil.find_line(self.remoteSDP.media[i], 'a=ice-ufrag:')) {
|
|
|
446
|
+ var tmp = $(this).find('transport');
|
|
|
447
|
+ self.remoteSDP.media[i] += 'a=ice-ufrag:' + tmp.attr('ufrag') + '\r\n';
|
|
|
448
|
+ self.remoteSDP.media[i] += 'a=ice-pwd:' + tmp.attr('pwd') + '\r\n';
|
|
|
449
|
+ tmp = $(this).find('transport>fingerprint');
|
|
|
450
|
+ if (tmp.length) {
|
|
|
451
|
+ self.remoteSDP.media[i] += 'a=fingerprint:' + tmp.attr('hash') + ' ' + tmp.text() + '\r\n';
|
|
|
452
|
+ } else {
|
|
|
453
|
+ console.log('no dtls fingerprint (webrtc issue #1718?)');
|
|
|
454
|
+ self.remoteSDP.media[i] += 'a=crypto:1 AES_CM_128_HMAC_SHA1_80 inline:BAADBAADBAADBAADBAADBAADBAADBAADBAADBAAD\r\n';
|
|
|
455
|
+ }
|
|
|
456
|
+ break;
|
|
|
457
|
+ }
|
|
|
458
|
+ }
|
|
|
459
|
+ }
|
|
|
460
|
+ });
|
|
|
461
|
+ this.remoteSDP.raw = this.remoteSDP.session + this.remoteSDP.media.join('');
|
|
|
462
|
+
|
|
|
463
|
+ // we need a complete SDP with ice-ufrag/ice-pwd in all parts
|
|
|
464
|
+ // this makes the assumption that the PRANSWER is constructed such that the ice-ufrag is in all mediaparts
|
|
|
465
|
+ // but it could be in the session part as well. since the code above constructs this sdp this can't happen however
|
|
|
466
|
+ var iscomplete = this.remoteSDP.media.filter(function (mediapart) {
|
|
|
467
|
+ return SDPUtil.find_line(mediapart, 'a=ice-ufrag:');
|
|
|
468
|
+ }).length == this.remoteSDP.media.length;
|
|
|
469
|
+
|
|
|
470
|
+ if (iscomplete) {
|
|
|
471
|
+ console.log('setting pranswer');
|
|
|
472
|
+ try {
|
|
|
473
|
+ this.peerconnection.setRemoteDescription(new RTCSessionDescription({type: 'pranswer', sdp: this.remoteSDP.raw }),
|
|
|
474
|
+ function() {
|
|
|
475
|
+ },
|
|
|
476
|
+ function(e) {
|
|
|
477
|
+ console.log('setRemoteDescription pranswer failed', e.toString());
|
|
|
478
|
+ });
|
|
|
479
|
+ } catch (e) {
|
|
|
480
|
+ console.error('setting pranswer failed', e);
|
|
|
481
|
+ }
|
|
|
482
|
+ } else {
|
|
|
483
|
+ //console.log('not yet setting pranswer');
|
|
|
484
|
+ }
|
|
|
485
|
+ }
|
|
|
486
|
+ // operate on each content element
|
|
|
487
|
+ elem.each(function () {
|
|
|
488
|
+ // would love to deactivate this, but firefox still requires it
|
|
|
489
|
+ var idx = -1;
|
|
|
490
|
+ var i;
|
|
|
491
|
+ for (i = 0; i < self.remoteSDP.media.length; i++) {
|
|
|
492
|
+ if (SDPUtil.find_line(self.remoteSDP.media[i], 'a=mid:' + $(this).attr('name')) ||
|
|
|
493
|
+ self.remoteSDP.media[i].indexOf('m=' + $(this).attr('name')) === 0) {
|
|
|
494
|
+ idx = i;
|
|
|
495
|
+ break;
|
|
|
496
|
+ }
|
|
|
497
|
+ }
|
|
|
498
|
+ if (idx == -1) { // fall back to localdescription
|
|
|
499
|
+ for (i = 0; i < self.localSDP.media.length; i++) {
|
|
|
500
|
+ if (SDPUtil.find_line(self.localSDP.media[i], 'a=mid:' + $(this).attr('name')) ||
|
|
|
501
|
+ self.localSDP.media[i].indexOf('m=' + $(this).attr('name')) === 0) {
|
|
|
502
|
+ idx = i;
|
|
|
503
|
+ break;
|
|
|
504
|
+ }
|
|
|
505
|
+ }
|
|
|
506
|
+ }
|
|
|
507
|
+ var name = $(this).attr('name');
|
|
|
508
|
+ // TODO: check ice-pwd and ice-ufrag?
|
|
|
509
|
+ $(this).find('transport>candidate').each(function () {
|
|
|
510
|
+ var line, candidate;
|
|
|
511
|
+ line = SDPUtil.candidateFromJingle(this);
|
|
|
512
|
+ candidate = new RTCIceCandidate({sdpMLineIndex: idx,
|
|
|
513
|
+ sdpMid: name,
|
|
|
514
|
+ candidate: line});
|
|
|
515
|
+ try {
|
|
|
516
|
+ self.peerconnection.addIceCandidate(candidate);
|
|
|
517
|
+ } catch (e) {
|
|
|
518
|
+ console.error('addIceCandidate failed', e.toString(), line);
|
|
|
519
|
+ }
|
|
|
520
|
+ });
|
|
|
521
|
+ });
|
|
|
522
|
+};
|
|
|
523
|
+
|
|
|
524
|
+JingleSession.prototype.sendAnswer = function (provisional) {
|
|
|
525
|
+ //console.log('createAnswer', provisional);
|
|
|
526
|
+ var self = this;
|
|
|
527
|
+ this.peerconnection.createAnswer(
|
|
|
528
|
+ function (sdp) {
|
|
|
529
|
+ self.createdAnswer(sdp, provisional);
|
|
|
530
|
+ },
|
|
|
531
|
+ function (e) {
|
|
|
532
|
+ console.error('createAnswer failed', e);
|
|
|
533
|
+ },
|
|
|
534
|
+ this.media_constraints
|
|
|
535
|
+ );
|
|
|
536
|
+};
|
|
|
537
|
+
|
|
|
538
|
+JingleSession.prototype.createdAnswer = function (sdp, provisional) {
|
|
|
539
|
+ //console.log('createAnswer callback');
|
|
|
540
|
+ var self = this;
|
|
|
541
|
+ this.localSDP = new SDP(sdp.sdp);
|
|
|
542
|
+ //this.localSDP.mangle();
|
|
|
543
|
+ this.usepranswer = provisional === true;
|
|
|
544
|
+ if (this.usetrickle) {
|
|
|
545
|
+ if (!this.usepranswer) {
|
|
|
546
|
+ var accept = $iq({to: this.peerjid,
|
|
|
547
|
+ type: 'set'})
|
|
|
548
|
+ .c('jingle', {xmlns: 'urn:xmpp:jingle:1',
|
|
|
549
|
+ action: 'session-accept',
|
|
|
550
|
+ initiator: this.initiator,
|
|
|
551
|
+ responder: this.responder,
|
|
|
552
|
+ sid: this.sid });
|
|
|
553
|
+ this.localSDP.toJingle(accept, this.initiator == this.me ? 'initiator' : 'responder');
|
|
|
554
|
+ this.connection.sendIQ(accept,
|
|
|
555
|
+ function () {
|
|
|
556
|
+ var ack = {};
|
|
|
557
|
+ ack.source = 'answer';
|
|
|
558
|
+ $(document).trigger('ack.jingle', [self.sid, ack]);
|
|
|
559
|
+ },
|
|
|
560
|
+ function (stanza) {
|
|
|
561
|
+ var error = ($(stanza).find('error').length) ? {
|
|
|
562
|
+ code: $(stanza).find('error').attr('code'),
|
|
|
563
|
+ reason: $(stanza).find('error :first')[0].tagName,
|
|
|
564
|
+ }:{};
|
|
|
565
|
+ error.source = 'answer';
|
|
|
566
|
+ $(document).trigger('error.jingle', [self.sid, error]);
|
|
|
567
|
+ },
|
|
|
568
|
+ 10000);
|
|
|
569
|
+ } else {
|
|
|
570
|
+ sdp.type = 'pranswer';
|
|
|
571
|
+ for (var i = 0; i < this.localSDP.media.length; i++) {
|
|
|
572
|
+ this.localSDP.media[i] = this.localSDP.media[i].replace('a=sendrecv\r\n', 'a=inactive\r\n');
|
|
|
573
|
+ }
|
|
|
574
|
+ this.localSDP.raw = this.localSDP.session + '\r\n' + this.localSDP.media.join('');
|
|
|
575
|
+ }
|
|
|
576
|
+ }
|
|
|
577
|
+ sdp.sdp = this.localSDP.raw;
|
|
|
578
|
+ this.peerconnection.setLocalDescription(sdp,
|
|
|
579
|
+ function () {
|
|
|
580
|
+ $(document).trigger('setLocalDescription.jingle', [self.sid]);
|
|
|
581
|
+ //console.log('setLocalDescription success');
|
|
|
582
|
+ },
|
|
|
583
|
+ function (e) {
|
|
|
584
|
+ console.error('setLocalDescription failed', e);
|
|
|
585
|
+ }
|
|
|
586
|
+ );
|
|
|
587
|
+ var cands = SDPUtil.find_lines(this.localSDP.raw, 'a=candidate:');
|
|
|
588
|
+ for (var j = 0; j < cands.length; j++) {
|
|
|
589
|
+ var cand = SDPUtil.parse_icecandidate(cands[j]);
|
|
|
590
|
+ if (cand.type == 'srflx') {
|
|
|
591
|
+ this.hadstuncandidate = true;
|
|
|
592
|
+ } else if (cand.type == 'relay') {
|
|
|
593
|
+ this.hadturncandidate = true;
|
|
|
594
|
+ }
|
|
|
595
|
+ }
|
|
|
596
|
+};
|
|
|
597
|
+
|
|
|
598
|
+JingleSession.prototype.sendTerminate = function (reason, text) {
|
|
|
599
|
+ var self = this,
|
|
|
600
|
+ term = $iq({to: this.peerjid,
|
|
|
601
|
+ type: 'set'})
|
|
|
602
|
+ .c('jingle', {xmlns: 'urn:xmpp:jingle:1',
|
|
|
603
|
+ action: 'session-terminate',
|
|
|
604
|
+ initiator: this.initiator,
|
|
|
605
|
+ sid: this.sid})
|
|
|
606
|
+ .c('reason')
|
|
|
607
|
+ .c(reason || 'success');
|
|
|
608
|
+
|
|
|
609
|
+ if (text) {
|
|
|
610
|
+ term.up().c('text').t(text);
|
|
|
611
|
+ }
|
|
|
612
|
+
|
|
|
613
|
+ this.connection.sendIQ(term,
|
|
|
614
|
+ function () {
|
|
|
615
|
+ self.peerconnection.close();
|
|
|
616
|
+ self.peerconnection = null;
|
|
|
617
|
+ self.terminate();
|
|
|
618
|
+ var ack = {};
|
|
|
619
|
+ ack.source = 'terminate';
|
|
|
620
|
+ $(document).trigger('ack.jingle', [self.sid, ack]);
|
|
|
621
|
+ },
|
|
|
622
|
+ function (stanza) {
|
|
|
623
|
+ var error = ($(stanza).find('error').length) ? {
|
|
|
624
|
+ code: $(stanza).find('error').attr('code'),
|
|
|
625
|
+ reason: $(stanza).find('error :first')[0].tagName,
|
|
|
626
|
+ }:{};
|
|
|
627
|
+ $(document).trigger('ack.jingle', [self.sid, error]);
|
|
|
628
|
+ },
|
|
|
629
|
+ 10000);
|
|
|
630
|
+ if (this.statsinterval !== null) {
|
|
|
631
|
+ window.clearInterval(this.statsinterval);
|
|
|
632
|
+ this.statsinterval = null;
|
|
|
633
|
+ }
|
|
|
634
|
+};
|
|
|
635
|
+
|
|
|
636
|
+
|
|
|
637
|
+JingleSession.prototype.addSource = function (elem) {
|
|
|
638
|
+ console.log('addssrc', new Date().getTime());
|
|
|
639
|
+ console.log('ice', this.peerconnection.iceConnectionState);
|
|
|
640
|
+ var sdp = new SDP(this.peerconnection.remoteDescription.sdp);
|
|
|
641
|
+
|
|
|
642
|
+ var self = this;
|
|
|
643
|
+ $(elem).each(function (idx, content) {
|
|
|
644
|
+ var name = $(content).attr('name');
|
|
|
645
|
+ var lines = '';
|
|
|
646
|
+ tmp = $(content).find('>source[xmlns="urn:xmpp:jingle:apps:rtp:ssma:0"]');
|
|
|
647
|
+ tmp.each(function () {
|
|
|
648
|
+ var ssrc = $(this).attr('ssrc');
|
|
|
649
|
+ $(this).find('>parameter').each(function () {
|
|
|
650
|
+ lines += 'a=ssrc:' + ssrc + ' ' + $(this).attr('name');
|
|
|
651
|
+ if ($(this).attr('value') && $(this).attr('value').length)
|
|
|
652
|
+ lines += ':' + $(this).attr('value');
|
|
|
653
|
+ lines += '\r\n';
|
|
|
654
|
+ });
|
|
|
655
|
+ });
|
|
|
656
|
+ sdp.media.forEach(function(media, idx) {
|
|
|
657
|
+ if (!SDPUtil.find_line(media, 'a=mid:' + name))
|
|
|
658
|
+ return;
|
|
|
659
|
+ sdp.media[idx] += lines;
|
|
|
660
|
+ if (!self.addssrc[idx]) self.addssrc[idx] = '';
|
|
|
661
|
+ self.addssrc[idx] += lines;
|
|
|
662
|
+ });
|
|
|
663
|
+ sdp.raw = sdp.session + sdp.media.join('');
|
|
|
664
|
+ });
|
|
|
665
|
+ this.modifySources();
|
|
|
666
|
+};
|
|
|
667
|
+
|
|
|
668
|
+JingleSession.prototype.removeSource = function (elem) {
|
|
|
669
|
+ console.log('removessrc', new Date().getTime());
|
|
|
670
|
+ console.log('ice', this.peerconnection.iceConnectionState);
|
|
|
671
|
+ var sdp = new SDP(this.peerconnection.remoteDescription.sdp);
|
|
|
672
|
+
|
|
|
673
|
+ var self = this;
|
|
|
674
|
+ $(elem).each(function (idx, content) {
|
|
|
675
|
+ var name = $(content).attr('name');
|
|
|
676
|
+ var lines = '';
|
|
|
677
|
+ tmp = $(content).find('>source[xmlns="urn:xmpp:jingle:apps:rtp:ssma:0"]');
|
|
|
678
|
+ tmp.each(function () {
|
|
|
679
|
+ var ssrc = $(this).attr('ssrc');
|
|
|
680
|
+ $(this).find('>parameter').each(function () {
|
|
|
681
|
+ lines += 'a=ssrc:' + ssrc + ' ' + $(this).attr('name');
|
|
|
682
|
+ if ($(this).attr('value') && $(this).attr('value').length)
|
|
|
683
|
+ lines += ':' + $(this).attr('value');
|
|
|
684
|
+ lines += '\r\n';
|
|
|
685
|
+ });
|
|
|
686
|
+ });
|
|
|
687
|
+ sdp.media.forEach(function(media, idx) {
|
|
|
688
|
+ if (!SDPUtil.find_line(media, 'a=mid:' + name))
|
|
|
689
|
+ return;
|
|
|
690
|
+ sdp.media[idx] += lines;
|
|
|
691
|
+ if (!self.removessrc[idx]) self.removessrc[idx] = '';
|
|
|
692
|
+ self.removessrc[idx] += lines;
|
|
|
693
|
+ });
|
|
|
694
|
+ sdp.raw = sdp.session + sdp.media.join('');
|
|
|
695
|
+ });
|
|
|
696
|
+ this.modifySources();
|
|
|
697
|
+};
|
|
|
698
|
+
|
|
|
699
|
+JingleSession.prototype.modifySources = function() {
|
|
|
700
|
+ var self = this;
|
|
|
701
|
+ if (this.peerconnection.signalingState == 'closed') return;
|
|
|
702
|
+ if (!(this.addssrc.length || this.removessrc.length || this.pendingop !== null)) return;
|
|
|
703
|
+ if (!(this.peerconnection.signalingState == 'stable' && this.peerconnection.iceConnectionState == 'connected')) {
|
|
|
704
|
+ console.warn('modifySources not yet', this.peerconnection.signalingState, this.peerconnection.iceConnectionState);
|
|
|
705
|
+ this.wait = true;
|
|
|
706
|
+ window.setTimeout(function() { self.modifySources(); }, 250);
|
|
|
707
|
+ return;
|
|
|
708
|
+ }
|
|
|
709
|
+ if (this.wait) {
|
|
|
710
|
+ window.setTimeout(function() { self.modifySources(); }, 2500);
|
|
|
711
|
+ this.wait = false;
|
|
|
712
|
+ return;
|
|
|
713
|
+ }
|
|
|
714
|
+
|
|
|
715
|
+ var sdp = new SDP(this.peerconnection.remoteDescription.sdp);
|
|
|
716
|
+
|
|
|
717
|
+ // add sources
|
|
|
718
|
+ this.addssrc.forEach(function(lines, idx) {
|
|
|
719
|
+ sdp.media[idx] += lines;
|
|
|
720
|
+ });
|
|
|
721
|
+ this.addssrc = [];
|
|
|
722
|
+
|
|
|
723
|
+ // remove sources
|
|
|
724
|
+ this.removessrc.forEach(function(lines, idx) {
|
|
|
725
|
+ lines = lines.split('\r\n');
|
|
|
726
|
+ lines.pop(); // remove empty last element;
|
|
|
727
|
+ lines.forEach(function(line) {
|
|
|
728
|
+ sdp.media[idx] = sdp.media[idx].replace(line + '\r\n', '');
|
|
|
729
|
+ });
|
|
|
730
|
+ });
|
|
|
731
|
+ this.removessrc = [];
|
|
|
732
|
+
|
|
|
733
|
+ sdp.raw = sdp.session + sdp.media.join('');
|
|
|
734
|
+ this.peerconnection.setRemoteDescription(new RTCSessionDescription({type: 'offer', sdp: sdp.raw}),
|
|
|
735
|
+ function() {
|
|
|
736
|
+ self.peerconnection.createAnswer(
|
|
|
737
|
+ function(modifiedAnswer) {
|
|
|
738
|
+ // change video direction, see https://github.com/jitsi/jitmeet/issues/41
|
|
|
739
|
+ if (self.pendingop !== null) {
|
|
|
740
|
+ var sdp = new SDP(modifiedAnswer.sdp);
|
|
|
741
|
+ if (sdp.media.length > 1) {
|
|
|
742
|
+ switch(self.pendingop) {
|
|
|
743
|
+ case 'mute':
|
|
|
744
|
+ sdp.media[1] = sdp.media[1].replace('a=sendrecv', 'a=recvonly');
|
|
|
745
|
+ break;
|
|
|
746
|
+ case 'unmute':
|
|
|
747
|
+ sdp.media[1] = sdp.media[1].replace('a=recvonly', 'a=sendrecv');
|
|
|
748
|
+ break;
|
|
|
749
|
+ }
|
|
|
750
|
+ sdp.raw = sdp.session + sdp.media.join('');
|
|
|
751
|
+ modifiedAnswer.sdp = sdp.raw;
|
|
|
752
|
+ }
|
|
|
753
|
+ self.pendingop = null;
|
|
|
754
|
+ }
|
|
|
755
|
+
|
|
|
756
|
+ self.peerconnection.setLocalDescription(modifiedAnswer,
|
|
|
757
|
+ function() {
|
|
|
758
|
+ //console.log('modified setLocalDescription ok');
|
|
|
759
|
+ $(document).trigger('setLocalDescription.jingle', [self.sid]);
|
|
|
760
|
+ },
|
|
|
761
|
+ function(error) {
|
|
|
762
|
+ console.log('modified setLocalDescription failed');
|
|
|
763
|
+ }
|
|
|
764
|
+ );
|
|
|
765
|
+ },
|
|
|
766
|
+ function(error) {
|
|
|
767
|
+ console.log('modified answer failed');
|
|
|
768
|
+ }
|
|
|
769
|
+ );
|
|
|
770
|
+ },
|
|
|
771
|
+ function(error) {
|
|
|
772
|
+ console.log('modify failed');
|
|
|
773
|
+ }
|
|
|
774
|
+ );
|
|
|
775
|
+};
|
|
|
776
|
+
|
|
|
777
|
+// SDP-based mute by going recvonly/sendrecv
|
|
|
778
|
+// FIXME: should probably black out the screen as well
|
|
|
779
|
+JingleSession.prototype.hardMuteVideo = function (muted) {
|
|
|
780
|
+ this.pendingop = muted ? 'mute' : 'unmute';
|
|
|
781
|
+ this.modifySources();
|
|
|
782
|
+
|
|
|
783
|
+ this.connection.jingle.localStream.getVideoTracks().forEach(function (track) {
|
|
|
784
|
+ track.enabled = !muted;
|
|
|
785
|
+ });
|
|
|
786
|
+};
|
|
|
787
|
+
|
|
|
788
|
+JingleSession.prototype.sendMute = function (muted, content) {
|
|
|
789
|
+ var info = $iq({to: this.peerjid,
|
|
|
790
|
+ type: 'set'})
|
|
|
791
|
+ .c('jingle', {xmlns: 'urn:xmpp:jingle:1',
|
|
|
792
|
+ action: 'session-info',
|
|
|
793
|
+ initiator: this.initiator,
|
|
|
794
|
+ sid: this.sid });
|
|
|
795
|
+ info.c(muted ? 'mute' : 'unmute', {xmlns: 'urn:xmpp:jingle:apps:rtp:info:1'});
|
|
|
796
|
+ info.attrs({'creator': this.me == this.initiator ? 'creator' : 'responder'});
|
|
|
797
|
+ if (content) {
|
|
|
798
|
+ info.attrs({'name': content});
|
|
|
799
|
+ }
|
|
|
800
|
+ this.connection.send(info);
|
|
|
801
|
+};
|
|
|
802
|
+
|
|
|
803
|
+JingleSession.prototype.sendRinging = function () {
|
|
|
804
|
+ var info = $iq({to: this.peerjid,
|
|
|
805
|
+ type: 'set'})
|
|
|
806
|
+ .c('jingle', {xmlns: 'urn:xmpp:jingle:1',
|
|
|
807
|
+ action: 'session-info',
|
|
|
808
|
+ initiator: this.initiator,
|
|
|
809
|
+ sid: this.sid });
|
|
|
810
|
+ info.c('ringing', {xmlns: 'urn:xmpp:jingle:apps:rtp:info:1'});
|
|
|
811
|
+ this.connection.send(info);
|
|
|
812
|
+};
|
|
|
813
|
+
|
|
|
814
|
+JingleSession.prototype.getStats = function (interval) {
|
|
|
815
|
+ var self = this;
|
|
|
816
|
+ var recv = {audio: 0, video: 0};
|
|
|
817
|
+ var lost = {audio: 0, video: 0};
|
|
|
818
|
+ var lastrecv = {audio: 0, video: 0};
|
|
|
819
|
+ var lastlost = {audio: 0, video: 0};
|
|
|
820
|
+ var loss = {audio: 0, video: 0};
|
|
|
821
|
+ var delta = {audio: 0, video: 0};
|
|
|
822
|
+ this.statsinterval = window.setInterval(function () {
|
|
|
823
|
+ if (self && self.peerconnection && self.peerconnection.getStats) {
|
|
|
824
|
+ self.peerconnection.getStats(function (stats) {
|
|
|
825
|
+ var results = stats.result();
|
|
|
826
|
+ // TODO: there are so much statistics you can get from this..
|
|
|
827
|
+ for (var i = 0; i < results.length; ++i) {
|
|
|
828
|
+ if (results[i].type == 'ssrc') {
|
|
|
829
|
+ var packetsrecv = results[i].stat('packetsReceived');
|
|
|
830
|
+ var packetslost = results[i].stat('packetsLost');
|
|
|
831
|
+ if (packetsrecv && packetslost) {
|
|
|
832
|
+ packetsrecv = parseInt(packetsrecv, 10);
|
|
|
833
|
+ packetslost = parseInt(packetslost, 10);
|
|
|
834
|
+
|
|
|
835
|
+ if (results[i].stat('googFrameRateReceived')) {
|
|
|
836
|
+ lastlost.video = lost.video;
|
|
|
837
|
+ lastrecv.video = recv.video;
|
|
|
838
|
+ recv.video = packetsrecv;
|
|
|
839
|
+ lost.video = packetslost;
|
|
|
840
|
+ } else {
|
|
|
841
|
+ lastlost.audio = lost.audio;
|
|
|
842
|
+ lastrecv.audio = recv.audio;
|
|
|
843
|
+ recv.audio = packetsrecv;
|
|
|
844
|
+ lost.audio = packetslost;
|
|
|
845
|
+ }
|
|
|
846
|
+ }
|
|
|
847
|
+ }
|
|
|
848
|
+ }
|
|
|
849
|
+ delta.audio = recv.audio - lastrecv.audio;
|
|
|
850
|
+ delta.video = recv.video - lastrecv.video;
|
|
|
851
|
+ loss.audio = (delta.audio > 0) ? Math.ceil(100 * (lost.audio - lastlost.audio) / delta.audio) : 0;
|
|
|
852
|
+ loss.video = (delta.video > 0) ? Math.ceil(100 * (lost.video - lastlost.video) / delta.video) : 0;
|
|
|
853
|
+ $(document).trigger('packetloss.jingle', [self.sid, loss]);
|
|
|
854
|
+ });
|
|
|
855
|
+ }
|
|
|
856
|
+ }, interval || 3000);
|
|
|
857
|
+ return this.statsinterval;
|
|
|
858
|
+};
|