-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.html
212 lines (176 loc) · 7.15 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Mediasoup Adapter Demo</title>
<meta name="description" content="Mediasoup Adapter Demo" />
<!-- <script src="https://aframe.io/releases/1.3.0/aframe.min.js"></script> -->
<script src="../dist/aframe.min.js"></script>
<!-- <script src="https://cdn.socket.io/4.5.3/socket.io.min.js"
integrity="sha384-WPFUvHkB1aHA5TDSZi6xtDgkF0wXJcIIxXhC6h8OT8EH3fC5PWro5pWJ1THjcfEi"
crossorigin="anonymous"></script> -->
<script src="../dist/socketio_client_4.5.3.min.js"></script>
<!-- <script src="https://unpkg.com/networked-aframe@^0.10.0/dist/networked-aframe.min.js"
crossorigin="anonymous"></script> -->
<script src="../dist/networked-aframe.min.js"></script>
<script src="../dist/mediasoup-adapter.js"></script>
<!-- mediasoup-client necessary -->
<script src="../dist/mediasoupclient.min.js"></script>
<script>
console.log('mediasoup-client-version', mediasoupClient.version);
</script>
<script src="./js/aframe-randomizer-components.min.js"></script>
<script src="./js/aframe-environment-component.min.js"></script>
<style>
.control {
position: absolute;
width: 600px;
height: 50px;
left: 20px;
bottom: 20px;
background: #4fc3c3;
border: 1px solid #232322;
border-radius: 10px;
display: flex;
justify-content: space-between;
align-items: center;
z-index: 99
}
#videoBtn,
#audioBtn,
#shareBtn {
height: 100%;
font-size: 20px;
font-family: 'Comic Sans MS';
border: 1px solid #232322;
border-radius: 10px;
}
</style>
</head>
<body>
<a-scene networked-scene="
room: default;
debug: true;
adapter: mediasoup;
">
<a-assets timeout="1000">
<img id="sky" crossorigin="anonymous">
<!-- Avatar -->
<template id="avatar-template">
<a-entity class="avatar">
<a-plane color="#FFF" width="4" height="3" position="0 5 0" material="side: back"
networked-video-source="streamName: video">
</a-plane>
<a-plane color="#FFF" width="4" height="3" position="0 1 0" material="side: back"
networked-video-source="streamName: screenshare">
</a-plane>
<!-- <a-plane color="#FFF" width="4" height="3" rotation="0 0 0" position="0 5 0" material="side: front"
networked-audio-source="streamName: audio">
</a-plane> -->
<a-sphere class="head" scale="0.45 0.5 0.4"></a-sphere>
<a-entity class="face" position="0 0.05 0">
<a-sphere class="eye" color="#efefef" position="0.16 0.1 -0.35" scale="0.12 0.12 0.12">
<a-sphere class="pupil" color="#000" position="0 0 -1" scale="0.2 0.2 0.2"></a-sphere>
</a-sphere>
<a-sphere class="eye" color="#efefef" position="-0.16 0.1 -0.35" scale="0.12 0.12 0.12">
<a-sphere class="pupil" color="#000" position="0 0 -1" scale="0.2 0.2 0.2"></a-sphere>
</a-sphere>
</a-entity>
</a-entity>
</template>
</a-assets>
<a-entity id="rig">
<a-entity id="player" networked="template:#avatar-template;attachTemplateToLocal:false" camera position="0 4 0"
wasd-controls look-controls>
<a-sphere class="head" visible="true" random-color></a-sphere>
</a-entity>
</a-entity>
<div class="control">
<button id="videoBtn" onClick="toggleStream(this, 'video')" disabled>enable webcam</button>
<button id="shareBtn" onClick="toggleStream(this, 'screenshare')" disabled>enable screenshare</button>
<button id="audioBtn" onClick="toggleStream(this, 'audio')" disabled>enable microphone</button>
</div>
<a-entity environment="preset:arches"></a-entity>
<a-entity light="type:ambient;intensity:0.5"></a-entity>
</a-scene>
<script>
document.querySelector('#sky').src = `https://cdn.pixabay.com/photo/2022/11/09/01/59/profile-7579739_1280.jpg?random=${+new Date()}`
// Called by Networked-Aframe when connected to server
function onConnect() {
console.log('onConnect', new Date());
}
let webcamStream = null;
let microphoneStream = null;
let screenshareStream = null;
// add templates to sync
NAF.schemas.add({
template: '#avatar-template',
components: [
'position',
'rotation',
{
selector: '.head',
component: 'material',
property: 'color'
}
]
});
async function toggleStream(target, type) {
let stream = type === 'video' ? webcamStream : type === 'audio' ? microphoneStream : screenshareStream
if (target.textContent.includes('enable')) {
try {
// enable/resume stream
if (!stream) {
// first time
switch (type) {
case 'video':
webcamStream = await navigator.mediaDevices.getUserMedia({ video: true, audio: false })
await NAF.connection.adapter.addLocalMediaStream(webcamStream, type)
break;
case 'audio':
microphoneStream = await navigator.mediaDevices.getUserMedia({ video: false, audio: true })
await NAF.connection.adapter.addLocalMediaStream(microphoneStream, type)
break;
case 'screenshare':
screenshareStream = await navigator.mediaDevices.getDisplayMedia()
await NAF.connection.adapter.addLocalMediaStream(screenshareStream, type)
break;
default:
return console.log(`unknown mediastream type: ${type}`)
break;
}
} else {
const { e, msg, length } = type === 'video' ? NAF.connection.adapter.enableCamera(true) :
type === 'audio' ? NAF.connection.adapter.enableMicrophone(true) :
type === 'screenshare' ? NAF.connection.adapter.enableScreenSharing(true) : null
if (e) return
if (!length) return console.log(`no ${type} producers now`);
}
} catch (e) {
return console.log(`error occured when enabing stream...`, e);
}
target.textContent = `disable ${type}`
} else {
// disable/pause stream
if (!stream) return console.log(`no ${type} stream yet`)
const { e, msg, length } = type === 'video' ? NAF.connection.adapter.enableCamera(false) :
type === 'audio' ? NAF.connection.adapter.enableMicrophone(false) :
type === 'screenshare' ? NAF.connection.adapter.enableScreenSharing(false) : null
if (e) return
if (!length) return console.log(`no ${type} producers now`);
target.textContent = `enable ${type}`
}
}
document.addEventListener('DOMContentLoaded', () => {
document.querySelector('a-scene').addEventListener('adapter-ready', ({ detail: adapter }) => {
// disable/enable simulcast
adapter.simulcastMode = false
// set heartbeat interval to xx secs
adapter.setHeartbeatTimer(30)
// allow to control the stream
videoBtn.disabled = audioBtn.disabled = shareBtn.disabled = false
})
})
</script>
</body>
</html>