MBRSetResponseGenerator.java 9.11 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.scene7.is.util.SizeInt
 *  com.scene7.is.util.TextEncodingTypeEnum
 *  com.scene7.is.util.XMLUtil
 *  com.scene7.is.util.callbacks.Option
 *  com.scene7.is.util.text.coders.NetPathCoder
 *  org.jetbrains.annotations.NotNull
 */
package com.scene7.is.ps.provider.fvctx;

import com.scene7.is.ps.provider.IZoomException;
import com.scene7.is.ps.provider.Request;
import com.scene7.is.ps.provider.fvctx.MediaSetRequest;
import com.scene7.is.ps.provider.fvctx.MediaSetVideoEntry;
import com.scene7.is.util.SizeInt;
import com.scene7.is.util.TextEncodingTypeEnum;
import com.scene7.is.util.XMLUtil;
import com.scene7.is.util.callbacks.Option;
import com.scene7.is.util.text.coders.NetPathCoder;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import org.jetbrains.annotations.NotNull;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

public class MBRSetResponseGenerator {
    private static final String NEW_LINE = "\n";
    private static final String M3U8_EXTENSION = ".m3u8";
    private static final String M3U8_HEADER = "#EXTM3U";
    private static final String M3U8_PREFIX = "#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=";
    private static final String[] M3U8_SUFFIX_PRECEDENCE = new String[]{"mp4", "f4v"};
    private static final String F4M_EXTENSION = ".f4m";
    private static final String[] F4M_SUFFIX_PRECEDENCE = new String[]{"f4v", "mp4"};
    private static final String F4M_ELEMENT_MANIFEST = "manifest";
    private static final String F4M_ELEMENT_MEDIA = "media";
    private static final String F4M_ATTR_HREF = "href";
    private static final String F4M_ATTR_BITRATE = "bitrate";
    private static final String F4M_ATTR_XMLNS = "xmlns";
    private static final String F4M_ATTR_VALUE_XMLNS = "http://ns.adobe.com/f4m/2.0";
    private static final String F4M_ATTR_WIDTH = "width";
    private static final String F4M_ATTR_HEIGHT = "height";
    private static final String STREAMING_CONTEXT_ERROR = "STREAMING_CONTEXT_NOT_DEFINED";

    @NotNull
    static byte[] generateM3U8Response(@NotNull MediaSetRequest mediaSetRequest) throws IZoomException {
        StringBuilder builder = new StringBuilder();
        builder.append("#EXTM3U");
        List<MediaSetVideoEntry> videoEntries = MBRSetResponseGenerator.filterVideos(mediaSetRequest.getVideoEntries(), M3U8_SUFFIX_PRECEDENCE);
        for (MediaSetVideoEntry videoEntry : videoEntries) {
            if (!MBRSetResponseGenerator.isValidVideoEntry(videoEntry)) continue;
            MBRSetResponseGenerator.appendNewLines(builder, 2);
            builder.append("#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=");
            builder.append(((Long)videoEntry.getBitRate().get()).toString());
            MBRSetResponseGenerator.appendNewLines(builder, 2);
            builder.append(MBRSetResponseGenerator.addTrailing((String)videoEntry.getHttpAppleStreamingContext().getOrElse((Object)"STREAMING_CONTEXT_NOT_DEFINED"), '/'));
            builder.append(MBRSetResponseGenerator.processFilePath(videoEntry.getFilePath()));
            builder.append(".m3u8");
        }
        MBRSetResponseGenerator.appendNewLines(builder, 1);
        try {
            return builder.toString().getBytes(mediaSetRequest.getRequest().getResponseEncoding().toString());
        }
        catch (UnsupportedEncodingException e) {
            throw new IZoomException(IZoomException.XML_ENCODING_ERROR, mediaSetRequest.getRequest().getResponseEncoding().toString(), e);
        }
    }

    @NotNull
    static byte[] generateF4MResponse(@NotNull MediaSetRequest mediaSetRequest) throws IZoomException {
        Document document = XMLUtil.createDocument();
        Element root = document.createElement("manifest");
        root.setAttribute("xmlns", "http://ns.adobe.com/f4m/2.0");
        document.appendChild(root);
        List<MediaSetVideoEntry> videoEntries = MBRSetResponseGenerator.filterVideos(mediaSetRequest.getVideoEntries(), F4M_SUFFIX_PRECEDENCE);
        for (MediaSetVideoEntry videoEntry : videoEntries) {
            if (!MBRSetResponseGenerator.isValidVideoEntry(videoEntry)) continue;
            Element media = document.createElement("media");
            StringBuilder pathBuilder = new StringBuilder();
            pathBuilder.append(MBRSetResponseGenerator.addTrailing((String)videoEntry.getHttpFlashStreamingContext().getOrElse((Object)"STREAMING_CONTEXT_NOT_DEFINED"), '/'));
            pathBuilder.append(MBRSetResponseGenerator.processFilePath(videoEntry.getFilePath()));
            pathBuilder.append(".f4m");
            media.setAttribute("href", pathBuilder.toString());
            long bitRate = (Long)videoEntry.getBitRate().get() / 1000;
            media.setAttribute("bitrate", Long.toString(bitRate));
            if (videoEntry.getVideoSize().isDefined()) {
                SizeInt videoSize = (SizeInt)videoEntry.getVideoSize().get();
                media.setAttribute("width", Integer.toString(videoSize.getWidth()));
                media.setAttribute("height", Integer.toString(videoSize.getHeight()));
            }
            root.appendChild(media);
        }
        try {
            return XMLUtil.toXMLBytes((Document)document, (TextEncodingTypeEnum)mediaSetRequest.getRequest().getResponseEncoding(), (boolean)false);
        }
        catch (UnsupportedEncodingException e) {
            throw new IZoomException(IZoomException.XML_ENCODING_ERROR, mediaSetRequest.getRequest().getResponseEncoding().toString(), e);
        }
    }

    private static void appendNewLines(@NotNull StringBuilder builder, int numNewLines) {
        for (int i = 0; i < numNewLines; ++i) {
            builder.append("\n");
        }
    }

    @NotNull
    private static List<MediaSetVideoEntry> filterVideos(@NotNull List<MediaSetVideoEntry> videoEntries, @NotNull String[] suffices) {
        if (!videoEntries.isEmpty()) {
            for (String suffix : suffices) {
                List<MediaSetVideoEntry> filteredList = MBRSetResponseGenerator.filterVideos(videoEntries, suffix);
                if (filteredList.isEmpty()) continue;
                return filteredList;
            }
            return new ArrayList<MediaSetVideoEntry>();
        }
        return new ArrayList<MediaSetVideoEntry>();
    }

    @NotNull
    private static List<MediaSetVideoEntry> filterVideos(@NotNull List<MediaSetVideoEntry> videoEntries, @NotNull String suffix) {
        ArrayList<MediaSetVideoEntry> filteredList = new ArrayList<MediaSetVideoEntry>();
        for (MediaSetVideoEntry videoEntry : videoEntries) {
            if (!videoEntry.getFileSuffix().equalsIgnoreCase(suffix)) continue;
            filteredList.add(videoEntry);
        }
        return filteredList;
    }

    private static boolean isValidVideoEntry(@NotNull MediaSetVideoEntry videoEntry) {
        if (videoEntry.getFileSuffix().isEmpty()) {
            return false;
        }
        if (!videoEntry.getBitRate().isDefined()) {
            return false;
        }
        if (videoEntry.getFilePath().isEmpty()) {
            return false;
        }
        return true;
    }

    @NotNull
    private static String processFilePath(@NotNull String filePath) {
        String dotStripped = MBRSetResponseGenerator.stripLeading(filePath, '.');
        dotStripped = MBRSetResponseGenerator.stripLeading(dotStripped, '/');
        return NetPathCoder.netPathCoder().encode(dotStripped);
    }

    @NotNull
    private static String addLeading(@NotNull String s, char c) {
        if (s.isEmpty()) {
            return s;
        }
        if (s.charAt(0) != c) {
            return s + c;
        }
        return s;
    }

    @NotNull
    private static String addTrailing(@NotNull String s, char c) {
        if (s.isEmpty()) {
            return s;
        }
        if (s.charAt(s.length() - 1) != c) {
            return s + c;
        }
        return s;
    }

    @NotNull
    private static String stripLeading(@NotNull String s, char c) {
        if (s.length() < 2) {
            return s;
        }
        if (s.charAt(0) == c) {
            return s.substring(1);
        }
        return s;
    }

    @NotNull
    private static String stripTrailing(@NotNull String s, char c) {
        if (s.isEmpty()) {
            return s;
        }
        if (s.charAt(s.length() - 1) == c) {
            return s.substring(0, s.length() - 1);
        }
        return s;
    }

    @NotNull
    static String stripLeadingAndTrailing(@NotNull String s, char c) {
        String leadingStripped = MBRSetResponseGenerator.stripLeading(s, c);
        return MBRSetResponseGenerator.stripTrailing(leadingStripped, c);
    }

    public static boolean isExtensionSupported(@NotNull String ext) {
        if (ext.equalsIgnoreCase(".m3u8")) {
            return true;
        }
        if (ext.equalsIgnoreCase(".f4m")) {
            return true;
        }
        return false;
    }

    @NotNull
    public static String deriveQueryPathFromExt(@NotNull String ext) {
        if (ext.equalsIgnoreCase(".m3u8")) {
            return "req=mbrset&fmt=m3u8";
        }
        if (ext.equalsIgnoreCase(".f4m")) {
            return "req=mbrset&fmt=f4m";
        }
        return "";
    }
}