sedmelluq / lavaplayer

Audio player library for Discord
Apache License 2.0
966 stars 247 forks source link

Traffic routing #221

Closed stefan-reich closed 4 years ago

stefan-reich commented 5 years ago

Hi, we just launched a Discord radio bot using JDA + LavaPlayer. Works great.

Question though: How does the routing work? We are streaming ShoutCast to Discord.

This is the traffic on my server with multiple streams: nethugs output

Downstream looks plausible, but upstream is really too low to contain any audio.

Does LavaPlayer somehow stream directly from server to server?

I don't see how that would be possible though as I'm linking JDA and LavaPlayer using an AudioSendHandler... so the music HAS to go through my bot, right?

Confusing. :-)

freyacodes commented 5 years ago

The AudioSendHandler sends frames directly to a Discord voice server.

Bear in mind that 68KB/sec is around 544Kbps. Enough to contain multiple streams at high quality.

stefan-reich commented 5 years ago

Thanks @Frederikam. The upstream is only <3KB/sec though - and I took the screenshot when it was at the highest value.

I think I know how to go about optimizing this though... It's a radio bot, so we only ever need to handle one incoming ShoutCast stream. We just convert the stream to Opus once and send the same byte array to all connected Discord voice channels. So there kind of is redundant traffic from me to Discord (all the duplicated byte arrays), but then again, Discord just doesn't know the concept of a radio. And possibly the streams are going to different servers, so the traffic is not actually wasted.

The real challenge in all this is to do actual Realtime Java :-D I think I can actually do it using a combination of tricks (sub 10 ms garbage collector, dedicated CPU etc.).

MCOfficer commented 5 years ago

Thanks @Frederikam. The upstream is only <3KB/sec though - and I took the screenshot when it was at the highest value.

I think I know how to go about optimizing this though... It's a radio bot, so we only ever need to handle one incoming ShoutCast stream. We just convert the stream to Opus once and send the same byte array to all connected Discord voice channels. So there kind of is redundant traffic from me to Discord (all the duplicated byte arrays), but then again, Discord just doesn't know the concept of a radio. And possibly the streams are going to different servers, so the traffic is not actually wasted.

The real challenge in all this is to do actual Realtime Java :-D I think I can actually do it using a combination of tricks (sub 10 ms garbage collector, dedicated CPU etc.).

to how many channels are you usually connected at once? 3KB/s should still be enough for up to 5 streams with medium quality.

freyacodes commented 5 years ago

We just convert the stream to Opus once and send the same byte array to all connected Discord voice channels.

Been there done that.

https://github.com/Frederikam/GensokyoBot/blob/master/FredBoat/src/main/java/com/frederikam/gensokyobot/audio/StreamCombiner.java

Beware the ugly outdated code.

stefan-reich commented 5 years ago

Thanks @Frederikam. The upstream is only <3KB/sec though - and I took the screenshot when it was at the highest value. I think I know how to go about optimizing this though... It's a radio bot, so we only ever need to handle one incoming ShoutCast stream. We just convert the stream to Opus once and send the same byte array to all connected Discord voice channels. So there kind of is redundant traffic from me to Discord (all the duplicated byte arrays), but then again, Discord just doesn't know the concept of a radio. And possibly the streams are going to different servers, so the traffic is not actually wasted. The real challenge in all this is to do actual Realtime Java :-D I think I can actually do it using a combination of tricks (sub 10 ms garbage collector, dedicated CPU etc.).

to how many channels are you usually connected at once? 3KB/s should still be enough for up to 5 streams with medium quality.

<= 4 channels I think, it's a new bot. I'm confused though, doesn't Discord allow us to choose between 64kbit and 96kbit? We selected 96. The music we hear does sound like that level of quality too. I'm really not sure how you're saying an audio stream takes less than 1 KB/s?

Thanks for the link.

freyacodes commented 5 years ago

Bots ignore the bitrate limit. I believe the true limit is always 128Kbps

stefan-reich commented 5 years ago

Then it should be way more than 3KB/s... the screenshot is the output from nethogs BTW.

On Mon, 30 Sep 2019 at 22:04, Frederik Ar. Mikkelsen < notifications@github.com> wrote:

Bots ignore the bitrate limit. I believe the true limit is always 128Kbps

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/sedmelluq/lavaplayer/issues/221?email_source=notifications&email_token=ABCPUQ4DR37ZEIY6XSH6CADQMJLTLA5CNFSM4I3ON432YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD765GXI#issuecomment-536728413, or mute the thread https://github.com/notifications/unsubscribe-auth/ABCPUQ4OZSR7GN3MBVGEKFTQMJLTLANCNFSM4I3ON43Q .

-- Stefan Reich BotCompany.de // Java-based operating systems

sedmelluq commented 4 years ago

Perhaps whatever showed the transfer speeds was not counting UDP. Closing as this is not an LP issue and is old.