Skip to content

Commit

Permalink
Suffix event names with player id
Browse files Browse the repository at this point in the history
  • Loading branch information
FelonEkonom committed Feb 13, 2025
1 parent 8922025 commit 7d2f4cc
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 17 deletions.
31 changes: 19 additions & 12 deletions assets/player.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,35 @@
export function createPlayerHook(iceServers = [{ urls: "stun:stun.l.google.com:19302" }]) {
function createPlayerHook(iceServers = [{ urls: "stun:stun.l.google.com:19302" }]) {
return {
async mounted() {
console.log("MOUNTED");

this.pc = new RTCPeerConnection({ iceServers: iceServers });
this.el.srcObject = new MediaStream();

// todo: get element by player id, different for every player
pc.ontrack = (event) =>
document.getElementById("videoPlayer").srcObject.addTrack(event.track);
this.pc.ontrack = (event) => {
console.log("NEW TRACK", this.el);
this.el.srcObject.addTrack(event.track);
};

this.pc.onicecandidate = (ev) => {
console.log("NEW BROWSER ICE CANDIDATE");
message = JSON.stringify({ type: "ice_candidate", data: ev.candidate });
this.pushEventTo(this.el, "webrtc_singaling", message);
this.pushEventTo(this.el, "webrtc_signaling", message);
};

// todo: event name ("webrtc_signaling") should be suffixed with the component id
this.handleEvent("webrtc_singaling", async (event) => {
const { type, data } = JSON.parse(event);
const eventName = "webrtc_signaling-" + this.el.id;
this.handleEvent(eventName, async (event) => {
console.log("NEW SIGNALING MESSAGE", event);

const { type, data } = event;

switch (type) {
case "sdp_offer":
console.log("Received SDP offer:", data);
await pc.setRemoteDescription(data);
await this.pc.setRemoteDescription(data);

const answer = await pc.createAnswer();
await pc.setLocalDescription(answer);
const answer = await this.pc.createAnswer();
await this.pc.setLocalDescription(answer);

message = JSON.stringify({ type: "sdp_answer", data: answer });
this.pushEventTo(this.el, "webrtc_signaling", message);
Expand All @@ -31,7 +38,7 @@ export function createPlayerHook(iceServers = [{ urls: "stun:stun.l.google.com:1
break;
case "ice_candidate":
console.log("Recieved ICE candidate:", data);
await pc.addIceCandidate(data);
await this.pc.addIceCandidate(data);
}
});
},
Expand Down
2 changes: 1 addition & 1 deletion lib/webrtc/player.ex
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ defmodule Boombox.Live.Player do

{:noreply,
socket
|> push_event("webrtc_signaling", message)}
|> push_event("webrtc_signaling-#{socket.assigns.player.id}", message)}
end

@impl true
Expand Down
6 changes: 2 additions & 4 deletions player_demo.exs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ defmodule Example.HomeLive do
this.pc = new RTCPeerConnection({ iceServers: iceServers });
this.el.srcObject = new MediaStream()
// todo: get element by player id, different for every player
this.pc.ontrack = (event) => {
console.log("NEW TRACK", this.el)
this.el.srcObject.addTrack(event.track);
Expand All @@ -115,14 +114,13 @@ defmodule Example.HomeLive do
this.pushEventTo(this.el, "webrtc_signaling", message);
};
// todo: event name ("webrtc_signaling") should be suffixed with the component id
this.handleEvent("webrtc_signaling", async (event) => {
const eventName = "webrtc_signaling-" + this.el.id
this.handleEvent(eventName, async (event) => {
console.log("NEW SIGNALING MESSAGE", event)
const { type, data } = event;
switch (type) {
case "sdp_offer":
console.log("Received SDP offer:", data);
Expand Down

0 comments on commit 7d2f4cc

Please sign in to comment.