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