-
Notifications
You must be signed in to change notification settings - Fork 30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support all raw formats #258
base: master
Are you sure you want to change the base?
Changes from all commits
2198dca
67aaa53
8d87090
3148be5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,21 +5,28 @@ use super::video_source_redirect::VideoSourceRedirect; | |
use paperclip::actix::Apiv2Schema; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
pub const KNOWN_RTP_RAW_FORMATS: [&str; 9] = [ | ||
"RGB", "RGBA", "BGR", "BGRA", "AYUV", "UYVY", "I420", "Y41B", "UYVP", | ||
]; | ||
pub const DEFAULT_RAW_FORMAT: &str = "I420"; | ||
|
||
#[derive(Apiv2Schema, Clone, Debug, PartialEq, Serialize, Deserialize)] | ||
pub enum VideoSourceType { | ||
Gst(VideoSourceGst), | ||
Local(VideoSourceLocal), | ||
Redirect(VideoSourceRedirect), | ||
} | ||
|
||
#[derive(Apiv2Schema, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Deserialize, Serialize)] | ||
#[derive(Apiv2Schema, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize)] | ||
#[serde(rename_all = "UPPERCASE")] | ||
pub enum VideoEncodeType { | ||
Unknown(String), | ||
H265, | ||
H264, | ||
Mjpg, | ||
Yuyv, | ||
#[serde(untagged)] | ||
Raw(String), | ||
#[serde(untagged)] | ||
Unknown(String), | ||
} | ||
|
||
#[derive(Apiv2Schema, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Deserialize, Serialize)] | ||
|
@@ -102,15 +109,64 @@ impl VideoSourceType { | |
} | ||
|
||
impl VideoEncodeType { | ||
//TODO: use trait fromstr, check others places | ||
pub fn from_str(fourcc: &str) -> VideoEncodeType { | ||
let fourcc = fourcc.to_uppercase(); | ||
match fourcc.as_str() { | ||
"H264" => VideoEncodeType::H264, | ||
"MJPG" => VideoEncodeType::Mjpg, | ||
"YUYV" => VideoEncodeType::Yuyv, | ||
_ => VideoEncodeType::Unknown(fourcc), | ||
|
||
let fourcc = match fourcc.as_str() { | ||
"H264" => return VideoEncodeType::H264, | ||
"MJPG" => return VideoEncodeType::Mjpg, | ||
// TODO: We can implement a [GstDeviceMonitor](https://gstreamer.freedesktop.org/documentation/gstreamer/gstdevicemonitor.html?gi-language=c) and then this manual mapping between v4l's and gst's fourcc will not be neccessary anymore. | ||
// A list of possible v4l fourcc from the Linux docs: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe that my previous comment shows how to do it with gstreamer VideoFormatInfo There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
// - [YUV](https://www.kernel.org/doc/html/v4.8/media/uapi/v4l/yuv-formats.html) | ||
// - [RGB](https://www.kernel.org/doc/html/v4.8/media/uapi/v4l/pixfmt-rgb.html) | ||
// - [compressed](https://www.kernel.org/doc/html/v4.8/media/uapi/v4l/pixfmt-013.html) | ||
"YUYV" => "YUY2".to_string(), | ||
_ => fourcc, | ||
}; | ||
|
||
// Use Gstreamer to recognize raw formats. [GStreamer raw formats](https://gstreamer.freedesktop.org/documentation/additional/design/mediatype-video-raw.html?gi-language=c#formats) | ||
use std::ffi::CString; | ||
if let Ok(c_char_array) = CString::new(fourcc.clone()).map(|c_str| c_str.into_raw()) { | ||
use gst_video::ffi::*; | ||
|
||
let gst_video_format = unsafe { gst_video_format_from_string(c_char_array) }; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe that my previous comment would also simplify this code. |
||
|
||
if !matches!( | ||
gst_video_format, | ||
GST_VIDEO_FORMAT_UNKNOWN | GST_VIDEO_FORMAT_ENCODED | ||
) { | ||
return VideoEncodeType::Raw(fourcc); | ||
} | ||
} | ||
|
||
VideoEncodeType::Unknown(fourcc) | ||
} | ||
} | ||
impl<'de> Deserialize<'de> for VideoEncodeType { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please add a space between the implementations. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you provide more information about why this implementation is necessary ? looking at the code this is not clear why. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Like, if we define the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So, it would be nice to have such explanation as a comment. |
||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> | ||
where | ||
D: serde::Deserializer<'de>, | ||
{ | ||
struct VideoEncodeTypeVisitor; | ||
|
||
use std::fmt; | ||
|
||
impl<'de> serde::de::Visitor<'de> for VideoEncodeTypeVisitor { | ||
type Value = VideoEncodeType; | ||
|
||
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { | ||
formatter.write_str("valid video encode type") | ||
} | ||
|
||
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E> | ||
where | ||
E: serde::de::Error, | ||
{ | ||
Ok(Self::Value::from_str(value)) | ||
} | ||
} | ||
|
||
deserializer.deserialize_str(VideoEncodeTypeVisitor) | ||
} | ||
} | ||
|
||
|
@@ -141,3 +197,27 @@ pub static STANDARD_SIZES: &[(u32, u32); 16] = &[ | |
(320, 240), | ||
(256, 144), | ||
]; | ||
|
||
mod tests { | ||
|
||
#[test] | ||
fn test_video_encode_type_serde() { | ||
use super::VideoEncodeType; | ||
|
||
for (encode, encode_str) in [ | ||
(VideoEncodeType::Mjpg, "\"MJPG\""), | ||
(VideoEncodeType::H264, "\"H264\""), | ||
(VideoEncodeType::Raw("I420".to_string()), "\"I420\""), | ||
(VideoEncodeType::Unknown("POTATO".to_string()), "\"POTATO\""), | ||
] { | ||
debug_assert_eq!(encode_str, serde_json::to_string(&encode).unwrap()); | ||
|
||
debug_assert_eq!(encode, serde_json::from_str(encode_str).unwrap()); | ||
} | ||
|
||
// Ensure UYUV is parsed as YUY2: | ||
let encode = VideoEncodeType::Raw("YUY2".to_string()); | ||
let legacy_encode_str = "\"YUYV\""; | ||
debug_assert_eq!(encode, serde_json::from_str(legacy_encode_str).unwrap()); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you look at https://docs.rs/gstreamer-video/latest/gstreamer_video/struct.VideoFormatInfo.html#method.name ?
Where the types are defined here:
https://docs.rs/gstreamer-video/latest/gstreamer_video/enum.VideoFormat.html