|
| 1 | +use gst::glib::subclass::types::ObjectSubclassExt; |
| 2 | +use gst::prelude::{ElementExt, GhostPadExt, GstBinExt, PadExt}; |
| 3 | +use gst::subclass::prelude::{BinImpl, ElementImpl, ObjectImpl, URIHandlerImpl}; |
| 4 | +use gst::subclass::prelude::{GstObjectImpl, ObjectSubclass}; |
| 5 | +use gst::{glib, GhostPad, PadDirection}; |
| 6 | +use gstreamer_base::gst; |
| 7 | + |
| 8 | +const ASSET_URI_SCHEME: &str = "asset"; |
| 9 | + |
| 10 | +#[derive(Default)] |
| 11 | +pub struct TauriAsset { |
| 12 | + // uri: Option<String>, |
| 13 | +} |
| 14 | + |
| 15 | +impl TauriAsset { |
| 16 | + pub fn new() -> Self { |
| 17 | + Self::default() |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +#[glib::object_subclass] |
| 22 | +impl ObjectSubclass for TauriAsset { |
| 23 | + const NAME: &'static str = "GstTauriAsset"; |
| 24 | + type Type = super::TauriAsset; |
| 25 | + type ParentType = gst::Bin; |
| 26 | + type Interfaces = (gst::URIHandler,); |
| 27 | +} |
| 28 | + |
| 29 | +impl GstObjectImpl for TauriAsset {} |
| 30 | +impl ObjectImpl for TauriAsset {} |
| 31 | +impl BinImpl for TauriAsset {} |
| 32 | +impl ElementImpl for TauriAsset {} |
| 33 | + |
| 34 | +impl URIHandlerImpl for TauriAsset { |
| 35 | + const URI_TYPE: gst::URIType = gst::URIType::Src; |
| 36 | + |
| 37 | + fn protocols() -> &'static [&'static str] { |
| 38 | + &[ASSET_URI_SCHEME] |
| 39 | + } |
| 40 | + |
| 41 | + fn uri(&self) -> Option<String> { |
| 42 | + None |
| 43 | + } |
| 44 | + |
| 45 | + fn set_uri(&self, uri: &str) -> Result<(), glib::Error> { |
| 46 | + // uri is like: asset://path/to/asset or asset://localhost/path/to/asset |
| 47 | + |
| 48 | + let sep = format!("{}://", ASSET_URI_SCHEME); |
| 49 | + let mut split = uri.split(sep.as_str()); |
| 50 | + let location = split |
| 51 | + .nth(1) |
| 52 | + .ok_or_else(|| glib::Error::new(gst::URIError::BadUri, "Could not get location from URI"))?; |
| 53 | + |
| 54 | + // directly having full path after asset:// or having localhost |
| 55 | + let location = location.strip_prefix("localhost").unwrap_or(location); |
| 56 | + |
| 57 | + // now location is like: /path/to/asset |
| 58 | + let internal_src = gst::ElementFactory::make("filesrc") |
| 59 | + .name("filesrc") |
| 60 | + .property("location", location) |
| 61 | + .build() |
| 62 | + .ok(); |
| 63 | + |
| 64 | + let element = self.obj(); |
| 65 | + element |
| 66 | + .add(internal_src.as_ref().unwrap()) |
| 67 | + .expect("Failed to add internal source"); |
| 68 | + |
| 69 | + let srcpad = internal_src |
| 70 | + .as_ref() |
| 71 | + .and_then(|src| src.static_pad("src")) |
| 72 | + .ok_or_else(|| { |
| 73 | + glib::Error::new( |
| 74 | + gst::URIError::BadUri, |
| 75 | + "Could not get src pad from internal source", |
| 76 | + ) |
| 77 | + })?; |
| 78 | + |
| 79 | + let ghostpad = GhostPad::new(PadDirection::Src); |
| 80 | + ghostpad |
| 81 | + .set_target(Some(&srcpad)) |
| 82 | + .ok() |
| 83 | + .ok_or_else(|| glib::Error::new(gst::URIError::BadUri, "Could not create ghost pad"))?; |
| 84 | + |
| 85 | + ghostpad |
| 86 | + .set_active(true) |
| 87 | + .ok() |
| 88 | + .ok_or_else(|| glib::Error::new(gst::URIError::BadUri, "Could not activate ghost pad"))?; |
| 89 | + |
| 90 | + let element = self.obj(); |
| 91 | + element.add_pad(&ghostpad).expect("Failed to add ghost pad"); |
| 92 | + Ok(()) |
| 93 | + } |
| 94 | +} |
0 commit comments