From 027e15201a9171d346c7c9079b4ff0d3668ca179 Mon Sep 17 00:00:00 2001 From: lipku Date: Sat, 27 Apr 2024 00:07:37 +0800 Subject: [PATCH] fix webrtc audio problem --- nerfreal.py | 23 +++++++++++++++-------- webrtc.py | 8 ++++---- 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/nerfreal.py b/nerfreal.py index 535e2a9..c4263a1 100644 --- a/nerfreal.py +++ b/nerfreal.py @@ -167,14 +167,21 @@ class NeRFReal: #print(f'[INFO] get_audio_out shape ',frame.shape) self.streamer.stream_frame_audio(frame) else: - frame1 = self.asr.get_audio_out() - frame2 = self.asr.get_audio_out() - frame = np.concatenate((frame1,frame2)) - frame = (frame * 32767).astype(np.int16) - new_frame = AudioFrame(format='s16', layout='mono', samples=frame.shape[0]) - new_frame.planes[0].update(frame.tobytes()) - new_frame.sample_rate=16000 - asyncio.run_coroutine_threadsafe(audio_track._queue.put(new_frame), loop) + for _ in range(2): + frame = self.asr.get_audio_out() + frame = (frame * 32767).astype(np.int16) + new_frame = AudioFrame(format='s16', layout='mono', samples=frame.shape[0]) + new_frame.planes[0].update(frame.tobytes()) + new_frame.sample_rate=16000 + asyncio.run_coroutine_threadsafe(audio_track._queue.put(new_frame), loop) + # frame1 = self.asr.get_audio_out() + # frame2 = self.asr.get_audio_out() + # frame = np.concatenate((frame1,frame2)) + # frame = (frame * 32767).astype(np.int16) + # new_frame = AudioFrame(format='s16', layout='mono', samples=frame.shape[0]) + # new_frame.planes[0].update(frame.tobytes()) + # new_frame.sample_rate=16000 + # asyncio.run_coroutine_threadsafe(audio_track._queue.put(new_frame), loop) # frame = (frame * 32767).astype(np.int16).tobytes() # self.fifo_audio.write(frame) else: diff --git a/webrtc.py b/webrtc.py index b6e216f..6a2f371 100644 --- a/webrtc.py +++ b/webrtc.py @@ -46,8 +46,8 @@ class PlayerStreamTrack(MediaStreamTrack): if self.kind == 'video': if hasattr(self, "_timestamp"): - self._timestamp = (time.time()-self._start) * VIDEO_CLOCK_RATE - # self._timestamp += int(VIDEO_PTIME * VIDEO_CLOCK_RATE) + # self._timestamp = (time.time()-self._start) * VIDEO_CLOCK_RATE + self._timestamp += int(VIDEO_PTIME * VIDEO_CLOCK_RATE) # wait = self._start + (self._timestamp / VIDEO_CLOCK_RATE) - time.time() # if wait>0: # await asyncio.sleep(wait) @@ -57,8 +57,8 @@ class PlayerStreamTrack(MediaStreamTrack): return self._timestamp, VIDEO_TIME_BASE else: #audio if hasattr(self, "_timestamp"): - self._timestamp = (time.time()-self._start) * SAMPLE_RATE - # self._timestamp += int(AUDIO_PTIME * SAMPLE_RATE) + # self._timestamp = (time.time()-self._start) * SAMPLE_RATE + self._timestamp += int(AUDIO_PTIME * SAMPLE_RATE) # wait = self._start + (self._timestamp / SAMPLE_RATE) - time.time() # if wait>0: # await asyncio.sleep(wait)