Skip to content

Commit

Permalink
Merge pull request #365 from Netflix/feature/VIDENG-1731-pass-sha1-to…
Browse files Browse the repository at this point in the history
…-imp-assembler

Allow users of IMPAssembler to supply pre-computed SHA-1 hashes for t…
  • Loading branch information
tcase-netflix authored Mar 13, 2024
2 parents d5e17f6 + b1f8aba commit beda248
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 5 deletions.
48 changes: 45 additions & 3 deletions src/main/java/com/netflix/imflibrary/writerTools/IMPAssembler.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public AssembledIMPResult assembleIMFFromFiles(SimpleTimeline simpleTimeline, Fi
Map<UUID, UUID> trackFileIdToResourceMap = new HashMap<>();
Map<UUID, List<Long>> sampleRateMap = new HashMap<>();
Map<UUID, BigInteger> sampleCountMap = new HashMap<>();
Map<UUID, byte[]> hashMap = new HashMap<>();


for (Track track : simpleTimeline.getTracks()) {
Expand All @@ -70,15 +71,30 @@ public AssembledIMPResult assembleIMFFromFiles(SimpleTimeline simpleTimeline, Fi
throw new IOException("Could not get header partition for file: " + trackEntry.getFile().getAbsolutePath());
}
byte[] headerPartitionBytes = headerPartitionPayloadRecord.getPayload();
byte[] hash = IMFUtils.generateSHA1Hash(resourceByteRangeProvider);


// get sha-1 hash or use cached value
byte[] hash = null;
if (trackEntry.getHash() != null) {
logger.info("Using hash from user: {}", trackEntry.getHash());
hash = trackEntry.getHash();
hashMap.put(IMPFixer.getTrackFileId(headerPartitionPayloadRecord), trackEntry.getHash());
} else if (hashMap.containsKey(IMPFixer.getTrackFileId(headerPartitionPayloadRecord))) {
logger.info("Using cached hash: {}", hashMap.get(IMPFixer.getTrackFileId(headerPartitionPayloadRecord)));
hash = hashMap.get(IMPFixer.getTrackFileId(headerPartitionPayloadRecord));
} else {
logger.info("Generating hash for file: {}", trackEntry.getFile().getAbsolutePath());
hash = IMFUtils.generateSHA1Hash(resourceByteRangeProvider);
hashMap.put(IMPFixer.getTrackFileId(headerPartitionPayloadRecord), hash);
}

UUID trackFileId = IMPFixer.getTrackFileId(headerPartitionPayloadRecord);
logger.info("UUID read from file: {}: {}", trackEntry.getFile().getName(), trackFileId.toString());
logger.info("Adding file {} to imfTrackFileMetadataMap", trackEntry.getFile().getName());
imfTrackFileMetadataMap.put(
trackFileId,
new IMPBuilder.IMFTrackFileMetadata(headerPartitionBytes,
hash,
hash, // a byte[] containing the SHA-1, Base64 encoded hash of the IMFTrack file
CompositionPlaylistBuilder_2016.defaultHashAlgorithm,
trackEntry.getFile().getName(),
resourceByteRangeProvider.getResourceSize())
Expand Down Expand Up @@ -387,14 +403,30 @@ public static class TrackEntry {
* @param entryPoint - the entry point, if null, defaults to 0
* @param duration - the duration, if null, defaults to intrinsic duration
* @param repeatCount - the repeat count, if null, defaults to 1
* @param hash - the SHA-1 hash of the file, optional, introspected if null
*/
public TrackEntry(@Nonnull File file, @Nullable Composition.EditRate sampleRate, @Nullable BigInteger intrinsicDuration, @Nullable BigInteger entryPoint, @Nullable BigInteger duration, @Nullable BigInteger repeatCount) {
public TrackEntry(@Nonnull File file, @Nullable Composition.EditRate sampleRate, @Nullable BigInteger intrinsicDuration, @Nullable BigInteger entryPoint, @Nullable BigInteger duration, @Nullable BigInteger repeatCount, @Nullable byte[] hash) {
this.file = file;
this.sampleRate = sampleRate;
this.intrinsicDuration = intrinsicDuration;
this.entryPoint = entryPoint;
this.duration = duration;
this.repeatCount = repeatCount;
this.hash = hash;
}

public TrackEntry(@Nonnull File file, @Nullable Composition.EditRate sampleRate, @Nullable BigInteger intrinsicDuration, @Nullable BigInteger entryPoint, @Nullable BigInteger duration, @Nullable BigInteger repeatCount) {
this(file, sampleRate, intrinsicDuration, entryPoint, duration, repeatCount, null);
}

public TrackEntry() {
this.file = null;
this.sampleRate = null;
this.intrinsicDuration = null;
this.entryPoint = null;
this.duration = null;
this.repeatCount = null;
this.hash = null;
}

public File getFile() {
Expand Down Expand Up @@ -451,5 +483,15 @@ public void setRepeatCount(BigInteger repeatCount) {
public BigInteger entryPoint;
public BigInteger duration;
public BigInteger repeatCount;

public byte[] getHash() {
return hash;
}

public void setHash(byte[] hash) {
this.hash = hash;
}

public byte[] hash;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,17 @@ private Object[][] trackEntries() {
BigInteger.valueOf(10),
BigInteger.valueOf(0),
BigInteger.valueOf(10),
BigInteger.valueOf(1)
BigInteger.valueOf(1),
java.util.Base64.getDecoder().decode("fL7SnTeNskm71I4otXqr/T0D5LQ=")
),
new IMPAssembler.TrackEntry(
TestHelper.findResourceByPath("TestIMP/MERIDIAN_Netflix_Photon_161006/MERIDIAN_Netflix_Photon_161006_ENG-51_00.mxf"),
new Composition.EditRate(48000L, 1L),
BigInteger.valueOf(8008),
BigInteger.valueOf(0),
BigInteger.valueOf(8008),
BigInteger.valueOf(1)
BigInteger.valueOf(1),
java.util.Base64.getDecoder().decode("X6GxGHTavnlIRLZiD7hHe5/CUh4=")
)

},
Expand Down

0 comments on commit beda248

Please sign in to comment.