# Examples ## Prerequisites ```bash pip install livekit-api livekit ``` ## Scripts | Script | What it does | |--------|--------------| | `view_stream.py` | Print video frame stats | | `send_command.py` | Send a walk command | | `read_state.py` | Print robot state @ 50 Hz | | `record_session.py` | Save state to CSV | ## Running ```bash # Generate a token first ./scripts/generate_token.py \ --api-key YOUR_KEY \ --api-secret YOUR_SECRET \ --room robot-room \ --identity test-client \ --role operator # Run any script python3 view_stream.py --token "eyJ..." ``` ## Common Pattern All scripts follow the same structure: ```python import asyncio from livekit import rtc LIVEKIT_URL = "wss://your-server.com" TOKEN = "your-token" async def main(): room = rtc.Room() await room.connect(LIVEKIT_URL, TOKEN) print(f"Connected to {room.name}") # Your logic here await room.disconnect() asyncio.run(main()) ``` ## View Stream ```python #!/usr/bin/env python3 """Print video frame stats.""" import asyncio from livekit import rtc LIVEKIT_URL = "wss://your-server.com" TOKEN = "your-token" async def main(): room = rtc.Room() await room.connect(LIVEKIT_URL, TOKEN) print(f"Connected to room: {room.name}") @room.on("track_subscribed") def on_track(track, publication, participant): if isinstance(track, rtc.VideoTrack): print(f"Video track: {track.name}") @track.on("frame_received") def on_frame(frame): print(f"Frame: {frame.width}x{frame.height}") await asyncio.sleep(30) await room.disconnect() asyncio.run(main()) ``` ## Send Command ```python #!/usr/bin/env python3 """Send a walk forward command.""" import asyncio import struct from livekit import rtc LIVEKIT_URL = "wss://your-server.com" TOKEN = "your-token" async def main(): room = rtc.Room() await room.connect(LIVEKIT_URL, TOKEN) # Wait for DataChannel await asyncio.sleep(1) # TeleopCommand_S: mode, vx, vy, vyaw, gait_mode, enable, e_stop cmd = struct.pack('= 15: ts, seq, mode, motors, estop = struct.unpack('