胡斌

use timer to feed captured bitmap to ffmpeg,to avoid locking of frame

正在显示 1 个修改的文件 包含 58 行增加12 行删除
... ... @@ -22,13 +22,32 @@ ipc.on('close-main-window', function () {
var lastRenderTime = 0;
var contentBitmap = null;
var duration = 10000;//set duration 10s
var start = Date.now()
ipc.on('to-main-timer', function () {
if (Date.now() - lastRenderTime >= 10000) {
/* if(lastMainTimerCalledTime){
var now = Date.now();
console.log("time-diff:",now - lastMainTimerCalledTime);
lastMainTimerCalledTime = now;
}
else{
lastMainTimerCalledTime = Date.now();
}
*/
if(contentBitmap && streamIn){
streamIn.write(contentBitmap);
}
if (Date.now() - start >= duration) {
if (streamIn) {
streamIn.end();
streamIn = null;
}
if (Date.now() - lastRenderTime >= 15000) {
if (Date.now() - start >= duration + 10000) {//quit after duration + 10s
quit();
}
}
... ... @@ -73,17 +92,29 @@ if(!argv.h){//width
argv.h = "600";
}
if(!argv.b){//bitrate
argv.b = "1000k";
}
if(!argv.o){//offscreen
argv.o = true;
}
function startFFmpeg(videosize) {
const args = ['-y','-f', 'rawvideo', '-pix_fmt', 'bgra', '-s' ];
var size = videosize.width + 'x' + videosize.height
args.push(size);
args.push('-r');
args.push(argv.f);
args.push.apply( args, ['-i', 'pipe:0'] );
args.push('-vf')
args.push('fps=fps='+argv.f)
args.push('-c:v');
args.push('libx264');
args.push('-b:v');
args.push('1024k');
args.push(argv.b);
args.push('-r:v');
args.push(argv.f);
args.push('-an');
... ... @@ -92,8 +123,13 @@ function startFFmpeg(videosize) {
console.log(args.join` `);
ffmpeg = spawn('ffmpeg', args);
ffmpeg.stderr.on('data', chunk => console.log(chunk.toString()));
streamIn = duplexer(ffmpeg.stdin, ffmpeg.stdout);
if (ffmpeg) {
ffmpeg.stderr.on('data', chunk => console.log(chunk.toString()));
streamIn = duplexer(ffmpeg.stdin, ffmpeg.stdout);
}
else {
console.log("can't start ffmpeg,please make sure ffmpeg is avaiable");
}
}
app.disableHardwareAcceleration()
... ... @@ -104,22 +140,27 @@ app.once('ready', () => {
width:argv.w,
height:argv.h,
webPreferences: {
offscreen: true
offscreen: argv.o
}
})
win.loadURL(argv.u)
win.webContents.executeJavaScript('const ipcToMain = require("electron").ipcRenderer;function timerToMain (){ ipcToMain.send("to-main-timer");}setInterval(timerToMain,10000);');
var intervalms = 1000 / parseInt(argv.f);
var injectScript = 'const ipcToMain = require("electron").ipcRenderer;function timerToMain (){ ipcToMain.send("to-main-timer");}setInterval(timerToMain,' + intervalms + ');';
win.webContents.executeJavaScript(injectScript);
var start = Date.now()
var duration = parseInt(argv.d)*1000
duration = parseInt(argv.d)*1000
win.webContents.on("paint", function (event, dirty, image) {
if (!ffmpeg) {
start = Date.now();
startFFmpeg(image.getSize());
if(!ffmpeg){
sendQuit();
}
}
lastRenderTime = Date.now();
var elapsed = lastRenderTime - start
... ... @@ -130,11 +171,12 @@ win.webContents.executeJavaScript('const ipcToMain = require("electron").ipcRend
}
console.log("done")
if(elapsed > duration + 5000){//delay 5s after closingSgit l pipe ,then close
win.webContents.executeJavaScript('require("electron").ipcRenderer.send("close-main-window")');
sendQuit();
}
}
else {
streamIn.write(image.getBitmap());
// streamIn.write(image.getBitmap());
contentBitmap = image.toBitmap();
}
//console.log("painting", elapsed, win.webContents.isOffscreen(), win.webContents.getFrameRate())
... ... @@ -142,3 +184,7 @@ win.webContents.executeJavaScript('const ipcToMain = require("electron").ipcRend
})
win.webContents.setFrameRate(parseInt(argv.f));
})
function sendQuit(){
win.webContents.executeJavaScript('require("electron").ipcRenderer.send("close-main-window")');
}
... ...