-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
407 lines (367 loc) · 14.6 KB
/
main.js
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
import AgoraRTC from 'agora-rtc-sdk-ng'
import * as THREE from 'three'
import { GLTFLoader } from 'three/examples/jsm/Addons.js'
import { FaceLandmarker, FilesetResolver } from '@mediapipe/tasks-vision'
import { initScene, initRenderLoop, getGraph } from './threeD'
import { showOverlayForm, createUserContainer, removeUserContainer, addVideoDiv, removeVideoDiv } from './ui'
// Create the Agora Client
const client = AgoraRTC.createClient({
codec: 'vp9',
mode: 'live',
role: 'host'
})
const localMedia = {
audio: {
track: null,
isActive: false
},
video: {
track: null,
isActive: false
},
canvas: {
track: null,
isActive: false
},
}
// Container for the remote streams
let remoteUsers = {}
const Loglevel = {
DEBUG: 0,
INFO: 1,
WARNING: 2,
ERROR: 3,
NONE: 4
}
AgoraRTC.enableLogUpload() // Auto upload logs to Agora
AgoraRTC.setLogLevel(Loglevel.ERROR) // Set Loglevel
//
let headRotation
let blendShapes
// Wait for DOM to load
document.addEventListener('DOMContentLoaded', async () => {
console.log('page-loaded')
addAgoraEventListeners() // Add the Agora Event Listeners
addLocalMediaControlListeners() // Add listeners to local media buttons
const joinform = document.getElementById('join-channel-form') // Get the join channel form
joinform.addEventListener('submit', handleJoin) // Add the function to handle form submission
showOverlayForm(true) // Show the overlay form
})
// User Form Submit Event
const handleJoin = async (event) => {
// stop the page from reloading
event.preventDefault()
// Get the channel name from the form input and remove any extra spaces
const glbInput = document.getElementById('form-rpm-url')
const glbURL = glbInput.value.trim()
// Check if the channel name is empty
if (!glbURL || glbURL === '') {
// Show error message and return early
glbInput.labels[0].style.color = '#F00'
glbInput.labels[0].textContent = '(Required) Ready Player Me URL'
return
}
// get the local-user container div
const localUserContainer = document.getElementById('local-user-container')
// show a loading animation
const loadingDiv = document.createElement('div')
loadingDiv.classList.add('lds-ripple')
loadingDiv.append(document.createElement('div'))
localUserContainer.append(loadingDiv)
// start the animation when the page loads
const { scene, camera, renderer } = await initScene(localUserContainer)
// use glb url to load Ready Player Me Avatar with morphtargets
const rpmMorphTargetsURL = glbURL + '?morphTargets=ARKit&textureAtlas=1024'
let nodes
// Load the GLB with morph targets
const loader = new GLTFLoader()
loader.load(rpmMorphTargetsURL,
async (gltf) => {
const avatar = gltf.scene
// build graph of avatar nodes
nodes = await getGraph(avatar)
const headMesh = nodes['Wolf3D_Avatar']
// adjust position
avatar.position.y = -1.65
avatar.position.z = 1
// TODO: remove testing output
console.log(avatar)
console.log(headMesh)
// add avatar to scene
scene.add(avatar)
// remove the loading spinner
loadingDiv.remove()
},
(event) => {
// outout loading details
console.log(event)
})
// initialize MediaPipe vision task
const faceLandmarker = await initVision()
// Init the local mic and camera
await initDevices('music_standard', '1080_3')
// Create video element
const video = document.createElement('video')
video.setAttribute('webkit-playsinline', 'webkit-playsinline');
video.setAttribute('playsinline', 'playsinline');
// Create a new MediaStream using camera track and set it the video's source object
video.srcObject = new MediaStream([localMedia.video.track.getMediaStreamTrack()])
// wait for source to finish loading
video.addEventListener("loadeddata", () => {
video.play() // start video playback
initPredictLoop(faceLandmarker, video) // start face landmarks prediction loop
})
// list of the mouth blend shapes
const mouthBlendShapes = [
'mouthSmile_L', 'mouthSmile_R', 'mouthFrown_L','mouthFrown_R',
'mouthOpen', 'mouthPucker','mouthWide','mouthShrugUpper','mouthShrugLower',
]
// multipliyer to embelish mouth movement
const exagerationMultiplier = 1.5
const threshold ={ min: 0.25, max: 0.6}
initRenderLoop(scene, camera, renderer, (time) => {
// return early if nodes or head rotation are null
if(!nodes || !headRotation) return
// apply rotatation data to head, neck, and shoulders bones
nodes.Head.rotation.set(headRotation.x, headRotation.y, headRotation.z)
nodes.Neck.rotation.set(headRotation.x/2, headRotation.y/2, headRotation.z/2)
nodes.Spine1.rotation.set(headRotation.x/3, headRotation.y/3, headRotation.z/3)
// loop through the blend shapes
blendShapes.forEach(blendShape => {
const headMesh = nodes.Wolf3D_Avatar
const blendShapeIndex = headMesh.morphTargetDictionary[blendShape.categoryName]
if (blendShapeIndex >= 0) {
// exaggerate the score for the mouth blend shapes
if (mouthBlendShapes.includes[blendShape.categoryName] && blendShape.score > threshold.min && blendShape.score < threshold.max ) {
blendShape.score *= exagerationMultiplier
}
headMesh.morphTargetInfluences[blendShapeIndex] = blendShape.score
}
})
})
const url = new URL(window.location.href)
const params = new URLSearchParams(url.search)
// generate a channel name if url param is not defined
const channelName = params.get('c') ?? generateChannelName()
// Join the channel
const appid = import.meta.env.VITE_AGORA_APP_ID
const uid = 0
const token = await getRtcToken(uid, channelName, 'publisher')
const localUid = await client.join(appid, channelName, token, uid)
// update the url
if (!params.has('c')){
// use url params to pass the channel name
url.searchParams.set('c', channelName)
window.history.pushState({}, "", url)
}
console.log(`joinedChannel with uid: ${localUid}`)
// Get video stream from canvas
const canvas = renderer.domElement
const fps = 30
const canvasStream = canvas.captureStream(fps)
// Get video track from canvas stream
const canvasVideoTrack = canvasStream.getVideoTracks()[0]
// use the canvasVideoTrack to create a custom Agora Video track
const customAgoraVideoTrack = AgoraRTC.createCustomVideoTrack({
mediaStreamTrack: canvasVideoTrack,
frameRate: fps
})
localMedia.canvas.track = customAgoraVideoTrack
localMedia.canvas.isActive = true
await client.publish([localMedia.audio.track, localMedia.canvas.track])
console.log('publishedTracks')
// Hide overlay form
showOverlayForm(false)
// show media controls (mic, video, leave)
document.getElementById('local-media-controls').style.display = 'block'
}
// init MediaPipe vision
const initVision = async () => {
// load latest Vision WASM
const vision = await FilesetResolver.forVisionTasks('https://cdn.jsdelivr.net/npm/@mediapipe/tasks-vision@latest/wasm')
// configure face landmark tracker
const faceLandmarker = await FaceLandmarker.createFromOptions(
vision, {
baseOptions: {
modelAssetPath: `https://storage.googleapis.com/mediapipe-models/face_landmarker/face_landmarker/float16/1/face_landmarker.task`,
},
outputFaceBlendshapes: true,
outputFacialTransformationMatrixes: true,
runningMode: 'VIDEO'
})
return faceLandmarker
}
const initPredictLoop = (faceLandmarker, video) => {
// flag to keep track of video stream's time
let lastVideoTime = -1
// prediction loop
const predict = () => {
// create a timestamp
const timeInMs = Date.now()
// while video is still streaming
if (lastVideoTime !== video.currentTime) {
lastVideoTime = video.currentTime
// run vison task to detect faces in video frame
const result = faceLandmarker.detectForVideo(video, timeInMs)
// get face matrix transformation for face 1
const faceMatrix = result.facialTransformationMatrixes
if (faceMatrix && faceMatrix.length > 0) {
const matrix = new THREE.Matrix4().fromArray(faceMatrix[0].data)
headRotation = new THREE.Euler().setFromRotationMatrix(matrix)
}
// get blend shape predictions for face 1
const blendShapePredictions = result.faceBlendshapes
if (blendShapePredictions && blendShapePredictions.length > 0){
blendShapes = blendShapePredictions[0].categories
}
}
// predect om every frame update
requestAnimationFrame(predict)
}
// start loop
requestAnimationFrame(predict)
}
// initialize mic and camera devices using Agora
const initDevices = async (audioConfig, cameraConfig) => {
if (!localMedia.audio.track || !localMedia.video.track) {
[ localMedia.audio.track, localMedia.video.track ] = await AgoraRTC.createMicrophoneAndCameraTracks({ audioConfig: audioConfig, videoConfig: cameraConfig })
}
// track audio state locally
localMedia.audio.isActive = true
localMedia.video.isActive = true
}
// Add client Event Listeners -- on page load
const addAgoraEventListeners = () => {
console.log(`Event Listeners added`)
// Add listeners for Agora Client Events
client.on('user-joined', handleRemotUserJoined)
client.on('user-left', handleRemotUserLeft)
client.on('user-published', handleRemotUserPublished)
client.on('user-unpublished', handleRemotUserUnpublished)
}
// New remote users joins the channel
const handleRemotUserJoined = async (user) => {
const uid = user.uid
remoteUsers[uid] = user // add the user to the remote users list
console.log(`User ${uid} joined the channel`)
await createUserContainer(uid)
}
// Remote user leaves the channel
const handleRemotUserLeft = async (user, reason) => {
const uid = user.uid
delete remoteUsers[uid]
console.log(`User ${uid} left the channel with reason:${reason}`)
await removeUserContainer(uid)
}
// Remote user publishes a track (audio or video)
const handleRemotUserPublished = async (user, mediaType) => {
const uid = user.uid
await client.subscribe(user, mediaType)
remoteUsers[uid] = user // update remote user reference
if (mediaType === 'video') {
addVideoDiv(uid) // create remote user div
user.videoTrack.play(`user-${uid}-video`) // play video on remote user div
} else if (mediaType === 'audio') {
user.audioTrack.play()
}
}
// Remote user unpublishes a track (audio or video)
const handleRemotUserUnpublished = async (user, mediaType) => {
const uid = user.uid
console.log(`User ${uid} unpublished their ${mediaType}`)
if (mediaType === 'video') {
removeVideoDiv(uid) // remove video div
}
}
// Add button listeners
const addLocalMediaControlListeners = () => {
// get buttons
const micToggleBtn = document.getElementById('mic-toggle')
const videoToggleBtn = document.getElementById('video-toggle')
const leaveChannelBtn = document.getElementById('leave-channel')
// Add clicks listners
micToggleBtn.addEventListener('click', handleMicToggle)
videoToggleBtn.addEventListener('click', handleVideoToggle)
leaveChannelBtn.addEventListener('click', handleLeaveChannel)
}
const handleMicToggle = async (event) => {
const isTrackActive = localMedia.audio.isActive // Get current audio state
await muteTrack(localMedia.audio.track, isTrackActive, event.target) // Mute/Unmute
localMedia.audio.isActive = !isTrackActive // Invert the audio state
}
const handleVideoToggle = async (event) => {
const isTrackActive = localMedia.canvas.isActive // Get current canvas state
await muteTrack(localMedia.canvas.track, isTrackActive, event.target) // Mute/Unmute
localMedia.canvas.isActive = !isTrackActive // Invert the video state
}
// Single function to mute audio/video tracks, using their common API
const muteTrack = async (track, mute, btn) => {
if (!track) return // Make sure the track exists
await track.setMuted(mute) // Mute the Track (Audio or Video)
btn.classList.toggle('media-active') // Add/Remove active class
btn.classList.toggle('muted') // Add/Remove muted class
}
const handleLeaveChannel = async () => {
// loop through and stop the local tracks
for (let mediaType in localMedia) {
const track = localMedia[mediaType].track
if (track) {
track.stop()
track.close()
localMedia[mediaType].track = null
localMedia[mediaType].isActive = false // reset the active flags
}
}
// Leave the channel
await client.leave()
console.log("client left channel successfully")
// Reset remote users
remoteUsers = {}
// Reset the UI
const mediaButtons = [document.getElementById('mic-toggle'), document.getElementById('video-toggle')]
mediaButtons.forEach(btn => {
btn.classList.add('media-active') // Add media-active class
btn.classList.remove('muted') // Remove mute class
});
document.getElementById('container').replaceChildren() // Clear the remote user divs
document.getElementById('local-user-container').replaceChildren() // Clear the local-user div
document.getElementById('local-media-controls').style.display = 'none' // hide media controls (mic, video, leave etc)
showOverlayForm(true) // Show the Join Form overlay
}
const getRtcToken = async (uid, channelName, role, expiration = 3600) => {
// Token-Server using: AgoraIO-Community/agora-token-service
const tokenServerURL = import.meta.env.VITE_AGORA_TOKEN_SERVER_URL + '/getToken'
const tokenRequest = {
"tokenType": "rtc",
"channel": channelName,
"uid": `${uid}`,
"role": role,
"expire": expiration // optional: expiration time in seconds (default: 3600)
}
try {
const tokenFetchResposne = await fetch(tokenServerURL, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(tokenRequest)
})
const data = await tokenFetchResposne.json()
return data.token
} catch (error) {
console.log(`fetch error: ${error}`)
}
}
const generateChannelName = () => {
const characters = 'abcdefghijklmnopqrstuvwxyz'
let randString = ''
for (let i=0; i < 9; i++) {
const randomIndex = Math.floor(Math.random() * characters.length)
randString += characters.charAt(randomIndex)
if ( (i+1)%3 == 0 && i < 8 ) {
randString += '-'
}
}
console.log(`channelName: ${randString}`)
return randString
}