You've already forked matrix-js-sdk
mirror of
https://github.com/matrix-org/matrix-js-sdk.git
synced 2025-12-04 05:02:41 +03:00
Glue in call handling into MatrixClient. Outbound calls work.
This commit is contained in:
177
lib/client.js
177
lib/client.js
@@ -15,6 +15,7 @@ var EventStatus = require("./models/event").EventStatus;
|
||||
var StubStore = require("./store/stub");
|
||||
var Room = require("./models/room");
|
||||
var User = require("./models/user");
|
||||
var webRtcCall = require("./webrtc/call");
|
||||
var utils = require("./utils");
|
||||
|
||||
// TODO:
|
||||
@@ -73,6 +74,17 @@ function MatrixClient(opts) {
|
||||
this._syncingRooms = {
|
||||
// room_id: Promise
|
||||
};
|
||||
this.callList = {
|
||||
// callId: MatrixCall
|
||||
};
|
||||
|
||||
// try constructing a MatrixCall to see if we are running in an environment
|
||||
// which has WebRTC. If we are, listen for and handle m.call.* events.
|
||||
var call = webRtcCall.createNewMatrixCall(this);
|
||||
if (call) {
|
||||
setupCallEventHandler(this);
|
||||
}
|
||||
|
||||
}
|
||||
utils.inherits(MatrixClient, EventEmitter);
|
||||
|
||||
@@ -1335,6 +1347,161 @@ function reEmit(reEmitEntity, emittableEntity, eventNames) {
|
||||
});
|
||||
}
|
||||
|
||||
function setupCallEventHandler(client) {
|
||||
var candidatesByCall = {
|
||||
// callId: [Candidate]
|
||||
};
|
||||
client.on("event", function(event) {
|
||||
if (event.getType().indexOf("m.call.") !== 0) {
|
||||
return; // not a call event
|
||||
}
|
||||
var content = event.getContent();
|
||||
var call = content.call_id ? client.callList[content.call_id] : undefined;
|
||||
var i;
|
||||
|
||||
if (event.getType() === "m.call.invite") {
|
||||
if (event.getSender() === client.credentials.userId) {
|
||||
return; // ignore invites you send
|
||||
}
|
||||
|
||||
if (event.getAge() > content.lifetime) {
|
||||
return; // expired call
|
||||
}
|
||||
|
||||
if (call && call.state === "ended") {
|
||||
return; // stale/old invite event
|
||||
}
|
||||
if (call) {
|
||||
console.log(
|
||||
"WARN: Already have a MatrixCall with id %s but got an " +
|
||||
"invite. Clobbering.",
|
||||
content.call_id
|
||||
);
|
||||
}
|
||||
|
||||
call = webRtcCall.createNewMatrixCall(client, event.getRoomId());
|
||||
if (!call) {
|
||||
console.log(
|
||||
"Incoming call ID " + content.call_id + " but this client " +
|
||||
"doesn't support WebRTC"
|
||||
);
|
||||
// don't hang up the call: there could be other clients
|
||||
// connected that do support WebRTC and declining the
|
||||
// the call on their behalf would be really annoying.
|
||||
return;
|
||||
}
|
||||
|
||||
call.callId = content.call_id;
|
||||
call._initWithInvite(event);
|
||||
client.callList[call.callId] = call;
|
||||
|
||||
// if we stashed candidate events for that call ID, play them back now
|
||||
if (candidatesByCall[call.callId]) {
|
||||
for (i = 0; i < candidatesByCall[call.callId].length; i++) {
|
||||
call._gotRemoteIceCandidate(
|
||||
candidatesByCall[call.callId][i]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// Were we trying to call that user (room)?
|
||||
var existingCall;
|
||||
var existingCalls = utils.values(client.callList);
|
||||
for (i = 0; i < existingCalls.length; ++i) {
|
||||
var thisCall = existingCalls[i];
|
||||
if (call.room_id === thisCall.room_id &&
|
||||
thisCall.direction === 'outbound' &&
|
||||
(["wait_local_media", "create_offer", "invite_sent"].indexOf(
|
||||
thisCall.state) !== -1)) {
|
||||
existingCall = thisCall;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (existingCall) {
|
||||
// If we've only got to wait_local_media or create_offer and
|
||||
// we've got an invite, pick the incoming call because we know
|
||||
// we haven't sent our invite yet otherwise, pick whichever
|
||||
// call has the lowest call ID (by string comparison)
|
||||
if (existingCall.state === 'wait_local_media' ||
|
||||
existingCall.state === 'create_offer' ||
|
||||
existingCall.callId > call.callId) {
|
||||
console.log(
|
||||
"Glare detected: answering incoming call " + call.callId +
|
||||
" and canceling outgoing call " + existingCall.callId
|
||||
);
|
||||
existingCall._replacedBy(call);
|
||||
call.answer();
|
||||
}
|
||||
else {
|
||||
console.log(
|
||||
"Glare detected: rejecting incoming call " + call.callId +
|
||||
" and keeping outgoing call " + existingCall.callId
|
||||
);
|
||||
call.hangup();
|
||||
}
|
||||
}
|
||||
else {
|
||||
client.emit("Call.incoming", call);
|
||||
}
|
||||
}
|
||||
else if (event.getType() === 'm.call.answer') {
|
||||
if (!call) {
|
||||
console.log("Got answer for unknown call ID " + content.call_id);
|
||||
return;
|
||||
}
|
||||
if (event.getSender() === client.credentials.userId) {
|
||||
if (call.state === 'ringing') {
|
||||
call._onAnsweredElsewhere(content);
|
||||
}
|
||||
}
|
||||
else {
|
||||
call._receivedAnswer(content);
|
||||
}
|
||||
}
|
||||
else if (event.getType() === 'm.call.candidates') {
|
||||
if (event.getSender() === client.credentials.userId) {
|
||||
return;
|
||||
}
|
||||
if (!call) {
|
||||
// store the candidates; we may get a call eventually.
|
||||
if (!candidatesByCall[content.call_id]) {
|
||||
candidatesByCall[content.call_id] = [];
|
||||
}
|
||||
candidatesByCall[content.call_id] = candidatesByCall[
|
||||
content.call_id
|
||||
].concat(content.candidates);
|
||||
}
|
||||
else {
|
||||
for (i = 0; i < content.candidates.length; i++) {
|
||||
call._gotRemoteIceCandidate(content.candidates[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (event.getType() === 'm.call.hangup') {
|
||||
// Note that we also observe our own hangups here so we can see
|
||||
// if we've already rejected a call that would otherwise be valid
|
||||
if (!call) {
|
||||
// if not live, store the fact that the call has ended because
|
||||
// we're probably getting events backwards so
|
||||
// the hangup will come before the invite
|
||||
call = webRtcCall.createNewMatrixCall(client, event.getRoomId());
|
||||
if (call) {
|
||||
call.callId = content.call_id;
|
||||
call._initWithHangup(event);
|
||||
client.callList[content.call_id] = call;
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (call.state !== 'ended') {
|
||||
call._onHangupReceived(content);
|
||||
client.callList[content.call_id] = undefined; // delete the call
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function createNewUser(client, userId) {
|
||||
var user = new User(userId);
|
||||
reEmit(client, user, ["User.avatarUrl", "User.displayName", "User.presence"]);
|
||||
@@ -1429,6 +1596,16 @@ module.exports.MatrixClient = MatrixClient;
|
||||
* });
|
||||
*/
|
||||
|
||||
/**
|
||||
* Fires whenever an incoming call arrives.
|
||||
* @event module:client~MatrixClient#"Call.incoming"
|
||||
* @param {MatrixCall} call The incoming call.
|
||||
* @example
|
||||
* matrixClient.on("Call.incoming", function(call){
|
||||
* call.answer(); // auto-answer
|
||||
* });
|
||||
*/
|
||||
|
||||
// EventEmitter JSDocs
|
||||
|
||||
/**
|
||||
|
||||
@@ -7,7 +7,7 @@ var utils = require("../utils");
|
||||
var EventEmitter = require("events").EventEmitter;
|
||||
var DEBUG = true; // set true to enable console logging.
|
||||
|
||||
// events: onHangup, callPlaced, error
|
||||
// events: onHangup, error, replaced
|
||||
|
||||
/**
|
||||
* Construct a new Matrix Call.
|
||||
@@ -68,8 +68,10 @@ MatrixCall.prototype.placeVoiceCall = function() {
|
||||
|
||||
/**
|
||||
* Place a video call to this room.
|
||||
* @param {Element} localVideoElement a DOM element with the local camera preview.
|
||||
* @param {Element} remoteVideoElement a DOM element to render video to.
|
||||
* @param {Element} localVideoElement a <code><video></code> DOM element
|
||||
* to render the local camera preview.
|
||||
* @param {Element} remoteVideoElement a <code><video></code> DOM element
|
||||
* to render video to.
|
||||
* @throws If you have not specified a listener for 'error' events.
|
||||
*/
|
||||
MatrixCall.prototype.placeVideoCall = function(localVideoElement, remoteVideoElement) {
|
||||
@@ -82,7 +84,7 @@ MatrixCall.prototype.placeVideoCall = function(localVideoElement, remoteVideoEle
|
||||
};
|
||||
|
||||
/**
|
||||
* Retrieve the local video DOM element.
|
||||
* Retrieve the local <code><video></code> DOM element.
|
||||
* @return {Element} The dom element
|
||||
*/
|
||||
MatrixCall.prototype.getLocalVideoElement = function() {
|
||||
@@ -90,7 +92,7 @@ MatrixCall.prototype.getLocalVideoElement = function() {
|
||||
};
|
||||
|
||||
/**
|
||||
* Retrieve the remote video DOM element.
|
||||
* Retrieve the remote <code><video></code> DOM element.
|
||||
* @return {Element} The dom element
|
||||
*/
|
||||
MatrixCall.prototype.getRemoteVideoElement = function() {
|
||||
@@ -98,9 +100,9 @@ MatrixCall.prototype.getRemoteVideoElement = function() {
|
||||
};
|
||||
|
||||
/**
|
||||
* Set the remote video DOM element. If this call is active, video will be
|
||||
* rendered to it.
|
||||
* @param {Element} element The DOM element.
|
||||
* Set the remote <code><video></code> DOM element. If this call is active,
|
||||
* video will be rendered to it immediately.
|
||||
* @param {Element} element The <code><video></code> DOM element.
|
||||
*/
|
||||
MatrixCall.prototype.setRemoteVideoElement = function(element) {
|
||||
this.remoteVideoElement = element;
|
||||
@@ -208,6 +210,7 @@ MatrixCall.prototype._replacedBy = function(newCall) {
|
||||
newCall.localVideoElement = this.localVideoElement;
|
||||
newCall.remoteVideoElement = this.remoteVideoElement;
|
||||
this.successor = newCall;
|
||||
this.emit("replaced", newCall);
|
||||
this.hangup(true);
|
||||
};
|
||||
|
||||
@@ -740,7 +743,7 @@ var _sendCandidateQueue = function(self) {
|
||||
};
|
||||
|
||||
var _placeCallWithConstraints = function(self, constraints) {
|
||||
self.emit("callPlaced", self);
|
||||
self.client.callList[self.callId] = self;
|
||||
self.webRtc.getUserMedia(
|
||||
constraints,
|
||||
hookCallback(self, self._gotUserMediaForInvite),
|
||||
@@ -830,6 +833,9 @@ module.exports.MatrixCall = MatrixCall;
|
||||
module.exports.createNewMatrixCall = function(client, roomId) {
|
||||
var w = global.window;
|
||||
var doc = global.document;
|
||||
if (!w || !doc) {
|
||||
return null;
|
||||
}
|
||||
var webRtc = {};
|
||||
webRtc.isOpenWebRTC = function() {
|
||||
var scripts = doc.getElementById("script");
|
||||
|
||||
Reference in New Issue
Block a user