blob: 528e166f937d88077b165ccf2b5e9e6bbde3788a [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"
sakald34a7112016-07-01 05:10:51 -070018#include "webrtc/api/android/jni/classreferenceholder.h"
19#include "webrtc/api/android/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
glaznev893a7ee2016-09-22 10:44:30 -070031// Color formats supported by encoder or decoder - should include all
32// colors from supportedColorList in MediaCodecVideoEncoder.java and
33// MediaCodecVideoDecoder.java. Supported color format set in encoder
34// and decoder could be different.
glaznev@webrtc.org18c92472015-02-18 18:42:55 +000035enum COLOR_FORMATTYPE {
36 COLOR_FormatYUV420Planar = 0x13,
37 COLOR_FormatYUV420SemiPlanar = 0x15,
38 COLOR_QCOM_FormatYUV420SemiPlanar = 0x7FA30C00,
39 // NV12 color format supported by QCOM codec, but not declared in MediaCodec -
40 // see /hardware/qcom/media/mm-core/inc/OMX_QCOMExtns.h
41 // This format is presumably similar to COLOR_FormatYUV420SemiPlanar,
42 // but requires some (16, 32?) byte alignment.
glaznev893a7ee2016-09-22 10:44:30 -070043 COLOR_QCOM_FORMATYVU420PackedSemiPlanar32m4ka = 0x7FA30C01,
44 COLOR_QCOM_FORMATYVU420PackedSemiPlanar16m4ka = 0x7FA30C02,
45 COLOR_QCOM_FORMATYVU420PackedSemiPlanar64x32Tile2m8ka = 0x7FA30C03,
glaznev@webrtc.org18c92472015-02-18 18:42:55 +000046 COLOR_QCOM_FORMATYUV420PackedSemiPlanar32m = 0x7FA30C04
47};
48
49// Arbitrary interval to poll the codec for new outputs.
50enum { kMediaCodecPollMs = 10 };
sakal1fc48102016-06-14 01:53:39 -070051// Arbitrary interval to poll at when there should be no more frames.
52enum { kMediaCodecPollNoFramesMs = 100 };
glaznev@webrtc.org18c92472015-02-18 18:42:55 +000053// Media codec maximum output buffer ready timeout.
glaznev@webrtc.orga4623d22015-02-25 00:02:50 +000054enum { kMediaCodecTimeoutMs = 1000 };
glaznev@webrtc.org18c92472015-02-18 18:42:55 +000055// Interval to print codec statistics (bitrate, fps, encoding/decoding time).
56enum { kMediaCodecStatisticsIntervalMs = 3000 };
glaznev@webrtc.orga4623d22015-02-25 00:02:50 +000057// Maximum amount of pending frames for VP8 decoder.
58enum { kMaxPendingFramesVp8 = 1 };
Alex Glaznev69a7fd52015-11-10 10:25:40 -080059// Maximum amount of pending frames for VP9 decoder.
60enum { kMaxPendingFramesVp9 = 1 };
glaznev@webrtc.orga4623d22015-02-25 00:02:50 +000061// Maximum amount of pending frames for H.264 decoder.
Alex Glaznevd5704842016-06-27 11:51:10 -070062enum { kMaxPendingFramesH264 = 3 };
glazneve55c42c2015-10-28 10:30:32 -070063// Maximum amount of decoded frames for which per-frame logging is enabled.
glaznevae95ff32016-02-04 11:47:12 -080064enum { kMaxDecodedLogFrames = 10 };
glaznevf4decb52016-01-15 13:49:22 -080065// Maximum amount of encoded frames for which per-frame logging is enabled.
glaznevae95ff32016-02-04 11:47:12 -080066enum { kMaxEncodedLogFrames = 10 };
glaznev@webrtc.org18c92472015-02-18 18:42:55 +000067
glaznev@webrtc.org18c92472015-02-18 18:42:55 +000068static inline void AllowBlockingCalls() {
69 rtc::Thread* current_thread = rtc::Thread::Current();
70 if (current_thread != NULL)
71 current_thread->SetAllowBlockingCalls(true);
72}
73
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +000074// Return the (singleton) Java Enum object corresponding to |index|;
75// |state_class_fragment| is something like "MediaSource$State".
Perec2922f2016-01-27 15:25:46 +010076static inline jobject JavaEnumFromIndexAndClassName(
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +000077 JNIEnv* jni, const std::string& state_class_fragment, int index) {
78 const std::string state_class = "org/webrtc/" + state_class_fragment;
79 return JavaEnumFromIndex(jni, FindClass(jni, state_class.c_str()),
80 state_class, index);
81}
82
Alex Glaznev782671f2015-06-12 16:40:44 -070083// Checks for any Java exception, prints stack backtrace and clears
84// currently thrown exception.
85static inline bool CheckException(JNIEnv* jni) {
86 if (jni->ExceptionCheck()) {
glaznevf4decb52016-01-15 13:49:22 -080087 LOG_TAG(rtc::LS_ERROR, TAG_COMMON) << "Java JNI exception.";
Alex Glaznev782671f2015-06-12 16:40:44 -070088 jni->ExceptionDescribe();
89 jni->ExceptionClear();
90 return true;
91 }
92 return false;
93}
94
glaznev@webrtc.org18c92472015-02-18 18:42:55 +000095} // namespace webrtc_jni
96
Henrik Kjellander15583c12016-02-10 10:53:12 +010097#endif // WEBRTC_API_JAVA_JNI_ANDROIDMEDIACODECCOMMON_H_