NekoX/TMessagesProj/jni/voip/tgcalls/platform/android/AndroidInterface.cpp

101 lines
5.4 KiB
C++
Raw Normal View History

2020-08-14 18:58:22 +02:00
#include "AndroidInterface.h"
#include <rtc_base/ssl_adapter.h>
#include <modules/utility/include/jvm_android.h>
#include <sdk/android/src/jni/android_video_track_source.h>
2021-06-25 02:43:10 +02:00
#include <sdk/android/src/jni/android_video_track_source.h>
#include <sdk/android/src/jni/pc/android_network_monitor.h>
2020-08-14 18:58:22 +02:00
#include <media/base/media_constants.h>
#include "VideoCapturerInterfaceImpl.h"
#include "sdk/android/native_api/base/init.h"
#include "sdk/android/native_api/codecs/wrapper.h"
#include "sdk/android/native_api/jni/class_loader.h"
#include "sdk/android/native_api/jni/jvm.h"
#include "sdk/android/native_api/jni/scoped_java_ref.h"
#include "sdk/android/native_api/video/video_source.h"
#include "api/video_codecs/builtin_video_encoder_factory.h"
#include "api/video_codecs/builtin_video_decoder_factory.h"
2022-03-11 17:49:54 +01:00
#include "api/video_track_source_proxy_factory.h"
2020-08-22 01:59:49 +02:00
#include "AndroidContext.h"
2020-08-14 18:58:22 +02:00
namespace tgcalls {
2020-08-15 23:06:36 +02:00
void AndroidInterface::configurePlatformAudio() {
}
2022-03-11 17:49:54 +01:00
std::unique_ptr<webrtc::VideoEncoderFactory> AndroidInterface::makeVideoEncoderFactory(std::shared_ptr<PlatformContext> platformContext, bool preferHardwareEncoding, bool isScreencast) {
2020-08-14 18:58:22 +02:00
JNIEnv *env = webrtc::AttachCurrentThreadIfNeeded();
2020-08-22 01:59:49 +02:00
AndroidContext *context = (AndroidContext *) platformContext.get();
jmethodID methodId = env->GetMethodID(context->getJavaCapturerClass(), "getSharedEGLContext", "()Lorg/webrtc/EglBase$Context;");
jobject eglContext = env->CallObjectMethod(context->getJavaCapturer(), methodId);
webrtc::ScopedJavaLocalRef<jclass> factory_class = webrtc::GetClass(env, "org/webrtc/DefaultVideoEncoderFactory");
jmethodID factory_constructor = env->GetMethodID(factory_class.obj(), "<init>", "(Lorg/webrtc/EglBase$Context;ZZ)V");
webrtc::ScopedJavaLocalRef<jobject> factory_object(env, env->NewObject(factory_class.obj(), factory_constructor, eglContext, false, true));
2020-08-14 18:58:22 +02:00
return webrtc::JavaToNativeVideoEncoderFactory(env, factory_object.obj());
}
2020-08-22 01:59:49 +02:00
std::unique_ptr<webrtc::VideoDecoderFactory> AndroidInterface::makeVideoDecoderFactory(std::shared_ptr<PlatformContext> platformContext) {
2020-08-14 18:58:22 +02:00
JNIEnv *env = webrtc::AttachCurrentThreadIfNeeded();
2020-08-22 01:59:49 +02:00
AndroidContext *context = (AndroidContext *) platformContext.get();
jmethodID methodId = env->GetMethodID(context->getJavaCapturerClass(), "getSharedEGLContext", "()Lorg/webrtc/EglBase$Context;");
jobject eglContext = env->CallObjectMethod(context->getJavaCapturer(), methodId);
webrtc::ScopedJavaLocalRef<jclass> factory_class = webrtc::GetClass(env, "org/webrtc/DefaultVideoDecoderFactory");
jmethodID factory_constructor = env->GetMethodID(factory_class.obj(), "<init>", "(Lorg/webrtc/EglBase$Context;)V");
webrtc::ScopedJavaLocalRef<jobject> factory_object(env, env->NewObject(factory_class.obj(), factory_constructor, eglContext));
2020-08-14 18:58:22 +02:00
return webrtc::JavaToNativeVideoDecoderFactory(env, factory_object.obj());
}
2020-08-15 23:06:36 +02:00
void AndroidInterface::adaptVideoSource(rtc::scoped_refptr<webrtc::VideoTrackSourceInterface> videoSource, int width, int height, int fps) {
}
2021-06-25 02:43:10 +02:00
rtc::scoped_refptr<webrtc::VideoTrackSourceInterface> AndroidInterface::makeVideoSource(rtc::Thread *signalingThread, rtc::Thread *workerThread, bool screencapture) {
2020-08-14 18:58:22 +02:00
JNIEnv *env = webrtc::AttachCurrentThreadIfNeeded();
2021-06-25 02:43:10 +02:00
_source[screencapture ? 1 : 0] = webrtc::CreateJavaVideoSource(env, signalingThread, false, false);
2022-03-11 17:49:54 +01:00
return webrtc::CreateVideoTrackSourceProxy(signalingThread, workerThread, _source[screencapture ? 1 : 0]);
2020-08-14 18:58:22 +02:00
}
2020-08-22 01:59:49 +02:00
bool AndroidInterface::supportsEncoding(const std::string &codecName, std::shared_ptr<PlatformContext> platformContext) {
2020-08-15 02:01:55 +02:00
if (hardwareVideoEncoderFactory == nullptr) {
2020-08-14 18:58:22 +02:00
JNIEnv *env = webrtc::AttachCurrentThreadIfNeeded();
2020-08-22 01:59:49 +02:00
AndroidContext *context = (AndroidContext *) platformContext.get();
jmethodID methodId = env->GetMethodID(context->getJavaCapturerClass(), "getSharedEGLContext", "()Lorg/webrtc/EglBase$Context;");
jobject eglContext = env->CallObjectMethod(context->getJavaCapturer(), methodId);
webrtc::ScopedJavaLocalRef<jclass> factory_class = webrtc::GetClass(env, "org/webrtc/HardwareVideoEncoderFactory");
jmethodID factory_constructor = env->GetMethodID(factory_class.obj(), "<init>", "(Lorg/webrtc/EglBase$Context;ZZ)V");
webrtc::ScopedJavaLocalRef<jobject> factory_object(env, env->NewObject(factory_class.obj(), factory_constructor, eglContext, false, true));
2020-08-14 18:58:22 +02:00
hardwareVideoEncoderFactory = webrtc::JavaToNativeVideoEncoderFactory(env, factory_object.obj());
}
auto formats = hardwareVideoEncoderFactory->GetSupportedFormats();
for (auto format : formats) {
if (format.name == codecName) {
return true;
}
}
return codecName == cricket::kVp8CodecName;
}
2020-10-01 03:59:32 +02:00
std::unique_ptr<VideoCapturerInterface> AndroidInterface::makeVideoCapturer(rtc::scoped_refptr<webrtc::VideoTrackSourceInterface> source, std::string deviceId, std::function<void(VideoState)> stateUpdated, std::function<void(PlatformCaptureInfo)> captureInfoUpdated, std::shared_ptr<PlatformContext> platformContext, std::pair<int, int> &outResolution) {
2021-06-25 02:43:10 +02:00
return std::make_unique<VideoCapturerInterfaceImpl>(_source[deviceId == "screen" ? 1 : 0], deviceId, stateUpdated, platformContext);
2020-08-14 18:58:22 +02:00
}
2021-06-25 02:43:10 +02:00
std::unique_ptr<rtc::NetworkMonitorFactory> AndroidInterface::createNetworkMonitorFactory() {
return std::make_unique<webrtc::jni::AndroidNetworkMonitorFactory>();
}
2020-08-14 18:58:22 +02:00
std::unique_ptr<PlatformInterface> CreatePlatformInterface() {
return std::make_unique<AndroidInterface>();
}
} // namespace tgcalls