showing stats every 5 secs now

This commit is contained in:
Master Kwoth 2016-01-27 06:33:31 +01:00
parent 2728aa9edc
commit e505d407c2
2 changed files with 23 additions and 5 deletions

View File

@ -2,8 +2,6 @@
using Discord.Audio; using Discord.Audio;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace NadekoBot.Classes.Music { namespace NadekoBot.Classes.Music {

View File

@ -12,6 +12,7 @@ using System.IO;
using System.Diagnostics; using System.Diagnostics;
using NadekoBot.Extensions; using NadekoBot.Extensions;
using System.Threading; using System.Threading;
using Timer = System.Timers.Timer;
namespace NadekoBot.Classes.Music { namespace NadekoBot.Classes.Music {
public enum StreamState { public enum StreamState {
@ -112,6 +113,8 @@ namespace NadekoBot.Classes.Music {
private readonly object _bufferLock = new object(); private readonly object _bufferLock = new object();
private CancellationTokenSource cancelSource; private CancellationTokenSource cancelSource;
public static Timer logTimer = new Timer();
public MusicStreamer(StreamRequest parent, string directUrl, Channel channel) { public MusicStreamer(StreamRequest parent, string directUrl, Channel channel) {
this.parent = parent; this.parent = parent;
this.channel = channel; this.channel = channel;
@ -120,7 +123,25 @@ namespace NadekoBot.Classes.Music {
Console.WriteLine("Created new streamer"); Console.WriteLine("Created new streamer");
State = StreamState.Queued; State = StreamState.Queued;
cancelSource = new CancellationTokenSource(); cancelSource = new CancellationTokenSource();
if (!logTimer.Enabled) {
logTimer.Interval = 5000;
logTimer.Start();
} }
logTimer.Elapsed += LogTimer_Elapsed;
}
private void LogTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) {
if (cancelSource.IsCancellationRequested) {
logTimer.Elapsed -= LogTimer_Elapsed;
return;
}
Console.WriteLine($"Music stats for {string.Join("", parent.Title.Take(parent.Title.Length > 10 ? 10 : parent.Title.Length))}");
Console.WriteLine($"Server: {parent.Server.Name}");
Console.WriteLine($"Title: - Length:{buffer.Length * 1.0f / 1.MB()}MB Status: {State} - Canceled: { cancelSource.IsCancellationRequested}");
Console.WriteLine("--------------------------------");
}
//todo app will crash if song is too long, should load only next 20-ish seconds //todo app will crash if song is too long, should load only next 20-ish seconds
private async Task BufferSong() { private async Task BufferSong() {
Console.WriteLine("Buffering..."); Console.WriteLine("Buffering...");
@ -129,15 +150,14 @@ namespace NadekoBot.Classes.Music {
FileName = "ffmpeg", FileName = "ffmpeg",
Arguments = $"-i {Url} -f s16le -ar 48000 -ac 2 pipe:1", Arguments = $"-i {Url} -f s16le -ar 48000 -ac 2 pipe:1",
UseShellExecute = false, UseShellExecute = false,
RedirectStandardError = false,
RedirectStandardOutput = true, RedirectStandardOutput = true,
CreateNoWindow = true, CreateNoWindow = true,
WindowStyle = ProcessWindowStyle.Hidden,
}); });
while (true) { while (true) {
while (buffer.writePos - buffer.readPos > 2.MB() && !cancelSource.IsCancellationRequested) { while (buffer.writePos - buffer.readPos > 2.MB() && !cancelSource.IsCancellationRequested) {
Console.WriteLine($"Got over 2MB more, waiting. Data length:{buffer.Length * 1.0f / 1.MB()}MB");
await Task.Delay(1000); await Task.Delay(1000);
} }