openvidu-server: improved collection of RecordingProperties REST API params

pull/621/head
pabloFuente 2021-04-09 17:24:01 +02:00
parent e3f66a75ec
commit 1d026e4a7d
2 changed files with 160 additions and 70 deletions

View File

@ -915,105 +915,190 @@ public class SessionRestController {
protected RecordingProperties.Builder getRecordingPropertiesFromParams(Map<?, ?> params, Session session) protected RecordingProperties.Builder getRecordingPropertiesFromParams(Map<?, ?> params, Session session)
throws Exception { throws Exception {
RecordingProperties.Builder builder = new RecordingProperties.Builder(); // Final properties being used
String nameFinal = null;
Boolean hasAudioFinal = null;
Boolean hasVideoFinal = null;
OutputMode outputModeFinal = null;
RecordingLayout recordingLayoutFinal = null;
String resolutionFinal = null;
Integer frameRateFinal = null;
Long shmSizeFinal = null;
String customLayoutFinal = null;
RecordingProperties defaultProps = session.getSessionProperties().defaultRecordingProperties();
// Default properties configured in Session
String nameDefault = defaultProps.name();
Boolean hasAudioDefault = defaultProps.hasAudio();
Boolean hasVideoDefault = defaultProps.hasVideo();
OutputMode outputModeDefault = defaultProps.outputMode();
RecordingLayout recordingLayoutDefault = defaultProps.recordingLayout();
String resolutionDefault = defaultProps.resolution();
Integer frameRateDefault = defaultProps.frameRate();
Long shmSizeDefault = defaultProps.shmSize();
String customLayoutDefault = defaultProps.customLayout();
// Provided properties through params
String sessionIdParam;
String nameParam;
Boolean hasAudioParam;
Boolean hasVideoParam;
String outputModeStringParam;
String recordingLayoutStringParam;
String resolutionParam;
Integer frameRateParam;
Long shmSizeParam = null;
String customLayoutParam;
String sessionId;
String name;
String outputModeString;
String resolution;
Integer frameRate;
Boolean hasAudio;
Boolean hasVideo;
String recordingLayoutString;
String customLayout;
Long shmSize = null;
try { try {
sessionId = (String) params.get("session"); sessionIdParam = (String) params.get("session");
name = (String) params.get("name"); nameParam = (String) params.get("name");
outputModeString = (String) params.get("outputMode"); hasAudioParam = (Boolean) params.get("hasAudio");
resolution = (String) params.get("resolution"); hasVideoParam = (Boolean) params.get("hasVideo");
frameRate = (Integer) params.get("frameRate"); outputModeStringParam = (String) params.get("outputMode");
hasAudio = (Boolean) params.get("hasAudio"); recordingLayoutStringParam = (String) params.get("recordingLayout");
hasVideo = (Boolean) params.get("hasVideo"); resolutionParam = (String) params.get("resolution");
recordingLayoutString = (String) params.get("recordingLayout"); frameRateParam = (Integer) params.get("frameRate");
customLayout = (String) params.get("customLayout");
if (params.get("shmSize") != null) { if (params.get("shmSize") != null) {
shmSize = Long.parseLong(params.get("shmSize").toString()); shmSizeParam = Long.parseLong(params.get("shmSize").toString());
} }
customLayoutParam = (String) params.get("customLayout");
} catch (ClassCastException | NumberFormatException e) { } catch (ClassCastException | NumberFormatException e) {
throw new Exception("Type error in some parameter: " + e.getMessage()); throw new Exception("Type error in some parameter: " + e.getMessage());
} }
if (sessionId == null) { if (sessionIdParam == null) {
// "session" parameter not found // "session" parameter not found
throw new Exception("\"session\" parameter is mandatory"); throw new Exception("\"session\" parameter is mandatory");
} }
if (name != null && !name.isEmpty()) { if (nameParam != null) {
if (!sessionManager.formatChecker.isValidRecordingName(name)) { if (!sessionManager.formatChecker.isValidRecordingName(nameParam)) {
throw new Exception("Parameter 'name' is wrong. Must be an alphanumeric string [a-zA-Z0-9_-]"); throw new Exception("Parameter 'name' is wrong. Must be an alphanumeric string [a-zA-Z0-9_-]+");
} }
nameFinal = nameParam;
} else if (nameDefault != null) {
nameFinal = nameDefault;
} else {
nameFinal = "";
} }
OutputMode finalOutputMode = OutputMode.COMPOSED; if (hasAudioParam != null) {
RecordingLayout recordingLayout = null; hasAudioFinal = hasAudioParam;
if (outputModeString != null && !outputModeString.isEmpty()) { } else if (hasAudioDefault != null) {
try { hasAudioFinal = hasAudioDefault;
finalOutputMode = OutputMode.valueOf(outputModeString); } else {
} catch (Exception e) { hasAudioFinal = RecordingProperties.DefaultValues.hasAudio;
throw new Exception("Type error in parameter 'outputMode'");
}
} }
if (RecordingUtils.IS_COMPOSED(finalOutputMode)) {
if (resolution != null && !sessionManager.formatChecker.isAcceptableRecordingResolution(resolution)) { if (hasVideoParam != null) {
throw new RuntimeException( hasVideoFinal = hasVideoParam;
"Wrong 'resolution' parameter. Acceptable values from 100 to 1999 for both width and height"); } else if (hasAudioDefault != null) {
} hasVideoFinal = hasVideoDefault;
if (frameRate != null && !sessionManager.formatChecker.isAcceptableRecordingFrameRate(frameRate)) { } else {
throw new RuntimeException("Wrong 'frameRate' parameter. Must be a number between 1 and 120"); hasVideoFinal = RecordingProperties.DefaultValues.hasVideo;
}
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) {
if (!hasAudioFinal && !hasVideoFinal) {
// Cannot start a recording with both "hasAudio" and "hasVideo" to false // 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"); throw new RuntimeException("Cannot start a recording with both \"hasAudio\" and \"hasVideo\" set to false");
} }
RecordingProperties defaultRecordingProperties = session.getSessionProperties().defaultRecordingProperties(); if (outputModeStringParam != null) {
try {
outputModeFinal = OutputMode.valueOf(outputModeStringParam);
// If outputMode is COMPOSED when defaultOutputMode is COMPOSED_QUICK_START, // If param outputMode is COMPOSED when default is COMPOSED_QUICK_START,
// change outputMode to COMPOSED_QUICK_START (and vice versa) // change outputMode to COMPOSED_QUICK_START (and vice versa)
OutputMode defaultOutputMode = defaultRecordingProperties.outputMode(); if (OutputMode.COMPOSED_QUICK_START.equals(outputModeDefault)
if (OutputMode.COMPOSED_QUICK_START.equals(defaultOutputMode) && OutputMode.COMPOSED.equals(finalOutputMode)) { && OutputMode.COMPOSED.equals(outputModeFinal)) {
finalOutputMode = OutputMode.COMPOSED_QUICK_START; outputModeFinal = OutputMode.COMPOSED_QUICK_START;
} else if (OutputMode.COMPOSED.equals(defaultOutputMode) } else if (OutputMode.COMPOSED.equals(outputModeDefault)
&& OutputMode.COMPOSED_QUICK_START.equals(finalOutputMode)) { && OutputMode.COMPOSED_QUICK_START.equals(outputModeFinal)) {
finalOutputMode = OutputMode.COMPOSED; outputModeFinal = OutputMode.COMPOSED;
}
} catch (Exception e) {
throw new Exception("Type error in parameter 'outputMode'");
}
} else if (outputModeDefault != null) {
outputModeFinal = outputModeDefault;
} else {
outputModeFinal = RecordingProperties.DefaultValues.outputMode;
} }
builder.outputMode(finalOutputMode == null ? defaultRecordingProperties.outputMode() : finalOutputMode); if (RecordingUtils.IS_COMPOSED(outputModeFinal)) {
if (RecordingUtils.IS_COMPOSED(finalOutputMode)) {
builder.resolution(resolution == null ? defaultRecordingProperties.resolution() : resolution); if (recordingLayoutStringParam != null) {
builder.frameRate(frameRate == null ? defaultRecordingProperties.frameRate() : frameRate); try {
builder.recordingLayout( recordingLayoutFinal = RecordingLayout.valueOf(recordingLayoutStringParam);
recordingLayout == null ? defaultRecordingProperties.recordingLayout() : recordingLayout); } catch (Exception e) {
if (RecordingLayout.CUSTOM.equals(recordingLayout)) { throw new Exception("Type error in parameter 'recordingLayout'");
builder.customLayout(customLayout == null ? defaultRecordingProperties.customLayout() : customLayout); }
} else if (recordingLayoutDefault != null) {
recordingLayoutFinal = recordingLayoutDefault;
} else {
recordingLayoutFinal = RecordingProperties.DefaultValues.recordingLayout;
} }
if (shmSize != null) {
if (shmSize < 134217728L) { if (resolutionParam != null) {
if (!sessionManager.formatChecker.isAcceptableRecordingResolution(resolutionParam)) {
throw new RuntimeException(
"Wrong 'resolution' parameter. Acceptable values from 100 to 1999 for both width and height");
}
resolutionFinal = resolutionParam;
} else if (resolutionDefault != null) {
resolutionFinal = resolutionDefault;
} else {
resolutionFinal = RecordingProperties.DefaultValues.resolution;
}
if (frameRateParam != null) {
if (!sessionManager.formatChecker.isAcceptableRecordingFrameRate(frameRateParam)) {
throw new RuntimeException(
"Wrong 'resolution' parameter. Acceptable values from 100 to 1999 for both width and height");
}
frameRateFinal = frameRateParam;
} else if (frameRateDefault != null) {
frameRateFinal = frameRateDefault;
} else {
frameRateFinal = RecordingProperties.DefaultValues.frameRate;
}
if (shmSizeParam != null) {
if (!sessionManager.formatChecker.isAcceptableRecordingShmSize(shmSizeParam)) {
throw new RuntimeException("Wrong \"shmSize\" parameter. Must be 134217728 (128 MB) minimum"); throw new RuntimeException("Wrong \"shmSize\" parameter. Must be 134217728 (128 MB) minimum");
} }
builder.shmSize(shmSize); shmSizeFinal = shmSizeParam;
} else if (shmSizeDefault != null) {
shmSizeFinal = shmSizeDefault;
} else {
shmSizeFinal = RecordingProperties.DefaultValues.shmSize;
}
if (RecordingLayout.CUSTOM.equals(recordingLayoutFinal)) {
if (customLayoutParam != null) {
customLayoutFinal = customLayoutParam;
} else if (customLayoutDefault != null) {
customLayoutFinal = customLayoutDefault;
} else {
customLayoutFinal = "";
}
}
}
RecordingProperties.Builder builder = new RecordingProperties.Builder();
builder.name(nameFinal).hasAudio(hasAudioFinal).hasVideo(hasVideoFinal).outputMode(outputModeFinal);
if (RecordingUtils.IS_COMPOSED(outputModeFinal) && hasVideoFinal) {
builder.recordingLayout(recordingLayoutFinal);
builder.resolution(resolutionFinal);
builder.frameRate(frameRateFinal);
builder.shmSize(shmSizeFinal);
if (RecordingLayout.CUSTOM.equals(recordingLayoutFinal)) {
builder.customLayout(customLayoutFinal);
} }
} }
builder.name(name).hasAudio(hasAudio != null ? hasAudio : true).hasVideo(hasVideo != null ? hasVideo : true);
return builder; return builder;
} }

View File

@ -30,6 +30,11 @@ public class FormatChecker {
return (frameRate > 0 && frameRate <= 120); return (frameRate > 0 && frameRate <= 120);
} }
public boolean isAcceptableRecordingShmSize(Long shmSize) {
// Long grater than 134217728 (128 MB)
return (shmSize >= 134217728L);
}
public boolean isServerMetadataFormatCorrect(String metadata) { public boolean isServerMetadataFormatCorrect(String metadata) {
return true; return true;
} }