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. * @throws InterruptedException If the thread is interrupted.
*/ */
private boolean readSample(ExtractorInput input) throws IOException, InterruptedException { private boolean readSample(ExtractorInput input) throws IOException, InterruptedException {
int outputSampleEncryptionDataSize = 0;
if (parserState == STATE_READING_SAMPLE_START) { if (parserState == STATE_READING_SAMPLE_START) {
if (currentTrackBundle == null) { if (currentTrackBundle == null) {
@Nullable TrackBundle currentTrackBundle = getNextFragmentRun(trackBundles); @Nullable TrackBundle currentTrackBundle = getNextFragmentRun(trackBundles);
@ -1269,6 +1270,7 @@ public class FragmentedMp4Extractor implements Extractor {
} }
sampleBytesWritten = currentTrackBundle.outputSampleEncryptionData(); sampleBytesWritten = currentTrackBundle.outputSampleEncryptionData();
sampleSize += sampleBytesWritten; sampleSize += sampleBytesWritten;
outputSampleEncryptionDataSize = sampleBytesWritten;
parserState = STATE_READING_SAMPLE_CONTINUE; parserState = STATE_READING_SAMPLE_CONTINUE;
sampleCurrentNalBytesRemaining = 0; sampleCurrentNalBytesRemaining = 0;
isAc4HeaderRequired = isAc4HeaderRequired =
@ -1338,7 +1340,7 @@ public class FragmentedMp4Extractor implements Extractor {
} }
} else { } else {
if (isAc4HeaderRequired) { if (isAc4HeaderRequired) {
Ac4Util.getAc4SampleHeader(sampleSize, scratch); Ac4Util.getAc4SampleHeader(sampleSize - outputSampleEncryptionDataSize, scratch);
int length = scratch.limit(); int length = scratch.limit();
output.sampleData(scratch, length); output.sampleData(scratch, length);
sampleSize += 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.source.SampleQueue.SampleExtrasHolder;
import com.google.android.exoplayer2.upstream.Allocation; import com.google.android.exoplayer2.upstream.Allocation;
import com.google.android.exoplayer2.upstream.Allocator; import com.google.android.exoplayer2.upstream.Allocator;
import com.google.android.exoplayer2.util.MimeTypes;
import com.google.android.exoplayer2.util.ParsableByteArray; import com.google.android.exoplayer2.util.ParsableByteArray;
import java.io.EOFException; import java.io.EOFException;
import java.io.IOException; import java.io.IOException;
@ -114,11 +115,13 @@ import java.nio.ByteBuffer;
* *
* @param buffer The buffer to populate. * @param buffer The buffer to populate.
* @param extrasHolder The extras holder whose offset should be read and subsequently adjusted. * @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. // Read encryption data if the sample is encrypted.
if (buffer.isEncrypted()) { if (buffer.isEncrypted()) {
readEncryptionData(buffer, extrasHolder); readEncryptionData(buffer, extrasHolder, mimeType);
} }
// Read sample data, extracting supplemental data into a separate buffer if needed. // Read sample data, extracting supplemental data into a separate buffer if needed.
if (buffer.hasSupplementalData()) { if (buffer.hasSupplementalData()) {
@ -215,8 +218,10 @@ import java.nio.ByteBuffer;
* *
* @param buffer The buffer into which the encryption data should be written. * @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 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; long offset = extrasHolder.offset;
// Read the signal byte. // Read the signal byte.
@ -265,8 +270,10 @@ import java.nio.ByteBuffer;
encryptedDataSizes[i] = scratch.readUnsignedIntToInt(); encryptedDataSizes[i] = scratch.readUnsignedIntToInt();
} }
} else { } else {
clearDataSizes[0] = 0; int addedHeaderSize = MimeTypes.AUDIO_AC4.equals(mimeType) ? 7 : 0;
encryptedDataSizes[0] = extrasHolder.size - (int) (offset - extrasHolder.offset); clearDataSizes[0] = addedHeaderSize;
encryptedDataSizes[0] = extrasHolder.size - (int) (offset - extrasHolder.offset)
- addedHeaderSize;
} }
// Populate the cryptoInfo. // Populate the cryptoInfo.

View File

@ -323,7 +323,8 @@ public class SampleQueue implements TrackOutput {
readSampleMetadata( readSampleMetadata(
formatHolder, buffer, formatRequired, loadingFinished, decodeOnlyUntilUs, extrasHolder); formatHolder, buffer, formatRequired, loadingFinished, decodeOnlyUntilUs, extrasHolder);
if (result == C.RESULT_BUFFER_READ && !buffer.isEndOfStream() && !buffer.isFlagsOnly()) { 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; return result;
} }