2017-11-11 05:05:51 +08:00
|
|
|
'use strict';
|
2017-10-13 21:50:17 +08:00
|
|
|
// Global stuff
|
|
|
|
var sharedWebcams = {};
|
|
|
|
|
|
|
|
const kurento = require('kurento-client');
|
|
|
|
const config = require('config');
|
|
|
|
const kurentoUrl = config.get('kurentoUrl');
|
2017-11-11 05:05:51 +08:00
|
|
|
const MCSApi = require('../mcs-core/lib/media/MCSApiStub');
|
2017-11-25 02:59:40 +08:00
|
|
|
const C = require('../bbb/messages/Constants');
|
2017-10-13 21:50:17 +08:00
|
|
|
|
|
|
|
if (config.get('acceptSelfSignedCertificate')) {
|
|
|
|
process.env.NODE_TLS_REJECT_UNAUTHORIZED=0;
|
|
|
|
}
|
|
|
|
|
2017-11-11 05:05:51 +08:00
|
|
|
module.exports = class Video {
|
2017-11-25 02:59:40 +08:00
|
|
|
constructor(_bbbGW, _id, _shared, _sessionId) {
|
2017-11-11 09:35:01 +08:00
|
|
|
this.mcs = new MCSApi();
|
2017-11-25 02:59:40 +08:00
|
|
|
this.bbbGW = _bbbGW;
|
2017-11-11 09:35:01 +08:00
|
|
|
this.id = _id;
|
2017-11-25 02:59:40 +08:00
|
|
|
this.sessionId = _sessionId;
|
2017-11-11 09:35:01 +08:00
|
|
|
this.meetingId = _id;
|
|
|
|
this.shared = _shared;
|
2017-11-25 02:59:40 +08:00
|
|
|
this.role = this.shared? 'share' : 'view'
|
2017-11-11 09:35:01 +08:00
|
|
|
this.webRtcEndpoint = null;
|
|
|
|
this.mediaId = null;
|
|
|
|
|
|
|
|
this.candidatesQueue = [];
|
2017-11-11 05:05:51 +08:00
|
|
|
}
|
2017-10-13 21:50:17 +08:00
|
|
|
|
2017-11-11 05:05:51 +08:00
|
|
|
onIceCandidate (_candidate) {
|
|
|
|
if (this.mediaId) {
|
|
|
|
try {
|
|
|
|
this.flushCandidatesQueue();
|
|
|
|
this.mcs.addIceCandidate(this.mediaId, _candidate);
|
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
console.log(err);
|
|
|
|
}
|
2017-10-13 21:50:17 +08:00
|
|
|
}
|
|
|
|
else {
|
2017-11-11 05:05:51 +08:00
|
|
|
this.candidatesQueue.push(_candidate);
|
2017-10-13 21:50:17 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-11-11 05:05:51 +08:00
|
|
|
flushCandidatesQueue () {
|
|
|
|
if (this.mediaId) {
|
|
|
|
try {
|
|
|
|
while(this.candidatesQueue.length) {
|
|
|
|
let candidate = this.candidatesQueue.shift();
|
|
|
|
this.mcs.addIceCandidate(this.mediaId, candidate);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
console.log(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-10-13 21:50:17 +08:00
|
|
|
|
2017-11-11 05:05:51 +08:00
|
|
|
mediaState (event) {
|
|
|
|
let msEvent = event.event;
|
2017-11-11 09:35:01 +08:00
|
|
|
|
2017-11-11 05:05:51 +08:00
|
|
|
switch (event.eventTag) {
|
2017-11-11 09:35:01 +08:00
|
|
|
|
2017-11-11 05:05:51 +08:00
|
|
|
case "OnIceCandidate":
|
2017-11-25 02:59:40 +08:00
|
|
|
//console.log(" [video] Sending ICE candidate to user => " + this.id);
|
2017-11-11 05:05:51 +08:00
|
|
|
let candidate = msEvent.candidate;
|
2017-11-25 02:59:40 +08:00
|
|
|
this.bbbGW.publish(JSON.stringify({
|
|
|
|
connectionId: this.sessionId,
|
|
|
|
type: 'video',
|
|
|
|
role: this.role,
|
|
|
|
id : 'iceCandidate',
|
|
|
|
cameraId: this.id,
|
|
|
|
candidate: candidate
|
|
|
|
}), C.FROM_VIDEO);
|
2017-11-11 05:05:51 +08:00
|
|
|
break;
|
2017-11-11 09:35:01 +08:00
|
|
|
|
2017-11-11 05:05:51 +08:00
|
|
|
case "MediaStateChanged":
|
|
|
|
break;
|
2017-11-11 09:35:01 +08:00
|
|
|
|
2017-11-11 05:05:51 +08:00
|
|
|
case "MediaFlowOutStateChange":
|
|
|
|
case "MediaFlowInStateChange":
|
|
|
|
console.log(' [video] ' + msEvent.type + '[' + msEvent.state + ']' + ' for endpoint ' + this.id);
|
2017-11-11 09:35:01 +08:00
|
|
|
|
2017-11-11 05:05:51 +08:00
|
|
|
if (msEvent.state === 'NOT_FLOWING') {
|
2017-12-15 23:57:29 +08:00
|
|
|
// this.bbbGW.publish(JSON.stringify({
|
|
|
|
// connectionId: this.sessionId,
|
|
|
|
// type: 'video',
|
|
|
|
// role: this.role,
|
|
|
|
// id : 'playStop',
|
|
|
|
// cameraId: this.id,
|
|
|
|
// }), C.FROM_VIDEO);
|
2017-11-11 09:35:01 +08:00
|
|
|
}
|
|
|
|
else if (msEvent.state === 'FLOWING') {
|
2017-11-25 02:59:40 +08:00
|
|
|
this.bbbGW.publish(JSON.stringify({
|
|
|
|
connectionId: this.sessionId,
|
|
|
|
type: 'video',
|
|
|
|
role: this.role,
|
|
|
|
id : 'playStart',
|
|
|
|
cameraId: this.id,
|
|
|
|
}), C.FROM_VIDEO);
|
2017-11-11 05:05:51 +08:00
|
|
|
}
|
2017-11-11 09:35:01 +08:00
|
|
|
|
2017-11-11 05:05:51 +08:00
|
|
|
break;
|
2017-11-11 09:35:01 +08:00
|
|
|
|
2017-11-11 05:05:51 +08:00
|
|
|
default: console.log(" [video] Unrecognized event");
|
|
|
|
}
|
|
|
|
}
|
2017-10-13 21:50:17 +08:00
|
|
|
|
2017-11-11 05:05:51 +08:00
|
|
|
async start (sdpOffer, callback) {
|
|
|
|
console.log(" [video] start");
|
|
|
|
let sdpAnswer;
|
2017-10-13 21:50:17 +08:00
|
|
|
|
2017-11-11 05:05:51 +08:00
|
|
|
try {
|
2017-11-18 02:57:39 +08:00
|
|
|
this.userId = await this.mcs.join(this.meetingId, 'SFU', {});
|
2017-11-11 05:05:51 +08:00
|
|
|
console.log(" [video] Join returned => " + this.userId);
|
2017-10-13 21:50:17 +08:00
|
|
|
|
2017-11-11 05:05:51 +08:00
|
|
|
if (this.shared) {
|
|
|
|
const ret = await this.mcs.publish(this.userId, this.meetingId, 'WebRtcEndpoint', {descriptor: sdpOffer});
|
2017-11-18 02:57:39 +08:00
|
|
|
|
2017-11-11 05:05:51 +08:00
|
|
|
this.mediaId = ret.sessionId;
|
|
|
|
sharedWebcams[this.id] = this.mediaId;
|
|
|
|
sdpAnswer = ret.answer;
|
|
|
|
this.flushCandidatesQueue();
|
|
|
|
this.mcs.on('MediaEvent' + this.mediaId, this.mediaState.bind(this));
|
2017-10-13 21:50:17 +08:00
|
|
|
|
2017-11-11 09:35:01 +08:00
|
|
|
console.log(" [video] Publish returned => " + this.mediaId);
|
2017-10-13 21:50:17 +08:00
|
|
|
|
2017-11-11 05:05:51 +08:00
|
|
|
return callback(null, sdpAnswer);
|
|
|
|
}
|
|
|
|
else {
|
2017-11-18 02:57:39 +08:00
|
|
|
const ret = await this.mcs.subscribe(this.userId, sharedWebcams[this.id], 'WebRtcEndpoint', {descriptor: sdpOffer});
|
2017-11-11 09:35:01 +08:00
|
|
|
|
2017-11-11 05:05:51 +08:00
|
|
|
this.mediaId = ret.sessionId;
|
|
|
|
sdpAnswer = ret.answer;
|
|
|
|
this.flushCandidatesQueue();
|
|
|
|
this.mcs.on('MediaEvent' + this.mediaId, this.mediaState.bind(this));
|
2017-10-13 21:50:17 +08:00
|
|
|
|
2017-11-11 09:35:01 +08:00
|
|
|
console.log(" [video] Subscribe returned => " + this.mediaId);
|
2017-11-11 05:05:51 +08:00
|
|
|
|
|
|
|
return callback(null, sdpAnswer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
console.log(" [video] MCS returned error => " + err);
|
|
|
|
return callback(err);
|
|
|
|
}
|
2017-10-13 21:50:17 +08:00
|
|
|
};
|
|
|
|
|
2017-11-18 02:57:39 +08:00
|
|
|
async stop () {
|
2017-11-25 02:59:40 +08:00
|
|
|
console.log(' [stop] Releasing endpoints for user ' + this.userId + ' at room ' + this.meetingId);
|
2017-11-18 02:57:39 +08:00
|
|
|
|
|
|
|
try {
|
|
|
|
await this.mcs.leave(this.meetingId, this.userId);
|
|
|
|
if (this.shared) {
|
|
|
|
sharedWebcams[this.id] = null;
|
|
|
|
}
|
|
|
|
this._candidatesQueue = null;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
catch (err) {
|
2017-11-25 02:59:40 +08:00
|
|
|
// TODO error handling
|
2017-11-18 02:57:39 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
return;
|
2017-10-13 21:50:17 +08:00
|
|
|
};
|
|
|
|
};
|