1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2026-01-03 23:22:30 +03:00

Sort out access levels for functions; add JSDoc.

This commit is contained in:
Kegan Dougal
2015-07-14 11:11:41 +01:00
parent 7cce41df2e
commit 3e60842c3b
2 changed files with 103 additions and 90 deletions

View File

@@ -78,7 +78,7 @@ MatrixCall.prototype.placeVideoCall = function(localVideoElement, remoteVideoEle
this.remoteVideoElement = remoteVideoElement;
_placeCallWithConstraints(this, _getUserMediaVideoContraints('video'));
this.type = 'video';
this.tryPlayRemoteStream();
_tryPlayRemoteStream(this);
};
/**
@@ -104,22 +104,23 @@ MatrixCall.prototype.getRemoteVideoElement = function() {
*/
MatrixCall.prototype.setRemoteVideoElement = function(element) {
this.remoteVideoElement = element;
this.tryPlayRemoteStream();
_tryPlayRemoteStream(this);
};
/**
* Configure this call from an invite event.
* Configure this call from an invite event. Used by MatrixClient.
* @protected
* @param {MatrixEvent} event The m.call.invite event
*/
MatrixCall.prototype.initWithInvite = function(event) {
MatrixCall.prototype._initWithInvite = function(event) {
this.msg = event.getContent();
this.peerConn = _createPeerConnection(this);
var self = this;
if (this.peerConn) {
this.peerConn.setRemoteDescription(
new this.webRtc.RtcSessionDescription(this.msg.offer),
hookCallback(self, self.onSetRemoteDescriptionSuccess),
hookCallback(self, self.onSetRemoteDescriptionError)
hookCallback(self, self._onSetRemoteDescriptionSuccess),
hookCallback(self, self._onSetRemoteDescriptionError)
);
}
this.state = 'ringing';
@@ -151,10 +152,11 @@ MatrixCall.prototype.initWithInvite = function(event) {
};
/**
* Configure this call from a hangup event.
* Configure this call from a hangup event. Used by MatrixClient.
* @protected
* @param {MatrixEvent} event The m.call.hangup event
*/
MatrixCall.prototype.initWithHangup = function(event) {
MatrixCall.prototype._initWithHangup = function(event) {
// perverse as it may seem, sometimes we want to instantiate a call with a
// hangup message (because when getting the state of the room on load, events
// come in reverse order and we want to remember that a call has been hung up)
@@ -172,33 +174,35 @@ MatrixCall.prototype.answer = function() {
if (!this.localAVStream && !this.waitForLocalAVStream) {
this.webRtc.getUserMedia(
_getUserMediaVideoContraints(this.type),
hookCallback(self, self.gotUserMediaForAnswer),
hookCallback(self, self._gotUserMediaForAnswer),
hookCallback(self, self.getUserMediaFailed)
);
this.state = 'wait_local_media';
} else if (this.localAVStream) {
this.gotUserMediaForAnswer(this.localAVStream);
this._gotUserMediaForAnswer(this.localAVStream);
} else if (this.waitForLocalAVStream) {
this.state = 'wait_local_media';
}
};
/**
* Replace this call with a new call, e.g. for glare resolution.
* Replace this call with a new call, e.g. for glare resolution. Used by
* MatrixClient.
* @protected
* @param {MatrixCall} newCall The new call.
*/
MatrixCall.prototype.replacedBy = function(newCall) {
MatrixCall.prototype._replacedBy = function(newCall) {
debuglog(this.callId + " being replaced by " + newCall.callId);
if (this.state == 'wait_local_media') {
debuglog("Telling new call to wait for local media");
newCall.waitForLocalAVStream = true;
} else if (this.state == 'create_offer') {
debuglog("Handing local stream to new call");
newCall.gotUserMediaForAnswer(this.localAVStream);
newCall._gotUserMediaForAnswer(this.localAVStream);
delete(this.localAVStream);
} else if (this.state == 'invite_sent') {
debuglog("Handing local stream to new call");
newCall.gotUserMediaForAnswer(this.localAVStream);
newCall._gotUserMediaForAnswer(this.localAVStream);
delete(this.localAVStream);
}
newCall.localVideoElement = this.localVideoElement;
@@ -220,16 +224,17 @@ MatrixCall.prototype.hangup = function(reason, suppressEvent) {
call_id: this.callId,
reason: reason
};
this.sendEvent('m.call.hangup', content);
sendEvent(this, 'm.call.hangup', content);
};
/**
* Internal
* @private
* @param {Object} stream
*/
MatrixCall.prototype.gotUserMediaForInvite = function(stream) {
MatrixCall.prototype._gotUserMediaForInvite = function(stream) {
if (this.successor) {
this.successor.gotUserMediaForAnswer(stream);
this.successor._gotUserMediaForAnswer(stream);
return;
}
if (this.state == 'ended') {
@@ -258,17 +263,18 @@ MatrixCall.prototype.gotUserMediaForInvite = function(stream) {
this.peerConn = _createPeerConnection(this);
this.peerConn.addStream(stream);
this.peerConn.createOffer(
hookCallback(self, self.gotLocalOffer),
hookCallback(self, self.getLocalOfferFailed)
hookCallback(self, self._gotLocalOffer),
hookCallback(self, self._getLocalOfferFailed)
);
self.state = 'create_offer';
};
/**
* Internal
* @private
* @param {Object} stream
*/
MatrixCall.prototype.gotUserMediaForAnswer = function(stream) {
MatrixCall.prototype._gotUserMediaForAnswer = function(stream) {
var self = this;
if (self.state == 'ended') {
return;
@@ -311,7 +317,7 @@ MatrixCall.prototype.gotUserMediaForAnswer = function(stream) {
type: self.peerConn.localDescription.type
}
};
self.sendEvent('m.call.answer', content);
sendEvent(self, 'm.call.answer', content);
self.state = 'connecting';
}, function() {
debuglog("Error setting local description!");
@@ -322,9 +328,10 @@ MatrixCall.prototype.gotUserMediaForAnswer = function(stream) {
/**
* Internal
* @private
* @param {Object} event
*/
MatrixCall.prototype.gotLocalIceCandidate = function(event) {
MatrixCall.prototype._gotLocalIceCandidate = function(event) {
if (event.candidate) {
debuglog(
"Got local ICE " + event.candidate.sdpMid + " candidate: " +
@@ -337,15 +344,16 @@ MatrixCall.prototype.gotLocalIceCandidate = function(event) {
sdpMid: event.candidate.sdpMid,
sdpMLineIndex: event.candidate.sdpMLineIndex
};
this.sendCandidate(c);
sendCandidate(this, c);
}
};
/**
* Internal
* Used by MatrixClient.
* @protected
* @param {Object} cand
*/
MatrixCall.prototype.gotRemoteIceCandidate = function(cand) {
MatrixCall.prototype._gotRemoteIceCandidate = function(cand) {
if (this.state == 'ended') {
//debuglog("Ignoring remote ICE candidate because call has ended");
return;
@@ -359,10 +367,11 @@ MatrixCall.prototype.gotRemoteIceCandidate = function(cand) {
};
/**
* Internal
* Used by MatrixClient.
* @protected
* @param {Object} msg
*/
MatrixCall.prototype.receivedAnswer = function(msg) {
MatrixCall.prototype._receivedAnswer = function(msg) {
if (this.state == 'ended') {
return;
}
@@ -370,17 +379,18 @@ MatrixCall.prototype.receivedAnswer = function(msg) {
var self = this;
this.peerConn.setRemoteDescription(
new this.webRtc.RtcSessionDescription(msg.answer),
hookCallback(self, self.onSetRemoteDescriptionSuccess),
hookCallback(self, self.onSetRemoteDescriptionError)
hookCallback(self, self._onSetRemoteDescriptionSuccess),
hookCallback(self, self._onSetRemoteDescriptionError)
);
this.state = 'connecting';
};
/**
* Internal
* @private
* @param {Object} description
*/
MatrixCall.prototype.gotLocalOffer = function(description) {
MatrixCall.prototype._gotLocalOffer = function(description) {
var self = this;
debuglog("Created offer: " + description);
@@ -409,7 +419,7 @@ MatrixCall.prototype.gotLocalOffer = function(description) {
},
lifetime: MatrixCall.CALL_TIMEOUT_MS
};
self.sendEvent('m.call.invite', content);
sendEvent(self, 'm.call.invite', content);
setTimeout(function() {
if (self.state == 'invite_sent') {
@@ -424,9 +434,10 @@ MatrixCall.prototype.gotLocalOffer = function(description) {
/**
* Internal
* @private
* @param {Object} error
*/
MatrixCall.prototype.getLocalOfferFailed = function(error) {
MatrixCall.prototype._getLocalOfferFailed = function(error) {
this.emit(
"error",
callError(MatrixCall.ERR_LOCAL_OFFER_FAILED, "Failed to start audio for call!")
@@ -435,8 +446,9 @@ MatrixCall.prototype.getLocalOfferFailed = function(error) {
/**
* Internal
* @private
*/
MatrixCall.prototype.getUserMediaFailed = function() {
MatrixCall.prototype._getUserMediaFailed = function() {
this.emit(
"error",
callError(
@@ -450,8 +462,9 @@ MatrixCall.prototype.getUserMediaFailed = function() {
/**
* Internal
* @private
*/
MatrixCall.prototype.onIceConnectionStateChanged = function() {
MatrixCall.prototype._onIceConnectionStateChanged = function() {
if (this.state == 'ended') {
return; // because ICE can still complete as we're ending the call
}
@@ -471,8 +484,9 @@ MatrixCall.prototype.onIceConnectionStateChanged = function() {
/**
* Internal
* @private
*/
MatrixCall.prototype.onSignallingStateChanged = function() {
MatrixCall.prototype._onSignallingStateChanged = function() {
debuglog(
"call " + this.callId + ": Signalling state changed to: " +
this.peerConn.signalingState
@@ -481,24 +495,27 @@ MatrixCall.prototype.onSignallingStateChanged = function() {
/**
* Internal
* @private
*/
MatrixCall.prototype.onSetRemoteDescriptionSuccess = function() {
MatrixCall.prototype._onSetRemoteDescriptionSuccess = function() {
debuglog("Set remote description");
};
/**
* Internal
* @private
* @param {Object} e
*/
MatrixCall.prototype.onSetRemoteDescriptionError = function(e) {
MatrixCall.prototype._onSetRemoteDescriptionError = function(e) {
debuglog("Failed to set remote description" + e);
};
/**
* Internal
* @private
* @param {Object} event
*/
MatrixCall.prototype.onAddStream = function(event) {
MatrixCall.prototype._onAddStream = function(event) {
debuglog("Stream added" + event);
var s = event.stream;
@@ -516,52 +533,31 @@ MatrixCall.prototype.onAddStream = function(event) {
var self = this;
forAllTracksOnStream(s, function(t) {
// not currently implemented in chrome
t.onstarted = self.onRemoteStreamTrackStarted;
t.onstarted = hookCallback(self, self._onRemoteStreamTrackStarted);
});
event.stream.onended = function(e) { self.onRemoteStreamEnded(e); };
event.stream.onended = hookCallback(self, self._onRemoteStreamEnded);
// not currently implemented in chrome
event.stream.onstarted = function(e) { self.onRemoteStreamStarted(e); };
event.stream.onstarted = hookCallback(self, self._onRemoteStreamStarted);
this.tryPlayRemoteStream();
_tryPlayRemoteStream(this);
};
/**
* Internal
* @private
* @param {Object} event
*/
MatrixCall.prototype.tryPlayRemoteStream = function(event) {
if (this.getRemoteVideoElement() && this.remoteAVStream) {
var player = this.getRemoteVideoElement();
player.autoplay = true;
player.src = this.URL.createObjectURL(this.remoteAVStream);
var self = this;
setTimeout(function() {
var vel = self.getRemoteVideoElement();
if (vel.play) {
vel.play();
}
// OpenWebRTC does not support oniceconnectionstatechange yet
if (self.webRtc.isOpenWebRTC()) {
self.state = 'connected';
}
}, 0);
}
};
/**
* Internal
* @param {Object} event
*/
MatrixCall.prototype.onRemoteStreamStarted = function(event) {
MatrixCall.prototype._onRemoteStreamStarted = function(event) {
this.state = 'connected';
};
/**
* Internal
* @private
* @param {Object} event
*/
MatrixCall.prototype.onRemoteStreamEnded = function(event) {
MatrixCall.prototype._onRemoteStreamEnded = function(event) {
debuglog("Remote stream ended");
this.state = 'ended';
this.hangupParty = 'remote';
@@ -574,50 +570,49 @@ MatrixCall.prototype.onRemoteStreamEnded = function(event) {
/**
* Internal
* @private
* @param {Object} event
*/
MatrixCall.prototype.onRemoteStreamTrackStarted = function(event) {
MatrixCall.prototype._onRemoteStreamTrackStarted = function(event) {
this.state = 'connected';
};
/**
* Internal
* Used by MatrixClient.
* @protected
* @param {Object} msg
*/
MatrixCall.prototype.onHangupReceived = function(msg) {
MatrixCall.prototype._onHangupReceived = function(msg) {
debuglog("Hangup received");
terminate(this, "remote", msg.reason, true);
};
/**
* Internal
* Used by MatrixClient.
* @protected
* @param {Object} msg
*/
MatrixCall.prototype.onAnsweredElsewhere = function(msg) {
MatrixCall.prototype._onAnsweredElsewhere = function(msg) {
debuglog("Answered elsewhere");
terminate(this, "remote", "answered_elsewhere", true);
};
/**
* Internal
* @param {MatrixCall} self
* @param {string} eventType
* @param {Object} content
* @return {Promise}
*/
MatrixCall.prototype.sendEvent = function(eventType, content) {
return this.client.sendEvent(this.roomId, eventType, content);
var sendEvent = function(self, eventType, content) {
return self.client.sendEvent(self.roomId, eventType, content);
};
/**
* Internal
* @param {Object} content
*/
MatrixCall.prototype.sendCandidate = function(content) {
var sendCandidate = function(self, content) {
// Sends candidates with are sent in a special way because we try to amalgamate
// them into one message
this.candidateSendQueue.push(content);
var self = this;
if (this.candidateSendTries === 0) {
self.candidateSendQueue.push(content);
if (self.candidateSendTries === 0) {
setTimeout(function() {
_sendCandidateQueue(self);
}, 100);
@@ -666,6 +661,24 @@ var stopAllMedia = function(self) {
}
};
var _tryPlayRemoteStream = function(self) {
if (self.getRemoteVideoElement() && self.remoteAVStream) {
var player = self.getRemoteVideoElement();
player.autoplay = true;
player.src = self.URL.createObjectURL(self.remoteAVStream);
setTimeout(function() {
var vel = self.getRemoteVideoElement();
if (vel.play) {
vel.play();
}
// OpenWebRTC does not support oniceconnectionstatechange yet
if (self.webRtc.isOpenWebRTC()) {
self.state = 'connected';
}
}, 0);
}
};
var checkForErrorListener = function(self) {
if (self.listeners("error").length === 0) {
throw new Error(
@@ -700,7 +713,7 @@ var _sendCandidateQueue = function(self) {
candidates: cands
};
debuglog("Attempting to send " + cands.length + " candidates");
self.sendEvent('m.call.candidates', content).then(function() {
sendEvent(self, 'm.call.candidates', content).then(function() {
self.candidateSendTries = 0;
_sendCandidateQueue(self);
}, function(error) {
@@ -730,8 +743,8 @@ var _placeCallWithConstraints = function(self, constraints) {
self.emit("callPlaced", self);
self.webRtc.getUserMedia(
constraints,
hookCallback(self, self.gotUserMediaForInvite),
hookCallback(self, self.getUserMediaFailed)
hookCallback(self, self._gotUserMediaForInvite),
hookCallback(self, self._getUserMediaFailed)
);
self.state = 'wait_local_media';
self.direction = 'outbound';
@@ -757,10 +770,10 @@ var _createPeerConnection = function(self) {
var pc = new self.webRtc.RtcPeerConnection({
iceServers: servers
});
pc.oniceconnectionstatechange = hookCallback(self, self.onIceConnectionStateChanged);
pc.onsignalingstatechange = hookCallback(self, self.onSignallingStateChanged);
pc.onicecandidate = hookCallback(self, self.gotLocalIceCandidate);
pc.onaddstream = hookCallback(self, self.onAddStream);
pc.oniceconnectionstatechange = hookCallback(self, self._onIceConnectionStateChanged);
pc.onsignalingstatechange = hookCallback(self, self._onSignallingStateChanged);
pc.onicecandidate = hookCallback(self, self._gotLocalIceCandidate);
pc.onaddstream = hookCallback(self, self._onAddStream);
return pc;
};

View File

@@ -7,7 +7,7 @@
"test": "istanbul cover --report cobertura --config .istanbul.yml -i \"lib/**/*.js\" jasmine-node -- spec --verbose --junitreport --forceexit --captureExceptions",
"build": "jshint -c .jshint lib/ && browserify browser-index.js -o dist/browser-matrix-dev.js",
"watch": "watchify browser-index.js -o dist/browser-matrix-dev.js -v",
"lint": "jshint -c .jshint lib spec && gjslint --unix_mode --disable 0131,0211,0200 --max_line_length 90 -r spec/ -r lib/",
"lint": "jshint -c .jshint lib spec && gjslint --unix_mode --disable 0131,0211,0200,0222 --max_line_length 90 -r spec/ -r lib/",
"release": "npm run build && mkdir dist/$npm_package_version && uglifyjs -c -m -o dist/$npm_package_version/browser-matrix-$npm_package_version.min.js dist/browser-matrix-dev.js && cp dist/browser-matrix-dev.js dist/$npm_package_version/browser-matrix-$npm_package_version.js"
},
"repository": {