glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 1 | /* |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame^] | 2 | * Copyright 2015 The WebRTC project authors. All Rights Reserved. |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 3 | * |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame^] | 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
Henrik Kjellander | 15583c1 | 2016-02-10 10:53:12 +0100 | [diff] [blame] | 11 | #ifndef WEBRTC_API_JAVA_JNI_ANDROIDMEDIACODECCOMMON_H_ |
| 12 | #define WEBRTC_API_JAVA_JNI_ANDROIDMEDIACODECCOMMON_H_ |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 13 | |
| 14 | #include <android/log.h> |
Per | ec2922f | 2016-01-27 15:25:46 +0100 | [diff] [blame] | 15 | #include <string> |
| 16 | |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 17 | #include "webrtc/base/thread.h" |
Henrik Kjellander | 15583c1 | 2016-02-10 10:53:12 +0100 | [diff] [blame] | 18 | #include "webrtc/api/java/jni/classreferenceholder.h" |
| 19 | #include "webrtc/api/java/jni/jni_helpers.h" |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 20 | #include "webrtc/base/logging.h" |
kjellander | a96e2d7 | 2016-02-04 23:52:28 -0800 | [diff] [blame] | 21 | #include "webrtc/base/thread.h" |
Henrik Kjellander | 98f5351 | 2015-10-28 18:17:40 +0100 | [diff] [blame] | 22 | #include "webrtc/system_wrappers/include/tick_util.h" |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 23 | |
| 24 | namespace webrtc_jni { |
| 25 | |
| 26 | // Uncomment this define to enable verbose logging for every encoded/decoded |
| 27 | // video frame. |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 28 | //#define TRACK_BUFFER_TIMING |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 29 | |
glaznev | f4decb5 | 2016-01-15 13:49:22 -0800 | [diff] [blame] | 30 | #define TAG_COMMON "MediaCodecVideo" |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 31 | |
| 32 | // Color formats supported by encoder - should mirror supportedColorList |
| 33 | // from MediaCodecVideoEncoder.java |
| 34 | enum COLOR_FORMATTYPE { |
| 35 | COLOR_FormatYUV420Planar = 0x13, |
| 36 | COLOR_FormatYUV420SemiPlanar = 0x15, |
| 37 | COLOR_QCOM_FormatYUV420SemiPlanar = 0x7FA30C00, |
| 38 | // NV12 color format supported by QCOM codec, but not declared in MediaCodec - |
| 39 | // see /hardware/qcom/media/mm-core/inc/OMX_QCOMExtns.h |
| 40 | // This format is presumably similar to COLOR_FormatYUV420SemiPlanar, |
| 41 | // but requires some (16, 32?) byte alignment. |
| 42 | COLOR_QCOM_FORMATYUV420PackedSemiPlanar32m = 0x7FA30C04 |
| 43 | }; |
| 44 | |
| 45 | // Arbitrary interval to poll the codec for new outputs. |
| 46 | enum { kMediaCodecPollMs = 10 }; |
| 47 | // Media codec maximum output buffer ready timeout. |
glaznev@webrtc.org | a4623d2 | 2015-02-25 00:02:50 +0000 | [diff] [blame] | 48 | enum { kMediaCodecTimeoutMs = 1000 }; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 49 | // Interval to print codec statistics (bitrate, fps, encoding/decoding time). |
| 50 | enum { kMediaCodecStatisticsIntervalMs = 3000 }; |
glaznev@webrtc.org | a4623d2 | 2015-02-25 00:02:50 +0000 | [diff] [blame] | 51 | // Maximum amount of pending frames for VP8 decoder. |
| 52 | enum { kMaxPendingFramesVp8 = 1 }; |
Alex Glaznev | 69a7fd5 | 2015-11-10 10:25:40 -0800 | [diff] [blame] | 53 | // Maximum amount of pending frames for VP9 decoder. |
| 54 | enum { kMaxPendingFramesVp9 = 1 }; |
glaznev@webrtc.org | a4623d2 | 2015-02-25 00:02:50 +0000 | [diff] [blame] | 55 | // Maximum amount of pending frames for H.264 decoder. |
glaznev | ae95ff3 | 2016-02-04 11:47:12 -0800 | [diff] [blame] | 56 | enum { kMaxPendingFramesH264 = 8 }; |
glaznev | e55c42c | 2015-10-28 10:30:32 -0700 | [diff] [blame] | 57 | // Maximum amount of decoded frames for which per-frame logging is enabled. |
glaznev | ae95ff3 | 2016-02-04 11:47:12 -0800 | [diff] [blame] | 58 | enum { kMaxDecodedLogFrames = 10 }; |
glaznev | f4decb5 | 2016-01-15 13:49:22 -0800 | [diff] [blame] | 59 | // Maximum amount of encoded frames for which per-frame logging is enabled. |
glaznev | ae95ff3 | 2016-02-04 11:47:12 -0800 | [diff] [blame] | 60 | enum { kMaxEncodedLogFrames = 10 }; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 61 | |
| 62 | static inline int64_t GetCurrentTimeMs() { |
| 63 | return webrtc::TickTime::Now().Ticks() / 1000000LL; |
| 64 | } |
| 65 | |
| 66 | static inline void AllowBlockingCalls() { |
| 67 | rtc::Thread* current_thread = rtc::Thread::Current(); |
| 68 | if (current_thread != NULL) |
| 69 | current_thread->SetAllowBlockingCalls(true); |
| 70 | } |
| 71 | |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 72 | // Return the (singleton) Java Enum object corresponding to |index|; |
| 73 | // |state_class_fragment| is something like "MediaSource$State". |
Per | ec2922f | 2016-01-27 15:25:46 +0100 | [diff] [blame] | 74 | static inline jobject JavaEnumFromIndexAndClassName( |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 75 | JNIEnv* jni, const std::string& state_class_fragment, int index) { |
| 76 | const std::string state_class = "org/webrtc/" + state_class_fragment; |
| 77 | return JavaEnumFromIndex(jni, FindClass(jni, state_class.c_str()), |
| 78 | state_class, index); |
| 79 | } |
| 80 | |
Alex Glaznev | 782671f | 2015-06-12 16:40:44 -0700 | [diff] [blame] | 81 | // Checks for any Java exception, prints stack backtrace and clears |
| 82 | // currently thrown exception. |
| 83 | static inline bool CheckException(JNIEnv* jni) { |
| 84 | if (jni->ExceptionCheck()) { |
glaznev | f4decb5 | 2016-01-15 13:49:22 -0800 | [diff] [blame] | 85 | LOG_TAG(rtc::LS_ERROR, TAG_COMMON) << "Java JNI exception."; |
Alex Glaznev | 782671f | 2015-06-12 16:40:44 -0700 | [diff] [blame] | 86 | jni->ExceptionDescribe(); |
| 87 | jni->ExceptionClear(); |
| 88 | return true; |
| 89 | } |
| 90 | return false; |
| 91 | } |
| 92 | |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 93 | } // namespace webrtc_jni |
| 94 | |
Henrik Kjellander | 15583c1 | 2016-02-10 10:53:12 +0100 | [diff] [blame] | 95 | #endif // WEBRTC_API_JAVA_JNI_ANDROIDMEDIACODECCOMMON_H_ |