removed buffering completely from music, sending ffmpeg stream directly

This commit is contained in:
Master Kwoth 2016-02-08 00:44:45 +01:00
parent 2d52d18847
commit a644a5857c
2 changed files with 18 additions and 59 deletions

View File

@ -32,13 +32,9 @@ namespace NadekoBot.Classes {
}
internal void InsertMany<T>(T objects) where T : IEnumerable<IDataModel> {
try {
using (var _conn = new SQLiteConnection(_filePath)) {
_conn.InsertAll(objects);
}
} catch (Exception ex) {
Console.WriteLine(ex);
}
}
internal void UpdateData<T>(T o) where T : IDataModel {

View File

@ -155,6 +155,8 @@ namespace NadekoBot.Classes.Music {
private readonly object _bufferLock = new object();
private bool prebufferingComplete = false;
private Process ffmpegProcess;
public MusicStreamer(StreamRequest parent, string directUrl) {
this.parent = parent;
this.buffer = new DualStream();
@ -180,59 +182,18 @@ namespace NadekoBot.Classes.Music {
CreateNoWindow = true,
WindowStyle = ProcessWindowStyle.Hidden,
});
int attempt = 0;
while (true) {
while (buffer.writePos - buffer.readPos > 5.MB() && State != StreamState.Completed) {
ffmpegProcess = p;
await Task.Delay(1000);
prebufferingComplete = true;
await Task.Delay(200);
}
if (State == StreamState.Completed) {
while (State != StreamState.Completed) {
await Task.Delay(500);
}
try {
p.CancelOutputRead();
p.Close();
} catch (Exception) { }
Console.WriteLine("Buffering canceled, stream is completed.");
return;
}
if (buffer.readPos > 5.MiB() && buffer.writePos > 5.MiB()) { // if buffer is over 5 MiB, create new one
var skip = 5.MB(); //remove only 5 MB, just in case
var newBuffer = new DualStream();
lock (_bufferLock) {
byte[] data = buffer.ToArray().Skip(skip).ToArray();
var newReadPos = buffer.readPos - skip;
var newPos = buffer.Position - skip;
buffer = newBuffer;
buffer.Write(data, 0, data.Length);
buffer.readPos = newReadPos;
buffer.Position = newPos;
}
}
var buf = new byte[2048];
int read = 0;
read = await p.StandardOutput.BaseStream.ReadAsync(buf, 0, 2048);
//Console.WriteLine($"Read: {read}");
if (read == 0) {
if (attempt == 5) {
try {
p.CancelOutputRead();
p.Close();
} catch (Exception) { }
Console.WriteLine($"Didn't read anything from the stream for {attempt} attempts. {buffer.Length/1.MB()}MB length");
return;
} else {
++attempt;
await Task.Delay(20);
}
} else {
attempt = 0;
await buffer.WriteAsync(buf, 0, read);
}
}
}
internal async Task StartPlayback() {
@ -256,13 +217,13 @@ namespace NadekoBot.Classes.Music {
Console.WriteLine($"Prebuffering finished in {bufferAttempts*500}");
}
// prebuffering wait stuff end
/*
if (buffer.Length > 0) {
Console.WriteLine("Prebuffering complete.");
} else {
Console.WriteLine("Didn't buffer jack shit.");
}
*/
int blockSize = 1920 * NadekoBot.client.Audio().Config.Channels;
byte[] voiceBuffer = new byte[blockSize];
@ -275,7 +236,9 @@ namespace NadekoBot.Classes.Music {
//adjust volume
lock (_bufferLock) {
readCount = buffer.Read(voiceBuffer, 0, voiceBuffer.Length);
readCount = ffmpegProcess.StandardOutput.BaseStream.Read(voiceBuffer, 0, voiceBuffer.Length);
if(readCount!=3840)
Console.WriteLine("Read: "+ readCount);
}
if (readCount == 0) {