Commit 94cc6060 by aquilescanta Committed by Oliver Woodman

Export variant codecs in HLS

Issue:#1772

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=134073672
parent 8cf10740
...@@ -380,7 +380,7 @@ public final class Format implements Parcelable { ...@@ -380,7 +380,7 @@ public final class Format implements Parcelable {
selectionFlags, language, subsampleOffsetUs, initializationData, drmInitData); 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) { @C.SelectionFlags int selectionFlags, String language) {
return new Format(id, containerMimeType, sampleMimeType, codecs, bitrate, maxInputSize, return new Format(id, containerMimeType, sampleMimeType, codecs, bitrate, maxInputSize,
width, height, frameRate, rotationDegrees, pixelWidthHeightRatio, projectionData, width, height, frameRate, rotationDegrees, pixelWidthHeightRatio, projectionData,
......
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
*/ */
package com.google.android.exoplayer2.source.hls; package com.google.android.exoplayer2.source.hls;
import android.text.TextUtils;
import android.util.SparseArray; import android.util.SparseArray;
import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.Format; import com.google.android.exoplayer2.Format;
...@@ -567,7 +568,7 @@ import java.util.LinkedList; ...@@ -567,7 +568,7 @@ import java.util.LinkedList;
if (i == primaryExtractorTrackIndex) { if (i == primaryExtractorTrackIndex) {
Format[] formats = new Format[chunkSourceTrackCount]; Format[] formats = new Format[chunkSourceTrackCount];
for (int j = 0; j < chunkSourceTrackCount; j++) { 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); trackGroups[i] = new TrackGroup(formats);
primaryTrackGroupIndex = i; primaryTrackGroupIndex = i;
...@@ -580,7 +581,7 @@ import java.util.LinkedList; ...@@ -580,7 +581,7 @@ import java.util.LinkedList;
trackFormat = muxedCaptionFormat; trackFormat = muxedCaptionFormat;
} }
} }
trackGroups[i] = new TrackGroup(getSampleFormat(trackFormat, sampleFormat)); trackGroups[i] = new TrackGroup(deriveFormat(trackFormat, sampleFormat));
} }
} }
this.trackGroups = new TrackGroupArray(trackGroups); this.trackGroups = new TrackGroupArray(trackGroups);
...@@ -599,18 +600,25 @@ import java.util.LinkedList; ...@@ -599,18 +600,25 @@ import java.util.LinkedList;
} }
/** /**
* Derives a sample format corresponding to a given container format, by combining it with sample * Derives a track format corresponding to a given container format, by combining it with sample
* level information obtained from a second sample format. * 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. * @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) { if (containerFormat == null) {
return sampleFormat; 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.width, containerFormat.height, containerFormat.selectionFlags,
containerFormat.language); containerFormat.language);
} }
...@@ -623,4 +631,29 @@ import java.util.LinkedList; ...@@ -623,4 +631,29 @@ import java.util.LinkedList;
return pendingResetPositionUs != C.TIME_UNSET; 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;
}
} }
...@@ -15,6 +15,9 @@ ...@@ -15,6 +15,9 @@
*/ */
package com.google.android.exoplayer2.util; package com.google.android.exoplayer2.util;
import android.text.TextUtils;
import com.google.android.exoplayer2.C;
/** /**
* Defines common MIME types and helper methods. * Defines common MIME types and helper methods.
*/ */
...@@ -192,6 +195,44 @@ public final class MimeTypes { ...@@ -192,6 +195,44 @@ public final class MimeTypes {
} }
/** /**
* 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}. * Returns the top-level type of {@code mimeType}.
* *
* @param mimeType The mimeType whose top-level type is required. * @param mimeType The mimeType whose top-level type is required.
......
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