1

我实现了使用 WebRTC 在两部 android 手机之间传输数据DataChannel

一方面,我发送数据:

boolean isBinaryFile = false;
File file = new File(path); // let's assume path is a .whatever file's path (txt, jpg, pdf..) 
ByteBuffer byteBuffer = ByteBuffer.wrap(convertFileToByteArray(file));
DataChannel.Buffer buf = new DataChannel.Buffer(byteBuffer, isBinaryFile); 
dataChannel.send(buf);

另一方面,无论isBinaryFile值如何,都应调用此回调:

public void onMessage( final DataChannel.Buffer buffer ){
    Log.e(TAG, "Incomming file on DataChannel");

    ByteBuffer data = buffer.data;
    byte[] bytes = new byte[ data.capacity() ];
    data.get(bytes);

    // If it's not a binary file (text)
    if( !buffer.binary ) {
        String strData = new String( bytes );
        Log.e(TAG, "Text file is : " + strData);
    } else {
        Log.e(TAG, "Received binary file ! :)");
    }
}

对于任何文件,当isBinaryFilefalse时,回调被调用,我可以打印文本,甚至重建文件(图像、pdf 等)。

isBinaryFile时,我收到以下错误:

Warning(rtpdataengine.cc:317): Not sending data because binary type is unsupported.

看了这篇,好像需要用SCTP DataChannels,但是不知道怎么用!

4

1 回答 1

1

终于找到解决办法了!

之前,我PeerConnectionRtpDataChannels约束构造了我的true;但要使用SCTP DataChannels,您必须让它默认(或将其设置为 false),如下所示:

MediaConstraints pcConstraints = signalingParameters.pcConstraints;
// pcConstraints.optional.add(new KeyValuePair("RtpDataChannels", "false"));
pcConstraints.optional.add(new KeyValuePair("DtlsSrtpKeyAgreement", "true"));
pc = factory.createPeerConnection(signalingParameters.iceServers,
            pcConstraints, pcObserver);

简单的 !:-)

于 2015-02-17T13:56:34.743 回答