Skip to content

Commit

Permalink
src: stream: Add Custom RTSP Media Factory
Browse files Browse the repository at this point in the history
  • Loading branch information
joaoantoniocardoso committed Apr 18, 2023
1 parent 2d09e91 commit 21614db
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/stream/rtsp/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
mod rtsp_media_factory;
pub mod rtsp_server;
78 changes: 78 additions & 0 deletions src/stream/rtsp/rtsp_media_factory.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/// /References:
/// 1 - https://gitlab.freedesktop.org/slomo/rtp-rapid-sync-example
/// 2 - https://github.com/gtk-rs/gtk3-rs/blob/master/examples/list_box_model/row_data/imp.rs
mod imp {
use std::sync::{Arc, Mutex};

use once_cell::sync::Lazy;

use gst::{
glib::{self, subclass::prelude::*, *},
prelude::*,
};
use gst_rtsp_server::subclass::prelude::*;

// The actual data structure that stores our values. This is not accessible
// directly from the outside.
#[derive(Default)]
pub struct Factory {
bin: Arc<Mutex<gst::Bin>>,
}

// Basic declaration of our type for the GObject type system
#[glib::object_subclass]
impl ObjectSubclass for Factory {
const NAME: &'static str = "RTSPMediaFactoryFromBin";
type Type = super::Factory;
type ParentType = gst_rtsp_server::RTSPMediaFactory;
}

// The ObjectImpl trait provides the setters/getters for GObject properties.
// Here we need to provide the values that are internally stored back to the
// caller, or store whatever new value the caller is providing.
//
// This maps between the GObject properties and our internal storage of the
// corresponding values of the properties.
impl ObjectImpl for Factory {
fn properties() -> &'static [glib::ParamSpec] {
static PROPERTIES: Lazy<Vec<glib::ParamSpec>> = Lazy::new(|| {
vec![glib::ParamSpecObject::builder::<gst::Bin>("bin")
.construct_only()
.build()]
});

PROPERTIES.as_ref()
}

fn set_property(&self, _id: usize, value: &glib::Value, pspec: &glib::ParamSpec) {
match pspec.name() {
"bin" => {
let bin = value.get().unwrap();
self.bin.set(bin);
}
_ => unimplemented!(),
}
}
}

impl RTSPMediaFactoryImpl for Factory {
// Create the custom stream producer bin.
fn create_element(&self, _url: &gst_rtsp::RTSPUrl) -> Option<gst::Element> {
let bin = self.bin.lock().unwrap();
bin.debug_to_dot_file_with_ts(gst::DebugGraphDetails::all(), "rtsp-bin-created");

Some(bin.to_owned().upcast())
}
}
}

gst::glib::wrapper! {
pub struct Factory(ObjectSubclass<imp::Factory>) @extends gst_rtsp_server::RTSPMediaFactory;
}

// Trivial constructor for the media factory.
impl Factory {
pub fn new(bin: gst::Bin) -> Self {
gst::glib::Object::builder().property("bin", bin).build()
}
}

0 comments on commit 21614db

Please sign in to comment.