(FRONT) FRONT (2025)

Observer.  Return to WebRTC service  

   1:  package com.voipplus.mmsclient.WebRTC;
   2:  
   3:  import android.util.Log;
   4:  
   5:  import java.nio.ByteBuffer;
   6:  
   7:  import livekit.org.webrtc.*;
   8:  
   9:  public class CustomPeerConnectionObserver implements PeerConnection.Observer {
  10:  
  11:      private static final String TAG = "CustomPeerConnectionObserver";
  12:      private final WebRTCService webRTCService;
  13:  
  14:      public CustomPeerConnectionObserver(WebRTCService webRTCService) {
  15:          this.webRTCService = webRTCService;
  16:      }
  17:  
  18:      // <editor-fold desc="Signaling State Management">
  19:      @Override
  20:      public void onSignalingChange(PeerConnection.SignalingState signalingState) {
  21:          WebRTCUtils.debugInfo(TAG,"🔄 Signaling state changed: " + signalingState, webRTCService.getWebRTCState(), webRTCService.safeGetPeerConnection(), webRTCService.safeGetDataChannel(), webRTCService.safeGetWebSocket(), webRTCService.getSafeSignalinServerUrl());
  22:          webRTCService.updateWebRTCState(signalingState);
  23:      }
  24:      // </editor-fold>
  25:  
  26:      // <editor-fold desc="ICE (Interactive Connectivity Establishment) Events">
  27:      @Override
  28:      public void onIceConnectionChange(PeerConnection.IceConnectionState iceConnectionState) {
  29:          WebRTCUtils.debugInfo(TAG,"🧊 ICE connection state changed: " + iceConnectionState, webRTCService.getWebRTCState(), webRTCService.safeGetPeerConnection(), webRTCService.safeGetDataChannel(), webRTCService.safeGetWebSocket(), webRTCService.getSafeSignalinServerUrl());
  30:          webRTCService.updateWebRTCState(iceConnectionState);
  31:      }
  32:  
  33:      @Override
  34:      public void onIceConnectionReceivingChange(boolean receiving) {
  35:          WebRTCUtils.debugInfo(TAG,"📡 ICE connection receiving change: " + receiving, webRTCService.getWebRTCState(), webRTCService.safeGetPeerConnection(), webRTCService.safeGetDataChannel(), webRTCService.safeGetWebSocket(), webRTCService.getSafeSignalinServerUrl());
  36:      }
  37:  
  38:      @Override
  39:      public void onIceGatheringChange(PeerConnection.IceGatheringState iceGatheringState) {
  40:          WebRTCUtils.debugInfo(TAG, "❄️ ICE gathering state changed: " + iceGatheringState, webRTCService.getWebRTCState(), webRTCService.safeGetPeerConnection(), webRTCServicce.safeGetDataChannel(), webRTCService.safeGetWebSocket(), webRTCService.getSafeSignalinServerUrl());
  41:      }
  42:  
  43:      @Override
  44:      public void onIceCandidate(IceCandidate iceCandidate) {
  45:          WebRTCUtils.debugInfo(TAG, "🔍 ICE candidate gathered: " + iceCandidate.sdp, webRTCService.getWebRTCState(), webRTCService.safeGetPeerConnection(), webRTCService.safeGetDataChannel(), webRTCService.safeGetWebSocket(), webRTCService.getSafeSignalinServerUrl());
  46:          if (webRTCService != null) {
  47:              webRTCService.sendIceCandidateToSignalingServer(iceCandidate);
  48:          }
  49:      }
  50:  
  51:      @Override
  52:      public void onIceCandidateError(IceCandidateErrorEvent event) {
  53:          WebRTCUtils.debugInfo(TAG, "❌ ICE candidate error: " + event.errorText, webRTCService.getWebRTCState(), webRTCService.safeGetPeerConnection(), webRTCService.safeGetDataChannel(), webRTCService.safeGetWebSocket(), webRTCService.getSafeSignalinServerUrl());
  54:      }
  55:  
  56:      @Override
  57:      public void onIceCandidatesRemoved(IceCandidate[] iceCandidates) {
  58:          WebRTCUtils.debugInfo(TAG,"🗑️ ICE candidates removed: " + iceCandidates.length, webRTCService.getWebRTCState(), webRTCService.safeGetPeerConnection(), webRTCService.ssafeGetDataChannel(), webRTCService.safeGetWebSocket(), webRTCService.getSafeSignalinServerUrl());
  59:      }
  60:  
  61:      @Override
  62:      public void onSelectedCandidatePairChanged(CandidatePairChangeEvent event) {
  63:          WebRTCUtils.debugInfo(TAG, "🔄 Selected candidate pair changed: " + event, webRTCService.getWebRTCState(), webRTCService.safeGetPeerConnection(), webRTCService.safeGetDataChannel(), webRTCService.safeGetWebSocket(), webRTCService.getSafeSignalinServerUrl());
  64:      }
  65:  
  66:      @Override
  67:      public void onStandardizedIceConnectionChange(PeerConnection.IceConnectionState newState) {
  68:          WebRTCUtils.debugInfo(TAG, "🌍 Standardized ICE connection state changed: " + newState, webRTCService.getWebRTCState(), webRTCService.safeGetPeerConnection(), webRTCService.safeGetDataChannel(), webRTCService.safeGetWebSocket(), webRTCService.getSafeSignalinServerUrl());
  69:      }
  70:      // </editor-fold>
  71:  
  72:      // <editor-fold desc="Media Stream Management">
  73:      @Override
  74:      public void onAddStream(MediaStream mediaStream) {
  75:          WebRTCUtils.debugInfo(TAG, "🎥 Media stream added: " + mediaStream.getId(), webRTCService.getWebRTCState(), webRTCService.safeGetPeerConnection(), webRTCService.safeGetDataChannel(), webRTCService.safeGetWebSocket(), webRTCService.getSafeSignalinServerUrl());
  76:  
  77:          AudioTrack audioTrack = !mediaStream.audioTracks.isEmpty() ? mediaStream.audioTracks.get(0) : null;
  78:          VideoTrack videoTrack = !mediaStream.videoTracks.isEmpty() ? mediaStream.videoTracks.get(0) : null;
  79:  
  80:          webRTCService.notifyMediaStreamAdded(audioTrack, videoTrack);
  81:      }
  82:  
  83:      @Override
  84:      public void onRemoveStream(MediaStream mediaStream) {
  85:          WebRTCUtils.debugInfo(TAG, "🛑 Media stream removed: " + mediaStream.getId(), webRTCService.getWebRTCState(), webRTCService.safeGetPeerConnection(), webRTCService.safeGetDataChannel(), webRTCService.safeGetWebSocket(), webRTCService.getSafeSignalinServerUrl());
  86:      }
  87:  
  88:      @Override
  89:      public void onTrack(RtpTransceiver transceiver) {
  90:          MediaStreamTrack track = transceiver.getReceiver().track();
  91:          WebRTCUtils.debugInfo(TAG, "🎙️ Track received: " + track.id(), webRTCService.getWebRTCState(), webRTCService.safeGetPeerConnection(), webRTCService.safeGetDataChannell(), webRTCService.safeGetWebSocket(), webRTCService.getSafeSignalinServerUrl());
  92:  
  93:          if (track instanceof AudioTrack) {
  94:              webRTCService.notifyMediaStreamAdded((AudioTrack) track, null);
  95:          } else if (track instanceof VideoTrack) {
  96:              webRTCService.notifyMediaStreamAdded(null, (VideoTrack) track);
  97:          }
  98:      }
  99:  
 100:      @Override
 101:      public void onAddTrack(RtpReceiver receiver, MediaStream[] mediaStreams) {
 102:          WebRTCUtils.debugInfo(TAG, "🎵 Track added: " + receiver.id(), webRTCService.getWebRTCState(), webRTCService.safeGetPeerConnection(), webRTCService.safeGetDataChannel(), webRTCService.safeGetWebSocket(), webRTCService.getSafeSignalinServerUrl());
 103:      }
 104:  
 105:      @Override
 106:      public void onRemoveTrack(RtpReceiver receiver) {
 107:          WebRTCUtils.debugInfo(TAG,"🚫 Track removed: " + receiver.id(), webRTCService.getWebRTCState(), webRTCService.safeGetPeerConnection(), webRTCService.safeGetDataChannel(), webRTCService.safeGetWebSocket(), webRTCService.getSafeSignalinServerUrl());
 108:      }
 109:      // </editor-fold>
 110:  
 111:      // <editor-fold desc="DataChannel Management">
 112:      @Override
 113:      public void onDataChannel(DataChannel dataChannel) {
 114:          WebRTCUtils.debugInfo(TAG, "✅ DataChannel opened: " + dataChannel.label(), webRTCService.getWebRTCState(), webRTCService.safeGetPeerConnection(), webRTCService.safeGetDataChannel(), webRTCService.safeGetWebSocket(), webRTCService.getSafeSignalinServerUrl());
 115:  
 116:          if (webRTCService == null) {
 117:              Log.e(TAG, "❌ WebRTCService is NULL, cannot handle DataChannel.");
 118:              return;
 119:          }
 120:  
 121:          webRTCService.getDataChannelManager().setDataChannel(dataChannel);
 122:          webRTCService.updateWebRTCState(WebRTCState.ESTABLISHED);
 123:  
 124:          dataChannel.registerObserver(new DataChannel.Observer() {
 125:              @Override
 126:              public void onBufferedAmountChange(long previousAmount) {
 127:                  WebRTCUtils.debugInfo(TAG, "📡 DataChannel buffer changed: " + previousAmount, webRTCService.getWebRTCState(), webRTCService.safeGetPeerConnection(), webRTCService.safeGetDataChannel(), webRTCService.safeGetWebSocket(), webRTCService.getSafeSignalinServerUrl());
 128:              }
 129:  
 130:              @Override
 131:              public void onStateChange() {
 132:                  WebRTCUtils.debugInfo(TAG,"📡 DataChannel state changed: " + dataChannel.state(), webRTCService.getWebRTCState(), webRTCService.safeGetPeerConnection(), webRTCService.safeGetDataChannel(), webRTCService.safeGetWebSocket(), webRTCService.getSafeSignalinServerUrl());
 133:                  if (dataChannel.state() == DataChannel.State.OPEN) {
 134:                      WebRTCUtils.debugInfo(TAG, "✅ DataChannel is now OPEN.", webRTCService.getWebRTCState(), webRTCService.safeGetPeerConnection(), webRTCService.safeGetDataChannel(), webRTCService.safeGetWebSocket(), webRTCService.getSafeSignalinServerUrl());
 135:                      dataChannel.send(new DataChannel.Buffer(
 136:                              ByteBuffer.wrap("Hello from Android".getBytes()), false
 137:                      ));
 138:                  }
 139:              }
 140:  
 141:              @Override
 142:              public void onMessage(DataChannel.Buffer buffer) {
 143:                  ByteBuffer data = buffer.data;
 144:                  byte[] bytes = new byte[data.remaining()];
 145:                  data.get(bytes);
 146:                  String message = new String(bytes, java.nio.charset.StandardCharsets.UTF_8);
 147:                  WebRTCUtils.debugInfo(TAG, "📩 DataChannel Message Received: " + message, webRTCService.getWebRTCState(), webRTCService.safeGetPeerConnection(), webRTCService.safeGetDataChannel(), webRTCService.safeGetWebSocket(), webRTCService.getSafeSignalinServerUrl());
 148:              }
 149:  
 150:          });
 151:      }
 152:      // </editor-fold>
 153:  
 154:      // <editor-fold desc="Peer Connection Management">
 155:      @Override
 156:      public void onConnectionChange(PeerConnection.PeerConnectionState newState) {
 157:          WebRTCUtils.debugInfo(TAG, "🔄 Peer connection state changed: " + newState, webRTCService.getWebRTCState(), webRTCService.safeGetPeerConnection(), webRTCService.safeGetDataChannel(), webRTCService.safeGetWebSocket(), webRTCService.getSafeSignalinServerUrl());
 158:          webRTCService.updatePeerConnectionState(newState);
 159:      }
 160:  
 161:      @Override
 162:      public void onRenegotiationNeeded() {
 163:          WebRTCUtils.debugInfo(TAG, "🔄 Renegotiation needed", webRTCService.getWebRTCState(), webRTCService.safeGetPeerConnection(), webRTCService.safeGetDataChannel(), webRTCService.safeGetWebSocket(), webRTCService.getSafeSignalinServerUrl());
 164:          webRTCService.startRenegotiation();
 165:      }
 166:      // </editor-fold>
 167:  }
 168:  

Return to WebRTC service




Android context:




Testing context:



Comments ( )
Link to this page: http://www.vb-net.com/AndroidMosaic/Observer.htm
< THANKS ME>