/*
This file is part of Libresonic.
Libresonic is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Libresonic is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Libresonic. If not, see <http://www.gnu.org/licenses/>.
Copyright 2016 (C) Libresonic Authors
Based upon Subsonic, Copyright 2009 (C) Sindre Mehus
*/
package org.libresonic.player.controller;
import org.libresonic.player.domain.MediaFile;
import org.libresonic.player.domain.User;
import org.libresonic.player.service.*;
import org.libresonic.player.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
/**
* Controller for the page used to play videos.
*
* @author Sindre Mehus
*/
@Controller
@RequestMapping("/videoPlayer")
public class VideoPlayerController {
public static final int DEFAULT_BIT_RATE = 2000;
public static final int[] BIT_RATES = {200, 300, 400, 500, 700, 1000, 1200, 1500, 2000, 3000, 5000};
@Autowired
private MediaFileService mediaFileService;
@Autowired
private PlayerService playerService;
@Autowired
private SecurityService securityService;
@RequestMapping(method = RequestMethod.GET)
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
User user = securityService.getCurrentUser(request);
Map<String, Object> map = new HashMap<String, Object>();
int id = ServletRequestUtils.getRequiredIntParameter(request, "id");
MediaFile file = mediaFileService.getMediaFile(id);
mediaFileService.populateStarredDate(file, user.getUsername());
Integer duration = file.getDurationSeconds();
String playerId = playerService.getPlayer(request, response).getId();
String url = NetworkService.getBaseUrl(request);
String streamUrl = url + "/stream?id=" + file.getId() + "&player=" + playerId;
String coverArtUrl = url + "/coverArt.view?id=" + file.getId();
map.put("video", file);
map.put("streamUrl", streamUrl);
map.put("remoteStreamUrl", streamUrl);
map.put("remoteCoverArtUrl", coverArtUrl);
map.put("duration", duration);
map.put("bitRates", BIT_RATES);
map.put("defaultBitRate", DEFAULT_BIT_RATE);
map.put("user", user);
return new ModelAndView("videoPlayer", "model", map);
}
public static Map<String, Integer> createSkipOffsets(int durationSeconds) {
LinkedHashMap<String, Integer> result = new LinkedHashMap<String, Integer>();
for (int i = 0; i < durationSeconds; i += 60) {
result.put(StringUtil.formatDuration(i), i);
}
return result;
}
}