# `Membrane.RTMPServer`
[🔗](https://github.com/membraneframework/membrane_rtmp_plugin/blob/v0.29.5/lib/membrane_rtmp_plugin/rtmp_server.ex#L1)

A simple RTMP server, which handles each new incoming connection. When a new client connects, the `handle_new_client` is invoked.
New connections remain in an incomplete RTMP handshake state, until another process makes demand for their data.
If no data is demanded within the client_timeout period, TCP socket is closed.

Options:
- handle_new_client: An anonymous function called when a new client connects.
    It receives the client reference, `app` and `stream_key`, allowing custom processing,
    like sending the reference to another process. The function should return a `t:Membrane.RTMPServer.client_behaviour_spec/0`
    which defines how the client should behave.
- port: Port on which RTMP server will listen. Defaults to 1935.
- use_ssl?: If true, SSL socket (for RTMPS) will be used. Otherwise, TCP socket (for RTMP) will be used. Defaults to false.
- ssl_options: SSL options to configure the SSL socket.
- client_timeout: Time after which an unused client connection is automatically closed, expressed in `Membrane.Time.t()` units. Defaults to 5 seconds.
- name: If not nil, value of this field will be used as a name under which the server's process will be registered. Defaults to nil.

## SSL Configuration

SSL options can be configured at the application level or passed as runtime options.

### Application Configuration

    config :membrane_rtmp_plugin, :ssl,
      certfile: "/path/to/cert.pem",
      keyfile: "/path/to/key.pem",
      verify: :verify_none,
      fail_if_no_peer_cert: false,
      versions: [:"tlsv1.2", :"tlsv1.3"]

### Runtime Options

    Membrane.RTMPServer.start_link(
      port: 1935,
      use_ssl?: true,
      ssl_options: [
        certfile: "/path/to/cert.pem",
        keyfile: "/path/to/key.pem"
      ],
      handle_new_client: &my_handler/3
    )

# `client_behaviour_spec`

```elixir
@type client_behaviour_spec() ::
  Membrane.RTMPServer.ClientHandler.t()
  | {Membrane.RTMPServer.ClientHandler.t(), opts :: any()}
```

A type representing how a client handler should behave.
If just a tuple is passed, the second element of that tuple is used as
an input argument of the `c:Membrane.RTMPServer.ClientHandler.handle_init/1`. Otherwise, an empty
map is passed to the `c:Membrane.RTMPServer.ClientHandler.handle_init/1`.

# `server_identifier`

```elixir
@type server_identifier() :: pid() | atom()
```

# `t`

```elixir
@type t() :: [
  port: :inet.port_number(),
  use_ssl?: boolean(),
  ssl_options: keyword() | nil,
  name: atom() | nil,
  handle_new_client: (client_ref :: pid(),
                      app :: String.t(),
                      stream_key :: String.t() -&gt;
                        client_behaviour_spec()),
  client_timeout: Membrane.Time.t()
]
```

Defines options for the RTMP server.

# `child_spec`

Returns a specification to start this module under a supervisor.

See `Supervisor`.

# `get_port`

```elixir
@spec get_port(server_identifier()) :: :inet.port_number()
```

Returns the port on which the server listens for connection.

# `parse_url`

```elixir
@spec parse_url(url :: String.t()) :: {boolean(), integer(), String.t(), String.t()}
```

Extracts ssl, port, app and stream_key from url.

# `start_link`

```elixir
@spec start_link(server_options :: t()) :: GenServer.on_start()
```

Starts the RTMP server.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
