Peer. Return to WebRTC service
1: package com.voipplus.mmsclient.WebRTC;
2:
3: import android.util.Log;
4:
5: import java.util.ArrayList;
6: import java.util.List;
7:
8: import livekit.org.webrtc.DataChannel;
9: import livekit.org.webrtc.IceCandidate;
10: import livekit.org.webrtc.MediaConstraints;
11: import livekit.org.webrtc.PeerConnection;
12: import livekit.org.webrtc.PeerConnectionFactory;
13: import livekit.org.webrtc.SdpObserver;
14: import livekit.org.webrtc.SessionDescription;
15:
16: public class PeerConnectionManager {
17: private static final String TAG = "PeerConnectionManager";
18: private PeerConnection peerConnection;
19: private PeerConnectionFactory peerConnectionFactory;
20: private final WebRTCService webRTCService;
21:
22: public PeerConnectionManager(WebRTCService webRTCService) {
23: this.webRTCService = webRTCService;
24: initializePeerConnectionFactory();
25: }
26:
27: // <editor-fold desc="Initialization Methods">
28: private void initializePeerConnectionFactory() {
29: WebRTCUtils.debugInfo(TAG, "🛠️ Initializing PeerConnectionFactory...", webRTCService.getWebRTCState(), peerConnection, webRTCService.safeGetDataChannel(), webRTCService.safeGetWebSocket(), webRTCService.getSafeSignalinServerUrl());
30: PeerConnectionFactory.InitializationOptions options =
31: PeerConnectionFactory.InitializationOptions.builder(webRTCService.getApplicationContext())
32: .setEnableInternalTracer(true)
33: .createInitializationOptions();
34: PeerConnectionFactory.initialize(options);
35:
36: peerConnectionFactory = PeerConnectionFactory.builder().createPeerConnectionFactory();
37: WebRTCUtils.debugInfo(TAG, "✅ PeerConnectionFactory initialized: " + peerConnectionFactory, webRTCService.getWebRTCState(), peerConnection, webRTCService.safeGetDataChannel(), webRTCService.safeGetWebSocket(), webRTCService.getSafeSignalinServerUrl());
38: createPeerConnection();
39: }
40:
41: private void createPeerConnection() {
42: WebRTCUtils.debugInfo(TAG, "🛠️ Creating PeerConnection...", webRTCService.getWebRTCState(), peerConnection, webRTCService.safeGetDataChannel(), webRTCService.safeGetWWebSocket(), webRTCService.getSafeSignalinServerUrl());
43: PeerConnection.RTCConfiguration rtcConfig = new PeerConnection.RTCConfiguration(null);
44: List<PeerConnection.IceServer> iceServers = new ArrayList<>();
45: PeerConnection.IceServer iceServer = PeerConnection.IceServer.builder("stun:stun.l.google.com:19302").createIceServer();
46: iceServers.add(iceServer);
47: rtcConfig.iceServers = iceServers;
48: peerConnection = peerConnectionFactory.createPeerConnection(rtcConfig, new CustomPeerConnectionObserver(webRTCService));
49: WebRTCUtils.debugInfo(TAG, "✅PeerConnection created: " + peerConnection, webRTCService.getWebRTCState(), peerConnection, webRTCService.safeGetDataChannel(), webRTCService.safeGetWebSocket(), webRTCService.getSafeSignalinServerUrl());
50: }
51: // </editor-fold>
52:
53: // <editor-fold desc="Offer/Answer Handling">
54: public void createOffer() {
55: if (peerConnection == null) {
56: WebRTCUtils.debugInfo(TAG, "❌ Cannot create SDP Offer: PeerConnection is null.", webRTCService.getWebRTCState(), peerConnection, webRTCService.safeGetDataChannel(), webRTCService.safeGetWebSocket(), webRTCService.getSafeSignalinServerUrl());
57: return;
58: }
59:
60: DataChannel.Init init = new DataChannel.Init();
61: init.ordered = true; // Ensure ordered messages
62: init.negotiated = false; // Let WebRTC negotiate automatically
63:
64: DataChannel dataChannel = peerConnection.createDataChannel("testDataChannel", init);
65: webRTCService.getDataChannelManager().setDataChannel(dataChannel);
66:
67: webRTCService.updateWebRTCState(WebRTCState.CREATING_OFFER);
68: WebRTCUtils.debugInfo(TAG, "Creating SDP Offer...", webRTCService.getWebRTCState(), peerConnection, webRTCService.safeGetDataChannel(), webRTCService.safeGetWebSocket(), webRTCService.getSafeSignalinServerUrl());
69:
70: peerConnection.createOffer(new SdpObserver() {
71: @Override
72: public void onCreateSuccess(SessionDescription sessionDescription) {
73: WebRTCUtils.debugInfo(TAG, "✅ SDP Offer created: " + sessionDescription.description, webRTCService.getWebRTCState(), peerConnection, webRTCService.safeGetDataChannel(), webRTCService.safeGetWebSocket(), webRTCService.getSafeSignalinServerUrl());
74: peerConnection.setLocalDescription(this, sessionDescription);
75: webRTCService.getSignalingManager().sendSignalingMessage("{"type": "offer", "sdp": "" + sessionDescription.description + ""}");
76: webRTCService.updateWebRTCState(WebRTCState.WAITING_FOR_ANSWER);
77: }
78:
79: @Override
80: public void onCreateFailure(String error) {
81: WebRTCUtils.debugInfo(TAG, "❌ SDP Offer creation failed: " + error, webRTCService.getWebRTCState(), peerConnection, webRTCService.safeGetDataChannel(), webRTCService.safeGetWebSocket(), webRTCService.getSafeSignalinServerUrl());
82: webRTCService.updateWebRTCState(WebRTCState.ERROR);
83: }
84:
85: @Override
86: public void onSetSuccess() {
87: WebRTCUtils.debugInfo(TAG, "onSetSuccess: SDP successfully set.", webRTCService.getWebRTCState(), peerConnection, webRTCService.safeGetDataChannel(), webRTCService.safeGetWebSocket(), webRTCService.getSafeSignalinServerUrl());
88: }
89:
90: @Override
91: public void onSetFailure(String error) {
92: WebRTCUtils.debugInfo(TAG, "❌ Failed to set SDP Offer: " + error, webRTCService.getWebRTCState(), peerConnection, webRTCService.safeGetDataChannel(), webRTCService.safeGetWebSocket(), webRTCService.getSafeSignalinServerUrl());
93: webRTCService.updateWebRTCState(WebRTCState.ERROR);
94: }
95: }, new MediaConstraints());
96: }
97:
98: public void handleRemoteAnswer(SessionDescription remoteSessionDescription) {
99: WebRTCUtils.debugInfo(TAG, "handleRemoteAnswer: Called. remoteSessionDescription=" + remoteSessionDescription, webRTCService.getWebRTCState(), peerConnection, webRTCService.safeGetDataChannel(), webRTCService.safeGetWebSocket(), webRTCService.getSafeSignalinServerUrl());
100:
101: if (webRTCService.getWebRTCState() != WebRTCState.WAITING_FOR_ANSWER) {
102: WebRTCUtils.debugInfo(TAG, "Received answer in unexpected state. State=" + webRTCService.getWebRTCState(), webRTCService.getWebRTCState(), peerConnection, webRTCService.safeGetDataChannel(), webRTCService.safeGetWebSocket(), webRTCService.getSafeSignalinServerUrl());
103: return;
104: }
105:
106: WebRTCUtils.debugInfo(TAG, "Setting remote SDP Answer...", webRTCService.getWebRTCState(), peerConnection, webRTCService.safeGetDataChannel(), webRTCService.safeGetWebSocket(), webRTCService.getSafeSignalinServerUrl());
107: peerConnection.setRemoteDescription(new SdpObserver() {
108: @Override
109: public void onSetSuccess() {
110: WebRTCUtils.debugInfo(TAG, "✅ Remote SDP Answer set successfully. State transitioning to ESTABLISHED.", webRTCService.getWebRTCState(), peerConnection, webRTCService.safeGetDataChannel(), webRTCService.safeGetWebSocket(), webRTCService.getSafeSignalinServerUrl());
111: webRTCService.updateWebRTCState(WebRTCState.ESTABLISHED);
112: }
113:
114: @Override
115: public void onSetFailure(String error) {
116: WebRTCUtils.debugInfo(TAG, "❌ Failed to set Remote SDP Answer: " + error, webRTCService.getWebRTCState(), peerConnection, webRTCService.safeGetDataChannel(), webRTCService.safeGetWebSocket(), webRTCService.getSafeSignalinServerUrl());
117: webRTCService.updateWebRTCState(WebRTCState.ERROR);
118: }
119:
120: @Override
121: public void onCreateSuccess(SessionDescription sessionDescription) {
122: WebRTCUtils.debugInfo(TAG, "onCreateSuccess.", webRTCService.getWebRTCState(), peerConnection, webRTCService.safeGetDataChannel(), webRTCService.safeGetWebSocket(), webRTCService.getSafeSignalinServerUrl());
123: }
124:
125: @Override
126: public void onCreateFailure(String error) {
127: WebRTCUtils.debugInfo(TAG, "onCreateFailure.", webRTCService.getWebRTCState(), peerConnection, webRTCService.safeGetDataChannel(), webRTCService.safeGetWebSocket(), webRTCService.getSafeSignalinServerUrl());
128: }
129: }, remoteSessionDescription);
130: }
131: // </editor-fold>
132:
133: // <editor-fold desc="ICE (Interactive Connectivity Establishment)">
134: public void addIceCandidate(IceCandidate candidate) {
135: WebRTCUtils.debugInfo(TAG, "addIceCandidate: Called. candidate=" + candidate, webRTCService.getWebRTCState(), peerConnection, webRTCService.safeGetDataChannel(), webRTCService.safeGetWebSocket(), webRTCService.getSafeSignalinServerUrl());
136:
137: if (peerConnection != null) {
138: peerConnection.addIceCandidate(candidate);
139: WebRTCUtils.debugInfo(TAG, "✅ Added ICE Candidate: " + candidate.sdp, webRTCService.getWebRTCState(), peerConnection, webRTCService.safeGetDataChannel(), webRTCService.safeGetWebSocket(), webRTCService.getSafeSignalinServerUrl());
140: } else {
141: WebRTCUtils.debugInfo(TAG, "❌ Failed to add ICE Candidate: PeerConnection is null.", webRTCService.getWebRTCState(), peerConnection, webRTCService.safeGetDataChannel(), webRTCService.safeGetWebSocket(), webRTCService.getSafeSignalinServerUrl());
142: }
143: }
144:
145: public void startRenegotiation() {
146: WebRTCUtils.debugInfo(TAG, "startRenegotiation: Attempting to restart ICE", webRTCService.getWebRTCState(), peerConnection, webRTCService.safeGetDataChannel(), webRTCService.safeGetWebSocket(), webRTCService.getSafeSignalinServerUrl());
147: if (peerConnection != null) {
148: peerConnection.restartIce();
149: } else {
150: WebRTCUtils.debugInfo(TAG, "❌ Failed torestart ICE: PeerConnection is null.", webRTCService.getWebRTCState(), peerConnection, webRTCService.safeGetDataChannel(), webRTCService.safeGetWebSocket(), webRTCService.getSafeSignalinServerUrl());
151: }
152: }
153: // </editor-fold>
154:
155: // <editor-fold desc="Connection Management">
156: public void closeConnection() {
157: WebRTCUtils.debugInfo(TAG, "closeConnection: Called.", webRTCService.getWebRTCState(), peerConnection, webRTCService.safeGetDataChannel(), webRTCService.safeGetWebSocket(), webRTCService.getSafeSignalinServerUrl());
158:
159: if (peerConnection != null) {
160: peerConnection.close();
161: peerConnection = null;
162: webRTCService.updateWebRTCState(WebRTCState.ERROR);
163: WebRTCUtils.debugInfo(TAG, "🔴 WebRTC Connection closed.", webRTCService.getWebRTCState(), peerConnection, webRTCService.safeGetDataChannel(), webRTCService.safeGetWebSocket(), webRTCService.getSafeSignalinServerUrl());
164: }
165: }
166:
167: public void updatePeerConnectionState(PeerConnection.PeerConnectionState newState) {
168: WebRTCUtils.debugInfo(TAG, "updatePeerConnectionState: PeerConnectionState=" + newState, webRTCService.getWebRTCState(), peerConnection, webRTCService.safeGetDataChannel(), webRTCService.safeGetWebSocket(), webRTCService.getSafeSignalinServerUrl());
169: if (newState == PeerConnection.PeerConnectionState.CONNECTED) {
170: webRTCService.updateWebRTCState(WebRTCState.ESTABLISHED);
171: }
172: }
173: // </editor-fold>
174:
175: // <editor-fold desc="Accessors">
176: public PeerConnectionFactory getPeerConnectionFactory() {
177: WebRTCUtils.debugInfo(TAG, "getPeerConnectionFactory: Called. Returning peerConnectionFactory=" + peerConnectionFactory, webRTCService.getWebRTCState(), peerConnection, webRTCService.safeGetDataChannel(), webRTCService.safeGetWebSocket(), webRTCService.getSafeSignalinServerUrl());
178: return peerConnectionFactory;
179: }
180:
181: public PeerConnection getPeerConnection() {
182: return peerConnection;
183: }
184: // </editor-fold>
185:
186:
187: public void handleIceCandidate(IceCandidate candidate) {
188: WebRTCUtils.debugInfo(TAG, "📡 Received remote ICE Candidate. Applying...",
189: webRTCService.getWebRTCState(), peerConnection, webRTCService.safeGetDataChannel(), webRTCService.safeGetWebSocket(), webRTCService.getSafeSignalinServerUrl());
190:
191: if (peerConnection == null) {
192: WebRTCUtils.debugInfo(TAG, "❌ PeerConnection is null. Cannot add ICE Candidate.",
193: webRTCService.getWebRTCState(), peerConnection, webRTCService.safeGetDataChannel(), webRTCService.safeGetWebSocket(), webRTCService.getSafeSignalinServerUrl());
194: return;
195: }
196:
197: peerConnection.addIceCandidate(candidate);
198: }
199:
200: }
201:
Return to WebRTC service
Android context:
Testing context:
Comments (
)
)
Link to this page:
http://www.vb-net.com/AndroidMosaic/Peer.htm
|
|