【问题标题】:Android WebRTC DataChannel binary transfer modeAndroid WebRTC DataChannel 二进制传输模式
【发布时间】:2015-04-17 22:49:48
【问题描述】:

我实现了使用 WebRTC 的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 等) .

isBinaryFiletrue 时,我收到以下错误:

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

看完this,貌似需要用SCTP DataChannels,但是不知道怎么用!

【问题讨论】:

  • 你是如何注册onMessage回调的?
  • 对不起,我迟到了。只需implements DataChannel.Observer
  • 你能帮忙解决这个stackoverflow.com/questions/29499725/… 吗?或者您能否分享一个将文件从一个android发送到另一个android的示例代码。只是一个有助于理解方式的示例代码......

标签: android file webrtc file-transfer rtcdatachannel


【解决方案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);

简单! :-)

【讨论】:

  • 如何创建数据通道?您的代码在任何地方都可用吗?
  • 请您指导我学习如何使用 webRTC 在两个机器人之间传输文件的教程!谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-27
  • 1970-01-01
  • 1970-01-01
  • 2013-01-07
  • 2016-02-11
  • 1970-01-01
相关资源
最近更新 更多