Android WebRTC DataChannel二进制传输模式

时间:2015-02-17 09:04:15

标签: android file webrtc file-transfer rtcdatachannel

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

一方面,我发送数据:

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

如果是任何文件,当isBinaryFile false 时,会调用回调并且我可以打印文本,甚至可以重建文件(图片, pdf,无论如何)。

isBinaryFile 为真时,我收到以下错误:

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

看完this后,看起来我需要使用SCTP DataChannels,但我不知道怎么做!

1 个答案:

答案 0 :(得分:1)

终于找到了解决方案!

在此之前,我将PeerConnection RtpDataChannels约束true构建到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); ;但要使用 SCTP DataChannels ,您必须将其设为默认值(或将其设置为false),如下所示:

{{1}}

简单! : - )