### Bash Script for Pion TURN Server Setup (Linux/Mac) Source: https://github.com/dkumor/rtcbot/blob/master/examples/mobile/README.md This bash script configures and starts the Pion TURN server on Linux or macOS. It makes the executable file runnable, sets environment variables for user credentials, realm, and UDP port, and then launches the server. Ensure you replace placeholders with your actual values. ```bash chmod +x ./simple-turn-linux-amd64 # allow executing the downloaded file export USERS='myusername=mypassword' export REALM=my.server.ip export UDP_PORT=3478 ./simple-turn-linux-amd64 # simple-turn-darwin-amd64 if on Mac ``` -------------------------------- ### Display Live Video Feed with CVCamera and CVDisplay Subscription Source: https://github.com/dkumor/rtcbot/blob/master/examples/basics/README.md Shows how to capture a live video feed using CVCamera, subscribe to its frames, and display them using CVDisplay. This example sets up a receiver function to get frames from the subscription and put them onto the display. ```python import asyncio from rtcbot import CVCamera, CVDisplay camera = CVCamera() display = CVDisplay() frameSubscription = camera.subscribe() async def receiver(): while True: frame = await frameSubscription.get() display.put_nowait(frame) asyncio.ensure_future(receiver()) try: asyncio.get_event_loop().run_forever() finally: camera.close() display.close() ``` -------------------------------- ### Complete Robot Example with RTCBot Integration Source: https://context7.com/dkumor/rtcbot/llms.txt A comprehensive example integrating various RTCBot functionalities for a remote-controlled robot. This setup includes video streaming from a camera, control via a gamepad, and sensor feedback through a serial connection to an Arduino. It utilizes 'aiohttp' for web serving and 'rtcbot' for WebRTC communication. Dependencies include 'aiohttp', 'rtcbot', 'asyncio'. ```python from aiohttp import web from rtcbot import RTCConnection, CVCamera, Gamepad, SerialConnection, getRTCBotJS import asyncio # Setup hardware camera = CVCamera(width=640, height=480, fps=30) arduino = SerialConnection( url="/dev/ttyACM0", baudrate=115200, writeFormat=" RTCBot: Basic

Click the Button

Open the browser's developer tools to see console messages (CTRL+SHIFT+C)

''') app = web.Application() app.add_routes(routes) web.run_app(app) ``` -------------------------------- ### Combining Video and Audio Streams with RTCBot Source: https://github.com/dkumor/rtcbot/blob/master/examples/basics/README.md Example showing how to simultaneously display a video feed and play audio input through headphones. It utilizes CVCamera, CVDisplay, Microphone, and Speaker, demonstrating RTCBot's unified API for different media types. ```python import asyncio from rtcbot import CVCamera, CVDisplay, Microphone, Speaker camera = CVCamera() display = CVDisplay() microphone = Microphone() speaker = Speaker() display.putSubscription(camera) speaker.putSubscription(microphone) try: asyncio.get_event_loop().run_forever() finally: camera.close() display.close() microphone.close() speaker.close() ``` -------------------------------- ### Python: Basic RTC Connection Setup (Initial Version) Source: https://github.com/dkumor/rtcbot/blob/master/examples/multiconnect/README.md A simplified initial version demonstrating a basic RTC connection setup. This version uses a global connection and lacks robust handling for multiple connections or reconnections. ```python # This sets up the connection @routes.post("/connect") async def connect(request): clientOffer = await request.json() # Set up the connection serverResponse = await conn.getLocalDescription(clientOffer) return web.json_response(serverResponse) async def cleanup(app=None): await conn.close() camera.close() ``` -------------------------------- ### GET / Source: https://github.com/dkumor/rtcbot/blob/master/examples/webrtc/README.md Serves the main HTML page for the RTCBot application, including the necessary JavaScript for client-side WebRTC interaction. ```APIDOC ## GET / ### Description This endpoint serves the main HTML page for the RTCBot application. It includes embedded JavaScript that initializes the WebRTC connection and handles button clicks, sending messages to the server. ### Method GET ### Endpoint / ### Response #### Success Response (200) - **content_type** (string) - 'text/html' - **text** (string) - The HTML content of the page. ### Response Example ```html RTCBot: Data Channel

Click the Button

Open the browser's developer tools to see console messages (CTRL+SHIFT+C)

``` ``` -------------------------------- ### Run Basic Asyncio Event Loop Source: https://github.com/dkumor/rtcbot/blob/master/examples/basics/README.md Demonstrates the most basic asyncio program structure, which involves getting the event loop and running it indefinitely. This program serves as a foundation for understanding asyncio applications. It requires the 'asyncio' library, which is built into Python 3. ```python import asyncio # Run the event loop asyncio.get_event_loop().run_forever() ``` -------------------------------- ### WebRTC Data Connection Setup with RTCBot Source: https://github.com/dkumor/rtcbot/blob/master/examples/webrtc/README.md Integrates the RTCBot library into an aiohttp server to establish a WebRTC data connection. This snippet includes serving the RTCBot JavaScript library and setting up a message handler for incoming data from the browser. It demonstrates the initial setup for bidirectional communication. ```python from aiohttp import web routes = web.RouteTableDef() from rtcbot import RTCConnection, getRTCBotJS # For this example, we use just one global connection conn = RTCConnection() @conn.subscribe def onMessage(msg): # Called when each message is sent print("Got message:", msg) # 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()) ``` -------------------------------- ### Configure Raspberry Pi Camera with PiCamera Source: https://context7.com/dkumor/rtcbot/llms.txt Example of using PiCamera for native Raspberry Pi camera module support. Demonstrates basic configuration including rotation, resolution, and frame rate for optimized CSI interface streaming. ```python from rtcbot import PiCamera, RTCConnection import asyncio # Basic Pi camera with rotation camera = PiCamera(rotation=180, width=1280, height=720, fps=30) ``` -------------------------------- ### Python: Full RTCBot Web Server Setup Source: https://github.com/dkumor/rtcbot/blob/master/examples/webrtc/README.md This Python code sets up a complete web server using `aiohttp` and `rtcbot` to handle WebRTC connections. It includes routes for serving the RTCBot JavaScript library, handling connection offers, and serving the main HTML page with embedded JavaScript for client-side logic. It manages the `RTCConnection` lifecycle, including shutdown. ```python from aiohttp import web routes = web.RouteTableDef() from rtcbot import RTCConnection, getRTCBotJS conn = RTCConnection() # For this example, we use just one global connection @conn.subscribe def onMessage(msg): # Called when messages received from browser print("Got message:", msg["data"]) conn.put_nowait({"data": "pong"}) # 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): return web.Response( content_type="text/html", text=""" RTCBot: Data Channel

Click the Button

Open the browser's developer tools to see console messages (CTRL+SHIFT+C)

""") async def cleanup(app=None): await conn.close() app = web.Application() app.add_routes(routes) app.on_shutdown.append(cleanup) web.run_app(app) ``` -------------------------------- ### GET /rtcbot.js Source: https://github.com/dkumor/rtcbot/blob/master/examples/webrtc/README.md Serves the RTCBot JavaScript library, which is essential for establishing and managing WebRTC connections from the browser. ```APIDOC ## GET /rtcbot.js ### Description This endpoint serves the RTCBot JavaScript library. Including this script in your HTML allows the browser to create and manage WebRTC connections with the server. ### Method GET ### Endpoint /rtcbot.js ### Response #### Success Response (200) - **content_type** (string) - 'application/javascript' - **text** (string) - The content of the RTCBot JavaScript library. ### Response Example ```javascript // Content of the RTCBot javascript library // ... (minified or regular javascript code) ... ``` ``` -------------------------------- ### PowerShell Script for Pion TURN Server Setup (Windows) Source: https://github.com/dkumor/rtcbot/blob/master/examples/mobile/README.md This PowerShell script sets up and runs the Pion TURN server on Windows. It configures the necessary environment variables for user credentials, realm, and UDP port before executing the server application. Remember to use your specific credentials and server IP. ```powershell $env:USERS = "myusername=mypassword" $env:REALM = "my.server.ip" $env:UDP_PORT = 3478 ./simple-turn-windows-amd64.exe ``` -------------------------------- ### Install and Stop coTURN Service Source: https://github.com/dkumor/rtcbot/blob/master/examples/mobile/README.md Installs the coTURN package and stops the coTURN service, preparing for configuration file modifications. This is a prerequisite for setting up the TURN server on Linux systems. ```bash sudo apt install coturn sudo systemctl stop coturn ``` -------------------------------- ### Python Server Setup for RTCBot Source: https://github.com/dkumor/rtcbot/blob/master/docs/javascript.rst This snippet outlines the Python server-side code required to serve the RTCBot Javascript library and handle the connection requests from the browser. ```APIDOC ## Python Server Implementation for RTCBot ### Description This section provides the Python code necessary for a web server to support RTCBot functionality. It includes serving the RTCBot Javascript file and handling the WebRTC connection negotiation process. ### Method GET, POST ### Endpoint - `/` (Serves index.html) - `/rtcbot.js` (Serves the RTCBot Javascript library) - `/connect` (Handles connection offers from the client) ### Parameters #### Query Parameters N/A #### Path Parameters N/A #### Request Body - For `/connect` POST request: JSON object containing the client's WebRTC offer. ### Request Example (Python Server) ```python from aiohttp import web routes = web.RouteTableDef() from rtcbot import RTCConnection, getRTCBotJS conn = None @routes.get("/") # Serve the html file async def index(request): with open("index.html", "r") as f: return web.Response(content_type="text/html", text=f.read()) # 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()) @routes.post("/connect") async def connect(request): global conn clientOffer = await request.json() conn = RTCConnection() response = await conn.getLocalDescription(clientOffer) return web.json_response(response) async def cleanup(app=None): if conn is not None: await conn.close() app = web.Application() app.add_routes(routes) app.on_shutdown.append(cleanup) web.run_app(app, port=8080) ``` ### Response #### Success Response (200) - For `/connect` POST request: JSON object containing the server's WebRTC answer. #### Response Example (Server Response to client) ```json { "sdp": "v=0...", "type": "answer" } ``` ``` -------------------------------- ### Gamepad Input Handling with rtcbot Source: https://context7.com/dkumor/rtcbot/llms.txt This example shows how to capture and process gamepad input using the rtcbot library. It includes basic event reading, streaming gamepad data over WebRTC, and controlling a robot's speed and direction based on analog stick movements. ```python import asyncio from rtcbot import Gamepad, RTCConnection # Basic gamepad reading gamepad = Gamepad() async def read_gamepad(): subscription = gamepad.subscribe() while True: event = await subscription.get() print(f"Event: {event['event']}, Code: {event['code']}, State: {event['state']}") # Example output: {'timestamp': 12345.67, 'event': 'Key', 'code': 'BTN_SOUTH', 'state': 1} # Stream gamepad to robot via WebRTC conn = RTCConnection() @gamepad.subscribe def forward_gamepad(event): conn.put_nowait({"type": "gamepad", "data": event}) # Control robot based on gamepad speed = 0.0 direction = 0.0 @gamepad.subscribe def control_robot(event): global speed, direction if event['code'] == 'ABS_Y': # Left stick Y axis speed = -event['state'] / 32768.0 # Normalize to -1.0 to 1.0 elif event['code'] == 'ABS_RX': # Right stick X axis direction = event['state'] / 32768.0 print(f"Speed: {speed:.2f}, Direction: {direction:.2f}") # Send to motors here asyncio.get_event_loop().run_forever() ``` -------------------------------- ### RTCBot Python Server Setup for Browser Communication Source: https://github.com/dkumor/rtcbot/blob/master/docs/javascript.rst Provides the Python code to set up a web server using aiohttp for RTCBot communication. It includes serving the RTCBot.js file, handling connection offers from the browser, and managing the RTC connection lifecycle. This code requires the aiohttp and rtcbot libraries. ```python from aiohttp import web routes = web.RouteTableDef() from rtcbot import RTCConnection, getRTCBotJS conn = None @routes.get("/") async def index(request): with open("index.html", "r") as f: return web.Response(content_type="text/html", text=f.read()) @routes.get("/rtcbot.js") async def rtcbotjs(request): return web.Response(content_type="application/javascript", text=getRTCBotJS()) @routes.post("/connect") async def connect(request): global conn clientOffer = await request.json() conn = RTCConnection() response = await conn.getLocalDescription(clientOffer) return web.json_response(response) async def cleanup(app=None): if conn is not None: await conn.close() app = web.Application() app.add_routes(routes) app.on_shutdown.append(cleanup) web.run_app(app, port=8080) ``` -------------------------------- ### Start and Verify coTURN Service Source: https://github.com/dkumor/rtcbot/blob/master/examples/mobile/README.md Restarts the coTURN service, checks its status to ensure it is running correctly, and then reboots the system. These commands are crucial for applying the coTURN configuration changes and ensuring the server operates as expected. ```bash sudo systemctl start coturn sudo systemctl status coturn sudo reboot ``` -------------------------------- ### JavaScript: Establish WebRTC Connection and Send Data Source: https://github.com/dkumor/rtcbot/blob/master/examples/webrtc/README.md This JavaScript code demonstrates how to establish a WebRTC connection from the client-side. It generates a local description (offer), sends it to the server's '/connect' endpoint, and then sets the remote description using the server's response. It also includes an example of sending data back to the Python server. ```javascript var conn = new rtcbot.RTCConnection(); async function connect() { let offer = await conn.getLocalDescription(); let response = await fetch("/connect", { method: "POST", cache: "no-cache", body: JSON.stringify(offer), }); await conn.setRemoteDescription(await response.json()); console.log("Ready!"); } connect(); var mybutton = document.querySelector("#mybutton"); mybutton.onclick = function () { conn.put_nowait("Button Clicked!"); }; ``` -------------------------------- ### Remote Robot Connection Setup (Python) Source: https://github.com/dkumor/rtcbot/blob/master/examples/mobile/README.md This Python snippet demonstrates how a remote robot connects to a server using a websocket to establish a WebRTC connection. It initializes a camera and an RTC connection, then uses a websocket to exchange connection descriptions with the server. ```python import asyncio from rtcbot import Websocket, RTCConnection, CVCamera cam = CVCamera() conn = RTCConnection() conn.video.putSubscription(cam) # Connect establishes a websocket connection to the server, # and uses it to send and receive info to establish webRTC connection. async def connect(): ws = Websocket("http://localhost:8080/ws") remoteDescription = await ws.get() robotDescription = await conn.getLocalDescription(remoteDescription) ws.put_nowait(robotDescription) print("Started WebRTC") await ws.close() asyncio.ensure_future(connect()) try: asyncio.get_event_loop().run_forever() finally: cam.close() conn.close() ``` -------------------------------- ### Python Global RTCConnection Example (For Contrast) Source: https://github.com/dkumor/rtcbot/blob/master/examples/multiconnect/README.md This snippet illustrates the previous approach of using a single global RTCConnection object. It shows how a camera is associated with this connection for video subscription. This is provided as a contrast to the new method of handling multiple connections, highlighting the limitations of the global approach. ```python camera = CVCamera() # For this example, we use just one global connection conn = RTCConnection() # Subscribe to the video feed conn.video.putSubscription(camera) ``` -------------------------------- ### Full RTCBot Remote Control System Example (Python) Source: https://github.com/dkumor/rtcbot/blob/master/examples/remotecontrol/README.md This is the complete Python code for setting up a remote control system using RTCBot. It includes setting up an aiohttp web server, initializing an RTC connection, subscribing to camera input, and handling incoming messages to control robot movement based on keyboard inputs. It also serves the necessary RTCBot JavaScript library. ```python from aiohttp import web routes = web.RouteTableDef() from rtcbot import RTCConnection, CVCamera, getRTCBotJS cam = CVCamera() # For this example, we use just one global connection conn = RTCConnection() conn.video.putSubscription(cam) keystates = {"w": False, "a": False, "s": False, "d": False} @conn.subscribe def onMessage(m): global keystates if m["keyCode"] == 87: # W keystates["w"] = m["type"] == "keydown" elif m["keyCode"] == 83: # S keystates["s"] = m["type"] == "keydown" elif m["keyCode"] == 65: # A keystates["a"] = m["type"] == "keydown" elif m["keyCode"] == 68: # D keystates["d"] = m["type"] == "keydown" print({ "forward": keystates["w"] * 1 - keystates["s"] * 1, "leftright": keystates["d"] * 1 - keystates["a"] * 1, }) # 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()) ``` -------------------------------- ### Basic Serial Communication with Arduino using Python Source: https://github.com/dkumor/rtcbot/blob/master/docs/arduino.rst Demonstrates sending a 'Hello World!' message to an Arduino and continuously reading incoming serial data. This example assumes a USB connection and uses asyncio for asynchronous operations. It reads data line by line by default. ```python import asyncio from rtcbot.arduino import SerialConnection conn = SerialConnection("/dev/ttyAMA1") async def sendAndReceive(conn): conn.put_nowait("Hello world!") while True: msg = await conn.get().decode('ascii') print(msg) await asyncio.sleep(1) asyncio.ensure_future(sendAndReceive(conn)) asyncio.get_event_loop().run_forever() ``` -------------------------------- ### RTCBot JavaScript Client API Source: https://context7.com/dkumor/rtcbot/llms.txt This JavaScript code snippet demonstrates the client-side implementation for RTCBot. It initializes an RTCConnection and a Gamepad object, subscribes to video streams, sends gamepad events to the server, and updates the UI with sensor data received from the server. It also handles the initial connection setup by sending a WebRTC offer to the server. ```javascript var conn = new rtcbot.RTCConnection(); var gamepad = new rtcbot.Gamepad(); conn.video.subscribe(s => { document.getElementById("video").srcObject = s; }); gamepad.subscribe(e => { conn.put_nowait({type: "gamepad", data: e}); }); conn.subscribe(msg => { if (msg.type === "sensors") { document.getElementById("sensors").textContent = `Distance: ${msg.distance.toFixed(1)} cm, Battery: ${msg.battery.toFixed(2)} V`; } }); async function start() { let offer = await conn.getLocalDescription(); let resp = await fetch("/connect", { method: "POST", headers: {"Content-Type": "application/json"}, body: JSON.stringify(offer) }); await conn.setRemoteDescription(await resp.json()); } start(); ``` -------------------------------- ### Python Web Server Setup for RTCBot Video Streaming Source: https://github.com/dkumor/rtcbot/blob/master/examples/multiconnect/README.md Sets up a basic aiohttp web server to serve an HTML page with RTCBot JavaScript, handle WebSocket connections for RTC, and stream video from a camera. It includes routes for serving the RTCBot JS library, the main HTML page, and handling client connection offers. ```python from aiohttp import web routes = web.RouteTableDef() from rtcbot import RTCConnection, getRTCBotJS, CVCamera camera = CVCamera() # This sets up the connection @routes.post("/connect") async def connect(request): clientOffer = await request.json() ## WHAT GOES HERE? ## return web.json_response(serverResponse) async def cleanup(app=None): camera.close() # 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()) # Serve the webpage! @routes.get("/") async def index(request): return web.Response( content_type="text/html", text=""" RTCBot: Video

Open the browser's developer tools to see console messages (CTRL+SHIFT+C)

""", ) app = web.Application() app.add_routes(routes) app.on_shutdown.append(cleanup) web.run_app(app) ``` -------------------------------- ### Basic Microphone to Speaker Echo (Python) Source: https://github.com/dkumor/rtcbot/blob/master/docs/audio.rst This example demonstrates a fundamental audio loop where the microphone input is directly routed to the speaker output, creating an echo effect. It requires the 'rtcbot' library and 'asyncio' for asynchronous operations. The 'Microphone' captures audio, and 'Speaker' plays it back. ```python import asyncio from rtcbot import Microphone, Speaker microphone = Microphone() speaker = Speaker() speaker.putSubscription(microphone) try: asyncio.get_event_loop().run_forever() finally: microphone.close() speaker.close() ``` -------------------------------- ### Python Web Server Setup and Connection Handling for RTCBot Source: https://github.com/dkumor/rtcbot/blob/master/examples/remotecontrol/README.md This Python code sets up a web server using aiohttp to handle WebRTC connections. It defines routes for the main HTML page and an API endpoint to exchange WebRTC session descriptions. It also manages the server shutdown process. ```python from aiohttp import web routes = web.RouteTableDef() # 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): return web.Response( content_type="text/html", text=r""" RTCBot: Remote Control

Open the browser's developer tools to see console messages (CTRL+SHIFT+C)

""") async def cleanup(app=None): await conn.close() app = web.Application() app.add_routes(routes) app.on_shutdown.append(cleanup) web.run_app(app) ``` -------------------------------- ### Bidirectional WebRTC Streaming with RTCBot (Python & JavaScript) Source: https://github.com/dkumor/rtcbot/blob/master/examples/streaming/README.md This example demonstrates setting up bidirectional audio and video streaming between a Python server and a web browser using RTCBot. It includes Python code for the server-side logic and HTML/JavaScript for the client-side connection. The Python code initializes the RTC connection, sets up video and audio subscriptions, and serves the RTCBot JavaScript library. The JavaScript code handles establishing the WebRTC connection, capturing media streams, and sending them to the server. ```python from aiohttp import web routes = web.RouteTableDef() from rtcbot import RTCConnection, getRTCBotJS, CVDisplay, Speaker display = CVDisplay() speaker = Speaker() # For this example, we use just one global connection conn = RTCConnection() display.putSubscription(conn.video.subscribe()) speaker.putSubscription(conn.audio.subscribe()) # 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): return web.Response( content_type="text/html", text=r""" RTCBot: Skeleton

Open the browser's developer tools to see console messages (CTRL+SHIFT+C)

""" ) async def cleanup(app=None): await conn.close() display.close() speaker.close() app = web.Application() app.add_routes(routes) app.on_shutdown.append(cleanup) web.run_app(app) ``` ```javascript var conn = new rtcbot.RTCConnection(); async function connect() { let streams = await navigator.mediaDevices.getUserMedia({audio: true, video: true}); conn.video.putSubscription(streams.getVideoTracks()[0]); conn.audio.putSubscription(streams.getAudioTracks()[0]); 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(); ``` -------------------------------- ### Shorthand Video Display using putSubscription Source: https://github.com/dkumor/rtcbot/blob/master/examples/basics/README.md A more concise way to display a live video feed by using the `putSubscription` method, which simplifies the process of receiving frames from a subscription and displaying them. ```python import asyncio from rtcbot import CVCamera, CVDisplay camera = CVCamera() display = CVDisplay() frameSubscription = camera.subscribe() display.putSubscription(frameSubscription) try: asyncio.get_event_loop().run_forever() finally: camera.close() display.close() ``` -------------------------------- ### Multiple Video Feeds to Different Displays Source: https://github.com/dkumor/rtcbot/blob/master/examples/basics/README.md Illustrates how to display the same video feed in two separate windows. It shows the use of multiple `CVDisplay` instances and how `camera.subscribe()` allows for independent subscriptions to the same source. ```python import asyncio from rtcbot import CVCamera, CVDisplay camera = CVCamera() display = CVDisplay() display2 = CVDisplay() display.putSubscription(camera) subscription2 = camera.subscribe() display2.putSubscription(subscription2) try: asyncio.get_event_loop().run_forever() finally: camera.close() display.close() display2.close() ``` -------------------------------- ### Install RTCBot Dependencies and Library (Bash) Source: https://github.com/dkumor/rtcbot/blob/master/README.md Installs necessary build tools and Python libraries for RTCBot on Ubuntu and Raspbian Buster. This includes packages for compilation, audio/video codecs, and OpenCV, followed by the RTCBot package itself. ```bash 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 sudo pip3 install rtcbot ``` -------------------------------- ### Establish WebRTC Connection and Display Video with Gamepad Control (Python) Source: https://github.com/dkumor/rtcbot/blob/master/examples/offloading/README.md This Python script sets up a WebRTC connection, subscribes to video frames for display, and integrates gamepad input for robot control. It manually initiates the connection by posting the local description to a server and configuring the remote description from the response. Dependencies include asyncio, aiohttp, cv2, json, and rtcbot. ```python import asyncio import aiohttp import cv2 import json from rtcbot import RTCConnection, Gamepad, CVDisplay disp = CVDisplay() g = Gamepad() conn = RTCConnection() @conn.video.subscribe def onFrame(frame): # Show a 4x larger image so that it is easy to see resized = cv2.resize(frame, (frame.shape[1] * 4, frame.shape[0] * 4)) disp.put_nowait(resized) async def connect(): localDescription = await conn.getLocalDescription() async with aiohttp.ClientSession() as session: async with session.post( "http://localhost:8080/connect", data=json.dumps(localDescription) ) as resp: response = await resp.json() await conn.setRemoteDescription(response) # Start sending gamepad controls g.subscribe(conn) asyncio.ensure_future(connect()) try: asyncio.get_event_loop().run_forever() finally: conn.close() disp.close() g.close() ``` -------------------------------- ### Asyncio Receiver and Sender with Queue Source: https://github.com/dkumor/rtcbot/blob/master/examples/basics/README.md Demonstrates an asynchronous receiver function that awaits data from a queue and prints it. It also shows how to ensure sender and receiver futures are running and keep the event loop alive. ```python async def receiver(): while True: data = await q.get() print("Received:", data) asyncio.ensure_future(sender()) asyncio.ensure_future(receiver()) asyncio.get_event_loop().run_forever() ``` -------------------------------- ### View Webcam Feed with RTCBot CVCamera and CVDisplay Source: https://github.com/dkumor/rtcbot/blob/master/examples/basics/README.md Demonstrates how to capture and display a live video feed from a webcam using RTCBot's CVCamera and CVDisplay. It subscribes to camera frames and displays them in a window. Dependencies include 'asyncio', 'rtcbot', and 'opencv-python'. Note: CVDisplay may not work on Mac. ```python import asyncio from rtcbot import CVCamera, CVDisplay camera = CVCamera() display = CVDisplay() @camera.subscribe def onFrame(frame): print("got video frame") display.put_nowait(frame) try: asyncio.get_event_loop().run_forever() finally: camera.close() display.close() ``` -------------------------------- ### WebSocket Endpoint for Robot Connection (Python) Source: https://github.com/dkumor/rtcbot/blob/master/examples/mobile/README.md This Python code defines a websocket endpoint for the server that handles incoming robot connections. It waits for a robot to connect, exchanges data for WebRTC setup, and maintains the connection until it's closed. ```python @routes.get("/ws") async def websocket(request): global ws ws = Websocket(request) print("Robot Connected") await ws # Wait until the websocket closes print("Robot disconnected") return ws.ws ``` -------------------------------- ### Python: Establish WebRTC Connection Offer Source: https://github.com/dkumor/rtcbot/blob/master/examples/webrtc/README.md This Python code snippet sets up a POST route to handle incoming connection offers from clients. It receives client-side offer data, processes it to generate a local description, and returns the necessary information for the client to complete the connection. ```python import web import rtcbot conn = rtcbot.RTCConnection() @web.routes.post("/connect") async def connect(request): clientOffer = await request.json() serverResponse = await conn.getLocalDescription(clientOffer) return web.json_response(serverResponse) ``` -------------------------------- ### CVCamera: Capture and Display Webcam Feed with OpenCV Source: https://github.com/dkumor/rtcbot/blob/master/docs/camera.rst This snippet demonstrates how to use the CVCamera class to capture video frames from a connected webcam using OpenCV. The captured frames are then displayed in real-time. It requires OpenCV to be installed. The output is BGR numpy arrays. ```python import asyncio from rtcbot import CVCamera, CVDisplay camera = CVCamera() display = CVDisplay() display.putSubscription(camera) try: asyncio.get_event_loop().run_forever() finally: camera.close() display.close() ``` -------------------------------- ### POST /connect Source: https://github.com/dkumor/rtcbot/blob/master/examples/webrtc/README.md Establishes a WebRTC connection by accepting a client's offer and returning a server response. ```APIDOC ## POST /connect ### Description This endpoint accepts an offer from the client to establish a WebRTC connection. It processes the client's offer and returns a server response to complete the connection setup. ### Method POST ### Endpoint /connect ### Parameters #### Request Body - **clientOffer** (object) - Required - The offer details from the client to establish the connection. ### Request Example ```json { "sdp": "v=0...", "type": "offer" } ``` ### Response #### Success Response (200) - **serverResponse** (object) - The server's response containing details to complete the WebRTC connection. #### Response Example ```json { "sdp": "v=0...", "type": "answer" } ``` ``` -------------------------------- ### Python: Stream Video and Audio using RTCBot Source: https://github.com/dkumor/rtcbot/blob/master/examples/streaming/README.md This Python code extends the video streaming example to include audio. It initializes both CVCamera and Microphone, and then puts subscriptions for both video and audio streams into the RTCConnection. Remember to close both the camera and microphone resources upon application shutdown. ```python from rtcbot import RTCConnection, getRTCBotJS, CVCamera, Microphone camera = CVCamera() mic = Microphone() conn = RTCConnection() conn.video.putSubscription(camera) conn.audio.putSubscription(mic) # ... rest of the server setup ... async def cleanup(app=None): await conn.close() camera.close() mic.close() # Close the microphone as well ``` -------------------------------- ### Python Backend for RTCBot Connection Source: https://github.com/dkumor/rtcbot/blob/master/examples/webrtc/README.md Sets up a web server endpoint to handle incoming WebRTC connection offers from clients. It receives the client's offer, generates a local description, and returns it. This is a crucial part of the WebRTC handshake. ```python from aiohttp import web routes = web.RouteTableDef() @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): return web.Response( content_type="text/html", text=f""" RTCBot: Data Channel

Click the Button

Open the browser's developer tools to see console messages (CTRL+SHIFT+C)

""") async def cleanup(app=None): await conn.close() app = web.Application() app.add_routes(routes) app.on_shutdown.append(cleanup) web.run_app(app) ``` -------------------------------- ### C Struct Messaging Setup for Arduino Source: https://github.com/dkumor/rtcbot/blob/master/docs/arduino.rst Defines a packed C struct named `controlMessage` on the Arduino side. This struct is intended to receive data sent from Python using `SerialConnection` with a specified `writeFormat`. The `__attribute__((packed))` directive ensures memory alignment compatibility. ```c++ #include typedef __attribute__ ((packed)) struct { int16_t value1; uint8_t value2; } controlMessage; ``` -------------------------------- ### Stream Video via WebRTC with rtcbot Source: https://context7.com/dkumor/rtcbot/llms.txt This snippet demonstrates how to stream video from a camera to a browser using WebRTC with the rtcbot library. It shows basic camera subscription and an alternative using picamera2 for newer Raspberry Pi OS versions. It also includes an example of saving frames to disk. ```python conn = RTCConnection() conn.video.putSubscription(camera) # Alternative: Use picamera2 (newer Raspberry Pi OS) from rtcbot import PiCamera2 camera2 = PiCamera2( width=1920, height=1080, hflip=True, vflip=False ) # Save frames to disk async def save_frames(): import cv2 subscription = camera.subscribe() for i in range(10): frame = await subscription.get() cv2.imwrite(f"frame_{i:03d}.jpg", frame) print(f"Saved frame {i}") camera.close() asyncio.run(save_frames()) ``` -------------------------------- ### Audio Input/Output with rtcbot Source: https://context7.com/dkumor/rtcbot/llms.txt This section covers audio input and output using the rtcbot library with the SoundCard library. It demonstrates how to echo microphone input to a speaker, stream audio over WebRTC, play remote audio, and analyze audio data in real-time. ```python import asyncio from rtcbot import Microphone, Speaker, RTCConnection # Echo microphone to speaker mic = Microphone(samplerate=48000, blocksize=1024) speaker = Speaker(samplerate=48000, blocksize=1024) speaker.putSubscription(mic) # Stream audio over WebRTC conn = RTCConnection() conn.audio.putSubscription(mic) # Receive and play remote audio remote_speaker = Speaker() remote_speaker.putSubscription(conn.audio.subscribe()) # Process audio data async def analyze_audio(): import numpy as np audio_sub = mic.subscribe() while True: audio_data = await audio_sub.get() # audio_data shape: (samples, channels) rms = np.sqrt(np.mean(audio_data**2)) db = 20 * np.log10(rms + 1e-10) print(f"Audio level: {db:.2f} dB, Shape: {audio_data.shape}") # Cleanup try: asyncio.get_event_loop().run_forever() finally: mic.close() speaker.close() ``` -------------------------------- ### Basic Serial Communication with Raspberry Pi using C++ (Arduino) Source: https://github.com/dkumor/rtcbot/blob/master/docs/arduino.rst Corresponds to the Python example, this Arduino code initializes serial communication and reads incoming bytes. It then prints a confirmation message along with the received character. It's designed to work with the basic string communication from the Python script. ```c++ void setup() { Serial.begin(115200); } void loop() { if (Serial.available() > 0) { Serial.print("I received: "); Serial.println(Serial.read()); } } ```