-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
113 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
use bytes::{Buf, Bytes, BytesMut}; | ||
use tokio_util::codec::Decoder; | ||
|
||
pub(crate) struct JpegFrameDecoder; | ||
|
||
impl JpegFrameDecoder { | ||
pub(crate) fn default() -> Self { | ||
Self {} | ||
} | ||
} | ||
|
||
impl Decoder for JpegFrameDecoder { | ||
type Item = Bytes; | ||
type Error = std::io::Error; | ||
|
||
fn decode(&mut self, buf: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> { | ||
if let Some(idx) = find_first_jpeg_eoi(buf) { | ||
let image_buf = buf.copy_to_bytes(idx + 2); | ||
Ok(Some(image_buf)) | ||
} else { | ||
Ok(None) | ||
} | ||
} | ||
} | ||
|
||
fn find_first_jpeg_eoi(bytes: &BytesMut) -> Option<usize> { | ||
if bytes.len() < 2 { | ||
return None; | ||
} | ||
|
||
(0..bytes.len() - 1).find(|&i| bytes[i] == 0xFF && bytes[i + 1] == 0xD9) | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use super::*; | ||
|
||
#[test] | ||
fn decoder_none() { | ||
let mut decoder = JpegFrameDecoder::default(); | ||
|
||
let data = [0xFF, 0xD8, 1, 1, 1]; | ||
let mut data = BytesMut::from(&data[..]); | ||
|
||
let res = decoder.decode(&mut data); | ||
assert!(res.unwrap().is_none()); | ||
} | ||
|
||
#[test] | ||
fn decoder_one() { | ||
let mut decoder = JpegFrameDecoder::default(); | ||
|
||
let data = [0xFF, 0xD8, 1, 1, 1, 0xFF, 0xD9]; | ||
let mut data = BytesMut::from(&data[..]); | ||
|
||
let res = decoder.decode(&mut data); | ||
let res = res.unwrap().unwrap(); | ||
assert_eq!(res, [0xFFu8, 0xD8, 1, 1, 1, 0xFF, 0xD9][..]); | ||
} | ||
|
||
#[test] | ||
fn decoder_multiple() { | ||
let mut decoder = JpegFrameDecoder::default(); | ||
|
||
let data = [ | ||
0xFF, 0xD8, 1, 1, 1, 0xFF, 0xD9, 0xFF, 0xD8, 2, 2, 2, 0xFF, 0xD9, | ||
]; | ||
let mut data = BytesMut::from(&data[..]); | ||
|
||
let res = decoder.decode(&mut data); | ||
let res = res.unwrap().unwrap(); | ||
assert_eq!(res, [0xFFu8, 0xD8, 1, 1, 1, 0xFF, 0xD9][..]); | ||
|
||
let res = decoder.decode(&mut data); | ||
let res = res.unwrap().unwrap(); | ||
assert_eq!(res, [0xFFu8, 0xD8, 2, 2, 2, 0xFF, 0xD9][..]); | ||
} | ||
|
||
#[test] | ||
fn find_first_jpeg_eoi_none() { | ||
let data = [0xFF, 0xD8, 1, 1, 1]; | ||
let pos = find_first_jpeg_eoi(&BytesMut::from(&data[..])); | ||
assert_eq!(pos, None) | ||
} | ||
|
||
#[test] | ||
fn find_first_jpeg_eoi_single() { | ||
let data = [0xFF, 0xD8, 1, 1, 1, 0xFF, 0xD9]; | ||
let pos = find_first_jpeg_eoi(&BytesMut::from(&data[..])); | ||
assert_eq!(pos, Some(5)) | ||
} | ||
|
||
#[test] | ||
fn find_first_jpeg_eoi_multiple() { | ||
let data = [ | ||
0xFF, 0xD8, 1, 1, 1, 0xFF, 0xD9, 0xFF, 0xD8, 2, 2, 2, 0xFF, 0xD9, | ||
]; | ||
let pos = find_first_jpeg_eoi(&BytesMut::from(&data[..])); | ||
assert_eq!(pos, Some(5)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters