| #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 "api/video_track_source_proxy.h" |
| 19 | #include "AndroidContext.h" |
| 20 |
| 21 |
| 22 | namespace tgcalls { |
| 23 |
| 24 | void AndroidInterface::configurePlatformAudio() { |
| 25 |
| 26 | } |
| 27 |
| 28 | float AndroidInterface::getDisplayAspectRatio() { |
| 29 | return 0; |
| 30 | } |
| 31 |
| 32 | std::unique_ptr<webrtc::VideoEncoderFactory> AndroidInterface::makeVideoEncoderFactory(std::shared_ptr<PlatformContext> platformContext) { |
| 33 | JNIEnv *env = webrtc::AttachCurrentThreadIfNeeded(); |
| 34 |
| 35 | AndroidContext *context = (AndroidContext *) platformContext.get(); |
| 36 | jmethodID methodId = env->GetMethodID(context->getJavaCapturerClass(), "getSharedEGLContext", "()Lorg/webrtc/EglBase$Context;"); |
| 37 | jobject eglContext = env->CallObjectMethod(context->getJavaCapturer(), methodId); |
| 38 |
| 39 | webrtc::ScopedJavaLocalRef<jclass> factory_class = webrtc::GetClass(env, "org/webrtc/DefaultVideoEncoderFactory"); |
| 40 | jmethodID factory_constructor = env->GetMethodID(factory_class.obj(), "<init>", "(Lorg/webrtc/EglBase$Context;ZZ)V"); |
| 41 | webrtc::ScopedJavaLocalRef<jobject> factory_object(env, env->NewObject(factory_class.obj(), factory_constructor, eglContext, false, true)); |
| 42 | return webrtc::JavaToNativeVideoEncoderFactory(env, factory_object.obj()); |
| 43 | } |
| 44 |
| 45 | std::unique_ptr<webrtc::VideoDecoderFactory> AndroidInterface::makeVideoDecoderFactory(std::shared_ptr<PlatformContext> platformContext) { |
| 46 | JNIEnv *env = webrtc::AttachCurrentThreadIfNeeded(); |
| 47 |
| 48 | AndroidContext *context = (AndroidContext *) platformContext.get(); |
| 49 | jmethodID methodId = env->GetMethodID(context->getJavaCapturerClass(), "getSharedEGLContext", "()Lorg/webrtc/EglBase$Context;"); |
| 50 | jobject eglContext = env->CallObjectMethod(context->getJavaCapturer(), methodId); |
| 51 |
| 52 | webrtc::ScopedJavaLocalRef<jclass> factory_class = webrtc::GetClass(env, "org/webrtc/DefaultVideoDecoderFactory"); |
| 53 | jmethodID factory_constructor = env->GetMethodID(factory_class.obj(), "<init>", "(Lorg/webrtc/EglBase$Context;)V"); |
| 54 | webrtc::ScopedJavaLocalRef<jobject> factory_object(env, env->NewObject(factory_class.obj(), factory_constructor, eglContext)); |
| 55 | return webrtc::JavaToNativeVideoDecoderFactory(env, factory_object.obj()); |
| 56 | } |
| 57 |
| 58 | void AndroidInterface::adaptVideoSource(rtc::scoped_refptr<webrtc::VideoTrackSourceInterface> videoSource, int width, int height, int fps) { |
| 59 |
| 60 | } |
| 61 |
| 62 | rtc::scoped_refptr<webrtc::VideoTrackSourceInterface> AndroidInterface::makeVideoSource(rtc::Thread *signalingThread, rtc::Thread *workerThread) { |
| 63 | JNIEnv *env = webrtc::AttachCurrentThreadIfNeeded(); |
| 64 | _source = webrtc::CreateJavaVideoSource(env, signalingThread, false, false); |
| 65 | return webrtc::VideoTrackSourceProxy::Create(signalingThread, workerThread, _source); |
| 66 | } |
| 67 |
| 68 | bool AndroidInterface::supportsEncoding(const std::string &codecName, std::shared_ptr<PlatformContext> platformContext) { |
| 69 | if (hardwareVideoEncoderFactory == nullptr) { |
| 70 | JNIEnv *env = webrtc::AttachCurrentThreadIfNeeded(); |
| 71 |
| 72 | AndroidContext *context = (AndroidContext *) platformContext.get(); |
| 73 | jmethodID methodId = env->GetMethodID(context->getJavaCapturerClass(), "getSharedEGLContext", "()Lorg/webrtc/EglBase$Context;"); |
| 74 | jobject eglContext = env->CallObjectMethod(context->getJavaCapturer(), methodId); |
| 75 |
| 76 | webrtc::ScopedJavaLocalRef<jclass> factory_class = webrtc::GetClass(env, "org/webrtc/HardwareVideoEncoderFactory"); |
| 77 | jmethodID factory_constructor = env->GetMethodID(factory_class.obj(), "<init>", "(Lorg/webrtc/EglBase$Context;ZZ)V"); |
| 78 | webrtc::ScopedJavaLocalRef<jobject> factory_object(env, env->NewObject(factory_class.obj(), factory_constructor, eglContext, false, true)); |
| 79 | hardwareVideoEncoderFactory = webrtc::JavaToNativeVideoEncoderFactory(env, factory_object.obj()); |
| 80 | } |
| 81 | auto formats = hardwareVideoEncoderFactory->GetSupportedFormats(); |
| 82 | for (auto format : formats) { |
| 83 | if (format.name == codecName) { |
| 84 | return true; |
| 85 | } |
| 86 | } |
| 87 | return codecName == cricket::kVp8CodecName; |
| 88 | } |
| 89 |
| 90 | std::unique_ptr<VideoCapturerInterface> AndroidInterface::makeVideoCapturer(rtc::scoped_refptr<webrtc::VideoTrackSourceInterface> source, bool useFrontCamera, std::function<void(VideoState)> stateUpdated, std::shared_ptr<PlatformContext> platformContext, std::pair<int, int> &outResolution) { |
| 91 | return std::make_unique<VideoCapturerInterfaceImpl>(_source, useFrontCamera, stateUpdated, platformContext); |
| 92 | } |
| 93 |
| 94 |
| 95 | std::unique_ptr<PlatformInterface> CreatePlatformInterface() { |
| 96 | return std::make_unique<AndroidInterface>(); |
| 97 | } |
| 98 |
| 99 | } // namespace tgcalls |
| 100 |