Skip to content
This repository was archived by the owner on Aug 27, 2022. It is now read-only.

Commit

Permalink
feature: Add preview from gphoto
Browse files Browse the repository at this point in the history
Change-Id: Idd63d19ca60913bf5dc89f3d7ed17eacfa9545e8
  • Loading branch information
couz74 authored and andi34 committed Nov 14, 2020
1 parent 95613bf commit 981f4a0
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 48 deletions.
35 changes: 35 additions & 0 deletions api/takeVideo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
header('Content-Type: application/json');

require_once('../lib/config.php');

function isRunning($pid) {
try {
$result = shell_exec(
sprintf("ps %d", $pid)
);

if ( count(preg_split("/\n/", $result)) > 2) {
return true;
}
} catch(Exception $e) {

}

return false;
}

if ($_POST['play'] === "true" ) {
$pid = exec('gphoto2 --stdout --capture-movie | ffmpeg -i - -vcodec rawvideo -pix_fmt yuv420p -threads 0 -f v4l2 /dev/video0 > /dev/null 2>&1 & echo $!', $out);
sleep(3);
die(json_encode([
'isRunning' => isRunning($pid),
'pid' => $pid - 1
]));
} elseif($_POST['play'] === "false") {
exec('kill -15 '.$_POST['pid']);
die(json_encode([
'isRunning' => isRunning($_POST['pid']),
'pid' => $_POST['pid']
]));
}
131 changes: 83 additions & 48 deletions src/js/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ const photoBooth = (function () {
takingPic = false,
nextCollageNumber = 0,
currentCollageFile = '',
imgFilter = config.default_imagefilter;

let ioClient;
imgFilter = config.default_imagefilter,
ioClient,
pid;

const modal = {
open: function (selector) {
Expand Down Expand Up @@ -154,42 +154,55 @@ const photoBooth = (function () {
api.stopVideo('preview');
}

if (!navigator.mediaDevices) {
return;
}

const getMedia =
navigator.mediaDevices.getUserMedia ||
navigator.mediaDevices.webkitGetUserMedia ||
navigator.mediaDevices.mozGetUserMedia ||
false;
const dataVideo = {
play: 'true'
};

if (!getMedia) {
if (!navigator.mediaDevices) {
return;
}

if (config.previewCamFlipHorizontal) {
$('#video--view').addClass('flip-horizontal');
$('#video--preview').addClass('flip-horizontal');
}
jQuery
.post('api/takeVideo.php', dataVideo)
.done(function (result) {
console.log('Start webcam', result);
pid = result.pid;
const getMedia =
navigator.mediaDevices.getUserMedia ||
navigator.mediaDevices.webkitGetUserMedia ||
navigator.mediaDevices.mozGetUserMedia ||
false;

if (!getMedia) {
return;
}

getMedia
.call(navigator.mediaDevices, webcamConstraints)
.then(function (stream) {
if (mode === 'preview') {
$('#video--preview').show();
videoPreview.srcObject = stream;
api.stream = stream;
wrapper.css('background-image', 'none');
wrapper.css('background-color', 'transparent');
} else {
$('#video--view').show();
videoView.srcObject = stream;
if (config.previewCamFlipHorizontal) {
$('#video--view').addClass('flip-horizontal');
$('#video--preview').addClass('flip-horizontal');
}
api.stream = stream;

getMedia
.call(navigator.mediaDevices, webcamConstraints)
.then(function (stream) {
if (mode === 'preview') {
$('#video--preview').show();
videoPreview.srcObject = stream;
api.stream = stream;
wrapper.css('background-image', 'none');
wrapper.css('background-color', 'transparent');
} else {
$('#video--view').show();
videoView.srcObject = stream;
}
api.stream = stream;
})
.catch(function (error) {
console.log('Could not get user media: ', error);
});
})
.catch(function (error) {
console.log('Could not get user media: ', error);
.fail(function (xhr, status, result) {
console.log('Could not start webcam', result);
});
};

Expand All @@ -205,6 +218,28 @@ const photoBooth = (function () {
}
};

api.stopVideoAndTakePic = function (data) {
if (api.stream) {
const dataVideo = {
play: 'false',
pid: pid
};

jQuery
.post('api/takeVideo.php', dataVideo)
.done(function (result) {
console.log('Stop webcam', result);
const track = api.stream.getTracks()[0];
track.stop();
$('#video--view').hide();
api.callTakePicApi(data);
})
.fail(function (xhr, status, result) {
console.log('Could not stop webcam', result);
});
}
};

api.thrill = function (photoStyle) {
api.closeNav();
api.reset();
Expand Down Expand Up @@ -261,13 +296,7 @@ const photoBooth = (function () {
.appendTo('.cheese');
}

if (config.preview_mode === 'device_cam' && config.previewCamTakesPic && !api.stream && !config.dev) {
console.log('No preview by device cam available!');

api.errorPic({
error: 'No preview by device cam available!'
});
} else if (config.no_cheese) {
if (config.no_cheese) {
api.takePic(photoStyle);
} else {
setTimeout(() => {
Expand All @@ -286,14 +315,7 @@ const photoBooth = (function () {
ioClient.emit('photobooth-socket', 'in progress');
}

if (config.preview_mode === 'device_cam') {
if (config.previewCamTakesPic && !config.dev) {
videoSensor.width = videoView.videoWidth;
videoSensor.height = videoView.videoHeight;
videoSensor.getContext('2d').drawImage(videoView, 0, 0);
}
api.stopVideo('view');
} else if (config.preview_mode === 'url') {
if (config.preview_mode === 'url') {
$('#ipcam--view').removeClass('streaming');
$('#ipcam--view').hide();
}
Expand All @@ -312,6 +334,19 @@ const photoBooth = (function () {
loader.css('background', config.colors.panel);
loader.css('background-color', config.colors.panel);

if (config.preview_mode === 'device_cam') {
if (config.previewCamTakesPic && !config.dev) {
videoSensor.width = videoView.videoWidth;
videoSensor.height = videoView.videoHeight;
videoSensor.getContext('2d').drawImage(videoView, 0, 0);
}
api.stopVideoAndTakePic(data);
} else {
api.callTakePicApi(data);
}
};

api.callTakePicApi = function (data) {
jQuery
.post('api/takePic.php', data)
.done(function (result) {
Expand Down Expand Up @@ -360,7 +395,7 @@ const photoBooth = (function () {
currentCollageFile = '';
nextCollageNumber = 0;

api.processPic(photoStyle, result);
api.processPic(data.style, result);
}
})
.fail(function (xhr, status, result) {
Expand Down

0 comments on commit 981f4a0

Please sign in to comment.