Search in sources :

Example 1 with FFmpegAudio

use of net.pms.encoders.FFmpegAudio 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)

Example 2 with FFmpegAudio

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

the class CueFolder method resolveOnce.

@Override
protected void resolveOnce() {
    if (playlistfile.length() < 10000000) {
        CueSheet sheet;
        try {
            sheet = CueParser.parse(playlistfile);
        } catch (IOException e) {
            LOGGER.info("Error in parsing cue: " + e.getMessage());
            return;
        }
        if (sheet != null) {
            List<FileData> files = sheet.getFileData();
            // only the first one
            if (files.size() > 0) {
                FileData f = files.get(0);
                List<TrackData> tracks = f.getTrackData();
                Player defaultPlayer = null;
                DLNAMediaInfo originalMedia = null;
                ArrayList<DLNAResource> addedResources = new ArrayList<>();
                for (int i = 0; i < tracks.size(); i++) {
                    TrackData track = tracks.get(i);
                    if (i > 0) {
                        double end = getTime(track.getIndices().get(0).getPosition());
                        if (addedResources.isEmpty()) {
                            // seems the first file was invalid or non existent
                            return;
                        }
                        DLNAResource prec = addedResources.get(i - 1);
                        int count = 0;
                        while (prec.isFolder() && i + count < addedResources.size()) {
                            // not used anymore
                            prec = addedResources.get(i + count);
                            count++;
                        }
                        prec.getSplitRange().setEnd(end);
                        prec.getMedia().setDuration(prec.getSplitRange().getDuration());
                        LOGGER.debug("Track #" + i + " split range: " + prec.getSplitRange().getStartOrZero() + " - " + prec.getSplitRange().getDuration());
                    }
                    Position start = track.getIndices().get(0).getPosition();
                    RealFile realFile = new RealFile(new File(playlistfile.getParentFile(), f.getFile()));
                    addChild(realFile);
                    addedResources.add(realFile);
                    if (i > 0 && realFile.getMedia() == null) {
                        realFile.setMedia(new DLNAMediaInfo());
                        realFile.getMedia().setMediaparsed(true);
                    }
                    realFile.resolve();
                    if (i == 0) {
                        originalMedia = realFile.getMedia();
                        if (originalMedia == null) {
                            LOGGER.trace("Couldn't resolve media \"{}\" for cue file \"{}\" - aborting", realFile.getName(), playlistfile.getAbsolutePath());
                            return;
                        }
                    }
                    realFile.getSplitRange().setStart(getTime(start));
                    realFile.setSplitTrack(i + 1);
                    // Assign a splitter engine if file is natively supported by renderer
                    if (realFile.getPlayer() == null) {
                        if (defaultPlayer == null) {
                            /*
									XXX why are we creating new player instances? aren't they
									supposed to be singletons?

									TODO don't hardwire the player here; leave it to the
									player factory to select the right player for the
									resource's format e.g:

										defaultPlayer = PlayerFactory.getPlayer(realFile);
								*/
                            if (realFile.getFormat() == null) {
                                LOGGER.error("No file format known for file \"{}\", assuming it is a video for now.", realFile.getName());
                                /*
										TODO (see above):

											r.resolveFormat(); // sets the format based on the filename
											defaultPlayer = PlayerFactory.getPlayer(realFile);
									*/
                                defaultPlayer = new MEncoderVideo();
                            } else {
                                if (realFile.getFormat().isAudio()) {
                                    defaultPlayer = new FFmpegAudio();
                                } else {
                                    defaultPlayer = new MEncoderVideo();
                                }
                            }
                        }
                        realFile.setPlayer(defaultPlayer);
                    }
                    if (realFile.getMedia() != null) {
                        try {
                            realFile.setMedia((DLNAMediaInfo) originalMedia.clone());
                        } catch (CloneNotSupportedException e) {
                            LOGGER.info("Error in cloning media info: " + e.getMessage());
                        }
                        if (realFile.getMedia() != null && realFile.getMedia().getFirstAudioTrack() != null) {
                            if (realFile.getFormat().isAudio()) {
                                realFile.getMedia().getFirstAudioTrack().setSongname(track.getTitle());
                            } else {
                                realFile.getMedia().getFirstAudioTrack().setSongname("Chapter #" + (i + 1));
                            }
                            realFile.getMedia().getFirstAudioTrack().setTrack(i + 1);
                            realFile.getMedia().setSize(-1);
                            if (StringUtils.isNotBlank(sheet.getTitle())) {
                                realFile.getMedia().getFirstAudioTrack().setAlbum(sheet.getTitle());
                            }
                            if (StringUtils.isNotBlank(sheet.getPerformer())) {
                                realFile.getMedia().getFirstAudioTrack().setArtist(sheet.getPerformer());
                            }
                            if (StringUtils.isNotBlank(track.getPerformer())) {
                                realFile.getMedia().getFirstAudioTrack().setArtist(track.getPerformer());
                            }
                        }
                    }
                }
                if (tracks.size() > 0 && addedResources.size() > 0) {
                    DLNAResource lastTrack = addedResources.get(addedResources.size() - 1);
                    Time lastTrackSplitRange = lastTrack.getSplitRange();
                    DLNAMediaInfo lastTrackMedia = lastTrack.getMedia();
                    if (lastTrackSplitRange != null && lastTrackMedia != null) {
                        lastTrackSplitRange.setEnd(lastTrackMedia.getDurationInSeconds());
                        lastTrackMedia.setDuration(lastTrackSplitRange.getDuration());
                        LOGGER.debug("Track #" + childrenNumber() + " split range: " + lastTrackSplitRange.getStartOrZero() + " - " + lastTrackSplitRange.getDuration());
                    }
                }
                PMS.get().storeFileInCache(playlistfile, Format.PLAYLIST);
            }
        }
    }
}
Also used : Player(net.pms.encoders.Player) ArrayList(java.util.ArrayList) Time(net.pms.dlna.Range.Time) IOException(java.io.IOException) MEncoderVideo(net.pms.encoders.MEncoderVideo) FFmpegAudio(net.pms.encoders.FFmpegAudio) File(java.io.File)

Aggregations

IOException (java.io.IOException)2 FFmpegAudio (net.pms.encoders.FFmpegAudio)2 Headers (com.sun.net.httpserver.Headers)1 File (java.io.File)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 ArrayList (java.util.ArrayList)1 RendererConfiguration (net.pms.configuration.RendererConfiguration)1 WebRender (net.pms.configuration.WebRender)1 Time (net.pms.dlna.Range.Time)1 FFMpegVideo (net.pms.encoders.FFMpegVideo)1 FFmpegWebVideo (net.pms.encoders.FFmpegWebVideo)1 MEncoderVideo (net.pms.encoders.MEncoderVideo)1 Player (net.pms.encoders.Player)1