xref: /aosp_15_r20/external/webrtc/modules/video_coding/codecs/test/android_codec_factory_helper.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright 2017 The WebRTC project authors. All Rights Reserved.
3  *
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.
9  */
10 
11 #include "modules/video_coding/codecs/test/android_codec_factory_helper.h"
12 
13 #include <jni.h>
14 #include <pthread.h>
15 #include <stddef.h>
16 
17 #include <memory>
18 
19 #include "modules/utility/include/jvm_android.h"
20 #include "rtc_base/checks.h"
21 #include "sdk/android/native_api/codecs/wrapper.h"
22 #include "sdk/android/native_api/jni/class_loader.h"
23 #include "sdk/android/native_api/jni/jvm.h"
24 #include "sdk/android/native_api/jni/scoped_java_ref.h"
25 #include "sdk/android/src/jni/jvm.h"
26 
27 namespace webrtc {
28 namespace test {
29 
30 namespace {
31 
32 static pthread_once_t g_initialize_once = PTHREAD_ONCE_INIT;
33 
EnsureInitializedOnce()34 void EnsureInitializedOnce() {
35   RTC_CHECK(::webrtc::jni::GetJVM() != nullptr);
36 
37   JNIEnv* jni = ::webrtc::jni::AttachCurrentThreadIfNeeded();
38   JavaVM* jvm = NULL;
39   RTC_CHECK_EQ(0, jni->GetJavaVM(&jvm));
40 
41   // Initialize the Java environment (currently only used by the audio manager).
42   webrtc::JVM::Initialize(jvm);
43 }
44 
45 }  // namespace
46 
InitializeAndroidObjects()47 void InitializeAndroidObjects() {
48   RTC_CHECK_EQ(0, pthread_once(&g_initialize_once, &EnsureInitializedOnce));
49 }
50 
CreateAndroidEncoderFactory()51 std::unique_ptr<VideoEncoderFactory> CreateAndroidEncoderFactory() {
52   JNIEnv* env = AttachCurrentThreadIfNeeded();
53   ScopedJavaLocalRef<jclass> factory_class =
54       GetClass(env, "org/webrtc/HardwareVideoEncoderFactory");
55   jmethodID factory_constructor = env->GetMethodID(
56       factory_class.obj(), "<init>", "(Lorg/webrtc/EglBase$Context;ZZ)V");
57   ScopedJavaLocalRef<jobject> factory_object(
58       env, env->NewObject(factory_class.obj(), factory_constructor,
59                           nullptr /* shared_context */,
60                           false /* enable_intel_vp8_encoder */,
61                           true /* enable_h264_high_profile */));
62   return JavaToNativeVideoEncoderFactory(env, factory_object.obj());
63 }
64 
CreateAndroidDecoderFactory()65 std::unique_ptr<VideoDecoderFactory> CreateAndroidDecoderFactory() {
66   JNIEnv* env = AttachCurrentThreadIfNeeded();
67   ScopedJavaLocalRef<jclass> factory_class =
68       GetClass(env, "org/webrtc/HardwareVideoDecoderFactory");
69   jmethodID factory_constructor = env->GetMethodID(
70       factory_class.obj(), "<init>", "(Lorg/webrtc/EglBase$Context;)V");
71   ScopedJavaLocalRef<jobject> factory_object(
72       env, env->NewObject(factory_class.obj(), factory_constructor,
73                           nullptr /* shared_context */));
74   return JavaToNativeVideoDecoderFactory(env, factory_object.obj());
75 }
76 
77 }  // namespace test
78 }  // namespace webrtc
79