Skip to content

Commit 991b0b1

Browse files
committed
Remove user timestamp helper methods
1 parent a37ce60 commit 991b0b1

4 files changed

Lines changed: 12 additions & 35 deletions

File tree

examples/data_tracks/publisher.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import os
22
import logging
33
import asyncio
4+
import time
45
from signal import SIGINT, SIGTERM
56
from livekit import rtc
67

@@ -19,7 +20,7 @@ async def push_frames(track: rtc.LocalDataTrack):
1920
logging.info("Pushing frame")
2021
data = await read_sensor()
2122
try:
22-
frame = rtc.DataTrackFrame(payload=data).with_user_timestamp_now()
23+
frame = rtc.DataTrackFrame(payload=data, user_timestamp=int(time.time() * 1000))
2324
track.try_push(frame)
2425
except rtc.PushFrameError as e:
2526
logging.error("Failed to push frame: %s", e)

examples/data_tracks/subscriber.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import os
22
import logging
33
import asyncio
4+
import time
45
from signal import SIGINT, SIGTERM
56
from livekit import rtc
67

@@ -19,8 +20,8 @@ async def subscribe(track: rtc.RemoteDataTrack):
1920
async for frame in track.subscribe():
2021
logging.info("Received frame (%d bytes)", len(frame.payload))
2122

22-
latency = frame.duration_since_timestamp()
23-
if latency is not None:
23+
if frame.user_timestamp is not None:
24+
latency = (int(time.time() * 1000) - frame.user_timestamp) / 1000.0
2425
logging.info("Latency: %.3f s", latency)
2526
except rtc.SubscribeDataTrackError as e:
2627
logging.error("Failed to subscribe to '%s': %s", track.info.name, e.message)

livekit-rtc/livekit/rtc/data_track.py

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
from __future__ import annotations
1616

17-
import time
1817
from dataclasses import dataclass
1918
from typing import AsyncIterator, Optional
2019

@@ -72,32 +71,6 @@ class DataTrackFrame:
7271
user_timestamp: Optional[int] = None
7372
"""The frame's user timestamp, if one is associated."""
7473

75-
def with_user_timestamp_now(self) -> DataTrackFrame:
76-
"""Associates the current Unix timestamp (in milliseconds) with the frame.
77-
78-
Returns a new ``DataTrackFrame`` with the timestamp set; the original is
79-
not modified.
80-
"""
81-
return DataTrackFrame(
82-
payload=self.payload,
83-
user_timestamp=int(time.time() * 1000),
84-
)
85-
86-
def duration_since_timestamp(self) -> Optional[float]:
87-
"""If the frame has a user timestamp, calculate how long has passed
88-
relative to the current system time.
89-
90-
The timestamp is assumed to be a Unix timestamp in milliseconds
91-
(as set by :meth:`with_user_timestamp_now` on the publisher side).
92-
93-
Returns:
94-
The elapsed duration in seconds, or ``None`` if no timestamp is set.
95-
"""
96-
if self.user_timestamp is None:
97-
return None
98-
elapsed_ms = int(time.time() * 1000) - self.user_timestamp
99-
return elapsed_ms / 1000.0
100-
10174

10275
class LocalDataTrack:
10376
"""Data track published by the local participant."""

tests/rtc/test_e2e.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import asyncio
2222
import os
23+
import time
2324
import uuid
2425
from typing import Callable, TypeVar
2526
import numpy as np
@@ -493,8 +494,9 @@ def on_data_track_unpublished(sid: str):
493494
async def push_frames():
494495
for i in range(FRAME_COUNT):
495496
frame = rtc.DataTrackFrame(
496-
payload=bytes([i] * PAYLOAD_SIZE)
497-
).with_user_timestamp_now()
497+
payload=bytes([i] * PAYLOAD_SIZE),
498+
user_timestamp=int(time.time() * 1000),
499+
)
498500
local_track.try_push(frame)
499501
await asyncio.sleep(0.1)
500502
local_track.unpublish()
@@ -507,9 +509,9 @@ async def publish_and_receive():
507509
assert all(b == first_byte for b in frame.payload), "Payload bytes are not uniform"
508510
assert len(frame.payload) == PAYLOAD_SIZE
509511
assert frame.user_timestamp is not None
510-
latency = frame.duration_since_timestamp()
511-
assert latency is not None and latency < 5.0, (
512-
f"Timestamp latency too high or missing: {latency}"
512+
latency = (int(time.time() * 1000) - frame.user_timestamp) / 1000.0
513+
assert latency < 5.0, (
514+
f"Timestamp latency too high: {latency}"
513515
)
514516
recv_count += 1
515517
await push_task

0 commit comments

Comments
 (0)