-
Notifications
You must be signed in to change notification settings - Fork 0
/
ip_camera.html
242 lines (208 loc) · 7.53 KB
/
ip_camera.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
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Smartphone IP Camera</title>
<style>
body {
font-family: Arial, sans-serif;
text-align: center;
}
video {
width: 100%;
max-width: 600px;
height: auto;
margin-top: 20px;
}
#camera-id, #viewer-id {
word-break: break-all;
font-weight: bold;
}
.section {
margin: 20px;
}
input, button, select {
padding: 10px;
font-size: 16px;
margin: 5px;
}
.sensor-data {
text-align: left;
margin: 20px;
}
</style>
</head>
<body>
<h1>Smartphone IP Camera</h1>
<div class="section">
<!-- Camera Section -->
<h2>Camera</h2>
<label for="camera-select">Select Camera:</label>
<select id="camera-select"></select>
<br>
<button id="start-camera">Start Camera</button>
<p>Your Camera ID: <span id="camera-id">Not connected</span></p>
<video id="local-video" autoplay muted></video>
<br>
<label for="viewer-id-input">Enter Viewer ID:</label>
<input type="text" id="viewer-id-input" placeholder="Viewer ID">
<button id="call-viewer">Call Viewer</button>
</div>
<div class="section">
<!-- Viewer Section -->
<h2>Viewer</h2>
<button id="initialize-viewer">Initialize Viewer</button>
<p>Your Viewer ID: <span id="viewer-id">Not connected</span></p>
<video id="remote-video" autoplay controls></video>
</div>
<div class="sensor-data">
<h2>Sensor Data</h2>
<p>Acceleration (m/s²): X=<span id="accel-x">0</span>, Y=<span id="accel-y">0</span>, Z=<span id="accel-z">0</span></p>
<p>Rotation Rate (°/s): Alpha=<span id="rot-alpha">0</span>, Beta=<span id="rot-beta">0</span>, Gamma=<span id="rot-gamma">0</span></p>
<p>Orientation (°): Alpha=<span id="orient-alpha">0</span>, Beta=<span id="orient-beta">0</span>, Gamma=<span id="orient-gamma">0</span></p>
</div>
<!-- Include PeerJS library -->
<script src="https://unpkg.com/[email protected]/dist/peerjs.min.js"></script>
<script>
let localStream;
let cameraPeer;
let viewerPeer;
let selectedCameraId;
// Enumerate cameras and populate the camera select dropdown
async function enumerateCameras() {
const devices = await navigator.mediaDevices.enumerateDevices();
const videoDevices = devices.filter(device => device.kind === 'videoinput');
const cameraSelect = document.getElementById('camera-select');
videoDevices.forEach((device, index) => {
const option = document.createElement('option');
option.value = device.deviceId;
option.text = device.label || `Camera ${index + 1}`;
cameraSelect.appendChild(option);
});
// Set the first camera as the default selected camera
if (videoDevices.length > 0) {
selectedCameraId = videoDevices[0].deviceId;
}
cameraSelect.onchange = () => {
selectedCameraId = cameraSelect.value;
};
}
// Call enumerateCameras on page load
enumerateCameras();
// Sensor Data Handling
function handleMotion(event) {
const accelX = event.acceleration.x || 0;
const accelY = event.acceleration.y || 0;
const accelZ = event.acceleration.z || 0;
document.getElementById('accel-x').textContent = accelX.toFixed(2);
document.getElementById('accel-y').textContent = accelY.toFixed(2);
document.getElementById('accel-z').textContent = accelZ.toFixed(2);
const rotAlpha = event.rotationRate.alpha || 0;
const rotBeta = event.rotationRate.beta || 0;
const rotGamma = event.rotationRate.gamma || 0;
document.getElementById('rot-alpha').textContent = rotAlpha.toFixed(2);
document.getElementById('rot-beta').textContent = rotBeta.toFixed(2);
document.getElementById('rot-gamma').textContent = rotGamma.toFixed(2);
}
function handleOrientation(event) {
const alpha = event.alpha || 0;
const beta = event.beta || 0;
const gamma = event.gamma || 0;
document.getElementById('orient-alpha').textContent = alpha.toFixed(2);
document.getElementById('orient-beta').textContent = beta.toFixed(2);
document.getElementById('orient-gamma').textContent = gamma.toFixed(2);
}
// Request permission for sensor access
function requestSensorAccess() {
if (typeof DeviceMotionEvent.requestPermission === 'function') {
DeviceMotionEvent.requestPermission()
.then(permissionState => {
if (permissionState === 'granted') {
window.addEventListener('devicemotion', handleMotion);
window.addEventListener('deviceorientation', handleOrientation);
} else {
alert('Permission to access device motion data denied.');
}
})
.catch(console.error);
} else {
// Non-iOS devices
window.addEventListener('devicemotion', handleMotion);
window.addEventListener('deviceorientation', handleOrientation);
}
}
// Request sensor access on page load
requestSensorAccess();
// Camera Functionality
document.getElementById('start-camera').onclick = async () => {
try {
// Request access to the selected camera and microphone
localStream = await navigator.mediaDevices.getUserMedia({
video: { deviceId: selectedCameraId ? { exact: selectedCameraId } : undefined },
audio: true
});
document.getElementById('local-video').srcObject = localStream;
// Initialize Camera Peer
cameraPeer = new Peer();
cameraPeer.on('open', (id) => {
document.getElementById('camera-id').textContent = id;
alert('Your Camera ID is: ' + id);
});
cameraPeer.on('error', (err) => {
console.error(err);
alert('An error occurred: ' + err);
});
} catch (err) {
console.error(err);
alert('Could not access camera or microphone: ' + err);
}
};
document.getElementById('call-viewer').onclick = () => {
const viewerId = document.getElementById('viewer-id-input').value.trim();
if (!viewerId) {
alert('Please enter a valid Viewer ID.');
return;
}
if (!cameraPeer || !cameraPeer.open) {
alert('Camera peer is not initialized. Please start the camera first.');
return;
}
// Initiate the call to the viewer
const call = cameraPeer.call(viewerId, localStream);
call.on('error', (err) => {
console.error(err);
alert('An error occurred during the call: ' + err);
});
call.on('close', () => {
console.log('The call has ended.');
});
};
// Viewer Functionality
document.getElementById('initialize-viewer').onclick = () => {
viewerPeer = new Peer();
viewerPeer.on('open', (id) => {
document.getElementById('viewer-id').textContent = id;
alert('Your Viewer ID is: ' + id);
});
viewerPeer.on('call', (call) => {
// Answer the call without sending a stream
call.answer(null);
call.on('stream', (remoteStream) => {
document.getElementById('remote-video').srcObject = remoteStream;
});
call.on('error', (err) => {
console.error(err);
alert('An error occurred during the call: ' + err);
});
call.on('close', () => {
console.log('The call has ended.');
});
});
viewerPeer.on('error', (err) => {
console.error(err);
alert('An error occurred: ' + err);
});
};
</script>
</body>
</html>