From b102a44d507e2ac4b38e3b2aa9f3541a8164ae21 Mon Sep 17 00:00:00 2001 From: pabloFuente Date: Fri, 10 Jun 2022 11:54:47 +0200 Subject: [PATCH] Move ConnectionProperties#fromJson from openvidu-server to openvidu-java-client --- .../io/openvidu/java/client/Connection.java | 20 +- .../java/client/ConnectionProperties.java | 217 +++++++++++++++++- .../client/test/ConnectionPropertiesTest.java | 111 +++++++++ .../kurento/core/KurentoSessionManager.java | 28 +-- .../server/rest/SessionRestController.java | 163 +------------ 5 files changed, 353 insertions(+), 186 deletions(-) create mode 100644 openvidu-java-client/src/test/java/io/openvidu/java/client/test/ConnectionPropertiesTest.java diff --git a/openvidu-java-client/src/main/java/io/openvidu/java/client/Connection.java b/openvidu-java-client/src/main/java/io/openvidu/java/client/Connection.java index 368d7d87..3126f64f 100644 --- a/openvidu-java-client/src/main/java/io/openvidu/java/client/Connection.java +++ b/openvidu-java-client/src/main/java/io/openvidu/java/client/Connection.java @@ -17,6 +17,7 @@ package io.openvidu.java.client; +import java.net.MalformedURLException; import java.util.ArrayList; import java.util.HashSet; import java.util.List; @@ -25,9 +26,7 @@ import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import com.google.gson.JsonArray; -import com.google.gson.JsonElement; import com.google.gson.JsonObject; -import com.google.gson.JsonParser; /** * See {@link io.openvidu.java.client.Session#getConnections()} @@ -328,9 +327,12 @@ public class Connection { builder.record(this.connectionProperties.record()); } // Keep old configuration in the rest of properties - builder.type(this.connectionProperties.getType()).data(this.connectionProperties.getData()) - .kurentoOptions(this.connectionProperties.getKurentoOptions()) - .rtspUri(this.connectionProperties.getRtspUri()); + try { + builder.type(this.connectionProperties.getType()).data(this.connectionProperties.getData()) + .kurentoOptions(this.connectionProperties.getKurentoOptions()) + .rtspUri(this.connectionProperties.getRtspUri()); + } catch (MalformedURLException e) { + } if (this.connectionProperties.adaptativeBitrate() != null) { builder.adaptativeBitrate(this.connectionProperties.adaptativeBitrate()); } @@ -340,8 +342,9 @@ public class Connection { if (this.connectionProperties.getNetworkCache() != null) { builder.networkCache(this.connectionProperties.getNetworkCache()); } - if (this.connectionProperties.getCustomIceServers() != null && !this.connectionProperties.getCustomIceServers().isEmpty()) { - for (IceServerProperties iceServerProperties: this.connectionProperties.getCustomIceServers()) { + if (this.connectionProperties.getCustomIceServers() != null + && !this.connectionProperties.getCustomIceServers().isEmpty()) { + for (IceServerProperties iceServerProperties : this.connectionProperties.getCustomIceServers()) { builder.addCustomIceServer(iceServerProperties); } } @@ -452,7 +455,8 @@ public class Connection { String credential = (iceJsonObj.has("credential") && !iceJsonObj.get("credential").isJsonNull()) ? iceJsonObj.get("credential").getAsString() : null; - customIceServers.add(new IceServerProperties.Builder().url(url).username(username).credential(credential).build()); + customIceServers.add( + new IceServerProperties.Builder().url(url).username(username).credential(credential).build()); }); } diff --git a/openvidu-java-client/src/main/java/io/openvidu/java/client/ConnectionProperties.java b/openvidu-java-client/src/main/java/io/openvidu/java/client/ConnectionProperties.java index 307e28d9..769b2965 100644 --- a/openvidu-java-client/src/main/java/io/openvidu/java/client/ConnectionProperties.java +++ b/openvidu-java-client/src/main/java/io/openvidu/java/client/ConnectionProperties.java @@ -1,12 +1,19 @@ package io.openvidu.java.client; +import java.net.MalformedURLException; +import java.net.URI; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +import com.google.gson.Gson; import com.google.gson.JsonArray; +import com.google.gson.JsonElement; import com.google.gson.JsonNull; import com.google.gson.JsonObject; -import java.util.ArrayList; -import java.util.List; - /** * See * {@link io.openvidu.java.client.Session#createConnection(ConnectionProperties)} @@ -172,8 +179,11 @@ public class ConnectionProperties { *
* Only for * {@link io.openvidu.java.client.ConnectionType#IPCAM} + * + * @throws MalformedURLException */ - public Builder rtspUri(String rtspUri) { + public Builder rtspUri(String rtspUri) throws MalformedURLException { + checkRtspUri(rtspUri); this.rtspUri = rtspUri; return this; } @@ -463,4 +473,203 @@ public class ConnectionProperties { return json; } + /** + * Obtain a {@link ConnectionProperties.Builder} directly from a JSON object in + * the form of a Map + * + * @return A {@link ConnectionProperties.Builder} + * @throws IllegalArgumentException If some parameter has a wrong type or an + * invalid value + */ + public static ConnectionProperties.Builder fromJson(Map params) throws IllegalArgumentException { + + ConnectionProperties.Builder builder = new ConnectionProperties.Builder(); + + String typeString; + String data; + try { + typeString = (String) params.get("type"); + data = (String) params.get("data"); + } catch (ClassCastException e) { + throw new IllegalArgumentException("Type error in some parameter: " + e.getMessage()); + } + + ConnectionType type; + try { + if (typeString != null) { + type = ConnectionType.valueOf(typeString); + } else { + type = ConnectionType.WEBRTC; + } + } catch (IllegalArgumentException e) { + throw new IllegalArgumentException("Parameter 'type' " + typeString + " is not defined"); + } + data = data != null ? data : ""; + + // Build COMMON options + builder.type(type).data(data).record(true); + + OpenViduRole role = null; + KurentoOptions kurentoOptions = null; + + if (ConnectionType.WEBRTC.equals(type)) { + String roleString; + try { + roleString = (String) params.get("role"); + } catch (ClassCastException e) { + throw new IllegalArgumentException("Type error in parameter 'role': " + e.getMessage()); + } + try { + if (roleString != null) { + role = OpenViduRole.valueOf(roleString); + } else { + role = OpenViduRole.PUBLISHER; + } + } catch (IllegalArgumentException e) { + throw new IllegalArgumentException("Parameter role " + params.get("role") + " is not defined"); + } + JsonObject kurentoOptionsJson = null; + if (params.get("kurentoOptions") != null) { + try { + kurentoOptionsJson = new Gson().toJsonTree(params.get("kurentoOptions"), Map.class) + .getAsJsonObject(); + } catch (Exception e) { + throw new IllegalArgumentException( + "Error in parameter 'kurentoOptions'. It is not a valid JSON object"); + } + } + if (kurentoOptionsJson != null) { + try { + KurentoOptions.Builder builder2 = new KurentoOptions.Builder(); + if (kurentoOptionsJson.has("videoMaxRecvBandwidth")) { + builder2.videoMaxRecvBandwidth(kurentoOptionsJson.get("videoMaxRecvBandwidth").getAsInt()); + } + if (kurentoOptionsJson.has("videoMinRecvBandwidth")) { + builder2.videoMinRecvBandwidth(kurentoOptionsJson.get("videoMinRecvBandwidth").getAsInt()); + } + if (kurentoOptionsJson.has("videoMaxSendBandwidth")) { + builder2.videoMaxSendBandwidth(kurentoOptionsJson.get("videoMaxSendBandwidth").getAsInt()); + } + if (kurentoOptionsJson.has("videoMinSendBandwidth")) { + builder2.videoMinSendBandwidth(kurentoOptionsJson.get("videoMinSendBandwidth").getAsInt()); + } + if (kurentoOptionsJson.has("allowedFilters")) { + JsonArray filters = kurentoOptionsJson.get("allowedFilters").getAsJsonArray(); + String[] arrayOfFilters = new String[filters.size()]; + Iterator it = filters.iterator(); + int index = 0; + while (it.hasNext()) { + arrayOfFilters[index] = it.next().getAsString(); + index++; + } + builder2.allowedFilters(arrayOfFilters); + } + kurentoOptions = builder2.build(); + } catch (Exception e) { + throw new IllegalArgumentException( + "Type error in some parameter of 'kurentoOptions': " + e.getMessage()); + } + } + + // Custom Ice Servers + JsonArray customIceServersJsonArray = null; + if (params.get("customIceServers") != null) { + try { + customIceServersJsonArray = new Gson().toJsonTree(params.get("customIceServers"), List.class) + .getAsJsonArray(); + } catch (Exception e) { + throw new IllegalArgumentException( + "Error in parameter 'customIceServersJson'. It is not a valid JSON object"); + } + } + if (customIceServersJsonArray != null) { + try { + for (int i = 0; i < customIceServersJsonArray.size(); i++) { + JsonObject customIceServerJson = customIceServersJsonArray.get(i).getAsJsonObject(); + IceServerProperties.Builder iceServerPropertiesBuilder = new IceServerProperties.Builder(); + iceServerPropertiesBuilder.url(customIceServerJson.get("url").getAsString()); + if (customIceServerJson.has("staticAuthSecret")) { + iceServerPropertiesBuilder + .staticAuthSecret(customIceServerJson.get("staticAuthSecret").getAsString()); + } + if (customIceServerJson.has("username")) { + iceServerPropertiesBuilder.username(customIceServerJson.get("username").getAsString()); + } + if (customIceServerJson.has("credential")) { + iceServerPropertiesBuilder.credential(customIceServerJson.get("credential").getAsString()); + } + IceServerProperties iceServerProperties = iceServerPropertiesBuilder.build(); + builder.addCustomIceServer(iceServerProperties); + } + } catch (Exception e) { + throw new IllegalArgumentException( + "Type error in some parameter of 'customIceServers': " + e.getMessage()); + } + } + + // Build WEBRTC options + builder.role(role).kurentoOptions(kurentoOptions); + + } else if (ConnectionType.IPCAM.equals(type)) { + String rtspUri; + Boolean adaptativeBitrate; + Boolean onlyPlayWithSubscribers; + Number networkCache; + try { + rtspUri = (String) params.get("rtspUri"); + adaptativeBitrate = (Boolean) params.get("adaptativeBitrate"); + onlyPlayWithSubscribers = (Boolean) params.get("onlyPlayWithSubscribers"); + networkCache = (Number) params.get("networkCache"); + } catch (ClassCastException e) { + throw new IllegalArgumentException("Type error in some parameter: " + e.getMessage()); + } + adaptativeBitrate = adaptativeBitrate != null ? adaptativeBitrate : true; + onlyPlayWithSubscribers = onlyPlayWithSubscribers != null ? onlyPlayWithSubscribers : true; + networkCache = networkCache != null ? networkCache : 2000; + if (rtspUri != null) { + try { + checkRtspUri(rtspUri); + } catch (MalformedURLException e) { + throw new IllegalArgumentException("Error in parameter 'rtspUri': " + e.getMessage()); + } + } + + // Build IPCAM options + try { + builder.rtspUri(rtspUri).adaptativeBitrate(adaptativeBitrate) + .onlyPlayWithSubscribers(onlyPlayWithSubscribers).networkCache(networkCache.intValue()).build(); + } catch (MalformedURLException e) { + throw new IllegalArgumentException("Type error in some parameter: " + e.getMessage()); + } + } + + Boolean record; + try { + record = (Boolean) params.get("record"); + } catch (ClassCastException e) { + throw new IllegalArgumentException("Type error in parameter 'record': " + e.getMessage()); + } + record = record != null ? record : true; + builder.record(record); + + return builder; + } + + /** + * @hidden + */ + public static URI checkRtspUri(String rtspUri) throws MalformedURLException { + try { + URI uri = new URI(rtspUri); + List allowedSchemes = Arrays.asList("file", "rtsp", "rtsps"); + if (!allowedSchemes.contains(uri.getScheme())) { + throw new MalformedURLException( + "RTSP URI does not contain a valid protocol " + allowedSchemes.toString()); + } + return uri; + } catch (Exception e) { + throw new MalformedURLException(e.getMessage()); + } + } + } diff --git a/openvidu-java-client/src/test/java/io/openvidu/java/client/test/ConnectionPropertiesTest.java b/openvidu-java-client/src/test/java/io/openvidu/java/client/test/ConnectionPropertiesTest.java new file mode 100644 index 00000000..195e3a3d --- /dev/null +++ b/openvidu-java-client/src/test/java/io/openvidu/java/client/test/ConnectionPropertiesTest.java @@ -0,0 +1,111 @@ +package io.openvidu.java.client.test; + +import static org.junit.Assert.assertThrows; + +import java.util.Map; + +import com.google.gson.Gson; +import com.google.gson.JsonObject; + +import io.openvidu.java.client.ConnectionProperties; +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +public class ConnectionPropertiesTest extends TestCase { + + public ConnectionPropertiesTest(String testName) { + super(testName); + } + + public static Test suite() { + return new TestSuite(ConnectionPropertiesTest.class); + } + + public void testWebrtcFromJsonSuccess() { + String jsonString = "{'type':'WEBRTC','data':'MY_CUSTOM_STRING','record':false,'role':'SUBSCRIBER','kurentoOptions':{'videoMaxRecvBandwidth':333,'videoMinRecvBandwidth':333,'videoMaxSendBandwidth':333,'videoMinSendBandwidth':333,'allowedFilters':['CustomFilter']},'customIceServers':[{'url':'turn:turn-domain.com:443','username':'MY_CUSTOM_STRING','credential':'MY_CUSTOM_STRING'}]}"; + JsonObject originalJson = new Gson().fromJson(jsonString, JsonObject.class); + Map map = mapFromJsonString(jsonString); + ConnectionProperties.Builder builder = ConnectionProperties.fromJson(map); + ConnectionProperties props = builder.build(); + JsonObject finalJson = props.toJson("MY_CUSTOM_STRING"); + finalJson = removeIpcamProps(finalJson); + assertEquals(originalJson, finalJson); + } + + public void testIpcamFromJsonSuccess() { + String jsonString = "{'type':'IPCAM','data':'MY_CUSTOM_STRING','record':false,'rtspUri':'rtsp://your.camera.ip.sdp','adaptativeBitrate':false,'onlyPlayWithSubscribers':false,'networkCache':333}"; + JsonObject originalJson = new Gson().fromJson(jsonString, JsonObject.class); + Map map = mapFromJsonString(jsonString); + ConnectionProperties.Builder builder = ConnectionProperties.fromJson(map); + ConnectionProperties props = builder.build(); + JsonObject finalJson = props.toJson("MY_CUSTOM_STRING"); + finalJson = removeWebrtcProps(finalJson); + assertEquals(originalJson, finalJson); + + jsonString = "{'type':'IPCAM','rtspUri':'rtsp://your.camera.ip.sdp'}"; + ConnectionProperties.fromJson(mapFromJsonString(jsonString)).build(); + + jsonString = "{'type':'IPCAM','rtspUri':'rtsps://your.camera.ip.sdp'}"; + ConnectionProperties.fromJson(mapFromJsonString(jsonString)).build(); + + jsonString = "{'type':'IPCAM','rtspUri':'file://your.camera.ip.sdp'}"; + ConnectionProperties.fromJson(mapFromJsonString(jsonString)).build(); + } + + public void testFromJsonError() { + Map map = mapFromJsonString("{'type':'NOT_EXISTS'}"); + assertException(map, "type"); + + map = mapFromJsonString("{'data':4000}"); + assertException(map, ""); + + map = mapFromJsonString("{'record':4000}"); + assertException(map, "record"); + + map = mapFromJsonString("{'role':'NOT_EXISTS'}"); + assertException(map, "role"); + + map = mapFromJsonString("{'kurentoOptions':{'allowedFilters':[{'OBJECT_NOT_EXPECTED':true}]}}"); + assertException(map, "kurentoOptions"); + + map = mapFromJsonString("{'customIceServers':[{'url':'NOT_A_VALID_URI'}]}"); + assertException(map, "customIceServers"); + + map = mapFromJsonString("{'type':'IPCAM','rtspUri':'NOT_A_VALID_RTSP_URI'}"); + assertException(map, "rtspUri"); + + map = mapFromJsonString("{'type':'IPCAM','rtspUri':'https://domain.com'}"); + assertException(map, "rtspUri"); + + map = mapFromJsonString("{'type':'IPCAM','rtspUri':'filse://your.camera.ip.sdp'}"); + assertException(map, "rtspUri"); + } + + private JsonObject removeIpcamProps(JsonObject json) { + json.remove("session"); + json.remove("adaptativeBitrate"); + json.remove("networkCache"); + json.remove("onlyPlayWithSubscribers"); + json.remove("rtspUri"); + return json; + } + + private JsonObject removeWebrtcProps(JsonObject json) { + json.remove("session"); + json.remove("kurentoOptions"); + json.remove("role"); + json.remove("customIceServers"); + return json; + } + + private Map mapFromJsonString(String json) { + return new Gson().fromJson(json, Map.class); + } + + private void assertException(Map params, String containsError) { + IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, + () -> ConnectionProperties.fromJson(params)); + assertTrue(exception.getMessage().contains(containsError)); + } +} diff --git a/openvidu-server/src/main/java/io/openvidu/server/kurento/core/KurentoSessionManager.java b/openvidu-server/src/main/java/io/openvidu/server/kurento/core/KurentoSessionManager.java index b60b9d8f..44a6de6d 100644 --- a/openvidu-server/src/main/java/io/openvidu/server/kurento/core/KurentoSessionManager.java +++ b/openvidu-server/src/main/java/io/openvidu/server/kurento/core/KurentoSessionManager.java @@ -19,8 +19,7 @@ package io.openvidu.server.kurento.core; import java.io.IOException; import java.net.InetAddress; -import java.net.MalformedURLException; -import java.net.URL; +import java.net.URI; import java.sql.Timestamp; import java.util.Arrays; import java.util.Collections; @@ -28,8 +27,6 @@ import java.util.HashSet; import java.util.NoSuchElementException; import java.util.Set; import java.util.concurrent.TimeUnit; -import java.util.regex.Matcher; -import java.util.regex.Pattern; import javax.annotation.PreDestroy; @@ -1134,25 +1131,12 @@ public class KurentoSessionManager extends SessionManager { // Generate the location for the IpCam GeoLocation location = null; - URL url = null; + URI uri = ConnectionProperties.checkRtspUri(kMediaOptions.rtspUri); + String protocol = uri.getScheme(); InetAddress ipAddress = null; - String protocol = null; - try { - Pattern pattern = Pattern.compile("^(file|rtsp|rtsps)://"); - Matcher matcher = pattern.matcher(kMediaOptions.rtspUri); - if (matcher.find()) { - protocol = matcher.group(0).replaceAll("://$", ""); - } else { - throw new MalformedURLException(); - } - String parsedUrl = kMediaOptions.rtspUri.replaceAll("^.*?://", "http://"); - url = new URL(parsedUrl); - } catch (Exception e) { - throw new MalformedURLException(); - } try { - ipAddress = InetAddress.getByName(url.getHost()); + ipAddress = InetAddress.getByName(uri.getHost()); location = this.geoLocationByIp.getLocationByIp(ipAddress); } catch (IOException e) { e.printStackTrace(); @@ -1163,8 +1147,8 @@ public class KurentoSessionManager extends SessionManager { } String rtspConnectionId = kMediaOptions.getTypeOfVideo() + "_" + protocol + "_" - + RandomStringUtils.randomAlphanumeric(4).toUpperCase() + "_" + url.getHost() - + (url.getPort() != -1 ? (":" + url.getPort()) : "") + url.getPath(); + + RandomStringUtils.randomAlphanumeric(4).toUpperCase() + "_" + uri.getHost() + + (uri.getPort() != -1 ? (":" + uri.getPort()) : "") + uri.getPath(); rtspConnectionId = rtspConnectionId.replace("/", "_").replace("-", "").replace(".", "_").replace(":", "_"); rtspConnectionId = IdentifierPrefixes.IPCAM_ID + rtspConnectionId; diff --git a/openvidu-server/src/main/java/io/openvidu/server/rest/SessionRestController.java b/openvidu-server/src/main/java/io/openvidu/server/rest/SessionRestController.java index fba84303..ce0ad625 100644 --- a/openvidu-server/src/main/java/io/openvidu/server/rest/SessionRestController.java +++ b/openvidu-server/src/main/java/io/openvidu/server/rest/SessionRestController.java @@ -21,7 +21,6 @@ import java.net.MalformedURLException; import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; -import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.concurrent.TimeUnit; @@ -46,7 +45,6 @@ import org.springframework.web.bind.annotation.RestController; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.JsonArray; -import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; @@ -56,9 +54,7 @@ import io.openvidu.client.internal.ProtocolElements; import io.openvidu.java.client.ConnectionProperties; import io.openvidu.java.client.ConnectionType; import io.openvidu.java.client.IceServerProperties; -import io.openvidu.java.client.KurentoOptions; import io.openvidu.java.client.MediaMode; -import io.openvidu.java.client.OpenViduRole; import io.openvidu.java.client.Recording.OutputMode; import io.openvidu.java.client.RecordingLayout; import io.openvidu.java.client.RecordingProperties; @@ -99,13 +95,13 @@ public class SessionRestController { protected OpenviduConfig openviduConfig; @RequestMapping(value = "/sessions", method = RequestMethod.POST) - public ResponseEntity initializeSession(@RequestBody(required = false) Map params) { + public ResponseEntity initializeSession(@RequestBody(required = false) Map params) { log.info("REST API: POST {}/sessions {}", RequestMappings.API, params != null ? params.toString() : "{}"); SessionProperties sessionProperties; try { - sessionProperties = getSessionPropertiesFromParams((Map) params).build(); + sessionProperties = getSessionPropertiesFromParams(params).build(); } catch (Exception e) { return this.generateErrorResponse(e.getMessage(), "/sessions", HttpStatus.BAD_REQUEST); } @@ -275,7 +271,7 @@ public class SessionRestController { @RequestMapping(value = "/sessions/{sessionId}/connection", method = RequestMethod.POST) public ResponseEntity initializeConnection(@PathVariable("sessionId") String sessionId, - @RequestBody Map params) { + @RequestBody Map params) { log.info("REST API: POST {} {}", RequestMappings.API + "/sessions/" + sessionId + "/connection", params.toString()); @@ -374,7 +370,7 @@ public class SessionRestController { } @RequestMapping(value = "/recordings/start", method = RequestMethod.POST) - public ResponseEntity startRecording(@RequestBody Map params) { + public ResponseEntity startRecording(@RequestBody Map params) { if (params == null) { return this.generateErrorResponse("Error in body parameters. Cannot be empty", "/recordings/start", @@ -559,7 +555,7 @@ public class SessionRestController { } @RequestMapping(value = "/tokens", method = RequestMethod.POST) - public ResponseEntity newToken(@RequestBody Map params) { + public ResponseEntity newToken(@RequestBody Map params) { if (params == null) { return this.generateErrorResponse("Error in body parameters. Cannot be empty", "/tokens", @@ -642,7 +638,7 @@ public class SessionRestController { } @RequestMapping(value = "/signal", method = RequestMethod.POST) - public ResponseEntity signal(@RequestBody Map params) { + public ResponseEntity signal(@RequestBody Map params) { if (params == null) { return this.generateErrorResponse("Error in body parameters. Cannot be empty", "/signal", @@ -837,158 +833,21 @@ public class SessionRestController { return builder; } - protected ConnectionProperties.Builder getConnectionPropertiesFromParams(Map params) throws Exception { + protected ConnectionProperties.Builder getConnectionPropertiesFromParams(Map params) throws Exception { - ConnectionProperties.Builder builder = new ConnectionProperties.Builder(); + ConnectionProperties.Builder builder = ConnectionProperties.fromJson(params); - String typeString; - String data; - try { - typeString = (String) params.get("type"); - data = (String) params.get("data"); - } catch (ClassCastException e) { - throw new Exception("Type error in some parameter: " + e.getMessage()); - } - - ConnectionType type; - try { - if (typeString != null) { - type = ConnectionType.valueOf(typeString); - } else { - type = ConnectionType.WEBRTC; - } - } catch (IllegalArgumentException e) { - throw new Exception("Parameter 'type' " + typeString + " is not defined"); - } - data = data != null ? data : ""; - - // Build COMMON options - builder.type(type).data(data).record(true); - - OpenViduRole role = null; - KurentoOptions kurentoOptions = null; + ConnectionType type = ConnectionProperties.fromJson(params).build().getType(); if (ConnectionType.WEBRTC.equals(type)) { - String roleString; - try { - roleString = (String) params.get("role"); - } catch (ClassCastException e) { - throw new Exception("Type error in parameter 'role': " + e.getMessage()); - } - try { - if (roleString != null) { - role = OpenViduRole.valueOf(roleString); - } else { - role = OpenViduRole.PUBLISHER; - } - } catch (IllegalArgumentException e) { - throw new Exception("Parameter role " + params.get("role") + " is not defined"); - } - JsonObject kurentoOptionsJson = null; - if (params.get("kurentoOptions") != null) { - try { - kurentoOptionsJson = new Gson().toJsonTree(params.get("kurentoOptions"), Map.class) - .getAsJsonObject(); - } catch (Exception e) { - throw new Exception("Error in parameter 'kurentoOptions'. It is not a valid JSON object"); - } - } - if (kurentoOptionsJson != null) { - try { - KurentoOptions.Builder builder2 = new KurentoOptions.Builder(); - if (kurentoOptionsJson.has("videoMaxRecvBandwidth")) { - builder2.videoMaxRecvBandwidth(kurentoOptionsJson.get("videoMaxRecvBandwidth").getAsInt()); - } - if (kurentoOptionsJson.has("videoMinRecvBandwidth")) { - builder2.videoMinRecvBandwidth(kurentoOptionsJson.get("videoMinRecvBandwidth").getAsInt()); - } - if (kurentoOptionsJson.has("videoMaxSendBandwidth")) { - builder2.videoMaxSendBandwidth(kurentoOptionsJson.get("videoMaxSendBandwidth").getAsInt()); - } - if (kurentoOptionsJson.has("videoMinSendBandwidth")) { - builder2.videoMinSendBandwidth(kurentoOptionsJson.get("videoMinSendBandwidth").getAsInt()); - } - if (kurentoOptionsJson.has("allowedFilters")) { - JsonArray filters = kurentoOptionsJson.get("allowedFilters").getAsJsonArray(); - String[] arrayOfFilters = new String[filters.size()]; - Iterator it = filters.iterator(); - int index = 0; - while (it.hasNext()) { - arrayOfFilters[index] = it.next().getAsString(); - index++; - } - builder2.allowedFilters(arrayOfFilters); - } - kurentoOptions = builder2.build(); - } catch (Exception e) { - throw new Exception("Type error in some parameter of 'kurentoOptions': " + e.getMessage()); - } - } - - // Custom Ice Servers - JsonArray customIceServersJsonArray = null; - if (params.get("customIceServers") != null) { - try { - customIceServersJsonArray = new Gson().toJsonTree(params.get("customIceServers"), List.class) - .getAsJsonArray(); - } catch (Exception e) { - throw new Exception("Error in parameter 'customIceServersJson'. It is not a valid JSON object"); - } - } - if (customIceServersJsonArray != null) { - try { - for (int i = 0; i < customIceServersJsonArray.size(); i++) { - JsonObject customIceServerJson = customIceServersJsonArray.get(i).getAsJsonObject(); - IceServerProperties.Builder iceServerPropertiesBuilder = new IceServerProperties.Builder(); - iceServerPropertiesBuilder.url(customIceServerJson.get("url").getAsString()); - if (customIceServerJson.has("staticAuthSecret")) { - iceServerPropertiesBuilder - .staticAuthSecret(customIceServerJson.get("staticAuthSecret").getAsString()); - } - if (customIceServerJson.has("username")) { - iceServerPropertiesBuilder.username(customIceServerJson.get("username").getAsString()); - } - if (customIceServerJson.has("credential")) { - iceServerPropertiesBuilder.credential(customIceServerJson.get("credential").getAsString()); - } - IceServerProperties iceServerProperties = iceServerPropertiesBuilder.build(); - builder.addCustomIceServer(iceServerProperties); - } - } catch (Exception e) { - throw new Exception("Type error in some parameter of 'customIceServers': " + e.getMessage()); - } - } else if (!openviduConfig.getWebrtcIceServersBuilders().isEmpty()) { - // If not defined in connection, check if defined in openvidu config + if (params.get("customIceServers") == null && !openviduConfig.getWebrtcIceServersBuilders().isEmpty()) { + // If not defined in Connection, check if defined in OpenVidu global config for (IceServerProperties.Builder iceServerPropertiesBuilder : openviduConfig .getWebrtcIceServersBuilders()) { IceServerProperties.Builder configIceBuilder = iceServerPropertiesBuilder.clone(); builder.addCustomIceServer(configIceBuilder.build()); } } - - // Build WEBRTC options - builder.role(role).kurentoOptions(kurentoOptions); - - } else if (ConnectionType.IPCAM.equals(type)) { - String rtspUri; - Boolean adaptativeBitrate; - Boolean onlyPlayWithSubscribers; - Integer networkCache; - try { - rtspUri = (String) params.get("rtspUri"); - adaptativeBitrate = (Boolean) params.get("adaptativeBitrate"); - onlyPlayWithSubscribers = (Boolean) params.get("onlyPlayWithSubscribers"); - networkCache = (Integer) params.get("networkCache"); - } catch (ClassCastException e) { - throw new Exception("Type error in some parameter: " + e.getMessage()); - } - adaptativeBitrate = adaptativeBitrate != null ? adaptativeBitrate : true; - onlyPlayWithSubscribers = onlyPlayWithSubscribers != null ? onlyPlayWithSubscribers : true; - networkCache = networkCache != null ? networkCache : 2000; - - // Build IPCAM options - builder.rtspUri(rtspUri).adaptativeBitrate(adaptativeBitrate) - .onlyPlayWithSubscribers(onlyPlayWithSubscribers).networkCache(networkCache).build(); } return builder;