1 | var util = require('util');
|
2 | var webrtc = require('webrtcsupport');
|
3 | var WildEmitter = require('wildemitter');
|
4 | var mockconsole = require('mockconsole');
|
5 | var localMedia = require('localmedia');
|
6 | var Peer = require('./peer');
|
7 |
|
8 |
|
9 | function WebRTC(opts) {
|
10 | var self = this;
|
11 | var options = opts || {};
|
12 | var config = this.config = {
|
13 | debug: false,
|
14 |
|
15 | peerConnectionConfig: {
|
16 | iceServers: [{"url": "stun:stun.l.google.com:19302"}]
|
17 | },
|
18 | peerConnectionConstraints: {
|
19 | optional: [
|
20 | {DtlsSrtpKeyAgreement: true}
|
21 | ]
|
22 | },
|
23 | receiveMedia: {
|
24 | mandatory: {
|
25 | OfferToReceiveAudio: true,
|
26 | OfferToReceiveVideo: true
|
27 | }
|
28 | },
|
29 | enableDataChannels: true
|
30 | };
|
31 | var item;
|
32 |
|
33 |
|
34 | this.screenSharingSupport = webrtc.screenSharing;
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 | this.logger = function () {
|
41 |
|
42 |
|
43 |
|
44 | if (opts.debug) {
|
45 | return opts.logger || console;
|
46 | } else {
|
47 |
|
48 |
|
49 | return opts.logger || mockconsole;
|
50 | }
|
51 | }();
|
52 |
|
53 |
|
54 | for (item in options) {
|
55 | this.config[item] = options[item];
|
56 | }
|
57 |
|
58 |
|
59 | if (!webrtc.support) {
|
60 | this.logger.error('Your browser doesn\'t seem to support WebRTC');
|
61 | }
|
62 |
|
63 |
|
64 | this.peers = [];
|
65 |
|
66 |
|
67 | localMedia.call(this, this.config);
|
68 |
|
69 | this.on('speaking', function () {
|
70 | if (!self.hardMuted) {
|
71 |
|
72 | self.peers.forEach(function (peer) {
|
73 | if (peer.enableDataChannels) {
|
74 | var dc = peer.getDataChannel('hark');
|
75 | if (dc.readyState != 'open') return;
|
76 | dc.send(JSON.stringify({type: 'speaking'}));
|
77 | }
|
78 | });
|
79 | }
|
80 | });
|
81 | this.on('stoppedSpeaking', function () {
|
82 | if (!self.hardMuted) {
|
83 |
|
84 | self.peers.forEach(function (peer) {
|
85 | if (peer.enableDataChannels) {
|
86 | var dc = peer.getDataChannel('hark');
|
87 | if (dc.readyState != 'open') return;
|
88 | dc.send(JSON.stringify({type: 'stoppedSpeaking'}));
|
89 | }
|
90 | });
|
91 | }
|
92 | });
|
93 | this.on('volumeChange', function (volume, treshold) {
|
94 | if (!self.hardMuted) {
|
95 |
|
96 | self.peers.forEach(function (peer) {
|
97 | if (peer.enableDataChannels) {
|
98 | var dc = peer.getDataChannel('hark');
|
99 | if (dc.readyState != 'open') return;
|
100 | dc.send(JSON.stringify({type: 'volume', volume: volume }));
|
101 | }
|
102 | });
|
103 | }
|
104 | });
|
105 |
|
106 |
|
107 | if (this.config.debug) {
|
108 | this.on('*', function (event, val1, val2) {
|
109 | var logger;
|
110 |
|
111 |
|
112 | if (self.config.logger === mockconsole) {
|
113 | logger = console;
|
114 | } else {
|
115 | logger = self.logger;
|
116 | }
|
117 | logger.log('event:', event, val1, val2);
|
118 | });
|
119 | }
|
120 | }
|
121 |
|
122 | util.inherits(WebRTC, localMedia);
|
123 |
|
124 | WebRTC.prototype.createPeer = function (opts) {
|
125 | var peer;
|
126 | opts.parent = this;
|
127 | peer = new Peer(opts);
|
128 | this.peers.push(peer);
|
129 | return peer;
|
130 | };
|
131 |
|
132 |
|
133 | WebRTC.prototype.removePeers = function (id, type) {
|
134 | this.getPeers(id, type).forEach(function (peer) {
|
135 | peer.end();
|
136 | });
|
137 | };
|
138 |
|
139 |
|
140 | WebRTC.prototype.getPeers = function (sessionId, type) {
|
141 | return this.peers.filter(function (peer) {
|
142 | return (!sessionId || peer.id === sessionId) && (!type || peer.type === type);
|
143 | });
|
144 | };
|
145 |
|
146 |
|
147 | WebRTC.prototype.sendToAll = function (message, payload) {
|
148 | this.peers.forEach(function (peer) {
|
149 | peer.send(message, payload);
|
150 | });
|
151 | };
|
152 |
|
153 |
|
154 |
|
155 | WebRTC.prototype.sendDirectlyToAll = function (channel, message, payload) {
|
156 | this.peers.forEach(function (peer) {
|
157 | if (peer.enableDataChannels) {
|
158 | peer.sendDirectly(channel, message, payload);
|
159 | }
|
160 | });
|
161 | };
|
162 |
|
163 | module.exports = WebRTC;
|