| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright 2013 The WebRTC project authors. All Rights Reserved. | |
| 3 * | |
| 4 * Use of this source code is governed by a BSD-style license | |
| 5 * that can be found in the LICENSE file in the root of the source | |
| 6 * tree. An additional intellectual property rights grant can be found | |
| 7 * in the file PATENTS. All contributing project authors may | |
| 8 * be found in the AUTHORS file in the root of the source tree. | |
| 9 */ | |
| 10 | |
| 11 // Hints for future visitors: | |
| 12 // This entire file is an implementation detail of the org.webrtc Java package, | |
| 13 // the most interesting bits of which are org.webrtc.PeerConnection{,Factory}. | |
| 14 // The layout of this file is roughly: | |
| 15 // - various helper C++ functions & classes that wrap Java counterparts and | |
| 16 // expose a C++ interface that can be passed to the C++ PeerConnection APIs | |
| 17 // - implementations of methods declared "static" in the Java package (named | |
| 18 // things like Java_org_webrtc_OMG_Can_This_Name_Be_Any_Longer, prescribed by | |
| 19 // the JNI spec). | |
| 20 // | |
| 21 // Lifecycle notes: objects are owned where they will be called; in other words | |
| 22 // FooObservers are owned by C++-land, and user-callable objects (e.g. | |
| 23 // PeerConnection and VideoTrack) are owned by Java-land. | |
| 24 // When this file allocates C++ RefCountInterfaces it AddRef()s an artificial | |
| 25 // ref simulating the jlong held in Java-land, and then Release()s the ref in | |
| 26 // the respective free call. Sometimes this AddRef is implicit in the | |
| 27 // construction of a scoped_refptr<> which is then .release()d. | |
| 28 // Any persistent (non-local) references from C++ to Java must be global or weak | |
| 29 // (in which case they must be checked before use)! | |
| 30 // | |
| 31 // Exception notes: pretty much all JNI calls can throw Java exceptions, so each | |
| 32 // call through a JNIEnv* pointer needs to be followed by an ExceptionCheck() | |
| 33 // call. In this file this is done in CHECK_EXCEPTION, making for much easier | |
| 34 // debugging in case of failure (the alternative is to wait for control to | |
| 35 // return to the Java frame that called code in this file, at which point it's | |
| 36 // impossible to tell which JNI call broke). | |
| 37 | |
| 38 #include <jni.h> | |
| 39 #undef JNIEXPORT | |
| 40 #define JNIEXPORT __attribute__((visibility("default"))) | |
| 41 | |
| 42 #include <limits> | |
| 43 #include <memory> | |
| 44 #include <utility> | |
| 45 | |
| 46 #include "webrtc/api/androidvideocapturer.h" | |
| 47 #include "webrtc/api/dtlsidentitystore.h" | |
| 48 #include "webrtc/api/java/jni/androidmediadecoder_jni.h" | |
| 49 #include "webrtc/api/java/jni/androidmediaencoder_jni.h" | |
| 50 #include "webrtc/api/java/jni/androidnetworkmonitor_jni.h" | |
| 51 #include "webrtc/api/java/jni/androidvideocapturer_jni.h" | |
| 52 #include "webrtc/api/java/jni/classreferenceholder.h" | |
| 53 #include "webrtc/api/java/jni/jni_helpers.h" | |
| 54 #include "webrtc/api/java/jni/native_handle_impl.h" | |
| 55 #include "webrtc/api/mediaconstraintsinterface.h" | |
| 56 #include "webrtc/api/peerconnectioninterface.h" | |
| 57 #include "webrtc/api/rtpreceiverinterface.h" | |
| 58 #include "webrtc/api/rtpsenderinterface.h" | |
| 59 #include "webrtc/api/webrtcsdp.h" | |
| 60 #include "webrtc/base/bind.h" | |
| 61 #include "webrtc/base/checks.h" | |
| 62 #include "webrtc/base/event_tracer.h" | |
| 63 #include "webrtc/base/logging.h" | |
| 64 #include "webrtc/base/logsinks.h" | |
| 65 #include "webrtc/base/messagequeue.h" | |
| 66 #include "webrtc/base/networkmonitor.h" | |
| 67 #include "webrtc/base/rtccertificategenerator.h" | |
| 68 #include "webrtc/base/ssladapter.h" | |
| 69 #include "webrtc/base/stringutils.h" | |
| 70 #include "webrtc/media/base/videocapturer.h" | |
| 71 #include "webrtc/media/engine/webrtcvideodecoderfactory.h" | |
| 72 #include "webrtc/media/engine/webrtcvideoencoderfactory.h" | |
| 73 #include "webrtc/system_wrappers/include/field_trial_default.h" | |
| 74 #include "webrtc/system_wrappers/include/logcat_trace_context.h" | |
| 75 #include "webrtc/system_wrappers/include/trace.h" | |
| 76 #include "webrtc/voice_engine/include/voe_base.h" | |
| 77 | |
| 78 using cricket::WebRtcVideoDecoderFactory; | |
| 79 using cricket::WebRtcVideoEncoderFactory; | |
| 80 using rtc::Bind; | |
| 81 using rtc::Thread; | |
| 82 using rtc::ThreadManager; | |
| 83 using webrtc::AudioSourceInterface; | |
| 84 using webrtc::AudioTrackInterface; | |
| 85 using webrtc::AudioTrackVector; | |
| 86 using webrtc::CreateSessionDescriptionObserver; | |
| 87 using webrtc::DataBuffer; | |
| 88 using webrtc::DataChannelInit; | |
| 89 using webrtc::DataChannelInterface; | |
| 90 using webrtc::DataChannelObserver; | |
| 91 using webrtc::IceCandidateInterface; | |
| 92 using webrtc::LogcatTraceContext; | |
| 93 using webrtc::MediaConstraintsInterface; | |
| 94 using webrtc::MediaSourceInterface; | |
| 95 using webrtc::MediaStreamInterface; | |
| 96 using webrtc::MediaStreamTrackInterface; | |
| 97 using webrtc::PeerConnectionFactoryInterface; | |
| 98 using webrtc::PeerConnectionInterface; | |
| 99 using webrtc::PeerConnectionObserver; | |
| 100 using webrtc::RtpReceiverInterface; | |
| 101 using webrtc::RtpSenderInterface; | |
| 102 using webrtc::SessionDescriptionInterface; | |
| 103 using webrtc::SetSessionDescriptionObserver; | |
| 104 using webrtc::StatsObserver; | |
| 105 using webrtc::StatsReport; | |
| 106 using webrtc::StatsReports; | |
| 107 using webrtc::VideoTrackSourceInterface; | |
| 108 using webrtc::VideoTrackInterface; | |
| 109 using webrtc::VideoTrackVector; | |
| 110 using webrtc::kVideoCodecVP8; | |
| 111 | |
| 112 namespace webrtc_jni { | |
| 113 | |
| 114 // Field trials initialization string | |
| 115 static char *field_trials_init_string = NULL; | |
| 116 | |
| 117 // Set in PeerConnectionFactory_initializeAndroidGlobals(). | |
| 118 static bool factory_static_initialized = false; | |
| 119 static bool video_hw_acceleration_enabled = true; | |
| 120 | |
| 121 // Return the (singleton) Java Enum object corresponding to |index|; | |
| 122 // |state_class_fragment| is something like "MediaSource$State". | |
| 123 static jobject JavaEnumFromIndex( | |
| 124 JNIEnv* jni, const std::string& state_class_fragment, int index) { | |
| 125 const std::string state_class = "org/webrtc/" + state_class_fragment; | |
| 126 return JavaEnumFromIndex(jni, FindClass(jni, state_class.c_str()), | |
| 127 state_class, index); | |
| 128 } | |
| 129 | |
| 130 static DataChannelInit JavaDataChannelInitToNative( | |
| 131 JNIEnv* jni, jobject j_init) { | |
| 132 DataChannelInit init; | |
| 133 | |
| 134 jclass j_init_class = FindClass(jni, "org/webrtc/DataChannel$Init"); | |
| 135 jfieldID ordered_id = GetFieldID(jni, j_init_class, "ordered", "Z"); | |
| 136 jfieldID max_retransmit_time_id = | |
| 137 GetFieldID(jni, j_init_class, "maxRetransmitTimeMs", "I"); | |
| 138 jfieldID max_retransmits_id = | |
| 139 GetFieldID(jni, j_init_class, "maxRetransmits", "I"); | |
| 140 jfieldID protocol_id = | |
| 141 GetFieldID(jni, j_init_class, "protocol", "Ljava/lang/String;"); | |
| 142 jfieldID negotiated_id = GetFieldID(jni, j_init_class, "negotiated", "Z"); | |
| 143 jfieldID id_id = GetFieldID(jni, j_init_class, "id", "I"); | |
| 144 | |
| 145 init.ordered = GetBooleanField(jni, j_init, ordered_id); | |
| 146 init.maxRetransmitTime = GetIntField(jni, j_init, max_retransmit_time_id); | |
| 147 init.maxRetransmits = GetIntField(jni, j_init, max_retransmits_id); | |
| 148 init.protocol = JavaToStdString( | |
| 149 jni, GetStringField(jni, j_init, protocol_id)); | |
| 150 init.negotiated = GetBooleanField(jni, j_init, negotiated_id); | |
| 151 init.id = GetIntField(jni, j_init, id_id); | |
| 152 | |
| 153 return init; | |
| 154 } | |
| 155 | |
| 156 class ConstraintsWrapper; | |
| 157 | |
| 158 // Adapter between the C++ PeerConnectionObserver interface and the Java | |
| 159 // PeerConnection.Observer interface. Wraps an instance of the Java interface | |
| 160 // and dispatches C++ callbacks to Java. | |
| 161 class PCOJava : public PeerConnectionObserver { | |
| 162 public: | |
| 163 // We need these using declarations because there are two versions of each of | |
| 164 // the below methods and we only override one of them. | |
| 165 // TODO(deadbeef): Remove once there's only one version of the methods. | |
| 166 using PeerConnectionObserver::OnAddStream; | |
| 167 using PeerConnectionObserver::OnRemoveStream; | |
| 168 using PeerConnectionObserver::OnDataChannel; | |
| 169 | |
| 170 PCOJava(JNIEnv* jni, jobject j_observer) | |
| 171 : j_observer_global_(jni, j_observer), | |
| 172 j_observer_class_(jni, GetObjectClass(jni, *j_observer_global_)), | |
| 173 j_media_stream_class_(jni, FindClass(jni, "org/webrtc/MediaStream")), | |
| 174 j_media_stream_ctor_(GetMethodID( | |
| 175 jni, *j_media_stream_class_, "<init>", "(J)V")), | |
| 176 j_audio_track_class_(jni, FindClass(jni, "org/webrtc/AudioTrack")), | |
| 177 j_audio_track_ctor_(GetMethodID( | |
| 178 jni, *j_audio_track_class_, "<init>", "(J)V")), | |
| 179 j_video_track_class_(jni, FindClass(jni, "org/webrtc/VideoTrack")), | |
| 180 j_video_track_ctor_(GetMethodID( | |
| 181 jni, *j_video_track_class_, "<init>", "(J)V")), | |
| 182 j_data_channel_class_(jni, FindClass(jni, "org/webrtc/DataChannel")), | |
| 183 j_data_channel_ctor_(GetMethodID( | |
| 184 jni, *j_data_channel_class_, "<init>", "(J)V")) { | |
| 185 } | |
| 186 | |
| 187 virtual ~PCOJava() { | |
| 188 ScopedLocalRefFrame local_ref_frame(jni()); | |
| 189 while (!remote_streams_.empty()) | |
| 190 DisposeRemoteStream(remote_streams_.begin()); | |
| 191 } | |
| 192 | |
| 193 void OnIceCandidate(const IceCandidateInterface* candidate) override { | |
| 194 ScopedLocalRefFrame local_ref_frame(jni()); | |
| 195 std::string sdp; | |
| 196 RTC_CHECK(candidate->ToString(&sdp)) << "got so far: " << sdp; | |
| 197 jclass candidate_class = FindClass(jni(), "org/webrtc/IceCandidate"); | |
| 198 jmethodID ctor = GetMethodID(jni(), candidate_class, | |
| 199 "<init>", "(Ljava/lang/String;ILjava/lang/String;)V"); | |
| 200 jstring j_mid = JavaStringFromStdString(jni(), candidate->sdp_mid()); | |
| 201 jstring j_sdp = JavaStringFromStdString(jni(), sdp); | |
| 202 jobject j_candidate = jni()->NewObject(candidate_class, ctor, j_mid, | |
| 203 candidate->sdp_mline_index(), j_sdp); | |
| 204 CHECK_EXCEPTION(jni()) << "error during NewObject"; | |
| 205 jmethodID m = GetMethodID(jni(), *j_observer_class_, | |
| 206 "onIceCandidate", "(Lorg/webrtc/IceCandidate;)V"); | |
| 207 jni()->CallVoidMethod(*j_observer_global_, m, j_candidate); | |
| 208 CHECK_EXCEPTION(jni()) << "error during CallVoidMethod"; | |
| 209 } | |
| 210 | |
| 211 void OnIceCandidatesRemoved( | |
| 212 const std::vector<cricket::Candidate>& candidates) override { | |
| 213 ScopedLocalRefFrame local_ref_frame(jni()); | |
| 214 jobjectArray candidates_array = ToJavaCandidateArray(jni(), candidates); | |
| 215 jmethodID m = | |
| 216 GetMethodID(jni(), *j_observer_class_, "onIceCandidatesRemoved", | |
| 217 "([Lorg/webrtc/IceCandidate;)V"); | |
| 218 jni()->CallVoidMethod(*j_observer_global_, m, candidates_array); | |
| 219 CHECK_EXCEPTION(jni()) << "Error during CallVoidMethod"; | |
| 220 } | |
| 221 | |
| 222 void OnSignalingChange( | |
| 223 PeerConnectionInterface::SignalingState new_state) override { | |
| 224 ScopedLocalRefFrame local_ref_frame(jni()); | |
| 225 jmethodID m = GetMethodID( | |
| 226 jni(), *j_observer_class_, "onSignalingChange", | |
| 227 "(Lorg/webrtc/PeerConnection$SignalingState;)V"); | |
| 228 jobject new_state_enum = | |
| 229 JavaEnumFromIndex(jni(), "PeerConnection$SignalingState", new_state); | |
| 230 jni()->CallVoidMethod(*j_observer_global_, m, new_state_enum); | |
| 231 CHECK_EXCEPTION(jni()) << "error during CallVoidMethod"; | |
| 232 } | |
| 233 | |
| 234 void OnIceConnectionChange( | |
| 235 PeerConnectionInterface::IceConnectionState new_state) override { | |
| 236 ScopedLocalRefFrame local_ref_frame(jni()); | |
| 237 jmethodID m = GetMethodID( | |
| 238 jni(), *j_observer_class_, "onIceConnectionChange", | |
| 239 "(Lorg/webrtc/PeerConnection$IceConnectionState;)V"); | |
| 240 jobject new_state_enum = JavaEnumFromIndex( | |
| 241 jni(), "PeerConnection$IceConnectionState", new_state); | |
| 242 jni()->CallVoidMethod(*j_observer_global_, m, new_state_enum); | |
| 243 CHECK_EXCEPTION(jni()) << "error during CallVoidMethod"; | |
| 244 } | |
| 245 | |
| 246 void OnIceConnectionReceivingChange(bool receiving) override { | |
| 247 ScopedLocalRefFrame local_ref_frame(jni()); | |
| 248 jmethodID m = GetMethodID( | |
| 249 jni(), *j_observer_class_, "onIceConnectionReceivingChange", "(Z)V"); | |
| 250 jni()->CallVoidMethod(*j_observer_global_, m, receiving); | |
| 251 CHECK_EXCEPTION(jni()) << "error during CallVoidMethod"; | |
| 252 } | |
| 253 | |
| 254 void OnIceGatheringChange( | |
| 255 PeerConnectionInterface::IceGatheringState new_state) override { | |
| 256 ScopedLocalRefFrame local_ref_frame(jni()); | |
| 257 jmethodID m = GetMethodID( | |
| 258 jni(), *j_observer_class_, "onIceGatheringChange", | |
| 259 "(Lorg/webrtc/PeerConnection$IceGatheringState;)V"); | |
| 260 jobject new_state_enum = JavaEnumFromIndex( | |
| 261 jni(), "PeerConnection$IceGatheringState", new_state); | |
| 262 jni()->CallVoidMethod(*j_observer_global_, m, new_state_enum); | |
| 263 CHECK_EXCEPTION(jni()) << "error during CallVoidMethod"; | |
| 264 } | |
| 265 | |
| 266 void OnAddStream(rtc::scoped_refptr<MediaStreamInterface> stream) override { | |
| 267 ScopedLocalRefFrame local_ref_frame(jni()); | |
| 268 // Java MediaStream holds one reference. Corresponding Release() is in | |
| 269 // MediaStream_free, triggered by MediaStream.dispose(). | |
| 270 stream->AddRef(); | |
| 271 jobject j_stream = | |
| 272 jni()->NewObject(*j_media_stream_class_, j_media_stream_ctor_, | |
| 273 reinterpret_cast<jlong>(stream.get())); | |
| 274 CHECK_EXCEPTION(jni()) << "error during NewObject"; | |
| 275 | |
| 276 for (const auto& track : stream->GetAudioTracks()) { | |
| 277 jstring id = JavaStringFromStdString(jni(), track->id()); | |
| 278 // Java AudioTrack holds one reference. Corresponding Release() is in | |
| 279 // MediaStreamTrack_free, triggered by AudioTrack.dispose(). | |
| 280 track->AddRef(); | |
| 281 jobject j_track = | |
| 282 jni()->NewObject(*j_audio_track_class_, j_audio_track_ctor_, | |
| 283 reinterpret_cast<jlong>(track.get()), id); | |
| 284 CHECK_EXCEPTION(jni()) << "error during NewObject"; | |
| 285 jfieldID audio_tracks_id = GetFieldID(jni(), | |
| 286 *j_media_stream_class_, | |
| 287 "audioTracks", | |
| 288 "Ljava/util/LinkedList;"); | |
| 289 jobject audio_tracks = GetObjectField(jni(), j_stream, audio_tracks_id); | |
| 290 jmethodID add = GetMethodID(jni(), | |
| 291 GetObjectClass(jni(), audio_tracks), | |
| 292 "add", | |
| 293 "(Ljava/lang/Object;)Z"); | |
| 294 jboolean added = jni()->CallBooleanMethod(audio_tracks, add, j_track); | |
| 295 CHECK_EXCEPTION(jni()) << "error during CallBooleanMethod"; | |
| 296 RTC_CHECK(added); | |
| 297 } | |
| 298 | |
| 299 for (const auto& track : stream->GetVideoTracks()) { | |
| 300 jstring id = JavaStringFromStdString(jni(), track->id()); | |
| 301 // Java VideoTrack holds one reference. Corresponding Release() is in | |
| 302 // MediaStreamTrack_free, triggered by VideoTrack.dispose(). | |
| 303 track->AddRef(); | |
| 304 jobject j_track = | |
| 305 jni()->NewObject(*j_video_track_class_, j_video_track_ctor_, | |
| 306 reinterpret_cast<jlong>(track.get()), id); | |
| 307 CHECK_EXCEPTION(jni()) << "error during NewObject"; | |
| 308 jfieldID video_tracks_id = GetFieldID(jni(), | |
| 309 *j_media_stream_class_, | |
| 310 "videoTracks", | |
| 311 "Ljava/util/LinkedList;"); | |
| 312 jobject video_tracks = GetObjectField(jni(), j_stream, video_tracks_id); | |
| 313 jmethodID add = GetMethodID(jni(), | |
| 314 GetObjectClass(jni(), video_tracks), | |
| 315 "add", | |
| 316 "(Ljava/lang/Object;)Z"); | |
| 317 jboolean added = jni()->CallBooleanMethod(video_tracks, add, j_track); | |
| 318 CHECK_EXCEPTION(jni()) << "error during CallBooleanMethod"; | |
| 319 RTC_CHECK(added); | |
| 320 } | |
| 321 remote_streams_[stream] = NewGlobalRef(jni(), j_stream); | |
| 322 | |
| 323 jmethodID m = GetMethodID(jni(), *j_observer_class_, "onAddStream", | |
| 324 "(Lorg/webrtc/MediaStream;)V"); | |
| 325 jni()->CallVoidMethod(*j_observer_global_, m, j_stream); | |
| 326 CHECK_EXCEPTION(jni()) << "error during CallVoidMethod"; | |
| 327 } | |
| 328 | |
| 329 void OnRemoveStream( | |
| 330 rtc::scoped_refptr<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 // Release the refptr reference so that DisposeRemoteStream can assert | |
| 341 // it removes the final reference. | |
| 342 stream = nullptr; | |
| 343 DisposeRemoteStream(it); | |
| 344 } | |
| 345 | |
| 346 void OnDataChannel( | |
| 347 rtc::scoped_refptr<DataChannelInterface> channel) override { | |
| 348 ScopedLocalRefFrame local_ref_frame(jni()); | |
| 349 jobject j_channel = jni()->NewObject( | |
| 350 *j_data_channel_class_, j_data_channel_ctor_, (jlong)channel.get()); | |
| 351 CHECK_EXCEPTION(jni()) << "error during NewObject"; | |
| 352 | |
| 353 jmethodID m = GetMethodID(jni(), *j_observer_class_, "onDataChannel", | |
| 354 "(Lorg/webrtc/DataChannel;)V"); | |
| 355 jni()->CallVoidMethod(*j_observer_global_, m, j_channel); | |
| 356 | |
| 357 // Channel is now owned by Java object, and will be freed from | |
| 358 // DataChannel.dispose(). Important that this be done _after_ the | |
| 359 // CallVoidMethod above as Java code might call back into native code and be | |
| 360 // surprised to see a refcount of 2. | |
| 361 int bumped_count = channel->AddRef(); | |
| 362 RTC_CHECK(bumped_count == 2) << "Unexpected refcount OnDataChannel"; | |
| 363 | |
| 364 CHECK_EXCEPTION(jni()) << "error during CallVoidMethod"; | |
| 365 } | |
| 366 | |
| 367 void OnRenegotiationNeeded() override { | |
| 368 ScopedLocalRefFrame local_ref_frame(jni()); | |
| 369 jmethodID m = | |
| 370 GetMethodID(jni(), *j_observer_class_, "onRenegotiationNeeded", "()V"); | |
| 371 jni()->CallVoidMethod(*j_observer_global_, m); | |
| 372 CHECK_EXCEPTION(jni()) << "error during CallVoidMethod"; | |
| 373 } | |
| 374 | |
| 375 void SetConstraints(ConstraintsWrapper* constraints) { | |
| 376 RTC_CHECK(!constraints_.get()) << "constraints already set!"; | |
| 377 constraints_.reset(constraints); | |
| 378 } | |
| 379 | |
| 380 const ConstraintsWrapper* constraints() { return constraints_.get(); } | |
| 381 | |
| 382 private: | |
| 383 typedef std::map<MediaStreamInterface*, jobject> NativeToJavaStreamsMap; | |
| 384 | |
| 385 void DisposeRemoteStream(const NativeToJavaStreamsMap::iterator& it) { | |
| 386 jobject j_stream = it->second; | |
| 387 remote_streams_.erase(it); | |
| 388 jni()->CallVoidMethod( | |
| 389 j_stream, GetMethodID(jni(), *j_media_stream_class_, "dispose", "()V")); | |
| 390 CHECK_EXCEPTION(jni()) << "error during MediaStream.dispose()"; | |
| 391 DeleteGlobalRef(jni(), j_stream); | |
| 392 } | |
| 393 | |
| 394 jobject ToJavaCandidate(JNIEnv* jni, | |
| 395 jclass* candidate_class, | |
| 396 const cricket::Candidate& candidate) { | |
| 397 std::string sdp = webrtc::SdpSerializeCandidate(candidate); | |
| 398 RTC_CHECK(!sdp.empty()) << "got an empty ICE candidate"; | |
| 399 jmethodID ctor = GetMethodID(jni, *candidate_class, "<init>", | |
| 400 "(Ljava/lang/String;ILjava/lang/String;)V"); | |
| 401 jstring j_mid = JavaStringFromStdString(jni, candidate.transport_name()); | |
| 402 jstring j_sdp = JavaStringFromStdString(jni, sdp); | |
| 403 // sdp_mline_index is not used, pass an invalid value -1. | |
| 404 jobject j_candidate = | |
| 405 jni->NewObject(*candidate_class, ctor, j_mid, -1, j_sdp); | |
| 406 CHECK_EXCEPTION(jni) << "error during Java Candidate NewObject"; | |
| 407 return j_candidate; | |
| 408 } | |
| 409 | |
| 410 jobjectArray ToJavaCandidateArray( | |
| 411 JNIEnv* jni, | |
| 412 const std::vector<cricket::Candidate>& candidates) { | |
| 413 jclass candidate_class = FindClass(jni, "org/webrtc/IceCandidate"); | |
| 414 jobjectArray java_candidates = | |
| 415 jni->NewObjectArray(candidates.size(), candidate_class, NULL); | |
| 416 int i = 0; | |
| 417 for (const cricket::Candidate& candidate : candidates) { | |
| 418 jobject j_candidate = ToJavaCandidate(jni, &candidate_class, candidate); | |
| 419 jni->SetObjectArrayElement(java_candidates, i++, j_candidate); | |
| 420 } | |
| 421 return java_candidates; | |
| 422 } | |
| 423 | |
| 424 JNIEnv* jni() { | |
| 425 return AttachCurrentThreadIfNeeded(); | |
| 426 } | |
| 427 | |
| 428 const ScopedGlobalRef<jobject> j_observer_global_; | |
| 429 const ScopedGlobalRef<jclass> j_observer_class_; | |
| 430 const ScopedGlobalRef<jclass> j_media_stream_class_; | |
| 431 const jmethodID j_media_stream_ctor_; | |
| 432 const ScopedGlobalRef<jclass> j_audio_track_class_; | |
| 433 const jmethodID j_audio_track_ctor_; | |
| 434 const ScopedGlobalRef<jclass> j_video_track_class_; | |
| 435 const jmethodID j_video_track_ctor_; | |
| 436 const ScopedGlobalRef<jclass> j_data_channel_class_; | |
| 437 const jmethodID j_data_channel_ctor_; | |
| 438 // C++ -> Java remote streams. The stored jobects are global refs and must be | |
| 439 // manually deleted upon removal. Use DisposeRemoteStream(). | |
| 440 NativeToJavaStreamsMap remote_streams_; | |
| 441 std::unique_ptr<ConstraintsWrapper> constraints_; | |
| 442 }; | |
| 443 | |
| 444 // Wrapper for a Java MediaConstraints object. Copies all needed data so when | |
| 445 // the constructor returns the Java object is no longer needed. | |
| 446 class ConstraintsWrapper : public MediaConstraintsInterface { | |
| 447 public: | |
| 448 ConstraintsWrapper(JNIEnv* jni, jobject j_constraints) { | |
| 449 PopulateConstraintsFromJavaPairList( | |
| 450 jni, j_constraints, "mandatory", &mandatory_); | |
| 451 PopulateConstraintsFromJavaPairList( | |
| 452 jni, j_constraints, "optional", &optional_); | |
| 453 } | |
| 454 | |
| 455 virtual ~ConstraintsWrapper() {} | |
| 456 | |
| 457 // MediaConstraintsInterface. | |
| 458 const Constraints& GetMandatory() const override { return mandatory_; } | |
| 459 | |
| 460 const Constraints& GetOptional() const override { return optional_; } | |
| 461 | |
| 462 private: | |
| 463 // Helper for translating a List<Pair<String, String>> to a Constraints. | |
| 464 static void PopulateConstraintsFromJavaPairList( | |
| 465 JNIEnv* jni, jobject j_constraints, | |
| 466 const char* field_name, Constraints* field) { | |
| 467 jfieldID j_id = GetFieldID(jni, | |
| 468 GetObjectClass(jni, j_constraints), field_name, "Ljava/util/List;"); | |
| 469 jobject j_list = GetObjectField(jni, j_constraints, j_id); | |
| 470 for (jobject entry : Iterable(jni, j_list)) { | |
| 471 jmethodID get_key = GetMethodID(jni, | |
| 472 GetObjectClass(jni, entry), "getKey", "()Ljava/lang/String;"); | |
| 473 jstring j_key = reinterpret_cast<jstring>( | |
| 474 jni->CallObjectMethod(entry, get_key)); | |
| 475 CHECK_EXCEPTION(jni) << "error during CallObjectMethod"; | |
| 476 jmethodID get_value = GetMethodID(jni, | |
| 477 GetObjectClass(jni, entry), "getValue", "()Ljava/lang/String;"); | |
| 478 jstring j_value = reinterpret_cast<jstring>( | |
| 479 jni->CallObjectMethod(entry, get_value)); | |
| 480 CHECK_EXCEPTION(jni) << "error during CallObjectMethod"; | |
| 481 field->push_back(Constraint(JavaToStdString(jni, j_key), | |
| 482 JavaToStdString(jni, j_value))); | |
| 483 } | |
| 484 } | |
| 485 | |
| 486 Constraints mandatory_; | |
| 487 Constraints optional_; | |
| 488 }; | |
| 489 | |
| 490 static jobject JavaSdpFromNativeSdp( | |
| 491 JNIEnv* jni, const SessionDescriptionInterface* desc) { | |
| 492 std::string sdp; | |
| 493 RTC_CHECK(desc->ToString(&sdp)) << "got so far: " << sdp; | |
| 494 jstring j_description = JavaStringFromStdString(jni, sdp); | |
| 495 | |
| 496 jclass j_type_class = FindClass( | |
| 497 jni, "org/webrtc/SessionDescription$Type"); | |
| 498 jmethodID j_type_from_canonical = GetStaticMethodID( | |
| 499 jni, j_type_class, "fromCanonicalForm", | |
| 500 "(Ljava/lang/String;)Lorg/webrtc/SessionDescription$Type;"); | |
| 501 jstring j_type_string = JavaStringFromStdString(jni, desc->type()); | |
| 502 jobject j_type = jni->CallStaticObjectMethod( | |
| 503 j_type_class, j_type_from_canonical, j_type_string); | |
| 504 CHECK_EXCEPTION(jni) << "error during CallObjectMethod"; | |
| 505 | |
| 506 jclass j_sdp_class = FindClass(jni, "org/webrtc/SessionDescription"); | |
| 507 jmethodID j_sdp_ctor = GetMethodID( | |
| 508 jni, j_sdp_class, "<init>", | |
| 509 "(Lorg/webrtc/SessionDescription$Type;Ljava/lang/String;)V"); | |
| 510 jobject j_sdp = jni->NewObject( | |
| 511 j_sdp_class, j_sdp_ctor, j_type, j_description); | |
| 512 CHECK_EXCEPTION(jni) << "error during NewObject"; | |
| 513 return j_sdp; | |
| 514 } | |
| 515 | |
| 516 template <class T> // T is one of {Create,Set}SessionDescriptionObserver. | |
| 517 class SdpObserverWrapper : public T { | |
| 518 public: | |
| 519 SdpObserverWrapper(JNIEnv* jni, jobject j_observer, | |
| 520 ConstraintsWrapper* constraints) | |
| 521 : constraints_(constraints), | |
| 522 j_observer_global_(jni, j_observer), | |
| 523 j_observer_class_(jni, GetObjectClass(jni, j_observer)) { | |
| 524 } | |
| 525 | |
| 526 virtual ~SdpObserverWrapper() {} | |
| 527 | |
| 528 // Can't mark override because of templating. | |
| 529 virtual void OnSuccess() { | |
| 530 ScopedLocalRefFrame local_ref_frame(jni()); | |
| 531 jmethodID m = GetMethodID(jni(), *j_observer_class_, "onSetSuccess", "()V"); | |
| 532 jni()->CallVoidMethod(*j_observer_global_, m); | |
| 533 CHECK_EXCEPTION(jni()) << "error during CallVoidMethod"; | |
| 534 } | |
| 535 | |
| 536 // Can't mark override because of templating. | |
| 537 virtual void OnSuccess(SessionDescriptionInterface* desc) { | |
| 538 ScopedLocalRefFrame local_ref_frame(jni()); | |
| 539 jmethodID m = GetMethodID( | |
| 540 jni(), *j_observer_class_, "onCreateSuccess", | |
| 541 "(Lorg/webrtc/SessionDescription;)V"); | |
| 542 jobject j_sdp = JavaSdpFromNativeSdp(jni(), desc); | |
| 543 jni()->CallVoidMethod(*j_observer_global_, m, j_sdp); | |
| 544 CHECK_EXCEPTION(jni()) << "error during CallVoidMethod"; | |
| 545 } | |
| 546 | |
| 547 protected: | |
| 548 // Common implementation for failure of Set & Create types, distinguished by | |
| 549 // |op| being "Set" or "Create". | |
| 550 void DoOnFailure(const std::string& op, const std::string& error) { | |
| 551 jmethodID m = GetMethodID(jni(), *j_observer_class_, "on" + op + "Failure", | |
| 552 "(Ljava/lang/String;)V"); | |
| 553 jstring j_error_string = JavaStringFromStdString(jni(), error); | |
| 554 jni()->CallVoidMethod(*j_observer_global_, m, j_error_string); | |
| 555 CHECK_EXCEPTION(jni()) << "error during CallVoidMethod"; | |
| 556 } | |
| 557 | |
| 558 JNIEnv* jni() { | |
| 559 return AttachCurrentThreadIfNeeded(); | |
| 560 } | |
| 561 | |
| 562 private: | |
| 563 std::unique_ptr<ConstraintsWrapper> constraints_; | |
| 564 const ScopedGlobalRef<jobject> j_observer_global_; | |
| 565 const ScopedGlobalRef<jclass> j_observer_class_; | |
| 566 }; | |
| 567 | |
| 568 class CreateSdpObserverWrapper | |
| 569 : public SdpObserverWrapper<CreateSessionDescriptionObserver> { | |
| 570 public: | |
| 571 CreateSdpObserverWrapper(JNIEnv* jni, jobject j_observer, | |
| 572 ConstraintsWrapper* constraints) | |
| 573 : SdpObserverWrapper(jni, j_observer, constraints) {} | |
| 574 | |
| 575 void OnFailure(const std::string& error) override { | |
| 576 ScopedLocalRefFrame local_ref_frame(jni()); | |
| 577 SdpObserverWrapper::DoOnFailure(std::string("Create"), error); | |
| 578 } | |
| 579 }; | |
| 580 | |
| 581 class SetSdpObserverWrapper | |
| 582 : public SdpObserverWrapper<SetSessionDescriptionObserver> { | |
| 583 public: | |
| 584 SetSdpObserverWrapper(JNIEnv* jni, jobject j_observer, | |
| 585 ConstraintsWrapper* constraints) | |
| 586 : SdpObserverWrapper(jni, j_observer, constraints) {} | |
| 587 | |
| 588 void OnFailure(const std::string& error) override { | |
| 589 ScopedLocalRefFrame local_ref_frame(jni()); | |
| 590 SdpObserverWrapper::DoOnFailure(std::string("Set"), error); | |
| 591 } | |
| 592 }; | |
| 593 | |
| 594 // Adapter for a Java DataChannel$Observer presenting a C++ DataChannelObserver | |
| 595 // and dispatching the callback from C++ back to Java. | |
| 596 class DataChannelObserverWrapper : public DataChannelObserver { | |
| 597 public: | |
| 598 DataChannelObserverWrapper(JNIEnv* jni, jobject j_observer) | |
| 599 : j_observer_global_(jni, j_observer), | |
| 600 j_observer_class_(jni, GetObjectClass(jni, j_observer)), | |
| 601 j_buffer_class_(jni, FindClass(jni, "org/webrtc/DataChannel$Buffer")), | |
| 602 j_on_buffered_amount_change_mid_(GetMethodID( | |
| 603 jni, *j_observer_class_, "onBufferedAmountChange", "(J)V")), | |
| 604 j_on_state_change_mid_( | |
| 605 GetMethodID(jni, *j_observer_class_, "onStateChange", "()V")), | |
| 606 j_on_message_mid_(GetMethodID(jni, *j_observer_class_, "onMessage", | |
| 607 "(Lorg/webrtc/DataChannel$Buffer;)V")), | |
| 608 j_buffer_ctor_(GetMethodID(jni, *j_buffer_class_, "<init>", | |
| 609 "(Ljava/nio/ByteBuffer;Z)V")) {} | |
| 610 | |
| 611 virtual ~DataChannelObserverWrapper() {} | |
| 612 | |
| 613 void OnBufferedAmountChange(uint64_t previous_amount) override { | |
| 614 ScopedLocalRefFrame local_ref_frame(jni()); | |
| 615 jni()->CallVoidMethod(*j_observer_global_, j_on_buffered_amount_change_mid_, | |
| 616 previous_amount); | |
| 617 CHECK_EXCEPTION(jni()) << "error during CallVoidMethod"; | |
| 618 } | |
| 619 | |
| 620 void OnStateChange() override { | |
| 621 ScopedLocalRefFrame local_ref_frame(jni()); | |
| 622 jni()->CallVoidMethod(*j_observer_global_, j_on_state_change_mid_); | |
| 623 CHECK_EXCEPTION(jni()) << "error during CallVoidMethod"; | |
| 624 } | |
| 625 | |
| 626 void OnMessage(const DataBuffer& buffer) override { | |
| 627 ScopedLocalRefFrame local_ref_frame(jni()); | |
| 628 jobject byte_buffer = jni()->NewDirectByteBuffer( | |
| 629 const_cast<char*>(buffer.data.data<char>()), buffer.data.size()); | |
| 630 jobject j_buffer = jni()->NewObject(*j_buffer_class_, j_buffer_ctor_, | |
| 631 byte_buffer, buffer.binary); | |
| 632 jni()->CallVoidMethod(*j_observer_global_, j_on_message_mid_, j_buffer); | |
| 633 CHECK_EXCEPTION(jni()) << "error during CallVoidMethod"; | |
| 634 } | |
| 635 | |
| 636 private: | |
| 637 JNIEnv* jni() { | |
| 638 return AttachCurrentThreadIfNeeded(); | |
| 639 } | |
| 640 | |
| 641 const ScopedGlobalRef<jobject> j_observer_global_; | |
| 642 const ScopedGlobalRef<jclass> j_observer_class_; | |
| 643 const ScopedGlobalRef<jclass> j_buffer_class_; | |
| 644 const jmethodID j_on_buffered_amount_change_mid_; | |
| 645 const jmethodID j_on_state_change_mid_; | |
| 646 const jmethodID j_on_message_mid_; | |
| 647 const jmethodID j_buffer_ctor_; | |
| 648 }; | |
| 649 | |
| 650 // Adapter for a Java StatsObserver presenting a C++ StatsObserver and | |
| 651 // dispatching the callback from C++ back to Java. | |
| 652 class StatsObserverWrapper : public StatsObserver { | |
| 653 public: | |
| 654 StatsObserverWrapper(JNIEnv* jni, jobject j_observer) | |
| 655 : j_observer_global_(jni, j_observer), | |
| 656 j_observer_class_(jni, GetObjectClass(jni, j_observer)), | |
| 657 j_stats_report_class_(jni, FindClass(jni, "org/webrtc/StatsReport")), | |
| 658 j_stats_report_ctor_(GetMethodID( | |
| 659 jni, *j_stats_report_class_, "<init>", | |
| 660 "(Ljava/lang/String;Ljava/lang/String;D" | |
| 661 "[Lorg/webrtc/StatsReport$Value;)V")), | |
| 662 j_value_class_(jni, FindClass( | |
| 663 jni, "org/webrtc/StatsReport$Value")), | |
| 664 j_value_ctor_(GetMethodID( | |
| 665 jni, *j_value_class_, "<init>", | |
| 666 "(Ljava/lang/String;Ljava/lang/String;)V")) { | |
| 667 } | |
| 668 | |
| 669 virtual ~StatsObserverWrapper() {} | |
| 670 | |
| 671 void OnComplete(const StatsReports& reports) override { | |
| 672 ScopedLocalRefFrame local_ref_frame(jni()); | |
| 673 jobjectArray j_reports = ReportsToJava(jni(), reports); | |
| 674 jmethodID m = GetMethodID(jni(), *j_observer_class_, "onComplete", | |
| 675 "([Lorg/webrtc/StatsReport;)V"); | |
| 676 jni()->CallVoidMethod(*j_observer_global_, m, j_reports); | |
| 677 CHECK_EXCEPTION(jni()) << "error during CallVoidMethod"; | |
| 678 } | |
| 679 | |
| 680 private: | |
| 681 jobjectArray ReportsToJava( | |
| 682 JNIEnv* jni, const StatsReports& reports) { | |
| 683 jobjectArray reports_array = jni->NewObjectArray( | |
| 684 reports.size(), *j_stats_report_class_, NULL); | |
| 685 int i = 0; | |
| 686 for (const auto* report : reports) { | |
| 687 ScopedLocalRefFrame local_ref_frame(jni); | |
| 688 jstring j_id = JavaStringFromStdString(jni, report->id()->ToString()); | |
| 689 jstring j_type = JavaStringFromStdString(jni, report->TypeToString()); | |
| 690 jobjectArray j_values = ValuesToJava(jni, report->values()); | |
| 691 jobject j_report = jni->NewObject(*j_stats_report_class_, | |
| 692 j_stats_report_ctor_, | |
| 693 j_id, | |
| 694 j_type, | |
| 695 report->timestamp(), | |
| 696 j_values); | |
| 697 jni->SetObjectArrayElement(reports_array, i++, j_report); | |
| 698 } | |
| 699 return reports_array; | |
| 700 } | |
| 701 | |
| 702 jobjectArray ValuesToJava(JNIEnv* jni, const StatsReport::Values& values) { | |
| 703 jobjectArray j_values = jni->NewObjectArray( | |
| 704 values.size(), *j_value_class_, NULL); | |
| 705 int i = 0; | |
| 706 for (const auto& it : values) { | |
| 707 ScopedLocalRefFrame local_ref_frame(jni); | |
| 708 // Should we use the '.name' enum value here instead of converting the | |
| 709 // name to a string? | |
| 710 jstring j_name = JavaStringFromStdString(jni, it.second->display_name()); | |
| 711 jstring j_value = JavaStringFromStdString(jni, it.second->ToString()); | |
| 712 jobject j_element_value = | |
| 713 jni->NewObject(*j_value_class_, j_value_ctor_, j_name, j_value); | |
| 714 jni->SetObjectArrayElement(j_values, i++, j_element_value); | |
| 715 } | |
| 716 return j_values; | |
| 717 } | |
| 718 | |
| 719 JNIEnv* jni() { | |
| 720 return AttachCurrentThreadIfNeeded(); | |
| 721 } | |
| 722 | |
| 723 const ScopedGlobalRef<jobject> j_observer_global_; | |
| 724 const ScopedGlobalRef<jclass> j_observer_class_; | |
| 725 const ScopedGlobalRef<jclass> j_stats_report_class_; | |
| 726 const jmethodID j_stats_report_ctor_; | |
| 727 const ScopedGlobalRef<jclass> j_value_class_; | |
| 728 const jmethodID j_value_ctor_; | |
| 729 }; | |
| 730 | |
| 731 // Wrapper dispatching rtc::VideoSinkInterface to a Java VideoRenderer | |
| 732 // instance. | |
| 733 class JavaVideoRendererWrapper | |
| 734 : public rtc::VideoSinkInterface<cricket::VideoFrame> { | |
| 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 OnFrame(const cricket::VideoFrame& video_frame) override { | |
| 755 ScopedLocalRefFrame local_ref_frame(jni()); | |
| 756 jobject j_frame = | |
| 757 (video_frame.video_frame_buffer()->native_handle() != nullptr) | |
| 758 ? CricketToJavaTextureFrame(&video_frame) | |
| 759 : CricketToJavaI420Frame(&video_frame); | |
| 760 // |j_callbacks_| is responsible for releasing |j_frame| with | |
| 761 // VideoRenderer.renderFrameDone(). | |
| 762 jni()->CallVoidMethod(*j_callbacks_, j_render_frame_id_, j_frame); | |
| 763 CHECK_EXCEPTION(jni()); | |
| 764 } | |
| 765 | |
| 766 private: | |
| 767 // Make a shallow copy of |frame| to be used with Java. The callee has | |
| 768 // ownership of the frame, and the frame should be released with | |
| 769 // VideoRenderer.releaseNativeFrame(). | |
| 770 static jlong javaShallowCopy(const cricket::VideoFrame* frame) { | |
| 771 return jlongFromPointer(frame->Copy()); | |
| 772 } | |
| 773 | |
| 774 // Return a VideoRenderer.I420Frame referring to the data in |frame|. | |
| 775 jobject CricketToJavaI420Frame(const cricket::VideoFrame* frame) { | |
| 776 jintArray strides = jni()->NewIntArray(3); | |
| 777 jint* strides_array = jni()->GetIntArrayElements(strides, NULL); | |
| 778 strides_array[0] = frame->video_frame_buffer()->StrideY(); | |
| 779 strides_array[1] = frame->video_frame_buffer()->StrideU(); | |
| 780 strides_array[2] = frame->video_frame_buffer()->StrideV(); | |
| 781 jni()->ReleaseIntArrayElements(strides, strides_array, 0); | |
| 782 jobjectArray planes = jni()->NewObjectArray(3, *j_byte_buffer_class_, NULL); | |
| 783 jobject y_buffer = jni()->NewDirectByteBuffer( | |
| 784 const_cast<uint8_t*>(frame->video_frame_buffer()->DataY()), | |
| 785 frame->video_frame_buffer()->StrideY() * | |
| 786 frame->video_frame_buffer()->height()); | |
| 787 size_t chroma_height = (frame->height() + 1) / 2; | |
| 788 jobject u_buffer = jni()->NewDirectByteBuffer( | |
| 789 const_cast<uint8_t*>(frame->video_frame_buffer()->DataU()), | |
| 790 frame->video_frame_buffer()->StrideU() * chroma_height); | |
| 791 jobject v_buffer = jni()->NewDirectByteBuffer( | |
| 792 const_cast<uint8_t*>(frame->video_frame_buffer()->DataV()), | |
| 793 frame->video_frame_buffer()->StrideV() * chroma_height); | |
| 794 | |
| 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->width(), frame->height(), | |
| 801 static_cast<int>(frame->rotation()), | |
| 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 = reinterpret_cast<NativeHandleImpl*>( | |
| 808 frame->video_frame_buffer()->native_handle()); | |
| 809 jfloatArray sampling_matrix = handle->sampling_matrix.ToJava(jni()); | |
| 810 | |
| 811 return jni()->NewObject( | |
| 812 *j_frame_class_, j_texture_frame_ctor_id_, | |
| 813 frame->width(), frame->height(), | |
| 814 static_cast<int>(frame->rotation()), | |
| 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 std::unique_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::CopyOnWriteBuffer(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 (path != "logcat:") { | |
| 893 RTC_CHECK_EQ(0, webrtc::Trace::SetTraceFile(path.c_str(), false)) | |
| 894 << "SetTraceFile failed"; | |
| 895 } else { | |
| 896 // Intentionally leak this to avoid needing to reason about its lifecycle. | |
| 897 // It keeps no state and functions only as a dispatch point. | |
| 898 static LogcatTraceContext* g_trace_callback = new LogcatTraceContext(); | |
| 899 } | |
| 900 } | |
| 901 if (nativeSeverity >= rtc::LS_SENSITIVE && nativeSeverity <= rtc::LS_ERROR) { | |
| 902 rtc::LogMessage::LogToDebug( | |
| 903 static_cast<rtc::LoggingSeverity>(nativeSeverity)); | |
| 904 } | |
| 905 } | |
| 906 | |
| 907 JOW(void, Logging_nativeEnableLogThreads)(JNIEnv* jni, jclass) { | |
| 908 rtc::LogMessage::LogThreads(true); | |
| 909 } | |
| 910 | |
| 911 JOW(void, Logging_nativeEnableLogTimeStamps)(JNIEnv* jni, jclass) { | |
| 912 rtc::LogMessage::LogTimestamps(true); | |
| 913 } | |
| 914 | |
| 915 JOW(void, Logging_nativeLog)( | |
| 916 JNIEnv* jni, jclass, jint j_severity, jstring j_tag, jstring j_message) { | |
| 917 std::string message = JavaToStdString(jni, j_message); | |
| 918 std::string tag = JavaToStdString(jni, j_tag); | |
| 919 LOG_TAG(static_cast<rtc::LoggingSeverity>(j_severity), tag) << message; | |
| 920 } | |
| 921 | |
| 922 JOW(void, PeerConnection_freePeerConnection)(JNIEnv*, jclass, jlong j_p) { | |
| 923 CHECK_RELEASE(reinterpret_cast<PeerConnectionInterface*>(j_p)); | |
| 924 } | |
| 925 | |
| 926 JOW(void, PeerConnection_freeObserver)(JNIEnv*, jclass, jlong j_p) { | |
| 927 PCOJava* p = reinterpret_cast<PCOJava*>(j_p); | |
| 928 delete p; | |
| 929 } | |
| 930 | |
| 931 JOW(void, MediaSource_free)(JNIEnv*, jclass, jlong j_p) { | |
| 932 CHECK_RELEASE(reinterpret_cast<MediaSourceInterface*>(j_p)); | |
| 933 } | |
| 934 | |
| 935 JOW(void, VideoRenderer_freeWrappedVideoRenderer)(JNIEnv*, jclass, jlong j_p) { | |
| 936 delete reinterpret_cast<JavaVideoRendererWrapper*>(j_p); | |
| 937 } | |
| 938 | |
| 939 JOW(void, VideoRenderer_releaseNativeFrame)( | |
| 940 JNIEnv* jni, jclass, jlong j_frame_ptr) { | |
| 941 delete reinterpret_cast<const cricket::VideoFrame*>(j_frame_ptr); | |
| 942 } | |
| 943 | |
| 944 JOW(void, MediaStreamTrack_free)(JNIEnv*, jclass, jlong j_p) { | |
| 945 reinterpret_cast<MediaStreamTrackInterface*>(j_p)->Release(); | |
| 946 } | |
| 947 | |
| 948 JOW(jboolean, MediaStream_nativeAddAudioTrack)( | |
| 949 JNIEnv* jni, jclass, jlong pointer, jlong j_audio_track_pointer) { | |
| 950 return reinterpret_cast<MediaStreamInterface*>(pointer)->AddTrack( | |
| 951 reinterpret_cast<AudioTrackInterface*>(j_audio_track_pointer)); | |
| 952 } | |
| 953 | |
| 954 JOW(jboolean, MediaStream_nativeAddVideoTrack)( | |
| 955 JNIEnv* jni, jclass, jlong pointer, jlong j_video_track_pointer) { | |
| 956 return reinterpret_cast<MediaStreamInterface*>(pointer) | |
| 957 ->AddTrack(reinterpret_cast<VideoTrackInterface*>(j_video_track_pointer)); | |
| 958 } | |
| 959 | |
| 960 JOW(jboolean, MediaStream_nativeRemoveAudioTrack)( | |
| 961 JNIEnv* jni, jclass, jlong pointer, jlong j_audio_track_pointer) { | |
| 962 return reinterpret_cast<MediaStreamInterface*>(pointer)->RemoveTrack( | |
| 963 reinterpret_cast<AudioTrackInterface*>(j_audio_track_pointer)); | |
| 964 } | |
| 965 | |
| 966 JOW(jboolean, MediaStream_nativeRemoveVideoTrack)( | |
| 967 JNIEnv* jni, jclass, jlong pointer, jlong j_video_track_pointer) { | |
| 968 return reinterpret_cast<MediaStreamInterface*>(pointer)->RemoveTrack( | |
| 969 reinterpret_cast<VideoTrackInterface*>(j_video_track_pointer)); | |
| 970 } | |
| 971 | |
| 972 JOW(jstring, MediaStream_nativeLabel)(JNIEnv* jni, jclass, jlong j_p) { | |
| 973 return JavaStringFromStdString( | |
| 974 jni, reinterpret_cast<MediaStreamInterface*>(j_p)->label()); | |
| 975 } | |
| 976 | |
| 977 JOW(void, MediaStream_free)(JNIEnv*, jclass, jlong j_p) { | |
| 978 CHECK_RELEASE(reinterpret_cast<MediaStreamInterface*>(j_p)); | |
| 979 } | |
| 980 | |
| 981 JOW(jlong, PeerConnectionFactory_nativeCreateObserver)( | |
| 982 JNIEnv * jni, jclass, jobject j_observer) { | |
| 983 return (jlong)new PCOJava(jni, j_observer); | |
| 984 } | |
| 985 | |
| 986 JOW(jboolean, PeerConnectionFactory_initializeAndroidGlobals)( | |
| 987 JNIEnv* jni, jclass, jobject context, | |
| 988 jboolean initialize_audio, jboolean initialize_video, | |
| 989 jboolean video_hw_acceleration) { | |
| 990 bool failure = false; | |
| 991 video_hw_acceleration_enabled = video_hw_acceleration; | |
| 992 AndroidNetworkMonitor::SetAndroidContext(jni, context); | |
| 993 if (!factory_static_initialized) { | |
| 994 if (initialize_video) { | |
| 995 failure |= AndroidVideoCapturerJni::SetAndroidObjects(jni, context); | |
| 996 } | |
| 997 if (initialize_audio) | |
| 998 failure |= webrtc::VoiceEngine::SetAndroidObjects(GetJVM(), context); | |
| 999 factory_static_initialized = true; | |
| 1000 } | |
| 1001 return !failure; | |
| 1002 } | |
| 1003 | |
| 1004 JOW(void, PeerConnectionFactory_initializeFieldTrials)( | |
| 1005 JNIEnv* jni, jclass, jstring j_trials_init_string) { | |
| 1006 field_trials_init_string = NULL; | |
| 1007 if (j_trials_init_string != NULL) { | |
| 1008 const char* init_string = | |
| 1009 jni->GetStringUTFChars(j_trials_init_string, NULL); | |
| 1010 int init_string_length = jni->GetStringUTFLength(j_trials_init_string); | |
| 1011 field_trials_init_string = new char[init_string_length + 1]; | |
| 1012 rtc::strcpyn(field_trials_init_string, init_string_length + 1, init_string); | |
| 1013 jni->ReleaseStringUTFChars(j_trials_init_string, init_string); | |
| 1014 LOG(LS_INFO) << "initializeFieldTrials: " << field_trials_init_string; | |
| 1015 } | |
| 1016 webrtc::field_trial::InitFieldTrialsFromString(field_trials_init_string); | |
| 1017 } | |
| 1018 | |
| 1019 JOW(void, PeerConnectionFactory_initializeInternalTracer)(JNIEnv* jni, jclass) { | |
| 1020 rtc::tracing::SetupInternalTracer(); | |
| 1021 } | |
| 1022 | |
| 1023 JOW(jboolean, PeerConnectionFactory_startInternalTracingCapture)( | |
| 1024 JNIEnv* jni, jclass, jstring j_event_tracing_filename) { | |
| 1025 if (!j_event_tracing_filename) | |
| 1026 return false; | |
| 1027 | |
| 1028 const char* init_string = | |
| 1029 jni->GetStringUTFChars(j_event_tracing_filename, NULL); | |
| 1030 LOG(LS_INFO) << "Starting internal tracing to: " << init_string; | |
| 1031 bool ret = rtc::tracing::StartInternalCapture(init_string); | |
| 1032 jni->ReleaseStringUTFChars(j_event_tracing_filename, init_string); | |
| 1033 return ret; | |
| 1034 } | |
| 1035 | |
| 1036 JOW(void, PeerConnectionFactory_stopInternalTracingCapture)( | |
| 1037 JNIEnv* jni, jclass) { | |
| 1038 rtc::tracing::StopInternalCapture(); | |
| 1039 } | |
| 1040 | |
| 1041 JOW(void, PeerConnectionFactory_shutdownInternalTracer)(JNIEnv* jni, jclass) { | |
| 1042 rtc::tracing::ShutdownInternalTracer(); | |
| 1043 } | |
| 1044 | |
| 1045 // Helper struct for working around the fact that CreatePeerConnectionFactory() | |
| 1046 // comes in two flavors: either entirely automagical (constructing its own | |
| 1047 // threads and deleting them on teardown, but no external codec factory support) | |
| 1048 // or entirely manual (requires caller to delete threads after factory | |
| 1049 // teardown). This struct takes ownership of its ctor's arguments to present a | |
| 1050 // single thing for Java to hold and eventually free. | |
| 1051 class OwnedFactoryAndThreads { | |
| 1052 public: | |
| 1053 OwnedFactoryAndThreads(std::unique_ptr<Thread> network_thread, | |
| 1054 std::unique_ptr<Thread> worker_thread, | |
| 1055 std::unique_ptr<Thread> signaling_thread, | |
| 1056 WebRtcVideoEncoderFactory* encoder_factory, | |
| 1057 WebRtcVideoDecoderFactory* decoder_factory, | |
| 1058 rtc::NetworkMonitorFactory* network_monitor_factory, | |
| 1059 PeerConnectionFactoryInterface* factory) | |
| 1060 : network_thread_(std::move(network_thread)), | |
| 1061 worker_thread_(std::move(worker_thread)), | |
| 1062 signaling_thread_(std::move(signaling_thread)), | |
| 1063 encoder_factory_(encoder_factory), | |
| 1064 decoder_factory_(decoder_factory), | |
| 1065 network_monitor_factory_(network_monitor_factory), | |
| 1066 factory_(factory) {} | |
| 1067 | |
| 1068 ~OwnedFactoryAndThreads() { | |
| 1069 CHECK_RELEASE(factory_); | |
| 1070 if (network_monitor_factory_ != nullptr) { | |
| 1071 rtc::NetworkMonitorFactory::ReleaseFactory(network_monitor_factory_); | |
| 1072 } | |
| 1073 } | |
| 1074 | |
| 1075 PeerConnectionFactoryInterface* factory() { return factory_; } | |
| 1076 WebRtcVideoEncoderFactory* encoder_factory() { return encoder_factory_; } | |
| 1077 WebRtcVideoDecoderFactory* decoder_factory() { return decoder_factory_; } | |
| 1078 rtc::NetworkMonitorFactory* network_monitor_factory() { | |
| 1079 return network_monitor_factory_; | |
| 1080 } | |
| 1081 void clear_network_monitor_factory() { network_monitor_factory_ = nullptr; } | |
| 1082 void InvokeJavaCallbacksOnFactoryThreads(); | |
| 1083 | |
| 1084 private: | |
| 1085 void JavaCallbackOnFactoryThreads(); | |
| 1086 | |
| 1087 const std::unique_ptr<Thread> network_thread_; | |
| 1088 const std::unique_ptr<Thread> worker_thread_; | |
| 1089 const std::unique_ptr<Thread> signaling_thread_; | |
| 1090 WebRtcVideoEncoderFactory* encoder_factory_; | |
| 1091 WebRtcVideoDecoderFactory* decoder_factory_; | |
| 1092 rtc::NetworkMonitorFactory* network_monitor_factory_; | |
| 1093 PeerConnectionFactoryInterface* factory_; // Const after ctor except dtor. | |
| 1094 }; | |
| 1095 | |
| 1096 void OwnedFactoryAndThreads::JavaCallbackOnFactoryThreads() { | |
| 1097 JNIEnv* jni = AttachCurrentThreadIfNeeded(); | |
| 1098 ScopedLocalRefFrame local_ref_frame(jni); | |
| 1099 jclass j_factory_class = FindClass(jni, "org/webrtc/PeerConnectionFactory"); | |
| 1100 jmethodID m = nullptr; | |
| 1101 if (network_thread_->IsCurrent()) { | |
| 1102 LOG(LS_INFO) << "Network thread JavaCallback"; | |
| 1103 m = GetStaticMethodID(jni, j_factory_class, "onNetworkThreadReady", "()V"); | |
| 1104 } | |
| 1105 if (worker_thread_->IsCurrent()) { | |
| 1106 LOG(LS_INFO) << "Worker thread JavaCallback"; | |
| 1107 m = GetStaticMethodID(jni, j_factory_class, "onWorkerThreadReady", "()V"); | |
| 1108 } | |
| 1109 if (signaling_thread_->IsCurrent()) { | |
| 1110 LOG(LS_INFO) << "Signaling thread JavaCallback"; | |
| 1111 m = GetStaticMethodID( | |
| 1112 jni, j_factory_class, "onSignalingThreadReady", "()V"); | |
| 1113 } | |
| 1114 if (m != nullptr) { | |
| 1115 jni->CallStaticVoidMethod(j_factory_class, m); | |
| 1116 CHECK_EXCEPTION(jni) << "error during JavaCallback::CallStaticVoidMethod"; | |
| 1117 } | |
| 1118 } | |
| 1119 | |
| 1120 void OwnedFactoryAndThreads::InvokeJavaCallbacksOnFactoryThreads() { | |
| 1121 LOG(LS_INFO) << "InvokeJavaCallbacksOnFactoryThreads."; | |
| 1122 network_thread_->Invoke<void>(RTC_FROM_HERE, | |
| 1123 [this] { JavaCallbackOnFactoryThreads(); }); | |
| 1124 worker_thread_->Invoke<void>(RTC_FROM_HERE, | |
| 1125 [this] { JavaCallbackOnFactoryThreads(); }); | |
| 1126 signaling_thread_->Invoke<void>(RTC_FROM_HERE, | |
| 1127 [this] { JavaCallbackOnFactoryThreads(); }); | |
| 1128 } | |
| 1129 | |
| 1130 PeerConnectionFactoryInterface::Options ParseOptionsFromJava(JNIEnv* jni, | |
| 1131 jobject options) { | |
| 1132 jclass options_class = jni->GetObjectClass(options); | |
| 1133 jfieldID network_ignore_mask_field = | |
| 1134 jni->GetFieldID(options_class, "networkIgnoreMask", "I"); | |
| 1135 int network_ignore_mask = | |
| 1136 jni->GetIntField(options, network_ignore_mask_field); | |
| 1137 | |
| 1138 jfieldID disable_encryption_field = | |
| 1139 jni->GetFieldID(options_class, "disableEncryption", "Z"); | |
| 1140 bool disable_encryption = | |
| 1141 jni->GetBooleanField(options, disable_encryption_field); | |
| 1142 | |
| 1143 jfieldID disable_network_monitor_field = | |
| 1144 jni->GetFieldID(options_class, "disableNetworkMonitor", "Z"); | |
| 1145 bool disable_network_monitor = | |
| 1146 jni->GetBooleanField(options, disable_network_monitor_field); | |
| 1147 | |
| 1148 PeerConnectionFactoryInterface::Options native_options; | |
| 1149 | |
| 1150 // This doesn't necessarily match the c++ version of this struct; feel free | |
| 1151 // to add more parameters as necessary. | |
| 1152 native_options.network_ignore_mask = network_ignore_mask; | |
| 1153 native_options.disable_encryption = disable_encryption; | |
| 1154 native_options.disable_network_monitor = disable_network_monitor; | |
| 1155 return native_options; | |
| 1156 } | |
| 1157 | |
| 1158 JOW(jlong, PeerConnectionFactory_nativeCreatePeerConnectionFactory)( | |
| 1159 JNIEnv* jni, jclass, jobject joptions) { | |
| 1160 // talk/ assumes pretty widely that the current Thread is ThreadManager'd, but | |
| 1161 // ThreadManager only WrapCurrentThread()s the thread where it is first | |
| 1162 // created. Since the semantics around when auto-wrapping happens in | |
| 1163 // webrtc/base/ are convoluted, we simply wrap here to avoid having to think | |
| 1164 // about ramifications of auto-wrapping there. | |
| 1165 rtc::ThreadManager::Instance()->WrapCurrentThread(); | |
| 1166 webrtc::Trace::CreateTrace(); | |
| 1167 | |
| 1168 std::unique_ptr<Thread> network_thread = | |
| 1169 rtc::Thread::CreateWithSocketServer(); | |
| 1170 network_thread->SetName("network_thread", nullptr); | |
| 1171 RTC_CHECK(network_thread->Start()) << "Failed to start thread"; | |
| 1172 | |
| 1173 std::unique_ptr<Thread> worker_thread = rtc::Thread::Create(); | |
| 1174 worker_thread->SetName("worker_thread", nullptr); | |
| 1175 RTC_CHECK(worker_thread->Start()) << "Failed to start thread"; | |
| 1176 | |
| 1177 std::unique_ptr<Thread> signaling_thread = rtc::Thread::Create(); | |
| 1178 signaling_thread->SetName("signaling_thread", NULL); | |
| 1179 RTC_CHECK(signaling_thread->Start()) << "Failed to start thread"; | |
| 1180 | |
| 1181 WebRtcVideoEncoderFactory* encoder_factory = nullptr; | |
| 1182 WebRtcVideoDecoderFactory* decoder_factory = nullptr; | |
| 1183 rtc::NetworkMonitorFactory* network_monitor_factory = nullptr; | |
| 1184 | |
| 1185 PeerConnectionFactoryInterface::Options options; | |
| 1186 bool has_options = joptions != NULL; | |
| 1187 if (has_options) { | |
| 1188 options = ParseOptionsFromJava(jni, joptions); | |
| 1189 } | |
| 1190 | |
| 1191 if (video_hw_acceleration_enabled) { | |
| 1192 encoder_factory = new MediaCodecVideoEncoderFactory(); | |
| 1193 decoder_factory = new MediaCodecVideoDecoderFactory(); | |
| 1194 } | |
| 1195 // Do not create network_monitor_factory only if the options are | |
| 1196 // provided and disable_network_monitor therein is set to true. | |
| 1197 if (!(has_options && options.disable_network_monitor)) { | |
| 1198 network_monitor_factory = new AndroidNetworkMonitorFactory(); | |
| 1199 rtc::NetworkMonitorFactory::SetFactory(network_monitor_factory); | |
| 1200 } | |
| 1201 | |
| 1202 rtc::scoped_refptr<PeerConnectionFactoryInterface> factory( | |
| 1203 webrtc::CreatePeerConnectionFactory( | |
| 1204 network_thread.get(), worker_thread.get(), signaling_thread.get(), | |
| 1205 nullptr, encoder_factory, 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 std::move(network_thread), std::move(worker_thread), | |
| 1215 std::move(signaling_thread), 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, jobject j_egl_context, | |
| 1253 jobject j_video_capturer, jobject j_constraints) { | |
| 1254 // Create a cricket::VideoCapturer from |j_video_capturer|. | |
| 1255 rtc::scoped_refptr<webrtc::AndroidVideoCapturerDelegate> delegate = | |
| 1256 new rtc::RefCountedObject<AndroidVideoCapturerJni>( | |
| 1257 jni, j_video_capturer, j_egl_context); | |
| 1258 std::unique_ptr<cricket::VideoCapturer> capturer( | |
| 1259 new webrtc::AndroidVideoCapturer(delegate)); | |
| 1260 // Create a webrtc::VideoTrackSourceInterface from the cricket::VideoCapturer, | |
| 1261 // native factory and constraints. | |
| 1262 std::unique_ptr<ConstraintsWrapper> constraints( | |
| 1263 new ConstraintsWrapper(jni, j_constraints)); | |
| 1264 rtc::scoped_refptr<PeerConnectionFactoryInterface> factory( | |
| 1265 factoryFromJava(native_factory)); | |
| 1266 rtc::scoped_refptr<VideoTrackSourceInterface> source( | |
| 1267 factory->CreateVideoSource(capturer.release(), constraints.get())); | |
| 1268 return (jlong)source.release(); | |
| 1269 } | |
| 1270 | |
| 1271 JOW(jlong, PeerConnectionFactory_nativeCreateVideoTrack)( | |
| 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<VideoTrackInterface> track(factory->CreateVideoTrack( | |
| 1277 JavaToStdString(jni, id), | |
| 1278 reinterpret_cast<VideoTrackSourceInterface*>(native_source))); | |
| 1279 return (jlong)track.release(); | |
| 1280 } | |
| 1281 | |
| 1282 JOW(jlong, PeerConnectionFactory_nativeCreateAudioSource)( | |
| 1283 JNIEnv* jni, jclass, jlong native_factory, jobject j_constraints) { | |
| 1284 std::unique_ptr<ConstraintsWrapper> constraints( | |
| 1285 new ConstraintsWrapper(jni, j_constraints)); | |
| 1286 rtc::scoped_refptr<PeerConnectionFactoryInterface> factory( | |
| 1287 factoryFromJava(native_factory)); | |
| 1288 rtc::scoped_refptr<AudioSourceInterface> source( | |
| 1289 factory->CreateAudioSource(constraints.get())); | |
| 1290 return (jlong)source.release(); | |
| 1291 } | |
| 1292 | |
| 1293 JOW(jlong, PeerConnectionFactory_nativeCreateAudioTrack)( | |
| 1294 JNIEnv* jni, jclass, jlong native_factory, jstring id, | |
| 1295 jlong native_source) { | |
| 1296 rtc::scoped_refptr<PeerConnectionFactoryInterface> factory( | |
| 1297 factoryFromJava(native_factory)); | |
| 1298 rtc::scoped_refptr<AudioTrackInterface> track(factory->CreateAudioTrack( | |
| 1299 JavaToStdString(jni, id), | |
| 1300 reinterpret_cast<AudioSourceInterface*>(native_source))); | |
| 1301 return (jlong)track.release(); | |
| 1302 } | |
| 1303 | |
| 1304 JOW(jboolean, PeerConnectionFactory_nativeStartAecDump)( | |
| 1305 JNIEnv* jni, jclass, jlong native_factory, jint file, | |
| 1306 jint filesize_limit_bytes) { | |
| 1307 rtc::scoped_refptr<PeerConnectionFactoryInterface> factory( | |
| 1308 factoryFromJava(native_factory)); | |
| 1309 return factory->StartAecDump(file, filesize_limit_bytes); | |
| 1310 } | |
| 1311 | |
| 1312 JOW(void, PeerConnectionFactory_nativeStopAecDump)( | |
| 1313 JNIEnv* jni, jclass, jlong native_factory) { | |
| 1314 rtc::scoped_refptr<PeerConnectionFactoryInterface> factory( | |
| 1315 factoryFromJava(native_factory)); | |
| 1316 factory->StopAecDump(); | |
| 1317 } | |
| 1318 | |
| 1319 JOW(jboolean, PeerConnectionFactory_nativeStartRtcEventLog) | |
| 1320 (JNIEnv* jni, | |
| 1321 jclass, | |
| 1322 jlong native_factory, | |
| 1323 jint file, | |
| 1324 jint filesize_limit_bytes) { | |
| 1325 rtc::scoped_refptr<PeerConnectionFactoryInterface> factory( | |
| 1326 factoryFromJava(native_factory)); | |
| 1327 return factory->StartRtcEventLog(file, filesize_limit_bytes); | |
| 1328 } | |
| 1329 | |
| 1330 JOW(void, PeerConnectionFactory_nativeStopRtcEventLog)( | |
| 1331 JNIEnv* jni, jclass, jlong native_factory) { | |
| 1332 rtc::scoped_refptr<PeerConnectionFactoryInterface> factory( | |
| 1333 factoryFromJava(native_factory)); | |
| 1334 factory->StopRtcEventLog(); | |
| 1335 } | |
| 1336 | |
| 1337 JOW(void, PeerConnectionFactory_nativeSetOptions)( | |
| 1338 JNIEnv* jni, jclass, jlong native_factory, jobject options) { | |
| 1339 rtc::scoped_refptr<PeerConnectionFactoryInterface> factory( | |
| 1340 factoryFromJava(native_factory)); | |
| 1341 PeerConnectionFactoryInterface::Options options_to_set = | |
| 1342 ParseOptionsFromJava(jni, options); | |
| 1343 factory->SetOptions(options_to_set); | |
| 1344 | |
| 1345 if (options_to_set.disable_network_monitor) { | |
| 1346 OwnedFactoryAndThreads* owner = | |
| 1347 reinterpret_cast<OwnedFactoryAndThreads*>(native_factory); | |
| 1348 if (owner->network_monitor_factory()) { | |
| 1349 rtc::NetworkMonitorFactory::ReleaseFactory( | |
| 1350 owner->network_monitor_factory()); | |
| 1351 owner->clear_network_monitor_factory(); | |
| 1352 } | |
| 1353 } | |
| 1354 } | |
| 1355 | |
| 1356 JOW(void, PeerConnectionFactory_nativeSetVideoHwAccelerationOptions)( | |
| 1357 JNIEnv* jni, jclass, jlong native_factory, jobject local_egl_context, | |
| 1358 jobject remote_egl_context) { | |
| 1359 OwnedFactoryAndThreads* owned_factory = | |
| 1360 reinterpret_cast<OwnedFactoryAndThreads*>(native_factory); | |
| 1361 | |
| 1362 jclass j_eglbase14_context_class = | |
| 1363 FindClass(jni, "org/webrtc/EglBase14$Context"); | |
| 1364 | |
| 1365 MediaCodecVideoEncoderFactory* encoder_factory = | |
| 1366 static_cast<MediaCodecVideoEncoderFactory*> | |
| 1367 (owned_factory->encoder_factory()); | |
| 1368 if (encoder_factory && | |
| 1369 jni->IsInstanceOf(local_egl_context, j_eglbase14_context_class)) { | |
| 1370 LOG(LS_INFO) << "Set EGL context for HW encoding."; | |
| 1371 encoder_factory->SetEGLContext(jni, local_egl_context); | |
| 1372 } | |
| 1373 | |
| 1374 MediaCodecVideoDecoderFactory* decoder_factory = | |
| 1375 static_cast<MediaCodecVideoDecoderFactory*> | |
| 1376 (owned_factory->decoder_factory()); | |
| 1377 if (decoder_factory) { | |
| 1378 LOG(LS_INFO) << "Set EGL context for HW decoding."; | |
| 1379 decoder_factory->SetEGLContext(jni, remote_egl_context); | |
| 1380 } | |
| 1381 } | |
| 1382 | |
| 1383 static PeerConnectionInterface::IceTransportsType | |
| 1384 JavaIceTransportsTypeToNativeType(JNIEnv* jni, jobject j_ice_transports_type) { | |
| 1385 std::string enum_name = GetJavaEnumName( | |
| 1386 jni, "org/webrtc/PeerConnection$IceTransportsType", | |
| 1387 j_ice_transports_type); | |
| 1388 | |
| 1389 if (enum_name == "ALL") | |
| 1390 return PeerConnectionInterface::kAll; | |
| 1391 | |
| 1392 if (enum_name == "RELAY") | |
| 1393 return PeerConnectionInterface::kRelay; | |
| 1394 | |
| 1395 if (enum_name == "NOHOST") | |
| 1396 return PeerConnectionInterface::kNoHost; | |
| 1397 | |
| 1398 if (enum_name == "NONE") | |
| 1399 return PeerConnectionInterface::kNone; | |
| 1400 | |
| 1401 RTC_CHECK(false) << "Unexpected IceTransportsType enum_name " << enum_name; | |
| 1402 return PeerConnectionInterface::kAll; | |
| 1403 } | |
| 1404 | |
| 1405 static PeerConnectionInterface::BundlePolicy | |
| 1406 JavaBundlePolicyToNativeType(JNIEnv* jni, jobject j_bundle_policy) { | |
| 1407 std::string enum_name = GetJavaEnumName( | |
| 1408 jni, "org/webrtc/PeerConnection$BundlePolicy", | |
| 1409 j_bundle_policy); | |
| 1410 | |
| 1411 if (enum_name == "BALANCED") | |
| 1412 return PeerConnectionInterface::kBundlePolicyBalanced; | |
| 1413 | |
| 1414 if (enum_name == "MAXBUNDLE") | |
| 1415 return PeerConnectionInterface::kBundlePolicyMaxBundle; | |
| 1416 | |
| 1417 if (enum_name == "MAXCOMPAT") | |
| 1418 return PeerConnectionInterface::kBundlePolicyMaxCompat; | |
| 1419 | |
| 1420 RTC_CHECK(false) << "Unexpected BundlePolicy enum_name " << enum_name; | |
| 1421 return PeerConnectionInterface::kBundlePolicyBalanced; | |
| 1422 } | |
| 1423 | |
| 1424 static PeerConnectionInterface::RtcpMuxPolicy | |
| 1425 JavaRtcpMuxPolicyToNativeType(JNIEnv* jni, jobject j_rtcp_mux_policy) { | |
| 1426 std::string enum_name = GetJavaEnumName( | |
| 1427 jni, "org/webrtc/PeerConnection$RtcpMuxPolicy", | |
| 1428 j_rtcp_mux_policy); | |
| 1429 | |
| 1430 if (enum_name == "NEGOTIATE") | |
| 1431 return PeerConnectionInterface::kRtcpMuxPolicyNegotiate; | |
| 1432 | |
| 1433 if (enum_name == "REQUIRE") | |
| 1434 return PeerConnectionInterface::kRtcpMuxPolicyRequire; | |
| 1435 | |
| 1436 RTC_CHECK(false) << "Unexpected RtcpMuxPolicy enum_name " << enum_name; | |
| 1437 return PeerConnectionInterface::kRtcpMuxPolicyNegotiate; | |
| 1438 } | |
| 1439 | |
| 1440 static PeerConnectionInterface::TcpCandidatePolicy | |
| 1441 JavaTcpCandidatePolicyToNativeType( | |
| 1442 JNIEnv* jni, jobject j_tcp_candidate_policy) { | |
| 1443 std::string enum_name = GetJavaEnumName( | |
| 1444 jni, "org/webrtc/PeerConnection$TcpCandidatePolicy", | |
| 1445 j_tcp_candidate_policy); | |
| 1446 | |
| 1447 if (enum_name == "ENABLED") | |
| 1448 return PeerConnectionInterface::kTcpCandidatePolicyEnabled; | |
| 1449 | |
| 1450 if (enum_name == "DISABLED") | |
| 1451 return PeerConnectionInterface::kTcpCandidatePolicyDisabled; | |
| 1452 | |
| 1453 RTC_CHECK(false) << "Unexpected TcpCandidatePolicy enum_name " << enum_name; | |
| 1454 return PeerConnectionInterface::kTcpCandidatePolicyEnabled; | |
| 1455 } | |
| 1456 | |
| 1457 static PeerConnectionInterface::CandidateNetworkPolicy | |
| 1458 JavaCandidateNetworkPolicyToNativeType(JNIEnv* jni, | |
| 1459 jobject j_candidate_network_policy) { | |
| 1460 std::string enum_name = | |
| 1461 GetJavaEnumName(jni, "org/webrtc/PeerConnection$CandidateNetworkPolicy", | |
| 1462 j_candidate_network_policy); | |
| 1463 | |
| 1464 if (enum_name == "ALL") | |
| 1465 return PeerConnectionInterface::kCandidateNetworkPolicyAll; | |
| 1466 | |
| 1467 if (enum_name == "LOW_COST") | |
| 1468 return PeerConnectionInterface::kCandidateNetworkPolicyLowCost; | |
| 1469 | |
| 1470 RTC_CHECK(false) << "Unexpected CandidateNetworkPolicy enum_name " | |
| 1471 << enum_name; | |
| 1472 return PeerConnectionInterface::kCandidateNetworkPolicyAll; | |
| 1473 } | |
| 1474 | |
| 1475 static rtc::KeyType JavaKeyTypeToNativeType(JNIEnv* jni, jobject j_key_type) { | |
| 1476 std::string enum_name = GetJavaEnumName( | |
| 1477 jni, "org/webrtc/PeerConnection$KeyType", j_key_type); | |
| 1478 | |
| 1479 if (enum_name == "RSA") | |
| 1480 return rtc::KT_RSA; | |
| 1481 if (enum_name == "ECDSA") | |
| 1482 return rtc::KT_ECDSA; | |
| 1483 | |
| 1484 RTC_CHECK(false) << "Unexpected KeyType enum_name " << enum_name; | |
| 1485 return rtc::KT_ECDSA; | |
| 1486 } | |
| 1487 | |
| 1488 static PeerConnectionInterface::ContinualGatheringPolicy | |
| 1489 JavaContinualGatheringPolicyToNativeType( | |
| 1490 JNIEnv* jni, jobject j_gathering_policy) { | |
| 1491 std::string enum_name = GetJavaEnumName( | |
| 1492 jni, "org/webrtc/PeerConnection$ContinualGatheringPolicy", | |
| 1493 j_gathering_policy); | |
| 1494 if (enum_name == "GATHER_ONCE") | |
| 1495 return PeerConnectionInterface::GATHER_ONCE; | |
| 1496 | |
| 1497 if (enum_name == "GATHER_CONTINUALLY") | |
| 1498 return PeerConnectionInterface::GATHER_CONTINUALLY; | |
| 1499 | |
| 1500 RTC_CHECK(false) << "Unexpected ContinualGatheringPolicy enum name " | |
| 1501 << enum_name; | |
| 1502 return PeerConnectionInterface::GATHER_ONCE; | |
| 1503 } | |
| 1504 | |
| 1505 static void JavaIceServersToJsepIceServers( | |
| 1506 JNIEnv* jni, jobject j_ice_servers, | |
| 1507 PeerConnectionInterface::IceServers* ice_servers) { | |
| 1508 for (jobject j_ice_server : Iterable(jni, j_ice_servers)) { | |
| 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 } | |
| 1529 | |
| 1530 static void JavaRTCConfigurationToJsepRTCConfiguration( | |
| 1531 JNIEnv* jni, | |
| 1532 jobject j_rtc_config, | |
| 1533 PeerConnectionInterface::RTCConfiguration* rtc_config) { | |
| 1534 jclass j_rtc_config_class = GetObjectClass(jni, j_rtc_config); | |
| 1535 | |
| 1536 jfieldID j_ice_transports_type_id = GetFieldID( | |
| 1537 jni, j_rtc_config_class, "iceTransportsType", | |
| 1538 "Lorg/webrtc/PeerConnection$IceTransportsType;"); | |
| 1539 jobject j_ice_transports_type = GetObjectField( | |
| 1540 jni, j_rtc_config, j_ice_transports_type_id); | |
| 1541 | |
| 1542 jfieldID j_bundle_policy_id = GetFieldID( | |
| 1543 jni, j_rtc_config_class, "bundlePolicy", | |
| 1544 "Lorg/webrtc/PeerConnection$BundlePolicy;"); | |
| 1545 jobject j_bundle_policy = GetObjectField( | |
| 1546 jni, j_rtc_config, j_bundle_policy_id); | |
| 1547 | |
| 1548 jfieldID j_rtcp_mux_policy_id = GetFieldID( | |
| 1549 jni, j_rtc_config_class, "rtcpMuxPolicy", | |
| 1550 "Lorg/webrtc/PeerConnection$RtcpMuxPolicy;"); | |
| 1551 jobject j_rtcp_mux_policy = GetObjectField( | |
| 1552 jni, j_rtc_config, j_rtcp_mux_policy_id); | |
| 1553 | |
| 1554 jfieldID j_tcp_candidate_policy_id = GetFieldID( | |
| 1555 jni, j_rtc_config_class, "tcpCandidatePolicy", | |
| 1556 "Lorg/webrtc/PeerConnection$TcpCandidatePolicy;"); | |
| 1557 jobject j_tcp_candidate_policy = GetObjectField( | |
| 1558 jni, j_rtc_config, j_tcp_candidate_policy_id); | |
| 1559 | |
| 1560 jfieldID j_candidate_network_policy_id = GetFieldID( | |
| 1561 jni, j_rtc_config_class, "candidateNetworkPolicy", | |
| 1562 "Lorg/webrtc/PeerConnection$CandidateNetworkPolicy;"); | |
| 1563 jobject j_candidate_network_policy = GetObjectField( | |
| 1564 jni, j_rtc_config, j_candidate_network_policy_id); | |
| 1565 | |
| 1566 jfieldID j_ice_servers_id = GetFieldID( | |
| 1567 jni, j_rtc_config_class, "iceServers", "Ljava/util/List;"); | |
| 1568 jobject j_ice_servers = GetObjectField(jni, j_rtc_config, j_ice_servers_id); | |
| 1569 | |
| 1570 jfieldID j_audio_jitter_buffer_max_packets_id = | |
| 1571 GetFieldID(jni, j_rtc_config_class, "audioJitterBufferMaxPackets", "I"); | |
| 1572 jfieldID j_audio_jitter_buffer_fast_accelerate_id = GetFieldID( | |
| 1573 jni, j_rtc_config_class, "audioJitterBufferFastAccelerate", "Z"); | |
| 1574 | |
| 1575 jfieldID j_ice_connection_receiving_timeout_id = | |
| 1576 GetFieldID(jni, j_rtc_config_class, "iceConnectionReceivingTimeout", "I"); | |
| 1577 | |
| 1578 jfieldID j_ice_backup_candidate_pair_ping_interval_id = GetFieldID( | |
| 1579 jni, j_rtc_config_class, "iceBackupCandidatePairPingInterval", "I"); | |
| 1580 | |
| 1581 jfieldID j_continual_gathering_policy_id = | |
| 1582 GetFieldID(jni, j_rtc_config_class, "continualGatheringPolicy", | |
| 1583 "Lorg/webrtc/PeerConnection$ContinualGatheringPolicy;"); | |
| 1584 jobject j_continual_gathering_policy = | |
| 1585 GetObjectField(jni, j_rtc_config, j_continual_gathering_policy_id); | |
| 1586 | |
| 1587 jfieldID j_ice_candidate_pool_size_id = | |
| 1588 GetFieldID(jni, j_rtc_config_class, "iceCandidatePoolSize", "I"); | |
| 1589 | |
| 1590 rtc_config->type = | |
| 1591 JavaIceTransportsTypeToNativeType(jni, j_ice_transports_type); | |
| 1592 rtc_config->bundle_policy = | |
| 1593 JavaBundlePolicyToNativeType(jni, j_bundle_policy); | |
| 1594 rtc_config->rtcp_mux_policy = | |
| 1595 JavaRtcpMuxPolicyToNativeType(jni, j_rtcp_mux_policy); | |
| 1596 rtc_config->tcp_candidate_policy = | |
| 1597 JavaTcpCandidatePolicyToNativeType(jni, j_tcp_candidate_policy); | |
| 1598 rtc_config->candidate_network_policy = | |
| 1599 JavaCandidateNetworkPolicyToNativeType(jni, j_candidate_network_policy); | |
| 1600 JavaIceServersToJsepIceServers(jni, j_ice_servers, &rtc_config->servers); | |
| 1601 rtc_config->audio_jitter_buffer_max_packets = | |
| 1602 GetIntField(jni, j_rtc_config, j_audio_jitter_buffer_max_packets_id); | |
| 1603 rtc_config->audio_jitter_buffer_fast_accelerate = GetBooleanField( | |
| 1604 jni, j_rtc_config, j_audio_jitter_buffer_fast_accelerate_id); | |
| 1605 rtc_config->ice_connection_receiving_timeout = | |
| 1606 GetIntField(jni, j_rtc_config, j_ice_connection_receiving_timeout_id); | |
| 1607 rtc_config->ice_backup_candidate_pair_ping_interval = GetIntField( | |
| 1608 jni, j_rtc_config, j_ice_backup_candidate_pair_ping_interval_id); | |
| 1609 rtc_config->continual_gathering_policy = | |
| 1610 JavaContinualGatheringPolicyToNativeType( | |
| 1611 jni, j_continual_gathering_policy); | |
| 1612 rtc_config->ice_candidate_pool_size = | |
| 1613 GetIntField(jni, j_rtc_config, j_ice_candidate_pool_size_id); | |
| 1614 } | |
| 1615 | |
| 1616 JOW(jlong, PeerConnectionFactory_nativeCreatePeerConnection)( | |
| 1617 JNIEnv *jni, jclass, jlong factory, jobject j_rtc_config, | |
| 1618 jobject j_constraints, jlong observer_p) { | |
| 1619 rtc::scoped_refptr<PeerConnectionFactoryInterface> f( | |
| 1620 reinterpret_cast<PeerConnectionFactoryInterface*>( | |
| 1621 factoryFromJava(factory))); | |
| 1622 | |
| 1623 PeerConnectionInterface::RTCConfiguration rtc_config; | |
| 1624 JavaRTCConfigurationToJsepRTCConfiguration(jni, j_rtc_config, &rtc_config); | |
| 1625 | |
| 1626 jclass j_rtc_config_class = GetObjectClass(jni, j_rtc_config); | |
| 1627 jfieldID j_key_type_id = GetFieldID(jni, j_rtc_config_class, "keyType", | |
| 1628 "Lorg/webrtc/PeerConnection$KeyType;"); | |
| 1629 jobject j_key_type = GetObjectField(jni, j_rtc_config, j_key_type_id); | |
| 1630 | |
| 1631 // Generate non-default certificate. | |
| 1632 rtc::KeyType key_type = JavaKeyTypeToNativeType(jni, j_key_type); | |
| 1633 if (key_type != rtc::KT_DEFAULT) { | |
| 1634 rtc::scoped_refptr<rtc::RTCCertificate> certificate = | |
| 1635 rtc::RTCCertificateGenerator::GenerateCertificate( | |
| 1636 rtc::KeyParams(key_type), rtc::Optional<uint64_t>()); | |
| 1637 if (!certificate) { | |
| 1638 LOG(LS_ERROR) << "Failed to generate certificate. KeyType: " << key_type; | |
| 1639 return 0; | |
| 1640 } | |
| 1641 rtc_config.certificates.push_back(certificate); | |
| 1642 } | |
| 1643 | |
| 1644 PCOJava* observer = reinterpret_cast<PCOJava*>(observer_p); | |
| 1645 observer->SetConstraints(new ConstraintsWrapper(jni, j_constraints)); | |
| 1646 rtc::scoped_refptr<PeerConnectionInterface> pc(f->CreatePeerConnection( | |
| 1647 rtc_config, observer->constraints(), NULL, NULL, observer)); | |
| 1648 return (jlong)pc.release(); | |
| 1649 } | |
| 1650 | |
| 1651 static rtc::scoped_refptr<PeerConnectionInterface> ExtractNativePC( | |
| 1652 JNIEnv* jni, jobject j_pc) { | |
| 1653 jfieldID native_pc_id = GetFieldID(jni, | |
| 1654 GetObjectClass(jni, j_pc), "nativePeerConnection", "J"); | |
| 1655 jlong j_p = GetLongField(jni, j_pc, native_pc_id); | |
| 1656 return rtc::scoped_refptr<PeerConnectionInterface>( | |
| 1657 reinterpret_cast<PeerConnectionInterface*>(j_p)); | |
| 1658 } | |
| 1659 | |
| 1660 JOW(jobject, PeerConnection_getLocalDescription)(JNIEnv* jni, jobject j_pc) { | |
| 1661 const SessionDescriptionInterface* sdp = | |
| 1662 ExtractNativePC(jni, j_pc)->local_description(); | |
| 1663 return sdp ? JavaSdpFromNativeSdp(jni, sdp) : NULL; | |
| 1664 } | |
| 1665 | |
| 1666 JOW(jobject, PeerConnection_getRemoteDescription)(JNIEnv* jni, jobject j_pc) { | |
| 1667 const SessionDescriptionInterface* sdp = | |
| 1668 ExtractNativePC(jni, j_pc)->remote_description(); | |
| 1669 return sdp ? JavaSdpFromNativeSdp(jni, sdp) : NULL; | |
| 1670 } | |
| 1671 | |
| 1672 JOW(jobject, PeerConnection_createDataChannel)( | |
| 1673 JNIEnv* jni, jobject j_pc, jstring j_label, jobject j_init) { | |
| 1674 DataChannelInit init = JavaDataChannelInitToNative(jni, j_init); | |
| 1675 rtc::scoped_refptr<DataChannelInterface> channel( | |
| 1676 ExtractNativePC(jni, j_pc)->CreateDataChannel( | |
| 1677 JavaToStdString(jni, j_label), &init)); | |
| 1678 // Mustn't pass channel.get() directly through NewObject to avoid reading its | |
| 1679 // vararg parameter as 64-bit and reading memory that doesn't belong to the | |
| 1680 // 32-bit parameter. | |
| 1681 jlong nativeChannelPtr = jlongFromPointer(channel.get()); | |
| 1682 RTC_CHECK(nativeChannelPtr) << "Failed to create DataChannel"; | |
| 1683 jclass j_data_channel_class = FindClass(jni, "org/webrtc/DataChannel"); | |
| 1684 jmethodID j_data_channel_ctor = GetMethodID( | |
| 1685 jni, j_data_channel_class, "<init>", "(J)V"); | |
| 1686 jobject j_channel = jni->NewObject( | |
| 1687 j_data_channel_class, j_data_channel_ctor, nativeChannelPtr); | |
| 1688 CHECK_EXCEPTION(jni) << "error during NewObject"; | |
| 1689 // Channel is now owned by Java object, and will be freed from there. | |
| 1690 int bumped_count = channel->AddRef(); | |
| 1691 RTC_CHECK(bumped_count == 2) << "Unexpected refcount"; | |
| 1692 return j_channel; | |
| 1693 } | |
| 1694 | |
| 1695 JOW(void, PeerConnection_createOffer)( | |
| 1696 JNIEnv* jni, jobject j_pc, jobject j_observer, jobject j_constraints) { | |
| 1697 ConstraintsWrapper* constraints = | |
| 1698 new ConstraintsWrapper(jni, j_constraints); | |
| 1699 rtc::scoped_refptr<CreateSdpObserverWrapper> observer( | |
| 1700 new rtc::RefCountedObject<CreateSdpObserverWrapper>( | |
| 1701 jni, j_observer, constraints)); | |
| 1702 ExtractNativePC(jni, j_pc)->CreateOffer(observer, constraints); | |
| 1703 } | |
| 1704 | |
| 1705 JOW(void, PeerConnection_createAnswer)( | |
| 1706 JNIEnv* jni, jobject j_pc, jobject j_observer, jobject j_constraints) { | |
| 1707 ConstraintsWrapper* constraints = | |
| 1708 new ConstraintsWrapper(jni, j_constraints); | |
| 1709 rtc::scoped_refptr<CreateSdpObserverWrapper> observer( | |
| 1710 new rtc::RefCountedObject<CreateSdpObserverWrapper>( | |
| 1711 jni, j_observer, constraints)); | |
| 1712 ExtractNativePC(jni, j_pc)->CreateAnswer(observer, constraints); | |
| 1713 } | |
| 1714 | |
| 1715 // Helper to create a SessionDescriptionInterface from a SessionDescription. | |
| 1716 static SessionDescriptionInterface* JavaSdpToNativeSdp( | |
| 1717 JNIEnv* jni, jobject j_sdp) { | |
| 1718 jfieldID j_type_id = GetFieldID( | |
| 1719 jni, GetObjectClass(jni, j_sdp), "type", | |
| 1720 "Lorg/webrtc/SessionDescription$Type;"); | |
| 1721 jobject j_type = GetObjectField(jni, j_sdp, j_type_id); | |
| 1722 jmethodID j_canonical_form_id = GetMethodID( | |
| 1723 jni, GetObjectClass(jni, j_type), "canonicalForm", | |
| 1724 "()Ljava/lang/String;"); | |
| 1725 jstring j_type_string = (jstring)jni->CallObjectMethod( | |
| 1726 j_type, j_canonical_form_id); | |
| 1727 CHECK_EXCEPTION(jni) << "error during CallObjectMethod"; | |
| 1728 std::string std_type = JavaToStdString(jni, j_type_string); | |
| 1729 | |
| 1730 jfieldID j_description_id = GetFieldID( | |
| 1731 jni, GetObjectClass(jni, j_sdp), "description", "Ljava/lang/String;"); | |
| 1732 jstring j_description = (jstring)GetObjectField(jni, j_sdp, j_description_id); | |
| 1733 std::string std_description = JavaToStdString(jni, j_description); | |
| 1734 | |
| 1735 return webrtc::CreateSessionDescription( | |
| 1736 std_type, std_description, NULL); | |
| 1737 } | |
| 1738 | |
| 1739 JOW(void, PeerConnection_setLocalDescription)( | |
| 1740 JNIEnv* jni, jobject j_pc, | |
| 1741 jobject j_observer, jobject j_sdp) { | |
| 1742 rtc::scoped_refptr<SetSdpObserverWrapper> observer( | |
| 1743 new rtc::RefCountedObject<SetSdpObserverWrapper>( | |
| 1744 jni, j_observer, reinterpret_cast<ConstraintsWrapper*>(NULL))); | |
| 1745 ExtractNativePC(jni, j_pc)->SetLocalDescription( | |
| 1746 observer, JavaSdpToNativeSdp(jni, j_sdp)); | |
| 1747 } | |
| 1748 | |
| 1749 JOW(void, PeerConnection_setRemoteDescription)( | |
| 1750 JNIEnv* jni, jobject j_pc, | |
| 1751 jobject j_observer, jobject j_sdp) { | |
| 1752 rtc::scoped_refptr<SetSdpObserverWrapper> observer( | |
| 1753 new rtc::RefCountedObject<SetSdpObserverWrapper>( | |
| 1754 jni, j_observer, reinterpret_cast<ConstraintsWrapper*>(NULL))); | |
| 1755 ExtractNativePC(jni, j_pc)->SetRemoteDescription( | |
| 1756 observer, JavaSdpToNativeSdp(jni, j_sdp)); | |
| 1757 } | |
| 1758 | |
| 1759 JOW(jboolean, PeerConnection_setConfiguration)( | |
| 1760 JNIEnv* jni, jobject j_pc, jobject j_rtc_config) { | |
| 1761 PeerConnectionInterface::RTCConfiguration rtc_config; | |
| 1762 JavaRTCConfigurationToJsepRTCConfiguration(jni, j_rtc_config, &rtc_config); | |
| 1763 return ExtractNativePC(jni, j_pc)->SetConfiguration(rtc_config); | |
| 1764 } | |
| 1765 | |
| 1766 JOW(jboolean, PeerConnection_nativeAddIceCandidate)( | |
| 1767 JNIEnv* jni, jobject j_pc, jstring j_sdp_mid, | |
| 1768 jint j_sdp_mline_index, jstring j_candidate_sdp) { | |
| 1769 std::string sdp_mid = JavaToStdString(jni, j_sdp_mid); | |
| 1770 std::string sdp = JavaToStdString(jni, j_candidate_sdp); | |
| 1771 std::unique_ptr<IceCandidateInterface> candidate( | |
| 1772 webrtc::CreateIceCandidate(sdp_mid, j_sdp_mline_index, sdp, NULL)); | |
| 1773 return ExtractNativePC(jni, j_pc)->AddIceCandidate(candidate.get()); | |
| 1774 } | |
| 1775 | |
| 1776 static cricket::Candidate GetCandidateFromJava(JNIEnv* jni, | |
| 1777 jobject j_candidate) { | |
| 1778 jclass j_candidate_class = GetObjectClass(jni, j_candidate); | |
| 1779 jfieldID j_sdp_mid_id = | |
| 1780 GetFieldID(jni, j_candidate_class, "sdpMid", "Ljava/lang/String;"); | |
| 1781 std::string sdp_mid = | |
| 1782 JavaToStdString(jni, GetStringField(jni, j_candidate, j_sdp_mid_id)); | |
| 1783 jfieldID j_sdp_id = | |
| 1784 GetFieldID(jni, j_candidate_class, "sdp", "Ljava/lang/String;"); | |
| 1785 std::string sdp = | |
| 1786 JavaToStdString(jni, GetStringField(jni, j_candidate, j_sdp_id)); | |
| 1787 cricket::Candidate candidate; | |
| 1788 if (!webrtc::SdpDeserializeCandidate(sdp_mid, sdp, &candidate, NULL)) { | |
| 1789 LOG(LS_ERROR) << "SdpDescrializeCandidate failed with sdp " << sdp; | |
| 1790 } | |
| 1791 return candidate; | |
| 1792 } | |
| 1793 | |
| 1794 JOW(jboolean, PeerConnection_nativeRemoveIceCandidates) | |
| 1795 (JNIEnv* jni, jobject j_pc, jobjectArray j_candidates) { | |
| 1796 std::vector<cricket::Candidate> candidates; | |
| 1797 size_t num_candidates = jni->GetArrayLength(j_candidates); | |
| 1798 for (size_t i = 0; i < num_candidates; ++i) { | |
| 1799 jobject j_candidate = jni->GetObjectArrayElement(j_candidates, i); | |
| 1800 candidates.push_back(GetCandidateFromJava(jni, j_candidate)); | |
| 1801 } | |
| 1802 return ExtractNativePC(jni, j_pc)->RemoveIceCandidates(candidates); | |
| 1803 } | |
| 1804 | |
| 1805 JOW(jboolean, PeerConnection_nativeAddLocalStream)( | |
| 1806 JNIEnv* jni, jobject j_pc, jlong native_stream) { | |
| 1807 return ExtractNativePC(jni, j_pc)->AddStream( | |
| 1808 reinterpret_cast<MediaStreamInterface*>(native_stream)); | |
| 1809 } | |
| 1810 | |
| 1811 JOW(void, PeerConnection_nativeRemoveLocalStream)( | |
| 1812 JNIEnv* jni, jobject j_pc, jlong native_stream) { | |
| 1813 ExtractNativePC(jni, j_pc)->RemoveStream( | |
| 1814 reinterpret_cast<MediaStreamInterface*>(native_stream)); | |
| 1815 } | |
| 1816 | |
| 1817 JOW(jobject, PeerConnection_nativeCreateSender)( | |
| 1818 JNIEnv* jni, jobject j_pc, jstring j_kind, jstring j_stream_id) { | |
| 1819 jclass j_rtp_sender_class = FindClass(jni, "org/webrtc/RtpSender"); | |
| 1820 jmethodID j_rtp_sender_ctor = | |
| 1821 GetMethodID(jni, j_rtp_sender_class, "<init>", "(J)V"); | |
| 1822 | |
| 1823 std::string kind = JavaToStdString(jni, j_kind); | |
| 1824 std::string stream_id = JavaToStdString(jni, j_stream_id); | |
| 1825 rtc::scoped_refptr<RtpSenderInterface> sender = | |
| 1826 ExtractNativePC(jni, j_pc)->CreateSender(kind, stream_id); | |
| 1827 if (!sender.get()) { | |
| 1828 return nullptr; | |
| 1829 } | |
| 1830 jlong nativeSenderPtr = jlongFromPointer(sender.get()); | |
| 1831 jobject j_sender = | |
| 1832 jni->NewObject(j_rtp_sender_class, j_rtp_sender_ctor, nativeSenderPtr); | |
| 1833 CHECK_EXCEPTION(jni) << "error during NewObject"; | |
| 1834 // Sender is now owned by the Java object, and will be freed from | |
| 1835 // RtpSender.dispose(), called by PeerConnection.dispose() or getSenders(). | |
| 1836 sender->AddRef(); | |
| 1837 return j_sender; | |
| 1838 } | |
| 1839 | |
| 1840 JOW(jobject, PeerConnection_nativeGetSenders)(JNIEnv* jni, jobject j_pc) { | |
| 1841 jclass j_array_list_class = FindClass(jni, "java/util/ArrayList"); | |
| 1842 jmethodID j_array_list_ctor = | |
| 1843 GetMethodID(jni, j_array_list_class, "<init>", "()V"); | |
| 1844 jmethodID j_array_list_add = | |
| 1845 GetMethodID(jni, j_array_list_class, "add", "(Ljava/lang/Object;)Z"); | |
| 1846 jobject j_senders = jni->NewObject(j_array_list_class, j_array_list_ctor); | |
| 1847 CHECK_EXCEPTION(jni) << "error during NewObject"; | |
| 1848 | |
| 1849 jclass j_rtp_sender_class = FindClass(jni, "org/webrtc/RtpSender"); | |
| 1850 jmethodID j_rtp_sender_ctor = | |
| 1851 GetMethodID(jni, j_rtp_sender_class, "<init>", "(J)V"); | |
| 1852 | |
| 1853 auto senders = ExtractNativePC(jni, j_pc)->GetSenders(); | |
| 1854 for (const auto& sender : senders) { | |
| 1855 jlong nativeSenderPtr = jlongFromPointer(sender.get()); | |
| 1856 jobject j_sender = | |
| 1857 jni->NewObject(j_rtp_sender_class, j_rtp_sender_ctor, nativeSenderPtr); | |
| 1858 CHECK_EXCEPTION(jni) << "error during NewObject"; | |
| 1859 // Sender is now owned by the Java object, and will be freed from | |
| 1860 // RtpSender.dispose(), called by PeerConnection.dispose() or getSenders(). | |
| 1861 sender->AddRef(); | |
| 1862 jni->CallBooleanMethod(j_senders, j_array_list_add, j_sender); | |
| 1863 CHECK_EXCEPTION(jni) << "error during CallBooleanMethod"; | |
| 1864 } | |
| 1865 return j_senders; | |
| 1866 } | |
| 1867 | |
| 1868 JOW(jobject, PeerConnection_nativeGetReceivers)(JNIEnv* jni, jobject j_pc) { | |
| 1869 jclass j_array_list_class = FindClass(jni, "java/util/ArrayList"); | |
| 1870 jmethodID j_array_list_ctor = | |
| 1871 GetMethodID(jni, j_array_list_class, "<init>", "()V"); | |
| 1872 jmethodID j_array_list_add = | |
| 1873 GetMethodID(jni, j_array_list_class, "add", "(Ljava/lang/Object;)Z"); | |
| 1874 jobject j_receivers = jni->NewObject(j_array_list_class, j_array_list_ctor); | |
| 1875 CHECK_EXCEPTION(jni) << "error during NewObject"; | |
| 1876 | |
| 1877 jclass j_rtp_receiver_class = FindClass(jni, "org/webrtc/RtpReceiver"); | |
| 1878 jmethodID j_rtp_receiver_ctor = | |
| 1879 GetMethodID(jni, j_rtp_receiver_class, "<init>", "(J)V"); | |
| 1880 | |
| 1881 auto receivers = ExtractNativePC(jni, j_pc)->GetReceivers(); | |
| 1882 for (const auto& receiver : receivers) { | |
| 1883 jlong nativeReceiverPtr = jlongFromPointer(receiver.get()); | |
| 1884 jobject j_receiver = jni->NewObject(j_rtp_receiver_class, | |
| 1885 j_rtp_receiver_ctor, nativeReceiverPtr); | |
| 1886 CHECK_EXCEPTION(jni) << "error during NewObject"; | |
| 1887 // Receiver is now owned by Java object, and will be freed from there. | |
| 1888 receiver->AddRef(); | |
| 1889 jni->CallBooleanMethod(j_receivers, j_array_list_add, j_receiver); | |
| 1890 CHECK_EXCEPTION(jni) << "error during CallBooleanMethod"; | |
| 1891 } | |
| 1892 return j_receivers; | |
| 1893 } | |
| 1894 | |
| 1895 JOW(bool, PeerConnection_nativeGetStats)( | |
| 1896 JNIEnv* jni, jobject j_pc, jobject j_observer, jlong native_track) { | |
| 1897 rtc::scoped_refptr<StatsObserverWrapper> observer( | |
| 1898 new rtc::RefCountedObject<StatsObserverWrapper>(jni, j_observer)); | |
| 1899 return ExtractNativePC(jni, j_pc)->GetStats( | |
| 1900 observer, | |
| 1901 reinterpret_cast<MediaStreamTrackInterface*>(native_track), | |
| 1902 PeerConnectionInterface::kStatsOutputLevelStandard); | |
| 1903 } | |
| 1904 | |
| 1905 JOW(jobject, PeerConnection_signalingState)(JNIEnv* jni, jobject j_pc) { | |
| 1906 PeerConnectionInterface::SignalingState state = | |
| 1907 ExtractNativePC(jni, j_pc)->signaling_state(); | |
| 1908 return JavaEnumFromIndex(jni, "PeerConnection$SignalingState", state); | |
| 1909 } | |
| 1910 | |
| 1911 JOW(jobject, PeerConnection_iceConnectionState)(JNIEnv* jni, jobject j_pc) { | |
| 1912 PeerConnectionInterface::IceConnectionState state = | |
| 1913 ExtractNativePC(jni, j_pc)->ice_connection_state(); | |
| 1914 return JavaEnumFromIndex(jni, "PeerConnection$IceConnectionState", state); | |
| 1915 } | |
| 1916 | |
| 1917 JOW(jobject, PeerConnection_iceGatheringState)(JNIEnv* jni, jobject j_pc) { | |
| 1918 PeerConnectionInterface::IceGatheringState state = | |
| 1919 ExtractNativePC(jni, j_pc)->ice_gathering_state(); | |
| 1920 return JavaEnumFromIndex(jni, "PeerConnection$IceGatheringState", state); | |
| 1921 } | |
| 1922 | |
| 1923 JOW(void, PeerConnection_close)(JNIEnv* jni, jobject j_pc) { | |
| 1924 ExtractNativePC(jni, j_pc)->Close(); | |
| 1925 return; | |
| 1926 } | |
| 1927 | |
| 1928 JOW(jobject, MediaSource_nativeState)(JNIEnv* jni, jclass, jlong j_p) { | |
| 1929 rtc::scoped_refptr<MediaSourceInterface> p( | |
| 1930 reinterpret_cast<MediaSourceInterface*>(j_p)); | |
| 1931 return JavaEnumFromIndex(jni, "MediaSource$State", p->state()); | |
| 1932 } | |
| 1933 | |
| 1934 JOW(jlong, VideoRenderer_nativeWrapVideoRenderer)( | |
| 1935 JNIEnv* jni, jclass, jobject j_callbacks) { | |
| 1936 std::unique_ptr<JavaVideoRendererWrapper> renderer( | |
| 1937 new JavaVideoRendererWrapper(jni, j_callbacks)); | |
| 1938 return (jlong)renderer.release(); | |
| 1939 } | |
| 1940 | |
| 1941 JOW(void, VideoRenderer_nativeCopyPlane)( | |
| 1942 JNIEnv *jni, jclass, jobject j_src_buffer, jint width, jint height, | |
| 1943 jint src_stride, jobject j_dst_buffer, jint dst_stride) { | |
| 1944 size_t src_size = jni->GetDirectBufferCapacity(j_src_buffer); | |
| 1945 size_t dst_size = jni->GetDirectBufferCapacity(j_dst_buffer); | |
| 1946 RTC_CHECK(src_stride >= width) << "Wrong source stride " << src_stride; | |
| 1947 RTC_CHECK(dst_stride >= width) << "Wrong destination stride " << dst_stride; | |
| 1948 RTC_CHECK(src_size >= src_stride * height) | |
| 1949 << "Insufficient source buffer capacity " << src_size; | |
| 1950 RTC_CHECK(dst_size >= dst_stride * height) | |
| 1951 << "Insufficient destination buffer capacity " << dst_size; | |
| 1952 uint8_t *src = | |
| 1953 reinterpret_cast<uint8_t*>(jni->GetDirectBufferAddress(j_src_buffer)); | |
| 1954 uint8_t *dst = | |
| 1955 reinterpret_cast<uint8_t*>(jni->GetDirectBufferAddress(j_dst_buffer)); | |
| 1956 if (src_stride == dst_stride) { | |
| 1957 memcpy(dst, src, src_stride * height); | |
| 1958 } else { | |
| 1959 for (int i = 0; i < height; i++) { | |
| 1960 memcpy(dst, src, width); | |
| 1961 src += src_stride; | |
| 1962 dst += dst_stride; | |
| 1963 } | |
| 1964 } | |
| 1965 } | |
| 1966 | |
| 1967 JOW(void, VideoSource_stop)(JNIEnv* jni, jclass, jlong j_p) { | |
| 1968 reinterpret_cast<VideoTrackSourceInterface*>(j_p)->Stop(); | |
| 1969 } | |
| 1970 | |
| 1971 JOW(void, VideoSource_restart)( | |
| 1972 JNIEnv* jni, jclass, jlong j_p_source, jlong j_p_format) { | |
| 1973 reinterpret_cast<VideoTrackSourceInterface*>(j_p_source)->Restart(); | |
| 1974 } | |
| 1975 | |
| 1976 JOW(jstring, MediaStreamTrack_nativeId)(JNIEnv* jni, jclass, jlong j_p) { | |
| 1977 return JavaStringFromStdString( | |
| 1978 jni, reinterpret_cast<MediaStreamTrackInterface*>(j_p)->id()); | |
| 1979 } | |
| 1980 | |
| 1981 JOW(jstring, MediaStreamTrack_nativeKind)(JNIEnv* jni, jclass, jlong j_p) { | |
| 1982 return JavaStringFromStdString( | |
| 1983 jni, reinterpret_cast<MediaStreamTrackInterface*>(j_p)->kind()); | |
| 1984 } | |
| 1985 | |
| 1986 JOW(jboolean, MediaStreamTrack_nativeEnabled)(JNIEnv* jni, jclass, jlong j_p) { | |
| 1987 return reinterpret_cast<MediaStreamTrackInterface*>(j_p)->enabled(); | |
| 1988 } | |
| 1989 | |
| 1990 JOW(jobject, MediaStreamTrack_nativeState)(JNIEnv* jni, jclass, jlong j_p) { | |
| 1991 return JavaEnumFromIndex( | |
| 1992 jni, | |
| 1993 "MediaStreamTrack$State", | |
| 1994 reinterpret_cast<MediaStreamTrackInterface*>(j_p)->state()); | |
| 1995 } | |
| 1996 | |
| 1997 JOW(jboolean, MediaStreamTrack_nativeSetEnabled)( | |
| 1998 JNIEnv* jni, jclass, jlong j_p, jboolean enabled) { | |
| 1999 return reinterpret_cast<MediaStreamTrackInterface*>(j_p) | |
| 2000 ->set_enabled(enabled); | |
| 2001 } | |
| 2002 | |
| 2003 JOW(void, VideoTrack_nativeAddRenderer)( | |
| 2004 JNIEnv* jni, jclass, | |
| 2005 jlong j_video_track_pointer, jlong j_renderer_pointer) { | |
| 2006 reinterpret_cast<VideoTrackInterface*>(j_video_track_pointer) | |
| 2007 ->AddOrUpdateSink( | |
| 2008 reinterpret_cast<rtc::VideoSinkInterface<cricket::VideoFrame>*>( | |
| 2009 j_renderer_pointer), | |
| 2010 rtc::VideoSinkWants()); | |
| 2011 } | |
| 2012 | |
| 2013 JOW(void, VideoTrack_nativeRemoveRenderer)( | |
| 2014 JNIEnv* jni, jclass, | |
| 2015 jlong j_video_track_pointer, jlong j_renderer_pointer) { | |
| 2016 reinterpret_cast<VideoTrackInterface*>(j_video_track_pointer) | |
| 2017 ->RemoveSink( | |
| 2018 reinterpret_cast<rtc::VideoSinkInterface<cricket::VideoFrame>*>( | |
| 2019 j_renderer_pointer)); | |
| 2020 } | |
| 2021 | |
| 2022 JOW(jlong, CallSessionFileRotatingLogSink_nativeAddSink)( | |
| 2023 JNIEnv* jni, jclass, | |
| 2024 jstring j_dirPath, jint j_maxFileSize, jint j_severity) { | |
| 2025 std::string dir_path = JavaToStdString(jni, j_dirPath); | |
| 2026 rtc::CallSessionFileRotatingLogSink* sink = | |
| 2027 new rtc::CallSessionFileRotatingLogSink(dir_path, j_maxFileSize); | |
| 2028 if (!sink->Init()) { | |
| 2029 LOG_V(rtc::LoggingSeverity::LS_WARNING) << | |
| 2030 "Failed to init CallSessionFileRotatingLogSink for path " << dir_path; | |
| 2031 delete sink; | |
| 2032 return 0; | |
| 2033 } | |
| 2034 rtc::LogMessage::AddLogToStream( | |
| 2035 sink, static_cast<rtc::LoggingSeverity>(j_severity)); | |
| 2036 return (jlong) sink; | |
| 2037 } | |
| 2038 | |
| 2039 JOW(void, CallSessionFileRotatingLogSink_nativeDeleteSink)( | |
| 2040 JNIEnv* jni, jclass, jlong j_sink) { | |
| 2041 rtc::CallSessionFileRotatingLogSink* sink = | |
| 2042 reinterpret_cast<rtc::CallSessionFileRotatingLogSink*>(j_sink); | |
| 2043 rtc::LogMessage::RemoveLogToStream(sink); | |
| 2044 delete sink; | |
| 2045 } | |
| 2046 | |
| 2047 JOW(jbyteArray, CallSessionFileRotatingLogSink_nativeGetLogData)( | |
| 2048 JNIEnv* jni, jclass, jstring j_dirPath) { | |
| 2049 std::string dir_path = JavaToStdString(jni, j_dirPath); | |
| 2050 std::unique_ptr<rtc::CallSessionFileRotatingStream> stream( | |
| 2051 new rtc::CallSessionFileRotatingStream(dir_path)); | |
| 2052 if (!stream->Open()) { | |
| 2053 LOG_V(rtc::LoggingSeverity::LS_WARNING) << | |
| 2054 "Failed to open CallSessionFileRotatingStream for path " << dir_path; | |
| 2055 return jni->NewByteArray(0); | |
| 2056 } | |
| 2057 size_t log_size = 0; | |
| 2058 if (!stream->GetSize(&log_size) || log_size == 0) { | |
| 2059 LOG_V(rtc::LoggingSeverity::LS_WARNING) << | |
| 2060 "CallSessionFileRotatingStream returns 0 size for path " << dir_path; | |
| 2061 return jni->NewByteArray(0); | |
| 2062 } | |
| 2063 | |
| 2064 size_t read = 0; | |
| 2065 std::unique_ptr<jbyte> buffer(static_cast<jbyte*>(malloc(log_size))); | |
| 2066 stream->ReadAll(buffer.get(), log_size, &read, nullptr); | |
| 2067 | |
| 2068 jbyteArray result = jni->NewByteArray(read); | |
| 2069 jni->SetByteArrayRegion(result, 0, read, buffer.get()); | |
| 2070 | |
| 2071 return result; | |
| 2072 } | |
| 2073 | |
| 2074 JOW(jboolean, RtpSender_nativeSetTrack)(JNIEnv* jni, | |
| 2075 jclass, | |
| 2076 jlong j_rtp_sender_pointer, | |
| 2077 jlong j_track_pointer) { | |
| 2078 return reinterpret_cast<RtpSenderInterface*>(j_rtp_sender_pointer) | |
| 2079 ->SetTrack(reinterpret_cast<MediaStreamTrackInterface*>(j_track_pointer)); | |
| 2080 } | |
| 2081 | |
| 2082 JOW(jlong, RtpSender_nativeGetTrack)(JNIEnv* jni, | |
| 2083 jclass, | |
| 2084 jlong j_rtp_sender_pointer, | |
| 2085 jlong j_track_pointer) { | |
| 2086 return jlongFromPointer( | |
| 2087 reinterpret_cast<RtpSenderInterface*>(j_rtp_sender_pointer) | |
| 2088 ->track() | |
| 2089 .release()); | |
| 2090 } | |
| 2091 | |
| 2092 static void JavaRtpParametersToJsepRtpParameters( | |
| 2093 JNIEnv* jni, | |
| 2094 jobject j_parameters, | |
| 2095 webrtc::RtpParameters* parameters) { | |
| 2096 RTC_CHECK(parameters != nullptr); | |
| 2097 jclass parameters_class = jni->FindClass("org/webrtc/RtpParameters"); | |
| 2098 jfieldID encodings_id = | |
| 2099 GetFieldID(jni, parameters_class, "encodings", "Ljava/util/LinkedList;"); | |
| 2100 jfieldID codecs_id = | |
| 2101 GetFieldID(jni, parameters_class, "codecs", "Ljava/util/LinkedList;"); | |
| 2102 | |
| 2103 // Convert encodings. | |
| 2104 jobject j_encodings = GetObjectField(jni, j_parameters, encodings_id); | |
| 2105 const int kBitrateUnlimited = -1; | |
| 2106 jclass j_encoding_parameters_class = | |
| 2107 jni->FindClass("org/webrtc/RtpParameters$Encoding"); | |
| 2108 jfieldID active_id = | |
| 2109 GetFieldID(jni, j_encoding_parameters_class, "active", "Z"); | |
| 2110 jfieldID bitrate_id = GetFieldID(jni, j_encoding_parameters_class, | |
| 2111 "maxBitrateBps", "Ljava/lang/Integer;"); | |
| 2112 jclass j_integer_class = jni->FindClass("java/lang/Integer"); | |
| 2113 jmethodID int_value_id = GetMethodID(jni, j_integer_class, "intValue", "()I"); | |
| 2114 | |
| 2115 for (jobject j_encoding_parameters : Iterable(jni, j_encodings)) { | |
| 2116 webrtc::RtpEncodingParameters encoding; | |
| 2117 encoding.active = GetBooleanField(jni, j_encoding_parameters, active_id); | |
| 2118 jobject j_bitrate = | |
| 2119 GetNullableObjectField(jni, j_encoding_parameters, bitrate_id); | |
| 2120 if (!IsNull(jni, j_bitrate)) { | |
| 2121 int bitrate_value = jni->CallIntMethod(j_bitrate, int_value_id); | |
| 2122 CHECK_EXCEPTION(jni) << "error during CallIntMethod"; | |
| 2123 encoding.max_bitrate_bps = bitrate_value; | |
| 2124 } else { | |
| 2125 encoding.max_bitrate_bps = kBitrateUnlimited; | |
| 2126 } | |
| 2127 parameters->encodings.push_back(encoding); | |
| 2128 } | |
| 2129 | |
| 2130 // Convert codecs. | |
| 2131 jobject j_codecs = GetObjectField(jni, j_parameters, codecs_id); | |
| 2132 jclass codec_class = jni->FindClass("org/webrtc/RtpParameters$Codec"); | |
| 2133 jfieldID payload_type_id = GetFieldID(jni, codec_class, "payloadType", "I"); | |
| 2134 jfieldID mime_type_id = | |
| 2135 GetFieldID(jni, codec_class, "mimeType", "Ljava/lang/String;"); | |
| 2136 jfieldID clock_rate_id = GetFieldID(jni, codec_class, "clockRate", "I"); | |
| 2137 jfieldID channels_id = GetFieldID(jni, codec_class, "channels", "I"); | |
| 2138 | |
| 2139 for (jobject j_codec : Iterable(jni, j_codecs)) { | |
| 2140 webrtc::RtpCodecParameters codec; | |
| 2141 codec.payload_type = GetIntField(jni, j_codec, payload_type_id); | |
| 2142 codec.mime_type = | |
| 2143 JavaToStdString(jni, GetStringField(jni, j_codec, mime_type_id)); | |
| 2144 codec.clock_rate = GetIntField(jni, j_codec, clock_rate_id); | |
| 2145 codec.channels = GetIntField(jni, j_codec, channels_id); | |
| 2146 parameters->codecs.push_back(codec); | |
| 2147 } | |
| 2148 } | |
| 2149 | |
| 2150 static jobject JsepRtpParametersToJavaRtpParameters( | |
| 2151 JNIEnv* jni, | |
| 2152 const webrtc::RtpParameters& parameters) { | |
| 2153 jclass parameters_class = jni->FindClass("org/webrtc/RtpParameters"); | |
| 2154 jmethodID parameters_ctor = | |
| 2155 GetMethodID(jni, parameters_class, "<init>", "()V"); | |
| 2156 jobject j_parameters = jni->NewObject(parameters_class, parameters_ctor); | |
| 2157 CHECK_EXCEPTION(jni) << "error during NewObject"; | |
| 2158 | |
| 2159 // Add encodings. | |
| 2160 jclass encoding_class = jni->FindClass("org/webrtc/RtpParameters$Encoding"); | |
| 2161 jmethodID encoding_ctor = GetMethodID(jni, encoding_class, "<init>", "()V"); | |
| 2162 jfieldID encodings_id = | |
| 2163 GetFieldID(jni, parameters_class, "encodings", "Ljava/util/LinkedList;"); | |
| 2164 jobject j_encodings = GetObjectField(jni, j_parameters, encodings_id); | |
| 2165 jmethodID encodings_add = GetMethodID(jni, GetObjectClass(jni, j_encodings), | |
| 2166 "add", "(Ljava/lang/Object;)Z"); | |
| 2167 jfieldID active_id = | |
| 2168 GetFieldID(jni, encoding_class, "active", "Z"); | |
| 2169 jfieldID bitrate_id = | |
| 2170 GetFieldID(jni, encoding_class, "maxBitrateBps", "Ljava/lang/Integer;"); | |
| 2171 | |
| 2172 jclass integer_class = jni->FindClass("java/lang/Integer"); | |
| 2173 jmethodID integer_ctor = GetMethodID(jni, integer_class, "<init>", "(I)V"); | |
| 2174 | |
| 2175 for (const webrtc::RtpEncodingParameters& encoding : parameters.encodings) { | |
| 2176 jobject j_encoding_parameters = | |
| 2177 jni->NewObject(encoding_class, encoding_ctor); | |
| 2178 CHECK_EXCEPTION(jni) << "error during NewObject"; | |
| 2179 jni->SetBooleanField(j_encoding_parameters, active_id, encoding.active); | |
| 2180 CHECK_EXCEPTION(jni) << "error during SetBooleanField"; | |
| 2181 if (encoding.max_bitrate_bps > 0) { | |
| 2182 jobject j_bitrate_value = | |
| 2183 jni->NewObject(integer_class, integer_ctor, encoding.max_bitrate_bps); | |
| 2184 CHECK_EXCEPTION(jni) << "error during NewObject"; | |
| 2185 jni->SetObjectField(j_encoding_parameters, bitrate_id, j_bitrate_value); | |
| 2186 CHECK_EXCEPTION(jni) << "error during SetObjectField"; | |
| 2187 } | |
| 2188 jboolean added = jni->CallBooleanMethod(j_encodings, encodings_add, | |
| 2189 j_encoding_parameters); | |
| 2190 CHECK_EXCEPTION(jni) << "error during CallBooleanMethod"; | |
| 2191 RTC_CHECK(added); | |
| 2192 } | |
| 2193 | |
| 2194 // Add codecs. | |
| 2195 jclass codec_class = jni->FindClass("org/webrtc/RtpParameters$Codec"); | |
| 2196 jmethodID codec_ctor = GetMethodID(jni, codec_class, "<init>", "()V"); | |
| 2197 jfieldID codecs_id = | |
| 2198 GetFieldID(jni, parameters_class, "codecs", "Ljava/util/LinkedList;"); | |
| 2199 jobject j_codecs = GetObjectField(jni, j_parameters, codecs_id); | |
| 2200 jmethodID codecs_add = GetMethodID(jni, GetObjectClass(jni, j_codecs), | |
| 2201 "add", "(Ljava/lang/Object;)Z"); | |
| 2202 jfieldID payload_type_id = GetFieldID(jni, codec_class, "payloadType", "I"); | |
| 2203 jfieldID mime_type_id = | |
| 2204 GetFieldID(jni, codec_class, "mimeType", "Ljava/lang/String;"); | |
| 2205 jfieldID clock_rate_id = GetFieldID(jni, codec_class, "clockRate", "I"); | |
| 2206 jfieldID channels_id = GetFieldID(jni, codec_class, "channels", "I"); | |
| 2207 | |
| 2208 for (const webrtc::RtpCodecParameters& codec : parameters.codecs) { | |
| 2209 jobject j_codec = jni->NewObject(codec_class, codec_ctor); | |
| 2210 CHECK_EXCEPTION(jni) << "error during NewObject"; | |
| 2211 jni->SetIntField(j_codec, payload_type_id, codec.payload_type); | |
| 2212 CHECK_EXCEPTION(jni) << "error during SetIntField"; | |
| 2213 jni->SetObjectField(j_codec, mime_type_id, | |
| 2214 JavaStringFromStdString(jni, codec.mime_type)); | |
| 2215 CHECK_EXCEPTION(jni) << "error during SetObjectField"; | |
| 2216 jni->SetIntField(j_codec, clock_rate_id, codec.clock_rate); | |
| 2217 CHECK_EXCEPTION(jni) << "error during SetIntField"; | |
| 2218 jni->SetIntField(j_codec, channels_id, codec.channels); | |
| 2219 CHECK_EXCEPTION(jni) << "error during SetIntField"; | |
| 2220 jboolean added = jni->CallBooleanMethod(j_codecs, codecs_add, j_codec); | |
| 2221 CHECK_EXCEPTION(jni) << "error during CallBooleanMethod"; | |
| 2222 RTC_CHECK(added); | |
| 2223 } | |
| 2224 | |
| 2225 return j_parameters; | |
| 2226 } | |
| 2227 | |
| 2228 JOW(jboolean, RtpSender_nativeSetParameters) | |
| 2229 (JNIEnv* jni, jclass, jlong j_rtp_sender_pointer, jobject j_parameters) { | |
| 2230 if (IsNull(jni, j_parameters)) { | |
| 2231 return false; | |
| 2232 } | |
| 2233 webrtc::RtpParameters parameters; | |
| 2234 JavaRtpParametersToJsepRtpParameters(jni, j_parameters, ¶meters); | |
| 2235 return reinterpret_cast<RtpSenderInterface*>(j_rtp_sender_pointer) | |
| 2236 ->SetParameters(parameters); | |
| 2237 } | |
| 2238 | |
| 2239 JOW(jobject, RtpSender_nativeGetParameters) | |
| 2240 (JNIEnv* jni, jclass, jlong j_rtp_sender_pointer) { | |
| 2241 webrtc::RtpParameters parameters = | |
| 2242 reinterpret_cast<RtpSenderInterface*>(j_rtp_sender_pointer) | |
| 2243 ->GetParameters(); | |
| 2244 return JsepRtpParametersToJavaRtpParameters(jni, parameters); | |
| 2245 } | |
| 2246 | |
| 2247 JOW(jstring, RtpSender_nativeId)( | |
| 2248 JNIEnv* jni, jclass, jlong j_rtp_sender_pointer) { | |
| 2249 return JavaStringFromStdString( | |
| 2250 jni, reinterpret_cast<RtpSenderInterface*>(j_rtp_sender_pointer)->id()); | |
| 2251 } | |
| 2252 | |
| 2253 JOW(void, RtpSender_free)(JNIEnv* jni, jclass, jlong j_rtp_sender_pointer) { | |
| 2254 reinterpret_cast<RtpSenderInterface*>(j_rtp_sender_pointer)->Release(); | |
| 2255 } | |
| 2256 | |
| 2257 JOW(jlong, RtpReceiver_nativeGetTrack)(JNIEnv* jni, | |
| 2258 jclass, | |
| 2259 jlong j_rtp_receiver_pointer, | |
| 2260 jlong j_track_pointer) { | |
| 2261 return jlongFromPointer( | |
| 2262 reinterpret_cast<RtpReceiverInterface*>(j_rtp_receiver_pointer) | |
| 2263 ->track() | |
| 2264 .release()); | |
| 2265 } | |
| 2266 | |
| 2267 JOW(jboolean, RtpReceiver_nativeSetParameters) | |
| 2268 (JNIEnv* jni, jclass, jlong j_rtp_sender_pointer, jobject j_parameters) { | |
| 2269 if (IsNull(jni, j_parameters)) { | |
| 2270 return false; | |
| 2271 } | |
| 2272 webrtc::RtpParameters parameters; | |
| 2273 JavaRtpParametersToJsepRtpParameters(jni, j_parameters, ¶meters); | |
| 2274 return reinterpret_cast<RtpReceiverInterface*>(j_rtp_sender_pointer) | |
| 2275 ->SetParameters(parameters); | |
| 2276 } | |
| 2277 | |
| 2278 JOW(jobject, RtpReceiver_nativeGetParameters) | |
| 2279 (JNIEnv* jni, jclass, jlong j_rtp_sender_pointer) { | |
| 2280 webrtc::RtpParameters parameters = | |
| 2281 reinterpret_cast<RtpReceiverInterface*>(j_rtp_sender_pointer) | |
| 2282 ->GetParameters(); | |
| 2283 return JsepRtpParametersToJavaRtpParameters(jni, parameters); | |
| 2284 } | |
| 2285 | |
| 2286 JOW(jstring, RtpReceiver_nativeId)( | |
| 2287 JNIEnv* jni, jclass, jlong j_rtp_receiver_pointer) { | |
| 2288 return JavaStringFromStdString( | |
| 2289 jni, | |
| 2290 reinterpret_cast<RtpReceiverInterface*>(j_rtp_receiver_pointer)->id()); | |
| 2291 } | |
| 2292 | |
| 2293 JOW(void, RtpReceiver_free)(JNIEnv* jni, jclass, jlong j_rtp_receiver_pointer) { | |
| 2294 reinterpret_cast<RtpReceiverInterface*>(j_rtp_receiver_pointer)->Release(); | |
| 2295 } | |
| 2296 | |
| 2297 } // namespace webrtc_jni | |
| OLD | NEW |