androidwebrtcrtcdatachannel

new PeerConnectionFactory() gives error on android


I am trying to implement WebRTC DataChannel on Android. I want to create a simple peerconnection object which will open DataChannel to send data over the network using WebRTC. I am getting error when I try to create my PeerConnection Object. I learnt that we use factory to create peerconnection object using factory.createPeerConnection().

For this, I have to create the PeerConnectionFactory object first. After this, I can use it to create PeerConnection Object. I get errors Could not find method android.media.MediaCodec.setParameters and Fatal Signal 11 (SIGSEGV) at 0x00000000 (code=1) when I try to create PeerConnectionFactory object. I also tried the following code with PeerConnectionFactory.initializeAndroidGlobals(this, false, false, false); This is what I am trying to do:

PeerConnectionFactory factory = new PeerConnectionFactory();

peer = new Peer();

This is how my Peer object looks like:

public class Peer implements SdpObserver, PeerConnection.Observer, DataChannel.Observer {

    private PeerConnection pc;
    private DataChannel dc;

    public Peer() {

      this.pc = factory.createPeerConnection(RTCConfig.getIceServer(), 
              RTCConfig.getMediaConstraints(), this);

      dc = this.pc.createDataChannel("sendDataChannel", new DataChannel.Init());

    }

    @Override
    public void onAddStream(MediaStream arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onDataChannel(DataChannel dataChannel) {
        this.dc = dataChannel;

    }

    @Override
    public void onIceCandidate(final IceCandidate candidate) {
        try {
            JSONObject payload = new JSONObject();
            payload.put("type", "candidate");
            payload.put("label", candidate.sdpMLineIndex);
            payload.put("id", candidate.sdpMid);
            payload.put("candidate", candidate.sdp);

            sendSocketMessageDataChannel(payload.toString());


          } catch (JSONException e) {
            e.printStackTrace();
          }

    }

    @Override
    public void onIceConnectionChange(IceConnectionState iceConnectionState) {

    }

    @Override
    public void onIceGatheringChange(IceGatheringState arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onRemoveStream(MediaStream arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onRenegotiationNeeded() {
        // TODO Auto-generated method stub

    }

    @Override
    public void onSignalingChange(SignalingState arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onCreateFailure(String msg) {
        Toast.makeText(getApplicationContext(),
                msg, Toast.LENGTH_SHORT)
                .show();

    }

    @Override
    public void onCreateSuccess(SessionDescription sdp) {
        try {

            JSONObject payload = new JSONObject();
            payload.put("type", sdp.type.canonicalForm());
            payload.put("sdp", sdp.description);

            sendSocketMessageDataChannel(payload.toString());

            pc.setLocalDescription(FilePeer.this, sdp);

          } catch (JSONException e) {
            e.printStackTrace();
          }

    }

    @Override
    public void onSetFailure(String arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onSetSuccess() {
        // TODO Auto-generated method stub

    }

    @Override
    public void onMessage(Buffer data) {
        Log.w("FILE", data.toString());

    }

    @Override
    public void onStateChange() {

        Toast.makeText(getApplicationContext(),
                "State Got Changed", Toast.LENGTH_SHORT)
                .show();

        /*
         byte[] bytes = new byte[10];

         bytes[0] = 0;
         bytes[1] = 1;
         bytes[2] = 2;
         bytes[3] = 3;
         bytes[4] = 4;
         bytes[5] = 5;
         bytes[6] = 6;
         bytes[7] = 7;
         bytes[8] = 8;
         bytes[9] = 9;

         ByteBuffer buf = ByteBuffer.wrap(bytes);



         Buffer b = new Buffer(buf, true);

         dc.send(b);
        */
    }

}

Can anybody point me to any sample source code which implements DataChannel on Android? Kindly also let me know if I am not doing it in a right way. I could not find the documentation for Android Native WebRTC which tells how to do it. I am trying to implement whatever I have learnt from using WebRTC on web.

Kindly, let me know if my question is not clear.


Solution

  • PeerConnectionFactory no longer requires initializing audio & video engines to be enabled.

    PeerConnectionFactory.initializeAndroidGlobals(this, false, false, false);

    Now you will be able to disable audio and video, and use data channels