2016-01-26 20:18:48 +01:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Discord;
|
|
|
|
|
using Discord.Commands;
|
|
|
|
|
using Discord.Audio;
|
|
|
|
|
using NadekoBot.Modules;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using NadekoBot.Extensions;
|
|
|
|
|
using System.Threading;
|
2016-01-27 06:33:31 +01:00
|
|
|
|
using Timer = System.Timers.Timer;
|
2016-02-02 23:41:13 +01:00
|
|
|
|
using VideoLibrary;
|
2016-01-26 20:18:48 +01:00
|
|
|
|
|
|
|
|
|
namespace NadekoBot.Classes.Music {
|
|
|
|
|
public enum StreamState {
|
|
|
|
|
Resolving,
|
|
|
|
|
Queued,
|
|
|
|
|
Buffering, //not using it atm
|
|
|
|
|
Playing,
|
|
|
|
|
Completed
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class StreamRequest {
|
|
|
|
|
public Server Server { get; }
|
|
|
|
|
public User User { get; }
|
|
|
|
|
public string Query { get; }
|
|
|
|
|
|
2016-01-29 08:23:15 +01:00
|
|
|
|
public string Title { get; internal set; } = String.Empty;
|
|
|
|
|
|
2016-01-26 20:18:48 +01:00
|
|
|
|
private MusicStreamer musicStreamer = null;
|
2016-01-29 13:28:51 +01:00
|
|
|
|
public StreamState State => musicStreamer?.State ?? privateState;
|
|
|
|
|
private StreamState privateState = StreamState.Resolving;
|
|
|
|
|
|
|
|
|
|
public bool IsPaused => MusicControls.IsPaused;
|
|
|
|
|
|
2016-02-04 14:17:15 +01:00
|
|
|
|
public float Volume => MusicControls?.Volume ?? 1.0f;
|
|
|
|
|
|
2016-01-31 19:51:27 +01:00
|
|
|
|
public MusicControls MusicControls;
|
2016-01-26 20:18:48 +01:00
|
|
|
|
|
2016-01-29 13:28:51 +01:00
|
|
|
|
public StreamRequest(CommandEventArgs e, string query, MusicControls mc) {
|
2016-01-26 20:18:48 +01:00
|
|
|
|
if (e == null)
|
|
|
|
|
throw new ArgumentNullException(nameof(e));
|
|
|
|
|
if (query == null)
|
|
|
|
|
throw new ArgumentNullException(nameof(query));
|
2016-01-29 13:28:51 +01:00
|
|
|
|
this.MusicControls = mc;
|
2016-01-26 20:18:48 +01:00
|
|
|
|
this.Server = e.Server;
|
|
|
|
|
this.Query = query;
|
2016-01-31 19:51:27 +01:00
|
|
|
|
Task.Run(() => ResolveStreamLink());
|
|
|
|
|
mc.SongQueue.Add(this);
|
2016-01-26 20:18:48 +01:00
|
|
|
|
}
|
|
|
|
|
|
2016-02-04 14:17:15 +01:00
|
|
|
|
private async void ResolveStreamLink() {
|
2016-02-02 23:41:13 +01:00
|
|
|
|
Video video = null;
|
2016-01-29 13:28:51 +01:00
|
|
|
|
try {
|
2016-01-30 13:51:47 +01:00
|
|
|
|
if (OnResolving != null)
|
|
|
|
|
OnResolving();
|
2016-02-04 14:17:15 +01:00
|
|
|
|
var links = await Searches.FindYoutubeUrlByKeywords(Query);
|
|
|
|
|
var videos = await YouTube.Default.GetAllVideosAsync(links);
|
|
|
|
|
video = videos
|
|
|
|
|
.Where(v => v.AdaptiveKind == AdaptiveKind.Audio)
|
|
|
|
|
.OrderByDescending(v => v.AudioBitrate)
|
|
|
|
|
.FirstOrDefault();
|
2016-01-26 20:18:48 +01:00
|
|
|
|
|
|
|
|
|
if (video == null) // do something with this error
|
2016-01-28 04:38:26 +01:00
|
|
|
|
throw new Exception("Could not load any video elements based on the query.");
|
2016-01-26 20:18:48 +01:00
|
|
|
|
|
2016-01-31 06:45:25 +01:00
|
|
|
|
Title = video.Title; //.Substring(0,video.Title.Length-10); // removing trailing "- You Tube"
|
2016-01-29 13:28:51 +01:00
|
|
|
|
} catch (Exception ex) {
|
|
|
|
|
privateState = StreamState.Completed;
|
2016-01-30 13:51:47 +01:00
|
|
|
|
if (OnResolvingFailed != null)
|
|
|
|
|
OnResolvingFailed(ex.Message);
|
2016-01-29 13:28:51 +01:00
|
|
|
|
Console.WriteLine($"Failed resolving the link.{ex.Message}");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2016-01-26 20:18:48 +01:00
|
|
|
|
|
2016-02-02 23:41:13 +01:00
|
|
|
|
musicStreamer = new MusicStreamer(this, video.Uri);
|
2016-01-29 13:28:51 +01:00
|
|
|
|
if (OnQueued != null)
|
|
|
|
|
OnQueued();
|
|
|
|
|
}
|
2016-01-26 20:18:48 +01:00
|
|
|
|
|
2016-01-28 04:38:26 +01:00
|
|
|
|
internal string PrintStats() => musicStreamer?.Stats();
|
|
|
|
|
|
2016-01-26 20:18:48 +01:00
|
|
|
|
public Action OnQueued = null;
|
|
|
|
|
public Action OnBuffering = null;
|
|
|
|
|
public Action OnStarted = null;
|
|
|
|
|
public Action OnCompleted = null;
|
2016-01-30 13:51:47 +01:00
|
|
|
|
public Action OnResolving = null;
|
|
|
|
|
public Action<string> OnResolvingFailed = null;
|
2016-01-26 20:18:48 +01:00
|
|
|
|
|
|
|
|
|
internal void Cancel() {
|
2016-01-28 04:38:26 +01:00
|
|
|
|
musicStreamer?.Cancel();
|
2016-01-26 20:18:48 +01:00
|
|
|
|
}
|
|
|
|
|
|
2016-01-29 13:28:51 +01:00
|
|
|
|
internal void Stop() {
|
|
|
|
|
musicStreamer?.Stop();
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-30 05:24:32 +01:00
|
|
|
|
internal async Task Start() {
|
|
|
|
|
int attemptsLeft = 4;
|
|
|
|
|
//wait for up to 4 seconds to resolve a link
|
|
|
|
|
try {
|
2016-01-26 20:18:48 +01:00
|
|
|
|
while (State == StreamState.Resolving) {
|
|
|
|
|
await Task.Delay(1000);
|
|
|
|
|
if (--attemptsLeft == 0) {
|
2016-01-29 13:28:51 +01:00
|
|
|
|
throw new TimeoutException("Resolving timed out.");
|
2016-01-26 20:18:48 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-01-30 05:24:32 +01:00
|
|
|
|
await musicStreamer.StartPlayback();
|
|
|
|
|
} catch (TimeoutException) {
|
|
|
|
|
Console.WriteLine("Resolving timed out.");
|
|
|
|
|
privateState = StreamState.Completed;
|
|
|
|
|
} catch (Exception ex) {
|
|
|
|
|
Console.WriteLine("Error in start playback." + ex.Message);
|
|
|
|
|
privateState = StreamState.Completed;
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-01-26 20:18:48 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class MusicStreamer {
|
|
|
|
|
private DualStream buffer;
|
|
|
|
|
|
|
|
|
|
public StreamState State { get; internal set; }
|
2016-01-28 04:38:26 +01:00
|
|
|
|
public string Url { get; }
|
|
|
|
|
private bool IsCanceled { get; set; }
|
2016-01-29 13:28:51 +01:00
|
|
|
|
public bool IsPaused => parent.IsPaused;
|
2016-01-26 20:18:48 +01:00
|
|
|
|
|
|
|
|
|
StreamRequest parent;
|
|
|
|
|
private readonly object _bufferLock = new object();
|
2016-01-30 05:24:32 +01:00
|
|
|
|
private bool prebufferingComplete = false;
|
2016-01-27 06:33:31 +01:00
|
|
|
|
|
2016-02-02 00:05:41 +01:00
|
|
|
|
public MusicStreamer(StreamRequest parent, string directUrl) {
|
2016-01-26 20:18:48 +01:00
|
|
|
|
this.parent = parent;
|
|
|
|
|
this.buffer = new DualStream();
|
|
|
|
|
this.Url = directUrl;
|
|
|
|
|
State = StreamState.Queued;
|
2016-01-27 06:33:31 +01:00
|
|
|
|
}
|
|
|
|
|
|
2016-01-28 04:38:26 +01:00
|
|
|
|
public string Stats() =>
|
|
|
|
|
"--------------------------------\n" +
|
2016-01-30 12:08:30 +01:00
|
|
|
|
$"Music stats for {string.Join("", parent.Title.TrimTo(50))}\n" +
|
2016-01-28 04:38:26 +01:00
|
|
|
|
$"Server: {parent.Server.Name}\n" +
|
|
|
|
|
$"Length:{buffer.Length * 1.0f / 1.MB()}MB Status: {State}\n" +
|
|
|
|
|
"--------------------------------\n";
|
2016-02-02 12:23:58 +01:00
|
|
|
|
|
2016-01-26 20:18:48 +01:00
|
|
|
|
private async Task BufferSong() {
|
|
|
|
|
//start feeding the buffer
|
|
|
|
|
var p = Process.Start(new ProcessStartInfo {
|
|
|
|
|
FileName = "ffmpeg",
|
|
|
|
|
Arguments = $"-i {Url} -f s16le -ar 48000 -ac 2 pipe:1",
|
|
|
|
|
UseShellExecute = false,
|
|
|
|
|
RedirectStandardOutput = true,
|
2016-02-03 10:55:14 +01:00
|
|
|
|
RedirectStandardError = false,
|
2016-01-26 20:18:48 +01:00
|
|
|
|
CreateNoWindow = true,
|
2016-01-27 06:33:31 +01:00
|
|
|
|
WindowStyle = ProcessWindowStyle.Hidden,
|
2016-01-26 20:18:48 +01:00
|
|
|
|
});
|
2016-01-29 08:23:15 +01:00
|
|
|
|
int attempt = 0;
|
2016-01-26 20:18:48 +01:00
|
|
|
|
while (true) {
|
2016-02-04 14:17:15 +01:00
|
|
|
|
int magickBuffer = 1;
|
2016-01-30 05:24:32 +01:00
|
|
|
|
//wait for the read pos to catch up with write pos
|
2016-02-04 14:17:15 +01:00
|
|
|
|
while (buffer.writePos - buffer.readPos > 1.MB() && State != StreamState.Completed) {
|
2016-01-30 05:24:32 +01:00
|
|
|
|
prebufferingComplete = true;
|
2016-02-04 14:17:15 +01:00
|
|
|
|
await Task.Delay(150);
|
2016-01-27 06:48:48 +01:00
|
|
|
|
}
|
2016-01-29 13:28:51 +01:00
|
|
|
|
|
|
|
|
|
if (State == StreamState.Completed) {
|
|
|
|
|
try {
|
|
|
|
|
p.CancelOutputRead();
|
|
|
|
|
p.Close();
|
|
|
|
|
} catch (Exception) { }
|
|
|
|
|
Console.WriteLine("Buffering canceled, stream is completed.");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-04 14:17:15 +01:00
|
|
|
|
if (buffer.readPos > 1.MiB() && buffer.writePos > 1.MiB()) { // if buffer is over 5 MiB, create new one
|
|
|
|
|
var skip = 1.MB(); //remove only 5 MB, just in case
|
2016-01-28 04:38:26 +01:00
|
|
|
|
var newBuffer = new DualStream();
|
2016-01-29 08:23:15 +01:00
|
|
|
|
|
2016-01-26 20:18:48 +01:00
|
|
|
|
lock (_bufferLock) {
|
2016-01-29 08:23:15 +01:00
|
|
|
|
byte[] data = buffer.ToArray().Skip(skip).ToArray();
|
2016-01-26 20:18:48 +01:00
|
|
|
|
var newReadPos = buffer.readPos - skip;
|
|
|
|
|
var newPos = buffer.Position - skip;
|
2016-01-28 04:38:26 +01:00
|
|
|
|
buffer = newBuffer;
|
2016-01-26 20:18:48 +01:00
|
|
|
|
buffer.Write(data, 0, data.Length);
|
|
|
|
|
buffer.readPos = newReadPos;
|
|
|
|
|
buffer.Position = newPos;
|
|
|
|
|
}
|
2016-01-29 13:28:51 +01:00
|
|
|
|
}
|
2016-01-29 08:23:15 +01:00
|
|
|
|
|
2016-01-26 20:18:48 +01:00
|
|
|
|
var buf = new byte[1024];
|
|
|
|
|
int read = 0;
|
|
|
|
|
read = await p.StandardOutput.BaseStream.ReadAsync(buf, 0, 1024);
|
2016-01-28 04:38:26 +01:00
|
|
|
|
//Console.WriteLine($"Read: {read}");
|
2016-01-26 20:18:48 +01:00
|
|
|
|
if (read == 0) {
|
2016-02-02 00:05:41 +01:00
|
|
|
|
if (attempt == 5) {
|
2016-01-29 08:23:15 +01:00
|
|
|
|
try {
|
|
|
|
|
p.CancelOutputRead();
|
|
|
|
|
p.Close();
|
|
|
|
|
} catch (Exception) { }
|
2016-01-27 06:48:48 +01:00
|
|
|
|
|
2016-01-29 08:23:15 +01:00
|
|
|
|
Console.WriteLine($"Didn't read anything from the stream for {attempt} attempts. {buffer.Length/1.MB()}MB length");
|
|
|
|
|
return;
|
|
|
|
|
} else {
|
|
|
|
|
++attempt;
|
2016-02-02 00:05:41 +01:00
|
|
|
|
await Task.Delay(20);
|
2016-01-29 08:23:15 +01:00
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
attempt = 0;
|
|
|
|
|
await buffer.WriteAsync(buf, 0, read);
|
2016-01-26 20:18:48 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-28 04:38:26 +01:00
|
|
|
|
internal async Task StartPlayback() {
|
|
|
|
|
Console.WriteLine("Starting playback.");
|
2016-01-29 13:28:51 +01:00
|
|
|
|
if (State == StreamState.Playing) return;
|
2016-01-28 04:38:26 +01:00
|
|
|
|
State = StreamState.Playing;
|
|
|
|
|
if (parent.OnBuffering != null)
|
|
|
|
|
parent.OnBuffering();
|
2016-02-04 14:17:15 +01:00
|
|
|
|
|
|
|
|
|
Task.Factory.StartNew(async () => {
|
|
|
|
|
await BufferSong();
|
|
|
|
|
}, TaskCreationOptions.LongRunning).ConfigureAwait(false);
|
2016-01-30 05:24:32 +01:00
|
|
|
|
|
|
|
|
|
// prebuffering wait stuff start
|
|
|
|
|
int bufferAttempts = 0;
|
|
|
|
|
int waitPerAttempt = 500;
|
2016-01-31 09:22:47 +01:00
|
|
|
|
while (!prebufferingComplete && bufferAttempts++ < 15) {
|
2016-01-30 05:24:32 +01:00
|
|
|
|
await Task.Delay(waitPerAttempt);
|
|
|
|
|
}
|
|
|
|
|
if (prebufferingComplete) {
|
|
|
|
|
Console.WriteLine($"Prebuffering finished in {bufferAttempts*500}");
|
2016-01-28 04:38:26 +01:00
|
|
|
|
}
|
2016-01-30 05:24:32 +01:00
|
|
|
|
// prebuffering wait stuff end
|
2016-01-30 13:51:47 +01:00
|
|
|
|
|
2016-01-28 04:38:26 +01:00
|
|
|
|
if (buffer.Length > 0) {
|
2016-01-26 20:18:48 +01:00
|
|
|
|
Console.WriteLine("Prebuffering complete.");
|
2016-01-28 04:38:26 +01:00
|
|
|
|
} else {
|
|
|
|
|
Console.WriteLine("Didn't buffer jack shit.");
|
|
|
|
|
}
|
2016-01-29 08:23:15 +01:00
|
|
|
|
|
2016-01-28 04:38:26 +01:00
|
|
|
|
int blockSize = 1920 * NadekoBot.client.Audio().Config.Channels;
|
|
|
|
|
byte[] voiceBuffer = new byte[blockSize];
|
2016-01-26 20:18:48 +01:00
|
|
|
|
|
2016-01-30 13:51:47 +01:00
|
|
|
|
if (parent.OnStarted != null)
|
|
|
|
|
parent.OnStarted();
|
|
|
|
|
|
2016-01-29 08:23:15 +01:00
|
|
|
|
int attempt = 0;
|
2016-01-28 04:38:26 +01:00
|
|
|
|
while (!IsCanceled) {
|
|
|
|
|
int readCount = 0;
|
2016-02-04 14:17:15 +01:00
|
|
|
|
//adjust volume
|
|
|
|
|
|
2016-01-28 04:38:26 +01:00
|
|
|
|
lock (_bufferLock) {
|
|
|
|
|
readCount = buffer.Read(voiceBuffer, 0, voiceBuffer.Length);
|
|
|
|
|
}
|
2016-01-26 20:18:48 +01:00
|
|
|
|
|
2016-01-28 04:38:26 +01:00
|
|
|
|
if (readCount == 0) {
|
2016-02-04 06:46:30 +01:00
|
|
|
|
if (attempt == 4) {
|
2016-02-03 10:55:14 +01:00
|
|
|
|
Console.WriteLine($"Failed to read {attempt} times. Breaking out. [{DateTime.Now.Second}]");
|
2016-01-29 08:23:15 +01:00
|
|
|
|
break;
|
|
|
|
|
} else {
|
|
|
|
|
++attempt;
|
2016-02-04 06:46:30 +01:00
|
|
|
|
await Task.Delay(15);
|
2016-01-29 08:23:15 +01:00
|
|
|
|
}
|
|
|
|
|
} else
|
|
|
|
|
attempt = 0;
|
2016-01-26 20:18:48 +01:00
|
|
|
|
|
2016-01-28 04:38:26 +01:00
|
|
|
|
if (State == StreamState.Completed) {
|
|
|
|
|
Console.WriteLine("Canceled");
|
|
|
|
|
break;
|
2016-01-26 20:18:48 +01:00
|
|
|
|
}
|
2016-02-04 14:17:15 +01:00
|
|
|
|
voiceBuffer = adjustVolume(voiceBuffer, parent.Volume);
|
2016-01-31 19:51:27 +01:00
|
|
|
|
parent.MusicControls.VoiceClient.Send(voiceBuffer, 0, voiceBuffer.Length);
|
2016-01-29 13:28:51 +01:00
|
|
|
|
|
|
|
|
|
while (IsPaused) {
|
|
|
|
|
await Task.Delay(50);
|
|
|
|
|
}
|
2016-01-28 04:38:26 +01:00
|
|
|
|
}
|
2016-01-31 19:51:27 +01:00
|
|
|
|
parent.MusicControls.VoiceClient.Wait();
|
2016-01-29 13:28:51 +01:00
|
|
|
|
Stop();
|
2016-01-28 04:38:26 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal void Cancel() {
|
|
|
|
|
IsCanceled = true;
|
|
|
|
|
}
|
2016-01-26 20:18:48 +01:00
|
|
|
|
|
2016-01-29 13:28:51 +01:00
|
|
|
|
internal void Stop() {
|
2016-02-04 14:17:15 +01:00
|
|
|
|
if (State == StreamState.Completed) return;
|
|
|
|
|
var oldState = State;
|
|
|
|
|
State = StreamState.Completed;
|
|
|
|
|
if (oldState == StreamState.Playing)
|
|
|
|
|
if (parent.OnCompleted != null)
|
|
|
|
|
parent.OnCompleted();
|
|
|
|
|
}
|
|
|
|
|
//stackoverflow ftw
|
|
|
|
|
private byte[] adjustVolume(byte[] audioSamples, float volume) {
|
|
|
|
|
if (volume == 1.0f)
|
|
|
|
|
return audioSamples;
|
|
|
|
|
byte[] array = new byte[audioSamples.Length];
|
|
|
|
|
for (int i = 0; i < array.Length; i += 2) {
|
|
|
|
|
|
|
|
|
|
// convert byte pair to int
|
|
|
|
|
short buf1 = audioSamples[i + 1];
|
|
|
|
|
short buf2 = audioSamples[i];
|
|
|
|
|
|
|
|
|
|
buf1 = (short)((buf1 & 0xff) << 8);
|
|
|
|
|
buf2 = (short)(buf2 & 0xff);
|
|
|
|
|
|
|
|
|
|
short res = (short)(buf1 | buf2);
|
|
|
|
|
res = (short)(res * volume);
|
|
|
|
|
|
|
|
|
|
// convert back
|
|
|
|
|
array[i] = (byte)res;
|
|
|
|
|
array[i + 1] = (byte)(res >> 8);
|
|
|
|
|
|
2016-01-28 04:38:26 +01:00
|
|
|
|
}
|
2016-02-04 14:17:15 +01:00
|
|
|
|
return array;
|
2016-01-26 20:18:48 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class DualStream : MemoryStream {
|
|
|
|
|
public long readPos;
|
|
|
|
|
public long writePos;
|
|
|
|
|
|
2016-01-28 04:38:26 +01:00
|
|
|
|
public DualStream() : base() {
|
|
|
|
|
readPos = writePos = 0;
|
|
|
|
|
}
|
2016-01-26 20:18:48 +01:00
|
|
|
|
|
|
|
|
|
public override int Read(byte[] buffer, int offset, int count) {
|
|
|
|
|
int read;
|
|
|
|
|
lock (this) {
|
|
|
|
|
Position = readPos;
|
|
|
|
|
read = base.Read(buffer, offset, count);
|
|
|
|
|
readPos = Position;
|
|
|
|
|
}
|
|
|
|
|
return read;
|
|
|
|
|
}
|
|
|
|
|
public override void Write(byte[] buffer, int offset, int count) {
|
|
|
|
|
lock (this) {
|
|
|
|
|
Position = writePos;
|
|
|
|
|
base.Write(buffer, offset, count);
|
|
|
|
|
writePos = Position;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|