use crate::error::SCError;
pub trait SCStreamDelegateTrait: Send {
fn output_video_effect_did_start_for_stream(&self) {}
fn output_video_effect_did_stop_for_stream(&self) {}
fn stream_did_become_active(&self) {}
fn stream_did_become_inactive(&self) {}
fn did_stop_with_error(&self, _error: SCError) {}
fn stream_did_stop(&self, _error: Option<String>) {}
}
pub struct ErrorHandler<F>
where
F: Fn(SCError) + Send + 'static,
{
handler: F,
}
impl<F> ErrorHandler<F>
where
F: Fn(SCError) + Send + 'static,
{
pub fn new(handler: F) -> Self {
Self { handler }
}
}
impl<F> SCStreamDelegateTrait for ErrorHandler<F>
where
F: Fn(SCError) + Send + 'static,
{
fn did_stop_with_error(&self, error: SCError) {
(self.handler)(error);
}
}
#[allow(clippy::struct_field_names)]
pub struct StreamCallbacks {
on_stop: Option<Box<dyn Fn(Option<String>) + Send + 'static>>,
on_error: Option<Box<dyn Fn(SCError) + Send + 'static>>,
on_active: Option<Box<dyn Fn() + Send + 'static>>,
on_inactive: Option<Box<dyn Fn() + Send + 'static>>,
on_video_effect_start: Option<Box<dyn Fn() + Send + 'static>>,
on_video_effect_stop: Option<Box<dyn Fn() + Send + 'static>>,
}
impl StreamCallbacks {
#[must_use]
pub fn new() -> Self {
Self {
on_stop: None,
on_error: None,
on_active: None,
on_inactive: None,
on_video_effect_start: None,
on_video_effect_stop: None,
}
}
#[must_use]
pub fn on_stop<F>(mut self, f: F) -> Self
where
F: Fn(Option<String>) + Send + 'static,
{
self.on_stop = Some(Box::new(f));
self
}
#[must_use]
pub fn on_error<F>(mut self, f: F) -> Self
where
F: Fn(SCError) + Send + 'static,
{
self.on_error = Some(Box::new(f));
self
}
#[must_use]
pub fn on_active<F>(mut self, f: F) -> Self
where
F: Fn() + Send + 'static,
{
self.on_active = Some(Box::new(f));
self
}
#[must_use]
pub fn on_inactive<F>(mut self, f: F) -> Self
where
F: Fn() + Send + 'static,
{
self.on_inactive = Some(Box::new(f));
self
}
#[must_use]
pub fn on_video_effect_start<F>(mut self, f: F) -> Self
where
F: Fn() + Send + 'static,
{
self.on_video_effect_start = Some(Box::new(f));
self
}
#[must_use]
pub fn on_video_effect_stop<F>(mut self, f: F) -> Self
where
F: Fn() + Send + 'static,
{
self.on_video_effect_stop = Some(Box::new(f));
self
}
}
impl Default for StreamCallbacks {
fn default() -> Self {
Self::new()
}
}
impl SCStreamDelegateTrait for StreamCallbacks {
fn stream_did_stop(&self, error: Option<String>) {
if let Some(ref f) = self.on_stop {
f(error);
}
}
fn did_stop_with_error(&self, error: SCError) {
if let Some(ref f) = self.on_error {
f(error);
}
}
fn stream_did_become_active(&self) {
if let Some(ref f) = self.on_active {
f();
}
}
fn stream_did_become_inactive(&self) {
if let Some(ref f) = self.on_inactive {
f();
}
}
fn output_video_effect_did_start_for_stream(&self) {
if let Some(ref f) = self.on_video_effect_start {
f();
}
}
fn output_video_effect_did_stop_for_stream(&self) {
if let Some(ref f) = self.on_video_effect_stop {
f();
}
}
}