Skip to content

Commit 4084f7a

Browse files
authored
Server handler opts (#41)
* Add an option to pass an argument to the handler
1 parent d23c2cc commit 4084f7a

File tree

5 files changed

+48
-18
lines changed

5 files changed

+48
-18
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ of dependencies in `mix.exs`:
1818
```elixir
1919
def deps do
2020
[
21-
{:membrane_rtsp, "~> 0.7.2"}
21+
{:membrane_rtsp, "~> 0.8.0"}
2222
]
2323
end
2424
```

lib/membrane_rtsp/server.ex

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,16 @@ defmodule Membrane.RTSP.Server do
2828

2929
alias __MODULE__.Conn
3030

31-
@type server_config :: [
32-
handler: module(),
33-
name: term(),
34-
port: :inet.port_number(),
35-
address: :inet.ip_address(),
36-
udp_rtp_port: :inet.port_number(),
37-
udp_rtcp_port: :inet.port_number(),
38-
session_timeout: non_neg_integer()
39-
]
31+
@type server_option ::
32+
{:handler, module()}
33+
| {:handler_config, term()}
34+
| {:name, term()}
35+
| {:port, :inet.port_number()}
36+
| {:address, :inet.ip_address()}
37+
| {:udp_rtp_port, :inet.port_number()}
38+
| {:udp_rtcp_port, :inet.port_number()}
39+
| {:session_timeout, non_neg_integer()}
40+
@type server_config :: [server_option()]
4041

4142
@doc """
4243
Start an instance of the RTSP server.
@@ -54,6 +55,7 @@ defmodule Membrane.RTSP.Server do
5455
## Options
5556
- `handler` - An implementation of the behaviour `Membrane.RTSP.Server.Handler`. Refer to the module
5657
documentation for more details. This field is required.
58+
- `handler_config` - Term that will be passed as an argument to `init/1` callback of the handler. Defaults to `nil`.
5759
- `name` - Used for name registration of the server. Defaults to `nil`.
5860
- `port` - The port where the server will listen for client connections. Defaults to: `554`
5961
- `address` - Specify the address where the `tcp` and `udp` sockets will be bound. Defaults to `:any`.
@@ -115,6 +117,7 @@ defmodule Membrane.RTSP.Server do
115117
state = %{
116118
socket: socket,
117119
handler: config[:handler],
120+
handler_state: config[:handler].init(config[:handler_config]),
118121
udp_rtp_socket: udp_rtp_socket,
119122
udp_rtcp_socket: udp_rtcp_socket,
120123
client_conns: %{},
@@ -136,7 +139,7 @@ defmodule Membrane.RTSP.Server do
136139
def handle_info({:new_connection, client_socket}, state) do
137140
child_state =
138141
state
139-
|> Map.take([:handler, :session_timeout, :udp_rtp_socket, :udp_rtcp_socket])
142+
|> Map.take([:handler, :handler_state, :session_timeout, :udp_rtp_socket, :udp_rtcp_socket])
140143
|> Map.put(:socket, client_socket)
141144

142145
case Conn.start(child_state) do

lib/membrane_rtsp/server/conn.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ defmodule Membrane.RTSP.Server.Conn do
1818
state = %Logic.State{
1919
socket: config.socket,
2020
request_handler: config.handler,
21-
request_handler_state: config.handler.handle_open_connection(config.socket),
21+
request_handler_state:
22+
config.handler.handle_open_connection(config.socket, config.handler_state),
2223
rtp_socket: config.udp_rtp_socket,
2324
rtcp_socket: config.udp_rtcp_socket,
2425
session_timeout: config.session_timeout

lib/membrane_rtsp/server/handler.ex

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ defmodule Membrane.RTSP.Server.Handler do
5353
5454
## State
5555
The handler may need to keep some state between the callback calls. To achieve this, the returned value from
56-
`c:handle_open_connection/1` callback will be used as a state and will be the last argument for the other callbacks.
56+
`c:init/1` callback will be used as a state and will be the last argument for the other callbacks.
5757
5858
> #### `Missing callbacks in the handler` {: .info}
5959
>
@@ -64,6 +64,10 @@ defmodule Membrane.RTSP.Server.Handler do
6464
> `GET_PARAMETER` is used to keep the session alive and the server is responsible for setting session timeout
6565
>
6666
> The other methods are not yet implemented.
67+
68+
## `use Membrane.RTSP.Server.Handler` {: .info}
69+
When you `use Membrane.RTSP.Server.Handler`, the module will set `@behaviour Membrane.RTSP.Server.Handler` and
70+
define the default implementation for `init/1` callback.
6771
"""
6872

6973
alias Membrane.RTSP.{Request, Response}
@@ -72,6 +76,7 @@ defmodule Membrane.RTSP.Server.Handler do
7276
Any term that will be used to keep state between the callbacks.
7377
"""
7478
@type state :: term()
79+
@type config :: term()
7580
@type control_path :: binary()
7681
@type ssrc :: non_neg_integer()
7782
@type conn :: :inet.socket()
@@ -106,12 +111,20 @@ defmodule Membrane.RTSP.Server.Handler do
106111
}
107112

108113
@doc """
109-
Callback called when a new connection is established.
114+
Optional callback called when the server is initialized.
115+
116+
The argument is a term passed to the server as the `handler_config` option.
117+
The returned value will be used as a state and passed as the last
118+
argument to the subsequent callbacks.
110119
111-
The returned value is used as a state and is passed as the last argument to
112-
the subsequent callbacks
120+
Default behavior is to return the argument unchanged.
113121
"""
114-
@callback handle_open_connection(conn()) :: state()
122+
@callback init(config()) :: state()
123+
124+
@doc """
125+
Callback called when a new connection is established.
126+
"""
127+
@callback handle_open_connection(conn(), state()) :: state()
115128

116129
@doc """
117130
Callback called when a connection is closed.
@@ -160,4 +173,17 @@ defmodule Membrane.RTSP.Server.Handler do
160173
the session.
161174
"""
162175
@callback handle_teardown(state()) :: {Response.t(), state()}
176+
177+
@optional_callbacks init: 1
178+
179+
defmacro __using__(_options) do
180+
quote do
181+
@behaviour unquote(__MODULE__)
182+
183+
@impl true
184+
def init(config), do: config
185+
186+
defoverridable init: 1
187+
end
188+
end
163189
end

mix.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
defmodule Membrane.RTSP.MixProject do
22
use Mix.Project
33

4-
@version "0.7.2"
4+
@version "0.8.0"
55
@github_url "https://github.com/membraneframework/membrane_rtsp"
66

77
def project do

0 commit comments

Comments
 (0)