-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
69 lines (61 loc) · 2.61 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>AR с HTML</title>
<script src="https://aframe.io/releases/1.2.0/aframe.min.js"></script>
<script src="https://rawgit.com/jeromeetienne/AR.js/master/aframe/build/aframe-ar.js"></script>
<script src="/socket.io/socket.io.js"></script>
</head>
<body>
<a-scene embedded arjs>
<!-- Видео -->
<video id="video" autoplay loop src="http://localhost:8080/video.mp4" crossorigin="anonymous"></video>
<a-assets>
<!-- Видео для текстуры -->
<video id="screenStream" autoplay></video>
</a-assets>
<!-- Маркер с кубом -->
<a-marker preset="hiro">
<a-box position="0 0.5 0" material="src: #video;"></a-box> <!-- Указываем, что видео будет текстурой -->
</a-marker>
<!-- Камера -->
<a-entity camera></a-entity>
</a-scene>
<script>
const socket = io.connect('http://localhost:3000');
let peerConnection;
const config = { iceServers: [{ urls: 'stun:stun.l.google.com:19302' }] };
const videoElement = document.getElementById('screenStream');
peerConnection = new RTCPeerConnection(config);
peerConnection.ontrack = (event) => {
videoElement.srcObject = event.streams[0];
};
peerConnection.onicecandidate = (event) => {
if (event.candidate) {
socket.emit('signal', { target: 'TARGET_ID', signal: { candidate: event.candidate } });
}
};
socket.on('signal', async (data) => {
if (data.signal.sdp) {
await peerConnection.setRemoteDescription(new RTCSessionDescription(data.signal.sdp));
if (data.signal.sdp.type === 'offer') {
const answer = await peerConnection.createAnswer();
await peerConnection.setLocalDescription(answer);
socket.emit('signal', { target: data.from, signal: { sdp: answer } });
}
}
if (data.signal.candidate) {
await peerConnection.addIceCandidate(new RTCIceCandidate(data.signal.candidate));
}
});
</script>
<script>
document.querySelector('a-scene').addEventListener('click', function () {
const video = document.getElementById('video');
video.play();
});
</script>
</body>
</html>