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