-
Notifications
You must be signed in to change notification settings - Fork 30
/
attribute_type.rs
1448 lines (1315 loc) · 51.4 KB
/
attribute_type.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
use std::net::IpAddr;
use std::str::FromStr;
use std::fmt;
use SdpType;
use error::SdpParserInternalError;
use network::{parse_nettype, parse_addrtype, parse_unicast_addr};
#[derive(Clone)]
pub enum SdpAttributeCandidateTransport {
Udp,
Tcp,
}
#[derive(Clone)]
pub enum SdpAttributeCandidateType {
Host,
Srflx,
Prflx,
Relay,
}
#[derive(Clone)]
pub enum SdpAttributeCandidateTcpType {
Active,
Passive,
Simultaneous,
}
#[derive(Clone)]
pub struct SdpAttributeCandidate {
pub foundation: String,
pub component: u32,
pub transport: SdpAttributeCandidateTransport,
pub priority: u64,
pub address: IpAddr,
pub port: u32,
pub c_type: SdpAttributeCandidateType,
pub raddr: Option<IpAddr>,
pub rport: Option<u32>,
pub tcp_type: Option<SdpAttributeCandidateTcpType>,
pub generation: Option<u32>,
pub ufrag: Option<String>,
pub networkcost: Option<u32>,
}
impl SdpAttributeCandidate {
pub fn new(foundation: String,
component: u32,
transport: SdpAttributeCandidateTransport,
priority: u64,
address: IpAddr,
port: u32,
c_type: SdpAttributeCandidateType)
-> SdpAttributeCandidate {
SdpAttributeCandidate {
foundation,
component,
transport,
priority,
address,
port,
c_type,
raddr: None,
rport: None,
tcp_type: None,
generation: None,
ufrag: None,
networkcost: None,
}
}
fn set_remote_address(&mut self, ip: IpAddr) {
self.raddr = Some(ip)
}
fn set_remote_port(&mut self, p: u32) {
self.rport = Some(p)
}
fn set_tcp_type(&mut self, t: SdpAttributeCandidateTcpType) {
self.tcp_type = Some(t)
}
fn set_generation(&mut self, g: u32) {
self.generation = Some(g)
}
fn set_ufrag(&mut self, u: String) {
self.ufrag = Some(u)
}
fn set_network_cost(&mut self, n: u32) {
self.networkcost = Some(n)
}
}
#[derive(Clone)]
pub struct SdpAttributeRemoteCandidate {
pub component: u32,
pub address: IpAddr,
pub port: u32,
}
#[derive(Clone)]
pub struct SdpAttributeSimulcastId {
pub id: String,
pub paused: bool,
}
impl SdpAttributeSimulcastId {
pub fn new(idstr: String) -> SdpAttributeSimulcastId {
if idstr.starts_with('~') {
SdpAttributeSimulcastId {
id: idstr[1..].to_string(),
paused: true,
}
} else {
SdpAttributeSimulcastId {
id: idstr,
paused: false,
}
}
}
}
#[derive(Clone)]
pub struct SdpAttributeSimulcastAlternatives {
pub ids: Vec<SdpAttributeSimulcastId>,
}
impl SdpAttributeSimulcastAlternatives {
pub fn new(idlist: String) -> SdpAttributeSimulcastAlternatives {
SdpAttributeSimulcastAlternatives {
ids: idlist
.split(',')
.map(|x| x.to_string())
.map(SdpAttributeSimulcastId::new)
.collect(),
}
}
}
#[derive(Clone)]
pub struct SdpAttributeSimulcast {
pub send: Vec<SdpAttributeSimulcastAlternatives>,
pub receive: Vec<SdpAttributeSimulcastAlternatives>,
}
impl SdpAttributeSimulcast {
fn parse_ids(&mut self, direction: SdpAttributeDirection, idlist: String) {
let list = idlist
.split(';')
.map(|x| x.to_string())
.map(SdpAttributeSimulcastAlternatives::new)
.collect();
// TODO prevent over-writing existing values
match direction {
SdpAttributeDirection::Recvonly => self.receive = list,
SdpAttributeDirection::Sendonly => self.send = list,
_ => (),
}
}
}
#[derive(Clone)]
pub struct SdpAttributeRtcp {
pub port: u32,
pub unicast_addr: Option<IpAddr>,
}
impl SdpAttributeRtcp {
pub fn new(port: u32) -> SdpAttributeRtcp {
SdpAttributeRtcp {
port,
unicast_addr: None,
}
}
fn set_addr(&mut self, addr: IpAddr) {
self.unicast_addr = Some(addr)
}
}
#[derive(Clone)]
pub struct SdpAttributeRtcpFb {
pub payload_type: u32,
// TODO parse this and use an enum instead?
pub feedback_type: String,
}
#[derive(Clone)]
pub enum SdpAttributeDirection {
Recvonly,
Sendonly,
Sendrecv,
}
#[derive(Clone)]
pub struct SdpAttributeExtmap {
pub id: u32,
pub direction: Option<SdpAttributeDirection>,
pub url: String,
pub extension_attributes: Option<String>
}
#[derive(Clone)]
pub struct SdpAttributeFmtp {
pub payload_type: u32,
pub tokens: Vec<String>,
}
#[derive(Clone)]
pub struct SdpAttributeFingerprint {
// TODO turn the supported hash algorithms into an enum?
pub hash_algorithm: String,
pub fingerprint: String,
}
#[derive(Clone)]
pub struct SdpAttributeSctpmap {
pub port: u32,
pub channels: u32,
}
#[derive(Clone)]
pub enum SdpAttributeGroupSemantic {
LipSynchronization,
FlowIdentification,
SingleReservationFlow,
AlternateNetworkAddressType,
ForwardErrorCorrection,
DecodingDependency,
Bundle,
}
#[derive(Clone)]
pub struct SdpAttributeGroup {
pub semantics: SdpAttributeGroupSemantic,
pub tags: Vec<String>,
}
#[derive(Clone)]
pub struct SdpAttributeMsid {
pub id: String,
pub appdata: Option<String>,
}
#[derive(Clone, Debug)]
pub struct SdpAttributeMsidSemantic {
pub semantic: String,
pub msids: Vec<String>,
}
#[derive(Clone)]
pub struct SdpAttributeRtpmap {
pub payload_type: u32,
pub codec_name: String,
pub frequency: Option<u32>,
pub channels: Option<u32>,
}
impl SdpAttributeRtpmap {
pub fn new(payload_type: u32, codec_name: String) -> SdpAttributeRtpmap {
SdpAttributeRtpmap {
payload_type,
codec_name,
frequency: None,
channels: None,
}
}
fn set_frequency(&mut self, f: u32) {
self.frequency = Some(f)
}
fn set_channels(&mut self, c: u32) {
self.channels = Some(c)
}
}
#[derive(Clone)]
pub enum SdpAttributeSetup {
Active,
Actpass,
Holdconn,
Passive,
}
#[derive(Clone)]
pub struct SdpAttributeSsrc {
pub id: u32,
pub attribute: Option<String>,
pub value: Option<String>,
}
impl SdpAttributeSsrc {
pub fn new(id: u32) -> SdpAttributeSsrc {
SdpAttributeSsrc {
id,
attribute: None,
value: None,
}
}
fn set_attribute(&mut self, a: &str) {
if a.find(':') == None {
self.attribute = Some(a.to_string());
} else {
let v: Vec<&str> = a.splitn(2, ':').collect();
self.attribute = Some(v[0].to_string());
self.value = Some(v[1].to_string());
}
}
}
#[derive(Clone)]
pub enum SdpAttribute {
BundleOnly,
Candidate(SdpAttributeCandidate),
EndOfCandidates,
Extmap(SdpAttributeExtmap),
Fingerprint(SdpAttributeFingerprint),
Fmtp(SdpAttributeFmtp),
Group(SdpAttributeGroup),
IceLite,
IceMismatch,
IceOptions(Vec<String>),
IcePwd(String),
IceUfrag(String),
Identity(String),
ImageAttr(String),
Inactive,
Label(String),
MaxMessageSize(u64),
MaxPtime(u64),
Mid(String),
Msid(SdpAttributeMsid),
MsidSemantic(SdpAttributeMsidSemantic),
Ptime(u64),
Rid(String),
Recvonly,
RemoteCandidate(SdpAttributeRemoteCandidate),
Rtpmap(SdpAttributeRtpmap),
Rtcp(SdpAttributeRtcp),
Rtcpfb(SdpAttributeRtcpFb),
RtcpMux,
RtcpRsize,
Sctpmap(SdpAttributeSctpmap),
SctpPort(u64),
Sendonly,
Sendrecv,
Setup(SdpAttributeSetup),
Simulcast(SdpAttributeSimulcast),
Ssrc(SdpAttributeSsrc),
SsrcGroup(String),
}
impl SdpAttribute {
pub fn allowed_at_session_level(&self) -> bool {
match *self {
SdpAttribute::BundleOnly |
SdpAttribute::Candidate(..) |
SdpAttribute::Fmtp(..) |
SdpAttribute::IceMismatch |
SdpAttribute::ImageAttr(..) |
SdpAttribute::Label(..) |
SdpAttribute::MaxMessageSize(..) |
SdpAttribute::MaxPtime(..) |
SdpAttribute::Mid(..) |
SdpAttribute::Msid(..) |
SdpAttribute::Ptime(..) |
SdpAttribute::Rid(..) |
SdpAttribute::RemoteCandidate(..) |
SdpAttribute::Rtpmap(..) |
SdpAttribute::Rtcp(..) |
SdpAttribute::Rtcpfb(..) |
SdpAttribute::RtcpMux |
SdpAttribute::RtcpRsize |
SdpAttribute::Sctpmap(..) |
SdpAttribute::SctpPort(..) |
SdpAttribute::Simulcast(..) |
SdpAttribute::Ssrc(..) |
SdpAttribute::SsrcGroup(..) => false,
SdpAttribute::EndOfCandidates |
SdpAttribute::Extmap(..) |
SdpAttribute::Fingerprint(..) |
SdpAttribute::Group(..) |
SdpAttribute::IceLite |
SdpAttribute::IceOptions(..) |
SdpAttribute::IcePwd(..) |
SdpAttribute::IceUfrag(..) |
SdpAttribute::Identity(..) |
SdpAttribute::Inactive |
SdpAttribute::MsidSemantic(..) |
SdpAttribute::Recvonly |
SdpAttribute::Sendonly |
SdpAttribute::Sendrecv |
SdpAttribute::Setup(..) => true,
}
}
pub fn allowed_at_media_level(&self) -> bool {
match *self {
SdpAttribute::Group(..) |
SdpAttribute::IceLite |
SdpAttribute::Identity(..) |
SdpAttribute::MsidSemantic(..) => false,
SdpAttribute::BundleOnly |
SdpAttribute::Candidate(..) |
SdpAttribute::EndOfCandidates |
SdpAttribute::Extmap(..) |
SdpAttribute::Fingerprint(..) |
SdpAttribute::Fmtp(..) |
SdpAttribute::IceMismatch |
SdpAttribute::IceOptions(..) |
SdpAttribute::IcePwd(..) |
SdpAttribute::IceUfrag(..) |
SdpAttribute::ImageAttr(..) |
SdpAttribute::Inactive |
SdpAttribute::Label(..) |
SdpAttribute::MaxMessageSize(..) |
SdpAttribute::MaxPtime(..) |
SdpAttribute::Mid(..) |
SdpAttribute::Msid(..) |
SdpAttribute::Ptime(..) |
SdpAttribute::Rid(..) |
SdpAttribute::Recvonly |
SdpAttribute::RemoteCandidate(..) |
SdpAttribute::Rtpmap(..) |
SdpAttribute::Rtcp(..) |
SdpAttribute::Rtcpfb(..) |
SdpAttribute::RtcpMux |
SdpAttribute::RtcpRsize |
SdpAttribute::Sctpmap(..) |
SdpAttribute::SctpPort(..) |
SdpAttribute::Sendonly |
SdpAttribute::Sendrecv |
SdpAttribute::Setup(..) |
SdpAttribute::Simulcast(..) |
SdpAttribute::Ssrc(..) |
SdpAttribute::SsrcGroup(..) => true,
}
}
}
impl fmt::Display for SdpAttribute {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let printable = match *self {
SdpAttribute::BundleOnly => "bundle-only",
SdpAttribute::Candidate(..) => "candidate",
SdpAttribute::EndOfCandidates => "end-of-candidates",
SdpAttribute::Extmap(..) => "extmap",
SdpAttribute::Fingerprint(..) => "fingerprint",
SdpAttribute::Fmtp(..) => "fmtp",
SdpAttribute::Group(..) => "group",
SdpAttribute::IceLite => "ice-lite",
SdpAttribute::IceMismatch => "ice-mismatch",
SdpAttribute::IceOptions(..) => "ice-options",
SdpAttribute::IcePwd(..) => "ice-pwd",
SdpAttribute::IceUfrag(..) => "ice-ufrag",
SdpAttribute::Identity(..) => "identity",
SdpAttribute::ImageAttr(..) => "imageattr",
SdpAttribute::Inactive => "inactive",
SdpAttribute::Label(..) => "label",
SdpAttribute::MaxMessageSize(..) => "max-message-size",
SdpAttribute::MaxPtime(..) => "max-ptime",
SdpAttribute::Mid(..) => "mid",
SdpAttribute::Msid(..) => "msid",
SdpAttribute::MsidSemantic(..) => "msid-semantic",
SdpAttribute::Ptime(..) => "ptime",
SdpAttribute::Rid(..) => "rid",
SdpAttribute::Recvonly => "recvonly",
SdpAttribute::RemoteCandidate(..) => "remote-candidate",
SdpAttribute::Rtpmap(..) => "rtpmap",
SdpAttribute::Rtcp(..) => "rtcp",
SdpAttribute::Rtcpfb(..) => "rtcp-fb",
SdpAttribute::RtcpMux => "rtcp-mux",
SdpAttribute::RtcpRsize => "rtcp-rsize",
SdpAttribute::Sctpmap(..) => "sctpmap",
SdpAttribute::SctpPort(..) => "sctp-port",
SdpAttribute::Sendonly => "sendonly",
SdpAttribute::Sendrecv => "sendrecv",
SdpAttribute::Setup(..) => "setup",
SdpAttribute::Simulcast(..) => "simulcast",
SdpAttribute::Ssrc(..) => "ssrc",
SdpAttribute::SsrcGroup(..) => "ssrc-group",
};
write!(f, "attribute: {}", printable)
}
}
impl FromStr for SdpAttribute {
type Err = SdpParserInternalError;
fn from_str(line: &str) -> Result<Self, Self::Err> {
let tokens: Vec<_> = line.splitn(2, ':').collect();
let name = tokens[0].to_lowercase();
let val = match tokens.get(1) {
Some(x) => x.trim(),
None => "",
};
if tokens.len() > 1 {
match name.as_str() {
"bundle-only" |
"end-of-candidates" |
"ice-lite" |
"ice-mismatch" |
"inactive" |
"recvonly" |
"rtcp-mux" |
"rtcp-rsize" |
"sendonly" |
"sendrecv" => {
return Err(SdpParserInternalError::Generic(format!("{} attribute is not allowed to have a value",
name)));
}
_ => (),
}
}
match name.as_str() {
"bundle-only" => Ok(SdpAttribute::BundleOnly),
"end-of-candidates" => Ok(SdpAttribute::EndOfCandidates),
"ice-lite" => Ok(SdpAttribute::IceLite),
"ice-mismatch" => Ok(SdpAttribute::IceMismatch),
"ice-pwd" => Ok(SdpAttribute::IcePwd(string_or_empty(val)?)),
"ice-ufrag" => Ok(SdpAttribute::IceUfrag(string_or_empty(val)?)),
"identity" => Ok(SdpAttribute::Identity(string_or_empty(val)?)),
"imageattr" => Ok(SdpAttribute::ImageAttr(string_or_empty(val)?)),
"inactive" => Ok(SdpAttribute::Inactive),
"label" => Ok(SdpAttribute::Label(string_or_empty(val)?)),
"max-message-size" => Ok(SdpAttribute::MaxMessageSize(val.parse()?)),
"maxptime" => Ok(SdpAttribute::MaxPtime(val.parse()?)),
"mid" => Ok(SdpAttribute::Mid(string_or_empty(val)?)),
"msid-semantic" => parse_msid_semantic(val),
"ptime" => Ok(SdpAttribute::Ptime(val.parse()?)),
"rid" => Ok(SdpAttribute::Rid(string_or_empty(val)?)),
"recvonly" => Ok(SdpAttribute::Recvonly),
"rtcp-mux" => Ok(SdpAttribute::RtcpMux),
"rtcp-rsize" => Ok(SdpAttribute::RtcpRsize),
"sendonly" => Ok(SdpAttribute::Sendonly),
"sendrecv" => Ok(SdpAttribute::Sendrecv),
"ssrc-group" => Ok(SdpAttribute::SsrcGroup(string_or_empty(val)?)),
"sctp-port" => parse_sctp_port(val),
"candidate" => parse_candidate(val),
"extmap" => parse_extmap(val),
"fingerprint" => parse_fingerprint(val),
"fmtp" => parse_fmtp(val),
"group" => parse_group(val),
"ice-options" => parse_ice_options(val),
"msid" => parse_msid(val),
"remote-candidates" => parse_remote_candidates(val),
"rtpmap" => parse_rtpmap(val),
"rtcp" => parse_rtcp(val),
"rtcp-fb" => parse_rtcp_fb(val),
"sctpmap" => parse_sctpmap(val),
"setup" => parse_setup(val),
"simulcast" => parse_simulcast(val),
"ssrc" => parse_ssrc(val),
_ => {
Err(SdpParserInternalError::Unsupported(format!("Unknown attribute type {}", name)))
}
}
}
}
fn string_or_empty(to_parse: &str) -> Result<String, SdpParserInternalError> {
if to_parse.is_empty() {
Err(SdpParserInternalError::Generic("This attribute is required to have a value"
.to_string()))
} else {
Ok(to_parse.to_string())
}
}
fn parse_sctp_port(to_parse: &str) -> Result<SdpAttribute, SdpParserInternalError> {
let port = to_parse.parse()?;
if port > 65535 {
return Err(SdpParserInternalError::Generic(format!("Sctpport port {} can only be a bit 16bit number",
port)));
}
Ok(SdpAttribute::SctpPort(port))
}
fn parse_candidate(to_parse: &str) -> Result<SdpAttribute, SdpParserInternalError> {
let tokens: Vec<&str> = to_parse.split_whitespace().collect();
if tokens.len() < 8 {
return Err(SdpParserInternalError::Generic("Candidate needs to have minimum eigth tokens"
.to_string()));
}
let component = tokens[1].parse::<u32>()?;
let transport = match tokens[2].to_lowercase().as_ref() {
"udp" => SdpAttributeCandidateTransport::Udp,
"tcp" => SdpAttributeCandidateTransport::Tcp,
_ => {
return Err(SdpParserInternalError::Generic("Unknonw candidate transport value"
.to_string()))
}
};
let priority = tokens[3].parse::<u64>()?;
let address = parse_unicast_addr(tokens[4])?;
let port = tokens[5].parse::<u32>()?;
if port > 65535 {
return Err(SdpParserInternalError::Generic("ICE candidate port can only be a bit 16bit number".to_string()));
}
match tokens[6].to_lowercase().as_ref() {
"typ" => (),
_ => {
return Err(SdpParserInternalError::Generic("Candidate attribute token must be 'typ'"
.to_string()))
}
};
let cand_type = match tokens[7].to_lowercase().as_ref() {
"host" => SdpAttributeCandidateType::Host,
"srflx" => SdpAttributeCandidateType::Srflx,
"prflx" => SdpAttributeCandidateType::Prflx,
"relay" => SdpAttributeCandidateType::Relay,
_ => return Err(SdpParserInternalError::Generic("Unknow candidate type value".to_string())),
};
let mut cand = SdpAttributeCandidate::new(tokens[0].to_string(),
component,
transport,
priority,
address,
port,
cand_type);
if tokens.len() > 8 {
let mut index = 8;
while tokens.len() > index + 1 {
match tokens[index].to_lowercase().as_ref() {
"generation" => {
let generation = tokens[index + 1].parse::<u32>()?;
cand.set_generation(generation);
index += 2;
}
"network-cost" => {
let cost = tokens[index + 1].parse::<u32>()?;
cand.set_network_cost(cost);
index += 2;
}
"raddr" => {
let addr = parse_unicast_addr(tokens[index + 1])?;
cand.set_remote_address(addr);
index += 2;
}
"rport" => {
let port = tokens[index + 1].parse::<u32>()?;
if port > 65535 {
return Err(SdpParserInternalError::Generic( "ICE candidate rport can only be a bit 16bit number".to_string()));
}
cand.set_remote_port(port);
index += 2;
}
"tcptype" => {
cand.set_tcp_type(match tokens[index + 1].to_lowercase().as_ref() {
"active" => SdpAttributeCandidateTcpType::Active,
"passive" => SdpAttributeCandidateTcpType::Passive,
"so" => SdpAttributeCandidateTcpType::Simultaneous,
_ => {
return Err(SdpParserInternalError::Generic("Unknown tcptype value in candidate line".to_string()))
}
});
index += 2;
}
"ufrag" => {
let ufrag = tokens[index + 1];
cand.set_ufrag(ufrag.to_string());
index += 2;
}
_ => {
return Err(SdpParserInternalError::Unsupported("Uknown candidate extension name"
.to_string()))
}
};
}
}
Ok(SdpAttribute::Candidate(cand))
}
// ABNF for extmap is defined in RFC 5285
// https://tools.ietf.org/html/rfc5285#section-7
fn parse_extmap(to_parse: &str) -> Result<SdpAttribute, SdpParserInternalError> {
let tokens: Vec<&str> = to_parse.split_whitespace().collect();
if tokens.len() < 2 {
return Err(SdpParserInternalError::Generic("Extmap needs to have at least two tokens".to_string()));
}
let id: u32;
let mut direction: Option<SdpAttributeDirection> = None;
if tokens[0].find('/') == None {
id = tokens[0].parse::<u32>()?;
} else {
let id_dir: Vec<&str> = tokens[0].splitn(2, '/').collect();
id = id_dir[0].parse::<u32>()?;
direction = Some(match id_dir[1].to_lowercase().as_ref() {
"recvonly" => SdpAttributeDirection::Recvonly,
"sendonly" => SdpAttributeDirection::Sendonly,
"sendrecv" => SdpAttributeDirection::Sendrecv,
_ => {
return Err(SdpParserInternalError::Generic("Unsupported direction in extmap value".to_string()))
}
})
}
// Consider replaceing to_parse.split_whitespace() above with splitn on space. Would we want the pattern to split on any amout of any kind of whitespace?
let extension_attributes = if tokens.len() == 2 {
None
} else {
let ext_string: String = tokens[2..].iter().map(|x| *x).collect();
if !valid_byte_string(&ext_string) {
return Err(SdpParserInternalError::Generic("Illegal character in extmap extension attributes".to_string()))
}
Some(ext_string)
};
Ok(SdpAttribute::Extmap(SdpAttributeExtmap {
id,
direction,
url: tokens[1].to_string(),
extension_attributes: extension_attributes
}))
}
fn parse_fingerprint(to_parse: &str) -> Result<SdpAttribute, SdpParserInternalError> {
let tokens: Vec<&str> = to_parse.split_whitespace().collect();
if tokens.len() != 2 {
return Err(SdpParserInternalError::Generic("Fingerprint needs to have two tokens"
.to_string()));
}
Ok(SdpAttribute::Fingerprint(SdpAttributeFingerprint {
hash_algorithm: tokens[0].to_string(),
fingerprint: tokens[1].to_string(),
}))
}
fn parse_fmtp(to_parse: &str) -> Result<SdpAttribute, SdpParserInternalError> {
let tokens: Vec<&str> = to_parse.split_whitespace().collect();
if tokens.len() != 2 {
return Err(SdpParserInternalError::Generic("Fmtp needs to have two tokens".to_string()));
}
Ok(SdpAttribute::Fmtp(SdpAttributeFmtp {
// TODO check for dynamic PT range
payload_type: tokens[0].parse::<u32>()?,
// TODO this should probably be slit into known tokens
// plus a list of unknown tokens
tokens: to_parse.split(';').map(|x| x.to_string()).collect(),
}))
}
fn parse_group(to_parse: &str) -> Result<SdpAttribute, SdpParserInternalError> {
let mut tokens = to_parse.split_whitespace();
let semantics = match tokens.next() {
None => {
return Err(SdpParserInternalError::Generic("Group attribute is missing semantics token"
.to_string()))
}
Some(x) => {
match x.to_uppercase().as_ref() {
"LS" => SdpAttributeGroupSemantic::LipSynchronization,
"FID" => SdpAttributeGroupSemantic::FlowIdentification,
"SRF" => SdpAttributeGroupSemantic::SingleReservationFlow,
"ANAT" => SdpAttributeGroupSemantic::AlternateNetworkAddressType,
"FEC" => SdpAttributeGroupSemantic::ForwardErrorCorrection,
"DDP" => SdpAttributeGroupSemantic::DecodingDependency,
"BUNDLE" => SdpAttributeGroupSemantic::Bundle,
_ => {
return Err(SdpParserInternalError::Generic("Unsupported group semantics"
.to_string()))
}
}
}
};
Ok(SdpAttribute::Group(SdpAttributeGroup {
semantics,
tags: tokens.map(|x| x.to_string()).collect(),
}))
}
fn parse_ice_options(to_parse: &str) -> Result<SdpAttribute, SdpParserInternalError> {
if to_parse.is_empty() {
return Err(SdpParserInternalError::Generic("ice-options is required to have a value"
.to_string()));
}
Ok(SdpAttribute::IceOptions(to_parse.split_whitespace().map(|x| x.to_string()).collect()))
}
fn parse_msid(to_parse: &str) -> Result<SdpAttribute, SdpParserInternalError> {
let mut tokens = to_parse.split_whitespace();
let id = match tokens.next() {
None => {
return Err(SdpParserInternalError::Generic("Msid attribute is missing msid-id token"
.to_string()))
}
Some(x) => x.to_string(),
};
let appdata = match tokens.next() {
None => None,
Some(x) => Some(x.to_string()),
};
Ok(SdpAttribute::Msid(SdpAttributeMsid { id, appdata }))
}
fn parse_msid_semantic(to_parse: &str) -> Result<SdpAttribute, SdpParserInternalError> {
let tokens: Vec<_> = to_parse.split_whitespace().collect();
if tokens.len() < 1 {
return Err(SdpParserInternalError::Generic("Msid-semantic attribute is missing msid-semantic token"
.to_string()))
}
// TODO: Should msids be checked to ensure they are non empty?
let semantic = SdpAttributeMsidSemantic {
semantic: tokens[0].to_string(),
msids: tokens[1..].iter().map(|x| x.to_string()).collect()
};
Ok(SdpAttribute::MsidSemantic(semantic))
}
fn parse_remote_candidates(to_parse: &str) -> Result<SdpAttribute, SdpParserInternalError> {
let mut tokens = to_parse.split_whitespace();
let component = match tokens.next() {
None => {
return Err(SdpParserInternalError::Generic(
"Remote-candidate attribute is missing component ID"
.to_string(),
))
}
Some(x) => x.parse::<u32>()?,
};
let address = match tokens.next() {
None => {
return Err(SdpParserInternalError::Generic(
"Remote-candidate attribute is missing connection address"
.to_string(),
))
}
Some(x) => parse_unicast_addr(x)?,
};
let port = match tokens.next() {
None => {
return Err(SdpParserInternalError::Generic(
"Remote-candidate attribute is missing port number".to_string(),
))
}
Some(x) => x.parse::<u32>()?,
};
if port > 65535 {
return Err(SdpParserInternalError::Generic(
"Remote-candidate port can only be a bit 16bit number".to_string(),
));
};
Ok(SdpAttribute::RemoteCandidate(SdpAttributeRemoteCandidate {
component,
address,
port,
}))
}
fn parse_rtpmap(to_parse: &str) -> Result<SdpAttribute, SdpParserInternalError> {
let tokens: Vec<&str> = to_parse.split_whitespace().collect();
if tokens.len() != 2 {
return Err(SdpParserInternalError::Generic("Rtpmap needs to have two tokens".to_string()));
}
// TODO limit this to dymaic PTs
let payload_type: u32 = tokens[0].parse::<u32>()?;
let split: Vec<&str> = tokens[1].split('/').collect();
if split.len() > 3 {
return Err(SdpParserInternalError::Generic("Rtpmap codec token can max 3 subtokens"
.to_string()));
}
let mut rtpmap = SdpAttributeRtpmap::new(payload_type, split[0].to_string());
if split.len() > 1 {
rtpmap.set_frequency(split[1].parse::<u32>()?);
}
if split.len() > 2 {
rtpmap.set_channels(split[2].parse::<u32>()?);
}
Ok(SdpAttribute::Rtpmap(rtpmap))
}
fn parse_rtcp(to_parse: &str) -> Result<SdpAttribute, SdpParserInternalError> {
let mut tokens = to_parse.split_whitespace();
let port = match tokens.next() {
None => {
return Err(SdpParserInternalError::Generic("Rtcp attribute is missing port number"
.to_string()))
}
Some(x) => x.parse::<u32>()?,
};
if port > 65535 {
return Err(SdpParserInternalError::Generic("Rtcp port can only be a bit 16bit number"
.to_string()));
};
let mut rtcp = SdpAttributeRtcp::new(port);
match tokens.next() {
None => (),
Some(x) => {
parse_nettype(x)?;
match tokens.next() {
None => {
return Err(SdpParserInternalError::Generic(
"Rtcp attribute is missing address type token"
.to_string(),
))
}
Some(x) => {
let addrtype = parse_addrtype(x)?;
let addr = match tokens.next() {
None => {
return Err(SdpParserInternalError::Generic(
"Rtcp attribute is missing ip address token"
.to_string(),
))
}
Some(x) => {
let addr = parse_unicast_addr(x)?;
if !addrtype.same_protocol(&addr) {
return Err(SdpParserInternalError::Generic(
"Failed to parse unicast address attribute.\
addrtype does not match address."
.to_string(),
));
}
addr
}
};
rtcp.set_addr(addr);
}
};
}
};
Ok(SdpAttribute::Rtcp(rtcp))
}
fn parse_rtcp_fb(to_parse: &str) -> Result<SdpAttribute, SdpParserInternalError> {
let tokens: Vec<&str> = to_parse.splitn(2, ' ').collect();
Ok(SdpAttribute::Rtcpfb(SdpAttributeRtcpFb {
// TODO limit this to dymaic PTs
payload_type: tokens[0].parse::<u32>()?,
feedback_type: match tokens.get(1) {
Some(x) => x.to_string(),
None => {
return Err(SdpParserInternalError::Generic(
"Error parsing rtcpfb".to_string(),
))
}
},
}))
}
fn parse_sctpmap(to_parse: &str) -> Result<SdpAttribute, SdpParserInternalError> {
let tokens: Vec<&str> = to_parse.split_whitespace().collect();
if tokens.len() != 3 {
return Err(SdpParserInternalError::Generic("Sctpmap needs to have three tokens"
.to_string()));
}
let port = tokens[0].parse::<u32>()?;
if port > 65535 {
return Err(SdpParserInternalError::Generic("Sctpmap port can only be a bit 16bit number"
.to_string()));
}
if tokens[1].to_lowercase() != "webrtc-datachannel" {
return Err(SdpParserInternalError::Generic("Unsupported sctpmap type token".to_string()));
}
Ok(SdpAttribute::Sctpmap(SdpAttributeSctpmap {
port,
channels: tokens[2].parse::<u32>()?,
}))
}
fn parse_setup(to_parse: &str) -> Result<SdpAttribute, SdpParserInternalError> {
Ok(SdpAttribute::Setup(match to_parse.to_lowercase().as_ref() {
"active" => SdpAttributeSetup::Active,
"actpass" => SdpAttributeSetup::Actpass,
"holdconn" => SdpAttributeSetup::Holdconn,
"passive" => SdpAttributeSetup::Passive,
_ => {
return Err(SdpParserInternalError::Generic(
"Unsupported setup value".to_string(),
))
}
}))
}
fn parse_simulcast(to_parse: &str) -> Result<SdpAttribute, SdpParserInternalError> {
let mut tokens = to_parse.split_whitespace();
let mut token = match tokens.next() {
None => {
return Err(SdpParserInternalError::Generic(
"Simulcast attribute is missing send/recv value".to_string(),
))
}
Some(x) => x,
};
let mut sc = SdpAttributeSimulcast {
send: Vec::new(),
receive: Vec::new(),
};
loop {
let sendrecv = match token.to_lowercase().as_ref() {
"send" => SdpAttributeDirection::Sendonly,
"recv" => SdpAttributeDirection::Recvonly,
_ => {