Search in sources :

Example 11 with OutputParams

use of net.pms.io.OutputParams in project UniversalMediaServer by UniversalMediaServer.

the class DLNAMediaInfo method getFFmpegThumbnail.

private ProcessWrapperImpl getFFmpegThumbnail(InputFile media, boolean resume, RendererConfiguration renderer) {
    /**
     * Note: The text output from FFmpeg is used by renderers that do
     * not use MediaInfo, so do not make any changes that remove or
     * minimize the amount of text given by FFmpeg here
     */
    String[] args = new String[14];
    args[0] = getFfmpegPath();
    File file = media.getFile();
    boolean dvrms = file != null && file.getAbsolutePath().toLowerCase().endsWith("dvr-ms");
    if (dvrms && isNotBlank(configuration.getFfmpegAlternativePath())) {
        args[0] = configuration.getFfmpegAlternativePath();
    }
    args[1] = "-ss";
    if (resume) {
        args[2] = Integer.toString((int) getDurationInSeconds());
    } else {
        args[2] = Integer.toString((int) Math.min(configuration.getThumbnailSeekPos(), getDurationInSeconds()));
    }
    args[3] = "-i";
    if (file != null) {
        args[4] = ProcessUtil.getShortFileNameIfWideChars(file.getAbsolutePath());
    } else {
        args[4] = "-";
    }
    args[5] = "-an";
    args[6] = "-an";
    // Thumbnail resolution
    int thumbnailWidth = 320;
    int thumbnailHeight = 180;
    double thumbnailRatio = 1.78;
    boolean isThumbnailPadding = true;
    if (renderer != null) {
        thumbnailWidth = renderer.getThumbnailWidth();
        thumbnailHeight = renderer.getThumbnailHeight();
        thumbnailRatio = renderer.getThumbnailRatio();
        isThumbnailPadding = renderer.isThumbnailPadding();
    }
    if (isThumbnailPadding) {
        args[7] = "-vf";
        args[8] = "scale='if(gt(a," + thumbnailRatio + ")," + thumbnailWidth + ",-1)':'if(gt(a," + thumbnailRatio + "),-1," + thumbnailHeight + ")', pad=" + thumbnailWidth + ":" + thumbnailHeight + ":(" + thumbnailWidth + "-iw)/2:(" + thumbnailHeight + "-ih)/2";
    } else {
        args[7] = "-vf";
        args[8] = "scale='if(gt(a," + thumbnailRatio + ")," + thumbnailWidth + ",-1)':'if(gt(a," + thumbnailRatio + "),-1," + thumbnailHeight + ")'";
    }
    args[9] = "-vframes";
    args[10] = "1";
    args[11] = "-f";
    args[12] = "image2";
    args[13] = "pipe:";
    // FIXME MPlayer should not be used if thumbnail generation is disabled
    if (!configuration.isThumbnailGenerationEnabled() || renderer != null && !renderer.isThumbnails() || configuration.isUseMplayerForVideoThumbs() && !dvrms) {
        args[2] = "0";
        for (int i = 5; i <= 13; i++) {
            args[i] = "-an";
        }
    }
    OutputParams params = new OutputParams(configuration);
    params.maxBufferSize = 1;
    params.stdin = media.getPush();
    // not serious if anything happens during the thumbnailer
    params.noexitcheck = true;
    // true: consume stderr on behalf of the caller i.e. parse()
    final ProcessWrapperImpl pw = new ProcessWrapperImpl(args, true, params, false, true);
    // FAILSAFE
    synchronized (parsingLock) {
        parsing = true;
    }
    Runnable r = new Runnable() {

        @Override
        public void run() {
            try {
                Thread.sleep(10000);
                synchronized (ffmpeg_failureLock) {
                    ffmpeg_failure = true;
                }
            } catch (InterruptedException e) {
            }
            pw.stopProcess();
            synchronized (parsingLock) {
                parsing = false;
            }
        }
    };
    Thread failsafe = new Thread(r, "FFmpeg Thumbnail Failsafe");
    failsafe.start();
    pw.runInSameThread();
    synchronized (parsingLock) {
        parsing = false;
    }
    return pw;
}
Also used : OutputParams(net.pms.io.OutputParams) AudioFile(org.jaudiotagger.audio.AudioFile) ProcessWrapperImpl(net.pms.io.ProcessWrapperImpl)

Example 12 with OutputParams

use of net.pms.io.OutputParams in project UniversalMediaServer by UniversalMediaServer.

the class DLNAMediaInfo method getAnnexBFrameHeader.

public byte[][] getAnnexBFrameHeader(InputFile f) {
    String[] cmdArray = new String[14];
    cmdArray[0] = configuration.getFfmpegPath();
    cmdArray[1] = "-i";
    if (f.getPush() == null && f.getFilename() != null) {
        cmdArray[2] = f.getFilename();
    } else {
        cmdArray[2] = "-";
    }
    cmdArray[3] = "-vframes";
    cmdArray[4] = "1";
    cmdArray[5] = "-c:v";
    cmdArray[6] = "copy";
    cmdArray[7] = "-f";
    cmdArray[8] = "h264";
    cmdArray[9] = "-bsf";
    cmdArray[10] = "h264_mp4toannexb";
    cmdArray[11] = "-an";
    cmdArray[12] = "-y";
    cmdArray[13] = "pipe:";
    byte[][] returnData = new byte[2][];
    OutputParams params = new OutputParams(configuration);
    params.maxBufferSize = 1;
    params.stdin = f.getPush();
    final ProcessWrapperImpl pw = new ProcessWrapperImpl(cmdArray, true, params);
    Runnable r = new Runnable() {

        @Override
        public void run() {
            try {
                Thread.sleep(3000);
                synchronized (ffmpeg_annexb_failureLock) {
                    ffmpeg_annexb_failure = true;
                }
            } catch (InterruptedException e) {
            }
            pw.stopProcess();
        }
    };
    Thread failsafe = new Thread(r, "FFMpeg AnnexB Frame Header Failsafe");
    failsafe.start();
    pw.runInSameThread();
    synchronized (ffmpeg_annexb_failureLock) {
        if (ffmpeg_annexb_failure) {
            return null;
        }
    }
    byte[] data = pw.getOutputByteArray().toByteArray();
    returnData[0] = data;
    int kf = 0;
    for (int i = 3; i < data.length; i++) {
        if (data[i - 3] == 1 && (data[i - 2] & 37) == 37 && (data[i - 1] & -120) == -120) {
            kf = i - 2;
            break;
        }
    }
    int st = 0;
    boolean found = false;
    if (kf > 0) {
        for (int i = kf; i >= 5; i--) {
            if (data[i - 5] == 0 && data[i - 4] == 0 && data[i - 3] == 0 && (data[i - 2] & 1) == 1 && (data[i - 1] & 39) == 39) {
                st = i - 5;
                found = true;
                break;
            }
        }
    }
    if (found) {
        byte[] header = new byte[kf - st];
        System.arraycopy(data, st, header, 0, kf - st);
        returnData[1] = header;
    }
    return returnData;
}
Also used : OutputParams(net.pms.io.OutputParams) ProcessWrapperImpl(net.pms.io.ProcessWrapperImpl)

Example 13 with OutputParams

use of net.pms.io.OutputParams in project UniversalMediaServer by UniversalMediaServer.

the class DCRaw method getImage.

/**
 * Converts {@code fileName} into PPM format.
 *
 * @param params the {@link OutputParams} to use. Can be {@code null}.
 * @param fileName the path of the image file to process.
 * @param imageInfo the {@link ImageInfo} for the image file. Can be {@code null}.
 * @return A byte array containing the converted image or {@code null}.
 * @throws IOException if an IO error occurs.
 */
@Override
public byte[] getImage(OutputParams params, String fileName, ImageInfo imageInfo) {
    if (LOGGER.isTraceEnabled()) {
        LOGGER.trace("Decoding image \"{}\" with DCRaw", fileName);
    }
    if (params == null) {
        params = new OutputParams(PMS.getConfiguration());
    }
    // Use device-specific pms conf
    PmsConfiguration configuration = PMS.getConfiguration(params);
    params.log = false;
    // Setting the buffer to the size of the source file or 5 MB. The
    // output won't be the same size as the input, but it will hopefully
    // give us a somewhat relevant buffer size. Every time the buffer has
    // to grow, the whole buffer must be copied in memory.
    params.outputByteArrayStreamBufferSize = imageInfo != null && imageInfo.getSize() != ImageInfo.SIZE_UNKNOWN ? (int) imageInfo.getSize() : 5000000;
    // First try to get the embedded thumbnail
    String[] cmdArray = new String[5];
    cmdArray[0] = configuration.getDCRawPath();
    cmdArray[1] = "-c";
    cmdArray[2] = "-M";
    cmdArray[3] = "-w";
    cmdArray[4] = fileName;
    ProcessWrapperImpl pw = new ProcessWrapperImpl(cmdArray, true, params, false, true);
    pw.runInSameThread();
    byte[] bytes = pw.getOutputByteArray().toByteArray();
    List<String> results = pw.getResults();
    if (bytes == null || bytes.length == 0) {
        if (!results.isEmpty() && results.get(0).startsWith("Cannot decode file")) {
            LOGGER.warn("DCRaw could not decode image \"{}\"", fileName);
        } else if (!results.isEmpty()) {
            LOGGER.debug("DCRaw failed to decode image \"{}\": {}", fileName, StringUtils.join(results, "\n"));
        }
        return null;
    }
    return bytes;
}
Also used : PmsConfiguration(net.pms.configuration.PmsConfiguration) OutputParams(net.pms.io.OutputParams) ProcessWrapperImpl(net.pms.io.ProcessWrapperImpl)

Example 14 with OutputParams

use of net.pms.io.OutputParams 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)

Example 15 with OutputParams

use of net.pms.io.OutputParams in project UniversalMediaServer by UniversalMediaServer.

the class WindowsUtil method installWin32Service.

/**
 * Executes the needed commands in order to install the Windows service
 * that starts whenever the machine is started.
 * This function is called from the General tab.
 *
 * @return true if UMS could be installed as a Windows service.
 * @see net.pms.newgui.GeneralTab#build()
 */
public static boolean installWin32Service() {
    String[] cmdArray = new String[] { "win32/service/wrapper.exe", "-i", "wrapper.conf" };
    ProcessWrapperImpl pwinstall = new ProcessWrapperImpl(cmdArray, true, new OutputParams(PMS.getConfiguration()));
    pwinstall.runInSameThread();
    return pwinstall.isSuccess();
}
Also used : OutputParams(net.pms.io.OutputParams) ProcessWrapperImpl(net.pms.io.ProcessWrapperImpl)

Aggregations

OutputParams (net.pms.io.OutputParams)17 ProcessWrapperImpl (net.pms.io.ProcessWrapperImpl)10 IOException (java.io.IOException)5 PmsConfiguration (net.pms.configuration.PmsConfiguration)5 ProcessWrapper (net.pms.io.ProcessWrapper)4 InputStream (java.io.InputStream)3 Matcher (java.util.regex.Matcher)3 DLNAResource (net.pms.dlna.DLNAResource)3 ImagePlayer (net.pms.encoders.ImagePlayer)3 Format (net.pms.formats.Format)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 File (java.io.File)2 FileInputStream (java.io.FileInputStream)2 OutputStream (java.io.OutputStream)2 MalformedURLException (java.net.MalformedURLException)2 Socket (java.net.Socket)2 URL (java.net.URL)2 SimpleDateFormat (java.text.SimpleDateFormat)2 ArrayList (java.util.ArrayList)2 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)2