2021-10-15 14:57:55 +00:00
|
|
|
// Copyright (C) 2021 OneStream Live <guillaume.desmottes@onestream.live>
|
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public License, v2.0.
|
|
|
|
// If a copy of the MPL was not distributed with this file, You can obtain one at
|
|
|
|
// <https://mozilla.org/MPL/2.0/>.
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2022-04-07 12:19:56 +00:00
|
|
|
use std::sync::{mpsc, Mutex, MutexGuard};
|
2022-04-08 13:33:00 +00:00
|
|
|
use std::sync::{Arc, Weak};
|
2021-10-15 14:57:55 +00:00
|
|
|
|
|
|
|
use gst::glib;
|
|
|
|
use gst::prelude::*;
|
|
|
|
use gst::subclass::prelude::*;
|
|
|
|
|
|
|
|
use once_cell::sync::Lazy;
|
|
|
|
|
|
|
|
static CAT: Lazy<gst::DebugCategory> = Lazy::new(|| {
|
|
|
|
gst::DebugCategory::new(
|
|
|
|
"uriplaylistbin",
|
|
|
|
gst::DebugColorFlags::empty(),
|
|
|
|
Some("Uri Playlist Bin"),
|
|
|
|
)
|
|
|
|
});
|
|
|
|
|
|
|
|
/// how many items are allowed to be prepared and waiting in the pipeline
|
|
|
|
const MAX_STREAMING_ITEMS: usize = 2;
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
enum PlaylistError {
|
|
|
|
PluginMissing { error: anyhow::Error },
|
|
|
|
ItemFailed { error: anyhow::Error, item: Item },
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::fmt::Display for PlaylistError {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
match self {
|
|
|
|
PlaylistError::PluginMissing { error } => {
|
|
|
|
write!(f, "{}", error)
|
|
|
|
}
|
|
|
|
PlaylistError::ItemFailed { error, item } => {
|
|
|
|
write!(f, "{} (URI: {})", error, item.uri())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::error::Error for PlaylistError {
|
|
|
|
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
|
|
|
|
match self {
|
|
|
|
PlaylistError::PluginMissing { error } | PlaylistError::ItemFailed { error, .. } => {
|
|
|
|
Some(error.as_ref())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Number of different streams currently handled by the element
|
2021-11-30 14:31:50 +00:00
|
|
|
#[derive(Debug, Default, Clone, PartialEq)]
|
2021-10-15 14:57:55 +00:00
|
|
|
struct StreamsTopology {
|
|
|
|
audio: u32,
|
|
|
|
video: u32,
|
|
|
|
text: u32,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl StreamsTopology {
|
|
|
|
fn n_streams(&self) -> u32 {
|
|
|
|
self.audio + self.video + self.text
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-28 11:52:20 +00:00
|
|
|
impl From<gst::StreamCollection> for StreamsTopology {
|
2021-10-15 14:57:55 +00:00
|
|
|
fn from(collection: gst::StreamCollection) -> Self {
|
|
|
|
let (mut audio, mut video, mut text) = (0, 0, 0);
|
|
|
|
for stream in collection.iter() {
|
|
|
|
match stream.stream_type() {
|
|
|
|
gst::StreamType::AUDIO => audio += 1,
|
|
|
|
gst::StreamType::VIDEO => video += 1,
|
|
|
|
gst::StreamType::TEXT => text += 1,
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Self { audio, video, text }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
struct Settings {
|
|
|
|
uris: Vec<String>,
|
|
|
|
iterations: u32,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Settings {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
uris: vec![],
|
|
|
|
iterations: 1,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-22 09:49:49 +00:00
|
|
|
#[derive(Debug)]
|
2022-04-08 09:21:42 +00:00
|
|
|
enum Status {
|
|
|
|
/// all good element is working
|
|
|
|
Running,
|
|
|
|
/// the element stopped because of an error
|
|
|
|
Error,
|
2022-04-08 09:23:31 +00:00
|
|
|
/// the element is being shut down
|
|
|
|
ShuttingDown,
|
2022-04-08 09:21:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Status {
|
|
|
|
/// check if the element should stop proceeding
|
|
|
|
fn done(&self) -> bool {
|
|
|
|
match self {
|
|
|
|
Status::Running => false,
|
2022-04-08 09:23:31 +00:00
|
|
|
Status::Error | Status::ShuttingDown => true,
|
2022-04-08 09:21:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-22 09:49:49 +00:00
|
|
|
#[derive(Debug)]
|
2021-10-15 14:57:55 +00:00
|
|
|
struct State {
|
|
|
|
streamsynchronizer: gst::Element,
|
|
|
|
concat_audio: Vec<gst::Element>,
|
|
|
|
concat_video: Vec<gst::Element>,
|
|
|
|
concat_text: Vec<gst::Element>,
|
|
|
|
|
|
|
|
playlist: Playlist,
|
|
|
|
|
|
|
|
/// the current number of streams handled by the element
|
|
|
|
streams_topology: StreamsTopology,
|
2022-04-08 09:21:42 +00:00
|
|
|
status: Status,
|
2021-10-15 14:57:55 +00:00
|
|
|
|
|
|
|
// we have max one item in one of each of those states
|
|
|
|
waiting_for_stream_collection: Option<Item>,
|
|
|
|
waiting_for_ss_eos: Option<Item>,
|
|
|
|
waiting_for_pads: Option<Item>,
|
|
|
|
blocked: Option<Item>,
|
|
|
|
// multiple items can be streaming, `concat` elements will block them all but the active one
|
|
|
|
streaming: Vec<Item>,
|
|
|
|
// items which have been fully played, waiting to be cleaned up
|
|
|
|
done: Vec<Item>,
|
2021-12-22 10:38:43 +00:00
|
|
|
|
|
|
|
// read-only properties
|
|
|
|
current_iteration: u32,
|
|
|
|
current_uri_index: u64,
|
2021-10-15 14:57:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl State {
|
|
|
|
fn new(uris: Vec<String>, iterations: u32, streamsynchronizer: gst::Element) -> Self {
|
|
|
|
Self {
|
|
|
|
concat_audio: vec![],
|
|
|
|
concat_video: vec![],
|
|
|
|
concat_text: vec![],
|
|
|
|
streamsynchronizer,
|
|
|
|
playlist: Playlist::new(uris, iterations),
|
|
|
|
streams_topology: StreamsTopology::default(),
|
2022-04-08 09:21:42 +00:00
|
|
|
status: Status::Running,
|
2021-10-15 14:57:55 +00:00
|
|
|
waiting_for_stream_collection: None,
|
|
|
|
waiting_for_ss_eos: None,
|
|
|
|
waiting_for_pads: None,
|
|
|
|
blocked: None,
|
|
|
|
streaming: vec![],
|
|
|
|
done: vec![],
|
2021-12-22 10:38:43 +00:00
|
|
|
current_iteration: 0,
|
|
|
|
current_uri_index: 0,
|
2021-10-15 14:57:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return the item whose decodebin is either `src` or an ancestor of `src`
|
|
|
|
fn find_item_from_src(&self, src: &gst::Object) -> Option<Item> {
|
|
|
|
// iterate in all the places we store `Item`, ordering does not matter
|
|
|
|
// as one decodebin element can be in only one Item.
|
|
|
|
let mut items = self
|
|
|
|
.waiting_for_stream_collection
|
|
|
|
.iter()
|
|
|
|
.chain(self.waiting_for_ss_eos.iter())
|
|
|
|
.chain(self.waiting_for_pads.iter())
|
|
|
|
.chain(self.blocked.iter())
|
|
|
|
.chain(self.streaming.iter())
|
|
|
|
.chain(self.done.iter());
|
|
|
|
|
|
|
|
items
|
|
|
|
.find(|item| {
|
|
|
|
let decodebin = item.uridecodebin();
|
|
|
|
let from_decodebin = src == &decodebin;
|
|
|
|
let bin = decodebin.downcast_ref::<gst::Bin>().unwrap();
|
|
|
|
from_decodebin || src.has_as_ancestor(bin)
|
|
|
|
})
|
|
|
|
.cloned()
|
|
|
|
}
|
|
|
|
|
2022-10-09 13:06:59 +00:00
|
|
|
fn unblock_item(&mut self, imp: &UriPlaylistBin) {
|
2021-10-15 14:57:55 +00:00
|
|
|
if let Some(blocked) = self.blocked.take() {
|
2022-04-07 12:19:56 +00:00
|
|
|
let (messages, channels) = blocked.set_streaming(self.streams_topology.n_streams());
|
2021-10-15 14:57:55 +00:00
|
|
|
|
2022-02-21 17:43:46 +00:00
|
|
|
gst::log!(
|
2021-10-15 14:57:55 +00:00
|
|
|
CAT,
|
2022-10-09 13:06:59 +00:00
|
|
|
imp: imp,
|
2021-10-15 14:57:55 +00:00
|
|
|
"send pending message of item #{} and unblock its pads",
|
|
|
|
blocked.index()
|
|
|
|
);
|
|
|
|
|
|
|
|
// send pending messages then unblock pads
|
|
|
|
for msg in messages {
|
2022-10-09 13:06:59 +00:00
|
|
|
let _ = imp.instance().post_message(msg);
|
2021-10-15 14:57:55 +00:00
|
|
|
}
|
2022-04-07 12:19:56 +00:00
|
|
|
|
|
|
|
channels.send(true);
|
2021-10-15 14:57:55 +00:00
|
|
|
|
|
|
|
self.streaming.push(blocked);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
pub struct UriPlaylistBin {
|
|
|
|
settings: Mutex<Settings>,
|
|
|
|
state: Mutex<Option<State>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
enum ItemState {
|
|
|
|
/// Waiting to create a decodebin element
|
|
|
|
Pending,
|
|
|
|
/// Waiting to receive the stream collection from its decodebin element
|
|
|
|
WaitingForStreamCollection { uridecodebin: gst::Element },
|
|
|
|
/// Waiting for streamsynchronizer to be eos on all its src pads.
|
|
|
|
/// Only used to block item whose streams topology is different from the one
|
|
|
|
/// currently handled by the element. In such case we need to wait for
|
|
|
|
/// streamsynchronizer to be flushed before adding/removing concat elements.
|
|
|
|
WaitingForStreamsynchronizerEos {
|
|
|
|
uridecodebin: gst::Element,
|
|
|
|
/// src pads from decodebin currently blocked
|
|
|
|
decodebin_pads: Vec<gst::Pad>,
|
|
|
|
/// number of streamsynchronizer src pads which are not eos yet
|
|
|
|
waiting_eos: u32,
|
|
|
|
stream_collection_msg: gst::Message,
|
2022-04-07 12:19:56 +00:00
|
|
|
// channels used to block pads flow until streamsynchronizer is eos
|
|
|
|
channels: Channels,
|
2021-10-15 14:57:55 +00:00
|
|
|
},
|
|
|
|
/// Waiting that pads of all the streams have been created on decodebin.
|
|
|
|
/// Required to ensure that streams are plugged to concat in the playlist order.
|
|
|
|
WaitingForPads {
|
|
|
|
uridecodebin: gst::Element,
|
|
|
|
n_pads_pendings: u32,
|
|
|
|
stream_collection_msg: gst::Message,
|
|
|
|
/// concat sink pads which have been requested to handle this item
|
|
|
|
concat_sink_pads: Vec<(gst::Element, gst::Pad)>,
|
2022-04-07 12:19:56 +00:00
|
|
|
// channels used to block pad flow in the Blocked state
|
|
|
|
channels: Channels,
|
2021-10-15 14:57:55 +00:00
|
|
|
},
|
|
|
|
/// Pads have been linked to `concat` elements but are blocked until the next item is linked to `concat` as well.
|
|
|
|
/// This is required to ensure gap-less transition between items.
|
|
|
|
Blocked {
|
|
|
|
uridecodebin: gst::Element,
|
|
|
|
stream_collection_msg: gst::Message,
|
|
|
|
stream_selected_msg: Option<gst::Message>,
|
|
|
|
concat_sink_pads: Vec<(gst::Element, gst::Pad)>,
|
2022-04-07 12:19:56 +00:00
|
|
|
channels: Channels,
|
2021-10-15 14:57:55 +00:00
|
|
|
},
|
|
|
|
/// Buffers are flowing
|
|
|
|
Streaming {
|
|
|
|
uridecodebin: gst::Element,
|
|
|
|
concat_sink_pads: Vec<(gst::Element, gst::Pad)>,
|
|
|
|
// number of pads which are not eos yet
|
|
|
|
waiting_eos: u32,
|
|
|
|
},
|
|
|
|
/// Item has been fully streamed
|
|
|
|
Done {
|
|
|
|
uridecodebin: gst::Element,
|
|
|
|
concat_sink_pads: Vec<(gst::Element, gst::Pad)>,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
struct Item {
|
|
|
|
inner: Arc<Mutex<ItemInner>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Item {
|
|
|
|
fn new(uri: String, index: usize) -> Self {
|
|
|
|
let inner = ItemInner {
|
|
|
|
uri,
|
|
|
|
index,
|
|
|
|
state: ItemState::Pending,
|
|
|
|
};
|
|
|
|
|
|
|
|
Self {
|
|
|
|
inner: Arc::new(Mutex::new(inner)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn uri(&self) -> String {
|
|
|
|
let inner = self.inner.lock().unwrap();
|
|
|
|
inner.uri.clone()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn index(&self) -> usize {
|
|
|
|
let inner = self.inner.lock().unwrap();
|
|
|
|
inner.index
|
|
|
|
}
|
|
|
|
|
|
|
|
fn uridecodebin(&self) -> gst::Element {
|
|
|
|
let inner = self.inner.lock().unwrap();
|
|
|
|
|
|
|
|
match &inner.state {
|
|
|
|
ItemState::WaitingForStreamCollection { uridecodebin }
|
|
|
|
| ItemState::WaitingForStreamsynchronizerEos { uridecodebin, .. }
|
|
|
|
| ItemState::WaitingForPads { uridecodebin, .. }
|
|
|
|
| ItemState::Blocked { uridecodebin, .. }
|
|
|
|
| ItemState::Streaming { uridecodebin, .. }
|
|
|
|
| ItemState::Done { uridecodebin, .. } => uridecodebin.clone(),
|
2022-02-22 16:29:02 +00:00
|
|
|
_ => panic!("invalid state: {:?}", inner.state),
|
2021-10-15 14:57:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn concat_sink_pads(&self) -> Vec<(gst::Element, gst::Pad)> {
|
|
|
|
let inner = self.inner.lock().unwrap();
|
|
|
|
|
|
|
|
match &inner.state {
|
|
|
|
ItemState::WaitingForPads {
|
|
|
|
concat_sink_pads, ..
|
|
|
|
}
|
|
|
|
| ItemState::Blocked {
|
|
|
|
concat_sink_pads, ..
|
|
|
|
}
|
|
|
|
| ItemState::Streaming {
|
|
|
|
concat_sink_pads, ..
|
|
|
|
}
|
|
|
|
| ItemState::Done {
|
|
|
|
concat_sink_pads, ..
|
|
|
|
} => concat_sink_pads.clone(),
|
2022-02-22 16:29:02 +00:00
|
|
|
_ => panic!("invalid state: {:?}", inner.state),
|
2021-10-15 14:57:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn dec_n_pads_pending(&self) -> u32 {
|
|
|
|
let mut inner = self.inner.lock().unwrap();
|
|
|
|
|
|
|
|
match &mut inner.state {
|
|
|
|
ItemState::WaitingForPads {
|
|
|
|
n_pads_pendings, ..
|
|
|
|
} => {
|
|
|
|
*n_pads_pendings -= 1;
|
|
|
|
*n_pads_pendings
|
|
|
|
}
|
2022-02-22 16:29:02 +00:00
|
|
|
_ => panic!("invalid state: {:?}", inner.state),
|
2021-10-15 14:57:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-07 12:19:56 +00:00
|
|
|
fn receiver(&self) -> mpsc::Receiver<bool> {
|
|
|
|
let mut inner = self.inner.lock().unwrap();
|
2021-10-15 14:57:55 +00:00
|
|
|
|
2022-04-07 12:19:56 +00:00
|
|
|
let channels = match &mut inner.state {
|
|
|
|
ItemState::WaitingForPads { channels, .. } => channels,
|
|
|
|
ItemState::WaitingForStreamsynchronizerEos { channels, .. } => channels,
|
2021-10-15 14:57:55 +00:00
|
|
|
// receiver is no longer supposed to be accessed once in the `Blocked` state
|
2022-02-22 16:29:02 +00:00
|
|
|
_ => panic!("invalid state: {:?}", inner.state),
|
2022-04-07 12:19:56 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
channels.get_receiver()
|
2021-10-15 14:57:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn add_blocked_pad(&self, pad: gst::Pad) {
|
|
|
|
let mut inner = self.inner.lock().unwrap();
|
|
|
|
|
|
|
|
match &mut inner.state {
|
|
|
|
ItemState::WaitingForStreamsynchronizerEos { decodebin_pads, .. } => {
|
|
|
|
decodebin_pads.push(pad);
|
|
|
|
}
|
2022-02-22 16:29:02 +00:00
|
|
|
_ => panic!("invalid state: {:?}", inner.state),
|
2021-10-15 14:57:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// decrement waiting_eos on a WaitingForStreamsynchronizeEos item, returns if all the streams are now eos or not
|
|
|
|
fn dec_waiting_eos_ss(&self) -> bool {
|
|
|
|
let mut inner = self.inner.lock().unwrap();
|
|
|
|
|
|
|
|
match &mut inner.state {
|
|
|
|
ItemState::WaitingForStreamsynchronizerEos { waiting_eos, .. } => {
|
|
|
|
*waiting_eos -= 1;
|
|
|
|
*waiting_eos == 0
|
|
|
|
}
|
2022-02-22 16:29:02 +00:00
|
|
|
_ => panic!("invalid state: {:?}", inner.state),
|
2021-10-15 14:57:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_streaming(&self) -> bool {
|
|
|
|
let inner = self.inner.lock().unwrap();
|
|
|
|
|
|
|
|
matches!(&inner.state, ItemState::Streaming { .. })
|
|
|
|
}
|
|
|
|
|
|
|
|
/// queue the stream-selected message of a blocked item
|
|
|
|
fn add_stream_selected(&self, msg: gst::Message) {
|
|
|
|
let mut inner = self.inner.lock().unwrap();
|
|
|
|
|
|
|
|
match &mut inner.state {
|
|
|
|
ItemState::Blocked {
|
|
|
|
stream_selected_msg,
|
|
|
|
..
|
|
|
|
} => {
|
|
|
|
*stream_selected_msg = Some(msg);
|
|
|
|
}
|
2022-02-22 16:29:02 +00:00
|
|
|
_ => panic!("invalid state: {:?}", inner.state),
|
2021-10-15 14:57:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// decrement waiting_eos on a Streaming item, returns if all the streams are now eos or not
|
|
|
|
fn dec_waiting_eos(&self) -> bool {
|
|
|
|
let mut inner = self.inner.lock().unwrap();
|
|
|
|
|
|
|
|
match &mut inner.state {
|
|
|
|
ItemState::Streaming { waiting_eos, .. } => {
|
|
|
|
*waiting_eos -= 1;
|
|
|
|
*waiting_eos == 0
|
|
|
|
}
|
2022-02-22 16:29:02 +00:00
|
|
|
_ => panic!("invalid state: {:?}", inner.state),
|
2021-10-15 14:57:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn add_concat_sink_pad(&self, concat: &gst::Element, sink_pad: &gst::Pad) {
|
|
|
|
let mut inner = self.inner.lock().unwrap();
|
|
|
|
|
|
|
|
match &mut inner.state {
|
|
|
|
ItemState::WaitingForPads {
|
|
|
|
concat_sink_pads, ..
|
|
|
|
} => {
|
|
|
|
concat_sink_pads.push((concat.clone(), sink_pad.clone()));
|
|
|
|
}
|
2022-02-22 16:29:02 +00:00
|
|
|
_ => panic!("invalid state: {:?}", inner.state),
|
2021-10-15 14:57:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// change state methods
|
|
|
|
|
|
|
|
// from the Pending state, called when starting to process the item
|
|
|
|
fn set_waiting_for_stream_collection(&self) -> Result<(), PlaylistError> {
|
|
|
|
let mut inner = self.inner.lock().unwrap();
|
|
|
|
|
|
|
|
let uridecodebin = gst::ElementFactory::make(
|
|
|
|
"uridecodebin3",
|
|
|
|
Some(&format!("playlist-decodebin-{}", inner.index)),
|
|
|
|
)
|
|
|
|
.map_err(|e| PlaylistError::PluginMissing { error: e.into() })?;
|
|
|
|
uridecodebin.set_property("uri", &inner.uri);
|
|
|
|
|
|
|
|
assert!(matches!(inner.state, ItemState::Pending));
|
|
|
|
inner.state = ItemState::WaitingForStreamCollection { uridecodebin };
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
// from the WaitingForStreamCollection state, called when we received the item stream collection
|
|
|
|
// and its stream topology matches what is currently being processed by the element.
|
2022-01-19 13:07:45 +00:00
|
|
|
fn set_waiting_for_pads(&self, n_streams: u32, msg: &gst::message::StreamCollection) {
|
2021-10-15 14:57:55 +00:00
|
|
|
let mut inner = self.inner.lock().unwrap();
|
|
|
|
assert!(matches!(
|
|
|
|
inner.state,
|
|
|
|
ItemState::WaitingForStreamCollection { .. }
|
|
|
|
));
|
|
|
|
|
|
|
|
match &inner.state {
|
|
|
|
ItemState::WaitingForStreamCollection { uridecodebin } => {
|
|
|
|
inner.state = ItemState::WaitingForPads {
|
|
|
|
uridecodebin: uridecodebin.clone(),
|
|
|
|
n_pads_pendings: n_streams,
|
|
|
|
stream_collection_msg: msg.copy(),
|
|
|
|
concat_sink_pads: vec![],
|
2022-04-07 12:19:56 +00:00
|
|
|
channels: Channels::default(),
|
2021-10-15 14:57:55 +00:00
|
|
|
};
|
|
|
|
}
|
2022-02-22 16:29:02 +00:00
|
|
|
_ => panic!("invalid state: {:?}", inner.state),
|
2021-10-15 14:57:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// from the WaitingForStreamCollection state, called when we received the item stream collection
|
|
|
|
// but its stream topology does not match what is currently being processed by the element,
|
|
|
|
// having to wait until streamsynchronizer is flushed to internally reorganize the element.
|
2022-01-19 13:07:45 +00:00
|
|
|
fn set_waiting_for_ss_eos(&self, waiting_eos: u32, msg: &gst::message::StreamCollection) {
|
2021-10-15 14:57:55 +00:00
|
|
|
let mut inner = self.inner.lock().unwrap();
|
|
|
|
|
|
|
|
match &inner.state {
|
|
|
|
ItemState::WaitingForStreamCollection { uridecodebin } => {
|
|
|
|
inner.state = ItemState::WaitingForStreamsynchronizerEos {
|
|
|
|
uridecodebin: uridecodebin.clone(),
|
|
|
|
decodebin_pads: vec![],
|
|
|
|
waiting_eos,
|
|
|
|
// FIXME: save deep copy once https://gitlab.freedesktop.org/gstreamer/gstreamer-rs/-/issues/363 is fixed
|
|
|
|
stream_collection_msg: msg.copy(),
|
2022-04-07 12:19:56 +00:00
|
|
|
channels: Channels::default(),
|
2021-10-15 14:57:55 +00:00
|
|
|
};
|
|
|
|
}
|
2022-02-22 16:29:02 +00:00
|
|
|
_ => panic!("invalid state: {:?}", inner.state),
|
2021-10-15 14:57:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// from the WaitingForStreamsynchronizerEos state, called when the streamsynchronizer has been flushed
|
|
|
|
// and the item can now be processed.
|
2022-04-07 12:19:56 +00:00
|
|
|
fn done_waiting_for_ss_eos(&self) -> (StreamsTopology, Vec<gst::Pad>, Channels) {
|
2021-10-15 14:57:55 +00:00
|
|
|
let mut inner = self.inner.lock().unwrap();
|
|
|
|
|
|
|
|
match &inner.state {
|
|
|
|
ItemState::WaitingForStreamsynchronizerEos {
|
|
|
|
uridecodebin,
|
|
|
|
decodebin_pads,
|
|
|
|
waiting_eos,
|
|
|
|
stream_collection_msg,
|
2022-04-07 12:19:56 +00:00
|
|
|
channels,
|
2021-10-15 14:57:55 +00:00
|
|
|
..
|
|
|
|
} => {
|
|
|
|
assert_eq!(*waiting_eos, 0);
|
|
|
|
|
|
|
|
let topology = match stream_collection_msg.view() {
|
|
|
|
gst::MessageView::StreamCollection(stream_collection_msg) => {
|
|
|
|
StreamsTopology::from(stream_collection_msg.stream_collection())
|
|
|
|
}
|
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
|
|
|
let pending_pads = decodebin_pads.clone();
|
2022-04-07 12:19:56 +00:00
|
|
|
let channels = channels.clone();
|
2021-10-15 14:57:55 +00:00
|
|
|
|
|
|
|
inner.state = ItemState::WaitingForPads {
|
|
|
|
uridecodebin: uridecodebin.clone(),
|
|
|
|
n_pads_pendings: topology.n_streams(),
|
|
|
|
stream_collection_msg: stream_collection_msg.copy(),
|
|
|
|
concat_sink_pads: vec![],
|
2022-04-07 12:19:56 +00:00
|
|
|
channels: Channels::default(),
|
2021-10-15 14:57:55 +00:00
|
|
|
};
|
|
|
|
|
2022-04-07 12:19:56 +00:00
|
|
|
(topology, pending_pads, channels)
|
2021-10-15 14:57:55 +00:00
|
|
|
}
|
2022-02-22 16:29:02 +00:00
|
|
|
_ => panic!("invalid state: {:?}", inner.state),
|
2021-10-15 14:57:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// from the WaitingForPads state, called when all the pads from decodebin have been added and connected to concat elements.
|
|
|
|
fn set_blocked(&self) {
|
|
|
|
let mut inner = self.inner.lock().unwrap();
|
|
|
|
|
|
|
|
match &mut inner.state {
|
|
|
|
ItemState::WaitingForPads {
|
|
|
|
uridecodebin,
|
2022-04-07 12:19:56 +00:00
|
|
|
channels,
|
2021-10-15 14:57:55 +00:00
|
|
|
stream_collection_msg,
|
|
|
|
concat_sink_pads,
|
|
|
|
..
|
|
|
|
} => {
|
|
|
|
inner.state = ItemState::Blocked {
|
|
|
|
uridecodebin: uridecodebin.clone(),
|
2022-04-07 12:19:56 +00:00
|
|
|
channels: channels.clone(),
|
2021-10-15 14:57:55 +00:00
|
|
|
concat_sink_pads: concat_sink_pads.clone(),
|
|
|
|
stream_collection_msg: stream_collection_msg.copy(),
|
|
|
|
stream_selected_msg: None,
|
|
|
|
};
|
|
|
|
}
|
2022-02-22 16:29:02 +00:00
|
|
|
_ => panic!("invalid state: {:?}", inner.state),
|
2021-10-15 14:57:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// from the Blocked state, called when the item streaming threads can be unblocked.
|
|
|
|
// Return the queued messages from this item and the sender to unblock their pads
|
2022-04-07 12:19:56 +00:00
|
|
|
fn set_streaming(&self, n_streams: u32) -> (Vec<gst::Message>, Channels) {
|
2021-10-15 14:57:55 +00:00
|
|
|
let mut inner = self.inner.lock().unwrap();
|
|
|
|
|
|
|
|
match &mut inner.state {
|
|
|
|
ItemState::Blocked {
|
|
|
|
uridecodebin,
|
2022-04-07 12:19:56 +00:00
|
|
|
channels,
|
2021-10-15 14:57:55 +00:00
|
|
|
stream_collection_msg,
|
|
|
|
stream_selected_msg,
|
|
|
|
concat_sink_pads,
|
|
|
|
..
|
|
|
|
} => {
|
|
|
|
let mut messages = vec![stream_collection_msg.copy()];
|
|
|
|
if let Some(msg) = stream_selected_msg {
|
|
|
|
messages.push(msg.copy());
|
|
|
|
}
|
2022-04-07 12:19:56 +00:00
|
|
|
let channels = channels.clone();
|
2021-10-15 14:57:55 +00:00
|
|
|
|
|
|
|
inner.state = ItemState::Streaming {
|
|
|
|
uridecodebin: uridecodebin.clone(),
|
|
|
|
waiting_eos: n_streams,
|
|
|
|
concat_sink_pads: concat_sink_pads.clone(),
|
|
|
|
};
|
|
|
|
|
2022-04-07 12:19:56 +00:00
|
|
|
(messages, channels)
|
2021-10-15 14:57:55 +00:00
|
|
|
}
|
2022-02-22 16:29:02 +00:00
|
|
|
_ => panic!("invalid state: {:?}", inner.state),
|
2021-10-15 14:57:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// from the Streaming state, called when the item has been fully processed and can be cleaned up
|
|
|
|
fn set_done(&self) {
|
|
|
|
let mut inner = self.inner.lock().unwrap();
|
|
|
|
|
|
|
|
match &mut inner.state {
|
|
|
|
ItemState::Streaming {
|
|
|
|
uridecodebin,
|
|
|
|
concat_sink_pads,
|
|
|
|
..
|
|
|
|
} => {
|
|
|
|
inner.state = ItemState::Done {
|
|
|
|
uridecodebin: uridecodebin.clone(),
|
|
|
|
concat_sink_pads: concat_sink_pads.clone(),
|
|
|
|
};
|
|
|
|
}
|
2022-02-22 16:29:02 +00:00
|
|
|
_ => panic!("invalid state: {:?}", inner.state),
|
2021-10-15 14:57:55 +00:00
|
|
|
}
|
|
|
|
}
|
2022-03-25 14:52:08 +00:00
|
|
|
|
2022-04-07 12:19:56 +00:00
|
|
|
fn channels(&self) -> Channels {
|
2022-03-25 14:52:08 +00:00
|
|
|
let inner = self.inner.lock().unwrap();
|
|
|
|
|
|
|
|
match &inner.state {
|
2022-04-07 12:19:56 +00:00
|
|
|
ItemState::WaitingForStreamsynchronizerEos { channels, .. } => channels.clone(),
|
|
|
|
ItemState::WaitingForPads { channels, .. } => channels.clone(),
|
|
|
|
ItemState::Blocked { channels, .. } => channels.clone(),
|
2022-03-25 14:52:08 +00:00
|
|
|
_ => panic!("invalid state: {:?}", inner.state),
|
|
|
|
}
|
|
|
|
}
|
2022-04-08 13:33:00 +00:00
|
|
|
|
|
|
|
fn downgrade(&self) -> Weak<Mutex<ItemInner>> {
|
|
|
|
Arc::downgrade(&self.inner)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn upgrade(weak: &Weak<Mutex<ItemInner>>) -> Option<Item> {
|
|
|
|
weak.upgrade().map(|inner| Item { inner })
|
|
|
|
}
|
2021-10-15 14:57:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
struct ItemInner {
|
|
|
|
uri: String,
|
|
|
|
index: usize,
|
|
|
|
state: ItemState,
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Playlist {
|
|
|
|
items: Box<dyn Iterator<Item = Item> + Send>,
|
2021-12-22 11:52:07 +00:00
|
|
|
|
|
|
|
uris: Vec<String>,
|
2021-10-15 14:57:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Playlist {
|
|
|
|
fn new(uris: Vec<String>, iterations: u32) -> Self {
|
2021-12-22 11:49:30 +00:00
|
|
|
Self {
|
2021-12-22 11:52:07 +00:00
|
|
|
items: Self::create_items(uris.clone(), iterations),
|
|
|
|
uris,
|
2021-12-22 11:49:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn create_items(
|
|
|
|
uris: Vec<String>,
|
|
|
|
iterations: u32,
|
|
|
|
) -> Box<dyn Iterator<Item = Item> + Send + Sync> {
|
2021-10-15 14:57:55 +00:00
|
|
|
fn infinite_iter(uris: Vec<String>) -> Box<dyn Iterator<Item = Item> + Send + Sync> {
|
|
|
|
Box::new(
|
|
|
|
uris.into_iter()
|
|
|
|
.cycle()
|
|
|
|
.enumerate()
|
|
|
|
.map(|(index, uri)| Item::new(uri, index)),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
fn finite_iter(
|
|
|
|
uris: Vec<String>,
|
|
|
|
iterations: u32,
|
|
|
|
) -> Box<dyn Iterator<Item = Item> + Send + Sync> {
|
|
|
|
let n = (iterations as usize)
|
|
|
|
.checked_mul(uris.len())
|
|
|
|
.unwrap_or(usize::MAX);
|
|
|
|
|
|
|
|
Box::new(
|
|
|
|
uris.into_iter()
|
|
|
|
.cycle()
|
|
|
|
.take(n)
|
|
|
|
.enumerate()
|
|
|
|
.map(|(index, uri)| Item::new(uri, index)),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-12-22 11:49:30 +00:00
|
|
|
if iterations == 0 {
|
2021-10-15 14:57:55 +00:00
|
|
|
infinite_iter(uris)
|
|
|
|
} else {
|
|
|
|
finite_iter(uris, iterations)
|
2021-12-22 11:49:30 +00:00
|
|
|
}
|
2021-10-15 14:57:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn next(&mut self) -> Result<Option<Item>, PlaylistError> {
|
|
|
|
let item = match self.items.next() {
|
|
|
|
None => return Ok(None),
|
|
|
|
Some(item) => item,
|
|
|
|
};
|
|
|
|
|
2021-12-22 11:52:07 +00:00
|
|
|
if item.index() == usize::MAX {
|
|
|
|
// prevent overflow with infinite playlist
|
|
|
|
self.items = Self::create_items(self.uris.clone(), 0);
|
|
|
|
}
|
|
|
|
|
2021-10-15 14:57:55 +00:00
|
|
|
item.set_waiting_for_stream_collection()?;
|
|
|
|
|
|
|
|
Ok(Some(item))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-22 09:49:49 +00:00
|
|
|
impl std::fmt::Debug for Playlist {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
f.debug_struct("Playlist")
|
|
|
|
.field("uris", &self.uris)
|
|
|
|
.finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-15 14:57:55 +00:00
|
|
|
fn stream_type_from_pad_name(name: &str) -> anyhow::Result<(gst::StreamType, usize)> {
|
|
|
|
if let Some(index) = name.strip_prefix("audio_") {
|
|
|
|
Ok((gst::StreamType::AUDIO, index.parse().unwrap()))
|
|
|
|
} else if let Some(index) = name.strip_prefix("video_") {
|
|
|
|
Ok((gst::StreamType::VIDEO, index.parse().unwrap()))
|
|
|
|
} else if let Some(index) = name.strip_prefix("text_") {
|
|
|
|
Ok((gst::StreamType::TEXT, index.parse().unwrap()))
|
|
|
|
} else {
|
|
|
|
Err(anyhow::anyhow!("type of pad {} not supported", name))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[glib::object_subclass]
|
|
|
|
impl ObjectSubclass for UriPlaylistBin {
|
|
|
|
const NAME: &'static str = "GstUriPlaylistBin";
|
|
|
|
type Type = super::UriPlaylistBin;
|
|
|
|
type ParentType = gst::Bin;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ObjectImpl for UriPlaylistBin {
|
|
|
|
fn properties() -> &'static [glib::ParamSpec] {
|
|
|
|
static PROPERTIES: Lazy<Vec<glib::ParamSpec>> = Lazy::new(|| {
|
|
|
|
vec![
|
2022-09-05 08:45:47 +00:00
|
|
|
glib::ParamSpecBoxed::builder::<Vec<String>>("uris")
|
2022-08-18 12:04:15 +00:00
|
|
|
.nick("URIs")
|
|
|
|
.blurb("URIs of the medias to play")
|
|
|
|
.mutable_ready()
|
|
|
|
.build(),
|
|
|
|
glib::ParamSpecUInt::builder("iterations")
|
|
|
|
.nick("Iterations")
|
|
|
|
.blurb("Number of time the playlist items should be played each (0 = unlimited)")
|
|
|
|
.default_value(1)
|
|
|
|
.mutable_ready()
|
|
|
|
.build(),
|
|
|
|
glib::ParamSpecUInt::builder("current-iteration")
|
|
|
|
.nick("Current iteration")
|
|
|
|
.blurb("The index of the current playlist iteration, or 0 if the iterations property is 0 (unlimited playlist)")
|
|
|
|
.read_only()
|
|
|
|
.build(),
|
|
|
|
glib::ParamSpecUInt64::builder("current-uri-index")
|
|
|
|
.nick("Current URI")
|
|
|
|
.blurb("The index from the uris property of the current URI being played")
|
|
|
|
.read_only()
|
|
|
|
.build(),
|
2021-10-15 14:57:55 +00:00
|
|
|
]
|
|
|
|
});
|
|
|
|
|
|
|
|
PROPERTIES.as_ref()
|
|
|
|
}
|
|
|
|
|
2022-10-09 13:06:59 +00:00
|
|
|
fn set_property(&self, _id: usize, value: &glib::Value, pspec: &glib::ParamSpec) {
|
2021-10-15 14:57:55 +00:00
|
|
|
match pspec.name() {
|
|
|
|
"uris" => {
|
|
|
|
let mut settings = self.settings.lock().unwrap();
|
|
|
|
let new_value = value.get().expect("type checked upstream");
|
2022-02-21 17:43:46 +00:00
|
|
|
gst::info!(
|
2021-10-15 14:57:55 +00:00
|
|
|
CAT,
|
2022-10-09 13:06:59 +00:00
|
|
|
imp: self,
|
2021-10-15 14:57:55 +00:00
|
|
|
"Changing uris from {:?} to {:?}",
|
|
|
|
settings.uris,
|
|
|
|
new_value,
|
|
|
|
);
|
|
|
|
settings.uris = new_value;
|
|
|
|
}
|
|
|
|
"iterations" => {
|
|
|
|
let mut settings = self.settings.lock().unwrap();
|
|
|
|
let new_value = value.get().expect("type checked upstream");
|
2022-02-21 17:43:46 +00:00
|
|
|
gst::info!(
|
2021-10-15 14:57:55 +00:00
|
|
|
CAT,
|
2022-10-09 13:06:59 +00:00
|
|
|
imp: self,
|
2021-10-15 14:57:55 +00:00
|
|
|
"Changing iterations from {:?} to {:?}",
|
|
|
|
settings.iterations,
|
|
|
|
new_value,
|
|
|
|
);
|
|
|
|
settings.iterations = new_value;
|
|
|
|
}
|
|
|
|
_ => unimplemented!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-09 13:06:59 +00:00
|
|
|
fn property(&self, _id: usize, pspec: &glib::ParamSpec) -> glib::Value {
|
2021-10-15 14:57:55 +00:00
|
|
|
match pspec.name() {
|
|
|
|
"uris" => {
|
|
|
|
let settings = self.settings.lock().unwrap();
|
|
|
|
settings.uris.to_value()
|
|
|
|
}
|
|
|
|
"iterations" => {
|
|
|
|
let settings = self.settings.lock().unwrap();
|
|
|
|
settings.iterations.to_value()
|
|
|
|
}
|
2021-12-22 10:38:43 +00:00
|
|
|
"current-iteration" => {
|
|
|
|
let state = self.state.lock().unwrap();
|
|
|
|
state
|
|
|
|
.as_ref()
|
|
|
|
.map(|state| state.current_iteration)
|
|
|
|
.unwrap_or(0)
|
|
|
|
.to_value()
|
|
|
|
}
|
|
|
|
"current-uri-index" => {
|
|
|
|
let state = self.state.lock().unwrap();
|
|
|
|
state
|
|
|
|
.as_ref()
|
|
|
|
.map(|state| state.current_uri_index)
|
|
|
|
.unwrap_or(0)
|
|
|
|
.to_value()
|
|
|
|
}
|
2021-10-15 14:57:55 +00:00
|
|
|
_ => unimplemented!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-09 13:06:59 +00:00
|
|
|
fn constructed(&self) {
|
|
|
|
self.parent_constructed();
|
2021-10-15 14:57:55 +00:00
|
|
|
|
2022-10-09 13:06:59 +00:00
|
|
|
let obj = self.instance();
|
2021-10-15 14:57:55 +00:00
|
|
|
obj.set_suppressed_flags(gst::ElementFlags::SOURCE | gst::ElementFlags::SINK);
|
|
|
|
obj.set_element_flags(gst::ElementFlags::SOURCE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl GstObjectImpl for UriPlaylistBin {}
|
|
|
|
|
|
|
|
impl BinImpl for UriPlaylistBin {
|
2022-10-09 13:06:59 +00:00
|
|
|
fn handle_message(&self, msg: gst::Message) {
|
2021-10-15 14:57:55 +00:00
|
|
|
match msg.view() {
|
|
|
|
gst::MessageView::StreamCollection(stream_collection_msg) => {
|
2022-10-09 13:06:59 +00:00
|
|
|
if let Err(e) = self.handle_stream_collection(stream_collection_msg) {
|
|
|
|
self.failed(e);
|
2021-10-15 14:57:55 +00:00
|
|
|
}
|
|
|
|
// stream collection will be send when the item starts streaming
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
gst::MessageView::StreamsSelected(stream_selected) => {
|
2022-10-09 13:06:59 +00:00
|
|
|
if !self.handle_stream_selected(stream_selected) {
|
2021-10-15 14:57:55 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
gst::MessageView::Error(error) => {
|
|
|
|
// find item which raised the error
|
2022-10-09 13:06:59 +00:00
|
|
|
let mut state_guard = self.state.lock().unwrap();
|
2021-10-15 14:57:55 +00:00
|
|
|
let state = state_guard.as_mut().unwrap();
|
|
|
|
|
|
|
|
let src = error.src().unwrap();
|
|
|
|
let item = state.find_item_from_src(&src);
|
|
|
|
|
|
|
|
drop(state_guard);
|
|
|
|
|
|
|
|
if let Some(item) = item {
|
|
|
|
// handle the error message so we can add the failing uri as error details
|
|
|
|
|
2022-10-09 13:06:59 +00:00
|
|
|
self.failed(PlaylistError::ItemFailed {
|
|
|
|
error: anyhow::anyhow!(
|
|
|
|
"Error when processing item #{} ({}): {}",
|
|
|
|
item.index(),
|
|
|
|
item.uri(),
|
|
|
|
error.error().to_string()
|
|
|
|
),
|
|
|
|
item,
|
|
|
|
});
|
2021-10-15 14:57:55 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
|
2022-10-09 13:06:59 +00:00
|
|
|
self.parent_handle_message(msg)
|
2021-10-15 14:57:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ElementImpl for UriPlaylistBin {
|
|
|
|
fn metadata() -> Option<&'static gst::subclass::ElementMetadata> {
|
|
|
|
static ELEMENT_METADATA: Lazy<gst::subclass::ElementMetadata> = Lazy::new(|| {
|
|
|
|
gst::subclass::ElementMetadata::new(
|
|
|
|
"Playlist Source",
|
|
|
|
"Generic/Source",
|
|
|
|
"Sequentially play uri streams",
|
|
|
|
"Guillaume Desmottes <guillaume.desmottes@onestream.live>",
|
|
|
|
)
|
|
|
|
});
|
|
|
|
|
|
|
|
Some(&*ELEMENT_METADATA)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn pad_templates() -> &'static [gst::PadTemplate] {
|
|
|
|
static PAD_TEMPLATES: Lazy<Vec<gst::PadTemplate>> = Lazy::new(|| {
|
|
|
|
let audio_src_pad_template = gst::PadTemplate::new(
|
|
|
|
"audio_%u",
|
|
|
|
gst::PadDirection::Src,
|
|
|
|
gst::PadPresence::Sometimes,
|
|
|
|
&gst::Caps::new_any(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let video_src_pad_template = gst::PadTemplate::new(
|
|
|
|
"video_%u",
|
|
|
|
gst::PadDirection::Src,
|
|
|
|
gst::PadPresence::Sometimes,
|
|
|
|
&gst::Caps::new_any(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let text_src_pad_template = gst::PadTemplate::new(
|
|
|
|
"text_%u",
|
|
|
|
gst::PadDirection::Src,
|
|
|
|
gst::PadPresence::Sometimes,
|
|
|
|
&gst::Caps::new_any(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
vec![
|
|
|
|
audio_src_pad_template,
|
|
|
|
video_src_pad_template,
|
|
|
|
text_src_pad_template,
|
|
|
|
]
|
|
|
|
});
|
|
|
|
|
|
|
|
PAD_TEMPLATES.as_ref()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn change_state(
|
|
|
|
&self,
|
|
|
|
transition: gst::StateChange,
|
|
|
|
) -> Result<gst::StateChangeSuccess, gst::StateChangeError> {
|
|
|
|
if transition == gst::StateChange::NullToReady {
|
2022-10-09 13:06:59 +00:00
|
|
|
if let Err(e) = self.start() {
|
|
|
|
self.failed(e);
|
2021-10-15 14:57:55 +00:00
|
|
|
return Err(gst::StateChangeError);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-25 14:52:08 +00:00
|
|
|
if transition == gst::StateChange::PausedToReady {
|
|
|
|
let mut state_guard = self.state.lock().unwrap();
|
|
|
|
let state = state_guard.as_mut().unwrap();
|
|
|
|
|
2022-04-08 09:23:31 +00:00
|
|
|
state.status = Status::ShuttingDown;
|
|
|
|
|
2022-03-25 14:52:08 +00:00
|
|
|
// The probe callback owns a ref on the item and so on the sender as well.
|
|
|
|
// As a result we have to explicitly unblock all receivers as dropping the sender
|
|
|
|
// is not enough.
|
|
|
|
if let Some(item) = state.waiting_for_ss_eos.take() {
|
2022-04-07 12:19:56 +00:00
|
|
|
item.channels().send(false);
|
2022-03-25 14:52:08 +00:00
|
|
|
}
|
|
|
|
if let Some(item) = state.waiting_for_pads.take() {
|
2022-04-07 12:19:56 +00:00
|
|
|
item.channels().send(false);
|
2022-03-25 14:52:08 +00:00
|
|
|
}
|
|
|
|
if let Some(item) = state.blocked.take() {
|
2022-04-07 12:19:56 +00:00
|
|
|
item.channels().send(false);
|
2022-03-25 14:52:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-09 13:06:59 +00:00
|
|
|
self.parent_change_state(transition)
|
2021-10-15 14:57:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl UriPlaylistBin {
|
2022-10-09 13:06:59 +00:00
|
|
|
fn start(&self) -> Result<(), PlaylistError> {
|
|
|
|
gst::debug!(CAT, imp: self, "Starting");
|
2021-10-15 14:57:55 +00:00
|
|
|
{
|
|
|
|
let mut state_guard = self.state.lock().unwrap();
|
|
|
|
assert!(state_guard.is_none());
|
|
|
|
|
|
|
|
let streamsynchronizer =
|
|
|
|
gst::ElementFactory::make("streamsynchronizer", Some("playlist-streamsync"))
|
|
|
|
.map_err(|e| PlaylistError::PluginMissing { error: e.into() })?;
|
|
|
|
|
2022-10-09 13:06:59 +00:00
|
|
|
self.instance().add(&streamsynchronizer).unwrap();
|
2021-10-15 14:57:55 +00:00
|
|
|
|
|
|
|
let settings = self.settings.lock().unwrap();
|
|
|
|
|
|
|
|
*state_guard = Some(State::new(
|
|
|
|
settings.uris.clone(),
|
|
|
|
settings.iterations,
|
|
|
|
streamsynchronizer,
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2022-10-09 13:06:59 +00:00
|
|
|
self.start_next_item()?;
|
2021-10-15 14:57:55 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-10-09 13:06:59 +00:00
|
|
|
fn start_next_item(&self) -> Result<(), PlaylistError> {
|
2021-10-15 14:57:55 +00:00
|
|
|
let mut state_guard = self.state.lock().unwrap();
|
|
|
|
let state = state_guard.as_mut().unwrap();
|
|
|
|
|
|
|
|
// clean up done items, so uridecodebin elements and concat sink pads don't pile up in the pipeline
|
|
|
|
while let Some(done) = state.done.pop() {
|
|
|
|
let uridecodebin = done.uridecodebin();
|
2022-10-09 13:06:59 +00:00
|
|
|
gst::log!(CAT, imp: self, "remove {} from bin", uridecodebin.name());
|
2021-10-15 14:57:55 +00:00
|
|
|
|
|
|
|
for (concat, sink_pad) in done.concat_sink_pads() {
|
|
|
|
// calling release_request_pad() while holding the pad stream lock would deadlock
|
|
|
|
concat.call_async(move |concat| {
|
|
|
|
concat.release_request_pad(&sink_pad);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// can't change state from the streaming thread
|
2022-10-09 13:06:59 +00:00
|
|
|
uridecodebin.call_async(move |uridecodebin| {
|
|
|
|
let _ = uridecodebin.set_state(gst::State::Null);
|
2021-10-15 14:57:55 +00:00
|
|
|
});
|
|
|
|
|
2022-10-09 13:06:59 +00:00
|
|
|
self.instance().remove(&uridecodebin).unwrap();
|
2021-10-15 14:57:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if state.waiting_for_stream_collection.is_some()
|
|
|
|
|| state.waiting_for_pads.is_some()
|
|
|
|
|| state.waiting_for_ss_eos.is_some()
|
|
|
|
{
|
|
|
|
// another item is being prepared
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
|
|
|
let n_streaming = state.streaming.len();
|
2022-04-22 12:40:18 +00:00
|
|
|
if n_streaming >= MAX_STREAMING_ITEMS {
|
2022-02-21 17:43:46 +00:00
|
|
|
gst::log!(
|
2021-10-15 14:57:55 +00:00
|
|
|
CAT,
|
2022-10-09 13:06:59 +00:00
|
|
|
imp: self,
|
2021-10-15 14:57:55 +00:00
|
|
|
"Too many items streaming ({}), wait before starting the next one",
|
|
|
|
n_streaming
|
|
|
|
);
|
|
|
|
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
|
|
|
let item = match state.playlist.next()? {
|
|
|
|
Some(item) => item,
|
|
|
|
None => {
|
2022-10-09 13:06:59 +00:00
|
|
|
gst::debug!(CAT, imp: self, "no more item to queue",);
|
2021-10-15 14:57:55 +00:00
|
|
|
|
|
|
|
// unblock last item
|
2022-10-09 13:06:59 +00:00
|
|
|
state.unblock_item(self);
|
2021-10-15 14:57:55 +00:00
|
|
|
|
2021-12-22 10:38:43 +00:00
|
|
|
self.update_current(state_guard);
|
|
|
|
|
2021-10-15 14:57:55 +00:00
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-02-21 17:43:46 +00:00
|
|
|
gst::debug!(
|
2021-10-15 14:57:55 +00:00
|
|
|
CAT,
|
2022-10-09 13:06:59 +00:00
|
|
|
imp: self,
|
2021-10-15 14:57:55 +00:00
|
|
|
"start decoding item #{}: {}",
|
|
|
|
item.index(),
|
|
|
|
item.uri()
|
|
|
|
);
|
|
|
|
|
|
|
|
let uridecodebin = item.uridecodebin();
|
|
|
|
|
2022-10-09 13:06:59 +00:00
|
|
|
self.instance().add(&uridecodebin).unwrap();
|
2021-10-15 14:57:55 +00:00
|
|
|
|
|
|
|
let item_clone = item.clone();
|
|
|
|
assert!(state.waiting_for_stream_collection.is_none());
|
|
|
|
state.waiting_for_stream_collection = Some(item);
|
|
|
|
|
2022-10-09 13:06:59 +00:00
|
|
|
uridecodebin.connect_pad_added(move |uridecodebin, src_pad| {
|
|
|
|
let element = match uridecodebin
|
|
|
|
.parent()
|
|
|
|
.and_then(|p| p.downcast::<super::UriPlaylistBin>().ok())
|
|
|
|
{
|
2021-10-15 14:57:55 +00:00
|
|
|
Some(element) => element,
|
|
|
|
None => return,
|
|
|
|
};
|
2022-01-17 18:09:18 +00:00
|
|
|
let imp = element.imp();
|
2022-03-25 13:33:02 +00:00
|
|
|
let mut state_guard = imp.state.lock().unwrap();
|
|
|
|
let state = state_guard.as_mut().unwrap();
|
2021-10-15 14:57:55 +00:00
|
|
|
|
2022-03-25 13:33:02 +00:00
|
|
|
if let Some(item) = state.waiting_for_ss_eos.as_ref() {
|
2021-10-15 14:57:55 +00:00
|
|
|
// block pad until streamsynchronizer is eos
|
2022-10-09 13:06:59 +00:00
|
|
|
let imp_weak = imp.downgrade();
|
2022-04-07 12:19:56 +00:00
|
|
|
let receiver = Mutex::new(item.receiver());
|
2021-10-15 14:57:55 +00:00
|
|
|
|
2022-02-21 17:43:46 +00:00
|
|
|
gst::debug!(
|
2021-10-15 14:57:55 +00:00
|
|
|
CAT,
|
2022-10-09 13:06:59 +00:00
|
|
|
imp: imp,
|
2021-10-15 14:57:55 +00:00
|
|
|
"Block pad {} until streamsynchronizer is flushed",
|
|
|
|
src_pad.name(),
|
|
|
|
);
|
|
|
|
|
|
|
|
src_pad.add_probe(gst::PadProbeType::BLOCK_DOWNSTREAM, move |pad, _info| {
|
2022-10-09 13:06:59 +00:00
|
|
|
let imp = match imp_weak.upgrade() {
|
|
|
|
Some(imp) => imp,
|
2021-10-15 14:57:55 +00:00
|
|
|
None => return gst::PadProbeReturn::Remove,
|
|
|
|
};
|
|
|
|
|
2022-10-09 13:06:59 +00:00
|
|
|
if let Some(parent) = pad.parent() {
|
|
|
|
let receiver = receiver.lock().unwrap();
|
|
|
|
let _ = receiver.recv();
|
|
|
|
|
|
|
|
gst::log!(
|
|
|
|
CAT,
|
|
|
|
imp: imp,
|
|
|
|
"pad {}:{} has been unblocked",
|
|
|
|
parent.name(),
|
|
|
|
pad.name()
|
|
|
|
);
|
|
|
|
}
|
2021-10-15 14:57:55 +00:00
|
|
|
|
|
|
|
gst::PadProbeReturn::Remove
|
|
|
|
});
|
|
|
|
|
|
|
|
item.add_blocked_pad(src_pad.clone());
|
|
|
|
} else {
|
2022-03-25 13:33:02 +00:00
|
|
|
drop(state_guard);
|
2022-01-17 18:09:18 +00:00
|
|
|
imp.process_decodebin_pad(src_pad);
|
2021-10-15 14:57:55 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
drop(state_guard);
|
|
|
|
|
2022-04-08 13:14:51 +00:00
|
|
|
uridecodebin
|
2021-10-15 14:57:55 +00:00
|
|
|
.sync_state_with_parent()
|
|
|
|
.map_err(|e| PlaylistError::ItemFailed {
|
|
|
|
error: e.into(),
|
|
|
|
item: item_clone,
|
|
|
|
})?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn handle_stream_collection(
|
|
|
|
&self,
|
2022-01-19 13:07:45 +00:00
|
|
|
stream_collection_msg: &gst::message::StreamCollection,
|
2021-10-15 14:57:55 +00:00
|
|
|
) -> Result<(), PlaylistError> {
|
|
|
|
let mut state_guard = self.state.lock().unwrap();
|
|
|
|
let state = state_guard.as_mut().unwrap();
|
|
|
|
let src = stream_collection_msg.src().unwrap();
|
|
|
|
|
|
|
|
if let Some(item) = state.waiting_for_stream_collection.clone() {
|
|
|
|
// check message is from the decodebin we are waiting for
|
|
|
|
let uridecodebin = item.uridecodebin();
|
|
|
|
|
|
|
|
if src.has_as_ancestor(&uridecodebin) {
|
|
|
|
let topology = StreamsTopology::from(stream_collection_msg.stream_collection());
|
|
|
|
|
2022-02-21 17:43:46 +00:00
|
|
|
gst::debug!(
|
2021-10-15 14:57:55 +00:00
|
|
|
CAT,
|
2022-10-09 13:06:59 +00:00
|
|
|
imp: self,
|
2021-10-15 14:57:55 +00:00
|
|
|
"got stream collection from {}: {:?}",
|
|
|
|
src.name(),
|
|
|
|
topology
|
|
|
|
);
|
|
|
|
|
|
|
|
if state.streams_topology.n_streams() == 0 {
|
|
|
|
state.streams_topology = topology.clone();
|
|
|
|
}
|
|
|
|
|
|
|
|
assert!(state.waiting_for_pads.is_none());
|
|
|
|
|
|
|
|
if state.streams_topology != topology {
|
2022-02-21 17:43:46 +00:00
|
|
|
gst::debug!(
|
2021-10-15 14:57:55 +00:00
|
|
|
CAT,
|
2022-10-09 13:06:59 +00:00
|
|
|
imp: self, "streams topoly changed ('{:?}' -> '{:?}'), waiting for streamsynchronize to be flushed",
|
2021-10-15 14:57:55 +00:00
|
|
|
state.streams_topology, topology);
|
|
|
|
item.set_waiting_for_ss_eos(
|
|
|
|
state.streams_topology.n_streams(),
|
|
|
|
stream_collection_msg,
|
|
|
|
);
|
|
|
|
state.waiting_for_ss_eos = Some(item);
|
|
|
|
|
|
|
|
// unblock previous item as we need it to be flushed out of streamsynchronizer
|
2022-10-09 13:06:59 +00:00
|
|
|
state.unblock_item(self);
|
2021-10-15 14:57:55 +00:00
|
|
|
} else {
|
|
|
|
item.set_waiting_for_pads(topology.n_streams(), stream_collection_msg);
|
|
|
|
state.waiting_for_pads = Some(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
state.waiting_for_stream_collection = None;
|
2021-12-22 10:38:43 +00:00
|
|
|
|
|
|
|
self.update_current(state_guard);
|
2021-10-15 14:57:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
// return true if the message can be forwarded
|
2022-10-09 13:06:59 +00:00
|
|
|
fn handle_stream_selected(&self, stream_selected_msg: &gst::message::StreamsSelected) -> bool {
|
2021-10-15 14:57:55 +00:00
|
|
|
let mut state_guard = self.state.lock().unwrap();
|
|
|
|
let state = state_guard.as_mut().unwrap();
|
|
|
|
let src = stream_selected_msg.src().unwrap();
|
|
|
|
|
|
|
|
if let Some(item) = state.blocked.clone() {
|
|
|
|
let uridecodebin = item.uridecodebin();
|
|
|
|
|
|
|
|
if src.has_as_ancestor(&uridecodebin) {
|
|
|
|
// stream-selected message is from the blocked item, queue the message until it's unblocked
|
2022-02-21 17:43:46 +00:00
|
|
|
gst::debug!(
|
2021-10-15 14:57:55 +00:00
|
|
|
CAT,
|
2022-10-09 13:06:59 +00:00
|
|
|
imp: self,
|
2021-10-15 14:57:55 +00:00
|
|
|
"queue stream-selected message from {} as item is currently blocked",
|
|
|
|
src.name(),
|
|
|
|
);
|
|
|
|
|
|
|
|
item.add_stream_selected(stream_selected_msg.copy());
|
|
|
|
false
|
|
|
|
} else {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn process_decodebin_pad(&self, src_pad: &gst::Pad) {
|
|
|
|
let element = self.instance();
|
|
|
|
|
|
|
|
let start_next = {
|
|
|
|
let mut state_guard = self.state.lock().unwrap();
|
|
|
|
let state = state_guard.as_mut().unwrap();
|
|
|
|
|
2022-04-08 09:21:42 +00:00
|
|
|
if state.status.done() {
|
2021-10-15 14:57:55 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-03-28 10:47:53 +00:00
|
|
|
let item = match state.waiting_for_pads.as_ref() {
|
|
|
|
Some(item) => item.clone(),
|
|
|
|
None => return, // element is being shutdown
|
|
|
|
};
|
2021-10-15 14:57:55 +00:00
|
|
|
|
|
|
|
// Parse the pad name to extract the stream type and its index.
|
|
|
|
// We could get the type from the Stream object from the StreamStart sticky event but we'd still have
|
|
|
|
// to parse the name for the index.
|
|
|
|
let pad_name = src_pad.name();
|
|
|
|
let (stream_type, stream_index) = match stream_type_from_pad_name(&pad_name) {
|
|
|
|
Ok((stream_type, stream_index)) => (stream_type, stream_index),
|
|
|
|
Err(e) => {
|
2022-10-09 13:06:59 +00:00
|
|
|
gst::warning!(CAT, imp: self, "Ignoring pad {}: {}", pad_name, e);
|
2021-10-15 14:57:55 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let concat = match stream_type {
|
|
|
|
gst::StreamType::AUDIO => state.concat_audio.get(stream_index),
|
|
|
|
gst::StreamType::VIDEO => state.concat_video.get(stream_index),
|
|
|
|
gst::StreamType::TEXT => state.concat_text.get(stream_index),
|
|
|
|
_ => unreachable!(), // early return on unsupported streams above
|
|
|
|
};
|
|
|
|
|
|
|
|
let concat = match concat {
|
|
|
|
None => {
|
2022-02-21 17:43:46 +00:00
|
|
|
gst::debug!(
|
2021-10-15 14:57:55 +00:00
|
|
|
CAT,
|
2022-10-09 13:06:59 +00:00
|
|
|
imp: self,
|
2021-10-15 14:57:55 +00:00
|
|
|
"stream {} from item #{}: creating concat element",
|
|
|
|
pad_name,
|
|
|
|
item.index()
|
|
|
|
);
|
|
|
|
|
|
|
|
let concat = match gst::ElementFactory::make(
|
|
|
|
"concat",
|
|
|
|
Some(&format!(
|
|
|
|
"playlist-concat-{}-{}",
|
|
|
|
stream_type.name(),
|
|
|
|
stream_index
|
|
|
|
)),
|
|
|
|
) {
|
|
|
|
Ok(concat) => concat,
|
|
|
|
Err(_) => {
|
|
|
|
drop(state_guard);
|
2022-10-09 13:06:59 +00:00
|
|
|
self.failed(PlaylistError::PluginMissing {
|
|
|
|
error: anyhow::anyhow!("element 'concat' missing"),
|
|
|
|
});
|
2021-10-15 14:57:55 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// this is done by the streamsynchronizer element downstream
|
|
|
|
concat.set_property("adjust-base", false);
|
|
|
|
|
|
|
|
element.add(&concat).unwrap();
|
|
|
|
|
|
|
|
concat.sync_state_with_parent().unwrap();
|
|
|
|
|
|
|
|
// link concat elements to streamsynchronizer
|
|
|
|
let concat_src = concat.static_pad("src").unwrap();
|
|
|
|
let sync_sink = state
|
|
|
|
.streamsynchronizer
|
|
|
|
.request_pad_simple("sink_%u")
|
|
|
|
.unwrap();
|
|
|
|
concat_src.link(&sync_sink).unwrap();
|
|
|
|
|
|
|
|
let element_weak = element.downgrade();
|
|
|
|
|
|
|
|
// add event probe on streamsynchronizer src pad. Will only be used when we are waiting for the
|
|
|
|
// streamsynchronizer to be flushed in order to handle streams topology changes.
|
|
|
|
let src_pad_name = sync_sink.name().to_string().replace("sink", "src");
|
|
|
|
let sync_src = state.streamsynchronizer.static_pad(&src_pad_name).unwrap();
|
|
|
|
sync_src.add_probe(gst::PadProbeType::EVENT_DOWNSTREAM, move |_pad, info| {
|
|
|
|
match info.data {
|
|
|
|
Some(gst::PadProbeData::Event(ref ev))
|
|
|
|
if ev.type_() == gst::EventType::Eos =>
|
|
|
|
{
|
|
|
|
let element = match element_weak.upgrade() {
|
|
|
|
Some(element) => element,
|
|
|
|
None => return gst::PadProbeReturn::Remove,
|
|
|
|
};
|
2022-01-17 18:09:18 +00:00
|
|
|
let imp = element.imp();
|
2021-10-15 14:57:55 +00:00
|
|
|
|
|
|
|
let item = {
|
2022-01-17 18:09:18 +00:00
|
|
|
let mut state_guard = imp.state.lock().unwrap();
|
2021-10-15 14:57:55 +00:00
|
|
|
let state = state_guard.as_mut().unwrap();
|
|
|
|
state.waiting_for_ss_eos.as_ref().cloned()
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Some(item) = item {
|
|
|
|
if item.dec_waiting_eos_ss() {
|
2022-10-09 13:06:59 +00:00
|
|
|
gst::debug!(CAT, imp: imp, "streamsynchronizer has been flushed, reorganize pipeline to fit new streams topology and unblock item");
|
|
|
|
imp.handle_topology_change();
|
2021-10-15 14:57:55 +00:00
|
|
|
gst::PadProbeReturn::Drop
|
|
|
|
} else {
|
|
|
|
gst::PadProbeReturn::Drop
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
gst::PadProbeReturn::Pass
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => gst::PadProbeReturn::Pass,
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// ghost streamsynchronizer src pad
|
|
|
|
let sync_src_name = sync_sink.name().as_str().replace("sink", "src");
|
|
|
|
let src = state.streamsynchronizer.static_pad(&sync_src_name).unwrap();
|
|
|
|
let ghost = gst::GhostPad::with_target(Some(pad_name.as_str()), &src).unwrap();
|
|
|
|
ghost.set_active(true).unwrap();
|
|
|
|
|
|
|
|
// proxy sticky events
|
|
|
|
src.sticky_events_foreach(|event| {
|
2021-12-01 13:33:45 +00:00
|
|
|
use std::ops::ControlFlow;
|
2021-12-01 13:57:45 +00:00
|
|
|
let _ = ghost.store_sticky_event(event);
|
2021-12-01 13:33:45 +00:00
|
|
|
ControlFlow::Continue(gst::EventForeachAction::Keep)
|
2021-10-15 14:57:55 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
ghost.set_event_function(|pad, parent, event| match event.view() {
|
|
|
|
gst::EventView::SelectStreams(_) => {
|
|
|
|
// TODO: handle select-streams event
|
2022-10-09 13:06:59 +00:00
|
|
|
if let Some(element) = parent {
|
|
|
|
gst::fixme!(
|
|
|
|
CAT,
|
|
|
|
obj: element,
|
|
|
|
"select-streams event not supported ('{:?}')",
|
|
|
|
event
|
|
|
|
);
|
|
|
|
}
|
2021-10-15 14:57:55 +00:00
|
|
|
false
|
|
|
|
}
|
2022-10-12 14:48:47 +00:00
|
|
|
_ => gst::Pad::event_default(pad, parent, event),
|
2021-10-15 14:57:55 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
element.add_pad(&ghost).unwrap();
|
|
|
|
|
|
|
|
match stream_type {
|
|
|
|
gst::StreamType::AUDIO => {
|
|
|
|
state.concat_audio.push(concat.clone());
|
|
|
|
}
|
|
|
|
gst::StreamType::VIDEO => {
|
|
|
|
state.concat_video.push(concat.clone());
|
|
|
|
}
|
|
|
|
gst::StreamType::TEXT => {
|
|
|
|
state.concat_text.push(concat.clone());
|
|
|
|
}
|
|
|
|
_ => unreachable!(), // early return on unsupported streams above
|
|
|
|
}
|
|
|
|
|
|
|
|
concat
|
|
|
|
}
|
|
|
|
Some(concat) => {
|
2022-02-21 17:43:46 +00:00
|
|
|
gst::debug!(
|
2021-10-15 14:57:55 +00:00
|
|
|
CAT,
|
2022-10-09 13:06:59 +00:00
|
|
|
imp: self,
|
2021-10-15 14:57:55 +00:00
|
|
|
"stream {} from item #{}: re-using concat element {}",
|
|
|
|
pad_name,
|
|
|
|
item.index(),
|
|
|
|
concat.name()
|
|
|
|
);
|
|
|
|
|
|
|
|
concat.clone()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let sink_pad = concat.request_pad_simple("sink_%u").unwrap();
|
|
|
|
src_pad.link(&sink_pad).unwrap();
|
|
|
|
|
|
|
|
item.add_concat_sink_pad(&concat, &sink_pad);
|
|
|
|
|
|
|
|
// block pad until next item is reaching the `Blocked` state
|
2022-04-07 12:19:56 +00:00
|
|
|
let receiver = Mutex::new(item.receiver());
|
2021-10-15 14:57:55 +00:00
|
|
|
let element_weak = element.downgrade();
|
2022-04-08 13:33:00 +00:00
|
|
|
// passing ownership of item to the probe callback would introduce a reference cycle as the item is owning the sinkpad
|
|
|
|
let item_weak = item.downgrade();
|
2021-10-15 14:57:55 +00:00
|
|
|
|
|
|
|
sink_pad.add_probe(gst::PadProbeType::BLOCK_DOWNSTREAM, move |pad, info| {
|
|
|
|
let element = match element_weak.upgrade() {
|
|
|
|
Some(element) => element,
|
|
|
|
None => return gst::PadProbeReturn::Remove,
|
|
|
|
};
|
|
|
|
let parent = pad.parent().unwrap();
|
2022-04-08 13:33:00 +00:00
|
|
|
let item = match Item::upgrade(&item_weak) {
|
|
|
|
Some(item) => item,
|
|
|
|
None => return gst::PadProbeReturn::Remove,
|
|
|
|
};
|
2021-10-15 14:57:55 +00:00
|
|
|
|
|
|
|
if !item.is_streaming() {
|
|
|
|
// block pad until next item is ready
|
2022-02-21 17:43:46 +00:00
|
|
|
gst::log!(
|
2021-10-15 14:57:55 +00:00
|
|
|
CAT,
|
|
|
|
obj: &element,
|
|
|
|
"blocking pad {}:{} until next item is ready",
|
|
|
|
parent.name(),
|
|
|
|
pad.name()
|
|
|
|
);
|
|
|
|
|
2022-04-07 12:19:56 +00:00
|
|
|
let receiver = receiver.lock().unwrap();
|
|
|
|
|
2022-04-01 08:01:46 +00:00
|
|
|
if let Ok(false) = receiver.recv() {
|
|
|
|
// we are shutting down so remove the probe.
|
|
|
|
// Don't handle Err(_) here as if the item has multiple pads, the sender may be dropped in unblock_item()
|
|
|
|
// before all probes received the message, resulting in a receiving error.
|
|
|
|
return gst::PadProbeReturn::Remove;
|
|
|
|
}
|
2021-10-15 14:57:55 +00:00
|
|
|
|
2022-02-21 17:43:46 +00:00
|
|
|
gst::log!(
|
2021-10-15 14:57:55 +00:00
|
|
|
CAT,
|
|
|
|
obj: &element,
|
|
|
|
"pad {}:{} has been unblocked",
|
|
|
|
parent.name(),
|
|
|
|
pad.name()
|
|
|
|
);
|
|
|
|
|
|
|
|
gst::PadProbeReturn::Pass
|
|
|
|
} else {
|
|
|
|
match info.data {
|
|
|
|
Some(gst::PadProbeData::Event(ref ev))
|
|
|
|
if ev.type_() == gst::EventType::Eos =>
|
|
|
|
{
|
|
|
|
if item.dec_waiting_eos() {
|
|
|
|
// all the streams are eos, item is now done
|
2022-02-21 17:43:46 +00:00
|
|
|
gst::log!(
|
2021-10-15 14:57:55 +00:00
|
|
|
CAT,
|
|
|
|
obj: &element,
|
|
|
|
"all streams of item #{} are eos",
|
|
|
|
item.index()
|
|
|
|
);
|
|
|
|
|
2022-01-17 18:09:18 +00:00
|
|
|
let imp = element.imp();
|
2021-10-15 14:57:55 +00:00
|
|
|
{
|
2022-01-17 18:09:18 +00:00
|
|
|
let mut state_guard = imp.state.lock().unwrap();
|
2021-10-15 14:57:55 +00:00
|
|
|
let state = state_guard.as_mut().unwrap();
|
|
|
|
|
|
|
|
let index = item.index();
|
|
|
|
|
|
|
|
let removed = state
|
|
|
|
.streaming
|
|
|
|
.iter()
|
|
|
|
.position(|i| i.index() == index)
|
|
|
|
.map(|e| state.streaming.remove(e));
|
|
|
|
|
|
|
|
if let Some(item) = removed {
|
|
|
|
item.set_done();
|
|
|
|
state.done.push(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-09 13:06:59 +00:00
|
|
|
if let Err(e) = imp.start_next_item() {
|
|
|
|
imp.failed(e);
|
2021-10-15 14:57:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
gst::PadProbeReturn::Remove
|
|
|
|
}
|
|
|
|
_ => gst::PadProbeReturn::Pass,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if item.dec_n_pads_pending() == 0 {
|
|
|
|
// we got all the pads
|
2022-02-21 17:43:46 +00:00
|
|
|
gst::debug!(
|
2021-10-15 14:57:55 +00:00
|
|
|
CAT,
|
2022-10-09 13:06:59 +00:00
|
|
|
imp: self,
|
2021-10-15 14:57:55 +00:00
|
|
|
"got all the pads for item #{}",
|
|
|
|
item.index()
|
|
|
|
);
|
|
|
|
|
|
|
|
// all pads have been linked to concat, unblock previous item
|
2022-10-09 13:06:59 +00:00
|
|
|
state.unblock_item(self);
|
2021-10-15 14:57:55 +00:00
|
|
|
|
|
|
|
state.waiting_for_pads = None;
|
|
|
|
// block item until the next one is fully linked to concat
|
|
|
|
item.set_blocked();
|
|
|
|
state.blocked = Some(item);
|
|
|
|
|
2021-12-22 10:38:43 +00:00
|
|
|
self.update_current(state_guard);
|
|
|
|
|
2021-10-15 14:57:55 +00:00
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if start_next {
|
2022-10-09 13:06:59 +00:00
|
|
|
gst::debug!(CAT, imp: self, "got all pending streams, queue next item");
|
2021-10-15 14:57:55 +00:00
|
|
|
|
2022-10-09 13:06:59 +00:00
|
|
|
if let Err(e) = self.start_next_item() {
|
|
|
|
self.failed(e);
|
2021-10-15 14:57:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// called when all previous items have been flushed from streamsynchronizer
|
|
|
|
/// and so the elements can reorganize itself to handle a pending changes in
|
|
|
|
/// streams topology.
|
2022-10-09 13:06:59 +00:00
|
|
|
fn handle_topology_change(&self) {
|
2022-04-07 12:19:56 +00:00
|
|
|
let (pending_pads, channels) = {
|
2021-10-15 14:57:55 +00:00
|
|
|
let mut state_guard = self.state.lock().unwrap();
|
|
|
|
let state = state_guard.as_mut().unwrap();
|
|
|
|
|
2022-03-28 10:47:53 +00:00
|
|
|
let item = match state.waiting_for_ss_eos.take() {
|
|
|
|
Some(item) => item,
|
|
|
|
None => return, // element is being shutdown
|
|
|
|
};
|
|
|
|
|
2022-04-07 12:19:56 +00:00
|
|
|
let (topology, pending_pads, channels) = item.done_waiting_for_ss_eos();
|
2021-10-15 14:57:55 +00:00
|
|
|
state.waiting_for_pads = Some(item);
|
|
|
|
|
|
|
|
// remove now useless concat elements, missing ones will be added when handling src pads from decodebin
|
|
|
|
|
|
|
|
fn remove_useless_concat(
|
2022-10-09 13:06:59 +00:00
|
|
|
imp: &UriPlaylistBin,
|
2021-10-15 14:57:55 +00:00
|
|
|
n_stream: usize,
|
|
|
|
concats: &mut Vec<gst::Element>,
|
|
|
|
streamsynchronizer: &gst::Element,
|
|
|
|
) {
|
|
|
|
while n_stream < concats.len() {
|
|
|
|
// need to remove concat elements
|
|
|
|
let concat = concats.pop().unwrap();
|
2022-10-09 13:06:59 +00:00
|
|
|
gst::log!(CAT, imp: imp, "remove {}", concat.name());
|
2021-10-15 14:57:55 +00:00
|
|
|
|
|
|
|
let concat_src = concat.static_pad("src").unwrap();
|
|
|
|
let ss_sink = concat_src.peer().unwrap();
|
|
|
|
|
|
|
|
// unlink and remove sink pad from streamsynchronizer
|
|
|
|
concat_src.unlink(&ss_sink).unwrap();
|
|
|
|
streamsynchronizer.release_request_pad(&ss_sink);
|
|
|
|
|
|
|
|
// remove associated ghost pad
|
2022-10-09 13:06:59 +00:00
|
|
|
let src_pads = imp.instance().src_pads();
|
2021-10-15 14:57:55 +00:00
|
|
|
let ghost = src_pads
|
|
|
|
.iter()
|
|
|
|
.find(|pad| {
|
|
|
|
let ghost = pad.downcast_ref::<gst::GhostPad>().unwrap();
|
|
|
|
ghost.target().is_none()
|
|
|
|
})
|
|
|
|
.unwrap();
|
2022-10-09 13:06:59 +00:00
|
|
|
imp.instance().remove_pad(ghost).unwrap();
|
2021-10-15 14:57:55 +00:00
|
|
|
|
2022-10-09 13:06:59 +00:00
|
|
|
imp.instance().remove(&concat).unwrap();
|
2021-10-15 14:57:55 +00:00
|
|
|
let _ = concat.set_state(gst::State::Null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
remove_useless_concat(
|
2022-10-09 13:06:59 +00:00
|
|
|
self,
|
2021-10-15 14:57:55 +00:00
|
|
|
topology.audio as usize,
|
|
|
|
&mut state.concat_audio,
|
|
|
|
&state.streamsynchronizer,
|
|
|
|
);
|
|
|
|
remove_useless_concat(
|
2022-10-09 13:06:59 +00:00
|
|
|
self,
|
2021-10-15 14:57:55 +00:00
|
|
|
topology.video as usize,
|
|
|
|
&mut state.concat_video,
|
|
|
|
&state.streamsynchronizer,
|
|
|
|
);
|
|
|
|
remove_useless_concat(
|
2022-10-09 13:06:59 +00:00
|
|
|
self,
|
2021-10-15 14:57:55 +00:00
|
|
|
topology.text as usize,
|
|
|
|
&mut state.concat_text,
|
|
|
|
&state.streamsynchronizer,
|
|
|
|
);
|
|
|
|
|
|
|
|
state.streams_topology = topology;
|
|
|
|
|
2022-04-07 12:19:56 +00:00
|
|
|
(pending_pads, channels)
|
2021-10-15 14:57:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// process decodebin src pads we already received and unblock them
|
|
|
|
for pad in pending_pads.iter() {
|
|
|
|
self.process_decodebin_pad(pad);
|
|
|
|
}
|
|
|
|
|
2022-04-07 12:19:56 +00:00
|
|
|
channels.send(true);
|
2021-10-15 14:57:55 +00:00
|
|
|
}
|
|
|
|
|
2022-10-09 13:06:59 +00:00
|
|
|
fn failed(&self, error: PlaylistError) {
|
2021-10-15 14:57:55 +00:00
|
|
|
{
|
|
|
|
let mut state_guard = self.state.lock().unwrap();
|
|
|
|
let state = state_guard.as_mut().unwrap();
|
|
|
|
|
2022-04-08 09:21:42 +00:00
|
|
|
if state.status.done() {
|
2021-10-15 14:57:55 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-04-08 09:21:42 +00:00
|
|
|
state.status = Status::Error;
|
2021-10-15 14:57:55 +00:00
|
|
|
|
|
|
|
if let Some(blocked) = state.blocked.take() {
|
|
|
|
// unblock streaming thread
|
|
|
|
blocked.set_streaming(state.streams_topology.n_streams());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let error_msg = error.to_string();
|
2022-10-09 13:06:59 +00:00
|
|
|
gst::error!(CAT, imp: self, "{}", error_msg);
|
2021-10-15 14:57:55 +00:00
|
|
|
|
|
|
|
match error {
|
|
|
|
PlaylistError::PluginMissing { .. } => {
|
2022-10-09 13:06:59 +00:00
|
|
|
gst::element_imp_error!(self, gst::CoreError::MissingPlugin, [&error_msg]);
|
2021-10-15 14:57:55 +00:00
|
|
|
}
|
|
|
|
PlaylistError::ItemFailed { item, .. } => {
|
|
|
|
// remove failing uridecodebin
|
|
|
|
let uridecodebin = item.uridecodebin();
|
|
|
|
uridecodebin.call_async(move |uridecodebin| {
|
|
|
|
let _ = uridecodebin.set_state(gst::State::Null);
|
|
|
|
});
|
2022-10-09 13:06:59 +00:00
|
|
|
let _ = self.instance().remove(&uridecodebin);
|
2021-10-15 14:57:55 +00:00
|
|
|
|
|
|
|
let details = gst::Structure::builder("details");
|
|
|
|
let details = details.field("uri", item.uri());
|
|
|
|
|
2022-10-09 13:06:59 +00:00
|
|
|
gst::element_imp_error!(
|
|
|
|
self,
|
2021-10-15 14:57:55 +00:00
|
|
|
gst::LibraryError::Failed,
|
|
|
|
[&error_msg],
|
|
|
|
details: details.build()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2021-12-22 10:38:43 +00:00
|
|
|
|
|
|
|
self.update_current(self.state.lock().unwrap());
|
|
|
|
}
|
|
|
|
|
|
|
|
fn update_current(&self, mut state_guard: MutexGuard<Option<State>>) {
|
|
|
|
let (uris_len, infinite) = {
|
|
|
|
let settings = self.settings.lock().unwrap();
|
|
|
|
|
|
|
|
(settings.uris.len(), settings.iterations == 0)
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Some(state) = state_guard.as_mut() {
|
|
|
|
// first streaming item is the one actually being played
|
|
|
|
if let Some(current) = state.streaming.get(0) {
|
|
|
|
let (mut current_iteration, current_uri_index) = (
|
|
|
|
(current.index() / uris_len) as u32,
|
|
|
|
(current.index() % uris_len) as u64,
|
|
|
|
);
|
|
|
|
|
|
|
|
if infinite {
|
|
|
|
current_iteration = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
let element = self.instance();
|
|
|
|
if current_iteration != state.current_iteration {
|
|
|
|
state.current_iteration = current_iteration;
|
|
|
|
element.notify("current-iteration");
|
|
|
|
}
|
|
|
|
if current_uri_index != state.current_uri_index {
|
|
|
|
state.current_uri_index = current_uri_index;
|
|
|
|
element.notify("current-uri-index");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-10-15 14:57:55 +00:00
|
|
|
}
|
|
|
|
}
|
2022-04-07 12:19:56 +00:00
|
|
|
|
|
|
|
#[derive(Default, Clone, Debug)]
|
|
|
|
struct Channels {
|
|
|
|
senders: Arc<Mutex<Vec<mpsc::Sender<bool>>>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Channels {
|
|
|
|
fn get_receiver(&self) -> mpsc::Receiver<bool> {
|
|
|
|
let mut senders = self.senders.lock().unwrap();
|
|
|
|
|
|
|
|
let (sender, receiver) = mpsc::channel();
|
|
|
|
senders.push(sender);
|
|
|
|
receiver
|
|
|
|
}
|
|
|
|
|
|
|
|
fn send(&self, val: bool) {
|
|
|
|
let mut senders = self.senders.lock().unwrap();
|
|
|
|
|
|
|
|
// remove sender if sending failed
|
|
|
|
senders.retain(|sender| sender.send(val).is_ok());
|
|
|
|
}
|
|
|
|
}
|