@@ -3,9 +3,11 @@ use std::sync::Arc;
33use dashmap:: DashMap ;
44use parking_lot:: RwLock ;
55use str0m:: {
6- Candidate ,
6+ Candidate , Input ,
77 change:: { SdpAnswer , SdpOffer } ,
8+ media:: MediaData ,
89} ;
10+ use tracing:: { debug, error, info} ;
911
1012use crate :: {
1113 entities:: media:: Media ,
@@ -77,6 +79,22 @@ impl Str0mRoom {
7779 ( callback) ( is_migrate) . await ;
7880 } ) ;
7981 } ) ;
82+
83+ // Set media data callback to forward to subscribers
84+ let room_clone = self . clone ( ) ;
85+ let participant_id_clone = participant_id. clone ( ) ;
86+ session_write. set_media_data_callback ( move |media_data| {
87+ let room_clone = room_clone. clone ( ) ;
88+ let participant_id = participant_id_clone. clone ( ) ;
89+ tokio:: spawn ( async move {
90+ if let Err ( e) = room_clone
91+ . forward_media_data ( & participant_id, media_data)
92+ . await
93+ {
94+ error ! ( "Failed to forward media data: {:?}" , e) ;
95+ }
96+ } ) ;
97+ } ) ;
8098 }
8199
82100 // Add the session to publishers
@@ -85,19 +103,22 @@ impl Str0mRoom {
85103
86104 // Handle SDP negotiation based on connection type
87105 if params. connection_type == ConnectionType :: SFU {
106+ println ! ( "[Str0mRoom] params.sdp: {:?}" , params. sdp. len( ) ) ;
88107 // For SFU, we need to accept the offer and create an answer
89- let offer: SdpOffer =
90- serde_json :: from_str ( & params . sdp ) . map_err ( |_| WebRTCError :: FailedToCreateOffer ) ?;
91-
108+ let offer: SdpOffer = SdpOffer :: from_sdp_string ( & params . sdp )
109+ . map_err ( |_| WebRTCError :: FailedToCreateOffer ) ?;
110+ println ! ( "===> parse offer" ) ;
92111 let mut session_write = session. write ( ) ;
93112 let answer = session_write. accept_offer ( offer) ?;
94113
114+ println ! ( "===> accept offer" ) ;
115+
95116 // Convert answer to string
96- let answer_json =
97- serde_json:: to_string ( & answer) . map_err ( |_| WebRTCError :: FailedToCreateAnswer ) ?;
117+ // let answer_json =
118+ // serde_json::to_string(&answer).map_err(|_| WebRTCError::FailedToCreateAnswer)?;
98119
99120 return Ok ( Some ( JoinRoomResponse {
100- sdp : answer_json ,
121+ sdp : answer . to_sdp_string ( ) ,
101122 is_recording : false ,
102123 } ) ) ;
103124 } else {
@@ -147,10 +168,10 @@ impl Str0mRoom {
147168 let subscriber_session = self . session_manager . create_session (
148169 participant_id. clone ( ) ,
149170 ConnectionType :: SFU ,
150- true , // is_video_enabled
151- true , // is_audio_enabled
152- false , // is_e2ee_enabled
153- StreamingProtocol :: HLS , // Use HLS as default for subscribers
171+ true , // is_video_enabled
172+ true , // is_audio_enabled
173+ false , // is_e2ee_enabled
174+ StreamingProtocol :: SFU ,
154175 ) ;
155176
156177 // Set up callbacks for subscriber
@@ -192,6 +213,40 @@ impl Str0mRoom {
192213 }
193214 }
194215
216+ /// Forward media data from a publisher to all its subscribers
217+ pub async fn forward_media_data (
218+ & self ,
219+ publisher_id : & str ,
220+ media_data : MediaData ,
221+ ) -> Result < ( ) , WebRTCError > {
222+ let prefix = format ! ( "p_{publisher_id}_" ) ;
223+
224+ // Find all subscribers for this publisher
225+ for entry in self . subscribers . iter ( ) {
226+ if entry. key ( ) . starts_with ( & prefix) {
227+ let subscriber = entry. value ( ) ;
228+ let mut session_write = subscriber. write ( ) ;
229+
230+ // For now, we'll just log the media data forwarding
231+ // In a real implementation, you would need to properly handle
232+ // the media data according to str0m's API
233+ debug ! (
234+ "Forwarding media data from publisher {} to subscriber {}: mid={:?}, data_len={}" ,
235+ publisher_id,
236+ entry. key( ) ,
237+ media_data. mid,
238+ media_data. data. len( )
239+ ) ;
240+
241+ // TODO: Implement proper media data forwarding using str0m's API
242+ // This would typically involve creating the appropriate media tracks
243+ // and writing the data to them
244+ }
245+ }
246+
247+ Ok ( ( ) )
248+ }
249+
195250 pub fn subscribe_hls_live_stream (
196251 & self ,
197252 params : SubscribeHlsLiveStreamParams ,
0 commit comments