简体   繁体   English

如何在Android上使用WebRTC进行实时通信?

[英]How to use WebRTC on Android for Real Time Communication?

I'm working on a simple project in which i need to control a Arduino Robot (2-servo motors) through an Android Phone's Gyroscope Sensor , via Internet. 我正在一个简单的项目中,需要通过互联网通过Android手机的Gyroscope Sensor控制Arduino机器人(2-伺服电机)。 As you can visualize, 3-axis Gyroscopic Coordinates change quite sensitively even with little change in Phone's orientation in 3D space, so i want to minimize the Lag to milliseconds. 如您所见,即使在3D空间中Phone方向变化不大,三轴陀螺仪坐标也会非常敏感地发生变化,因此我想将滞后时间最小化到毫秒。

Now aside from hardware, my first milestone is to send Gyroscope coordinates from Android to a computer through internet as Fast and Continuously as possible (like a RealTime stream of Numeric data). 现在,除了硬件之外,我的第一个里程碑是将Android上的陀螺仪坐标尽可能快且连续地通过互联网发送到计算机(例如实时的数字数据流)。 I know traditional HTTP based Client-Server mechanism will be quite slow therefore i've looked into following: 我知道传统的HTTP based Client-Server机制将非常慢,因此我研究了以下内容:

  1. Google's Chanel API Google的Chanel API
  2. WebRTC WebRTC
  3. WebSockets Web套接字

According to my research, Channel API shows latency that from 10ms to even in Seconds. 根据我的研究,Channel API显示的延迟从10毫秒到偶数秒。 Also GAE limits requests to 30 seconds. GAE还将请求限制为30秒。 Here is a Channels API stress test: http://channelapistresstest.appspot.com/ Try clicking "send 5"-button a lot, and you will see latency numbers going up to several seconds. 这是一个Channels API压力测试: http ://channelapistresstest.appspot.com/尝试多次单击“发送5”按钮,您会看到等待时间上升到几秒钟。

Now WebRTC sounds most promising and faster than WebSockets. 现在,WebRTC听起来比WebSockets最有前途且速度更快。 I'll be really grateful if someone can guide me about practical implementation of WebRTC in Native JAVA and Native Android ( Any good libraries? i want to send coords. from Android and Receive via a JAVA-SE client on Desktop). 如果有人可以指导我有关本机JAVA和本机Android中WebRTC的实际实现,我将非常感激(是否有任何好的库?我想从Android发送坐标,并通过桌面上的JAVA-SE客户端进行接收)。 I'm not interested in Hybrid App solutions (like Crosswalk ). 我对Hybrid App解决方案(例如Crosswalk )不感兴趣。 I would also like to know about How should i setup a Signalling Server. 我也想知道我应该如何设置信令服务器。 Summing it up i have following questions: 总结一下,我有以下问题:

  1. Which method should i use? 我应该使用哪种方法? (Channel API, WebSockets, WebRTC or something else) with native Java and android support? (通道API,WebSockets,WebRTC或其他)具有本机Java和android支持?
  2. Incase of WebRTC, how should i setup a Signalling server? 如果是WebRTC,我应该如何设置信令服务器? (just brief description) or a WebSocket Server for WebSockets? (只是简要说明)还是WebSocket的WebSocket服务器?
  3. Can i make use of Google's Cloud platform or something similar to reduce complexity at my end? 我可以利用Google的Cloud平台或类似工具降低复杂性吗?
  4. Any overall suggestion? 有什么总体建议吗?

Thanks in advance. 提前致谢。

You do not want to use WebRTC. 您不想使用WebRTC。 WebRTC requires you to setup a separate signaling channel like WebSockets anyway, so it is needlessly complex and very heavyweight for what you are trying to do. WebRTC无论如何都需要您设置一个单独的信令通道(例如WebSockets),因此它对于您要执行的操作来说是不必要的复杂且非常繁重。

If your requirement is simply to communicate a series of gyroscope values from an device to another, I recommend: 如果您只是要将一系列陀螺仪值从一台设备传送到另一台设备,我建议:

  1. Start with a simple socket server 从简单的套接字服务器开始
  2. Connect your clients to this server via a socket 通过套接字将客户端连接到该服务器
  3. Relay messages from one client to the other 将消息从一个客户端中继到另一个客户端

A simple server to print socket input to standard output is just a few lines of Python , for example. 例如,将套接字输入输出到标准输出的简单服务器就是几行Python This does require you to learn to use sockets because your clients need to be interpreting the byte stream in the same way. 这确实需要您学习使用套接字,因为您的客户端需要以相同的方式解释字节流。 You can also use WebSockets, but this may increase the complexity of your server significantly; 您也可以使用WebSockets,但这可能会大大增加服务器的复杂性。 Java EE is significantly more complex than Java SE, for example. 例如,Java EE比Java SE复杂得多。

If you want data integrity (I imagine you do), you should use TCP. 如果您想要数据完整性(我想您愿意),则应使用TCP。

If you are really worried about latency, you should also skip going to the Internet. 如果您真的担心延迟,还应该跳过Internet。 Run everything on a LAN. 在LAN上运行所有内容。 I imagine you have to see the device under control anyway, so there's no point on going out to the Internet. 我想您无论如何都必须看到设备处于受控状态,因此上网毫无意义。

I would ignore WebRTC for this project. 对于该项目,我将忽略WebRTC。 It is more complicated to setup and requires a special server. 设置起来比较复杂,需要一台特殊的服务器。
WebSocket should be just fine for this project. WebSocket应该适合该项目。 It is as fast as TCP. 它和TCP一样快。
If you would like to avoid even this delay at all cost, but packet loss is not a problem, then I would go for simple UDP. 如果您想不惜一切代价避免这种延迟,但是丢包不是问题,那么我将选择简单的UDP。 For all these, you don't need any library, just a few lines of code. 对于所有这些,您不需要任何库,只需几行代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM