Skip to content

Commit 9c0c716

Browse files
author
LChen
committed
压缩算法替换为zstd,减少资源消耗
1 parent 1c2dce6 commit 9c0c716

File tree

2 files changed

+9
-8
lines changed

2 files changed

+9
-8
lines changed

dscom/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ edition = "2021"
66
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
77

88
[dependencies]
9-
bzip2 = "0.4.3"
9+
zstd = "0.9"

dscom/src/lib.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
use std::io::Read;
2-
3-
use bzip2::{read::{BzEncoder, BzDecoder}, Compression};
41

52
// 默认帧率
63
pub const FPS: u64 = 20;
74

85
// 传输像素保留位数(右边0越多压缩程度越大)
96
pub const BIT_SAVE: u8 = 0b1111_1000;
107

8+
// 传输压缩水平0-21 0消耗资源最小但是压缩率小(需要带宽大) 21消耗资源最大,但但是压缩率大(需要带宽小)
9+
pub const COMPRESS_LEVEL: i32 = 3;
10+
1111
// key事件 start
1212
pub const KEY_UP: u8 = 1;
1313
pub const KEY_DOWN: u8 = 2;
@@ -23,19 +23,20 @@ pub const MOVE:u8 = 7;
2323

2424

2525
pub fn compress(src: &[u8], dst: &mut Vec<u8>) -> usize {
26+
2627
unsafe{
2728
dst.set_len(0);
2829
}
29-
let mut compressor = BzEncoder::new(src, Compression::best());
30-
return compressor.read_to_end(dst).unwrap();
30+
zstd::stream::copy_encode(src, &mut *dst, COMPRESS_LEVEL).unwrap();
31+
return dst.len();
3132
}
3233

3334
pub fn decompress(src: &[u8], dst: &mut Vec<u8>) -> usize {
3435
unsafe {
3536
dst.set_len(0);
3637
}
37-
let mut decompressor = BzDecoder::new(src);
38-
return decompressor.read_to_end(dst).unwrap();
38+
zstd::stream::copy_decode(src, &mut *dst).unwrap();
39+
return dst.len();
3940
}
4041

4142
#[cfg(test)]

0 commit comments

Comments
 (0)