Octoroom.Cloudflare behaviour (octoroom v0.1.0)

Copy Markdown View Source

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.

Summary

Types

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

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

Callbacks

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.

Creates an empty session (one peer connection).

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.

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

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

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

Types

config()

@type config() :: %{:client => module(), optional(atom()) => term()}

mid()

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

pulled()

@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()

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

session_id()

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

session_track()

@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).

Callbacks

close_tracks(config, session_id, list, offer)

@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(config)

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

Creates an empty session (one peer connection).

pull_tracks(config, session_id, list)

@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(config, session_id, offer, list)

@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(config, session_id, answer)

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

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

session_state(config, session_id)

@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.

Functions

close_tracks(config, session, mids, offer)

config()

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

new_session(config)

pull_tracks(config, session, tracks)

push_tracks(config, session, offer, tracks)

renegotiate(config, session, answer)

session_state(config, session)