# `Sippy`
[🔗](https://pushin.eu/hermanverschooten/sippy/blob/v0.1.0/lib/sippy.ex#L1)

Make and receive phone calls through a PBX, with WAV prompts and DTMF.

Every call is a process. Functions that take a `pid` work on calls from
both `call/2` and `start_inbound_call/2`, unless their documentation says
otherwise.

## Making a call

    config = Sippy.config(pbx_host: "pbx.example.com", username: "100", password: "secret")

    {:ok, call} =
      Sippy.call("200", config: config, audio: "message.wav", hangup_after_audio: 1_000)

## Receiving calls

    {:ok, _registrar} = Sippy.register(config)

    receive do
      {:incoming_call, incoming} ->
        {:ok, call} = Sippy.start_inbound_call(incoming, parent_pid: self())
        :ok = Sippy.answer(call, "welcome.wav")
    end

## Messages

The registrar's `:notify` process receives:

  * `{:incoming_call, %Sippy.IncomingCall{}}` - a new call is ringing
  * `{:incoming_call_cancelled, call_id}` - the caller gave up, or the call
    rang longer than `:pending_call_timeout`

A call's `:parent_pid` receives:

  * `{:call_answered, pid}` - an outbound call was answered
  * `{:dtmf, pid, digit}` - a key was pressed: `"0"`-`"9"`, `"*"`, `"#"`
    or `"A"`-`"D"`
  * `{:audio_completed, pid}` - the audio finished playing; audio that was
    stopped or replaced is not reported
  * `{:transfer_completed, pid}` - the PBX carried out a transfer of an
    inbound call; the call then hangs up and ends with `:transferred`
  * `{:transfer_failed, pid, status}` - the PBX refused a transfer, or the
    target did not take it; the call goes on
  * `{:call_ended, pid, reason}` - the call ended for a `t:end_reason/0`;
    not sent when the application hung up itself

An outbound call also exits with `{:shutdown, result}`, where `result`
holds `:success`, `:answered_at`, `:ended_at`, `:duration` (seconds) and
`:dtmf` (all digits pressed), or `:error` when it failed before that.
Monitor the process to read it.

## Limitations

Sippy talks SIP over UDP and IPv4 to a PBX, sends G.711 (PCMU or PCMA)
audio from WAV files and reads DTMF. It does not receive or record the
other side's audio, and has no TCP, TLS, SRTP or NAT traversal.

# `dialog_info`

```elixir
@type dialog_info() :: %{
  call_id: String.t(),
  from_tag: String.t(),
  to_tag: String.t() | nil
}
```

# `end_reason`

```elixir
@type end_reason() ::
  :remote_hangup
  | :transferred
  | {:rejected, pos_integer()}
  | :ring_timeout
  | :hangup_after_audio
  | :max_duration
  | :no_common_codec
  | :invalid_sdp
  | :auth_failed
  | {:error, term()}
```

Why a call ended, as sent in `{:call_ended, pid, reason}`.

  * `:remote_hangup` - the other side hung up
  * `:transferred` - an inbound call was transferred
  * `{:rejected, status}` - an outbound call got a final response such as
    486
  * `:ring_timeout` - nobody answered within `:ring_timeout`
  * `:hangup_after_audio` - `:hangup_after_audio` passed after the audio
  * `:max_duration` - the call lasted `:max_duration`
  * `:no_common_codec` / `:invalid_sdp` - the answer has no PCMU or PCMA,
    or no usable address
  * `:auth_failed` - the PBX rejected the credentials
  * `{:error, reason}` - anything else, such as an unreadable WAV file

# `answer`

```elixir
@spec answer(pid(), Path.t() | nil) :: :ok | {:error, term()}
```

Answers an inbound call, optionally playing a WAV file.

# `attended_transfer`

```elixir
@spec attended_transfer(pid(), pid(), String.t()) :: :ok | {:error, term()}
```

Transfers an inbound call to the party on an outbound call.

The inbound call is typically on hold and the outbound call answered.
Returns once the request is sent; the outcome arrives as
`{:transfer_completed, pid}` or `{:transfer_failed, pid, status}`.

# `blind_transfer`

```elixir
@spec blind_transfer(pid(), String.t()) :: :ok | {:error, term()}
```

Transfers an inbound call to an extension or number without calling it first.

Returns once the request is sent; the outcome arrives as
`{:transfer_completed, pid}` or `{:transfer_failed, pid, status}`.

# `call`

```elixir
@spec call(String.t(), keyword()) :: DynamicSupervisor.on_start_child()
```

Calls a number or extension through the PBX.

The call is placed as `sip:<destination>@<pbx_host>`.

## Options

  * `:config` - `Sippy.Config` with the PBX settings (required)
  * `:audio` - WAV file to play once the call is answered
  * `:parent_pid` - process that receives the call's messages
  * `:ring_timeout` - milliseconds to wait for an answer (default: `30_000`)
  * `:hangup_after_audio` - milliseconds after the audio ends to hang up,
    or `false` (default: `false`)
  * `:max_duration` - milliseconds an answered call may last, or
    `:infinity` (default: `600_000`)

The `:parent_pid` receives the messages listed in the module
documentation. Without one, end the call with `:hangup_after_audio`,
`hangup/1` or `:max_duration`.

Raises `ArgumentError` for unknown options or invalid values.

# `caller_id`

```elixir
@spec caller_id(pid()) :: String.t() | nil
```

The caller's number or user part of an inbound call, if any.

# `config`

```elixir
@spec config(keyword()) :: Sippy.Config.t()
```

Creates a configuration for connecting to the PBX.

See `Sippy.Config` for the options. `:pbx_host`, `:username` and
`:password` are required; unknown options raise an `ArgumentError`.

# `dialog_info`

```elixir
@spec dialog_info(pid()) :: dialog_info()
```

The Call-ID and tags of an outbound call, for `attended_transfer/3`.

# `dtmf_digits`

```elixir
@spec dtmf_digits(pid()) :: [String.t()]
```

The DTMF digits pressed so far during a call, oldest first.

# `hangup`

```elixir
@spec hangup(pid()) :: :ok
```

Hangs up a call from `call/2` or `start_inbound_call/2`.

# `hold`

```elixir
@spec hold(pid()) :: :ok | {:error, term()}
```

Puts an inbound call on hold with a sendonly re-INVITE.

# `play_audio`

```elixir
@spec play_audio(pid(), Path.t()) :: :ok | {:error, term()}
```

Plays a WAV file on an answered call, replacing any audio that is playing.

# `register`

```elixir
@spec register(Sippy.Config.t(), keyword()) :: GenServer.on_start()
```

Registers with the PBX to receive incoming calls.

Starts a `Sippy.Registrar` linked to the caller. To run it under a
supervisor, add `{Sippy.Registrar, config: config, notify: pid}` to the
supervision tree instead.

## Options

  * `:notify` - pid or name that receives the call messages
    (default: the calling process)
  * `:name` - registers the registrar under this name

The `:notify` process receives `{:incoming_call, %Sippy.IncomingCall{}}`
and `{:incoming_call_cancelled, call_id}`.

# `registered?`

```elixir
@spec registered?(GenServer.server()) :: boolean()
```

Whether the PBX accepted the registrar's last REGISTER.

# `reject`

```elixir
@spec reject(Sippy.IncomingCall.t(), :busy | :decline | :unavailable) ::
  :ok | {:error, :unknown_call}
```

Turns down an incoming call that has not been answered.

`reason` is `:busy` (`486 Busy Here`, the default), `:decline`
(`603 Decline`) or `:unavailable` (`480 Temporarily Unavailable`).

Returns `{:error, :unknown_call}` when the call was already cancelled,
rejected or answered.

# `ring`

```elixir
@spec ring(Sippy.IncomingCall.t()) :: :ok | {:error, :unknown_call}
```

Sends 180 Ringing for an incoming call that has not been answered.

Returns `{:error, :unknown_call}` when the call was already cancelled,
rejected or answered.

# `start_inbound_call`

```elixir
@spec start_inbound_call(Sippy.IncomingCall.t(), keyword()) ::
  DynamicSupervisor.on_start_child() | {:error, :unknown_call}
```

Takes over an incoming call so it can be answered with `answer/2`.

## Options

  * `:parent_pid` - process that receives the call's messages
  * `:max_duration` - milliseconds an answered call may last, or
    `:infinity` (default: `600_000`)

The `:parent_pid` receives `{:dtmf, pid, digit}`, `{:audio_completed, pid}`
and `{:call_ended, pid, reason}`, see the module documentation.

Returns `{:error, :unknown_call}` when the call was already cancelled,
rejected or taken over.

# `stop_audio`

```elixir
@spec stop_audio(pid()) :: :ok
```

Stops the audio playing on a call.

# `unhold`

```elixir
@spec unhold(pid()) :: :ok | {:error, term()}
```

Resumes an inbound call from hold with a sendrecv re-INVITE.

# `unregister`

```elixir
@spec unregister(GenServer.server()) :: :ok
```

Unregisters from the PBX and stops the registrar.

---

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