-
Notifications
You must be signed in to change notification settings - Fork 0
/
iam.html
267 lines (224 loc) · 8.34 KB
/
iam.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
<!DOCTYPE html>
<!--https://www.webcodegeeks.com/html5/html5-file-upload-example/-->
<html>
<head>
<title>112.camera</title>
<meta charset="utf-8" />
<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" />
<link href='css/showme.css' rel='stylesheet' />
<style>
body
{
overflow-x: hidden;
overflow-y: hidden;
font-family: "Lucida Sans Unicode", "Lucida Grande", "Arial", Helvetica, sans-serif;
background-color:#05466D;
color: #eeeeee;
padding: 0% 2.5%;
height: 100%;
}
.streaming_on
{
border: 3px solid red;
border-radius: 10px;
}
#userid
{
text-align: center;
font-size: 2.5em;
width: 100%;
overflow: hidden;
text-overflow: ellipsis;
font-family: "Lucida Sans Unicode", "Lucida Grande", "Arial", Helvetica, sans-serif;
background: transparent;
border: none;
border-bottom: 1px solid #607d8b;
color: #61A0C6;
font-weight: normal
}
</style>
<script src="lib/jquery-3.3.1.js?a=8" ></script>
<script src="lib/peerjs-0.3.18.js?a=8" ></script>
<script src='lib/showme.js?a=8' ></script>
<script src='params.js?a=8' ></script>
</head>
<body>
<center>
<div id='config'>
<div style='font-weight:normal; margin-top:1.5em'><b style='margin-bottom:.5em'>
Help the 112 to 📷 through a 🔗</b><br><br>Enter your ✆ number:
</div>
<br>
<input id='userid' type='tel' autofocus>
<br>
<br>
<div style='font-size:2em;border-radius:5px;padding:.1em;background:#AA2900;color:#61A0C6;' id='go'>GO</div><br>
</div>
<div id='text_for_112' style='color:#607d8b'>DEMO ONLY - NOT FOR REAL USE<br>I UNDERSTAND THE RISKS<br>After clicking on GO, your IP adress & browser identification will be recorded for security & development purposes and retained, during max 1 year. The images are never stored. Check <a href='https://docs.my-poppy.eu/privacy_poppy.html' style="color:#607d83">here</a> our privacy policy</div>
<br>
<video height="350" autoplay muted="muted" id="video-0" style="border-radius:10px; display:none"></video><br> <!-- VIDEO ELEMENT FOR WEBRTC -->
<div id='red_dot' class='blink_me' style='position:fixed; top:3.7em; right:1.5em; font-size:3em;margin-top:-2em;color:red;display:none'>●</div>
<div id='stop_video' style='position:fixed; bottom:1em; right:0; left:0; text-align:center; display:none'>To stop video: close the page</div>
</center>
<div class='center-div blink_me' id='waiting' style='display:none'>
CONNECTING...
</div>
<script>
/*
[url]?id=123456789 : prefills the number
[url]?id=123456789&auto=1 : prefills the number & initiates the connection immediately
[url]?id=123456789&auto=1&ts=99999999 : prefills the number & initiates the connection immediately, but not later than epoch 99999999
*/
var params = get_params();
var global_peer = null; // later, should be an array
var global_video = document.querySelector('video');
// on click on #go -> initializes the stream
$('#go').click(function()
{
peer_init(false);
});
// ---------------------------------------------------------------
// 1. check if should pre-fill the id and/or launch automatically
// ---------------------------------------------------------------
if (params.id != null)
{
$('#userid').val(parseInt(params.id));
if (params.auto != null)
{
if (params.auto == 1)
{
if (params.ts == null) peer_init(true); // if no timestamp, initiates the connection
else if (params.ts > Date.now() - 300000) peer_init(true); // if a timestamp, initiates the connection only if not expired
else history.pushState({ }, '112.camera', location.origin + '?id='+params.id); // if expired, rewrites so that to avoid that an old reconnection link triggers the streaming again
}
}
}
// -------------------------------------------------------------
// 2. initialize peer & mediastream
// -------------------------------------------------------------
function peer_init(auto)
{
if (!hasGetUserMedia())
{
return false;
}
var userid = parseInt($('#userid').val());
if (!validate_userid(userid))
{
error_msg();
return false;
}
if (auto)
{
$('#config').hide();
go(userid);
}
else
{ // animate
$('#config').slideUp(1500, function(){go(userid)});
}
function go(myuserid)
{
var url_to_connect = location.origin.replace('https://', '') + '/' + myuserid ;
$('#text_for_112').html('<br>Ask the 112 call taker to connect on<br><b>' + url_to_connect +'</b>').css('color', 'white')
$('#waiting').fadeIn()
}
var mypeerid = userid + '-1'; // the citizen (me)
var remotepeerid = userid + '-00'; // the call takers
$.post( 'query.php',
{
action: 'get_ice_servers',
},
function(e)
{
var connexion_timeout = null;
var ice_servers = null;
if (e.ice_servers != null)
{
ice_servers = e.ice_servers; // TODO: validate
peer_loop(ice_servers)
}
if (e.message != null)
{
switch(e.message)
{
case 'too_many_attempts': error_msg('The daily limit has been exceeded. Please mail us at [email protected] to expand it.'); break;
case 'unknown_action': error_msg('Unknown action'); break;
default: error_msg('Unknown error'); break;
}
}
function peer_loop(my_ice_servers)
{
global_peer = new Peer(mypeerid, {config: {iceServers: my_ice_servers}, host:peerjs_url, debug:3, port: 443, secure:true} );
// to reconnect when has been disconnected: the remote peer send a data connection request -> reconnect
global_peer.on('connection', reconnect)
var heartbeater = makePeerHeartbeater( global_peer );
//console.log(global_peer)
global_video.classList.remove("streaming_on")
$('#red_dot').hide()
if (connexion_timeout != null)
{
clearTimeout(connexion_timeout);
}
global_peer.on('open', function(e) { console.log('My peer ID is: ' + e); });
global_peer.on('close', function(e) { console.log('CLOSE: ' , e); });
global_peer.on('disconnected', function(e) { console.log('DISCONNECTED: ' , e); });
global_peer.on('error', function(e)
{
switch(e.type)
{
case 'peer-unavailable':
reconnect()
break;
default:
console.log(e.type)
error_msg()
stop_connect()
}
});
navigator.mediaDevices.getUserMedia({video: { facingMode: "environment" }, audio:true})
.then(function(my_media_stream)
{
global_video.srcObject = my_media_stream;
var call = global_peer.call(remotepeerid, my_media_stream);
connexion_timeout = setTimeout(function()
{
clearTimeout(connexion_timeout); connexion_timeout = null;
global_video.classList.add("streaming_on");
$('#red_dot').show();
$('#video-0').show();
$('#stop_video').fadeIn();
$('#waiting').hide()
}, 7000);
})
.catch(function(e)
{
console.log('ERROR', e)
stop_connect()
});
function stop_connect()
{
peer_destroy(global_peer, global_video, function(){global_peer=null; global_video.classList.remove("streaming_on"); $('#red_dot').hide(); })
}
function reconnect()
{
peer_destroy(global_peer, global_video, function(){global_peer=null; global_video.classList.remove("streaming_on"); $('#red_dot').hide(); setTimeout(function(){peer_loop(my_ice_servers)},1000); })
}
}
}, 'json');
function networkChange(e)
{
switch(e.type)
{
case 'offline': $('#waiting').html('Waiting for network').addClass('blink_me').show(); break; // should also handle the case where is waiting for connexion -> no network -> again waiting
case 'online': setTimeout(function(){location.href=location.origin + '/?id=' + userid + "&auto=1&ts="+Date.now();}, 45000); break; // easyest: reload the page. all my other more subtle attempts have failed
// timeout: server needs time to clean the id
// ok for demo, but in production, should be better -- instataneous, not 45 sec wait...
}
}
this.addEventListener('online', networkChange);
this.addEventListener('offline', networkChange);
}
</script>
</body>
</html>