blob: 2f9e05d1f54570735c4d6f86dcd872bb68a38b64 [file] [log] [blame]
glaznev@webrtc.org18c92472015-02-18 18:42:55 +00001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2015 The WebRTC project authors. All Rights Reserved.
glaznev@webrtc.org18c92472015-02-18 18:42:55 +00003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * 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.org18c92472015-02-18 18:42:55 +00009 */
10
Henrik Kjellander15583c12016-02-10 10:53:12 +010011#ifndef WEBRTC_API_JAVA_JNI_ANDROIDMEDIACODECCOMMON_H_
12#define WEBRTC_API_JAVA_JNI_ANDROIDMEDIACODECCOMMON_H_
glaznev@webrtc.org18c92472015-02-18 18:42:55 +000013
14#include <android/log.h>
Perec2922f2016-01-27 15:25:46 +010015#include <string>
16
glaznev@webrtc.org18c92472015-02-18 18:42:55 +000017#include "webrtc/base/thread.h"
Henrik Kjellander15583c12016-02-10 10:53:12 +010018#include "webrtc/api/java/jni/classreferenceholder.h"
19#include "webrtc/api/java/jni/jni_helpers.h"
Alex Glaznevfddf6e52015-10-07 16:51:02 -070020#include "webrtc/base/logging.h"
kjellandera96e2d72016-02-04 23:52:28 -080021#include "webrtc/base/thread.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010022#include "webrtc/system_wrappers/include/tick_util.h"
glaznev@webrtc.org18c92472015-02-18 18:42:55 +000023
24namespace webrtc_jni {
25
26// Uncomment this define to enable verbose logging for every encoded/decoded
27// video frame.
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +000028//#define TRACK_BUFFER_TIMING
glaznev@webrtc.org18c92472015-02-18 18:42:55 +000029
glaznevf4decb52016-01-15 13:49:22 -080030#define TAG_COMMON "MediaCodecVideo"
glaznev@webrtc.org18c92472015-02-18 18:42:55 +000031
32// Color formats supported by encoder - should mirror supportedColorList
33// from MediaCodecVideoEncoder.java
34enum 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.
46enum { kMediaCodecPollMs = 10 };
47// Media codec maximum output buffer ready timeout.
glaznev@webrtc.orga4623d22015-02-25 00:02:50 +000048enum { kMediaCodecTimeoutMs = 1000 };
glaznev@webrtc.org18c92472015-02-18 18:42:55 +000049// Interval to print codec statistics (bitrate, fps, encoding/decoding time).
50enum { kMediaCodecStatisticsIntervalMs = 3000 };
glaznev@webrtc.orga4623d22015-02-25 00:02:50 +000051// Maximum amount of pending frames for VP8 decoder.
52enum { kMaxPendingFramesVp8 = 1 };
Alex Glaznev69a7fd52015-11-10 10:25:40 -080053// Maximum amount of pending frames for VP9 decoder.
54enum { kMaxPendingFramesVp9 = 1 };
glaznev@webrtc.orga4623d22015-02-25 00:02:50 +000055// Maximum amount of pending frames for H.264 decoder.
glaznevae95ff32016-02-04 11:47:12 -080056enum { kMaxPendingFramesH264 = 8 };
glazneve55c42c2015-10-28 10:30:32 -070057// Maximum amount of decoded frames for which per-frame logging is enabled.
glaznevae95ff32016-02-04 11:47:12 -080058enum { kMaxDecodedLogFrames = 10 };
glaznevf4decb52016-01-15 13:49:22 -080059// Maximum amount of encoded frames for which per-frame logging is enabled.
glaznevae95ff32016-02-04 11:47:12 -080060enum { kMaxEncodedLogFrames = 10 };
glaznev@webrtc.org18c92472015-02-18 18:42:55 +000061
62static inline int64_t GetCurrentTimeMs() {
63 return webrtc::TickTime::Now().Ticks() / 1000000LL;
64}
65
66static 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.orgb28474c2015-02-23 17:44:27 +000072// Return the (singleton) Java Enum object corresponding to |index|;
73// |state_class_fragment| is something like "MediaSource$State".
Perec2922f2016-01-27 15:25:46 +010074static inline jobject JavaEnumFromIndexAndClassName(
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +000075 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 Glaznev782671f2015-06-12 16:40:44 -070081// Checks for any Java exception, prints stack backtrace and clears
82// currently thrown exception.
83static inline bool CheckException(JNIEnv* jni) {
84 if (jni->ExceptionCheck()) {
glaznevf4decb52016-01-15 13:49:22 -080085 LOG_TAG(rtc::LS_ERROR, TAG_COMMON) << "Java JNI exception.";
Alex Glaznev782671f2015-06-12 16:40:44 -070086 jni->ExceptionDescribe();
87 jni->ExceptionClear();
88 return true;
89 }
90 return false;
91}
92
glaznev@webrtc.org18c92472015-02-18 18:42:55 +000093} // namespace webrtc_jni
94
Henrik Kjellander15583c12016-02-10 10:53:12 +010095#endif // WEBRTC_API_JAVA_JNI_ANDROIDMEDIACODECCOMMON_H_