This commit is contained in:
yumoqing 2024-09-11 13:40:45 +08:00
parent 12577b2267
commit 3b044efd7e
3 changed files with 23 additions and 19 deletions

View File

@ -5,12 +5,11 @@ from aiortc import MediaStreamTrack, VideoStreamTrack, AudioStreamTrack
class MyMediaPlayer(MediaPlayer): class MyMediaPlayer(MediaPlayer):
pass pass
class MyTrackBase(MediaStreamTrack): class MyTrackBase:
def __init__(self, source=None): def __init__(self, source=None):
super().__init__()
self.source = source self.source = source
self.set_source_track() self.set_source_track()
print(f'{self.kind=}, {self.__class__.__name__}') print(f'{self.kind=}, {self.__class__.__name__}, {dir(self)}')
def set_source_track(self): def set_source_track(self):
if self.kind == 'audio': if self.kind == 'audio':
@ -27,7 +26,7 @@ class MyTrackBase(MediaStreamTrack):
self.set_source_track() self.set_source_track()
async def recv(self): async def recv(self):
print(f'{self.__class__.__name__}, {self.source.duration=}, {self.source.time=}') print(f'============{self.__class__.__name__}, {self.source.duration=}, {self.source.time=}')
if self.source is None: if self.source is None:
return None return None
if self.track.readyState != 'live': if self.track.readyState != 'live':
@ -36,8 +35,14 @@ class MyTrackBase(MediaStreamTrack):
return f return f
class MyAudioStreamTrack(MyTrackBase): class MyAudioStreamTrack(MyTrackBase, AudioStreamTrack):
kind = 'audio' def __init__(self, source=None):
AudioStreamTrack.__init__(self)
MyTrackBase.__init__(self, source)
print(dir(self), self.__class__.__name__)
class MyVideoStreamTrack(MyTrackBase): class MyVideoStreamTrack(MyTrackBase, VideoStreamTrack):
kind = 'video' def __init__(self, source=None):
VideoStreamTrack.__init__(self)
MyTrackBase.__init__(self, source)
print(dir(self), self.__class__.__name__)

View File

@ -41,7 +41,7 @@ async def pc_get_local_candidates(pc, peer):
for t in its: for t in its:
for c in t._connection.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)}') # print(f'{c=}, {dir(c)}')
c.sdpMid = str(peer.sdp_id) c.sdpMid = str(peer.sdp_id)
peer.sdp_id += 1 peer.sdp_id += 1
peer.l_candidates.append(c) peer.l_candidates.append(c)
@ -97,7 +97,7 @@ class RTCLLM:
'sdpMid':candidate.sdpMid, 'sdpMid':candidate.sdpMid,
'type': candidate.type 'type': candidate.type
} }
print('***********on_icecandidate()', candi) # print('***********on_icecandidate()', candi)
await self.ws_send(json.dumps({ await self.ws_send(json.dumps({
"type":"iceCandidate", "type":"iceCandidate",
"to":to, "to":to,
@ -105,7 +105,7 @@ class RTCLLM:
})) }))
async def save_onlineList(self, data): async def save_onlineList(self, data):
print(f'{self}, {type(self)}') # print(f'{self}, {type(self)}')
self.onlineList = data.onlineList self.onlineList = data.onlineList
async def vad_voiceend(self, peer, audio): async def vad_voiceend(self, peer, audio):
@ -199,7 +199,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
print('accepted candidate=', candidate) # print('accepted candidate=', candidate)
""" """
rtc_candidate = RTCIceCandidate( rtc_candidate = RTCIceCandidate(
ip=ip, ip=ip,
@ -217,7 +217,7 @@ class RTCLLM:
rtc_candidate.sdpMid = candidate['sdpMid'] rtc_candidate.sdpMid = candidate['sdpMid']
rtc_candidate.sdpMLineIndex = candidate['sdpMLineIndex'] rtc_candidate.sdpMLineIndex = candidate['sdpMLineIndex']
await pc.addIceCandidate(rtc_candidate) await pc.addIceCandidate(rtc_candidate)
print('addIceCandidate ok') # print('addIceCandidate ok')
handlers = { handlers = {
'onlineList':save_onlineList, 'onlineList':save_onlineList,

View File

@ -15,7 +15,6 @@ class AudioTrackVad(MediaStreamTrack):
def __init__(self, track, stage=3, onvoiceend=None): def __init__(self, track, stage=3, onvoiceend=None):
super().__init__() super().__init__()
self.track = track self.track = track
print(dir(track), 'AudioTrackVad.__init__()')
self.onvoiceend = onvoiceend self.onvoiceend = onvoiceend
self.vad = webrtcvad.Vad(stage) self.vad = webrtcvad.Vad(stage)
# self.sample_rate = self.track.getSettings().sampleRate # self.sample_rate = self.track.getSettings().sampleRate
@ -56,7 +55,7 @@ class AudioTrackVad(MediaStreamTrack):
for f in frames: for f in frames:
if self.debug: if self.debug:
self.debug = False self.debug = False
print(f'{type(f)}, {f.samples=}, {f.format.bytes=}, {f.sample_rate=}, {f.format=}, {f.is_corrupt=}, {f.layout=}, {f.planes=}, {f.side_data=}') # print(f'{type(f)}, {f.samples=}, {f.format.bytes=}, {f.sample_rate=}, {f.format=}, {f.is_corrupt=}, {f.layout=}, {f.planes=}, {f.side_data=}')
try: try:
await self.vad_check(f) await self.vad_check(f)
except Exception as e: except Exception as e:
@ -92,7 +91,7 @@ class AudioTrackVad(MediaStreamTrack):
for f, s in self.ring_buffer: for f, s in self.ring_buffer:
self.voiced_frames.append(f) self.voiced_frames.append(f)
self.ring_buffer.clear() self.ring_buffer.clear()
print('start voice .....', len(self.voiced_frames)) # print('start voice .....', len(self.voiced_frames))
else: else:
# We're in the TRIGGERED state, so collect the audio data # We're in the TRIGGERED state, so collect the audio data
# and add it to the ring buffer. # and add it to the ring buffer.
@ -131,7 +130,7 @@ class AudioTrackVad(MediaStreamTrack):
""" """
audio_data = self.to_mono16000_data() audio_data = self.to_mono16000_data()
path = temp_file(suffix='.wav') path = temp_file(suffix='.wav')
print(f'temp_file={path}') # print(f'temp_file={path}')
with contextlib.closing(wave.open(path, 'wb')) as wf: with contextlib.closing(wave.open(path, 'wb')) as wf:
wf.setnchannels(1) wf.setnchannels(1)
@ -139,8 +138,8 @@ class AudioTrackVad(MediaStreamTrack):
wf.setframerate(16000) wf.setframerate(16000)
wf.writeframes(audio_data) wf.writeframes(audio_data)
print('************wrote*******') # print('************wrote*******')
if self.onvoiceend: if self.onvoiceend:
await self.onvoiceend(path) await self.onvoiceend(path)
print('************over*******') # print('************over*******')