async def object_from_string(message_str): if(isinstance(message_str,list)): return message_str message = None; if(isinstance(message_str,dict)): if "sdp" in message_str["message"]: message = message_str["message"]["sdp"] if "candidate" in message_str["message"]: message = message_str["message"] #["candidate"] if "stick" in message_str["message"]: message = message_str["message"] else: message = json.loads(message_str) if message is not None: if "type" in message and message["type"] in ["answer", "offer"]: return RTCSessionDescription(**message) elif ("type" in message and message["type"] == "candidate" and message["candidate"]) or message["candidate"]: candidate = None if(isinstance(message["candidate"], dict )): candidate = candidate_from_sdp(message["candidate"]["candidate"].split(":", 1)[1]) else: candidate = candidate_from_sdp(message["candidate"].split(":", 1)[1]) if(isinstance(message["candidate"], dict )): candidate.sdpMid = message["candidate"]["sdpMid"] candidate.sdpMLineIndex = message["candidate"]["sdpMLineIndex"] else: candidate.sdpMid = message["id"] candidate.sdpMLineIndex = message["label"] return candidate elif message["type"] == "bye": return BYE return message
async def handle_message(message): print('<', message) if message['type'] == 'bye': if player: player.stop() recorder.stop() return True if message['type'] == 'offer': await pc.setRemoteDescription(RTCSessionDescription(**message)) await pc.setLocalDescription(await pc.createAnswer()) await signaling.send_message(description_to_dict(pc.localDescription)) if player: player.start() recorder.start() elif message['type'] == 'answer': await pc.setRemoteDescription(RTCSessionDescription(**message)) if player: player.start() recorder.start() elif message['type'] == 'candidate': candidate = candidate_from_sdp(message['candidate'].split(':', 1)[1]) candidate.sdpMid = message['id'] candidate.sdpMLineIndex = message['label'] pc.addIceCandidate(candidate) return False
async def handle_add_candidate_for_webcam(self, msg, ws): pc = self.pcs[ws] msg = json.loads(msg) print("add_candidate: ", msg["candidate"]) candidate = candidate_from_sdp(msg["candidate"].split(":", 1)[1]) candidate.sdpMid = msg["sdpMid"] candidate.sdpMLineIndex = msg["sdpMLineIndex"] pc.addIceCandidate(candidate)
def object_from_string(message_str): message = json.loads(message_str) if message['type'] in ['answer', 'offer']: return RTCSessionDescription(**message) elif message['type'] == 'candidate': candidate = candidate_from_sdp(message['candidate'].split(':', 1)[1]) candidate.sdpMid = message['id'] candidate.sdpMLineIndex = message['label'] return candidate
def object_from_string(message_str): message = json.loads(message_str) if message["type"] in ["answer", "offer"]: return RTCSessionDescription(**message) elif message["type"] == "candidate": candidate = candidate_from_sdp(message["candidate"].split(":", 1)[1]) candidate.sdpMid = message["id"] candidate.sdpMLineIndex = message["label"] return candidate
def object_from_string(message_str): obj = json.loads(message_str) data = obj['data'] source = obj['source'] if data['type'] in ['answer', 'offer']: return RTCSessionDescription(**data), source elif data['type'] == 'candidate': candidate = candidate_from_sdp(data['candidate'].split(':', 1)[1]) candidate.sdpMid = data['id'] candidate.sdpMLineIndex = data['label'] return candidate, source
async def handle_message(message): print('<', message) if message['type'] == 'offer': await pc.setRemoteDescription(RTCSessionDescription(**message)) await pc.setLocalDescription(await pc.createAnswer()) await signaling.send_description(pc.localDescription) elif message['type'] == 'answer': await pc.setRemoteDescription(RTCSessionDescription(**message)) elif message['type'] == 'candidate': candidate = candidate_from_sdp(message['candidate'].split(':', 1)[1]) candidate.sdpMid = message['id'] candidate.sdpMLineIndex = message['label'] pc.addIceCandidate(candidate)
async def receive(self): message = await self.__message_queue.get() # message = json.loads(message)['msg'] print('<', message) # msg = json.dumps(message) if message['type'] in ['answer', 'offer']: sdp = message['sdp'] #return RTCSessionDescription(**msg) return RTCSessionDescription(sdp=sdp, type=message['type']) elif message['type'] == 'candidate': candidate = candidate_from_sdp(message['candidate'].split(':', 1)[1]) candidate.sdpMid = message['sdpMid'] candidate.sdpMLineIndex = message['sdpMLineIndex'] return candidate else: return None
async def handleCandidate(self, ice=None): """Handle new peer candidate.""" log.debug(f'handleCandidate:', ice) candidate = ice['candidate'] sdpMLineIndex = ice['sdpMLineIndex'] sdpMid = ice['sdpMid'] peerConnection = self.connection.peerConnection provider = self.connection.provider try: log.debug('Adding ICE candidate for peer id %s', self.connection.peer) rtc_ice_candidate = candidate_from_sdp(candidate) rtc_ice_candidate.sdpMid = sdpMid rtc_ice_candidate.sdpMLineIndex = sdpMLineIndex log.debug('RTCIceCandidate: %r', rtc_ice_candidate) log.debug('peerConnection: %r', peerConnection) peerConnection.addIceCandidate(rtc_ice_candidate) log.debug('Added ICE candidate for peer %s', self.connection.peer) except Exception as err: provider.emitError(PeerErrorType.WebRTC, err) log.exception("Failed to handleCandidate, ", err)
async def websocket_session(session): logger.info("ws conection start") partner = None global PC, CONFIG try: async with session.ws_connect('ws://127.0.0.1:5000/ss') as ws: print("ws connected, sending join...") await ws.send_str(json.dumps({'type': 'login', 'name': USER_NAME})) print("join sent listening to responses") while True: print('alive') async for msg in ws: #print(msg) if msg.type == WSMsgType.TEXT: msg_data = json.loads(msg.data) print("parsing messages") if 'type' in msg_data: try: typ = msg_data['type'] if typ == 'login': if msg_data['success']: print( 'login success, creating peer connection' ) await setup_peer() print("now what...") elif typ == 'offer': print("got an offer from: {0}".format( msg_data['name'])) offer = msg_data['offer'] partner = msg_data['name'] answer = await accept_offer( offer['sdp'], offer['type']) await ws.send_str( json.dumps({ 'type': 'answer', 'name': msg_data['name'], 'answer': { "sdp": answer.sdp, "type": answer.type } })) print("answer sent to offer") elif typ == 'answer': answer = await handle_answer(msg_data) # await ws.send_str(json.dumps({'type': 'answer', 'answer': answer})) elif typ == 'candidate': print("got candidate: ", msg_data['candidate']) m_can = msg_data["candidate"] if len(m_can['candidate']) > 1: candidate = candidate_from_sdp( m_can['candidate'].split(":", 1)[1]) candidate.sdpMid = m_can["sdpMid"] candidate.sdpMLineIndex = m_can[ "sdpMLineIndex"] print(candidate) PC.addIceCandidate(candidate) elif typ == 'info': print("got info: {0}".format(msg_data)) await asyncio.sleep(0.1) elif typ == 'leave': if msg_data['name'] == partner: print("my partner {0} left".format( partner)) partner = None await PC.close() # reset for next time await setup_peer() else: print('not sure what to do...') print(msg_data) await asyncio.sleep(0.1) except Exception as exc: print(exc) traceback.print_exc(file=sys.stdout) await asyncio.sleep(0.5) if msg.type in (WSMsgType.CLOSED, WSMsgType.ERROR): print("closed or errro") break print("no message") await asyncio.sleep(0.1) except Exception as exc: raise exc