Skip to content

Commit

Permalink
Migrate HLS to Format.Builder
Browse files Browse the repository at this point in the history
Issue: #2863
PiperOrigin-RevId: 296482726
  • Loading branch information
ojw28 committed Feb 25, 2020
1 parent f342df2 commit 6fc5e6b
Show file tree
Hide file tree
Showing 8 changed files with 171 additions and 275 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1347,39 +1347,6 @@ public Format copyWithLabel(@Nullable String label) {
return buildUpon().setLabel(label).build();
}

// TODO: Inline into HlsSampleStreamWrapper and remove.
public Format copyWithContainerInfo(
@Nullable String id,
@Nullable String label,
@Nullable String sampleMimeType,
@Nullable String codecs,
@Nullable Metadata metadata,
int bitrate,
int width,
int height,
int channelCount,
@C.SelectionFlags int selectionFlags,
@Nullable String language) {
if (this.metadata != null) {
metadata = this.metadata.copyWithAppendedEntriesFrom(metadata);
}

return buildUpon()
.setId(id)
.setLabel(label)
.setLanguage(language)
.setSelectionFlags(selectionFlags)
.setAverageBitrate(bitrate)
.setPeakBitrate(bitrate)
.setMetadata(metadata)
.setCodecs(codecs)
.setSampleMimeType(sampleMimeType)
.setWidth(width)
.setHeight(height)
.setChannelCount(channelCount)
.build();
}

/** @deprecated Use {@link #withManifestFormatInfo(Format)}. */
@Deprecated
public Format copyWithManifestFormatInfo(Format manifestFormat) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,15 +235,11 @@ private static TsExtractor createTsExtractor(
// closed caption track on channel 0.
muxedCaptionFormats =
Collections.singletonList(
Format.createTextSampleFormat(
/* id= */ null,
MimeTypes.APPLICATION_CEA608,
/* selectionFlags= */ 0,
/* language= */ null));
new Format.Builder().setSampleMimeType(MimeTypes.APPLICATION_CEA608).build());
} else {
muxedCaptionFormats = Collections.emptyList();
}
String codecs = format.codecs;
@Nullable String codecs = format.codecs;
if (!TextUtils.isEmpty(codecs)) {
// Sometimes AAC and H264 streams are declared in TS chunks even though they don't really
// exist. If we know from the codec attribute that they don't exist, then we can
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,11 @@ private void buildAndPrepareMainSampleStreamWrapper(
}

TrackGroup id3TrackGroup =
new TrackGroup(Format.createSampleFormat(/* id= */ "ID3", MimeTypes.APPLICATION_ID3));
new TrackGroup(
new Format.Builder()
.setId("ID3")
.setSampleMimeType(MimeTypes.APPLICATION_ID3)
.build());
muxedTrackGroups.add(id3TrackGroup);

sampleStreamWrapper.prepareWithMasterPlaylistInfo(
Expand Down Expand Up @@ -785,33 +789,34 @@ private static Map<String, DrmInitData> deriveOverridingDrmInitData(
}

private static Format deriveVideoFormat(Format variantFormat) {
String codecs = Util.getCodecsOfType(variantFormat.codecs, C.TRACK_TYPE_VIDEO);
String sampleMimeType = MimeTypes.getMediaMimeType(codecs);
return Format.createVideoContainerFormat(
variantFormat.id,
variantFormat.label,
variantFormat.containerMimeType,
sampleMimeType,
codecs,
variantFormat.metadata,
variantFormat.bitrate,
variantFormat.width,
variantFormat.height,
variantFormat.frameRate,
/* initializationData= */ null,
variantFormat.selectionFlags,
variantFormat.roleFlags);
@Nullable String codecs = Util.getCodecsOfType(variantFormat.codecs, C.TRACK_TYPE_VIDEO);
@Nullable String sampleMimeType = MimeTypes.getMediaMimeType(codecs);
return new Format.Builder()
.setId(variantFormat.id)
.setLabel(variantFormat.label)
.setContainerMimeType(variantFormat.containerMimeType)
.setSampleMimeType(sampleMimeType)
.setCodecs(codecs)
.setMetadata(variantFormat.metadata)
.setAverageBitrate(variantFormat.averageBitrate)
.setPeakBitrate(variantFormat.peakBitrate)
.setWidth(variantFormat.width)
.setHeight(variantFormat.height)
.setFrameRate(variantFormat.frameRate)
.setSelectionFlags(variantFormat.selectionFlags)
.setRoleFlags(variantFormat.roleFlags)
.build();
}

private static Format deriveAudioFormat(
Format variantFormat, @Nullable Format mediaTagFormat, boolean isPrimaryTrackInVariant) {
String codecs;
Metadata metadata;
@Nullable String codecs;
@Nullable Metadata metadata;
int channelCount = Format.NO_VALUE;
int selectionFlags = 0;
int roleFlags = 0;
String language = null;
String label = null;
@Nullable String language = null;
@Nullable String label = null;
if (mediaTagFormat != null) {
codecs = mediaTagFormat.codecs;
metadata = mediaTagFormat.metadata;
Expand All @@ -831,22 +836,23 @@ private static Format deriveAudioFormat(
label = variantFormat.label;
}
}
String sampleMimeType = MimeTypes.getMediaMimeType(codecs);
int bitrate = isPrimaryTrackInVariant ? variantFormat.bitrate : Format.NO_VALUE;
return Format.createAudioContainerFormat(
variantFormat.id,
label,
variantFormat.containerMimeType,
sampleMimeType,
codecs,
metadata,
bitrate,
channelCount,
/* sampleRate= */ Format.NO_VALUE,
/* initializationData= */ null,
selectionFlags,
roleFlags,
language);
@Nullable String sampleMimeType = MimeTypes.getMediaMimeType(codecs);
int averageBitrate = isPrimaryTrackInVariant ? variantFormat.averageBitrate : Format.NO_VALUE;
int peakBitrate = isPrimaryTrackInVariant ? variantFormat.peakBitrate : Format.NO_VALUE;
return new Format.Builder()
.setId(variantFormat.id)
.setLabel(label)
.setContainerMimeType(variantFormat.containerMimeType)
.setSampleMimeType(sampleMimeType)
.setCodecs(codecs)
.setMetadata(metadata)
.setAverageBitrate(averageBitrate)
.setPeakBitrate(peakBitrate)
.setChannelCount(channelCount)
.setSelectionFlags(selectionFlags)
.setRoleFlags(roleFlags)
.setLanguage(language)
.build();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -1269,38 +1269,50 @@ private static int getTrackTypeScore(int trackType) {
*
* @param playlistFormat The format information obtained from the master playlist.
* @param sampleFormat The format information obtained from the samples.
* @param propagateBitrate Whether the bitrate from the playlist format should be included in the
* derived format.
* @param propagateBitrates Whether the bitrates from the playlist format should be included in
* the derived format.
* @return The derived track format.
*/
private static Format deriveFormat(
@Nullable Format playlistFormat, Format sampleFormat, boolean propagateBitrate) {
@Nullable Format playlistFormat, Format sampleFormat, boolean propagateBitrates) {
if (playlistFormat == null) {
return sampleFormat;
}
int bitrate = propagateBitrate ? playlistFormat.bitrate : Format.NO_VALUE;
int channelCount =
playlistFormat.channelCount != Format.NO_VALUE
? playlistFormat.channelCount
: sampleFormat.channelCount;

int sampleTrackType = MimeTypes.getTrackType(sampleFormat.sampleMimeType);
@Nullable String codecs = Util.getCodecsOfType(playlistFormat.codecs, sampleTrackType);
@Nullable String mimeType = MimeTypes.getMediaMimeType(codecs);
if (mimeType == null) {
mimeType = sampleFormat.sampleMimeType;
}
return sampleFormat.copyWithContainerInfo(
playlistFormat.id,
playlistFormat.label,
mimeType,
codecs,
playlistFormat.metadata,
bitrate,
playlistFormat.width,
playlistFormat.height,
channelCount,
playlistFormat.selectionFlags,
playlistFormat.language);
@Nullable String sampleMimeType = MimeTypes.getMediaMimeType(codecs);

Format.Builder formatBuilder =
sampleFormat
.buildUpon()
.setId(playlistFormat.id)
.setLabel(playlistFormat.label)
.setLanguage(playlistFormat.language)
.setSelectionFlags(playlistFormat.selectionFlags)
.setAverageBitrate(propagateBitrates ? playlistFormat.averageBitrate : Format.NO_VALUE)
.setPeakBitrate(propagateBitrates ? playlistFormat.peakBitrate : Format.NO_VALUE)
.setCodecs(codecs)
.setWidth(playlistFormat.width)
.setHeight(playlistFormat.height);

if (sampleMimeType != null) {
formatBuilder.setSampleMimeType(sampleMimeType);
}

if (playlistFormat.channelCount != Format.NO_VALUE) {
formatBuilder.setChannelCount(playlistFormat.channelCount);
}

if (playlistFormat.metadata != null) {
Metadata metadata = playlistFormat.metadata;
if (sampleFormat.metadata != null) {
metadata = sampleFormat.metadata.copyWithAppendedEntriesFrom(metadata);
}
formatBuilder.setMetadata(metadata);
}

return formatBuilder.build();
}

private static boolean isMediaChunk(Chunk chunk) {
Expand Down Expand Up @@ -1409,9 +1421,9 @@ private static class EmsgUnwrappingTrackOutput implements TrackOutput {

// TODO(ibaker): Create a Formats util class with common constants like this.
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 EMSG_FORMAT =
Format.createSampleFormat(/* id= */ null, MimeTypes.APPLICATION_EMSG);
new Format.Builder().setSampleMimeType(MimeTypes.APPLICATION_EMSG).build();

private final EventMessageDecoder emsgDecoder;
private final TrackOutput delegate;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,11 @@ private void processSample() throws ParserException {
private TrackOutput buildTrackOutput(long subsampleOffsetUs) {
TrackOutput trackOutput = output.track(0, C.TRACK_TYPE_TEXT);
trackOutput.format(
Format.createTextSampleFormat(
/* id= */ null, MimeTypes.TEXT_VTT, /* selectionFlags= */ 0, language)
.copyWithSubsampleOffsetUs(subsampleOffsetUs));
new Format.Builder()
.setSampleMimeType(MimeTypes.TEXT_VTT)
.setLanguage(language)
.setSubsampleOffsetUs(subsampleOffsetUs)
.build());
output.endTracks();
return trackOutput;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,16 +102,7 @@ public Variant(
*/
public static Variant createMediaPlaylistVariantUrl(Uri url) {
Format format =
Format.createContainerFormat(
"0",
/* label= */ null,
MimeTypes.APPLICATION_M3U8,
/* sampleMimeType= */ null,
/* codecs= */ null,
/* bitrate= */ Format.NO_VALUE,
/* selectionFlags= */ 0,
/* roleFlags= */ 0,
/* language= */ null);
new Format.Builder().setId("0").setContainerMimeType(MimeTypes.APPLICATION_M3U8).build();
return new Variant(
url,
format,
Expand Down
Loading

0 comments on commit 6fc5e6b

Please sign in to comment.