Skip to content

Commit

Permalink
src: stream, video: Update code to use the generic Raw
Browse files Browse the repository at this point in the history
  • Loading branch information
joaoantoniocardoso committed Aug 9, 2023
1 parent 29bdde0 commit ede3fa3
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 28 deletions.
21 changes: 16 additions & 5 deletions src/stream/pipeline/fake_pipeline.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use crate::{
stream::types::CaptureConfiguration,
stream::{
pipeline::{DEFAULT_RAW_FORMAT, KNOWN_RTP_RAW_FORMATS},
types::CaptureConfiguration,
},
video::{
types::{VideoEncodeType, VideoSourceType},
video_source_gst::VideoSourceGstType,
Expand Down Expand Up @@ -70,14 +73,15 @@ impl FakePipeline {
format!(concat!(
"videotestsrc pattern={pattern} is-live=true do-timestamp=true",
" ! timeoverlay",
" ! video/x-raw,format=I420",
" ! video/x-raw,format={format}",
" ! x264enc tune=zerolatency speed-preset=ultrafast bitrate=5000",
" ! h264parse",
" ! capsfilter name={filter_name} caps=video/x-h264,profile={profile},stream-format=avc,alignment=au,width={width},height={height},framerate={interval_denominator}/{interval_numerator}",
" ! rtph264pay aggregate-mode=zero-latency config-interval=10 pt=96",
" ! tee name={sink_tee_name} allow-not-linked=true"
),
pattern = pattern,
format = DEFAULT_RAW_FORMAT,
profile = "constrained-baseline",
width = configuration.width,
height = configuration.height,
Expand All @@ -87,20 +91,26 @@ impl FakePipeline {
sink_tee_name = sink_tee_name,
)
}
VideoEncodeType::Yuyv => {
VideoEncodeType::Raw(fourcc) => {
let mut fourcc = fourcc.clone();
if !KNOWN_RTP_RAW_FORMATS.contains(&fourcc.as_str()) {
fourcc = DEFAULT_RAW_FORMAT.to_string();
}

format!(
concat!(
// Because application-rtp templates doesn't accept "YUY2", we
// need to transcode it. We are arbitrarily chosing the closest
// format available ("UYVY").
"videotestsrc pattern={pattern} is-live=true do-timestamp=true",
" ! timeoverlay",
" ! video/x-raw,format=I420",
" ! capsfilter name={filter_name} caps=video/x-raw,format=I420,width={width},height={height},framerate={interval_denominator}/{interval_numerator}",
" ! capsfilter name={filter_name} caps=video/x-raw,format={format},width={width},height={height},framerate={interval_denominator}/{interval_numerator}",
" ! rtpvrawpay pt=96",
" ! tee name={sink_tee_name} allow-not-linked=true",
),
pattern = pattern,
format = fourcc,
width = configuration.width,
height = configuration.height,
interval_denominator = configuration.frame_interval.denominator,
Expand All @@ -114,13 +124,14 @@ impl FakePipeline {
concat!(
"videotestsrc pattern={pattern} is-live=true do-timestamp=true",
" ! timeoverlay",
" ! video/x-raw,format=I420",
" ! video/x-raw,format={format}",
" ! jpegenc quality=85 idct-method=1",
" ! capsfilter name={filter_name} caps=image/jpeg,width={width},height={height},framerate={interval_denominator}/{interval_numerator}",
" ! rtpjpegpay pt=96",
" ! tee name={sink_tee_name} allow-not-linked=true",
),
pattern = pattern,
format = DEFAULT_RAW_FORMAT,
width = configuration.width,
height = configuration.height,
interval_denominator = configuration.frame_interval.denominator,
Expand Down
15 changes: 12 additions & 3 deletions src/stream/pipeline/v4l_pipeline.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use crate::{
stream::types::CaptureConfiguration,
stream::{
pipeline::{DEFAULT_RAW_FORMAT, KNOWN_RTP_RAW_FORMATS},
types::CaptureConfiguration,
},
video::types::{VideoEncodeType, VideoSourceType},
video_stream::types::VideoAndStreamInformation,
};
Expand Down Expand Up @@ -69,16 +72,22 @@ impl V4lPipeline {
sink_tee_name = sink_tee_name,
)
}
VideoEncodeType::Yuyv => {
VideoEncodeType::Raw(fourcc) => {
let mut fourcc = fourcc.clone();
if !KNOWN_RTP_RAW_FORMATS.contains(&fourcc.as_str()) {
fourcc = DEFAULT_RAW_FORMAT.to_string();
}

format!(
concat!(
"v4l2src device={device} do-timestamp=false",
" ! videoconvert",
" ! capsfilter name={filter_name} caps=video/x-raw,format=I420,width={width},height={height},framerate={interval_denominator}/{interval_numerator}",
" ! capsfilter name={filter_name} caps=video/x-raw,format={format},width={width},height={height},framerate={interval_denominator}/{interval_numerator}",
" ! rtpvrawpay pt=96",
" ! tee name={sink_tee_name} allow-not-linked=true"
),
device = device,
format = fourcc,
width = width,
height = height,
interval_denominator = interval_denominator,
Expand Down
7 changes: 5 additions & 2 deletions src/stream/sink/image_sink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ use image::FlatSamples;
use gst::prelude::*;

use super::SinkInterface;
use crate::{stream::pipeline::runner::PipelineRunner, video::types::VideoEncodeType};
use crate::{
stream::pipeline::{runner::PipelineRunner, DEFAULT_RAW_FORMAT, KNOWN_RTP_RAW_FORMATS},
video::types::VideoEncodeType,
};

type ClonableResult<T> = Result<T, Arc<Error>>;

Expand Down Expand Up @@ -356,7 +359,7 @@ impl ImageSink {
_transcoding_elements.push(parser);
_transcoding_elements.push(decoder);
}
VideoEncodeType::Yuyv => {
VideoEncodeType::Raw(_) => {
let depayloader = gst::ElementFactory::make("rtpvrawdepay").build()?;
_transcoding_elements.push(depayloader);
}
Expand Down
4 changes: 3 additions & 1 deletion src/video/video_source_gst.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use crate::stream::pipeline::DEFAULT_RAW_FORMAT;

use super::types::*;
use super::video_source::{VideoSource, VideoSourceAvailable};
use super::video_source_local::VideoSourceLocal;
Expand Down Expand Up @@ -66,7 +68,7 @@ impl VideoSource for VideoSourceGst {
sizes: sizes.clone(),
},
Format {
encode: VideoEncodeType::Yuyv,
encode: VideoEncodeType::Raw(DEFAULT_RAW_FORMAT.to_string()),
sizes: sizes.clone(),
},
Format {
Expand Down
117 changes: 100 additions & 17 deletions src/video/video_source_local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,10 @@ mod tests {
mod device_identification_tests {
use super::*;
use crate::{
stream::types::{CaptureConfiguration, StreamInformation},
stream::{
pipeline::DEFAULT_RAW_FORMAT,
types::{CaptureConfiguration, StreamInformation},
},
video_stream::types::VideoAndStreamInformation,
};
use VideoEncodeType::*;
Expand Down Expand Up @@ -777,11 +780,26 @@ mod device_identification_tests {

let candidates = vec![
add_available_camera("A", "/dev/video0", "usb_port_0", vec![H264]),
add_available_camera("A", "/dev/video1", "usb_port_0", vec![Yuyv, Mjpg]),
add_available_camera(
"A",
"/dev/video1",
"usb_port_0",
vec![Raw(DEFAULT_RAW_FORMAT.to_string()), Mjpg],
),
add_available_camera("A", "/dev/video2", "usb_port_1", vec![H264]),
add_available_camera("A", "/dev/video3", "usb_port_1", vec![Yuyv, Mjpg]),
add_available_camera(
"A",
"/dev/video3",
"usb_port_1",
vec![Raw(DEFAULT_RAW_FORMAT.to_string()), Mjpg],
),
add_available_camera("B", "/dev/video4", "usb_port_2", vec![H264]),
add_available_camera("B", "/dev/video5", "usb_port_2", vec![Yuyv, Mjpg]),
add_available_camera(
"B",
"/dev/video5",
"usb_port_2",
vec![Raw(DEFAULT_RAW_FORMAT.to_string()), Mjpg],
),
];

let same_name_candidates = VideoSourceLocal::get_cameras_with_same_name(&candidates, "A");
Expand All @@ -798,8 +816,18 @@ mod device_identification_tests {
let candidates = vec![
add_available_camera("A", "/dev/video0", "usb_port_0", vec![H264]),
add_available_camera("B", "/dev/video1", "usb_port_1", vec![H264]),
add_available_camera("C", "/dev/video2", "usb_port_0", vec![Yuyv, Mjpg]),
add_available_camera("D", "/dev/video3", "usb_port_1", vec![Yuyv, Mjpg]),
add_available_camera(
"C",
"/dev/video2",
"usb_port_0",
vec![Raw(DEFAULT_RAW_FORMAT.to_string()), Mjpg],
),
add_available_camera(
"D",
"/dev/video3",
"usb_port_1",
vec![Raw(DEFAULT_RAW_FORMAT.to_string()), Mjpg],
),
];

let same_encode_candidates =
Expand All @@ -816,9 +844,19 @@ mod device_identification_tests {

let candidates = vec![
add_available_camera("A", "/dev/video0", "usb_port_0", vec![H264]),
add_available_camera("B", "/dev/video1", "usb_port_0", vec![Yuyv, Mjpg]),
add_available_camera(
"B",
"/dev/video1",
"usb_port_0",
vec![Raw(DEFAULT_RAW_FORMAT.to_string()), Mjpg],
),
add_available_camera("C", "/dev/video2", "usb_port_1", vec![H264]),
add_available_camera("D", "/dev/video3", "usb_port_1", vec![Yuyv, Mjpg]),
add_available_camera(
"D",
"/dev/video3",
"usb_port_1",
vec![Raw(DEFAULT_RAW_FORMAT.to_string()), Mjpg],
),
];

let same_encode_candidates = VideoSourceLocal::get_cameras_with_same_bus(
Expand All @@ -838,10 +876,25 @@ mod device_identification_tests {

let candidates = vec![
add_available_camera("A", "/dev/video0", "usb_port_0", vec![H264]),
add_available_camera("A", "/dev/video1", "usb_port_0", vec![Yuyv, Mjpg]),
add_available_camera(
"A",
"/dev/video1",
"usb_port_0",
vec![Raw(DEFAULT_RAW_FORMAT.to_string()), Mjpg],
),
add_available_camera("B", "/dev/video2", "usb_port_1", vec![H264]),
add_available_camera("B", "/dev/video3", "usb_port_1", vec![Yuyv, Mjpg]),
add_available_camera("C", "/dev/video3", "usb_port_1", vec![H264, Yuyv, Mjpg]),
add_available_camera(
"B",
"/dev/video3",
"usb_port_1",
vec![Raw(DEFAULT_RAW_FORMAT.to_string()), Mjpg],
),
add_available_camera(
"C",
"/dev/video3",
"usb_port_1",
vec![H264, Raw(DEFAULT_RAW_FORMAT.to_string()), Mjpg],
),
];
let stream = create_stream("A", "/dev/video0", "usb_port_0", H264);
let (VideoSourceType::Local(source), CaptureConfiguration::Video(capture_configuration)) = (&stream.video_source, &stream.stream_information.configuration) else {
Expand Down Expand Up @@ -878,9 +931,19 @@ mod device_identification_tests {

let candidates = vec![
add_available_camera("A", "/dev/video0", current_usb_bus, vec![H264]),
add_available_camera("A", "/dev/video1", current_usb_bus, vec![Yuyv, Mjpg]),
add_available_camera(
"A",
"/dev/video1",
current_usb_bus,
vec![Raw(DEFAULT_RAW_FORMAT.to_string()), Mjpg],
),
add_available_camera("B", "/dev/video2", "usb_port_3", vec![H264]),
add_available_camera("B", "/dev/video3", "usb_port_3", vec![Yuyv, Mjpg]),
add_available_camera(
"B",
"/dev/video3",
"usb_port_3",
vec![Raw(DEFAULT_RAW_FORMAT.to_string()), Mjpg],
),
];

for n in (0..3).collect::<Vec<_>>() {
Expand Down Expand Up @@ -922,8 +985,18 @@ mod device_identification_tests {
let candidates = vec![
add_available_camera("A", current_path, "usb_port_0", vec![H264]),
add_available_camera("A", last_path, "usb_port_1", vec![H264]),
add_available_camera("A", "/dev/video3", "usb_port_0", vec![Yuyv, Mjpg]),
add_available_camera("A", "/dev/video5", "usb_port_1", vec![Yuyv, Mjpg]),
add_available_camera(
"A",
"/dev/video3",
"usb_port_0",
vec![Raw(DEFAULT_RAW_FORMAT.to_string()), Mjpg],
),
add_available_camera(
"A",
"/dev/video5",
"usb_port_1",
vec![Raw(DEFAULT_RAW_FORMAT.to_string()), Mjpg],
),
];

for n in (0..=1).collect::<Vec<_>>() {
Expand Down Expand Up @@ -957,9 +1030,19 @@ mod device_identification_tests {

let candidates = vec![
add_available_camera("A", "/dev/video0", current_usb_bus, vec![H264]),
add_available_camera("A", "/dev/video1", current_usb_bus, vec![Yuyv, Mjpg]),
add_available_camera(
"A",
"/dev/video1",
current_usb_bus,
vec![Raw(DEFAULT_RAW_FORMAT.to_string()), Mjpg],
),
add_available_camera("A", "/dev/video4", "usb_port_2", vec![H264]),
add_available_camera("A", "/dev/video5", "usb_port_2", vec![Yuyv, Mjpg]),
add_available_camera(
"A",
"/dev/video5",
"usb_port_2",
vec![Raw(DEFAULT_RAW_FORMAT.to_string()), Mjpg],
),
];

for n in (0..5).collect::<Vec<_>>() {
Expand Down

0 comments on commit ede3fa3

Please sign in to comment.