1+ use std:: pin:: Pin ;
12use std:: time:: Duration ;
23use std:: { collections:: HashMap , sync:: Arc } ;
34
45use pulsebeam_runtime:: actor:: ActorKind ;
56use pulsebeam_runtime:: prelude:: * ;
67use pulsebeam_runtime:: { actor, mailbox, net} ;
78use str0m:: { Rtc , RtcError , error:: SdpError } ;
8- use tokio:: time:: Instant ;
9+ use tokio:: time:: { Instant , Sleep } ;
910use tokio_metrics:: TaskMonitor ;
1011
1112use crate :: participant:: core:: { CoreEvent , ParticipantCore } ;
@@ -81,16 +82,19 @@ impl actor::Actor<ParticipantMessageSet> for ParticipantActor {
8182 . await ;
8283 let mut stats_interval = tokio:: time:: interval ( Duration :: from_millis ( 200 ) ) ;
8384 let mut current_deadline = Instant :: now ( ) + Duration :: from_secs ( 1 ) ;
85+ let mut new_deadline = Some ( current_deadline) ;
86+
8487 let rtc_timer = tokio:: time:: sleep_until ( current_deadline) ;
8588 tokio:: pin!( rtc_timer) ;
8689
8790 loop {
88- let Some ( new_deadline) = self . core . poll_rtc ( ) else {
89- break ;
90- } ;
91- if new_deadline != current_deadline {
92- rtc_timer. as_mut ( ) . reset ( new_deadline) ;
93- current_deadline = new_deadline;
91+ match new_deadline {
92+ Some ( new_deadline) if new_deadline != current_deadline => {
93+ rtc_timer. as_mut ( ) . reset ( new_deadline) ;
94+ current_deadline = new_deadline;
95+ }
96+ None => break ,
97+ _ => { }
9498 }
9599
96100 let events: Vec < _ > = self . core . drain_events ( ) . collect ( ) ;
@@ -99,7 +103,6 @@ impl actor::Actor<ParticipantMessageSet> for ParticipantActor {
99103 }
100104
101105 tokio:: select! {
102- biased;
103106 res = ctx. sys_rx. recv( ) => {
104107 match res {
105108 Some ( msg) => match msg {
@@ -115,15 +118,22 @@ impl actor::Actor<ParticipantMessageSet> for ParticipantActor {
115118 Ok ( _) = self . egress. writable( ) , if !self . core. batcher. is_empty( ) => {
116119 self . core. batcher. flush( & self . egress) ;
117120 } ,
118- Some ( batch) = gateway_rx. recv( ) => self . core. handle_udp_packet_batch( batch) ,
119121 Some ( ( meta, pkt) ) = self . core. downstream. next( ) => {
120122 self . core. handle_forward_rtp( meta, pkt) ;
123+ while let Some ( ( mid, pkt) ) = self . core. downstream. next( ) . now_or_never( ) . flatten( ) {
124+ self . core. handle_forward_rtp( mid, pkt) ;
125+ }
126+
127+ new_deadline = self . core. poll_rtc( ) ;
128+ } ,
129+ Some ( batch) = gateway_rx. recv( ) => {
130+ new_deadline = self . core. handle_udp_packet_batch( batch) ;
121131 } ,
122132 now = stats_interval. tick( ) => {
123133 self . core. poll_stats( now) ;
124134 }
125135 _ = & mut rtc_timer => {
126- self . core. handle_timeout( ) ;
136+ new_deadline = self . core. handle_timeout( ) ;
127137 } ,
128138 }
129139 }
0 commit comments