Skip to content

Commit 2d03ed6

Browse files
committed
Added tutorial on ThreadedSubscriptionProducer
This makes it easier to see how to use sensors and other external things with RTCBot. Also fixed a couple little bugs in closing data channels that are being used to send data.
1 parent 0fe6ae6 commit 2d03ed6

28 files changed

+513
-52
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ async def index(request):
7474
with open("index.html", "r") as f:
7575
return web.Response(content_type="text/html", text=f.read())
7676

77-
async def cleanup(app):
77+
async def cleanup(app=None):
7878
await conn.close()
7979
camera.close()
8080

docs/installing.rst

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,16 @@ Then, you can complete the installation with pip::
2222

2323
sudo pip3 install rtcbot
2424

25+
.. warning::
26+
You might need to reboot your Pi for RTCBot to work! If rtcbot freezes on import, it means that you need to start PulseAudio.
27+
2528
.. note::
2629
It is recommended that you use the Pi 4 with RTCBot. While it was tested to work down to the Raspberry Pi 3B, it was observed to have
2730
extra latency, since the CPU had difficulty keeping up with encoding the video stream while processing controller input.
2831
This is because RTCBot currently cannot take advantage of the Pi's hardware acceleration,
2932
meaning that all video encoding is done in software.
3033

3134
.. note::
32-
You might need to reboot your Pi for RTCBot to work!
33-
34-
.. warning::
3535
These instructions were made with reference to Raspbian Buster.
3636
While the library *does* work on Raspbian Stretch,
3737
you'll need to install aiohttp through pip, and avoid installing opencv.
@@ -50,8 +50,8 @@ Then, you can complete the installation with pip::
5050

5151
sudo pip3 install rtcbot
5252

53-
.. note::
54-
You might need to reboot, or manually start pulseaudio if it was not previously installed.
53+
.. warning::
54+
You might need to reboot, or manually start PulseAudio if it was not previously installed. If RTCBot freezes on import, it means that PulseAudio is not running.
5555

5656
Mac
5757
+++++++++++

docs/javascript.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ Next, to establish the connection with Python, you include the Python counterpar
6969
return web.json_response(response)
7070

7171

72-
async def cleanup(app):
72+
async def cleanup(app=None):
7373
if conn is not None:
7474
await conn.close()
7575

examples/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ The full code for each of the tutorials can be seen in the `examples directory <
1919
remotecontrol/README.md
2020
mobile/README.md
2121
offloading/README.md
22+
threads/README.md

examples/mobile/README.md

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ async def index(request):
125125
</html>
126126
""")
127127

128-
async def cleanup(app):
128+
async def cleanup(app=None):
129129
global ws
130130
if ws is not None:
131131
c = ws.close()
@@ -171,10 +171,9 @@ 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-
175174
## rtcbot.dev
176175

177-
The above example requires you to have your own internet-accessible server at a known IP address to set up the connection, if your remote code is not on your local network. The server's only real purpose is to help *establish* a connection - once the connection is established, it does not do anything.
176+
The above example requires you to have your own internet-accessible server at a known IP address to set up the connection, if your remote code is not on your local network. The server's only real purpose is to help _establish_ a connection - once the connection is established, it does not do anything.
178177

179178
For this reason, I am hosting a free testing server online at `https://rtcbot.dev` that performs the equivalent of the following operation from the above server code:
180179

@@ -226,11 +225,12 @@ and the local browser's connection code becomes:
226225

227226
```js
228227
let response = await fetch("https://rtcbot.dev/myRandomSequence11", {
229-
method: "POST",
230-
cache: "no-cache",
231-
body: JSON.stringify(offer)
228+
method: "POST",
229+
cache: "no-cache",
230+
body: JSON.stringify(offer),
232231
});
233232
```
233+
234234
With `rtcbot.dev`, you no longer need your local server code to run websockets or a connection service. Its only purpose is to give the browser the html and javascript necessary to establish a connection. We will get rid of the browser entirely in the next tutorial.
235235

236236
## If it doesn't work over 4G
@@ -255,6 +255,7 @@ There are two options through which to setup a TURN server: [coTURN](https://git
255255
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.
256256

257257
**Linux/Mac**:
258+
258259
```bash
259260
chmod +x ./simple-turn-linux-amd64 # allow executing the downloaded file
260261
export USERS='myusername=mypassword'
@@ -264,6 +265,7 @@ export UDP_PORT=3478
264265
```
265266

266267
**Windows**: You can run the following from powershell:
268+
267269
```powershell
268270
$env:USERS = "myusername=mypassword"
269271
$env:REALM = "my.server.ip"
@@ -272,6 +274,7 @@ $env:UDP_PORT = 3478
272274
```
273275

274276
With the Pion server running, you will need to let both Python and Javascript know about it when creating your `RTCConnection`:
277+
275278
```python
276279
from aiortc import RTCConfiguration, RTCIceServer
277280

@@ -286,7 +289,7 @@ myConnection = RTCConnection(rtcConfiguration=RTCConfiguration([
286289
var conn = new rtcbot.RTCConnection(true, {
287290
iceServers:[
288291
{ urls: ["stun:stun.l.google.com:19302"] },
289-
{ urls: "turn:my.server.ip:3478?transport=udp",
292+
{ urls: "turn:my.server.ip:3478?transport=udp",
290293
username: "myusername", credential: "mypassword", },
291294
]);
292295
```
@@ -296,6 +299,7 @@ var conn = new rtcbot.RTCConnection(true, {
296299
Setting up a coTURN server takes a bit more work and is only supported on Linux and Mac. The following steps will assume a Linux system running Ubuntu.
297300
298301
Install coTURN and stop the coTURN service to modify config files with
302+
299303
```bash
300304
sudo apt install coturn
301305
sudo systemctl stop coturn
@@ -304,6 +308,7 @@ sudo systemctl stop coturn
304308
Edit the file `/etc/default/coturn` by uncommenting the line `TURNSERVER_ENABLED=1`. This will allow coTURN to start in daemon mode on boot.
305309
306310
Edit another file `/etc/turnserver.conf` and add the following lines. Be sure to put your system's public facing IP address in place of `<PUBLIC_NETWORK_IP>`, your domain name in place of `<DOMAIN>`, and your own credentials in place of `<USERNAME>` and `<PASSWORD>`.
311+
307312
```
308313
listening-port=3478
309314
tls-listening-port=5349
@@ -323,13 +328,15 @@ lt-cred-mech
323328
```
324329
325330
Restart the coTURN service, check that it's running, and reboot.
331+
326332
```bash
327333
sudo systemctl start coturn
328334
sudo systemctl status coturn
329335
sudo reboot
330336
```
331337
332338
With the coTURN server running, you will need to let both Python and Javascript know about it when creating your `RTCConnection`:
339+
333340
```python
334341
from aiortc import RTCConfiguration, RTCIceServer
335342

@@ -344,7 +351,7 @@ myConnection = RTCConnection(rtcConfiguration=RTCConfiguration([
344351
var conn = new rtcbot.RTCConnection(true, {
345352
iceServers:[
346353
{ urls: ["stun:stun.l.google.com:19302"] },
347-
{ urls: "turn:<PUBLIC_NETWORK_IP:3478?transport=udp",
354+
{ urls: "turn:<PUBLIC_NETWORK_IP:3478?transport=udp",
348355
username: "myusername", credential: "mypassword", },
349356
]);
350357
```

examples/mobile/server.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ async def index(request):
8484
)
8585

8686

87-
async def cleanup(app):
87+
async def cleanup(app=None):
8888
global ws
8989
if ws is not None:
9090
c = ws.close()

examples/offloading/README.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ and an average desktop.
55

66
The ideal situation would be if you could strap an entire desktop to your robot. With RTCBot, we can do the next best thing: we can stream the robot's inputs to a desktop, which can then perform computation, and send back commands.
77

8-
In this tutorial, we will go back to a single file for both server and robot for simplicitly. The tutorial is split into two parts. In the first part, we set up a connection to the robot from Python, allowing you to control the robot with an xbox controller without a browser.
9-
10-
The second part of the tutorial adds a neural network into the mix, which tries to imitate the xbox controller's controls.
8+
In this tutorial, we will go back to a single file for both server and robot for simplicitly. We set up a connection to the robot from Python, allowing you to control the robot with an xbox controller without a browser.
119

1210
```eval_rst
1311
.. note::
@@ -130,4 +128,3 @@ Control message: {'timestamp': 1553379217.004892, 'code': 'ABS_X', 'state': -125
130128
.. warning::
131129
The output for the `Gamepad` object is currently different in Javascript and in Python. Make sure you don't mix them up!
132130
```
133-

examples/offloading/robot.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ async def connect(request):
2323
return web.json_response(serverResponse)
2424

2525

26-
async def cleanup(app):
26+
async def cleanup(app=None):
2727
await conn.close()
2828
cam.close()
2929

examples/remotecontrol/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ async def index(request):
6868
</html>
6969
""")
7070

71-
async def cleanup(app):
71+
async def cleanup(app=None):
7272
await conn.close()
7373

7474
app = web.Application()
@@ -152,7 +152,7 @@ We now add keyboard support. This is done with the `rtcbot.Keyboard` javascript
152152
)
153153

154154

155-
async def cleanup(app):
155+
async def cleanup(app=None):
156156
await conn.close()
157157

158158
app = web.Application()
@@ -349,7 +349,7 @@ async def index(request):
349349
</html>
350350
""")
351351

352-
async def cleanup(app):
352+
async def cleanup(app=None):
353353
await conn.close()
354354

355355
app = web.Application()

examples/remotecontrol/gamepad.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ async def index(request):
7272
)
7373

7474

75-
async def cleanup(app):
75+
async def cleanup(app=None):
7676
await conn.close()
7777

7878

0 commit comments

Comments
 (0)