Moved expected content length calculation to TranscodingService

master
Evan Harris 5 years ago
parent b28ffb0d1a
commit 6cfbe0ff88
No known key found for this signature in database
GPG Key ID: FF3BD4DA59FF9EDC
  1. 23
      airsonic-main/src/main/java/org/airsonic/player/controller/StreamController.java
  2. 43
      airsonic-main/src/main/java/org/airsonic/player/service/TranscodingService.java

@ -329,29 +329,6 @@ public class StreamController {
return null; return null;
} }
private long getFileLength(TranscodingService.Parameters parameters) {
MediaFile file = parameters.getMediaFile();
if (!parameters.isDownsample() && !parameters.isTranscode()) {
return file.getFileSize();
}
Integer duration = file.getDurationSeconds();
Integer maxBitRate = parameters.getMaxBitRate();
if (duration == null) {
LOG.warn("Unknown duration for " + file + ". Unable to estimate transcoded size.");
return file.getFileSize();
}
if (maxBitRate == null) {
LOG.error("Unknown bit rate for " + file + ". Unable to estimate transcoded size.");
return file.getFileSize();
}
// Over-estimate size a bit (2 seconds) so don't cut off early in case of small calculation differences
return (duration + 2) * (long)maxBitRate * 1000L / 8L;
}
@Nullable @Nullable
private HttpRange getRange(HttpServletRequest request, Integer fileDuration, Long fileSize) { private HttpRange getRange(HttpServletRequest request, Integer fileDuration, Long fileSize) {

@ -216,6 +216,7 @@ public class TranscodingService {
} }
parameters.setMaxBitRate(maxBitRate); parameters.setMaxBitRate(maxBitRate);
parameters.setExpectedLength(getExpectedLength(parameters));
parameters.setRangeAllowed(isRangeAllowed(parameters)); parameters.setRangeAllowed(isRangeAllowed(parameters));
return parameters; return parameters;
} }
@ -488,6 +489,32 @@ public class TranscodingService {
return matches != null && matches.length > 0; return matches != null && matches.length > 0;
} }
/**
* Returns the length (or predicted/expected length) of a (possibly padded) media stream
*/
private Long getExpectedLength(Parameters parameters) {
MediaFile file = parameters.getMediaFile();
if (!parameters.isDownsample() && !parameters.isTranscode()) {
return file.getFileSize();
}
Integer duration = file.getDurationSeconds();
Integer maxBitRate = parameters.getMaxBitRate();
if (duration == null) {
LOG.warn("Unknown duration for " + file + ". Unable to estimate transcoded size.");
return null;
}
if (maxBitRate == null) {
LOG.error("Unknown bit rate for " + file + ". Unable to estimate transcoded size.");
return null;
}
// Over-estimate size a bit (2 seconds) so don't cut off early in case of small calculation differences
return (duration + 2) * (long)maxBitRate * 1000L / 8L;
}
private boolean isRangeAllowed(Parameters parameters) { private boolean isRangeAllowed(Parameters parameters) {
Transcoding transcoding = parameters.getTranscoding(); Transcoding transcoding = parameters.getTranscoding();
List<String> steps = Arrays.asList(); List<String> steps = Arrays.asList();
@ -498,7 +525,12 @@ public class TranscodingService {
steps = Arrays.asList(settingsService.getDownsamplingCommand()); steps = Arrays.asList(settingsService.getDownsamplingCommand());
} }
else { else {
return true; // neither transcoding or downsampling return true; // neither transcoding nor downsampling
}
// Verify that were able to predict the length
if (parameters.getExpectedLength() == null) {
return false;
} }
// Check if last configured step uses the bitrate, if so, range should be pretty safe // Check if last configured step uses the bitrate, if so, range should be pretty safe
@ -540,6 +572,7 @@ public class TranscodingService {
public static class Parameters { public static class Parameters {
private boolean downsample; private boolean downsample;
private Long expectedLength;
private boolean rangeAllowed; private boolean rangeAllowed;
private final MediaFile mediaFile; private final MediaFile mediaFile;
private final VideoTranscodingSettings videoTranscodingSettings; private final VideoTranscodingSettings videoTranscodingSettings;
@ -575,6 +608,14 @@ public class TranscodingService {
this.rangeAllowed = rangeAllowed; this.rangeAllowed = rangeAllowed;
} }
public Long getExpectedLength() {
return this.expectedLength;
}
public void setExpectedLength(Long expectedLength) {
this.expectedLength = expectedLength;
}
public void setTranscoding(Transcoding transcoding) { public void setTranscoding(Transcoding transcoding) {
this.transcoding = transcoding; this.transcoding = transcoding;
} }

Loading…
Cancel
Save