Skip to content

Commit

Permalink
Format.Builder: Finish migration
Browse files Browse the repository at this point in the history
Issue: #5978
PiperOrigin-RevId: 297876336
  • Loading branch information
ojw28 committed Feb 28, 2020
1 parent 0649c7b commit 67b29bb
Show file tree
Hide file tree
Showing 49 changed files with 391 additions and 465 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -266,22 +266,16 @@ private static FlacBinarySearchSeeker outputSeekMap(
private static void outputFormat(
FlacStreamMetadata streamMetadata, @Nullable Metadata metadata, TrackOutput output) {
Format mediaFormat =
Format.createAudioSampleFormat(
/* id= */ null,
MimeTypes.AUDIO_RAW,
/* codecs= */ null,
streamMetadata.getDecodedBitrate(),
streamMetadata.getMaxDecodedFrameSize(),
streamMetadata.channels,
streamMetadata.sampleRate,
getPcmEncoding(streamMetadata.bitsPerSample),
/* encoderDelay= */ 0,
/* encoderPadding= */ 0,
/* initializationData= */ null,
/* drmInitData= */ null,
/* selectionFlags= */ 0,
/* language= */ null,
metadata);
new Format.Builder()
.setSampleMimeType(MimeTypes.AUDIO_RAW)
.setAverageBitrate(streamMetadata.getDecodedBitrate())
.setPeakBitrate(streamMetadata.getDecodedBitrate())
.setMaxInputSize(streamMetadata.getMaxDecodedFrameSize())
.setChannelCount(streamMetadata.channels)
.setSampleRate(streamMetadata.sampleRate)
.setPcmEncoding(getPcmEncoding(streamMetadata.bitsPerSample))
.setMetadata(metadata)
.build();
output.format(mediaFormat);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1473,6 +1473,7 @@ public Format copyWithVideoSize(int width, int height) {
return buildUpon().setWidth(width).setHeight(height).build();
}

/** Returns a copy of this format with the specified {@link #exoMediaCryptoType}. */
public Format copyWithExoMediaCryptoType(
@Nullable Class<? extends ExoMediaCrypto> exoMediaCryptoType) {
return buildUpon().setExoMediaCryptoType(exoMediaCryptoType).build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,18 +163,14 @@ public static Format parseAc3AnnexFFormat(
if ((nextByte & 0x04) != 0) { // lfeon
channelCount++;
}
return Format.createAudioSampleFormat(
trackId,
MimeTypes.AUDIO_AC3,
/* codecs= */ null,
Format.NO_VALUE,
Format.NO_VALUE,
channelCount,
sampleRate,
/* initializationData= */ null,
drmInitData,
/* selectionFlags= */ 0,
language);
return new Format.Builder()
.setId(trackId)
.setSampleMimeType(MimeTypes.AUDIO_AC3)
.setChannelCount(channelCount)
.setSampleRate(sampleRate)
.setDrmInitData(drmInitData)
.setLanguage(language)
.build();
}

/**
Expand Down Expand Up @@ -218,18 +214,14 @@ public static Format parseEAc3AnnexFFormat(
mimeType = MimeTypes.AUDIO_E_AC3_JOC;
}
}
return Format.createAudioSampleFormat(
trackId,
mimeType,
/* codecs= */ null,
Format.NO_VALUE,
Format.NO_VALUE,
channelCount,
sampleRate,
/* initializationData= */ null,
drmInitData,
/* selectionFlags= */ 0,
language);
return new Format.Builder()
.setId(trackId)
.setSampleMimeType(mimeType)
.setChannelCount(channelCount)
.setSampleRate(sampleRate)
.setDrmInitData(drmInitData)
.setLanguage(language)
.build();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,18 +104,14 @@ public static Format parseAc4AnnexEFormat(
ParsableByteArray data, String trackId, String language, @Nullable DrmInitData drmInitData) {
data.skipBytes(1); // ac4_dsi_version, bitstream_version[0:5]
int sampleRate = ((data.readUnsignedByte() & 0x20) >> 5 == 1) ? 48000 : 44100;
return Format.createAudioSampleFormat(
trackId,
MimeTypes.AUDIO_AC4,
/* codecs= */ null,
/* bitrate= */ Format.NO_VALUE,
/* maxInputSize= */ Format.NO_VALUE,
CHANNEL_COUNT_2,
sampleRate,
/* initializationData= */ null,
drmInitData,
/* selectionFlags= */ 0,
language);
return new Format.Builder()
.setId(trackId)
.setSampleMimeType(MimeTypes.AUDIO_AC4)
.setChannelCount(CHANNEL_COUNT_2)
.setSampleRate(sampleRate)
.setDrmInitData(drmInitData)
.setLanguage(language)
.build();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,15 @@ public static Format parseDtsFormat(
: TWICE_BITRATE_KBPS_BY_RATE[rate] * 1000 / 2;
frameBits.skipBits(10); // MIX, DYNF, TIMEF, AUXF, HDCD, EXT_AUDIO_ID, EXT_AUDIO, ASPF
channelCount += frameBits.readBits(2) > 0 ? 1 : 0; // LFF
return Format.createAudioSampleFormat(trackId, MimeTypes.AUDIO_DTS, null, bitrate,
Format.NO_VALUE, channelCount, sampleRate, null, drmInitData, 0, language);
return new Format.Builder()
.setId(trackId)
.setSampleMimeType(MimeTypes.AUDIO_DTS)
.setAverageBitrate(bitrate)
.setChannelCount(channelCount)
.setSampleRate(sampleRate)
.setDrmInitData(drmInitData)
.setLanguage(language)
.build();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ public final class EventMessage implements Metadata.Entry {
@VisibleForTesting public static final String SCTE35_SCHEME_ID = "urn:scte:scte35:2014:bin";

private static final Format ID3_FORMAT =
Format.createSampleFormat(/* id= */ null, MimeTypes.APPLICATION_ID3);
new Format.Builder().setSampleMimeType(MimeTypes.APPLICATION_ID3).build();
private static final Format SCTE35_FORMAT =
Format.createSampleFormat(/* id= */ null, MimeTypes.APPLICATION_SCTE35);
new Format.Builder().setSampleMimeType(MimeTypes.APPLICATION_SCTE35).build();

/** The message scheme. */
public final String schemeIdUri;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,11 @@ protected final int readSource(
} else if (result == C.RESULT_FORMAT_READ) {
Format format = formatHolder.format;
if (format.subsampleOffsetUs != Format.OFFSET_SAMPLE_RELATIVE) {
format = format.copyWithSubsampleOffsetUs(format.subsampleOffsetUs + streamOffsetUs);
format =
format
.buildUpon()
.setSubsampleOffsetUs(format.subsampleOffsetUs + streamOffsetUs)
.build();
formatHolder.format = format;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ interface Listener {
private static final Map<String, String> ICY_METADATA_HEADERS = createIcyMetadataHeaders();

private static final Format ICY_FORMAT =
Format.createSampleFormat("icy", MimeTypes.APPLICATION_ICY);
new Format.Builder().setId("icy").setSampleMimeType(MimeTypes.APPLICATION_ICY).build();

private final Uri uri;
private final DataSource dataSource;
Expand Down Expand Up @@ -716,20 +716,21 @@ private void maybeFinishPrepare() {
boolean[] trackIsAudioVideoFlags = new boolean[trackCount];
for (int i = 0; i < trackCount; i++) {
Format trackFormat = Assertions.checkNotNull(sampleQueues[i].getUpstreamFormat());
String mimeType = trackFormat.sampleMimeType;
@Nullable String mimeType = trackFormat.sampleMimeType;
boolean isAudio = MimeTypes.isAudio(mimeType);
boolean isAudioVideo = isAudio || MimeTypes.isVideo(mimeType);
trackIsAudioVideoFlags[i] = isAudioVideo;
haveAudioVideoTracks |= isAudioVideo;
IcyHeaders icyHeaders = this.icyHeaders;
@Nullable IcyHeaders icyHeaders = this.icyHeaders;
if (icyHeaders != null) {
if (isAudio || sampleQueueTrackIds[i].isIcyTrack) {
Metadata metadata = trackFormat.metadata;
trackFormat =
trackFormat.copyWithMetadata(
metadata == null
? new Metadata(icyHeaders)
: metadata.copyWithAppendedEntries(icyHeaders));
@Nullable Metadata metadata = trackFormat.metadata;
if (metadata == null) {
metadata = new Metadata(icyHeaders);
} else {
metadata = metadata.copyWithAppendedEntries(icyHeaders);
}
trackFormat = trackFormat.buildUpon().setMetadata(metadata).build();
}
// Update the track format with the bitrate from the ICY header only if it declares neither
// an average or peak bitrate of its own.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,11 @@ protected final void invalidateUpstreamFormatAdjustment() {
@CallSuper
protected Format getAdjustedUpstreamFormat(Format format) {
if (sampleOffsetUs != 0 && format.subsampleOffsetUs != Format.OFFSET_SAMPLE_RELATIVE) {
format = format.copyWithSubsampleOffsetUs(format.subsampleOffsetUs + sampleOffsetUs);
format =
format
.buildUpon()
.setSubsampleOffsetUs(format.subsampleOffsetUs + sampleOffsetUs)
.build();
}
return format;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,17 @@
public final class SilenceMediaSource extends BaseMediaSource {

private static final int SAMPLE_RATE_HZ = 44100;
@C.PcmEncoding private static final int ENCODING = C.ENCODING_PCM_16BIT;
@C.PcmEncoding private static final int PCM_ENCODING = C.ENCODING_PCM_16BIT;
private static final int CHANNEL_COUNT = 2;
private static final Format FORMAT =
Format.createAudioSampleFormat(
/* id=*/ null,
MimeTypes.AUDIO_RAW,
/* codecs= */ null,
/* bitrate= */ Format.NO_VALUE,
/* maxInputSize= */ Format.NO_VALUE,
CHANNEL_COUNT,
SAMPLE_RATE_HZ,
ENCODING,
/* initializationData= */ null,
/* drmInitData= */ null,
/* selectionFlags= */ 0,
/* language= */ null);
new Format.Builder()
.setSampleMimeType(MimeTypes.AUDIO_RAW)
.setChannelCount(CHANNEL_COUNT)
.setSampleRate(SAMPLE_RATE_HZ)
.setPcmEncoding(PCM_ENCODING)
.build();
private static final byte[] SILENCE_SAMPLE =
new byte[Util.getPcmFrameSize(ENCODING, CHANNEL_COUNT) * 1024];
new byte[Util.getPcmFrameSize(PCM_ENCODING, CHANNEL_COUNT) * 1024];

private final long durationUs;

Expand Down Expand Up @@ -243,11 +236,11 @@ public int skipData(long positionUs) {

private static long getAudioByteCount(long durationUs) {
long audioSampleCount = durationUs * SAMPLE_RATE_HZ / C.MICROS_PER_SECOND;
return Util.getPcmFrameSize(ENCODING, CHANNEL_COUNT) * audioSampleCount;
return Util.getPcmFrameSize(PCM_ENCODING, CHANNEL_COUNT) * audioSampleCount;
}

private static long getAudioPositionUs(long bytes) {
long audioSampleCount = bytes / Util.getPcmFrameSize(ENCODING, CHANNEL_COUNT);
long audioSampleCount = bytes / Util.getPcmFrameSize(PCM_ENCODING, CHANNEL_COUNT);
return audioSampleCount * C.MICROS_PER_SECOND / SAMPLE_RATE_HZ;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@
@RunWith(AndroidJUnit4.class)
public class SimpleDecoderAudioRendererTest {

private static final Format FORMAT = Format.createSampleFormat(null, MimeTypes.AUDIO_RAW);
private static final Format FORMAT =
new Format.Builder().setSampleMimeType(MimeTypes.AUDIO_RAW).build();

@Mock private AudioSink mockAudioSink;
private SimpleDecoderAudioRenderer audioRenderer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public class MetadataRendererTest {
0x00, 0x00, 0x00, 0x00)); // CRC_32 (ignored, check happens at extraction).

private static final Format EMSG_FORMAT =
Format.createSampleFormat(null, MimeTypes.APPLICATION_EMSG);
new Format.Builder().setSampleMimeType(MimeTypes.APPLICATION_EMSG).build();

private final EventMessageEncoder eventMessageEncoder = new EventMessageEncoder();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -454,40 +454,25 @@ public void onPrepareError(DownloadHelper helper, IOException e) {
}

private static Format createVideoFormat(int bitrate) {
return Format.createVideoSampleFormat(
/* id= */ null,
/* sampleMimeType= */ MimeTypes.VIDEO_H264,
/* codecs= */ null,
/* bitrate= */ bitrate,
/* maxInputSize= */ Format.NO_VALUE,
/* width= */ 480,
/* height= */ 360,
/* frameRate= */ Format.NO_VALUE,
/* initializationData= */ null,
/* drmInitData= */ null);
return new Format.Builder()
.setSampleMimeType(MimeTypes.VIDEO_H264)
.setAverageBitrate(bitrate)
.build();
}

private static Format createAudioFormat(String language) {
return Format.createAudioSampleFormat(
/* id= */ null,
/* sampleMimeType= */ MimeTypes.AUDIO_AAC,
/* codecs= */ null,
/* bitrate= */ 48000,
/* maxInputSize= */ Format.NO_VALUE,
/* channelCount= */ 2,
/* sampleRate */ 44100,
/* initializationData= */ null,
/* drmInitData= */ null,
/* selectionFlags= */ C.SELECTION_FLAG_DEFAULT,
/* language= */ language);
return new Format.Builder()
.setSampleMimeType(MimeTypes.AUDIO_AAC)
.setLanguage(language)
.build();
}

private static Format createTextFormat(String language) {
return Format.createTextSampleFormat(
/* id= */ null,
/* sampleMimeType= */ MimeTypes.TEXT_VTT,
/* selectionFlags= */ C.SELECTION_FLAG_DEFAULT,
/* language= */ language);
return new Format.Builder()
.setSampleMimeType(MimeTypes.TEXT_VTT)
.setSelectionFlags(C.SELECTION_FLAG_DEFAULT)
.setLanguage(language)
.build();
}

private static void assertSingleTrackSelectionEquals(
Expand Down
Loading

0 comments on commit 67b29bb

Please sign in to comment.