Search in sources :

Example 1 with FFmpegWebVideo

use of net.pms.encoders.FFmpegWebVideo in project UniversalMediaServer by UniversalMediaServer.

the class RemoteMediaHandler method handle.

@Override
public void handle(HttpExchange httpExchange) throws IOException {
    if (RemoteUtil.deny(httpExchange)) {
        throw new IOException("Access denied");
    }
    RootFolder root = parent.getRoot(RemoteUtil.userName(httpExchange), httpExchange);
    if (root == null) {
        throw new IOException("Unknown root");
    }
    Headers h = httpExchange.getRequestHeaders();
    for (String h1 : h.keySet()) {
        LOGGER.debug("key " + h1 + "=" + h.get(h1));
    }
    String id = RemoteUtil.getId(path, httpExchange);
    id = RemoteUtil.strip(id);
    RendererConfiguration defaultRenderer = renderer;
    if (renderer == null) {
        defaultRenderer = root.getDefaultRenderer();
    }
    DLNAResource resource = root.getDLNAResource(id, defaultRenderer);
    if (resource == null) {
        // another error
        LOGGER.debug("media unkonwn");
        throw new IOException("Bad id");
    }
    if (!resource.isCodeValid(resource)) {
        LOGGER.debug("coded object with invalid code");
        throw new IOException("Bad code");
    }
    DLNAMediaSubtitle sid = null;
    String mimeType = root.getDefaultRenderer().getMimeType(resource.mimeType(), resource.getMedia());
    // DLNAResource dlna = res.get(0);
    WebRender renderer = (WebRender) defaultRenderer;
    DLNAMediaInfo media = resource.getMedia();
    if (media == null) {
        media = new DLNAMediaInfo();
        resource.setMedia(media);
    }
    if (mimeType.equals(FormatConfiguration.MIMETYPE_AUTO) && media.getMimeType() != null) {
        mimeType = media.getMimeType();
    }
    int code = 200;
    resource.setDefaultRenderer(defaultRenderer);
    if (resource.getFormat().isVideo()) {
        if (flash) {
            mimeType = "video/flash";
        } else if (!RemoteUtil.directmime(mimeType) || RemoteUtil.transMp4(mimeType, media)) {
            mimeType = renderer != null ? renderer.getVideoMimeType() : RemoteUtil.transMime();
            if (FileUtil.isUrl(resource.getSystemName())) {
                resource.setPlayer(new FFmpegWebVideo());
            } else if (!(resource instanceof DVDISOTitle)) {
                resource.setPlayer(new FFMpegVideo());
            }
        // code = 206;
        }
        if (PMS.getConfiguration().getWebSubs() && resource.getMediaSubtitle() != null && resource.getMediaSubtitle().isExternal()) {
            // fetched on the side
            sid = resource.getMediaSubtitle();
            resource.setMediaSubtitle(null);
        }
    }
    if (!RemoteUtil.directmime(mimeType) && resource.getFormat().isAudio()) {
        resource.setPlayer(new FFmpegAudio());
        code = 206;
    }
    media.setMimeType(mimeType);
    Range.Byte range = RemoteUtil.parseRange(httpExchange.getRequestHeaders(), resource.length());
    LOGGER.debug("Sending {} with mime type {} to {}", resource, mimeType, renderer);
    InputStream in = resource.getInputStream(range, root.getDefaultRenderer());
    if (range.getEnd() == 0) {
        // For web resources actual length may be unknown until we open the stream
        range.setEnd(resource.length());
    }
    Headers headers = httpExchange.getResponseHeaders();
    headers.add("Content-Type", mimeType);
    headers.add("Accept-Ranges", "bytes");
    long end = range.getEnd();
    long start = range.getStart();
    String rStr = start + "-" + end + "/*";
    headers.add("Content-Range", "bytes " + rStr);
    if (start != 0) {
        code = 206;
    }
    headers.add("Server", PMS.get().getServerName());
    headers.add("Connection", "keep-alive");
    httpExchange.sendResponseHeaders(code, 0);
    OutputStream os = httpExchange.getResponseBody();
    if (renderer != null) {
        renderer.start(resource);
    }
    if (sid != null) {
        resource.setMediaSubtitle(sid);
    }
    RemoteUtil.dump(in, os, renderer);
}
Also used : WebRender(net.pms.configuration.WebRender) Headers(com.sun.net.httpserver.Headers) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FFMpegVideo(net.pms.encoders.FFMpegVideo) IOException(java.io.IOException) FFmpegAudio(net.pms.encoders.FFmpegAudio) RendererConfiguration(net.pms.configuration.RendererConfiguration) FFmpegWebVideo(net.pms.encoders.FFmpegWebVideo)

Aggregations

Headers (com.sun.net.httpserver.Headers)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 RendererConfiguration (net.pms.configuration.RendererConfiguration)1 WebRender (net.pms.configuration.WebRender)1 FFMpegVideo (net.pms.encoders.FFMpegVideo)1 FFmpegAudio (net.pms.encoders.FFmpegAudio)1 FFmpegWebVideo (net.pms.encoders.FFmpegWebVideo)1