Permalink
Browse files

initial pass at 'h5.userMedia' for 'getUserMedia()'

  • Loading branch information...
1 parent faf8169 commit b06fb9f873ac42e3a1c37e5d453b02201ae4b3e1 @getify committed Aug 20, 2012
Showing with 109 additions and 0 deletions.
  1. +14 −0 example-usermedia.html
  2. +12 −0 example-usermedia.js
  3. +83 −0 usermedia.h5ive.js
View
@@ -0,0 +1,14 @@
+<!DOCTYPE html>
+<html>
+<head>
+<title>Example: h5.userMedia</title>
+</head>
+<body>
+<h1>Example: h5.userMedia</h1>
+<video id="mycam" width="300" height="200" autoplay>
+
+<script src="core.h5ive.js"></script>
+<script src="usermedia.h5ive.js"></script>
+<script src="example-usermedia.js"></script>
+</body>
+</html>
View
@@ -0,0 +1,12 @@
+h5
+.userMedia({
+ video: true
+})
+.stream(function(src){
+ var video = document.getElementById("mycam");
+ video.src = src;
+ video.play();
+})
+.failed(function(){
+ alert("Access to the media failed.");
+})
View
@@ -0,0 +1,83 @@
+/*! usermedia.h5ive.js | (c) Kyle Simpson | MIT License: http://getify.mit-license.org */
+
+(function(h5){
+
+ if (!h5) throw new Error("userMedia.h5ive: core.h5ive required.");
+
+ var gUM = (navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia);
+
+ h5.userMedia = function(opts,successCB,failedCB) {
+ var publicAPI, success_cb, failed_cb,
+ success_args = [], failed_args = [], opts_str
+ ;
+
+ function stream(cb) {
+ if (!success_cb) success_cb = cb;
+ else if (success_cb === true) cb.apply(h5.userMedia,success_args);
+ else throw new Error("Success callback already defined.");
+ return publicAPI;
+ }
+
+ function failed(cb) {
+ if (!failed_cb) failed_cb = cb;
+ else if (success_cb === true) cb.apply(h5.userMedia,failed_args);
+ else throw new Error("Failed callback already defined.");
+ return publicAPI;
+ }
+
+ function handleSuccess() {
+ if (window.webkitURL) {
+ arguments[0] = webkitURL.createObjectURL(arguments[0]);
+ }
+ if (success_cb && typeof success_cb === "function") {
+ success_cb.apply(h5.userMedia,arguments);
+ }
+ else {
+ success_cb = true;
+ success_args = [].slice.call(arguments);
+ }
+ }
+
+ function handleFailure() {
+ if (failed_cb && typeof failed_cb === "function") {
+ failed_cb.apply(h5.userMedia,arguments);
+ }
+ else {
+ failed_cb = true;
+ failed_args = [].slice.call(arguments);
+ }
+ }
+
+ success_cb = successCB;
+ failed_cb = failedCB;
+
+ if (gUM) {
+ for (idx in opts) { if (opts.hasOwnProperty(idx)) {
+ opts_str += (opts_str != "" ? "," : "") + idx;
+ }}
+ try {
+ console.log(JSON.stringify(opts));
+ gUM.call(navigator,opts,handleSuccess,handleFailure);
+ }
+ catch (err) {
+ try {
+ gUM.call(navigator,opts_str,handleSuccess,handleFailure);
+ }
+ catch (err2) {
+ handleFailure("'getUserMedia' failed.");
+ }
+ }
+ }
+ else {
+ handleFailure("'getUserMedia' is not available.");
+ }
+
+ publicAPI = {
+ stream: stream,
+ failed: failed
+ };
+
+ return publicAPI;
+ };
+
+})(this.h5);

0 comments on commit b06fb9f

Please sign in to comment.