| #include <sdk/android/src/jni/android_video_track_source.h> |
| 6 | #include <sdk/android/src/jni/android_video_track_source.h> |
| 7 | #include <sdk/android/src/jni/pc/android_network_monitor.h> |
| 8 | #include <media/base/media_constants.h> |
| 9 |
| 10 | #include "VideoCapturerInterfaceImpl.h" |
| 11 |
| 12 | #include "sdk/android/native_api/base/init.h" |
| 13 | #include "sdk/android/native_api/codecs/wrapper.h" |
| 14 | #include "sdk/android/native_api/jni/class_loader.h" |
| 15 | #include "sdk/android/native_api/jni/jvm.h" |
| 16 | #include "sdk/android/native_api/jni/scoped_java_ref.h" |
| 17 | #include "sdk/android/native_api/video/video_source.h" |
| 18 | #include "api/video_codecs/builtin_video_encoder_factory.h" |
| 19 | #include "api/video_codecs/builtin_video_decoder_factory.h" |
| 20 | #include "api/video_track_source_proxy_factory.h" |
| 21 | #include "AndroidContext.h" |
| 22 |
| 23 |
| 24 | namespace tgcalls { |
| 25 |
| 26 | void AndroidInterface::configurePlatformAudio(int numChannels) { |
| 27 |
| 28 | } |
| 29 |
| 30 | std::unique_ptr<webrtc::VideoEncoderFactory> AndroidInterface::makeVideoEncoderFactory(std::shared_ptr<PlatformContext> platformContext, bool preferHardwareEncoding, bool isScreencast) { |
| 31 | JNIEnv *env = webrtc::AttachCurrentThreadIfNeeded(); |
| 32 |
| 33 | AndroidContext *context = (AndroidContext *) platformContext.get(); |
| 34 | jmethodID methodId = env->GetMethodID(context->getJavaCapturerClass(), "getSharedEGLContext", "()Lorg/webrtc/EglBase$Context;"); |
| 35 | jobject eglContext = env->CallObjectMethod(context->getJavaCapturer(), methodId); |
| 36 |
| 37 | webrtc::ScopedJavaLocalRef<jclass> factory_class = webrtc::GetClass(env, "org/webrtc/DefaultVideoEncoderFactory"); |
| 38 | jmethodID factory_constructor = env->GetMethodID(factory_class.obj(), "<init>", "(Lorg/webrtc/EglBase$Context;ZZ)V"); |
| 39 | webrtc::ScopedJavaLocalRef<jobject> factory_object(env, env->NewObject(factory_class.obj(), factory_constructor, eglContext, false, true)); |
| 40 | return webrtc::JavaToNativeVideoEncoderFactory(env, factory_object.obj()); |
| 41 | } |
| 42 |
| 43 | std::unique_ptr<webrtc::VideoDecoderFactory> AndroidInterface::makeVideoDecoderFactory(std::shared_ptr<PlatformContext> platformContext) { |
| 44 | JNIEnv *env = webrtc::AttachCurrentThreadIfNeeded(); |
| 45 |
| 46 | AndroidContext *context = (AndroidContext *) platformContext.get(); |
| 47 | jmethodID methodId = env->GetMethodID(context->getJavaCapturerClass(), "getSharedEGLContext", "()Lorg/webrtc/EglBase$Context;"); |
| 48 | jobject eglContext = env->CallObjectMethod(context->getJavaCapturer(), methodId); |
| 49 |
| 50 | webrtc::ScopedJavaLocalRef<jclass> factory_class = webrtc::GetClass(env, "org/webrtc/DefaultVideoDecoderFactory"); |
| 51 | jmethodID factory_constructor = env->GetMethodID(factory_class.obj(), "<init>", "(Lorg/webrtc/EglBase$Context;)V"); |
| 52 | webrtc::ScopedJavaLocalRef<jobject> factory_object(env, env->NewObject(factory_class.obj(), factory_constructor, eglContext)); |
| 53 | return webrtc::JavaToNativeVideoDecoderFactory(env, factory_object.obj()); |
| 54 | } |
| 55 |
| 56 | void AndroidInterface::adaptVideoSource(rtc::scoped_refptr<webrtc::VideoTrackSourceInterface> videoSource, int width, int height, int fps) { |
| 57 |
| 58 | } |
| 59 |
| 60 | rtc::scoped_refptr<webrtc::VideoTrackSourceInterface> AndroidInterface::makeVideoSource(rtc::Thread *signalingThread, rtc::Thread *workerThread, bool screencapture) { |
| 61 | JNIEnv *env = webrtc::AttachCurrentThreadIfNeeded(); |
| 62 | _source[screencapture ? 1 : 0] = webrtc::CreateJavaVideoSource(env, signalingThread, false, false); |
| 63 | return webrtc::CreateVideoTrackSourceProxy(signalingThread, workerThread, _source[screencapture ? 1 : 0].get()); |
| 64 | } |
| 65 |
| 66 | bool AndroidInterface::supportsEncoding(const std::string &codecName, std::shared_ptr<PlatformContext> platformContext) { |
| 67 | if (hardwareVideoEncoderFactory == nullptr) { |
| 68 | JNIEnv *env = webrtc::AttachCurrentThreadIfNeeded(); |
| 69 |
| 70 | AndroidContext *context = (AndroidContext *) platformContext.get(); |
| 71 | jmethodID methodId = env->GetMethodID(context->getJavaCapturerClass(), "getSharedEGLContext", "()Lorg/webrtc/EglBase$Context;"); |
| 72 | jobject eglContext = env->CallObjectMethod(context->getJavaCapturer(), methodId); |
| 73 |
| 74 | webrtc::ScopedJavaLocalRef<jclass> factory_class = webrtc::GetClass(env, "org/webrtc/HardwareVideoEncoderFactory"); |
| 75 | jmethodID factory_constructor = env->GetMethodID(factory_class.obj(), "<init>", "(Lorg/webrtc/EglBase$Context;ZZ)V"); |
| 76 | webrtc::ScopedJavaLocalRef<jobject> factory_object(env, env->NewObject(factory_class.obj(), factory_constructor, eglContext, false, true)); |
| 77 | hardwareVideoEncoderFactory = webrtc::JavaToNativeVideoEncoderFactory(env, factory_object.obj()); |
| 78 | } |
| 79 | auto formats = hardwareVideoEncoderFactory->GetSupportedFormats(); |
| 80 | for (auto format : formats) { |
| 81 | if (format.name == codecName) { |
| 82 | return true; |
| 83 | } |
| 84 | } |
| 85 | return codecName == cricket::kVp8CodecName; |
| 86 | } |
| 87 |
| 88 | 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) { |
| 89 | return std::make_unique<VideoCapturerInterfaceImpl>(_source[deviceId == "screen" ? 1 : 0], deviceId, stateUpdated, platformContext); |
| 90 | } |
| 91 |
| 92 | std::unique_ptr<rtc::NetworkMonitorFactory> AndroidInterface::createNetworkMonitorFactory() { |
| 93 | return std::make_unique<webrtc::jni::AndroidNetworkMonitorFactory>(); |
| 94 | } |
| 95 |
| 96 | std::unique_ptr<PlatformInterface> CreatePlatformInterface() { |
| 97 | return std::make_unique<AndroidInterface>(); |
| 98 | } |
| 99 |
| 100 | } // namespace tgcalls |
| 101 |