android中的PeerConnection实例始终为null?

时间:2016-08-04 08:20:04

标签: android webrtc

我必须在Android应用程序中实现WebRTC,因为我正在使用libjingle库,版本为11139。在这里,我始终得到pc(PeerConnection class instance)始终为空。我检查了

的值
factory(PeerConnectionFactory)
iceServers(LinkedList<IceServers>
mediaConstraints
Peer.this(PCObserver interface)) 

但所有这些都不是null。那么为什么我总是得到结果null。我在这里做错了吗?

pc = factory.createPeerConnection(iceServers, mediaConstraints, Peer.this);

编辑:

 public CallManager(TagoveApplication context, CustomSocket server, CallType callType) {
    this.server = server;
    this.context = context;
    initializeFactoryFieldTrials(); //initialize peer conn factory field trials
    PeerConnectionFactory.initializeAndroidGlobals(context, true, true, true);

    //PeerConnectionFactory.initializeAndroidGlobals(context, true, true, true, VideoRendererGui.getEGLContext());
    factory = new PeerConnectionFactory();

    iceServers.add(new PeerConnection.IceServer("turn:xxx.xxx.xxx.xxx:xxxx?transport=udp", "xxxxxxxx", "xxxxxxxxx"));

    iceServers.add(new PeerConnection.IceServer("stun:stunserver.org"));
    iceServers.add(new PeerConnection.IceServer("stun:stun.ekiga.net"));
    iceServers.add(new PeerConnection.IceServer("stun:stun.fwdnet.net"));
    iceServers.add(new PeerConnection.IceServer("stun:stun.ideasip.com"));
    iceServers.add(new PeerConnection.IceServer("stun:stun.iptel.org"));
    iceServers.add(new PeerConnection.IceServer("stun:stun.rixtelecom.se"));
    iceServers.add(new PeerConnection.IceServer("stun:stun.schlund.de"));

    pcConstraints.mandatory.add(new MediaConstraints.KeyValuePair("OfferToReceiveAudio", "true"));
    pcConstraints.mandatory.add(new MediaConstraints.KeyValuePair("OfferToReceiveVideo", "true"));
    pcConstraints.optional.add(new MediaConstraints.KeyValuePair("DtlsSrtpKeyAgreement", "true"));

    this.callType = callType;
}

创建Peer构造函数:

    public Peer(
        String label,
        PeerConnectionFactory factory,
        LinkedList<PeerConnection.IceServer> iceServers,
        MediaConstraints mediaConstraints,
        PeerCallbacks peerCallbacks,
        StreamChangeListener listener,
        boolean incoming){

    this.label=label;
    this.peerCBacks=peerCallbacks;

    //Create Peer connection using RTCConfiguration
    Log.d("PCTest","Peer factory value - "+String.valueOf(factory));
    Log.d("PCTest","ice servers size - "+iceServers.size());
    Log.d("PCTest","media constraints - "+String.valueOf(mediaConstraints));

    PeerConnection.RTCConfiguration rtcConfig = new PeerConnection.RTCConfiguration(iceServers);
    rtcConfig.bundlePolicy = PeerConnection.BundlePolicy.MAXBUNDLE;
    rtcConfig.rtcpMuxPolicy = PeerConnection.RtcpMuxPolicy.REQUIRE;
    rtcConfig.keyType = PeerConnection.KeyType.ECDSA;
    Log.d("","");
    this.pc = factory.createPeerConnection(rtcConfig, mediaConstraints, this);
    Log.d("PCTest","Peer constructor called pc value - "+String.valueOf(this.pc));

    this.streamListener=listener;
    log("new +"+" "+label+ "  "+(peerCallbacks!=null? "notNull":"issNull")+" ++ "+incoming);
}

3 个答案:

答案 0 :(得分:1)

答案适用于官方webRTC android API compile 'org.webrtc:google-webrtc:1.0.+'

创建IceServers时应该使用正确的TlsCertPolicy。 如果您的眩晕/转弯服务器不安全,例如:stun:stun1.l.google.com:19302而不是stuns:stun1.l.google.com:19302,那么您应该将TLS证书策略设置为TLS_CERT_POLICY_INSECURE_NO_CHECK。 可以使用IceServerBuilder以这种方式完成:

List<PeerConnection.IceServer> iceServers = new ArrayList<>();
PeerConnection.IceServer.Builder iceServerBuilder = PeerConnection.IceServer.builder("stun:stun1.l.google.com:19302");
iceServerBuilder.setTlsCertPolicy(PeerConnection.TlsCertPolicy.TLS_CERT_POLICY_INSECURE_NO_CHECK); //this does the magic.
PeerConnection.IceServer iceServer =  iceServerBuilder.createIceServer();
iceServers.add(iceServer);
localPeer = peerConnectionFactory.createPeerConnection(iceServers, sdpConstraints,sdpObserver);

答案 1 :(得分:0)

确保正确初始化工厂,如下所示:

private void createPeerConnectionFactoryInternal(Context context) {
    Log.d(TAG, "Create peer connection factory. Use video: " + peerConnectionParameters.videoCallEnabled);
    // Initialize field trials.
    String field_trials = FIELD_TRIAL_AUTOMATIC_RESIZE;
    // Check if VP9 is used by default.
    if (peerConnectionParameters.videoCallEnabled && peerConnectionParameters.videoCodec != null &&
            peerConnectionParameters.videoCodec.equals(MediaConfiguration.VideoCodec.VP9.toString())) {
        field_trials += FIELD_TRIAL_VP9;
    }
    PeerConnectionFactory.initializeFieldTrials(field_trials);

    if (!PeerConnectionFactory.initializeAndroidGlobals(context, true, true,
            peerConnectionParameters.videoCodecHwAcceleration)) {
        observer.onPeerConnectionError("Failed to initializeAndroidGlobals");
    }
    factory = new PeerConnectionFactory();

    Log.d(TAG, "Peer connection factory created.");
}

编辑: 另外,请确保您的Peer.class已实现Observer接口

编辑2: 也许,试试这种方式: 在班上创建新课程:

private class PeerObserver implements PeerConnection.Observer {

    @Override
    public void onSignalingChange(PeerConnection.SignalingState newState) {
        Log.d(TAG, "onSignalingChange");
    }

    @Override
    public void onIceConnectionChange(PeerConnection.IceConnectionState newState) {
        Log.d(TAG, "onIceConnectionChange");
    }

    @Override
    public void onIceGatheringChange(PeerConnection.IceGatheringState newState) {
        Log.d(TAG, "onIceGatheringChange");
    }

    @Override
    public void onIceCandidate(IceCandidate candidate) {
        Log.d(TAG, "onIceCandidate");
    }

    @Override
    public void onError() {
        Log.d(TAG, "onError");
    }

    @Override
    public void onAddStream(MediaStream stream) {
        Log.d(TAG, "onAddStream");
    }

    @Override
    public void onRemoveStream(MediaStream stream) {
        Log.d(TAG, "onRemoveStream");
    }

    @Override
    public void onDataChannel(DataChannel dataChannel) {
        Log.d(TAG, "onDataChannel");
    }

    @Override
    public void onRenegotiationNeeded() {
        Log.d(TAG, "onRenegotiationNeeded");
    }

}

现在,在factory.createPeerConnection之前添加PeerObserver observer = new PeerObserver();并初始化PeerConnection pc = factory.createPeerConnection(iceServers,mediaConstraints,observer);

iceServers sholud看起来像这样

ArrayList<PeerConnection.IceServer> iceServers = new ArrayList<PeerConnection.IceServer>();

iceServers.add(new PeerConnection.IceServer(“stun:stun.l.google.com:19302”));

答案 2 :(得分:0)

如果仍然有人在处理这个问题,我会发现我遇到的问题。

打开服务器需要设置的用户名和密码。眩晕服务器只需要URL。

我现在正在使用版本“ org.webrtc:google-webrtc:1.0.22672”,该版本仅需要执行以下操作即可使对等连接不为空 晕眩:

PeerConnection.IceServer.Builder iceServerBuilder = PeerConnection.IceServer.builder("stun:hostname:1234");

//        iceServerBuilder.setUsername("test");
//        iceServerBuilder.setPassword("passord");

TURN:     PeerConnection.IceServer.Builder iceServerBuilder = PeerConnection.IceServer.builder(“ turn:hostname:1234”);

       iceServerBuilder.setUsername("test");
       iceServerBuilder.setPassword("passord");

较新版本的WebRTC应该可以使用以下语法。不确定100%是否需要以下所有内容,但您可以取出一些内容看是否破裂。我在VideoCapturer的较新版本中遇到另一个空对象问题。

    PeerConnection.IceServer.Builder iceServerBuilder = PeerConnection.IceServer.builder("turn:hostname:1234?transport=tcp");
    iceServerBuilder.setTlsCertPolicy(PeerConnection.TlsCertPolicy.TLS_CERT_POLICY_INSECURE_NO_CHECK);
    iceServerBuilder.setUsername("test");
    iceServerBuilder.setPassword("passord");
    PeerConnection.IceServer peerIceServer = iceServerBuilder.createIceServer();