This commit is contained in:
yumoqing 2024-08-27 18:39:25 +08:00
parent 738c4f24bc
commit 84f61c8b71

View File

@ -1,2 +1,108 @@
from aiortc import MediaStreamTrack, RTCPeerConnection, RTCSessionDescription
import asyncio
from appPublic.dictObject import DictObject
from aiortc import MediaStreamTrack, RTCPeerConnection, RTCSessionDescription, RTCIceCandidate
from aiortc.contrib.media import MediaBlackhole, MediaPlayer, MediaRecorder, MediaRelay
# from websockets.asyncio.client import connect
from websockets.asyncio.client import connect
import websockets
class RTCLLM:
def __init__(self, ws_url, iceServers):
self.ws_url = ws_url
self.iceServers = iceServers
self.peers = DictObject()
def get_pc(self, data):
return self.peers[data['from'].id].pc
async def run(self):
async with connect(self.ws_url) as self.ws:
self.login()
while True:
msg = await self.ws.recv()
data = DictObject(**json.loads(msg))
f = self.handler.get(data.type)
await f(data)
self.ws.close()
async def login(self):
await self.ws.send(json.dumps({
'type':'login',
'info':{
'id':'rtcllm_agent',
'name':'rtcllm_agent'
}
}))
async def save_onlineList(self, data):
self.onlineList = data.onlineList
async def auto_accept_call(self, data):
pc = await self.createPeerConnection(data['from'])
self.peers[data['from'].id] = DictObject(**{
'info':data['from'],
'pc':pc
})
await self.ws.send(json.dumps({'type':'callAccepted', 'to':data['from']}))
async def response_offer(self, data):
pc = self.get_pc(data)
offer = RTCSessionDescription(data.offer)
await pc.setRemoteDescription(offer)
answer = await pc.createAnswer()
await pc.setLocalDescription(answer)
self.ws.send(json.dumps({
'type':'answer',
'answer':pc.localDescription,
'to':data['from']
}))
@pc.on("datachannel")
def datachannel_handle(channel):
@channel.on("message")
def recvdata(channel):
pass
"""
@pc.on("connectionstatechange")
@pc.on("track")
"""
async def accept_answer(self, data):
pc = self.get_pc(data)
answer = RTCSessionDescription(data.answer);
pc.setRemoteDescription(answer)
async def accept_iceCandidate(self, data):
pc = self.get_pc(data)
candidate = RTCIceCandidate(data.candidate)
# pc.addIceCandidate(RTCIceCandidate.from_string(candidate))
await pc.addIceCandidate(candidate)
async def createPeerConnection(self, peerinfo):
opts = {
iceServers:self.iceServers
}
pc = RTCPeerConnection(opts)
handlers = {
'onlineList':save_onlineList,
'callRequest':auto_accept_call,
'offer':response_offer,
'answer':accept_answer,
'iceCandidate':accept_iceCandidate,
}
async def main():
agent = RTCLLM(ws_url='https://sage.opencomputing.cn/wss/ws/rtc_signaling.ws',
iceServers=[{
'urls':'stun:stun.opencomputing.cn'},{
'urls':'turn:stun.opencomputing.cn',
'username':'turn',
'credential':'server'
}])
await agent.run()
if __name__ == '__main__':
asyncio.run(main())