From 0e73372c23330558d742aea7ed129762b9abb21e Mon Sep 17 00:00:00 2001 From: Master Kwoth Date: Sun, 2 Jul 2017 13:19:05 +0200 Subject: [PATCH] buffer is now 50MB --- .../DataStructures/PoopyRingBuffer.cs | 8 +++--- src/NadekoBot/Services/Music/SongBuffer.cs | 25 ++++++++++--------- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/src/NadekoBot/DataStructures/PoopyRingBuffer.cs b/src/NadekoBot/DataStructures/PoopyRingBuffer.cs index 0a2ca39f..214ee110 100644 --- a/src/NadekoBot/DataStructures/PoopyRingBuffer.cs +++ b/src/NadekoBot/DataStructures/PoopyRingBuffer.cs @@ -1,5 +1,7 @@ -using System; +using NadekoBot.Extensions; +using System; using System.Collections.Generic; +using System.IO; using System.Linq; using System.Text; using System.Threading; @@ -48,7 +50,7 @@ namespace NadekoBot.DataStructures 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.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 () => { while (toWrite > RemainingCapacity) - await Task.Delay(100, cancelToken); + await Task.Delay(50, cancelToken); await _locker.WaitAsync(cancelToken); try diff --git a/src/NadekoBot/Services/Music/SongBuffer.cs b/src/NadekoBot/Services/Music/SongBuffer.cs index 64fe4750..1d9fa9ef 100644 --- a/src/NadekoBot/Services/Music/SongBuffer.cs +++ b/src/NadekoBot/Services/Music/SongBuffer.cs @@ -24,12 +24,17 @@ namespace NadekoBot.Services.Music this.p = Process.Start(new ProcessStartInfo { 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, RedirectStandardOutput = true, - RedirectStandardError = false, + RedirectStandardError = true, CreateNoWindow = true, }); + var t = Task.Run(() => + { + this.p.BeginErrorReadLine(); + this.p.WaitForExit(); + }); } public Task StartBuffering(CancellationToken cancelToken) @@ -40,7 +45,8 @@ namespace NadekoBot.Services.Music try { byte[] buffer = new byte[readSize]; - while (!cancelToken.IsCancellationRequested) + int bytesRead = -1; + while (!cancelToken.IsCancellationRequested && bytesRead != 0) { var toRead = buffer.Length; //var remCap = _outStream.RemainingCapacity; @@ -54,14 +60,8 @@ namespace NadekoBot.Services.Music // } // toRead = remCap; //} - - int 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; - } + + bytesRead = await p.StandardOutput.BaseStream.ReadAsync(buffer, 0, toRead, cancelToken).ConfigureAwait(false); await _outStream.WriteAsync(buffer, 0, bytesRead, cancelToken); if (_outStream.RemainingCapacity < _outStream.Capacity * 0.9f) @@ -70,8 +70,9 @@ namespace NadekoBot.Services.Music } Console.WriteLine("FFMPEG killed or song canceled"); } - catch + catch (Exception ex) { + Console.WriteLine(ex); if(toReturn.TrySetResult(false)) Console.WriteLine("Prebuffering failed"); //ignored