rtcllm/rtcllm/rtc.py

170 lines
4.6 KiB
Python
Raw Normal View History

2024-08-27 18:39:25 +08:00
import asyncio
2024-08-28 18:26:47 +08:00
import json
from functools import partial
2024-08-27 18:39:25 +08:00
from appPublic.dictObject import DictObject
from aiortc import MediaStreamTrack, RTCPeerConnection, RTCSessionDescription, RTCIceCandidate
2024-08-26 18:11:08 +08:00
from aiortc.contrib.media import MediaBlackhole, MediaPlayer, MediaRecorder, MediaRelay
2024-08-27 18:39:25 +08:00
# from websockets.asyncio.client import connect
from websockets.asyncio.client import connect
import websockets
2024-08-28 18:26:47 +08:00
from vad import AudioTrackVad
2024-08-27 18:39:25 +08:00
class RTCLLM:
def __init__(self, ws_url, iceServers):
self.ws_url = ws_url
self.iceServers = iceServers
self.peers = DictObject()
2024-08-28 18:26:47 +08:00
self.info = {
'id':'rtcllm_agent',
'name':'rtcllm_agent'
}
self.dc = None
2024-08-27 18:39:25 +08:00
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:
2024-08-28 18:26:47 +08:00
await self.login()
2024-08-27 18:39:25 +08:00
while True:
msg = await self.ws.recv()
data = DictObject(**json.loads(msg))
2024-08-28 18:26:47 +08:00
d = data.data
print(f'ws recv(): {d.type=}')
func = self.handlers.get(d.type)
if func:
f = partial(func, self)
await f(d)
2024-08-27 18:39:25 +08:00
self.ws.close()
async def login(self):
await self.ws.send(json.dumps({
'type':'login',
2024-08-28 18:26:47 +08:00
'info':self.info
2024-08-27 18:39:25 +08:00
}))
2024-08-28 18:26:47 +08:00
async def on_icecandidate(self, pc, candidate):
print('on_icecandidate()', self, pc, candidate)
if candidate:
await self.ws.send(json.dumps({
"type":"candidate",
"candidate":candidate,
}))
2024-08-27 18:39:25 +08:00
async def save_onlineList(self, data):
2024-08-28 18:26:47 +08:00
print(f'{self}, {type(self)}')
2024-08-27 18:39:25 +08:00
self.onlineList = data.onlineList
2024-08-28 18:26:47 +08:00
async def vad_voiceend(self, fn):
print(f'vad_voiceend():{fn=}')
2024-08-27 18:39:25 +08:00
async def auto_accept_call(self, data):
2024-08-28 18:26:47 +08:00
opts = DictObject(iceServers=self.iceServers)
pc = RTCPeerConnection(opts)
2024-08-27 18:39:25 +08:00
self.peers[data['from'].id] = DictObject(**{
'info':data['from'],
'pc':pc
})
2024-08-28 18:26:47 +08:00
print(f'{opts=}, {pc=}, {pc.myconfiguration=}')
2024-08-27 18:39:25 +08:00
await self.ws.send(json.dumps({'type':'callAccepted', 'to':data['from']}))
2024-08-28 18:26:47 +08:00
async def pc_track(self, peerid, track):
peer = self.peers[peerid]
pc = peer.pc
if track.kind == 'audio':
2024-08-29 18:45:36 +08:00
vadtrack = AudioTrackVad(track, stage=3, onvoiceend=self.vad_voiceend)
2024-08-28 18:26:47 +08:00
peer.vadtrack = vadtrack
vadtrack.start_vad()
async def pc_connectionState_changed(self, peerid):
peer = self.peers[peerid]
pc = peer.pc
print(f'************************************{pc.connectionState=}')
if pc.connectionState == 'connected':
peer.dc = await pc.createDataChannel(peer.info.name)
return
if pc.connectionState == 'closed':
await pc.close()
if peer.dc:
await peer.dc.close()
peers = {
k:v for k,v in self.peers.items() if k != peerid
}
self.peers = peers
if len([k for k in self.peers.keys()]) == 0:
await self.ws.close()
2024-08-27 18:39:25 +08:00
async def response_offer(self, data):
pc = self.get_pc(data)
2024-08-28 18:26:47 +08:00
if pc is None:
print(f'{self.peers=}, {data=}')
return
2024-08-29 18:45:36 +08:00
pc.on("connectionstate", partial(self.pc_connectionState_changed, data['from'].id))
2024-08-28 18:26:47 +08:00
pc.on('track', partial(self.pc_track, data['from'].id))
pc.on('icecandidate', partial(self.on_icecandidate, pc))
offer = RTCSessionDescription(** data.offer)
2024-08-27 18:39:25 +08:00
await pc.setRemoteDescription(offer)
answer = await pc.createAnswer()
await pc.setLocalDescription(answer)
2024-08-28 18:26:47 +08:00
await self.ws.send(json.dumps({
2024-08-27 18:39:25 +08:00
'type':'answer',
2024-08-28 18:26:47 +08:00
'answer':{'type':pc.localDescription.type, 'sdp':pc.localDescription.sdp},
2024-08-27 18:39:25 +08:00
'to':data['from']
}))
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)
2024-08-28 18:26:47 +08:00
candidate = data.candidate
ip = candidate['candidate'].split(' ')[4]
port = candidate['candidate'].split(' ')[5]
protocol = candidate['candidate'].split(' ')[7]
priority = candidate['candidate'].split(' ')[3]
foundation = candidate['candidate'].split(' ')[0]
component = candidate['candidate'].split(' ')[1]
type = candidate['candidate'].split(' ')[7]
rtc_candidate = RTCIceCandidate(
ip=ip,
port=port,
protocol=protocol,
priority=priority,
foundation=foundation,
component=component,
type=type,
sdpMid=candidate['sdpMid'],
sdpMLineIndex=candidate['sdpMLineIndex']
)
await pc.addIceCandidate(rtc_candidate)
print('addIceCandidate ok')
2024-08-27 18:39:25 +08:00
handlers = {
'onlineList':save_onlineList,
'callRequest':auto_accept_call,
'offer':response_offer,
'answer':accept_answer,
'iceCandidate':accept_iceCandidate,
}
async def main():
2024-08-28 18:26:47 +08:00
agent = RTCLLM(ws_url='wss://sage.opencomputing.cn/wss/ws/rtc_signaling.ws',
2024-08-27 18:39:25 +08:00
iceServers=[{
'urls':'stun:stun.opencomputing.cn'},{
'urls':'turn:stun.opencomputing.cn',
'username':'turn',
'credential':'server'
}])
2024-08-28 18:26:47 +08:00
print('running ...')
2024-08-27 18:39:25 +08:00
await agent.run()
if __name__ == '__main__':
asyncio.run(main())