1- use std:: { collections:: HashMap , sync:: Arc } ;
1+ use std:: { collections:: HashMap , sync:: Arc , time :: Duration } ;
22
3- use futures:: stream:: { FuturesUnordered , StreamExt } ;
3+ use futures:: {
4+ future:: Either ,
5+ stream:: { FuturesUnordered , StreamExt } ,
6+ } ;
47use str0m:: Rtc ;
58
69use crate :: {
@@ -10,6 +13,8 @@ use crate::{
1013} ;
1114use pulsebeam_runtime:: actor;
1215
16+ const EMPTY_ROOM_TIMEOUT : Duration = Duration :: from_secs ( 30 ) ;
17+
1318#[ derive( Debug ) ]
1419pub enum RoomMessage {
1520 PublishTrack ( Arc < TrackMeta > ) ,
@@ -60,7 +65,15 @@ impl actor::Actor for RoomActor {
6065 }
6166
6267 async fn run ( & mut self , ctx : & mut actor:: ActorContext < Self > ) -> Result < ( ) , actor:: ActorError > {
63- pulsebeam_runtime:: actor_loop!( self , ctx, pre_select: { } ,
68+ pulsebeam_runtime:: actor_loop!( self , ctx,
69+ pre_select: {
70+ let empty_room_timer =
71+ if self . state. participants. is_empty( ) {
72+ Either :: Left ( tokio:: time:: sleep( EMPTY_ROOM_TIMEOUT ) )
73+ } else {
74+ Either :: Right ( futures:: future:: pending:: <( ) >( ) )
75+ } ;
76+ } ,
6477 select: {
6578 Some ( ( participant_id, _) ) = self . participant_tasks. next( ) => {
6679 self . handle_participant_left( participant_id) . await ;
@@ -69,6 +82,11 @@ impl actor::Actor for RoomActor {
6982 Some ( ( track_meta, _) ) = self . track_tasks. next( ) => {
7083 self . handle_track_unpublished( track_meta) . await ;
7184 }
85+
86+ _ = empty_room_timer => {
87+ tracing:: info!( "room has been empty for: {EMPTY_ROOM_TIMEOUT:?}, exiting." ) ;
88+ break ;
89+ }
7290 }
7391 ) ;
7492
0 commit comments