Skip to content

Commit

Permalink
Added all of the video logic and rewrote it
Browse files Browse the repository at this point in the history
  • Loading branch information
gfranko committed May 16, 2014
2 parents fa2d61e + 32047b8 commit 90b490d
Show file tree
Hide file tree
Showing 11 changed files with 336 additions and 283 deletions.
2 changes: 1 addition & 1 deletion build/gifshot.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ gulp.task('clean', function() {
'transformAMDChecks': false,
// Wraps the library in an IIFE (Immediately Invoked Function Expression)
'wrap': {
'start': ';(function() {',
'end': '}());'
'start': ';(function(window, navigator, document, undefined) {',
'end': '}(window, window.navigator, document));'
},
'aggressiveOptimizations': true
}));
Expand Down
9 changes: 9 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<html>
<head>
<script src="src/gifshot.js"></script>
</head>

<body>

</body>
</html>
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,14 @@
"name": "Greg Franko"
}
],
"scripts": {},
"scripts": {
"preview": "node server/server.js",
"build": "gulp",
"watch": "gulp watch"
},
"devDependencies": {
"amdclean": "^2.0.0",
"express": "^4.2.0",
"gulp": "^3.6.2",
"gulp-jshint": "^1.6.0",
"gulp-rename": "^1.2.0",
Expand Down
10 changes: 10 additions & 0 deletions server/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// server.js (Express 4.0)
var express = require('express'),
app = express();

// SERVER CONFIGURATION
app.use(express.static(__dirname + '/../')); // set the static files location /public/img will be /img for users

app.listen(8001);

console.log('To preview gifshot, go to localhost:8001');
232 changes: 134 additions & 98 deletions src/gifshot.js
Original file line number Diff line number Diff line change
@@ -1,110 +1,146 @@
;(function() {var gumHelper, index;
gumHelper = function () {
// A couple of shims for having a common interface
window.URL = window.URL || window.webkitURL || window.mozURL || window.msURL;
navigator.getMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia;
//
var video;
var cameraStream;
/**
* Requests permission for using the user's camera,
* starts reading video from the selected camera, and calls
* `okCallback` when the video dimensions are known (with a fallback
* for when the dimensions are not reported on time),
* or calls `errorCallback` if something goes wrong
*/
function startStreaming(errorCallback, onStreaming, okCallback) {
var videoElement;
var cameraStream;
var attempts = 0;
var readyListener = function (event) {
findVideoSize();
};
var findVideoSize = function () {
;(function(window, navigator, document, undefined) {var utils, videoStream, index;
utils = {
'URL': window.URL || window.webkitURL || window.mozURL || window.msURL,
'getUserMedia': function () {
var getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia;
return getUserMedia ? getUserMedia.bind(navigator) : getUserMedia;
}(),
'isObject': function (obj) {
if (!obj) {
return false;
}
return Object.prototype.toString.call(obj) === '[object Object]';
},
'isArray': function (arr) {
if (!arr) {
return false;
}
if ('isArray' in Array) {
return Array.isArray(arr);
} else {
return Object.prototype.toString.call(arr) === '[object Array]';
}
},
'isFunction': function (func) {
if (!func) {
return false;
}
return Object.prototype.toString.call(func) === '[object Function]';
},
'noop': function () {
}
};
videoStream = function () {
return {
'videoElement': undefined,
'cameraStream': undefined,
'defaultVideoDimensions': {
'height': 640,
'width': 480
},
'findVideoSize': function findVideoSize(obj) {
var videoElement = obj.videoElement, cameraStream = obj.cameraStream, completedCallback = obj.completedCallback;
if (!videoElement) {
return;
}
if (videoElement.videoWidth > 0 && videoElement.videoHeight > 0) {
videoElement.removeEventListener('loadeddata', readyListener);
onDimensionsReady(videoElement.videoWidth, videoElement.videoHeight);
videoElement.removeEventListener('loadeddata', this.findVideoSize);
completedCallback({
'videoElement': videoElement,
'cameraStream': cameraStream,
'videoWidth': videoElement.videoWidth,
'videoHeight': videoElement.videoHeight
});
} else {
if (attempts < 10) {
attempts++;
if (findVideoSize.attempts < 10) {
findVideoSize.attempts += 1;
setTimeout(findVideoSize, 200);
} else {
onDimensionsReady(640, 480);
completedCallback({
'videoElement': videoElement,
'cameraStream': cameraStream,
'videoWidth': this.defaultVideoDimensions.width,
'videoHeight': this.defaultVideoDimensions.height
});
}
}
};
var onDimensionsReady = function (width, height) {
okCallback(cameraStream, videoElement, width, height);
};
videoElement = document.createElement('video');
videoElement.autoplay = true;
videoElement.addEventListener('loadeddata', readyListener);
navigator.getMedia({ video: true }, function (stream) {
onStreaming();
if (videoElement.mozSrcObject) {
videoElement.mozSrcObject = stream;
},
'onStreamingTimeout': function (callback) {
callback(new Error('Timed out while trying to start streaming'));
},
'errorCallback': function (obj) {
// ERROR!!!
new Error('getUserMedia cannot access the camera', obj);
},
'startStreaming': function (obj) {
var self = this, errorCallback = utils.isFunction(obj.error) ? obj.error : utils.noop, streamedCallback = utils.isFunction(obj.streamed) ? obj.streamed : utils.noop, completedCallback = utils.isFunction(obj.completed) ? obj.completed : utils.noop, videoElement = document.createElement('video'), cameraStream;
videoElement.autoplay = true;
utils.getUserMedia({ 'video': true }, function (stream) {
streamedCallback();
if (videoElement.mozSrcObject) {
videoElement.mozSrcObject = stream;
} else if (utils.URL) {
videoElement.src = utils.URL.createObjectURL(stream);
}
cameraStream = stream;
videoElement.play();
videoElement.addEventListener('loadeddata', function (event) {
self.findVideoSize({
'videoElement': videoElement,
'cameraStream': cameraStream,
'completedCallback': completedCallback
});
});
}, errorCallback);
},
startVideoStreaming: function (callback, options) {
options = options || {};
var self = this, noGetUserMediaSupportTimeout, timeoutLength = options.timeout !== undefined ? options.timeout : 0;
if (utils.isFunction(utils.getUserMedia)) {
// Some browsers apparently have support for video streaming because of the
// presence of the getUserMedia function, but then do not answer our
// calls for streaming.
// So we'll set up this timeout and if nothing happens after a while, we'll
// conclude that there's no actual getUserMedia support.
if (timeoutLength > 0) {
noGetUserMediaSupportTimeout = setTimeout(function () {
self.onStreamingTimeout(callback);
}, 10000);
}
this.startStreaming({
'error': self.errorCallback,
'streamed': function () {
// The streaming started somehow, so we can assume there is getUserMedia support
clearTimeout(noGetUserMediaSupportTimeout);
},
'completed': function (obj) {
var videoElement = this.videoElement = obj.videoElement, cameraStream = this.cameraStream = obj.cameraStream, width = obj.videoWidth, height = obj.videoHeight;
callback(cameraStream, videoElement, width, height);
}
});
} else {
videoElement.src = window.URL.createObjectURL(stream);
callback(new Error('Native device media streaming (getUserMedia) not supported in this browser.'));
}
cameraStream = stream;
videoElement.play();
}, errorCallback);
}
/**
* Try to initiate video streaming, and transparently handle cases
* where that is not possible (includes 'deceptive' browsers, see inline
* comment for more info)
*/
function startVideoStreaming(callback, options) {
options = options || {};
var noGUMSupportTimeout;
var timeoutLength = options.timeout !== undefined ? options.timeout : 0;
if (navigator.getMedia) {
// Some browsers apparently have support for video streaming because of the
// presence of the getUserMedia function, but then do not answer our
// calls for streaming.
// So we'll set up this timeout and if nothing happens after a while, we'll
// conclude that there's no actual getUserMedia support.
if (timeoutLength > 0) {
noGUMSupportTimeout = setTimeout(onStreamingTimeout, 10000);
},
'stopVideoStreaming': function () {
var cameraStream = this.cameraStream, videoElement = this.videoElement;
if (cameraStream) {
cameraStream.stop();
}
if (videoElement) {
videoElement.pause();
// TODO free src url object
videoElement.src = null;
videoElement = null;
}
startStreaming(function () {
// ERROR!!!
callback(new Error('getUserMedia cannot access the camera'));
}, function () {
// The streaming started somehow, so we can assume /there is/
// gUM support
clearTimeout(noGUMSupportTimeout);
}, function (stream, videoElement, width, height) {
// Keep references, for stopping the stream later on.
cameraStream = stream;
video = videoElement;
callback(null, stream, videoElement, width, height);
});
} else {
callback(new Error('Native device media streaming (getUserMedia) not supported in this browser.'));
}
function onStreamingTimeout() {
callback(new Error('Timed out while trying to start streaming'));
}
}
function stopVideoStreaming() {
if (cameraStream) {
cameraStream.stop();
}
if (video) {
video.pause();
// TODO free src url object
video.src = null;
video = null;
}
}
var GumHelper = {
startVideoStreaming: startVideoStreaming,
stopVideoStreaming: stopVideoStreaming
};
return GumHelper;
};
}();
index = function () {
console.log(gumHelper);
}();}());
videoStream.startVideoStreaming(function (cameraStream, videoElement, width, height) {
console.log('cameraStream', cameraStream);
console.log('videoElement', videoElement);
console.log('width', width);
console.log('height', height);
});
}();}(window, window.navigator, document));
9 changes: 0 additions & 9 deletions src/index.html

This file was deleted.

Loading

0 comments on commit 90b490d

Please sign in to comment.