use of org.libresonic.player.util.Pair in project libresonic by Libresonic.
the class HLSController method parseBitRate.
/**
* Parses a string containing the bitrate and an optional width/height, e.g., 1200@640x480
*/
protected Pair<Integer, Dimension> parseBitRate(String bitRate) throws IllegalArgumentException {
Matcher matcher = BITRATE_PATTERN.matcher(bitRate);
if (!matcher.matches()) {
throw new IllegalArgumentException("Invalid bitrate specification: " + bitRate);
}
int kbps = Integer.parseInt(matcher.group(1));
if (matcher.group(3) == null) {
return new Pair<Integer, Dimension>(kbps, null);
} else {
int width = Integer.parseInt(matcher.group(3));
int height = Integer.parseInt(matcher.group(4));
return new Pair<Integer, Dimension>(kbps, new Dimension(width, height));
}
}
use of org.libresonic.player.util.Pair in project libresonic by Libresonic.
the class HLSController method handleRequest.
@RequestMapping(method = RequestMethod.GET)
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
response.setHeader("Access-Control-Allow-Origin", "*");
int id = ServletRequestUtils.getIntParameter(request, "id");
MediaFile mediaFile = mediaFileService.getMediaFile(id);
Player player = playerService.getPlayer(request, response);
String username = player.getUsername();
if (mediaFile == null) {
response.sendError(HttpServletResponse.SC_NOT_FOUND, "Media file not found: " + id);
return null;
}
if (username != null && !securityService.isFolderAccessAllowed(mediaFile, username)) {
response.sendError(HttpServletResponse.SC_FORBIDDEN, "Access to file " + mediaFile.getId() + " is forbidden for user " + username);
return null;
}
Integer duration = mediaFile.getDurationSeconds();
if (duration == null || duration == 0) {
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Unknown duration for media file: " + id);
return null;
}
response.setContentType("application/vnd.apple.mpegurl");
response.setCharacterEncoding(StringUtil.ENCODING_UTF8);
List<Pair<Integer, Dimension>> bitRates = parseBitRates(request);
PrintWriter writer = response.getWriter();
if (bitRates.size() > 1) {
generateVariantPlaylist(request, id, player, bitRates, writer);
} else {
generateNormalPlaylist(request, id, player, bitRates.size() == 1 ? bitRates.get(0) : null, duration, writer);
}
return null;
}
Aggregations