File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11import os
22import logging
33import asyncio
4+ import time
45from signal import SIGINT , SIGTERM
56from 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 )
Original file line number Diff line number Diff line change 11import os
22import logging
33import asyncio
4+ import time
45from signal import SIGINT , SIGTERM
56from 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 )
Original file line number Diff line number Diff line change 1414
1515from __future__ import annotations
1616
17- import time
1817from dataclasses import dataclass
1918from 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
10275class LocalDataTrack :
10376 """Data track published by the local participant."""
Original file line number Diff line number Diff line change 2020
2121import asyncio
2222import os
23+ import time
2324import uuid
2425from typing import Callable , TypeVar
2526import 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
You can’t perform that action at this time.
0 commit comments