Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(462)

Side by Side Diff: talk/app/webrtc/java/jni/peerconnection_jni.cc

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

Powered by Google App Engine
This is Rietveld 408576698