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

Webrtc improvements #3789

Merged
merged 11 commits into from
Dec 27, 2022
8 changes: 4 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -423,13 +423,13 @@ dependencies {

// webrtc
implementation group: 'org.java-websocket', name: 'Java-WebSocket', version: '1.5.2'
implementation 'dev.onvoid.webrtc:webrtc-java:0.5.0'
implementation 'dev.onvoid.webrtc:webrtc-java:0.7.0'
if (osdetector.os.is('windows'))
implementation 'dev.onvoid.webrtc:webrtc-java:0.5.0:windows-x86_64'
implementation 'dev.onvoid.webrtc:webrtc-java:0.7.0:windows-x86_64'
else if (osdetector.os.is('osx'))
implementation 'dev.onvoid.webrtc:webrtc-java:0.5.0:macos-x86_64'
implementation 'dev.onvoid.webrtc:webrtc-java:0.7.0:macos-x86_64'
else if (osdetector.os.is('linux'))
implementation 'dev.onvoid.webrtc:webrtc-java:0.5.0:linux-x86_64'
implementation 'dev.onvoid.webrtc:webrtc-java:0.7.0:linux-x86_64'

// protobuf
implementation "io.grpc:grpc-protobuf:1.47.0"
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/net/rptools/clientserver/ConnectionFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import net.rptools.clientserver.simple.server.ServerConnection;
import net.rptools.clientserver.simple.server.SocketServerConnection;
import net.rptools.clientserver.simple.server.WebRTCServerConnection;
import net.rptools.maptool.client.AppState;
import net.rptools.maptool.server.ServerConfig;

public class ConnectionFactory {
Expand All @@ -34,15 +33,15 @@ public static ConnectionFactory getInstance() {

public ClientConnection createClientConnection(String id, ServerConfig config)
throws IOException {
if (!AppState.useWebRTC() || config.isPersonalServer())
if (!config.getUseWebRTC() || config.isPersonalServer())
return new SocketClientConnection(id, config.getHostName(), config.getPort());

return new WebRTCClientConnection(id, config);
}

public ServerConnection createServerConnection(ServerConfig config, HandshakeProvider handshake)
throws IOException {
if (!AppState.useWebRTC() || config.isPersonalServer())
if (!config.getUseWebRTC() || config.isPersonalServer())
return new SocketServerConnection(config.getPort(), handshake);

return new WebRTCServerConnection(config, handshake);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,15 +162,37 @@ public void onError(Exception ex) {
}

private void init() {
RTCIceServer iceServer = new RTCIceServer();
iceServer.urls.add("stun:stun.l.google.com:19302");
iceServer.urls.add("stun:stun1.l.google.com:19302");
iceServer.urls.add("stun:stun2.l.google.com:19302");
iceServer.urls.add("stun:stun3.l.google.com:19302");
iceServer.urls.add("stun:stun4.l.google.com:19302");

rtcConfig = new RTCConfiguration();
rtcConfig.iceServers.add(iceServer);

var googleStun = new RTCIceServer();
googleStun.urls.add("stun:stun.l.google.com:19302");
googleStun.urls.add("stun:stun1.l.google.com:19302");
googleStun.urls.add("stun:stun2.l.google.com:19302");
googleStun.urls.add("stun:stun3.l.google.com:19302");
googleStun.urls.add("stun:stun4.l.google.com:19302");
rtcConfig.iceServers.add(googleStun);

var openRelayStun = new RTCIceServer();
openRelayStun.urls.add("stun:openrelay.metered.ca:80");
rtcConfig.iceServers.add(openRelayStun);

var openRelayTurn = new RTCIceServer();
openRelayTurn.urls.add("turn:openrelay.metered.ca:80");
openRelayTurn.username = "openrelayproject";
openRelayTurn.password = "openrelayproject";
rtcConfig.iceServers.add(openRelayTurn);

var openRelayTurn2 = new RTCIceServer();
openRelayTurn2.urls.add("turn:openrelay.metered.ca:443");
openRelayTurn2.username = "openrelayproject";
openRelayTurn2.password = "openrelayproject";
rtcConfig.iceServers.add(openRelayTurn2);

var openRelayTurn3 = new RTCIceServer();
openRelayTurn3.urls.add("turn:openrelay.metered.ca:443?transport=tcp");
openRelayTurn3.username = "openrelayproject";
openRelayTurn3.password = "openrelayproject";
rtcConfig.iceServers.add(openRelayTurn3);
}

@Override
Expand Down Expand Up @@ -253,7 +275,7 @@ public void onFailure(String error) {

private void onLogin(LoginMessageDto message) {
if (!message.success) {
MapTool.showError("Player already taken!");
MapTool.showError("Handshake.msg.playerAlreadyConnected");
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ private void handleSignalingMessage(String message) {
case "login" -> {
var loginMsg = gson.fromJson(message, LoginMessageDto.class);
if (!loginMsg.success) {
MapTool.showError("Servername already taken!");
MapTool.showError("ServerDialog.error.serverAlreadyExists");
}
}
case "offer" -> {
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/net/rptools/maptool/client/AppActions.java
Original file line number Diff line number Diff line change
Expand Up @@ -2242,7 +2242,8 @@ protected void executeAction() {
serverProps.getPort(),
serverProps.getRPToolsName(),
"localhost",
serverProps.getUseEasyConnect());
serverProps.getUseEasyConnect(),
serverProps.getUseWebRtc());

// Use the existing campaign
Campaign campaign = MapTool.getCampaign();
Expand Down Expand Up @@ -2414,7 +2415,8 @@ protected void executeAction() {
"",
dialog.getPort(),
prefs.getServerName(),
dialog.getServer());
dialog.getServer(),
dialog.getUseWebRTC());

String password =
prefs.getUsePublicKey()
Expand Down
9 changes: 0 additions & 9 deletions src/main/java/net/rptools/maptool/client/AppState.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ public class AppState {
private static boolean collectProfilingData = false;
private static boolean isLoggingToConsole = false;
private static boolean isLockedForBackgroundTask = false;
private static boolean useWebRTC = false;
private static boolean enableFullScreenUI = true;

private static PropertyChangeSupport changeSupport = new PropertyChangeSupport(AppState.class);
Expand Down Expand Up @@ -218,14 +217,6 @@ public static void setNotificationEnforced(boolean enforce) {
enforceNotification = enforce;
}

public static boolean useWebRTC() {
return useWebRTC;
}

public static void setUseWebRTC(boolean use) {
useWebRTC = use;
}

public static boolean isFullScreenUIEnabled() {
return enableFullScreenUI;
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/rptools/maptool/client/MapTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -1035,7 +1035,7 @@ public static void startServer(
try {
MapToolRegistry.RegisterResponse result =
MapToolRegistry.getInstance()
.registerInstance(config.getServerName(), config.getPort());
.registerInstance(config.getServerName(), config.getPort(), config.getUseWebRTC());
if (result == MapToolRegistry.RegisterResponse.NAME_EXISTS) {
MapTool.showError("msg.error.alreadyRegistered");
}
Expand Down
14 changes: 13 additions & 1 deletion src/main/java/net/rptools/maptool/client/MapToolRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public static MapToolRegistry getInstance() {
public static class SeverConnectionDetails {
public String address;
public int port;
public boolean webrtc;
}

public enum RegisterResponse {
Expand All @@ -94,6 +95,16 @@ public SeverConnectionDetails findInstance(String id) {
details.address = json.getAsJsonPrimitive("address").getAsString();
details.port = json.getAsJsonPrimitive("port").getAsInt();

// currently the webrtc property is sent as int. In the future this will
// change to boolean. So we check what the type is. Can be removed when
// we get it as boolean.
var webrtcProperty = json.getAsJsonPrimitive("webrtc");
if (webrtcProperty.isBoolean()) {
details.webrtc = webrtcProperty.getAsBoolean();
} else {
details.webrtc = webrtcProperty.getAsInt() > 0;
}

return details;

} catch (IOException | NullPointerException e) {
Expand Down Expand Up @@ -121,11 +132,12 @@ public List<String> findAllInstances() {
}
}

public RegisterResponse registerInstance(String id, int port) {
public RegisterResponse registerInstance(String id, int port, boolean webrtc) {
JsonObject body = new JsonObject();
body.addProperty("name", id);
body.addProperty("port", port);
body.addProperty("address", getAddress());
body.addProperty("webrtc", webrtc);
if (MapTool.isDevelopment()) {
body.addProperty("version", "Dev");
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,10 @@
import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableColumn;
import net.rptools.maptool.client.AppConstants;
import net.rptools.maptool.client.AppState;
import net.rptools.maptool.client.MapTool;
import net.rptools.maptool.client.MapToolRegistry;
import net.rptools.maptool.client.MapToolRegistry.SeverConnectionDetails;
Expand All @@ -62,8 +59,7 @@ public class ConnectToServerDialog extends AbeillePanel<ConnectToServerDialogPre

private boolean accepted;
private GenericDialog dialog;
private int port;
private String hostname;
private SeverConnectionDetails connectionDetails = new SeverConnectionDetails();

/** This is the default constructor */
public ConnectToServerDialog() {
Expand All @@ -78,11 +74,15 @@ protected void preModelBind() {
}

public int getPort() {
return port;
return connectionDetails.port;
}

public String getServer() {
return hostname;
return connectionDetails.address;
}

public boolean getUseWebRTC() {
return connectionDetails.webrtc;
}

public void showDialog() {
Expand All @@ -100,30 +100,7 @@ public void showDialog() {

boolean usePublicKey = getUsePublicKeyCheckBox().isSelected();
getPasswordTextField().setEnabled(!usePublicKey);
getServerNameTextField()
.getDocument()
.addDocumentListener(
new DocumentListener() {
private void checkName() {
getUseWebRTCCheckBox().setEnabled(getServerNameTextField().getText().length() > 0);
}

@Override
public void insertUpdate(DocumentEvent e) {
checkName();
}

@Override
public void removeUpdate(DocumentEvent e) {
checkName();
}

@Override
public void changedUpdate(DocumentEvent e) {
checkName();
}
});
getUseWebRTCCheckBox().setEnabled(getServerNameTextField().getText().length() > 0);
dialog.showDialog();
}

Expand Down Expand Up @@ -264,10 +241,6 @@ public void initRefreshButton() {
getRefreshButton().addActionListener(e -> updateRemoteServerList());
}

public JCheckBox getUseWebRTCCheckBox() {
return (JCheckBox) getComponent("@useWebRTC");
}

public JTextField getUsernameTextField() {
return (JTextField) getComponent("@username");
}
Expand Down Expand Up @@ -327,8 +300,8 @@ private void handleOK() {
}
// OK
ServerInfo info = (ServerInfo) getLocalServerList().getSelectedValue();
port = info.port;
hostname = info.address.getHostAddress();
connectionDetails.port = info.port;
connectionDetails.address = info.address.getHostAddress();
}
if (SwingUtil.hasComponent(selectedPanel, "directPanel")) {
// TODO: put these into a validation method
Expand All @@ -352,8 +325,8 @@ private void handleOK() {
getHostTextField().setText(host);

// OK
port = portTemp;
hostname = host;
connectionDetails.port = portTemp;
connectionDetails.address = host;
}
if (SwingUtil.hasComponent(selectedPanel, "rptoolsPanel")) {
String serverName = getServerNameTextField().getText().trim();
Expand All @@ -369,18 +342,12 @@ private void handleOK() {
MapTool.showError(I18N.getText("ServerDialog.error.serverNotFound", serverName));
return;
}
hostname = serverInfo.address;
try {
port = serverInfo.port;
} catch (NumberFormatException nfe) {
MapTool.showError("ServerDialog.error.portNumberException");
return;
}
connectionDetails = serverInfo;
}
try {
InetAddress server = InetAddress.getByName(hostname);
InetAddress server = InetAddress.getByName(connectionDetails.address);
InetAddress extAddress = InetAddress.getByName(externalAddress);
if (extAddress != null && extAddress.equals(server) && !getUseWebRTCCheckBox().isSelected()) {
if (extAddress != null && extAddress.equals(server) && !connectionDetails.webrtc) {
boolean yes =
MapTool.confirm(
"ConnectToServerDialog.warning.doNotUseExternalAddress",
Expand All @@ -393,8 +360,6 @@ private void handleOK() {
}
if (commit()) {
accepted = true;
JCheckBox useWebRTCCheckBox = getUseWebRTCCheckBox();
AppState.setUseWebRTC(useWebRTCCheckBox.isEnabled() && useWebRTCCheckBox.isSelected());
dialog.closeDialog();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import net.rptools.maptool.client.AppPreferences;
import net.rptools.maptool.client.AppState;
import net.rptools.maptool.client.MapTool;
import net.rptools.maptool.client.swing.AbeillePanel;
import net.rptools.maptool.client.swing.GenericDialog;
Expand Down Expand Up @@ -264,9 +263,11 @@ public void initOKButton() {
prefs.setLockTokenEditOnStart(lockTokenEditOnStartup.isSelected());
prefs.setLockPlayerMovementOnStart(lockPlayerMoveOnStartup.isSelected());
prefs.setPlayerLibraryLock(lockPlayerLibrary.isSelected());

JCheckBox useWebRTCCheckBox = getUseWebRTCCheckBox();
AppState.setUseWebRTC(
prefs.setKeyUseWebrtc(
useWebRTCCheckBox.isEnabled() && useWebRTCCheckBox.isSelected());

accepted = true;
dialog.closeDialog();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ public class StartServerDialogPreferences {
private static final String KEY_START_LOCKED_PLAYER_MOVEMENT = "lockPlayerMovementOnStartup";
private static final String KEY_LOCK_PLAYER_LIBRARY = "lockPlayerLibrary";

private static final String KEY_USE_WEBRTC = "useWebRTC";

private static Boolean useToolTipsForUnformattedRolls = null;

public Player.Role getRole() {
Expand Down Expand Up @@ -259,4 +261,12 @@ public boolean getPlayerLibraryLock() {
public void setPlayerLibraryLock(boolean flag) {
prefs.putBoolean(KEY_LOCK_PLAYER_LIBRARY, flag);
}

public boolean getUseWebRtc() {
return prefs.getBoolean(KEY_USE_WEBRTC, false);
}

public void setKeyUseWebrtc(boolean flag) {
prefs.putBoolean(KEY_USE_WEBRTC, flag);
}
}
Loading