You've already forked matrix-js-sdk
mirror of
https://github.com/matrix-org/matrix-js-sdk.git
synced 2025-08-18 05:42:00 +03:00
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);
|
||||
|
||||
@@ -1358,6 +1370,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);
|
||||
delete client.callList[content.call_id];
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function createNewUser(client, userId) {
|
||||
var user = new User(userId);
|
||||
reEmit(client, user, ["User.avatarUrl", "User.displayName", "User.presence"]);
|
||||
@@ -1452,6 +1619,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
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user