moved ffmpeg to using, idk maybe this helps

This commit is contained in:
Master Kwoth 2016-02-22 00:44:03 +01:00
parent e5ebdb6de0
commit 7d11f94088

View File

@ -170,65 +170,57 @@ namespace NadekoBot.Classes.Music {
private async Task BufferSong() { private async Task BufferSong() {
//start feeding the buffer //start feeding the buffer
var p = Process.Start(new ProcessStartInfo { var psi = new ProcessStartInfo {
FileName = "ffmpeg", FileName = "ffmpeg",
Arguments = $"-i {Url} -f s16le -ar 48000 -ac 2 pipe:1 -loglevel quiet", //+ (NadekoBot.IsLinux ? "2> /dev/null" : "2>NUL"), Arguments = $"-i {Url} -f s16le -ar 48000 -ac 2 pipe:1 -loglevel quiet", //+ (NadekoBot.IsLinux ? "2> /dev/null" : "2>NUL"),
UseShellExecute = false, UseShellExecute = false,
RedirectStandardOutput = true, RedirectStandardOutput = true,
}); };
int attempt = 0; using (var p = Process.Start(psi)) {
while (true) { int attempt = 0;
while (buffer.writePos - buffer.readPos > 5.MB() && State != StreamState.Completed) { while (true) {
prebufferingComplete = true; while (buffer.writePos - buffer.readPos > 5.MB() && State != StreamState.Completed) {
await Task.Delay(200); prebufferingComplete = true;
} await Task.Delay(200);
if (State == StreamState.Completed) {
try {
p.CancelOutputRead();
p.Close();
} }
catch { }
Console.WriteLine("Buffering canceled, stream is completed.");
return;
}
if (buffer.readPos > 5.MiB() && buffer.writePos > 5.MiB()) {
var skip = 5.MB();
lock (_bufferLock) {
byte[] data = new byte[buffer.Length - skip];
Buffer.BlockCopy(buffer.GetBuffer(), skip, data, 0, (int)(buffer.Length - skip));
var newReadPos = buffer.readPos - skip;
var newPos = buffer.Position - skip;
buffer = new DualStream();
buffer.Write(data, 0, data.Length);
buffer.readPos = newReadPos;
buffer.Position = newPos;
}
}
int blockSize = 1920 * NadekoBot.client.GetService<AudioService>()?.Config?.Channels ?? 3840;
var buf = new byte[blockSize];
int read = 0;
read = await p.StandardOutput.BaseStream.ReadAsync(buf, 0, blockSize);
//Console.WriteLine($"Read: {read}");
if (read == 0) {
if (attempt == 5) {
try {
p.Dispose();
}
catch { }
Console.WriteLine($"Didn't read anything from the stream for {attempt} attempts. {buffer.Length / 1.MB()}MB length"); if (State == StreamState.Completed) {
Console.WriteLine("Buffering canceled, stream is completed.");
return; return;
} }
else { if (buffer.readPos > 5.MiB() && buffer.writePos > 5.MiB()) {
++attempt; var skip = 5.MB();
await Task.Delay(20); lock (_bufferLock) {
byte[] data = new byte[buffer.Length - skip];
Buffer.BlockCopy(buffer.GetBuffer(), skip, data, 0, (int)(buffer.Length - skip));
var newReadPos = buffer.readPos - skip;
var newPos = buffer.Position - skip;
buffer = new DualStream();
buffer.Write(data, 0, data.Length);
buffer.readPos = newReadPos;
buffer.Position = newPos;
}
} }
} int blockSize = 1920 * NadekoBot.client.GetService<AudioService>()?.Config?.Channels ?? 3840;
else { var buf = new byte[blockSize];
attempt = 0; int read = 0;
lock (_bufferLock) { read = await p.StandardOutput.BaseStream.ReadAsync(buf, 0, blockSize);
buffer.Write(buf, 0, read); //Console.WriteLine($"Read: {read}");
if (read == 0) {
if (attempt == 5) {
Console.WriteLine($"Didn't read anything from the stream for {attempt} attempts. {buffer.Length / 1.MB()}MB length");
return;
}
else {
++attempt;
await Task.Delay(20);
}
}
else {
attempt = 0;
lock (_bufferLock) {
buffer.Write(buf, 0, read);
}
} }
} }
} }
@ -241,7 +233,7 @@ namespace NadekoBot.Classes.Music {
if (parent.OnBuffering != null) if (parent.OnBuffering != null)
parent.OnBuffering(); parent.OnBuffering();
Task.Run(async () => { Task.Factory.StartNew(async () => {
await BufferSong(); await BufferSong();
}).ConfigureAwait(false); }).ConfigureAwait(false);