openvidu: Add OPENVIDU_WEBRTC_ICE_SERVERS configuration paramater

pull/698/head
cruizba 2022-02-13 19:33:41 +01:00
parent 40ed2c5efc
commit 4d579cf8b3
3 changed files with 59 additions and 1 deletions

View File

@ -1516,9 +1516,11 @@ export class Session extends EventDispatcher {
let rtcIceServer: RTCIceServer = {
urls: [ iceServer.url ]
}
logger.log("STUN/TURN server IP: " + iceServer.url);
if (iceServer.username != null && iceServer.credential != null) {
rtcIceServer.username = iceServer.username;
rtcIceServer.credential = iceServer.credential;
logger.log('TURN credentials [' + iceServer.username + ':' + iceServer.credential + ']');
}
this.openvidu.iceServers.push(rtcIceServer);
}

View File

@ -38,6 +38,7 @@ import java.util.Map.Entry;
import javax.annotation.PostConstruct;
import io.openvidu.java.client.IceServerProperties;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.validator.routines.DomainValidator;
@ -220,10 +221,12 @@ public class OpenviduConfig {
private MediaServer mediaServerInfo = MediaServer.kurento;
// Media properties
// Webrtc properties
private boolean webrtcSimulcast = false;
private List<IceServerProperties> webrtcIceServers;
// Plain config properties getters
public String getCoturnDatabaseDbname() {
@ -290,6 +293,10 @@ public class OpenviduConfig {
return this.webrtcSimulcast;
}
public List<IceServerProperties> getWebrtcIceServers() {
return webrtcIceServers;
}
public String getOpenViduRecordingPath() {
return this.openviduRecordingPath;
}
@ -619,6 +626,8 @@ public class OpenviduConfig {
checkCertificateType();
webrtcIceServers = loadWebrtcIceServers("OPENVIDU_WEBRTC_ICE_SERVERS");
}
private void checkCertificateType() {
@ -1147,4 +1156,46 @@ public class OpenviduConfig {
}
}
private List<IceServerProperties> loadWebrtcIceServers(String property) {
String rawIceServers = asOptionalString(property);
List<IceServerProperties> webrtcIceServers = new ArrayList<>();
if (rawIceServers == null || rawIceServers.isEmpty()) {
return webrtcIceServers;
}
List<String> arrayIceServers = asJsonStringsArray(property);
for (String iceServerString : arrayIceServers) {
try {
IceServerProperties iceServerProperties = readIceServer(property, iceServerString);
webrtcIceServers.add(iceServerProperties);
} catch (Exception e) {
addError(property, iceServerString + " is not a valid webrtc ice server: " + e.getMessage());
}
}
return webrtcIceServers;
}
private IceServerProperties readIceServer(String property, String iceServerString) {
String url = null, username = null, credential = null;
String[] iceServerPropList = iceServerString.split(",");
for (String iceServerProp: iceServerPropList) {
String[] iceServerPropEntry = iceServerProp.split("=");
if (iceServerPropEntry.length == 2) {
if (iceServerProp.startsWith("url=")) {
url = iceServerPropEntry[1];
} else if (iceServerProp.startsWith("username=")) {
username = iceServerPropEntry[1];
} else if (iceServerProp.startsWith("credential=")) {
credential = iceServerPropEntry[1];
} else {
addError(property, "Wrong parameter: " + iceServerProp);
}
} else {
addError(property, "Wrong parameter: " + iceServerProp);
}
}
IceServerProperties iceServerProperties = new IceServerProperties.Builder()
.url(url).username(username).credential(credential).build();
return iceServerProperties;
}
}

View File

@ -934,6 +934,11 @@ public class SessionRestController {
} catch (Exception e) {
throw new Exception("Type error in some parameter of 'customIceServers': " + e.getMessage());
}
} else if(!openviduConfig.getWebrtcIceServers().isEmpty()){
// If not defined in connection, check if defined in openvidu config
for (IceServerProperties iceServerProperties: openviduConfig.getWebrtcIceServers()) {
builder.addCustomIceServer(iceServerProperties);
}
}
// Build WEBRTC options