Commit cc2a8bb8 by aquilescanta Committed by Oliver Woodman

Fix encryption data extraction when default values should be used

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=206963956
parent d458b90c
...@@ -731,7 +731,7 @@ public final class FragmentedMp4Extractor implements Extractor { ...@@ -731,7 +731,7 @@ public final class FragmentedMp4Extractor implements Extractor {
private static void parseSaiz(TrackEncryptionBox encryptionBox, ParsableByteArray saiz, private static void parseSaiz(TrackEncryptionBox encryptionBox, ParsableByteArray saiz,
TrackFragment out) throws ParserException { TrackFragment out) throws ParserException {
int vectorSize = encryptionBox.initializationVectorSize; int vectorSize = encryptionBox.perSampleIvSize;
saiz.setPosition(Atom.HEADER_SIZE); saiz.setPosition(Atom.HEADER_SIZE);
int fullAtom = saiz.readInt(); int fullAtom = saiz.readInt();
int flags = Atom.parseFullAtomFlags(fullAtom); int flags = Atom.parseFullAtomFlags(fullAtom);
...@@ -1266,11 +1266,9 @@ public final class FragmentedMp4Extractor implements Extractor { ...@@ -1266,11 +1266,9 @@ public final class FragmentedMp4Extractor implements Extractor {
// Encryption data. // Encryption data.
TrackOutput.CryptoData cryptoData = null; TrackOutput.CryptoData cryptoData = null;
if (fragment.definesEncryptionData) { TrackEncryptionBox encryptionBox = currentTrackBundle.getEncryptionBoxIfEncrypted();
if (encryptionBox != null) {
sampleFlags |= C.BUFFER_FLAG_ENCRYPTED; sampleFlags |= C.BUFFER_FLAG_ENCRYPTED;
TrackEncryptionBox encryptionBox = fragment.trackEncryptionBox != null
? fragment.trackEncryptionBox
: track.getSampleDescriptionEncryptionBox(fragment.header.sampleDescriptionIndex);
cryptoData = encryptionBox.cryptoData; cryptoData = encryptionBox.cryptoData;
} }
...@@ -1467,16 +1465,16 @@ public final class FragmentedMp4Extractor implements Extractor { ...@@ -1467,16 +1465,16 @@ public final class FragmentedMp4Extractor implements Extractor {
* @return The number of written bytes. * @return The number of written bytes.
*/ */
public int outputSampleEncryptionData() { public int outputSampleEncryptionData() {
if (!fragment.definesEncryptionData) { TrackEncryptionBox encryptionBox = getEncryptionBoxIfEncrypted();
if (encryptionBox == null) {
return 0; return 0;
} }
TrackEncryptionBox encryptionBox = getEncryptionBox();
ParsableByteArray initializationVectorData; ParsableByteArray initializationVectorData;
int vectorSize; int vectorSize;
if (encryptionBox.initializationVectorSize != 0) { if (encryptionBox.perSampleIvSize != 0) {
initializationVectorData = fragment.sampleEncryptionData; initializationVectorData = fragment.sampleEncryptionData;
vectorSize = encryptionBox.initializationVectorSize; vectorSize = encryptionBox.perSampleIvSize;
} else { } else {
// The default initialization vector should be used. // The default initialization vector should be used.
byte[] initVectorData = encryptionBox.defaultInitializationVector; byte[] initVectorData = encryptionBox.defaultInitializationVector;
...@@ -1485,7 +1483,7 @@ public final class FragmentedMp4Extractor implements Extractor { ...@@ -1485,7 +1483,7 @@ public final class FragmentedMp4Extractor implements Extractor {
vectorSize = initVectorData.length; vectorSize = initVectorData.length;
} }
boolean subsampleEncryption = fragment.sampleHasSubsampleEncryptionTable[currentSampleIndex]; boolean subsampleEncryption = fragment.sampleHasSubsampleEncryptionTable(currentSampleIndex);
// Write the signal byte, containing the vector size and the subsample encryption flag. // Write the signal byte, containing the vector size and the subsample encryption flag.
encryptionSignalByte.data[0] = (byte) (vectorSize | (subsampleEncryption ? 0x80 : 0)); encryptionSignalByte.data[0] = (byte) (vectorSize | (subsampleEncryption ? 0x80 : 0));
...@@ -1508,25 +1506,27 @@ public final class FragmentedMp4Extractor implements Extractor { ...@@ -1508,25 +1506,27 @@ public final class FragmentedMp4Extractor implements Extractor {
/** Skips the encryption data for the current sample. */ /** Skips the encryption data for the current sample. */
private void skipSampleEncryptionData() { private void skipSampleEncryptionData() {
if (!fragment.definesEncryptionData) { TrackEncryptionBox encryptionBox = getEncryptionBoxIfEncrypted();
if (encryptionBox == null) {
return; return;
} }
ParsableByteArray sampleEncryptionData = fragment.sampleEncryptionData; ParsableByteArray sampleEncryptionData = fragment.sampleEncryptionData;
TrackEncryptionBox encryptionBox = getEncryptionBox(); if (encryptionBox.perSampleIvSize != 0) {
if (encryptionBox.initializationVectorSize != 0) { sampleEncryptionData.skipBytes(encryptionBox.perSampleIvSize);
sampleEncryptionData.skipBytes(encryptionBox.initializationVectorSize);
} }
if (fragment.sampleHasSubsampleEncryptionTable[currentSampleIndex]) { if (fragment.sampleHasSubsampleEncryptionTable(currentSampleIndex)) {
sampleEncryptionData.skipBytes(6 * sampleEncryptionData.readUnsignedShort()); sampleEncryptionData.skipBytes(6 * sampleEncryptionData.readUnsignedShort());
} }
} }
private TrackEncryptionBox getEncryptionBox() { private TrackEncryptionBox getEncryptionBoxIfEncrypted() {
int sampleDescriptionIndex = fragment.header.sampleDescriptionIndex; int sampleDescriptionIndex = fragment.header.sampleDescriptionIndex;
return fragment.trackEncryptionBox != null TrackEncryptionBox encryptionBox =
? fragment.trackEncryptionBox fragment.trackEncryptionBox != null
: track.getSampleDescriptionEncryptionBox(sampleDescriptionIndex); ? fragment.trackEncryptionBox
: track.getSampleDescriptionEncryptionBox(sampleDescriptionIndex);
return encryptionBox != null && encryptionBox.isEncrypted ? encryptionBox : null;
} }
} }
......
...@@ -45,33 +45,36 @@ public final class TrackEncryptionBox { ...@@ -45,33 +45,36 @@ public final class TrackEncryptionBox {
*/ */
public final TrackOutput.CryptoData cryptoData; public final TrackOutput.CryptoData cryptoData;
/** /** The initialization vector size in bytes for the samples in the corresponding sample group. */
* The initialization vector size in bytes for the samples in the corresponding sample group. public final int perSampleIvSize;
*/
public final int initializationVectorSize;
/** /**
* If {@link #initializationVectorSize} is 0, holds the default initialization vector as defined * If {@link #perSampleIvSize} is 0, holds the default initialization vector as defined in the
* in the track encryption box or sample group description box. Null otherwise. * track encryption box or sample group description box. Null otherwise.
*/ */
public final byte[] defaultInitializationVector; public final byte[] defaultInitializationVector;
/** /**
* @param isEncrypted See {@link #isEncrypted}. * @param isEncrypted See {@link #isEncrypted}.
* @param schemeType See {@link #schemeType}. * @param schemeType See {@link #schemeType}.
* @param initializationVectorSize See {@link #initializationVectorSize}. * @param perSampleIvSize See {@link #perSampleIvSize}.
* @param keyId See {@link TrackOutput.CryptoData#encryptionKey}. * @param keyId See {@link TrackOutput.CryptoData#encryptionKey}.
* @param defaultEncryptedBlocks See {@link TrackOutput.CryptoData#encryptedBlocks}. * @param defaultEncryptedBlocks See {@link TrackOutput.CryptoData#encryptedBlocks}.
* @param defaultClearBlocks See {@link TrackOutput.CryptoData#clearBlocks}. * @param defaultClearBlocks See {@link TrackOutput.CryptoData#clearBlocks}.
* @param defaultInitializationVector See {@link #defaultInitializationVector}. * @param defaultInitializationVector See {@link #defaultInitializationVector}.
*/ */
public TrackEncryptionBox(boolean isEncrypted, @Nullable String schemeType, public TrackEncryptionBox(
int initializationVectorSize, byte[] keyId, int defaultEncryptedBlocks, boolean isEncrypted,
int defaultClearBlocks, @Nullable byte[] defaultInitializationVector) { @Nullable String schemeType,
Assertions.checkArgument(initializationVectorSize == 0 ^ defaultInitializationVector == null); int perSampleIvSize,
byte[] keyId,
int defaultEncryptedBlocks,
int defaultClearBlocks,
@Nullable byte[] defaultInitializationVector) {
Assertions.checkArgument(perSampleIvSize == 0 ^ defaultInitializationVector == null);
this.isEncrypted = isEncrypted; this.isEncrypted = isEncrypted;
this.schemeType = schemeType; this.schemeType = schemeType;
this.initializationVectorSize = initializationVectorSize; this.perSampleIvSize = perSampleIvSize;
this.defaultInitializationVector = defaultInitializationVector; this.defaultInitializationVector = defaultInitializationVector;
cryptoData = new TrackOutput.CryptoData(schemeToCryptoMode(schemeType), keyId, cryptoData = new TrackOutput.CryptoData(schemeToCryptoMode(schemeType), keyId,
defaultEncryptedBlocks, defaultClearBlocks); defaultEncryptedBlocks, defaultClearBlocks);
......
...@@ -190,4 +190,8 @@ import java.io.IOException; ...@@ -190,4 +190,8 @@ import java.io.IOException;
return sampleDecodingTimeTable[index] + sampleCompositionTimeOffsetTable[index]; return sampleDecodingTimeTable[index] + sampleCompositionTimeOffsetTable[index];
} }
/** Returns whether the sample at the given index has a subsample encryption table. */
public boolean sampleHasSubsampleEncryptionTable(int index) {
return definesEncryptionData && sampleHasSubsampleEncryptionTable[index];
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment