1 pub mod errors;
2
3 use errors::ConfigError;
4 use serde_derive::Deserialize;
5 use std::fs;
6 use std::vec::Vec;
7
8 #[derive(Debug, Deserialize, Clone)]
9 pub struct Config {
10 pub rtmp: Option<RtmpConfig>,
11 pub rtsp: Option<RtspConfig>,
12 pub webrtc: Option<WebRTCConfig>,
13 pub httpflv: Option<HttpFlvConfig>,
14 pub hls: Option<HlsConfig>,
15 pub httpapi: Option<HttpApiConfig>,
16 pub httpnotify: Option<HttpNotifierConfig>,
17 pub log: Option<LogConfig>,
18 }
19
20 impl Config {
new( rtmp_port: usize, rtsp_port: usize, webrtc_port: usize, httpflv_port: usize, hls_port: usize, log_level: String, ) -> Self21 pub fn new(
22 rtmp_port: usize,
23 rtsp_port: usize,
24 webrtc_port: usize,
25 httpflv_port: usize,
26 hls_port: usize,
27 log_level: String,
28 ) -> Self {
29 let mut rtmp_config: Option<RtmpConfig> = None;
30 if rtmp_port > 0 {
31 rtmp_config = Some(RtmpConfig {
32 enabled: true,
33 gop_num: Some(1),
34 port: rtmp_port,
35 pull: None,
36 push: None,
37 });
38 }
39
40 let mut rtsp_config: Option<RtspConfig> = None;
41 if rtsp_port > 0 {
42 rtsp_config = Some(RtspConfig {
43 enabled: true,
44 port: rtsp_port,
45 });
46 }
47
48 let mut webrtc_config: Option<WebRTCConfig> = None;
49 if webrtc_port > 0 {
50 webrtc_config = Some(WebRTCConfig {
51 enabled: true,
52 port: webrtc_port,
53 });
54 }
55
56 let mut httpflv_config: Option<HttpFlvConfig> = None;
57 if httpflv_port > 0 {
58 httpflv_config = Some(HttpFlvConfig {
59 enabled: true,
60 port: httpflv_port,
61 });
62 }
63
64 let mut hls_config: Option<HlsConfig> = None;
65 if hls_port > 0 {
66 hls_config = Some(HlsConfig {
67 enabled: true,
68 port: hls_port,
69 need_record: false,
70 });
71 }
72
73 let log_config = Some(LogConfig {
74 level: log_level,
75 file: None,
76 });
77
78 Self {
79 rtmp: rtmp_config,
80 rtsp: rtsp_config,
81 webrtc: webrtc_config,
82 httpflv: httpflv_config,
83 hls: hls_config,
84 httpapi: None,
85 httpnotify: None,
86 log: log_config,
87 }
88 }
89 }
90
91 #[derive(Debug, Deserialize, Clone)]
92 pub struct RtmpConfig {
93 pub enabled: bool,
94 pub port: usize,
95 pub gop_num: Option<usize>,
96 pub pull: Option<RtmpPullConfig>,
97 pub push: Option<Vec<RtmpPushConfig>>,
98 }
99 #[derive(Debug, Deserialize, Clone)]
100 pub struct RtmpPullConfig {
101 pub enabled: bool,
102 pub address: String,
103 pub port: u16,
104 }
105 #[derive(Debug, Deserialize, Clone)]
106 pub struct RtmpPushConfig {
107 pub enabled: bool,
108 pub address: String,
109 pub port: usize,
110 }
111
112 #[derive(Debug, Deserialize, Clone)]
113 pub struct RtspConfig {
114 pub enabled: bool,
115 pub port: usize,
116 }
117
118 #[derive(Debug, Deserialize, Clone)]
119 pub struct WebRTCConfig {
120 pub enabled: bool,
121 pub port: usize,
122 }
123
124 #[derive(Debug, Deserialize, Clone)]
125 pub struct HttpFlvConfig {
126 pub enabled: bool,
127 pub port: usize,
128 }
129
130 #[derive(Debug, Deserialize, Clone)]
131 pub struct HlsConfig {
132 pub enabled: bool,
133 pub port: usize,
134 //record or not
135 pub need_record: bool,
136 }
137
138 pub enum LogLevel {
139 Info,
140 Warn,
141 Error,
142 Trace,
143 Debug,
144 }
145
146 #[derive(Debug, Deserialize, Clone)]
147 pub struct LogConfig {
148 pub level: String,
149 pub file: Option<LogFile>,
150 }
151
152 #[derive(Debug, Deserialize, Clone)]
153 pub struct LogFile {
154 pub enabled: bool,
155 pub rotate: String,
156 pub path: String,
157 }
158
159 #[derive(Debug, Deserialize, Clone)]
160 pub struct HttpApiConfig {
161 pub port: usize,
162 }
163
164 #[derive(Debug, Deserialize, Clone)]
165 pub struct HttpNotifierConfig {
166 pub enabled: bool,
167 pub on_publish: Option<String>,
168 pub on_unpublish: Option<String>,
169 pub on_play: Option<String>,
170 pub on_stop: Option<String>,
171 }
172
load(cfg_path: &String) -> Result<Config, ConfigError>173 pub fn load(cfg_path: &String) -> Result<Config, ConfigError> {
174 let content = fs::read_to_string(cfg_path)?;
175 let decoded_config = toml::from_str(&content[..]).unwrap();
176 Ok(decoded_config)
177 }
178
179 #[test]
test_toml_parse()180 fn test_toml_parse() {
181 let path = std::env::current_dir();
182 match path {
183 Ok(val) => println!("The current directory is {}\n", val.display()),
184 Err(err) => println!("{}", err),
185 }
186
187 let str = fs::read_to_string(
188 "./src/config/config.toml",
189 );
190
191 match str {
192 Ok(val) => {
193 println!("++++++{val}\n");
194 let decoded: Config = toml::from_str(&val[..]).unwrap();
195
196 let rtmp = decoded.httpnotify;
197
198 if let Some(val) = rtmp {
199 println!("++++++{val:?}\n");
200 }
201 }
202 Err(err) => println!("======{err}"),
203 }
204 }
205