This commit is contained in:
yumoqing 2024-09-04 14:37:10 +08:00
parent 8edfd26796
commit 6694af192e
2 changed files with 16 additions and 28 deletions

View File

@ -22,15 +22,17 @@ from aav import MyAudioTrack, MyVideoTrack
videos = ['./1.mp4', './2.mp4'] videos = ['./1.mp4', './2.mp4']
async def pc_get_local_candidates(pc, peer): async def pc_get_local_candidates(pc, peer):
ts = pc.getTransceivers() its = pc.__dict__.get('_RTCPeerConnection__iceTransports')
coros = map(lambda t: t.iceGatherer.gather(), ts) coros = map(lambda t: t.iceGatherer.gather(), its )
await asyncio.gather(*coros) await asyncio.gather(*coros)
ss = pc.__dict__.get('_RTCPeerConnection__iceTransport')
if not peer.l_candidates: if not peer.l_candidates:
peer.l_candidates = [] peer.l_candidates = []
for t in ts: for t in its:
for c in t.local_candidates(): for c in t._connection.local_candidates:
if c not in peer.l_candidates: if c not in peer.l_candidates:
print(f'{c=}, {dir(c)}')
peer.l_candidates.append(c) peer.l_candidates.append(c)
pc.emit('icecandidate', c) pc.emit('icecandidate', c)
@ -76,20 +78,14 @@ class RTCLLM:
'info':self.info 'info':self.info
})) }))
async def on_icecandidate(self, pc, candidate): async def on_icecandidate(self, pc, to, candidate):
print('******************** on_icecandidate()', self, pc, candidate) print('******************** on_icecandidate()', self, pc, candidate, candidate.__class__.__name__)
if candidate: if candidate:
await self.ws_send(json.dumps({ await self.ws_send(json.dumps({
"type":"candidate", "type":"iceCandidate",
"candidate":candidate, "to":to,
"candidate":{'candidate':candidate.to_sdp(), 'type': candidate.type}
})) }))
async def send_candidate(self, to, candidate):
print(f'send_candidate():{candidate=}')
await self.ws_send(json.dumps({
'type':'candidate',
'to':to,
'candidate':candidate
}))
async def save_onlineList(self, data): async def save_onlineList(self, data):
print(f'{self}, {type(self)}') print(f'{self}, {type(self)}')
@ -173,7 +169,7 @@ class RTCLLM:
return return
pc.on("connectionstatechange", partial(self.pc_connectionState_changed, data['from'].id)) pc.on("connectionstatechange", partial(self.pc_connectionState_changed, data['from'].id))
pc.on('track', partial(self.pc_track, data['from'].id)) pc.on('track', partial(self.pc_track, data['from'].id))
pc.on('icecandidate', partial(self.on_icecandidate, pc)) pc.on('icecandidate', partial(self.on_icecandidate, pc, data['from']))
# self.play_video(data) # self.play_video(data)
offer = RTCSessionDescription(** data.offer) offer = RTCSessionDescription(** data.offer)
@ -186,8 +182,6 @@ class RTCLLM:
'to':data['from'] 'to':data['from']
})) }))
cands = await pc.get_local_candidates(peer) cands = await pc.get_local_candidates(peer)
for c in cands:
await self.send_candidate(data['from'], c)
""" """
offer = await pc.createOffer() offer = await pc.createOffer()
@ -207,13 +201,7 @@ class RTCLLM:
async def accept_iceCandidate(self, data): async def accept_iceCandidate(self, data):
pc = self.get_pc(data) pc = self.get_pc(data)
candidate = data.candidate candidate = data.candidate
ip = candidate['candidate'].split(' ')[4] print('accepted candidate=', candidate)
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( rtc_candidate = RTCIceCandidate(
ip=ip, ip=ip,
@ -245,8 +233,8 @@ async def main():
hf_socks5proxy() hf_socks5proxy()
agent = RTCLLM(ws_url='wss://sage.opencomputing.cn/wss/ws/rtc_signaling.ws', agent = RTCLLM(ws_url='wss://sage.opencomputing.cn/wss/ws/rtc_signaling.ws',
iceServers=[{ iceServers=[{
'urls':'stun:stun.opencomputing.cn'},{ 'urls':'stun:stun.opencomputing.cn:13478'},{
'urls':'turn:stun.opencomputing.cn', 'urls':'turn:stun.opencomputing.cn:13479',
'username':'turn', 'username':'turn',
'credential':'server' 'credential':'server'
}]) }])

View File

@ -117,7 +117,7 @@ class AudioTrackVad(MediaStreamTrack):
fs = self.resample(f, sample_rate=16000) fs = self.resample(f, sample_rate=16000)
lst += fs lst += fs
audio_data = b''.join([self.frame2bytes(f) for f in lst]) audio_data = b''.join([self.frame2bytes(f) for f in lst])
return to_mono16000_data return audio_data
async def gen_base64(self): async def gen_base64(self):
audio_data = self.to_mono16000_data() audio_data = self.to_mono16000_data()