Skip to content

Commit

Permalink
Add used SDP examples
Browse files Browse the repository at this point in the history
  • Loading branch information
LVala committed Sep 27, 2023
1 parent 66e474d commit 1439f77
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 12 deletions.
2 changes: 1 addition & 1 deletion examples/example.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Mix.install([{:gun, "~> 2.0.1"}, {:ex_webrtc, path: "./", force: true}, {:jason, "~> 1.4.0"}])

require Logger
Logger.configure(level: :debug)
Logger.configure(level: :info)

defmodule Peer do
use GenServer
Expand Down
13 changes: 9 additions & 4 deletions examples/example.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ const start_connection = async (ws) => {
const pc = new RTCPeerConnection(pcConfig);

pc.onconnectionstatechange = _ => console.log("Connection state changed:", pc.connectionState);

pc.onicecandidateerror = event => console.log("ICE candidate error:", event);
pc.oniceconnectionstatechange = _ => console.log("ICE connection state changed:", pc.iceConnectionState);
pc.onicegatheringstatechange = _ => console.log("ICE gathering state changed:", pc.iceGatheringState);
pc.onsignalingstatechange = _ => console.log("Signaling state changed:", pc.signalingState);
pc.ontrack = event => console.log("New track:", event);
pc.onicecandidate = event => {
console.log("New local ICE candidate:", event.candidate);

Expand All @@ -22,16 +26,17 @@ const start_connection = async (ws) => {
}
};

pc.ontrack = null; // TODO

ws.onmessage = event => {
const msg = JSON.parse(event.data);
console.log("Received message:", msg);

if (msg.type === "answer") {
console.log("Recieved SDP answer:", msg);
pc.setRemoteDescription(msg);
} else if (msg.type === "ice") {
console.log("Recieved remote ICE candidate:", msg.data);
pc.addIceCandidate(msg.data);
} else {
console.log("Received unexpected message:", msg);
}
};

Expand Down
33 changes: 33 additions & 0 deletions examples/sdp_answer.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
v=0
o=- 7596991810024734139 2 IN IP4 127.0.0.1
s=-
t=0 0
a=group:BUNDLE 0
a=extmap-allow-mixed
a=msid-semantic: WMS
m=audio 9 UDP/TLS/RTP/SAVPF 111 63 9 0 8 13 110 126
c=IN IP4 0.0.0.0
a=rtcp:9 IN IP4 0.0.0.0
a=ice-ufrag:vx/1
a=ice-pwd:ldFUrCsXvndFY2L1u0UQ7ikf
a=ice-options:trickle
a=fingerprint:sha-256 76:61:77:1E:7C:2E:BB:CD:19:B5:27:4E:A7:40:84:06:6B:17:97:AB:C4:61:90:16:EE:96:9F:9E:BD:42:96:3D
a=setup:passive
a=mid:0
a=extmap:1 urn:ietf:params:rtp-hdrext:ssrc-audio-level
a=extmap:2 http://www.webrtc.org/experiments/rtp-hdrext/abs-send-time
a=extmap:3 http://www.ietf.org/id/draft-holmer-rmcat-transport-wide-cc-extensions-01
a=extmap:4 urn:ietf:params:rtp-hdrext:sdes:mid
a=recvonly
a=rtcp-mux
a=rtpmap:111 opus/48000/2
a=rtcp-fb:111 transport-cc
a=fmtp:111 minptime=10;useinbandfec=1
a=rtpmap:63 red/48000/2
a=fmtp:63 111/111
a=rtpmap:9 G722/8000
a=rtpmap:0 PCMU/8000
a=rtpmap:8 PCMA/8000
a=rtpmap:13 CN/8000
a=rtpmap:110 telephone-event/48000
a=rtpmap:126 telephone-event/8000
36 changes: 36 additions & 0 deletions examples/sdp_offer.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
v=0
o=- 8941027839948408955 2 IN IP4 127.0.0.1
s=-
t=0 0
a=group:BUNDLE 0
a=extmap-allow-mixed
a=msid-semantic: WMS 4d0a4923-289d-4f4c-931a-c72db00c055f
m=audio 9 UDP/TLS/RTP/SAVPF 111 63 9 0 8 13 110 126
c=IN IP4 0.0.0.0
a=rtcp:9 IN IP4 0.0.0.0
a=ice-ufrag:q1Cp
a=ice-pwd:AazQ7Pof8R7suS3j9aod8DjV
a=ice-options:trickle
a=fingerprint:sha-256 42:85:3D:4C:72:D6:C2:5B:AE:EE:7F:6A:53:7C:04:63:6E:B9:55:18:46:E5:82:31:57:EE:21:F0:E7:60:82:CD
a=setup:actpass
a=mid:0
a=extmap:1 urn:ietf:params:rtp-hdrext:ssrc-audio-level
a=extmap:2 http://www.webrtc.org/experiments/rtp-hdrext/abs-send-time
a=extmap:3 http://www.ietf.org/id/draft-holmer-rmcat-transport-wide-cc-extensions-01
a=extmap:4 urn:ietf:params:rtp-hdrext:sdes:mid
a=sendrecv
a=msid:4d0a4923-289d-4f4c-931a-c72db00c055f 6a7555a6-7a70-4196-a58f-89567c49b12d
a=rtcp-mux
a=rtpmap:111 opus/48000/2
a=rtcp-fb:111 transport-cc
a=fmtp:111 minptime=10;useinbandfec=1
a=rtpmap:63 red/48000/2
a=fmtp:63 111/111
a=rtpmap:9 G722/8000
a=rtpmap:0 PCMU/8000
a=rtpmap:8 PCMA/8000
a=rtpmap:13 CN/8000
a=rtpmap:110 telephone-event/48000
a=rtpmap:126 telephone-event/8000
a=ssrc:2776039216 cname:1qch+ontR0BspBY6
a=ssrc:2776039216 msid:4d0a4923-289d-4f4c-931a-c72db00c055f 6a7555a6-7a70-4196-a58f-89567c49b12d
45 changes: 38 additions & 7 deletions lib/ex_webrtc/peer_connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,42 @@ defmodule ExWebRTC.PeerConnection do
signaling_state: :stable
]

@dummy_sdp "
v=0\r\n
o=- 7596991810024734139 2 IN IP4 127.0.0.1\r\n
s=-\r\n
t=0 0\r\n
a=group:BUNDLE 0\r\n
a=extmap-allow-mixed\r\n
a=msid-semantic: WMS\r\n
m=audio 9 UDP/TLS/RTP/SAVPF 111 63 9 0 8 13 110 126\r\n
c=IN IP4 0.0.0.0\r\n
a=rtcp:9 IN IP4 0.0.0.0\r\n
a=ice-ufrag:vx/1\r\n
a=ice-pwd:ldFUrCsXvndFY2L1u0UQ7ikf\r\n
a=ice-options:trickle\r\n
a=fingerprint:sha-256 76:61:77:1E:7C:2E:BB:CD:19:B5:27:4E:A7:40:84:06:6B:17:97:AB:C4:61:90:16:EE:96:9F:9E:BD:42:96:3D\r\n
a=setup:passive\r\n
a=mid:0\r\n
a=extmap:1 urn:ietf:params:rtp-hdrext:ssrc-audio-level\r\n
a=extmap:2 http://www.webrtc.org/experiments/rtp-hdrext/abs-send-time\r\n
a=extmap:3 http://www.ietf.org/id/draft-holmer-rmcat-transport-wide-cc-extensions-01\r\n
a=extmap:4 urn:ietf:params:rtp-hdrext:sdes:mid\r\n
a=recvonly\r\n
a=rtcp-mux\r\n
a=rtpmap:111 opus/48000/2\r\n
a=rtcp-fb:111 transport-cc\r\n
a=fmtp:111 minptime=10;useinbandfec=1\r\n
a=rtpmap:63 red/48000/2\r\n
a=fmtp:63 111/111\r\n
a=rtpmap:9 G722/8000\r\n
a=rtpmap:0 PCMU/8000\r\n
a=rtpmap:8 PCMA/8000\r\n
a=rtpmap:13 CN/8000\r\n
a=rtpmap:110 telephone-event/48000\r\n
a=rtpmap:126 telephone-event/8000\r\n
"

#### API ####

def start_link(configuration \\ []) do
Expand Down Expand Up @@ -71,6 +107,7 @@ defmodule ExWebRTC.PeerConnection do
config = struct(Configuration, config)
:ok = Configuration.check_support(config)

# ATM, ExICE does not support relay via TURN
stun_servers =
config.ice_servers
|> Enum.flat_map(&if(is_list(&1.urls), do: &1.urls, else: [&1.urls]))
Expand All @@ -90,14 +127,9 @@ defmodule ExWebRTC.PeerConnection do
@impl true
def handle_call({:create_answer, _options}, _from, state)
when state.signaling_state in [:have_remote_offer, :have_local_pranswer] do
# hardcoded answer based generated by chrome with no added tracks
# in response to offer also generated by chrome with 1 audio track
{:ok, ufrag, pwd} = ICEAgent.get_local_credentials(state.ice_agent)

test_answer =
"v=0\r\no=- 7596991810024734139 2 IN IP4 127.0.0.1\r\ns=-\r\nt=0 0\r\na=group:BUNDLE 0\r\na=extmap-allow-mixed\r\na=msid-semantic: WMS\r\nm=audio 9 UDP/TLS/RTP/SAVPF 111 63 9 0 8 13 110 126\r\nc=IN IP4 0.0.0.0\r\na=rtcp:9 IN IP4 0.0.0.0\r\na=ice-ufrag:vx/1\r\na=ice-pwd:ldFUrCsXvndFY2L1u0UQ7ikf\r\na=ice-options:trickle\r\na=fingerprint:sha-256 76:61:77:1E:7C:2E:BB:CD:19:B5:27:4E:A7:40:84:06:6B:17:97:AB:C4:61:90:16:EE:96:9F:9E:BD:42:96:3D\r\na=setup:active\r\na=mid:0\r\na=extmap:1 urn:ietf:params:rtp-hdrext:ssrc-audio-level\r\na=extmap:2 http://www.webrtc.org/experiments/rtp-hdrext/abs-send-time\r\na=extmap:3 http://www.ietf.org/id/draft-holmer-rmcat-transport-wide-cc-extensions-01\r\na=extmap:4 urn:ietf:params:rtp-hdrext:sdes:mid\r\na=recvonly\r\na=rtcp-mux\r\na=rtpmap:111 opus/48000/2\r\na=rtcp-fb:111 transport-cc\r\na=fmtp:111 minptime=10;useinbandfec=1\r\na=rtpmap:63 red/48000/2\r\na=fmtp:63 111/111\r\na=rtpmap:9 G722/8000\r\na=rtpmap:0 PCMU/8000\r\na=rtpmap:8 PCMA/8000\r\na=rtpmap:13 CN/8000\r\na=rtpmap:110 telephone-event/48000\r\na=rtpmap:126 telephone-event/8000\r\n"

sdp = ExSDP.parse!(test_answer)
sdp = ExSDP.parse!(@dummy_sdp)
media = hd(sdp.media)

attrs =
Expand Down Expand Up @@ -148,7 +180,6 @@ defmodule ExWebRTC.PeerConnection do
end
end

@impl true
def handle_call({:add_ice_candidate, candidate}, _from, state) do
with "candidate:" <> attr <- candidate.candidate do
ICEAgent.add_remote_candidate(state.ice_agent, attr)
Expand Down

0 comments on commit 1439f77

Please sign in to comment.