Karan Adhikari commited on
Commit
f97130e
·
unverified ·
1 Parent(s): 5eb973b

Update stream_routes.py

Browse files
Backend/fastapi/routes/stream_routes.py CHANGED
@@ -385,17 +385,34 @@ async def media_streamer(
385
  @router.get("/stream/stats")
386
  async def get_stream_stats():
387
  now = time.time()
 
388
  PRUNE_SECONDS = 3
389
- INACTIVE_TIMEOUT = 10
390
 
391
  for sid, info in list(ACTIVE_STREAMS.items()):
392
- status = info.get("status")
393
- # Check end_ts first, which is set when a stream organically finishes
394
- last_ts = info.get("end_ts") or info.get("last_ts") or info.get("start_ts", now)
395
- if status == "active" and (now - last_ts > INACTIVE_TIMEOUT):
396
- info["status"] = "finished"
397
- info["end_ts"] = last_ts
398
- if status in ("cancelled", "error", "finished"):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
399
  if now - last_ts > PRUNE_SECONDS:
400
  try:
401
  RECENT_STREAMS.appendleft(ACTIVE_STREAMS.pop(sid))
 
385
  @router.get("/stream/stats")
386
  async def get_stream_stats():
387
  now = time.time()
388
+
389
  PRUNE_SECONDS = 3
390
+ INACTIVE_TIMEOUT = 15 # 15 sec no data = inactive
391
 
392
  for sid, info in list(ACTIVE_STREAMS.items()):
393
+ status = info.get("status", "active")
394
+
395
+ current_bytes = info.get("total_bytes", 0)
396
+
397
+ if "last_bytes" not in info:
398
+ info["last_bytes"] = current_bytes
399
+ info["last_activity_ts"] = now
400
+
401
+
402
+ if current_bytes > info["last_bytes"]:
403
+ # Data is flowing → update activity timestamp
404
+ info["last_bytes"] = current_bytes
405
+ info["last_activity_ts"] = now
406
+ info["status"] = "active" # ensure it stays active if resumed
407
+ else:
408
+ # No data flow → check inactivity timeout
409
+ if now - info["last_activity_ts"] > INACTIVE_TIMEOUT:
410
+ if status == "active":
411
+ info["status"] = "inactive"
412
+ info["end_ts"] = now
413
+
414
+ if info.get("status") in ("cancelled", "error", "finished", "inactive"):
415
+ last_ts = info.get("end_ts", info.get("last_activity_ts", now))
416
  if now - last_ts > PRUNE_SECONDS:
417
  try:
418
  RECENT_STREAMS.appendleft(ACTIVE_STREAMS.pop(sid))