openvidu-java-client: improve default values of Properties

pull/739/head
pabloFuente 2022-06-16 12:21:11 +02:00
parent e5fc3927b5
commit 3908dfc146
3 changed files with 88 additions and 62 deletions

View File

@ -4,6 +4,7 @@ import java.net.MalformedURLException;
import java.net.URI; import java.net.URI;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -20,6 +21,19 @@ import com.google.gson.JsonObject;
*/ */
public class ConnectionProperties { public class ConnectionProperties {
public static class DefaultValues {
public static final ConnectionType type = ConnectionType.WEBRTC;
public static final String data = "";
public static final Boolean record = true;
public static final OpenViduRole role = OpenViduRole.PUBLISHER;
public static final KurentoOptions kurentoOptions = null;
public static final String rtspUri = null;
public static final Boolean adaptativeBitrate = true;
public static final Boolean onlyPlayWithSubscribers = true;
public static final Integer networkCache = 2000;
public static final List<IceServerProperties> customIceServers = null;
}
private ConnectionType type; private ConnectionType type;
// COMMON // COMMON
private String data; private String data;
@ -57,7 +71,6 @@ public class ConnectionProperties {
private Boolean onlyPlayWithSubscribers; private Boolean onlyPlayWithSubscribers;
private Integer networkCache; private Integer networkCache;
/** /**
* Builder for {@link io.openvidu.java.client.ConnectionProperties}. * Builder for {@link io.openvidu.java.client.ConnectionProperties}.
*/ */
@ -132,10 +145,9 @@ public class ConnectionProperties {
/** /**
* Call this method to flag the streams published by this Connection to be * Call this method to flag the streams published by this Connection to be
* recorded or not. This only affects * recorded or not. This only affects <a href=
* <a href="https://docs.openvidu.io/en/stable/advanced-features/recording/#individual-recording-selection"> * "https://docs.openvidu.io/en/stable/advanced-features/recording/#individual-recording-selection">
* INDIVIDUAL recording * INDIVIDUAL recording </a>. If not set, by default will be true.
* </a>. If not set, by default will be true.
*/ */
public Builder record(boolean record) { public Builder record(boolean record) {
this.record = record; this.record = record;
@ -242,28 +254,34 @@ public class ConnectionProperties {
} }
/** /**
* On certain type of networks, clients using default OpenVidu STUN/TURN server can not be reached it because * On certain type of networks, clients using default OpenVidu STUN/TURN server
* firewall rules and network topologies at the client side. This method allows you to configure your * can not be reached it because firewall rules and network topologies at the
* own ICE Server for specific connections if you need it. This is usually not necessary, only it is usefull for * client side. This method allows you to configure your own ICE Server for
* OpenVidu users behind firewalls which allows traffic from/to specific ports which may need a custom * specific connections if you need it. This is usually not necessary, only it
* ICE Server configuration * is usefull for OpenVidu users behind firewalls which allows traffic from/to
* specific ports which may need a custom ICE Server configuration
* *
* Add an ICE Server if in your use case you need this connection to use your own ICE Server deployment. * Add an ICE Server if in your use case you need this connection to use your
* When the user uses this connection, it will use the specified ICE Servers defined here. * own ICE Server deployment. When the user uses this connection, it will use
* the specified ICE Servers defined here.
* *
* The level of precedence for ICE Server configuration on every OpenVidu connection is: * The level of precedence for ICE Server configuration on every OpenVidu
* connection is:
* <ol> * <ol>
* <li>Configured ICE Server using Openvidu.setAdvancedCofiguration() at openvidu-browser.</li> * <li>Configured ICE Server using Openvidu.setAdvancedCofiguration() at
* openvidu-browser.</li>
* <li>Configured ICE server at * <li>Configured ICE server at
* {@link io.openvidu.java.client.ConnectionProperties#customIceServers ConnectionProperties.customIceServers}</li> * {@link io.openvidu.java.client.ConnectionProperties#customIceServers
* <li>Configured ICE Server at global configuration parameter: OPENVIDU_WEBRTC_ICE_SERVERS</li> * ConnectionProperties.customIceServers}</li>
* <li>Configured ICE Server at global configuration parameter:
* OPENVIDU_WEBRTC_ICE_SERVERS</li>
* <li>Default deployed Coturn within OpenVidu deployment</li> * <li>Default deployed Coturn within OpenVidu deployment</li>
* </ol> * </ol>
* <br> * <br>
* If no value is found at level 1, level 2 will be used, and so on until level 4. * If no value is found at level 1, level 2 will be used, and so on until level
* 4. <br>
* This method is equivalent to level 2 of precedence. <br>
* <br> * <br>
* This method is equivalent to level 2 of precedence.
* <br><br>
* <strong>Only for * <strong>Only for
* {@link io.openvidu.java.client.ConnectionType#WEBRTC}</strong> * {@link io.openvidu.java.client.ConnectionType#WEBRTC}</strong>
*/ */
@ -303,15 +321,14 @@ public class ConnectionProperties {
} }
/** /**
* <a href="https://docs.openvidu.io/en/stable/openvidu-pro/" * <a href="https://docs.openvidu.io/en/stable/openvidu-pro/" style="display:
* style="display: inline-block; background-color: rgb(0, 136, 170); color: * inline-block; background-color: rgb(0, 136, 170); color: white; font-weight:
* white; font-weight: bold; padding: 0px 5px; margin-right: 5px; border-radius: * bold; padding: 0px 5px; margin-right: 5px; border-radius: 3px; font-size:
* 3px; font-size: 13px; line-height:21px; font-family: Montserrat, * 13px; line-height:21px; font-family: Montserrat, sans-serif">PRO</a> Whether
* sans-serif">PRO</a> Whether the streams published by this Connection will be * the streams published by this Connection will be recorded or not. This only
* recorded or not. This only affects * affects <a href=
* <a href="https://docs.openvidu.io/en/stable/advanced-features/recording/#individual-recording-selection"> * "https://docs.openvidu.io/en/stable/advanced-features/recording/#individual-recording-selection">
* INDIVIDUAL recording * INDIVIDUAL recording </a>.
* </a>.
*/ */
public Boolean record() { public Boolean record() {
return this.record; return this.record;
@ -401,11 +418,12 @@ public class ConnectionProperties {
} }
/** /**
* Returns a list of custom ICE Servers configured for this connection. * Returns a list of custom ICE Servers configured for this connection. <br>
* <br><br> * <br>
* See {@link io.openvidu.java.client.ConnectionProperties.Builder#addCustomIceServer(IceServerProperties)} for more * See
* information. * {@link io.openvidu.java.client.ConnectionProperties.Builder#addCustomIceServer(IceServerProperties)}
* <br><br> * for more information. <br>
* <br>
* <strong>Only for * <strong>Only for
* {@link io.openvidu.java.client.ConnectionType#WEBRTC}</strong> * {@link io.openvidu.java.client.ConnectionType#WEBRTC}</strong>
*/ */
@ -485,6 +503,10 @@ public class ConnectionProperties {
ConnectionProperties.Builder builder = new ConnectionProperties.Builder(); ConnectionProperties.Builder builder = new ConnectionProperties.Builder();
if (params == null) {
params = new HashMap<>();
}
String typeString; String typeString;
String data; String data;
try { try {
@ -499,12 +521,12 @@ public class ConnectionProperties {
if (typeString != null) { if (typeString != null) {
type = ConnectionType.valueOf(typeString); type = ConnectionType.valueOf(typeString);
} else { } else {
type = ConnectionType.WEBRTC; type = DefaultValues.type;
} }
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
throw new IllegalArgumentException("Parameter 'type' " + typeString + " is not defined"); throw new IllegalArgumentException("Parameter 'type' " + typeString + " is not defined");
} }
data = data != null ? data : ""; data = data != null ? data : DefaultValues.data;
// Build COMMON options // Build COMMON options
builder.type(type).data(data).record(true); builder.type(type).data(data).record(true);
@ -523,7 +545,7 @@ public class ConnectionProperties {
if (roleString != null) { if (roleString != null) {
role = OpenViduRole.valueOf(roleString); role = OpenViduRole.valueOf(roleString);
} else { } else {
role = OpenViduRole.PUBLISHER; role = DefaultValues.role;
} }
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
throw new IllegalArgumentException("Parameter role " + params.get("role") + " is not defined"); throw new IllegalArgumentException("Parameter role " + params.get("role") + " is not defined");
@ -623,9 +645,10 @@ public class ConnectionProperties {
} catch (ClassCastException e) { } catch (ClassCastException e) {
throw new IllegalArgumentException("Type error in some parameter: " + e.getMessage()); throw new IllegalArgumentException("Type error in some parameter: " + e.getMessage());
} }
adaptativeBitrate = adaptativeBitrate != null ? adaptativeBitrate : true; adaptativeBitrate = adaptativeBitrate != null ? adaptativeBitrate : DefaultValues.adaptativeBitrate;
onlyPlayWithSubscribers = onlyPlayWithSubscribers != null ? onlyPlayWithSubscribers : true; onlyPlayWithSubscribers = onlyPlayWithSubscribers != null ? onlyPlayWithSubscribers
networkCache = networkCache != null ? networkCache : 2000; : DefaultValues.onlyPlayWithSubscribers;
networkCache = networkCache != null ? networkCache : DefaultValues.networkCache;
if (rtspUri != null) { if (rtspUri != null) {
try { try {
checkRtspUri(rtspUri); checkRtspUri(rtspUri);
@ -649,7 +672,7 @@ public class ConnectionProperties {
} catch (ClassCastException e) { } catch (ClassCastException e) {
throw new IllegalArgumentException("Type error in parameter 'record': " + e.getMessage()); throw new IllegalArgumentException("Type error in parameter 'record': " + e.getMessage());
} }
record = record != null ? record : true; record = record != null ? record : DefaultValues.record;
builder.record(record); builder.record(record);
return builder; return builder;

View File

@ -31,6 +31,17 @@ import com.google.gson.reflect.TypeToken;
*/ */
public class SessionProperties { public class SessionProperties {
public static class DefaultValues {
public static final MediaMode mediaMode = MediaMode.ROUTED;
public static final RecordingMode recordingMode = RecordingMode.MANUAL;
public static final RecordingProperties defaultRecordingProperties = new RecordingProperties.Builder().build();
public static final String customSessionId = "";
public static final String mediaNode = null;
public static final VideoCodec forcedVideoCodec = VideoCodec.MEDIA_SERVER_PREFERRED;
public static final VideoCodec forcedVideoCodecResolved = VideoCodec.NONE;
public static final Boolean allowTranscoding = false;
}
private MediaMode mediaMode; private MediaMode mediaMode;
private RecordingMode recordingMode; private RecordingMode recordingMode;
private RecordingProperties defaultRecordingProperties; private RecordingProperties defaultRecordingProperties;
@ -45,14 +56,14 @@ public class SessionProperties {
*/ */
public static class Builder { public static class Builder {
private MediaMode mediaMode = MediaMode.ROUTED; private MediaMode mediaMode = DefaultValues.mediaMode;
private RecordingMode recordingMode = RecordingMode.MANUAL; private RecordingMode recordingMode = DefaultValues.recordingMode;
private RecordingProperties defaultRecordingProperties = new RecordingProperties.Builder().build(); private RecordingProperties defaultRecordingProperties = DefaultValues.defaultRecordingProperties;
private String customSessionId = ""; private String customSessionId = DefaultValues.customSessionId;
private String mediaNode; private String mediaNode = DefaultValues.mediaNode;
private VideoCodec forcedVideoCodec = VideoCodec.MEDIA_SERVER_PREFERRED; private VideoCodec forcedVideoCodec = DefaultValues.forcedVideoCodec;
private VideoCodec forcedVideoCodecResolved = VideoCodec.NONE; private VideoCodec forcedVideoCodecResolved = DefaultValues.forcedVideoCodecResolved;
private Boolean allowTranscoding = false; private Boolean allowTranscoding = DefaultValues.allowTranscoding;
/** /**
* Returns the {@link io.openvidu.java.client.SessionProperties} object properly * Returns the {@link io.openvidu.java.client.SessionProperties} object properly
@ -338,18 +349,14 @@ public class SessionProperties {
} }
try { try {
// Safe parameter retrieval. Default values if not defined // Safe parameter retrieval. Let default values if not defined
if (recordingModeString != null) { if (recordingModeString != null) {
RecordingMode recordingMode = RecordingMode.valueOf(recordingModeString); RecordingMode recordingMode = RecordingMode.valueOf(recordingModeString);
builder = builder.recordingMode(recordingMode); builder = builder.recordingMode(recordingMode);
} else {
builder = builder.recordingMode(RecordingMode.MANUAL);
} }
if (mediaModeString != null) { if (mediaModeString != null) {
MediaMode mediaMode = MediaMode.valueOf(mediaModeString); MediaMode mediaMode = MediaMode.valueOf(mediaModeString);
builder = builder.mediaMode(mediaMode); builder = builder.mediaMode(mediaMode);
} else {
builder = builder.mediaMode(MediaMode.ROUTED);
} }
if (customSessionId != null && !customSessionId.isEmpty()) { if (customSessionId != null && !customSessionId.isEmpty()) {
if (!isValidCustomSessionId(customSessionId)) { if (!isValidCustomSessionId(customSessionId)) {
@ -358,12 +365,10 @@ public class SessionProperties {
} }
builder = builder.customSessionId(customSessionId); builder = builder.customSessionId(customSessionId);
} }
if (forcedVideoCodec != null) { if (forcedVideoCodec != null) {
builder = builder.forcedVideoCodec(forcedVideoCodec); builder = builder.forcedVideoCodec(forcedVideoCodec);
builder = builder.forcedVideoCodecResolved(forcedVideoCodec); builder = builder.forcedVideoCodecResolved(forcedVideoCodec);
} }
if (allowTranscoding != null) { if (allowTranscoding != null) {
builder = builder.allowTranscoding(allowTranscoding); builder = builder.allowTranscoding(allowTranscoding);
} }
@ -380,7 +385,6 @@ public class SessionProperties {
} }
if (defaultRecordingPropertiesJson != null) { if (defaultRecordingPropertiesJson != null) {
try { try {
String jsonString = defaultRecordingPropertiesJson.toString(); String jsonString = defaultRecordingPropertiesJson.toString();
RecordingProperties.Builder recBuilder = RecordingProperties RecordingProperties.Builder recBuilder = RecordingProperties
.fromJson(new Gson().fromJson(jsonString, Map.class), null); .fromJson(new Gson().fromJson(jsonString, Map.class), null);
@ -390,8 +394,6 @@ public class SessionProperties {
throw new IllegalArgumentException( throw new IllegalArgumentException(
"Parameter 'defaultRecordingProperties' is not valid: " + e.getMessage()); "Parameter 'defaultRecordingProperties' is not valid: " + e.getMessage());
} }
} else {
builder.defaultRecordingProperties(new RecordingProperties.Builder().build());
} }
String mediaNode = getMediaNodeProperty(params); String mediaNode = getMediaNodeProperty(params);

View File

@ -269,10 +269,10 @@ public class SessionRestController {
@RequestMapping(value = "/sessions/{sessionId}/connection", method = RequestMethod.POST) @RequestMapping(value = "/sessions/{sessionId}/connection", method = RequestMethod.POST)
public ResponseEntity<?> initializeConnection(@PathVariable("sessionId") String sessionId, public ResponseEntity<?> initializeConnection(@PathVariable("sessionId") String sessionId,
@RequestBody Map<String, ?> params) { @RequestBody(required = false) Map<String, ?> params) {
log.info("REST API: POST {} {}", RequestMappings.API + "/sessions/" + sessionId + "/connection", log.info("REST API: POST {} {}", RequestMappings.API + "/sessions/" + sessionId + "/connection",
params.toString()); params != null ? params.toString() : "{}");
Session session = this.sessionManager.getSessionWithNotActive(sessionId); Session session = this.sessionManager.getSessionWithNotActive(sessionId);
if (session == null) { if (session == null) {
@ -838,7 +838,8 @@ public class SessionRestController {
ConnectionType type = ConnectionProperties.fromJson(params).build().getType(); ConnectionType type = ConnectionProperties.fromJson(params).build().getType();
if (ConnectionType.WEBRTC.equals(type)) { if (ConnectionType.WEBRTC.equals(type)) {
if (params.get("customIceServers") == null && !openviduConfig.getWebrtcIceServersBuilders().isEmpty()) { if (params != null && params.get("customIceServers") == null
&& !openviduConfig.getWebrtcIceServersBuilders().isEmpty()) {
// If not defined in Connection, check if defined in OpenVidu global config // If not defined in Connection, check if defined in OpenVidu global config
for (IceServerProperties.Builder iceServerPropertiesBuilder : openviduConfig for (IceServerProperties.Builder iceServerPropertiesBuilder : openviduConfig
.getWebrtcIceServersBuilders()) { .getWebrtcIceServersBuilders()) {