make FFmpegService more reliable (fixes inexplicable hangs in reading ffmpeg output which breaks discohawk audio extractor sometimes and probably other things sometimes unpredictably). fixes #2952

This commit is contained in:
zeromus 2021-10-03 01:25:45 -04:00
parent d3e1a6db99
commit 253d532a63
1 changed files with 47 additions and 2 deletions

View File

@ -1,4 +1,5 @@
using System;
using System.Threading;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
@ -79,9 +80,53 @@ namespace BizHawk.Common
RedirectStandardError = true
};
string result = "";
//probably naturally thread-safe...
//make a lock if not
Action<StreamReader,bool> readerloop = (reader,ignore) =>
{
string line = "";
int idx = 0;
for (; ; )
{
int c = reader.Read();
if (c == -1)
break;
else if (c == '\r')
idx = 0;
else if (c == '\n')
{
if(!ignore)
lock(oInfo)
result += line + "\n";
line = "";
idx = 0;
}
else
{
if (idx < line.Length)
line = line.Substring(0, idx) + (char)c + line.Substring(idx + 1);
else
line += (char)c;
idx++;
}
}
if(!ignore)
if(line != "")
lock(oInfo)
result += line + "\n"; //not sure about the final \n but i concat it after each finished line so why not.. whatever
};
Process proc = Process.Start(oInfo);
string result = proc.StandardOutput.ReadToEnd();
result += proc.StandardError.ReadToEnd();
var tout = new Thread(() => readerloop(proc.StandardOutput,false));
var terr = new Thread(() => readerloop(proc.StandardError,false));
tout.Start();
terr.Start();
proc.WaitForExit();
return new RunResults