resync with Extractor changes

parent da7ae2a9
...@@ -13,11 +13,11 @@ ...@@ -13,11 +13,11 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package com.google.android.exoplayer.hls.parser; package com.google.android.exoplayer.extractor.ts;
import com.google.android.exoplayer.C; import com.google.android.exoplayer.C;
import com.google.android.exoplayer.MediaFormat; import com.google.android.exoplayer.MediaFormat;
import com.google.android.exoplayer.upstream.BufferPool; import com.google.android.exoplayer.extractor.TrackOutput;
import com.google.android.exoplayer.util.CodecSpecificDataUtil; import com.google.android.exoplayer.util.CodecSpecificDataUtil;
import com.google.android.exoplayer.util.MimeTypes; import com.google.android.exoplayer.util.MimeTypes;
import com.google.android.exoplayer.util.ParsableBitArray; import com.google.android.exoplayer.util.ParsableBitArray;
...@@ -48,7 +48,8 @@ import java.util.Collections; ...@@ -48,7 +48,8 @@ import java.util.Collections;
// Used to find the header. // Used to find the header.
private boolean hasCrc; private boolean hasCrc;
// Parsed from the header. // Used when parsing the header.
private boolean hasOutputFormat;
private long frameDurationUs; private long frameDurationUs;
private int sampleSize; private int sampleSize;
...@@ -149,8 +150,8 @@ import java.util.Collections; ...@@ -149,8 +150,8 @@ import java.util.Collections;
1 // Layer3 1 // Layer3
}; };
public MpaReader(BufferPool bufferPool) { public MpaReader(TrackOutput output) {
super(bufferPool); super(output);
mpaScratch = new ParsableBitArray(new byte[HEADER_SIZE + CRC_SIZE]); mpaScratch = new ParsableBitArray(new byte[HEADER_SIZE + CRC_SIZE]);
state = STATE_FINDING_SYNC; state = STATE_FINDING_SYNC;
} }
...@@ -171,18 +172,17 @@ import java.util.Collections; ...@@ -171,18 +172,17 @@ import java.util.Collections;
case STATE_READING_HEADER: case STATE_READING_HEADER:
int targetLength = hasCrc ? HEADER_SIZE + CRC_SIZE : HEADER_SIZE; int targetLength = hasCrc ? HEADER_SIZE + CRC_SIZE : HEADER_SIZE;
if (continueRead(data, mpaScratch.getData(), targetLength)) { if (continueRead(data, mpaScratch.getData(), targetLength)) {
startSample(timeUs);
parseHeader(); parseHeader();
bytesRead = 0; bytesRead = targetLength;
state = STATE_READING_SAMPLE; state = STATE_READING_SAMPLE;
} }
break; break;
case STATE_READING_SAMPLE: case STATE_READING_SAMPLE:
int bytesToRead = Math.min(data.bytesLeft(), sampleSize - bytesRead); int bytesToRead = Math.min(data.bytesLeft(), sampleSize - bytesRead);
appendData(data, bytesToRead); output.sampleData(data, bytesToRead);
bytesRead += bytesToRead; bytesRead += bytesToRead;
if (bytesRead == sampleSize) { if (bytesRead == sampleSize) {
commitSample(true); output.sampleMetadata(timeUs, C.SAMPLE_FLAG_SYNC, sampleSize, 0, null);
timeUs += frameDurationUs; timeUs += frameDurationUs;
bytesRead = 0; bytesRead = 0;
state = STATE_FINDING_SYNC; state = STATE_FINDING_SYNC;
...@@ -243,7 +243,7 @@ import java.util.Collections; ...@@ -243,7 +243,7 @@ import java.util.Collections;
* @param LSF Low Sample rate Format (MPEG 2) * @param LSF Low Sample rate Format (MPEG 2)
* @param bitrate The bitrate in bits per second * @param bitrate The bitrate in bits per second
* @param samplesPerSec The sampling rate in hertz * @param samplesPerSec The sampling rate in hertz
* @param paddingSize * @param -paddingSize
* @return Frame size in bytes * @return Frame size in bytes
*/ */
private static int CalcMpaFrameSize (int layer, int LSF, int bitrate, int samplesPerSec, int paddingSize) { private static int CalcMpaFrameSize (int layer, int LSF, int bitrate, int samplesPerSec, int paddingSize) {
...@@ -255,9 +255,9 @@ import java.util.Collections; ...@@ -255,9 +255,9 @@ import java.util.Collections;
*/ */
private void parseHeader() { private void parseHeader() {
int headerLength = hasCrc ? HEADER_SIZE + CRC_SIZE : HEADER_SIZE; int headerLength = hasCrc ? HEADER_SIZE + CRC_SIZE : HEADER_SIZE;
mpaScratch.setPosition(0);
if (!hasMediaFormat()) { if (!hasOutputFormat) {
mpaScratch.setPosition(0);
mpaScratch.skipBits(12); mpaScratch.skipBits(12);
int isLSF = (!mpaScratch.readBit()) ? 1 : 0; int isLSF = (!mpaScratch.readBit()) ? 1 : 0;
int layer = mpaScratch.readBits(2) ^ 3; int layer = mpaScratch.readBits(2) ^ 3;
...@@ -279,14 +279,15 @@ import java.util.Collections; ...@@ -279,14 +279,15 @@ import java.util.Collections;
MediaFormat mediaFormat = MediaFormat.createAudioFormat(/*isLSF == 1 ?*/ MimeTypes.AUDIO_MPEG/* : MimeTypes.AUDIO_MP1L2*/, MediaFormat mediaFormat = MediaFormat.createAudioFormat(/*isLSF == 1 ?*/ MimeTypes.AUDIO_MPEG/* : MimeTypes.AUDIO_MP1L2*/,
MediaFormat.NO_VALUE, audioParams.second, audioParams.first, MediaFormat.NO_VALUE, audioParams.second, audioParams.first,
Collections.singletonList(audioSpecificConfig)); Collections.singletonList(audioSpecificConfig));
output.format(mediaFormat);
hasOutputFormat = true;
frameDurationUs = (C.MICROS_PER_SECOND * MPA_SAMPLES_PER_FRAME[isLSF][layer]) / mediaFormat.sampleRate; frameDurationUs = (C.MICROS_PER_SECOND * MPA_SAMPLES_PER_FRAME[isLSF][layer]) / mediaFormat.sampleRate;
setMediaFormat(mediaFormat); sampleSize = CalcMpaFrameSize(layer, isLSF, bitRate * 1000, sampleRate, paddingBit);
sampleSize = CalcMpaFrameSize(layer, isLSF, bitRate * 1000, sampleRate, paddingBit) - headerLength;
} }
mpaScratch.setPosition(0); mpaScratch.setPosition(0);
ParsableByteArray header = new ParsableByteArray(mpaScratch.getData(),headerLength); ParsableByteArray header = new ParsableByteArray(mpaScratch.getData(),headerLength);
appendData(header, headerLength); output.sampleData(header, headerLength);
} }
} }
...@@ -248,8 +248,10 @@ public final class TsExtractor implements Extractor { ...@@ -248,8 +248,10 @@ public final class TsExtractor implements Extractor {
ElementaryStreamReader pesPayloadReader = null; ElementaryStreamReader pesPayloadReader = null;
switch (streamType) { switch (streamType) {
case TS_STREAM_TYPE_MPA: case TS_STREAM_TYPE_MPA:
pesPayloadReader = new MpaReader(output.track(TS_STREAM_TYPE_MPA));
break;
case TS_STREAM_TYPE_MPA_LSF: case TS_STREAM_TYPE_MPA_LSF:
pesPayloadReader = new MpaReader(bufferPool); pesPayloadReader = new MpaReader(output.track(TS_STREAM_TYPE_MPA_LSF));
break; break;
case TS_STREAM_TYPE_AAC: case TS_STREAM_TYPE_AAC:
pesPayloadReader = new AdtsReader(output.track(TS_STREAM_TYPE_AAC)); pesPayloadReader = new AdtsReader(output.track(TS_STREAM_TYPE_AAC));
......
...@@ -31,7 +31,6 @@ public class MimeTypes { ...@@ -31,7 +31,6 @@ public class MimeTypes {
public static final String VIDEO_VP9 = BASE_TYPE_VIDEO + "/x-vnd.on2.vp9"; public static final String VIDEO_VP9 = BASE_TYPE_VIDEO + "/x-vnd.on2.vp9";
public static final String VIDEO_MP4V = BASE_TYPE_VIDEO + "/mp4v-es"; public static final String VIDEO_MP4V = BASE_TYPE_VIDEO + "/mp4v-es";
public static final String AUDIO_MPEG = BASE_TYPE_AUDIO + "/mpeg";
public static final String AUDIO_MP4 = BASE_TYPE_AUDIO + "/mp4"; public static final String AUDIO_MP4 = BASE_TYPE_AUDIO + "/mp4";
public static final String AUDIO_AAC = BASE_TYPE_AUDIO + "/mp4a-latm"; public static final String AUDIO_AAC = BASE_TYPE_AUDIO + "/mp4a-latm";
public static final String AUDIO_AC3 = BASE_TYPE_AUDIO + "/ac3"; public static final String AUDIO_AC3 = BASE_TYPE_AUDIO + "/ac3";
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment