Search in sources :

Example 6 with WebRender

use of net.pms.configuration.WebRender in project UniversalMediaServer by UniversalMediaServer.

the class RemotePlayHandler method handle.

@Override
public void handle(HttpExchange t) throws IOException {
    if (RemoteUtil.deny(t)) {
        throw new IOException("Access denied");
    }
    String p = t.getRequestURI().getPath();
    if (p.contains("/play/")) {
        LOGGER.debug("got a play request " + t.getRequestURI());
        String id = RemoteUtil.getId("play/", t);
        String response = mkPage(id, t);
        // LOGGER.trace("play page " + response);
        RemoteUtil.respond(t, response, 200, "text/html");
    } else if (p.contains("/playerstatus/")) {
        String json = IOUtils.toString(t.getRequestBody(), "UTF-8");
        LOGGER.trace("got player status: " + json);
        RemoteUtil.respond(t, "", 200, "text/html");
        RootFolder root = parent.getRoot(RemoteUtil.userName(t), t);
        if (root == null) {
            LOGGER.debug("root not found");
            throw new IOException("Unknown root");
        }
        WebRender renderer = (WebRender) root.getDefaultRenderer();
        ((WebRender.WebPlayer) renderer.getPlayer()).setData(json);
    } else if (p.contains("/playlist/")) {
        String[] tmp = p.split("/");
        // sanity
        if (tmp.length < 3) {
            throw new IOException("Bad request");
        }
        String op = tmp[tmp.length - 2];
        String id = tmp[tmp.length - 1];
        DLNAResource r = PMS.getGlobalRepo().get(id);
        if (r != null) {
            RootFolder root = parent.getRoot(RemoteUtil.userName(t), t);
            if (root == null) {
                LOGGER.debug("root not found");
                throw new IOException("Unknown root");
            }
            WebRender renderer = (WebRender) root.getDefaultRenderer();
            if (op.equals("add")) {
                PMS.get().getDynamicPls().add(r);
                renderer.notify(renderer.OK, "Added '" + r.getDisplayName() + "' to dynamic playlist");
            } else if (op.equals("del") && (r.getParent() instanceof Playlist)) {
                ((Playlist) r.getParent()).remove(r);
                renderer.notify(renderer.INFO, "Removed '" + r.getDisplayName() + "' from playlist");
            }
        }
        RemoteUtil.respond(t, returnPage(), 200, "text/html");
    }
}
Also used : WebRender(net.pms.configuration.WebRender) DLNAResource(net.pms.dlna.DLNAResource) Playlist(net.pms.dlna.Playlist) RootFolder(net.pms.dlna.RootFolder) IOException(java.io.IOException)

Example 7 with WebRender

use of net.pms.configuration.WebRender in project UniversalMediaServer by UniversalMediaServer.

the class RemoteRawHandler method handle.

@Override
public void handle(HttpExchange t) throws IOException {
    LOGGER.debug("got a raw request " + t.getRequestURI());
    if (RemoteUtil.deny(t)) {
        throw new IOException("Access denied");
    }
    RootFolder root = parent.getRoot(RemoteUtil.userName(t), t);
    if (root == null) {
        throw new IOException("Unknown root");
    }
    String id;
    id = RemoteUtil.strip(RemoteUtil.getId("raw/", t));
    LOGGER.debug("raw id " + id);
    List<DLNAResource> res = root.getDLNAResources(id, false, 0, 0, root.getDefaultRenderer());
    if (res.size() != 1) {
        // another error
        LOGGER.debug("media unkonwn");
        throw new IOException("Bad id");
    }
    DLNAResource dlna = res.get(0);
    long len;
    String mime = null;
    InputStream in;
    Range.Byte range;
    if (dlna.getMedia() != null && dlna.getMedia().isImage() && dlna.getMedia().getImageInfo() != null) {
        boolean supported = false;
        ImageInfo imageInfo = dlna.getMedia().getImageInfo();
        if (root.getDefaultRenderer() instanceof WebRender) {
            WebRender renderer = (WebRender) root.getDefaultRenderer();
            supported = renderer.isImageFormatSupported(imageInfo.getFormat());
        }
        mime = dlna.getFormat() != null ? dlna.getFormat().mimeType() : root.getDefaultRenderer().getMimeType(dlna.mimeType(), dlna.getMedia());
        len = supported && imageInfo.getSize() != ImageInfo.SIZE_UNKNOWN ? imageInfo.getSize() : dlna.length();
        range = new Range.Byte(0l, len);
        if (supported) {
            in = dlna.getInputStream();
        } else {
            InputStream imageInputStream;
            if (dlna.getPlayer() instanceof ImagePlayer) {
                ProcessWrapper transcodeProcess = dlna.getPlayer().launchTranscode(dlna, dlna.getMedia(), new OutputParams(PMS.getConfiguration()));
                imageInputStream = transcodeProcess != null ? transcodeProcess.getInputStream(0) : null;
            } else {
                imageInputStream = dlna.getInputStream();
            }
            Image image = Image.toImage(imageInputStream, 3840, 2400, ScaleType.MAX, ImageFormat.JPEG, false);
            len = image.getBytes(false).length;
            in = image == null ? null : new ByteArrayInputStream(image.getBytes(false));
        }
    } else {
        len = dlna.length();
        dlna.setPlayer(null);
        range = RemoteUtil.parseRange(t.getRequestHeaders(), len);
        in = dlna.getInputStream(range, root.getDefaultRenderer());
        if (len == 0) {
            // For web resources actual length may be unknown until we open the stream
            len = dlna.length();
        }
        mime = root.getDefaultRenderer().getMimeType(dlna.mimeType(), dlna.getMedia());
    }
    Headers hdr = t.getResponseHeaders();
    LOGGER.debug("Sending media \"{}\" with mime type \"{}\"", dlna, mime);
    hdr.add("Content-Type", mime);
    hdr.add("Accept-Ranges", "bytes");
    hdr.add("Server", PMS.get().getServerName());
    hdr.add("Connection", "keep-alive");
    hdr.add("Transfer-Encoding", "chunked");
    if (in.available() != len) {
        hdr.add("Content-Range", "bytes " + range.getStart() + "-" + in.available() + "/" + len);
        t.sendResponseHeaders(206, in.available());
    } else {
        t.sendResponseHeaders(200, 0);
    }
    OutputStream os = new BufferedOutputStream(t.getResponseBody(), 512 * 1024);
    LOGGER.debug("start raw dump");
    RemoteUtil.dump(in, os);
}
Also used : ImagePlayer(net.pms.encoders.ImagePlayer) WebRender(net.pms.configuration.WebRender) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Headers(com.sun.net.httpserver.Headers) OutputStream(java.io.OutputStream) BufferedOutputStream(java.io.BufferedOutputStream) IOException(java.io.IOException) Range(net.pms.dlna.Range) Image(net.pms.image.Image) DLNAResource(net.pms.dlna.DLNAResource) ProcessWrapper(net.pms.io.ProcessWrapper) ByteArrayInputStream(java.io.ByteArrayInputStream) RootFolder(net.pms.dlna.RootFolder) ImageInfo(net.pms.image.ImageInfo) OutputParams(net.pms.io.OutputParams) BufferedOutputStream(java.io.BufferedOutputStream)

Aggregations

WebRender (net.pms.configuration.WebRender)7 IOException (java.io.IOException)4 RootFolder (net.pms.dlna.RootFolder)4 RendererConfiguration (net.pms.configuration.RendererConfiguration)3 DLNAResource (net.pms.dlna.DLNAResource)3 Headers (com.sun.net.httpserver.Headers)2 InputStream (java.io.InputStream)2 OutputStream (java.io.OutputStream)2 HashMap (java.util.HashMap)2 Playlist (net.pms.dlna.Playlist)2 OutputParams (net.pms.io.OutputParams)2 BufferedOutputStream (java.io.BufferedOutputStream)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 Range (net.pms.dlna.Range)1 VirtualVideoAction (net.pms.dlna.virtual.VirtualVideoAction)1 FFMpegVideo (net.pms.encoders.FFMpegVideo)1 FFmpegAudio (net.pms.encoders.FFmpegAudio)1