buffer is now 50MB

This commit is contained in:
Master Kwoth 2017-07-02 13:19:05 +02:00
parent 7de15bf444
commit 0e73372c23
2 changed files with 18 additions and 15 deletions

View File

@ -1,5 +1,7 @@
using System; using NadekoBot.Extensions;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading; using System.Threading;
@ -48,7 +50,7 @@ namespace NadekoBot.DataStructures
private readonly SemaphoreSlim _locker = new SemaphoreSlim(1, 1); private readonly SemaphoreSlim _locker = new SemaphoreSlim(1, 1);
public PoopyRingBuffer(int capacity = 3840 * 300) public PoopyRingBuffer(int capacity = 50_000_000)
{ {
this.Capacity = capacity + 1; this.Capacity = capacity + 1;
this.buffer = new byte[this.Capacity]; this.buffer = new byte[this.Capacity];
@ -97,7 +99,7 @@ namespace NadekoBot.DataStructures
public Task WriteAsync(byte[] b, int offset, int toWrite, CancellationToken cancelToken) => Task.Run(async () => public Task WriteAsync(byte[] b, int offset, int toWrite, CancellationToken cancelToken) => Task.Run(async () =>
{ {
while (toWrite > RemainingCapacity) while (toWrite > RemainingCapacity)
await Task.Delay(100, cancelToken); await Task.Delay(50, cancelToken);
await _locker.WaitAsync(cancelToken); await _locker.WaitAsync(cancelToken);
try try

View File

@ -24,12 +24,17 @@ namespace NadekoBot.Services.Music
this.p = Process.Start(new ProcessStartInfo this.p = Process.Start(new ProcessStartInfo
{ {
FileName = "ffmpeg", FileName = "ffmpeg",
Arguments = $"-i {songUri} -f s16le -ar 48000 -vn -ac 2 pipe:1 -loglevel quiet -nostdin", Arguments = $"-ss 0 -i {songUri} -f s16le -ar 48000 -vn -ac 2 pipe:1 -loglevel quiet",
UseShellExecute = false, UseShellExecute = false,
RedirectStandardOutput = true, RedirectStandardOutput = true,
RedirectStandardError = false, RedirectStandardError = true,
CreateNoWindow = true, CreateNoWindow = true,
}); });
var t = Task.Run(() =>
{
this.p.BeginErrorReadLine();
this.p.WaitForExit();
});
} }
public Task<bool> StartBuffering(CancellationToken cancelToken) public Task<bool> StartBuffering(CancellationToken cancelToken)
@ -40,7 +45,8 @@ namespace NadekoBot.Services.Music
try try
{ {
byte[] buffer = new byte[readSize]; byte[] buffer = new byte[readSize];
while (!cancelToken.IsCancellationRequested) int bytesRead = -1;
while (!cancelToken.IsCancellationRequested && bytesRead != 0)
{ {
var toRead = buffer.Length; var toRead = buffer.Length;
//var remCap = _outStream.RemainingCapacity; //var remCap = _outStream.RemainingCapacity;
@ -55,13 +61,7 @@ namespace NadekoBot.Services.Music
// toRead = remCap; // toRead = remCap;
//} //}
int bytesRead = await p.StandardOutput.BaseStream.ReadAsync(buffer, 0, toRead, cancelToken).ConfigureAwait(false); bytesRead = await p.StandardOutput.BaseStream.ReadAsync(buffer, 0, toRead, cancelToken).ConfigureAwait(false);
if (bytesRead == 0)
{
Console.WriteLine("I'm not reading anything from ffmpeg");
await Task.Delay(20);
continue;
}
await _outStream.WriteAsync(buffer, 0, bytesRead, cancelToken); await _outStream.WriteAsync(buffer, 0, bytesRead, cancelToken);
if (_outStream.RemainingCapacity < _outStream.Capacity * 0.9f) if (_outStream.RemainingCapacity < _outStream.Capacity * 0.9f)
@ -70,8 +70,9 @@ namespace NadekoBot.Services.Music
} }
Console.WriteLine("FFMPEG killed or song canceled"); Console.WriteLine("FFMPEG killed or song canceled");
} }
catch catch (Exception ex)
{ {
Console.WriteLine(ex);
if(toReturn.TrySetResult(false)) if(toReturn.TrySetResult(false))
Console.WriteLine("Prebuffering failed"); Console.WriteLine("Prebuffering failed");
//ignored //ignored