@@ -9,7 +9,8 @@ use uuid::Uuid;
99use crate :: rdp:: client:: { attempt_rdp_connection, ConnectionOutcome , SessionCommand } ;
1010use crate :: rdp:: clipboard:: SessionClipboardBackend ;
1111use crate :: rdp:: display:: FrameBuffer ;
12- use crate :: rdp:: frame_transport:: { encode_full_frame_packet, encode_image_update_packet} ;
12+ use crate :: rdp:: frame_transport:: { encode_full_frame_packet, encode_h264_packet, encode_image_update_packet} ;
13+ use crate :: renderer:: h264_encoder:: H264FrameEncoder ;
1314use crate :: store:: connections:: ConnectionConfig ;
1415
1516pub const MAX_SESSIONS : usize = 10 ;
@@ -100,6 +101,7 @@ struct SessionActor {
100101 reconnect_attempts : u32 ,
101102 last_error : Option < String > ,
102103 auto_reconnect : bool ,
104+ h264_encoder : Option < H264FrameEncoder > ,
103105}
104106
105107impl SessionActor {
@@ -119,6 +121,18 @@ impl SessionActor {
119121 config. display_height . unwrap_or ( 1080 ) ,
120122 ) ;
121123
124+ // Try to create the H.264 encoder; fall back to None if it fails
125+ let h264_encoder = match H264FrameEncoder :: new ( width, height) {
126+ Ok ( enc) => {
127+ log:: info!( "H.264 encoder initialized for {}x{}" , width, height) ;
128+ Some ( enc)
129+ }
130+ Err ( e) => {
131+ log:: warn!( "Failed to initialize H.264 encoder, will use raw RGBA: {}" , e) ;
132+ None
133+ }
134+ } ;
135+
122136 Self {
123137 id,
124138 config,
@@ -139,6 +153,7 @@ impl SessionActor {
139153 reconnect_attempts : 0 ,
140154 last_error : None ,
141155 auto_reconnect,
156+ h264_encoder,
142157 }
143158 }
144159
@@ -210,7 +225,47 @@ impl SessionActor {
210225 }
211226 }
212227
228+ /// Try to send a frame as H.264. Returns Ok(true) if sent, Ok(false) if encoder unavailable.
229+ fn try_send_h264_frame (
230+ & mut self ,
231+ rgba_data : & [ u8 ] ,
232+ width : u32 ,
233+ height : u32 ,
234+ ) -> Result < bool , DisconnectReason > {
235+ let encoder = match self . h264_encoder . as_mut ( ) {
236+ Some ( enc) => enc,
237+ None => return Ok ( false ) ,
238+ } ;
239+
240+ match encoder. encode_rgba ( rgba_data, width, height) {
241+ Ok ( h264_data) => {
242+ if h264_data. is_empty ( ) {
243+ return Ok ( false ) ;
244+ }
245+ let packet = encode_h264_packet ( width as u16 , height as u16 , & h264_data) ;
246+ self . send_frame_packet ( packet) ?;
247+ Ok ( true )
248+ }
249+ Err ( e) => {
250+ log:: warn!( "H.264 encode failed, falling back to raw RGBA: {}" , e) ;
251+ // Disable H.264 encoder on failure
252+ self . h264_encoder = None ;
253+ Ok ( false )
254+ }
255+ }
256+ }
257+
213258 fn send_mock_frame ( & mut self ) -> Result < ( ) , DisconnectReason > {
259+ // Try H.264 encoding first for the mock frame
260+ let w = self . frame_buffer . width ;
261+ let h = self . frame_buffer . height ;
262+ // We need to clone data to avoid borrow conflict with self
263+ let rgba_data = self . frame_buffer . data . clone ( ) ;
264+ if self . try_send_h264_frame ( & rgba_data, w, h) ? {
265+ return Ok ( ( ) ) ;
266+ }
267+
268+ // Fall back to raw RGBA
214269 let packet = encode_full_frame_packet ( & self . frame_buffer ) ;
215270 self . send_frame_packet ( packet)
216271 }
0 commit comments