org.webrtc.IceCandidate Java Examples

The following examples show how to use org.webrtc.IceCandidate. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CallActivity.java    From RTCStartupDemo with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void onIceCandidate(IceCandidate iceCandidate) {
    Log.i(TAG, "onIceCandidate: " + iceCandidate);

    try {
        JSONObject message = new JSONObject();
        message.put("userId", RTCSignalClient.getInstance().getUserId());
        message.put("msgType", RTCSignalClient.MESSAGE_TYPE_CANDIDATE);
        message.put("label", iceCandidate.sdpMLineIndex);
        message.put("id", iceCandidate.sdpMid);
        message.put("candidate", iceCandidate.sdp);
        RTCSignalClient.getInstance().sendMessage(message);
    } catch (JSONException e) {
        e.printStackTrace();
    }
}
 
Example #2
Source File: ConferenceClient.java    From owt-client-android with Apache License 2.0 6 votes vote down vote up
@Override
public void onIceCandidate(final String id, final IceCandidate candidate) {
    try {
        JSONObject candidateObj = new JSONObject();
        candidateObj.put("sdpMLineIndex", candidate.sdpMLineIndex);
        candidateObj.put("sdpMid", candidate.sdpMid);
        candidateObj.put("candidate",
                candidate.sdp.indexOf("a=") == 0 ? candidate.sdp : "a=" + candidate.sdp);

        JSONObject candidateMsg = new JSONObject();
        candidateMsg.put("type", "candidate");
        candidateMsg.put("candidate", candidateObj);

        JSONObject msg = new JSONObject();
        msg.put("id", id);
        msg.put("signaling", candidateMsg);

        sendSignalingMessage("soac", msg, null);
    } catch (JSONException e) {
        DCHECK(e);
    }
}
 
Example #3
Source File: WebRtcCallService.java    From mollyim-android with GNU General Public License v3.0 6 votes vote down vote up
private void handleReceivedIceCandidates(Intent intent) {
  CallId     callId       = getCallId(intent);
  Integer    remoteDevice = intent.getIntExtra(EXTRA_REMOTE_DEVICE, -1);
  ArrayList<IceCandidateParcel> iceCandidateParcels = intent.getParcelableArrayListExtra(EXTRA_ICE_CANDIDATES);

  Log.i(TAG, "handleReceivedIceCandidates: id: " + callId.format(remoteDevice) + ", count: " + iceCandidateParcels.size());

  LinkedList<IceCandidate> iceCandidates = new LinkedList();
  for (IceCandidateParcel parcel : iceCandidateParcels) {
    iceCandidates.add(parcel.getIceCandidate());
  }

  try {
    callManager.receivedIceCandidates(callId, remoteDevice, iceCandidates);
  } catch  (CallException e) {
    callFailure("receivedIceCandidates() failed: ", e);
  }
}
 
Example #4
Source File: WebRtcCallService.java    From mollyim-android with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void onSendIceCandidates(CallId callId, Remote remote, Integer remoteDevice, Boolean broadcast, List<IceCandidate> iceCandidates) {
  Log.i(TAG, "onSendIceCandidates: id: " + callId.format(remoteDevice));

  if (remote instanceof RemotePeer) {
    RemotePeer remotePeer = (RemotePeer)remote;
    Intent     intent     = new Intent(this, WebRtcCallService.class);

    ArrayList<IceCandidateParcel> iceCandidateParcels = new ArrayList<>(iceCandidates.size());
    for (IceCandidate iceCandidate : iceCandidates) {
      iceCandidateParcels.add(new IceCandidateParcel(iceCandidate));
    }

    intent.setAction(ACTION_SEND_ICE_CANDIDATES)
          .putExtra(EXTRA_CALL_ID,           callId.longValue())
          .putExtra(EXTRA_REMOTE_PEER,       remotePeer)
          .putExtra(EXTRA_REMOTE_DEVICE,     remoteDevice)
          .putExtra(EXTRA_BROADCAST,         broadcast)
          .putParcelableArrayListExtra(EXTRA_ICE_CANDIDATES, iceCandidateParcels);

    startService(intent);
  } else {
    throw new AssertionError("Received remote is not instanceof RemotePeer");
  }
}
 
Example #5
Source File: PeerConnectionChannel.java    From owt-client-android with Apache License 2.0 6 votes vote down vote up
protected void drainRemoteCandidates() {
    DCHECK(pcExecutor);
    DCHECK(queuedRemoteCandidates);
    synchronized (remoteIceLock) {
        for (final IceCandidate candidate : queuedRemoteCandidates) {
            pcExecutor.execute(() -> {
                if (disposed()) {
                    return;
                }
                Log.d(LOG_TAG, "add ice candidate");
                peerConnection.addIceCandidate(candidate);
                queuedRemoteCandidates.remove(candidate);
            });
        }
    }
}
 
Example #6
Source File: MainActivity.java    From webrtc-android-tutorial with Apache License 2.0 6 votes vote down vote up
private void call() {
    List<PeerConnection.IceServer> iceServers = new ArrayList<>();
    iceServers.add(PeerConnection.IceServer.builder("stun:stun.l.google.com:19302").createIceServer());
    peerConnection = peerConnectionFactory.createPeerConnection(iceServers, new PeerConnectionAdapter("localconnection") {
        @Override
        public void onIceCandidate(IceCandidate iceCandidate) {
            super.onIceCandidate(iceCandidate);
            SignalingClient.get().sendIceCandidate(iceCandidate);
        }

        @Override
        public void onAddStream(MediaStream mediaStream) {
            super.onAddStream(mediaStream);
            VideoTrack remoteVideoTrack = mediaStream.videoTracks.get(0);
            runOnUiThread(() -> {
                remoteVideoTrack.addSink(remoteView);
            });
        }
    });

    peerConnection.addStream(mediaStream);
}
 
Example #7
Source File: PeerConnectionChannel.java    From owt-client-android with Apache License 2.0 6 votes vote down vote up
public void processSignalingMessage(JSONObject data) throws JSONException {
    String signalingType = data.getString("type");
    if (signalingType.equals("candidates")) {
        IceCandidate candidate = new IceCandidate(data.getString("sdpMid"),
                data.getInt("sdpMLineIndex"),
                data.getString("candidate"));
        addOrQueueCandidate(candidate);
    } else if (signalingType.equals("offer") || signalingType.equals("answer")) {
        // When gets an SDP during onError, it means that i'm waiting for peer to re-configure
        // itself to keep compatibility with me.
        if (onError) {
            // Ignore the SDP only once.
            onError = false;
            return;
        }
        String sdpString = data.getString("sdp");
        SessionDescription remoteSdp = new SessionDescription(
                SessionDescription.Type.fromCanonicalForm(signalingType),
                sdpString);
        setRemoteDescription(remoteSdp);
    }
}
 
Example #8
Source File: MainActivity.java    From webrtc-android-tutorial with Apache License 2.0 6 votes vote down vote up
private synchronized PeerConnection getOrCreatePeerConnection(String socketId) {
    PeerConnection peerConnection = peerConnectionMap.get(socketId);
    if(peerConnection != null) {
        return peerConnection;
    }
    peerConnection = peerConnectionFactory.createPeerConnection(iceServers, new PeerConnectionAdapter("PC:" + socketId) {
        @Override
        public void onIceCandidate(IceCandidate iceCandidate) {
            super.onIceCandidate(iceCandidate);
            SignalingClient.get().sendIceCandidate(iceCandidate, socketId);
        }

        @Override
        public void onAddStream(MediaStream mediaStream) {
            super.onAddStream(mediaStream);
            VideoTrack remoteVideoTrack = mediaStream.videoTracks.get(0);
            runOnUiThread(() -> {
                remoteVideoTrack.addSink(remoteViews[remoteViewsIndex++]);
            });
        }
    });
    peerConnection.addStream(mediaStream);
    peerConnectionMap.put(socketId, peerConnection);
    return peerConnection;
}
 
Example #9
Source File: PeerConnectionChannel.java    From owt-client-android with Apache License 2.0 6 votes vote down vote up
private void addOrQueueCandidate(final IceCandidate iceCandidate) {
    if (disposed()) {
        return;
    }
    DCHECK(pcExecutor);
    DCHECK(iceCandidate);
    pcExecutor.execute(() -> {
        if (disposed()) {
            return;
        }
        if (peerConnection.signalingState() == PeerConnection.SignalingState.STABLE) {
            Log.d(LOG_TAG, "add ice candidate");
            peerConnection.addIceCandidate(iceCandidate);
        } else {
            synchronized (remoteIceLock) {
                Log.d(LOG_TAG, "queue ice candidate");
                queuedRemoteCandidates.add(iceCandidate);
            }
        }
    });
}
 
Example #10
Source File: PeersManager.java    From WebRTCapp with Apache License 2.0 6 votes vote down vote up
private void createLocalPeerConnection(MediaConstraints sdpConstraints) {
    final List<PeerConnection.IceServer> iceServers = new ArrayList<>();
    PeerConnection.IceServer iceServer = new PeerConnection.IceServer("stun:stun.l.google.com:19302");
    iceServers.add(iceServer);

    localPeer = peerConnectionFactory.createPeerConnection(iceServers, sdpConstraints, new CustomPeerConnectionObserver("localPeerCreation") {
        @Override
        public void onIceCandidate(IceCandidate iceCandidate) {
            super.onIceCandidate(iceCandidate);
            Map<String, String> iceCandidateParams = new HashMap<>();
            iceCandidateParams.put("sdpMid", iceCandidate.sdpMid);
            iceCandidateParams.put("sdpMLineIndex", Integer.toString(iceCandidate.sdpMLineIndex));
            iceCandidateParams.put("candidate", iceCandidate.sdp);
            if (webSocketAdapter.getUserId() != null) {
                iceCandidateParams.put("endpointName", webSocketAdapter.getUserId());
                webSocketAdapter.sendJson(webSocket, "onIceCandidate", iceCandidateParams);
            } else {
                webSocketAdapter.addIceCandidate(iceCandidateParams);
            }
        }
    });
}
 
Example #11
Source File: WebRtcCallService.java    From bcm-android with GNU General Public License v3.0 6 votes vote down vote up
private void handleRemoteIceCandidate(Intent intent) {
    ALog.w(TAG, "remoteIce candidate...");
    if (WebRtcCallService.accountContext == null || !WebRtcCallService.accountContext.equals(getAccountContextFromIntent(intent))) {
        return;
    }

    if (Util.isEquals(this.callId, getCallId(intent))) {
        IceCandidate candidate = new IceCandidate(intent.getStringExtra(EXTRA_ICE_SDP_MID),
                intent.getIntExtra(EXTRA_ICE_SDP_LINE_INDEX, 0),
                intent.getStringExtra(EXTRA_ICE_SDP));

        ALog.d(TAG, "handleRemoteIceCandidate " + new Gson().toJson(candidate));

        if (peerConnection != null) {
            peerConnection.addIceCandidate(candidate);
        } else if (pendingIncomingIceUpdates != null) {
            pendingIncomingIceUpdates.add(candidate);
        }
    }
}
 
Example #12
Source File: ConferenceClient.java    From owt-client-android with Apache License 2.0 6 votes vote down vote up
@Override
public void onIceCandidatesRemoved(final String id, final IceCandidate[] candidates) {
    try {
        JSONArray removedCandidates = new JSONArray();
        for (IceCandidate candidate : candidates) {
            JSONObject candidateObj = new JSONObject();
            candidateObj.put("sdpMLineIndex", candidate.sdpMLineIndex);
            candidateObj.put("sdpMid", candidate.sdpMid);
            candidateObj.put("candidate",
                    candidate.sdp.indexOf("a=") == 0 ? candidate.sdp : "a=" + candidate.sdp);
            removedCandidates.put(candidateObj);
        }

        JSONObject rmCanMsg = new JSONObject();
        rmCanMsg.put("type", "removed-candidates");
        rmCanMsg.put("candidates", removedCandidates);

        JSONObject msg = new JSONObject();
        msg.put("id", id);
        msg.put("signaling", rmCanMsg);
        sendSignalingMessage("soac", msg, null);
    } catch (JSONException e) {
        DCHECK(e);
    }
}
 
Example #13
Source File: PeerConnectionClient.java    From sample-videoRTC with Apache License 2.0 5 votes vote down vote up
@Override
public void onIceCandidatesRemoved(final IceCandidate[] candidates) {
  executor.execute(new Runnable() {
    @Override
    public void run() {
      events.onIceCandidatesRemoved(candidates);
    }
  });
}
 
Example #14
Source File: CallActivity.java    From sample-videoRTC with Apache License 2.0 5 votes vote down vote up
@Override
public void onRemoteIceCandidatesRemoved(final IceCandidate[] candidates) {
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            if (peerConnectionClient == null) {
                Log.e(TAG, "Received ICE candidate removals for a non-initialized peer connection.");
                return;
            }
            peerConnectionClient.removeRemoteIceCandidates(candidates);
        }
    });
}
 
Example #15
Source File: CallActivity.java    From sample-videoRTC with Apache License 2.0 5 votes vote down vote up
@Override
public void onIceCandidate(final IceCandidate candidate) {
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            if (appRtcClient != null) {
                appRtcClient.sendLocalIceCandidate(candidate);
            }
        }
    });
}
 
Example #16
Source File: CallActivity.java    From sample-videoRTC with Apache License 2.0 5 votes vote down vote up
@Override
public void onIceCandidatesRemoved(final IceCandidate[] candidates) {
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            if (appRtcClient != null) {
                appRtcClient.sendLocalIceCandidateRemovals(candidates);
            }
        }
    });
}
 
Example #17
Source File: ConferencePeerConnectionChannel.java    From owt-client-android with Apache License 2.0 5 votes vote down vote up
@Override
public void onSetSuccess() {
    if (signalingState == PeerConnection.SignalingState.STABLE) {
        remoteSdpSet = true;
        for (IceCandidate iceCandidate : queuedLocalCandidates) {
            observer.onIceCandidate(key, iceCandidate);
        }
        queuedLocalCandidates.clear();

        if (stream instanceof LocalStream) {
            setMaxBitrate(stream.id());
        }
    }
}
 
Example #18
Source File: P2PClient.java    From owt-client-android with Apache License 2.0 5 votes vote down vote up
@Override
public void onIceCandidate(String peerId, IceCandidate candidate) {
    try {
        JSONObject candidateObject = new JSONObject();
        candidateObject.put("type", "candidates");
        candidateObject.put("candidate", candidate.sdp);
        candidateObject.put("sdpMLineIndex", candidate.sdpMLineIndex);
        candidateObject.put("sdpMid", candidate.sdpMid);

        sendSignalingMessage(peerId, SIGNALING_MESSAGE, candidateObject, null);
    } catch (JSONException e) {
        DCHECK(e);
    }
}
 
Example #19
Source File: CallActivity.java    From sample-videoRTC with Apache License 2.0 5 votes vote down vote up
@Override
public void onRemoteIceCandidate(final IceCandidate candidate) {
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            if (peerConnectionClient == null) {
                Log.e(TAG, "Received ICE candidate for a non-initialized peer connection.");
                return;
            }
            peerConnectionClient.addRemoteIceCandidate(candidate);
        }
    });
}
 
Example #20
Source File: PeerConnectionClient.java    From sample-videoRTC with Apache License 2.0 5 votes vote down vote up
public void addRemoteIceCandidate(final IceCandidate candidate) {
  executor.execute(new Runnable() {
    @Override
    public void run() {
      if (peerConnection != null && !isError) {
        if (queuedRemoteCandidates != null) {
          queuedRemoteCandidates.add(candidate);
        } else {
          peerConnection.addIceCandidate(candidate);
        }
      }
    }
  });
}
 
Example #21
Source File: WebSocketRTCClient.java    From sample-videoRTC with Apache License 2.0 5 votes vote down vote up
private JSONObject toJsonCandidate(final IceCandidate candidate) {
  JSONObject json = new JSONObject();
  jsonPut(json, "label", candidate.sdpMLineIndex);
  jsonPut(json, "id", candidate.sdpMid);
  jsonPut(json, "candidate", candidate.sdp);
  return json;
}
 
Example #22
Source File: WebSocketRTCClient.java    From sample-videoRTC with Apache License 2.0 5 votes vote down vote up
@Override
public void sendLocalIceCandidateRemovals(final IceCandidate[] candidates) {
  handler.post(new Runnable() {
    @Override
    public void run() {
      JSONObject json = new JSONObject();
      jsonPut(json, "type", "remove-candidates");
      JSONArray jsonArray = new JSONArray();
      for (final IceCandidate candidate : candidates) {
        jsonArray.put(toJsonCandidate(candidate));
      }
      jsonPut(json, "candidates", jsonArray);
      if (initiator) {
        // Call initiator sends ice candidates to GAE server.
        if (roomState != ConnectionState.CONNECTED) {
          reportError("Sending ICE candidate removals in non connected state.");
          return;
        }
        sendPostMessage(MessageType.MESSAGE, messageUrl, json.toString());
        if (connectionParameters.loopback) {
          events.onRemoteIceCandidatesRemoved(candidates);
        }
      } else {
        // Call receiver sends ice candidates to websocket server.
        wsClient.send(json.toString());
      }
    }
  });
}
 
Example #23
Source File: WebSocketRTCClient.java    From sample-videoRTC with Apache License 2.0 5 votes vote down vote up
@Override
public void sendLocalIceCandidate(final IceCandidate candidate) {
  handler.post(new Runnable() {
    @Override
    public void run() {
      JSONObject json = new JSONObject();
      jsonPut(json, "type", "candidate");
      jsonPut(json, "label", candidate.sdpMLineIndex);
      jsonPut(json, "id", candidate.sdpMid);
      jsonPut(json, "candidate", candidate.sdp);
      if (initiator) {
        // Call initiator sends ice candidates to GAE server.
        if (roomState != ConnectionState.CONNECTED) {
          reportError("Sending ICE candidate in non connected state.");
          return;
        }
        sendPostMessage(MessageType.MESSAGE, messageUrl, json.toString());
        if (connectionParameters.loopback) {
          events.onRemoteIceCandidate(candidate);
        }
      } else {
        // Call receiver sends ice candidates to websocket server.
        wsClient.send(json.toString());
      }
    }
  });
}
 
Example #24
Source File: ConferencePeerConnectionChannel.java    From owt-client-android with Apache License 2.0 5 votes vote down vote up
@Override
public void onIceCandidate(final IceCandidate iceCandidate) {
    callbackExecutor.execute(() -> {
        if (remoteSdpSet) {
            observer.onIceCandidate(key, iceCandidate);
        } else {
            queuedLocalCandidates.add(iceCandidate);
        }
    });
}
 
Example #25
Source File: VideoChatHelper.java    From Socket.io-FLSocketIM-Android with MIT License 5 votes vote down vote up
@Override
public void onIceCandidate(IceCandidate iceCandidate) {

    JSONObject dataObject = new JSONObject();
    try {
        dataObject.put("id", iceCandidate.sdpMid);
        dataObject.put("label", iceCandidate.sdpMLineIndex);
        dataObject.put("candidate", iceCandidate.sdp);
        dataObject.put("socketId", this.id);

        SocketManager.socket.emit("__ice_candidate", dataObject);
    } catch (JSONException e) {
        e.printStackTrace();
    }
}
 
Example #26
Source File: WebRtcClient.java    From imsdk-android with MIT License 5 votes vote down vote up
@Override
public void onIceCandidate(final IceCandidate candidate) {
    LogUtil.d(TAG + candidate.sdp);
    Logger.i(TAG + " onIceCandidate" + candidate.sdp);
    WebRtcJson.WebRtcPayload payload = new WebRtcJson.WebRtcPayload();
    payload.candidate = candidate.sdp;
    payload.id = candidate.sdpMid;
    payload.label = candidate.sdpMLineIndex;
    Logger.i(TAG + " SEND ICE: " + JsonUtils.getGson().toJson(payload));
    sendWebrtcMessage("candidate", payload);
}
 
Example #27
Source File: CallActivity.java    From RTCStartupDemo with GNU General Public License v3.0 5 votes vote down vote up
private void onRemoteCandidateReceived(String userId, JSONObject message) {
    logcatOnUI("Receive Remote Candidate ...");
    try {
        IceCandidate remoteIceCandidate = new IceCandidate(message.getString("id"), message.getInt("label"), message.getString("candidate"));
        mPeerConnection.addIceCandidate(remoteIceCandidate);
    } catch (JSONException e) {
        e.printStackTrace();
    }
}
 
Example #28
Source File: CallActivity.java    From RTCStartupDemo with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onIceCandidatesRemoved(IceCandidate[] iceCandidates) {
    for (int i = 0; i < iceCandidates.length; i++) {
        Log.i(TAG, "onIceCandidatesRemoved: " + iceCandidates[i]);
    }
    mPeerConnection.removeIceCandidates(iceCandidates);
}
 
Example #29
Source File: CustomWebSocketListener.java    From WebRTCapp with Apache License 2.0 5 votes vote down vote up
private void saveIceCandidate(JSONObject json, String endPointName) throws JSONException {
    IceCandidate iceCandidate = new IceCandidate(json.getString("sdpMid"), Integer.parseInt(json.getString("sdpMLineIndex")), json.getString("candidate"));
    if (endPointName == null) {
        localPeer.addIceCandidate(iceCandidate);
    } else {
        participants.get(endPointName).getPeerConnection().addIceCandidate(iceCandidate);
    }
}
 
Example #30
Source File: PeerConnectionClient.java    From sample-videoRTC with Apache License 2.0 5 votes vote down vote up
private void drainCandidates() {
  if (queuedRemoteCandidates != null) {
    Log.d(TAG, "Add " + queuedRemoteCandidates.size() + " remote candidates");
    for (IceCandidate candidate : queuedRemoteCandidates) {
      peerConnection.addIceCandidate(candidate);
    }
    queuedRemoteCandidates = null;
  }
}