openvidu-server: simplify logic for recording track selection

Two changes that try to make the code easier to read and maintain in the
future. The logic itself doesn't change.

- Do not repeat the MediaProfile assignments. This makes the flow much
easier to understand. And less error prone.

- Do not mix error handling with actual logic. By throwing early, the
actual selection logic that follows is more obvious.
pull/660/head
Juan Navarro 2021-09-20 18:55:30 +02:00
parent 4e6e37fe39
commit 0678d86eeb
1 changed files with 40 additions and 36 deletions

View File

@ -371,46 +371,50 @@ public class SingleStreamRecordingService extends RecordingService {
boolean propertiesHasAudio = properties.hasAudio(); boolean propertiesHasAudio = properties.hasAudio();
boolean propertiesHasVideo = properties.hasVideo(); boolean propertiesHasVideo = properties.hasVideo();
if (streamHasAudio) { // Detect some error conditions to provide a hint about what's wrong
if (streamHasVideo) {
// Stream has both audio and video tracks
if (propertiesHasAudio) { if (!streamHasAudio && !streamHasVideo) {
if (propertiesHasVideo) {
profile = MediaProfileSpecType.WEBM;
} else {
profile = MediaProfileSpecType.WEBM_AUDIO_ONLY;
}
} else {
profile = MediaProfileSpecType.WEBM_VIDEO_ONLY;
}
} else {
// Stream has audio track only
if (propertiesHasAudio) {
profile = MediaProfileSpecType.WEBM_AUDIO_ONLY;
} else {
// ERROR: RecordingProperties set to video only but there's no video track
throw new OpenViduException(
Code.MEDIA_TYPE_STREAM_INCOMPATIBLE_WITH_RECORDING_PROPERTIES_ERROR_CODE,
"RecordingProperties set to \"hasAudio(false)\" but stream is audio-only");
}
}
} else if (streamHasVideo) {
// Stream has video track only
if (propertiesHasVideo) {
profile = MediaProfileSpecType.WEBM_VIDEO_ONLY;
} else {
// ERROR: RecordingProperties set to audio only but there's no audio track
throw new OpenViduException(Code.MEDIA_TYPE_STREAM_INCOMPATIBLE_WITH_RECORDING_PROPERTIES_ERROR_CODE,
"RecordingProperties set to \"hasVideo(false)\" but stream is video-only");
}
} else {
// ERROR: Stream has no track at all. This branch should never be reachable // ERROR: Stream has no track at all. This branch should never be reachable
throw new OpenViduException(Code.MEDIA_TYPE_STREAM_INCOMPATIBLE_WITH_RECORDING_PROPERTIES_ERROR_CODE, throw new OpenViduException(
Code.MEDIA_TYPE_STREAM_INCOMPATIBLE_WITH_RECORDING_PROPERTIES_ERROR_CODE,
"Stream has no track at all. Cannot be recorded"); "Stream has no track at all. Cannot be recorded");
} }
if (!propertiesHasAudio && !propertiesHasVideo) {
// ERROR: Properties don't enable any track. This branch should never be reachable
throw new OpenViduException(
Code.MEDIA_TYPE_RECORDING_PROPERTIES_ERROR_CODE,
"RecordingProperties cannot set both \"hasAudio(false)\" and \"hasVideo(false)\"");
}
boolean streamOnlyAudio = streamHasAudio && !streamHasVideo;
if (streamOnlyAudio && !propertiesHasAudio) {
throw new OpenViduException(
Code.MEDIA_TYPE_STREAM_INCOMPATIBLE_WITH_RECORDING_PROPERTIES_ERROR_CODE,
"RecordingProperties set to \"hasAudio(false)\" but stream is audio-only");
}
boolean streamOnlyVideo = !streamHasAudio && streamHasVideo;
if (streamOnlyVideo & !propertiesHasVideo) {
throw new OpenViduException(
Code.MEDIA_TYPE_STREAM_INCOMPATIBLE_WITH_RECORDING_PROPERTIES_ERROR_CODE,
"RecordingProperties set to \"hasVideo(false)\" but stream is video-only");
}
// Detect the requested media kinds and select the appropriate profile
boolean recordAudio = streamHasAudio && propertiesHasAudio;
boolean recordVideo = streamHasVideo && propertiesHasVideo;
if (recordAudio && recordVideo) {
profile = MediaProfileSpecType.WEBM;
}
else if (recordAudio) {
profile = MediaProfileSpecType.WEBM_AUDIO_ONLY;
}
else if (recordVideo) {
profile = MediaProfileSpecType.WEBM_VIDEO_ONLY;
}
return profile; return profile;
} }