This repository was archived by the owner on Apr 23, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 65
Allow consuming stream data from a client app #372
Open
ferjm
wants to merge
5
commits into
servo:main
Choose a base branch
from
ferjm:canvas.capturestream
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2e28a2b
Expose method to push data to media stream
ferjm b3b77f3
Implement Debug, Deserialize and Serialize for MediaStreamId
ferjm d46d87d
Use Uuid serde feature
ferjm 6a8f315
Attach encoding adapters to ServoMediaStreamSrc
ferjm 23083e6
Detach independent video pipeline with a decodebin for media streams
ferjm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -14,6 +14,7 @@ path = "lib.rs" | |
|
|
||
| [dependencies] | ||
| boxfnonce = "0.1.0" | ||
| euclid = "0.20" | ||
| mime = "0.3.13" | ||
| log = "0.4" | ||
|
|
||
|
|
||
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,7 +1,10 @@ | ||
| use super::BACKEND_BASE_TIME; | ||
|
|
||
| use euclid::default::Size2D; | ||
| use glib::prelude::*; | ||
| use gst; | ||
| use gst::prelude::*; | ||
| use gst_app::AppSrc; | ||
| use servo_media_streams::registry::{ | ||
| get_stream, register_stream, unregister_stream, MediaStreamId, | ||
| }; | ||
|
|
@@ -29,6 +32,7 @@ pub struct GStreamerMediaStream { | |
| type_: MediaStreamType, | ||
| elements: Vec<gst::Element>, | ||
| pipeline: Option<gst::Pipeline>, | ||
| video_app_source: Option<AppSrc>, | ||
| } | ||
|
|
||
| impl MediaStream for GStreamerMediaStream { | ||
|
|
@@ -47,6 +51,15 @@ impl MediaStream for GStreamerMediaStream { | |
| fn ty(&self) -> MediaStreamType { | ||
| self.type_ | ||
| } | ||
|
|
||
| fn push_data(&self, data: Vec<u8>) { | ||
| if let Some(ref appsrc) = self.video_app_source { | ||
| let buffer = gst::Buffer::from_slice(data); | ||
| if let Err(error) = appsrc.push_buffer(buffer) { | ||
| warn!("{}", error); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl GStreamerMediaStream { | ||
|
|
@@ -56,6 +69,7 @@ impl GStreamerMediaStream { | |
| type_, | ||
| elements, | ||
| pipeline: None, | ||
| video_app_source: None, | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -91,6 +105,10 @@ impl GStreamerMediaStream { | |
| self.elements.last().unwrap().clone() | ||
| } | ||
|
|
||
| pub fn first_element(&self) -> gst::Element { | ||
| self.elements.first().unwrap().clone() | ||
| } | ||
|
|
||
| pub fn attach_to_pipeline(&mut self, pipeline: &gst::Pipeline) { | ||
| assert!(self.pipeline.is_none()); | ||
| let elements: Vec<_> = self.elements.iter().collect(); | ||
|
|
@@ -123,7 +141,7 @@ impl GStreamerMediaStream { | |
| .set_property("is-live", &true) | ||
| .expect("videotestsrc doesn't have expected 'is-live' property"); | ||
|
|
||
| Self::create_video_from(videotestsrc) | ||
| Self::create_video_from(videotestsrc, None) | ||
| } | ||
|
|
||
| /// Attaches encoding adapters to the stream, returning the source element | ||
|
|
@@ -174,14 +192,74 @@ impl GStreamerMediaStream { | |
| } | ||
| } | ||
|
|
||
| pub fn create_video_from(source: gst::Element) -> MediaStreamId { | ||
| pub fn set_video_app_source(&mut self, source: &AppSrc) { | ||
| self.video_app_source = Some(source.clone()); | ||
| } | ||
|
|
||
| pub fn create_video_from(source: gst::Element, size: Option<Size2D<u32>>) -> MediaStreamId { | ||
|
Member
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. We shouldn't be doing decoding within |
||
| let src = gst::ElementFactory::make("proxysrc", None).unwrap(); | ||
| let videoconvert = gst::ElementFactory::make("videoconvert", None).unwrap(); | ||
| let queue = gst::ElementFactory::make("queue", None).unwrap(); | ||
|
|
||
| register_stream(Arc::new(Mutex::new(GStreamerMediaStream::new( | ||
| let stream = Arc::new(Mutex::new(GStreamerMediaStream::new( | ||
| MediaStreamType::Video, | ||
| vec![source, videoconvert, queue], | ||
| )))) | ||
| vec![src, videoconvert, queue], | ||
| ))); | ||
|
|
||
| let pipeline = gst::Pipeline::new(Some("video pipeline")); | ||
| let clock = gst::SystemClock::obtain(); | ||
| pipeline.set_start_time(gst::ClockTime::none()); | ||
| pipeline.set_base_time(*BACKEND_BASE_TIME); | ||
| pipeline.use_clock(Some(&clock)); | ||
|
|
||
| let decodebin = gst::ElementFactory::make("decodebin", None).unwrap(); | ||
|
|
||
| let stream_ = stream.clone(); | ||
| let video_pipeline = pipeline.clone(); | ||
| decodebin.connect_pad_added(move |decodebin, _| { | ||
| // Append a proxysink to the video pipeline. | ||
| let proxy_sink = gst::ElementFactory::make("proxysink", None).unwrap(); | ||
| video_pipeline.add(&proxy_sink).unwrap(); | ||
| gst::Element::link_many(&[decodebin, &proxy_sink]).unwrap(); | ||
|
|
||
| // And connect the video and media stream pipelines. | ||
| let stream = stream_.lock().unwrap(); | ||
| let first_element = stream.first_element(); | ||
| first_element | ||
| .set_property("proxysink", &proxy_sink) | ||
| .unwrap(); | ||
|
|
||
| proxy_sink.sync_state_with_parent().unwrap(); | ||
| decodebin.sync_state_with_parent().unwrap(); | ||
| }); | ||
|
|
||
| if let Some(size) = size { | ||
| let caps = gst::Caps::builder("video/x-raw") | ||
| .field("format", &gst_video::VideoFormat::Bgra.to_string()) | ||
| .field("pixel-aspect-ratio", &gst::Fraction::from((1, 1))) | ||
| .field("width", &(size.width as i32)) | ||
| .field("height", &(size.height as i32)) | ||
| .build(); | ||
| source | ||
| .set_property("caps", &caps) | ||
| .expect("source doesn't have expected 'caps' property"); | ||
| } | ||
|
|
||
| if let Some(appsrc) = source.downcast_ref::<AppSrc>() { | ||
| appsrc.set_property_format(gst::Format::Time); | ||
| stream.lock().unwrap().set_video_app_source(appsrc); | ||
| } | ||
|
|
||
| pipeline.add_many(&[&source, &decodebin]).unwrap(); | ||
| gst::Element::link_many(&[&source, &decodebin]).unwrap(); | ||
|
|
||
| pipeline.set_state(gst::State::Playing).unwrap(); | ||
|
|
||
| #[cfg(debug_assertions)] | ||
| pipeline | ||
| .upcast::<gst::Bin>() | ||
| .debug_to_dot_file(gst::DebugGraphDetails::all(), "VideoPipeline_PLAYING"); | ||
|
|
||
| register_stream(stream) | ||
| } | ||
|
|
||
| pub fn create_audio() -> MediaStreamId { | ||
|
|
@@ -212,11 +290,21 @@ impl GStreamerMediaStream { | |
| proxy_src.set_property("proxysink", &proxy_sink).unwrap(); | ||
| let stream = match ty { | ||
| MediaStreamType::Audio => Self::create_audio_from(proxy_src), | ||
| MediaStreamType::Video => Self::create_video_from(proxy_src), | ||
| MediaStreamType::Video => Self::create_video_from(proxy_src, None), | ||
| }; | ||
|
|
||
| (stream, GstreamerMediaSocket { proxy_sink }) | ||
| } | ||
|
|
||
| pub fn push_data(stream: &MediaStreamId, data: Vec<u8>) { | ||
| let stream = get_stream(stream).expect("Media streams registry does not contain such ID"); | ||
| let mut stream = stream.lock().unwrap(); | ||
| let stream = stream | ||
| .as_mut_any() | ||
| .downcast_mut::<GStreamerMediaStream>() | ||
| .unwrap(); | ||
| stream.push_data(data); | ||
| } | ||
| } | ||
|
|
||
| impl Drop for GStreamerMediaStream { | ||
|
|
||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I don't think we should be doing this in
create_videoinput_stream, that's specifically for screen capture input. We should have a separatecreate_capture_streamIMO.