5

我正在尝试在 Android 上实现 WebRTC DataChannel。我想创建一个简单的 peerconnection 对象,它将打开 DataChannel 以使用 WebRTC 通过网络发送数据。尝试创建 PeerConnection 对象时出现错误。我了解到我们使用 factory 来创建 peerconnection 对象factory.createPeerConnection()

为此,我必须首先创建 PeerConnectionFactory 对象。在此之后,我可以使用它来创建 PeerConnection 对象。当我尝试创建 PeerConnectionFactory 对象时Could not find method android.media.MediaCodec.setParameters出现错误。Fatal Signal 11 (SIGSEGV) at 0x00000000 (code=1)我还尝试了以下代码,PeerConnectionFactory.initializeAndroidGlobals(this, false, false, false);这就是我想要做的:

PeerConnectionFactory factory = new PeerConnectionFactory();

peer = new Peer();

这就是我的 Peer 对象的样子:

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

}

谁能指出我在 Android 上实现 DataChannel 的任何示例源代码?如果我没有以正确的方式做这件事,也请告诉我。我找不到说明如何操作的 Android Native WebRTC 文档。我正在尝试实现我从在 Web 上使用 WebRTC 中学到的任何东西。

如果我的问题不清楚,请告诉我。

4

3 回答 3

7

PeerConnectionFactory 不再需要初始化音频和视频引擎来启用。

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

现在您将能够禁用音频和视频,并使用数据通道

于 2017-09-14T20:49:53.977 回答
6

这是 Android 的 WebRTC 代码中的一个已知错误。以下线程更多地讨论了这个错误:

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

该错误目前处于打开状态。但是,有一个可用的解决方法,现在可以使用。在 Android Globals 中,我们需要将音频和视频参数作为 true 传递:

PeerConnectionFactory.initializeAndroidGlobals(getApplicationContext(), true, true, VideoRendererGui.getEGLContext());
于 2015-04-13T11:52:48.077 回答
2

改用这个PeerConnectionFactory.initializeAndroidGlobals(acontext, TRUE, false, false, NULL);

然后创建工厂。factory = new PeerConnectionFactory();

然后在您的类 Peer 中创建对等连接,如下所示:factory.createPeerConnection(iceServers, sdpMediaConstraints, this);

这对我来说只为视频流建立没有 EGLContext 的 DataChannel。

更新:如果您仍然有此错误,请转到较新的版本!这是非常不赞成的。

于 2017-08-25T08:46:52.580 回答