Changes based on internal review

This commit is contained in:
Rohit Singh 2024-01-17 16:57:19 +00:00
parent 32576be3e3
commit 50385be7ff
11 changed files with 90 additions and 95 deletions

View File

@ -29,7 +29,6 @@ import androidx.media3.common.ParserException;
import androidx.media3.common.util.ParsableBitArray;
import androidx.media3.common.util.UnstableApi;
import androidx.media3.common.util.Util;
import androidx.media3.extractor.ts.TsUtil;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
@ -259,7 +258,7 @@ public final class DtsUtil {
* @param frame The DTS Core frame to parse.
* @param trackId The track identifier to set on the format.
* @param language The language to set on the format.
* @param audioType The audio type to set on the format.
* @param roleFlags The role flags to set on the format.
* @param drmInitData {@link DrmInitData} to be included in the format.
* @return The DTS format parsed from data in the header.
*/
@ -267,7 +266,7 @@ public final class DtsUtil {
byte[] frame,
@Nullable String trackId,
@Nullable String language,
@TsUtil.AudioType int audioType,
@C.RoleFlags int roleFlags,
@Nullable DrmInitData drmInitData) {
ParsableBitArray frameBits = getNormalizedFrame(frame);
frameBits.skipBits(32 + 1 + 5 + 1 + 7 + 14); // SYNC, FTYPE, SHORT, CPF, NBLKS, FSIZE
@ -290,7 +289,7 @@ public final class DtsUtil {
.setSampleRate(sampleRate)
.setDrmInitData(drmInitData)
.setLanguage(language)
.setRoleFlags(TsUtil.parseRoleFlagsFromAudioType(audioType))
.setRoleFlags(roleFlags)
.build();
}

View File

@ -60,7 +60,7 @@ public final class Ac3Reader implements ElementaryStreamReader {
private final ParsableBitArray headerScratchBits;
private final ParsableByteArray headerScratchBytes;
@Nullable private final String language;
@TsUtil.AudioType private final int audioType;
private final @C.RoleFlags int roleFlags;
private @MonotonicNonNull String formatId;
private @MonotonicNonNull TrackOutput output;
@ -81,22 +81,22 @@ public final class Ac3Reader implements ElementaryStreamReader {
/** Constructs a new reader for (E-)AC-3 elementary streams. */
public Ac3Reader() {
this(null, TsUtil.AUDIO_TYPE_UNDEFINED);
this(null, /* roleFlags= */ 0);
}
/**
* Constructs a new reader for (E-)AC-3 elementary streams.
*
* @param language Track language.
* @param audioType Track audio type.
* @param roleFlags Track role flags.
*/
public Ac3Reader(@Nullable String language, @TsUtil.AudioType int audioType) {
public Ac3Reader(@Nullable String language, @C.RoleFlags int roleFlags) {
headerScratchBits = new ParsableBitArray(new byte[HEADER_SIZE]);
headerScratchBytes = new ParsableByteArray(headerScratchBits.data);
state = STATE_FINDING_SYNC;
timeUs = C.TIME_UNSET;
this.language = language;
this.audioType = audioType;
this.roleFlags = roleFlags;
}
@Override
@ -219,7 +219,7 @@ public final class Ac3Reader implements ElementaryStreamReader {
.setChannelCount(frameInfo.channelCount)
.setSampleRate(frameInfo.sampleRate)
.setLanguage(language)
.setRoleFlags(TsUtil.parseRoleFlagsFromAudioType(audioType))
.setRoleFlags(roleFlags)
.setPeakBitrate(frameInfo.bitrate);
// AC3 has constant bitrate, so averageBitrate = peakBitrate
if (MimeTypes.AUDIO_AC3.equals(frameInfo.mimeType)) {

View File

@ -57,7 +57,7 @@ public final class Ac4Reader implements ElementaryStreamReader {
private final ParsableBitArray headerScratchBits;
private final ParsableByteArray headerScratchBytes;
@Nullable private final String language;
@TsUtil.AudioType private final int audioType;
private final @C.RoleFlags int roleFlags;
private @MonotonicNonNull String formatId;
private @MonotonicNonNull TrackOutput output;
@ -79,16 +79,16 @@ public final class Ac4Reader implements ElementaryStreamReader {
/** Constructs a new reader for AC-4 elementary streams. */
public Ac4Reader() {
this(null, TsUtil.AUDIO_TYPE_UNDEFINED);
this(null, /* roleFlags= */ 0);
}
/**
* Constructs a new reader for AC-4 elementary streams.
*
* @param language Track language.
* @param audioType Track audio type.
* @param roleFlags Track role flags.
*/
public Ac4Reader(@Nullable String language, @TsUtil.AudioType int audioType) {
public Ac4Reader(@Nullable String language, @C.RoleFlags int roleFlags) {
headerScratchBits = new ParsableBitArray(new byte[Ac4Util.HEADER_SIZE_FOR_PARSER]);
headerScratchBytes = new ParsableByteArray(headerScratchBits.data);
state = STATE_FINDING_SYNC;
@ -97,7 +97,7 @@ public final class Ac4Reader implements ElementaryStreamReader {
hasCRC = false;
timeUs = C.TIME_UNSET;
this.language = language;
this.audioType = audioType;
this.roleFlags = roleFlags;
}
@Override
@ -220,7 +220,7 @@ public final class Ac4Reader implements ElementaryStreamReader {
.setChannelCount(frameInfo.channelCount)
.setSampleRate(frameInfo.sampleRate)
.setLanguage(language)
.setRoleFlags(TsUtil.parseRoleFlagsFromAudioType(audioType))
.setRoleFlags(roleFlags)
.build();
output.format(format);
}

View File

@ -71,7 +71,7 @@ public final class AdtsReader implements ElementaryStreamReader {
private final ParsableBitArray adtsScratch;
private final ParsableByteArray id3HeaderBuffer;
@Nullable private final String language;
@TsUtil.AudioType private final int audioType;
private final @C.RoleFlags int roleFlags;
private @MonotonicNonNull String formatId;
private @MonotonicNonNull TrackOutput output;
@ -106,15 +106,15 @@ public final class AdtsReader implements ElementaryStreamReader {
* @param exposeId3 True if the reader should expose ID3 information.
*/
public AdtsReader(boolean exposeId3) {
this(exposeId3, null, TsUtil.AUDIO_TYPE_UNDEFINED);
this(exposeId3, null, /* roleFlags= */ 0);
}
/**
* @param exposeId3 True if the reader should expose ID3 information.
* @param language Track language.
* @param audioType Track audio type.
* @param roleFlags Track role flags.
*/
public AdtsReader(boolean exposeId3, @Nullable String language, @TsUtil.AudioType int audioType) {
public AdtsReader(boolean exposeId3, @Nullable String language, @C.RoleFlags int roleFlags) {
adtsScratch = new ParsableBitArray(new byte[HEADER_SIZE + CRC_SIZE]);
id3HeaderBuffer = new ParsableByteArray(Arrays.copyOf(ID3_IDENTIFIER, ID3_HEADER_SIZE));
setFindingSampleState();
@ -124,7 +124,7 @@ public final class AdtsReader implements ElementaryStreamReader {
timeUs = C.TIME_UNSET;
this.exposeId3 = exposeId3;
this.language = language;
this.audioType = audioType;
this.roleFlags = roleFlags;
}
/** Returns whether an integer matches an ADTS SYNC word. */
@ -513,7 +513,7 @@ public final class AdtsReader implements ElementaryStreamReader {
.setSampleRate(aacConfig.sampleRateHz)
.setInitializationData(Collections.singletonList(audioSpecificConfig))
.setLanguage(language)
.setRoleFlags(TsUtil.parseRoleFlagsFromAudioType(audioType))
.setRoleFlags(roleFlags)
.build();
// In this class a sample is an access unit, but the MediaFormat sample rate specifies the
// number of PCM audio samples per second.

View File

@ -151,20 +151,20 @@ public final class DefaultTsPayloadReaderFactory implements TsPayloadReader.Fact
switch (streamType) {
case TsExtractor.TS_STREAM_TYPE_MPA:
case TsExtractor.TS_STREAM_TYPE_MPA_LSF:
return new PesReader(new MpegAudioReader(esInfo.language, esInfo.audioType));
return new PesReader(new MpegAudioReader(esInfo.language, esInfo.getRoleFlags()));
case TsExtractor.TS_STREAM_TYPE_AAC_ADTS:
return isSet(FLAG_IGNORE_AAC_STREAM)
? null
: new PesReader(new AdtsReader(false, esInfo.language, esInfo.audioType));
: new PesReader(new AdtsReader(false, esInfo.language, esInfo.getRoleFlags()));
case TsExtractor.TS_STREAM_TYPE_AAC_LATM:
return isSet(FLAG_IGNORE_AAC_STREAM)
? null
: new PesReader(new LatmReader(esInfo.language, esInfo.audioType));
: new PesReader(new LatmReader(esInfo.language, esInfo.getRoleFlags()));
case TsExtractor.TS_STREAM_TYPE_AC3:
case TsExtractor.TS_STREAM_TYPE_E_AC3:
return new PesReader(new Ac3Reader(esInfo.language, esInfo.audioType));
return new PesReader(new Ac3Reader(esInfo.language, esInfo.getRoleFlags()));
case TsExtractor.TS_STREAM_TYPE_AC4:
return new PesReader(new Ac4Reader(esInfo.language, esInfo.audioType));
return new PesReader(new Ac4Reader(esInfo.language, esInfo.getRoleFlags()));
case TsExtractor.TS_STREAM_TYPE_HDMV_DTS:
if (!isSet(FLAG_ENABLE_HDMV_DTS_AUDIO_STREAMS)) {
return null;
@ -173,10 +173,10 @@ public final class DefaultTsPayloadReaderFactory implements TsPayloadReader.Fact
case TsExtractor.TS_STREAM_TYPE_DTS:
case TsExtractor.TS_STREAM_TYPE_DTS_HD:
return new PesReader(
new DtsReader(esInfo.language, DtsReader.EXTSS_HEADER_SIZE_MAX, esInfo.audioType));
new DtsReader(esInfo.language, esInfo.getRoleFlags(), DtsReader.EXTSS_HEADER_SIZE_MAX));
case TsExtractor.TS_STREAM_TYPE_DTS_UHD:
return new PesReader(
new DtsReader(esInfo.language, DtsReader.FTOC_MAX_HEADER_SIZE, esInfo.audioType));
new DtsReader(esInfo.language, esInfo.getRoleFlags(), DtsReader.FTOC_MAX_HEADER_SIZE));
case TsExtractor.TS_STREAM_TYPE_H262:
case TsExtractor.TS_STREAM_TYPE_DC2_H262:
return new PesReader(new H262Reader(buildUserDataReader(esInfo)));

View File

@ -68,7 +68,7 @@ public final class DtsReader implements ElementaryStreamReader {
private final AtomicInteger uhdAudioChunkId;
@Nullable private final String language;
@TsUtil.AudioType private final int audioType;
private final @C.RoleFlags int roleFlags;
private @MonotonicNonNull String formatId;
private @MonotonicNonNull TrackOutput output;
@ -94,10 +94,10 @@ public final class DtsReader implements ElementaryStreamReader {
* Constructs a new reader for DTS elementary streams.
*
* @param language Track language.
* @param audioType Track audio type.
* @param roleFlags Track role flags.
* @param maxHeaderSize Maximum size of the header in a frame.
*/
public DtsReader(@Nullable String language, int maxHeaderSize, @TsUtil.AudioType int audioType) {
public DtsReader(@Nullable String language, @C.RoleFlags int roleFlags, int maxHeaderSize) {
headerScratchBytes = new ParsableByteArray(new byte[maxHeaderSize]);
state = STATE_FINDING_SYNC;
timeUs = C.TIME_UNSET;
@ -105,7 +105,7 @@ public final class DtsReader implements ElementaryStreamReader {
extensionSubstreamHeaderSize = C.LENGTH_UNSET;
uhdHeaderSize = C.LENGTH_UNSET;
this.language = language;
this.audioType = audioType;
this.roleFlags = roleFlags;
}
@Override
@ -266,7 +266,7 @@ public final class DtsReader implements ElementaryStreamReader {
private void parseCoreHeader() {
byte[] frameData = headerScratchBytes.getData();
if (format == null) {
format = DtsUtil.parseDtsFormat(frameData, formatId, language, audioType, null);
format = DtsUtil.parseDtsFormat(frameData, formatId, language, roleFlags, null);
output.format(format);
}
sampleSize = DtsUtil.getDtsFrameSize(frameData);
@ -317,7 +317,7 @@ public final class DtsReader implements ElementaryStreamReader {
.setChannelCount(dtsHeader.channelCount)
.setSampleRate(dtsHeader.sampleRate)
.setLanguage(language)
.setRoleFlags(TsUtil.parseRoleFlagsFromAudioType(audioType))
.setRoleFlags(roleFlags)
.build();
output.format(format);
}

View File

@ -49,7 +49,7 @@ public final class LatmReader implements ElementaryStreamReader {
private static final int SYNC_BYTE_SECOND = 0xE0;
@Nullable private final String language;
@TsUtil.AudioType private final int audioType;
private final @C.RoleFlags int roleFlags;
private final ParsableByteArray sampleDataBuffer;
private final ParsableBitArray sampleBitArray;
@ -79,11 +79,11 @@ public final class LatmReader implements ElementaryStreamReader {
/**
* @param language Track language.
* @param audioType Track audio type.
* @param roleFlags Track role flags.
*/
public LatmReader(@Nullable String language, @TsUtil.AudioType int audioType) {
public LatmReader(@Nullable String language, @C.RoleFlags int roleFlags) {
this.language = language;
this.audioType = audioType;
this.roleFlags = roleFlags;
sampleDataBuffer = new ParsableByteArray(INITIAL_BUFFER_SIZE);
sampleBitArray = new ParsableBitArray(sampleDataBuffer.getData());
timeUs = C.TIME_UNSET;
@ -220,7 +220,7 @@ public final class LatmReader implements ElementaryStreamReader {
.setSampleRate(sampleRateHz)
.setInitializationData(Collections.singletonList(initData))
.setLanguage(language)
.setRoleFlags(TsUtil.parseRoleFlagsFromAudioType(audioType))
.setRoleFlags(roleFlags)
.build();
if (!format.equals(this.format)) {
this.format = format;

View File

@ -44,7 +44,7 @@ public final class MpegAudioReader implements ElementaryStreamReader {
private final ParsableByteArray headerScratch;
private final MpegAudioUtil.Header header;
@Nullable private final String language;
@TsUtil.AudioType private final int audioType;
private final @C.RoleFlags int roleFlags;
private @MonotonicNonNull TrackOutput output;
private @MonotonicNonNull String formatId;
@ -64,10 +64,10 @@ public final class MpegAudioReader implements ElementaryStreamReader {
private long timeUs;
public MpegAudioReader() {
this(null, TsUtil.AUDIO_TYPE_UNDEFINED);
this(null, /* roleFlags= */ 0);
}
public MpegAudioReader(@Nullable String language, @TsUtil.AudioType int audioType) {
public MpegAudioReader(@Nullable String language, @C.RoleFlags int roleFlags) {
state = STATE_FINDING_HEADER;
// The first byte of an MPEG Audio frame header is always 0xFF.
headerScratch = new ParsableByteArray(4);
@ -75,7 +75,7 @@ public final class MpegAudioReader implements ElementaryStreamReader {
header = new MpegAudioUtil.Header();
timeUs = C.TIME_UNSET;
this.language = language;
this.audioType = audioType;
this.roleFlags = roleFlags;
}
@Override
@ -202,7 +202,7 @@ public final class MpegAudioReader implements ElementaryStreamReader {
.setChannelCount(header.channels)
.setSampleRate(header.sampleRate)
.setLanguage(language)
.setRoleFlags(TsUtil.parseRoleFlagsFromAudioType(audioType))
.setRoleFlags(roleFlags)
.build();
output.format(format);
hasOutputFormat = true;

View File

@ -15,6 +15,7 @@
*/
package androidx.media3.extractor.ts;
import static androidx.media3.extractor.ts.TsPayloadReader.EsInfo.AUDIO_TYPE_UNDEFINED;
import static androidx.media3.extractor.ts.TsPayloadReader.FLAG_PAYLOAD_UNIT_START_INDICATOR;
import static java.lang.annotation.ElementType.TYPE_USE;
@ -753,8 +754,7 @@ public final class TsExtractor implements Extractor {
// Setup an ID3 track regardless of whether there's a corresponding entry, in case one
// appears intermittently during playback. See [Internal: b/20261500].
EsInfo id3EsInfo =
new EsInfo(
TS_STREAM_TYPE_ID3, null, TsUtil.AUDIO_TYPE_UNDEFINED, null, Util.EMPTY_BYTE_ARRAY);
new EsInfo(TS_STREAM_TYPE_ID3, null, AUDIO_TYPE_UNDEFINED, null, Util.EMPTY_BYTE_ARRAY);
id3Reader = payloadReaderFactory.createPayloadReader(TS_STREAM_TYPE_ID3, id3EsInfo);
if (id3Reader != null) {
id3Reader.init(
@ -844,7 +844,7 @@ public final class TsExtractor implements Extractor {
int descriptorsEndPosition = descriptorsStartPosition + length;
int streamType = -1;
String language = null;
int audioType = TsUtil.AUDIO_TYPE_UNDEFINED;
@EsInfo.AudioType int audioType = AUDIO_TYPE_UNDEFINED;
List<DvbSubtitleInfo> dvbSubtitleInfos = null;
while (data.getPosition() < descriptorsEndPosition) {
int descriptorTag = data.readUnsignedByte();

View File

@ -16,10 +16,12 @@
package androidx.media3.extractor.ts;
import static java.lang.annotation.ElementType.TYPE_USE;
import static java.lang.annotation.RetentionPolicy.SOURCE;
import android.util.SparseArray;
import androidx.annotation.IntDef;
import androidx.annotation.Nullable;
import androidx.media3.common.C;
import androidx.media3.common.ParserException;
import androidx.media3.common.util.ParsableByteArray;
import androidx.media3.common.util.TimestampAdjuster;
@ -65,9 +67,48 @@ public interface TsPayloadReader {
/** Holds information associated with a PMT entry. */
final class EsInfo {
/**
* The audio type of the stream, as defined by ISO/IEC 13818-1, section 2.6.18.
*
* <p>One of {@link #AUDIO_TYPE_UNDEFINED}, {@link #AUDIO_TYPE_CLEAN_EFFECTS}, {@link
* #AUDIO_TYPE_HEARING_IMPAIRED} or {@link #AUDIO_TYPE_VISUAL_IMPAIRED_COMMENTARY}.
*/
@Documented
@Retention(SOURCE)
@Target(TYPE_USE)
@IntDef({
AUDIO_TYPE_UNDEFINED,
AUDIO_TYPE_CLEAN_EFFECTS,
AUDIO_TYPE_HEARING_IMPAIRED,
AUDIO_TYPE_VISUAL_IMPAIRED_COMMENTARY
})
public @interface AudioType {}
public static final int AUDIO_TYPE_UNDEFINED = 0;
/** Indicates the track has no language. */
public static final int AUDIO_TYPE_CLEAN_EFFECTS = 1;
/** Indicates the track is prepared for the hearing impaired. */
public static final int AUDIO_TYPE_HEARING_IMPAIRED = 2;
/** Indicates the track is prepared for the visually impaired viewer. */
public static final int AUDIO_TYPE_VISUAL_IMPAIRED_COMMENTARY = 3;
public @C.RoleFlags int getRoleFlags() {
switch (audioType) {
case AUDIO_TYPE_HEARING_IMPAIRED:
return C.ROLE_FLAG_ENHANCED_DIALOG_INTELLIGIBILITY;
case AUDIO_TYPE_VISUAL_IMPAIRED_COMMENTARY:
return C.ROLE_FLAG_DESCRIBES_VIDEO;
default:
return 0;
}
}
public final int streamType;
@Nullable public final String language;
@TsUtil.AudioType public final int audioType;
public final @AudioType int audioType;
public final List<DvbSubtitleInfo> dvbSubtitleInfos;
public final byte[] descriptorBytes;
@ -82,7 +123,7 @@ public interface TsPayloadReader {
public EsInfo(
int streamType,
@Nullable String language,
@TsUtil.AudioType int audioType,
@AudioType int audioType,
@Nullable List<DvbSubtitleInfo> dvbSubtitleInfos,
byte[] descriptorBytes) {
this.streamType = streamType;

View File

@ -16,60 +16,15 @@
package androidx.media3.extractor.ts;
import static java.lang.annotation.ElementType.TYPE_USE;
import static java.lang.annotation.RetentionPolicy.SOURCE;
import androidx.annotation.IntDef;
import androidx.media3.common.C;
import androidx.media3.common.util.ParsableByteArray;
import androidx.media3.common.util.UnstableApi;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
/** Utilities method for extracting MPEG-TS streams. */
@UnstableApi
public final class TsUtil {
/**
* The audio type of the stream, as defined by ISO/IEC 13818-1, section 2.6.18.
*
* <p>One of {@link #AUDIO_TYPE_UNDEFINED}, {@link #AUDIO_TYPE_CLEAN_EFFECTS}, {@link
* #AUDIO_TYPE_HEARING_IMPAIRED} or {@link #AUDIO_TYPE_VISUAL_IMPAIRED_COMMENTARY}.
*/
@Documented
@Retention(SOURCE)
@Target(TYPE_USE)
@IntDef({
AUDIO_TYPE_UNDEFINED,
AUDIO_TYPE_CLEAN_EFFECTS,
AUDIO_TYPE_HEARING_IMPAIRED,
AUDIO_TYPE_VISUAL_IMPAIRED_COMMENTARY
})
public @interface AudioType {}
public static final int AUDIO_TYPE_UNDEFINED = 0;
/** Indicates the track has no language. */
public static final int AUDIO_TYPE_CLEAN_EFFECTS = 1;
/** Indicates the track is prepared for the hearing impaired. */
public static final int AUDIO_TYPE_HEARING_IMPAIRED = 2;
/** Indicates the track is prepared for the visually impaired viewer. */
public static final int AUDIO_TYPE_VISUAL_IMPAIRED_COMMENTARY = 3;
public static @C.RoleFlags int parseRoleFlagsFromAudioType(@AudioType int audioType) {
switch (audioType) {
case AUDIO_TYPE_HEARING_IMPAIRED:
return C.ROLE_FLAG_ENHANCED_DIALOG_INTELLIGIBILITY;
case AUDIO_TYPE_VISUAL_IMPAIRED_COMMENTARY:
return C.ROLE_FLAG_DESCRIBES_VIDEO;
default:
return 0;
}
}
/**
* Returns whether a TS packet starts at {@code searchPosition} according to the MPEG-TS
* synchronization recommendations.