Chromium Code Reviews| Index: media/base/android/java/src/org/chromium/media/MediaCodecBridge.java |
| diff --git a/media/base/android/java/src/org/chromium/media/MediaCodecBridge.java b/media/base/android/java/src/org/chromium/media/MediaCodecBridge.java |
| index 8af311153324667ccc26d78bef864672996b6e43..ea4e8e69e54eecb789721416f9546e902ca9fa35 100644 |
| --- a/media/base/android/java/src/org/chromium/media/MediaCodecBridge.java |
| +++ b/media/base/android/java/src/org/chromium/media/MediaCodecBridge.java |
| @@ -143,6 +143,48 @@ class MediaCodecBridge { |
| } |
| } |
| + /** A wrapper around a MediaFormat. */ |
| + @MainDex |
| + private static class GetOutputFormatResult { |
|
watk
2016/03/03 22:21:37
I added this so that we didn't have to have two wr
|
| + private final int mStatus; |
| + // May be null if mStatus is not MEDIA_CODEC_OK. |
| + private final MediaFormat mFormat; |
| + |
| + private GetOutputFormatResult(int status, MediaFormat format) { |
| + mStatus = status; |
| + mFormat = format; |
| + } |
| + |
| + private boolean formatHasCropValues() { |
| + return mFormat.containsKey(KEY_CROP_RIGHT) && mFormat.containsKey(KEY_CROP_LEFT) |
| + && mFormat.containsKey(KEY_CROP_BOTTOM) && mFormat.containsKey(KEY_CROP_TOP); |
| + } |
| + |
| + @CalledByNative("GetOutputFormatResult") |
| + private int status() { |
| + return mStatus; |
| + } |
| + |
| + @CalledByNative("GetOutputFormatResult") |
| + private int width() { |
| + return formatHasCropValues() |
| + ? mFormat.getInteger(KEY_CROP_RIGHT) - mFormat.getInteger(KEY_CROP_LEFT) + 1 |
| + : mFormat.getInteger(MediaFormat.KEY_WIDTH); |
| + } |
| + |
| + @CalledByNative("GetOutputFormatResult") |
| + private int height() { |
| + return formatHasCropValues() |
| + ? mFormat.getInteger(KEY_CROP_BOTTOM) - mFormat.getInteger(KEY_CROP_TOP) + 1 |
| + : mFormat.getInteger(MediaFormat.KEY_HEIGHT); |
| + } |
| + |
| + @CalledByNative("GetOutputFormatResult") |
| + private int sampleRate() { |
| + return mFormat.getInteger(MediaFormat.KEY_SAMPLE_RATE); |
| + } |
| + } |
| + |
| private MediaCodecBridge( |
| MediaCodec mediaCodec, String mime, boolean adaptivePlaybackSupported) { |
| assert mediaCodec != null; |
| @@ -255,45 +297,54 @@ class MediaCodecBridge { |
| } |
| } |
| - private boolean outputFormatHasCropValues(MediaFormat format) { |
| - return format.containsKey(KEY_CROP_RIGHT) && format.containsKey(KEY_CROP_LEFT) |
| - && format.containsKey(KEY_CROP_BOTTOM) && format.containsKey(KEY_CROP_TOP); |
| - } |
| - |
| - @CalledByNative |
| - private int getOutputHeight() { |
| - MediaFormat format = mMediaCodec.getOutputFormat(); |
| - return outputFormatHasCropValues(format) |
| - ? format.getInteger(KEY_CROP_BOTTOM) - format.getInteger(KEY_CROP_TOP) + 1 |
| - : format.getInteger(MediaFormat.KEY_HEIGHT); |
| - } |
| - |
| - @CalledByNative |
| - private int getOutputWidth() { |
| - MediaFormat format = mMediaCodec.getOutputFormat(); |
| - return outputFormatHasCropValues(format) |
| - ? format.getInteger(KEY_CROP_RIGHT) - format.getInteger(KEY_CROP_LEFT) + 1 |
| - : format.getInteger(MediaFormat.KEY_WIDTH); |
| - } |
| - |
| @CalledByNative |
| - private int getOutputSamplingRate() { |
| - MediaFormat format = mMediaCodec.getOutputFormat(); |
| - return format.getInteger(MediaFormat.KEY_SAMPLE_RATE); |
| + private GetOutputFormatResult getOutputFormat() { |
| + MediaFormat format; |
| + try { |
| + format = mMediaCodec.getOutputFormat(); |
| + } catch (IllegalStateException e) { |
| + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP |
| + && e instanceof android.media.MediaCodec.CodecException) { |
| + Log.e(TAG, "Failed to get output format", e); |
| + return new GetOutputFormatResult(MEDIA_CODEC_ERROR, null); |
| + } |
| + throw e; |
| + } |
| + return new GetOutputFormatResult(MEDIA_CODEC_OK, format); |
| } |
| + /** Returns null on CodecException. */ |
| @CalledByNative |
| private ByteBuffer getInputBuffer(int index) { |
| if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) { |
| - return mMediaCodec.getInputBuffer(index); |
| + try { |
| + return mMediaCodec.getInputBuffer(index); |
| + } catch (IllegalStateException e) { |
| + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP |
|
qinmin
2016/03/03 23:15:24
we should never throw the exception for all builds
watk
2016/03/04 01:33:32
Fair enough. It always catches IllegalStateExcepti
|
| + && e instanceof android.media.MediaCodec.CodecException) { |
| + Log.e(TAG, "Failed to get input buffer", e); |
| + return null; |
| + } |
| + throw e; |
| + } |
| } |
| return mInputBuffers[index]; |
| } |
| + /** Returns null on CodecException. */ |
| @CalledByNative |
| private ByteBuffer getOutputBuffer(int index) { |
| if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) { |
| - return mMediaCodec.getOutputBuffer(index); |
| + try { |
| + return mMediaCodec.getOutputBuffer(index); |
| + } catch (IllegalStateException e) { |
| + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP |
| + && e instanceof android.media.MediaCodec.CodecException) { |
| + Log.e(TAG, "Failed to get output buffer", e); |
| + return null; |
| + } |
| + throw e; |
| + } |
| } |
| return mOutputBuffers[index]; |
| } |