1
+ /*
2
+ * Copyright 2021 Alex Andres
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ #include " JNI_VideoCapture.h"
18
+ #include " api/VideoTrackSink.h"
19
+ #include " media/video/VideoCapture.h"
20
+ #include " media/video/VideoDevice.h"
21
+ #include " JavaRef.h"
22
+ #include " JavaObject.h"
23
+ #include " JavaString.h"
24
+ #include " JavaUtils.h"
25
+
26
+ JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_video_VideoCapture_setVideoCaptureDevice
27
+ (JNIEnv * env, jobject caller, jobject device)
28
+ {
29
+ if (!device) {
30
+ env->Throw (jni::JavaNullPointerException (env, " VideoDevice is null" ));
31
+ return ;
32
+ }
33
+
34
+ jni::VideoCapture * videoSource = GetHandle<jni::VideoCapture>(env, caller);
35
+ CHECK_HANDLE (videoSource);
36
+
37
+ const auto javaClass = jni::JavaClasses::get<jni::VideoDevice::JavaVideoDeviceClass>(env);
38
+ const auto dev = jni::VideoDevice::toNativeVideoDevice (env, jni::JavaLocalRef<jobject>(env, device));
39
+
40
+ videoSource->setDevice (std::make_shared<jni::avdev::VideoDevice>(dev.getName (), dev.getDescriptor ()));
41
+ }
42
+
43
+ JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_video_VideoCapture_setVideoCaptureCapability
44
+ (JNIEnv * env, jobject caller, jobject jcapability)
45
+ {
46
+ jni::VideoCapture * videoSource = GetHandle<jni::VideoCapture>(env, caller);
47
+ CHECK_HANDLE (videoSource);
48
+
49
+ if (!jcapability) {
50
+ env->Throw (jni::JavaNullPointerException (env, " VideoCaptureCapability is null" ));
51
+ return ;
52
+ }
53
+
54
+ jint width = env->GetIntField (jcapability, GetFieldID (env, jcapability, " width" , " I" ));
55
+ jint height = env->GetIntField (jcapability, GetFieldID (env, jcapability, " height" , " I" ));
56
+ jint frameRate = env->GetIntField (jcapability, GetFieldID (env, jcapability, " frameRate" , " I" ));
57
+
58
+ webrtc::VideoCaptureCapability capability;
59
+ capability.width = static_cast <int32_t >(width);
60
+ capability.height = static_cast <int32_t >(height);
61
+ capability.maxFPS = static_cast <int32_t >(frameRate);
62
+
63
+ videoSource->setVideoCaptureCapability (capability);
64
+ }
65
+
66
+ JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_video_VideoCapture_setVideoSink
67
+ (JNIEnv * env, jobject caller, jobject jsink)
68
+ {
69
+ if (jsink == nullptr ) {
70
+ env->Throw (jni::JavaNullPointerException (env, " VideoTrackSink must not be null" ));
71
+ return ;
72
+ }
73
+
74
+ jni::VideoCapture * videoSource = GetHandle<jni::VideoCapture>(env, caller);
75
+ CHECK_HANDLE (videoSource);
76
+
77
+ videoSource->setVideoSink (std::make_unique<jni::VideoTrackSink>(env, jni::JavaGlobalRef<jobject>(env, jsink)));
78
+ }
79
+
80
+ JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_video_VideoCapture_start
81
+ (JNIEnv * env, jobject caller)
82
+ {
83
+ jni::VideoCapture * videoSource = GetHandle<jni::VideoCapture>(env, caller);
84
+ CHECK_HANDLE (videoSource);
85
+
86
+ try {
87
+ videoSource->start ();
88
+ }
89
+ catch (...) {
90
+ ThrowCxxJavaException (env);
91
+ }
92
+ }
93
+
94
+ JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_video_VideoCapture_stop
95
+ (JNIEnv * env, jobject caller)
96
+ {
97
+ jni::VideoCapture * videoSource = GetHandle<jni::VideoCapture>(env, caller);
98
+ CHECK_HANDLE (videoSource);
99
+
100
+ try {
101
+ videoSource->stop ();
102
+ }
103
+ catch (...) {
104
+ ThrowCxxJavaException (env);
105
+ }
106
+ }
107
+
108
+ JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_video_VideoCapture_dispose
109
+ (JNIEnv * env, jobject caller)
110
+ {
111
+ jni::VideoCapture * videoSource = GetHandle<jni::VideoCapture>(env, caller);
112
+ CHECK_HANDLE (videoSource);
113
+
114
+ SetHandle<std::nullptr_t >(env, caller, nullptr );
115
+
116
+ videoSource = nullptr ;
117
+ }
118
+
119
+ JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_video_VideoCapture_initialize
120
+ (JNIEnv * env, jobject caller)
121
+ {
122
+ SetHandle (env, caller, new jni::VideoCapture ());
123
+ }
0 commit comments