Akshay Bissa
Akshay Bissa

Reputation: 109

PeerConnection instance in android is always null?

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

Upvotes: 7

Views: 6449

Answers (3)

Wyatt Lansdale
Wyatt Lansdale

Reputation: 86

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

Upvotes: 1

Ramiz Ansari
Ramiz Ansari

Reputation: 524

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

Upvotes: 5

Mateusz
Mateusz

Reputation: 22

Make sure you initialize factory correctly, something like this:

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

EDIT: Also, make sure your Peer.class has implemented Observer interface

EDIT 2: Maybe, try this way: Create new class in your class:

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

}

and now, before factory.createPeerConnection add PeerObserver observer = new PeerObserver(); and initialize PeerConnection pc = factory.createPeerConnection(iceServers, mediaConstraints, observer);

iceServers sholud look something like this

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

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

Upvotes: 0

Related Questions