Created
February 28, 2012 08:58
-
-
Save tgvashworth/1931422 to your computer and use it in GitHub Desktop.
navigator.getUserMedia shim - navigator.getMedia. Normalises & detects object & string option styles.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<video id="video" autoplay> | |
<source type="video/webm" src="http://miketaylr.com/misc/dontstop.webm"> | |
</video> | |
<script src="userMedia.js"></script> | |
<script> | |
// Tell em how it is | |
if(!navigator.getUserMedia) { | |
console.log("Don't think you got it, sonny."); | |
} | |
var hollaback = function(stream) { | |
video.src = stream; | |
} | |
, errback = function(e) { | |
console.log(e) | |
} | |
, video = document.getElementById('video'); | |
// Gogogo. | |
navigator.userMedia({video: true, audio: true}, hollaback, errback); | |
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// navigator.userMedia() | |
// An attempt to normalise navigator.getUserMedia(...) behaviour | |
// by Tom Ashworth, based on the work of @miketaylr | |
// https://gist.github.com/f2ac64ed7fc467ccdfe3 | |
navigator.userMedia = (function (win) { | |
// Normalise vendor-specific window.URL & navigator.getUserMedia() | |
window.URL || (window.URL = window.webkitURL || window.msURL || window.oURL); | |
navigator.getUserMedia || (navigator.getUserMedia = navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia); | |
if(!navigator.getUserMedia) { return function(){}; } // Needs better error for non-existence? | |
// detect if object or string style options by creating an iframe and blowing it up | |
// style jacked from @kangax | |
// jacked here from @miketaylr: //gist.github.com/f2ac64ed7fc467ccdfe3 | |
var optionStyle = (function () { | |
var el = document.createElement('iframe'), | |
root = document.body || document.documentElement, | |
string = true, object = true, nop = function(){}; | |
root.appendChild(el); | |
var f = win.frames[win.frames.length-1]; | |
f.navigator.getUserMedia || (f.navigator.getUserMedia = f.navigator.webkitGetUserMedia || f.navigator.mozGetuserMedia || f.navigator.msGetUserMedia); | |
try { // Try spec (object) syntax | |
f.navigator.getUserMedia({video: true, audio: true}, nop); | |
} catch (e) { | |
object = false; | |
try { // Try string syntax | |
f.navigator.getUserMedia("video, audio", nop); | |
} catch (e) { // Neither supported | |
string = false; | |
} | |
} finally { // Clean up | |
root.removeChild(el); | |
el = null; | |
} | |
return { string: string, object: object } | |
}(window)); | |
function normaliseOptions (op) { | |
var options = {}; | |
// Neither supported... whut. | |
if( !optionStyle.string && !optionStyle.object ) { | |
return undefined; | |
} | |
// Supports a string, so return the string passed | |
if( optionStyle.string && !optionStyle.object ) { | |
return op; | |
} | |
// Supports an object, if a string was passed | |
// turn it into an object (future-proof-ish) | |
if( typeof op == 'string' ) { | |
var tmp = op.split(','); | |
for(var i=0,l=tmp.length;i<l;i++) { | |
var media = tmp[i].trim(); | |
options[media] = true; | |
} | |
} else if( typeof op == 'object' ) { | |
// Supports object, passed object | |
options = op; | |
} else { | |
// Tsch | |
return undefined; | |
} | |
console.log(options); | |
return options; | |
} | |
function successNormalised (stream) { | |
stream = (window.URL && window.URL.createObjectURL) ? window.URL.createObjectURL(stream) : stream; | |
return successCallback(stream); | |
} | |
var successCallback = function () {} | |
, errorCallback = function () {}; | |
return function (options, success, error) { | |
// Parse the options so they is proper like | |
var options = normaliseOptions(options); | |
successCallback = success; | |
errorCallback = error; | |
// Fire her up | |
return navigator.getUserMedia(options, successNormalised, errorCallback); | |
}; | |
}(window)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
So this isn't quite a 'shim' as such, because it doesn't introduce functionality in non-supported browsers, but it does normalise the behaviour between standard compliant and non-compliant implementations.