new PeerConnectionFactory() gives error on android

2019-06-22 01:19发布

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.

3条回答
对你真心纯属浪费
2楼-- · 2019-06-22 01:24

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

查看更多
姐就是有狂的资本
3楼-- · 2019-06-22 01:30

Use this instead PeerConnectionFactory.initializeAndroidGlobals(acontext, TRUE, false, false, NULL);

Then create the factory. factory = new PeerConnectionFactory();

Then in your class Peer create the peer connection as this : factory.createPeerConnection(iceServers, sdpMediaConstraints, this);.

This worked for me to establish ONLY DataChannel without EGLContext for video streaming.

UPDATE: If you still have this error, go to a newer version! This is very deprecated.

查看更多
萌系小妹纸
4楼-- · 2019-06-22 01:34

This is a known bug in WebRTC code for Android. Following threads talk more on this bug:

https://code.google.com/p/webrtc/issues/detail?id=3416 https://code.google.com/p/webrtc/issues/detail?id=3234

The bug is currently in open status. However, there is a workaround available which will work for now. In Android Globals, we need to pass the audio and video parameters as true:

PeerConnectionFactory.initializeAndroidGlobals(getApplicationContext(), true, true, VideoRendererGui.getEGLContext());
查看更多
登录 后发表回答