blob: db7260ade55c422edc3c1055006a07a2963fd2fa [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"
glaznev@webrtc.org18c92472015-02-18 18:42:55 +000022
23namespace webrtc_jni {
24
25// Uncomment this define to enable verbose logging for every encoded/decoded
26// video frame.
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +000027//#define TRACK_BUFFER_TIMING
glaznev@webrtc.org18c92472015-02-18 18:42:55 +000028
glaznevf4decb52016-01-15 13:49:22 -080029#define TAG_COMMON "MediaCodecVideo"
glaznev@webrtc.org18c92472015-02-18 18:42:55 +000030
31// Color formats supported by encoder - should mirror supportedColorList
32// from MediaCodecVideoEncoder.java
33enum COLOR_FORMATTYPE {
34 COLOR_FormatYUV420Planar = 0x13,
35 COLOR_FormatYUV420SemiPlanar = 0x15,
36 COLOR_QCOM_FormatYUV420SemiPlanar = 0x7FA30C00,
37 // NV12 color format supported by QCOM codec, but not declared in MediaCodec -
38 // see /hardware/qcom/media/mm-core/inc/OMX_QCOMExtns.h
39 // This format is presumably similar to COLOR_FormatYUV420SemiPlanar,
40 // but requires some (16, 32?) byte alignment.
41 COLOR_QCOM_FORMATYUV420PackedSemiPlanar32m = 0x7FA30C04
42};
43
44// Arbitrary interval to poll the codec for new outputs.
45enum { kMediaCodecPollMs = 10 };
46// Media codec maximum output buffer ready timeout.
glaznev@webrtc.orga4623d22015-02-25 00:02:50 +000047enum { kMediaCodecTimeoutMs = 1000 };
glaznev@webrtc.org18c92472015-02-18 18:42:55 +000048// Interval to print codec statistics (bitrate, fps, encoding/decoding time).
49enum { kMediaCodecStatisticsIntervalMs = 3000 };
glaznev@webrtc.orga4623d22015-02-25 00:02:50 +000050// Maximum amount of pending frames for VP8 decoder.
51enum { kMaxPendingFramesVp8 = 1 };
Alex Glaznev69a7fd52015-11-10 10:25:40 -080052// Maximum amount of pending frames for VP9 decoder.
53enum { kMaxPendingFramesVp9 = 1 };
glaznev@webrtc.orga4623d22015-02-25 00:02:50 +000054// Maximum amount of pending frames for H.264 decoder.
glaznevae95ff32016-02-04 11:47:12 -080055enum { kMaxPendingFramesH264 = 8 };
glazneve55c42c2015-10-28 10:30:32 -070056// Maximum amount of decoded frames for which per-frame logging is enabled.
glaznevae95ff32016-02-04 11:47:12 -080057enum { kMaxDecodedLogFrames = 10 };
glaznevf4decb52016-01-15 13:49:22 -080058// Maximum amount of encoded frames for which per-frame logging is enabled.
glaznevae95ff32016-02-04 11:47:12 -080059enum { kMaxEncodedLogFrames = 10 };
glaznev@webrtc.org18c92472015-02-18 18:42:55 +000060
glaznev@webrtc.org18c92472015-02-18 18:42:55 +000061static inline void AllowBlockingCalls() {
62 rtc::Thread* current_thread = rtc::Thread::Current();
63 if (current_thread != NULL)
64 current_thread->SetAllowBlockingCalls(true);
65}
66
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +000067// Return the (singleton) Java Enum object corresponding to |index|;
68// |state_class_fragment| is something like "MediaSource$State".
Perec2922f2016-01-27 15:25:46 +010069static inline jobject JavaEnumFromIndexAndClassName(
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +000070 JNIEnv* jni, const std::string& state_class_fragment, int index) {
71 const std::string state_class = "org/webrtc/" + state_class_fragment;
72 return JavaEnumFromIndex(jni, FindClass(jni, state_class.c_str()),
73 state_class, index);
74}
75
Alex Glaznev782671f2015-06-12 16:40:44 -070076// Checks for any Java exception, prints stack backtrace and clears
77// currently thrown exception.
78static inline bool CheckException(JNIEnv* jni) {
79 if (jni->ExceptionCheck()) {
glaznevf4decb52016-01-15 13:49:22 -080080 LOG_TAG(rtc::LS_ERROR, TAG_COMMON) << "Java JNI exception.";
Alex Glaznev782671f2015-06-12 16:40:44 -070081 jni->ExceptionDescribe();
82 jni->ExceptionClear();
83 return true;
84 }
85 return false;
86}
87
glaznev@webrtc.org18c92472015-02-18 18:42:55 +000088} // namespace webrtc_jni
89
Henrik Kjellander15583c12016-02-10 10:53:12 +010090#endif // WEBRTC_API_JAVA_JNI_ANDROIDMEDIACODECCOMMON_H_