use of com.google.android.exoplayer2.util.Assertions.checkNotNull in project ExoPlayer by google.
the class WavHeaderReader method peek.
/**
* Peeks and returns a {@code WavHeader}.
*
* @param input Input stream to peek the WAV header from.
* @throws ParserException If the input file is an incorrect RIFF WAV.
* @throws IOException If peeking from the input fails.
* @throws InterruptedException If interrupted while peeking from input.
* @return A new {@code WavHeader} peeked from {@code input}, or null if the input is not a
* supported WAV format.
*/
public static WavHeader peek(ExtractorInput input) throws IOException, InterruptedException {
Assertions.checkNotNull(input);
// Allocate a scratch buffer large enough to store the format chunk.
ParsableByteArray scratch = new ParsableByteArray(16);
// Attempt to read the RIFF chunk.
ChunkHeader chunkHeader = ChunkHeader.peek(input, scratch);
if (chunkHeader.id != Util.getIntegerCodeForString("RIFF")) {
return null;
}
input.peekFully(scratch.data, 0, 4);
scratch.setPosition(0);
int riffFormat = scratch.readInt();
if (riffFormat != Util.getIntegerCodeForString("WAVE")) {
Log.e(TAG, "Unsupported RIFF format: " + riffFormat);
return null;
}
// Skip chunks until we find the format chunk.
chunkHeader = ChunkHeader.peek(input, scratch);
while (chunkHeader.id != Util.getIntegerCodeForString("fmt ")) {
input.advancePeekPosition((int) chunkHeader.size);
chunkHeader = ChunkHeader.peek(input, scratch);
}
Assertions.checkState(chunkHeader.size >= 16);
input.peekFully(scratch.data, 0, 16);
scratch.setPosition(0);
int type = scratch.readLittleEndianUnsignedShort();
int numChannels = scratch.readLittleEndianUnsignedShort();
int sampleRateHz = scratch.readLittleEndianUnsignedIntToInt();
int averageBytesPerSecond = scratch.readLittleEndianUnsignedIntToInt();
int blockAlignment = scratch.readLittleEndianUnsignedShort();
int bitsPerSample = scratch.readLittleEndianUnsignedShort();
int expectedBlockAlignment = numChannels * bitsPerSample / 8;
if (blockAlignment != expectedBlockAlignment) {
throw new ParserException("Expected block alignment: " + expectedBlockAlignment + "; got: " + blockAlignment);
}
@C.PcmEncoding int encoding = Util.getPcmEncoding(bitsPerSample);
if (encoding == C.ENCODING_INVALID) {
Log.e(TAG, "Unsupported WAV bit depth: " + bitsPerSample);
return null;
}
if (type != TYPE_PCM && type != TYPE_WAVE_FORMAT_EXTENSIBLE) {
Log.e(TAG, "Unsupported WAV format type: " + type);
return null;
}
// If present, skip extensionSize, validBitsPerSample, channelMask, subFormatGuid, ...
input.advancePeekPosition((int) chunkHeader.size - 16);
return new WavHeader(numChannels, sampleRateHz, averageBytesPerSecond, blockAlignment, bitsPerSample, encoding);
}
use of com.google.android.exoplayer2.util.Assertions.checkNotNull in project ExoPlayer by google.
the class VpxDecoder method decode.
@Override
@Nullable
protected VpxDecoderException decode(DecoderInputBuffer inputBuffer, VideoDecoderOutputBuffer outputBuffer, boolean reset) {
if (reset && lastSupplementalData != null) {
// Don't propagate supplemental data across calls to flush the decoder.
lastSupplementalData.clear();
}
ByteBuffer inputData = Util.castNonNull(inputBuffer.data);
int inputSize = inputData.limit();
CryptoInfo cryptoInfo = inputBuffer.cryptoInfo;
final long result = inputBuffer.isEncrypted() ? vpxSecureDecode(vpxDecContext, inputData, inputSize, cryptoConfig, cryptoInfo.mode, Assertions.checkNotNull(cryptoInfo.key), Assertions.checkNotNull(cryptoInfo.iv), cryptoInfo.numSubSamples, cryptoInfo.numBytesOfClearData, cryptoInfo.numBytesOfEncryptedData) : vpxDecode(vpxDecContext, inputData, inputSize);
if (result != NO_ERROR) {
if (result == DRM_ERROR) {
String message = "Drm error: " + vpxGetErrorMessage(vpxDecContext);
CryptoException cause = new CryptoException(vpxGetErrorCode(vpxDecContext), message);
return new VpxDecoderException(message, cause);
} else {
return new VpxDecoderException("Decode error: " + vpxGetErrorMessage(vpxDecContext));
}
}
if (inputBuffer.hasSupplementalData()) {
ByteBuffer supplementalData = Assertions.checkNotNull(inputBuffer.supplementalData);
int size = supplementalData.remaining();
if (size > 0) {
if (lastSupplementalData == null || lastSupplementalData.capacity() < size) {
lastSupplementalData = ByteBuffer.allocate(size);
} else {
lastSupplementalData.clear();
}
lastSupplementalData.put(supplementalData);
lastSupplementalData.flip();
}
}
if (!inputBuffer.isDecodeOnly()) {
outputBuffer.init(inputBuffer.timeUs, outputMode, lastSupplementalData);
int getFrameResult = vpxGetFrame(vpxDecContext, outputBuffer);
if (getFrameResult == 1) {
outputBuffer.addFlag(C.BUFFER_FLAG_DECODE_ONLY);
} else if (getFrameResult == -1) {
return new VpxDecoderException("Buffer initialization failed.");
}
outputBuffer.format = inputBuffer.format;
}
return null;
}
use of com.google.android.exoplayer2.util.Assertions.checkNotNull in project ExoPlayer by google.
the class MetadataRenderer method decodeWrappedMetadata.
/**
* Iterates through {@code metadata.entries} and checks each one to see if contains wrapped
* metadata. If it does, then we recursively decode the wrapped metadata. If it doesn't (recursion
* base-case), we add the {@link Metadata.Entry} to {@code decodedEntries} (output parameter).
*/
private void decodeWrappedMetadata(Metadata metadata, List<Metadata.Entry> decodedEntries) {
for (int i = 0; i < metadata.length(); i++) {
@Nullable Format wrappedMetadataFormat = metadata.get(i).getWrappedMetadataFormat();
if (wrappedMetadataFormat != null && decoderFactory.supportsFormat(wrappedMetadataFormat)) {
MetadataDecoder wrappedMetadataDecoder = decoderFactory.createDecoder(wrappedMetadataFormat);
// wrappedMetadataFormat != null so wrappedMetadataBytes must be non-null too.
byte[] wrappedMetadataBytes = Assertions.checkNotNull(metadata.get(i).getWrappedMetadataBytes());
buffer.clear();
buffer.ensureSpaceForWrite(wrappedMetadataBytes.length);
castNonNull(buffer.data).put(wrappedMetadataBytes);
buffer.flip();
@Nullable Metadata innerMetadata = wrappedMetadataDecoder.decode(buffer);
if (innerMetadata != null) {
// The decoding succeeded, so we'll try another level of unwrapping.
decodeWrappedMetadata(innerMetadata, decodedEntries);
}
} else {
// Entry doesn't contain any wrapped metadata, so output it directly.
decodedEntries.add(metadata.get(i));
}
}
}
use of com.google.android.exoplayer2.util.Assertions.checkNotNull in project ExoPlayer by google.
the class BundledExtractorsAdapter method init.
@Override
public void init(DataReader dataReader, Uri uri, Map<String, List<String>> responseHeaders, long position, long length, ExtractorOutput output) throws IOException {
ExtractorInput extractorInput = new DefaultExtractorInput(dataReader, position, length);
this.extractorInput = extractorInput;
if (extractor != null) {
return;
}
Extractor[] extractors = extractorsFactory.createExtractors(uri, responseHeaders);
if (extractors.length == 1) {
this.extractor = extractors[0];
} else {
for (Extractor extractor : extractors) {
try {
if (extractor.sniff(extractorInput)) {
this.extractor = extractor;
break;
}
} catch (EOFException e) {
// Do nothing.
} finally {
Assertions.checkState(this.extractor != null || extractorInput.getPosition() == position);
extractorInput.resetPeekPosition();
}
}
if (extractor == null) {
throw new UnrecognizedInputFormatException("None of the available extractors (" + Util.getCommaDelimitedSimpleClassNames(extractors) + ") could read the stream.", Assertions.checkNotNull(uri));
}
}
extractor.init(output);
}
use of com.google.android.exoplayer2.util.Assertions.checkNotNull in project ExoPlayer by google.
the class HttpMediaDrmCallback method executePost.
private static byte[] executePost(HttpDataSource.Factory dataSourceFactory, String url, @Nullable byte[] httpBody, Map<String, String> requestProperties) throws MediaDrmCallbackException {
StatsDataSource dataSource = new StatsDataSource(dataSourceFactory.createDataSource());
int manualRedirectCount = 0;
DataSpec dataSpec = new DataSpec.Builder().setUri(url).setHttpRequestHeaders(requestProperties).setHttpMethod(DataSpec.HTTP_METHOD_POST).setHttpBody(httpBody).setFlags(DataSpec.FLAG_ALLOW_GZIP).build();
DataSpec originalDataSpec = dataSpec;
try {
while (true) {
DataSourceInputStream inputStream = new DataSourceInputStream(dataSource, dataSpec);
try {
return Util.toByteArray(inputStream);
} catch (InvalidResponseCodeException e) {
@Nullable String redirectUrl = getRedirectUrl(e, manualRedirectCount);
if (redirectUrl == null) {
throw e;
}
manualRedirectCount++;
dataSpec = dataSpec.buildUpon().setUri(redirectUrl).build();
} finally {
Util.closeQuietly(inputStream);
}
}
} catch (Exception e) {
throw new MediaDrmCallbackException(originalDataSpec, Assertions.checkNotNull(dataSource.getLastOpenedUri()), dataSource.getResponseHeaders(), dataSource.getBytesRead(), /* cause= */
e);
}
}
Aggregations