free ffmpeg process

This commit is contained in:
Master Kwoth 2016-02-28 18:59:50 +01:00
parent 6e23aa2d67
commit 57e6cd2e49

View File

@ -135,28 +135,37 @@ namespace NadekoBot.Classes.Music {
private Task BufferSong(CancellationToken cancelToken) => private Task BufferSong(CancellationToken cancelToken) =>
Task.Run(async () => { Task.Run(async () => {
var p = Process.Start(new ProcessStartInfo { Process p = null;
FileName = "ffmpeg", try {
Arguments = $"-i {SongInfo.Uri} -f s16le -ar 48000 -ac 2 pipe:1 -loglevel quiet", p = Process.Start(new ProcessStartInfo {
UseShellExecute = false, FileName = "ffmpeg",
RedirectStandardOutput = true, Arguments = $"-i {SongInfo.Uri} -f s16le -ar 48000 -ac 2 pipe:1 -loglevel quiet",
}); UseShellExecute = false,
RedirectStandardOutput = true,
int blockSize = 3840; });
byte[] buffer = new byte[blockSize]; int blockSize = 3840;
int attempt = 0; byte[] buffer = new byte[blockSize];
while (!cancelToken.IsCancellationRequested) { int attempt = 0;
int read = await p.StandardOutput.BaseStream.ReadAsync(buffer, 0, blockSize); while (!cancelToken.IsCancellationRequested) {
if (read == 0) int read = await p.StandardOutput.BaseStream.ReadAsync(buffer, 0, blockSize);
if (attempt++ == 20) if (read == 0)
break; if (attempt++ == 20)
break;
else
await Task.Delay(50);
else else
await Task.Delay(50); attempt = 0;
else await songBuffer.WriteAsync(buffer, read, cancelToken);
attempt = 0; if (songBuffer.ContentLength > 2.MB())
await songBuffer.WriteAsync(buffer, read, cancelToken); prebufferingComplete = true;
if (songBuffer.ContentLength > 2.MB()) }
prebufferingComplete = true; }
finally {
if (p != null) {
p.CancelOutputRead();
p.Close();
p.Dispose();
}
} }
Console.WriteLine($"Buffering done. [{songBuffer.ContentLength}]"); Console.WriteLine($"Buffering done. [{songBuffer.ContentLength}]");
}); });
@ -169,6 +178,7 @@ namespace NadekoBot.Classes.Music {
while (!prebufferingComplete && bufferAttempts++ < toAttemptTimes) { while (!prebufferingComplete && bufferAttempts++ < toAttemptTimes) {
await Task.Delay(waitPerAttempt); await Task.Delay(waitPerAttempt);
} }
Console.WriteLine($"Prebuffering done? in {waitPerAttempt*bufferAttempts}");
int blockSize = 3840; int blockSize = 3840;
byte[] buffer = new byte[blockSize]; byte[] buffer = new byte[blockSize];
int attempt = 0; int attempt = 0;