Skip to content

Commit

Permalink
Add base api
Browse files Browse the repository at this point in the history
  • Loading branch information
Mindgamesnl committed Feb 1, 2024
1 parent add57d3 commit faee9b4
Show file tree
Hide file tree
Showing 7 changed files with 158 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class DefaultApi implements AudioApi {
private final WorldApiImpl worldApi = new WorldApiImpl();
private final MediaApiImpl mediaApi = new MediaApiImpl();
private final RegistryApiImpl registryApi = new RegistryApiImpl();
private final VoiceApiImpl voiceApi = new VoiceApiImpl();

public static AudioApi i() {
if (instance != null) return instance;
Expand Down Expand Up @@ -58,4 +59,9 @@ public MediaApi getMediaApi() {
public RegistryApi getRegistryApi() {
return registryApi;
}

@Override
public VoiceApi getVoiceApi() {
return voiceApi;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package com.craftmend.openaudiomc.api.impl;

import com.craftmend.openaudiomc.api.interfaces.Client;
import com.craftmend.openaudiomc.api.interfaces.VoiceApi;
import com.craftmend.openaudiomc.generic.client.objects.ClientConnection;
import com.craftmend.openaudiomc.generic.client.objects.VoicePeerOptions;

import java.util.UUID;

public class VoiceApiImpl implements VoiceApi {

@Override
public boolean hasPeer(Client haystack, Client needle) {
return hasPeer(haystack, needle.getUser().getUniqueId());
}

@Override
public boolean hasPeer(Client haystack, UUID needle) {
ClientConnection clientConnection = (ClientConnection) haystack;
return clientConnection.getRtcSessionManager().isPeer(needle);
}

@Override
public void updatePeerOptions(Client client, Client peerToUpdate, VoicePeerOptions options) {
client.updatePeerOptions(peerToUpdate, options);
}

private boolean isProximityPeer(Client haystack, Client needle) {
ClientConnection haystackConnection = (ClientConnection) haystack;
return haystackConnection.getRtcSessionManager().getCurrentProximityPeers().contains(needle.getUser().getUniqueId());
}

public boolean isGlobalPeer(Client haystack, Client needle) {
ClientConnection haystackConnection = (ClientConnection) haystack;
return haystackConnection.getRtcSessionManager().getCurrentGlobalPeers().contains(needle.getUser().getUniqueId());
}

@Override
public void addStaticPeer(Client client, Client peerToAdd, boolean visible, boolean mutual) {
VoicePeerOptions options = new VoicePeerOptions();
options.setSpatialAudio(false);
options.setVisible(visible);

ClientConnection clientConnection = (ClientConnection) client;
ClientConnection peerConnection = (ClientConnection) peerToAdd;

if (!clientConnection.getRtcSessionManager().isReady() || !peerConnection.getRtcSessionManager().isReady()) {
throw new IllegalStateException("Both clients must be ready (connected and have voice chat enabled) before adding a peer");
}

if (isProximityPeer(client, peerToAdd)) {
client.updatePeerOptions(peerToAdd, options);
clientConnection.getRtcSessionManager().getCurrentGlobalPeers().add(peerToAdd.getUser().getUniqueId());
clientConnection.getRtcSessionManager().getCurrentProximityPeers().remove(peerToAdd.getUser().getUniqueId());
} else {
clientConnection.getRtcSessionManager().getCurrentGlobalPeers().add(peerToAdd.getUser().getUniqueId());
clientConnection.getPeerQueue().addSubscribe(peerConnection, clientConnection, options);
}

if (mutual) {
addStaticPeer(peerToAdd, client, visible, false);
}
}

@Override
public void removeStaticPeer(Client client, Client peerToRemove, boolean mutual) {
if (isGlobalPeer(client, peerToRemove)) {
ClientConnection clientConnection = (ClientConnection) client;
ClientConnection peerConnection = (ClientConnection) peerToRemove;
clientConnection.getRtcSessionManager().getCurrentGlobalPeers().remove(peerToRemove.getUser().getUniqueId());
clientConnection.getPeerQueue().drop(peerConnection.getRtcSessionManager().getStreamKey());
}

if (mutual) {
removeStaticPeer(peerToRemove, client, false);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ public interface AudioApi {
*/
RegistryApi getRegistryApi();

/**
* Get the voice API instance, used to manage voice chat
* @return Voice api instance
*/
VoiceApi getVoiceApi();

static AudioApi getInstance() {
return DefaultApi.i();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,6 @@ public interface Client {
*/
void preloadMedia(String source);

void updatePeerOptions(Client peer, VoicePeerOptions options);

void updatePeerOptions(Client peer, VoicePeerOptions options);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.craftmend.openaudiomc.api.interfaces;

import com.craftmend.openaudiomc.generic.client.objects.VoicePeerOptions;

import java.util.UUID;

public interface VoiceApi {

/*
* The VoiceApi contains registry, as well as control endpoints for voice-chat related features.
* This implementation is only available on the Spigot instance, even if the plugin is running in a BungeeCord/Velocity or Vistas network.
* Accessing this API on a non-spigot instance will result in undefined behavior or runtime exceptions.
*/

/**
* Register a client as a voice-chat peer
* @param haystack The client that will be the host
* @param needle The client that will be the peer
* @return true if the client was registered, false if the client was already registered
*/
boolean hasPeer(Client haystack, Client needle);

/**
* Register a client as a voice-chat peer
* @param haystack The client that will be the host
* @param needle The client that will be the peer
* @return true if the client was registered, false if the client was already registered
*/
boolean hasPeer(Client haystack, UUID needle);

/**
* Push new options for a peer, changing how its rendered in the client
* @param client The web client that should receive this update
* @param peerToUpdate The peer that should be updated
* @param options The new options
*/
void updatePeerOptions(Client client, Client peerToUpdate, VoicePeerOptions options);

/**
* Add a peer (partner) to someone's voice chat.
* This would let the client hear the peerToAdd as a global voice (without spatial audio/distance) until it's removed.
* @param client The web client that should receive this update
* @param peerToAdd The peer that should be added
* @param visible Whether the peer should be visible in the client
* @param mutual Whether the peer should also hear the client (repeat the call for mutual)
*/
void addStaticPeer(Client client, Client peerToAdd, boolean visible, boolean mutual);

/**
* Remove a global peer from someone's voice chat.
* This would remove a static peer if they have been added through addStaticPeer, but not
* if they have been added through the regular voice-chat system.
* @param client The web client that should receive this update
* @param peerToRemove The peer that should be removed
* @param mutual Whether the peer should also stop hearing the client (repeat the call for mutual)
*/
void removeStaticPeer(Client client, Client peerToRemove, boolean mutual);

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

public enum RtcBlockReason {

IN_DISABLED_REGION
IN_DISABLED_REGION,
EXEMPTED_FROM_PEER_TICKING

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ public class PeerQueue {
private final Set<String> dropQueue = new HashSet<>();
private final Set<ClientVoiceSubscribePayload.SerializedPeer> subscribeQueue = new HashSet<>();

private Lock lock = new ReentrantLock();
private final Lock lock = new ReentrantLock();

public void drop(String streamKey) {
lock.lock();
dropQueue.add(streamKey);

// do we have a subscribe queued? if so, remove it
// do we have a sub queued? if so, remove it
subscribeQueue.removeIf(clientVoiceSubscribePayload -> clientVoiceSubscribePayload.getStreamKey().equals(streamKey));
lock.unlock();
}
Expand All @@ -33,6 +33,10 @@ public void addSubscribe(
) {
ClientVoiceSubscribePayload.SerializedPeer peer = ClientVoiceSubscribePayload.SerializedPeer.fromClient(toListenTo, originLocation, options);
lock.lock();

// remove old, if present, its possible for this to be called twice with different options
subscribeQueue.removeIf(clientVoiceSubscribePayload -> clientVoiceSubscribePayload.getStreamKey().equals(peer.getStreamKey()));

subscribeQueue.add(peer);

// do we have a drop queued? if so, remove it
Expand Down

0 comments on commit faee9b4

Please sign in to comment.