-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
78 lines (66 loc) · 2.47 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Video Capture and Transmission</title>
</head>
<body>
<video id="video" width="640" height="480" autoplay></video>
<button id="startButton">Start Recording</button>
<button id="stopButton" style="display: none;">Stop Recording</button>
<script src="https://cdn.socket.io/4.0.0/socket.io.min.js"></script>
<script>
document.addEventListener('DOMContentLoaded', () => {
const videoElement = document.getElementById('video');
const startButton = document.getElementById('startButton');
const stopButton = document.getElementById('stopButton');
// Initialize socket.io connection
const socket = io();
// Get user media and stream video
navigator.mediaDevices.getUserMedia({ video: true })
.then((stream) => {
videoElement.srcObject = stream;
// Start capturing and transmitting video data
let isRecording = false;
let mediaRecorder;
startButton.addEventListener('click', () => {
if (!isRecording) {
mediaRecorder = new MediaRecorder(stream);
const chunks = [];
mediaRecorder.ondataavailable = (event) => {
if (event.data.size > 0) {
chunks.push(event.data);
}
};
mediaRecorder.onstop = () => {
const blob = new Blob(chunks, { type: 'video/webm' });
const reader = new FileReader();
reader.onloadend = () => {
const base64data = reader.result.split(',')[1];
socket.emit('videoData', { type: 'video', data: base64data });
};
reader.readAsDataURL(blob);
};
mediaRecorder.start();
isRecording = true;
startButton.style.display = 'none';
stopButton.style.display = 'inline-block';
}
});
stopButton.addEventListener('click', () => {
if (isRecording) {
mediaRecorder.stop();
isRecording = false;
startButton.style.display = 'inline-block';
stopButton.style.display = 'none';
}
});
})
.catch((error) => {
console.error('Error accessing camera:', error);
});
});
</script>
</body>
</html>