gun/examples/basic/video.html
2020-05-23 20:08:30 -07:00

83 lines
2.5 KiB
HTML

<!DOCTYPE html>
<video id="video" width="100%"></video>
<center>
<input id="pass" placeholder="password">
<button id="play">Play</button>
Record <button class="record">Camera</button> or <button class="record">Screen</button>
</center>
<script src="../jquery.js"></script>
<script src="../../../gun/gun.js"></script>
<script src="../../../gun/sea.js"></script>
<script>
var gun = Gun(location.origin + '/gun');
var record = {recorder: null, recording: false};
$('.record').on('click', function(){
if(record.ing){ return }
record.stream($(this).text());
})
record.stream = function(type){
if('Camera' === type){
navigator.getMedia({ video: true, audio: true }, stream, error);
}
if('Screen' === type){
navigator.mediaDevices.getDisplayMedia({ video: true, audio: true }).then(stream, error);
}
function stream(stream){
var chunks = []; // we have a stream, we can record it
record.ing = new MediaRecorder(stream);
record.ing.ondataavailable = eve => chunks.push(eve.data);
record.ing.onstop = eve => record.save(new Blob(chunks));
record.ing.start()
$('#play').text("End");
}
function error(err){ console.log(err) }
}
record.save = function(data){
record.file = record.file || new FileReader();
record.file.readAsDataURL(data);
record.file.onloadend = async function(){
var b64 = record.file.result, pass;
b64 = $('#video').get(0).src = "data:video/webm" + b64.slice(b64.indexOf(';'));
if($('#pass').val()){ b64 = await SEA.encrypt(b64, $('#pass').val()) }
gun.get('test').get('video').put(b64);
$('#play').trigger('click');
}
}
$('#play').on('click', function(){
if(record.ing){
if(record.ing.stop){ record.ing.stop() }
record.ing = false;
return;
}
if(record.playing){
$('#video').get(0).pause();
$('#play').text("Play");
record.playing = false;
return;
}
if($('#video').get(0).src){
$('#video').get(0).play();
$('#play').text("Pause");
record.playing = true;
return;
}
$('#play').text("Loading");
gun.get('test').get('video').once(async function(data){
if($('#pass').val()){ data = await SEA.decrypt(data, $('#pass').val()) }
try{ $('#video').get(0).src = data }catch(e){}
$('#play').trigger('click');
})
})
$('#video').on('ended', function(){ $('#play').trigger('click') })
$('#video').on('error', function(){ $('#play').text("Failed"); })
navigator.getMedia = (navigator.getUserMedia || navigator.webkitGetUserMedia
|| navigator.mozGetUserMedia || navigator.msGetUserMedia);
</script>