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

Side by Side Diff: webrtc/api/android/jni/peerconnection_jni.cc

Issue 2513723002: Created a java wrapper for the callback OnAddTrack to PeerConnection.Observer (Closed)
Patch Set: Create java wrapper for callback. Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright 2013 The WebRTC project authors. All Rights Reserved. 2 * Copyright 2013 The WebRTC project authors. All Rights Reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 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 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 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after
166 // the below methods and we only override one of them. 166 // the below methods and we only override one of them.
167 // TODO(deadbeef): Remove once there's only one version of the methods. 167 // TODO(deadbeef): Remove once there's only one version of the methods.
168 using PeerConnectionObserver::OnAddStream; 168 using PeerConnectionObserver::OnAddStream;
169 using PeerConnectionObserver::OnRemoveStream; 169 using PeerConnectionObserver::OnRemoveStream;
170 using PeerConnectionObserver::OnDataChannel; 170 using PeerConnectionObserver::OnDataChannel;
171 171
172 PCOJava(JNIEnv* jni, jobject j_observer) 172 PCOJava(JNIEnv* jni, jobject j_observer)
173 : j_observer_global_(jni, j_observer), 173 : j_observer_global_(jni, j_observer),
174 j_observer_class_(jni, GetObjectClass(jni, *j_observer_global_)), 174 j_observer_class_(jni, GetObjectClass(jni, *j_observer_global_)),
175 j_media_stream_class_(jni, FindClass(jni, "org/webrtc/MediaStream")), 175 j_media_stream_class_(jni, FindClass(jni, "org/webrtc/MediaStream")),
176 j_media_stream_ctor_(GetMethodID( 176 j_media_stream_ctor_(
177 jni, *j_media_stream_class_, "<init>", "(J)V")), 177 GetMethodID(jni, *j_media_stream_class_, "<init>", "(J)V")),
178 j_audio_track_class_(jni, FindClass(jni, "org/webrtc/AudioTrack")), 178 j_audio_track_class_(jni, FindClass(jni, "org/webrtc/AudioTrack")),
179 j_audio_track_ctor_(GetMethodID( 179 j_audio_track_ctor_(
180 jni, *j_audio_track_class_, "<init>", "(J)V")), 180 GetMethodID(jni, *j_audio_track_class_, "<init>", "(J)V")),
181 j_video_track_class_(jni, FindClass(jni, "org/webrtc/VideoTrack")), 181 j_video_track_class_(jni, FindClass(jni, "org/webrtc/VideoTrack")),
182 j_video_track_ctor_(GetMethodID( 182 j_video_track_ctor_(
183 jni, *j_video_track_class_, "<init>", "(J)V")), 183 GetMethodID(jni, *j_video_track_class_, "<init>", "(J)V")),
184 j_data_channel_class_(jni, FindClass(jni, "org/webrtc/DataChannel")), 184 j_data_channel_class_(jni, FindClass(jni, "org/webrtc/DataChannel")),
185 j_data_channel_ctor_(GetMethodID( 185 j_data_channel_ctor_(
186 jni, *j_data_channel_class_, "<init>", "(J)V")) { 186 GetMethodID(jni, *j_data_channel_class_, "<init>", "(J)V")),
187 } 187 j_rtp_receiver_class_(jni, FindClass(jni, "org/webrtc/RtpReceiver")),
188 j_rtp_receiver_ctor_(
189 GetMethodID(jni, *j_rtp_receiver_class_, "<init>", "(J)V")) {}
188 190
189 virtual ~PCOJava() { 191 virtual ~PCOJava() {
190 ScopedLocalRefFrame local_ref_frame(jni()); 192 ScopedLocalRefFrame local_ref_frame(jni());
191 while (!remote_streams_.empty()) 193 while (!remote_streams_.empty())
192 DisposeRemoteStream(remote_streams_.begin()); 194 DisposeRemoteStream(remote_streams_.begin());
193 } 195 }
194 196
195 void OnIceCandidate(const IceCandidateInterface* candidate) override { 197 void OnIceCandidate(const IceCandidateInterface* candidate) override {
196 ScopedLocalRefFrame local_ref_frame(jni()); 198 ScopedLocalRefFrame local_ref_frame(jni());
197 std::string sdp; 199 std::string sdp;
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after
367 } 369 }
368 370
369 void OnRenegotiationNeeded() override { 371 void OnRenegotiationNeeded() override {
370 ScopedLocalRefFrame local_ref_frame(jni()); 372 ScopedLocalRefFrame local_ref_frame(jni());
371 jmethodID m = 373 jmethodID m =
372 GetMethodID(jni(), *j_observer_class_, "onRenegotiationNeeded", "()V"); 374 GetMethodID(jni(), *j_observer_class_, "onRenegotiationNeeded", "()V");
373 jni()->CallVoidMethod(*j_observer_global_, m); 375 jni()->CallVoidMethod(*j_observer_global_, m);
374 CHECK_EXCEPTION(jni()) << "error during CallVoidMethod"; 376 CHECK_EXCEPTION(jni()) << "error during CallVoidMethod";
375 } 377 }
376 378
379 void OnAddTrack(
380 rtc::scoped_refptr<RtpReceiverInterface> receiver,
381 std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams) override {
382 ScopedLocalRefFrame local_ref_frame(jni());
383 jobject j_rtp_receiver = jni()->NewObject(
384 *j_rtp_receiver_class_, j_rtp_receiver_ctor_, (jlong)receiver.get());
385 CHECK_EXCEPTION(jni()) << "error during NewObject";
Taylor Brandstetter 2016/11/17 23:02:53 Need to call receiver->AddRef() after this, like i
Zhi Huang 2016/11/18 02:22:11 Good catch! This could be a potential problem.
386
387 jobjectArray j_stream_array = ToJavaMediaStreamArray(jni(), streams);
388 jmethodID m =
389 GetMethodID(jni(), *j_observer_class_, "onAddTrack",
390 "(Lorg/webrtc/RtpReceiver;[Lorg/webrtc/MediaStream;)V");
391 jni()->CallVoidMethod(*j_observer_global_, m, j_rtp_receiver,
392 j_stream_array);
393 CHECK_EXCEPTION(jni()) << "Error during CallVoidMethod";
394 }
395
377 void SetConstraints(ConstraintsWrapper* constraints) { 396 void SetConstraints(ConstraintsWrapper* constraints) {
378 RTC_CHECK(!constraints_.get()) << "constraints already set!"; 397 RTC_CHECK(!constraints_.get()) << "constraints already set!";
379 constraints_.reset(constraints); 398 constraints_.reset(constraints);
380 } 399 }
381 400
382 const ConstraintsWrapper* constraints() { return constraints_.get(); } 401 const ConstraintsWrapper* constraints() { return constraints_.get(); }
383 402
384 private: 403 private:
385 typedef std::map<MediaStreamInterface*, jobject> NativeToJavaStreamsMap; 404 typedef std::map<MediaStreamInterface*, jobject> NativeToJavaStreamsMap;
386 405
(...skipping 29 matching lines...) Expand all
416 jobjectArray java_candidates = 435 jobjectArray java_candidates =
417 jni->NewObjectArray(candidates.size(), candidate_class, NULL); 436 jni->NewObjectArray(candidates.size(), candidate_class, NULL);
418 int i = 0; 437 int i = 0;
419 for (const cricket::Candidate& candidate : candidates) { 438 for (const cricket::Candidate& candidate : candidates) {
420 jobject j_candidate = ToJavaCandidate(jni, &candidate_class, candidate); 439 jobject j_candidate = ToJavaCandidate(jni, &candidate_class, candidate);
421 jni->SetObjectArrayElement(java_candidates, i++, j_candidate); 440 jni->SetObjectArrayElement(java_candidates, i++, j_candidate);
422 } 441 }
423 return java_candidates; 442 return java_candidates;
424 } 443 }
425 444
445 jobjectArray ToJavaMediaStreamArray(
446 JNIEnv* jni,
447 std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams) {
448 jobjectArray java_streams =
449 jni->NewObjectArray(streams.size(), *j_media_stream_class_, nullptr);
450 int i = 0;
451 for (rtc::scoped_refptr<MediaStreamInterface> stream : streams) {
452 NativeToJavaStreamsMap::iterator it = remote_streams_.find(stream);
453 if (it != remote_streams_.end()) {
454 jobject j_stream = it->second;
455 CHECK_EXCEPTION(jni) << "error during NewObject";
Taylor Brandstetter 2016/11/17 23:02:53 I think this CHECK_EXCEPTION needs to be right aft
Zhi Huang 2016/11/18 02:22:10 Done.
456 jni->SetObjectArrayElement(java_streams, i++, j_stream);
457 }
Taylor Brandstetter 2016/11/17 23:02:53 There should be an "else" with an RTC_DCHECK.
Zhi Huang 2016/11/18 02:22:10 What about always creating new stream if we cannot
458 }
459 return java_streams;
460 }
461
426 JNIEnv* jni() { 462 JNIEnv* jni() {
427 return AttachCurrentThreadIfNeeded(); 463 return AttachCurrentThreadIfNeeded();
428 } 464 }
429 465
430 const ScopedGlobalRef<jobject> j_observer_global_; 466 const ScopedGlobalRef<jobject> j_observer_global_;
431 const ScopedGlobalRef<jclass> j_observer_class_; 467 const ScopedGlobalRef<jclass> j_observer_class_;
432 const ScopedGlobalRef<jclass> j_media_stream_class_; 468 const ScopedGlobalRef<jclass> j_media_stream_class_;
433 const jmethodID j_media_stream_ctor_; 469 const jmethodID j_media_stream_ctor_;
434 const ScopedGlobalRef<jclass> j_audio_track_class_; 470 const ScopedGlobalRef<jclass> j_audio_track_class_;
435 const jmethodID j_audio_track_ctor_; 471 const jmethodID j_audio_track_ctor_;
436 const ScopedGlobalRef<jclass> j_video_track_class_; 472 const ScopedGlobalRef<jclass> j_video_track_class_;
437 const jmethodID j_video_track_ctor_; 473 const jmethodID j_video_track_ctor_;
438 const ScopedGlobalRef<jclass> j_data_channel_class_; 474 const ScopedGlobalRef<jclass> j_data_channel_class_;
439 const jmethodID j_data_channel_ctor_; 475 const jmethodID j_data_channel_ctor_;
476 const ScopedGlobalRef<jclass> j_rtp_receiver_class_;
477 const jmethodID j_rtp_receiver_ctor_;
440 // C++ -> Java remote streams. The stored jobects are global refs and must be 478 // C++ -> Java remote streams. The stored jobects are global refs and must be
441 // manually deleted upon removal. Use DisposeRemoteStream(). 479 // manually deleted upon removal. Use DisposeRemoteStream().
442 NativeToJavaStreamsMap remote_streams_; 480 NativeToJavaStreamsMap remote_streams_;
443 std::unique_ptr<ConstraintsWrapper> constraints_; 481 std::unique_ptr<ConstraintsWrapper> constraints_;
444 }; 482 };
445 483
446 // Wrapper for a Java MediaConstraints object. Copies all needed data so when 484 // Wrapper for a Java MediaConstraints object. Copies all needed data so when
447 // the constructor returns the Java object is no longer needed. 485 // the constructor returns the Java object is no longer needed.
448 class ConstraintsWrapper : public MediaConstraintsInterface { 486 class ConstraintsWrapper : public MediaConstraintsInterface {
449 public: 487 public:
(...skipping 1456 matching lines...) Expand 10 before | Expand all | Expand 10 after
1906 jclass j_array_list_class = FindClass(jni, "java/util/ArrayList"); 1944 jclass j_array_list_class = FindClass(jni, "java/util/ArrayList");
1907 jmethodID j_array_list_ctor = 1945 jmethodID j_array_list_ctor =
1908 GetMethodID(jni, j_array_list_class, "<init>", "()V"); 1946 GetMethodID(jni, j_array_list_class, "<init>", "()V");
1909 jmethodID j_array_list_add = 1947 jmethodID j_array_list_add =
1910 GetMethodID(jni, j_array_list_class, "add", "(Ljava/lang/Object;)Z"); 1948 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); 1949 jobject j_receivers = jni->NewObject(j_array_list_class, j_array_list_ctor);
1912 CHECK_EXCEPTION(jni) << "error during NewObject"; 1950 CHECK_EXCEPTION(jni) << "error during NewObject";
1913 1951
1914 jclass j_rtp_receiver_class = FindClass(jni, "org/webrtc/RtpReceiver"); 1952 jclass j_rtp_receiver_class = FindClass(jni, "org/webrtc/RtpReceiver");
1915 jmethodID j_rtp_receiver_ctor = 1953 jmethodID j_rtp_receiver_ctor =
1916 GetMethodID(jni, j_rtp_receiver_class, "<init>", "(J)V"); 1954 GetMethodID(jni, j_rtp_receiver_class, "<init>", "(J)V");
Taylor Brandstetter 2016/11/17 23:02:53 Can now use j_rtp_receiver_class_ and j_rtp_receiv
Zhi Huang 2016/11/18 02:22:11 They are not in the same scope
1917 1955
1918 auto receivers = ExtractNativePC(jni, j_pc)->GetReceivers(); 1956 auto receivers = ExtractNativePC(jni, j_pc)->GetReceivers();
1919 for (const auto& receiver : receivers) { 1957 for (const auto& receiver : receivers) {
1920 jlong nativeReceiverPtr = jlongFromPointer(receiver.get()); 1958 jlong nativeReceiverPtr = jlongFromPointer(receiver.get());
1921 jobject j_receiver = jni->NewObject(j_rtp_receiver_class, 1959 jobject j_receiver = jni->NewObject(j_rtp_receiver_class,
1922 j_rtp_receiver_ctor, nativeReceiverPtr); 1960 j_rtp_receiver_ctor, nativeReceiverPtr);
1923 CHECK_EXCEPTION(jni) << "error during NewObject"; 1961 CHECK_EXCEPTION(jni) << "error during NewObject";
1924 // Receiver is now owned by Java object, and will be freed from there. 1962 // Receiver is now owned by Java object, and will be freed from there.
1925 receiver->AddRef(); 1963 receiver->AddRef();
1926 jni->CallBooleanMethod(j_receivers, j_array_list_add, j_receiver); 1964 jni->CallBooleanMethod(j_receivers, j_array_list_add, j_receiver);
(...skipping 478 matching lines...) Expand 10 before | Expand all | Expand 10 after
2405 return JavaStringFromStdString( 2443 return JavaStringFromStdString(
2406 jni, 2444 jni,
2407 reinterpret_cast<RtpReceiverInterface*>(j_rtp_receiver_pointer)->id()); 2445 reinterpret_cast<RtpReceiverInterface*>(j_rtp_receiver_pointer)->id());
2408 } 2446 }
2409 2447
2410 JOW(void, RtpReceiver_free)(JNIEnv* jni, jclass, jlong j_rtp_receiver_pointer) { 2448 JOW(void, RtpReceiver_free)(JNIEnv* jni, jclass, jlong j_rtp_receiver_pointer) {
2411 reinterpret_cast<RtpReceiverInterface*>(j_rtp_receiver_pointer)->Release(); 2449 reinterpret_cast<RtpReceiverInterface*>(j_rtp_receiver_pointer)->Release();
2412 } 2450 }
2413 2451
2414 } // namespace webrtc_jni 2452 } // namespace webrtc_jni
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698