Icontem

File: js/CamKit.js

Recommend this page to a friend!
  Classes of Raul  >  JavaScript Video Cam Kit  >  js/CamKit.js  >  Download  
File: js/CamKit.js
Role: Class source
Content type: text/plain
Description: The CamKit Class
Class: JavaScript Video Cam Kit
Embed webcam video and microphone audio on a page
Author: By
Last change: Better sctructure, better recognizing of supported browsers
Date: 5 years ago
Size: 1,780 bytes
 

Contents

Class file image Download
CamKit = function()
{
    this.selector = '';

    this.video = true;
    this.audio = true;

    this.width = 0;
    this.height = 0;

    this.init = function() {

		// Errors messages
        var errorsMsgs = {
            NOT_SUPPORTED:  "Your browser does not support getUserMedia() method, please update it.",
            SELECTOR_EMPTY: "Query selector is empty. Use CamKit.selector to select a DOM element."
        };

        // Normalise window.URL
        window.URL || (window.URL = window.webkitURL || window.msURL || window.oURL);

        // Normalise navigator.getUserMedia to navigator.getMedia function
        navigator.getMedia = ( navigator.getUserMedia ||
            navigator.webkitGetUserMedia ||
            navigator.mozGetUserMedia ||
            navigator.msGetUserMedia);

        // If the navigator supports the method getUserMedia
        if(!navigator.getMedia){
           console.log(errorsMsgs.NOT_SUPPORTED)
           return false;
        }

        // Attribute missing
        if(this.selector == '') {
            console.log(errorsMsgs.SELECTOR_EMPTY)
            return false;
        }

        var selector = this.selector;
        var width = this.width;
        var heigth = this.height;

        var attr = {video: this.video, audio: this.audio}


        navigator.getMedia(attr, function(stream){
            video = document.querySelector(selector);
            video.src = (window.URL && window.URL.createObjectURL) ? window.URL.createObjectURL(stream) : stream;
            video.width = width;
            video.height = heigth;
        }, function(errorCode){
            console.log(errorsMsgs.NOT_SUPPORTED + "\n" + e);
        });

        return true;
    }

}