Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PeerConnection instance in android is always null?

Tags:

android

webrtc

I have to implement WebRTC in android application, for that I am using libjingle library, ver-11139. In this I always get pc(PeerConnection class instance) is always null. I have checked the values of

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

but all of them are not null. Then why I am always getting the result null. Am I doing something wrong here???

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

Edit:

 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;
}

Creating Peer constructor:

    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);
}
like image 473
Akshay Bissa Avatar asked Aug 04 '16 08:08

Akshay Bissa


2 Answers

Answer is for Official webRTC android API compile 'org.webrtc:google-webrtc:1.0.+'

You should use correct TlsCertPolicy, while creating IceServers. If your stun/turn servers are insecure, Eg: stun:stun1.l.google.com:19302 instead of stuns:stun1.l.google.com:19302 then you should set TLS Certificate Policy to TLS_CERT_POLICY_INSECURE_NO_CHECK. it can be done this way using 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);
like image 199
Ramiz Ansari Avatar answered Oct 29 '22 17:10

Ramiz Ansari


If anyone is still dealing with this I figured out the problem I was having.

Turn servers required username and password to be set. Stun servers only require the url.

I am now using version 'org.webrtc:google-webrtc:1.0.22672' which only required the following to get the peer connection to not be null STUN:

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");

The newer versions of WebRTC should work with the below syntax. Not 100% sure if everything below is needed but you can take some out and see if it breaks. I was having another null object problem with the newer version for the 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();
like image 27
Wyatt Lansdale Avatar answered Oct 29 '22 17:10

Wyatt Lansdale