Skip to content

Commit d405344

Browse files
committed
Merge pull request #26 from nathanoehlman/master
Add additional observable monitor events to couple
2 parents f09d561 + 65cc70c commit d405344

File tree

1 file changed

+28
-14
lines changed

1 file changed

+28
-14
lines changed

couple.js

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ function couple(pc, targetId, signaller, opts) {
113113

114114
function abort(stage, sdp, cb) {
115115
return function(err) {
116+
mon.emit('negotiate:abort', stage, sdp);
117+
116118
// log the error
117119
console.error('rtc/couple error (' + stage + '): ', err);
118120

@@ -130,13 +132,7 @@ function couple(pc, targetId, signaller, opts) {
130132
// apply any queued candidates
131133
queuedCandidates.splice(0).forEach(function(data) {
132134
debug('applying queued candidate', data);
133-
134-
try {
135-
pc.addIceCandidate(createIceCandidate(data));
136-
}
137-
catch (e) {
138-
debug('invalidate candidate specified: ', data);
139-
}
135+
addIceCandidate(data);
140136
});
141137
}
142138
}
@@ -283,6 +279,7 @@ function couple(pc, targetId, signaller, opts) {
283279
// debug('gathering state = ' + pc.iceGatheringState);
284280
// debug('connection state = ' + pc.iceConnectionState);
285281
// debug('signaling state = ' + pc.signalingState);
282+
mon.emit('negotiate:' + methodName);
286283

287284
pc[methodName](
288285
function(desc) {
@@ -292,6 +289,7 @@ function couple(pc, targetId, signaller, opts) {
292289
desc.sdp = sdpFilter(desc.sdp, pc, methodName);
293290
}
294291

292+
mon.emit('negotiate:' + methodName + ':created', desc);
295293
q.push({ op: queueLocalDesc(desc) });
296294
cb();
297295
},
@@ -338,19 +336,22 @@ function couple(pc, targetId, signaller, opts) {
338336
if (evt.candidate) {
339337
resetDisconnectTimer();
340338

341-
signaller.to(targetId).send('/candidate', evt.candidate);
339+
mon.emit('icecandidate:local', evt.candidate);
340+
signaller.to(targetId).send('/candidate', evt.candidate);
342341
endOfCandidates = false;
343342
}
344343
else if (! endOfCandidates) {
345344
endOfCandidates = true;
346345
debug('ice gathering state complete');
346+
mon.emit('icecandidate:gathered');
347347
signaller.to(targetId).send('/endofcandidates', {});
348348
}
349349
}
350350

351351
function handleNegotiateRequest(src) {
352352
if (src.id === targetId) {
353353
debug('got negotiate request from ' + targetId + ', creating offer');
354+
mon.emit('negotiate:request', src.id);
354355
q.push({ op: createOffer });
355356
}
356357
}
@@ -364,23 +365,22 @@ function couple(pc, targetId, signaller, opts) {
364365
if (pc.signalingState != 'stable' || (! pc.remoteDescription)) {
365366
debug('queuing candidate');
366367
queuedCandidates.push(data);
368+
mon.emit('icecandidate:remote', data);
367369

368370
mon.removeListener('change', applyCandidatesWhenStable);
369371
mon.on('change', applyCandidatesWhenStable);
370372
return;
371373
}
372374

373-
try {
374-
pc.addIceCandidate(createIceCandidate(data));
375-
}
376-
catch (e) {
377-
debug('invalidate candidate specified: ', data);
378-
}
375+
addIceCandidate(data);
379376
}
380377

381378
function handleSdp(data, src) {
382379
var abortType = data.type === 'offer' ? 'createAnswer' : 'createOffer';
383380

381+
// Emit SDP
382+
mon.emit('sdp:received', data);
383+
384384
// if the source is unknown or not a match, then abort
385385
if ((! src) || (src.id !== targetId)) {
386386
return debug('received sdp but dropping due to unmatched src');
@@ -412,6 +412,17 @@ function couple(pc, targetId, signaller, opts) {
412412
}});
413413
}
414414

415+
function addIceCandidate(data) {
416+
try {
417+
pc.addIceCandidate(createIceCandidate(data));
418+
mon.emit('icecandidate:added', data);
419+
}
420+
catch (e) {
421+
debug('invalidate candidate specified: ', data);
422+
mon.emit('icecandidate:added', data, e);
423+
}
424+
}
425+
415426
function isClosed() {
416427
return CLOSED_STATES.indexOf(pc.iceConnectionState) >= 0;
417428
}
@@ -439,6 +450,7 @@ function couple(pc, targetId, signaller, opts) {
439450
function() {
440451
// send the sdp
441452
signaller.to(targetId).send('/sdp', desc);
453+
mon.emit('negotiate:setlocaldescription', desc);
442454

443455
// callback
444456
cb();
@@ -449,6 +461,7 @@ function couple(pc, targetId, signaller, opts) {
449461
function(err) {
450462
debug('error setting local description', err);
451463
debug(desc.sdp);
464+
mon.emit('negotiate:setlocaldescription', desc, err);
452465
// setTimeout(function() {
453466
// setLocalDesc(task, cb, (retryCount || 0) + 1);
454467
// }, 500);
@@ -470,6 +483,7 @@ function couple(pc, targetId, signaller, opts) {
470483
// when regotiation is needed look for the peer
471484
if (reactive) {
472485
pc.onnegotiationneeded = function() {
486+
mon.emit('negotiate:renegotiate');
473487
debug('renegotiation required, will create offer in 50ms');
474488
clearTimeout(offerTimeout);
475489
offerTimeout = setTimeout(queue(createOffer), 50);

0 commit comments

Comments
 (0)