View Source Lifecycle

  • Initialize the library. Register messages to the current pid.
iex> {:ok, specter} = Specter.init(ice_servers: ["stun:stun.l.google.com:19302"])
iex> {:ok, media_engine} = Specter.new_media_engine(specter)
iex> {:ok, registry} = Specter.new_registry(specter, media_engine)

iex> true = Specter.media_engine_exists?(specter, media_engine)
iex> true = Specter.registry_exists?(specter, registry)

iex> {:ok, api} = Specter.new_api(specter, media_engine, registry)

iex> false = Specter.media_engine_exists?(specter, media_engine)
iex> false = Specter.registry_exists?(specter, registry)
iex> {:ok, pc_1} = Specter.PeerConnection.new(specter, api)
iex> :ok = receive do: ({:peer_connection_ready, ^pc_1} -> :ok),
...>    after: (100 -> {:error, :timeout})

iex> true = Specter.PeerConnection.exists?(specter, pc_1)
iex> :ok = Specter.PeerConnection.create_data_channel(specter, pc_1, "data")
iex> :ok = receive do: ({:data_channel_created, ^pc_1} -> :ok),
...>    after: (100 -> {:error, :timeout})
iex> :ok = Specter.PeerConnection.create_offer(specter, pc_1)
iex> {:ok, offer} = receive do: ({:offer, ^pc_1, offer} -> {:ok, offer}),
...>    after: (100 -> {:error, :timeout})

iex> :ok = Specter.PeerConnection.set_local_description(specter, pc_1, offer)
iex> :ok = receive do: ({:ok, ^pc_1, :set_local_description} -> :ok),
...>    after: (100 -> {:error, :timeout})
  • Create a second peer connection, to answer back
iex> {:ok, media_engine} = Specter.new_media_engine(specter)
iex> {:ok, registry} = Specter.new_registry(specter, media_engine)
iex> {:ok, api} = Specter.new_api(specter, media_engine, registry)
iex> {:ok, pc_2} = Specter.PeerConnection.new(specter, api)
iex> :ok = receive do: ({:peer_connection_ready, ^pc_2} -> :ok),
...>    after: (100 -> {:error, :timeout})
##  give the offer to the second peer connection
iex> :ok = Specter.PeerConnection.set_remote_description(specter, pc_2, offer)
iex> :ok = receive do: ({:ok, ^pc_2, :set_remote_description} -> :ok),
...>    after: (100 -> {:error, :timeout})

##  create an answer
iex> :ok = Specter.PeerConnection.create_answer(specter, pc_2)
iex> {:ok, answer} = receive do: ({:answer, ^pc_2, answer} -> {:ok, answer}),
...>    after: (100 -> {:error, :timeout})

iex> :ok = Specter.PeerConnection.set_local_description(specter, pc_2, answer)
iex> :ok = receive do: ({:ok, ^pc_2, :set_local_description} -> :ok),
...>    after: (100 -> {:error, :timeout})

##  give the answer to the first peer connection
iex> :ok = Specter.PeerConnection.set_remote_description(specter, pc_1, answer)
iex> :ok = receive do: ({:ok, ^pc_1, :set_remote_description} -> :ok),
...>    after: (100 -> {:error, :timeout})
iex> {:ok, candidate} = receive do: ({:ice_candidate, ^pc_1, c} -> {:ok, c}),
...>    after: (100 -> {:error, :timeout})
iex> :ok = Specter.PeerConnection.add_ice_candidate(specter, pc_2, candidate)

## .... and so on.

iex> {:ok, candidate} = receive do: ({:ice_candidate, ^pc_2, c} -> {:ok, c}),
...>    after: (100 -> {:error, :timeout})
iex>:ok = Specter.PeerConnection.add_ice_candidate(specter, pc_1, candidate)

## .... and so on.
iex> :ok = Specter.PeerConnection.close(specter, pc_1)
iex> receive do: ({:peer_connection_closed, ^pc_1} -> :ok),
...>    after: (100 -> {:error, :timeout})
:ok

iex> :ok = Specter.PeerConnection.close(specter, pc_2)
iex> receive do: ({:peer_connection_closed, ^pc_2} -> :ok),
...>    after: (100 -> {:error, :timeout})
:ok