scuffle_rtmp/session/server/
handler.rs

1//! Defines types for handling session events.
2
3use bytes::Bytes;
4
5use super::error::ServerSessionError;
6use crate::command_messages::UnknownCommand;
7use crate::messages::UnknownMessage;
8
9/// Data received from a session.
10#[derive(Debug, Clone)]
11pub enum SessionData {
12    /// Video data.
13    Video {
14        /// Timestamp of the data.
15        timestamp: u32,
16        /// Data.
17        data: Bytes,
18    },
19    /// Audio data.
20    Audio {
21        /// Timestamp of the data.
22        timestamp: u32,
23        /// Data.
24        data: Bytes,
25    },
26    /// Metadata.
27    Amf0 {
28        /// Timestamp of the data.
29        timestamp: u32,
30        /// Data.
31        data: Bytes,
32    },
33}
34
35/// Handler for session events.
36pub trait SessionHandler {
37    /// Called when a stream is published.
38    fn on_publish(
39        &mut self,
40        stream_id: u32,
41        app_name: &str,
42        stream_name: &str,
43    ) -> impl std::future::Future<Output = Result<(), ServerSessionError>> + Send;
44
45    /// Called when a stream is unpublished.
46    fn on_unpublish(&mut self, stream_id: u32) -> impl std::future::Future<Output = Result<(), ServerSessionError>> + Send;
47
48    /// Called when an unknown/undefined message is received.
49    fn on_unknown_message(
50        &mut self,
51        stream_id: u32,
52        message: UnknownMessage,
53    ) -> impl std::future::Future<Output = Result<(), ServerSessionError>> + Send {
54        async move {
55            tracing::warn!(stream_id = %stream_id, message = ?message, "unknown message");
56            Ok(())
57        }
58    }
59
60    /// Called when an unknown/undefined command is received.
61    fn on_unknown_command(
62        &mut self,
63        stream_id: u32,
64        command: UnknownCommand,
65    ) -> impl std::future::Future<Output = Result<(), ServerSessionError>> + Send {
66        async move {
67            tracing::debug!(stream_id = %stream_id, command = ?command, "unknown command");
68            Ok(())
69        }
70    }
71
72    /// Called when data is received.
73    fn on_data(
74        &mut self,
75        stream_id: u32,
76        data: SessionData,
77    ) -> impl std::future::Future<Output = Result<(), ServerSessionError>> + Send;
78}