Skip to content

Commit 43f8e8b

Browse files
authored
Merge branch 'master' into unmarshal-optimization
2 parents 9fdf053 + 71b2875 commit 43f8e8b

File tree

19 files changed

+125
-67
lines changed

19 files changed

+125
-67
lines changed

examples/examples/signal/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ edition = "2021"
88
[dependencies]
99
tokio = { version = "1.15.0", features = ["full"] }
1010
anyhow = "1.0.52"
11-
base64 = "0.13.0"
11+
base64 = "0.21.2"
1212
lazy_static = "1.4"
1313
hyper = { version = "0.14.16", features = ["full"] }

examples/examples/signal/src/lib.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
#![allow(dead_code)]
33

44
use anyhow::Result;
5+
use base64::prelude::BASE64_STANDARD;
6+
use base64::Engine;
57
use hyper::service::{make_service_fn, service_fn};
68
use hyper::{Body, Method, Request, Response, Server, StatusCode};
79
use std::net::SocketAddr;
@@ -92,13 +94,13 @@ pub fn encode(b: &str) -> String {
9294
// b = zip(b)
9395
//}
9496

95-
base64::encode(b)
97+
BASE64_STANDARD.encode(b)
9698
}
9799

98100
/// decode decodes the input from base64
99101
/// It can optionally unzip the input after decoding
100102
pub fn decode(s: &str) -> Result<String> {
101-
let b = base64::decode(s)?;
103+
let b = BASE64_STANDARD.decode(s)?;
102104

103105
//if COMPRESS {
104106
// b = unzip(b)

stun/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ tokio = { version = "1.19", features = ["full"] }
2121
lazy_static = "1.4"
2222
url = "2.2"
2323
rand = "0.8.5"
24-
base64 = "0.13.0"
24+
base64 = "0.21.2"
2525
subtle = "2.4"
2626
crc = "3.0"
2727
ring = "0.16.20"

stun/benches/bench.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use base64::prelude::BASE64_STANDARD;
2+
use base64::Engine;
13
use criterion::{criterion_group, criterion_main, Criterion};
24
use rand::rngs::StdRng;
35
use rand::{Rng, SeedableRng};
@@ -623,7 +625,7 @@ fn benchmark_xoraddr(c: &mut Criterion) {
623625

624626
{
625627
let mut m = Message::new();
626-
let transaction_id = base64::decode("jxhBARZwX+rsC6er").unwrap();
628+
let transaction_id = BASE64_STANDARD.decode("jxhBARZwX+rsC6er").unwrap();
627629

628630
m.transaction_id.0.copy_from_slice(&transaction_id);
629631
let addr_value = [0, 1, 156, 213, 244, 159, 56, 174]; //hex.DecodeString("00019cd5f49f38ae")

stun/examples/stun_decode.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use base64::prelude::BASE64_STANDARD;
2+
use base64::Engine;
13
use clap::{App, Arg};
24

35
use stun::message::Message;
@@ -28,7 +30,7 @@ fn main() {
2830
}
2931

3032
let encoded_data = matches.value_of("data").unwrap();
31-
let decoded_data = match base64::decode(encoded_data) {
33+
let decoded_data = match BASE64_STANDARD.decode(encoded_data) {
3234
Ok(d) => d,
3335
Err(e) => panic!("Unable to decode base64 value: {e}"),
3436
};

stun/src/message.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ use crate::agent::*;
55
use crate::attributes::*;
66
use crate::error::*;
77

8+
use base64::prelude::BASE64_STANDARD;
9+
use base64::Engine;
810
use rand::Rng;
911
use std::fmt;
1012
use std::io::{Read, Write};
@@ -64,7 +66,7 @@ pub struct Message {
6466

6567
impl fmt::Display for Message {
6668
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
67-
let t_id = base64::encode(self.transaction_id.0);
69+
let t_id = BASE64_STANDARD.encode(self.transaction_id.0);
6870
write!(
6971
f,
7072
"{} l={} attrs={} id={}",

stun/src/xoraddr/xoraddr_test.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use super::*;
22
use crate::checks::*;
33

4+
use base64::prelude::BASE64_STANDARD;
5+
use base64::Engine;
46
use std::io::BufReader;
57

68
#[test]
@@ -36,7 +38,7 @@ fn test_xor_safe_bsmaller() -> Result<()> {
3638
#[test]
3739
fn test_xormapped_address_get_from() -> Result<()> {
3840
let mut m = Message::new();
39-
let transaction_id = base64::decode("jxhBARZwX+rsC6er").unwrap();
41+
let transaction_id = BASE64_STANDARD.decode("jxhBARZwX+rsC6er").unwrap();
4042
m.transaction_id.0.copy_from_slice(&transaction_id);
4143
let addr_value = vec![0x00, 0x01, 0x9c, 0xd5, 0xf4, 0x9f, 0x38, 0xae];
4244
m.add(ATTR_XORMAPPED_ADDRESS, &addr_value);
@@ -104,7 +106,7 @@ fn test_xormapped_address_get_from() -> Result<()> {
104106
#[test]
105107
fn test_xormapped_address_get_from_invalid() -> Result<()> {
106108
let mut m = Message::new();
107-
let transaction_id = base64::decode("jxhBARZwX+rsC6er").unwrap();
109+
let transaction_id = BASE64_STANDARD.decode("jxhBARZwX+rsC6er").unwrap();
108110
m.transaction_id.0.copy_from_slice(&transaction_id);
109111
let expected_ip: IpAddr = "213.141.156.236".parse().unwrap();
110112
let expected_port = 21254u16;
@@ -134,7 +136,7 @@ fn test_xormapped_address_get_from_invalid() -> Result<()> {
134136
#[test]
135137
fn test_xormapped_address_add_to() -> Result<()> {
136138
let mut m = Message::new();
137-
let transaction_id = base64::decode("jxhBARZwX+rsC6er").unwrap();
139+
let transaction_id = BASE64_STANDARD.decode("jxhBARZwX+rsC6er").unwrap();
138140
m.transaction_id.0.copy_from_slice(&transaction_id);
139141
let expected_ip: IpAddr = "213.141.156.236".parse().unwrap();
140142
let expected_port = 21254u16;
@@ -166,7 +168,7 @@ fn test_xormapped_address_add_to() -> Result<()> {
166168
#[test]
167169
fn test_xormapped_address_add_to_ipv6() -> Result<()> {
168170
let mut m = Message::new();
169-
let transaction_id = base64::decode("jxhBARZwX+rsC6er").unwrap();
171+
let transaction_id = BASE64_STANDARD.decode("jxhBARZwX+rsC6er").unwrap();
170172
m.transaction_id.0.copy_from_slice(&transaction_id);
171173
let expected_ip: IpAddr = "fe80::dc2b:44ff:fe20:6009".parse().unwrap();
172174
let expected_port = 21254u16;

turn/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ stun = { version = "0.4.3", path = "../stun" }
2020
tokio = { version = "1.19", features = ["full"] }
2121
async-trait = "0.1.56"
2222
log = "0.4.16"
23-
base64 = "0.13.0"
23+
base64 = "0.21.2"
2424
rand = "0.8.5"
2525
ring = "0.16.20"
2626
md-5 = "0.10.1"

turn/src/auth/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ use crate::error::*;
66
use std::net::SocketAddr;
77
use std::time::{Duration, SystemTime, UNIX_EPOCH};
88

9+
use base64::prelude::BASE64_STANDARD;
10+
use base64::Engine;
911
use md5::{Digest, Md5};
1012
use ring::hmac;
1113

@@ -30,7 +32,7 @@ fn long_term_credentials(username: &str, shared_secret: &str) -> String {
3032
shared_secret.as_bytes(),
3133
);
3234
let password = hmac::sign(&mac, username.as_bytes()).as_ref().to_vec();
33-
base64::encode(password)
35+
BASE64_STANDARD.encode(password)
3436
}
3537

3638
// generate_auth_key is a convenience function to easily generate keys in the format used by AuthHandler

turn/src/client/mod.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ use crate::error::*;
1111
use crate::proto::{
1212
chandata::*, data::*, lifetime::*, peeraddr::*, relayaddr::*, reqtrans::*, PROTO_UDP,
1313
};
14+
use base64::prelude::BASE64_STANDARD;
15+
use base64::Engine;
1416
use binding::*;
1517
use relay_conn::*;
1618
use transaction::*;
@@ -103,7 +105,7 @@ impl RelayConnObserver for ClientInternal {
103105
to: &str,
104106
ignore_result: bool,
105107
) -> Result<TransactionResult> {
106-
let tr_key = base64::encode(msg.transaction_id.0);
108+
let tr_key = BASE64_STANDARD.encode(msg.transaction_id.0);
107109

108110
let mut tr = Transaction::new(TransactionConfig {
109111
key: tr_key.clone(),
@@ -341,7 +343,7 @@ impl ClientInternal {
341343
// - stun.ClassSuccessResponse
342344
// - stun.ClassErrorResponse
343345

344-
let tr_key = base64::encode(msg.transaction_id.0);
346+
let tr_key = BASE64_STANDARD.encode(msg.transaction_id.0);
345347

346348
let mut tm = tr_map.lock().await;
347349
if tm.find(&tr_key).is_none() {

webrtc/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ bytes = "1"
4040
thiserror = "1.0"
4141
waitgroup = "0.1.2"
4242
regex = "1.7.1"
43+
smol_str = "0.2.0"
4344
url = "2.2"
4445
rustls = { version = "0.19.0", features = ["dangerous_configuration"]}
4546
rcgen = { version = "0.10.0", features = ["pem", "x509-parser"]}

webrtc/src/lib.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ pub use rtcp;
1212
pub use rtp;
1313
pub use sctp;
1414
pub use sdp;
15+
use serde::{Deserialize, Serialize};
16+
use smol_str::SmolStr;
1517
pub use srtp;
1618
pub use stun;
1719
pub use turn;
@@ -43,3 +45,25 @@ pub(crate) const SDP_ATTRIBUTE_RID: &str = "rid";
4345
pub(crate) const GENERATED_CERTIFICATE_ORIGIN: &str = "WebRTC";
4446
pub(crate) const SDES_REPAIR_RTP_STREAM_ID_URI: &str =
4547
"urn:ietf:params:rtp-hdrext:sdes:repaired-rtp-stream-id";
48+
49+
#[derive(Clone, Debug, Default, PartialEq)]
50+
pub struct SmallStr(SmolStr);
51+
52+
impl Serialize for SmallStr {
53+
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
54+
where
55+
S: serde::Serializer,
56+
{
57+
serializer.serialize_str(self.0.as_str())
58+
}
59+
}
60+
61+
impl<'de> Deserialize<'de> for SmallStr {
62+
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
63+
where
64+
D: serde::Deserializer<'de>,
65+
{
66+
let s = String::deserialize(deserializer)?;
67+
Ok(SmallStr(SmolStr::new(s)))
68+
}
69+
}

webrtc/src/peer_connection/mod.rs

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ use crate::sctp_transport::RTCSctpTransport;
6666
use crate::stats::StatsReport;
6767
use crate::track::track_local::TrackLocal;
6868
use crate::track::track_remote::TrackRemote;
69+
use crate::SmallStr;
6970

7071
use ::ice::candidate::candidate_base::unmarshal_candidate;
7172
use ::ice::candidate::Candidate;
@@ -77,6 +78,7 @@ use interceptor::{stats, Attributes, Interceptor, RTCPWriter};
7778
use peer_connection_internal::*;
7879
use rand::{thread_rng, Rng};
7980
use rcgen::KeyPair;
81+
use smol_str::SmolStr;
8082
use srtp::stream::Stream;
8183
use std::future::Future;
8284
use std::pin::Pin;
@@ -454,7 +456,9 @@ impl RTCPeerConnection {
454456
// return true
455457
// }
456458
let mid = t.mid();
457-
let m = mid.as_ref().and_then(|mid| get_by_mid(mid, local_desc));
459+
let m = mid
460+
.as_ref()
461+
.and_then(|mid| get_by_mid(mid.0.as_str(), local_desc));
458462
// Step 5.2
459463
if !t.stopped.load(Ordering::SeqCst) {
460464
if m.is_none() {
@@ -493,8 +497,9 @@ impl RTCPeerConnection {
493497
RTCSdpType::Offer => {
494498
// Step 5.3.2
495499
if let Some(remote_desc) = &current_remote_description {
496-
if let Some(rm) =
497-
t.mid().and_then(|mid| get_by_mid(&mid, remote_desc))
500+
if let Some(rm) = t
501+
.mid()
502+
.and_then(|mid| get_by_mid(mid.0.as_str(), remote_desc))
498503
{
499504
if get_peer_direction(m) != t.direction()
500505
&& get_peer_direction(rm) != t.direction().reverse()
@@ -511,18 +516,20 @@ impl RTCPeerConnection {
511516
Some(d) => d,
512517
None => return true,
513518
};
514-
let offered_direction =
515-
match t.mid().and_then(|mid| get_by_mid(&mid, remote_desc)) {
516-
Some(d) => {
517-
let dir = get_peer_direction(d);
518-
if dir == RTCRtpTransceiverDirection::Unspecified {
519-
RTCRtpTransceiverDirection::Inactive
520-
} else {
521-
dir
522-
}
519+
let offered_direction = match t
520+
.mid()
521+
.and_then(|mid| get_by_mid(mid.0.as_str(), remote_desc))
522+
{
523+
Some(d) => {
524+
let dir = get_peer_direction(d);
525+
if dir == RTCRtpTransceiverDirection::Unspecified {
526+
RTCRtpTransceiverDirection::Inactive
527+
} else {
528+
dir
523529
}
524-
None => RTCRtpTransceiverDirection::Inactive,
525-
};
530+
}
531+
None => RTCRtpTransceiverDirection::Inactive,
532+
};
526533

527534
let current_direction = get_peer_direction(m);
528535
// Step 5.3.3
@@ -544,8 +551,8 @@ impl RTCPeerConnection {
544551
};
545552

546553
if let Some(remote_desc) = &*params.current_remote_description.lock().await {
547-
return get_by_mid(&search_mid, local_desc).is_some()
548-
|| get_by_mid(&search_mid, remote_desc).is_some();
554+
return get_by_mid(search_mid.0.as_str(), local_desc).is_some()
555+
|| get_by_mid(search_mid.0.as_str(), remote_desc).is_some();
549556
}
550557
}
551558
}
@@ -795,10 +802,13 @@ impl RTCPeerConnection {
795802
}
796803
}
797804

798-
t.set_mid(mid)?;
805+
t.set_mid(crate::SmallStr(SmolStr::from(mid)))?;
799806
} else {
800807
let greater_mid = self.internal.greater_mid.fetch_add(1, Ordering::SeqCst);
801-
t.set_mid(format!("{}", greater_mid + 1))?;
808+
t.set_mid(crate::SmallStr(SmolStr::from(format!(
809+
"{}",
810+
greater_mid + 1
811+
))))?;
802812
}
803813
}
804814

@@ -1358,7 +1368,7 @@ impl RTCPeerConnection {
13581368

13591369
if let Some(t) = t {
13601370
if t.mid().is_none() {
1361-
t.set_mid(mid_value.to_owned())?;
1371+
t.set_mid(crate::SmallStr(SmolStr::from(mid_value)))?;
13621372
}
13631373
} else {
13641374
let local_direction =
@@ -1404,7 +1414,7 @@ impl RTCPeerConnection {
14041414
self.internal.add_rtp_transceiver(Arc::clone(&t)).await;
14051415

14061416
if t.mid().is_none() {
1407-
t.set_mid(mid_value.to_owned())?;
1417+
t.set_mid(SmallStr(SmolStr::from(mid_value)))?;
14081418
}
14091419
}
14101420
}

0 commit comments

Comments
 (0)