Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds examples/mirror.html #70

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions golang/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,9 @@ run: build

run-debug: build
ENV=local ./backend

run-pretty: build
# open http://localhost:3001
ENV=local ./backend 2>&1 | jq --raw-output '[("# " + .level + (" " * 10))[0:10], (.func + (" " * 20))[0:20], .msg] | @tsv'
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not have the local env var change the logging formatter used? https://github.com/ryanrolds/club/blob/master/golang/cmd/backend/logging.go#L21



4 changes: 2 additions & 2 deletions golang/pkg/signaling/room_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ func (r *Room) Receive(message Message) {
var group ReceiverGroup

// When joining a group, make sure to remove them from their previous group
recevier := member.GetParent()
receiver := member.GetParent()
if group != nil {
group = r.GetGroup(recevier.ID())
group = r.GetGroup(receiver.ID())
group.RemoveMember(member)
member.SetParent(nil)
}
Expand Down
2 changes: 1 addition & 1 deletion golang/pkg/signaling/websocket_peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func NewWebsocketPeer(conn WebsocketConn, parent ReceiverNode) *WebsocketPeer {
func (p *WebsocketPeer) Receive(message Message) {
logrus.Debugf("queueing message for %s", p.ID())
p.messages <- message
logrus.Debugf("finshed queuing message for %s", p.ID())
logrus.Debugf("finished queuing message for %s", p.ID())
}

func (p *WebsocketPeer) PumpWrite() {
Expand Down
130 changes: 130 additions & 0 deletions golang/static/examples/mirror.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
<!DOCTYPE html>
<html>

<head>
<style type="text/css">
div#videos>video {
display: inline-block;
width: 33%;
}

#background {
border: 5px solid red;
display: none;
}
canvas {
background-color: #ccc;
width: 33%;
display: none;
}

</style>
<script src="/js/event-target.js"></script>
<script src="/js/mirrormedia.js"></script>
<script src="/js/peering.js"></script>
<script src="/js/signaling.js"></script>
</head>

<body>
<!-- https://png-pixel.com -->
<img
id="background"
src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg==">

<canvas id="canvas" style="border:1px solid #d3d3d3;">
Your browser does not support the HTML5 canvas tag.
</canvas>

<div id="videos">
<video
controls=1 id="local" autoplay muted></video>
</div>

<script type="text/javascript">
var mirrorBackground = document.getElementById('background')
var mirrorVideo = document.getElementById('local')
var mirrorCanvas = document.getElementById('canvas')
let mirror = new MirrorMedia(mirrorVideo, mirrorCanvas)
let text = 'Mirror, Mirror, on the Wall... '
let remotePeerId = null

mirror.setupMedia().then(() => {
const framesPerTextScroll = 10
let framesUntilTextScroll = framesPerTextScroll
const ctx = mirrorCanvas.getContext('2d')

setInterval(() => {
let image = mirrorBackground

if (remotePeerId) {
const remoteVideo = document.getElementById(remotePeerId)
if (remoteVideo) {
mirrorCanvas.width = remoteVideo.videoWidth
mirrorCanvas.height = remoteVideo.videoHeight
image = remoteVideo
}
}
ctx.fillStyle = 'darkslateblue'
ctx.fillRect(0, 0, mirrorCanvas.width, mirrorCanvas.height)
ctx.drawImage(image, 0, 0, mirrorCanvas.width, mirrorCanvas.height)
ctx.font = 'italic 40pt Calibri'
ctx.fillStyle = 'lightgreen'
ctx.fillText(text, 10, mirrorCanvas.height / 2)

--framesUntilTextScroll
if (--framesUntilTextScroll === 0) {
framesUntilTextScroll = framesPerTextScroll
text = text.slice(1) + text.slice(0, 1)
}
}, 100)

let signals = new SignalingServer()
let peers = new Peering("videos", mirror.getStream(), signals)

signals.addEventListener("connected", async (event) => {
await mirror.onConnected()
await peers.onConnected()
signals.sendJoin()
})

signals.addEventListener("join", async (event) => {
let offer = await peers.onJoin(event.detail)
remotePeerId = event.detail.peerId
console.log('join remotePeerId', remotePeerId);
signals.sendOffer(event.detail.peerId, offer)
})

signals.addEventListener("leave", async (event) => {
await peers.onLeave(event.detail)
})

signals.addEventListener("offer", async (event) => {
remotePeerId = event.detail.peerId
console.log('offer remotePeerId', remotePeerId);
let answer = await peers.onOffer(event.detail)
signals.sendAnswer(event.detail.peerId, answer)
})

signals.addEventListener("icecandidate", async (event) => {
peers.onICECandidate(event.detail)
})

signals.addEventListener("answer", async (event) => {
await peers.onAnswer(event.detail)
})

signals.addEventListener("disconnected", async (event) => {
await mirror.onDisconnected()
await peers.onDisconnected()
})

let isHTTPS = location.protocol !== 'https:'
signals.connect((isHTTPS ? "ws" : "wss") + "://" + location.host + "/room")
window.addEventListener("unload", function () {
signals.sendLeave()
})
})
</script>
</body>

</html>
10 changes: 9 additions & 1 deletion golang/static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,17 @@
signals.connect((isHTTPS ? "ws" : "wss") + "://" + location.host + "/room")
window.addEventListener("unload", function () {
signals.sendLeave()
});
})
})
</script>

<hr>
<a href="/examples/audio.html">audio.html</a>
<a href="/examples/groups.html">groups.html</a>
<a href="/examples/local.html">local.html</a>
<a href="/examples/peer.html">peer.html</a>
<a href="/examples/peer.html">stun.html</a>
<a href="/examples/mirror.html">mirror.html</a>
</body>

</html>
25 changes: 25 additions & 0 deletions golang/static/js/mirrormedia.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

// eslint-disable-next-line no-unused-vars
class MirrorMedia {
constructor(video, canvas) {
this.id = video.id
this.video = video
this.canvas = canvas
this.stream = null
}

async setupMedia() {
const stream = this.canvas.captureStream(25)

this.stream = stream

this.video.srcObject = stream
}

getStream() {
return this.stream
}

async onConnected() {}
async onDisconnected() {}
}
7 changes: 7 additions & 0 deletions golang/static/js/peering.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,16 @@ class Peering {
let video = document.createElement("video")
video.id = peerId
video.autoplay = true
video.controls = true
video.muted = true
video.poster = 'https://upload.wikimedia.org/wikipedia/commons/d/d3/WUERFEL5_0-_bis_5-dimensionale_Wuerfelanaloge.png'

this.videosElm.appendChild(video)

setTimeout(() => {
video.play()
}, 1000)

Comment on lines +82 to +85
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any more details on the race condition that requires this? Are we not emitting an event somewhere? Maybe the issue is the lack of handling of the negotiationneeded, which the frontend should be handling correctly now.

peer.addEventListener('icecandidate', ({ candidate }) => {
if (candidate) {
this.signals.sendICECandidate(peerId, candidate)
Expand Down