Export variant codecs in HLS

Issue:#1772

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=134073672
This commit is contained in:
aquilescanta 2016-09-23 07:56:25 -07:00 committed by Oliver Woodman
parent 8cf107408d
commit 94cc606091
3 changed files with 83 additions and 9 deletions

View file

@ -380,7 +380,7 @@ public final class Format implements Parcelable {
selectionFlags, language, subsampleOffsetUs, initializationData, drmInitData);
}
public Format copyWithContainerInfo(String id, int bitrate, int width, int height,
public Format copyWithContainerInfo(String id, String codecs, int bitrate, int width, int height,
@C.SelectionFlags int selectionFlags, String language) {
return new Format(id, containerMimeType, sampleMimeType, codecs, bitrate, maxInputSize,
width, height, frameRate, rotationDegrees, pixelWidthHeightRatio, projectionData,

View file

@ -15,6 +15,7 @@
*/
package com.google.android.exoplayer2.source.hls;
import android.text.TextUtils;
import android.util.SparseArray;
import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.Format;
@ -567,7 +568,7 @@ import java.util.LinkedList;
if (i == primaryExtractorTrackIndex) {
Format[] formats = new Format[chunkSourceTrackCount];
for (int j = 0; j < chunkSourceTrackCount; j++) {
formats[j] = getSampleFormat(chunkSourceTrackGroup.getFormat(j), sampleFormat);
formats[j] = deriveFormat(chunkSourceTrackGroup.getFormat(j), sampleFormat);
}
trackGroups[i] = new TrackGroup(formats);
primaryTrackGroupIndex = i;
@ -580,7 +581,7 @@ import java.util.LinkedList;
trackFormat = muxedCaptionFormat;
}
}
trackGroups[i] = new TrackGroup(getSampleFormat(trackFormat, sampleFormat));
trackGroups[i] = new TrackGroup(deriveFormat(trackFormat, sampleFormat));
}
}
this.trackGroups = new TrackGroupArray(trackGroups);
@ -599,18 +600,25 @@ import java.util.LinkedList;
}
/**
* Derives a sample format corresponding to a given container format, by combining it with sample
* level information obtained from a second sample format.
* Derives a track format corresponding to a given container format, by combining it with sample
* level information obtained from the samples.
*
* @param containerFormat The container format for which the sample format should be derived.
* @param containerFormat The container format for which the track format should be derived.
* @param sampleFormat A sample format from which to obtain sample level information.
* @return The derived sample format.
* @return The derived track format.
*/
private static Format getSampleFormat(Format containerFormat, Format sampleFormat) {
private static Format deriveFormat(Format containerFormat, Format sampleFormat) {
if (containerFormat == null) {
return sampleFormat;
}
return sampleFormat.copyWithContainerInfo(containerFormat.id, containerFormat.bitrate,
String codecs = null;
int sampleTrackType = MimeTypes.getTrackType(sampleFormat.sampleMimeType);
if (sampleTrackType == C.TRACK_TYPE_AUDIO) {
codecs = getAudioCodecs(containerFormat.codecs);
} else if (sampleTrackType == C.TRACK_TYPE_VIDEO) {
codecs = getVideoCodecs(containerFormat.codecs);
}
return sampleFormat.copyWithContainerInfo(containerFormat.id, codecs, containerFormat.bitrate,
containerFormat.width, containerFormat.height, containerFormat.selectionFlags,
containerFormat.language);
}
@ -623,4 +631,29 @@ import java.util.LinkedList;
return pendingResetPositionUs != C.TIME_UNSET;
}
private static String getAudioCodecs(String codecs) {
return getCodecsOfType(codecs, C.TRACK_TYPE_AUDIO);
}
private static String getVideoCodecs(String codecs) {
return getCodecsOfType(codecs, C.TRACK_TYPE_VIDEO);
}
private static String getCodecsOfType(String codecs, int trackType) {
if (TextUtils.isEmpty(codecs)) {
return null;
}
String[] codecArray = codecs.split("(\\s*,\\s*)|(\\s*$)");
StringBuilder builder = new StringBuilder();
for (String codec : codecArray) {
if (trackType == MimeTypes.getTrackTypeOfCodec(codec)) {
if (builder.length() > 0) {
builder.append(",");
}
builder.append(codec);
}
}
return builder.length() > 0 ? builder.toString() : null;
}
}

View file

@ -15,6 +15,9 @@
*/
package com.google.android.exoplayer2.util;
import android.text.TextUtils;
import com.google.android.exoplayer2.C;
/**
* Defines common MIME types and helper methods.
*/
@ -191,6 +194,44 @@ public final class MimeTypes {
return null;
}
/**
* Returns the {@link C}{@code .TRACK_TYPE_*} constant that corresponds to a specified mime type.
* {@link C#TRACK_TYPE_UNKNOWN} if the mime type is not known or the mapping cannot be
* established.
*
* @param mimeType The mimeType.
* @return The {@link C}{@code .TRACK_TYPE_*} constant that corresponds to a specified mime type.
*/
public static int getTrackType(String mimeType) {
if (TextUtils.isEmpty(mimeType)) {
return C.TRACK_TYPE_UNKNOWN;
} else if (isAudio(mimeType)) {
return C.TRACK_TYPE_AUDIO;
} else if (isVideo(mimeType)) {
return C.TRACK_TYPE_VIDEO;
} else if (isText(mimeType) || APPLICATION_EIA608.equals(mimeType)
|| APPLICATION_SUBRIP.equals(mimeType) || APPLICATION_TTML.equals(mimeType)
|| APPLICATION_TX3G.equals(mimeType) || APPLICATION_MP4VTT.equals(mimeType)
|| APPLICATION_RAWCC.equals(mimeType) || APPLICATION_VOBSUB.equals(mimeType)
|| APPLICATION_PGS.equals(mimeType)) {
return C.TRACK_TYPE_TEXT;
} else if (APPLICATION_ID3.equals(mimeType)) {
return C.TRACK_TYPE_METADATA;
} else {
return C.TRACK_TYPE_UNKNOWN;
}
}
/**
* Equivalent to {@code getTrackType(getMediaMimeType(codec))}.
*
* @param codec The codec.
* @return The {@link C}{@code .TRACK_TYPE_*} constant that corresponds to a specified codec.
*/
public static int getTrackTypeOfCodec(String codec) {
return getTrackType(getMediaMimeType(codec));
}
/**
* Returns the top-level type of {@code mimeType}.
*