OLD | NEW |
| (Empty) |
1 /* | |
2 * libjingle | |
3 * Copyright 2013 Google Inc. | |
4 * | |
5 * Redistribution and use in source and binary forms, with or without | |
6 * modification, are permitted provided that the following conditions are met: | |
7 * | |
8 * 1. Redistributions of source code must retain the above copyright notice, | |
9 * this list of conditions and the following disclaimer. | |
10 * 2. Redistributions in binary form must reproduce the above copyright notice, | |
11 * this list of conditions and the following disclaimer in the documentation | |
12 * and/or other materials provided with the distribution. | |
13 * 3. The name of the author may not be used to endorse or promote products | |
14 * derived from this software without specific prior written permission. | |
15 * | |
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED | |
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF | |
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO | |
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; | |
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | |
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR | |
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF | |
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
26 */ | |
27 | |
28 // Hints for future visitors: | |
29 // This entire file is an implementation detail of the org.webrtc Java package, | |
30 // the most interesting bits of which are org.webrtc.PeerConnection{,Factory}. | |
31 // The layout of this file is roughly: | |
32 // - various helper C++ functions & classes that wrap Java counterparts and | |
33 // expose a C++ interface that can be passed to the C++ PeerConnection APIs | |
34 // - implementations of methods declared "static" in the Java package (named | |
35 // things like Java_org_webrtc_OMG_Can_This_Name_Be_Any_Longer, prescribed by | |
36 // the JNI spec). | |
37 // | |
38 // Lifecycle notes: objects are owned where they will be called; in other words | |
39 // FooObservers are owned by C++-land, and user-callable objects (e.g. | |
40 // PeerConnection and VideoTrack) are owned by Java-land. | |
41 // When this file allocates C++ RefCountInterfaces it AddRef()s an artificial | |
42 // ref simulating the jlong held in Java-land, and then Release()s the ref in | |
43 // the respective free call. Sometimes this AddRef is implicit in the | |
44 // construction of a scoped_refptr<> which is then .release()d. | |
45 // Any persistent (non-local) references from C++ to Java must be global or weak | |
46 // (in which case they must be checked before use)! | |
47 // | |
48 // Exception notes: pretty much all JNI calls can throw Java exceptions, so each | |
49 // call through a JNIEnv* pointer needs to be followed by an ExceptionCheck() | |
50 // call. In this file this is done in CHECK_EXCEPTION, making for much easier | |
51 // debugging in case of failure (the alternative is to wait for control to | |
52 // return to the Java frame that called code in this file, at which point it's | |
53 // impossible to tell which JNI call broke). | |
54 | |
55 #include <jni.h> | |
56 #undef JNIEXPORT | |
57 #define JNIEXPORT __attribute__((visibility("default"))) | |
58 | |
59 #include <limits> | |
60 #include <utility> | |
61 | |
62 #include "talk/app/webrtc/androidvideocapturer.h" | |
63 #include "talk/app/webrtc/dtlsidentitystore.h" | |
64 #include "talk/app/webrtc/java/jni/androidmediadecoder_jni.h" | |
65 #include "talk/app/webrtc/java/jni/androidmediaencoder_jni.h" | |
66 #include "talk/app/webrtc/java/jni/androidvideocapturer_jni.h" | |
67 #include "talk/app/webrtc/java/jni/androidnetworkmonitor_jni.h" | |
68 #include "talk/app/webrtc/java/jni/classreferenceholder.h" | |
69 #include "talk/app/webrtc/java/jni/jni_helpers.h" | |
70 #include "talk/app/webrtc/java/jni/native_handle_impl.h" | |
71 #include "talk/app/webrtc/mediaconstraintsinterface.h" | |
72 #include "talk/app/webrtc/peerconnectioninterface.h" | |
73 #include "talk/app/webrtc/rtpreceiverinterface.h" | |
74 #include "talk/app/webrtc/rtpsenderinterface.h" | |
75 #include "talk/app/webrtc/videosourceinterface.h" | |
76 #include "webrtc/base/bind.h" | |
77 #include "webrtc/base/checks.h" | |
78 #include "webrtc/base/event_tracer.h" | |
79 #include "webrtc/base/logging.h" | |
80 #include "webrtc/base/logsinks.h" | |
81 #include "webrtc/base/messagequeue.h" | |
82 #include "webrtc/base/networkmonitor.h" | |
83 #include "webrtc/base/ssladapter.h" | |
84 #include "webrtc/base/stringutils.h" | |
85 #include "webrtc/media/base/videocapturer.h" | |
86 #include "webrtc/media/base/videorenderer.h" | |
87 #include "webrtc/media/devices/videorendererfactory.h" | |
88 #include "webrtc/media/webrtc/webrtcvideodecoderfactory.h" | |
89 #include "webrtc/media/webrtc/webrtcvideoencoderfactory.h" | |
90 #include "webrtc/system_wrappers/include/field_trial_default.h" | |
91 #include "webrtc/system_wrappers/include/logcat_trace_context.h" | |
92 #include "webrtc/system_wrappers/include/trace.h" | |
93 #include "webrtc/voice_engine/include/voe_base.h" | |
94 | |
95 using cricket::WebRtcVideoDecoderFactory; | |
96 using cricket::WebRtcVideoEncoderFactory; | |
97 using rtc::Bind; | |
98 using rtc::Thread; | |
99 using rtc::ThreadManager; | |
100 using rtc::scoped_ptr; | |
101 using webrtc::AudioSourceInterface; | |
102 using webrtc::AudioTrackInterface; | |
103 using webrtc::AudioTrackVector; | |
104 using webrtc::CreateSessionDescriptionObserver; | |
105 using webrtc::DataBuffer; | |
106 using webrtc::DataChannelInit; | |
107 using webrtc::DataChannelInterface; | |
108 using webrtc::DataChannelObserver; | |
109 using webrtc::IceCandidateInterface; | |
110 using webrtc::LogcatTraceContext; | |
111 using webrtc::MediaConstraintsInterface; | |
112 using webrtc::MediaSourceInterface; | |
113 using webrtc::MediaStreamInterface; | |
114 using webrtc::MediaStreamTrackInterface; | |
115 using webrtc::PeerConnectionFactoryInterface; | |
116 using webrtc::PeerConnectionInterface; | |
117 using webrtc::PeerConnectionObserver; | |
118 using webrtc::RtpReceiverInterface; | |
119 using webrtc::RtpSenderInterface; | |
120 using webrtc::SessionDescriptionInterface; | |
121 using webrtc::SetSessionDescriptionObserver; | |
122 using webrtc::StatsObserver; | |
123 using webrtc::StatsReport; | |
124 using webrtc::StatsReports; | |
125 using webrtc::VideoRendererInterface; | |
126 using webrtc::VideoSourceInterface; | |
127 using webrtc::VideoTrackInterface; | |
128 using webrtc::VideoTrackVector; | |
129 using webrtc::kVideoCodecVP8; | |
130 | |
131 namespace webrtc_jni { | |
132 | |
133 // Field trials initialization string | |
134 static char *field_trials_init_string = NULL; | |
135 | |
136 // Set in PeerConnectionFactory_initializeAndroidGlobals(). | |
137 static bool factory_static_initialized = false; | |
138 static bool video_hw_acceleration_enabled = true; | |
139 | |
140 // Return the (singleton) Java Enum object corresponding to |index|; | |
141 // |state_class_fragment| is something like "MediaSource$State". | |
142 static jobject JavaEnumFromIndex( | |
143 JNIEnv* jni, const std::string& state_class_fragment, int index) { | |
144 const std::string state_class = "org/webrtc/" + state_class_fragment; | |
145 return JavaEnumFromIndex(jni, FindClass(jni, state_class.c_str()), | |
146 state_class, index); | |
147 } | |
148 | |
149 static DataChannelInit JavaDataChannelInitToNative( | |
150 JNIEnv* jni, jobject j_init) { | |
151 DataChannelInit init; | |
152 | |
153 jclass j_init_class = FindClass(jni, "org/webrtc/DataChannel$Init"); | |
154 jfieldID ordered_id = GetFieldID(jni, j_init_class, "ordered", "Z"); | |
155 jfieldID max_retransmit_time_id = | |
156 GetFieldID(jni, j_init_class, "maxRetransmitTimeMs", "I"); | |
157 jfieldID max_retransmits_id = | |
158 GetFieldID(jni, j_init_class, "maxRetransmits", "I"); | |
159 jfieldID protocol_id = | |
160 GetFieldID(jni, j_init_class, "protocol", "Ljava/lang/String;"); | |
161 jfieldID negotiated_id = GetFieldID(jni, j_init_class, "negotiated", "Z"); | |
162 jfieldID id_id = GetFieldID(jni, j_init_class, "id", "I"); | |
163 | |
164 init.ordered = GetBooleanField(jni, j_init, ordered_id); | |
165 init.maxRetransmitTime = GetIntField(jni, j_init, max_retransmit_time_id); | |
166 init.maxRetransmits = GetIntField(jni, j_init, max_retransmits_id); | |
167 init.protocol = JavaToStdString( | |
168 jni, GetStringField(jni, j_init, protocol_id)); | |
169 init.negotiated = GetBooleanField(jni, j_init, negotiated_id); | |
170 init.id = GetIntField(jni, j_init, id_id); | |
171 | |
172 return init; | |
173 } | |
174 | |
175 class ConstraintsWrapper; | |
176 | |
177 // Adapter between the C++ PeerConnectionObserver interface and the Java | |
178 // PeerConnection.Observer interface. Wraps an instance of the Java interface | |
179 // and dispatches C++ callbacks to Java. | |
180 class PCOJava : public PeerConnectionObserver { | |
181 public: | |
182 PCOJava(JNIEnv* jni, jobject j_observer) | |
183 : j_observer_global_(jni, j_observer), | |
184 j_observer_class_(jni, GetObjectClass(jni, *j_observer_global_)), | |
185 j_media_stream_class_(jni, FindClass(jni, "org/webrtc/MediaStream")), | |
186 j_media_stream_ctor_(GetMethodID( | |
187 jni, *j_media_stream_class_, "<init>", "(J)V")), | |
188 j_audio_track_class_(jni, FindClass(jni, "org/webrtc/AudioTrack")), | |
189 j_audio_track_ctor_(GetMethodID( | |
190 jni, *j_audio_track_class_, "<init>", "(J)V")), | |
191 j_video_track_class_(jni, FindClass(jni, "org/webrtc/VideoTrack")), | |
192 j_video_track_ctor_(GetMethodID( | |
193 jni, *j_video_track_class_, "<init>", "(J)V")), | |
194 j_data_channel_class_(jni, FindClass(jni, "org/webrtc/DataChannel")), | |
195 j_data_channel_ctor_(GetMethodID( | |
196 jni, *j_data_channel_class_, "<init>", "(J)V")) { | |
197 } | |
198 | |
199 virtual ~PCOJava() { | |
200 ScopedLocalRefFrame local_ref_frame(jni()); | |
201 while (!remote_streams_.empty()) | |
202 DisposeRemoteStream(remote_streams_.begin()); | |
203 } | |
204 | |
205 void OnIceCandidate(const IceCandidateInterface* candidate) override { | |
206 ScopedLocalRefFrame local_ref_frame(jni()); | |
207 std::string sdp; | |
208 RTC_CHECK(candidate->ToString(&sdp)) << "got so far: " << sdp; | |
209 jclass candidate_class = FindClass(jni(), "org/webrtc/IceCandidate"); | |
210 jmethodID ctor = GetMethodID(jni(), candidate_class, | |
211 "<init>", "(Ljava/lang/String;ILjava/lang/String;)V"); | |
212 jstring j_mid = JavaStringFromStdString(jni(), candidate->sdp_mid()); | |
213 jstring j_sdp = JavaStringFromStdString(jni(), sdp); | |
214 jobject j_candidate = jni()->NewObject( | |
215 candidate_class, ctor, j_mid, candidate->sdp_mline_index(), j_sdp); | |
216 CHECK_EXCEPTION(jni()) << "error during NewObject"; | |
217 jmethodID m = GetMethodID(jni(), *j_observer_class_, | |
218 "onIceCandidate", "(Lorg/webrtc/IceCandidate;)V"); | |
219 jni()->CallVoidMethod(*j_observer_global_, m, j_candidate); | |
220 CHECK_EXCEPTION(jni()) << "error during CallVoidMethod"; | |
221 } | |
222 | |
223 void OnSignalingChange( | |
224 PeerConnectionInterface::SignalingState new_state) override { | |
225 ScopedLocalRefFrame local_ref_frame(jni()); | |
226 jmethodID m = GetMethodID( | |
227 jni(), *j_observer_class_, "onSignalingChange", | |
228 "(Lorg/webrtc/PeerConnection$SignalingState;)V"); | |
229 jobject new_state_enum = | |
230 JavaEnumFromIndex(jni(), "PeerConnection$SignalingState", new_state); | |
231 jni()->CallVoidMethod(*j_observer_global_, m, new_state_enum); | |
232 CHECK_EXCEPTION(jni()) << "error during CallVoidMethod"; | |
233 } | |
234 | |
235 void OnIceConnectionChange( | |
236 PeerConnectionInterface::IceConnectionState new_state) override { | |
237 ScopedLocalRefFrame local_ref_frame(jni()); | |
238 jmethodID m = GetMethodID( | |
239 jni(), *j_observer_class_, "onIceConnectionChange", | |
240 "(Lorg/webrtc/PeerConnection$IceConnectionState;)V"); | |
241 jobject new_state_enum = JavaEnumFromIndex( | |
242 jni(), "PeerConnection$IceConnectionState", new_state); | |
243 jni()->CallVoidMethod(*j_observer_global_, m, new_state_enum); | |
244 CHECK_EXCEPTION(jni()) << "error during CallVoidMethod"; | |
245 } | |
246 | |
247 void OnIceConnectionReceivingChange(bool receiving) override { | |
248 ScopedLocalRefFrame local_ref_frame(jni()); | |
249 jmethodID m = GetMethodID( | |
250 jni(), *j_observer_class_, "onIceConnectionReceivingChange", "(Z)V"); | |
251 jni()->CallVoidMethod(*j_observer_global_, m, receiving); | |
252 CHECK_EXCEPTION(jni()) << "error during CallVoidMethod"; | |
253 } | |
254 | |
255 void OnIceGatheringChange( | |
256 PeerConnectionInterface::IceGatheringState new_state) override { | |
257 ScopedLocalRefFrame local_ref_frame(jni()); | |
258 jmethodID m = GetMethodID( | |
259 jni(), *j_observer_class_, "onIceGatheringChange", | |
260 "(Lorg/webrtc/PeerConnection$IceGatheringState;)V"); | |
261 jobject new_state_enum = JavaEnumFromIndex( | |
262 jni(), "PeerConnection$IceGatheringState", new_state); | |
263 jni()->CallVoidMethod(*j_observer_global_, m, new_state_enum); | |
264 CHECK_EXCEPTION(jni()) << "error during CallVoidMethod"; | |
265 } | |
266 | |
267 void OnAddStream(MediaStreamInterface* stream) override { | |
268 ScopedLocalRefFrame local_ref_frame(jni()); | |
269 // Java MediaStream holds one reference. Corresponding Release() is in | |
270 // MediaStream_free, triggered by MediaStream.dispose(). | |
271 stream->AddRef(); | |
272 jobject j_stream = | |
273 jni()->NewObject(*j_media_stream_class_, j_media_stream_ctor_, | |
274 reinterpret_cast<jlong>(stream)); | |
275 CHECK_EXCEPTION(jni()) << "error during NewObject"; | |
276 | |
277 for (const auto& track : stream->GetAudioTracks()) { | |
278 jstring id = JavaStringFromStdString(jni(), track->id()); | |
279 // Java AudioTrack holds one reference. Corresponding Release() is in | |
280 // MediaStreamTrack_free, triggered by AudioTrack.dispose(). | |
281 track->AddRef(); | |
282 jobject j_track = | |
283 jni()->NewObject(*j_audio_track_class_, j_audio_track_ctor_, | |
284 reinterpret_cast<jlong>(track.get()), id); | |
285 CHECK_EXCEPTION(jni()) << "error during NewObject"; | |
286 jfieldID audio_tracks_id = GetFieldID(jni(), | |
287 *j_media_stream_class_, | |
288 "audioTracks", | |
289 "Ljava/util/LinkedList;"); | |
290 jobject audio_tracks = GetObjectField(jni(), j_stream, audio_tracks_id); | |
291 jmethodID add = GetMethodID(jni(), | |
292 GetObjectClass(jni(), audio_tracks), | |
293 "add", | |
294 "(Ljava/lang/Object;)Z"); | |
295 jboolean added = jni()->CallBooleanMethod(audio_tracks, add, j_track); | |
296 CHECK_EXCEPTION(jni()) << "error during CallBooleanMethod"; | |
297 RTC_CHECK(added); | |
298 } | |
299 | |
300 for (const auto& track : stream->GetVideoTracks()) { | |
301 jstring id = JavaStringFromStdString(jni(), track->id()); | |
302 // Java VideoTrack holds one reference. Corresponding Release() is in | |
303 // MediaStreamTrack_free, triggered by VideoTrack.dispose(). | |
304 track->AddRef(); | |
305 jobject j_track = | |
306 jni()->NewObject(*j_video_track_class_, j_video_track_ctor_, | |
307 reinterpret_cast<jlong>(track.get()), id); | |
308 CHECK_EXCEPTION(jni()) << "error during NewObject"; | |
309 jfieldID video_tracks_id = GetFieldID(jni(), | |
310 *j_media_stream_class_, | |
311 "videoTracks", | |
312 "Ljava/util/LinkedList;"); | |
313 jobject video_tracks = GetObjectField(jni(), j_stream, video_tracks_id); | |
314 jmethodID add = GetMethodID(jni(), | |
315 GetObjectClass(jni(), video_tracks), | |
316 "add", | |
317 "(Ljava/lang/Object;)Z"); | |
318 jboolean added = jni()->CallBooleanMethod(video_tracks, add, j_track); | |
319 CHECK_EXCEPTION(jni()) << "error during CallBooleanMethod"; | |
320 RTC_CHECK(added); | |
321 } | |
322 remote_streams_[stream] = NewGlobalRef(jni(), j_stream); | |
323 | |
324 jmethodID m = GetMethodID(jni(), *j_observer_class_, "onAddStream", | |
325 "(Lorg/webrtc/MediaStream;)V"); | |
326 jni()->CallVoidMethod(*j_observer_global_, m, j_stream); | |
327 CHECK_EXCEPTION(jni()) << "error during CallVoidMethod"; | |
328 } | |
329 | |
330 void OnRemoveStream(MediaStreamInterface* stream) override { | |
331 ScopedLocalRefFrame local_ref_frame(jni()); | |
332 NativeToJavaStreamsMap::iterator it = remote_streams_.find(stream); | |
333 RTC_CHECK(it != remote_streams_.end()) << "unexpected stream: " << std::hex | |
334 << stream; | |
335 jobject j_stream = it->second; | |
336 jmethodID m = GetMethodID(jni(), *j_observer_class_, "onRemoveStream", | |
337 "(Lorg/webrtc/MediaStream;)V"); | |
338 jni()->CallVoidMethod(*j_observer_global_, m, j_stream); | |
339 CHECK_EXCEPTION(jni()) << "error during CallVoidMethod"; | |
340 DisposeRemoteStream(it); | |
341 } | |
342 | |
343 void OnDataChannel(DataChannelInterface* channel) override { | |
344 ScopedLocalRefFrame local_ref_frame(jni()); | |
345 jobject j_channel = jni()->NewObject( | |
346 *j_data_channel_class_, j_data_channel_ctor_, (jlong)channel); | |
347 CHECK_EXCEPTION(jni()) << "error during NewObject"; | |
348 | |
349 jmethodID m = GetMethodID(jni(), *j_observer_class_, "onDataChannel", | |
350 "(Lorg/webrtc/DataChannel;)V"); | |
351 jni()->CallVoidMethod(*j_observer_global_, m, j_channel); | |
352 | |
353 // Channel is now owned by Java object, and will be freed from | |
354 // DataChannel.dispose(). Important that this be done _after_ the | |
355 // CallVoidMethod above as Java code might call back into native code and be | |
356 // surprised to see a refcount of 2. | |
357 int bumped_count = channel->AddRef(); | |
358 RTC_CHECK(bumped_count == 2) << "Unexpected refcount OnDataChannel"; | |
359 | |
360 CHECK_EXCEPTION(jni()) << "error during CallVoidMethod"; | |
361 } | |
362 | |
363 void OnRenegotiationNeeded() override { | |
364 ScopedLocalRefFrame local_ref_frame(jni()); | |
365 jmethodID m = | |
366 GetMethodID(jni(), *j_observer_class_, "onRenegotiationNeeded", "()V"); | |
367 jni()->CallVoidMethod(*j_observer_global_, m); | |
368 CHECK_EXCEPTION(jni()) << "error during CallVoidMethod"; | |
369 } | |
370 | |
371 void SetConstraints(ConstraintsWrapper* constraints) { | |
372 RTC_CHECK(!constraints_.get()) << "constraints already set!"; | |
373 constraints_.reset(constraints); | |
374 } | |
375 | |
376 const ConstraintsWrapper* constraints() { return constraints_.get(); } | |
377 | |
378 private: | |
379 typedef std::map<MediaStreamInterface*, jobject> NativeToJavaStreamsMap; | |
380 | |
381 void DisposeRemoteStream(const NativeToJavaStreamsMap::iterator& it) { | |
382 jobject j_stream = it->second; | |
383 remote_streams_.erase(it); | |
384 jni()->CallVoidMethod( | |
385 j_stream, GetMethodID(jni(), *j_media_stream_class_, "dispose", "()V")); | |
386 CHECK_EXCEPTION(jni()) << "error during MediaStream.dispose()"; | |
387 DeleteGlobalRef(jni(), j_stream); | |
388 } | |
389 | |
390 JNIEnv* jni() { | |
391 return AttachCurrentThreadIfNeeded(); | |
392 } | |
393 | |
394 const ScopedGlobalRef<jobject> j_observer_global_; | |
395 const ScopedGlobalRef<jclass> j_observer_class_; | |
396 const ScopedGlobalRef<jclass> j_media_stream_class_; | |
397 const jmethodID j_media_stream_ctor_; | |
398 const ScopedGlobalRef<jclass> j_audio_track_class_; | |
399 const jmethodID j_audio_track_ctor_; | |
400 const ScopedGlobalRef<jclass> j_video_track_class_; | |
401 const jmethodID j_video_track_ctor_; | |
402 const ScopedGlobalRef<jclass> j_data_channel_class_; | |
403 const jmethodID j_data_channel_ctor_; | |
404 // C++ -> Java remote streams. The stored jobects are global refs and must be | |
405 // manually deleted upon removal. Use DisposeRemoteStream(). | |
406 NativeToJavaStreamsMap remote_streams_; | |
407 scoped_ptr<ConstraintsWrapper> constraints_; | |
408 }; | |
409 | |
410 // Wrapper for a Java MediaConstraints object. Copies all needed data so when | |
411 // the constructor returns the Java object is no longer needed. | |
412 class ConstraintsWrapper : public MediaConstraintsInterface { | |
413 public: | |
414 ConstraintsWrapper(JNIEnv* jni, jobject j_constraints) { | |
415 PopulateConstraintsFromJavaPairList( | |
416 jni, j_constraints, "mandatory", &mandatory_); | |
417 PopulateConstraintsFromJavaPairList( | |
418 jni, j_constraints, "optional", &optional_); | |
419 } | |
420 | |
421 virtual ~ConstraintsWrapper() {} | |
422 | |
423 // MediaConstraintsInterface. | |
424 const Constraints& GetMandatory() const override { return mandatory_; } | |
425 | |
426 const Constraints& GetOptional() const override { return optional_; } | |
427 | |
428 private: | |
429 // Helper for translating a List<Pair<String, String>> to a Constraints. | |
430 static void PopulateConstraintsFromJavaPairList( | |
431 JNIEnv* jni, jobject j_constraints, | |
432 const char* field_name, Constraints* field) { | |
433 jfieldID j_id = GetFieldID(jni, | |
434 GetObjectClass(jni, j_constraints), field_name, "Ljava/util/List;"); | |
435 jobject j_list = GetObjectField(jni, j_constraints, j_id); | |
436 jmethodID j_iterator_id = GetMethodID(jni, | |
437 GetObjectClass(jni, j_list), "iterator", "()Ljava/util/Iterator;"); | |
438 jobject j_iterator = jni->CallObjectMethod(j_list, j_iterator_id); | |
439 CHECK_EXCEPTION(jni) << "error during CallObjectMethod"; | |
440 jmethodID j_has_next = GetMethodID(jni, | |
441 GetObjectClass(jni, j_iterator), "hasNext", "()Z"); | |
442 jmethodID j_next = GetMethodID(jni, | |
443 GetObjectClass(jni, j_iterator), "next", "()Ljava/lang/Object;"); | |
444 while (jni->CallBooleanMethod(j_iterator, j_has_next)) { | |
445 CHECK_EXCEPTION(jni) << "error during CallBooleanMethod"; | |
446 jobject entry = jni->CallObjectMethod(j_iterator, j_next); | |
447 CHECK_EXCEPTION(jni) << "error during CallObjectMethod"; | |
448 jmethodID get_key = GetMethodID(jni, | |
449 GetObjectClass(jni, entry), "getKey", "()Ljava/lang/String;"); | |
450 jstring j_key = reinterpret_cast<jstring>( | |
451 jni->CallObjectMethod(entry, get_key)); | |
452 CHECK_EXCEPTION(jni) << "error during CallObjectMethod"; | |
453 jmethodID get_value = GetMethodID(jni, | |
454 GetObjectClass(jni, entry), "getValue", "()Ljava/lang/String;"); | |
455 jstring j_value = reinterpret_cast<jstring>( | |
456 jni->CallObjectMethod(entry, get_value)); | |
457 CHECK_EXCEPTION(jni) << "error during CallObjectMethod"; | |
458 field->push_back(Constraint(JavaToStdString(jni, j_key), | |
459 JavaToStdString(jni, j_value))); | |
460 } | |
461 CHECK_EXCEPTION(jni) << "error during CallBooleanMethod"; | |
462 } | |
463 | |
464 Constraints mandatory_; | |
465 Constraints optional_; | |
466 }; | |
467 | |
468 static jobject JavaSdpFromNativeSdp( | |
469 JNIEnv* jni, const SessionDescriptionInterface* desc) { | |
470 std::string sdp; | |
471 RTC_CHECK(desc->ToString(&sdp)) << "got so far: " << sdp; | |
472 jstring j_description = JavaStringFromStdString(jni, sdp); | |
473 | |
474 jclass j_type_class = FindClass( | |
475 jni, "org/webrtc/SessionDescription$Type"); | |
476 jmethodID j_type_from_canonical = GetStaticMethodID( | |
477 jni, j_type_class, "fromCanonicalForm", | |
478 "(Ljava/lang/String;)Lorg/webrtc/SessionDescription$Type;"); | |
479 jstring j_type_string = JavaStringFromStdString(jni, desc->type()); | |
480 jobject j_type = jni->CallStaticObjectMethod( | |
481 j_type_class, j_type_from_canonical, j_type_string); | |
482 CHECK_EXCEPTION(jni) << "error during CallObjectMethod"; | |
483 | |
484 jclass j_sdp_class = FindClass(jni, "org/webrtc/SessionDescription"); | |
485 jmethodID j_sdp_ctor = GetMethodID( | |
486 jni, j_sdp_class, "<init>", | |
487 "(Lorg/webrtc/SessionDescription$Type;Ljava/lang/String;)V"); | |
488 jobject j_sdp = jni->NewObject( | |
489 j_sdp_class, j_sdp_ctor, j_type, j_description); | |
490 CHECK_EXCEPTION(jni) << "error during NewObject"; | |
491 return j_sdp; | |
492 } | |
493 | |
494 template <class T> // T is one of {Create,Set}SessionDescriptionObserver. | |
495 class SdpObserverWrapper : public T { | |
496 public: | |
497 SdpObserverWrapper(JNIEnv* jni, jobject j_observer, | |
498 ConstraintsWrapper* constraints) | |
499 : constraints_(constraints), | |
500 j_observer_global_(jni, j_observer), | |
501 j_observer_class_(jni, GetObjectClass(jni, j_observer)) { | |
502 } | |
503 | |
504 virtual ~SdpObserverWrapper() {} | |
505 | |
506 // Can't mark override because of templating. | |
507 virtual void OnSuccess() { | |
508 ScopedLocalRefFrame local_ref_frame(jni()); | |
509 jmethodID m = GetMethodID(jni(), *j_observer_class_, "onSetSuccess", "()V"); | |
510 jni()->CallVoidMethod(*j_observer_global_, m); | |
511 CHECK_EXCEPTION(jni()) << "error during CallVoidMethod"; | |
512 } | |
513 | |
514 // Can't mark override because of templating. | |
515 virtual void OnSuccess(SessionDescriptionInterface* desc) { | |
516 ScopedLocalRefFrame local_ref_frame(jni()); | |
517 jmethodID m = GetMethodID( | |
518 jni(), *j_observer_class_, "onCreateSuccess", | |
519 "(Lorg/webrtc/SessionDescription;)V"); | |
520 jobject j_sdp = JavaSdpFromNativeSdp(jni(), desc); | |
521 jni()->CallVoidMethod(*j_observer_global_, m, j_sdp); | |
522 CHECK_EXCEPTION(jni()) << "error during CallVoidMethod"; | |
523 } | |
524 | |
525 protected: | |
526 // Common implementation for failure of Set & Create types, distinguished by | |
527 // |op| being "Set" or "Create". | |
528 void DoOnFailure(const std::string& op, const std::string& error) { | |
529 jmethodID m = GetMethodID(jni(), *j_observer_class_, "on" + op + "Failure", | |
530 "(Ljava/lang/String;)V"); | |
531 jstring j_error_string = JavaStringFromStdString(jni(), error); | |
532 jni()->CallVoidMethod(*j_observer_global_, m, j_error_string); | |
533 CHECK_EXCEPTION(jni()) << "error during CallVoidMethod"; | |
534 } | |
535 | |
536 JNIEnv* jni() { | |
537 return AttachCurrentThreadIfNeeded(); | |
538 } | |
539 | |
540 private: | |
541 scoped_ptr<ConstraintsWrapper> constraints_; | |
542 const ScopedGlobalRef<jobject> j_observer_global_; | |
543 const ScopedGlobalRef<jclass> j_observer_class_; | |
544 }; | |
545 | |
546 class CreateSdpObserverWrapper | |
547 : public SdpObserverWrapper<CreateSessionDescriptionObserver> { | |
548 public: | |
549 CreateSdpObserverWrapper(JNIEnv* jni, jobject j_observer, | |
550 ConstraintsWrapper* constraints) | |
551 : SdpObserverWrapper(jni, j_observer, constraints) {} | |
552 | |
553 void OnFailure(const std::string& error) override { | |
554 ScopedLocalRefFrame local_ref_frame(jni()); | |
555 SdpObserverWrapper::DoOnFailure(std::string("Create"), error); | |
556 } | |
557 }; | |
558 | |
559 class SetSdpObserverWrapper | |
560 : public SdpObserverWrapper<SetSessionDescriptionObserver> { | |
561 public: | |
562 SetSdpObserverWrapper(JNIEnv* jni, jobject j_observer, | |
563 ConstraintsWrapper* constraints) | |
564 : SdpObserverWrapper(jni, j_observer, constraints) {} | |
565 | |
566 void OnFailure(const std::string& error) override { | |
567 ScopedLocalRefFrame local_ref_frame(jni()); | |
568 SdpObserverWrapper::DoOnFailure(std::string("Set"), error); | |
569 } | |
570 }; | |
571 | |
572 // Adapter for a Java DataChannel$Observer presenting a C++ DataChannelObserver | |
573 // and dispatching the callback from C++ back to Java. | |
574 class DataChannelObserverWrapper : public DataChannelObserver { | |
575 public: | |
576 DataChannelObserverWrapper(JNIEnv* jni, jobject j_observer) | |
577 : j_observer_global_(jni, j_observer), | |
578 j_observer_class_(jni, GetObjectClass(jni, j_observer)), | |
579 j_buffer_class_(jni, FindClass(jni, "org/webrtc/DataChannel$Buffer")), | |
580 j_on_buffered_amount_change_mid_(GetMethodID( | |
581 jni, *j_observer_class_, "onBufferedAmountChange", "(J)V")), | |
582 j_on_state_change_mid_( | |
583 GetMethodID(jni, *j_observer_class_, "onStateChange", "()V")), | |
584 j_on_message_mid_(GetMethodID(jni, *j_observer_class_, "onMessage", | |
585 "(Lorg/webrtc/DataChannel$Buffer;)V")), | |
586 j_buffer_ctor_(GetMethodID(jni, *j_buffer_class_, "<init>", | |
587 "(Ljava/nio/ByteBuffer;Z)V")) {} | |
588 | |
589 virtual ~DataChannelObserverWrapper() {} | |
590 | |
591 void OnBufferedAmountChange(uint64_t previous_amount) override { | |
592 ScopedLocalRefFrame local_ref_frame(jni()); | |
593 jni()->CallVoidMethod(*j_observer_global_, j_on_buffered_amount_change_mid_, | |
594 previous_amount); | |
595 CHECK_EXCEPTION(jni()) << "error during CallVoidMethod"; | |
596 } | |
597 | |
598 void OnStateChange() override { | |
599 ScopedLocalRefFrame local_ref_frame(jni()); | |
600 jni()->CallVoidMethod(*j_observer_global_, j_on_state_change_mid_); | |
601 CHECK_EXCEPTION(jni()) << "error during CallVoidMethod"; | |
602 } | |
603 | |
604 void OnMessage(const DataBuffer& buffer) override { | |
605 ScopedLocalRefFrame local_ref_frame(jni()); | |
606 jobject byte_buffer = jni()->NewDirectByteBuffer( | |
607 const_cast<char*>(buffer.data.data<char>()), buffer.data.size()); | |
608 jobject j_buffer = jni()->NewObject(*j_buffer_class_, j_buffer_ctor_, | |
609 byte_buffer, buffer.binary); | |
610 jni()->CallVoidMethod(*j_observer_global_, j_on_message_mid_, j_buffer); | |
611 CHECK_EXCEPTION(jni()) << "error during CallVoidMethod"; | |
612 } | |
613 | |
614 private: | |
615 JNIEnv* jni() { | |
616 return AttachCurrentThreadIfNeeded(); | |
617 } | |
618 | |
619 const ScopedGlobalRef<jobject> j_observer_global_; | |
620 const ScopedGlobalRef<jclass> j_observer_class_; | |
621 const ScopedGlobalRef<jclass> j_buffer_class_; | |
622 const jmethodID j_on_buffered_amount_change_mid_; | |
623 const jmethodID j_on_state_change_mid_; | |
624 const jmethodID j_on_message_mid_; | |
625 const jmethodID j_buffer_ctor_; | |
626 }; | |
627 | |
628 // Adapter for a Java StatsObserver presenting a C++ StatsObserver and | |
629 // dispatching the callback from C++ back to Java. | |
630 class StatsObserverWrapper : public StatsObserver { | |
631 public: | |
632 StatsObserverWrapper(JNIEnv* jni, jobject j_observer) | |
633 : j_observer_global_(jni, j_observer), | |
634 j_observer_class_(jni, GetObjectClass(jni, j_observer)), | |
635 j_stats_report_class_(jni, FindClass(jni, "org/webrtc/StatsReport")), | |
636 j_stats_report_ctor_(GetMethodID( | |
637 jni, *j_stats_report_class_, "<init>", | |
638 "(Ljava/lang/String;Ljava/lang/String;D" | |
639 "[Lorg/webrtc/StatsReport$Value;)V")), | |
640 j_value_class_(jni, FindClass( | |
641 jni, "org/webrtc/StatsReport$Value")), | |
642 j_value_ctor_(GetMethodID( | |
643 jni, *j_value_class_, "<init>", | |
644 "(Ljava/lang/String;Ljava/lang/String;)V")) { | |
645 } | |
646 | |
647 virtual ~StatsObserverWrapper() {} | |
648 | |
649 void OnComplete(const StatsReports& reports) override { | |
650 ScopedLocalRefFrame local_ref_frame(jni()); | |
651 jobjectArray j_reports = ReportsToJava(jni(), reports); | |
652 jmethodID m = GetMethodID(jni(), *j_observer_class_, "onComplete", | |
653 "([Lorg/webrtc/StatsReport;)V"); | |
654 jni()->CallVoidMethod(*j_observer_global_, m, j_reports); | |
655 CHECK_EXCEPTION(jni()) << "error during CallVoidMethod"; | |
656 } | |
657 | |
658 private: | |
659 jobjectArray ReportsToJava( | |
660 JNIEnv* jni, const StatsReports& reports) { | |
661 jobjectArray reports_array = jni->NewObjectArray( | |
662 reports.size(), *j_stats_report_class_, NULL); | |
663 int i = 0; | |
664 for (const auto* report : reports) { | |
665 ScopedLocalRefFrame local_ref_frame(jni); | |
666 jstring j_id = JavaStringFromStdString(jni, report->id()->ToString()); | |
667 jstring j_type = JavaStringFromStdString(jni, report->TypeToString()); | |
668 jobjectArray j_values = ValuesToJava(jni, report->values()); | |
669 jobject j_report = jni->NewObject(*j_stats_report_class_, | |
670 j_stats_report_ctor_, | |
671 j_id, | |
672 j_type, | |
673 report->timestamp(), | |
674 j_values); | |
675 jni->SetObjectArrayElement(reports_array, i++, j_report); | |
676 } | |
677 return reports_array; | |
678 } | |
679 | |
680 jobjectArray ValuesToJava(JNIEnv* jni, const StatsReport::Values& values) { | |
681 jobjectArray j_values = jni->NewObjectArray( | |
682 values.size(), *j_value_class_, NULL); | |
683 int i = 0; | |
684 for (const auto& it : values) { | |
685 ScopedLocalRefFrame local_ref_frame(jni); | |
686 // Should we use the '.name' enum value here instead of converting the | |
687 // name to a string? | |
688 jstring j_name = JavaStringFromStdString(jni, it.second->display_name()); | |
689 jstring j_value = JavaStringFromStdString(jni, it.second->ToString()); | |
690 jobject j_element_value = | |
691 jni->NewObject(*j_value_class_, j_value_ctor_, j_name, j_value); | |
692 jni->SetObjectArrayElement(j_values, i++, j_element_value); | |
693 } | |
694 return j_values; | |
695 } | |
696 | |
697 JNIEnv* jni() { | |
698 return AttachCurrentThreadIfNeeded(); | |
699 } | |
700 | |
701 const ScopedGlobalRef<jobject> j_observer_global_; | |
702 const ScopedGlobalRef<jclass> j_observer_class_; | |
703 const ScopedGlobalRef<jclass> j_stats_report_class_; | |
704 const jmethodID j_stats_report_ctor_; | |
705 const ScopedGlobalRef<jclass> j_value_class_; | |
706 const jmethodID j_value_ctor_; | |
707 }; | |
708 | |
709 // Adapter presenting a cricket::VideoRenderer as a | |
710 // webrtc::VideoRendererInterface. | |
711 class VideoRendererWrapper : public VideoRendererInterface { | |
712 public: | |
713 static VideoRendererWrapper* Create(cricket::VideoRenderer* renderer) { | |
714 if (renderer) | |
715 return new VideoRendererWrapper(renderer); | |
716 return NULL; | |
717 } | |
718 | |
719 virtual ~VideoRendererWrapper() {} | |
720 | |
721 void RenderFrame(const cricket::VideoFrame* video_frame) override { | |
722 ScopedLocalRefFrame local_ref_frame(AttachCurrentThreadIfNeeded()); | |
723 renderer_->RenderFrame(video_frame->GetCopyWithRotationApplied()); | |
724 } | |
725 | |
726 private: | |
727 explicit VideoRendererWrapper(cricket::VideoRenderer* renderer) | |
728 : renderer_(renderer) {} | |
729 scoped_ptr<cricket::VideoRenderer> renderer_; | |
730 }; | |
731 | |
732 // Wrapper dispatching webrtc::VideoRendererInterface to a Java VideoRenderer | |
733 // instance. | |
734 class JavaVideoRendererWrapper : public VideoRendererInterface { | |
735 public: | |
736 JavaVideoRendererWrapper(JNIEnv* jni, jobject j_callbacks) | |
737 : j_callbacks_(jni, j_callbacks), | |
738 j_render_frame_id_(GetMethodID( | |
739 jni, GetObjectClass(jni, j_callbacks), "renderFrame", | |
740 "(Lorg/webrtc/VideoRenderer$I420Frame;)V")), | |
741 j_frame_class_(jni, | |
742 FindClass(jni, "org/webrtc/VideoRenderer$I420Frame")), | |
743 j_i420_frame_ctor_id_(GetMethodID( | |
744 jni, *j_frame_class_, "<init>", "(III[I[Ljava/nio/ByteBuffer;J)V")), | |
745 j_texture_frame_ctor_id_(GetMethodID( | |
746 jni, *j_frame_class_, "<init>", | |
747 "(IIII[FJ)V")), | |
748 j_byte_buffer_class_(jni, FindClass(jni, "java/nio/ByteBuffer")) { | |
749 CHECK_EXCEPTION(jni); | |
750 } | |
751 | |
752 virtual ~JavaVideoRendererWrapper() {} | |
753 | |
754 void RenderFrame(const cricket::VideoFrame* video_frame) override { | |
755 ScopedLocalRefFrame local_ref_frame(jni()); | |
756 jobject j_frame = (video_frame->GetNativeHandle() != nullptr) | |
757 ? CricketToJavaTextureFrame(video_frame) | |
758 : CricketToJavaI420Frame(video_frame); | |
759 // |j_callbacks_| is responsible for releasing |j_frame| with | |
760 // VideoRenderer.renderFrameDone(). | |
761 jni()->CallVoidMethod(*j_callbacks_, j_render_frame_id_, j_frame); | |
762 CHECK_EXCEPTION(jni()); | |
763 } | |
764 | |
765 private: | |
766 // Make a shallow copy of |frame| to be used with Java. The callee has | |
767 // ownership of the frame, and the frame should be released with | |
768 // VideoRenderer.releaseNativeFrame(). | |
769 static jlong javaShallowCopy(const cricket::VideoFrame* frame) { | |
770 return jlongFromPointer(frame->Copy()); | |
771 } | |
772 | |
773 // Return a VideoRenderer.I420Frame referring to the data in |frame|. | |
774 jobject CricketToJavaI420Frame(const cricket::VideoFrame* frame) { | |
775 jintArray strides = jni()->NewIntArray(3); | |
776 jint* strides_array = jni()->GetIntArrayElements(strides, NULL); | |
777 strides_array[0] = frame->GetYPitch(); | |
778 strides_array[1] = frame->GetUPitch(); | |
779 strides_array[2] = frame->GetVPitch(); | |
780 jni()->ReleaseIntArrayElements(strides, strides_array, 0); | |
781 jobjectArray planes = jni()->NewObjectArray(3, *j_byte_buffer_class_, NULL); | |
782 jobject y_buffer = | |
783 jni()->NewDirectByteBuffer(const_cast<uint8_t*>(frame->GetYPlane()), | |
784 frame->GetYPitch() * frame->GetHeight()); | |
785 jobject u_buffer = jni()->NewDirectByteBuffer( | |
786 const_cast<uint8_t*>(frame->GetUPlane()), frame->GetChromaSize()); | |
787 jobject v_buffer = jni()->NewDirectByteBuffer( | |
788 const_cast<uint8_t*>(frame->GetVPlane()), frame->GetChromaSize()); | |
789 jni()->SetObjectArrayElement(planes, 0, y_buffer); | |
790 jni()->SetObjectArrayElement(planes, 1, u_buffer); | |
791 jni()->SetObjectArrayElement(planes, 2, v_buffer); | |
792 return jni()->NewObject( | |
793 *j_frame_class_, j_i420_frame_ctor_id_, | |
794 frame->GetWidth(), frame->GetHeight(), | |
795 static_cast<int>(frame->GetVideoRotation()), | |
796 strides, planes, javaShallowCopy(frame)); | |
797 } | |
798 | |
799 // Return a VideoRenderer.I420Frame referring texture object in |frame|. | |
800 jobject CricketToJavaTextureFrame(const cricket::VideoFrame* frame) { | |
801 NativeHandleImpl* handle = | |
802 reinterpret_cast<NativeHandleImpl*>(frame->GetNativeHandle()); | |
803 jfloatArray sampling_matrix = jni()->NewFloatArray(16); | |
804 jni()->SetFloatArrayRegion(sampling_matrix, 0, 16, handle->sampling_matrix); | |
805 return jni()->NewObject( | |
806 *j_frame_class_, j_texture_frame_ctor_id_, | |
807 frame->GetWidth(), frame->GetHeight(), | |
808 static_cast<int>(frame->GetVideoRotation()), | |
809 handle->oes_texture_id, sampling_matrix, javaShallowCopy(frame)); | |
810 } | |
811 | |
812 JNIEnv* jni() { | |
813 return AttachCurrentThreadIfNeeded(); | |
814 } | |
815 | |
816 ScopedGlobalRef<jobject> j_callbacks_; | |
817 jmethodID j_render_frame_id_; | |
818 ScopedGlobalRef<jclass> j_frame_class_; | |
819 jmethodID j_i420_frame_ctor_id_; | |
820 jmethodID j_texture_frame_ctor_id_; | |
821 ScopedGlobalRef<jclass> j_byte_buffer_class_; | |
822 }; | |
823 | |
824 | |
825 static DataChannelInterface* ExtractNativeDC(JNIEnv* jni, jobject j_dc) { | |
826 jfieldID native_dc_id = GetFieldID(jni, | |
827 GetObjectClass(jni, j_dc), "nativeDataChannel", "J"); | |
828 jlong j_d = GetLongField(jni, j_dc, native_dc_id); | |
829 return reinterpret_cast<DataChannelInterface*>(j_d); | |
830 } | |
831 | |
832 JOW(jlong, DataChannel_registerObserverNative)( | |
833 JNIEnv* jni, jobject j_dc, jobject j_observer) { | |
834 scoped_ptr<DataChannelObserverWrapper> observer( | |
835 new DataChannelObserverWrapper(jni, j_observer)); | |
836 ExtractNativeDC(jni, j_dc)->RegisterObserver(observer.get()); | |
837 return jlongFromPointer(observer.release()); | |
838 } | |
839 | |
840 JOW(void, DataChannel_unregisterObserverNative)( | |
841 JNIEnv* jni, jobject j_dc, jlong native_observer) { | |
842 ExtractNativeDC(jni, j_dc)->UnregisterObserver(); | |
843 delete reinterpret_cast<DataChannelObserverWrapper*>(native_observer); | |
844 } | |
845 | |
846 JOW(jstring, DataChannel_label)(JNIEnv* jni, jobject j_dc) { | |
847 return JavaStringFromStdString(jni, ExtractNativeDC(jni, j_dc)->label()); | |
848 } | |
849 | |
850 JOW(jobject, DataChannel_state)(JNIEnv* jni, jobject j_dc) { | |
851 return JavaEnumFromIndex( | |
852 jni, "DataChannel$State", ExtractNativeDC(jni, j_dc)->state()); | |
853 } | |
854 | |
855 JOW(jlong, DataChannel_bufferedAmount)(JNIEnv* jni, jobject j_dc) { | |
856 uint64_t buffered_amount = ExtractNativeDC(jni, j_dc)->buffered_amount(); | |
857 RTC_CHECK_LE(buffered_amount, std::numeric_limits<int64_t>::max()) | |
858 << "buffered_amount overflowed jlong!"; | |
859 return static_cast<jlong>(buffered_amount); | |
860 } | |
861 | |
862 JOW(void, DataChannel_close)(JNIEnv* jni, jobject j_dc) { | |
863 ExtractNativeDC(jni, j_dc)->Close(); | |
864 } | |
865 | |
866 JOW(jboolean, DataChannel_sendNative)(JNIEnv* jni, jobject j_dc, | |
867 jbyteArray data, jboolean binary) { | |
868 jbyte* bytes = jni->GetByteArrayElements(data, NULL); | |
869 bool ret = ExtractNativeDC(jni, j_dc)->Send(DataBuffer( | |
870 rtc::Buffer(bytes, jni->GetArrayLength(data)), | |
871 binary)); | |
872 jni->ReleaseByteArrayElements(data, bytes, JNI_ABORT); | |
873 return ret; | |
874 } | |
875 | |
876 JOW(void, DataChannel_dispose)(JNIEnv* jni, jobject j_dc) { | |
877 CHECK_RELEASE(ExtractNativeDC(jni, j_dc)); | |
878 } | |
879 | |
880 JOW(void, Logging_nativeEnableTracing)( | |
881 JNIEnv* jni, jclass, jstring j_path, jint nativeLevels, | |
882 jint nativeSeverity) { | |
883 std::string path = JavaToStdString(jni, j_path); | |
884 if (nativeLevels != webrtc::kTraceNone) { | |
885 webrtc::Trace::set_level_filter(nativeLevels); | |
886 if (path != "logcat:") { | |
887 RTC_CHECK_EQ(0, webrtc::Trace::SetTraceFile(path.c_str(), false)) | |
888 << "SetTraceFile failed"; | |
889 } else { | |
890 // Intentionally leak this to avoid needing to reason about its lifecycle. | |
891 // It keeps no state and functions only as a dispatch point. | |
892 static LogcatTraceContext* g_trace_callback = new LogcatTraceContext(); | |
893 } | |
894 } | |
895 if (nativeSeverity >= rtc::LS_SENSITIVE && nativeSeverity <= rtc::LS_ERROR) { | |
896 rtc::LogMessage::LogToDebug( | |
897 static_cast<rtc::LoggingSeverity>(nativeSeverity)); | |
898 } | |
899 } | |
900 | |
901 JOW(void, Logging_nativeEnableLogThreads)(JNIEnv* jni, jclass) { | |
902 rtc::LogMessage::LogThreads(true); | |
903 } | |
904 | |
905 JOW(void, Logging_nativeEnableLogTimeStamps)(JNIEnv* jni, jclass) { | |
906 rtc::LogMessage::LogTimestamps(true); | |
907 } | |
908 | |
909 JOW(void, Logging_nativeLog)( | |
910 JNIEnv* jni, jclass, jint j_severity, jstring j_tag, jstring j_message) { | |
911 std::string message = JavaToStdString(jni, j_message); | |
912 std::string tag = JavaToStdString(jni, j_tag); | |
913 LOG_TAG(static_cast<rtc::LoggingSeverity>(j_severity), tag) << message; | |
914 } | |
915 | |
916 JOW(void, PeerConnection_freePeerConnection)(JNIEnv*, jclass, jlong j_p) { | |
917 CHECK_RELEASE(reinterpret_cast<PeerConnectionInterface*>(j_p)); | |
918 } | |
919 | |
920 JOW(void, PeerConnection_freeObserver)(JNIEnv*, jclass, jlong j_p) { | |
921 PCOJava* p = reinterpret_cast<PCOJava*>(j_p); | |
922 delete p; | |
923 } | |
924 | |
925 JOW(void, MediaSource_free)(JNIEnv*, jclass, jlong j_p) { | |
926 CHECK_RELEASE(reinterpret_cast<MediaSourceInterface*>(j_p)); | |
927 } | |
928 | |
929 JOW(void, VideoCapturer_free)(JNIEnv*, jclass, jlong j_p) { | |
930 delete reinterpret_cast<cricket::VideoCapturer*>(j_p); | |
931 } | |
932 | |
933 JOW(void, VideoRenderer_freeWrappedVideoRenderer)(JNIEnv*, jclass, jlong j_p) { | |
934 delete reinterpret_cast<JavaVideoRendererWrapper*>(j_p); | |
935 } | |
936 | |
937 JOW(void, VideoRenderer_releaseNativeFrame)( | |
938 JNIEnv* jni, jclass, jlong j_frame_ptr) { | |
939 delete reinterpret_cast<const cricket::VideoFrame*>(j_frame_ptr); | |
940 } | |
941 | |
942 JOW(void, MediaStreamTrack_free)(JNIEnv*, jclass, jlong j_p) { | |
943 reinterpret_cast<MediaStreamTrackInterface*>(j_p)->Release(); | |
944 } | |
945 | |
946 JOW(jboolean, MediaStream_nativeAddAudioTrack)( | |
947 JNIEnv* jni, jclass, jlong pointer, jlong j_audio_track_pointer) { | |
948 return reinterpret_cast<MediaStreamInterface*>(pointer)->AddTrack( | |
949 reinterpret_cast<AudioTrackInterface*>(j_audio_track_pointer)); | |
950 } | |
951 | |
952 JOW(jboolean, MediaStream_nativeAddVideoTrack)( | |
953 JNIEnv* jni, jclass, jlong pointer, jlong j_video_track_pointer) { | |
954 return reinterpret_cast<MediaStreamInterface*>(pointer) | |
955 ->AddTrack(reinterpret_cast<VideoTrackInterface*>(j_video_track_pointer)); | |
956 } | |
957 | |
958 JOW(jboolean, MediaStream_nativeRemoveAudioTrack)( | |
959 JNIEnv* jni, jclass, jlong pointer, jlong j_audio_track_pointer) { | |
960 return reinterpret_cast<MediaStreamInterface*>(pointer)->RemoveTrack( | |
961 reinterpret_cast<AudioTrackInterface*>(j_audio_track_pointer)); | |
962 } | |
963 | |
964 JOW(jboolean, MediaStream_nativeRemoveVideoTrack)( | |
965 JNIEnv* jni, jclass, jlong pointer, jlong j_video_track_pointer) { | |
966 return reinterpret_cast<MediaStreamInterface*>(pointer)->RemoveTrack( | |
967 reinterpret_cast<VideoTrackInterface*>(j_video_track_pointer)); | |
968 } | |
969 | |
970 JOW(jstring, MediaStream_nativeLabel)(JNIEnv* jni, jclass, jlong j_p) { | |
971 return JavaStringFromStdString( | |
972 jni, reinterpret_cast<MediaStreamInterface*>(j_p)->label()); | |
973 } | |
974 | |
975 JOW(void, MediaStream_free)(JNIEnv*, jclass, jlong j_p) { | |
976 CHECK_RELEASE(reinterpret_cast<MediaStreamInterface*>(j_p)); | |
977 } | |
978 | |
979 JOW(jlong, PeerConnectionFactory_nativeCreateObserver)( | |
980 JNIEnv * jni, jclass, jobject j_observer) { | |
981 return (jlong)new PCOJava(jni, j_observer); | |
982 } | |
983 | |
984 JOW(jboolean, PeerConnectionFactory_initializeAndroidGlobals)( | |
985 JNIEnv* jni, jclass, jobject context, | |
986 jboolean initialize_audio, jboolean initialize_video, | |
987 jboolean video_hw_acceleration) { | |
988 bool failure = false; | |
989 video_hw_acceleration_enabled = video_hw_acceleration; | |
990 AndroidNetworkMonitor::SetAndroidContext(jni, context); | |
991 if (!factory_static_initialized) { | |
992 if (initialize_video) { | |
993 failure |= AndroidVideoCapturerJni::SetAndroidObjects(jni, context); | |
994 } | |
995 if (initialize_audio) | |
996 failure |= webrtc::VoiceEngine::SetAndroidObjects(GetJVM(), context); | |
997 factory_static_initialized = true; | |
998 } | |
999 return !failure; | |
1000 } | |
1001 | |
1002 JOW(void, PeerConnectionFactory_initializeFieldTrials)( | |
1003 JNIEnv* jni, jclass, jstring j_trials_init_string) { | |
1004 field_trials_init_string = NULL; | |
1005 if (j_trials_init_string != NULL) { | |
1006 const char* init_string = | |
1007 jni->GetStringUTFChars(j_trials_init_string, NULL); | |
1008 int init_string_length = jni->GetStringUTFLength(j_trials_init_string); | |
1009 field_trials_init_string = new char[init_string_length + 1]; | |
1010 rtc::strcpyn(field_trials_init_string, init_string_length + 1, init_string); | |
1011 jni->ReleaseStringUTFChars(j_trials_init_string, init_string); | |
1012 LOG(LS_INFO) << "initializeFieldTrials: " << field_trials_init_string; | |
1013 } | |
1014 webrtc::field_trial::InitFieldTrialsFromString(field_trials_init_string); | |
1015 } | |
1016 | |
1017 JOW(void, PeerConnectionFactory_initializeInternalTracer)(JNIEnv* jni, jclass) { | |
1018 rtc::tracing::SetupInternalTracer(); | |
1019 } | |
1020 | |
1021 JOW(jboolean, PeerConnectionFactory_startInternalTracingCapture)( | |
1022 JNIEnv* jni, jclass, jstring j_event_tracing_filename) { | |
1023 if (!j_event_tracing_filename) | |
1024 return false; | |
1025 | |
1026 const char* init_string = | |
1027 jni->GetStringUTFChars(j_event_tracing_filename, NULL); | |
1028 LOG(LS_INFO) << "Starting internal tracing to: " << init_string; | |
1029 bool ret = rtc::tracing::StartInternalCapture(init_string); | |
1030 jni->ReleaseStringUTFChars(j_event_tracing_filename, init_string); | |
1031 return ret; | |
1032 } | |
1033 | |
1034 JOW(void, PeerConnectionFactory_stopInternalTracingCapture)( | |
1035 JNIEnv* jni, jclass) { | |
1036 rtc::tracing::StopInternalCapture(); | |
1037 } | |
1038 | |
1039 JOW(void, PeerConnectionFactory_shutdownInternalTracer)(JNIEnv* jni, jclass) { | |
1040 rtc::tracing::ShutdownInternalTracer(); | |
1041 } | |
1042 | |
1043 // Helper struct for working around the fact that CreatePeerConnectionFactory() | |
1044 // comes in two flavors: either entirely automagical (constructing its own | |
1045 // threads and deleting them on teardown, but no external codec factory support) | |
1046 // or entirely manual (requires caller to delete threads after factory | |
1047 // teardown). This struct takes ownership of its ctor's arguments to present a | |
1048 // single thing for Java to hold and eventually free. | |
1049 class OwnedFactoryAndThreads { | |
1050 public: | |
1051 OwnedFactoryAndThreads(Thread* worker_thread, | |
1052 Thread* signaling_thread, | |
1053 WebRtcVideoEncoderFactory* encoder_factory, | |
1054 WebRtcVideoDecoderFactory* decoder_factory, | |
1055 rtc::NetworkMonitorFactory* network_monitor_factory, | |
1056 PeerConnectionFactoryInterface* factory) | |
1057 : worker_thread_(worker_thread), | |
1058 signaling_thread_(signaling_thread), | |
1059 encoder_factory_(encoder_factory), | |
1060 decoder_factory_(decoder_factory), | |
1061 network_monitor_factory_(network_monitor_factory), | |
1062 factory_(factory) {} | |
1063 | |
1064 ~OwnedFactoryAndThreads() { | |
1065 CHECK_RELEASE(factory_); | |
1066 if (network_monitor_factory_ != nullptr) { | |
1067 rtc::NetworkMonitorFactory::ReleaseFactory(network_monitor_factory_); | |
1068 } | |
1069 } | |
1070 | |
1071 PeerConnectionFactoryInterface* factory() { return factory_; } | |
1072 WebRtcVideoEncoderFactory* encoder_factory() { return encoder_factory_; } | |
1073 WebRtcVideoDecoderFactory* decoder_factory() { return decoder_factory_; } | |
1074 rtc::NetworkMonitorFactory* network_monitor_factory() { | |
1075 return network_monitor_factory_; | |
1076 } | |
1077 void clear_network_monitor_factory() { network_monitor_factory_ = nullptr; } | |
1078 void InvokeJavaCallbacksOnFactoryThreads(); | |
1079 | |
1080 private: | |
1081 void JavaCallbackOnFactoryThreads(); | |
1082 | |
1083 const scoped_ptr<Thread> worker_thread_; | |
1084 const scoped_ptr<Thread> signaling_thread_; | |
1085 WebRtcVideoEncoderFactory* encoder_factory_; | |
1086 WebRtcVideoDecoderFactory* decoder_factory_; | |
1087 rtc::NetworkMonitorFactory* network_monitor_factory_; | |
1088 PeerConnectionFactoryInterface* factory_; // Const after ctor except dtor. | |
1089 }; | |
1090 | |
1091 void OwnedFactoryAndThreads::JavaCallbackOnFactoryThreads() { | |
1092 JNIEnv* jni = AttachCurrentThreadIfNeeded(); | |
1093 ScopedLocalRefFrame local_ref_frame(jni); | |
1094 jclass j_factory_class = FindClass(jni, "org/webrtc/PeerConnectionFactory"); | |
1095 jmethodID m = nullptr; | |
1096 if (Thread::Current() == worker_thread_) { | |
1097 LOG(LS_INFO) << "Worker thread JavaCallback"; | |
1098 m = GetStaticMethodID(jni, j_factory_class, "onWorkerThreadReady", "()V"); | |
1099 } | |
1100 if (Thread::Current() == signaling_thread_) { | |
1101 LOG(LS_INFO) << "Signaling thread JavaCallback"; | |
1102 m = GetStaticMethodID( | |
1103 jni, j_factory_class, "onSignalingThreadReady", "()V"); | |
1104 } | |
1105 if (m != nullptr) { | |
1106 jni->CallStaticVoidMethod(j_factory_class, m); | |
1107 CHECK_EXCEPTION(jni) << "error during JavaCallback::CallStaticVoidMethod"; | |
1108 } | |
1109 } | |
1110 | |
1111 void OwnedFactoryAndThreads::InvokeJavaCallbacksOnFactoryThreads() { | |
1112 LOG(LS_INFO) << "InvokeJavaCallbacksOnFactoryThreads."; | |
1113 worker_thread_->Invoke<void>( | |
1114 Bind(&OwnedFactoryAndThreads::JavaCallbackOnFactoryThreads, this)); | |
1115 signaling_thread_->Invoke<void>( | |
1116 Bind(&OwnedFactoryAndThreads::JavaCallbackOnFactoryThreads, this)); | |
1117 } | |
1118 | |
1119 PeerConnectionFactoryInterface::Options ParseOptionsFromJava(JNIEnv* jni, | |
1120 jobject options) { | |
1121 jclass options_class = jni->GetObjectClass(options); | |
1122 jfieldID network_ignore_mask_field = | |
1123 jni->GetFieldID(options_class, "networkIgnoreMask", "I"); | |
1124 int network_ignore_mask = | |
1125 jni->GetIntField(options, network_ignore_mask_field); | |
1126 | |
1127 jfieldID disable_encryption_field = | |
1128 jni->GetFieldID(options_class, "disableEncryption", "Z"); | |
1129 bool disable_encryption = | |
1130 jni->GetBooleanField(options, disable_encryption_field); | |
1131 | |
1132 jfieldID disable_network_monitor_field = | |
1133 jni->GetFieldID(options_class, "disableNetworkMonitor", "Z"); | |
1134 bool disable_network_monitor = | |
1135 jni->GetBooleanField(options, disable_network_monitor_field); | |
1136 | |
1137 PeerConnectionFactoryInterface::Options native_options; | |
1138 | |
1139 // This doesn't necessarily match the c++ version of this struct; feel free | |
1140 // to add more parameters as necessary. | |
1141 native_options.network_ignore_mask = network_ignore_mask; | |
1142 native_options.disable_encryption = disable_encryption; | |
1143 native_options.disable_network_monitor = disable_network_monitor; | |
1144 return native_options; | |
1145 } | |
1146 | |
1147 JOW(jlong, PeerConnectionFactory_nativeCreatePeerConnectionFactory)( | |
1148 JNIEnv* jni, jclass, jobject joptions) { | |
1149 // talk/ assumes pretty widely that the current Thread is ThreadManager'd, but | |
1150 // ThreadManager only WrapCurrentThread()s the thread where it is first | |
1151 // created. Since the semantics around when auto-wrapping happens in | |
1152 // webrtc/base/ are convoluted, we simply wrap here to avoid having to think | |
1153 // about ramifications of auto-wrapping there. | |
1154 rtc::ThreadManager::Instance()->WrapCurrentThread(); | |
1155 webrtc::Trace::CreateTrace(); | |
1156 Thread* worker_thread = new Thread(); | |
1157 worker_thread->SetName("worker_thread", NULL); | |
1158 Thread* signaling_thread = new Thread(); | |
1159 signaling_thread->SetName("signaling_thread", NULL); | |
1160 RTC_CHECK(worker_thread->Start() && signaling_thread->Start()) | |
1161 << "Failed to start threads"; | |
1162 WebRtcVideoEncoderFactory* encoder_factory = nullptr; | |
1163 WebRtcVideoDecoderFactory* decoder_factory = nullptr; | |
1164 rtc::NetworkMonitorFactory* network_monitor_factory = nullptr; | |
1165 | |
1166 PeerConnectionFactoryInterface::Options options; | |
1167 bool has_options = joptions != NULL; | |
1168 if (has_options) { | |
1169 options = ParseOptionsFromJava(jni, joptions); | |
1170 } | |
1171 | |
1172 if (video_hw_acceleration_enabled) { | |
1173 encoder_factory = new MediaCodecVideoEncoderFactory(); | |
1174 decoder_factory = new MediaCodecVideoDecoderFactory(); | |
1175 } | |
1176 // Do not create network_monitor_factory only if the options are | |
1177 // provided and disable_network_monitor therein is set to true. | |
1178 if (!(has_options && options.disable_network_monitor)) { | |
1179 network_monitor_factory = new AndroidNetworkMonitorFactory(); | |
1180 rtc::NetworkMonitorFactory::SetFactory(network_monitor_factory); | |
1181 } | |
1182 | |
1183 rtc::scoped_refptr<PeerConnectionFactoryInterface> factory( | |
1184 webrtc::CreatePeerConnectionFactory(worker_thread, | |
1185 signaling_thread, | |
1186 NULL, | |
1187 encoder_factory, | |
1188 decoder_factory)); | |
1189 RTC_CHECK(factory) << "Failed to create the peer connection factory; " | |
1190 << "WebRTC/libjingle init likely failed on this device"; | |
1191 // TODO(honghaiz): Maybe put the options as the argument of | |
1192 // CreatePeerConnectionFactory. | |
1193 if (has_options) { | |
1194 factory->SetOptions(options); | |
1195 } | |
1196 OwnedFactoryAndThreads* owned_factory = new OwnedFactoryAndThreads( | |
1197 worker_thread, signaling_thread, | |
1198 encoder_factory, decoder_factory, | |
1199 network_monitor_factory, factory.release()); | |
1200 owned_factory->InvokeJavaCallbacksOnFactoryThreads(); | |
1201 return jlongFromPointer(owned_factory); | |
1202 } | |
1203 | |
1204 JOW(void, PeerConnectionFactory_nativeFreeFactory)(JNIEnv*, jclass, jlong j_p) { | |
1205 delete reinterpret_cast<OwnedFactoryAndThreads*>(j_p); | |
1206 if (field_trials_init_string) { | |
1207 webrtc::field_trial::InitFieldTrialsFromString(NULL); | |
1208 delete field_trials_init_string; | |
1209 field_trials_init_string = NULL; | |
1210 } | |
1211 webrtc::Trace::ReturnTrace(); | |
1212 } | |
1213 | |
1214 static PeerConnectionFactoryInterface* factoryFromJava(jlong j_p) { | |
1215 return reinterpret_cast<OwnedFactoryAndThreads*>(j_p)->factory(); | |
1216 } | |
1217 | |
1218 JOW(void, PeerConnectionFactory_nativeThreadsCallbacks)( | |
1219 JNIEnv*, jclass, jlong j_p) { | |
1220 OwnedFactoryAndThreads *factory = | |
1221 reinterpret_cast<OwnedFactoryAndThreads*>(j_p); | |
1222 factory->InvokeJavaCallbacksOnFactoryThreads(); | |
1223 } | |
1224 | |
1225 JOW(jlong, PeerConnectionFactory_nativeCreateLocalMediaStream)( | |
1226 JNIEnv* jni, jclass, jlong native_factory, jstring label) { | |
1227 rtc::scoped_refptr<PeerConnectionFactoryInterface> factory( | |
1228 factoryFromJava(native_factory)); | |
1229 rtc::scoped_refptr<MediaStreamInterface> stream( | |
1230 factory->CreateLocalMediaStream(JavaToStdString(jni, label))); | |
1231 return (jlong)stream.release(); | |
1232 } | |
1233 | |
1234 JOW(jlong, PeerConnectionFactory_nativeCreateVideoSource)( | |
1235 JNIEnv* jni, jclass, jlong native_factory, jlong native_capturer, | |
1236 jobject j_constraints) { | |
1237 scoped_ptr<ConstraintsWrapper> constraints( | |
1238 new ConstraintsWrapper(jni, j_constraints)); | |
1239 rtc::scoped_refptr<PeerConnectionFactoryInterface> factory( | |
1240 factoryFromJava(native_factory)); | |
1241 rtc::scoped_refptr<VideoSourceInterface> source( | |
1242 factory->CreateVideoSource( | |
1243 reinterpret_cast<cricket::VideoCapturer*>(native_capturer), | |
1244 constraints.get())); | |
1245 return (jlong)source.release(); | |
1246 } | |
1247 | |
1248 JOW(jlong, PeerConnectionFactory_nativeCreateVideoTrack)( | |
1249 JNIEnv* jni, jclass, jlong native_factory, jstring id, | |
1250 jlong native_source) { | |
1251 rtc::scoped_refptr<PeerConnectionFactoryInterface> factory( | |
1252 factoryFromJava(native_factory)); | |
1253 rtc::scoped_refptr<VideoTrackInterface> track( | |
1254 factory->CreateVideoTrack( | |
1255 JavaToStdString(jni, id), | |
1256 reinterpret_cast<VideoSourceInterface*>(native_source))); | |
1257 return (jlong)track.release(); | |
1258 } | |
1259 | |
1260 JOW(jlong, PeerConnectionFactory_nativeCreateAudioSource)( | |
1261 JNIEnv* jni, jclass, jlong native_factory, jobject j_constraints) { | |
1262 scoped_ptr<ConstraintsWrapper> constraints( | |
1263 new ConstraintsWrapper(jni, j_constraints)); | |
1264 rtc::scoped_refptr<PeerConnectionFactoryInterface> factory( | |
1265 factoryFromJava(native_factory)); | |
1266 rtc::scoped_refptr<AudioSourceInterface> source( | |
1267 factory->CreateAudioSource(constraints.get())); | |
1268 return (jlong)source.release(); | |
1269 } | |
1270 | |
1271 JOW(jlong, PeerConnectionFactory_nativeCreateAudioTrack)( | |
1272 JNIEnv* jni, jclass, jlong native_factory, jstring id, | |
1273 jlong native_source) { | |
1274 rtc::scoped_refptr<PeerConnectionFactoryInterface> factory( | |
1275 factoryFromJava(native_factory)); | |
1276 rtc::scoped_refptr<AudioTrackInterface> track(factory->CreateAudioTrack( | |
1277 JavaToStdString(jni, id), | |
1278 reinterpret_cast<AudioSourceInterface*>(native_source))); | |
1279 return (jlong)track.release(); | |
1280 } | |
1281 | |
1282 JOW(jboolean, PeerConnectionFactory_nativeStartAecDump)( | |
1283 JNIEnv* jni, jclass, jlong native_factory, jint file, | |
1284 jint filesize_limit_bytes) { | |
1285 rtc::scoped_refptr<PeerConnectionFactoryInterface> factory( | |
1286 factoryFromJava(native_factory)); | |
1287 return factory->StartAecDump(file, filesize_limit_bytes); | |
1288 } | |
1289 | |
1290 JOW(void, PeerConnectionFactory_nativeStopAecDump)( | |
1291 JNIEnv* jni, jclass, jlong native_factory) { | |
1292 rtc::scoped_refptr<PeerConnectionFactoryInterface> factory( | |
1293 factoryFromJava(native_factory)); | |
1294 factory->StopAecDump(); | |
1295 } | |
1296 | |
1297 JOW(jboolean, PeerConnectionFactory_nativeStartRtcEventLog)( | |
1298 JNIEnv* jni, jclass, jlong native_factory, jint file) { | |
1299 rtc::scoped_refptr<PeerConnectionFactoryInterface> factory( | |
1300 factoryFromJava(native_factory)); | |
1301 return factory->StartRtcEventLog(file); | |
1302 } | |
1303 | |
1304 JOW(void, PeerConnectionFactory_nativeStopRtcEventLog)( | |
1305 JNIEnv* jni, jclass, jlong native_factory) { | |
1306 rtc::scoped_refptr<PeerConnectionFactoryInterface> factory( | |
1307 factoryFromJava(native_factory)); | |
1308 factory->StopRtcEventLog(); | |
1309 } | |
1310 | |
1311 JOW(void, PeerConnectionFactory_nativeSetOptions)( | |
1312 JNIEnv* jni, jclass, jlong native_factory, jobject options) { | |
1313 rtc::scoped_refptr<PeerConnectionFactoryInterface> factory( | |
1314 factoryFromJava(native_factory)); | |
1315 PeerConnectionFactoryInterface::Options options_to_set = | |
1316 ParseOptionsFromJava(jni, options); | |
1317 factory->SetOptions(options_to_set); | |
1318 | |
1319 if (options_to_set.disable_network_monitor) { | |
1320 OwnedFactoryAndThreads* owner = | |
1321 reinterpret_cast<OwnedFactoryAndThreads*>(native_factory); | |
1322 if (owner->network_monitor_factory()) { | |
1323 rtc::NetworkMonitorFactory::ReleaseFactory( | |
1324 owner->network_monitor_factory()); | |
1325 owner->clear_network_monitor_factory(); | |
1326 } | |
1327 } | |
1328 } | |
1329 | |
1330 JOW(void, PeerConnectionFactory_nativeSetVideoHwAccelerationOptions)( | |
1331 JNIEnv* jni, jclass, jlong native_factory, jobject local_egl_context, | |
1332 jobject remote_egl_context) { | |
1333 OwnedFactoryAndThreads* owned_factory = | |
1334 reinterpret_cast<OwnedFactoryAndThreads*>(native_factory); | |
1335 | |
1336 jclass j_eglbase14_context_class = | |
1337 FindClass(jni, "org/webrtc/EglBase14$Context"); | |
1338 | |
1339 MediaCodecVideoEncoderFactory* encoder_factory = | |
1340 static_cast<MediaCodecVideoEncoderFactory*> | |
1341 (owned_factory->encoder_factory()); | |
1342 if (encoder_factory && | |
1343 jni->IsInstanceOf(local_egl_context, j_eglbase14_context_class)) { | |
1344 LOG(LS_INFO) << "Set EGL context for HW encoding."; | |
1345 encoder_factory->SetEGLContext(jni, local_egl_context); | |
1346 } | |
1347 | |
1348 MediaCodecVideoDecoderFactory* decoder_factory = | |
1349 static_cast<MediaCodecVideoDecoderFactory*> | |
1350 (owned_factory->decoder_factory()); | |
1351 if (decoder_factory && | |
1352 jni->IsInstanceOf(remote_egl_context, j_eglbase14_context_class)) { | |
1353 LOG(LS_INFO) << "Set EGL context for HW decoding."; | |
1354 decoder_factory->SetEGLContext(jni, remote_egl_context); | |
1355 } | |
1356 } | |
1357 | |
1358 static PeerConnectionInterface::IceTransportsType | |
1359 JavaIceTransportsTypeToNativeType(JNIEnv* jni, jobject j_ice_transports_type) { | |
1360 std::string enum_name = GetJavaEnumName( | |
1361 jni, "org/webrtc/PeerConnection$IceTransportsType", | |
1362 j_ice_transports_type); | |
1363 | |
1364 if (enum_name == "ALL") | |
1365 return PeerConnectionInterface::kAll; | |
1366 | |
1367 if (enum_name == "RELAY") | |
1368 return PeerConnectionInterface::kRelay; | |
1369 | |
1370 if (enum_name == "NOHOST") | |
1371 return PeerConnectionInterface::kNoHost; | |
1372 | |
1373 if (enum_name == "NONE") | |
1374 return PeerConnectionInterface::kNone; | |
1375 | |
1376 RTC_CHECK(false) << "Unexpected IceTransportsType enum_name " << enum_name; | |
1377 return PeerConnectionInterface::kAll; | |
1378 } | |
1379 | |
1380 static PeerConnectionInterface::BundlePolicy | |
1381 JavaBundlePolicyToNativeType(JNIEnv* jni, jobject j_bundle_policy) { | |
1382 std::string enum_name = GetJavaEnumName( | |
1383 jni, "org/webrtc/PeerConnection$BundlePolicy", | |
1384 j_bundle_policy); | |
1385 | |
1386 if (enum_name == "BALANCED") | |
1387 return PeerConnectionInterface::kBundlePolicyBalanced; | |
1388 | |
1389 if (enum_name == "MAXBUNDLE") | |
1390 return PeerConnectionInterface::kBundlePolicyMaxBundle; | |
1391 | |
1392 if (enum_name == "MAXCOMPAT") | |
1393 return PeerConnectionInterface::kBundlePolicyMaxCompat; | |
1394 | |
1395 RTC_CHECK(false) << "Unexpected BundlePolicy enum_name " << enum_name; | |
1396 return PeerConnectionInterface::kBundlePolicyBalanced; | |
1397 } | |
1398 | |
1399 static PeerConnectionInterface::RtcpMuxPolicy | |
1400 JavaRtcpMuxPolicyToNativeType(JNIEnv* jni, jobject j_rtcp_mux_policy) { | |
1401 std::string enum_name = GetJavaEnumName( | |
1402 jni, "org/webrtc/PeerConnection$RtcpMuxPolicy", | |
1403 j_rtcp_mux_policy); | |
1404 | |
1405 if (enum_name == "NEGOTIATE") | |
1406 return PeerConnectionInterface::kRtcpMuxPolicyNegotiate; | |
1407 | |
1408 if (enum_name == "REQUIRE") | |
1409 return PeerConnectionInterface::kRtcpMuxPolicyRequire; | |
1410 | |
1411 RTC_CHECK(false) << "Unexpected RtcpMuxPolicy enum_name " << enum_name; | |
1412 return PeerConnectionInterface::kRtcpMuxPolicyNegotiate; | |
1413 } | |
1414 | |
1415 static PeerConnectionInterface::TcpCandidatePolicy | |
1416 JavaTcpCandidatePolicyToNativeType( | |
1417 JNIEnv* jni, jobject j_tcp_candidate_policy) { | |
1418 std::string enum_name = GetJavaEnumName( | |
1419 jni, "org/webrtc/PeerConnection$TcpCandidatePolicy", | |
1420 j_tcp_candidate_policy); | |
1421 | |
1422 if (enum_name == "ENABLED") | |
1423 return PeerConnectionInterface::kTcpCandidatePolicyEnabled; | |
1424 | |
1425 if (enum_name == "DISABLED") | |
1426 return PeerConnectionInterface::kTcpCandidatePolicyDisabled; | |
1427 | |
1428 RTC_CHECK(false) << "Unexpected TcpCandidatePolicy enum_name " << enum_name; | |
1429 return PeerConnectionInterface::kTcpCandidatePolicyEnabled; | |
1430 } | |
1431 | |
1432 static rtc::KeyType JavaKeyTypeToNativeType(JNIEnv* jni, jobject j_key_type) { | |
1433 std::string enum_name = GetJavaEnumName( | |
1434 jni, "org/webrtc/PeerConnection$KeyType", j_key_type); | |
1435 | |
1436 if (enum_name == "RSA") | |
1437 return rtc::KT_RSA; | |
1438 if (enum_name == "ECDSA") | |
1439 return rtc::KT_ECDSA; | |
1440 | |
1441 RTC_CHECK(false) << "Unexpected KeyType enum_name " << enum_name; | |
1442 return rtc::KT_ECDSA; | |
1443 } | |
1444 | |
1445 static PeerConnectionInterface::ContinualGatheringPolicy | |
1446 JavaContinualGatheringPolicyToNativeType( | |
1447 JNIEnv* jni, jobject j_gathering_policy) { | |
1448 std::string enum_name = GetJavaEnumName( | |
1449 jni, "org/webrtc/PeerConnection$ContinualGatheringPolicy", | |
1450 j_gathering_policy); | |
1451 if (enum_name == "GATHER_ONCE") | |
1452 return PeerConnectionInterface::GATHER_ONCE; | |
1453 | |
1454 if (enum_name == "GATHER_CONTINUALLY") | |
1455 return PeerConnectionInterface::GATHER_CONTINUALLY; | |
1456 | |
1457 RTC_CHECK(false) << "Unexpected ContinualGatheringPolicy enum name " | |
1458 << enum_name; | |
1459 return PeerConnectionInterface::GATHER_ONCE; | |
1460 } | |
1461 | |
1462 static void JavaIceServersToJsepIceServers( | |
1463 JNIEnv* jni, jobject j_ice_servers, | |
1464 PeerConnectionInterface::IceServers* ice_servers) { | |
1465 jclass list_class = GetObjectClass(jni, j_ice_servers); | |
1466 jmethodID iterator_id = GetMethodID( | |
1467 jni, list_class, "iterator", "()Ljava/util/Iterator;"); | |
1468 jobject iterator = jni->CallObjectMethod(j_ice_servers, iterator_id); | |
1469 CHECK_EXCEPTION(jni) << "error during CallObjectMethod"; | |
1470 jmethodID iterator_has_next = GetMethodID( | |
1471 jni, GetObjectClass(jni, iterator), "hasNext", "()Z"); | |
1472 jmethodID iterator_next = GetMethodID( | |
1473 jni, GetObjectClass(jni, iterator), "next", "()Ljava/lang/Object;"); | |
1474 while (jni->CallBooleanMethod(iterator, iterator_has_next)) { | |
1475 CHECK_EXCEPTION(jni) << "error during CallBooleanMethod"; | |
1476 jobject j_ice_server = jni->CallObjectMethod(iterator, iterator_next); | |
1477 CHECK_EXCEPTION(jni) << "error during CallObjectMethod"; | |
1478 jclass j_ice_server_class = GetObjectClass(jni, j_ice_server); | |
1479 jfieldID j_ice_server_uri_id = | |
1480 GetFieldID(jni, j_ice_server_class, "uri", "Ljava/lang/String;"); | |
1481 jfieldID j_ice_server_username_id = | |
1482 GetFieldID(jni, j_ice_server_class, "username", "Ljava/lang/String;"); | |
1483 jfieldID j_ice_server_password_id = | |
1484 GetFieldID(jni, j_ice_server_class, "password", "Ljava/lang/String;"); | |
1485 jstring uri = reinterpret_cast<jstring>( | |
1486 GetObjectField(jni, j_ice_server, j_ice_server_uri_id)); | |
1487 jstring username = reinterpret_cast<jstring>( | |
1488 GetObjectField(jni, j_ice_server, j_ice_server_username_id)); | |
1489 jstring password = reinterpret_cast<jstring>( | |
1490 GetObjectField(jni, j_ice_server, j_ice_server_password_id)); | |
1491 PeerConnectionInterface::IceServer server; | |
1492 server.uri = JavaToStdString(jni, uri); | |
1493 server.username = JavaToStdString(jni, username); | |
1494 server.password = JavaToStdString(jni, password); | |
1495 ice_servers->push_back(server); | |
1496 } | |
1497 CHECK_EXCEPTION(jni) << "error during CallBooleanMethod"; | |
1498 } | |
1499 | |
1500 static void JavaRTCConfigurationToJsepRTCConfiguration( | |
1501 JNIEnv* jni, | |
1502 jobject j_rtc_config, | |
1503 PeerConnectionInterface::RTCConfiguration* rtc_config) { | |
1504 jclass j_rtc_config_class = GetObjectClass(jni, j_rtc_config); | |
1505 | |
1506 jfieldID j_ice_transports_type_id = GetFieldID( | |
1507 jni, j_rtc_config_class, "iceTransportsType", | |
1508 "Lorg/webrtc/PeerConnection$IceTransportsType;"); | |
1509 jobject j_ice_transports_type = GetObjectField( | |
1510 jni, j_rtc_config, j_ice_transports_type_id); | |
1511 | |
1512 jfieldID j_bundle_policy_id = GetFieldID( | |
1513 jni, j_rtc_config_class, "bundlePolicy", | |
1514 "Lorg/webrtc/PeerConnection$BundlePolicy;"); | |
1515 jobject j_bundle_policy = GetObjectField( | |
1516 jni, j_rtc_config, j_bundle_policy_id); | |
1517 | |
1518 jfieldID j_rtcp_mux_policy_id = GetFieldID( | |
1519 jni, j_rtc_config_class, "rtcpMuxPolicy", | |
1520 "Lorg/webrtc/PeerConnection$RtcpMuxPolicy;"); | |
1521 jobject j_rtcp_mux_policy = GetObjectField( | |
1522 jni, j_rtc_config, j_rtcp_mux_policy_id); | |
1523 | |
1524 jfieldID j_tcp_candidate_policy_id = GetFieldID( | |
1525 jni, j_rtc_config_class, "tcpCandidatePolicy", | |
1526 "Lorg/webrtc/PeerConnection$TcpCandidatePolicy;"); | |
1527 jobject j_tcp_candidate_policy = GetObjectField( | |
1528 jni, j_rtc_config, j_tcp_candidate_policy_id); | |
1529 | |
1530 jfieldID j_ice_servers_id = GetFieldID( | |
1531 jni, j_rtc_config_class, "iceServers", "Ljava/util/List;"); | |
1532 jobject j_ice_servers = GetObjectField(jni, j_rtc_config, j_ice_servers_id); | |
1533 | |
1534 jfieldID j_audio_jitter_buffer_max_packets_id = | |
1535 GetFieldID(jni, j_rtc_config_class, "audioJitterBufferMaxPackets", "I"); | |
1536 jfieldID j_audio_jitter_buffer_fast_accelerate_id = GetFieldID( | |
1537 jni, j_rtc_config_class, "audioJitterBufferFastAccelerate", "Z"); | |
1538 | |
1539 jfieldID j_ice_connection_receiving_timeout_id = | |
1540 GetFieldID(jni, j_rtc_config_class, "iceConnectionReceivingTimeout", "I"); | |
1541 | |
1542 jfieldID j_ice_backup_candidate_pair_ping_interval_id = GetFieldID( | |
1543 jni, j_rtc_config_class, "iceBackupCandidatePairPingInterval", "I"); | |
1544 | |
1545 jfieldID j_continual_gathering_policy_id = | |
1546 GetFieldID(jni, j_rtc_config_class, "continualGatheringPolicy", | |
1547 "Lorg/webrtc/PeerConnection$ContinualGatheringPolicy;"); | |
1548 jobject j_continual_gathering_policy = | |
1549 GetObjectField(jni, j_rtc_config, j_continual_gathering_policy_id); | |
1550 | |
1551 rtc_config->type = | |
1552 JavaIceTransportsTypeToNativeType(jni, j_ice_transports_type); | |
1553 rtc_config->bundle_policy = | |
1554 JavaBundlePolicyToNativeType(jni, j_bundle_policy); | |
1555 rtc_config->rtcp_mux_policy = | |
1556 JavaRtcpMuxPolicyToNativeType(jni, j_rtcp_mux_policy); | |
1557 rtc_config->tcp_candidate_policy = | |
1558 JavaTcpCandidatePolicyToNativeType(jni, j_tcp_candidate_policy); | |
1559 JavaIceServersToJsepIceServers(jni, j_ice_servers, &rtc_config->servers); | |
1560 rtc_config->audio_jitter_buffer_max_packets = | |
1561 GetIntField(jni, j_rtc_config, j_audio_jitter_buffer_max_packets_id); | |
1562 rtc_config->audio_jitter_buffer_fast_accelerate = GetBooleanField( | |
1563 jni, j_rtc_config, j_audio_jitter_buffer_fast_accelerate_id); | |
1564 rtc_config->ice_connection_receiving_timeout = | |
1565 GetIntField(jni, j_rtc_config, j_ice_connection_receiving_timeout_id); | |
1566 rtc_config->ice_backup_candidate_pair_ping_interval = GetIntField( | |
1567 jni, j_rtc_config, j_ice_backup_candidate_pair_ping_interval_id); | |
1568 rtc_config->continual_gathering_policy = | |
1569 JavaContinualGatheringPolicyToNativeType( | |
1570 jni, j_continual_gathering_policy); | |
1571 } | |
1572 | |
1573 JOW(jlong, PeerConnectionFactory_nativeCreatePeerConnection)( | |
1574 JNIEnv *jni, jclass, jlong factory, jobject j_rtc_config, | |
1575 jobject j_constraints, jlong observer_p) { | |
1576 rtc::scoped_refptr<PeerConnectionFactoryInterface> f( | |
1577 reinterpret_cast<PeerConnectionFactoryInterface*>( | |
1578 factoryFromJava(factory))); | |
1579 | |
1580 PeerConnectionInterface::RTCConfiguration rtc_config; | |
1581 JavaRTCConfigurationToJsepRTCConfiguration(jni, j_rtc_config, &rtc_config); | |
1582 | |
1583 jclass j_rtc_config_class = GetObjectClass(jni, j_rtc_config); | |
1584 jfieldID j_key_type_id = GetFieldID(jni, j_rtc_config_class, "keyType", | |
1585 "Lorg/webrtc/PeerConnection$KeyType;"); | |
1586 jobject j_key_type = GetObjectField(jni, j_rtc_config, j_key_type_id); | |
1587 | |
1588 // Create ECDSA certificate. | |
1589 if (JavaKeyTypeToNativeType(jni, j_key_type) == rtc::KT_ECDSA) { | |
1590 scoped_ptr<rtc::SSLIdentity> ssl_identity( | |
1591 rtc::SSLIdentity::Generate(webrtc::kIdentityName, rtc::KT_ECDSA)); | |
1592 if (ssl_identity.get()) { | |
1593 rtc_config.certificates.push_back( | |
1594 rtc::RTCCertificate::Create(std::move(ssl_identity))); | |
1595 LOG(LS_INFO) << "ECDSA certificate created."; | |
1596 } else { | |
1597 // Failing to create certificate should not abort peer connection | |
1598 // creation. Instead default encryption (currently RSA) will be used. | |
1599 LOG(LS_WARNING) << | |
1600 "Failed to generate SSLIdentity. Default encryption will be used."; | |
1601 } | |
1602 } | |
1603 | |
1604 PCOJava* observer = reinterpret_cast<PCOJava*>(observer_p); | |
1605 observer->SetConstraints(new ConstraintsWrapper(jni, j_constraints)); | |
1606 rtc::scoped_refptr<PeerConnectionInterface> pc(f->CreatePeerConnection( | |
1607 rtc_config, observer->constraints(), NULL, NULL, observer)); | |
1608 return (jlong)pc.release(); | |
1609 } | |
1610 | |
1611 static rtc::scoped_refptr<PeerConnectionInterface> ExtractNativePC( | |
1612 JNIEnv* jni, jobject j_pc) { | |
1613 jfieldID native_pc_id = GetFieldID(jni, | |
1614 GetObjectClass(jni, j_pc), "nativePeerConnection", "J"); | |
1615 jlong j_p = GetLongField(jni, j_pc, native_pc_id); | |
1616 return rtc::scoped_refptr<PeerConnectionInterface>( | |
1617 reinterpret_cast<PeerConnectionInterface*>(j_p)); | |
1618 } | |
1619 | |
1620 JOW(jobject, PeerConnection_getLocalDescription)(JNIEnv* jni, jobject j_pc) { | |
1621 const SessionDescriptionInterface* sdp = | |
1622 ExtractNativePC(jni, j_pc)->local_description(); | |
1623 return sdp ? JavaSdpFromNativeSdp(jni, sdp) : NULL; | |
1624 } | |
1625 | |
1626 JOW(jobject, PeerConnection_getRemoteDescription)(JNIEnv* jni, jobject j_pc) { | |
1627 const SessionDescriptionInterface* sdp = | |
1628 ExtractNativePC(jni, j_pc)->remote_description(); | |
1629 return sdp ? JavaSdpFromNativeSdp(jni, sdp) : NULL; | |
1630 } | |
1631 | |
1632 JOW(jobject, PeerConnection_createDataChannel)( | |
1633 JNIEnv* jni, jobject j_pc, jstring j_label, jobject j_init) { | |
1634 DataChannelInit init = JavaDataChannelInitToNative(jni, j_init); | |
1635 rtc::scoped_refptr<DataChannelInterface> channel( | |
1636 ExtractNativePC(jni, j_pc)->CreateDataChannel( | |
1637 JavaToStdString(jni, j_label), &init)); | |
1638 // Mustn't pass channel.get() directly through NewObject to avoid reading its | |
1639 // vararg parameter as 64-bit and reading memory that doesn't belong to the | |
1640 // 32-bit parameter. | |
1641 jlong nativeChannelPtr = jlongFromPointer(channel.get()); | |
1642 RTC_CHECK(nativeChannelPtr) << "Failed to create DataChannel"; | |
1643 jclass j_data_channel_class = FindClass(jni, "org/webrtc/DataChannel"); | |
1644 jmethodID j_data_channel_ctor = GetMethodID( | |
1645 jni, j_data_channel_class, "<init>", "(J)V"); | |
1646 jobject j_channel = jni->NewObject( | |
1647 j_data_channel_class, j_data_channel_ctor, nativeChannelPtr); | |
1648 CHECK_EXCEPTION(jni) << "error during NewObject"; | |
1649 // Channel is now owned by Java object, and will be freed from there. | |
1650 int bumped_count = channel->AddRef(); | |
1651 RTC_CHECK(bumped_count == 2) << "Unexpected refcount"; | |
1652 return j_channel; | |
1653 } | |
1654 | |
1655 JOW(void, PeerConnection_createOffer)( | |
1656 JNIEnv* jni, jobject j_pc, jobject j_observer, jobject j_constraints) { | |
1657 ConstraintsWrapper* constraints = | |
1658 new ConstraintsWrapper(jni, j_constraints); | |
1659 rtc::scoped_refptr<CreateSdpObserverWrapper> observer( | |
1660 new rtc::RefCountedObject<CreateSdpObserverWrapper>( | |
1661 jni, j_observer, constraints)); | |
1662 ExtractNativePC(jni, j_pc)->CreateOffer(observer, constraints); | |
1663 } | |
1664 | |
1665 JOW(void, PeerConnection_createAnswer)( | |
1666 JNIEnv* jni, jobject j_pc, jobject j_observer, jobject j_constraints) { | |
1667 ConstraintsWrapper* constraints = | |
1668 new ConstraintsWrapper(jni, j_constraints); | |
1669 rtc::scoped_refptr<CreateSdpObserverWrapper> observer( | |
1670 new rtc::RefCountedObject<CreateSdpObserverWrapper>( | |
1671 jni, j_observer, constraints)); | |
1672 ExtractNativePC(jni, j_pc)->CreateAnswer(observer, constraints); | |
1673 } | |
1674 | |
1675 // Helper to create a SessionDescriptionInterface from a SessionDescription. | |
1676 static SessionDescriptionInterface* JavaSdpToNativeSdp( | |
1677 JNIEnv* jni, jobject j_sdp) { | |
1678 jfieldID j_type_id = GetFieldID( | |
1679 jni, GetObjectClass(jni, j_sdp), "type", | |
1680 "Lorg/webrtc/SessionDescription$Type;"); | |
1681 jobject j_type = GetObjectField(jni, j_sdp, j_type_id); | |
1682 jmethodID j_canonical_form_id = GetMethodID( | |
1683 jni, GetObjectClass(jni, j_type), "canonicalForm", | |
1684 "()Ljava/lang/String;"); | |
1685 jstring j_type_string = (jstring)jni->CallObjectMethod( | |
1686 j_type, j_canonical_form_id); | |
1687 CHECK_EXCEPTION(jni) << "error during CallObjectMethod"; | |
1688 std::string std_type = JavaToStdString(jni, j_type_string); | |
1689 | |
1690 jfieldID j_description_id = GetFieldID( | |
1691 jni, GetObjectClass(jni, j_sdp), "description", "Ljava/lang/String;"); | |
1692 jstring j_description = (jstring)GetObjectField(jni, j_sdp, j_description_id); | |
1693 std::string std_description = JavaToStdString(jni, j_description); | |
1694 | |
1695 return webrtc::CreateSessionDescription( | |
1696 std_type, std_description, NULL); | |
1697 } | |
1698 | |
1699 JOW(void, PeerConnection_setLocalDescription)( | |
1700 JNIEnv* jni, jobject j_pc, | |
1701 jobject j_observer, jobject j_sdp) { | |
1702 rtc::scoped_refptr<SetSdpObserverWrapper> observer( | |
1703 new rtc::RefCountedObject<SetSdpObserverWrapper>( | |
1704 jni, j_observer, reinterpret_cast<ConstraintsWrapper*>(NULL))); | |
1705 ExtractNativePC(jni, j_pc)->SetLocalDescription( | |
1706 observer, JavaSdpToNativeSdp(jni, j_sdp)); | |
1707 } | |
1708 | |
1709 JOW(void, PeerConnection_setRemoteDescription)( | |
1710 JNIEnv* jni, jobject j_pc, | |
1711 jobject j_observer, jobject j_sdp) { | |
1712 rtc::scoped_refptr<SetSdpObserverWrapper> observer( | |
1713 new rtc::RefCountedObject<SetSdpObserverWrapper>( | |
1714 jni, j_observer, reinterpret_cast<ConstraintsWrapper*>(NULL))); | |
1715 ExtractNativePC(jni, j_pc)->SetRemoteDescription( | |
1716 observer, JavaSdpToNativeSdp(jni, j_sdp)); | |
1717 } | |
1718 | |
1719 JOW(jboolean, PeerConnection_setConfiguration)( | |
1720 JNIEnv* jni, jobject j_pc, jobject j_rtc_config) { | |
1721 PeerConnectionInterface::RTCConfiguration rtc_config; | |
1722 JavaRTCConfigurationToJsepRTCConfiguration(jni, j_rtc_config, &rtc_config); | |
1723 return ExtractNativePC(jni, j_pc)->SetConfiguration(rtc_config); | |
1724 } | |
1725 | |
1726 JOW(jboolean, PeerConnection_nativeAddIceCandidate)( | |
1727 JNIEnv* jni, jobject j_pc, jstring j_sdp_mid, | |
1728 jint j_sdp_mline_index, jstring j_candidate_sdp) { | |
1729 std::string sdp_mid = JavaToStdString(jni, j_sdp_mid); | |
1730 std::string sdp = JavaToStdString(jni, j_candidate_sdp); | |
1731 scoped_ptr<IceCandidateInterface> candidate( | |
1732 webrtc::CreateIceCandidate(sdp_mid, j_sdp_mline_index, sdp, NULL)); | |
1733 return ExtractNativePC(jni, j_pc)->AddIceCandidate(candidate.get()); | |
1734 } | |
1735 | |
1736 JOW(jboolean, PeerConnection_nativeAddLocalStream)( | |
1737 JNIEnv* jni, jobject j_pc, jlong native_stream) { | |
1738 return ExtractNativePC(jni, j_pc)->AddStream( | |
1739 reinterpret_cast<MediaStreamInterface*>(native_stream)); | |
1740 } | |
1741 | |
1742 JOW(void, PeerConnection_nativeRemoveLocalStream)( | |
1743 JNIEnv* jni, jobject j_pc, jlong native_stream) { | |
1744 ExtractNativePC(jni, j_pc)->RemoveStream( | |
1745 reinterpret_cast<MediaStreamInterface*>(native_stream)); | |
1746 } | |
1747 | |
1748 JOW(jobject, PeerConnection_nativeCreateSender)( | |
1749 JNIEnv* jni, jobject j_pc, jstring j_kind, jstring j_stream_id) { | |
1750 jclass j_rtp_sender_class = FindClass(jni, "org/webrtc/RtpSender"); | |
1751 jmethodID j_rtp_sender_ctor = | |
1752 GetMethodID(jni, j_rtp_sender_class, "<init>", "(J)V"); | |
1753 | |
1754 std::string kind = JavaToStdString(jni, j_kind); | |
1755 std::string stream_id = JavaToStdString(jni, j_stream_id); | |
1756 rtc::scoped_refptr<RtpSenderInterface> sender = | |
1757 ExtractNativePC(jni, j_pc)->CreateSender(kind, stream_id); | |
1758 if (!sender.get()) { | |
1759 return nullptr; | |
1760 } | |
1761 jlong nativeSenderPtr = jlongFromPointer(sender.get()); | |
1762 jobject j_sender = | |
1763 jni->NewObject(j_rtp_sender_class, j_rtp_sender_ctor, nativeSenderPtr); | |
1764 CHECK_EXCEPTION(jni) << "error during NewObject"; | |
1765 // Sender is now owned by the Java object, and will be freed from | |
1766 // RtpSender.dispose(), called by PeerConnection.dispose() or getSenders(). | |
1767 sender->AddRef(); | |
1768 return j_sender; | |
1769 } | |
1770 | |
1771 JOW(jobject, PeerConnection_nativeGetSenders)(JNIEnv* jni, jobject j_pc) { | |
1772 jclass j_array_list_class = FindClass(jni, "java/util/ArrayList"); | |
1773 jmethodID j_array_list_ctor = | |
1774 GetMethodID(jni, j_array_list_class, "<init>", "()V"); | |
1775 jmethodID j_array_list_add = | |
1776 GetMethodID(jni, j_array_list_class, "add", "(Ljava/lang/Object;)Z"); | |
1777 jobject j_senders = jni->NewObject(j_array_list_class, j_array_list_ctor); | |
1778 CHECK_EXCEPTION(jni) << "error during NewObject"; | |
1779 | |
1780 jclass j_rtp_sender_class = FindClass(jni, "org/webrtc/RtpSender"); | |
1781 jmethodID j_rtp_sender_ctor = | |
1782 GetMethodID(jni, j_rtp_sender_class, "<init>", "(J)V"); | |
1783 | |
1784 auto senders = ExtractNativePC(jni, j_pc)->GetSenders(); | |
1785 for (const auto& sender : senders) { | |
1786 jlong nativeSenderPtr = jlongFromPointer(sender.get()); | |
1787 jobject j_sender = | |
1788 jni->NewObject(j_rtp_sender_class, j_rtp_sender_ctor, nativeSenderPtr); | |
1789 CHECK_EXCEPTION(jni) << "error during NewObject"; | |
1790 // Sender is now owned by the Java object, and will be freed from | |
1791 // RtpSender.dispose(), called by PeerConnection.dispose() or getSenders(). | |
1792 sender->AddRef(); | |
1793 jni->CallBooleanMethod(j_senders, j_array_list_add, j_sender); | |
1794 CHECK_EXCEPTION(jni) << "error during CallBooleanMethod"; | |
1795 } | |
1796 return j_senders; | |
1797 } | |
1798 | |
1799 JOW(jobject, PeerConnection_nativeGetReceivers)(JNIEnv* jni, jobject j_pc) { | |
1800 jclass j_array_list_class = FindClass(jni, "java/util/ArrayList"); | |
1801 jmethodID j_array_list_ctor = | |
1802 GetMethodID(jni, j_array_list_class, "<init>", "()V"); | |
1803 jmethodID j_array_list_add = | |
1804 GetMethodID(jni, j_array_list_class, "add", "(Ljava/lang/Object;)Z"); | |
1805 jobject j_receivers = jni->NewObject(j_array_list_class, j_array_list_ctor); | |
1806 CHECK_EXCEPTION(jni) << "error during NewObject"; | |
1807 | |
1808 jclass j_rtp_receiver_class = FindClass(jni, "org/webrtc/RtpReceiver"); | |
1809 jmethodID j_rtp_receiver_ctor = | |
1810 GetMethodID(jni, j_rtp_receiver_class, "<init>", "(J)V"); | |
1811 | |
1812 auto receivers = ExtractNativePC(jni, j_pc)->GetReceivers(); | |
1813 for (const auto& receiver : receivers) { | |
1814 jlong nativeReceiverPtr = jlongFromPointer(receiver.get()); | |
1815 jobject j_receiver = jni->NewObject(j_rtp_receiver_class, | |
1816 j_rtp_receiver_ctor, nativeReceiverPtr); | |
1817 CHECK_EXCEPTION(jni) << "error during NewObject"; | |
1818 // Receiver is now owned by Java object, and will be freed from there. | |
1819 receiver->AddRef(); | |
1820 jni->CallBooleanMethod(j_receivers, j_array_list_add, j_receiver); | |
1821 CHECK_EXCEPTION(jni) << "error during CallBooleanMethod"; | |
1822 } | |
1823 return j_receivers; | |
1824 } | |
1825 | |
1826 JOW(bool, PeerConnection_nativeGetStats)( | |
1827 JNIEnv* jni, jobject j_pc, jobject j_observer, jlong native_track) { | |
1828 rtc::scoped_refptr<StatsObserverWrapper> observer( | |
1829 new rtc::RefCountedObject<StatsObserverWrapper>(jni, j_observer)); | |
1830 return ExtractNativePC(jni, j_pc)->GetStats( | |
1831 observer, | |
1832 reinterpret_cast<MediaStreamTrackInterface*>(native_track), | |
1833 PeerConnectionInterface::kStatsOutputLevelStandard); | |
1834 } | |
1835 | |
1836 JOW(jobject, PeerConnection_signalingState)(JNIEnv* jni, jobject j_pc) { | |
1837 PeerConnectionInterface::SignalingState state = | |
1838 ExtractNativePC(jni, j_pc)->signaling_state(); | |
1839 return JavaEnumFromIndex(jni, "PeerConnection$SignalingState", state); | |
1840 } | |
1841 | |
1842 JOW(jobject, PeerConnection_iceConnectionState)(JNIEnv* jni, jobject j_pc) { | |
1843 PeerConnectionInterface::IceConnectionState state = | |
1844 ExtractNativePC(jni, j_pc)->ice_connection_state(); | |
1845 return JavaEnumFromIndex(jni, "PeerConnection$IceConnectionState", state); | |
1846 } | |
1847 | |
1848 JOW(jobject, PeerConnection_iceGatheringState)(JNIEnv* jni, jobject j_pc) { | |
1849 PeerConnectionInterface::IceGatheringState state = | |
1850 ExtractNativePC(jni, j_pc)->ice_gathering_state(); | |
1851 return JavaEnumFromIndex(jni, "PeerConnection$IceGatheringState", state); | |
1852 } | |
1853 | |
1854 JOW(void, PeerConnection_close)(JNIEnv* jni, jobject j_pc) { | |
1855 ExtractNativePC(jni, j_pc)->Close(); | |
1856 return; | |
1857 } | |
1858 | |
1859 JOW(jobject, MediaSource_nativeState)(JNIEnv* jni, jclass, jlong j_p) { | |
1860 rtc::scoped_refptr<MediaSourceInterface> p( | |
1861 reinterpret_cast<MediaSourceInterface*>(j_p)); | |
1862 return JavaEnumFromIndex(jni, "MediaSource$State", p->state()); | |
1863 } | |
1864 | |
1865 JOW(jlong, VideoRenderer_nativeWrapVideoRenderer)( | |
1866 JNIEnv* jni, jclass, jobject j_callbacks) { | |
1867 scoped_ptr<JavaVideoRendererWrapper> renderer( | |
1868 new JavaVideoRendererWrapper(jni, j_callbacks)); | |
1869 return (jlong)renderer.release(); | |
1870 } | |
1871 | |
1872 JOW(void, VideoRenderer_nativeCopyPlane)( | |
1873 JNIEnv *jni, jclass, jobject j_src_buffer, jint width, jint height, | |
1874 jint src_stride, jobject j_dst_buffer, jint dst_stride) { | |
1875 size_t src_size = jni->GetDirectBufferCapacity(j_src_buffer); | |
1876 size_t dst_size = jni->GetDirectBufferCapacity(j_dst_buffer); | |
1877 RTC_CHECK(src_stride >= width) << "Wrong source stride " << src_stride; | |
1878 RTC_CHECK(dst_stride >= width) << "Wrong destination stride " << dst_stride; | |
1879 RTC_CHECK(src_size >= src_stride * height) | |
1880 << "Insufficient source buffer capacity " << src_size; | |
1881 RTC_CHECK(dst_size >= dst_stride * height) | |
1882 << "Isufficient destination buffer capacity " << dst_size; | |
1883 uint8_t *src = | |
1884 reinterpret_cast<uint8_t*>(jni->GetDirectBufferAddress(j_src_buffer)); | |
1885 uint8_t *dst = | |
1886 reinterpret_cast<uint8_t*>(jni->GetDirectBufferAddress(j_dst_buffer)); | |
1887 if (src_stride == dst_stride) { | |
1888 memcpy(dst, src, src_stride * height); | |
1889 } else { | |
1890 for (int i = 0; i < height; i++) { | |
1891 memcpy(dst, src, width); | |
1892 src += src_stride; | |
1893 dst += dst_stride; | |
1894 } | |
1895 } | |
1896 } | |
1897 | |
1898 JOW(void, VideoSource_stop)(JNIEnv* jni, jclass, jlong j_p) { | |
1899 reinterpret_cast<VideoSourceInterface*>(j_p)->Stop(); | |
1900 } | |
1901 | |
1902 JOW(void, VideoSource_restart)( | |
1903 JNIEnv* jni, jclass, jlong j_p_source, jlong j_p_format) { | |
1904 reinterpret_cast<VideoSourceInterface*>(j_p_source)->Restart(); | |
1905 } | |
1906 | |
1907 JOW(jstring, MediaStreamTrack_nativeId)(JNIEnv* jni, jclass, jlong j_p) { | |
1908 return JavaStringFromStdString( | |
1909 jni, reinterpret_cast<MediaStreamTrackInterface*>(j_p)->id()); | |
1910 } | |
1911 | |
1912 JOW(jstring, MediaStreamTrack_nativeKind)(JNIEnv* jni, jclass, jlong j_p) { | |
1913 return JavaStringFromStdString( | |
1914 jni, reinterpret_cast<MediaStreamTrackInterface*>(j_p)->kind()); | |
1915 } | |
1916 | |
1917 JOW(jboolean, MediaStreamTrack_nativeEnabled)(JNIEnv* jni, jclass, jlong j_p) { | |
1918 return reinterpret_cast<MediaStreamTrackInterface*>(j_p)->enabled(); | |
1919 } | |
1920 | |
1921 JOW(jobject, MediaStreamTrack_nativeState)(JNIEnv* jni, jclass, jlong j_p) { | |
1922 return JavaEnumFromIndex( | |
1923 jni, | |
1924 "MediaStreamTrack$State", | |
1925 reinterpret_cast<MediaStreamTrackInterface*>(j_p)->state()); | |
1926 } | |
1927 | |
1928 JOW(jboolean, MediaStreamTrack_nativeSetState)( | |
1929 JNIEnv* jni, jclass, jlong j_p, jint j_new_state) { | |
1930 MediaStreamTrackInterface::TrackState new_state = | |
1931 (MediaStreamTrackInterface::TrackState)j_new_state; | |
1932 return reinterpret_cast<MediaStreamTrackInterface*>(j_p) | |
1933 ->set_state(new_state); | |
1934 } | |
1935 | |
1936 JOW(jboolean, MediaStreamTrack_nativeSetEnabled)( | |
1937 JNIEnv* jni, jclass, jlong j_p, jboolean enabled) { | |
1938 return reinterpret_cast<MediaStreamTrackInterface*>(j_p) | |
1939 ->set_enabled(enabled); | |
1940 } | |
1941 | |
1942 JOW(void, VideoTrack_nativeAddRenderer)( | |
1943 JNIEnv* jni, jclass, | |
1944 jlong j_video_track_pointer, jlong j_renderer_pointer) { | |
1945 reinterpret_cast<VideoTrackInterface*>(j_video_track_pointer)->AddRenderer( | |
1946 reinterpret_cast<VideoRendererInterface*>(j_renderer_pointer)); | |
1947 } | |
1948 | |
1949 JOW(void, VideoTrack_nativeRemoveRenderer)( | |
1950 JNIEnv* jni, jclass, | |
1951 jlong j_video_track_pointer, jlong j_renderer_pointer) { | |
1952 reinterpret_cast<VideoTrackInterface*>(j_video_track_pointer)->RemoveRenderer( | |
1953 reinterpret_cast<VideoRendererInterface*>(j_renderer_pointer)); | |
1954 } | |
1955 | |
1956 JOW(jlong, CallSessionFileRotatingLogSink_nativeAddSink)( | |
1957 JNIEnv* jni, jclass, | |
1958 jstring j_dirPath, jint j_maxFileSize, jint j_severity) { | |
1959 std::string dir_path = JavaToStdString(jni, j_dirPath); | |
1960 rtc::CallSessionFileRotatingLogSink* sink = | |
1961 new rtc::CallSessionFileRotatingLogSink(dir_path, j_maxFileSize); | |
1962 if (!sink->Init()) { | |
1963 LOG_V(rtc::LoggingSeverity::LS_WARNING) << | |
1964 "Failed to init CallSessionFileRotatingLogSink for path " << dir_path; | |
1965 delete sink; | |
1966 return 0; | |
1967 } | |
1968 rtc::LogMessage::AddLogToStream( | |
1969 sink, static_cast<rtc::LoggingSeverity>(j_severity)); | |
1970 return (jlong) sink; | |
1971 } | |
1972 | |
1973 JOW(void, CallSessionFileRotatingLogSink_nativeDeleteSink)( | |
1974 JNIEnv* jni, jclass, jlong j_sink) { | |
1975 rtc::CallSessionFileRotatingLogSink* sink = | |
1976 reinterpret_cast<rtc::CallSessionFileRotatingLogSink*>(j_sink); | |
1977 rtc::LogMessage::RemoveLogToStream(sink); | |
1978 delete sink; | |
1979 } | |
1980 | |
1981 JOW(jbyteArray, CallSessionFileRotatingLogSink_nativeGetLogData)( | |
1982 JNIEnv* jni, jclass, jstring j_dirPath) { | |
1983 std::string dir_path = JavaToStdString(jni, j_dirPath); | |
1984 rtc::scoped_ptr<rtc::CallSessionFileRotatingStream> stream( | |
1985 new rtc::CallSessionFileRotatingStream(dir_path)); | |
1986 if (!stream->Open()) { | |
1987 LOG_V(rtc::LoggingSeverity::LS_WARNING) << | |
1988 "Failed to open CallSessionFileRotatingStream for path " << dir_path; | |
1989 return jni->NewByteArray(0); | |
1990 } | |
1991 size_t log_size = 0; | |
1992 if (!stream->GetSize(&log_size) || log_size == 0) { | |
1993 LOG_V(rtc::LoggingSeverity::LS_WARNING) << | |
1994 "CallSessionFileRotatingStream returns 0 size for path " << dir_path; | |
1995 return jni->NewByteArray(0); | |
1996 } | |
1997 | |
1998 size_t read = 0; | |
1999 rtc::scoped_ptr<jbyte> buffer(static_cast<jbyte*>(malloc(log_size))); | |
2000 stream->ReadAll(buffer.get(), log_size, &read, nullptr); | |
2001 | |
2002 jbyteArray result = jni->NewByteArray(read); | |
2003 jni->SetByteArrayRegion(result, 0, read, buffer.get()); | |
2004 | |
2005 return result; | |
2006 } | |
2007 | |
2008 JOW(jboolean, RtpSender_nativeSetTrack)(JNIEnv* jni, | |
2009 jclass, | |
2010 jlong j_rtp_sender_pointer, | |
2011 jlong j_track_pointer) { | |
2012 return reinterpret_cast<RtpSenderInterface*>(j_rtp_sender_pointer) | |
2013 ->SetTrack(reinterpret_cast<MediaStreamTrackInterface*>(j_track_pointer)); | |
2014 } | |
2015 | |
2016 JOW(jlong, RtpSender_nativeGetTrack)(JNIEnv* jni, | |
2017 jclass, | |
2018 jlong j_rtp_sender_pointer, | |
2019 jlong j_track_pointer) { | |
2020 return jlongFromPointer( | |
2021 reinterpret_cast<RtpSenderInterface*>(j_rtp_sender_pointer) | |
2022 ->track() | |
2023 .release()); | |
2024 } | |
2025 | |
2026 JOW(jstring, RtpSender_nativeId)( | |
2027 JNIEnv* jni, jclass, jlong j_rtp_sender_pointer) { | |
2028 return JavaStringFromStdString( | |
2029 jni, reinterpret_cast<RtpSenderInterface*>(j_rtp_sender_pointer)->id()); | |
2030 } | |
2031 | |
2032 JOW(void, RtpSender_free)(JNIEnv* jni, jclass, jlong j_rtp_sender_pointer) { | |
2033 reinterpret_cast<RtpSenderInterface*>(j_rtp_sender_pointer)->Release(); | |
2034 } | |
2035 | |
2036 JOW(jlong, RtpReceiver_nativeGetTrack)(JNIEnv* jni, | |
2037 jclass, | |
2038 jlong j_rtp_receiver_pointer, | |
2039 jlong j_track_pointer) { | |
2040 return jlongFromPointer( | |
2041 reinterpret_cast<RtpReceiverInterface*>(j_rtp_receiver_pointer) | |
2042 ->track() | |
2043 .release()); | |
2044 } | |
2045 | |
2046 JOW(jstring, RtpReceiver_nativeId)( | |
2047 JNIEnv* jni, jclass, jlong j_rtp_receiver_pointer) { | |
2048 return JavaStringFromStdString( | |
2049 jni, | |
2050 reinterpret_cast<RtpReceiverInterface*>(j_rtp_receiver_pointer)->id()); | |
2051 } | |
2052 | |
2053 JOW(void, RtpReceiver_free)(JNIEnv* jni, jclass, jlong j_rtp_receiver_pointer) { | |
2054 reinterpret_cast<RtpReceiverInterface*>(j_rtp_receiver_pointer)->Release(); | |
2055 } | |
2056 | |
2057 } // namespace webrtc_jni | |
OLD | NEW |