rtmp_rs/session/
context.rs

1//! Handler context
2//!
3//! Context passed to handler callbacks containing session information
4//! and methods to interact with the connection.
5
6use std::net::SocketAddr;
7use std::sync::Arc;
8
9use crate::protocol::message::ConnectParams;
10use crate::protocol::quirks::EncoderType;
11use crate::stats::SessionStats;
12
13/// Context passed to RtmpHandler callbacks
14///
15/// Provides read-only access to session information. For operations
16/// that modify state, use the return values from handler methods.
17#[derive(Debug, Clone)]
18pub struct SessionContext {
19    /// Unique session ID
20    pub session_id: u64,
21
22    /// Remote peer address
23    pub peer_addr: SocketAddr,
24
25    /// Application name (from connect)
26    pub app: String,
27
28    /// Detected encoder type
29    pub encoder_type: EncoderType,
30
31    /// Connect parameters (if available)
32    pub connect_params: Option<Arc<ConnectParams>>,
33
34    /// Current session statistics
35    pub stats: SessionStats,
36}
37
38impl SessionContext {
39    /// Create a new context
40    pub fn new(session_id: u64, peer_addr: SocketAddr) -> Self {
41        Self {
42            session_id,
43            peer_addr,
44            app: String::new(),
45            encoder_type: EncoderType::Unknown,
46            connect_params: None,
47            stats: SessionStats::default(),
48        }
49    }
50
51    /// Update with connect parameters
52    pub fn with_connect(&mut self, params: ConnectParams, encoder_type: EncoderType) {
53        self.app = params.app.clone();
54        self.encoder_type = encoder_type;
55        self.connect_params = Some(Arc::new(params));
56    }
57
58    /// Get the TC URL if available
59    pub fn tc_url(&self) -> Option<&str> {
60        self.connect_params
61            .as_ref()
62            .and_then(|p| p.tc_url.as_deref())
63    }
64
65    /// Get the page URL if available
66    pub fn page_url(&self) -> Option<&str> {
67        self.connect_params
68            .as_ref()
69            .and_then(|p| p.page_url.as_deref())
70    }
71
72    /// Get the flash version string if available
73    pub fn flash_ver(&self) -> Option<&str> {
74        self.connect_params
75            .as_ref()
76            .and_then(|p| p.flash_ver.as_deref())
77    }
78}
79
80/// Stream context passed to media callbacks
81#[derive(Debug, Clone)]
82pub struct StreamContext {
83    /// Parent session context
84    pub session: SessionContext,
85
86    /// Message stream ID
87    pub stream_id: u32,
88
89    /// Stream key
90    pub stream_key: String,
91
92    /// Whether this is a publishing or playing stream
93    pub is_publishing: bool,
94}
95
96impl StreamContext {
97    /// Create a new stream context
98    pub fn new(
99        session: SessionContext,
100        stream_id: u32,
101        stream_key: String,
102        is_publishing: bool,
103    ) -> Self {
104        Self {
105            session,
106            stream_id,
107            stream_key,
108            is_publishing,
109        }
110    }
111}