diff --git a/labs/kurento-html5-video/lib/video.js b/labs/kurento-html5-video/lib/video.js index e8821df934b4a57ba1c87038255b0a16231cc981..19ed8a0b4832f0a9b699bf8e8d3b631d0416390c 100644 --- a/labs/kurento-html5-video/lib/video.js +++ b/labs/kurento-html5-video/lib/video.js @@ -7,6 +7,8 @@ var sharedWebcams = {}; const kurento = require('kurento-client'); const config = require('config'); const kurentoUrl = config.get('kurentoUrl'); +const EventEmitter = require('events').EventEmitter; +const inherits = require('util').inherits; if (config.get('acceptSelfSignedCertificate')) { process.env.NODE_TLS_REJECT_UNAUTHORIZED=0; @@ -64,6 +66,7 @@ function Video(_ws, _id, _shared) { var webRtcEndpoint = null; var notFlowingTimeout = null; var notFlowingTimer = 15000; + EventEmitter.call(this); var candidatesQueue = []; @@ -79,6 +82,7 @@ function Video(_ws, _id, _shared) { }; this.start = function(sdpOffer, callback) { + var self = this; getKurentoClient(function(error, kurentoClient) { @@ -114,7 +118,9 @@ function Video(_ws, _id, _shared) { ws.sendMessage({ id : 'playStop', cameraId : id }); }, notFlowingTimer); } else if (event.state === 'FLOWING' && event.type === 'MediaFlowInStateChange') { + console.log(" [o] Media flowing "); + self.emit("READY"); if (notFlowingTimeout) { clearTimeout(notFlowingTimeout); notFlowingTimeout = null; @@ -127,7 +133,7 @@ function Video(_ws, _id, _shared) { _webRtcEndpoint.on('MediaFlowInStateChange', flowInOut); _webRtcEndpoint.on('MediaFlowOutStateChange', flowInOut); - _webRtcEndpoint.on('MediaStateChanged', (e) => { console.log(id); console.log(e)} ); + _webRtcEndpoint.on('MediaStateChanged', (e) => { self.emit("READY"); console.log(id); console.log(e)} ); connectMediaElements(_webRtcEndpoint, function(error) { @@ -180,6 +186,7 @@ function Video(_ws, _id, _shared) { } webRtcEndpoint = _webRtcEndpoint; + console.log(" [webrtc] Created webRtcEndpoint => " + webRtcEndpoint.id); return callback(null, _webRtcEndpoint); }); @@ -200,16 +207,14 @@ function Video(_ws, _id, _shared) { if (sharedWebcams[id]) { var wRtc = sharedWebcams[id]; - wRtc.connect(webRtcEndpoint, function(error) { - + console.log(" [webrtc] Connected " + wRtc.id + " => " + webRtcEndpoint.id); if (error) { return callback(error); } return callback(null); }); } - } }; @@ -228,6 +233,7 @@ function Video(_ws, _id, _shared) { console.log(' [stop] Webcam is shared, releasing ' + id); if (mediaPipelines[id]) { + console.log( '[stop] Releasing pipeline ' + id); mediaPipelines[id].release(); } else { console.log(" [mediaPipeline] PLEASE DONT TRY STOPPING THINGS TWICE"); @@ -242,5 +248,6 @@ function Video(_ws, _id, _shared) { return this; }; +inherits(Video, EventEmitter); module.exports = Video; diff --git a/labs/kurento-html5-video/npm-debug.log b/labs/kurento-html5-video/npm-debug.log deleted file mode 100644 index 7e148f8238c00fd95850ae361f50b9204ac21ce4..0000000000000000000000000000000000000000 --- a/labs/kurento-html5-video/npm-debug.log +++ /dev/null @@ -1,45 +0,0 @@ -0 info it worked if it ends with ok -1 verbose cli [ '/usr/bin/nodejs', '/usr/bin/npm', 'start' ] -2 info using npm@3.5.2 -3 info using node@v4.2.6 -4 verbose run-script [ 'prestart', 'start', 'poststart' ] -5 info lifecycle bbb-html5-video-kurento-bridge@1.0.0~prestart: bbb-html5-video-kurento-bridge@1.0.0 -6 silly lifecycle bbb-html5-video-kurento-bridge@1.0.0~prestart: no script for prestart, continuing -7 info lifecycle bbb-html5-video-kurento-bridge@1.0.0~start: bbb-html5-video-kurento-bridge@1.0.0 -8 verbose lifecycle bbb-html5-video-kurento-bridge@1.0.0~start: unsafe-perm in lifecycle true -9 verbose lifecycle bbb-html5-video-kurento-bridge@1.0.0~start: PATH: /usr/share/npm/bin/node-gyp-bin:/home/mconf/dev/bigbluebutton/labs/kurento-html5-video/node_modules/.bin:/usr/share/npm/bin/node-gyp-bin:/home/mconf/dev/bigbluebutton/labs/kurento-html5-video/node_modules/.bin:/usr/share/npm/bin/node-gyp-bin:/home/mconf/dev/bigbluebutton/bigbluebutton-html5/node_modules/.bin:/usr/share/npm/bin/node-gyp-bin:/home/mconf/dev/bigbluebutton/bigbluebutton-html5/node_modules/.bin:/home/mconf/bin:/home/mconf/.local/bin:/home/mconf/bin:/home/mconf/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/home/mconf/dev/tools/grails/bin:/home/mconf/dev/tools/gradle/bin:/home/mconf/dev/tools/flex/bin:/home/mconf/dev/tools/sbt/bin::/home/mconf/dev/tools/maven/bin:/home/mconf/dev/tools/grails/bin:/home/mconf/dev/tools/gradle/bin:/home/mconf/dev/tools/flex/bin:/home/mconf/dev/tools/sbt/bin::/home/mconf/dev/tools/maven/bin -10 verbose lifecycle bbb-html5-video-kurento-bridge@1.0.0~start: CWD: /home/mconf/dev/bigbluebutton/labs/kurento-html5-video -11 silly lifecycle bbb-html5-video-kurento-bridge@1.0.0~start: Args: [ '-c', 'nodejs server.js' ] -12 silly lifecycle bbb-html5-video-kurento-bridge@1.0.0~start: Returned: code: 1 signal: null -13 info lifecycle bbb-html5-video-kurento-bridge@1.0.0~start: Failed to exec start script -14 verbose stack Error: bbb-html5-video-kurento-bridge@1.0.0 start: `nodejs server.js` -14 verbose stack Exit status 1 -14 verbose stack at EventEmitter.<anonymous> (/usr/share/npm/lib/utils/lifecycle.js:232:16) -14 verbose stack at emitTwo (events.js:87:13) -14 verbose stack at EventEmitter.emit (events.js:172:7) -14 verbose stack at ChildProcess.<anonymous> (/usr/share/npm/lib/utils/spawn.js:24:14) -14 verbose stack at emitTwo (events.js:87:13) -14 verbose stack at ChildProcess.emit (events.js:172:7) -14 verbose stack at maybeClose (internal/child_process.js:821:16) -14 verbose stack at Process.ChildProcess._handle.onexit (internal/child_process.js:211:5) -15 verbose pkgid bbb-html5-video-kurento-bridge@1.0.0 -16 verbose cwd /home/mconf/dev/bigbluebutton/labs/kurento-html5-video -17 error Linux 4.4.0-89-generic -18 error argv "/usr/bin/nodejs" "/usr/bin/npm" "start" -19 error node v4.2.6 -20 error npm v3.5.2 -21 error code ELIFECYCLE -22 error bbb-html5-video-kurento-bridge@1.0.0 start: `nodejs server.js` -22 error Exit status 1 -23 error Failed at the bbb-html5-video-kurento-bridge@1.0.0 start script 'nodejs server.js'. -23 error Make sure you have the latest version of node.js and npm installed. -23 error If you do, this is most likely a problem with the bbb-html5-video-kurento-bridge package, -23 error not with npm itself. -23 error Tell the author that this fails on your system: -23 error nodejs server.js -23 error You can get information on how to open an issue for this project with: -23 error npm bugs bbb-html5-video-kurento-bridge -23 error Or if that isn't available, you can get their info via: -23 error npm owner ls bbb-html5-video-kurento-bridge -23 error There is likely additional logging output above. -24 verbose exit [ 1, true ] diff --git a/labs/kurento-html5-video/server.js b/labs/kurento-html5-video/server.js index e2e333b032bfa958e723b399e0b03be70a856b32..07adef8bdcd8841a600a29305a3a6c01ddfaf583 100755 --- a/labs/kurento-html5-video/server.js +++ b/labs/kurento-html5-video/server.js @@ -4,6 +4,8 @@ * */ +'use strict'; + var cookieParser = require('cookie-parser') var express = require('express'); var session = require('express-session') @@ -82,26 +84,15 @@ wss.on('connection', function(ws) { case 'start': console.log('[' + message.id + '] connection ' + sessionId); - - var video = new Video(ws, message.cameraId, message.cameraShared); - sessions[sessionId].videos[message.cameraId] = video; - - video.start(message.sdpOffer, function(error, sdpAnswer) { - if (error) { - return ws.sendMessage({id : 'error', message : error }); - } - - // Get ice candidates that arrived before video was created - if (sessions[sessionId].iceQueue) { - var queue = sessions[sessionId].iceQueue[message.cameraId]; - while (queue && queue.length > 0) { - video.onIceCandidate(queue.pop()); - } - } - - ws.sendMessage({id : 'startResponse', cameraId: message.cameraId, sdpAnswer : sdpAnswer}); - }); - + if (video) { + video.once('READY', function() { + console.log("Video is ready"); + startVideo(message, ws, sessionId); + }); + } + else { + startVideo(message, ws, sessionId); + } break; case 'stop': @@ -110,6 +101,7 @@ wss.on('connection', function(ws) { if (video) { video.stop(sessionId); + delete sessions[sessionId].videos[message.cameraId]; } else { console.log(" [stop] Why is there no video on STOP?"); } @@ -129,12 +121,15 @@ wss.on('connection', function(ws) { var stopSession = function(sessionId) { + if(typeof sessions[sessionId] === 'undefined') { + console.log(' [>] Session ' + sessionId + ' was already terminated'); + return; + } console.log(' [>] Stopping session ' + sessionId); - var videoIds = Object.keys(sessions[sessionId]); + var videoIds = Object.keys(sessions[sessionId].videos); for (var i = 0; i < videoIds.length; i++) { - var video = sessions[sessionId].videos[videoIds[i]]; if (video){ console.log(video); @@ -174,5 +169,26 @@ function onIceCandidate(sessionId, id, candidate) { } } +function startVideo(message, ws, sessionId) { + console.log('[' + message.id + '] connection ' + sessionId); + let video = new Video(ws, message.cameraId, message.cameraShared); + sessions[sessionId].videos[message.cameraId] = video; + + video.start(message.sdpOffer, function(error, sdpAnswer) { + if (error) { + return ws.sendMessage({id : 'error', message : error }); + } + + // Get ice candidates that arrived before video was created + if (sessions[sessionId].iceQueue) { + var queue = sessions[sessionId].iceQueue[message.cameraId]; + while (queue && queue.length > 0) { + video.onIceCandidate(queue.pop()); + } + } + ws.sendMessage({id : 'startResponse', cameraId: message.cameraId, sdpAnswer : sdpAnswer}); + }); +} + process.on('SIGTERM', stopAll); process.on('SIGINT', stopAll);