From eba804b5ce1baff5185afefcfff28187630d462b Mon Sep 17 00:00:00 2001 From: Master Kwoth Date: Mon, 3 Jul 2017 23:27:17 +0200 Subject: [PATCH] Cleanup, improvements --- .../DataStructures/PoopyRingBuffer.cs | 2 +- .../Administration/GuildTimezoneService.cs | 5 +++- src/NadekoBot/Services/Music/MusicPlayer.cs | 4 +-- src/NadekoBot/Services/Music/SongBuffer.cs | 29 +++++++++++++++---- 4 files changed, 30 insertions(+), 10 deletions(-) diff --git a/src/NadekoBot/DataStructures/PoopyRingBuffer.cs b/src/NadekoBot/DataStructures/PoopyRingBuffer.cs index 6c1e1118..acb7db8e 100644 --- a/src/NadekoBot/DataStructures/PoopyRingBuffer.cs +++ b/src/NadekoBot/DataStructures/PoopyRingBuffer.cs @@ -35,7 +35,7 @@ namespace NadekoBot.DataStructures private readonly SemaphoreSlim _locker = new SemaphoreSlim(1, 1); - public PoopyRingBuffer(int capacity = 50_000_000) + public PoopyRingBuffer(int capacity = 81920 * 100) { this.Capacity = capacity + 1; this.buffer = new byte[this.Capacity]; diff --git a/src/NadekoBot/Services/Administration/GuildTimezoneService.cs b/src/NadekoBot/Services/Administration/GuildTimezoneService.cs index 1d469211..6e4757fe 100644 --- a/src/NadekoBot/Services/Administration/GuildTimezoneService.cs +++ b/src/NadekoBot/Services/Administration/GuildTimezoneService.cs @@ -21,7 +21,10 @@ namespace NadekoBot.Services.Administration TimeZoneInfo tz; try { - tz = TimeZoneInfo.FindSystemTimeZoneById(x.TimeZoneId); + if (x.TimeZoneId == null) + tz = null; + else + tz = TimeZoneInfo.FindSystemTimeZoneById(x.TimeZoneId); } catch { diff --git a/src/NadekoBot/Services/Music/MusicPlayer.cs b/src/NadekoBot/Services/Music/MusicPlayer.cs index 5df2955a..fc11e1ab 100644 --- a/src/NadekoBot/Services/Music/MusicPlayer.cs +++ b/src/NadekoBot/Services/Music/MusicPlayer.cs @@ -321,10 +321,10 @@ namespace NadekoBot.Services.Music } newVoiceChannel = false; var curUser = await VoiceChannel.Guild.GetCurrentUserAsync(); - _audioClient = await VoiceChannel.ConnectAsync(); if (curUser.VoiceChannel != null) { - await _audioClient.StopAsync(); + var ac = await VoiceChannel.ConnectAsync(); + await ac.StopAsync(); await Task.Delay(1000); } _audioClient = await VoiceChannel.ConnectAsync(); diff --git a/src/NadekoBot/Services/Music/SongBuffer.cs b/src/NadekoBot/Services/Music/SongBuffer.cs index 8cc23462..d0d550b3 100644 --- a/src/NadekoBot/Services/Music/SongBuffer.cs +++ b/src/NadekoBot/Services/Music/SongBuffer.cs @@ -10,7 +10,7 @@ namespace NadekoBot.Services.Music { public class SongBuffer : IDisposable { - const int readSize = 38400; + const int readSize = 81920; private Process p; private PoopyRingBuffer _outStream = new PoopyRingBuffer(); @@ -52,8 +52,13 @@ namespace NadekoBot.Services.Music var toReturn = new TaskCompletionSource(); var _ = Task.Run(async () => { + int maxLoopsPerSec = 25; + var sw = Stopwatch.StartNew(); + var delay = 1000 / maxLoopsPerSec; + int currentLoops = 0; try { + ++currentLoops; byte[] buffer = new byte[readSize]; int bytesRead = 1; while (!cancelToken.IsCancellationRequested && !this.p.HasExited) @@ -67,7 +72,7 @@ namespace NadekoBot.Services.Music lock (locker) written = _outStream.Write(buffer, 0, bytesRead); if (!written) - await Task.Delay(32, cancelToken); + await Task.Delay(2000, cancelToken); } while (!written); lock (locker) @@ -75,9 +80,15 @@ namespace NadekoBot.Services.Music if (toReturn.TrySetResult(true)) _log.Info("Prebuffering finished"); - await Task.Delay(5); // @.@ + _log.Info(_outStream.Length); + await Task.Delay(10); } - _log.Info("FFMPEG killed, song canceled, or song fully downloaded"); + if (cancelToken.IsCancellationRequested) + _log.Info("Song canceled"); + else if (p.HasExited) + _log.Info("Song buffered completely (FFmpeg exited)"); + else if (bytesRead == 0) + _log.Info("Nothing read"); } catch (System.ComponentModel.Win32Exception) { @@ -119,8 +130,14 @@ Check the guides for your platform on how to setup ffmpeg correctly: { _log.Error(ex); } - try { this.p.Kill(); } - catch { } + try + { + if(!this.p.HasExited) + this.p.Kill(); + } + catch + { + } _outStream.Dispose(); this.p.Dispose(); }