def set_up_components(): global cam, conn cam = cameras.MicroCamera( type="remote_usb") # !!! CHANGE FOR CUSTOM CAMERA OBJ conn = RTCConnection() conn.video.putSubscription(cam) conn.subscribe(onMessage)
def __init__(self, cam, stop_event): self.thread = None self.conn = RTCConnection() self.cam = cam self.stop_event = stop_event self.conn.video.putSubscription(cam) asyncio.ensure_future(self.connect()) self.loop = asyncio.get_event_loop()
class Socketrunthread(): def __init__(self, cam, stop_event): self.thread = None self.conn = RTCConnection() self.cam = cam self.stop_event = stop_event self.conn.video.putSubscription(cam) asyncio.ensure_future(self.connect()) self.loop = asyncio.get_event_loop() def start(self): if not self.stop_event: self.thread = Thread(target=self.start_loop, args=()) self.thread.start() print("Ok") @self.conn.subscribe def OnMessage(m): print(m) if m == "open": ard_snd("*") def start_loop(self): try: asyncio.set_event_loop(self.loop) self.loop.run_forever() except KeyboardInterrupt: pass def finish(self): try: self.conn.close() self.loop.stop() self.stop_event = False self.thread.join() print("Thread stopped") except RuntimeError: print("Exception Handled successfully") async def connect(self): global cams ws = Websocket("http://localhost:8080/xyz") remoteDescription = await ws.get() print("The remote description is of type: " + str(type(remoteDescription)) + "\n") robotDescription = await self.conn.getLocalDescription( remoteDescription) print("The robot description is of type: " + str(type(robotDescription)) + "\n") ws.put_nowait(robotDescription) print("Started WebRTC") cams = True print("Cams set to :" + str(cams)) await ws.close()
class Socketrunthread(): def __init__(self, cam, stop_event): self.thread = None self.conn = RTCConnection() self.cam = cam self.stop_event = stop_event self.conn.video.putSubscription(cam) asyncio.ensure_future(self.connect()) self.loop = asyncio.get_event_loop() def start(self): if not self.stop_event: self.thread = Thread(target=self.start_loop, args=()) self.thread.start() def start_loop(self): try: asyncio.set_event_loop(self.loop) # print("Next stmt loop forever") self.loop.run_forever() # print("After run forever") except KeyboardInterrupt: pass def finish(self): # self.cam.close() self.conn.close() self.loop.stop() self.stop_event = False self.thread.join() print("Thread stopped") async def connect(self): ws = Websocket("http://localhost:8080/xyz") remoteDescription = await ws.get() print("The remote description is of type: " + str(type(remoteDescription)) + "\n") robotDescription = await self.conn.getLocalDescription( remoteDescription) print("The robot description is of type: " + str(type(robotDescription)) + "\n") ws.put_nowait(robotDescription) print("Started WebRTC") await ws.close()
async def test_multipleDataChannels(self): c1 = RTCConnection() c2 = RTCConnection() dcs1 = c1.onDataChannel() dcs2 = c2.onDataChannel() # CONTINUE... await c1.close() await c2.close()
async def registerOnServerAndAwaitRtcConnections(): print("sending /registerRobot") ws = Websocket(REMOTE_WEB_SERVER + '/registerRobot') while True: remoteDescription = await ws.get() print("{:%Y-%m-%d %H:%M:%S}: new web user requested connect".format(datetime.datetime.now())) connection = RTCConnection() connection.video.putSubscription(videoFrameSubscription) connection.subscribe(onMessage) connection.putSubscription(cv2ColorSubscription) connection.put_nowait({ "action": "hsv", "hsv": hsv }) @connection.onClose def close(): print("{:%Y-%m-%d %H:%M:%S}: Connection Closed".format(datetime.datetime.now())) connections.remove(connection) connections.append(connection) robotDescription = await connection.getLocalDescription(remoteDescription) ws.put_nowait(robotDescription)
# desktop.py 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://192.168.0.3:8080/connect", data=json.dumps(localDescription)) as resp: response = await resp.json() await conn.setRemoteDescription(response) # Start sending gamepad controls #g.subscribe(conn)
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) import time import random import asyncio def get_sensor_data(): time.sleep(0.5) # Represents an operation that takes half a second to complete return random.random() async def send_sensor_data(): while True: await asyncio.sleep(1) data = get_sensor_data() conn.put_nowait(data) # Send data to browser asyncio.ensure_future(send_sensor_data()) # Serve the RTCBot javascript library at /rtcbot.js
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(m): print("key press", m) # 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("/")
# %% imports from MQTTDevice import MQTTDevice import paho.mqtt.client as mqtt from random import randint from time import time, sleep import sys import asyncio from rtcbot import Websocket, RTCConnection, PiCamera from aiortc import RTCConfiguration, RTCIceServer cam = PiCamera() conn = RTCConnection(rtcConfiguration=RTCConfiguration([ RTCIceServer(urls="stun:stun.l.google.com:19302"), RTCIceServer(urls="turn:io.scilifelab.se:5349", username="******", credential="3a53d013d9996594d591") ])) # set parameters setup_name = "S015" device_ID = "RAS01" device_MQTT_name = "RASPI_" + str(randint(0, 100000)) mqtt_broker_ip = "localhost" mqtt_client_name = "raspi1" # not necessary mqtt_client_pass = "******" # not necessary mqtt_port = 1883 mqtt_keepalive = 60 mqtt_uselogin = False # conn = RTCConnection()
from collections import OrderedDict codec_parameters = OrderedDict([ ("packetization-mode", "1"), ("level-asymmetry-allowed", "1"), ("profile-level-id", "42001f"), ]) pi_capability = RTCRtpCodecCapability(mimeType="video/H264", clockRate=90000, channels=None, parameters=codec_parameters) preferences = [pi_capability] camera = PiCamera(fps=15, width=640, height=480) # For this example, we use just one global connection conn = RTCConnection(video_codec_preference=preferences) 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)
# desktop.py 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)
async def test_basic(self): """ Creates a connection, and ensures that multiple messages go through in both directions. """ testmsg1 = "Testy mc test-test" testmsg2 = "Hai wrld" c1 = RTCConnection() c2 = RTCConnection() q1 = c1.subscribe() q2 = c2.subscribe() offer = await c1.getLocalDescription() response = await c2.getLocalDescription(offer) await c1.setRemoteDescription(response) c1.put_nowait(testmsg1) c2.put_nowait(testmsg2) c1.send("OMG") # Just to check c2.put_nowait("OMG2") msg1 = await asyncio.wait_for(q1.get(), 5) msg2 = await asyncio.wait_for(q2.get(), 5) self.assertEqual(msg1, testmsg2) self.assertEqual(msg2, testmsg1) msg1 = await asyncio.wait_for(q1.get(), 5) msg2 = await asyncio.wait_for(q2.get(), 5) self.assertEqual(msg1, "OMG2") self.assertEqual(msg2, "OMG") await c1.close() await c2.close()
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("/")
SubscriptionClosed, ) import logging from contextlib import suppress import signal logging.basicConfig(level=logging.DEBUG) m = Microphone() s = Speaker() d = CVDisplay() cam = CVCamera() # cam.subscribe(d) c1 = RTCConnection() c2 = RTCConnection() c1.video.putSubscription(cam) c1.audio.putSubscription(m) c2.video.subscribe(d) c2.audio.subscribe(s) async def testMe(): offer = await c1.getLocalDescription() response = await c2.getLocalDescription(offer) await c1.setRemoteDescription(response)