-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bump to 0.2.9 and remove jda-nas requirement
- Loading branch information
1 parent
a68c6e2
commit f54ad34
Showing
5 changed files
with
173 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
api/src/main/java/com/sedmelluq/discord/lavaplayer/jdaudp/NativeAudioSendFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package com.sedmelluq.discord.lavaplayer.jdaudp; | ||
|
||
import com.sedmelluq.discord.lavaplayer.udpqueue.natives.UdpQueueManager; | ||
import com.sedmelluq.lava.common.tools.DaemonThreadFactory; | ||
import com.sedmelluq.lava.common.tools.ExecutorTools; | ||
import net.dv8tion.jda.api.audio.factory.IAudioSendFactory; | ||
import net.dv8tion.jda.api.audio.factory.IAudioSendSystem; | ||
import net.dv8tion.jda.api.audio.factory.IPacketProvider; | ||
|
||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.ConcurrentHashMap.KeySetView; | ||
import java.util.concurrent.ScheduledExecutorService; | ||
import java.util.concurrent.ScheduledThreadPoolExecutor; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.atomic.AtomicLong; | ||
|
||
public class NativeAudioSendFactory implements IAudioSendFactory { | ||
private static final int DEFAULT_BUFFER_DURATION = 400; | ||
private static final int PACKET_INTERVAL = 20; | ||
private static final int MAXIMUM_PACKET_SIZE = 4096; | ||
|
||
private final int bufferDuration; | ||
private final AtomicLong identifierCounter = new AtomicLong(); | ||
private final KeySetView<NativeAudioSendSystem, Boolean> systems = ConcurrentHashMap.newKeySet(); | ||
private final Object lock = new Object(); | ||
private volatile UdpQueueManager queueManager; | ||
private ScheduledExecutorService scheduler; | ||
|
||
public NativeAudioSendFactory() { | ||
this(DEFAULT_BUFFER_DURATION); | ||
} | ||
|
||
public NativeAudioSendFactory(int bufferDuration) { | ||
this.bufferDuration = bufferDuration; | ||
} | ||
|
||
private void initialiseQueueManager() { | ||
scheduler = new ScheduledThreadPoolExecutor(1, new DaemonThreadFactory("native-udp")); | ||
queueManager = new UdpQueueManager(bufferDuration / PACKET_INTERVAL, | ||
TimeUnit.MILLISECONDS.toNanos(PACKET_INTERVAL), MAXIMUM_PACKET_SIZE); | ||
|
||
scheduler.scheduleWithFixedDelay(this::populateQueues, 0, 40, TimeUnit.MILLISECONDS); | ||
|
||
Thread thread = new Thread(process(queueManager)); | ||
thread.setPriority((Thread.NORM_PRIORITY + Thread.MAX_PRIORITY) / 2); | ||
thread.setDaemon(true); | ||
thread.start(); | ||
} | ||
|
||
private ScheduledExecutorService shutdownQueueManager() { | ||
queueManager.close(); | ||
queueManager = null; | ||
|
||
ScheduledExecutorService currentScheduler = scheduler; | ||
scheduler = null; | ||
return currentScheduler; | ||
} | ||
|
||
@Override | ||
public IAudioSendSystem createSendSystem(IPacketProvider packetProvider) { | ||
return new NativeAudioSendSystem(identifierCounter.incrementAndGet(), this, packetProvider); | ||
} | ||
|
||
void addInstance(NativeAudioSendSystem system) { | ||
synchronized (lock) { | ||
systems.add(system); | ||
|
||
if (queueManager == null) { | ||
initialiseQueueManager(); | ||
} | ||
} | ||
} | ||
|
||
void removeInstance(NativeAudioSendSystem system) { | ||
ScheduledExecutorService schedulerToShutDown = null; | ||
|
||
synchronized (lock) { | ||
if (systems.remove(system) && systems.isEmpty() && queueManager != null) { | ||
schedulerToShutDown = shutdownQueueManager(); | ||
} | ||
} | ||
|
||
if (schedulerToShutDown != null) { | ||
ExecutorTools.shutdownExecutor(schedulerToShutDown, "native udp queue populator"); | ||
} | ||
} | ||
|
||
private void populateQueues() { | ||
UdpQueueManager manager = queueManager; /* avoid getfield opcode */ | ||
|
||
if (manager != null) { | ||
for (NativeAudioSendSystem system : systems) { | ||
system.populateQueue(manager); | ||
} | ||
} | ||
} | ||
|
||
private static Runnable process(UdpQueueManager unbake) { | ||
return unbake::process; | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
api/src/main/java/com/sedmelluq/discord/lavaplayer/jdaudp/NativeAudioSendSystem.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package com.sedmelluq.discord.lavaplayer.jdaudp; | ||
|
||
import com.sedmelluq.discord.lavaplayer.udpqueue.natives.UdpQueueManager; | ||
import net.dv8tion.jda.api.audio.factory.IAudioSendSystem; | ||
import net.dv8tion.jda.api.audio.factory.IPacketProvider; | ||
|
||
import java.net.InetSocketAddress; | ||
import java.nio.ByteBuffer; | ||
|
||
public class NativeAudioSendSystem implements IAudioSendSystem { | ||
private final long queueKey; | ||
private final NativeAudioSendFactory audioSendSystem; | ||
private final IPacketProvider packetProvider; | ||
|
||
public NativeAudioSendSystem(long queueKey, NativeAudioSendFactory audioSendSystem, IPacketProvider packetProvider) { | ||
this.queueKey = queueKey; | ||
this.audioSendSystem = audioSendSystem; | ||
this.packetProvider = packetProvider; | ||
} | ||
|
||
@Override | ||
public void start() { | ||
audioSendSystem.addInstance(this); | ||
} | ||
|
||
@Override | ||
public void shutdown() { | ||
audioSendSystem.removeInstance(this); | ||
} | ||
|
||
void populateQueue(UdpQueueManager queueManager) { | ||
int remaining = queueManager.getRemainingCapacity(queueKey); | ||
boolean emptyQueue = queueManager.getCapacity() - remaining > 0; | ||
|
||
for (int i = 0; i < remaining; i++) { | ||
ByteBuffer packet = packetProvider.getNextPacketRaw(emptyQueue); | ||
InetSocketAddress address = packetProvider.getSocketAddress(); | ||
|
||
if (packet == null || !queueManager.queuePacket(queueKey, packet, address)) { | ||
break; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters