Search in sources :

Example 1 with SegmentInfo

use of com.att.aro.core.videoanalysis.impl.SegmentInfo in project VideoOptimzer by attdevsupport.

the class VideoEvent method createSegmentInfo.

private SegmentInfo createSegmentInfo(Manifest manifest, Segment segment) {
    SegmentInfo segmentInfo = new SegmentInfo();
    segmentInfo.setSize(segment.getSize());
    segmentInfo.setDuration(segment.getEndPlayTime() - segment.getStartPlayTime());
    segmentInfo.setSegmentID(segment.getSegmentIndex());
    segmentInfo.setBitrate(segmentInfo.getDuration() != 0 ? (segment.getSize() * 8 / segmentInfo.getDuration() / 1000) : 0);
    segmentInfo.setStartTime(segment.getStartPlayTime());
    segmentInfo.setContentType(manifest.getContentType());
    if (manifest.getContentType() == ContentType.VIDEO) {
        segmentInfo.setVideo(true);
    } else {
        segmentInfo.setVideo(false);
    }
    return segmentInfo;
}
Also used : SegmentInfo(com.att.aro.core.videoanalysis.impl.SegmentInfo)

Example 2 with SegmentInfo

use of com.att.aro.core.videoanalysis.impl.SegmentInfo in project VideoOptimzer by attdevsupport.

the class VideoStreamConstructor method locateChildManifestAndSegmentInfo.

/**
 * Locate ChildManifest using keys based on multiple storage strategies, depending on Manifest & Manifest usage
 *
 * @param request
 * @param timeStamp
 * @param manifestCollection
 * @return
 */
public ChildManifest locateChildManifestAndSegmentInfo(HttpRequestResponseInfo request, byte[] content, Double timeStamp, ManifestCollection manifestCollection) {
    childManifest = null;
    segmentInfo = null;
    for (UrlMatchDef urlMatchDef : manifestCollection.getManifest().getSegUrlMatchDef()) {
        String key0 = manifestBuilder.locateKey(urlMatchDef, request);
        String key = genKey(key0, request, urlMatchDef);
        Double timeKey = timeStamp;
        if ((childManifest = manifestCollection.getTimestampChildManifestMap().get(timeKey)) != null) {
            LOG.debug("childManifest :" + childManifest.dumpManifest(600));
            if ((segmentInfo = childManifest.getSegmentInfoTrie().get(key)) != null) {
                return childManifest;
            }
        }
        timeKey = request.getTimeStamp();
        do {
            if ((timeKey = manifestCollection.getTimestampChildManifestMap().lowerKey(timeKey)) != null) {
                childManifest = manifestCollection.getTimestampChildManifestMap().get(timeKey);
            }
        } while (timeKey != null && (segmentInfo = childManifest.getSegmentInfoTrie().get(key)) == null);
        if ((childManifest = manifestCollection.getSegmentChildManifestTrie().get(key0)) != null || (childManifest = manifestCollection.getChildManifest(key0)) != null) {
            boolean encoded = childManifest.getSegmentInfoTrie().size() != 0 && VideoType.DASH_ENCODEDSEGMENTLIST.equals(childManifest.getManifest().getVideoType()) ? true : false;
            String brKey = buildByteRangeKey(request, encoded);
            if (brKey != null) {
                // As part of standard dash implementation, we assume that the segment information exists in sidx box in the fragmented mp4 received in the response of the request.
                if (content != null) {
                    populateSegmentInformation(request, content, childManifest);
                }
                if ((segmentInfo = childManifest.getSegmentInfoTrie().get(brKey.toLowerCase())) == null) {
                    segmentInfo = childManifest.getSegmentInfoTrie().get(brKey.toUpperCase());
                }
            }
            if (segmentInfo == null) {
                if ((segmentInfo = childManifest.getSegmentInfoTrie().get(key)) == null) {
                    if (!encoded && brKey != null) {
                        Map.Entry<String, SegmentInfo> entry = childManifest.getSegmentInfoTrie().select(brKey);
                        if (entry.getKey().split("-")[1].equals(brKey.split("-")[1])) {
                            segmentInfo = entry.getValue();
                        }
                    } else {
                        segmentInfo = childManifest.getSegmentInfoTrie().get(key);
                    }
                }
            }
            LOG.debug("childManifest: " + childManifest.getUriName() + ", URL match def: " + urlMatchDef);
            if (segmentInfo != null) {
                break;
            }
        } else if (timeStamp != null) {
            childManifest = manifestCollection.getTimestampChildManifestMap().get(timeStamp);
            if (childManifest == null || !childManifest.getSegmentInfoTrie().keySet().parallelStream().filter(segmentUriName -> {
                return request.getObjUri().toString().contains(segmentUriName);
            }).findFirst().isPresent()) {
                childManifest = manifestCollection.getUriNameChildMap().entrySet().stream().filter(x -> {
                    return x.getValue().getSegmentInfoTrie().keySet().parallelStream().filter(segmentUriName -> {
                        return request.getObjUri().toString().contains(segmentUriName);
                    }).findFirst().isPresent();
                }).findFirst().map(x -> x.getValue()).orElseGet(() -> null);
            }
            if (childManifest == null) {
                LOG.debug("ChildManifest wasn't found for segment request: " + request.getObjUri() + ", URL match def: " + urlMatchDef);
                nullSegmentInfo();
                continue;
            }
            segmentInfo = null;
            if ((segmentInfo = childManifest.getSegmentInfoTrie().get(key)) == null) {
                childManifest.getSegmentInfoTrie().entrySet().stream().filter(segmentInfoEntry -> segmentInfoEntry.getKey().contains(key)).findFirst().map(segmentInfoEntry -> segmentInfo = segmentInfoEntry.getValue()).orElseGet(() -> nullSegmentInfo());
            }
            if (segmentInfo != null) {
                break;
            }
        }
    }
    return childManifest;
}
Also used : UrlMatchDef(com.att.aro.core.videoanalysis.pojo.UrlMatchDef) StringUtils(org.apache.commons.lang.StringUtils) VideoFormat(com.att.aro.core.videoanalysis.pojo.VideoFormat) SortedSet(java.util.SortedSet) IFileManager(com.att.aro.core.fileio.IFileManager) Autowired(org.springframework.beans.factory.annotation.Autowired) Random(java.util.Random) FFmpegRunner(com.att.aro.core.videoanalysis.videoframe.FFmpegRunner) Logger(org.apache.log4j.Logger) ManifestBuilderHLS(com.att.aro.core.videoanalysis.impl.ManifestBuilderHLS) ByteArrayInputStream(java.io.ByteArrayInputStream) Map(java.util.Map) IStringParse(com.att.aro.core.util.IStringParse) VideoType(com.att.aro.core.videoanalysis.pojo.VideoEvent.VideoType) Path(java.nio.file.Path) HttpDirection(com.att.aro.core.packetanalysis.pojo.HttpDirection) NonNull(lombok.NonNull) Box(org.mp4parser.Box) VideoStream(com.att.aro.core.videoanalysis.pojo.VideoStream) IsoFile(org.mp4parser.IsoFile) IHttpRequestResponseHelper(com.att.aro.core.packetanalysis.IHttpRequestResponseHelper) ChildManifest(com.att.aro.core.videoanalysis.pojo.ChildManifest) Pattern(java.util.regex.Pattern) ManifestType(com.att.aro.core.videoanalysis.pojo.Manifest.ManifestType) SortedMap(java.util.SortedMap) Getter(lombok.Getter) HashMap(java.util.HashMap) ManifestBuilderDASH(com.att.aro.core.videoanalysis.impl.ManifestBuilderDASH) HttpRequestResponseInfo(com.att.aro.core.packetanalysis.pojo.HttpRequestResponseInfo) LinkedHashMap(java.util.LinkedHashMap) Value(org.springframework.beans.factory.annotation.Value) ManifestCollection(com.att.aro.core.videoanalysis.pojo.ManifestCollection) Manifest(com.att.aro.core.videoanalysis.pojo.Manifest) StringParse(com.att.aro.core.util.StringParse) Files(java.nio.file.Files) Util(com.att.aro.core.util.Util) IOException(java.io.IOException) FileUtils(org.apache.commons.io.FileUtils) File(java.io.File) ManifestBuilder(com.att.aro.core.videoanalysis.impl.ManifestBuilder) VideoEvent(com.att.aro.core.videoanalysis.pojo.VideoEvent) StreamingVideoData(com.att.aro.core.videoanalysis.pojo.StreamingVideoData) IExternalProcessRunner(com.att.aro.core.commandline.IExternalProcessRunner) TreeMap(java.util.TreeMap) Paths(java.nio.file.Paths) UrlMatchDef(com.att.aro.core.videoanalysis.pojo.UrlMatchDef) CRC32(java.util.zip.CRC32) SegmentInfo(com.att.aro.core.videoanalysis.impl.SegmentInfo) LogManager(org.apache.log4j.LogManager) SegmentIndexBox(org.mp4parser.boxes.iso14496.part12.SegmentIndexBox) SegmentInfo(com.att.aro.core.videoanalysis.impl.SegmentInfo) Map(java.util.Map) SortedMap(java.util.SortedMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) TreeMap(java.util.TreeMap)

Example 3 with SegmentInfo

use of com.att.aro.core.videoanalysis.impl.SegmentInfo in project VideoOptimzer by attdevsupport.

the class VideoStreamConstructor method processSidxBox.

private void processSidxBox(byte[] content, ChildManifest childManifest) {
    LOG.info("Processing sidx information for URI: " + childManifest.getUriName());
    File tempFile = null;
    IsoFile isoFile = null;
    try {
        // Write content data (mp4 file) to a temporary file
        tempFile = File.createTempFile("temp" + RANDOM.nextInt(), ".mp4");
        FileUtils.writeByteArrayToFile(tempFile, content);
        // Parse mp4 file and process Segment Box
        isoFile = new IsoFile(tempFile);
        for (Box box : isoFile.getBoxes()) {
            if (box instanceof SegmentIndexBox) {
                SegmentIndexBox sidxBox = (SegmentIndexBox) box;
                int lastSegmentIndex = -1;
                // Update last segment index to last entry in the segment list for the corresponding child manifest's segments
                if (!childManifest.getSegmentInfoTrie().isEmpty()) {
                    lastSegmentIndex = Integer.valueOf(childManifest.getSegmentInfoTrie().lastKey().split("-")[1]);
                }
                double timePos = 0;
                int idx = childManifest.getSegmentInfoTrie().size();
                Map<String, Double> segmentInformationMap = segmentInformationByFile.get(childManifest);
                // Iterate through Segment Index Box entries
                for (SegmentIndexBox.Entry entry : sidxBox.getEntries()) {
                    String key = String.valueOf((lastSegmentIndex + 1)) + "-" + String.valueOf((lastSegmentIndex + entry.getReferencedSize()));
                    segmentInformationMap.put(key, (double) entry.getSubsegmentDuration() / sidxBox.getTimeScale());
                    // Add SegmentInfo to Child Manifest
                    SegmentInfo segmentInfo = new SegmentInfo();
                    segmentInfo.setDuration((double) entry.getSubsegmentDuration() / sidxBox.getTimeScale());
                    segmentInfo.setStartTime(timePos);
                    segmentInfo.setSegmentID(idx++);
                    segmentInfo.setContentType(childManifest.getContentType());
                    segmentInfo.setVideo(childManifest.isVideo());
                    segmentInfo.setSize(entry.getReferencedSize());
                    segmentInfo.setQuality(String.valueOf(childManifest.getQuality()));
                    childManifest.addSegment(key, segmentInfo);
                    timePos += segmentInfo.getDuration();
                    lastSegmentIndex += entry.getReferencedSize();
                }
                // Not expecting and will not process another Segment Index Box
                break;
            }
        }
    } catch (Exception e) {
        LOG.error("Something went wrong while reading sidx box", e);
    } finally {
        if (tempFile != null && !tempFile.delete()) {
            tempFile.deleteOnExit();
        }
        if (isoFile != null) {
            try {
                isoFile.close();
            } catch (IOException e) {
                LOG.warn("Unable to close iso file resource", e);
            }
        }
    }
}
Also used : IsoFile(org.mp4parser.IsoFile) Box(org.mp4parser.Box) SegmentIndexBox(org.mp4parser.boxes.iso14496.part12.SegmentIndexBox) IOException(java.io.IOException) IOException(java.io.IOException) SegmentIndexBox(org.mp4parser.boxes.iso14496.part12.SegmentIndexBox) SegmentInfo(com.att.aro.core.videoanalysis.impl.SegmentInfo) IsoFile(org.mp4parser.IsoFile) File(java.io.File)

Aggregations

SegmentInfo (com.att.aro.core.videoanalysis.impl.SegmentInfo)3 File (java.io.File)2 IOException (java.io.IOException)2 IExternalProcessRunner (com.att.aro.core.commandline.IExternalProcessRunner)1 IFileManager (com.att.aro.core.fileio.IFileManager)1 IHttpRequestResponseHelper (com.att.aro.core.packetanalysis.IHttpRequestResponseHelper)1 HttpDirection (com.att.aro.core.packetanalysis.pojo.HttpDirection)1 HttpRequestResponseInfo (com.att.aro.core.packetanalysis.pojo.HttpRequestResponseInfo)1 IStringParse (com.att.aro.core.util.IStringParse)1 StringParse (com.att.aro.core.util.StringParse)1 Util (com.att.aro.core.util.Util)1 ManifestBuilder (com.att.aro.core.videoanalysis.impl.ManifestBuilder)1 ManifestBuilderDASH (com.att.aro.core.videoanalysis.impl.ManifestBuilderDASH)1 ManifestBuilderHLS (com.att.aro.core.videoanalysis.impl.ManifestBuilderHLS)1 ChildManifest (com.att.aro.core.videoanalysis.pojo.ChildManifest)1 Manifest (com.att.aro.core.videoanalysis.pojo.Manifest)1 ManifestType (com.att.aro.core.videoanalysis.pojo.Manifest.ManifestType)1 ManifestCollection (com.att.aro.core.videoanalysis.pojo.ManifestCollection)1 StreamingVideoData (com.att.aro.core.videoanalysis.pojo.StreamingVideoData)1 UrlMatchDef (com.att.aro.core.videoanalysis.pojo.UrlMatchDef)1