OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.media; | 5 package org.chromium.media; |
6 | 6 |
| 7 import android.annotation.TargetApi; |
7 import android.media.AudioFormat; | 8 import android.media.AudioFormat; |
8 import android.media.AudioManager; | 9 import android.media.AudioManager; |
9 import android.media.AudioTrack; | 10 import android.media.AudioTrack; |
10 import android.media.MediaCodec; | 11 import android.media.MediaCodec; |
11 import android.media.MediaCodecInfo; | 12 import android.media.MediaCodecInfo; |
12 import android.media.MediaCodecList; | 13 import android.media.MediaCodecList; |
13 import android.media.MediaCrypto; | 14 import android.media.MediaCrypto; |
14 import android.media.MediaFormat; | 15 import android.media.MediaFormat; |
15 import android.os.Build; | 16 import android.os.Build; |
16 import android.os.Bundle; | 17 import android.os.Bundle; |
17 import android.util.Log; | 18 import android.util.Log; |
18 import android.view.Surface; | 19 import android.view.Surface; |
19 | 20 |
20 import org.chromium.base.CalledByNative; | 21 import org.chromium.base.CalledByNative; |
21 import org.chromium.base.JNINamespace; | 22 import org.chromium.base.JNINamespace; |
22 | 23 |
23 import java.nio.ByteBuffer; | 24 import java.nio.ByteBuffer; |
24 import java.util.ArrayList; | 25 import java.util.ArrayList; |
25 import java.util.HashMap; | 26 import java.util.HashMap; |
26 import java.util.Map; | 27 import java.util.Map; |
27 | 28 |
28 /** | 29 /** |
29 * A wrapper of the MediaCodec class to facilitate exception capturing and | 30 * A wrapper of the MediaCodec class to facilitate exception capturing and |
30 * audio rendering. | 31 * audio rendering. |
31 */ | 32 */ |
32 @JNINamespace("media") | 33 @JNINamespace("media") |
| 34 @TargetApi(Build.VERSION_CODES.JELLY_BEAN) |
33 class MediaCodecBridge { | 35 class MediaCodecBridge { |
34 private static final String TAG = "MediaCodecBridge"; | 36 private static final String TAG = "MediaCodecBridge"; |
35 | 37 |
36 // Error code for MediaCodecBridge. Keep this value in sync with | 38 // Error code for MediaCodecBridge. Keep this value in sync with |
37 // MediaCodecStatus in media_codec_bridge.h. | 39 // MediaCodecStatus in media_codec_bridge.h. |
38 private static final int MEDIA_CODEC_OK = 0; | 40 private static final int MEDIA_CODEC_OK = 0; |
39 private static final int MEDIA_CODEC_DEQUEUE_INPUT_AGAIN_LATER = 1; | 41 private static final int MEDIA_CODEC_DEQUEUE_INPUT_AGAIN_LATER = 1; |
40 private static final int MEDIA_CODEC_DEQUEUE_OUTPUT_AGAIN_LATER = 2; | 42 private static final int MEDIA_CODEC_DEQUEUE_OUTPUT_AGAIN_LATER = 2; |
41 private static final int MEDIA_CODEC_OUTPUT_BUFFERS_CHANGED = 3; | 43 private static final int MEDIA_CODEC_OUTPUT_BUFFERS_CHANGED = 3; |
42 private static final int MEDIA_CODEC_OUTPUT_FORMAT_CHANGED = 4; | 44 private static final int MEDIA_CODEC_OUTPUT_FORMAT_CHANGED = 4; |
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
205 ArrayList<CodecInfo> codecInfos = new ArrayList<CodecInfo>( | 207 ArrayList<CodecInfo> codecInfos = new ArrayList<CodecInfo>( |
206 decoderInfoMap.size() + encoderInfoMap.size()); | 208 decoderInfoMap.size() + encoderInfoMap.size()); |
207 codecInfos.addAll(encoderInfoMap.values()); | 209 codecInfos.addAll(encoderInfoMap.values()); |
208 codecInfos.addAll(decoderInfoMap.values()); | 210 codecInfos.addAll(decoderInfoMap.values()); |
209 return codecInfos.toArray(new CodecInfo[codecInfos.size()]); | 211 return codecInfos.toArray(new CodecInfo[codecInfos.size()]); |
210 } | 212 } |
211 | 213 |
212 /** | 214 /** |
213 * Get a name of default android codec. | 215 * Get a name of default android codec. |
214 */ | 216 */ |
| 217 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2) |
215 @SuppressWarnings("deprecation") | 218 @SuppressWarnings("deprecation") |
216 @CalledByNative | 219 @CalledByNative |
217 private static String getDefaultCodecName(String mime, int direction) { | 220 private static String getDefaultCodecName(String mime, int direction) { |
218 String codecName = ""; | 221 String codecName = ""; |
219 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { | 222 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { |
220 try { | 223 try { |
221 MediaCodec mediaCodec = null; | 224 MediaCodec mediaCodec = null; |
222 if (direction == MEDIA_CODEC_ENCODER) { | 225 if (direction == MEDIA_CODEC_ENCODER) { |
223 mediaCodec = MediaCodec.createEncoderByType(mime); | 226 mediaCodec = MediaCodec.createEncoderByType(mime); |
224 } else { | 227 } else { |
225 mediaCodec = MediaCodec.createDecoderByType(mime); | 228 mediaCodec = MediaCodec.createDecoderByType(mime); |
226 } | 229 } |
227 codecName = mediaCodec.getName(); | 230 codecName = mediaCodec.getName(); |
228 mediaCodec.release(); | 231 mediaCodec.release(); |
229 } catch (Exception e) { | 232 } catch (Exception e) { |
230 Log.w(TAG, "getDefaultCodecName: Failed to create MediaCodec: " | 233 Log.w(TAG, "getDefaultCodecName: Failed to create MediaCodec: " |
231 + mime + ", direction: " + direction, e); | 234 + mime + ", direction: " + direction, e); |
232 } | 235 } |
233 } | 236 } |
234 return codecName; | 237 return codecName; |
235 } | 238 } |
236 | 239 |
237 /** | 240 /** |
238 * Get a list of encoder supported color formats for specified mime type. | 241 * Get a list of encoder supported color formats for specified mime type. |
239 */ | 242 */ |
| 243 @TargetApi(Build.VERSION_CODES.LOLLIPOP) |
| 244 @SuppressWarnings("deprecation") |
240 @CalledByNative | 245 @CalledByNative |
241 private static int[] getEncoderColorFormatsForMime(String mime) { | 246 private static int[] getEncoderColorFormatsForMime(String mime) { |
242 MediaCodecInfo[] codecs = null; | 247 MediaCodecInfo[] codecs = null; |
243 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { | 248 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { |
244 MediaCodecList mediaCodecList = new MediaCodecList(MediaCodecList.AL
L_CODECS); | 249 MediaCodecList mediaCodecList = new MediaCodecList(MediaCodecList.AL
L_CODECS); |
245 codecs = mediaCodecList.getCodecInfos(); | 250 codecs = mediaCodecList.getCodecInfos(); |
246 } else { | 251 } else { |
247 int count = MediaCodecList.getCodecCount(); | 252 int count = MediaCodecList.getCodecCount(); |
248 if (count <= 0) { | 253 if (count <= 0) { |
249 return null; | 254 return null; |
(...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
488 mMediaCodec.getInputBuffer(index); | 493 mMediaCodec.getInputBuffer(index); |
489 } | 494 } |
490 mMediaCodec.queueInputBuffer(index, offset, size, presentationTimeUs
, flags); | 495 mMediaCodec.queueInputBuffer(index, offset, size, presentationTimeUs
, flags); |
491 } catch (Exception e) { | 496 } catch (Exception e) { |
492 Log.e(TAG, "Failed to queue input buffer", e); | 497 Log.e(TAG, "Failed to queue input buffer", e); |
493 return MEDIA_CODEC_ERROR; | 498 return MEDIA_CODEC_ERROR; |
494 } | 499 } |
495 return MEDIA_CODEC_OK; | 500 return MEDIA_CODEC_OK; |
496 } | 501 } |
497 | 502 |
| 503 @TargetApi(Build.VERSION_CODES.KITKAT) |
498 @CalledByNative | 504 @CalledByNative |
499 private void setVideoBitrate(int bps) { | 505 private void setVideoBitrate(int bps) { |
500 Bundle b = new Bundle(); | 506 Bundle b = new Bundle(); |
501 b.putInt(MediaCodec.PARAMETER_KEY_VIDEO_BITRATE, bps); | 507 b.putInt(MediaCodec.PARAMETER_KEY_VIDEO_BITRATE, bps); |
502 mMediaCodec.setParameters(b); | 508 mMediaCodec.setParameters(b); |
503 } | 509 } |
504 | 510 |
505 @CalledByNative | 511 @CalledByNative |
506 private void requestKeyFrameSoon() { | 512 private void requestKeyFrameSoon() { |
507 Bundle b = new Bundle(); | 513 Bundle b = new Bundle(); |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
629 return format; | 635 return format; |
630 } | 636 } |
631 | 637 |
632 @CalledByNative | 638 @CalledByNative |
633 private boolean isAdaptivePlaybackSupported(int width, int height) { | 639 private boolean isAdaptivePlaybackSupported(int width, int height) { |
634 if (!mAdaptivePlaybackSupported) | 640 if (!mAdaptivePlaybackSupported) |
635 return false; | 641 return false; |
636 return width <= MAX_ADAPTIVE_PLAYBACK_WIDTH && height <= MAX_ADAPTIVE_PL
AYBACK_HEIGHT; | 642 return width <= MAX_ADAPTIVE_PLAYBACK_WIDTH && height <= MAX_ADAPTIVE_PL
AYBACK_HEIGHT; |
637 } | 643 } |
638 | 644 |
| 645 @TargetApi(Build.VERSION_CODES.KITKAT) |
639 private static boolean codecSupportsAdaptivePlayback(MediaCodec mediaCodec,
String mime) { | 646 private static boolean codecSupportsAdaptivePlayback(MediaCodec mediaCodec,
String mime) { |
640 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT || mediaCodec ==
null) { | 647 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT || mediaCodec ==
null) { |
641 return false; | 648 return false; |
642 } | 649 } |
643 try { | 650 try { |
644 MediaCodecInfo info = mediaCodec.getCodecInfo(); | 651 MediaCodecInfo info = mediaCodec.getCodecInfo(); |
645 if (info.isEncoder()) { | 652 if (info.isEncoder()) { |
646 return false; | 653 return false; |
647 } | 654 } |
648 MediaCodecInfo.CodecCapabilities capabilities = info.getCapabilities
ForType(mime); | 655 MediaCodecInfo.CodecCapabilities capabilities = info.getCapabilities
ForType(mime); |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
769 return AudioFormat.CHANNEL_OUT_QUAD; | 776 return AudioFormat.CHANNEL_OUT_QUAD; |
770 case 6: | 777 case 6: |
771 return AudioFormat.CHANNEL_OUT_5POINT1; | 778 return AudioFormat.CHANNEL_OUT_5POINT1; |
772 case 8: | 779 case 8: |
773 return AudioFormat.CHANNEL_OUT_7POINT1; | 780 return AudioFormat.CHANNEL_OUT_7POINT1; |
774 default: | 781 default: |
775 return AudioFormat.CHANNEL_OUT_DEFAULT; | 782 return AudioFormat.CHANNEL_OUT_DEFAULT; |
776 } | 783 } |
777 } | 784 } |
778 } | 785 } |
OLD | NEW |