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()
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) asyncio.ensure_future(connect()) try: asyncio.get_event_loop().run_forever() finally: conn.close() disp.close() #g.close()
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()