Skip to main content

rtflow_core/
observe.rs

1//! Allocation-conscious control-plane events.
2use crate::{
3    error::{ErrorCode, Operation},
4    identity::{MountId, SessionId, TaskId},
5};
6use std::{any::Any, fmt, net::SocketAddr, sync::Arc, time::Duration};
7
8#[derive(Clone, Debug)]
9#[non_exhaustive]
10pub enum FrameworkEvent {
11    Server(ServerEvent),
12    Session(SessionEvent),
13    Mount(MountEvent),
14    Proxy(ProxyEvent),
15    Client(ClientEvent),
16    Pipeline(PipelineEvent),
17    Runtime(RuntimeEvent),
18    Security(SecurityEvent),
19}
20
21#[derive(Clone, Debug)]
22#[non_exhaustive]
23pub enum ServerEvent {
24    Started { local_addr: SocketAddr },
25    Stopping,
26    Stopped,
27    AcceptFailed { error: Arc<str> },
28}
29
30#[derive(Clone, Debug)]
31#[non_exhaustive]
32pub enum SessionEvent {
33    Accepted {
34        id: SessionId,
35        peer: SocketAddr,
36    },
37    Rejected {
38        peer: SocketAddr,
39        reason: RejectReason,
40    },
41    Closed {
42        id: SessionId,
43        peer: SocketAddr,
44        elapsed: Duration,
45        error: Option<ErrorCode>,
46    },
47    /// A playback session ended (TEARDOWN, PAUSE or disconnect) after having
48    /// played `mount`. Emitted once per distinct mount a session played.
49    PlaybackStopped {
50        peer: SocketAddr,
51        mount: MountId,
52        elapsed: Duration,
53    },
54}
55
56#[derive(Clone, Copy, Debug, Eq, PartialEq)]
57pub enum RejectReason {
58    GlobalLimit,
59    PeerLimit,
60    AccessDenied,
61    RateLimited,
62    ShuttingDown,
63}
64
65#[derive(Clone, Debug)]
66#[non_exhaustive]
67pub enum MountEvent {
68    Registered { id: MountId },
69    Unregistered { id: MountId },
70}
71
72#[derive(Clone, Debug)]
73#[non_exhaustive]
74pub enum ProxyEvent {
75    Connecting {
76        mount: MountId,
77        attempt: u32,
78    },
79    Streaming {
80        mount: MountId,
81    },
82    Backoff {
83        mount: MountId,
84        attempt: u32,
85        delay: Duration,
86    },
87    Failed {
88        mount: MountId,
89        code: ErrorCode,
90    },
91}
92
93/// RTSP client lifecycle events (receive/play sessions).
94#[derive(Clone, Debug)]
95#[non_exhaustive]
96pub enum ClientEvent {
97    Connected {
98        id: SessionId,
99        transport: &'static str,
100        peer: SocketAddr,
101    },
102    Fallback {
103        id: SessionId,
104        from: &'static str,
105        to: &'static str,
106    },
107    ReconnectBackoff {
108        id: SessionId,
109        attempt: u32,
110        delay: Duration,
111    },
112    KeepAliveFailed {
113        id: SessionId,
114        consecutive: u32,
115    },
116    Redirect {
117        id: SessionId,
118        location: String,
119    },
120    Failed {
121        id: SessionId,
122        code: ErrorCode,
123    },
124    Closed {
125        id: SessionId,
126        elapsed: Duration,
127        error: Option<ErrorCode>,
128    },
129}
130
131#[derive(Clone, Debug)]
132#[non_exhaustive]
133pub enum PipelineEvent {
134    ElementFailed { element: Arc<str>, code: ErrorCode },
135}
136
137#[derive(Clone, Debug)]
138#[non_exhaustive]
139pub enum RuntimeEvent {
140    TaskStarted {
141        id: TaskId,
142        name: Arc<str>,
143    },
144    TaskFinished {
145        id: TaskId,
146        name: Arc<str>,
147    },
148    TaskFailed {
149        id: TaskId,
150        name: Arc<str>,
151        operation: Operation,
152        code: ErrorCode,
153    },
154    TaskPanicked {
155        id: TaskId,
156        name: Arc<str>,
157    },
158    TaskCancelled {
159        id: TaskId,
160        name: Arc<str>,
161    },
162    TaskAborted {
163        id: TaskId,
164        name: Arc<str>,
165    },
166}
167
168#[derive(Clone, Debug)]
169#[non_exhaustive]
170pub enum SecurityEvent {
171    AuthenticationFailed {
172        peer: SocketAddr,
173        mount: Option<MountId>,
174    },
175    RateLimited {
176        peer: SocketAddr,
177    },
178    TlsHandshakeFailed {
179        peer: SocketAddr,
180        error: Arc<str>,
181    },
182}
183
184pub trait EventSink: Send + Sync + Any + 'static {
185    fn record(&self, event: &FrameworkEvent);
186    /// Downcast support so infrastructure can extract concrete sinks
187    /// (e.g. the metrics registry from a [`MetricsEventSink`](crate::metrics::MetricsEventSink)).
188    fn as_any(&self) -> &dyn Any;
189}
190
191#[derive(Default)]
192pub struct NoopEventSink;
193impl EventSink for NoopEventSink {
194    fn record(&self, _: &FrameworkEvent) {}
195    fn as_any(&self) -> &dyn Any {
196        self
197    }
198}
199
200#[derive(Default)]
201pub struct TracingEventSink;
202impl EventSink for TracingEventSink {
203    fn record(&self, event: &FrameworkEvent) {
204        tracing::info!(target: "rtflow::event", event = ?event, "framework event");
205    }
206    fn as_any(&self) -> &dyn Any {
207        self
208    }
209}
210
211impl fmt::Debug for dyn EventSink {
212    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
213        formatter.write_str("EventSink")
214    }
215}
216
217/// Deliver one framework event to a sink (cross-crate event plumbing).
218#[doc(hidden)]
219pub fn emit(sink: &Arc<dyn EventSink>, event: FrameworkEvent) {
220    sink.record(&event);
221}