back to old music code
This commit is contained in:
parent
39e58bd6cc
commit
6c80f36ecd
@ -155,8 +155,6 @@ namespace NadekoBot.Classes.Music {
|
|||||||
private readonly object _bufferLock = new object();
|
private readonly object _bufferLock = new object();
|
||||||
private bool prebufferingComplete = false;
|
private bool prebufferingComplete = false;
|
||||||
|
|
||||||
private Process ffmpegProcess;
|
|
||||||
|
|
||||||
public MusicStreamer(StreamRequest parent, string directUrl) {
|
public MusicStreamer(StreamRequest parent, string directUrl) {
|
||||||
this.parent = parent;
|
this.parent = parent;
|
||||||
this.buffer = new DualStream();
|
this.buffer = new DualStream();
|
||||||
@ -182,18 +180,59 @@ namespace NadekoBot.Classes.Music {
|
|||||||
CreateNoWindow = true,
|
CreateNoWindow = true,
|
||||||
WindowStyle = ProcessWindowStyle.Hidden,
|
WindowStyle = ProcessWindowStyle.Hidden,
|
||||||
});
|
});
|
||||||
|
int attempt = 0;
|
||||||
ffmpegProcess = p;
|
while (true) {
|
||||||
await Task.Delay(1000);
|
while (buffer.writePos - buffer.readPos > 5.MB() && State != StreamState.Completed) {
|
||||||
prebufferingComplete = true;
|
prebufferingComplete = true;
|
||||||
|
await Task.Delay(200);
|
||||||
while (State != StreamState.Completed) {
|
|
||||||
await Task.Delay(500);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (State == StreamState.Completed) {
|
||||||
try {
|
try {
|
||||||
p.CancelOutputRead();
|
p.CancelOutputRead();
|
||||||
p.Close();
|
p.Close();
|
||||||
} catch (Exception) { }
|
} 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() {
|
internal async Task StartPlayback() {
|
||||||
@ -217,13 +256,13 @@ namespace NadekoBot.Classes.Music {
|
|||||||
Console.WriteLine($"Prebuffering finished in {bufferAttempts*500}");
|
Console.WriteLine($"Prebuffering finished in {bufferAttempts*500}");
|
||||||
}
|
}
|
||||||
// prebuffering wait stuff end
|
// prebuffering wait stuff end
|
||||||
/*
|
|
||||||
if (buffer.Length > 0) {
|
if (buffer.Length > 0) {
|
||||||
Console.WriteLine("Prebuffering complete.");
|
Console.WriteLine("Prebuffering complete.");
|
||||||
} else {
|
} else {
|
||||||
Console.WriteLine("Didn't buffer jack shit.");
|
Console.WriteLine("Didn't buffer jack shit.");
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
int blockSize = 1920 * NadekoBot.client.Audio().Config.Channels;
|
int blockSize = 1920 * NadekoBot.client.Audio().Config.Channels;
|
||||||
byte[] voiceBuffer = new byte[blockSize];
|
byte[] voiceBuffer = new byte[blockSize];
|
||||||
|
|
||||||
@ -236,9 +275,7 @@ namespace NadekoBot.Classes.Music {
|
|||||||
//adjust volume
|
//adjust volume
|
||||||
|
|
||||||
lock (_bufferLock) {
|
lock (_bufferLock) {
|
||||||
readCount = ffmpegProcess.StandardOutput.BaseStream.Read(voiceBuffer, 0, voiceBuffer.Length);
|
readCount = buffer.Read(voiceBuffer, 0, voiceBuffer.Length);
|
||||||
if(readCount!=3840)
|
|
||||||
Console.WriteLine("Read: "+ readCount);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (readCount == 0) {
|
if (readCount == 0) {
|
||||||
|
Loading…
Reference in New Issue
Block a user