0

关于这个问题已经有很多问题,但它们都与 Javascript 有关。我正在尝试使用 Android,但我onIceCandidate()打电话时永远不会被调用setLocalDescription

private PeerConnection initPeerConnection(boolean isLocal) {

    MediaConstraints sdpConstraints = new MediaConstraints();
    sdpConstraints.mandatory.add(new MediaConstraints.KeyValuePair("offerToReceiveAudio", "true"));
    sdpConstraints.mandatory.add(new MediaConstraints.KeyValuePair("offerToReceiveVideo", "true"));

    List<PeerConnection.IceServer> servers = new ArrayList<>();

    return peerConnectionFactory.createPeerConnection(servers, new CustomPeerConnectionObserver() {
        @Override
        public void onIceCandidate(IceCandidate iceCandidate) {
            super.onIceCandidate(iceCandidate);
            if (isLocal)
                onIceCandidateReceived(localPeerConnection, iceCandidate);
            else
                onIceCandidateReceived(remotePeerConnection, iceCandidate);
        }

        @Override
        public void onAddStream(MediaStream mediaStream) {
            super.onAddStream(mediaStream);
            if (!isLocal)
                gotRemoteStream(mediaStream);
        }
    });
}

创建连接:

    localPeerConnection = initPeerConnection(true);
    remotePeerConnection = initPeerConnection(false);

创建报价。现在它应该触发 onIceCandidate():

localPeerConnection.createOffer(new SimpleSdpObserver() {
        @Override
        public void onCreateSuccess(SessionDescription sessionDescription) {
            Log.e("SDP", sessionDescription.description + sessionDescription.type.toString());
            localPeerConnection.setLocalDescription(new SimpleSdpObserver(), sessionDescription);
            sendOffer(sessionDescription);
        }
    }, sdpMediaConstraints);

但不幸的是它不会触发它。怎么了?什么是正确的?

注意:任何特定于Android JavaScript的答案将不胜感激。

4

1 回答 1

0

你很近。这是我的 Android 代码中的一个片段:

private void createAndSendSubscriberOffer(boolean iceRestart) {
    llConnectionListener.onWebRTCOfferStart();
    peerConnection.createOffer(getSubscriberSDPObserver(), getSubscriberMediaConstraints(iceRestart));
}

@NotNull
private SimpleSdpObserver getSubscriberSDPObserver() {
    return new SimpleSdpObserver() {
        @Override
        public void onCreateSuccess(final SessionDescription sessionDescription) {
            peerConnection.setLocalDescription(new SimpleSdpObserver() {
                @Override
                public void onSetSuccess() {
                    Log.i(TAG, "Local description set.");
                    sendOfferToSocket(sessionDescription);
                }

                @Override
                public void onSetFailure(String s) {
                    Log.i(TAG, s);
                }
            }, sessionDescription);
        }
    };
}

我认为您唯一缺少的是您应该使用 onSetSuccess 发送报价。

于 2020-12-01T22:10:21.193 回答