Skip to content

Commit a50882c

Browse files
committed
Send ICE candidates from subscriber
1 parent 3cbf351 commit a50882c

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

assets/publisher.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,6 @@ export function createPublisherHook(iceServers = []) {
184184

185185
view.disableControls(view);
186186

187-
console.log(iceServers);
188187
view.pc = new RTCPeerConnection({ iceServers: iceServers });
189188

190189
// handle local events

assets/subscriber.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ export function createSubscriberHook(iceServers = []) {
22
return {
33
async mounted() {
44
this.pc = new RTCPeerConnection({ iceServers: iceServers });
5+
6+
this.pc.onicecandidate = (ev) => {
7+
this.pushEventTo(this.el, "ice", JSON.stringify(ev.candidate));
8+
};
9+
510
this.pc.ontrack = (ev) => {
611
console.log("ontrack");
712
this.el.srcObject = ev.streams[0];

lib/live_ex_webrtc/subscriber.ex

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ defmodule LiveExWebRTC.Subscriber do
1111
"""
1212
end
1313

14+
def handle_event(_event, _unsigned_params, %{assigns: %{pc: nil}} = socket) do
15+
{:noreply, socket}
16+
end
17+
1418
def handle_event("offer", unsigned_params, socket) do
1519
offer = SessionDescription.from_json(unsigned_params)
1620
{:ok, pc} = spawn_peer_connection(socket)
@@ -37,6 +41,22 @@ defmodule LiveExWebRTC.Subscriber do
3741
{:noreply, socket}
3842
end
3943

44+
def handle_event("ice", "null", socket) do
45+
:ok = PeerConnection.add_ice_candidate(socket.assigns.pc, %{candidate: ""})
46+
{:noreply, socket}
47+
end
48+
49+
def handle_event("ice", unsigned_params, socket) do
50+
cand =
51+
unsigned_params
52+
|> Jason.decode!()
53+
|> ExWebRTC.ICECandidate.from_json()
54+
55+
:ok = PeerConnection.add_ice_candidate(socket.assigns.pc, cand)
56+
57+
{:noreply, socket}
58+
end
59+
4060
defp spawn_peer_connection(socket) do
4161
pc_opts =
4262
[

0 commit comments

Comments
 (0)