Skip to content

Commit

Permalink
Server handler opts (#41)
Browse files Browse the repository at this point in the history
* Add an option to pass an argument to the handler
  • Loading branch information
Noarkhh committed Aug 23, 2024
1 parent d23c2cc commit 4084f7a
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 18 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ of dependencies in `mix.exs`:
```elixir
def deps do
[
{:membrane_rtsp, "~> 0.7.2"}
{:membrane_rtsp, "~> 0.8.0"}
]
end
```
Expand Down
23 changes: 13 additions & 10 deletions lib/membrane_rtsp/server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,16 @@ defmodule Membrane.RTSP.Server do

alias __MODULE__.Conn

@type server_config :: [
handler: module(),
name: term(),
port: :inet.port_number(),
address: :inet.ip_address(),
udp_rtp_port: :inet.port_number(),
udp_rtcp_port: :inet.port_number(),
session_timeout: non_neg_integer()
]
@type server_option ::
{:handler, module()}
| {:handler_config, term()}
| {:name, term()}
| {:port, :inet.port_number()}
| {:address, :inet.ip_address()}
| {:udp_rtp_port, :inet.port_number()}
| {:udp_rtcp_port, :inet.port_number()}
| {:session_timeout, non_neg_integer()}
@type server_config :: [server_option()]

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

case Conn.start(child_state) do
Expand Down
3 changes: 2 additions & 1 deletion lib/membrane_rtsp/server/conn.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ defmodule Membrane.RTSP.Server.Conn do
state = %Logic.State{
socket: config.socket,
request_handler: config.handler,
request_handler_state: config.handler.handle_open_connection(config.socket),
request_handler_state:
config.handler.handle_open_connection(config.socket, config.handler_state),
rtp_socket: config.udp_rtp_socket,
rtcp_socket: config.udp_rtcp_socket,
session_timeout: config.session_timeout
Expand Down
36 changes: 31 additions & 5 deletions lib/membrane_rtsp/server/handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ defmodule Membrane.RTSP.Server.Handler do
## State
The handler may need to keep some state between the callback calls. To achieve this, the returned value from
`c:handle_open_connection/1` callback will be used as a state and will be the last argument for the other callbacks.
`c:init/1` callback will be used as a state and will be the last argument for the other callbacks.
> #### `Missing callbacks in the handler` {: .info}
>
Expand All @@ -64,6 +64,10 @@ defmodule Membrane.RTSP.Server.Handler do
> `GET_PARAMETER` is used to keep the session alive and the server is responsible for setting session timeout
>
> The other methods are not yet implemented.
## `use Membrane.RTSP.Server.Handler` {: .info}
When you `use Membrane.RTSP.Server.Handler`, the module will set `@behaviour Membrane.RTSP.Server.Handler` and
define the default implementation for `init/1` callback.
"""

alias Membrane.RTSP.{Request, Response}
Expand All @@ -72,6 +76,7 @@ defmodule Membrane.RTSP.Server.Handler do
Any term that will be used to keep state between the callbacks.
"""
@type state :: term()
@type config :: term()
@type control_path :: binary()
@type ssrc :: non_neg_integer()
@type conn :: :inet.socket()
Expand Down Expand Up @@ -106,12 +111,20 @@ defmodule Membrane.RTSP.Server.Handler do
}

@doc """
Callback called when a new connection is established.
Optional callback called when the server is initialized.
The argument is a term passed to the server as the `handler_config` option.
The returned value will be used as a state and passed as the last
argument to the subsequent callbacks.
The returned value is used as a state and is passed as the last argument to
the subsequent callbacks
Default behavior is to return the argument unchanged.
"""
@callback handle_open_connection(conn()) :: state()
@callback init(config()) :: state()

@doc """
Callback called when a new connection is established.
"""
@callback handle_open_connection(conn(), state()) :: state()

@doc """
Callback called when a connection is closed.
Expand Down Expand Up @@ -160,4 +173,17 @@ defmodule Membrane.RTSP.Server.Handler do
the session.
"""
@callback handle_teardown(state()) :: {Response.t(), state()}

@optional_callbacks init: 1

defmacro __using__(_options) do
quote do
@behaviour unquote(__MODULE__)

@impl true
def init(config), do: config

defoverridable init: 1
end
end
end
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule Membrane.RTSP.MixProject do
use Mix.Project

@version "0.7.2"
@version "0.8.0"
@github_url "https://github.com/membraneframework/membrane_rtsp"

def project do
Expand Down

0 comments on commit 4084f7a

Please sign in to comment.