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;
}
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;
}
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);
}
}
}
}
Aggregations