Skip to content

Commit

Permalink
fix: drop packets only if source state diff is more than or equals 10
Browse files Browse the repository at this point in the history
  • Loading branch information
Apehum committed Aug 18, 2023
1 parent eb06ceb commit 94af69c
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public interface ServerAudioSource<S extends SourceInfo> extends AudioSource<S>

/**
* Marks source as dirty.
* On next received packet, source will send SourceInfoPacket to all listeners
* On next UDP packet sent, source will send SourceInfoPacket to all listeners
*/
void setDirty();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,7 @@ public void handle(@NotNull SourceAudioPacket packet) {
voiceClient.getSourceManager().getSourceById(packet.getSourceId())
.ifPresent(source -> {
if (source.getSourceInfo().getState() != packet.getSourceState()) {
BaseVoice.DEBUG_LOGGER.log(
"Drop audio packet with bad source state: packet source state={}, source={}",
packet.getSourceState(), source.getSourceInfo()
);
voiceClient.getSourceManager().sendSourceInfoRequest(packet.getSourceId(), true);
return;
}

source.process(packet);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@ import su.plo.voice.api.event.EventPriority
import su.plo.voice.api.event.EventSubscribe
import su.plo.voice.api.util.AudioUtil
import su.plo.voice.api.util.Params
import su.plo.voice.audio.codec.AudioDecoderPlc
import su.plo.voice.client.BaseVoiceClient
import su.plo.voice.client.audio.SoundOcclusion
import su.plo.voice.audio.codec.AudioDecoderPlc
import su.plo.voice.client.config.VoiceClientConfig
import su.plo.voice.client.utils.diff
import su.plo.voice.client.utils.level
import su.plo.voice.client.utils.toFloatArray
import su.plo.voice.proto.data.audio.codec.CodecInfo
Expand Down Expand Up @@ -229,8 +230,9 @@ abstract class BaseClientAudioSource<T> constructor(
}

private suspend fun processAudioPacket(packet: SourceAudioPacket) = mutex.withLock {
if (packet.sourceState != sourceInfo.state) {
LOGGER.warn("Drop packet with bad source state {}", sourceInfo)
// drop packets if source state diff by more than 10
if (sourceInfo.state.diff(packet.sourceState) >= 10) {
BaseVoice.DEBUG_LOGGER.warn("Drop packet with bad source state {}", sourceInfo)
return
}

Expand Down
8 changes: 8 additions & 0 deletions client/src/main/kotlin/su/plo/voice/client/utils/Byte.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package su.plo.voice.client.utils

fun Byte.diff(other: Byte): Byte =
if (other > this) {
(other - this).toByte()
} else {
(Byte.MAX_VALUE - this + (other - Byte.MIN_VALUE) + 1).toByte()
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ public synchronized void setStereo(boolean stereo) {
if (this.stereo != stereo) {
this.stereo = stereo;
setDirty();
increaseSourceState();
// increase source state by 10, so client can detect that
// it was a major change and drop all packets with source state diff more than 10
increaseSourceState(10);
}
}

Expand All @@ -75,7 +77,7 @@ public synchronized void setIconVisible(boolean visible) {
if (this.iconVisible != visible) {
this.iconVisible = visible;
setDirty();
increaseSourceState();
increaseSourceState(1);
}
}

Expand All @@ -84,7 +86,7 @@ public void setName(@Nullable String name) {
if (!Objects.equals(this.name, name)) {
this.name = name;
setDirty();
increaseSourceState();
increaseSourceState(1);
}
}

Expand Down Expand Up @@ -123,10 +125,16 @@ public boolean matchFilters(@NotNull VoicePlayer player) {
return true;
}

protected void increaseSourceState() {
protected void increaseSourceState(int addition) {
state.updateAndGet((operand) -> {
int value = operand + 1;
return value > Byte.MAX_VALUE ? Byte.MIN_VALUE : value;
int value = operand + addition;

if (value > Byte.MAX_VALUE) {
int remainder = value % Byte.MIN_VALUE;
return Byte.MIN_VALUE + remainder;
} else {
return value;
}
});
}
}

0 comments on commit 94af69c

Please sign in to comment.