forked from rtc-io/rtc-quickconnect
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
969 lines (774 loc) · 29.2 KB
/
index.js
File metadata and controls
969 lines (774 loc) · 29.2 KB
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
/* jshint node: true */
/* global location */
'use strict';
var rtc = require('rtc-tools');
var mbus = require('mbus');
var detectPlugin = require('rtc-core/plugin');
var debug = rtc.logger('rtc-quickconnect');
var extend = require('cog/extend');
/**
# rtc-quickconnect
This is a high level helper module designed to help you get up
an running with WebRTC really, really quickly. By using this module you
are trading off some flexibility, so if you need a more flexible
configuration you should drill down into lower level components of the
[rtc.io](http://www.rtc.io) suite. In particular you should check out
[rtc](https://github.com/rtc-io/rtc).
## Example Usage
In the simplest case you simply call quickconnect with a single string
argument which tells quickconnect which server to use for signaling:
<<< examples/simple.js
<<< docs/events.md
<<< docs/examples.md
## Regarding Signalling and a Signalling Server
Signaling is an important part of setting up a WebRTC connection and for
our examples we use our own test instance of the
[rtc-switchboard](https://github.com/rtc-io/rtc-switchboard). For your
testing and development you are more than welcome to use this also, but
just be aware that we use this for our testing so it may go up and down
a little. If you need something more stable, why not consider deploying
an instance of the switchboard yourself - it's pretty easy :)
## Reference
```
quickconnect(signalhost, opts?) => rtc-sigaller instance (+ helpers)
```
### Valid Quick Connect Options
The options provided to the `rtc-quickconnect` module function influence the
behaviour of some of the underlying components used from the rtc.io suite.
Listed below are some of the commonly used options:
- `ns` (default: '')
An optional namespace for your signalling room. While quickconnect
will generate a unique hash for the room, this can be made to be more
unique by providing a namespace. Using a namespace means two demos
that have generated the same hash but use a different namespace will be
in different rooms.
- `room` (default: null) _added 0.6_
Rather than use the internal hash generation
(plus optional namespace) for room name generation, simply use this room
name instead. __NOTE:__ Use of the `room` option takes precendence over
`ns`.
- `debug` (default: false)
Write rtc.io suite debug output to the browser console.
- `expectedLocalStreams` (default: not specified) _added 3.0_
By providing a positive integer value for this option will mean that
the created quickconnect instance will wait until the specified number of
streams have been added to the quickconnect "template" before announcing
to the signaling server.
- `manualJoin` (default: `false`)
Set this value to `true` if you would prefer to call the `join` function
to connecting to the signalling server, rather than having that happen
automatically as soon as quickconnect is ready to.
#### Options for Peer Connection Creation
Options that are passed onto the
[rtc.createConnection](https://github.com/rtc-io/rtc#createconnectionopts-constraints)
function:
- `iceServers`
This provides a list of ice servers that can be used to help negotiate a
connection between peers.
#### Options for P2P negotiation
Under the hood, quickconnect uses the
[rtc/couple](https://github.com/rtc-io/rtc#rtccouple) logic, and the options
passed to quickconnect are also passed onto this function.
**/
module.exports = function (signalhost, opts) {
var hash = typeof location != 'undefined' && location.hash.slice(1);
var signaller = require('rtc-pluggable-signaller')(extend({
signaller: signalhost,
// use the primus endpoint as a fallback in case we are talking to an
// older switchboard instance
endpoints: ['/', '/primus']
}, opts));
var getPeerData = require('./lib/getpeerdata')(signaller.peers);
var generateIceServers = require('rtc-core/genice');
// init configurable vars
var ns = (opts || {}).ns || '';
var room = (opts || {}).room;
var debugging = (opts || {}).debug;
var allowJoin = !(opts || {}).manualJoin;
var profile = {};
var announced = false;
// Schemes allow customisation about how connections are made
// In particular, providing schemes allows providing different sets of ICE servers
// between peers
var schemes = require('./lib/schemes')(signaller, opts);
// collect the local streams
var localStreams = [];
// create the calls map
var calls = signaller.calls = require('./lib/calls')(signaller, opts);
// create the known data channels registry
var channels = {};
var pending = {};
// Reconnecting indicates peers that are in the process of reconnecting
var reconnecting = {};
// save the plugins passed to the signaller
var plugins = signaller.plugins = (opts || {}).plugins || [];
var plugin = detectPlugin(plugins);
var pluginReady;
// check how many local streams have been expected (default: 0)
var expectedLocalStreams = parseInt((opts || {}).expectedLocalStreams, 10) || 0;
var announceTimer = 0;
var updateTimer = 0;
var CLOSED_STATES = ['failed', 'closed'];
function checkReadyToAnnounce() {
clearTimeout(announceTimer);
// if we have already announced do nothing!
if (announced) {
return;
}
if (!allowJoin) {
return;
}
// if we have a plugin but it's not initialized we aren't ready
if (plugin && (!pluginReady)) {
return;
}
// if we are waiting for a set number of streams, then wait until we have
// the required number
if (expectedLocalStreams && localStreams.length < expectedLocalStreams) {
return;
}
// announce ourselves to our new friend
announceTimer = setTimeout(function () {
var data = extend({ room: room }, profile);
// announce and emit the local announce event
signaller.announce(data);
announced = true;
}, 0);
}
function connect(id, connectOpts) {
debug('connecting to ' + id);
if (!id) return debug('invalid target peer ID');
if (pending[id]) {
return debug('a connection is already pending for ' + id + ', as of ' + (Date.now() - pending[id]) + 'ms ago');
}
connectOpts = connectOpts || {};
var scheme = schemes.get(connectOpts.scheme, true);
var data = getPeerData(id);
var pc;
var monitor;
var call;
// if the room is not a match, abort
if (data.room !== room) {
return debug('mismatching room, expected: ' + room + ', got: ' + (data && data.room));
}
if (data.id !== id) {
return debug('mismatching ids, expected: ' + id + ', got: ' + data.id);
}
pending[id] = Date.now();
// end any call to this id so we know we are starting fresh
calls.end(id);
signaller('peer:prepare', id, data, scheme);
function clearPending(msg) {
debug('connection for ' + id + ' is no longer pending [' + (msg || 'no reason') + '], connect available again');
if (pending[id]) {
delete pending[id];
}
if (reconnecting[id]) {
delete reconnecting[id];
}
}
// Regenerate ICE servers (or use existing cached ICE)
generateIceServers(extend({ targetPeer: id }, opts, (scheme || {}).connection), function (err, iceServers) {
if (err) {
signaller('icegeneration:error', id, scheme && scheme.id, err);
} else {
signaller('peer:iceservers', id, scheme && scheme.id, iceServers || []);
}
// create a peer connection
// iceServers that have been created using genice taking precendence
pc = rtc.createConnection(
extend({}, opts, { iceServers: iceServers }),
(opts || {}).constraints
);
signaller('peer:connect', id, pc, data);
// add this connection to the calls list
call = calls.create(id, pc, data);
// add the local streams
localStreams.forEach(function (stream) {
pc.addStream(stream);
});
// add the data channels
// do this differently based on whether the connection is a
// master or a slave connection
if (signaller.isMaster(id)) {
debug('is master, creating data channels: ', Object.keys(channels));
// create the channels
Object.keys(channels).forEach(function (label) {
gotPeerChannel(pc.createDataChannel(label, channels[label]), pc, data);
});
}
else {
pc.ondatachannel = function (evt) {
var channel = evt && evt.channel;
// if we have no channel, abort
if (!channel) {
return;
}
if (channels[channel.label] !== undefined) {
gotPeerChannel(channel, pc, getPeerData(id));
}
};
}
// couple the connections
debug('coupling ' + signaller.id + ' to ' + id);
monitor = rtc.couple(pc, id, signaller, extend({}, opts, {
logger: mbus('pc.' + id, signaller)
}));
// Apply the monitor to the call
call.monitor = monitor;
// once active, trigger the peer connect event
monitor.once('connected', function () {
clearPending('connected successfully');
calls.start(id, pc, data);
});
monitor.once('closed', function () {
clearPending('closed');
calls.end(id);
});
monitor.once('aborted', function () {
clearPending('aborted');
});
monitor.once('failed', function () {
clearPending('failed');
calls.fail(id);
});
// The following states are intermediate states based on the disconnection timer
monitor.once('failing', calls.failing.bind(null, id));
monitor.once('recovered', calls.recovered.bind(null, id));
// Fire the couple event
signaller('peer:couple', id, pc, data, monitor);
// if we are the master connnection, create the offer
// NOTE: this only really for the sake of politeness, as rtc couple
// implementation handles the slave attempting to create an offer
if (signaller.isMaster(id)) {
monitor.createOffer();
}
signaller('peer:prepared', id);
});
}
function getActiveCall(peerId) {
var call = calls.get(peerId);
if (!call) {
throw new Error('No active call for peer: ' + peerId);
}
return call;
}
function gotPeerChannel(channel, pc, data) {
var channelMonitor;
var channelConnectionTimer;
function channelReady() {
var call = calls.get(data.id);
var args = [data.id, channel, data, pc];
// decouple the channel.onopen listener
debug('reporting channel "' + channel.label + '" ready, have call: ' + (!!call));
clearInterval(channelMonitor);
clearTimeout(channelConnectionTimer);
channel.onopen = null;
// save the channel
if (call) {
call.channels.set(channel.label, channel);
// Remove the channel from the call on close, and emit the required events
channel.onclose = function () {
debug('channel "' + channel.label + '" to ' + data.id + ' has closed');
var args = [data.id, channel, channel.label];
// decouple the events
channel.onopen = null;
// Remove the channel entry
call.channels.remove(channel.label);
// emit the plain channel:closed event
signaller.apply(signaller, ['channel:closed'].concat(args));
// emit the labelled version of the event
signaller.apply(signaller, ['channel:closed:' + channel.label].concat(args));
}
}
// trigger the %channel.label%:open event
debug('triggering channel:opened events for channel: ' + channel.label);
// emit the plain channel:opened event
signaller.apply(signaller, ['channel:opened'].concat(args));
// emit the channel:opened:%label% eve
signaller.apply(
signaller,
['channel:opened:' + channel.label].concat(args)
);
}
// If the channel has failed to create for some reason, recreate the channel
function recreateChannel() {
debug('recreating data channel: ' + channel.label);
// Clear timers
clearInterval(channelMonitor);
clearTimeout(channelConnectionTimer);
// Force the channel to close if it is in an open state
if (['connecting', 'open'].indexOf(channel.readyState) !== -1) {
channel.close();
}
// Recreate the channel using the cached options
signaller.createDataChannel(channel.label, channels[channel.label])
}
debug('channel ' + channel.label + ' discovered for peer: ' + data.id);
if (channel.readyState === 'open') {
return channelReady();
}
debug('channel not ready, current state = ' + channel.readyState);
channel.onopen = channelReady;
// monitor the channel open (don't trust the channel open event just yet)
channelMonitor = setInterval(function () {
debug('checking channel state, current state = ' + channel.readyState + ', connection state ' + pc.iceConnectionState);
if (channel.readyState === 'open') {
channelReady();
}
// If the underlying connection has failed/closed, or if the ready state of the channel has transitioned to a closure
// state, then terminate the monitor
else if (CLOSED_STATES.indexOf(pc.iceConnectionState) !== -1 || CLOSED_STATES.indexOf(channel.readyState) !== -1) {
debug('connection or channel has terminated, cancelling channel monitor');
clearInterval(channelMonitor);
clearTimeout(channelConnectionTimer);
}
// If the connection has connected, but the channel is stuck in the connecting state
// start a timer. If this expires, then we will attempt to created the data channel
else if (pc.iceConnectionState === 'connected' && channel.readyState === 'connecting' && !channelConnectionTimer) {
channelConnectionTimer = setTimeout(function () {
if (channel.readyState !== 'connecting') return;
var args = [data.id, channel, data, pc];
// emit the plain channel:failed event
signaller.apply(signaller, ['channel:failed'].concat(args));
// emit the channel:opened:%label% eve
signaller.apply(
signaller,
['channel:failed:' + channel.label].concat(args)
);
// Recreate the channel
return recreateChannel();
}, (opts || {}).channelTimeout || 2000);
}
}, 500);
}
function initPlugin() {
return plugin && plugin.init(opts, function (err) {
if (err) {
return console.error('Could not initialize plugin: ', err);
}
pluginReady = true;
checkReadyToAnnounce();
});
}
function handleLocalAnnounce(data) {
// if we send an announce with an updated room then update our local room name
if (data && typeof data.room != 'undefined') {
room = data.room;
}
}
function handlePeerFilter(id, data) {
// only connect with the peer if we are ready
data.allow = data.allow && (localStreams.length >= expectedLocalStreams);
}
function handlePeerUpdate(data) {
// Do not allow peer updates if we are not announced
if (!announced) return;
var id = data && data.id;
var activeCall = id && calls.get(id);
// if we have received an update for a peer that has no active calls,
// and is not currently in the process of setting up a call
// then pass this onto the announce handler
if (id && (!activeCall) && !pending[id] && !reconnecting[id]) {
debug('received peer update from peer ' + id + ', no active calls');
signaller('peer:autoreconnect', id);
return signaller.reconnectTo(id);
}
}
function handlePeerLeave(data) {
var id = data && data.id;
if (id) {
delete pending[id];
calls.end(id);
}
}
function handlePeerClose(id) {
if (!announced) return;
delete pending[id];
debug('call has from ' + signaller.id + ' to ' + id + ' has ended, reannouncing');
return signaller.profile();
}
// if the room is not defined, then generate the room name
if (!room) {
// if the hash is not assigned, then create a random hash value
if (typeof location != 'undefined' && (!hash)) {
hash = location.hash = '' + (Math.pow(2, 53) * Math.random());
}
room = ns + '#' + hash;
}
if (debugging) {
rtc.logger.enable.apply(rtc.logger, Array.isArray(debug) ? debugging : ['*']);
}
signaller.on('peer:announce', function (data) {
connect(data.id, { scheme: data.scheme });
});
signaller.on('peer:update', handlePeerUpdate);
signaller.on('message:reconnect', function (data, sender, message) {
debug('received reconnect message');
// Sender arguments are always last
if (!message) {
message = sender;
sender = data;
data = undefined;
}
if (!sender.id) return console.warn('Could not reconnect, no sender ID');
// Abort any current calls
calls.abort(sender.id);
delete reconnecting[sender.id];
signaller('peer:reconnecting', sender.id, data || {});
// Reapply sender information in case aborts caused it to be removed
signaller.peers.set(sender.id, sender);
connect(sender.id, data || {});
// If this is the master, echo the reconnection back to the peer instructing that
// the reconnection has been accepted and to connect
var isMaster = signaller.isMaster(sender.id);
if (isMaster) {
signaller.to(sender.id).send('/reconnect', data || {});
}
});
/**
### Quickconnect Broadcast and Data Channel Helper Functions
The following are functions that are patched into the `rtc-signaller`
instance that make working with and creating functional WebRTC applications
a lot simpler.
**/
/**
#### addStream
```
addStream(stream:MediaStream) => qc
```
Add the stream to active calls and also save the stream so that it
can be added to future calls.
**/
signaller.broadcast = signaller.addStream = function (stream) {
localStreams.push(stream);
// if we have any active calls, then add the stream
calls.values().forEach(function (data) {
if (data.pc.addTrack) {
// Firefox + Chrome 64 and above
stream.getTracks().forEach(function (track) {
data.pc.addTrack(track, stream);
});
} else {
// Upto chrome 63
data.pc.addStream(stream);
}
});
checkReadyToAnnounce();
return signaller;
};
/**
#### endCall
The `endCall` function terminates the active call with the given ID.
If a call with the call ID does not exist it will do nothing.
**/
signaller.endCall = calls.end;
/**
#### endCalls()
The `endCalls` function terminates all the active calls that have been
created in this quickconnect instance. Calling `endCalls` does not
kill the connection with the signalling server.
**/
signaller.endCalls = function () {
calls.keys().forEach(calls.end);
};
/**
#### close()
The `close` function provides a convenient way of closing all associated
peer connections. This function simply uses the `endCalls` function and
the underlying `leave` function of the signaller to do a "full cleanup"
of all connections.
**/
signaller.close = function () {
// We are no longer announced
announced = false;
// Remove any pending update annoucements
if (updateTimer) clearTimeout(updateTimer);
// Cleanup
signaller.endCalls();
signaller.leave();
};
/**
#### createDataChannel(label, config)
Request that a data channel with the specified `label` is created on
the peer connection. When the data channel is open and available, an
event will be triggered using the label of the data channel.
For example, if a new data channel was requested using the following
call:
```js
var qc = quickconnect('https://switchboard.rtc.io/').createDataChannel('test');
```
Then when the data channel is ready for use, a `test:open` event would
be emitted by `qc`.
**/
signaller.createDataChannel = function (label, opts) {
// create a channel on all existing calls
calls.keys().forEach(function (peerId) {
var call = calls.get(peerId);
var dc;
// if we are the master connection, create the data channel
if (call && call.pc && signaller.isMaster(peerId)) {
var existingChannel = call.channels.get(label);
if (existingChannel && existingChannel.readyState !== 'closed') {
return debug('Attempted to create data channel "' + label + '" for a call to ' + peerId + ' but an open channel already exists');
}
dc = call.pc.createDataChannel(label, opts);
gotPeerChannel(dc, call.pc, getPeerData(peerId));
}
});
// save the data channel opts in the local channels dictionary
channels[label] = opts || null;
return signaller;
};
/**
#### join()
The `join` function is used when `manualJoin` is set to true when creating
a quickconnect instance. Call the `join` function once you are ready to
join the signalling server and initiate connections with other people.
**/
signaller.join = function () {
allowJoin = true;
checkReadyToAnnounce();
};
/**
#### `get(name)`
The `get` function returns the property value for the specified property name.
**/
signaller.get = function (name) {
return profile[name];
};
/**
#### `getLocalStreams()`
Return a copy of the local streams that have currently been configured
**/
signaller.getLocalStreams = function () {
return [].concat(localStreams);
};
/**
#### reactive()
Flag that this session will be a reactive connection.
**/
signaller.reactive = function () {
// add the reactive flag
opts = opts || {};
opts.reactive = true;
// chain
return signaller;
};
/**
#### registerScheme
Registers a connection scheme for use, and check it for validity
**/
signaller.registerScheme = schemes.add;
/**
#### getSche,e
Returns the connection sheme given by ID
**/
signaller.getScheme = schemes.get;
/**
#### removeStream
```
removeStream(stream:MediaStream)
```
Remove the specified stream from both the local streams that are to
be connected to new peers, and also from any active calls.
**/
signaller.removeStream = function (stream) {
var localIndex = localStreams.indexOf(stream);
// remove the stream from any active calls
calls.values().forEach(function (call) {
// If `RTCPeerConnection.removeTrack` exists (Firefox), then use that
// as `RTCPeerConnection.removeStream` is not supported
if (call.pc.removeTrack) {
stream.getTracks().forEach(function (track) {
try {
call.pc.removeTrack(call.pc.getSenders().find(function (sender) { return sender.track == track; }));
} catch (e) {
// When using LocalMediaStreamTracks, this seems to throw an error due to
// LocalMediaStreamTrack not implementing the RTCRtpSender inteface.
// Without `removeStream` and with `removeTrack` not allowing for local stream
// removal, this needs some thought when dealing with FF renegotiation
console.error('Error removing media track', e);
}
});
}
// Otherwise we just use `RTCPeerConnection.removeStream`
else {
try {
call.pc.removeStream(stream);
} catch (e) {
console.error('Failed to remove media stream', e);
}
}
});
// remove the stream from the localStreams array
if (localIndex >= 0) {
localStreams.splice(localIndex, 1);
}
return signaller;
};
/**
#### requestChannel
```
requestChannel(targetId, label, callback)
```
This is a function that can be used to respond to remote peers supplying
a data channel as part of their configuration. As per the `receiveStream`
function this function will either fire the callback immediately if the
channel is already available, or once the channel has been discovered on
the call.
**/
signaller.requestChannel = function (targetId, label, callback) {
var call = getActiveCall(targetId);
var channel = call && call.channels.get(label);
// if we have then channel trigger the callback immediately
if (channel) {
callback(null, channel);
return signaller;
}
// if not, wait for it
signaller.once('channel:opened:' + label, function (id, dc) {
callback(null, dc);
});
return signaller;
};
/**
#### requestStream
```
requestStream(targetId, idx, callback)
```
Used to request a remote stream from a quickconnect instance. If the
stream is already available in the calls remote streams, then the callback
will be triggered immediately, otherwise this function will monitor
`stream:added` events and wait for a match.
In the case that an unknown target is requested, then an exception will
be thrown.
**/
signaller.requestStream = function (targetId, idx, callback) {
var call = getActiveCall(targetId);
var stream;
function waitForStream(peerId) {
if (peerId !== targetId) {
return;
}
// get the stream
stream = call.pc.getRemoteStreams()[idx];
// if we have the stream, then remove the listener and trigger the cb
if (stream) {
signaller.removeListener('stream:added', waitForStream);
callback(null, stream);
}
}
// look for the stream in the remote streams of the call
stream = call.pc.getRemoteStreams()[idx];
// if we found the stream then trigger the callback
if (stream) {
callback(null, stream);
return signaller;
}
// otherwise wait for the stream
signaller.on('stream:added', waitForStream);
return signaller;
};
/**
#### profile(data)
Update the profile data with the attached information, so when
the signaller announces it includes this data in addition to any
room and id information.
**/
signaller.profile = function (data) {
extend(profile, data || {});
// if we have already announced, then reannounce our profile to provide
// others a `peer:update` event
if (announced) {
clearTimeout(updateTimer);
updateTimer = setTimeout(function () {
// Check that our announced status hasn't changed
if (!announced) return;
debug('[' + signaller.id + '] reannouncing');
signaller.announce(profile);
}, (opts || {}).updateDelay || 1000);
}
return signaller;
};
/**
#### waitForCall
```
waitForCall(targetId, callback)
```
Wait for a call from the specified targetId. If the call is already
active the callback will be fired immediately, otherwise we will wait
for a `call:started` event that matches the requested `targetId`
**/
signaller.waitForCall = function (targetId, callback) {
var call = calls.get(targetId);
if (call && call.active) {
callback(null, call.pc);
return signaller;
}
signaller.on('call:started', function handleNewCall(id) {
if (id === targetId) {
signaller.removeListener('call:started', handleNewCall);
callback(null, calls.get(id).pc);
}
});
};
/**
Attempts to reconnect to a certain target peer. It will close any existing
call to that peer, and restart the connection process
**/
signaller.reconnectTo = function (id, reconnectOpts) {
if (!id) return;
signaller.to(id).send('/reconnect', reconnectOpts)
// If this is the master, connect, otherwise the master will send a /reconnect
// message back instructing the connection to start
var isMaster = signaller.isMaster(id);
if (isMaster) {
// Abort any current calls
signaller('log', 'aborting call');
// Preserve peer data
var peerData = signaller.peers.get(id);
try {
calls.abort(id);
} catch (e) {
signaller('log', e.message);
}
signaller('log', 'call aborted');
signaller('peer:reconnecting', id, reconnectOpts || {});
// Reapply peer data in case it was deleted during abort
signaller.peers.set(id, peerData);
return connect(id, reconnectOpts);
}
// Flag that we are waiting for the master to indicate the reconnection is a go
else {
reconnecting[id] = Date.now();
}
};
// if we have an expected number of local streams, then use a filter to
// check if we should respond
if (expectedLocalStreams) {
signaller.on('peer:filter', handlePeerFilter);
}
// respond to local announce messages
signaller.on('local:announce', handleLocalAnnounce);
// handle ping messages
signaller.on('message:ping', calls.ping);
// Handle when a remote peer leaves that the appropriate closing occurs this
// side as well
signaller.on('message:leave', handlePeerLeave);
// When a call:ended, we reannounce ourselves. This offers a degree of failure handling
// as if a call has dropped unexpectedly (ie. failure/unable to connect) the other peers
// connected to the signaller will attempt to reconnect
signaller.on('call:ended', handlePeerClose);
// if we plugin is active, then initialize it
if (plugin) {
initPlugin();
} else {
// Test if we are ready to announce
process.nextTick(function () {
checkReadyToAnnounce();
});
}
// pass the signaller on
return signaller;
};