1 /* 2 * Copyright 2017 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 #ifndef SDK_ANDROID_SRC_JNI_PC_PEER_CONNECTION_H_ 12 #define SDK_ANDROID_SRC_JNI_PC_PEER_CONNECTION_H_ 13 14 #include <map> 15 #include <memory> 16 #include <vector> 17 18 #include "api/peer_connection_interface.h" 19 #include "pc/media_stream_observer.h" 20 #include "sdk/android/src/jni/jni_helpers.h" 21 #include "sdk/android/src/jni/pc/media_constraints.h" 22 #include "sdk/android/src/jni/pc/media_stream.h" 23 #include "sdk/android/src/jni/pc/rtp_receiver.h" 24 #include "sdk/android/src/jni/pc/rtp_transceiver.h" 25 26 namespace webrtc { 27 namespace jni { 28 29 void JavaToNativeRTCConfiguration( 30 JNIEnv* jni, 31 const JavaRef<jobject>& j_rtc_config, 32 PeerConnectionInterface::RTCConfiguration* rtc_config); 33 34 rtc::KeyType GetRtcConfigKeyType(JNIEnv* env, 35 const JavaRef<jobject>& j_rtc_config); 36 37 ScopedJavaLocalRef<jobject> NativeToJavaAdapterType(JNIEnv* env, 38 int adapterType); 39 40 // Adapter between the C++ PeerConnectionObserver interface and the Java 41 // PeerConnection.Observer interface. Wraps an instance of the Java interface 42 // and dispatches C++ callbacks to Java. 43 class PeerConnectionObserverJni : public PeerConnectionObserver { 44 public: 45 PeerConnectionObserverJni(JNIEnv* jni, const JavaRef<jobject>& j_observer); 46 ~PeerConnectionObserverJni() override; 47 48 // Implementation of PeerConnectionObserver interface, which propagates 49 // the callbacks to the Java observer. 50 void OnIceCandidate(const IceCandidateInterface* candidate) override; 51 void OnIceCandidateError(const std::string& address, 52 int port, 53 const std::string& url, 54 int error_code, 55 const std::string& error_text) override; 56 57 void OnIceCandidatesRemoved( 58 const std::vector<cricket::Candidate>& candidates) override; 59 void OnSignalingChange( 60 PeerConnectionInterface::SignalingState new_state) override; 61 void OnIceConnectionChange( 62 PeerConnectionInterface::IceConnectionState new_state) override; 63 void OnStandardizedIceConnectionChange( 64 PeerConnectionInterface::IceConnectionState new_state) override; 65 void OnConnectionChange( 66 PeerConnectionInterface::PeerConnectionState new_state) override; 67 void OnIceConnectionReceivingChange(bool receiving) override; 68 void OnIceGatheringChange( 69 PeerConnectionInterface::IceGatheringState new_state) override; 70 void OnIceSelectedCandidatePairChanged( 71 const cricket::CandidatePairChangeEvent& event) override; 72 void OnAddStream(rtc::scoped_refptr<MediaStreamInterface> stream) override; 73 void OnRemoveStream(rtc::scoped_refptr<MediaStreamInterface> stream) override; 74 void OnDataChannel(rtc::scoped_refptr<DataChannelInterface> channel) override; 75 void OnRenegotiationNeeded() override; 76 void OnAddTrack(rtc::scoped_refptr<RtpReceiverInterface> receiver, 77 const std::vector<rtc::scoped_refptr<MediaStreamInterface>>& 78 streams) override; 79 void OnTrack( 80 rtc::scoped_refptr<RtpTransceiverInterface> transceiver) override; 81 void OnRemoveTrack( 82 rtc::scoped_refptr<RtpReceiverInterface> receiver) override; 83 84 private: 85 typedef std::map<MediaStreamInterface*, JavaMediaStream> 86 NativeToJavaStreamsMap; 87 typedef std::map<MediaStreamTrackInterface*, RtpReceiverInterface*> 88 NativeMediaStreamTrackToNativeRtpReceiver; 89 90 // If the NativeToJavaStreamsMap contains the stream, return it. 91 // Otherwise, create a new Java MediaStream. Returns a global jobject. 92 JavaMediaStream& GetOrCreateJavaStream( 93 JNIEnv* env, 94 const rtc::scoped_refptr<MediaStreamInterface>& stream); 95 96 // Converts array of streams, creating or re-using Java streams as necessary. 97 ScopedJavaLocalRef<jobjectArray> NativeToJavaMediaStreamArray( 98 JNIEnv* jni, 99 const std::vector<rtc::scoped_refptr<MediaStreamInterface>>& streams); 100 101 const ScopedJavaGlobalRef<jobject> j_observer_global_; 102 103 // C++ -> Java remote streams. 104 NativeToJavaStreamsMap remote_streams_; 105 std::vector<JavaRtpReceiverGlobalOwner> rtp_receivers_; 106 // Holds a reference to the Java transceivers given to the AddTrack 107 // callback, so that the shared ownership by the Java object will be 108 // properly disposed. 109 std::vector<JavaRtpTransceiverGlobalOwner> rtp_transceivers_; 110 }; 111 112 // PeerConnection doesn't take ownership of the observer. In Java API, we don't 113 // want the client to have to manually dispose the observer. To solve this, this 114 // wrapper class is used for object ownership. 115 // 116 // Also stores reference to the deprecated PeerConnection constraints for now. 117 class OwnedPeerConnection { 118 public: 119 OwnedPeerConnection( 120 rtc::scoped_refptr<PeerConnectionInterface> peer_connection, 121 std::unique_ptr<PeerConnectionObserver> observer); 122 // Deprecated. PC constraints are deprecated. 123 OwnedPeerConnection( 124 rtc::scoped_refptr<PeerConnectionInterface> peer_connection, 125 std::unique_ptr<PeerConnectionObserver> observer, 126 std::unique_ptr<MediaConstraints> constraints); 127 ~OwnedPeerConnection(); 128 pc()129 PeerConnectionInterface* pc() const { return peer_connection_.get(); } constraints()130 const MediaConstraints* constraints() const { return constraints_.get(); } 131 132 private: 133 rtc::scoped_refptr<PeerConnectionInterface> peer_connection_; 134 std::unique_ptr<PeerConnectionObserver> observer_; 135 std::unique_ptr<MediaConstraints> constraints_; 136 }; 137 138 } // namespace jni 139 } // namespace webrtc 140 141 #endif // SDK_ANDROID_SRC_JNI_PC_PEER_CONNECTION_H_ 142