| #include <algorithm> |
| 6 |
| 7 | #include "AndroidInterface.h" |
| 8 | #include "AndroidContext.h" |
| 9 | #include "sdk/android/native_api/jni/jvm.h" |
| 10 |
| 11 | namespace tgcalls { |
| 12 |
| 13 | VideoCameraCapturer::VideoCameraCapturer(rtc::scoped_refptr<webrtc::JavaVideoTrackSourceInterface> source, std::string deviceId, std::function<void(VideoState)> stateUpdated, std::shared_ptr<PlatformContext> platformContext) : _source(source), _stateUpdated(stateUpdated), _platformContext(platformContext) { |
| 14 | AndroidContext *context = (AndroidContext *) platformContext.get(); |
| 15 | JNIEnv *env = webrtc::AttachCurrentThreadIfNeeded(); |
| 16 | jmethodID methodId = env->GetMethodID(context->getJavaCapturerClass(), "init", "(JLjava/lang/String;)V"); |
| 17 | env->CallVoidMethod(context->getJavaCapturer(), methodId, (jlong) (intptr_t) this, env->NewStringUTF(deviceId.c_str())); |
| 18 | } |
| 19 |
| 20 | void VideoCameraCapturer::setState(VideoState state) { |
| 21 | _state = state; |
| 22 | if (_stateUpdated) { |
| 23 | _stateUpdated(_state); |
| 24 | } |
| 25 | JNIEnv *env = webrtc::AttachCurrentThreadIfNeeded(); |
| 26 | auto context = (AndroidContext *) _platformContext.get(); |
| 27 | jmethodID methodId = env->GetMethodID(context->getJavaCapturerClass(), "onStateChanged", "(JI)V"); |
| 28 | env->CallVoidMethod(context->getJavaCapturer(), methodId, (jlong) (intptr_t) this, (jint) state); |
| 29 | } |
| 30 |
| 31 | void VideoCameraCapturer::setPreferredCaptureAspectRatio(float aspectRatio) { |
| 32 | _aspectRatio = aspectRatio; |
| 33 | JNIEnv *env = webrtc::AttachCurrentThreadIfNeeded(); |
| 34 | auto context = (AndroidContext *) _platformContext.get(); |
| 35 | jmethodID methodId = env->GetMethodID(context->getJavaCapturerClass(), "onAspectRatioRequested", "(F)V"); |
| 36 | env->CallVoidMethod(context->getJavaCapturer(), methodId, (jfloat) aspectRatio); |
| 37 | } |
| 38 |
| 39 | void VideoCameraCapturer::setUncroppedSink(std::shared_ptr<rtc::VideoSinkInterface<webrtc::VideoFrame>> sink) { |
| 40 | if (_uncroppedSink != nullptr) { |
| 41 | _source->RemoveSink(_uncroppedSink.get()); |
| 42 | } |
| 43 | if (sink != nullptr) { |
| 44 | _source->AddOrUpdateSink(sink.get(), rtc::VideoSinkWants()); |
| 45 | } |
| 46 | _uncroppedSink = sink; |
| 47 | } |
| 48 |
| 49 | webrtc::ScopedJavaLocalRef<jobject> VideoCameraCapturer::GetJavaVideoCapturerObserver(JNIEnv *env) { |
| 50 | return _source->GetJavaVideoCapturerObserver(env); |
| 51 | } |
| 52 |
| 53 | } // namespace tgcalls |
| 54 |
| 55 | extern "C" { |
| 56 |
| 57 | JNIEXPORT jobject Java_org_telegram_messenger_voip_VideoCapturerDevice_nativeGetJavaVideoCapturerObserver(JNIEnv *env, jclass clazz, jlong ptr) { |
| 58 | tgcalls::VideoCameraCapturer *capturer = (tgcalls::VideoCameraCapturer *) (intptr_t) ptr; |
| 59 | return capturer->GetJavaVideoCapturerObserver(env).Release(); |
| 60 | } |
| 61 |
| 62 | } |