下面列出了android.media.MediaExtractor#SAMPLE_FLAG_ENCRYPTED 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@SuppressLint("InlinedApi")
private void readSampleEncryptionData(ParsableByteArray sampleEncryptionData, SampleHolder out) {
TrackEncryptionBox encryptionBox =
track.sampleDescriptionEncryptionBoxes[fragmentRun.sampleDescriptionIndex];
byte[] keyId = encryptionBox.keyId;
boolean isEncrypted = encryptionBox.isEncrypted;
int vectorSize = encryptionBox.initializationVectorSize;
boolean subsampleEncryption = fragmentRun.sampleHasSubsampleEncryptionTable[sampleIndex];
byte[] vector = out.cryptoInfo.iv;
if (vector == null || vector.length != 16) {
vector = new byte[16];
}
sampleEncryptionData.readBytes(vector, 0, vectorSize);
int subsampleCount = subsampleEncryption ? sampleEncryptionData.readUnsignedShort() : 1;
int[] clearDataSizes = out.cryptoInfo.numBytesOfClearData;
if (clearDataSizes == null || clearDataSizes.length < subsampleCount) {
clearDataSizes = new int[subsampleCount];
}
int[] encryptedDataSizes = out.cryptoInfo.numBytesOfEncryptedData;
if (encryptedDataSizes == null || encryptedDataSizes.length < subsampleCount) {
encryptedDataSizes = new int[subsampleCount];
}
if (subsampleEncryption) {
for (int i = 0; i < subsampleCount; i++) {
clearDataSizes[i] = sampleEncryptionData.readUnsignedShort();
encryptedDataSizes[i] = sampleEncryptionData.readUnsignedIntToInt();
}
} else {
clearDataSizes[0] = 0;
encryptedDataSizes[0] = fragmentRun.sampleSizeTable[sampleIndex];
}
out.cryptoInfo.set(subsampleCount, clearDataSizes, encryptedDataSizes, keyId, vector,
isEncrypted ? MediaCodec.CRYPTO_MODE_AES_CTR : MediaCodec.CRYPTO_MODE_UNENCRYPTED);
if (isEncrypted) {
out.flags |= MediaExtractor.SAMPLE_FLAG_ENCRYPTED;
}
}
@Override
public int readSample(int track, SampleHolder sampleHolder) {
int sampleTrack = mediaExtractor.getSampleTrackIndex();
if (sampleTrack != track) {
return sampleTrack < 0 ? SampleSource.END_OF_STREAM : SampleSource.NOTHING_READ;
}
if (sampleHolder.data != null) {
int offset = sampleHolder.data.position();
sampleHolder.size = mediaExtractor.readSampleData(sampleHolder.data, offset);
sampleHolder.data.position(offset + sampleHolder.size);
} else {
sampleHolder.size = 0;
}
sampleHolder.timeUs = mediaExtractor.getSampleTime();
sampleHolder.flags = mediaExtractor.getSampleFlags();
if ((sampleHolder.flags & MediaExtractor.SAMPLE_FLAG_ENCRYPTED) != 0) {
sampleHolder.cryptoInfo.setFromExtractorV16(mediaExtractor);
}
// // yy debug stuff
// if (MediaCodecVideoTrackRenderer.bb == null) {
// MediaCodecVideoTrackRenderer.bb = new ArrayList<byte[]>();
// }
// int len = sampleHolder.size;
// byte[] dd = new byte[len];
// for (int i=0; i<len; i++) {
// dd[i] = sampleHolder.data.get(i);
// }
// MediaCodecVideoTrackRenderer.bb.add(dd);
//
// if (MediaCodecVideoTrackRenderer.pts == null) {
// MediaCodecVideoTrackRenderer.pts = new ArrayList<Long>();
// }
// MediaCodecVideoTrackRenderer.pts.add(new Long(sampleHolder.timeUs));
mediaExtractor.advance();
return SampleSource.SAMPLE_READ;
}