-
Notifications
You must be signed in to change notification settings - Fork 3
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
DoctorBud
wants to merge
1
commit into
ryanrolds:master
Choose a base branch
from
DoctorBud:mirror-play
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
peer.addEventListener('icecandidate', ({ candidate }) => { | ||
if (candidate) { | ||
this.signals.sendICECandidate(peerId, candidate) | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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