1
+ extern crate clap;
1
2
extern crate glib;
2
3
extern crate gstreamer as gst;
3
4
extern crate gstreamer_sdp;
@@ -86,20 +87,26 @@ fn check_plugins() -> bool {
86
87
}
87
88
88
89
fn setup_call ( app_control : & Arc < Mutex < AppControl > > ) -> AppState {
89
- app_control. lock ( ) . unwrap ( ) . app_state = AppState :: PeerConnecting ;
90
- println ! ( "Setting up signalling server call with 1" ) ;
91
- app_control. lock ( ) . unwrap ( ) . out . send ( "SESSION 1" ) . unwrap ( ) ;
90
+ let mut app_control = app_control. lock ( ) . unwrap ( ) ;
91
+ app_control. app_state = AppState :: PeerConnecting ;
92
+ println ! (
93
+ "Setting up signalling server call with {}" ,
94
+ app_control. peer_id
95
+ ) ;
96
+ app_control
97
+ . ws_sender
98
+ . send ( format ! ( "SESSION {}" , app_control. peer_id) )
99
+ . unwrap ( ) ;
92
100
return AppState :: PeerConnecting ;
93
101
}
94
102
95
103
fn register_with_server ( app_control : & Arc < Mutex < AppControl > > ) -> AppState {
96
- app_control. lock ( ) . unwrap ( ) . app_state = AppState :: ServerRegistering ;
104
+ let mut app_control = app_control. lock ( ) . unwrap ( ) ;
105
+ app_control. app_state = AppState :: ServerRegistering ;
97
106
let our_id = rand:: thread_rng ( ) . gen_range ( 10 , 10_000 ) ;
98
107
println ! ( "Registering id {} with server" , our_id) ;
99
108
app_control
100
- . lock ( )
101
- . unwrap ( )
102
- . out
109
+ . ws_sender
103
110
. send ( format ! ( "HELLO {}" , our_id) )
104
111
. unwrap ( ) ;
105
112
return AppState :: ServerRegistering ;
@@ -117,7 +124,8 @@ fn send_sdp_offer(
117
124
app_control : & Arc < Mutex < AppControl > > ,
118
125
offer : gstreamer_webrtc:: WebRTCSessionDescription ,
119
126
) {
120
- if app_control. lock ( ) . unwrap ( ) . app_state < AppState :: PeerCallNegotiating {
127
+ let app_control = app_control. lock ( ) . unwrap ( ) ;
128
+ if app_control. app_state < AppState :: PeerCallNegotiating {
121
129
// TODO signal and cleanup
122
130
panic ! ( "Can't send offer, not in call" ) ;
123
131
} ;
@@ -127,12 +135,7 @@ fn send_sdp_offer(
127
135
"sdp" : sdp_message_as_text( offer) . unwrap( ) ,
128
136
}
129
137
} ) ;
130
- app_control
131
- . lock ( )
132
- . unwrap ( )
133
- . out
134
- . send ( message. to_string ( ) )
135
- . unwrap ( ) ;
138
+ app_control. ws_sender . send ( message. to_string ( ) ) . unwrap ( ) ;
136
139
}
137
140
138
141
fn on_offer_created (
@@ -260,7 +263,8 @@ fn send_ice_candidate_message(
260
263
app_control : & Arc < Mutex < AppControl > > ,
261
264
values : & [ glib:: Value ] ,
262
265
) -> Option < glib:: Value > {
263
- if app_control. lock ( ) . unwrap ( ) . app_state < AppState :: PeerCallNegotiating {
266
+ let app_control = app_control. lock ( ) . unwrap ( ) ;
267
+ if app_control. app_state < AppState :: PeerCallNegotiating {
264
268
panic ! ( "Can't send ICE, not in call" ) ;
265
269
}
266
270
@@ -273,12 +277,7 @@ fn send_ice_candidate_message(
273
277
"sdpMLineIndex" : mlineindex,
274
278
}
275
279
} ) ;
276
- app_control
277
- . lock ( )
278
- . unwrap ( )
279
- . out
280
- . send ( message. to_string ( ) )
281
- . unwrap ( ) ;
280
+ app_control. ws_sender . send ( message. to_string ( ) ) . unwrap ( ) ;
282
281
None
283
282
}
284
283
@@ -326,7 +325,8 @@ struct WsClient {
326
325
327
326
struct AppControl {
328
327
app_state : AppState ,
329
- out : ws:: Sender ,
328
+ ws_sender : ws:: Sender ,
329
+ peer_id : String ,
330
330
}
331
331
332
332
impl WsClient {
@@ -338,7 +338,6 @@ impl WsClient {
338
338
impl ws:: Handler for WsClient {
339
339
fn on_open ( & mut self , _: ws:: Handshake ) -> ws:: Result < ( ) > {
340
340
self . update_state ( AppState :: ServerConnected ) ;
341
- // TODO: replace with random
342
341
self . update_state ( register_with_server ( & self . app_control . clone ( ) ) ) ;
343
342
Ok ( ( ) )
344
343
}
@@ -348,7 +347,6 @@ impl ws::Handler for WsClient {
348
347
let msg_text = msg. into_text ( ) . unwrap ( ) ;
349
348
if msg_text == "HELLO" {
350
349
if self . app_control . lock ( ) . unwrap ( ) . app_state != AppState :: ServerRegistering {
351
- // TODO: signal and cleanup
352
350
panic ! ( "ERROR: Received HELLO when not registering" ) ;
353
351
}
354
352
self . update_state ( AppState :: ServerRegistered ) ;
@@ -357,7 +355,6 @@ impl ws::Handler for WsClient {
357
355
}
358
356
if msg_text == "SESSION_OK" {
359
357
if self . app_control . lock ( ) . unwrap ( ) . app_state != AppState :: PeerConnecting {
360
- // TODO: signal and cleanup
361
358
panic ! ( "ERROR: Received SESSION_OK when not calling" ) ;
362
359
}
363
360
self . update_state ( AppState :: PeerConnected ) ;
@@ -391,7 +388,7 @@ impl ws::Handler for WsClient {
391
388
self . app_control
392
389
. lock ( )
393
390
. unwrap ( )
394
- . out
391
+ . ws_sender
395
392
. close ( ws:: CloseCode :: Normal )
396
393
. unwrap ( ) ;
397
394
panic ! ( "Got websocket error {:?}" , error) ;
@@ -440,25 +437,53 @@ impl ws::Handler for WsClient {
440
437
441
438
Ok ( ( ) )
442
439
}
440
+
441
+ fn on_close ( & mut self , _code : ws:: CloseCode , _reason : & str ) {
442
+ self . app_control . lock ( ) . unwrap ( ) . app_state = AppState :: ServerClosed ;
443
+ }
443
444
}
444
445
445
- fn connect_to_websocket_server_async ( ) {
446
- ws:: connect ( "ws://signalling:8443" , |out| WsClient {
446
+ fn connect_to_websocket_server_async ( peer_id : & str , server : & str ) {
447
+ println ! ( "Connecting to server {}" , server) ;
448
+ ws:: connect ( server, |ws_sender| WsClient {
447
449
webrtc : None ,
448
450
app_control : Arc :: new ( Mutex :: new ( AppControl {
449
- out : out,
451
+ ws_sender : ws_sender,
452
+ peer_id : peer_id. to_string ( ) ,
450
453
app_state : AppState :: ServerConnecting ,
451
454
} ) ) ,
452
455
} ) . unwrap ( ) ;
453
456
}
454
457
455
458
fn main ( ) {
459
+ let matches = clap:: App :: new ( "Sendrcv rust" )
460
+ . arg (
461
+ clap:: Arg :: with_name ( "peer-id" )
462
+ . help ( "String ID of the peer to connect to" )
463
+ . long ( "peer-id" )
464
+ . required ( true )
465
+ . takes_value ( true ) ,
466
+ )
467
+ . arg (
468
+ clap:: Arg :: with_name ( "server" )
469
+ . help ( "Signalling server to connect to" )
470
+ . long ( "server" )
471
+ . required ( false )
472
+ . takes_value ( true ) ,
473
+ )
474
+ . get_matches ( ) ;
475
+
456
476
gst:: init ( ) . unwrap ( ) ;
457
477
458
478
if !check_plugins ( ) {
459
479
return ;
460
480
}
461
481
let main_loop = glib:: MainLoop :: new ( None , false ) ;
462
- connect_to_websocket_server_async ( ) ;
482
+ connect_to_websocket_server_async (
483
+ matches. value_of ( "peer-id" ) . unwrap ( ) ,
484
+ matches
485
+ . value_of ( "server" )
486
+ . unwrap_or ( "wss://webrtc.nirbheek.in:8443" ) ,
487
+ ) ;
463
488
main_loop. run ( ) ;
464
489
}
0 commit comments