美文网首页
Android WebRTC 05 --- ice交换(send

Android WebRTC 05 --- ice交换(send

作者: 沪漂意哥哥 | 来源:发表于2022-03-22 09:21 被阅读0次

一. 设置远端sdp(setRemoteDescription)

mPeer.pc.setRemoteDescription(mPeer, sdp);

二. 获取ice(setRemoteDescription)

@Override
public void onIceCandidate(IceCandidate iceCandidate) {
    Log.i(TAG, "onIceCandidate: "+iceCandidate.toString());
    // 发送IceCandidate
    webSocket.sendIceCandidate(socketId, iceCandidate);
}

三. socket发送ice

public void sendIceCandidate(String socketId, IceCandidate iceCandidate) {
    HashMap<String, Object> childMap = new HashMap();
    childMap.put("id", iceCandidate.sdpMid);
    childMap.put("label", iceCandidate.sdpMLineIndex);
    childMap.put("candidate", iceCandidate.sdp);
    childMap.put("socketId", socketId);
    HashMap<String, Object> map = new HashMap();
    map.put("eventName", "__ice_candidate");
    map.put("data", childMap);
    JSONObject object = new JSONObject(map);
    String jsonString = object.toString();
    Log.d(TAG, "send-->" + jsonString);
    mWebSocketClient.send(jsonString);
}

四. socke接收ice

// 处理交换信息
private void handleRemoteCandidate(Map map) {
    Log.i(TAG, "JavaWebSocket  6   handleRemoteCandidate: ");
    Map data = (Map) map.get("data");
    String socketId;
    if (data != null) {
        socketId = (String) data.get("socketId");
        String sdpMid = (String) data.get("id");
        sdpMid = (null == sdpMid) ? "video" : sdpMid;
        int sdpMLineIndex = (int) Double.parseDouble(String.valueOf(data.get("label")));
        String candidate = (String) data.get("candidate");
        IceCandidate iceCandidate = new IceCandidate(sdpMid, sdpMLineIndex, candidate);
        events.onRemoteIceCandidate(socketId, iceCandidate);
    }
}

五. 设置远端ice

@Override
public void onRemoteIceCandidate(String socketId, IceCandidate iceCandidate) {
    handler.post(() -> {
        if (_peerHelper != null) {
            _peerHelper.onRemoteIceCandidate(socketId, iceCandidate);
        }
    });
}
public void onRemoteIceCandidate(String socketId, IceCandidate iceCandidate) {
    executor.execute(() -> {
        Peer peer = connectionPeerDic.get(socketId);
        if (peer != null) {
            peer.pc.addIceCandidate(iceCandidate);
        }
    });
}

六. 代码地址

https://gitee.com/luisliuyi/webrtc-android03

相关文章

网友评论

      本文标题:Android WebRTC 05 --- ice交换(send

      本文链接:https://www.haomeiwen.com/subject/qhozdrtx.html