Add AC-4 DRM Support

This commit is contained in:
ybai001 2019-12-23 10:49:03 +08:00
parent c17d1467f9
commit cc5e981e89
3 changed files with 17 additions and 7 deletions

View File

@ -1222,6 +1222,7 @@ public class FragmentedMp4Extractor implements Extractor {
* @throws InterruptedException If the thread is interrupted.
*/
private boolean readSample(ExtractorInput input) throws IOException, InterruptedException {
int outputSampleEncryptionDataSize = 0;
if (parserState == STATE_READING_SAMPLE_START) {
if (currentTrackBundle == null) {
@Nullable TrackBundle currentTrackBundle = getNextFragmentRun(trackBundles);
@ -1269,6 +1270,7 @@ public class FragmentedMp4Extractor implements Extractor {
}
sampleBytesWritten = currentTrackBundle.outputSampleEncryptionData();
sampleSize += sampleBytesWritten;
outputSampleEncryptionDataSize = sampleBytesWritten;
parserState = STATE_READING_SAMPLE_CONTINUE;
sampleCurrentNalBytesRemaining = 0;
isAc4HeaderRequired =
@ -1338,7 +1340,7 @@ public class FragmentedMp4Extractor implements Extractor {
}
} else {
if (isAc4HeaderRequired) {
Ac4Util.getAc4SampleHeader(sampleSize, scratch);
Ac4Util.getAc4SampleHeader(sampleSize - outputSampleEncryptionDataSize, scratch);
int length = scratch.limit();
output.sampleData(scratch, length);
sampleSize += length;

View File

@ -23,6 +23,7 @@ import com.google.android.exoplayer2.extractor.TrackOutput.CryptoData;
import com.google.android.exoplayer2.source.SampleQueue.SampleExtrasHolder;
import com.google.android.exoplayer2.upstream.Allocation;
import com.google.android.exoplayer2.upstream.Allocator;
import com.google.android.exoplayer2.util.MimeTypes;
import com.google.android.exoplayer2.util.ParsableByteArray;
import java.io.EOFException;
import java.io.IOException;
@ -114,11 +115,13 @@ import java.nio.ByteBuffer;
*
* @param buffer The buffer to populate.
* @param extrasHolder The extras holder whose offset should be read and subsequently adjusted.
* @param mimeType The MIME type.
*/
public void readToBuffer(DecoderInputBuffer buffer, SampleExtrasHolder extrasHolder) {
public void readToBuffer(DecoderInputBuffer buffer, SampleExtrasHolder extrasHolder,
String mimeType) {
// Read encryption data if the sample is encrypted.
if (buffer.isEncrypted()) {
readEncryptionData(buffer, extrasHolder);
readEncryptionData(buffer, extrasHolder, mimeType);
}
// Read sample data, extracting supplemental data into a separate buffer if needed.
if (buffer.hasSupplementalData()) {
@ -215,8 +218,10 @@ import java.nio.ByteBuffer;
*
* @param buffer The buffer into which the encryption data should be written.
* @param extrasHolder The extras holder whose offset should be read and subsequently adjusted.
* @param mimeType The MIME type.
*/
private void readEncryptionData(DecoderInputBuffer buffer, SampleExtrasHolder extrasHolder) {
private void readEncryptionData(DecoderInputBuffer buffer, SampleExtrasHolder extrasHolder,
String mimeType) {
long offset = extrasHolder.offset;
// Read the signal byte.
@ -265,8 +270,10 @@ import java.nio.ByteBuffer;
encryptedDataSizes[i] = scratch.readUnsignedIntToInt();
}
} else {
clearDataSizes[0] = 0;
encryptedDataSizes[0] = extrasHolder.size - (int) (offset - extrasHolder.offset);
int addedHeaderSize = MimeTypes.AUDIO_AC4.equals(mimeType) ? 7 : 0;
clearDataSizes[0] = addedHeaderSize;
encryptedDataSizes[0] = extrasHolder.size - (int) (offset - extrasHolder.offset)
- addedHeaderSize;
}
// Populate the cryptoInfo.

View File

@ -323,7 +323,8 @@ public class SampleQueue implements TrackOutput {
readSampleMetadata(
formatHolder, buffer, formatRequired, loadingFinished, decodeOnlyUntilUs, extrasHolder);
if (result == C.RESULT_BUFFER_READ && !buffer.isEndOfStream() && !buffer.isFlagsOnly()) {
sampleDataQueue.readToBuffer(buffer, extrasHolder);
sampleDataQueue.readToBuffer(buffer, extrasHolder,
downstreamFormat == null ? null : downstreamFormat.sampleMimeType);
}
return result;
}