Skip to content

Commit

Permalink
let's break the entire fucking api and therefore bump version to 2.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
alula committed Apr 27, 2022
1 parent 59f083e commit 9a882f0
Show file tree
Hide file tree
Showing 22 changed files with 107 additions and 105 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ subprojects {
apply plugin: 'java'

group = 'moe.kyokobot'
version = '1.1.0-SNAPSHOT'
version = '2.0.0-SNAPSHOT'

sourceCompatibility = 11
targetCompatibility = 11
Expand Down
6 changes: 3 additions & 3 deletions core/src/main/java/moe/kyokobot/koe/KoeClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@

public interface KoeClient extends Closeable {
@NotNull
VoiceConnection createConnection(long guildId);
MediaConnection createConnection(long guildId);

@Nullable
VoiceConnection getConnection(long guildId);
MediaConnection getConnection(long guildId);

void destroyConnection(long guildId);

@NotNull
Map<Long, VoiceConnection> getConnections();
Map<Long, MediaConnection> getConnections();

@Override
void close();
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/moe/kyokobot/koe/KoeEventAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public void gatewayClosed(int code, String reason, boolean byRemote) {
}

@Override
public void userConnected(String id, int audioSSRC, int videoSSRC) {
public void userConnected(String id, int audioSSRC, int videoSSRC, int rtxSSRC) {
//
}

Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/moe/kyokobot/koe/KoeEventListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public interface KoeEventListener {

void gatewayClosed(int code, @Nullable String reason, boolean byRemote);

void userConnected(String id, int audioSSRC, int videoSSRC);
void userConnected(String id, int audioSSRC, int videoSSRC, int rtxSSRC);

void userDisconnected(String id);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

import moe.kyokobot.koe.media.MediaFrameProvider;
import moe.kyokobot.koe.codec.Codec;
import moe.kyokobot.koe.gateway.VoiceGatewayConnection;
import moe.kyokobot.koe.gateway.MediaGatewayConnection;
import moe.kyokobot.koe.handler.ConnectionHandler;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.Closeable;
import java.util.concurrent.CompletionStage;

public interface VoiceConnection extends Closeable {
public interface MediaConnection extends Closeable {
/**
* Connects to Discord voice server using specified info.
* @param info Discord voice server connection information
Expand All @@ -31,12 +31,12 @@ public interface VoiceConnection extends Closeable {
KoeOptions getOptions();

@Nullable
MediaFrameProvider getSender();
MediaFrameProvider getAudioSender();

long getGuildId();

@Nullable
VoiceGatewayConnection getGatewayConnection();
MediaGatewayConnection getGatewayConnection();

@Nullable
VoiceServerInfo getVoiceServerInfo();
Expand All @@ -54,7 +54,7 @@ public interface VoiceConnection extends Closeable {

/**
* Stops polling media frames.
* @see VoiceConnection#startFramePolling()
* @see MediaConnection#startFramePolling()
*/
void stopFramePolling();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package moe.kyokobot.koe.codec.netty;

import moe.kyokobot.koe.VoiceConnection;
import moe.kyokobot.koe.MediaConnection;
import moe.kyokobot.koe.codec.Codec;
import moe.kyokobot.koe.codec.FramePoller;
import moe.kyokobot.koe.codec.FramePollerFactory;
Expand All @@ -12,7 +12,7 @@
import java.util.function.Function;

public class NettyFramePollerFactory implements FramePollerFactory {
private Map<Codec, Function<VoiceConnection, FramePoller>> codecMap;
private Map<Codec, Function<MediaConnection, FramePoller>> codecMap;

public NettyFramePollerFactory() {
codecMap = new HashMap<>();
Expand All @@ -21,7 +21,7 @@ public NettyFramePollerFactory() {

@Override
@Nullable
public FramePoller createFramePoller(Codec codec, VoiceConnection connection) {
public FramePoller createFramePoller(Codec codec, MediaConnection connection) {
var constructor = codecMap.get(codec);
if (constructor != null) {
return constructor.apply(connection);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package moe.kyokobot.koe.codec.netty;

import moe.kyokobot.koe.VoiceConnection;
import moe.kyokobot.koe.MediaConnection;
import moe.kyokobot.koe.codec.AbstractFramePoller;
import moe.kyokobot.koe.codec.OpusCodec;
import org.slf4j.Logger;
Expand All @@ -12,7 +12,7 @@
public class NettyOpusFramePoller extends AbstractFramePoller {
private static final Logger logger = LoggerFactory.getLogger(NettyOpusFramePoller.class);

public NettyOpusFramePoller(VoiceConnection connection) {
public NettyOpusFramePoller(MediaConnection connection) {
super(connection);
this.timestamp = new AtomicInteger();
}
Expand Down Expand Up @@ -43,7 +43,7 @@ private void pollFrame() {

try {
var handler = connection.getConnectionHandler();
var sender = connection.getSender();
var sender = connection.getAudioSender();
var codec = OpusCodec.INSTANCE;

if (sender != null && handler != null && sender.canSendFrame(codec)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import io.netty.util.concurrent.EventExecutor;
import moe.kyokobot.koe.VoiceServerInfo;
import moe.kyokobot.koe.internal.NettyBootstrapFactory;
import moe.kyokobot.koe.internal.VoiceConnectionImpl;
import moe.kyokobot.koe.internal.MediaConnectionImpl;
import moe.kyokobot.koe.internal.json.JsonObject;
import moe.kyokobot.koe.internal.json.JsonParser;
import moe.kyokobot.koe.internal.util.NettyFutureWrapper;
Expand All @@ -34,10 +34,10 @@
import java.util.Objects;
import java.util.concurrent.CompletableFuture;

public abstract class AbstractVoiceGatewayConnection implements VoiceGatewayConnection {
private static final Logger logger = LoggerFactory.getLogger(AbstractVoiceGatewayConnection.class);
public abstract class AbstractMediaGatewayConnection implements MediaGatewayConnection {
private static final Logger logger = LoggerFactory.getLogger(AbstractMediaGatewayConnection.class);

protected final VoiceConnectionImpl connection;
protected final MediaConnectionImpl connection;
protected final VoiceServerInfo voiceServerInfo;
protected final URI websocketURI;
protected final Bootstrap bootstrap;
Expand All @@ -49,7 +49,7 @@ public abstract class AbstractVoiceGatewayConnection implements VoiceGatewayConn
private volatile boolean open;
private volatile boolean closed = false;

public AbstractVoiceGatewayConnection(@NotNull VoiceConnectionImpl connection,
public AbstractMediaGatewayConnection(@NotNull MediaConnectionImpl connection,
@NotNull VoiceServerInfo voiceServerInfo,
int version) {
try {
Expand Down Expand Up @@ -156,7 +156,7 @@ protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Except
if (msg instanceof FullHttpResponse) {
try {
handshaker.finishHandshake(ch, (FullHttpResponse) msg);
AbstractVoiceGatewayConnection.this.open = true;
AbstractMediaGatewayConnection.this.open = true;
connectFuture.complete(null);
} catch (WebSocketHandshakeException e) {
connectFuture.completeExceptionally(e);
Expand All @@ -183,7 +183,7 @@ protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Except
if (logger.isDebugEnabled()) {
logger.debug("Websocket closed, code: {}, reason: {}", frame.statusCode(), frame.reasonText());
}
AbstractVoiceGatewayConnection.this.open = false;
AbstractMediaGatewayConnection.this.open = false;
onClose(frame.statusCode(), frame.reasonText(), true);
}
}
Expand Down
12 changes: 6 additions & 6 deletions core/src/main/java/moe/kyokobot/koe/gateway/GatewayVersion.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
package moe.kyokobot.koe.gateway;

import moe.kyokobot.koe.VoiceServerInfo;
import moe.kyokobot.koe.internal.VoiceConnectionImpl;
import moe.kyokobot.koe.internal.MediaConnectionImpl;

public enum GatewayVersion {
V4(VoiceGatewayV4Connection::new),
V5(VoiceGatewayV5Connection::new);
V4(MediaGatewayV4Connection::new),
V5(MediaGatewayV5Connection::new);

private final VoiceGatewayConnectionFactory factory;
private final MediaGatewayConnectionFactory factory;

public VoiceGatewayConnection createConnection(VoiceConnectionImpl connection, VoiceServerInfo voiceServerInfo) {
public MediaGatewayConnection createConnection(MediaConnectionImpl connection, VoiceServerInfo voiceServerInfo) {
return factory.create(connection, voiceServerInfo);
}

GatewayVersion(VoiceGatewayConnectionFactory factory) {
GatewayVersion(MediaGatewayConnectionFactory factory) {
this.factory = factory;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import java.util.concurrent.CompletableFuture;

public interface VoiceGatewayConnection {
public interface MediaGatewayConnection {
boolean isOpen();

CompletableFuture<Void> start();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package moe.kyokobot.koe.gateway;

import moe.kyokobot.koe.VoiceServerInfo;
import moe.kyokobot.koe.internal.MediaConnectionImpl;

@FunctionalInterface
public interface MediaGatewayConnectionFactory {
MediaGatewayConnection create(MediaConnectionImpl connection, VoiceServerInfo voiceServerInfo);
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package moe.kyokobot.koe.gateway;

import moe.kyokobot.koe.VoiceServerInfo;
import moe.kyokobot.koe.codec.OpusCodec;
import moe.kyokobot.koe.crypto.EncryptionMode;
import moe.kyokobot.koe.internal.VoiceConnectionImpl;
import moe.kyokobot.koe.internal.MediaConnectionImpl;
import moe.kyokobot.koe.internal.handler.DiscordUDPConnection;
import moe.kyokobot.koe.internal.json.JsonArray;
import moe.kyokobot.koe.internal.json.JsonObject;
Expand All @@ -18,17 +19,13 @@
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

public class VoiceGatewayV4Connection extends AbstractVoiceGatewayConnection {
private static final Logger logger = LoggerFactory.getLogger(VoiceGatewayV4Connection.class);
public class MediaGatewayV4Connection extends AbstractMediaGatewayConnection {
private static final Logger logger = LoggerFactory.getLogger(MediaGatewayV4Connection.class);
private static final JsonArray SUPPORTED_CODECS;

static {
SUPPORTED_CODECS = new JsonArray();
SUPPORTED_CODECS.add(new JsonObject()
.add("name", "opus")
.add("type", "audio")
.add("priority", 1000)
.add("payload_type", 120));
SUPPORTED_CODECS.add(OpusCodec.INSTANCE.getJsonDescription());
}

private int ssrc;
Expand All @@ -37,7 +34,7 @@ public class VoiceGatewayV4Connection extends AbstractVoiceGatewayConnection {
private UUID rtcConnectionId;
private ScheduledFuture heartbeatFuture;

public VoiceGatewayV4Connection(VoiceConnectionImpl connection, VoiceServerInfo voiceServerInfo) {
public MediaGatewayV4Connection(MediaConnectionImpl connection, VoiceServerInfo voiceServerInfo) {
super(connection, voiceServerInfo, 4);
}

Expand Down Expand Up @@ -76,7 +73,7 @@ protected void handlePayload(JsonObject object) {
address = new InetSocketAddress(ip, port);

connection.getDispatcher().gatewayReady((InetSocketAddress) address, ssrc);
logger.debug("Voice READY, ssrc: {}", ssrc);
logger.debug("Got READY, ssrc: {}", ssrc);
selectProtocol("udp");
break;
}
Expand All @@ -99,7 +96,8 @@ protected void handlePayload(JsonObject object) {
var user = data.getString("user_id");
var audioSsrc = data.getInt("audio_ssrc", 0);
var videoSsrc = data.getInt("video_ssrc", 0);
connection.getDispatcher().userConnected(user, audioSsrc, videoSsrc);
var rtxSsrc = data.getInt("rtx_ssrc", 0);
connection.getDispatcher().userConnected(user, audioSsrc, videoSsrc, rtxSsrc);
break;
}
case Op.CLIENT_DISCONNECT: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package moe.kyokobot.koe.gateway;

import moe.kyokobot.koe.VoiceServerInfo;
import moe.kyokobot.koe.codec.Codec;
import moe.kyokobot.koe.codec.DefaultCodecs;
import moe.kyokobot.koe.crypto.EncryptionMode;
import moe.kyokobot.koe.internal.VoiceConnectionImpl;
import moe.kyokobot.koe.internal.MediaConnectionImpl;
import moe.kyokobot.koe.internal.handler.DiscordUDPConnection;
import moe.kyokobot.koe.internal.json.JsonArray;
import moe.kyokobot.koe.internal.json.JsonObject;
Expand All @@ -17,27 +19,18 @@
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class VoiceGatewayV5Connection extends AbstractVoiceGatewayConnection {
private static final Logger logger = LoggerFactory.getLogger(VoiceGatewayV5Connection.class);
private static final JsonArray SUPPORTED_CODECS;

static {
SUPPORTED_CODECS = new JsonArray();
SUPPORTED_CODECS.add(new JsonObject()
.add("name", "opus")
.add("type", "audio")
.add("priority", 1000)
.add("payload_type", 120));
}
public class MediaGatewayV5Connection extends AbstractMediaGatewayConnection {
private static final Logger logger = LoggerFactory.getLogger(MediaGatewayV5Connection.class);

private int ssrc;
private SocketAddress address;
private List<String> encryptionModes;
private UUID rtcConnectionId;
private ScheduledFuture heartbeatFuture;

public VoiceGatewayV5Connection(VoiceConnectionImpl connection, VoiceServerInfo voiceServerInfo) {
public MediaGatewayV5Connection(MediaConnectionImpl connection, VoiceServerInfo voiceServerInfo) {
super(connection, voiceServerInfo, 5);
}

Expand Down Expand Up @@ -100,7 +93,8 @@ protected void handlePayload(JsonObject object) {
var user = data.getString("user_id");
var audioSsrc = data.getInt("audio_ssrc", 0);
var videoSsrc = data.getInt("video_ssrc", 0);
connection.getDispatcher().userConnected(user, audioSsrc, videoSsrc);
var rtxSsrc = data.getInt("rtx_ssrc", 0);
connection.getDispatcher().userConnected(user, audioSsrc, videoSsrc, rtxSsrc);
break;
}
case Op.CLIENT_DISCONNECT: {
Expand All @@ -109,6 +103,9 @@ protected void handlePayload(JsonObject object) {
connection.getDispatcher().userDisconnected(user);
break;
}
case Op.VIDEO_SINK_WANTS: {
break;
}
default:
break;
}
Expand Down Expand Up @@ -160,9 +157,14 @@ private void selectProtocol(String protocol) {
.add("port", ourAddress.getPort())
.add("mode", mode);

var codecs = new JsonArray();
Stream.concat(DefaultCodecs.audioCodecs.values().stream(), DefaultCodecs.videoCodecs.values().stream())
.map(Codec::getJsonDescription)
.forEach(codecs::add);

sendInternalPayload(Op.SELECT_PROTOCOL, new JsonObject()
.add("protocol", "udp")
.add("codecs", SUPPORTED_CODECS)
.add("codecs", codecs)
.add("rtc_connection_id", rtcConnectionId.toString())
.add("data", udpInfo)
.combine(udpInfo));
Expand Down

This file was deleted.

Loading

0 comments on commit 9a882f0

Please sign in to comment.