|
<!DOCTYPE html> |
|
<html lang="en"> |
|
|
|
<head> |
|
<meta charset="UTF-8"> |
|
<meta name="viewport" content="width=device-width, initial-scale=1.0"> |
|
<title>Parental Control</title> |
|
<style> |
|
html, |
|
body { |
|
margin: 0; |
|
padding: 0; |
|
width: 100%; |
|
height: 100%; |
|
overflow: hidden; |
|
} |
|
|
|
iframe { |
|
width: 100%; |
|
height: 100%; |
|
border: none; |
|
display: block; |
|
} |
|
</style> |
|
</head> |
|
|
|
<body> |
|
|
|
|
|
<iframe src="godot/index.html"></iframe> |
|
|
|
<script> |
|
|
|
const serverUrl = "./api/process" |
|
const FETCH_TIMEOUT = 5000 |
|
var token = "" |
|
|
|
|
|
const controller = new AbortController() |
|
const timeout = setTimeout(() => controller.abort(), FETCH_TIMEOUT) |
|
|
|
|
|
window.addEventListener('message', function (event) { |
|
if (event.data?.type === 'game_token') { |
|
token = event.data.data |
|
console.log("Token reçu :", token) |
|
} |
|
}) |
|
|
|
fetch(`./api/data?token=${token}`, { |
|
method: 'GET', |
|
signal: controller.signal |
|
}) |
|
.then(response => { |
|
clearTimeout(timeout) |
|
if (!response.ok) { |
|
throw new Error(`Server check failed: ${response.status}`) |
|
} |
|
console.log('Server check successful') |
|
setupAudioRecording() |
|
}) |
|
.catch(error => { |
|
clearTimeout(timeout) |
|
const errorMessage = error.name === 'AbortError' |
|
? 'Server request timed out. Please try again later.' |
|
: 'Could not connect to the server. Please try again later.' |
|
throw error |
|
}) |
|
|
|
|
|
function setupAudioRecording() { |
|
|
|
if (!navigator.mediaDevices?.getUserMedia) { |
|
console.error('Your browser does not support audio recording.') |
|
console.error('Please try using a modern browser like Chrome, Firefox, or Edge.') |
|
throw new Error('Audio recording not supported') |
|
} |
|
|
|
navigator.mediaDevices.getUserMedia({ audio: true }) |
|
.then(stream => { |
|
const audioContext = new (window.AudioContext || window.webkitAudioContext)() |
|
const mediaRecorder = new MediaRecorder(stream) |
|
const audioBuffer = [] |
|
const MAX_BUFFER_SIZE = 12 |
|
|
|
mediaRecorder.ondataavailable = event => { |
|
|
|
audioBuffer.push(event.data) |
|
if (audioBuffer.length > MAX_BUFFER_SIZE) { |
|
audioBuffer.shift() |
|
} |
|
|
|
|
|
const mergedBlob = new Blob(audioBuffer, { type: 'audio/webm' }) |
|
|
|
|
|
const reader = new FileReader() |
|
reader.readAsDataURL(mergedBlob) |
|
reader.onloadend = () => { |
|
const base64Audio = reader.result.split(',')[1] |
|
|
|
|
|
const audioController = new AbortController() |
|
const audioTimeout = setTimeout(() => audioController.abort(), FETCH_TIMEOUT) |
|
|
|
fetch(`${serverUrl}?token=${token}`, { |
|
method: 'POST', |
|
headers: { |
|
'Content-Type': 'application/json' |
|
}, |
|
body: JSON.stringify({ |
|
audio_chunk: base64Audio |
|
}), |
|
signal: audioController.signal |
|
}).then(response => { |
|
clearTimeout(audioTimeout) |
|
console.log('Audio chunk sent successfully') |
|
}).catch(error => { |
|
clearTimeout(audioTimeout) |
|
}) |
|
} |
|
} |
|
|
|
|
|
const chunkInterval = 300 |
|
mediaRecorder.start() |
|
setInterval(() => { |
|
mediaRecorder.stop() |
|
mediaRecorder.start() |
|
}, chunkInterval) |
|
}) |
|
.catch(error => { |
|
console.error('Error accessing microphone:', error) |
|
}) |
|
} |
|
</script> |
|
</body> |
|
|
|
</html> |