Skip to content

Commit

Permalink
Remove fallback codec lookup configuration flags (#115)
Browse files Browse the repository at this point in the history
  • Loading branch information
izzytwosheds authored Apr 28, 2021
1 parent 3c22196 commit 00391e6
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,12 @@

public final class MediaCodecDecoder implements Decoder {

private final boolean fallbackToGetCodecByType;
private final boolean filterByTypeAndFormat;

private MediaCodec mediaCodec;

private boolean isRunning;
private boolean isReleased;
private final MediaCodec.BufferInfo outputBufferInfo = new MediaCodec.BufferInfo();

public MediaCodecDecoder() {
this(true);
}

public MediaCodecDecoder(boolean fallbackToGetCodecByType) {
this(fallbackToGetCodecByType, false);
}

public MediaCodecDecoder(boolean fallbackToGetCodecByType, boolean filterByTypeAndFormat) {
this.fallbackToGetCodecByType = fallbackToGetCodecByType;
this.filterByTypeAndFormat = filterByTypeAndFormat;
}

@Override
public void init(@NonNull MediaFormat mediaFormat, @Nullable Surface surface) throws TrackTranscoderException {
mediaCodec = CodecUtils.getAndConfigureCodec(
Expand All @@ -53,9 +37,7 @@ public void init(@NonNull MediaFormat mediaFormat, @Nullable Surface surface) th
false,
TrackTranscoderException.Error.DECODER_NOT_FOUND,
TrackTranscoderException.Error.DECODER_FORMAT_NOT_FOUND,
TrackTranscoderException.Error.DECODER_CONFIGURATION_ERROR,
fallbackToGetCodecByType,
filterByTypeAndFormat);
TrackTranscoderException.Error.DECODER_CONFIGURATION_ERROR);
isReleased = mediaCodec == null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,29 +24,13 @@

public class MediaCodecEncoder implements Encoder {

private final boolean fallbackToGetCodecByType;
private final boolean filterByTypeAndFormat;

private MediaCodec mediaCodec;

private boolean isReleased = true;
private boolean isRunning;

private final MediaCodec.BufferInfo encoderOutputBufferInfo = new MediaCodec.BufferInfo();

public MediaCodecEncoder() {
this(true);
}

public MediaCodecEncoder(boolean fallbackToGetCodecByType) {
this(fallbackToGetCodecByType, false);
}

public MediaCodecEncoder(boolean fallbackToGetCodecByType, boolean filterByTypeAndFormat) {
this.fallbackToGetCodecByType = fallbackToGetCodecByType;
this.filterByTypeAndFormat = filterByTypeAndFormat;
}

@Override
public void init(@NonNull MediaFormat targetFormat) throws TrackTranscoderException {
// unless specified otherwise, we use default color format for the surface
Expand All @@ -60,9 +44,7 @@ public void init(@NonNull MediaFormat targetFormat) throws TrackTranscoderExcept
true,
TrackTranscoderException.Error.ENCODER_NOT_FOUND,
TrackTranscoderException.Error.ENCODER_FORMAT_NOT_FOUND,
TrackTranscoderException.Error.ENCODER_CONFIGURATION_ERROR,
fallbackToGetCodecByType,
filterByTypeAndFormat);
TrackTranscoderException.Error.ENCODER_CONFIGURATION_ERROR);
isReleased = mediaCodec == null;
}

Expand Down
61 changes: 18 additions & 43 deletions litr/src/main/java/com/linkedin/android/litr/utils/CodecUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,6 @@ && getProfileRank(mimeType, codecProfileLevel.profile) <= maxProfileRank) {
* @param codecNotFoundError message to provide in {@link TrackTranscoderException} if codec could not be found
* @param codecFormatNotFoundError message to provide in {@link TrackTranscoderException} if codec could not be found by format
* @param codecConfigurationError message to provide in {@link TrackTranscoderException} if codec could not configured
* @param fallbackToGetCodecByType flag indicating if older getCodecByType API should be used if getting by format fails
* @return configured instance of {@link MediaCodec}, or a {@link TrackTranscoderException} will be thrown
*/
@NonNull
Expand All @@ -183,18 +182,16 @@ public static MediaCodec getAndConfigureCodec(@NonNull MediaFormat mediaFormat,
boolean isEncoder,
@NonNull TrackTranscoderException.Error codecNotFoundError,
@NonNull TrackTranscoderException.Error codecFormatNotFoundError,
@NonNull TrackTranscoderException.Error codecConfigurationError,
boolean fallbackToGetCodecByType,
boolean filterByTypeAndFormat) throws TrackTranscoderException {
@NonNull TrackTranscoderException.Error codecConfigurationError) throws TrackTranscoderException {
MediaCodec mediaCodec;
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mediaCodec = getAndConfigureCodecByConfig(mediaFormat, surface, isEncoder, filterByTypeAndFormat);
mediaCodec = getAndConfigureCodecByConfig(mediaFormat, surface, isEncoder);
} else {
mediaCodec = getAndConfigureCodecByType(mediaFormat, surface, isEncoder, filterByTypeAndFormat);
mediaCodec = getAndConfigureCodecByType(mediaFormat, surface, isEncoder);
}
if (mediaCodec == null) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && fallbackToGetCodecByType) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
throw new IllegalStateException("Try fallbackToGetCodecByType");
} else {
throw new TrackTranscoderException(codecNotFoundError, mediaFormat, null, null);
Expand All @@ -203,9 +200,9 @@ public static MediaCodec getAndConfigureCodec(@NonNull MediaFormat mediaFormat,
return mediaCodec;
} catch (IOException | IllegalStateException e) {
Exception exception = e;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && fallbackToGetCodecByType) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP ) {
try {
mediaCodec = getAndConfigureCodecByType(mediaFormat, surface, isEncoder, filterByTypeAndFormat);
mediaCodec = getAndConfigureCodecByType(mediaFormat, surface, isEncoder);
if (mediaCodec == null) {
throw new TrackTranscoderException(codecNotFoundError, mediaFormat, null, null);
}
Expand All @@ -225,52 +222,30 @@ public static MediaCodec getAndConfigureCodec(@NonNull MediaFormat mediaFormat,
@Nullable
private static MediaCodec getAndConfigureCodecByType(@NonNull MediaFormat mediaFormat,
@Nullable Surface surface,
boolean isEncoder,
boolean filterByTypeAndFormat) throws IOException, IllegalStateException {
boolean isEncoder) throws IOException, IllegalStateException {
String mimeType = mediaFormat.getString(MediaFormat.KEY_MIME);
MediaCodec mediaCodec = null;
if (filterByTypeAndFormat) {
List<Callable<MediaCodec>> supportedMediaCodecs = findCodecForFormatOrType(isEncoder, mimeType, null);
if (!supportedMediaCodecs.isEmpty()) {
mediaCodec = createAndConfigureCodec(mediaFormat, surface, isEncoder, supportedMediaCodecs);
}
} else {
mediaCodec = isEncoder
? MediaCodec.createEncoderByType(mimeType)
: MediaCodec.createDecoderByType(mimeType);
if (mediaCodec != null) {
configureMediaFormat(mediaCodec, mediaFormat, surface, isEncoder);
}
List<Callable<MediaCodec>> supportedMediaCodecs = findCodecForFormatOrType(isEncoder, mimeType, null);
if (!supportedMediaCodecs.isEmpty()) {
mediaCodec = createAndConfigureCodec(mediaFormat, surface, isEncoder, supportedMediaCodecs);
}

return mediaCodec;
}

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Nullable
private static MediaCodec getAndConfigureCodecByConfig(@NonNull MediaFormat mediaFormat,
@Nullable Surface surface,
boolean isEncoder,
boolean filterByTypeAndFormat) throws IOException, IllegalStateException {
boolean isEncoder) throws IOException, IllegalStateException {
MediaCodec mediaCodec = null;
if (filterByTypeAndFormat) {
String mimeType = mediaFormat.getString(MediaFormat.KEY_MIME);
List<Callable<MediaCodec>> supportedMediaCodecs = findCodecForFormatOrType(isEncoder, mimeType,
mediaFormat);
if (!supportedMediaCodecs.isEmpty()) {
mediaCodec = createAndConfigureCodec(mediaFormat, surface, isEncoder, supportedMediaCodecs);
}
} else {
MediaCodecList mediaCodecList = new MediaCodecList(MediaCodecList.ALL_CODECS);
String codecName = isEncoder
? mediaCodecList.findEncoderForFormat(mediaFormat)
: mediaCodecList.findDecoderForFormat(mediaFormat);
if (codecName != null) {
mediaCodec = MediaCodec.createByCodecName(codecName);
}
if (mediaCodec != null) {
configureMediaFormat(mediaCodec, mediaFormat, surface, isEncoder);
}
String mimeType = mediaFormat.getString(MediaFormat.KEY_MIME);
List<Callable<MediaCodec>> supportedMediaCodecs = findCodecForFormatOrType(isEncoder, mimeType,
mediaFormat);
if (!supportedMediaCodecs.isEmpty()) {
mediaCodec = createAndConfigureCodec(mediaFormat, surface, isEncoder, supportedMediaCodecs);
}

return mediaCodec;
}

Expand Down

0 comments on commit 00391e6

Please sign in to comment.