2019-11-24 20:12:40 +00:00
|
|
|
// Copyright (C) 2019 François Laignel <fengalin@free.fr>
|
|
|
|
//
|
|
|
|
// This library is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU Library General Public
|
|
|
|
// License as published by the Free Software Foundation; either
|
|
|
|
// version 2 of the License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// Library General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Library General Public
|
|
|
|
// License along with this library; if not, write to the
|
|
|
|
// Free Software Foundation, Inc., 51 Franklin Street, Suite 500,
|
|
|
|
// Boston, MA 02110-1335, USA.
|
2022-01-15 19:18:47 +00:00
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: LGPL-2.1-or-later
|
2019-11-24 20:12:40 +00:00
|
|
|
|
2021-06-03 18:20:54 +00:00
|
|
|
use gst::glib;
|
2019-12-02 09:30:07 +00:00
|
|
|
use gst::prelude::*;
|
|
|
|
|
2024-01-31 15:07:56 +00:00
|
|
|
use once_cell::sync::Lazy;
|
2019-12-02 09:30:07 +00:00
|
|
|
|
|
|
|
use std::sync::mpsc;
|
2019-11-24 20:12:40 +00:00
|
|
|
|
2020-11-22 14:24:55 +00:00
|
|
|
static CAT: Lazy<gst::DebugCategory> = Lazy::new(|| {
|
|
|
|
gst::DebugCategory::new(
|
2019-12-02 09:30:07 +00:00
|
|
|
"ts-test",
|
|
|
|
gst::DebugColorFlags::empty(),
|
|
|
|
Some("Thread-sharing test"),
|
2020-11-22 14:24:55 +00:00
|
|
|
)
|
|
|
|
});
|
2019-12-02 09:30:07 +00:00
|
|
|
|
2019-11-24 20:12:40 +00:00
|
|
|
fn init() {
|
|
|
|
use std::sync::Once;
|
|
|
|
static INIT: Once = Once::new();
|
|
|
|
|
|
|
|
INIT.call_once(|| {
|
|
|
|
gst::init().unwrap();
|
|
|
|
gstthreadshare::plugin_register_static().expect("gstthreadshare pipeline test");
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2022-08-24 21:47:01 +00:00
|
|
|
#[cfg(not(windows))]
|
2019-12-02 09:30:07 +00:00
|
|
|
fn multiple_contexts_queue() {
|
2019-11-24 20:12:40 +00:00
|
|
|
use std::net;
|
|
|
|
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
|
2019-12-02 09:30:07 +00:00
|
|
|
use std::sync::mpsc;
|
2019-11-24 20:12:40 +00:00
|
|
|
|
|
|
|
init();
|
|
|
|
|
|
|
|
const CONTEXT_NB: u32 = 2;
|
|
|
|
const SRC_NB: u16 = 4;
|
|
|
|
const CONTEXT_WAIT: u32 = 1;
|
|
|
|
const BUFFER_NB: u32 = 3;
|
2019-12-02 09:30:07 +00:00
|
|
|
const FIRST_PORT: u16 = 40000;
|
2019-11-24 20:12:40 +00:00
|
|
|
|
|
|
|
let l = glib::MainLoop::new(None, false);
|
2022-10-22 16:06:29 +00:00
|
|
|
let pipeline = gst::Pipeline::default();
|
2019-11-24 20:12:40 +00:00
|
|
|
|
2019-12-02 09:30:07 +00:00
|
|
|
let (sender, receiver) = mpsc::channel();
|
2019-11-24 20:12:40 +00:00
|
|
|
|
|
|
|
for i in 0..SRC_NB {
|
2022-10-19 16:18:43 +00:00
|
|
|
let src = gst::ElementFactory::make("ts-udpsrc")
|
2023-01-25 08:23:46 +00:00
|
|
|
.name(format!("src-{i}").as_str())
|
2022-10-19 16:18:43 +00:00
|
|
|
.property("context", format!("context-{}", (i as u32) % CONTEXT_NB))
|
|
|
|
.property("context-wait", CONTEXT_WAIT)
|
|
|
|
.property("port", (FIRST_PORT + i) as i32)
|
|
|
|
.build()
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let queue = gst::ElementFactory::make("ts-queue")
|
2023-01-25 08:23:46 +00:00
|
|
|
.name(format!("queue-{i}").as_str())
|
2022-10-19 16:18:43 +00:00
|
|
|
.property("context", format!("context-{}", (i as u32) % CONTEXT_NB))
|
|
|
|
.property("context-wait", CONTEXT_WAIT)
|
|
|
|
.build()
|
|
|
|
.unwrap();
|
|
|
|
|
2022-10-22 16:06:29 +00:00
|
|
|
let sink = gst_app::AppSink::builder()
|
2023-01-25 08:23:46 +00:00
|
|
|
.name(format!("sink-{i}").as_str())
|
2022-10-22 16:06:29 +00:00
|
|
|
.sync(false)
|
|
|
|
.async_(false)
|
|
|
|
.build();
|
2019-11-24 20:12:40 +00:00
|
|
|
|
2022-10-22 16:06:29 +00:00
|
|
|
pipeline
|
2023-03-09 14:46:52 +00:00
|
|
|
.add_many([&src, &queue, sink.upcast_ref()])
|
2022-10-22 16:06:29 +00:00
|
|
|
.unwrap();
|
2023-03-09 14:46:52 +00:00
|
|
|
gst::Element::link_many([&src, &queue, sink.upcast_ref()]).unwrap();
|
2019-11-24 20:12:40 +00:00
|
|
|
|
2019-12-02 09:30:07 +00:00
|
|
|
let sender_clone = sender.clone();
|
2022-10-22 16:06:29 +00:00
|
|
|
sink.set_callbacks(
|
2021-05-31 07:33:50 +00:00
|
|
|
gst_app::AppSinkCallbacks::builder()
|
|
|
|
.new_sample(move |appsink| {
|
2021-11-21 16:15:04 +00:00
|
|
|
let _sample = appsink.pull_sample().unwrap();
|
2021-05-31 07:33:50 +00:00
|
|
|
|
|
|
|
sender_clone.send(()).unwrap();
|
|
|
|
Ok(gst::FlowSuccess::Ok)
|
|
|
|
})
|
|
|
|
.build(),
|
|
|
|
);
|
2019-11-24 20:12:40 +00:00
|
|
|
}
|
|
|
|
|
2019-12-02 09:30:07 +00:00
|
|
|
let pipeline_clone = pipeline.clone();
|
|
|
|
let l_clone = l.clone();
|
|
|
|
let mut test_scenario = Some(move || {
|
|
|
|
let buffer = [0; 160];
|
|
|
|
let socket = net::UdpSocket::bind("0.0.0.0:0").unwrap();
|
|
|
|
|
|
|
|
let ipaddr = IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1));
|
|
|
|
let destinations = (FIRST_PORT..(FIRST_PORT + SRC_NB))
|
|
|
|
.map(|port| SocketAddr::new(ipaddr, port))
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
for _ in 0..BUFFER_NB {
|
|
|
|
for dest in &destinations {
|
2022-02-21 17:43:46 +00:00
|
|
|
gst::debug!(CAT, "multiple_contexts_queue: sending buffer to {:?}", dest);
|
2019-12-02 09:30:07 +00:00
|
|
|
socket.send_to(&buffer, dest).unwrap();
|
|
|
|
std::thread::sleep(std::time::Duration::from_millis(CONTEXT_WAIT as u64));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-21 17:43:46 +00:00
|
|
|
gst::debug!(
|
2019-12-02 09:30:07 +00:00
|
|
|
CAT,
|
|
|
|
"multiple_contexts_queue: waiting for all buffers notifications"
|
|
|
|
);
|
|
|
|
for _ in 0..(BUFFER_NB * (SRC_NB as u32)) {
|
|
|
|
receiver.recv().unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
pipeline_clone.set_state(gst::State::Null).unwrap();
|
|
|
|
l_clone.quit();
|
|
|
|
});
|
|
|
|
|
2021-04-12 12:49:54 +00:00
|
|
|
let bus = pipeline.bus().unwrap();
|
2019-11-24 20:12:40 +00:00
|
|
|
let l_clone = l.clone();
|
2023-04-14 09:46:43 +00:00
|
|
|
let _bus_watch = bus
|
|
|
|
.add_watch(move |_, msg| {
|
|
|
|
use gst::MessageView;
|
|
|
|
|
|
|
|
match msg.view() {
|
|
|
|
MessageView::StateChanged(state_changed) => {
|
|
|
|
if let Some(source) = state_changed.src() {
|
|
|
|
if source.type_() == gst::Pipeline::static_type()
|
|
|
|
&& state_changed.old() == gst::State::Paused
|
|
|
|
&& state_changed.current() == gst::State::Playing
|
|
|
|
{
|
|
|
|
if let Some(test_scenario) = test_scenario.take() {
|
|
|
|
std::thread::spawn(test_scenario);
|
|
|
|
}
|
2019-12-02 09:30:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-04-14 09:46:43 +00:00
|
|
|
MessageView::Error(err) => {
|
|
|
|
gst::error!(
|
|
|
|
CAT,
|
|
|
|
"multiple_contexts_queue: Error from {:?}: {} ({:?})",
|
|
|
|
err.src().map(|s| s.path_string()),
|
|
|
|
err.error(),
|
|
|
|
err.debug()
|
|
|
|
);
|
|
|
|
l_clone.quit();
|
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
};
|
2019-11-24 20:12:40 +00:00
|
|
|
|
2023-07-06 13:27:28 +00:00
|
|
|
glib::ControlFlow::Continue
|
2023-04-14 09:46:43 +00:00
|
|
|
})
|
|
|
|
.unwrap();
|
2019-11-24 20:12:40 +00:00
|
|
|
|
2019-12-02 09:30:07 +00:00
|
|
|
pipeline.set_state(gst::State::Playing).unwrap();
|
|
|
|
|
2022-02-21 17:43:46 +00:00
|
|
|
gst::debug!(CAT, "Starting main loop for multiple_contexts_queue...");
|
2019-12-02 09:30:07 +00:00
|
|
|
l.run();
|
2022-02-21 17:43:46 +00:00
|
|
|
gst::debug!(CAT, "Stopping main loop for multiple_contexts_queue...");
|
2019-12-02 09:30:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2022-08-24 21:47:01 +00:00
|
|
|
#[cfg(not(windows))]
|
2019-12-02 09:30:07 +00:00
|
|
|
fn multiple_contexts_proxy() {
|
|
|
|
use std::net;
|
|
|
|
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
|
|
|
|
|
|
|
|
init();
|
|
|
|
|
|
|
|
const CONTEXT_NB: u32 = 2;
|
|
|
|
const SRC_NB: u16 = 4;
|
|
|
|
const CONTEXT_WAIT: u32 = 1;
|
|
|
|
const BUFFER_NB: u32 = 3;
|
|
|
|
// Don't overlap with `multiple_contexts_queue`
|
|
|
|
const OFFSET: u16 = 10;
|
|
|
|
const FIRST_PORT: u16 = 40000 + OFFSET;
|
|
|
|
|
|
|
|
let l = glib::MainLoop::new(None, false);
|
2022-10-22 16:06:29 +00:00
|
|
|
let pipeline = gst::Pipeline::default();
|
2019-12-02 09:30:07 +00:00
|
|
|
|
|
|
|
let (sender, receiver) = mpsc::channel();
|
|
|
|
|
|
|
|
for i in 0..SRC_NB {
|
|
|
|
let pipeline_index = i + OFFSET;
|
|
|
|
|
2022-10-19 16:18:43 +00:00
|
|
|
let src = gst::ElementFactory::make("ts-udpsrc")
|
2023-01-25 08:23:46 +00:00
|
|
|
.name(format!("src-{pipeline_index}").as_str())
|
2022-10-19 16:18:43 +00:00
|
|
|
.property("context", format!("context-{}", (i as u32) % CONTEXT_NB))
|
|
|
|
.property("context-wait", CONTEXT_WAIT)
|
|
|
|
.property("port", (FIRST_PORT + i) as i32)
|
|
|
|
.build()
|
|
|
|
.unwrap();
|
2019-12-02 09:30:07 +00:00
|
|
|
|
2022-10-19 16:18:43 +00:00
|
|
|
let proxysink = gst::ElementFactory::make("ts-proxysink")
|
2023-01-25 08:23:46 +00:00
|
|
|
.name(format!("proxysink-{pipeline_index}").as_str())
|
|
|
|
.property("proxy-context", format!("proxy-{pipeline_index}"))
|
2022-10-19 16:18:43 +00:00
|
|
|
.build()
|
|
|
|
.unwrap();
|
2019-12-02 09:30:07 +00:00
|
|
|
|
2022-10-19 16:18:43 +00:00
|
|
|
let proxysrc = gst::ElementFactory::make("ts-proxysrc")
|
2023-01-25 08:23:46 +00:00
|
|
|
.name(format!("proxysrc-{pipeline_index}").as_str())
|
2022-10-19 16:18:43 +00:00
|
|
|
.property(
|
|
|
|
"context",
|
|
|
|
&format!("context-{}", (pipeline_index as u32) % CONTEXT_NB),
|
|
|
|
)
|
2023-01-25 08:23:46 +00:00
|
|
|
.property("proxy-context", format!("proxy-{pipeline_index}"))
|
2022-10-19 16:18:43 +00:00
|
|
|
.build()
|
|
|
|
.unwrap();
|
|
|
|
|
2022-10-22 16:06:29 +00:00
|
|
|
let sink = gst_app::AppSink::builder()
|
2023-01-25 08:23:46 +00:00
|
|
|
.name(format!("sink-{pipeline_index}").as_str())
|
2022-10-22 16:06:29 +00:00
|
|
|
.sync(false)
|
|
|
|
.async_(false)
|
|
|
|
.build();
|
2019-12-02 09:30:07 +00:00
|
|
|
|
|
|
|
pipeline
|
2023-03-09 14:46:52 +00:00
|
|
|
.add_many([&src, &proxysink, &proxysrc, sink.upcast_ref()])
|
2019-12-02 09:30:07 +00:00
|
|
|
.unwrap();
|
|
|
|
src.link(&proxysink).unwrap();
|
|
|
|
proxysrc.link(&sink).unwrap();
|
|
|
|
|
|
|
|
let sender_clone = sender.clone();
|
2022-10-22 16:06:29 +00:00
|
|
|
sink.set_callbacks(
|
2021-05-31 07:33:50 +00:00
|
|
|
gst_app::AppSinkCallbacks::builder()
|
|
|
|
.new_sample(move |appsink| {
|
2021-11-21 16:15:04 +00:00
|
|
|
let _sample = appsink.pull_sample().unwrap();
|
2021-05-31 07:33:50 +00:00
|
|
|
|
|
|
|
sender_clone.send(()).unwrap();
|
|
|
|
Ok(gst::FlowSuccess::Ok)
|
|
|
|
})
|
|
|
|
.build(),
|
|
|
|
);
|
2019-12-02 09:30:07 +00:00
|
|
|
}
|
|
|
|
|
2019-11-24 20:12:40 +00:00
|
|
|
let pipeline_clone = pipeline.clone();
|
|
|
|
let l_clone = l.clone();
|
2019-12-02 09:30:07 +00:00
|
|
|
let mut test_scenario = Some(move || {
|
2019-11-24 20:12:40 +00:00
|
|
|
let buffer = [0; 160];
|
|
|
|
let socket = net::UdpSocket::bind("0.0.0.0:0").unwrap();
|
|
|
|
|
|
|
|
let ipaddr = IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1));
|
2019-12-02 09:30:07 +00:00
|
|
|
let destinations = (FIRST_PORT..(FIRST_PORT + SRC_NB))
|
2019-11-24 20:12:40 +00:00
|
|
|
.map(|port| SocketAddr::new(ipaddr, port))
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
for _ in 0..BUFFER_NB {
|
|
|
|
for dest in &destinations {
|
2022-02-21 17:43:46 +00:00
|
|
|
gst::debug!(CAT, "multiple_contexts_proxy: sending buffer to {:?}", dest);
|
2019-11-24 20:12:40 +00:00
|
|
|
socket.send_to(&buffer, dest).unwrap();
|
2019-12-02 09:30:07 +00:00
|
|
|
std::thread::sleep(std::time::Duration::from_millis(CONTEXT_WAIT as u64));
|
2019-11-24 20:12:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-21 17:43:46 +00:00
|
|
|
gst::debug!(
|
2019-12-02 09:30:07 +00:00
|
|
|
CAT,
|
|
|
|
"multiple_contexts_proxy: waiting for all buffers notifications"
|
|
|
|
);
|
|
|
|
for _ in 0..(BUFFER_NB * (SRC_NB as u32)) {
|
|
|
|
receiver.recv().unwrap();
|
|
|
|
}
|
2019-11-24 20:12:40 +00:00
|
|
|
|
|
|
|
pipeline_clone.set_state(gst::State::Null).unwrap();
|
|
|
|
l_clone.quit();
|
|
|
|
});
|
|
|
|
|
2021-04-12 12:49:54 +00:00
|
|
|
let bus = pipeline.bus().unwrap();
|
2019-12-02 09:30:07 +00:00
|
|
|
let l_clone = l.clone();
|
2023-04-14 09:46:43 +00:00
|
|
|
let _bus_watch = bus
|
|
|
|
.add_watch(move |_, msg| {
|
|
|
|
use gst::MessageView;
|
|
|
|
|
|
|
|
match msg.view() {
|
|
|
|
MessageView::StateChanged(state_changed) => {
|
|
|
|
if let Some(source) = state_changed.src() {
|
|
|
|
if source.type_() == gst::Pipeline::static_type()
|
|
|
|
&& state_changed.old() == gst::State::Paused
|
|
|
|
&& state_changed.current() == gst::State::Playing
|
|
|
|
{
|
|
|
|
if let Some(test_scenario) = test_scenario.take() {
|
|
|
|
std::thread::spawn(test_scenario);
|
|
|
|
}
|
2019-12-02 09:30:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-04-14 09:46:43 +00:00
|
|
|
MessageView::Error(err) => {
|
|
|
|
gst::error!(
|
|
|
|
CAT,
|
|
|
|
"multiple_contexts_proxy: Error from {:?}: {} ({:?})",
|
|
|
|
err.src().map(|s| s.path_string()),
|
|
|
|
err.error(),
|
|
|
|
err.debug()
|
|
|
|
);
|
|
|
|
l_clone.quit();
|
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
};
|
2019-11-24 20:12:40 +00:00
|
|
|
|
2023-07-06 13:27:28 +00:00
|
|
|
glib::ControlFlow::Continue
|
2023-04-14 09:46:43 +00:00
|
|
|
})
|
|
|
|
.unwrap();
|
2019-11-24 20:12:40 +00:00
|
|
|
|
2019-12-02 09:30:07 +00:00
|
|
|
pipeline.set_state(gst::State::Playing).unwrap();
|
|
|
|
|
2022-02-21 17:43:46 +00:00
|
|
|
gst::debug!(CAT, "Starting main loop for multiple_contexts_proxy...");
|
2019-11-24 20:12:40 +00:00
|
|
|
l.run();
|
2022-02-21 17:43:46 +00:00
|
|
|
gst::debug!(CAT, "Stopping main loop for multiple_contexts_proxy...");
|
2019-11-24 20:12:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2019-12-02 09:30:07 +00:00
|
|
|
fn eos() {
|
|
|
|
const CONTEXT: &str = "test_eos";
|
2019-11-24 20:12:40 +00:00
|
|
|
|
|
|
|
init();
|
|
|
|
|
|
|
|
let l = glib::MainLoop::new(None, false);
|
2022-10-22 16:06:29 +00:00
|
|
|
let pipeline = gst::Pipeline::default();
|
2019-11-24 20:12:40 +00:00
|
|
|
|
2021-11-06 07:34:10 +00:00
|
|
|
let caps = gst::Caps::builder("foo/bar").build();
|
2019-11-24 20:12:40 +00:00
|
|
|
|
2022-10-19 16:18:43 +00:00
|
|
|
let src = gst::ElementFactory::make("ts-appsrc")
|
|
|
|
.name("src-eos")
|
|
|
|
.property("caps", &caps)
|
|
|
|
.property("do-timestamp", true)
|
2022-11-01 08:27:48 +00:00
|
|
|
.property("context", CONTEXT)
|
2022-10-19 16:18:43 +00:00
|
|
|
.build()
|
|
|
|
.unwrap();
|
2019-12-02 09:30:07 +00:00
|
|
|
|
2022-10-19 16:18:43 +00:00
|
|
|
let queue = gst::ElementFactory::make("ts-queue")
|
|
|
|
.name("queue-eos")
|
2022-11-01 08:27:48 +00:00
|
|
|
.property("context", CONTEXT)
|
2022-10-19 16:18:43 +00:00
|
|
|
.build()
|
|
|
|
.unwrap();
|
2019-12-02 09:30:07 +00:00
|
|
|
|
2022-10-22 16:06:29 +00:00
|
|
|
let sink = gst_app::AppSink::builder()
|
2022-10-19 16:18:43 +00:00
|
|
|
.name("sink-eos")
|
2022-10-22 16:06:29 +00:00
|
|
|
.sync(false)
|
|
|
|
.async_(false)
|
|
|
|
.build();
|
2019-12-02 09:30:07 +00:00
|
|
|
|
2022-10-22 16:06:29 +00:00
|
|
|
pipeline
|
2023-03-09 14:46:52 +00:00
|
|
|
.add_many([&src, &queue, sink.upcast_ref()])
|
2022-10-22 16:06:29 +00:00
|
|
|
.unwrap();
|
2023-03-09 14:46:52 +00:00
|
|
|
gst::Element::link_many([&src, &queue, sink.upcast_ref()]).unwrap();
|
2019-12-02 09:30:07 +00:00
|
|
|
|
|
|
|
let (sample_notifier, sample_notif_rcv) = mpsc::channel();
|
|
|
|
let (eos_notifier, eos_notif_rcv) = mpsc::channel();
|
2022-10-22 16:06:29 +00:00
|
|
|
sink.set_callbacks(
|
2021-05-31 07:33:50 +00:00
|
|
|
gst_app::AppSinkCallbacks::builder()
|
|
|
|
.new_sample(move |appsink| {
|
2022-02-21 17:43:46 +00:00
|
|
|
gst::debug!(CAT, obj: appsink, "eos: pulling sample");
|
2021-11-21 16:15:04 +00:00
|
|
|
let _ = appsink.pull_sample().unwrap();
|
2021-05-31 07:33:50 +00:00
|
|
|
|
|
|
|
sample_notifier.send(()).unwrap();
|
|
|
|
|
|
|
|
Ok(gst::FlowSuccess::Ok)
|
|
|
|
})
|
|
|
|
.eos(move |_appsink| eos_notifier.send(()).unwrap())
|
|
|
|
.build(),
|
|
|
|
);
|
2019-11-24 20:12:40 +00:00
|
|
|
|
2019-12-02 09:30:07 +00:00
|
|
|
fn push_buffer(src: &gst::Element) -> bool {
|
2022-02-21 17:43:46 +00:00
|
|
|
gst::debug!(CAT, obj: src, "eos: pushing buffer");
|
2021-11-21 16:15:04 +00:00
|
|
|
src.emit_by_name::<bool>("push-buffer", &[&gst::Buffer::from_slice(vec![0; 1024])])
|
2019-12-02 09:30:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let pipeline_clone = pipeline.clone();
|
2019-11-24 20:12:40 +00:00
|
|
|
let l_clone = l.clone();
|
2019-12-02 09:30:07 +00:00
|
|
|
let mut scenario = Some(move || {
|
|
|
|
// Initialize the dataflow
|
|
|
|
assert!(push_buffer(&src));
|
|
|
|
|
|
|
|
sample_notif_rcv.recv().unwrap();
|
|
|
|
|
2021-11-21 16:15:04 +00:00
|
|
|
assert!(src.emit_by_name::<bool>("end-of-stream", &[]));
|
2019-12-02 09:30:07 +00:00
|
|
|
|
|
|
|
eos_notif_rcv.recv().unwrap();
|
|
|
|
|
2020-04-20 19:35:06 +00:00
|
|
|
// FIXME not ideal, but better than previous approach.
|
|
|
|
// I think the "end-of-stream" signal should block
|
|
|
|
// until the **src** element has actually reached EOS
|
|
|
|
loop {
|
|
|
|
std::thread::sleep(std::time::Duration::from_millis(10));
|
|
|
|
if !push_buffer(&src) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2019-12-02 09:30:07 +00:00
|
|
|
|
|
|
|
pipeline_clone.set_state(gst::State::Null).unwrap();
|
|
|
|
l_clone.quit();
|
|
|
|
});
|
|
|
|
|
|
|
|
let l_clone = l.clone();
|
2023-04-14 09:46:43 +00:00
|
|
|
let _bus_watch = pipeline
|
2021-04-12 12:49:54 +00:00
|
|
|
.bus()
|
2019-11-30 18:51:31 +00:00
|
|
|
.unwrap()
|
|
|
|
.add_watch(move |_, msg| {
|
|
|
|
use gst::MessageView;
|
|
|
|
|
|
|
|
match msg.view() {
|
|
|
|
MessageView::StateChanged(state_changed) => {
|
2021-04-12 12:49:54 +00:00
|
|
|
if let Some(source) = state_changed.src() {
|
|
|
|
if source.type_() != gst::Pipeline::static_type() {
|
2023-07-06 13:27:28 +00:00
|
|
|
return glib::ControlFlow::Continue;
|
2019-11-30 18:51:31 +00:00
|
|
|
}
|
2021-04-12 12:49:54 +00:00
|
|
|
if state_changed.old() == gst::State::Paused
|
|
|
|
&& state_changed.current() == gst::State::Playing
|
2019-11-30 18:51:31 +00:00
|
|
|
{
|
|
|
|
if let Some(scenario) = scenario.take() {
|
|
|
|
std::thread::spawn(scenario);
|
|
|
|
}
|
2019-12-02 09:30:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-11-30 18:51:31 +00:00
|
|
|
MessageView::Error(err) => {
|
2022-02-21 17:43:46 +00:00
|
|
|
gst::error!(
|
2019-11-30 18:51:31 +00:00
|
|
|
CAT,
|
|
|
|
"eos: Error from {:?}: {} ({:?})",
|
2021-04-12 12:49:54 +00:00
|
|
|
err.src().map(|s| s.path_string()),
|
|
|
|
err.error(),
|
|
|
|
err.debug()
|
2019-11-30 18:51:31 +00:00
|
|
|
);
|
|
|
|
l_clone.quit();
|
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
};
|
2019-11-24 20:12:40 +00:00
|
|
|
|
2023-07-06 13:27:28 +00:00
|
|
|
glib::ControlFlow::Continue
|
2019-11-30 18:51:31 +00:00
|
|
|
})
|
|
|
|
.unwrap();
|
2019-11-24 20:12:40 +00:00
|
|
|
|
2019-12-02 09:30:07 +00:00
|
|
|
pipeline.set_state(gst::State::Playing).unwrap();
|
|
|
|
|
2022-02-21 17:43:46 +00:00
|
|
|
gst::debug!(CAT, "Starting main loop for eos...");
|
2019-12-02 09:30:07 +00:00
|
|
|
l.run();
|
2022-02-21 17:43:46 +00:00
|
|
|
gst::debug!(CAT, "Stopping main loop for eos...");
|
2019-12-02 09:30:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn premature_shutdown() {
|
|
|
|
init();
|
|
|
|
|
|
|
|
const APPSRC_CONTEXT_WAIT: u32 = 0;
|
|
|
|
const QUEUE_CONTEXT_WAIT: u32 = 1;
|
|
|
|
const QUEUE_ITEMS_CAPACITY: u32 = 1;
|
|
|
|
|
|
|
|
let l = glib::MainLoop::new(None, false);
|
2022-10-22 16:06:29 +00:00
|
|
|
let pipeline = gst::Pipeline::default();
|
2019-12-02 09:30:07 +00:00
|
|
|
|
2021-11-06 07:34:10 +00:00
|
|
|
let caps = gst::Caps::builder("foo/bar").build();
|
2019-12-02 09:30:07 +00:00
|
|
|
|
2022-10-19 16:18:43 +00:00
|
|
|
let src = gst::ElementFactory::make("ts-appsrc")
|
|
|
|
.name("src-ps")
|
|
|
|
.property("caps", &caps)
|
|
|
|
.property("do-timestamp", true)
|
|
|
|
.property("context", "appsrc-context")
|
|
|
|
.property("context-wait", APPSRC_CONTEXT_WAIT)
|
|
|
|
.build()
|
|
|
|
.unwrap();
|
2019-12-02 09:30:07 +00:00
|
|
|
|
2022-10-19 16:18:43 +00:00
|
|
|
let queue = gst::ElementFactory::make("ts-queue")
|
|
|
|
.name("queue-ps")
|
|
|
|
.property("context", "queue-context")
|
|
|
|
.property("context-wait", QUEUE_CONTEXT_WAIT)
|
|
|
|
.property("max-size-buffers", QUEUE_ITEMS_CAPACITY)
|
|
|
|
.build()
|
|
|
|
.unwrap();
|
2019-12-02 09:30:07 +00:00
|
|
|
|
2022-10-22 16:06:29 +00:00
|
|
|
let sink = gst_app::AppSink::builder()
|
2022-10-19 16:18:43 +00:00
|
|
|
.name("sink-ps")
|
2022-10-22 16:06:29 +00:00
|
|
|
.sync(false)
|
|
|
|
.async_(false)
|
|
|
|
.build();
|
2019-12-02 09:30:07 +00:00
|
|
|
|
2022-10-22 16:06:29 +00:00
|
|
|
pipeline
|
2023-03-09 14:46:52 +00:00
|
|
|
.add_many([&src, &queue, sink.upcast_ref()])
|
2022-10-22 16:06:29 +00:00
|
|
|
.unwrap();
|
2023-03-09 14:46:52 +00:00
|
|
|
gst::Element::link_many([&src, &queue, sink.upcast_ref()]).unwrap();
|
2019-12-02 09:30:07 +00:00
|
|
|
|
2020-03-19 18:34:51 +00:00
|
|
|
let (appsink_sender, appsink_receiver) = mpsc::channel();
|
2019-12-02 09:30:07 +00:00
|
|
|
|
2022-10-22 16:06:29 +00:00
|
|
|
sink.set_callbacks(
|
2021-05-31 07:33:50 +00:00
|
|
|
gst_app::AppSinkCallbacks::builder()
|
|
|
|
.new_sample(move |appsink| {
|
2022-02-21 17:43:46 +00:00
|
|
|
gst::debug!(CAT, obj: appsink, "premature_shutdown: pulling sample");
|
2021-11-21 16:15:04 +00:00
|
|
|
let _sample = appsink.pull_sample().unwrap();
|
2021-05-31 07:33:50 +00:00
|
|
|
|
|
|
|
appsink_sender.send(()).unwrap();
|
|
|
|
|
|
|
|
Ok(gst::FlowSuccess::Ok)
|
|
|
|
})
|
|
|
|
.build(),
|
|
|
|
);
|
2019-12-02 09:30:07 +00:00
|
|
|
|
2020-03-19 18:34:51 +00:00
|
|
|
fn push_buffer(src: &gst::Element, intent: &str) -> bool {
|
2022-02-21 17:43:46 +00:00
|
|
|
gst::debug!(
|
2020-03-19 18:34:51 +00:00
|
|
|
CAT,
|
|
|
|
obj: src,
|
|
|
|
"premature_shutdown: pushing buffer {}",
|
|
|
|
intent
|
|
|
|
);
|
2021-11-21 16:15:04 +00:00
|
|
|
src.emit_by_name::<bool>("push-buffer", &[&gst::Buffer::from_slice(vec![0; 1024])])
|
2019-12-02 09:30:07 +00:00
|
|
|
}
|
|
|
|
|
2019-11-24 20:12:40 +00:00
|
|
|
let pipeline_clone = pipeline.clone();
|
|
|
|
let l_clone = l.clone();
|
2019-12-02 09:30:07 +00:00
|
|
|
let mut scenario = Some(move || {
|
2022-02-21 17:43:46 +00:00
|
|
|
gst::debug!(CAT, "premature_shutdown: STEP 1: Playing");
|
2019-12-02 09:30:07 +00:00
|
|
|
// Initialize the dataflow
|
2020-03-19 18:34:51 +00:00
|
|
|
assert!(push_buffer(&src, "(initial)"));
|
2019-12-02 09:30:07 +00:00
|
|
|
|
|
|
|
// Wait for the buffer to reach AppSink
|
2020-03-19 18:34:51 +00:00
|
|
|
appsink_receiver.recv().unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
appsink_receiver.try_recv().unwrap_err(),
|
|
|
|
mpsc::TryRecvError::Empty
|
|
|
|
);
|
2019-12-02 09:30:07 +00:00
|
|
|
|
2020-03-19 18:34:51 +00:00
|
|
|
assert!(push_buffer(&src, "before Playing -> Paused"));
|
2019-12-02 09:30:07 +00:00
|
|
|
|
2022-02-21 17:43:46 +00:00
|
|
|
gst::debug!(CAT, "premature_shutdown: STEP 2: Playing -> Paused");
|
2019-12-02 09:30:07 +00:00
|
|
|
pipeline_clone.set_state(gst::State::Paused).unwrap();
|
|
|
|
|
2022-02-21 17:43:46 +00:00
|
|
|
gst::debug!(CAT, "premature_shutdown: STEP 3: Paused -> Playing");
|
2019-12-02 09:30:07 +00:00
|
|
|
pipeline_clone.set_state(gst::State::Playing).unwrap();
|
|
|
|
|
2022-02-21 17:43:46 +00:00
|
|
|
gst::debug!(CAT, "premature_shutdown: Playing again");
|
2019-12-02 09:30:07 +00:00
|
|
|
|
2022-02-21 17:43:46 +00:00
|
|
|
gst::debug!(CAT, "Waiting for buffer sent before Playing -> Paused");
|
2020-03-19 18:34:51 +00:00
|
|
|
appsink_receiver.recv().unwrap();
|
2019-12-02 09:30:07 +00:00
|
|
|
|
2020-03-19 18:34:51 +00:00
|
|
|
assert!(push_buffer(&src, "after Paused -> Playing"));
|
2022-02-21 17:43:46 +00:00
|
|
|
gst::debug!(CAT, "Waiting for buffer sent after Paused -> Playing");
|
2020-03-19 18:34:51 +00:00
|
|
|
appsink_receiver.recv().unwrap();
|
2019-11-24 20:12:40 +00:00
|
|
|
|
2019-12-02 09:30:07 +00:00
|
|
|
// Fill up the (dataqueue) and abruptly shutdown
|
2020-03-19 18:34:51 +00:00
|
|
|
assert!(push_buffer(&src, "filling 1"));
|
|
|
|
assert!(push_buffer(&src, "filling 2"));
|
2019-12-02 09:30:07 +00:00
|
|
|
|
2022-02-21 17:43:46 +00:00
|
|
|
gst::debug!(CAT, "premature_shutdown: STEP 4: Playing -> Null");
|
2019-12-02 09:30:07 +00:00
|
|
|
|
|
|
|
pipeline_clone.set_state(gst::State::Null).unwrap();
|
|
|
|
|
2020-03-19 18:34:51 +00:00
|
|
|
assert!(!push_buffer(&src, "after Null"));
|
2019-12-02 09:30:07 +00:00
|
|
|
|
|
|
|
l_clone.quit();
|
|
|
|
});
|
|
|
|
|
|
|
|
let l_clone = l.clone();
|
2023-04-14 09:46:43 +00:00
|
|
|
let _bus_watch = pipeline
|
2021-04-12 12:49:54 +00:00
|
|
|
.bus()
|
2019-11-30 18:51:31 +00:00
|
|
|
.unwrap()
|
|
|
|
.add_watch(move |_, msg| {
|
|
|
|
use gst::MessageView;
|
|
|
|
|
|
|
|
match msg.view() {
|
|
|
|
MessageView::StateChanged(state_changed) => {
|
2021-04-12 12:49:54 +00:00
|
|
|
if let Some(source) = state_changed.src() {
|
|
|
|
if source.type_() != gst::Pipeline::static_type() {
|
2023-07-06 13:27:28 +00:00
|
|
|
return glib::ControlFlow::Continue;
|
2019-11-30 18:51:31 +00:00
|
|
|
}
|
2021-04-12 12:49:54 +00:00
|
|
|
if state_changed.old() == gst::State::Paused
|
|
|
|
&& state_changed.current() == gst::State::Playing
|
2019-11-30 18:51:31 +00:00
|
|
|
{
|
|
|
|
if let Some(scenario) = scenario.take() {
|
|
|
|
std::thread::spawn(scenario);
|
|
|
|
}
|
2019-12-02 09:30:07 +00:00
|
|
|
}
|
|
|
|
}
|
2019-11-24 20:12:40 +00:00
|
|
|
}
|
2019-11-30 18:51:31 +00:00
|
|
|
MessageView::Error(err) => {
|
2022-02-21 17:43:46 +00:00
|
|
|
gst::error!(
|
2019-11-30 18:51:31 +00:00
|
|
|
CAT,
|
|
|
|
"premature_shutdown: Error from {:?}: {} ({:?})",
|
2021-04-12 12:49:54 +00:00
|
|
|
err.src().map(|s| s.path_string()),
|
|
|
|
err.error(),
|
|
|
|
err.debug()
|
2019-11-30 18:51:31 +00:00
|
|
|
);
|
|
|
|
l_clone.quit();
|
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
};
|
2019-11-24 20:12:40 +00:00
|
|
|
|
2023-07-06 13:27:28 +00:00
|
|
|
glib::ControlFlow::Continue
|
2019-11-30 18:51:31 +00:00
|
|
|
})
|
|
|
|
.unwrap();
|
2019-11-24 20:12:40 +00:00
|
|
|
|
|
|
|
pipeline.set_state(gst::State::Playing).unwrap();
|
|
|
|
|
2022-02-21 17:43:46 +00:00
|
|
|
gst::debug!(CAT, "Starting main loop for premature_shutdown...");
|
2019-11-24 20:12:40 +00:00
|
|
|
l.run();
|
2022-02-21 17:43:46 +00:00
|
|
|
gst::debug!(CAT, "Stopped main loop for premature_shutdown...");
|
2019-11-24 20:12:40 +00:00
|
|
|
}
|
2022-06-13 19:47:21 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn socket_play_null_play() {
|
|
|
|
use gio::{
|
|
|
|
prelude::SocketExt, InetAddress, InetSocketAddress, SocketFamily, SocketProtocol,
|
|
|
|
SocketType,
|
|
|
|
};
|
|
|
|
|
|
|
|
const TEST: &str = "socket_play_null_play";
|
|
|
|
|
|
|
|
init();
|
|
|
|
|
|
|
|
let l = glib::MainLoop::new(None, false);
|
2022-10-22 16:06:29 +00:00
|
|
|
let pipeline = gst::Pipeline::default();
|
2022-06-13 19:47:21 +00:00
|
|
|
|
|
|
|
let socket = gio::Socket::new(
|
|
|
|
SocketFamily::Ipv4,
|
|
|
|
SocketType::Datagram,
|
|
|
|
SocketProtocol::Udp,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
socket
|
|
|
|
.bind(
|
|
|
|
&InetSocketAddress::new(&InetAddress::from_string("127.0.0.1").unwrap(), 4500),
|
|
|
|
true,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
2022-10-19 16:18:43 +00:00
|
|
|
let sink = gst::ElementFactory::make("ts-udpsink")
|
2023-01-25 08:23:46 +00:00
|
|
|
.name(format!("sink-{TEST}").as_str())
|
2022-10-19 16:18:43 +00:00
|
|
|
.property("socket", &socket)
|
2022-11-01 08:27:48 +00:00
|
|
|
.property("context", TEST)
|
2022-10-19 16:18:43 +00:00
|
|
|
.property("context-wait", 20u32)
|
|
|
|
.build()
|
|
|
|
.unwrap();
|
2022-06-13 19:47:21 +00:00
|
|
|
|
|
|
|
pipeline.add(&sink).unwrap();
|
|
|
|
|
|
|
|
let pipeline_clone = pipeline.clone();
|
|
|
|
let l_clone = l.clone();
|
|
|
|
let (sender, receiver) = std::sync::mpsc::channel();
|
|
|
|
let mut scenario = Some(move || {
|
|
|
|
gst::debug!(CAT, "{}: to Null", TEST);
|
|
|
|
pipeline_clone.set_state(gst::State::Null).unwrap();
|
|
|
|
|
|
|
|
gst::debug!(CAT, "{}: Play again", TEST);
|
|
|
|
let res = pipeline_clone.set_state(gst::State::Playing);
|
|
|
|
l_clone.quit();
|
|
|
|
sender.send(res).unwrap();
|
|
|
|
});
|
|
|
|
|
|
|
|
let l_clone = l.clone();
|
2023-04-14 09:46:43 +00:00
|
|
|
let _bus_watch = pipeline
|
2022-06-13 19:47:21 +00:00
|
|
|
.bus()
|
|
|
|
.unwrap()
|
|
|
|
.add_watch(move |_, msg| {
|
|
|
|
use gst::MessageView;
|
|
|
|
|
|
|
|
match msg.view() {
|
|
|
|
MessageView::StateChanged(state_changed) => {
|
|
|
|
if let Some(source) = state_changed.src() {
|
|
|
|
if source.type_() != gst::Pipeline::static_type() {
|
2023-07-06 13:27:28 +00:00
|
|
|
return glib::ControlFlow::Continue;
|
2022-06-13 19:47:21 +00:00
|
|
|
}
|
|
|
|
if state_changed.old() == gst::State::Paused
|
|
|
|
&& state_changed.current() == gst::State::Playing
|
|
|
|
{
|
|
|
|
if let Some(scenario) = scenario.take() {
|
|
|
|
std::thread::spawn(scenario);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
MessageView::Error(err) => {
|
|
|
|
gst::error!(
|
|
|
|
CAT,
|
|
|
|
"{}: Error from {:?}: {} ({:?})",
|
|
|
|
TEST,
|
|
|
|
err.src().map(|s| s.path_string()),
|
|
|
|
err.error(),
|
|
|
|
err.debug()
|
|
|
|
);
|
|
|
|
l_clone.quit();
|
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
};
|
|
|
|
|
2023-07-06 13:27:28 +00:00
|
|
|
glib::ControlFlow::Continue
|
2022-06-13 19:47:21 +00:00
|
|
|
})
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
gst::debug!(CAT, "{}: Playing", TEST);
|
|
|
|
pipeline.set_state(gst::State::Playing).unwrap();
|
|
|
|
|
|
|
|
gst::debug!(CAT, "Starting main loop for {}...", TEST);
|
|
|
|
l.run();
|
|
|
|
gst::debug!(CAT, "Stopping main loop for {}...", TEST);
|
|
|
|
let _ = pipeline.set_state(gst::State::Null);
|
|
|
|
|
|
|
|
if let Err(err) = receiver.recv().unwrap() {
|
2023-01-25 08:23:46 +00:00
|
|
|
panic!("{TEST}: {err}");
|
2022-06-13 19:47:21 +00:00
|
|
|
}
|
|
|
|
}
|