Source: core.js

/** @module VideoRTC */

var Janus = require('./externals/janus.nojquery');
var Promise = require('bluebird/js/release/bluebird');

var echoTest = require('./lib/echoTest').echoTest;
var splitClient = require('./lib/splitClient').splitClient;
var splitAgent = require('./lib/splitAgent').splitAgent;
var videoRoom = require('./lib/videoRoom').videoRoom;
var videoPlayer = require('./lib/videoPlayer').videoPlayer;
var getDevices = require('./lib/getDevices').getDevices;
var detect = require('./lib/detect').detect;
var videoMail = require('./lib/videoMail').videoMail;
var videoCall = require('./lib/videoCall').videoCall;

if(!window.VideoRTC) window.VideoRTC = {};

function VideoRTC(endPoints, iceServers, debugLevel) {
    if(!endPoints && !iceServers) return null;
    debugLevel = (debugLevel === 'all' || debugLevel === 'trace' || debugLevel === 'debug' || debugLevel === 'log' || debugLevel === 'warn' || debugLevel === 'error' ? debugLevel : false);

    window.VideoRTC.server = {
        endPoints: endPoints,
        iceServers: iceServers,
        debugLevel: debugLevel
    };

    window.VideoRTC.connection = {
        status: false,
        handle: null
    };

    window.VideoRTC.getBrowser = getBrowser;

    window.VideoRTC.isMobile = isMobile;

    return {
        connect: connect,
        disconnect: disconnect,
        getBrowser: getBrowser,
        isConnected: isConnected,
        isMobile: isMobile
    };
}

/**
 * It is checked if there is any active session between Client and the VideoGateway
 * @return {Boolean} Is the client connected with the VideoGateway?
 * @example
 * var status = myVideoApp.isConnected(); // true or false
 * console.log("Status: " + status);
 */
var isConnected = function() {
    return window.VideoRTC.connection.status;
};

/**
 * A session is created between the Client and the VideoGateway
 * @return {Promise<VideoRTC>} VideoRTC methods (Success): echoTest, detect, getDevices, splitAgent, splitClient, videoRoom, videoMail, videoPlayer, videoCall
 * @example
 * var connection = myVideoApp.connect()
 *     .then(function(usecases) {
 *         // Use Cases (Classes)
 *         ...
 *     })
 *     .catch(function(cause) {
 *         console.log(cause || "Error connecting with the VideoRTC");
 *     })
 */
var connect = function() {
    return new Promise(function (resolve, reject) {
        // We connect with the VideoRTC
        Janus.init({
            debug: window.VideoRTC.server.debugLevel,
            callback: function() {
                if(Janus.isWebrtcSupported()) {
                    window.VideoRTC.connection.handle = new Janus(
                        {
                            server: window.VideoRTC.server.endPoints,
                            iceServers: window.VideoRTC.server.iceServers,
                            success: function() {
                                // The session was successfully created and is ready to be used
                                Janus.log('Connected with Video Gateway');
                                window.VideoRTC.connection.status = true;
                                resolve({
                                    detect: detect,
                                    echoTest: echoTest,
                                    getDevices: getDevices,
                                    splitAgent: splitAgent,
                                    splitClient: splitClient,
                                    videoCall: videoCall,
                                    videoRoom: videoRoom,
                                    videoMail: videoMail,
                                    videoPlayer: videoPlayer
                                });
                            },
                            error: function(cause) {
                                // The session was NOT successfully created
                                Janus.error(cause);
                                window.VideoRTC.connection.status = false;
                                reject();
                            },
                            destroyed: function() {
                                // The session was destroyed and can't be used any more
                                Janus.error('Destroyed Connection');
                                window.VideoRTC.connection.status = false;
                                reject();
                            }
                        }
                    );
                }
                else {
                reject("No WebRTC support...");
                }
            }
        });
    });
};

/**
 * The current session and all connections with the VideoGateway are closed
 * @return {nothing}
 * @example
 * myVideoApp.disconnect()
 */
var disconnect = function() {
    if(window.VideoRTC.connection && window.VideoRTC.connection.handle && window.VideoRTC.connection.handle.destroy) window.VideoRTC.connection.handle.destroy();
    window.VideoRTC.connection = {
        status: false,
        handle: null
    };
}

/**
 * Detects if the requester client is using or not a mobile device
 * @return {Boolean} Is the client using a Mobile device?
 * @example
 * var isMobile = myVideoApp.isMobile(); // true or false
 * console.log("Mobile device?: " + isMobile);
 */
var isMobile = function() {
    return /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);
};

/**
 * Get the browser name used by the client
 * @return {string} Browser name: "chrome", "firefox", "safari", "opera" or "IE"
 * @example
 * var browserName = myVideoApp.getBrowser(); // "chrome", "firefox"...
 * console.log("Browser: " + browserName);
 */
var getBrowser = function() {
    //IE
    if (navigator.appName == "Microsoft Internet Explorer") {
        return "IE";
    }
    //Chrome
    else if ((navigator.userAgent.toLowerCase().indexOf('chrome') > -1) && (navigator.userAgent.toLowerCase().indexOf('safari') > -1) && (navigator.appName == "Netscape")) {
        return "chrome";
    }
    //Firefox
    else if ((navigator.userAgent.toLowerCase().indexOf('firefox') > -1) && (navigator.appName == "Netscape")) {
        return "firefox";
    }
    //Safari
    else if ((navigator.userAgent.toLowerCase().indexOf('safari') > -1) && !(navigator.userAgent.toLowerCase().indexOf('chrome') > -1) && (navigator.appName == "Netscape")) {
        return "safari";
    }
    //Opera
    else if (navigator.appName == "Opera") {
        return "opera";
    } else {
        return "chrome";
    }
};

module.exports = VideoRTC;