| #include <sdk/android/src/jni/android_video_track_source.h> |
| 6 | #include <media/base/media_constants.h> |
| 7 |
| 8 | #include "VideoCapturerInterfaceImpl.h" |
| 9 |
| 10 | #include "sdk/android/native_api/base/init.h" |
| 11 | #include "sdk/android/native_api/codecs/wrapper.h" |
| 12 | #include "sdk/android/native_api/jni/class_loader.h" |
| 13 | #include "sdk/android/native_api/jni/jvm.h" |
| 14 | #include "sdk/android/native_api/jni/scoped_java_ref.h" |
| 15 | #include "sdk/android/native_api/video/video_source.h" |
| 16 | #include "api/video_codecs/builtin_video_encoder_factory.h" |
| 17 | #include "api/video_codecs/builtin_video_decoder_factory.h" |
| 18 | #include "pc/video_track_source_proxy.h" |
| 19 | #include "sdk/android/src/jni/android_network_monitor.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 = |
| 38 | webrtc::GetClass(env, "org/webrtc/DefaultVideoEncoderFactory"); |
| 39 | jmethodID factory_constructor = env->GetMethodID( |
| 40 | factory_class.obj(), "<init>", "(Lorg/webrtc/EglBase$Context;ZZ)V"); |
| 41 | webrtc::ScopedJavaLocalRef<jobject> factory_object( |
| 42 | env, env->NewObject(factory_class.obj(), factory_constructor, |
| 43 | nullptr /* shared_context */, |
| 44 | false /* enable_intel_vp8_encoder */, |
| 45 | true /* enable_h264_high_profile */)); |
| 46 | return webrtc::JavaToNativeVideoEncoderFactory(env, factory_object.obj()); |
| 47 | } |
| 48 |
| 49 | std::unique_ptr<webrtc::VideoDecoderFactory> AndroidInterface::makeVideoDecoderFactory(std::shared_ptr<PlatformContext> platformContext) { |
| 50 | JNIEnv *env = webrtc::AttachCurrentThreadIfNeeded(); |
| 51 |
| 52 | // AndroidContext *context = (AndroidContext *) platformContext.get(); |
| 53 | // jmethodID methodId = env->GetMethodID(context->getJavaCapturerClass(), "getSharedEGLContext", "()Lorg/webrtc/EglBase$Context;"); |
| 54 | // jobject eglContext = env->CallObjectMethod(context->getJavaCapturer(), methodId); |
| 55 |
| 56 | webrtc::ScopedJavaLocalRef<jclass> factory_class = |
| 57 | webrtc::GetClass(env, "org/webrtc/DefaultVideoDecoderFactory"); |
| 58 | jmethodID factory_constructor = env->GetMethodID( |
| 59 | factory_class.obj(), "<init>", "(Lorg/webrtc/EglBase$Context;)V"); |
| 60 | webrtc::ScopedJavaLocalRef<jobject> factory_object( |
| 61 | env, env->NewObject(factory_class.obj(), factory_constructor, |
| 62 | nullptr /* shared_context */)); |
| 63 | return webrtc::JavaToNativeVideoDecoderFactory(env, factory_object.obj()); |
| 64 | } |
| 65 |
| 66 | void AndroidInterface::adaptVideoSource(rtc::scoped_refptr<webrtc::VideoTrackSourceInterface> videoSource, int width, int height, int fps) { |
| 67 |
| 68 | } |
| 69 |
| 70 | rtc::scoped_refptr<webrtc::VideoTrackSourceInterface> AndroidInterface::makeVideoSource(rtc::Thread *signalingThread, rtc::Thread *workerThread, bool screencapture) { |
| 71 | JNIEnv *env = webrtc::AttachCurrentThreadIfNeeded(); |
| 72 | _source[screencapture ? 1 : 0] = webrtc::CreateJavaVideoSource(env, signalingThread, false, false); |
| 73 | return webrtc::CreateVideoTrackSourceProxy(signalingThread, workerThread, _source[screencapture ? 1 : 0].get()); |
| 74 | } |
| 75 |
| 76 | bool AndroidInterface::supportsEncoding(const std::string &codecName, std::shared_ptr<PlatformContext> platformContext) { |
| 77 | if (hardwareVideoEncoderFactory == nullptr) { |
| 78 | JNIEnv *env = webrtc::AttachCurrentThreadIfNeeded(); |
| 79 |
| 80 | // AndroidContext *context = (AndroidContext *) platformContext.get(); |
| 81 | // jmethodID methodId = env->GetMethodID(context->getJavaCapturerClass(), "getSharedEGLContext", "()Lorg/webrtc/EglBase$Context;"); |
| 82 | // jobject eglContext = env->CallObjectMethod(context->getJavaCapturer(), methodId); |
| 83 |
| 84 | webrtc::ScopedJavaLocalRef<jclass> factory_class = |
| 85 | webrtc::GetClass(env, "org/webrtc/HardwareVideoEncoderFactory"); |
| 86 | jmethodID factory_constructor = env->GetMethodID( |
| 87 | factory_class.obj(), "<init>", "(Lorg/webrtc/EglBase$Context;ZZ)V"); |
| 88 | webrtc::ScopedJavaLocalRef<jobject> factory_object( |
| 89 | env, env->NewObject(factory_class.obj(), factory_constructor, |
| 90 | nullptr, |
| 91 | false, |
| 92 | true)); |
| 93 | hardwareVideoEncoderFactory = webrtc::JavaToNativeVideoEncoderFactory(env, factory_object.obj()); |
| 94 | } |
| 95 | auto formats = hardwareVideoEncoderFactory->GetSupportedFormats(); |
| 96 | for (auto format : formats) { |
| 97 | if (format.name == codecName) { |
| 98 | return true; |
| 99 | } |
| 100 | } |
| 101 | return codecName == cricket::kVp8CodecName; |
| 102 | } |
| 103 |
| 104 | std::unique_ptr<VideoCapturerInterface> AndroidInterface::makeVideoCapturer( |
| 105 | rtc::scoped_refptr<webrtc::VideoTrackSourceInterface> source, |
| 106 | std::string deviceId, |
| 107 | std::function<void(VideoState)> stateUpdated, |
| 108 | std::function<void(PlatformCaptureInfo)> captureInfoUpdated, |
| 109 | std::shared_ptr<PlatformContext> platformContext, |
| 110 | std::pair<int, int> &outResolution |
| 111 | ) { |
| 112 | return std::make_unique<VideoCapturerInterfaceImpl>(_source[deviceId == "screen" ? 1 : 0], deviceId, stateUpdated, platformContext); |
| 113 | } |
| 114 |
| 115 | std::unique_ptr<rtc::NetworkMonitorFactory> AndroidInterface::createNetworkMonitorFactory() { |
| 116 | return std::make_unique<webrtc::jni::AndroidNetworkMonitorFactory>(); |
| 117 | } |
| 118 |
| 119 | std::unique_ptr<PlatformInterface> CreatePlatformInterface() { |
| 120 | return std::make_unique<AndroidInterface>(); |
| 121 | } |
| 122 |
| 123 | } // namespace tgcalls |
| 124 |