@@ -59,8 +59,8 @@ def __init__(self, track: MediaStreamTrack, pipeline: Pipeline):
5959
6060 self ._lock = asyncio .Lock ()
6161 self ._fps_interval_frame_count = 0
62- self ._last_fps_calculation_time = time . monotonic ()
63- self ._fps_loop_start_time = self . _last_fps_calculation_time
62+ self ._last_fps_calculation_time = None
63+ self ._fps_loop_start_time = time . monotonic ()
6464 self ._fps = 0.0
6565 self ._fps_measurements = deque (maxlen = 60 )
6666 self ._running_event = asyncio .Event ()
@@ -84,19 +84,22 @@ async def _calculate_fps_loop(self):
8484 await self ._running_event .wait ()
8585 self ._fps_loop_start_time = time .monotonic ()
8686 while self .readyState != "ended" :
87- await asyncio .sleep (1 ) # Calculate FPS every second.
8887 async with self ._lock :
89- current_time_monotonic = time .monotonic ()
90- time_diff = current_time_monotonic - self ._last_fps_calculation_time
91- if time_diff > 0 :
88+ current_time = time .monotonic ()
89+ if self ._last_fps_calculation_time is not None :
90+ time_diff = current_time - self . _last_fps_calculation_time
9291 self ._fps = self ._fps_interval_frame_count / time_diff
9392 self ._fps_measurements .append (
94- self ._fps
95- ) # Store the FPS measurement
93+ {
94+ "timestamp" : current_time - self ._fps_loop_start_time ,
95+ "fps" : self ._fps ,
96+ }
97+ ) # Store the FPS measurement with timestamp
9698
97- # Reset start_time and frame_count for the next interval.
98- self ._last_fps_calculation_time = current_time_monotonic
99- self ._fps_interval_frame_count = 0
99+ # Reset start_time and frame_count for the next interval.
100+ self ._last_fps_calculation_time = current_time
101+ self ._fps_interval_frame_count = 0
102+ await asyncio .sleep (1 ) # Calculate FPS every second.
100103
101104 @property
102105 async def fps (self ) -> float :
@@ -128,7 +131,9 @@ async def average_fps(self) -> float:
128131 async with self ._lock :
129132 if not self ._fps_measurements :
130133 return 0.0
131- return sum (self ._fps_measurements ) / len (self ._fps_measurements )
134+ return sum (
135+ measurement ["fps" ] for measurement in self ._fps_measurements
136+ ) / len (self ._fps_measurements )
132137
133138 @property
134139 async def last_fps_calculation_time (self ) -> float :
@@ -405,7 +410,9 @@ async def on_shutdown(app: web.Application):
405410 # Add routes for getting stream statistics.
406411 stream_stats = StreamStats (app )
407412 app .router .add_get ("/streams/stats" , stream_stats .collect_all_stream_metrics )
408- app .router .add_get ("/stream/{stream_id}/stats" , stream_stats .collect_stream_metrics_by_id )
413+ app .router .add_get (
414+ "/stream/{stream_id}/stats" , stream_stats .collect_stream_metrics_by_id
415+ )
409416
410417 # Add hosted platform route prefix.
411418 # NOTE: This ensures that the local and hosted experiences have consistent routes.
0 commit comments