|
| 1 | +defmodule Dtmf.PeerHandler do |
| 2 | + require Logger |
| 3 | + |
| 4 | + alias ExWebRTC.{ |
| 5 | + ICECandidate, |
| 6 | + MediaStreamTrack, |
| 7 | + PeerConnection, |
| 8 | + RTPCodecParameters, |
| 9 | + RTP.Depayloader, |
| 10 | + RTP.JitterBuffer, |
| 11 | + SessionDescription |
| 12 | + } |
| 13 | + |
| 14 | + @behaviour WebSock |
| 15 | + |
| 16 | + @ice_servers [ |
| 17 | + %{urls: "stun:stun.l.google.com:19302"} |
| 18 | + ] |
| 19 | + |
| 20 | + @audio_codecs [ |
| 21 | + %RTPCodecParameters{ |
| 22 | + payload_type: 111, |
| 23 | + mime_type: "audio/opus", |
| 24 | + clock_rate: 48_000, |
| 25 | + channels: 2 |
| 26 | + }, |
| 27 | + %RTPCodecParameters{ |
| 28 | + payload_type: 126, |
| 29 | + mime_type: "audio/telephone-event", |
| 30 | + clock_rate: 8000, |
| 31 | + channels: 1 |
| 32 | + } |
| 33 | + ] |
| 34 | + |
| 35 | + @impl true |
| 36 | + def init(_) do |
| 37 | + {:ok, pc} = |
| 38 | + PeerConnection.start_link( |
| 39 | + ice_servers: @ice_servers, |
| 40 | + video_codecs: [], |
| 41 | + audio_codecs: @audio_codecs |
| 42 | + ) |
| 43 | + |
| 44 | + state = %{ |
| 45 | + peer_connection: pc, |
| 46 | + in_audio_track_id: nil, |
| 47 | + # The flow of this example is as follows: |
| 48 | + # we first feed rtp packets into jitter buffer to |
| 49 | + # wait for retransmissions and fix ordering. |
| 50 | + # Once ordering and gaps are fixed, we feed packets |
| 51 | + # to the depayloader, which detects DTMF events. |
| 52 | + # Note that depayloader takes all RTP packets (both Opus and DTMF), |
| 53 | + # but ignores those that are not DTMF ones. |
| 54 | + # This is to avoid demuxing packets by the user. |
| 55 | + jitter_buffer: nil, |
| 56 | + depayloader: nil |
| 57 | + } |
| 58 | + |
| 59 | + {:ok, state} |
| 60 | + end |
| 61 | + |
| 62 | + @impl true |
| 63 | + def handle_in({msg, [opcode: :text]}, state) do |
| 64 | + msg |
| 65 | + |> Jason.decode!() |
| 66 | + |> handle_ws_msg(state) |
| 67 | + end |
| 68 | + |
| 69 | + @impl true |
| 70 | + def handle_info({:ex_webrtc, _from, msg}, state) do |
| 71 | + handle_webrtc_msg(msg, state) |
| 72 | + end |
| 73 | + |
| 74 | + @impl true |
| 75 | + def handle_info(:jitter_buffer_timeout, state) do |
| 76 | + state.jitter_buffer |
| 77 | + |> JitterBuffer.handle_timeout() |
| 78 | + |> handle_jitter_buffer_result(state) |
| 79 | + end |
| 80 | + |
| 81 | + @impl true |
| 82 | + def handle_info({:EXIT, pc, reason}, %{peer_connection: pc} = state) do |
| 83 | + # Bandit traps exits under the hood so our PeerConnection.start_link |
| 84 | + # won't automatically bring this process down. |
| 85 | + Logger.info("Peer connection process exited, reason: #{inspect(reason)}") |
| 86 | + {:stop, {:shutdown, :pc_closed}, state} |
| 87 | + end |
| 88 | + |
| 89 | + @impl true |
| 90 | + def terminate(reason, _state) do |
| 91 | + Logger.info("WebSocket connection was terminated, reason: #{inspect(reason)}") |
| 92 | + end |
| 93 | + |
| 94 | + defp handle_ws_msg(%{"type" => "offer", "data" => data}, state) do |
| 95 | + Logger.info("Received SDP offer:\n#{data["sdp"]}") |
| 96 | + |
| 97 | + offer = SessionDescription.from_json(data) |
| 98 | + :ok = PeerConnection.set_remote_description(state.peer_connection, offer) |
| 99 | + |
| 100 | + {:ok, answer} = PeerConnection.create_answer(state.peer_connection) |
| 101 | + :ok = PeerConnection.set_local_description(state.peer_connection, answer) |
| 102 | + |
| 103 | + answer_json = SessionDescription.to_json(answer) |
| 104 | + |
| 105 | + msg = |
| 106 | + %{"type" => "answer", "data" => answer_json} |
| 107 | + |> Jason.encode!() |
| 108 | + |
| 109 | + Logger.info("Sent SDP answer:\n#{answer_json["sdp"]}") |
| 110 | + |
| 111 | + {:push, {:text, msg}, state} |
| 112 | + end |
| 113 | + |
| 114 | + defp handle_ws_msg(%{"type" => "ice", "data" => data}, state) do |
| 115 | + Logger.info("Received ICE candidate: #{data["candidate"]}") |
| 116 | + |
| 117 | + candidate = ICECandidate.from_json(data) |
| 118 | + :ok = PeerConnection.add_ice_candidate(state.peer_connection, candidate) |
| 119 | + {:ok, state} |
| 120 | + end |
| 121 | + |
| 122 | + defp handle_webrtc_msg({:connection_state_change, conn_state}, state) do |
| 123 | + Logger.info("Connection state changed: #{conn_state}") |
| 124 | + |
| 125 | + if conn_state == :failed do |
| 126 | + {:stop, {:shutdown, :pc_failed}, state} |
| 127 | + else |
| 128 | + {:ok, state} |
| 129 | + end |
| 130 | + end |
| 131 | + |
| 132 | + defp handle_webrtc_msg({:ice_candidate, candidate}, state) do |
| 133 | + candidate_json = ICECandidate.to_json(candidate) |
| 134 | + |
| 135 | + msg = |
| 136 | + %{"type" => "ice", "data" => candidate_json} |
| 137 | + |> Jason.encode!() |
| 138 | + |
| 139 | + Logger.info("Sent ICE candidate: #{candidate_json["candidate"]}") |
| 140 | + |
| 141 | + {:push, {:text, msg}, state} |
| 142 | + end |
| 143 | + |
| 144 | + defp handle_webrtc_msg({:track, %MediaStreamTrack{kind: :audio, id: id}}, state) do |
| 145 | + # Find dtmf codec. Its config (payload type) might have changed during negotiation. |
| 146 | + tr = |
| 147 | + state.peer_connection |
| 148 | + |> PeerConnection.get_transceivers() |
| 149 | + |> Enum.find(fn tr -> tr.receiver.track.id == id end) |
| 150 | + |
| 151 | + codec = Enum.find(tr.codecs, fn codec -> codec.mime_type == "audio/telephone-event" end) |
| 152 | + |
| 153 | + if codec == nil do |
| 154 | + raise "DTMF for the track has not been negotiated." |
| 155 | + end |
| 156 | + |
| 157 | + jitter_buffer = JitterBuffer.new() |
| 158 | + {:ok, depayloader} = Depayloader.new(codec) |
| 159 | + |
| 160 | + state = %{ |
| 161 | + state |
| 162 | + | in_audio_track_id: id, |
| 163 | + jitter_buffer: jitter_buffer, |
| 164 | + depayloader: depayloader |
| 165 | + } |
| 166 | + |
| 167 | + {:ok, state} |
| 168 | + end |
| 169 | + |
| 170 | + defp handle_webrtc_msg({:rtp, id, nil, packet}, %{in_audio_track_id: id} = state) do |
| 171 | + state.jitter_buffer |
| 172 | + |> JitterBuffer.insert(packet) |
| 173 | + |> handle_jitter_buffer_result(state) |
| 174 | + end |
| 175 | + |
| 176 | + defp handle_webrtc_msg(_msg, state), do: {:ok, state} |
| 177 | + |
| 178 | + defp handle_jitter_buffer_result({packets, timeout, jitter_buffer}, state) do |
| 179 | + state = %{state | jitter_buffer: jitter_buffer} |
| 180 | + |
| 181 | + if timeout != nil do |
| 182 | + Process.send_after(self(), :jitter_buffer_timeout, timeout) |
| 183 | + end |
| 184 | + |
| 185 | + state = |
| 186 | + Enum.reduce(packets, state, fn packet, state -> |
| 187 | + case Depayloader.depayload(state.depayloader, packet) do |
| 188 | + {nil, depayloader} -> |
| 189 | + %{state | depayloader: depayloader} |
| 190 | + |
| 191 | + {event, depayloader} -> |
| 192 | + Logger.info("Received DTMF event: #{event.event}") |
| 193 | + %{state | depayloader: depayloader} |
| 194 | + end |
| 195 | + end) |
| 196 | + |
| 197 | + {:ok, state} |
| 198 | + end |
| 199 | +end |
0 commit comments