Skip to content

Commit 964dcb2

Browse files
committed
update audio time calculation
1 parent 223be69 commit 964dcb2

File tree

2 files changed

+35
-31
lines changed

2 files changed

+35
-31
lines changed

examples/video-stream/video_play.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,15 @@ async def stream_audio(self) -> AsyncIterable[tuple[rtc.AudioFrame, float]]:
7979
# Convert audio frame to raw int16 samples
8080
frame = av_frame.to_ndarray().T # Transpose to (samples, channels)
8181
frame = (frame * 32768).astype(np.int16)
82+
duration = len(frame) / self.info.audio_sample_rate
8283
yield (
8384
rtc.AudioFrame(
8485
data=frame.tobytes(),
8586
sample_rate=self.info.audio_sample_rate,
8687
num_channels=frame.shape[1],
8788
samples_per_channel=frame.shape[0],
8889
),
89-
av_frame.time,
90+
av_frame.time + duration,
9091
)
9192

9293
def reset(self):
@@ -128,7 +129,7 @@ async def main(room: rtc.Room, room_name: str, media_path: str):
128129
media_info = streamer.info
129130

130131
# Create video and audio sources/tracks
131-
queue_size_ms = 50 # TODO: testing with different sizes
132+
queue_size_ms = 1000 # TODO: testing with different sizes
132133
video_source = rtc.VideoSource(
133134
width=media_info.video_width,
134135
height=media_info.video_height,

livekit-rtc/livekit/rtc/synchronizer.py

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -90,39 +90,42 @@ async def _capture_video(self) -> None:
9090
while not self._stopped:
9191
frame, timestamp = await self._video_queue.get()
9292

93-
# debug
94-
frame_rgba = np.frombuffer(frame.data, dtype=np.uint8).reshape(
95-
frame.height, frame.width, 4
96-
)
97-
frame_bgr = cv2.cvtColor(frame_rgba[:, :, :3], cv2.COLOR_RGBA2BGR)
98-
frame_bgr = cv2.putText(
99-
frame_bgr,
100-
f"{self.actual_fps:.2f}fps, video time: {timestamp:.3f}s, audio time: {self.last_audio_time:.3f}s",
101-
(10, 100),
102-
cv2.FONT_HERSHEY_SIMPLEX,
103-
1,
104-
(0, 0, 255),
105-
2,
106-
)
107-
frame_rgba = cv2.cvtColor(frame_bgr, cv2.COLOR_BGR2RGBA)
108-
frame = VideoFrame(
109-
width=frame.width,
110-
height=frame.height,
111-
type=frame.type,
112-
data=frame_rgba.tobytes(),
113-
)
114-
count += 1
115-
if count % 30 == 0:
116-
print(
117-
f"{self.actual_fps:.2f}fps, last video time: {self.last_video_time:.3f}s, "
118-
f"last audio time: {self.last_audio_time:.3f}s"
93+
async with self._fps_controller:
94+
# debug
95+
frame_rgba = np.frombuffer(frame.data, dtype=np.uint8).reshape(
96+
frame.height, frame.width, 4
97+
)
98+
frame_bgr = cv2.cvtColor(frame_rgba[:, :, :3], cv2.COLOR_RGBA2BGR)
99+
frame_bgr = cv2.putText(
100+
frame_bgr,
101+
f"{self.actual_fps:.2f}fps, video time: {timestamp:.3f}s, "
102+
f"audio time: {self.last_audio_time:.3f}s, diff: {timestamp - self.last_audio_time:.3f}s",
103+
(10, 100),
104+
cv2.FONT_HERSHEY_SIMPLEX,
105+
1,
106+
(0, 0, 255),
107+
2,
108+
)
109+
frame_rgba = cv2.cvtColor(frame_bgr, cv2.COLOR_BGR2RGBA)
110+
frame = VideoFrame(
111+
width=frame.width,
112+
height=frame.height,
113+
type=frame.type,
114+
data=frame_rgba.tobytes(),
119115
)
120-
# end debug
116+
count += 1
117+
# end debug
121118

122-
async with self._fps_controller:
123119
self._video_source.capture_frame(frame)
124120
if timestamp is not None:
125121
self._last_video_time = timestamp
122+
123+
if count % 30 == 0:
124+
diff = self.last_video_time - self.last_audio_time
125+
print(
126+
f"{self.actual_fps:.2f}fps, last video time: {self.last_video_time:.3f}s, "
127+
f"last audio time: {self.last_audio_time:.3f}s, diff: {diff:.3f}s"
128+
)
126129
self._video_queue.task_done()
127130

128131
async def aclose(self) -> None:
@@ -140,7 +143,7 @@ def last_video_time(self) -> float:
140143

141144
@property
142145
def last_audio_time(self) -> float:
143-
return self._last_audio_time
146+
return self._last_audio_time - self._audio_source.queued_duration
144147

145148

146149
class _FPSController:

0 commit comments

Comments
 (0)