Android WebRTC DataChannel binary transfer mode

佐手、 提交于 2019-12-06 13:38:47

问题


I achieved to transfer data between two android phones using WebRTC's DataChannel :

On one side, I send the data:

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);

On the other side, this callback should be called regardless of isBinaryFile value :

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

In the case of any file, when isBinaryFile is false, the callback is called and I'm able to print the text, or even reconstruct the file (images, pdf, whatever).

When isBinaryFile is true I get the following error:

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

After reading this, looks like I need to use SCTP DataChannels, but I don't know how !


回答1:


Finally found a solution !

Before, I constructed my PeerConnection with RtpDataChannels constraint to true; but to use SCTP DataChannels, you must let it to default (or set it to false), like this:

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);

Simple ! :-)



来源:https://stackoverflow.com/questions/28558153/android-webrtc-datachannel-binary-transfer-mode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!