Skip to main content

RTCBot

PyPI npm Documentation Status Join the chat at https://gitter.im/rtcbot/community Tests

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.

The documentation includes tutorials that guide in developing your robot, starting from a basic connection between a Raspberry Pi and Browser, and encompass creating a video-streaming robot controlled entirely over a 4G mobile connection, all the way to a powerful system that offloads complex computation to a desktop PC in real-time.

All communication happens through WebRTC, using Python 3's asyncio and the wonderful aiortc library, meaning that your robot can be controlled with low latency both from the browser and through Python, even when it is not connected to your local network.

The library is explained piece by piece in the documentation.

See Documentation & Tutorials

Installing

RTCBot relies on some Python libraries that will need to be compiled by pip, so you need to install their dependencies. The following commands will work on Ubuntu and Raspbian Buster:

sudo apt-get install build-essential python3-numpy python3-cffi python3-aiohttp \
        libavformat-dev libavcodec-dev libavdevice-dev libavutil-dev \
        libswscale-dev libswresample-dev libavfilter-dev libopus-dev \
        libvpx-dev pkg-config libsrtp2-dev python3-opencv pulseaudio

And then install rtcbot:

sudo pip3 install rtcbot

For installation instructions for Windows and Mac, refer to the documentation

Example

This example uses RTCBot to live stream a webcam to the browser. For details, please look at the tutorials.

Python code that streams video to the browser:

from aiohttp import web
routes = web.RouteTableDef()

from rtcbot import RTCConnection, getRTCBotJS, CVCamera

camera = CVCamera()
# For this example, we use just one global connection
conn = RTCConnection()
conn.video.putSubscription(camera)

# Serve the RTCBot javascript library at /rtcbot.js
@routes.get("/rtcbot.js")
async def rtcbotjs(request):
    return web.Response(content_type="application/javascript", text=getRTCBotJS())

# This sets up the connection
@routes.post("/connect")
async def connect(request):
    clientOffer = await request.json()
    serverResponse = await conn.getLocalDescription(clientOffer)
    return web.json_response(serverResponse)

@routes.get("/")
async def index(request):
    with open("index.html", "r") as f:
        return web.Response(content_type="text/html", text=f.read())

async def cleanup(app=None):
    await conn.close()
    camera.close()

app = web.Application()
app.add_routes(routes)
app.on_shutdown.append(cleanup)
web.run_app(app)

Browser code (index.html) that displays the video stream:

<html>
  <head>
    <title>RTCBot: Video</title>
    <script src="/rtcbot.js"></script>
  </head>
  <body style="text-align: center;padding-top: 30px;">
    <video autoplay playsinline></video> <audio autoplay></audio>
    <p>
      Open the browser's developer tools to see console messages (CTRL+SHIFT+C)
    </p>
    <script>
      var conn = new rtcbot.RTCConnection();

      conn.video.subscribe(function (stream) {
        document.querySelector("video").srcObject = stream;
      });

      async function connect() {
        let offer = await conn.getLocalDescription();

        // POST the information to /connect
        let response = await fetch("/connect", {
          method: "POST",
          cache: "no-cache",
          body: JSON.stringify(offer),
        });

        await conn.setRemoteDescription(await response.json());

        console.log("Ready!");
      }
      connect();
    </script>
  </body>
</html>

Development

To use rtcbot code directly, clone the repository, and install the requirements:

pip install -r requirements.txt

Then, you will need to perform the javascript build step, to prepare the browser code. This step requires both make and npm to be installed on your machine. To build the javascript, type in:

make js

This creates rtcbot/rtcbot.js, which is returned by rtcbot.getRTCBotJS().

After these two steps, you should be able to successfully import and use the library.

Metadata

Release files for rtcbot 0.2.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for rtcbot 0.2.5
File Size Uploaded
rtcbot-0.2.5.tar.gz 2.3 MB Details

Built distribution (wheel)

Table of built distributions (wheels) for rtcbot 0.2.5
File Interpreter ABI Platform
rtcbot-0.2.5-py3-none-any.whl Python 3 none any Details

Total release size: 2.4 MB

Release files / rtcbot-0.2.5.tar.gz

Download URL rtcbot-0.2.5.tar.gz
Size 2.3 MB
Tags Source
SHA-256 checksum
How to use checksums
9874e3f9e4fd1fdcd6f68c2f9c75445a8fe65a5c3ba89e44a825ff6176de70b6
BLAKE2b-256 checksum
How to use checksums
b38f1093211cab368ce754a564a8e7e44f562c925f68cd585ca9c9917edf95a7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.3

Release files / rtcbot-0.2.5-py3-none-any.whl

Download URL rtcbot-0.2.5-py3-none-any.whl
Size 43.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a664cd520988f7b3b3fcadc5013abfd7ee0bff0512d64fbb319529fe9f7bace2
BLAKE2b-256 checksum
How to use checksums
3390f34bad6afa3e55b3f4b2017e4fb7da848924d6d5625fca886377ab8772a1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.3

Release history Release notifications | RSS feed

This release

0.2.5 This release

2 release files

0.2.4

2 release files

0.2.3

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.0

2 release files

0.0.9

2 release files

0.0.8

2 release files

0.0.7

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page