Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/rework proximity ticking #388

Merged
merged 11 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ public void registerAlias(String aliasName, String value) {

@Override
public void setProximityFilter(Filter<ClientConnection, Player> filter) {
OpenAudioMc.getService(SpigotVoiceChatService.class).getProximityTicker().setFilter(filter);
OpenAudioMc.getService(SpigotVoiceChatService.class).getPeerTicker().setFilter(filter);
}

@Override
public void addProximityFilter(Filter<ClientConnection, Player> filter) {
OpenAudioMc.getService(SpigotVoiceChatService.class).getProximityTicker().addFilter(filter);
OpenAudioMc.getService(SpigotVoiceChatService.class).getPeerTicker().addFilter(filter);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.craftmend.openaudiomc.api.interfaces;

import com.craftmend.openaudiomc.generic.client.objects.VoicePeerOptions;
import com.craftmend.openaudiomc.generic.media.objects.Media;
import com.craftmend.openaudiomc.generic.user.User;

Expand Down Expand Up @@ -65,4 +66,6 @@ public interface Client {
*/
void preloadMedia(String source);

void updatePeerOptions(Client peer, VoicePeerOptions options);

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@
import com.craftmend.openaudiomc.generic.networking.packets.client.ui.PacketClientModerationStatus;
import com.craftmend.openaudiomc.generic.networking.packets.client.ui.PacketClientProtocolRevisionPacket;
import com.craftmend.openaudiomc.generic.networking.packets.client.ui.PacketClientSetVolume;
import com.craftmend.openaudiomc.generic.networking.packets.client.voice.PacketClientVoiceOptionsUpdate;
import com.craftmend.openaudiomc.generic.networking.payloads.client.media.ClientPreFetchPayload;
import com.craftmend.openaudiomc.generic.networking.payloads.client.voice.ClientVoiceOptionsPayload;
import com.craftmend.openaudiomc.generic.rest.Task;
import com.craftmend.openaudiomc.generic.node.packets.ClientConnectedPacket;
import com.craftmend.openaudiomc.generic.node.packets.ClientDisconnectedPacket;
Expand Down Expand Up @@ -297,4 +299,22 @@ public void preloadMedia(String source) {
ClientPreFetchPayload payload = new ClientPreFetchPayload(OpenAudioMc.getService(MediaService.class).process(source), "api", false);
sendPacket(new PacketClientPreFetch(payload));
}

@Override
public void updatePeerOptions(Client peer, VoicePeerOptions options) {
Objects.requireNonNull(peer, "Peer cannot be null");
Objects.requireNonNull(options, "Options cannot be null");

// do we have this peer?
if (!rtcSessionManager.isPeer(peer.getUser().getUniqueId())) {
throw new IllegalArgumentException("Peer is not connected to this client");
}

// update the options
ClientConnection peerCon = OpenAudioMc.getService(NetworkingService.class).getClient(peer.getUser().getUniqueId());
PacketClientVoiceOptionsUpdate packet = new PacketClientVoiceOptionsUpdate(
new ClientVoiceOptionsPayload(peerCon.getRtcSessionManager().getStreamKey(), options)
);
sendPacket(packet);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void addSubscribe(
lock.unlock();
}

public void flush(ClientConnection toSendTo) {
public void flushDropsAndSubscriptions(ClientConnection toSendTo) {
lock.lock();
if (!dropQueue.isEmpty()) {
String[] streamKeys = dropQueue.toArray(new String[0]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,17 @@ public class RtcSessionManager implements Serializable {

@Getter private boolean isMicrophoneEnabled = false;
@Getter private boolean isVoicechatDeafened = false;
@Getter private final transient Set<UUID> listeningTo = ConcurrentHashMap.newKeySet();

@Getter private final transient Set<UUID> currentGlobalPeers = ConcurrentHashMap.newKeySet();
@Getter private final transient Set<UUID> currentProximityPeers = ConcurrentHashMap.newKeySet();
@Getter private final transient Set<ClientRtcLocationUpdate> locationUpdateQueue = ConcurrentHashMap.newKeySet();
@Getter private final transient Set<RtcBlockReason> blockReasons = new HashSet<>();
@Getter private final transient Set<RtcStateFlag> stateFlags = new HashSet<>();
@Getter private final transient Set<UUID> recentPeerAdditions = new HashSet<>();
@Getter private final transient Set<UUID> recentPeerRemovals = new HashSet<>();

// these are used for messaging, not tracking. N amount players joined, etc
@Getter private final transient Set<UUID> currentProximityAdditions = new HashSet<>();
@Getter private final transient Set<UUID> currentProximityDrops = new HashSet<>();

@Setter @Getter private String streamKey;
private transient Location lastPassedLocation = null;
private final transient ClientConnection clientConnection;
Expand All @@ -51,7 +56,8 @@ public RtcSessionManager(ClientConnection clientConnection) {

this.clientConnection.onDisconnect(() -> {
// go over all other clients, check if we might have a relations ship and break up if thats the case
listeningTo.clear();
currentProximityPeers.clear();
currentGlobalPeers.clear();
this.isMicrophoneEnabled = false;
makePeersDrop();
locationUpdateQueue.clear();
Expand All @@ -72,19 +78,19 @@ public boolean requestLinkage(ClientConnection peer, boolean mutual, VoicePeerOp
return false;

// only force the other user to subscribe if they are not already listening to me and mutual is true
if (mutual && !peer.getRtcSessionManager().listeningTo.contains(clientConnection.getOwner().getUniqueId())) {
peer.getRtcSessionManager().getListeningTo().add(clientConnection.getOwner().getUniqueId());
if (mutual && !peer.getRtcSessionManager().currentProximityPeers.contains(clientConnection.getOwner().getUniqueId())) {
peer.getRtcSessionManager().getCurrentProximityPeers().add(clientConnection.getOwner().getUniqueId());
peer.getPeerQueue().addSubscribe(clientConnection, peer, options);
AudioApi.getInstance().getEventDriver().fire(new PlayerEnterVoiceProximityEvent(clientConnection, peer, VoiceEventCause.NORMAL));
peer.getRtcSessionManager().updateLocationWatcher();
}

// in case that I'm already listening to the other user, don't do anything
// we do this after the mutual handling, so that still continues if I'm already listening to the other user
if (listeningTo.contains(peer.getOwner().getUniqueId()))
if (currentProximityPeers.contains(peer.getOwner().getUniqueId()))
return false;

listeningTo.add(peer.getOwner().getUniqueId());
currentProximityPeers.add(peer.getOwner().getUniqueId());
clientConnection.getPeerQueue().addSubscribe(peer, clientConnection, options);
AudioApi.getInstance().getEventDriver().fire(new PlayerEnterVoiceProximityEvent(peer, clientConnection, VoiceEventCause.NORMAL));

Expand Down Expand Up @@ -122,9 +128,9 @@ public void makePeersDrop() {
if (peer.getOwner().getUniqueId() == clientConnection.getOwner().getUniqueId())
continue;

if (peer.getRtcSessionManager().listeningTo.contains(clientConnection.getOwner().getUniqueId())) {
if (peer.getRtcSessionManager().currentProximityPeers.contains(clientConnection.getOwner().getUniqueId())) {
// send unsub packet
peer.getRtcSessionManager().listeningTo.remove(clientConnection.getOwner().getUniqueId());
peer.getRtcSessionManager().currentProximityPeers.remove(clientConnection.getOwner().getUniqueId());
peer.getRtcSessionManager().updateLocationWatcher();
peer.getPeerQueue().drop(streamKey);

Expand All @@ -146,7 +152,7 @@ public void forceUpdateLocation(Location location) {
if (peer.getOwner().getUniqueId() == clientConnection.getOwner().getUniqueId())
continue;

if (peer.getRtcSessionManager().listeningTo.contains(clientConnection.getOwner().getUniqueId())) {
if (peer.getRtcSessionManager().currentProximityPeers.contains(clientConnection.getOwner().getUniqueId())) {
peer.getRtcSessionManager().locationUpdateQueue.add(
ClientRtcLocationUpdate
.fromClientWithLocation(clientConnection, location, Vector3.from(peer))
Expand All @@ -162,7 +168,7 @@ public void updateLocationWatcher() {
// player logged out, ignoring
return;
}
if (listeningTo.isEmpty()) {
if (currentProximityPeers.isEmpty()) {
spigotConnection.getLocationFollowers().remove(PlayerLocationFollower.PROXIMITY_VOICE_CHAT);
} else {
spigotConnection.getLocationFollowers().add(PlayerLocationFollower.PROXIMITY_VOICE_CHAT);
Expand Down Expand Up @@ -208,4 +214,9 @@ public void setMicrophoneEnabled(boolean state) {
AudioApi.getInstance().getEventDriver().fire(new MicrophoneMuteEvent(clientConnection));
}
}

public boolean isPeer(UUID uuid) {
return currentProximityPeers.contains(uuid) || currentGlobalPeers.contains(uuid);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public String onRequest(OfflinePlayer player, @NotNull String params) {
return Integer.toString(OpenAudioMc.getService(
SpigotPlayerService.class
).getClient(player.getUniqueId())
.getClientConnection().getRtcSessionManager().getListeningTo().size());
.getClientConnection().getRtcSessionManager().getCurrentProximityPeers().size());
}

if(params.equals("voicechat_count"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,12 @@
import com.craftmend.openaudiomc.generic.service.Service;
import com.craftmend.openaudiomc.generic.storage.enums.StorageKey;
import com.craftmend.openaudiomc.generic.user.User;
import com.craftmend.openaudiomc.spigot.OpenAudioMcSpigot;
import com.craftmend.openaudiomc.spigot.modules.players.SpigotPlayerService;
import com.craftmend.openaudiomc.spigot.modules.voicechat.filters.FilterService;
import com.craftmend.openaudiomc.spigot.modules.voicechat.filters.PeerFilter;
import com.craftmend.openaudiomc.spigot.modules.voicechat.filters.impl.GamemodeFilter;
import com.craftmend.openaudiomc.spigot.modules.voicechat.filters.impl.TeamFilter;
import com.craftmend.openaudiomc.spigot.modules.voicechat.tasks.PlayerProximityTicker;
import com.craftmend.openaudiomc.spigot.modules.voicechat.tasks.PlayerPeerTicker;
import com.craftmend.openaudiomc.spigot.modules.voicechat.tasks.PlayerVicinityMessageTask;
import com.craftmend.openaudiomc.spigot.modules.voicechat.tasks.TickVoicePacketQueue;
import lombok.AllArgsConstructor;
Expand All @@ -41,7 +40,7 @@ public class SpigotVoiceChatService extends Service {
private NetworkingService networkingService;

@Getter
private PlayerProximityTicker proximityTicker;
private PlayerPeerTicker peerTicker;
private boolean firstRun = true;
private int broadcastTickLoop = 0;

Expand All @@ -64,8 +63,8 @@ public void onEnable() {
int maxDistance = StorageKey.SETTINGS_VC_RADIUS.getInt();

// tick every second
proximityTicker = new PlayerProximityTicker(maxDistance, new PeerFilter());
taskService.scheduleAsyncRepeatingTask(proximityTicker, 20, 20);
peerTicker = new PlayerPeerTicker(maxDistance, new PeerFilter());
taskService.scheduleAsyncRepeatingTask(peerTicker, 20, 20);
taskService.scheduleAsyncRepeatingTask(new TickVoicePacketQueue(), 3, 3);
}
firstRun = false;
Expand All @@ -83,8 +82,8 @@ public void onEnable() {
return;
}

event.getSpeaker().getRtcSessionManager().getRecentPeerAdditions().add(event.getListener().getOwner().getUniqueId());
event.getSpeaker().getRtcSessionManager().getRecentPeerRemovals().remove(event.getListener().getOwner().getUniqueId());
event.getSpeaker().getRtcSessionManager().getCurrentProximityAdditions().add(event.getListener().getOwner().getUniqueId());
event.getSpeaker().getRtcSessionManager().getCurrentProximityDrops().remove(event.getListener().getOwner().getUniqueId());
});

eventDriver.on(PlayerLeaveVoiceProximityEvent.class).setHandler(event -> {
Expand All @@ -98,8 +97,8 @@ public void onEnable() {
return;
}

event.getSpeaker().getRtcSessionManager().getRecentPeerRemovals().add(event.getListener().getOwner().getUniqueId());
event.getSpeaker().getRtcSessionManager().getRecentPeerAdditions().remove(event.getListener().getOwner().getUniqueId());
event.getSpeaker().getRtcSessionManager().getCurrentProximityDrops().add(event.getListener().getOwner().getUniqueId());
event.getSpeaker().getRtcSessionManager().getCurrentProximityAdditions().remove(event.getListener().getOwner().getUniqueId());
});

// do vc tick loop
Expand All @@ -115,11 +114,11 @@ public void onEnable() {
for (ClientConnection client : networkingService.getClients()) {
RtcSessionManager manager = client.getRtcSessionManager();
// handle their join messages, if any
if (!manager.getRecentPeerAdditions().isEmpty()) {
if (!manager.getCurrentProximityAdditions().isEmpty()) {
// do these
if (manager.getRecentPeerAdditions().size() == 1) {
if (manager.getCurrentProximityAdditions().size() == 1) {
// do single
ClientConnection other = clientFromId(manager.getRecentPeerAdditions().stream().findFirst().get());
ClientConnection other = clientFromId(manager.getCurrentProximityAdditions().stream().findFirst().get());
if (other != null) {
sendMessage(client.getUser(), Platform.translateColors(
StorageKey.MESSAGE_VC_USER_ADDED.getString()
Expand All @@ -128,21 +127,21 @@ public void onEnable() {
}
} else {
// do multiple
MultiNameReference mnr = new MultiNameReference(manager.getRecentPeerAdditions());
MultiNameReference mnr = new MultiNameReference(manager.getCurrentProximityAdditions());
sendMessage(client.getUser(), Platform.translateColors(
StorageKey.MESSAGE_VC_USERS_ADDED.getString()
.replace("%count", mnr.getOtherCount() + "")
.replace("%name", mnr.getFirstName()))
);
}
manager.getRecentPeerAdditions().clear();
manager.getCurrentProximityAdditions().clear();
}

if (!manager.getRecentPeerRemovals().isEmpty()) {
if (!manager.getCurrentProximityDrops().isEmpty()) {
// do these
if (manager.getRecentPeerRemovals().size() == 1) {
if (manager.getCurrentProximityDrops().size() == 1) {
// do single
ClientConnection other = clientFromId(manager.getRecentPeerRemovals().stream().findFirst().get());
ClientConnection other = clientFromId(manager.getCurrentProximityDrops().stream().findFirst().get());
if (other != null) {
sendMessage(client.getUser(), Platform.translateColors(
StorageKey.MESSAGE_VC_USER_LEFT.getString()
Expand All @@ -151,14 +150,14 @@ public void onEnable() {
}
} else {
// do multiple
MultiNameReference mnr = new MultiNameReference(manager.getRecentPeerRemovals());
MultiNameReference mnr = new MultiNameReference(manager.getCurrentProximityDrops());
sendMessage(client.getUser(), Platform.translateColors(
StorageKey.MESSAGE_VC_USERS_LEFT.getString()
.replace("%count", mnr.getOtherCount() + "")
.replace("%name", mnr.getFirstName()))
);
}
manager.getRecentPeerRemovals().clear();
manager.getCurrentProximityDrops().clear();
}
}
});
Expand Down
Loading
Loading