# `Octoroom.Cloudflare`
[🔗](https://github.com/samrat/octoroom/blob/v0.1.0/lib/octoroom/cloudflare.ex#L1)

The Cloudflare Realtime SFU operations Octoroom uses, as a behaviour.
`Octoroom.Cloudflare.HTTP` calls the real API; tests use an in-memory fake.

`config` is a map with at least `:client` (the implementing module); the
HTTP client also needs `:app_id` and `:app_secret`.

# `config`

```elixir
@type config() :: %{:client =&gt; module(), optional(atom()) =&gt; term()}
```

# `mid`

```elixir
@type mid() :: String.t()
```

# `pulled`

```elixir
@type pulled() ::
  %{session_id: session_id(), track_name: String.t(), mid: mid()}
  | %{session_id: session_id(), track_name: String.t(), error: String.t()}
```

One track of a pull: added (with its mid) or refused (with Cloudflare's error code).

# `sdp`

```elixir
@type sdp() :: String.t()
```

# `session_id`

```elixir
@type session_id() :: String.t()
```

# `session_track`

```elixir
@type session_track() :: %{
  location: :local | :remote,
  track_name: String.t(),
  mid: mid(),
  session_id: session_id() | nil,
  status: :active | :inactive | term()
}
```

A track as Cloudflare reports it in a session: pushed (`:local`) or pulled
(`:remote`, with the session it's pulled from).

# `close_tracks`

```elixir
@callback close_tracks(config(), session_id(), [mid()], offer :: sdp() | nil) ::
  {:ok, %{sdp: sdp() | nil}} | {:error, term()}
```

Closes tracks in a session. With the client's `offer` (made after stopping
the transceivers) it returns Cloudflare's answer; with `nil` the close is
forced and no answer comes back.

# `new_session`

```elixir
@callback new_session(config()) :: {:ok, session_id()} | {:error, term()}
```

Creates an empty session (one peer connection).

# `pull_tracks`

```elixir
@callback pull_tracks(config(), session_id(), [
  %{session_id: session_id(), track_name: String.t()}
]) ::
  {:ok, %{sdp: sdp() | nil, renegotiate: boolean(), tracks: [pulled()]}}
  | {:error, term()}
```

Pulls remote tracks into a session in one call. Per-track failures (such as
`empty_track_error`) come back in `tracks`; the offer covers the tracks that
were added. When `renegotiate` is true, the client's answer goes to
`renegotiate/3`.

# `push_tracks`

```elixir
@callback push_tracks(config(), session_id(), offer :: sdp(), [
  %{mid: mid(), track_name: String.t()}
]) ::
  {:ok, %{sdp: sdp()}} | {:error, term()}
```

Publishes local tracks from the client's `offer`. Returns Cloudflare's answer.

# `renegotiate`

```elixir
@callback renegotiate(config(), session_id(), answer :: sdp()) :: :ok | {:error, term()}
```

Completes a Cloudflare-initiated renegotiation with the client's answer.

# `session_state`

```elixir
@callback session_state(config(), session_id()) ::
  {:ok, %{tracks: [session_track()]}} | {:error, :disconnected | term()}
```

Reads back a session's tracks. `{:error, :disconnected}` when the
session's peer connection is gone.

# `close_tracks`

# `config`

The config from `config :octoroom, cloudflare: [...]`, with the HTTP client by default.

# `new_session`

# `pull_tracks`

# `push_tracks`

# `renegotiate`

# `session_state`

---

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