@Override public int read(DataSource dataSource) throws IOException { int bytesRead = dataSource.read(tsPacketBuffer.data, tsPacketBytesRead, TS_PACKET_SIZE - tsPacketBytesRead); if (bytesRead == -1) { return -1; } tsPacketBytesRead += bytesRead; if (tsPacketBytesRead < TS_PACKET_SIZE) { // We haven't read the whole packet yet. return bytesRead; } // Reset before reading the packet. tsPacketBytesRead = 0; tsPacketBuffer.setPosition(0); tsPacketBuffer.setLimit(TS_PACKET_SIZE); int syncByte = tsPacketBuffer.readUnsignedByte(); if (syncByte != TS_SYNC_BYTE) { return bytesRead; } tsPacketBuffer.readBytes(tsScratch, 3); tsScratch.skipBits(1); // transport_error_indicator boolean payloadUnitStartIndicator = tsScratch.readBit(); tsScratch.skipBits(1); // transport_priority int pid = tsScratch.readBits(13); tsScratch.skipBits(2); // transport_scrambling_control boolean adaptationFieldExists = tsScratch.readBit(); boolean payloadExists = tsScratch.readBit(); // Last 4 bits of scratch are skipped: continuity_counter // Skip the adaptation field. if (adaptationFieldExists) { int adaptationFieldLength = tsPacketBuffer.readUnsignedByte(); tsPacketBuffer.skip(adaptationFieldLength); } // Read the payload. if (payloadExists) { TsPayloadReader payloadReader = tsPayloadReaders.get(pid); if (payloadReader != null) { payloadReader.consume(tsPacketBuffer, payloadUnitStartIndicator); } } if (!prepared) { prepared = checkPrepared(); } return bytesRead; }
// @VisibleForTesting /* package */ static void appendNumberOfSamples( ParsableByteArray buffer, long packetSampleCount) { buffer.setLimit(buffer.limit() + 4); // The vorbis decoder expects the number of samples in the packet // to be appended to the audio data as an int32 buffer.data[buffer.limit() - 4] = (byte) ((packetSampleCount) & 0xFF); buffer.data[buffer.limit() - 3] = (byte) ((packetSampleCount >>> 8) & 0xFF); buffer.data[buffer.limit() - 2] = (byte) ((packetSampleCount >>> 16) & 0xFF); buffer.data[buffer.limit() - 1] = (byte) ((packetSampleCount >>> 24) & 0xFF); }
@Override public void consume(ParsableByteArray data, boolean payloadUnitStartIndicator) { if (payloadUnitStartIndicator) { switch (state) { case STATE_FINDING_HEADER: case STATE_READING_HEADER: // Expected. break; case STATE_READING_HEADER_EXTENSION: Log.w(TAG, "Unexpected start indicator reading extended header"); break; case STATE_READING_BODY: // If payloadSize == -1 then the length of the previous packet was unspecified, and so // we only know that it's finished now that we've seen the start of the next one. This // is expected. If payloadSize != -1, then the length of the previous packet was known, // but we didn't receive that amount of data. This is not expected. if (payloadSize != -1) { Log.w(TAG, "Unexpected start indicator: expected " + payloadSize + " more bytes"); } // Either way, if the body was started, notify the reader that it has now finished. if (bodyStarted) { pesPayloadReader.packetFinished(); } break; } setState(STATE_READING_HEADER); } while (data.bytesLeft() > 0) { switch (state) { case STATE_FINDING_HEADER: data.skip(data.bytesLeft()); break; case STATE_READING_HEADER: if (continueRead(data, pesScratch.getData(), HEADER_SIZE)) { setState(parseHeader() ? STATE_READING_HEADER_EXTENSION : STATE_FINDING_HEADER); } break; case STATE_READING_HEADER_EXTENSION: int readLength = Math.min(MAX_HEADER_EXTENSION_SIZE, extendedHeaderLength); // Read as much of the extended header as we're interested in, and skip the rest. if (continueRead(data, pesScratch.getData(), readLength) && continueRead(data, null, extendedHeaderLength)) { parseHeaderExtension(); bodyStarted = false; setState(STATE_READING_BODY); } break; case STATE_READING_BODY: readLength = data.bytesLeft(); int padding = payloadSize == -1 ? 0 : readLength - payloadSize; if (padding > 0) { readLength -= padding; data.setLimit(data.getPosition() + readLength); } pesPayloadReader.consume(data, timeUs, !bodyStarted); bodyStarted = true; if (payloadSize != -1) { payloadSize -= readLength; if (payloadSize == 0) { pesPayloadReader.packetFinished(); setState(STATE_READING_HEADER); } } break; } } }