• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 OnIceCandidatesRemoved(
52       const std::vector<cricket::Candidate>& candidates) override;
53   void OnSignalingChange(
54       PeerConnectionInterface::SignalingState new_state) override;
55   void OnIceConnectionChange(
56       PeerConnectionInterface::IceConnectionState new_state) override;
57   void OnStandardizedIceConnectionChange(
58       PeerConnectionInterface::IceConnectionState new_state) override;
59   void OnConnectionChange(
60       PeerConnectionInterface::PeerConnectionState new_state) override;
61   void OnIceConnectionReceivingChange(bool receiving) override;
62   void OnIceGatheringChange(
63       PeerConnectionInterface::IceGatheringState new_state) override;
64   void OnIceSelectedCandidatePairChanged(
65       const cricket::CandidatePairChangeEvent& event) override;
66   void OnAddStream(rtc::scoped_refptr<MediaStreamInterface> stream) override;
67   void OnRemoveStream(rtc::scoped_refptr<MediaStreamInterface> stream) override;
68   void OnDataChannel(rtc::scoped_refptr<DataChannelInterface> channel) override;
69   void OnRenegotiationNeeded() override;
70   void OnAddTrack(rtc::scoped_refptr<RtpReceiverInterface> receiver,
71                   const std::vector<rtc::scoped_refptr<MediaStreamInterface>>&
72                       streams) override;
73   void OnTrack(
74       rtc::scoped_refptr<RtpTransceiverInterface> transceiver) override;
75 
76  private:
77   typedef std::map<MediaStreamInterface*, JavaMediaStream>
78       NativeToJavaStreamsMap;
79   typedef std::map<MediaStreamTrackInterface*, RtpReceiverInterface*>
80       NativeMediaStreamTrackToNativeRtpReceiver;
81 
82   // If the NativeToJavaStreamsMap contains the stream, return it.
83   // Otherwise, create a new Java MediaStream. Returns a global jobject.
84   JavaMediaStream& GetOrCreateJavaStream(
85       JNIEnv* env,
86       const rtc::scoped_refptr<MediaStreamInterface>& stream);
87 
88   // Converts array of streams, creating or re-using Java streams as necessary.
89   ScopedJavaLocalRef<jobjectArray> NativeToJavaMediaStreamArray(
90       JNIEnv* jni,
91       const std::vector<rtc::scoped_refptr<MediaStreamInterface>>& streams);
92 
93   const ScopedJavaGlobalRef<jobject> j_observer_global_;
94 
95   // C++ -> Java remote streams.
96   NativeToJavaStreamsMap remote_streams_;
97   std::vector<JavaRtpReceiverGlobalOwner> rtp_receivers_;
98   // Holds a reference to the Java transceivers given to the AddTrack
99   // callback, so that the shared ownership by the Java object will be
100   // properly disposed.
101   std::vector<JavaRtpTransceiverGlobalOwner> rtp_transceivers_;
102 };
103 
104 // PeerConnection doesn't take ownership of the observer. In Java API, we don't
105 // want the client to have to manually dispose the observer. To solve this, this
106 // wrapper class is used for object ownership.
107 //
108 // Also stores reference to the deprecated PeerConnection constraints for now.
109 class OwnedPeerConnection {
110  public:
111   OwnedPeerConnection(
112       rtc::scoped_refptr<PeerConnectionInterface> peer_connection,
113       std::unique_ptr<PeerConnectionObserver> observer);
114   // Deprecated. PC constraints are deprecated.
115   OwnedPeerConnection(
116       rtc::scoped_refptr<PeerConnectionInterface> peer_connection,
117       std::unique_ptr<PeerConnectionObserver> observer,
118       std::unique_ptr<MediaConstraints> constraints);
119   ~OwnedPeerConnection();
120 
pc()121   PeerConnectionInterface* pc() const { return peer_connection_.get(); }
constraints()122   const MediaConstraints* constraints() const { return constraints_.get(); }
123 
124  private:
125   rtc::scoped_refptr<PeerConnectionInterface> peer_connection_;
126   std::unique_ptr<PeerConnectionObserver> observer_;
127   std::unique_ptr<MediaConstraints> constraints_;
128 };
129 
130 }  // namespace jni
131 }  // namespace webrtc
132 
133 #endif  // SDK_ANDROID_SRC_JNI_PC_PEER_CONNECTION_H_
134