Skip to content

Commit 4372dae

Browse files
committed
Added TURN tutorial & removed symlinks
The symlinks led to confusion for people using the examples without first compiling the js. Closes #5
1 parent c5cb072 commit 4372dae

File tree

17 files changed

+108
-37
lines changed

17 files changed

+108
-37
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ dist: js phony
1717
publish: dist phony
1818
cd js; npm publish
1919
twine upload dist/*
20+
anaconda upload dist/*.tar.gz
2021

2122
test: phony
2223
pytest --cov=rtcbot --timeout=20

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
[![npm](https://img.shields.io/npm/v/rtcbot.svg?style=flat-square)](https://www.npmjs.com/package/rtcbot)
55
[![Documentation Status](https://readthedocs.org/projects/rtcbot/badge/?version=latest&style=flat-square)](https://rtcbot.readthedocs.io/en/latest/?badge=latest)
66
[![Join the chat at https://gitter.im/rtcbot/community](https://img.shields.io/gitter/room/dkumor/rtcbot.svg?style=flat-square)](https://gitter.im/rtcbot/community?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
7-
[![Tests](https://github.com/dkumor/rtcbot/workflows/tests/badge.svg)](https://github.com/dkumor/rtcbot/actions)
7+
![Tests](https://github.com/dkumor/rtcbot/workflows/tests/badge.svg)
88

99
RTCBot's purpose is to provide a set of simple modules that help in developing remote-controlled robots in Python, with a focus on the Raspberry Pi.
1010

docs/installing.rst

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,40 +4,53 @@ Installing RTCBot
44
RTCBot uses some very powerful libraries, which can make it a bit difficult to install on some systems.
55

66

7-
Raspberry Pi
7+
Raspbian
88
++++++++++++++
99

10-
The raspberry pi does not have OpenCV available for python3,
11-
meaning that RTCBot's CVCamera and CVDisplay will not be available unless you manually compile them.
12-
Thankfully, you can still use the official camera module, by installing picamera::
10+
RTCBot requires several dependencies which are best installed using apt-get::
1311

14-
sudo apt-get install python3-numpy
12+
sudo apt-get install python3-numpy python3-cffi python3-aiohttp \
13+
libavformat-dev libavcodec-dev libavdevice-dev libavutil-dev \
14+
libswscale-dev libswresample-dev libavfilter-dev libopus-dev \
15+
libvpx-dev pkg-config libsrtp2-dev python3-opencv pulseaudio
1516

1617
Then, you can install rtcbot with pip::
1718

1819
sudo pip3 install picamera rtcbot
1920

20-
The installation will take a long time, since many of RTCBot's dependencies need to be compiled.
21+
.. warning::
22+
These instructions were made with reference to Raspbian Buster on the Raspberry Pi 4.
23+
Some things might work differently on older versions of raspbian.
2124

22-
Linux
25+
Ubuntu
2326
+++++++++++
2427

25-
Before starting, you will want to install OpenCV, numpy and ffmpeg::
28+
RTCbot requires several dependencies which are best installed using apt-get::
2629

27-
sudo apt-get install python3-numpy python3-opencv ffmpeg
30+
sudo apt-get install python3-numpy python3-cffi python3-aiohttp \
31+
libavformat-dev libavcodec-dev libavdevice-dev libavutil-dev \
32+
libswscale-dev libswresample-dev libavfilter-dev libopus-dev \
33+
libvpx-dev pkg-config libsrtp2-dev python3-opencv pulseaudio
2834

29-
Then, you can install rtcbot using pip::
35+
Then, you can install rtcbot with pip::
3036

31-
pip3 install rtcbot
37+
sudo pip3 install picamera rtcbot
3238

3339
Windows
3440
+++++++++++
3541

36-
To install on Windows, you will need to use Anaconda. With anaconda, install opencv, numpy, ...
42+
To install on Windows, you will want to use Anaconda `Anaconda <https://www.anaconda.com/distribution/#download-section>`_.
3743

38-
Then ...
44+
.. note::
45+
These instructions are incomplete. If you succeed in installing rtcbot
46+
on windows, please open a pull request with instructions!
3947

4048
Mac
4149
+++++++++++
4250

43-
On mac, follow the windows instructions - the library will only work through anaconda.
51+
To install on Mac, you will want to use Anaconda `Anaconda <https://www.anaconda.com/distribution/#download-section>`_.
52+
53+
54+
.. note::
55+
These instructions are incomplete. If you succeed in installing rtcbot
56+
on mac, please open a pull request with instructions!

examples/basics/rtcbot

Lines changed: 0 additions & 1 deletion
This file was deleted.

examples/mobile/README.md

Lines changed: 64 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ Rather than connecting to the robot, we will have two separate Python programs.
1010
```
1111

1212
In a previous tutorial, we developed a connection that streamed video to the browser. This tutorial will implement exactly the same functionality,
13-
but with a robot on a remote connection.
13+
but with the robot on a remote connection.
1414

1515
The browser-side code will remain unchanged - all of the work here will be in Python.
1616

1717
## Server Code
1818

19-
Most of the server code is identical. The only difference is that we set up a listener at `/ws`, which will establish a websocket connection with the robot:
19+
Most of the server code is unchanged. The only difference is that we set up a listener at `/ws`, which will establish a websocket connection with the robot:
2020

2121
```python
2222
ws = None # Websocket connection to the robot
@@ -140,7 +140,7 @@ web.run_app(app)
140140

141141
## Remote Code
142142

143-
In this tutorial, we will just run both server and robot on the local machine. The same code will work over the internet simply by setting the right IP for the robot to connect to. The robot connects to the server with a websocket, and waits for the message that will allow it to initialize its WebRTC connection.
143+
In this tutorial, we will just run both server and robot on the local machine. The robot connects to the server with a websocket, and waits for the message that will allow it to initialize its WebRTC connection.
144144

145145
```python
146146
import asyncio
@@ -171,12 +171,71 @@ finally:
171171

172172
With these two pieces of code, you first start the server, then start the robot, and finally open `http://localhost:8080` in the browser to view a video stream coming directly from the robot, even if the robot has an unknown IP.
173173

174+
## If it doesn't work over 4G
175+
176+
The above example should work for most people. However, some mobile network operators perform routing that disallows creating a direct WebRTC connection to a mobile device over 4G. If this is your situation, you need to use what is called a TURN server, which will forward data between the browser and robot.
177+
178+
```eval_rst
179+
.. note::
180+
You can check if your mobile operator allows such connections by using your phone to create a wifi hotspot, to which you can connect your robot. If video streaming works with the code above, you can ignore this section!
181+
```
182+
183+
```eval_rst
184+
.. warning::
185+
Because a TURN server essentially serves as a proxy through which an entire WebRTC connection is routed, it can send and receive quite a bit of data - make sure that you don't
186+
exceed your download and upload limits!
187+
```
188+
189+
While installing and configuring [coTURN](https://github.com/coturn/coturn) on linux is recommended for permanent setups,
190+
for simplicity we will run the [Pion TURN server](https://github.com/pion/turn) on the same computer that is running our server code.
191+
192+
The Pion server is easy to set up on Windows,Mac and Linux - all you need to do is [download the executable](https://github.com/pion/turn/releases/tag/1.0.3), and run it from the command line as shown.
193+
194+
**Linux/Mac**:
195+
```bash
196+
chmod +x ./simple-turn # allow executing the downloaded file
197+
export USERS='myusername=mypassword'
198+
export REALM=my.server.ip
199+
export UDP_PORT=3478
200+
./simple-turn-linux-amd64 # simple-turn-darwin-amd64 if on Mac
201+
```
202+
203+
**Windows**: You can run the following from powershell:
204+
```powershell
205+
$env:USERS = "myusername=mypassword"
206+
$env:REALM = "my.server.ip"
207+
$env:UDP_PORT = 3478
208+
./simple-turn-windows-amd64.exe
209+
```
210+
211+
With the server running, you will need to let both Python and Javascript know about it when creating your `RTCConnection`:
212+
```python
213+
from aiortc import RTCConfiguration, RTCIceServer
214+
215+
myConnection = RTCConnection(rtcConfiguration=RTCConfiguration([
216+
RTCIceServer(urls="stun:stun.l.google.com:19302"),
217+
RTCIceServer(urls="turn:my.server.ip:3478",
218+
username="myusername",credential="mypassword")
219+
]))
220+
```
221+
222+
```javascript
223+
var conn = new rtcbot.RTCConnection(rtcConfiguration=[
224+
{ urls: ["stun:stun.l.google.com:19302"] },
225+
{ urls: ["turn:my.server.ip:3478?transport=udp",
226+
username: "myusername", credential: "mypassword"], },
227+
]);
228+
```
229+
230+
With the above code, you should be able to stream video to your browser using 4G, even if your mobile operator disallows direct connections.
231+
232+
174233
## Summary
175234

176-
This tutorial split up the server and robot code into distinct pieces. Also introduced was rtcbot's websocket wrapper, allowing you to easily establish a data-only connection.
235+
This tutorial split up the server and robot code into distinct pieces. Also introduced was rtcbot's websocket wrapper, allowing you to easily establish a data-only connection. Finally, TURN servers were introduced, and instructions were given on how to set one up if direct connections fail.
177236

178237
## Extra Notes
179238

180239
Be aware that throughout these tutorials, all error handling and robustness was left out in the interest of
181-
clarity of the fundamental program flow. In reality, you will probably want to make sure that the connection
240+
clarity in the fundamental program flow. In reality, you will probably want to make sure that the connection
182241
did not have an error, and add the ability to connect and disconnect multiple times.

examples/mobile/rtcbot

Lines changed: 0 additions & 1 deletion
This file was deleted.

examples/offloading/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ The second part of the tutorial adds a neural network into the mix, which tries
1111

1212
```eval_rst
1313
.. note::
14-
While with a weak SBC like the Raspberry Pi there might be a non-negligible delay between sending a video frame and getting back a command, this is not a limitation of the approach, since it is possible to stream `video games with barely-noticeable lag <https://arstechnica.com/gaming/2019/03/googles-multiyear-quest-to-overcome-ids-stadia-streaming-skepticism/>`_. In particular, rtcbot currently cannot take advantage of the Pi's hardware acceleration, meaning that all video encoding is done in software, which ends up adding to video delay.
14+
While with a Raspberry Pi there might be a non-negligible delay between sending a video frame and getting back a command, this is not a limitation of the approach, since it is possible to stream `video games with barely-noticeable lag <https://arstechnica.com/gaming/2019/03/googles-multiyear-quest-to-overcome-ids-stadia-streaming-skepticism/>`_. In particular, rtcbot currently cannot take advantage of the Pi's hardware acceleration, meaning that all video encoding is done in software, which ends up adding to video delay.
1515
```
1616

1717
## Python to Python Streaming
@@ -137,6 +137,6 @@ This portion of the tutorial focuses on modifying the desktop code from the prev
137137
a neural network which is trained exploiting the computational power available on a desktop PC.
138138

139139
This is an _advanced_ topic, requiring a working knowledge of machine learning basics. It is also assumed
140-
that you are using linux on your desktop, and that you have an nvidia graphics card compatible with [pytorch](https://pytorch.org). If you choose to attempt this part on a windows desktop or on a mac, be aware that you might run into some problems that might need a bit of googling skills to solve.
140+
that you are using linux on your desktop, and that you have an nvidia graphics card compatible with [pytorch](https://pytorch.org). If you choose to attempt this part on a windows desktop or on a mac, be aware that you might run into some problems that might require some googling skills to solve.
141141

142142
### Under Construction...

examples/offloading/rtcbot

Lines changed: 0 additions & 1 deletion
This file was deleted.

examples/remotecontrol/rtcbot

Lines changed: 0 additions & 1 deletion
This file was deleted.

examples/streaming/rtcbot

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)