-
-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
131 additions
and
20 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
11 changes: 11 additions & 0 deletions
11
core/src/main/java/com/rtm516/mcxboxbroadcast/core/webrtc/compression/CompressionCodec.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,11 @@ | ||
package com.rtm516.mcxboxbroadcast.core.webrtc.compression; | ||
|
||
import io.netty.buffer.ByteBuf; | ||
|
||
public interface CompressionCodec { | ||
ByteBuf encode(ByteBuf msg) throws Exception; | ||
|
||
ByteBuf decode(ByteBuf msg) throws Exception; | ||
|
||
int compressionIdentifier(); | ||
} |
39 changes: 39 additions & 0 deletions
39
.../src/main/java/com/rtm516/mcxboxbroadcast/core/webrtc/compression/CompressionHandler.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,39 @@ | ||
package com.rtm516.mcxboxbroadcast.core.webrtc.compression; | ||
|
||
import io.netty.buffer.ByteBuf; | ||
import org.cloudburstmc.protocol.bedrock.data.PacketCompressionAlgorithm; | ||
|
||
public class CompressionHandler { | ||
private final CompressionCodec chosenCodec; | ||
private final NoneCompressionCodec fallback; | ||
private final int threshold; | ||
|
||
public CompressionHandler(PacketCompressionAlgorithm algorithm, int threshold) { | ||
this.chosenCodec = switch (algorithm) { | ||
case ZLIB -> new ZlibCompressionCodec(); | ||
case NONE -> new NoneCompressionCodec(); | ||
default -> throw new IllegalArgumentException("Unsupported compression algorithm: " + algorithm); | ||
}; | ||
|
||
if (chosenCodec instanceof NoneCompressionCodec codec) { | ||
this.fallback = codec; | ||
} else { | ||
this.fallback = new NoneCompressionCodec(); | ||
} | ||
this.threshold = threshold; | ||
} | ||
|
||
public ByteBuf encode(ByteBuf buf) throws Exception { | ||
if (buf.readableBytes() > threshold) { | ||
return chosenCodec.encode(buf); | ||
} | ||
return fallback.encode(buf); | ||
} | ||
|
||
public ByteBuf decode(ByteBuf buf) throws Exception { | ||
if (buf.readableBytes() > threshold) { | ||
return chosenCodec.decode(buf); | ||
} | ||
return fallback.decode(buf); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
...rc/main/java/com/rtm516/mcxboxbroadcast/core/webrtc/compression/NoneCompressionCodec.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,26 @@ | ||
package com.rtm516.mcxboxbroadcast.core.webrtc.compression; | ||
|
||
import io.netty.buffer.ByteBuf; | ||
import io.netty.buffer.Unpooled; | ||
|
||
public class NoneCompressionCodec implements CompressionCodec { | ||
@Override | ||
public ByteBuf encode(ByteBuf msg) throws Exception { | ||
return Unpooled.buffer(msg.readableBytes() + 1) | ||
.writeByte(compressionIdentifier()) | ||
.writeBytes(msg); | ||
} | ||
|
||
@Override | ||
public ByteBuf decode(ByteBuf msg) throws Exception { | ||
if (msg.readUnsignedByte() != compressionIdentifier()) { | ||
throw new IllegalArgumentException("Unexpected compression identifier"); | ||
} | ||
return msg; | ||
} | ||
|
||
@Override | ||
public int compressionIdentifier() { | ||
return 0xFF; | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
...rc/main/java/com/rtm516/mcxboxbroadcast/core/webrtc/compression/ZlibCompressionCodec.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,38 @@ | ||
package com.rtm516.mcxboxbroadcast.core.webrtc.compression; | ||
|
||
import io.netty.buffer.ByteBuf; | ||
import io.netty.buffer.Unpooled; | ||
import org.cloudburstmc.protocol.common.util.Zlib; | ||
|
||
public class ZlibCompressionCodec implements CompressionCodec { | ||
private static final int MAX_DECOMPRESSED_BYTES = Integer.getInteger("bedrock.maxDecompressedBytes", 1024 * 1024 * 10); | ||
|
||
private final Zlib zlib = Zlib.RAW; | ||
private final int level = 7; | ||
|
||
@Override | ||
public ByteBuf encode(ByteBuf msg) throws Exception { | ||
ByteBuf outBuf = Unpooled.buffer(msg.readableBytes() << 3 + 1); | ||
outBuf.writeByte(compressionIdentifier()); | ||
|
||
try { | ||
zlib.deflate(msg, outBuf, level); | ||
return outBuf.retain(); | ||
} finally { | ||
outBuf.release(); | ||
} | ||
} | ||
|
||
@Override | ||
public ByteBuf decode(ByteBuf msg) throws Exception { | ||
if (msg.readUnsignedByte() != compressionIdentifier()) { | ||
throw new IllegalArgumentException("Unexpected compression identifier"); | ||
} | ||
return zlib.inflate(msg, MAX_DECOMPRESSED_BYTES); | ||
} | ||
|
||
@Override | ||
public int compressionIdentifier() { | ||
return 0x00; | ||
} | ||
} |