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 f14a09f8..eb4d2f58 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 @@ -325,28 +325,10 @@ public class SessionRestController { } String sessionId; - String name; - String outputModeString; - String resolution; - Boolean hasAudio; - Boolean hasVideo; - String recordingLayoutString; - String customLayout; - Long shmSize = null; try { sessionId = (String) params.get("session"); - name = (String) params.get("name"); - outputModeString = (String) params.get("outputMode"); - resolution = (String) params.get("resolution"); - hasAudio = (Boolean) params.get("hasAudio"); - hasVideo = (Boolean) params.get("hasVideo"); - recordingLayoutString = (String) params.get("recordingLayout"); - customLayout = (String) params.get("customLayout"); - if (params.get("shmSize") != null) { - shmSize = new Long(params.get("shmSize").toString()); - } - } catch (ClassCastException | NumberFormatException e) { - return this.generateErrorResponse("Type error in some parameter", "/recordings/start", + } catch (Exception e) { + return this.generateErrorResponse("Type error in parameter \"session\"", "/recordings/start", HttpStatus.BAD_REQUEST); } @@ -356,46 +338,6 @@ public class SessionRestController { HttpStatus.BAD_REQUEST); } - if (name != null && !name.isEmpty()) { - if (!sessionManager.formatChecker.isValidRecordingName(name)) { - return this.generateErrorResponse( - "Parameter 'name' is wrong. Must be an alphanumeric string [a-zA-Z0-9_-]", "/sessions", - HttpStatus.BAD_REQUEST); - } - } - - OutputMode finalOutputMode = OutputMode.COMPOSED; - RecordingLayout recordingLayout = null; - if (outputModeString != null && !outputModeString.isEmpty()) { - try { - finalOutputMode = OutputMode.valueOf(outputModeString); - } catch (Exception e) { - return this.generateErrorResponse("Type error in parameter 'outputMode'", "/recordings/start", - HttpStatus.BAD_REQUEST); - } - } - if (RecordingUtils.IS_COMPOSED(finalOutputMode)) { - if (resolution != null && !sessionManager.formatChecker.isAcceptableRecordingResolution(resolution)) { - return this.generateErrorResponse( - "Wrong 'resolution' parameter. Acceptable values from 100 to 1999 for both width and height", - "/recordings/start", HttpStatus.UNPROCESSABLE_ENTITY); - } - if (recordingLayoutString != null && !recordingLayoutString.isEmpty()) { - try { - recordingLayout = RecordingLayout.valueOf(recordingLayoutString); - } catch (Exception e) { - return this.generateErrorResponse("Type error in parameter 'recordingLayout'", "/recordings/start", - HttpStatus.BAD_REQUEST); - } - } - } - if ((hasAudio != null && hasVideo != null) && !hasAudio && !hasVideo) { - // Cannot start a recording with both "hasAudio" and "hasVideo" to false - return this.generateErrorResponse( - "Cannot start a recording with both \"hasAudio\" and \"hasVideo\" set to false", - "/recordings/start", HttpStatus.UNPROCESSABLE_ENTITY); - } - Session session = sessionManager.getSession(sessionId); if (session == null) { session = sessionManager.getSessionNotActive(sessionId); @@ -422,41 +364,17 @@ public class SessionRestController { return new ResponseEntity<>(HttpStatus.NOT_ACCEPTABLE); } - // If outputMode is COMPOSED when defaultOutputMode is COMPOSED_QUICK_START, - // change outputMode to COMPOSED_QUICK_START (and vice versa) - OutputMode defaultOutputMode = session.getSessionProperties().defaultOutputMode(); - if (OutputMode.COMPOSED_QUICK_START.equals(defaultOutputMode) && OutputMode.COMPOSED.equals(finalOutputMode)) { - finalOutputMode = OutputMode.COMPOSED_QUICK_START; - } else if (OutputMode.COMPOSED.equals(defaultOutputMode) - && OutputMode.COMPOSED_QUICK_START.equals(finalOutputMode)) { - finalOutputMode = OutputMode.COMPOSED; + RecordingProperties recordingProperties; + try { + recordingProperties = getRecordingPropertiesFromParams(params, session.getSessionProperties()).build(); + } catch (RuntimeException e) { + return this.generateErrorResponse(e.getMessage(), "/sessions", HttpStatus.UNPROCESSABLE_ENTITY); + } catch (Exception e) { + return this.generateErrorResponse(e.getMessage(), "/sessions", HttpStatus.BAD_REQUEST); } - RecordingProperties.Builder builder = new RecordingProperties.Builder(); - builder.outputMode( - finalOutputMode == null ? session.getSessionProperties().defaultOutputMode() : finalOutputMode); - if (RecordingUtils.IS_COMPOSED(finalOutputMode)) { - if (resolution != null) { - builder.resolution(resolution); - } - builder.recordingLayout(recordingLayout == null ? session.getSessionProperties().defaultRecordingLayout() - : recordingLayout); - if (RecordingLayout.CUSTOM.equals(recordingLayout)) { - builder.customLayout( - customLayout == null ? session.getSessionProperties().defaultCustomLayout() : customLayout); - } - if (shmSize != null) { - if (shmSize < 134217728L) { - return this.generateErrorResponse("Wrong \"shmSize\" parameter. Must be 134217728 (128 MB) minimum", - "/recordings/start", HttpStatus.UNPROCESSABLE_ENTITY); - } - builder.shmSize(shmSize); - } - } - builder.name(name).hasAudio(hasAudio != null ? hasAudio : true).hasVideo(hasVideo != null ? hasVideo : true); - try { - Recording startedRecording = this.recordingManager.startRecording(session, builder.build()); + Recording startedRecording = this.recordingManager.startRecording(session, recordingProperties); return new ResponseEntity<>(startedRecording.toJson().toString(), RestUtils.getResponseHeaders(), HttpStatus.OK); } catch (OpenViduException e) { @@ -973,6 +891,95 @@ public class SessionRestController { return builder; } + protected RecordingProperties.Builder getRecordingPropertiesFromParams(Map params, + SessionProperties sessionProperties) throws Exception { + + RecordingProperties.Builder builder = new RecordingProperties.Builder(); + + String sessionId; + String name; + String outputModeString; + String resolution; + Boolean hasAudio; + Boolean hasVideo; + String recordingLayoutString; + String customLayout; + Long shmSize = null; + try { + sessionId = (String) params.get("session"); + name = (String) params.get("name"); + outputModeString = (String) params.get("outputMode"); + resolution = (String) params.get("resolution"); + hasAudio = (Boolean) params.get("hasAudio"); + hasVideo = (Boolean) params.get("hasVideo"); + recordingLayoutString = (String) params.get("recordingLayout"); + customLayout = (String) params.get("customLayout"); + if (params.get("shmSize") != null) { + shmSize = Long.parseLong(params.get("shmSize").toString()); + } + } catch (ClassCastException | NumberFormatException e) { + throw new Exception("Type error in some parameter: " + e.getMessage()); + } + + if (sessionId == null) { + // "session" parameter not found + throw new Exception("\"session\" parameter is mandatory"); + } + + if (name != null && !name.isEmpty()) { + if (!sessionManager.formatChecker.isValidRecordingName(name)) { + throw new Exception("Parameter 'name' is wrong. Must be an alphanumeric string [a-zA-Z0-9_-]"); + } + } + + OutputMode finalOutputMode = OutputMode.COMPOSED; + RecordingLayout recordingLayout = null; + if (outputModeString != null && !outputModeString.isEmpty()) { + try { + finalOutputMode = OutputMode.valueOf(outputModeString); + } catch (Exception e) { + throw new Exception("Type error in parameter 'outputMode'"); + } + } + if (RecordingUtils.IS_COMPOSED(finalOutputMode)) { + if (resolution != null && !sessionManager.formatChecker.isAcceptableRecordingResolution(resolution)) { + throw new RuntimeException( + "Wrong 'resolution' parameter. Acceptable values from 100 to 1999 for both width and height"); + } + if (recordingLayoutString != null && !recordingLayoutString.isEmpty()) { + try { + recordingLayout = RecordingLayout.valueOf(recordingLayoutString); + } catch (Exception e) { + throw new Exception("Type error in parameter 'recordingLayout'"); + } + } + } + if ((hasAudio != null && hasVideo != null) && !hasAudio && !hasVideo) { + // Cannot start a recording with both "hasAudio" and "hasVideo" to false + throw new RuntimeException("Cannot start a recording with both \"hasAudio\" and \"hasVideo\" set to false"); + } + + builder.outputMode(finalOutputMode == null ? sessionProperties.defaultOutputMode() : finalOutputMode); + if (RecordingUtils.IS_COMPOSED(finalOutputMode)) { + if (resolution != null) { + builder.resolution(resolution); + } + builder.recordingLayout( + recordingLayout == null ? sessionProperties.defaultRecordingLayout() : recordingLayout); + if (RecordingLayout.CUSTOM.equals(recordingLayout)) { + builder.customLayout(customLayout == null ? sessionProperties.defaultCustomLayout() : customLayout); + } + if (shmSize != null) { + if (shmSize < 134217728L) { + throw new RuntimeException("Wrong \"shmSize\" parameter. Must be 134217728 (128 MB) minimum"); + } + builder.shmSize(shmSize); + } + } + builder.name(name).hasAudio(hasAudio != null ? hasAudio : true).hasVideo(hasVideo != null ? hasVideo : true); + return builder; + } + protected Token getTokenFromConnectionId(String connectionId, Iterator> iterator) { boolean found = false; Token token = null;