Advertisement
raul3k

CamKit

Mar 3rd, 2014
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. CamKit = function()
  2. {
  3.     this.selector = '';
  4.  
  5.     this.video = true;
  6.     this.audio = true;
  7.  
  8.     this.width = 0;
  9.     this.height = 0;
  10.  
  11.     this.init = function() {
  12.  
  13.         // Errors messages
  14.         var errorsMsgs = {
  15.             NOT_SUPPORTED:  "Your browser does not support getUserMedia() method, please update it.",
  16.             SELECTOR_EMPTY: "Query selector is empty. Use CamKit.selector to select a DOM element."
  17.         };
  18.  
  19.         // Normalise window.URL
  20.         window.URL || (window.URL = window.webkitURL || window.msURL || window.oURL);
  21.  
  22.         // Normalise navigator.getUserMedia to navigator.getMedia function
  23.         navigator.getMedia = ( navigator.getUserMedia ||
  24.             navigator.webkitGetUserMedia ||
  25.             navigator.mozGetUserMedia ||
  26.             navigator.msGetUserMedia);
  27.  
  28.         // If the navigator supports the method getUserMedia
  29.         if(!navigator.getMedia){
  30.            console.log(errorsMsgs.NOT_SUPPORTED)
  31.            return false;
  32.         }
  33.  
  34.         // Attribute missing
  35.         if(this.selector == '') {
  36.             console.log(errorsMsgs.SELECTOR_EMPTY)
  37.             return false;
  38.         }
  39.  
  40.         var selector = this.selector;
  41.         var width = this.width;
  42.         var heigth = this.height;
  43.  
  44.         var attr = {video: this.video, audio: this.audio}
  45.  
  46.  
  47.         navigator.getMedia(attr, function(stream){
  48.             video = document.querySelector(selector);
  49.             video.src = (window.URL && window.URL.createObjectURL) ? window.URL.createObjectURL(stream) : stream;
  50.             video.width = width;
  51.             video.height = heigth;
  52.         }, function(errorCode){
  53.             console.log(errorsMsgs.NOT_SUPPORTED + "\n" + e);
  54.         });
  55.  
  56.         return true;
  57.     }
  58.  
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement