Android模拟器配置turn server

1 模拟器如何配置turn?

qemu/android/android-webrtc/README.md中介绍了android emulator如何配置trun server

## Configuring TURN

Traversal Uing Relays around NAT (TURN) is a protocol that assists in traversal
of network address translators (NAT) or firewalls for multimedia applications.
It may be used with the Transmission Control Protocol (TCP) and User Datagram
Protocol (UDP). It is most useful for clients on networks masqueraded by
symmetric NAT devices. TURN does not aid in running servers on well known ports
in the private network through a NAT; it supports the connection of a user
behind a NAT to only a single peer, as in telephony, for example.

Turn makes it possible to connect to the emulator over WebRTC, even if the
network is closed down and no direct peer connection can be established. It
does this by relaying the packets through a server that acts as a middle man.
Configuration of turn is done in the video bridge by invoking an external
program that provides the turn configuration.

For example in GCE you can enable the turn api (http://go/turnaas) and pass in
the following curl command to obtain a turn configuration:

sh
$ curl -s -X POST https://networktraversal.googleapis.com/v1alpha/iceconfig?key=some_secret


This will produce a configuration that the bridge will distribute to the
clients to configure the peer connection. Depending on your network
configuration you might need to provide your own executable that provides this
information. Your executable should observe the following rules:

 - Produce a result on stdout.
 - Produces a result in under 1000 ms.
 - Produce a valid [JSON RTCConfiguration object](https://developer.mozilla.org/en-US/docs/Web/API/RTCPeerConnection/RTCPeerConnection).
 - Contain at least an `"iceServers"` array.
 - The exit value should be 0 on success

An example result by using curl:

json
{
  "lifetimeDuration": "86400s",
  "iceServers": [
    {
      "urls": [
        "stun:171.194.202.127:19302",
        "stun:[2607:f8b0:400f:c00::7f]:19302"
      ]
    },
    {
      "urls": [
        "turn:171.194.202.127:19305?transport=udp",
        "turn:[2607:f8b0:400f:c00::7f]:19305?transport=udp",
        "turn:171.194.202.127:19305?transport=tcp",
        "turn:[2607:f8b0:400f:c00::7f]:19305?transport=tcp"
      ],
      "username": "",
      "credential": "",
      "maxRateKbps": "8000"
    }
  ],
  "blockStatus": "NOT_BLOCKED",
  "iceTransportPolicy": "all"
}

The executable and its parameters can be directly provided to the emulator with
the `-turncfg` parameter.

For example:
emulator @P_64 -grpc 5556 -turncfg "curl -s -X POST https://networktraversal.googleapis.com/v1alpha/iceconfig?key=secret"

启动模拟器时指定-turncfg选项,从某个网站上拉取trun server的配置,目前这个trun server的外网地址已经无法访问,需要手动再配置一个可以访问的turn server。

emulator @P_64 -grpc 5556 -turncfg "curl -s -X POST https://networktraversal.googleapis.com/v1alpha/iceconfig?key=secret"

2 模拟器如何传递turn config?

1:web peer调用grpc接口requestRtcStream发起peer节点之间的连接
2:grpc server(模拟器)通过socket发送一个带"start"标识的message
3:goldfish-webrtc-bridge收到message
4:执行curl命令拉取turn配置并解析
5:将turn配置保存到带"start"标识的message中再发给模拟器
6:web peer调用grpc接口receiveJsepMessage接收turn配置
7:web peer使用turn配置初始化peerconnection

3 外网配置turn server

3.1 安装coturn

sudo apt-get install coturn

3.2 修改配置

修改配置文件l/etc/turnserver.conf

listening-port=3478 #指定侦听的端口
external-ip=72.44.70.121 #指定云主机的公网IP地址
user=username1:key1 #访问 stun/turn服务的用户名和密码
realm=stun.xxx.cn #域名,这个一定要设置

3.3 启动服务

service coturn restart

3.4 测试连接

打开 trickle-ice ,按里面的要求输入 stun/turn 地址、用户和密码后就可以探测stun/turn服务是否正常了。测试的结果如下图所示,relay返回了应答消息。

选区_020.png

4 web端配置turn config

turn config是由模拟器发送给web peer的,不修改模拟器代码的情况下,可以直接在web peer端配置turn,初始化节点时(RTCPeerConnection(signal.start))直接传入配置。

_handleJsepMessage = message => {
    try {
      const signal = JSON.parse(message);
      console.log("handle message: [" + message + "]");
      if (signal.start) {
        var str = '{"start":{"lifetimeDuration":"86400s","iceServers":[{"urls":["stun:stun.l.google.com:19302"]},{"urls":["turn:72.44.70.121:3478?transport=udp","turn:72.44.70.121:3478?transport=tcp"],"username":"username1","credential":"key1","maxRateKbps":"80000"}],"blockStatus":"NOT_BLOCKED","iceTransportPolicy":"all"}}';
        const signal_turn = JSON.parse(str);
        this._handleStart(signal_turn);
      }
      if (signal.sdp) this._handleSDP(signal);
      if (signal.bye) this._handleBye();
      if (signal.candidate) this._handleCandidate(signal);
    } catch (e) {
      console.log("Failed to handle message: [" + message + "], due to: " + e);
    }
  };

你可能感兴趣的:(Android模拟器配置turn server)