mduppes commited on
Commit
a17771d
1 Parent(s): bb98523

backend_max_speakers (#14)

Browse files

- Backend changes to read in MAX_SPEAKERS env var (343dd345a0767df37e58cfb01d5963d23bf38253)
- Update correct file (7571f4ca2df769af86a904202e0e147c24cea6d7)
- Merge changes (6a17cbca89841bbb939d12f019c4ce7e63e11294)
- Check int type (a5278e321d0a99e5e75fe4c90fed1886599fe556)

Files changed (1) hide show
  1. seamless_server/app_pubsub.py +15 -0
seamless_server/app_pubsub.py CHANGED
@@ -123,9 +123,12 @@ class ServerLock(TypedDict):
123
  client_id: str
124
  member_object: Member
125
 
 
126
 
127
  if os.environ.get("LOCK_SERVER_COMPLETELY", "0") == "1":
128
  logger.info("LOCK_SERVER_COMPLETELY is set. Server will be locked on startup.")
 
 
129
  dummy_server_lock_member_object = Member(
130
  client_id="seamless_user", session_id="dummy", name="Seamless User"
131
  )
@@ -556,6 +559,12 @@ async def join_room(sid, client_id, room_id_from_client, config_dict):
556
 
557
  return {"roomsJoined": sio.rooms(sid), "roomID": room_id}
558
 
 
 
 
 
 
 
559
 
560
  # TODO: Add code to prevent more than one speaker from connecting/streaming at a time
561
  @sio.event
@@ -576,6 +585,12 @@ async def configure_stream(sid, config):
576
  )
577
  return {"status": "error", "message": "member_or_room_is_none"}
578
 
 
 
 
 
 
 
579
  # If there is a server lock WITH an active transcoder session, prevent other users from configuring and starting a stream
580
  # If the server lock client does NOT have an active transcoder session allow this to proceed, knowing that
581
  # this stream will be interrupted if the server lock client starts streaming
 
123
  client_id: str
124
  member_object: Member
125
 
126
+ MAX_SPEAKERS = os.environ.get("MAX_SPEAKERS")
127
 
128
  if os.environ.get("LOCK_SERVER_COMPLETELY", "0") == "1":
129
  logger.info("LOCK_SERVER_COMPLETELY is set. Server will be locked on startup.")
130
+ if MAX_SPEAKERS is not None and int(MAX_SPEAKERS):
131
+ logger.info(f"MAX_SPEAKERS is set to: {MAX_SPEAKERS}")
132
  dummy_server_lock_member_object = Member(
133
  client_id="seamless_user", session_id="dummy", name="Seamless User"
134
  )
 
559
 
560
  return {"roomsJoined": sio.rooms(sid), "roomID": room_id}
561
 
562
+ def allow_speaker(room, client_id):
563
+ if MAX_SPEAKERS is not None and client_id in room.speakers:
564
+ room_statuses = {room_id: room.get_room_status_dict() for room_id, room in rooms.items()}
565
+ speakers = sum(room_status["activeTranscoders"] for room_status in room_statuses.values())
566
+ return speakers < int(MAX_SPEAKERS)
567
+ return True
568
 
569
  # TODO: Add code to prevent more than one speaker from connecting/streaming at a time
570
  @sio.event
 
585
  )
586
  return {"status": "error", "message": "member_or_room_is_none"}
587
 
588
+ if not allow_speaker(room, client_id):
589
+ logger.error(
590
+ f"In MAX_SPEAKERS mode we only allow one speaker at a time. Ignoring request to configure stream from client {client_id}."
591
+ )
592
+ return {"status": "error", "message": "max_speakers"}
593
+
594
  # If there is a server lock WITH an active transcoder session, prevent other users from configuring and starting a stream
595
  # If the server lock client does NOT have an active transcoder session allow this to proceed, knowing that
596
  # this stream will be interrupted if the server lock client starts streaming