@@ -3,7 +3,7 @@ defmodule Shinkai.Config do
33
44 use GenServer
55
6- @ top_level_keys [ :rtmp , :server , :hls ]
6+ @ top_level_keys [ :rtmp , :server , :hls , :rtsp ]
77
88 @ rtmp_schema [
99 enabled: [
@@ -14,7 +14,24 @@ defmodule Shinkai.Config do
1414 port: [
1515 type: { :in , 0 .. ( 2 ** 16 - 1 ) } ,
1616 default: 1935 ,
17- doc: "RTMP listening port"
17+ doc: "RTMP listening port" ,
18+ type_doc: "`t::socket.port_number/0`" ,
19+ type_spec: quote do: :socket . port_number ( )
20+ ]
21+ ]
22+
23+ @ rtsp_schema [
24+ enabled: [
25+ type: :boolean ,
26+ default: true ,
27+ doc: "Enable or disable rtsp"
28+ ] ,
29+ port: [
30+ type: { :in , 0 .. ( 2 ** 16 - 1 ) } ,
31+ default: 8554 ,
32+ doc: "RTSP listening port" ,
33+ type_doc: "`t::socket.port_number/0`" ,
34+ type_spec: quote do: :socket . port_number ( )
1835 ]
1936 ]
2037
@@ -27,7 +44,9 @@ defmodule Shinkai.Config do
2744 port: [
2845 type: { :in , 0 .. ( 2 ** 16 - 1 ) } ,
2946 default: 8888 ,
30- doc: "http port"
47+ doc: "http port" ,
48+ type_doc: "`t::socket.port_number/0`" ,
49+ type_spec: quote do: :socket . port_number ( )
3150 ] ,
3251 certfile: [
3352 type: { :or , [ :string , nil ] } ,
@@ -44,7 +63,8 @@ defmodule Shinkai.Config do
4463 @ hls_schema [
4564 storage_dir: [
4665 type: :string ,
47- default: "/tmp/shinkai/hls"
66+ default: "/tmp/shinkai/hls" ,
67+ doc: "Directory to store HLS segments"
4868 ] ,
4969 max_segments: [
5070 type: :non_neg_integer ,
@@ -53,18 +73,37 @@ defmodule Shinkai.Config do
5373 ] ,
5474 segment_duration: [
5575 type: :non_neg_integer ,
56- default: 2000
76+ default: 2000 ,
77+ doc: "Segment duration in milliseconds"
5778 ] ,
5879 part_duration: [
5980 type: :non_neg_integer ,
60- default: 300
81+ default: 300 ,
82+ doc: "Part duration in milliseconds for low-latency HLS"
6183 ] ,
6284 segment_type: [
6385 type: { :custom , __MODULE__ , :validate_hls_segment_type , [ ] } ,
64- default: :fmp4
86+ default: :fmp4 ,
87+ doc: "Type of segments to generate, either `:fmp4`, `:mpeg_ts` or `:low_latency`"
6588 ]
6689 ]
6790
91+ @ doc false
92+ @ spec server_schema ( ) :: keyword ( )
93+ def server_schema , do: @ server_schema
94+
95+ @ doc false
96+ @ spec rtmp_schema ( ) :: keyword ( )
97+ def rtmp_schema , do: @ rtmp_schema
98+
99+ @ doc false
100+ @ spec hls_schema ( ) :: keyword ( )
101+ def hls_schema , do: @ hls_schema
102+
103+ @ doc false
104+ @ spec rtsp_schema ( ) :: keyword ( )
105+ def rtsp_schema , do: @ rtsp_schema
106+
68107 def start_link ( config ) do
69108 GenServer . start_link ( __MODULE__ , config , name: __MODULE__ )
70109 end
@@ -88,10 +127,15 @@ defmodule Shinkai.Config do
88127
89128 app_configs = Enum . map ( @ top_level_keys , & { & 1 , Application . get_env ( :shinkai , & 1 , [ ] ) } )
90129
91- Enum . map ( @ top_level_keys , fn key ->
92- [ ]
93- |> Keyword . merge ( app_configs [ key ] )
94- |> Keyword . merge ( user_config [ key ] )
130+ app_configs =
131+ @ top_level_keys
132+ |> Enum . map ( & { & 1 , [ ] } )
133+ |> Keyword . merge ( app_configs )
134+ |> parse_and_validate ( )
135+
136+ Enum . map ( app_configs , fn { key , config } ->
137+ config
138+ |> Keyword . merge ( user_config [ key ] || [ ] )
95139 |> then ( & { key , & 1 } )
96140 end )
97141 end
@@ -120,9 +164,7 @@ defmodule Shinkai.Config do
120164
121165 case Map . keys ( config ) -- keys do
122166 [ ] ->
123- @ top_level_keys
124- |> Enum . map ( & { & 1 , [ ] } )
125- |> Keyword . merge ( Enum . map ( config , fn { key , value } -> { String . to_atom ( key ) , value } end ) )
167+ Enum . map ( config , fn { key , value } -> { String . to_atom ( key ) , value } end )
126168
127169 invalid_keys ->
128170 raise ArgumentError , """
@@ -137,19 +179,16 @@ defmodule Shinkai.Config do
137179
138180 defp parse_and_validate ( [ ] , acc ) , do: acc
139181
140- defp parse_and_validate ( [ { :hls , hls_config } | rest ] , acc ) do
141- hls_config = do_parse_and_validate ( hls_config , @ hls_schema )
142- parse_and_validate ( rest , [ { :hls , hls_config } | acc ] )
143- end
144-
145- defp parse_and_validate ( [ { :server , server_config } | rest ] , acc ) do
146- server_config = do_parse_and_validate ( server_config , @ server_schema )
147- parse_and_validate ( rest , [ { :server , server_config } | acc ] )
148- end
182+ defp parse_and_validate ( [ { key , config } | rest ] , acc ) do
183+ config =
184+ case key do
185+ :hls -> do_parse_and_validate ( config , @ hls_schema )
186+ :server -> do_parse_and_validate ( config , @ server_schema )
187+ :rtmp -> do_parse_and_validate ( config , @ rtmp_schema )
188+ :rtsp -> do_parse_and_validate ( config , @ rtsp_schema )
189+ end
149190
150- defp parse_and_validate ( [ { :rtmp , rtmp_config } | rest ] , acc ) do
151- rtmp_config = do_parse_and_validate ( rtmp_config , @ rtmp_schema )
152- parse_and_validate ( rest , [ { :rtmp , rtmp_config } | acc ] )
191+ parse_and_validate ( rest , [ { key , config } | acc ] )
153192 end
154193
155194 defp do_parse_and_validate ( config , schema ) do
0 commit comments