not-ilinked / Anarchy

The superior Discord API wrapper

Home Page:https://anarchyteam.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Issue with stream closing unexpectedly

BomboBombone opened this issue · comments

commented

When trying to build the music bot provided in the demo project i noted that the stream was closing unexpectedly soon on many songs. I also noted that this problem was cause by the Read method of the audio stream from ffmpeg sending a 0 (so no bytes read from the stream) practically mid-song. To fix this problem i switched to using the GetAudio method of your lib instead, and the copyFrom method version that uses a bytearray instead of a stream, and this way the issue was fixed. The problem is that for longer songs it need to load everything in ram before playing, so I'd like it to work with the stream instead. Thank you for your support

Can confirm this being a thing, but i'm not sure of how to fix it.

commented

I managed to fix it myself, we can discuss it in private in detail if you want and I'll tell you how to fix it. My fix is temporary but the idea seems to work fine, so I can share it with you. I spent the last 5 days on this bug.
I wont share my code right now due to it not being fully optimized

@not-ilinked Does the socket use some sort of "ping" to the server? Alot of popular apps do that to keep it "Alive"

@Jreverse Anarchy already does that. If it didn't it'd be kicked off the voice channel pretty quickly

commented

the whole problem existed due to a conflict with ffmpeg stream and Microsoft's read function for "big" streams. To implement a pseudo stream you'd need to generate a series of small audio streams with ffmpeg and read them. To avoid lagging and stuttering in the process of passing from a stream to another you'd just create a "back buffer" audio stream which is generated while the main buffer is being read from and sent to discord. My optimal buffer duration is 3 seconds but for implementation purposes I pumped it up to 5 seconds, giving even the slowest computers and connections the time to create the back buffer stream for the next 5 seconds, and so on. This opens the doors for audio manipulation with ffmpeg. For example in my project the bot is able to change the playback speed and volume.