Commit e7df7352 authored by Yoann HOUPERT's avatar Yoann HOUPERT

use one web socket

parent 32ec84d7
......@@ -27,7 +27,7 @@ const loader = require('./lib/loader.js')('./client');
const controllerFactory = require('./lib/controller.js');
const proxy = require('./lib/proxy.js')(config.proxy);
const resumeGenerator = require('./lib/resume.js')(config.offline)
const resumeGenerator = require('./lib/resume.js')(config.offline);
console.log('starting hublot...');
......
......@@ -30,10 +30,11 @@ robot = {
room: '',
clientConfig: {},
previousReco: [],
recordedParticipantsWS: {},
recordedParticipantsWS: undefined,
participantsMediaRecorders: {},
isDisconnected: false,
intervalList: [],
speakerId : {},
processAudio(stream, callback, interval) {
const mediaRecorder = new MediaRecorder(stream);
......@@ -90,22 +91,25 @@ robot = {
}
},
openSTTSocket(easyrtcid) {
const ws = robotLib.stt.getTranscriptSocket(e => {
console.log('> ' + e.text);
robotLib.reco.send(
{
from: robot.room,
text: e.from + '\t' + e.until + '\t' + easyrtcid + '\t' + e.text
});
});
ws.addEventListener('error', e => {
// Try to open new connection on error
console.error('STT ws for ' + easyrtcid + ' error. Trying to reopen');
console.error(e);
robot.openSTTSocket(easyrtcid);
});
robot.recordedParticipantsWS[easyrtcid] = ws;
openSTTSocket() {
if(robot.recordedParticipantsWS === undefined){
const ws = robotLib.stt.getTranscriptSocket(e => {
console.log('> ' + e.text);
robotLib.reco.send(
{
from: robot.room,
text: e.from + '\t' + e.until + '\t' + speakerId + '\t' + e.text
});
});
ws.addEventListener('error', e => {
// Try to open new connection on error
console.error('STT ws for ' + easyrtcid + ' error. Trying to reopen');
console.error(e);
robot.openSTTSocket();
});
robot.recordedParticipantsWS = ws;
}
},
getUserStream(easyrtcid) {
......@@ -117,12 +121,13 @@ robot = {
},
audioGenerator(easyrtcid, e) {
robot.recordedParticipantsWS[easyrtcid].send(e.data);
speakerId = easyrtcid;
robot.recordedParticipantsWS.send(e.data);
},
recordParticipant(easyrtcid) {
robot.openSTTSocket(easyrtcid);
robot.openSTTSocket();
robot.getUserStream(easyrtcid);
},
......@@ -130,9 +135,6 @@ robot = {
if (robot.participantsMediaRecorders[easyrtcid]) {
robot.participantsMediaRecorders[easyrtcid].stop();
}
if (robot.recordedParticipantsWS[easyrtcid]) {
robot.recordedParticipantsWS[easyrtcid].close();
}
},
checkDisconnect() {
......@@ -147,7 +149,7 @@ robot = {
}
robot.intervalList = [];
const keyMap = Object.keys(robot.recordedParticipantsWS);
const keyMap = Object.keys(robot.participantsMediaRecorders);
if (keyMap) {
for (let i = 0; i < keyMap.length; i++) {
try {
......@@ -156,6 +158,7 @@ robot = {
console.log('This user is inactive');
}
}
robot.recordedParticipantsWS.close();
}
},
......@@ -176,7 +179,6 @@ robot = {
};
robotController.onAttendeeRemove = (e, data) => {
robot.stopRecordParticipant(data.easyrtcid);
robot.checkDisconnect();
};
......
......@@ -77,8 +77,8 @@ module.exports = config => {
connectionToClient.on('close', () => connectionToKaldi.close());
connectionToKaldi.on('close', () => {
resume.endStream(streamData)
connectionToClient.close()
resume.endStream(streamData);
connectionToClient.close();
});
});
}
......@@ -145,8 +145,8 @@ module.exports = config => {
}
return {
create: (resume) => {
resume.createFolder()
create: resume => {
resume.createFolder();
const server = http.createServer();
......
......@@ -21,8 +21,7 @@ const fs = require('fs');
const uuidv1 = require('uuid/v1');
const ffmpegInstaller = require('@ffmpeg-installer/ffmpeg');
const ffprobeInstaller = require('@ffprobe-installer/ffprobe');
const ffprobeInstaller = require('@ffprobe-installer/ffprobe');
const ffmpeg = require('fluent-ffmpeg');
......@@ -32,11 +31,10 @@ ffmpeg.setFfprobePath(ffprobeInstaller.path);
module.exports = ffmpeg;
module.exports = config => {
let timestamp = undefined;
let timestamp;
return {
createFolder : () => {
createFolder: () => {
if (!fs.existsSync(config.folder.audioRaw)) {
fs.mkdirSync(config.folder.audioRaw);
}
......@@ -44,71 +42,34 @@ module.exports = config => {
fs.mkdirSync(config.folder.audio);
}
},
generateStream : () => {
if(timestamp === undefined){
timestamp = new Date().getTime()
generateStream: () => {
if (timestamp === undefined) {
timestamp = new Date().getTime();
}
const name = uuidv1()
const path = config.folder.audioRaw+"/"+name+".wav"
const name = uuidv1();
const path = config.folder.audioRaw + '/' + name + '.wav';
const streamData = {
"wstream" : fs.createWriteStream(path),
"pathFile" : path,
"fileName": name,
"timestamp" : new Date().getTime()
}
return streamData
wstream: fs.createWriteStream(path),
pathFile: path,
fileName: name,
timestamp: new Date().getTime()
};
return streamData;
},
endStream : (streamData) => {
const toSecond = 1000
streamData.wstream.close();
let delay = ((timestamp - streamData.timestamp) / toSecond) * -1;
try{
//var silence = ffmpeg().input('anullsrc=sample_rate=16000').inputFormat('lavfi').duration(delay).output(config.folder.audio+"/silence_"+streamData.fileName+".wav").run();
//var stream = ffmpeg(streamData.pathFile).toFormat(config.audio.format).noVideo().audioBitrate(config.audio.bitrate).audioChannels(config.audio.channels).audioFrequency(config.audio.srate).save(config.folder.audio+"/"+streamData.fileName+".wav")
ffmpeg()
.input('anullsrc=sample_rate='+config.audio.srate)
.inputFormat('lavfi')
.duration(delay)
.output(config.folder.audio+"/silence_"+streamData.fileName+".wav")
.on('end', function() {
ffmpeg(streamData.pathFile)
.toFormat(config.audio.format)
.noVideo()
.audioBitrate(config.audio.bitrate)
.audioChannels(config.audio.channels)
.audioFrequency(config.audio.srate)
.save(config.folder.audio+"/"+streamData.fileName+".wav")
.on('end', function() {
ffmpeg()
.input(config.folder.audio+"/silence_"+streamData.fileName+".wav")
.input(config.folder.audio+"/"+streamData.fileName+".wav")
.on('error', function(err) {
console.log('An error occurred: ' + err.message);
})
.on('end', function() {
console.log('Merging finished !');
})
.mergeToFile(config.folder.audioMerged+"/"+streamData.fileName+".wav", config.folder.audio)
})
})
.run()
/*
endStream: streamData => {
const toSecond = 1000;
streamData.wstream.close();
const delay = ((timestamp - streamData.timestamp) / toSecond) * -1;
try {
ffmpeg(streamData.pathFile)
.toFormat(config.audio.format)
.noVideo()
.audioBitrate(config.audio.bitrate)
.audioChannels(config.audio.channels)
.audioFrequency(config.audio.srate)
.save(config.folder.audio+"/"+streamData.fileName+".wav")*/
} catch(err){
.save(config.folder.audio + '/' + streamData.fileName + '.wav');
} catch (err) {
// Sometimes, the client will close the connection unexpectedly
// (e.g. when stopping). This is expected
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment