Comparing version 1.6.0 to 1.7.0
{ | ||
"name": "webrtc", | ||
"version": "1.6.0", | ||
"version": "1.7.0", | ||
"keywords": [ | ||
@@ -18,6 +18,3 @@ "webrtc", | ||
"rtcpeerconnection": "2.1.x", | ||
"getusermedia": "0.2.1", | ||
"sdp-jingle-json": "1.0.x", | ||
"hark": "0.2.x", | ||
"mediastream-gain": "0.1.1", | ||
"localmedia": "0.0.8", | ||
"mockconsole": "0.0.1" | ||
@@ -24,0 +21,0 @@ }, |
189
webrtc.js
@@ -0,8 +1,7 @@ | ||
var util = require('util'); | ||
var webrtc = require('webrtcsupport'); | ||
var getUserMedia = require('getusermedia'); | ||
var PeerConnection = require('rtcpeerconnection'); | ||
var WildEmitter = require('wildemitter'); | ||
var hark = require('hark'); | ||
var GainController = require('mediastream-gain'); | ||
var mockconsole = require('mockconsole'); | ||
var localMedia = require('localmedia'); | ||
@@ -15,5 +14,2 @@ | ||
debug: false, | ||
localVideoEl: '', | ||
remoteVideosEl: '', | ||
autoRequestMedia: false, | ||
// makes the entire PC config overridable | ||
@@ -28,7 +24,2 @@ peerConnectionConfig: { | ||
}, | ||
autoAdjustMic: false, | ||
media: { | ||
audio: true, | ||
video: true | ||
}, | ||
receiveMedia: { | ||
@@ -40,6 +31,5 @@ mandatory: { | ||
}, | ||
detectSpeakingEvents: true, | ||
enableDataChannels: true | ||
}; | ||
var item, connection; | ||
var item; | ||
@@ -79,4 +69,28 @@ // expose screensharing check | ||
WildEmitter.call(this); | ||
// call localMedia constructor | ||
localMedia.call(this, this.config); | ||
this.on('speaking', function () { | ||
if (!self.hardMuted) { | ||
self.sendToAll('speaking'); | ||
} | ||
}); | ||
this.on('stoppedSpeaking', function () { | ||
if (!self.hardMuted) { | ||
self.sendToAll('stopped_speaking'); | ||
} | ||
}); | ||
this.on('volumeChange', function (volume, treshold) { | ||
if (!self.hardMuted) { | ||
// FIXME: should use sendDirectlyToAll, but currently has different semantics wrt payload | ||
self.peers.forEach(function (peer) { | ||
if (peer.enableDataChannels) { | ||
var dc = peer.getDataChannel('hark'); | ||
if (dc.readyState != 'open') return; | ||
dc.send(JSON.stringify({type: 'volume', volume: volume })); | ||
} | ||
}); | ||
} | ||
}); | ||
// log events in debug mode | ||
@@ -98,7 +112,3 @@ if (this.config.debug) { | ||
WebRTC.prototype = Object.create(WildEmitter.prototype, { | ||
constructor: { | ||
value: WebRTC | ||
} | ||
}); | ||
util.inherits(WebRTC, localMedia); | ||
@@ -113,128 +123,2 @@ WebRTC.prototype.createPeer = function (opts) { | ||
WebRTC.prototype.startLocalMedia = function (mediaConstraints, cb) { | ||
var self = this; | ||
var constraints = mediaConstraints || {video: true, audio: true}; | ||
getUserMedia(constraints, function (err, stream) { | ||
if (!err) { | ||
if (constraints.audio && self.config.detectSpeakingEvents) { | ||
self.setupAudioMonitor(stream); | ||
} | ||
self.localStream = stream; | ||
if (self.config.autoAdjustMic) { | ||
self.gainController = new GainController(stream); | ||
// start out somewhat muted if we can track audio | ||
self.setMicIfEnabled(0.5); | ||
} | ||
self.emit('localStream', stream); | ||
} | ||
if (cb) cb(err, stream); | ||
}); | ||
}; | ||
WebRTC.prototype.stopLocalMedia = function () { | ||
if (this.localStream) { | ||
this.localStream.stop(); | ||
this.emit('localStreamStopped'); | ||
} | ||
}; | ||
// Audio controls | ||
WebRTC.prototype.mute = function () { | ||
this._audioEnabled(false); | ||
this.hardMuted = true; | ||
this.emit('audioOff'); | ||
}; | ||
WebRTC.prototype.unmute = function () { | ||
this._audioEnabled(true); | ||
this.hardMuted = false; | ||
this.emit('audioOn'); | ||
}; | ||
// Audio monitor | ||
WebRTC.prototype.setupAudioMonitor = function (stream) { | ||
this.logger.log('Setup audio'); | ||
var audio = hark(stream); | ||
var self = this; | ||
var timeout; | ||
audio.on('speaking', function () { | ||
self.emit('speaking'); | ||
if (self.hardMuted) return; | ||
self.setMicIfEnabled(1); | ||
self.sendToAll('speaking', {}); | ||
}); | ||
audio.on('stopped_speaking', function () { | ||
if (timeout) clearTimeout(timeout); | ||
timeout = setTimeout(function () { | ||
self.emit('stoppedSpeaking'); | ||
if (self.hardMuted) return; | ||
self.setMicIfEnabled(0.5); | ||
self.sendToAll('stopped_speaking', {}); | ||
}, 1000); | ||
}); | ||
if (this.config.enableDataChannels) { | ||
// until https://code.google.com/p/chromium/issues/detail?id=121673 is fixed... | ||
audio.on('volume_change', function (volume, treshold) { | ||
self.emit('volumeChange', volume, treshold); | ||
if (self.hardMuted) return; | ||
// FIXME: should use sendDirectlyToAll, but currently has different semantics wrt payload | ||
self.peers.forEach(function (peer) { | ||
if (peer.enableDataChannels) { | ||
var dc = peer.getDataChannel('hark'); | ||
if (dc.readyState != 'open') return; | ||
dc.send(JSON.stringify({type: 'volume', volume: volume })); | ||
} | ||
}); | ||
}); | ||
} | ||
}; | ||
// We do this as a seperate method in order to | ||
// still leave the "setMicVolume" as a working | ||
// method. | ||
WebRTC.prototype.setMicIfEnabled = function (volume) { | ||
if (!this.config.autoAdjustMic) return; | ||
this.gainController.setGain(volume); | ||
}; | ||
// Video controls | ||
WebRTC.prototype.pauseVideo = function () { | ||
this._videoEnabled(false); | ||
this.emit('videoOff'); | ||
}; | ||
WebRTC.prototype.resumeVideo = function () { | ||
this._videoEnabled(true); | ||
this.emit('videoOn'); | ||
}; | ||
// Combined controls | ||
WebRTC.prototype.pause = function () { | ||
this._audioEnabled(false); | ||
this.pauseVideo(); | ||
}; | ||
WebRTC.prototype.resume = function () { | ||
this._audioEnabled(true); | ||
this.resumeVideo(); | ||
}; | ||
// Internal methods for enabling/disabling audio/video | ||
WebRTC.prototype._audioEnabled = function (bool) { | ||
// work around for chrome 27 bug where disabling tracks | ||
// doesn't seem to work (works in canary, remove when working) | ||
this.setMicIfEnabled(bool ? 1 : 0); | ||
this.localStream.getAudioTracks().forEach(function (track) { | ||
track.enabled = !!bool; | ||
}); | ||
}; | ||
WebRTC.prototype._videoEnabled = function (bool) { | ||
this.localStream.getVideoTracks().forEach(function (track) { | ||
track.enabled = !!bool; | ||
}); | ||
}; | ||
// removes peers | ||
@@ -296,2 +180,13 @@ WebRTC.prototype.removePeers = function (id, type) { | ||
this.pc.on('iceConnectionStateChange', this.emit.bind(this, 'iceConnectionStateChange')); | ||
this.pc.on('iceConnectionStateChange', function () { | ||
switch (self.pc.iceConnectionState) { | ||
case 'failed': | ||
// currently, in chrome only the initiator goes to failed | ||
// so we need to signal this to the peer | ||
if (self.pc.config.isInitiator) { | ||
self.parent.emit('iceFailed', {id: self.id}); | ||
} | ||
break; | ||
} | ||
}); | ||
this.pc.on('signalingStateChange', this.emit.bind(this, 'signalingStateChange')); | ||
@@ -308,3 +203,5 @@ this.logger = this.parent.logger; | ||
} else { | ||
this.pc.addStream(this.parent.localStream); | ||
this.parent.localStreams.forEach(function (stream) { | ||
self.pc.addStream(stream); | ||
}); | ||
} | ||
@@ -311,0 +208,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
159956
5
4385
+ Addedlocalmedia@0.0.8
+ Addedgetscreenmedia@1.5.6(transitive)
+ Addedgetusermedia@0.2.22.0.1(transitive)
+ Addedlocalmedia@0.0.8(transitive)
+ Addedmediastream-gain@0.1.4(transitive)
+ Addedsdp@1.5.4(transitive)
+ Addedsdp-jingle-json@1.3.0(transitive)
+ Addedwebrtc-adapter@2.1.0(transitive)
+ Addedwebrtcsupport@1.3.2(transitive)
- Removedgetusermedia@0.2.1
- Removedhark@0.2.x
- Removedmediastream-gain@0.1.1
- Removedsdp-jingle-json@1.0.x
- Removedgetusermedia@0.2.1(transitive)
- Removedmediastream-gain@0.1.1(transitive)
- Removedsdp-jingle-json@1.0.0(transitive)