@@ -22,7 +22,7 @@ use std::sync::{mpsc, Arc, Mutex};
22
22
use std:: thread;
23
23
use websocket:: message:: OwnedMessage ;
24
24
25
- const STUN_SERVER : & ' static str = "stun://stun.l.google.com:19302 " ;
25
+ const STUN_SERVER : & str = "stun://stun.l.google.com:19302 " ;
26
26
lazy_static ! {
27
27
static ref RTP_CAPS_OPUS : gst:: GstRc <gst:: CapsRef > = {
28
28
gst:: Caps :: new_simple(
@@ -46,9 +46,9 @@ lazy_static! {
46
46
} ;
47
47
}
48
48
49
- #[ derive( PartialEq , PartialOrd , Eq , Debug , Clone , Ord ) ]
49
+ #[ derive( PartialEq , PartialOrd , Eq , Debug , Copy , Clone , Ord ) ]
50
50
enum AppState {
51
- AppStateErr = 1 ,
51
+ Error = 1 ,
52
52
ServerConnected ,
53
53
ServerRegistering = 2000 ,
54
54
ServerRegisteringError ,
@@ -76,7 +76,7 @@ enum JsonMsg {
76
76
} ,
77
77
}
78
78
79
- #[ derive( Debug ) ]
79
+ #[ derive( Copy , Clone , Debug , PartialEq , Eq ) ]
80
80
enum MediaType {
81
81
Audio ,
82
82
Video ,
@@ -138,14 +138,14 @@ fn check_plugins() -> Result<(), Error> {
138
138
missing. push ( plugin_name)
139
139
}
140
140
}
141
- if missing. len ( ) > 0 {
141
+ if ! missing. is_empty ( ) {
142
142
Err ( MissingElements ( missing) ) ?
143
143
} else {
144
144
Ok ( ( ) )
145
145
}
146
146
}
147
147
148
- fn send_sdp_offer ( app_control : & AppControl , offer : gst_webrtc:: WebRTCSessionDescription ) {
148
+ fn send_sdp_offer ( app_control : & AppControl , offer : & gst_webrtc:: WebRTCSessionDescription ) {
149
149
if app_control. app_state_cmp (
150
150
AppState :: PeerCallNegotiating ,
151
151
"Can't send offer, not in call" ,
@@ -162,7 +162,7 @@ fn send_sdp_offer(app_control: &AppControl, offer: gst_webrtc::WebRTCSessionDesc
162
162
163
163
fn on_offer_created (
164
164
app_control : & AppControl ,
165
- webrtc : gst:: Element ,
165
+ webrtc : & gst:: Element ,
166
166
promise : & gst:: Promise ,
167
167
) -> Result < ( ) , Error > {
168
168
if !app_control. app_state_eq (
@@ -180,7 +180,7 @@ fn on_offer_created(
180
180
. expect ( "Invalid argument" ) ;
181
181
webrtc. emit ( "set-local-description" , & [ & offer, & None :: < gst:: Promise > ] ) ?;
182
182
183
- send_sdp_offer ( & app_control, offer) ;
183
+ send_sdp_offer ( & app_control, & offer) ;
184
184
Ok ( ( ) )
185
185
}
186
186
@@ -190,7 +190,7 @@ fn on_negotiation_needed(app_control: &AppControl, values: &[glib::Value]) -> Re
190
190
let webrtc_clone = webrtc. clone ( ) ;
191
191
let app_control_clone = app_control. clone ( ) ;
192
192
let promise = gst:: Promise :: new_with_change_func ( move |promise| {
193
- on_offer_created ( & app_control_clone, webrtc, promise) . unwrap ( ) ;
193
+ on_offer_created ( & app_control_clone, & webrtc, promise) . unwrap ( ) ;
194
194
} ) ;
195
195
webrtc_clone. emit ( "create-offer" , & [ & None :: < gst:: Structure > , & promise] ) ?;
196
196
Ok ( ( ) )
@@ -252,20 +252,21 @@ fn on_incoming_decodebin_stream(
252
252
253
253
let caps = pad. get_current_caps ( ) . unwrap ( ) ;
254
254
let name = caps. get_structure ( 0 ) . unwrap ( ) . get_name ( ) ;
255
- match if name. starts_with ( "video" ) {
255
+
256
+ let handled = if name. starts_with ( "video" ) {
256
257
handle_media_stream ( & pad, & pipe, MediaType :: Video )
257
258
} else if name. starts_with ( "audio" ) {
258
259
handle_media_stream ( & pad, & pipe, MediaType :: Audio )
259
260
} else {
260
261
println ! ( "Unknown pad {:?}, ignoring" , pad) ;
261
262
Ok ( ( ) )
262
- } {
263
- Ok ( ( ) ) => return None ,
264
- Err ( err) => {
265
- app_control. send_bus_error ( format ! ( "Error adding pad with caps {} {:?}" , name, err) ) ;
266
- return None ;
267
- }
268
263
} ;
264
+
265
+ if let Err ( err) = handled {
266
+ app_control. send_bus_error ( & format ! ( "Error adding pad with caps {} {:?}" , name, err) ) ;
267
+ }
268
+
269
+ None
269
270
}
270
271
271
272
fn on_incoming_stream (
@@ -302,7 +303,7 @@ fn send_ice_candidate_message(app_control: &AppControl, values: &[glib::Value])
302
303
let mlineindex = values[ 1 ] . get :: < u32 > ( ) . expect ( "Invalid argument" ) ;
303
304
let candidate = values[ 2 ] . get :: < String > ( ) . expect ( "Invalid argument" ) ;
304
305
let message = serde_json:: to_string ( & JsonMsg :: Ice {
305
- candidate : candidate ,
306
+ candidate,
306
307
sdp_mline_index : mlineindex,
307
308
} ) . unwrap ( ) ;
308
309
app_control. send_text_msg ( message. to_string ( ) ) ;
@@ -374,7 +375,7 @@ fn add_audio_source(pipeline: &gst::Pipeline, webrtcbin: &gst::Element) -> Resul
374
375
impl AppControl {
375
376
fn app_state_eq ( & self , state : AppState , error_msg : & ' static str ) -> bool {
376
377
if { self . 0 . lock ( ) . unwrap ( ) . app_state != state } {
377
- self . send_bus_error ( String :: from ( error_msg) ) ;
378
+ self . send_bus_error ( error_msg) ;
378
379
false
379
380
} else {
380
381
true
@@ -384,14 +385,14 @@ impl AppControl {
384
385
fn app_state_cmp ( & self , state : AppState , error_msg : & ' static str ) -> Ordering {
385
386
match { self . 0 . lock ( ) . unwrap ( ) . app_state . cmp ( & state) } {
386
387
Ordering :: Less => {
387
- self . send_bus_error ( String :: from ( error_msg) ) ;
388
+ self . send_bus_error ( error_msg) ;
388
389
Ordering :: Less
389
390
}
390
391
_foo => _foo,
391
392
}
392
393
}
393
394
394
- fn send_bus_error ( & self , body : String ) {
395
+ fn send_bus_error ( & self , body : & str ) {
395
396
let mbuilder =
396
397
gst:: Message :: new_application ( gst:: Structure :: new ( "error" , & [ ( "body" , & body) ] ) ) ;
397
398
let _ = self . 0 . lock ( ) . unwrap ( ) . bus . post ( & mbuilder. build ( ) ) ;
@@ -422,7 +423,8 @@ impl AppControl {
422
423
423
424
fn start_pipeline ( & self ) -> Result < ( ) , Error > {
424
425
let pipe = self . construct_pipeline ( ) ?;
425
- let webrtc = pipe. clone ( )
426
+ let webrtc = pipe
427
+ . clone ( )
426
428
. dynamic_cast :: < gst:: Bin > ( )
427
429
. unwrap ( )
428
430
. get_by_name ( "sendrecv" )
@@ -495,20 +497,21 @@ impl AppControl {
495
497
AppState :: ServerRegisteringError => AppState :: ServerRegisteringError ,
496
498
AppState :: PeerConnectionError => AppState :: PeerConnectionError ,
497
499
AppState :: PeerCallError => AppState :: PeerCallError ,
498
- AppState :: AppStateErr => AppState :: AppStateErr ,
499
- AppState :: ServerConnected => AppState :: AppStateErr ,
500
- AppState :: ServerRegistered => AppState :: AppStateErr ,
501
- AppState :: PeerCallStarted => AppState :: AppStateErr ,
500
+ AppState :: Error => AppState :: Error ,
501
+ AppState :: ServerConnected => AppState :: Error ,
502
+ AppState :: ServerRegistered => AppState :: Error ,
503
+ AppState :: PeerCallStarted => AppState :: Error ,
502
504
} ;
503
- return Err ( WsError ( error) ) ?;
505
+
506
+ Err ( WsError ( error) ) ?
504
507
}
505
- fn handle_sdp ( & self , type_ : String , sdp : String ) {
508
+ fn handle_sdp ( & self , type_ : & str , sdp : & str ) {
506
509
if !self . app_state_eq ( AppState :: PeerCallNegotiating , "Not ready to handle sdp" ) {
507
510
return ;
508
511
}
509
512
510
513
if type_ != "answer" {
511
- self . send_bus_error ( String :: from ( "Sdp type is not \" anser \" " ) ) ;
514
+ self . send_bus_error ( "Sdp type is not \" answer \" " ) ;
512
515
return ;
513
516
}
514
517
@@ -527,7 +530,7 @@ impl AppControl {
527
530
. unwrap ( ) ;
528
531
app_control. app_state = AppState :: PeerCallStarted ;
529
532
}
530
- fn handle_ice ( & self , sdp_mline_index : u32 , candidate : String ) {
533
+ fn handle_ice ( & self , sdp_mline_index : u32 , candidate : & str ) {
531
534
let app_control = self . 0 . lock ( ) . unwrap ( ) ;
532
535
app_control
533
536
. webrtc
@@ -536,7 +539,7 @@ impl AppControl {
536
539
. emit ( "add-ice-candidate" , & [ & sdp_mline_index, & candidate] )
537
540
. unwrap ( ) ;
538
541
}
539
- fn on_message ( & mut self , msg : String ) -> Result < ( ) , Error > {
542
+ fn on_message ( & mut self , msg : & str ) -> Result < ( ) , Error > {
540
543
if msg == "HELLO" {
541
544
return self . handle_hello ( ) ;
542
545
}
@@ -548,18 +551,18 @@ impl AppControl {
548
551
println ! ( "Got error message! {}" , msg) ;
549
552
return self . handle_error ( ) ;
550
553
}
551
- let json_msg: JsonMsg = serde_json:: from_str ( & msg) ?;
554
+ let json_msg: JsonMsg = serde_json:: from_str ( msg) ?;
552
555
match json_msg {
553
- JsonMsg :: Sdp { type_, sdp } => self . handle_sdp ( type_, sdp) ,
556
+ JsonMsg :: Sdp { type_, sdp } => self . handle_sdp ( & type_, & sdp) ,
554
557
JsonMsg :: Ice {
555
558
sdp_mline_index,
556
559
candidate,
557
- } => self . handle_ice ( sdp_mline_index, candidate) ,
560
+ } => self . handle_ice ( sdp_mline_index, & candidate) ,
558
561
} ;
559
562
Ok ( ( ) )
560
563
}
561
564
562
- fn close_and_quit ( & self , err : Error ) {
565
+ fn close_and_quit ( & self , err : & Error ) {
563
566
let app_control = self . 0 . lock ( ) . unwrap ( ) ;
564
567
println ! ( "{}\n quitting" , err) ;
565
568
app_control
@@ -614,13 +617,12 @@ fn send_loop(
614
617
return ;
615
618
}
616
619
} ;
617
- match msg {
618
- OwnedMessage :: Close ( _) => {
619
- let _ = sender. send_message ( & msg) ;
620
- return ;
621
- }
622
- _ => ( ) ,
620
+
621
+ if let OwnedMessage :: Close ( _) = msg {
622
+ let _ = sender. send_message ( & msg) ;
623
+ return ;
623
624
}
625
+
624
626
match sender. send_message ( & msg) {
625
627
Ok ( ( ) ) => ( ) ,
626
628
Err ( err) => println ! ( "Error sending {:?}" , err) ,
@@ -683,7 +685,7 @@ fn handle_application_msg(
683
685
let msg = struc. get_value ( "body" ) . unwrap ( ) ;
684
686
app_control. on_message ( msg. get ( ) . unwrap ( ) )
685
687
}
686
- "ws-error" => Err ( WsError ( app_control. 0 . lock ( ) . unwrap ( ) . app_state . clone ( ) ) ) ?,
688
+ "ws-error" => Err ( WsError ( app_control. 0 . lock ( ) . unwrap ( ) . app_state ) ) ?,
687
689
"error" => {
688
690
let msg: String = struc. get_value ( "body" ) . unwrap ( ) . get ( ) . unwrap ( ) ;
689
691
Err ( BusError ( msg) ) ?
@@ -697,12 +699,9 @@ fn handle_application_msg(
697
699
698
700
fn main ( ) {
699
701
gst:: init ( ) . unwrap ( ) ;
700
- match check_plugins ( ) {
701
- Err ( err) => {
702
- println ! ( "{:?}" , err) ;
703
- return ;
704
- }
705
- _ => { }
702
+ if let Err ( err) = check_plugins ( ) {
703
+ println ! ( "{:?}" , err) ;
704
+ return ;
706
705
}
707
706
708
707
let ( server, peer_id) = parse_args ( ) ;
@@ -733,8 +732,8 @@ fn main() {
733
732
734
733
let app_control = AppControl ( Arc :: new ( Mutex :: new ( AppControlInner {
735
734
webrtc : None ,
736
- pipeline : pipeline ,
737
- send_msg_tx : send_msg_tx ,
735
+ pipeline,
736
+ send_msg_tx,
738
737
bus : bus. clone ( ) ,
739
738
main_loop : main_loop. clone ( ) ,
740
739
peer_id : peer_id. to_string ( ) ,
@@ -746,16 +745,15 @@ fn main() {
746
745
let mut app_control = app_control. clone ( ) ;
747
746
use gst:: message:: MessageView ;
748
747
match msg. view ( ) {
749
- MessageView :: Error ( err) => app_control. close_and_quit ( Error :: from ( err. get_error ( ) ) ) ,
748
+ MessageView :: Error ( err) => app_control. close_and_quit ( & Error :: from ( err. get_error ( ) ) ) ,
750
749
MessageView :: Warning ( warning) => {
751
750
println ! ( "Warning: \" {}\" " , warning. get_debug( ) . unwrap( ) ) ;
752
751
}
753
752
MessageView :: Application ( a) => {
754
753
let struc = a. get_structure ( ) . unwrap ( ) ;
755
- match handle_application_msg ( & mut app_control, struc) {
756
- Err ( err) => app_control. close_and_quit ( err) ,
757
- _ => { }
758
- } ;
754
+ if let Err ( err) = handle_application_msg ( & mut app_control, struc) {
755
+ app_control. close_and_quit ( & err)
756
+ }
759
757
}
760
758
_ => { }
761
759
} ;
0 commit comments