/**
* @license OpenTok JavaScript Library v2.2.9.1
* http://www.tokbox.com/
*
* Copyright (c) 2014 TokBox, Inc.
* Released under the MIT license
* http://opensource.org/licenses/MIT
*
* Date: September 08 10:17:05 2014
*/
(function(window) {
if (!window.OT) window.OT = {};
OT.properties = {
version: 'v2.2.9.1', // The current version (eg. v2.0.4) (This is replaced by gradle)
build: '72b534e', // The current build hash (This is replaced by gradle)
// Whether or not to turn on debug logging by default
debug: 'false',
// The URL of the tokbox website
websiteURL: 'http://www.tokbox.com',
// The URL of the CDN
cdnURL: 'http://static.opentok.com',
// The URL to use for logging
loggingURL: 'http://hlg.tokbox.com/prod',
// The anvil API URL
apiURL: 'http://anvil.opentok.com',
// What protocol to use when connecting to the rumor web socket
messagingProtocol: 'wss',
// What port to use when connection to the rumor web socket
messagingPort: 443,
// If this environment supports SSL
supportSSL: 'true',
// The CDN to use if we're using SSL
cdnURLSSL: 'https://static.opentok.com',
// The URL to use for logging
loggingURLSSL: 'https://hlg.tokbox.com/prod',
// The anvil API URL to use if we're using SSL
apiURLSSL: 'https://anvil.opentok.com',
minimumVersion: {
firefox: parseFloat('29'),
chrome: parseFloat('34')
}
};
})(window);
/**
* @license Common JS Helpers on OpenTok 0.2.0 3fa583f master
* http://www.tokbox.com/
*
* Copyright (c) 2014 TokBox, Inc.
* Released under the MIT license
* http://opensource.org/licenses/MIT
*
* Date: August 08 12:31:42 2014
*
*/
// OT Helper Methods
//
// helpers.js <- the root file
// helpers/lib/{helper topic}.js <- specialised helpers for specific tasks/topics
// (i.e. video, dom, etc)
//
// @example Getting a DOM element by it's id
// var element = OTHelpers('domId');
//
//
/*jshint browser:true, smarttabs:true*/
!(function(window, undefined) {
var OTHelpers = function(domId) {
return document.getElementById(domId);
};
var previousOTHelpers = window.OTHelpers;
window.OTHelpers = OTHelpers;
// A guard to detect when IE has performed cleans on unload
window.___othelpers = true;
OTHelpers.keys = Object.keys || function(object) {
var keys = [], hasOwnProperty = Object.prototype.hasOwnProperty;
for(var key in object) {
if(hasOwnProperty.call(object, key)) {
keys.push(key);
}
}
return keys;
};
var _each = Array.prototype.forEach || function(iter, ctx) {
for(var idx = 0, count = this.length || 0; idx < count; ++idx) {
if(idx in this) {
iter.call(ctx, this[idx], idx);
}
}
};
OTHelpers.forEach = function(array, iter, ctx) {
return _each.call(array, iter, ctx);
};
var _map = Array.prototype.map || function(iter, ctx) {
var collect = [];
_each.call(this, function(item, idx) {
collect.push(iter.call(ctx, item, idx));
});
return collect;
};
OTHelpers.map = function(array, iter) {
return _map.call(array, iter);
};
var _filter = Array.prototype.filter || function(iter, ctx) {
var collect = [];
_each.call(this, function(item, idx) {
if(iter.call(ctx, item, idx)) {
collect.push(item);
}
});
return collect;
};
OTHelpers.filter = function(array, iter, ctx) {
return _filter.call(array, iter, ctx);
};
var _some = Array.prototype.some || function(iter, ctx) {
var any = false;
for(var idx = 0, count = this.length || 0; idx < count; ++idx) {
if(idx in this) {
if(iter.call(ctx, this[idx], idx)) {
any = true;
break;
}
}
}
return any;
};
OTHelpers.some = function(array, iter, ctx) {
return _some.call(array, iter, ctx);
};
var _indexOf = Array.prototype.indexOf || function(searchElement, fromIndex) {
var i,
pivot = (fromIndex) ? fromIndex : 0,
length;
if (!this) {
throw new TypeError();
}
length = this.length;
if (length === 0 || pivot >= length) {
return -1;
}
if (pivot < 0) {
pivot = length - Math.abs(pivot);
}
for (i = pivot; i < length; i++) {
if (this[i] === searchElement) {
return i;
}
}
return -1;
};
OTHelpers.arrayIndexOf = function(array, searchElement, fromIndex) {
return _indexOf.call(array, searchElement, fromIndex);
};
var _bind = Function.prototype.bind || function() {
var args = Array.prototype.slice.call(arguments),
ctx = args.shift(),
fn = this;
return function() {
return fn.apply(ctx, args.concat(Array.prototype.slice.call(arguments)));
};
};
OTHelpers.bind = function() {
var args = Array.prototype.slice.call(arguments),
fn = args.shift();
return _bind.apply(fn, args);
};
var _trim = String.prototype.trim || function() {
return this.replace(/^\s+|\s+$/g, '');
};
OTHelpers.trim = function(str) {
return _trim.call(str);
};
OTHelpers.noConflict = function() {
OTHelpers.noConflict = function() {
return OTHelpers;
};
window.OTHelpers = previousOTHelpers;
return OTHelpers;
};
OTHelpers.isNone = function(obj) {
return obj === undefined || obj === null;
};
OTHelpers.isObject = function(obj) {
return obj === Object(obj);
};
OTHelpers.isFunction = function(obj) {
return !!obj && (obj.toString().indexOf('()') !== -1 ||
Object.prototype.toString.call(obj) === '[object Function]');
};
OTHelpers.isArray = OTHelpers.isFunction(Array.isArray) && Array.isArray ||
function (vArg) {
return Object.prototype.toString.call(vArg) === '[object Array]';
};
OTHelpers.isEmpty = function(obj) {
if (obj === null || obj === undefined) return true;
if (OTHelpers.isArray(obj) || typeof(obj) === 'string') return obj.length === 0;
// Objects without enumerable owned properties are empty.
for (var key in obj) {
if (obj.hasOwnProperty(key)) return false;
}
return true;
};
// Extend a target object with the properties from one or
// more source objects
//
// @example:
// dest = OTHelpers.extend(dest, source1, source2, source3);
//
OTHelpers.extend = function(/* dest, source1[, source2, ..., , sourceN]*/) {
var sources = Array.prototype.slice.call(arguments),
dest = sources.shift();
OTHelpers.forEach(sources, function(source) {
for (var key in source) {
dest[key] = source[key];
}
});
return dest;
};
// Ensures that the target object contains certain defaults.
//
// @example
// var options = OTHelpers.defaults(options, {
// loading: true // loading by default
// });
//
OTHelpers.defaults = function(/* dest, defaults1[, defaults2, ..., , defaultsN]*/) {
var sources = Array.prototype.slice.call(arguments),
dest = sources.shift();
OTHelpers.forEach(sources, function(source) {
for (var key in source) {
if (dest[key] === void 0) dest[key] = source[key];
}
});
return dest;
};
OTHelpers.clone = function(obj) {
if (!OTHelpers.isObject(obj)) return obj;
return OTHelpers.isArray(obj) ? obj.slice() : OTHelpers.extend({}, obj);
};
// Handy do nothing function
OTHelpers.noop = function() {};
// Returns the number of millisceonds since the the UNIX epoch, this is functionally
// equivalent to executing new Date().getTime().
//
// Where available, we use 'performance.now' which is more accurate and reliable,
// otherwise we default to new Date().getTime().
OTHelpers.now = (function() {
var performance = window.performance || {},
navigationStart,
now = performance.now ||
performance.mozNow ||
performance.msNow ||
performance.oNow ||
performance.webkitNow;
if (now) {
now = OTHelpers.bind(now, performance);
navigationStart = performance.timing.navigationStart;
return function() { return navigationStart + now(); };
} else {
return function() { return new Date().getTime(); };
}
})();
var _browser = function() {
var userAgent = window.navigator.userAgent.toLowerCase(),
appName = window.navigator.appName,
navigatorVendor,
browser = 'unknown',
version = -1;
if (userAgent.indexOf('opera') > -1 || userAgent.indexOf('opr') > -1) {
browser = 'Opera';
if (/opr\/([0-9]{1,}[\.0-9]{0,})/.exec(userAgent) !== null) {
version = parseFloat( RegExp.$1 );
}
} else if (userAgent.indexOf('firefox') > -1) {
browser = 'Firefox';
if (/firefox\/([0-9]{1,}[\.0-9]{0,})/.exec(userAgent) !== null) {
version = parseFloat( RegExp.$1 );
}
} else if (appName === 'Microsoft Internet Explorer') {
// IE 10 and below
browser = 'IE';
if (/msie ([0-9]{1,}[\.0-9]{0,})/.exec(userAgent) !== null) {
version = parseFloat( RegExp.$1 );
}
} else if (appName === 'Netscape' && userAgent.indexOf('trident') > -1) {
// IE 11+
browser = 'IE';
if (/trident\/.*rv:([0-9]{1,}[\.0-9]{0,})/.exec(userAgent) !== null) {
version = parseFloat( RegExp.$1 );
}
} else if (userAgent.indexOf('chrome') > -1) {
browser = 'Chrome';
if (/chrome\/([0-9]{1,}[\.0-9]{0,})/.exec(userAgent) !== null) {
version = parseFloat( RegExp.$1 );
}
} else if ((navigatorVendor = window.navigator.vendor) &&
navigatorVendor.toLowerCase().indexOf('apple') > -1) {
browser = 'Safari';
if (/version\/([0-9]{1,}[\.0-9]{0,})/.exec(userAgent) !== null) {
version = parseFloat( RegExp.$1 );
}
}
return {
browser: browser,
version: version,
iframeNeedsLoad: userAgent.indexOf('webkit') < 0
};
}();
OTHelpers.browser = function() {
return _browser.browser;
};
OTHelpers.browserVersion = function() {
return _browser;
};
OTHelpers.canDefineProperty = true;
try {
Object.defineProperty({}, 'x', {});
} catch (err) {
OTHelpers.canDefineProperty = false;
}
// A helper for defining a number of getters at once.
//
// @example: from inside an object
// OTHelpers.defineGetters(this, {
// apiKey: function() { return _apiKey; },
// token: function() { return _token; },
// connected: function() { return this.is('connected'); },
// capabilities: function() { return _socket.capabilities; },
// sessionId: function() { return _sessionId; },
// id: function() { return _sessionId; }
// });
//
OTHelpers.defineGetters = function(self, getters, enumerable) {
var propsDefinition = {};
if (enumerable === void 0) enumerable = false;
for (var key in getters) {
propsDefinition[key] = {
get: getters[key],
enumerable: enumerable
};
}
OTHelpers.defineProperties(self, propsDefinition);
};
var generatePropertyFunction = function(object, getter, setter) {
if(getter && !setter) {
return function() {
return getter.call(object);
};
} else if(getter && setter) {
return function(value) {
if(value !== void 0) {
setter.call(object, value);
}
return getter.call(object);
};
} else {
return function(value) {
if(value !== void 0) {
setter.call(object, value);
}
};
}
};
OTHelpers.defineProperties = function(object, getterSetters) {
for (var key in getterSetters) {
object[key] = generatePropertyFunction(object, getterSetters[key].get,
getterSetters[key].set);
}
};
// Polyfill Object.create for IE8
//
// See https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Object/create
//
if (!Object.create) {
Object.create = function (o) {
if (arguments.length > 1) {
throw new Error('Object.create implementation only accepts the first parameter.');
}
function F() {}
F.prototype = o;
return new F();
};
}
OTHelpers.setCookie = function(key, value) {
try {
localStorage.setItem(key, value);
} catch (err) {
// Store in browser cookie
var date = new Date();
date.setTime(date.getTime()+(365*24*60*60*1000));
var expires = '; expires=' + date.toGMTString();
document.cookie = key + '=' + value + expires + '; path=/';
}
};
OTHelpers.getCookie = function(key) {
var value;
try {
value = localStorage.getItem('opentok_client_id');
return value;
} catch (err) {
// Check browser cookies
var nameEQ = key + '=';
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0) === ' ') {
c = c.substring(1,c.length);
}
if (c.indexOf(nameEQ) === 0) {
value = c.substring(nameEQ.length,c.length);
}
}
if (value) {
return value;
}
}
return null;
};
// These next bits are included from Underscore.js. The original copyright
// notice is below.
//
// http://underscorejs.org
// (c) 2009-2011 Jeremy Ashkenas, DocumentCloud Inc.
// (c) 2011-2013 Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors
// Underscore may be freely distributed under the MIT license.
// Invert the keys and values of an object. The values must be serializable.
OTHelpers.invert = function(obj) {
var result = {};
for (var key in obj) if (obj.hasOwnProperty(key)) result[obj[key]] = key;
return result;
};
// List of HTML entities for escaping.
var entityMap = {
escape: {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
'\'': ''',
'/': '/'
}
};
entityMap.unescape = OTHelpers.invert(entityMap.escape);
// Regexes containing the keys and values listed immediately above.
var entityRegexes = {
escape: new RegExp('[' + OTHelpers.keys(entityMap.escape).join('') + ']', 'g'),
unescape: new RegExp('(' + OTHelpers.keys(entityMap.unescape).join('|') + ')', 'g')
};
// Functions for escaping and unescaping strings to/from HTML interpolation.
OTHelpers.forEach(['escape', 'unescape'], function(method) {
OTHelpers[method] = function(string) {
if (string === null || string === undefined) return '';
return ('' + string).replace(entityRegexes[method], function(match) {
return entityMap[method][match];
});
};
});
// By default, Underscore uses ERB-style template delimiters, change the
// following template settings to use alternative delimiters.
OTHelpers.templateSettings = {
evaluate : /<%([\s\S]+?)%>/g,
interpolate : /<%=([\s\S]+?)%>/g,
escape : /<%-([\s\S]+?)%>/g
};
// When customizing `templateSettings`, if you don't want to define an
// interpolation, evaluation or escaping regex, we need one that is
// guaranteed not to match.
var noMatch = /(.)^/;
// Certain characters need to be escaped so that they can be put into a
// string literal.
var escapes = {
'\'': '\'',
'\\': '\\',
'\r': 'r',
'\n': 'n',
'\t': 't',
'\u2028': 'u2028',
'\u2029': 'u2029'
};
var escaper = /\\|'|\r|\n|\t|\u2028|\u2029/g;
// JavaScript micro-templating, similar to John Resig's implementation.
// Underscore templating handles arbitrary delimiters, preserves whitespace,
// and correctly escapes quotes within interpolated code.
OTHelpers.template = function(text, data, settings) {
var render;
settings = OTHelpers.defaults({}, settings, OTHelpers.templateSettings);
// Combine delimiters into one regular expression via alternation.
var matcher = new RegExp([
(settings.escape || noMatch).source,
(settings.interpolate || noMatch).source,
(settings.evaluate || noMatch).source
].join('|') + '|$', 'g');
// Compile the template source, escaping string literals appropriately.
var index = 0;
var source = '__p+=\'';
text.replace(matcher, function(match, escape, interpolate, evaluate, offset) {
source += text.slice(index, offset)
.replace(escaper, function(match) { return '\\' + escapes[match]; });
if (escape) {
source += '\'+\n((__t=(' + escape + '))==null?\'\':OTHelpers.escape(__t))+\n\'';
}
if (interpolate) {
source += '\'+\n((__t=(' + interpolate + '))==null?\'\':__t)+\n\'';
}
if (evaluate) {
source += '\';\n' + evaluate + '\n__p+=\'';
}
index = offset + match.length;
return match;
});
source += '\';\n';
// If a variable is not specified, place data values in local scope.
if (!settings.variable) source = 'with(obj||{}){\n' + source + '}\n';
source = 'var __t,__p=\'\',__j=Array.prototype.join,' +
'print=function(){__p+=__j.call(arguments,\'\');};\n' +
source + 'return __p;\n';
try {
// evil is necessary for the new Function line
/*jshint evil:true */
render = new Function(settings.variable || 'obj', source);
} catch (e) {
e.source = source;
throw e;
}
if (data) return render(data);
var template = function(data) {
return render.call(this, data);
};
// Provide the compiled function source as a convenience for precompilation.
template.source = 'function(' + (settings.variable || 'obj') + '){\n' + source + '}';
return template;
};
})(window);
/*jshint browser:true, smarttabs:true*/
// tb_require('../../helpers.js')
(function(window, OTHelpers, undefined) {
OTHelpers.statable = function(self, possibleStates, initialState, stateChanged,
stateChangedFailed) {
var previousState,
currentState = self.currentState = initialState;
var setState = function(state) {
if (currentState !== state) {
if (OTHelpers.arrayIndexOf(possibleStates, state) === -1) {
if (stateChangedFailed && OTHelpers.isFunction(stateChangedFailed)) {
stateChangedFailed('invalidState', state);
}
return;
}
self.previousState = previousState = currentState;
self.currentState = currentState = state;
if (stateChanged && OTHelpers.isFunction(stateChanged)) stateChanged(state, previousState);
}
};
// Returns a number of states and returns true if the current state
// is any of them.
//
// @example
// if (this.is('connecting', 'connected')) {
// // do some stuff
// }
//
self.is = function (/* state0:String, state1:String, ..., stateN:String */) {
return OTHelpers.arrayIndexOf(arguments, currentState) !== -1;
};
// Returns a number of states and returns true if the current state
// is none of them.
//
// @example
// if (this.isNot('connecting', 'connected')) {
// // do some stuff
// }
//
self.isNot = function (/* state0:String, state1:String, ..., stateN:String */) {
return OTHelpers.arrayIndexOf(arguments, currentState) === -1;
};
return setState;
};
})(window, window.OTHelpers);
/*!
* This is a modified version of Robert Kieffer awesome uuid.js library.
* The only modifications we've made are to remove the Node.js specific
* parts of the code and the UUID version 1 generator (which we don't
* use). The original copyright notice is below.
*
* node-uuid/uuid.js
*
* Copyright (c) 2010 Robert Kieffer
* Dual licensed under the MIT and GPL licenses.
* Documentation and details at https://github.com/broofa/node-uuid
*/
// tb_require('../helpers.js')
/*global crypto:true, Uint32Array:true, Buffer:true */
/*jshint browser:true, smarttabs:true*/
(function(window, OTHelpers, undefined) {
// Unique ID creation requires a high quality random # generator, but
// Math.random() does not guarantee "cryptographic quality". So we feature
// detect for more robust APIs, normalizing each method to return 128-bits
// (16 bytes) of random data.
var mathRNG, whatwgRNG;
// Math.random()-based RNG. All platforms, very fast, unknown quality
var _rndBytes = new Array(16);
mathRNG = function() {
var r, b = _rndBytes, i = 0;
for (i = 0; i < 16; i++) {
if ((i & 0x03) === 0) r = Math.random() * 0x100000000;
b[i] = r >>> ((i & 0x03) << 3) & 0xff;
}
return b;
};
// WHATWG crypto-based RNG - http://wiki.whatwg.org/wiki/Crypto
// WebKit only (currently), moderately fast, high quality
if (window.crypto && crypto.getRandomValues) {
var _rnds = new Uint32Array(4);
whatwgRNG = function() {
crypto.getRandomValues(_rnds);
for (var c = 0 ; c < 16; c++) {
_rndBytes[c] = _rnds[c >> 2] >>> ((c & 0x03) * 8) & 0xff;
}
return _rndBytes;
};
}
// Select RNG with best quality
var _rng = whatwgRNG || mathRNG;
// Buffer class to use
var BufferClass = typeof(Buffer) === 'function' ? Buffer : Array;
// Maps for number <-> hex string conversion
var _byteToHex = [];
var _hexToByte = {};
for (var i = 0; i < 256; i++) {
_byteToHex[i] = (i + 0x100).toString(16).substr(1);
_hexToByte[_byteToHex[i]] = i;
}
// **`parse()` - Parse a UUID into it's component bytes**
function parse(s, buf, offset) {
var i = (buf && offset) || 0, ii = 0;
buf = buf || [];
s.toLowerCase().replace(/[0-9a-f]{2}/g, function(oct) {
if (ii < 16) { // Don't overflow!
buf[i + ii++] = _hexToByte[oct];
}
});
// Zero out remaining bytes if string was short
while (ii < 16) {
buf[i + ii++] = 0;
}
return buf;
}
// **`unparse()` - Convert UUID byte array (ala parse()) into a string**
function unparse(buf, offset) {
var i = offset || 0, bth = _byteToHex;
return bth[buf[i++]] + bth[buf[i++]] +
bth[buf[i++]] + bth[buf[i++]] + '-' +
bth[buf[i++]] + bth[buf[i++]] + '-' +
bth[buf[i++]] + bth[buf[i++]] + '-' +
bth[buf[i++]] + bth[buf[i++]] + '-' +
bth[buf[i++]] + bth[buf[i++]] +
bth[buf[i++]] + bth[buf[i++]] +
bth[buf[i++]] + bth[buf[i++]];
}
// **`v4()` - Generate random UUID**
// See https://github.com/broofa/node-uuid for API details
function v4(options, buf, offset) {
// Deprecated - 'format' argument, as supported in v1.2
var i = buf && offset || 0;
if (typeof(options) === 'string') {
buf = options === 'binary' ? new BufferClass(16) : null;
options = null;
}
options = options || {};
var rnds = options.random || (options.rng || _rng)();
// Per 4.4, set bits for version and `clock_seq_hi_and_reserved`
rnds[6] = (rnds[6] & 0x0f) | 0x40;
rnds[8] = (rnds[8] & 0x3f) | 0x80;
// Copy bytes to buffer, if provided
if (buf) {
for (var ii = 0; ii < 16; ii++) {
buf[i + ii] = rnds[ii];
}
}
return buf || unparse(rnds);
}
// Export public API
var uuid = v4;
uuid.v4 = v4;
uuid.parse = parse;
uuid.unparse = unparse;
uuid.BufferClass = BufferClass;
// Export RNG options
uuid.mathRNG = mathRNG;
uuid.whatwgRNG = whatwgRNG;
OTHelpers.uuid = uuid;
}(window, window.OTHelpers));
/*jshint browser:true, smarttabs:true*/
// tb_require('../helpers.js')
(function(window, OTHelpers, undefined) {
OTHelpers.useLogHelpers = function(on){
// Log levels for OTLog.setLogLevel
on.DEBUG = 5;
on.LOG = 4;
on.INFO = 3;
on.WARN = 2;
on.ERROR = 1;
on.NONE = 0;
var _logLevel = on.NONE,
_logs = [],
_canApplyConsole = true;
try {
Function.prototype.bind.call(window.console.log, window.console);
} catch (err) {
_canApplyConsole = false;
}
// Some objects can't be logged in the console, mostly these are certain
// types of native objects that are exposed to JS. This is only really a
// problem with IE, hence only the IE version does anything.
var makeLogArgumentsSafe = function(args) { return args; };
if (OTHelpers.browser() === 'IE') {
makeLogArgumentsSafe = function(args) {
return [toDebugString(Array.prototype.slice.apply(args))];
};
}
// Generates a logging method for a particular method and log level.
//
// Attempts to handle the following cases:
// * the desired log method doesn't exist, call fallback (if available) instead
// * the console functionality isn't available because the developer tools (in IE)
// aren't open, call fallback (if available)
// * attempt to deal with weird IE hosted logging methods as best we can.
//
function generateLoggingMethod(method, level, fallback) {
return function() {
if (on.shouldLog(level)) {
var cons = window.console,
args = makeLogArgumentsSafe(arguments);
// In IE, window.console may not exist if the developer tools aren't open
// This also means that cons and cons[method] can appear at any moment
// hence why we retest this every time.
if (cons && cons[method]) {
// the desired console method isn't a real object, which means
// that we can't use apply on it. We force it to be a real object
// using Function.bind, assuming that's available.
if (cons[method].apply || _canApplyConsole) {
if (!cons[method].apply) {
cons[method] = Function.prototype.bind.call(cons[method], cons);
}
cons[method].apply(cons, args);
}
else {
// This isn't the same result as the above, but it's better
// than nothing.
cons[method](args);
}
}
else if (fallback) {
fallback.apply(on, args);
// Skip appendToLogs, we delegate entirely to the fallback
return;
}
appendToLogs(method, makeLogArgumentsSafe(arguments));
}
};
}
on.log = generateLoggingMethod('log', on.LOG);
// Generate debug, info, warn, and error logging methods, these all fallback to on.log
on.debug = generateLoggingMethod('debug', on.DEBUG, on.log);
on.info = generateLoggingMethod('info', on.INFO, on.log);
on.warn = generateLoggingMethod('warn', on.WARN, on.log);
on.error = generateLoggingMethod('error', on.ERROR, on.log);
on.setLogLevel = function(level) {
_logLevel = typeof(level) === 'number' ? level : 0;
on.debug('TB.setLogLevel(' + _logLevel + ')');
return _logLevel;
};
on.getLogs = function() {
return _logs;
};
// Determine if the level is visible given the current logLevel.
on.shouldLog = function(level) {
return _logLevel >= level;
};
// Format the current time nicely for logging. Returns the current
// local time.
function formatDateStamp() {
var now = new Date();
return now.toLocaleTimeString() + now.getMilliseconds();
}
function toJson(object) {
try {
return JSON.stringify(object);
} catch(e) {
return object.toString();
}
}
function toDebugString(object) {
var components = [];
if (typeof(object) === 'undefined') {
// noop
}
else if (object === null) {
components.push('NULL');
}
else if (OTHelpers.isArray(object)) {
for (var i=0; i
* The following code adds an event handler for one event:
* If you pass in multiple event names and a handler method, the handler is
* registered for each of those events: You can also pass in a third
* The method also supports an alternate syntax, in which the first parameter is an object
* that is a hash map of event names and handler functions and the second parameter (optional)
* is the context for this in each handler:
*
* If you do not add a handler for an event, the event is ignored locally.
* If you pass in one event name and a handler method, the handler is removed for that
* event: If you pass in multiple event names and a handler method, the handler is removed for
* those events: If you pass in an event name (or names) and no handler method, all handlers are
* removed for those events: If you pass in no arguments, all event handlers are removed for all events
* dispatched by the object:
* The method also supports an alternate syntax, in which the first parameter is an object that
* is a hash map of event names and handler functions and the second parameter (optional) is
* the context for this in each handler:
*
* The following code adds a one-time event handler for the If you pass in multiple event names and a handler method, the handler is registered
* for each of those events: You can also pass in a third
* The method also supports an alternate syntax, in which the first parameter is an object that
* is a hash map of event names and handler functions and the second parameter (optional) is the
* context for this in each handler:
*
* This method registers a method as an event listener for a specific event.
*
*
*
* If a handler is not registered for an event, the event is ignored locally. If the
* event listener function does not exist, the event is ignored locally.
*
* Throws an exception if the
* Removes an event listener for a specific event.
*
*
*
* Throws an exception if the
* Calling
* The OpenTok JavaScript library displays log messages in the debugger console (such as
* Firebug), if one exists.
*
* The following example logs the session ID to the console, by calling
* on, once, and off
* methods of objects that can dispatch events.
*
* @class EventDispatcher
*/
OTHelpers.eventing = function(self, syncronous) {
var _events = {};
// Call the defaultAction, passing args
function executeDefaultAction(defaultAction, args) {
if (!defaultAction) return;
defaultAction.apply(null, args.slice());
}
// Execute each handler in +listeners+ with +args+.
//
// Each handler will be executed async. On completion the defaultAction
// handler will be executed with the args.
//
// @param [Array] listeners
// An array of functions to execute. Each will be passed args.
//
// @param [Array] args
// An array of arguments to execute each function in +listeners+ with.
//
// @param [String] name
// The name of this event.
//
// @param [Function, Null, Undefined] defaultAction
// An optional function to execute after every other handler. This will execute even
// if +listeners+ is empty. +defaultAction+ will be passed args as a normal
// handler would.
//
// @return Undefined
//
function executeListenersAsyncronously(name, args, defaultAction) {
var listeners = _events[name];
if (!listeners || listeners.length === 0) return;
var listenerAcks = listeners.length;
OTHelpers.forEach(listeners, function(listener) { // , index
function filterHandlerAndContext(_listener) {
return _listener.context === listener.context && _listener.handler === listener.handler;
}
// We run this asynchronously so that it doesn't interfere with execution if an
// error happens
OTHelpers.callAsync(function() {
try {
// have to check if the listener has not been removed
if (_events[name] && OTHelpers.some(_events[name], filterHandlerAndContext)) {
(listener.closure || listener.handler).apply(listener.context || null, args);
}
}
finally {
listenerAcks--;
if (listenerAcks === 0) {
executeDefaultAction(defaultAction, args);
}
}
});
});
}
// This is identical to executeListenersAsyncronously except that handlers will
// be executed syncronously.
//
// On completion the defaultAction handler will be executed with the args.
//
// @param [Array] listeners
// An array of functions to execute. Each will be passed args.
//
// @param [Array] args
// An array of arguments to execute each function in +listeners+ with.
//
// @param [String] name
// The name of this event.
//
// @param [Function, Null, Undefined] defaultAction
// An optional function to execute after every other handler. This will execute even
// if +listeners+ is empty. +defaultAction+ will be passed args as a normal
// handler would.
//
// @return Undefined
//
function executeListenersSyncronously(name, args) { // defaultAction is not used
var listeners = _events[name];
if (!listeners || listeners.length === 0) return;
OTHelpers.forEach(listeners, function(listener) { // index
(listener.closure || listener.handler).apply(listener.context || null, args);
});
}
var executeListeners = syncronous === true ?
executeListenersSyncronously : executeListenersAsyncronously;
var removeAllListenersNamed = function (eventName, context) {
if (_events[eventName]) {
if (context) {
// We are removing by context, get only events that don't
// match that context
_events[eventName] = OTHelpers.filter(_events[eventName], function(listener){
return listener.context !== context;
});
}
else {
delete _events[eventName];
}
}
};
var addListeners = OTHelpers.bind(function (eventNames, handler, context, closure) {
var listener = {handler: handler};
if (context) listener.context = context;
if (closure) listener.closure = closure;
OTHelpers.forEach(eventNames, function(name) {
if (!_events[name]) _events[name] = [];
_events[name].push(listener);
var addedListener = name + ':added';
if (_events[addedListener]) {
executeListeners(addedListener, [_events[name].length]);
}
});
}, self);
var removeListeners = function (eventNames, handler, context) {
function filterHandlerAndContext(listener) {
return !(listener.handler === handler && listener.context === context);
}
OTHelpers.forEach(eventNames, OTHelpers.bind(function(name) {
if (_events[name]) {
_events[name] = OTHelpers.filter(_events[name], filterHandlerAndContext);
if (_events[name].length === 0) delete _events[name];
var removedListener = name + ':removed';
if (_events[ removedListener]) {
executeListeners(removedListener, [_events[name] ? _events[name].length : 0]);
}
}
}, self));
};
// Execute any listeners bound to the +event+ Event.
//
// Each handler will be executed async. On completion the defaultAction
// handler will be executed with the args.
//
// @param [Event] event
// An Event object.
//
// @param [Function, Null, Undefined] defaultAction
// An optional function to execute after every other handler. This will execute even
// if there are listeners bound to this event. +defaultAction+ will be passed
// args as a normal handler would.
//
// @return this
//
self.dispatchEvent = function(event, defaultAction) {
if (!event.type) {
OTHelpers.error('OTHelpers.Eventing.dispatchEvent: Event has no type');
OTHelpers.error(event);
throw new Error('OTHelpers.Eventing.dispatchEvent: Event has no type');
}
if (!event.target) {
event.target = this;
}
if (!_events[event.type] || _events[event.type].length === 0) {
executeDefaultAction(defaultAction, [event]);
return;
}
executeListeners(event.type, [event], defaultAction);
return this;
};
// Execute each handler for the event called +name+.
//
// Each handler will be executed async, and any exceptions that they throw will
// be caught and logged
//
// How to pass these?
// * defaultAction
//
// @example
// foo.on('bar', function(name, message) {
// alert("Hello " + name + ": " + message);
// });
//
// foo.trigger('OpenTok', 'asdf'); // -> Hello OpenTok: asdf
//
//
// @param [String] eventName
// The name of this event.
//
// @param [Array] arguments
// Any additional arguments beyond +eventName+ will be passed to the handlers.
//
// @return this
//
self.trigger = function(eventName) {
if (!_events[eventName] || _events[eventName].length === 0) {
return;
}
var args = Array.prototype.slice.call(arguments);
// Remove the eventName arg
args.shift();
executeListeners(eventName, args);
return this;
};
/**
* Adds an event handler function for one or more events.
*
*
* obj.on("eventName", function (event) {
* // This is the event handler.
* });
*
*
*
* obj.on("eventName1 eventName2",
* function (event) {
* // This is the event handler.
* });
*
*
* context parameter (which is optional) to
* define the value of this in the handler method:obj.on("eventName",
* function (event) {
* // This is the event handler.
* },
* obj);
*
*
*
* obj.on(
* {
* eventName1: function (event) {
* // This is the handler for eventName1.
* },
* eventName2: function (event) {
* // This is the handler for eventName2.
* }
* },
* obj);
*
*
* this in the event
* handler function.
*
* @returns {EventDispatcher} The EventDispatcher object.
*
* @memberOf EventDispatcher
* @method #on
* @see off()
* @see once()
* @see Events
*/
self.on = function(eventNames, handlerOrContext, context) {
if (typeof(eventNames) === 'string' && handlerOrContext) {
addListeners(eventNames.split(' '), handlerOrContext, context);
}
else {
for (var name in eventNames) {
if (eventNames.hasOwnProperty(name)) {
addListeners([name], eventNames[name], handlerOrContext);
}
}
}
return this;
};
/**
* Removes an event handler or handlers.
*
* obj.off("eventName", eventHandler);
*
* obj.off("eventName1 eventName2", eventHandler);
*
* obj.off("event1Name event2Name");
*
* obj.off();
*
*
* obj.off(
* {
* eventName1: event1Handler,
* eventName2: event2Handler
* });
*
*
* @param {String} type (Optional) The string identifying the type of event. You can
* use a space to specify multiple events, as in "accessAllowed accessDenied
* accessDialogClosed". If you pass in no type value (or other arguments),
* all event handlers are removed for the object.
* @param {Function} handler (Optional) The event handler function to remove. The handler
* must be the same function object as was passed into on(). Be careful with
* helpers like bind() that return a new function when called. If you pass in
* no handler, all event handlers are removed for the specified event
* type.
* @param {Object} context (Optional) If you specify a context, the event handler
* is removed for all specified events and handlers that use the specified context. (The
* context must match the context passed into on().)
*
* @returns {Object} The object that dispatched the event.
*
* @memberOf EventDispatcher
* @method #off
* @see on()
* @see once()
* @see Events
*/
self.off = function(eventNames, handlerOrContext, context) {
if (typeof eventNames === 'string') {
if (handlerOrContext && OTHelpers.isFunction(handlerOrContext)) {
removeListeners(eventNames.split(' '), handlerOrContext, context);
}
else {
OTHelpers.forEach(eventNames.split(' '), function(name) {
removeAllListenersNamed(name, handlerOrContext);
}, this);
}
} else if (!eventNames) {
// remove all bound events
_events = {};
} else {
for (var name in eventNames) {
if (eventNames.hasOwnProperty(name)) {
removeListeners([name], eventNames[name], handlerOrContext);
}
}
}
return this;
};
/**
* Adds an event handler function for one or more events. Once the handler is called,
* the specified handler method is removed as a handler for this event. (When you use
* the on() method to add an event handler, the handler is not
* removed when it is called.) The once() method is the equivilent of
* calling the on()
* method and calling off() the first time the handler is invoked.
*
* accessAllowed event:
*
* obj.once("eventName", function (event) {
* // This is the event handler.
* });
*
*
* obj.once("eventName1 eventName2"
* function (event) {
* // This is the event handler.
* });
*
*
* context parameter (which is optional) to define
* the value of
* this in the handler method:obj.once("eventName",
* function (event) {
* // This is the event handler.
* },
* obj);
*
*
*
* obj.once(
* {
* eventName1: function (event) {
* // This is the event handler for eventName1.
* },
* eventName2: function (event) {
* // This is the event handler for eventName1.
* }
* },
* obj);
*
*
* @param {String} type The string identifying the type of event. You can specify multiple
* event names in this string, separating them with a space. The event handler will process
* the first occurence of the events. After the first event, the handler is removed (for
* all specified events).
* @param {Function} handler The handler function to process the event. This function takes
* the event object as a parameter.
* @param {Object} context (Optional) Defines the value of this in the event
* handler function.
*
* @returns {Object} The object that dispatched the event.
*
* @memberOf EventDispatcher
* @method #once
* @see on()
* @see off()
* @see Events
*/
self.once = function(eventNames, handler, context) {
var names = eventNames.split(' '),
fun = OTHelpers.bind(function() {
var result = handler.apply(context || null, arguments);
removeListeners(names, handler, context);
return result;
}, this);
addListeners(names, handler, context, fun);
return this;
};
/**
* Deprecated; use on() or once() instead.
* listener name is invalid.
* this in the event
* handler function.
*
* @memberOf EventDispatcher
* @method #addEventListener
* @see on()
* @see once()
* @see Events
*/
// See 'on' for usage.
// @depreciated will become a private helper function in the future.
self.addEventListener = function(eventName, handler, context) {
OTHelpers.warn('The addEventListener() method is deprecated. Use on() or once() instead.');
addListeners([eventName], handler, context);
};
/**
* Deprecated; use on() or once() instead.
* listener name is invalid.
* context, the event
* handler is removed for all specified events and event listeners that use the specified
context. (The context must match the context passed into
* addEventListener().)
*
* @memberOf EventDispatcher
* @method #removeEventListener
* @see off()
* @see Events
*/
// See 'off' for usage.
// @depreciated will become a private helper function in the future.
self.removeEventListener = function(eventName, handler, context) {
OTHelpers.warn('The removeEventListener() method is deprecated. Use off() instead.');
removeListeners([eventName], handler, context);
};
return self;
};
OTHelpers.eventing.Event = function() {
return function (type, cancelable) {
this.type = type;
this.cancelable = cancelable !== undefined ? cancelable : true;
var _defaultPrevented = false;
this.preventDefault = function() {
if (this.cancelable) {
_defaultPrevented = true;
} else {
OTHelpers.warn('Event.preventDefault :: Trying to preventDefault ' +
'on an Event that isn\'t cancelable');
}
};
this.isDefaultPrevented = function() {
return _defaultPrevented;
};
};
};
})(window, window.OTHelpers);
/*jshint browser:true, smarttabs:true*/
// tb_require('../helpers.js')
// tb_require('./callbacks.js')
// DOM helpers
(function(window, OTHelpers, undefined) {
OTHelpers.isElementNode = function(node) {
return node && typeof node === 'object' && node.nodeType === 1;
};
// Returns true if the client supports element.classList
OTHelpers.supportsClassList = function() {
var hasSupport = (typeof document !== 'undefined') &&
('classList' in document.createElement('a'));
OTHelpers.supportsClassList = function() { return hasSupport; };
return hasSupport;
};
OTHelpers.removeElement = function(element) {
if (element && element.parentNode) {
element.parentNode.removeChild(element);
}
};
OTHelpers.removeElementById = function(elementId) {
/*jshint newcap:false */
this.removeElement(OTHelpers(elementId));
};
OTHelpers.removeElementsByType = function(parentElem, type) {
if (!parentElem) return;
var elements = parentElem.getElementsByTagName(type);
// elements is a "live" NodesList collection. Meaning that the collection
// itself will be mutated as we remove elements from the DOM. This means
// that "while there are still elements" is safer than "iterate over each
// element" as the collection length and the elements indices will be modified
// with each iteration.
while (elements.length) {
parentElem.removeChild(elements[0]);
}
};
OTHelpers.emptyElement = function(element) {
while (element.firstChild) {
element.removeChild(element.firstChild);
}
return element;
};
OTHelpers.createElement = function(nodeName, attributes, children, doc) {
var element = (doc || document).createElement(nodeName);
if (attributes) {
for (var name in attributes) {
if (typeof(attributes[name]) === 'object') {
if (!element[name]) element[name] = {};
var subAttrs = attributes[name];
for (var n in subAttrs) {
element[name][n] = subAttrs[n];
}
}
else if (name === 'className') {
element.className = attributes[name];
}
else {
element.setAttribute(name, attributes[name]);
}
}
}
var setChildren = function(child) {
if(typeof child === 'string') {
element.innerHTML = element.innerHTML + child;
} else {
element.appendChild(child);
}
};
if(OTHelpers.isArray(children)) {
OTHelpers.forEach(children, setChildren);
} else if(children) {
setChildren(children);
}
return element;
};
OTHelpers.createButton = function(innerHTML, attributes, events) {
var button = OTHelpers.createElement('button', attributes, innerHTML);
if (events) {
for (var name in events) {
if (events.hasOwnProperty(name)) {
OTHelpers.on(button, name, events[name]);
}
}
button._boundEvents = events;
}
return button;
};
// Detects when an element is not part of the document flow because
// it or one of it's ancesters has display:none.
OTHelpers.isDisplayNone = function(element) {
if ( (element.offsetWidth === 0 || element.offsetHeight === 0) &&
OTHelpers.css(element, 'display') === 'none') return true;
if (element.parentNode && element.parentNode.style) {
return OTHelpers.isDisplayNone(element.parentNode);
}
return false;
};
OTHelpers.findElementWithDisplayNone = function(element) {
if ( (element.offsetWidth === 0 || element.offsetHeight === 0) &&
OTHelpers.css(element, 'display') === 'none') return element;
if (element.parentNode && element.parentNode.style) {
return OTHelpers.findElementWithDisplayNone(element.parentNode);
}
return null;
};
function objectHasProperties(obj) {
for (var key in obj) {
if (obj.hasOwnProperty(key)) return true;
}
return false;
}
// Allows an +onChange+ callback to be triggered when specific style properties
// of +element+ are notified. The callback accepts a single parameter, which is
// a hash where the keys are the style property that changed and the values are
// an array containing the old and new values ([oldValue, newValue]).
//
// Width and Height changes while the element is display: none will not be
// fired until such time as the element becomes visible again.
//
// This function returns the MutationObserver itself. Once you no longer wish
// to observe the element you should call disconnect on the observer.
//
// Observing changes:
// // observe changings to the width and height of object
// dimensionsObserver = OTHelpers.observeStyleChanges(object,
// ['width', 'height'], function(changeSet) {
// OT.debug("The new width and height are " +
// changeSet.width[1] + ',' + changeSet.height[1]);
// });
//
// Cleaning up
// // stop observing changes
// dimensionsObserver.disconnect();
// dimensionsObserver = null;
//
OTHelpers.observeStyleChanges = function(element, stylesToObserve, onChange) {
var oldStyles = {};
var getStyle = function getStyle(style) {
switch (style) {
case 'width':
return OTHelpers.width(element);
case 'height':
return OTHelpers.height(element);
default:
return OTHelpers.css(element);
}
};
// get the inital values
OTHelpers.forEach(stylesToObserve, function(style) {
oldStyles[style] = getStyle(style);
});
var observer = new MutationObserver(function(mutations) {
var changeSet = {};
OTHelpers.forEach(mutations, function(mutation) {
if (mutation.attributeName !== 'style') return;
var isHidden = OTHelpers.isDisplayNone(element);
OTHelpers.forEach(stylesToObserve, function(style) {
if(isHidden && (style === 'width' || style === 'height')) return;
var newValue = getStyle(style);
if (newValue !== oldStyles[style]) {
changeSet[style] = [oldStyles[style], newValue];
oldStyles[style] = newValue;
}
});
});
if (objectHasProperties(changeSet)) {
// Do this after so as to help avoid infinite loops of mutations.
OTHelpers.callAsync(function() {
onChange.call(null, changeSet);
});
}
});
observer.observe(element, {
attributes:true,
attributeFilter: ['style'],
childList:false,
characterData:false,
subtree:false
});
return observer;
};
// trigger the +onChange+ callback whenever
// 1. +element+ is removed
// 2. or an immediate child of +element+ is removed.
//
// This function returns the MutationObserver itself. Once you no longer wish
// to observe the element you should call disconnect on the observer.
//
// Observing changes:
// // observe changings to the width and height of object
// nodeObserver = OTHelpers.observeNodeOrChildNodeRemoval(object, function(removedNodes) {
// OT.debug("Some child nodes were removed");
// OTHelpers.forEach(removedNodes, function(node) {
// OT.debug(node);
// });
// });
//
// Cleaning up
// // stop observing changes
// nodeObserver.disconnect();
// nodeObserver = null;
//
OTHelpers.observeNodeOrChildNodeRemoval = function(element, onChange) {
var observer = new MutationObserver(function(mutations) {
var removedNodes = [];
OTHelpers.forEach(mutations, function(mutation) {
if (mutation.removedNodes.length) {
removedNodes = removedNodes.concat(Array.prototype.slice.call(mutation.removedNodes));
}
});
if (removedNodes.length) {
// Do this after so as to help avoid infinite loops of mutations.
OTHelpers.callAsync(function() {
onChange(removedNodes);
});
}
});
observer.observe(element, {
attributes:false,
childList:true,
characterData:false,
subtree:true
});
return observer;
};
})(window, window.OTHelpers);
/*jshint browser:true, smarttabs:true*/
// tb_require('../helpers.js')
// tb_require('./dom.js')
(function(window, OTHelpers, undefined) {
OTHelpers.Modal = function(options) {
OTHelpers.eventing(this, true);
var callback = arguments[arguments.length - 1];
if(!OTHelpers.isFunction(callback)) {
throw new Error('OTHelpers.Modal2 must be given a callback');
}
if(arguments.length < 2) {
options = {};
}
var domElement = document.createElement('iframe');
domElement.id = options.id || OTHelpers.uuid();
domElement.style.position = 'absolute';
domElement.style.position = 'fixed';
domElement.style.height = '100%';
domElement.style.width = '100%';
domElement.style.top = '0px';
domElement.style.left = '0px';
domElement.style.right = '0px';
domElement.style.bottom = '0px';
domElement.style.zIndex = 1000;
domElement.style.border = '0';
try {
domElement.style.backgroundColor = 'rgba(0,0,0,0.2)';
} catch (err) {
// Old IE browsers don't support rgba and we still want to show the upgrade message
// but we just make the background of the iframe completely transparent.
domElement.style.backgroundColor = 'transparent';
domElement.setAttribute('allowTransparency', 'true');
}
domElement.scrolling = 'no';
domElement.setAttribute('scrolling', 'no');
// This is necessary for IE, as it will not inherit it's doctype from
// the parent frame.
var frameContent = '' +
'' +
'' +
'OT.setLogLevel() sets the log level for runtime log messages that
* are the OpenTok library generates. The default value for the log level is OT.ERROR.
* OT.log().
* The code also logs an error message when it attempts to publish a stream before the Session
* object dispatches a sessionConnected event.
*
* OT.setLogLevel(OT.LOG);
* session = OT.initSession(sessionId);
* OT.log(sessionId);
* publisher = OT.initPublisher("publishContainer");
* session.publish(publisher);
*
*
* @param {Number} logLevel The degree of logging desired by the developer:
*
*
*
* OT.NONE API logging is disabled.
* OT.ERROR Logging of errors only.
* OT.WARN Logging of warnings and errors.
* OT.INFO Logging of other useful information, in addition to
* warnings and errors.
* OT.LOG Logging of OT.log() messages, in addition
* to OpenTok info, warning,
* and error messages.
* OT.DEBUG Fine-grained logging of all API actions, as well as
* OT.log() messages.
*
OT.LOG or OT.DEBUG,
* by calling OT.setLogLevel(OT.LOG) or OT.setLogLevel(OT.DEBUG).
*
* @param {String} message The string to log.
*
* @name OT.log
* @memberof OT
* @function
* @see OT.setLogLevel()
*/
})(window);
!(function() {
var addCss = function(document, url, callback) {
var head = document.head || document.getElementsByTagName('head')[0];
var cssTag = OT.$.createElement('link', {
type: 'text/css',
media: 'screen',
rel: 'stylesheet',
href: url
});
head.appendChild(cssTag);
OT.$.on(cssTag, 'error', function(error) {
OT.error('Could not load CSS for dialog', url, error && error.message || error);
});
OT.$.on(cssTag, 'load', callback);
};
var addDialogCSS = function(document, urls, callback) {
var allURLs = [
'//fonts.googleapis.com/css?family=Didact+Gothic',
OT.properties.cssURL
].concat(urls);
var remainingStylesheets = allURLs.length;
OT.$.forEach(allURLs, function(stylesheetUrl) {
addCss(document, stylesheetUrl, function() {
if(--remainingStylesheets <= 0) {
callback();
}
});
});
};
var templateElement = function(classes, children, tagName) {
var el = OT.$.createElement(tagName || 'div', { 'class': classes }, children, this);
el.on = OT.$.bind(OT.$.on, OT.$, el);
el.off = OT.$.bind(OT.$.off, OT.$, el);
return el;
};
var checkBoxElement = function (classes, nameAndId, onChange) {
var checkbox = templateElement.call(this, '', null, 'input').on('change', onChange);
if (OT.$.browser() === 'IE' && OT.$.browserVersion().version <= 8) {
// Fix for IE8 not triggering the change event
checkbox.on('click', function() {
checkbox.blur();
checkbox.focus();
});
}
checkbox.setAttribute('name', nameAndId);
checkbox.setAttribute('id', nameAndId);
checkbox.setAttribute('type', 'checkbox');
return checkbox;
};
var linkElement = function(children, href, classes) {
var link = templateElement.call(this, classes || '', children, 'a');
link.setAttribute('href', href);
return link;
};
OT.Dialogs = {};
OT.Dialogs.AllowDeny = {
Chrome: {},
Firefox: {}
};
OT.Dialogs.AllowDeny.Chrome.initialPrompt = function() {
var modal = new OT.$.Modal(function(window, document) {
var el = OT.$.bind(templateElement, document),
close, root;
close = el('OT_closeButton', '×')
.on('click', function() {
modal.trigger('closeButtonClicked');
modal.close();
});
root = el('OT_root OT_dialog OT_dialog-allow-deny-chrome-first', [
close,
el('OT_dialog-messages', [
el('OT_dialog-messages-main', 'Allow camera and mic access'),
el('OT_dialog-messages-minor', 'Click the Allow button in the upper-right corner ' +
'of your browser to enable real-time communication.'),
el('OT_dialog-allow-highlight-chrome')
])
]);
addDialogCSS(document, [], function() {
document.body.appendChild(root);
});
});
return modal;
};
OT.Dialogs.AllowDeny.Chrome.previouslyDenied = function(website) {
var modal = new OT.$.Modal(function(window, document) {
var el = OT.$.bind(templateElement, document),
close,
root;
close = el('OT_closeButton', '×')
.on('click', function() {
modal.trigger('closeButtonClicked');
modal.close();
});
root = el('OT_root OT_dialog OT_dialog-allow-deny-chrome-pre-denied', [
close,
el('OT_dialog-messages', [
el('OT_dialog-messages-main', 'Allow camera and mic access'),
el('OT_dialog-messages-minor', [
'To interact with this app, follow these 3 steps:',
el('OT_dialog-3steps', [
el('OT_dialog-3steps-step', [
el('OT_dialog-3steps-step-num', '1'),
'Find this icon in the URL bar and click it',
el('OT_dialog-allow-camera-icon')
]),
el('OT_dialog-3steps-seperator'),
el('OT_dialog-3steps-step', [
el('OT_dialog-3steps-step-num', '2'),
'Select "Ask if ' + website + ' wants to access your camera and mic" ' +
'and then click Done.'
]),
el('OT_dialog-3steps-seperator'),
el('OT_dialog-3steps-step', [
el('OT_dialog-3steps-step-num', '3'),
'Refresh your browser.'
])
])
])
])
]);
addDialogCSS(document, [], function() {
document.body.appendChild(root);
});
});
return modal;
};
OT.Dialogs.AllowDeny.Chrome.deniedNow = function() {
var modal = new OT.$.Modal(function(window, document) {
var el = OT.$.bind(templateElement, document),
root;
root = el('OT_root OT_dialog-blackout',
el('OT_dialog OT_dialog-allow-deny-chrome-now-denied', [
el('OT_dialog-messages', [
el('OT_dialog-messages-main ',
el('OT_dialog-allow-camera-icon')
),
el('OT_dialog-messages-minor',
'Find & click this icon to allow camera and mic access.'
)
])
])
);
addDialogCSS(document, [], function() {
document.body.appendChild(root);
});
});
return modal;
};
OT.Dialogs.AllowDeny.Firefox.maybeDenied = function() {
var modal = new OT.$.Modal(function(window, document) {
var el = OT.$.bind(templateElement, document),
close,
root;
close = el('OT_closeButton', '×')
.on('click', function() {
modal.trigger('closeButtonClicked');
modal.close();
});
root = el('OT_root OT_dialog OT_dialog-allow-deny-firefox-maybe-denied', [
close,
el('OT_dialog-messages', [
el('OT_dialog-messages-main', 'Please allow camera & mic access'),
el('OT_dialog-messages-minor', [
'To interact with this app, follow these 3 steps:',
el('OT_dialog-3steps', [
el('OT_dialog-3steps-step', [
el('OT_dialog-3steps-step-num', '1'),
'Reload the page, or click the camera icon ' +
'in the browser URL bar.'
]),
el('OT_dialog-3steps-seperator'),
el('OT_dialog-3steps-step', [
el('OT_dialog-3steps-step-num', '2'),
'In the menu, select your camera & mic.'
]),
el('OT_dialog-3steps-seperator'),
el('OT_dialog-3steps-step', [
el('OT_dialog-3steps-step-num', '3'),
'Click "Share Selected Devices."'
])
])
])
])
]);
addDialogCSS(document, [], function() {
document.body.appendChild(root);
});
});
return modal;
};
OT.Dialogs.AllowDeny.Firefox.denied = function() {
var modal = new OT.$.Modal(function(window, document) {
var el = OT.$.bind(templateElement, document),
btn = OT.$.bind(templateElement, document, 'OT_dialog-button OT_dialog-button-large'),
root,
refreshButton;
refreshButton = btn('Reload')
.on('click', function() {
modal.trigger('refresh');
});
root = el('OT_root OT_dialog-blackout',
el('OT_dialog OT_dialog-allow-deny-firefox-denied', [
el('OT_dialog-messages', [
el('OT_dialog-messages-minor',
'Access to camera and microphone has been denied. ' +
'Click the button to reload page.'
)
]),
el('OT_dialog-single-button', refreshButton)
])
);
addDialogCSS(document, [], function() {
document.body.appendChild(root);
});
});
return modal;
};
OT.Dialogs.Plugin = {};
OT.Dialogs.Plugin.promptToInstall = function() {
var modal = new OT.$.Modal(function(window, document) {
var el = OT.$.bind(templateElement, document),
btn = function(children, size) {
var classes = 'OT_dialog-button ' +
(size ? 'OT_dialog-button-' + size : 'OT_dialog-button-large'),
b = el(classes, children);
b.enable = function() {
OT.$.removeClass(this, 'OT_dialog-button-disabled');
return this;
};
b.disable = function() {
OT.$.addClass(this, 'OT_dialog-button-disabled');
return this;
};
return b;
},
downloadButton = btn('Download plugin'),
cancelButton = btn('cancel', 'small'),
refreshButton = btn('Refresh browser'),
acceptEULA,
checkbox,
close,
root;
function onDownload() {
modal.trigger('download');
setTimeout(function() {
root.querySelector('.OT_dialog-messages-main').innerHTML =
'Plugin installation successful';
var sections = root.querySelectorAll('.OT_dialog-single-button-wide');
OT.$.addClass(sections[0], 'OT_dialog-hidden');
OT.$.removeClass(sections[1], 'OT_dialog-hidden');
}, 3000);
}
function onRefresh() {
modal.trigger('refresh');
}
function onToggleEULA() {
if (checkbox.checked) {
enableButtons();
}
else {
disableButtons();
}
}
function enableButtons() {
downloadButton.enable();
downloadButton.on('click', onDownload);
refreshButton.enable();
refreshButton.on('click', onRefresh);
}
function disableButtons() {
downloadButton.disable();
downloadButton.off('click', onDownload);
refreshButton.disable();
refreshButton.off('click', onRefresh);
}
downloadButton.disable();
refreshButton.disable();
cancelButton.on('click', function() {
modal.trigger('cancelButtonClicked');
modal.close();
});
close = el('OT_closeButton', '×')
.on('click', function() {
modal.trigger('closeButtonClicked');
modal.close();
});
acceptEULA = linkElement.call(document,
'end-user license agreement',
'http://tokbox.com/support/ie-eula');
checkbox = checkBoxElement.call(document, null, 'acceptEULA', onToggleEULA);
root = el('OT_root OT_dialog OT_dialog-plugin-prompt', [
close,
el('OT_dialog-messages', [
el('OT_dialog-messages-main', 'This app requires real-time communication')
]),
el('OT_dialog-single-button-wide', [
el('OT_dialog-single-button-with-title', [
el('OT_dialog-button-title', [
checkbox,
(function() {
var x = el('', 'accept', 'label');
x.setAttribute('for', checkbox.id);
x.style.margin = '0 5px';
return x;
})(),
acceptEULA
]),
downloadButton,
cancelButton
])
]),
el('OT_dialog-single-button-wide OT_dialog-hidden', [
el('OT_dialog-single-button-with-title', [
el('OT_dialog-button-title', [
'You can now enjoy webRTC enabled video via Internet Explorer.'
]),
refreshButton
])
])
]);
addDialogCSS(document, [], function() {
document.body.appendChild(root);
});
});
return modal;
};
OT.Dialogs.Plugin.promptToReinstall = function() {
var modal = new OT.$.Modal(function(window, document) {
var el = OT.$.bind(templateElement, document),
close,
okayButton,
root;
close = el('OT_closeButton', '×');
okayButton = el('OT_dialog-button', 'Okay');
OT.$.on(okayButton, 'click', function() {
modal.trigger('okay');
});
OT.$.on(close, 'click', function() {
modal.trigger('closeButtonClicked');
modal.close();
});
root = el('OT_ROOT OT_dialog OT_dialog-plugin-reinstall', [
close,
el('OT_dialog-messages', [
el('OT_dialog-messages-main', 'Reinstall Opentok Plugin'),
el('OT_dialog-messages-minor', 'Uh oh! Try reinstalling the OpenTok plugin again to ' +
'enable real-time video communication for Internet Explorer.')
]),
el('OT_dialog-single-button', okayButton)
]);
addDialogCSS(document, [], function() {
document.body.appendChild(root);
});
});
return modal;
};
OT.Dialogs.Plugin.updateInProgress = function() {
var progressBar,
progressText,
progressValue = 0;
var modal = new OT.$.Modal(function(window, document) {
var el = OT.$.bind(templateElement, document),
root;
progressText = el('OT_dialog-plugin-upgrade-percentage', '0%', 'strong');
progressBar = el('OT_dialog-progress-bar-fill');
root = el('OT_ROOT OT_dialog OT_dialog-plugin-upgrading', [
el('OT_dialog-messages', [
el('OT_dialog-messages-main', [
'One moment please... ',
progressText
]),
el('OT_dialog-progress-bar', progressBar),
el('OT_dialog-messages-minor', 'Please wait while the OpenTok plugin is updated')
])
]);
addDialogCSS(document, [], function() {
document.body.appendChild(root);
if(progressValue != null) {
modal.setUpdateProgress(progressValue);
}
});
});
modal.setUpdateProgress = function(newProgress) {
if(progressBar && progressText) {
if(newProgress > 99) {
OT.$.css(progressBar, 'width', '');
progressText.innerHTML = '100%';
} else if(newProgress < 1) {
OT.$.css(progressBar, 'width', '0%');
progressText.innerHTML = '0%';
} else {
OT.$.css(progressBar, 'width', newProgress + '%');
progressText.innerHTML = newProgress + '%';
}
} else {
progressValue = newProgress;
}
};
return modal;
};
OT.Dialogs.Plugin.updateComplete = function(error) {
var modal = new OT.$.Modal(function(window, document) {
var el = OT.$.bind(templateElement, document),
reloadButton,
root;
reloadButton = el('OT_dialog-button', 'Reload').on('click', function() {
modal.trigger('reload');
});
var msgs;
if(error) {
msgs = ['Update Failed.', error + '' || 'NO ERROR'];
} else {
msgs = ['Update Complete.',
'The OpenTok plugin has been succesfully updated. ' +
'Please reload your browser.'];
}
root = el('OT_root OT_dialog OT_dialog-plugin-upgraded', [
el('OT_dialog-messages', [
el('OT_dialog-messages-main', msgs[0]),
el('OT_dialog-messages-minor', msgs[1])
]),
el('OT_dialog-single-button', reloadButton)
]);
addDialogCSS(document, [], function() {
document.body.appendChild(root);
});
});
return modal;
};
})();
!(function(window) {
// IMPORTANT This file should be included straight after helpers.js
if (!window.OT) window.OT = {};
if (!OT.properties) {
throw new Error('OT.properties does not exist, please ensure that you include a valid ' +
'properties file.');
}
OT.useSSL = function () {
return OT.properties.supportSSL && (window.location.protocol.indexOf('https') >= 0 ||
window.location.protocol.indexOf('chrome-extension') >= 0);
};
// Consumes and overwrites OT.properties. Makes it better and stronger!
OT.properties = function(properties) {
var props = OT.$.clone(properties);
props.debug = properties.debug === 'true' || properties.debug === true;
props.supportSSL = properties.supportSSL === 'true' || properties.supportSSL === true;
if (window.OTProperties) {
// Allow window.OTProperties to override cdnURL, configURL, assetURL and cssURL
if (window.OTProperties.cdnURL) props.cdnURL = window.OTProperties.cdnURL;
if (window.OTProperties.cdnURLSSL) props.cdnURLSSL = window.OTProperties.cdnURLSSL;
if (window.OTProperties.configURL) props.configURL = window.OTProperties.configURL;
if (window.OTProperties.assetURL) props.assetURL = window.OTProperties.assetURL;
if (window.OTProperties.cssURL) props.cssURL = window.OTProperties.cssURL;
}
if (!props.assetURL) {
if (OT.useSSL()) {
props.assetURL = props.cdnURLSSL + '/webrtc/' + props.version;
} else {
props.assetURL = props.cdnURL + '/webrtc/' + props.version;
}
}
var isIE89 = OT.$.browser() === 'IE' && OT.$.browserVersion().version <= 9;
if (!(isIE89 && window.location.protocol.indexOf('https') < 0)) {
props.apiURL = props.apiURLSSL;
props.loggingURL = props.loggingURLSSL;
}
if (!props.configURL) props.configURL = props.assetURL + '/js/dynamic_config.min.js';
if (!props.cssURL) props.cssURL = props.assetURL + '/css/ot.min.css';
return props;
}(OT.properties);
})(window);
!(function() {
//--------------------------------------
// JS Dynamic Config
//--------------------------------------
OT.Config = (function() {
var _loaded = false,
_global = {},
_partners = {},
_script,
_head = document.head || document.getElementsByTagName('head')[0],
_loadTimer,
_clearTimeout = function() {
if (_loadTimer) {
clearTimeout(_loadTimer);
_loadTimer = null;
}
},
_cleanup = function() {
_clearTimeout();
if (_script) {
_script.onload = _script.onreadystatechange = null;
if ( _head && _script.parentNode ) {
_head.removeChild( _script );
}
_script = undefined;
}
},
_onLoad = function() {
// Only IE and Opera actually support readyState on Script elements.
if (_script.readyState && !/loaded|complete/.test( _script.readyState )) {
// Yeah, we're not ready yet...
return;
}
_clearTimeout();
if (!_loaded) {
// Our config script is loaded but there is not config (as
// replaceWith wasn't called). Something went wrong. Possibly
// the file we loaded wasn't actually a valid config file.
_this._onLoadTimeout();
}
},
_getModule = function(moduleName, apiKey) {
if (apiKey && _partners[apiKey] && _partners[apiKey][moduleName]) {
return _partners[apiKey][moduleName];
}
return _global[moduleName];
},
_this;
_this = {
// In ms
loadTimeout: 4000,
load: function(configUrl) {
if (!configUrl) throw new Error('You must pass a valid configUrl to Config.load');
_loaded = false;
setTimeout(function() {
_script = document.createElement( 'script' );
_script.async = 'async';
_script.src = configUrl;
_script.onload = _script.onreadystatechange = OT.$.bind(_onLoad, this);
_head.appendChild(_script);
},1);
_loadTimer = setTimeout(function() {
_this._onLoadTimeout();
}, this.loadTimeout);
},
_onLoadTimeout: function() {
_cleanup();
OT.warn('TB DynamicConfig failed to load in ' + _this.loadTimeout + ' ms');
this.trigger('dynamicConfigLoadFailed');
},
isLoaded: function() {
return _loaded;
},
reset: function() {
_cleanup();
_loaded = false;
_global = {};
_partners = {};
},
// This is public so that the dynamic config file can load itself.
// Using it for other purposes is discouraged, but not forbidden.
replaceWith: function(config) {
_cleanup();
if (!config) config = {};
_global = config.global || {};
_partners = config.partners || {};
if (!_loaded) _loaded = true;
this.trigger('dynamicConfigChanged');
},
// @example Get the value that indicates whether exceptionLogging is enabled
// OT.Config.get('exceptionLogging', 'enabled');
//
// @example Get a key for a specific partner, fallback to the default if there is
// no key for that partner
// OT.Config.get('exceptionLogging', 'enabled', 'apiKey');
//
get: function(moduleName, key, apiKey) {
var module = _getModule(moduleName, apiKey);
return module ? module[key] : null;
}
};
OT.$.eventing(_this);
return _this;
})();
})(window);
/**
* @license TB Plugin 0.4.0.8 72b534e HEAD
* http://www.tokbox.com/
*
* Copyright (c) 2014 TokBox, Inc.
* Released under the MIT license
* http://opensource.org/licenses/MIT
*
* Date: September 08 10:17:49 2014
*
*/
/* jshint globalstrict: true, strict: false, undef: true, unused: false,
trailing: true, browser: true, smarttabs:true */
/* global scope:true, OT:true */
/* exported TBPlugin */
/* jshint ignore:start */
(function(scope) {
/* jshint ignore:end */
// If we've already be setup, bail
if (scope.TBPlugin !== void 0) return;
// TB must exist first, otherwise we can't do anything
if (scope.OT === void 0) return;
// Establish the environment that we're running in
var env = OT.$.browserVersion(),
isSupported = env.browser === 'IE' && env.version >= 8,
pluginReady = false;
var TBPlugin = {
isSupported: function () { return isSupported; },
isReady: function() { return pluginReady; }
};
scope.TBPlugin = TBPlugin;
// We only support IE, version 10 or above right now
if (!TBPlugin.isSupported()) {
TBPlugin.isInstalled = function isInstalled () { return false; };
return;
}
// tb_require('./header.js')
/* exported shim */
// Shims for various missing things from JS
// Applied only after init is called to prevent unnecessary polution
var shim = function shim () {
if (!Function.prototype.bind) {
Function.prototype.bind = function (oThis) {
if (typeof this !== 'function') {
// closest thing possible to the ECMAScript 5 internal IsCallable function
throw new TypeError('Function.prototype.bind - what is trying to be bound is not callable');
}
var aArgs = Array.prototype.slice.call(arguments, 1),
fToBind = this,
FNOP = function () {},
fBound = function () {
return fToBind.apply(this instanceof FNOP && oThis ?
this : oThis, aArgs.concat(Array.prototype.slice.call(arguments)));
};
FNOP.prototype = this.prototype;
fBound.prototype = new FNOP();
return fBound;
};
}
if(!Array.isArray) {
Array.isArray = function (vArg) {
return Object.prototype.toString.call(vArg) === '[object Array]';
};
}
if (!Array.prototype.indexOf) {
Array.prototype.indexOf = function (searchElement, fromIndex) {
var i,
pivot = (fromIndex) ? fromIndex : 0,
length;
if (!this) {
throw new TypeError();
}
length = this.length;
if (length === 0 || pivot >= length) {
return -1;
}
if (pivot < 0) {
pivot = length - Math.abs(pivot);
}
for (i = pivot; i < length; i++) {
if (this[i] === searchElement) {
return i;
}
}
return -1;
};
}
if (!Array.prototype.map)
{
Array.prototype.map = function(fun /*, thisArg */)
{
'use strict';
if (this === void 0 || this === null)
throw new TypeError();
var t = Object(this);
var len = t.length >>> 0;
if (typeof fun !== 'function') {
throw new TypeError();
}
var res = new Array(len);
var thisArg = arguments.length >= 2 ? arguments[1] : void 0;
for (var i = 0; i < len; i++)
{
// NOTE: Absolute correctness would demand Object.defineProperty
// be used. But this method is fairly new, and failure is
// possible only if Object.prototype or Array.prototype
// has a property |i| (very unlikely), so use a less-correct
// but more portable alternative.
if (i in t)
res[i] = fun.call(thisArg, t[i], i, t);
}
return res;
};
}
};
// tb_require('./header.js')
// tb_require('./shims.js')
/* jshint globalstrict: true, strict: false, undef: true, unused: true,
trailing: true, browser: true, smarttabs:true */
/* global OT:true, TBPlugin:true, pluginInfo:true, debug:true, scope:true,
_document:true */
/* exported createMediaCaptureController:true, createPeerController:true,
injectObject:true, plugins:true, mediaCaptureObject:true,
removeAllObjects:true, curryCallAsync:true */
var objectTimeouts = {},
mediaCaptureObject,
plugins = {};
var curryCallAsync = function curryCallAsync (fn) {
return function() {
var args = Array.prototype.slice.call(arguments);
args.unshift(fn);
OT.$.callAsync.apply(OT.$, args);
};
};
var generatePluginUuid = function generatePluginUuid () {
return OT.$.uuid().replace(/\-+/g, '');
};
var clearObjectLoadTimeout = function clearObjectLoadTimeout (callbackId) {
if (!callbackId) return;
if (objectTimeouts[callbackId]) {
clearTimeout(objectTimeouts[callbackId]);
delete objectTimeouts[callbackId];
}
if (scope[callbackId]) {
try {
delete scope[callbackId];
} catch (err) {
scope[callbackId] = void 0;
}
}
};
var removeObjectFromDom = function removeObjectFromDom (object) {
clearObjectLoadTimeout(object.getAttribute('tb_callbackId'));
if (mediaCaptureObject && mediaCaptureObject.id === object.id) {
mediaCaptureObject = null;
}
else if (plugins.hasOwnProperty(object.id)) {
delete plugins[object.id];
}
object.parentNode.removeChild(object);
};
// @todo bind destroy to unload, may need to coordinate with TB
// jshint -W098
var removeAllObjects = function removeAllObjects () {
if (mediaCaptureObject) mediaCaptureObject.destroy();
for (var id in plugins) {
if (plugins.hasOwnProperty(id)) {
plugins[id].destroy();
}
}
};
// Reference counted wrapper for a plugin object
var PluginObject = function PluginObject (plugin) {
var _plugin = plugin,
_liveObjects = [];
this._ = _plugin;
this.addRef = function(ref) {
_liveObjects.push(ref);
return this;
};
this.removeRef = function(ref) {
if (_liveObjects.length === 0) return;
var index = _liveObjects.indexOf(ref);
if (index !== -1) {
_liveObjects.splice(index, 1);
}
if (_liveObjects.length === 0) {
this.destroy();
}
return this;
};
this.isValid = function() {
return _plugin.valid;
};
// Event Handling Mechanisms
var eventHandlers = {};
var onCustomEvent = OT.$.bind(curryCallAsync(function onCustomEvent() {
var args = Array.prototype.slice.call(arguments),
name = args.shift();
if (!eventHandlers.hasOwnProperty(name) && eventHandlers[name].length) {
return;
}
OT.$.forEach(eventHandlers[name], function(handler) {
handler[0].apply(handler[1], args);
});
}), this);
this.on = function (name, callback, context) {
if (!eventHandlers.hasOwnProperty(name)) {
eventHandlers[name] = [];
}
eventHandlers[name].push([callback, context]);
return this;
};
this.off = function (name, callback, context) {
if (!eventHandlers.hasOwnProperty(name) ||
eventHandlers[name].length === 0) {
return;
}
OT.$.filter(eventHandlers[name], function(listener) {
return listener[0] === callback &&
listener[1] === context;
});
return this;
};
this.once = function (name, callback, context) {
var fn = function () {
this.off(name, fn, this);
return callback.apply(context, arguments);
};
this.on(name, fn, this);
return this;
};
this.onReady = function(readyCallback) {
if (_plugin.on) {
// If the plugin supports custom events we'll use them
_plugin.on(-1, {customEvent: curryCallAsync(onCustomEvent, this)});
}
// Only the main plugin has an initialise method
if (_plugin.initialise) {
this.on('ready', OT.$.bind(curryCallAsync(readyCallback), this));
_plugin.initialise();
}
else {
readyCallback.call(null);
}
};
this.destroy = function() {
while (_liveObjects.length) {
_liveObjects.shift().destroy();
}
removeObjectFromDom(_plugin);
_plugin = null;
};
this.setStream = function(stream, completion) {
if (completion) {
if (stream.hasVideo()) {
// FIX ME renderingStarted currently doesn't first
// this.once('renderingStarted', completion);
var verifyStream = function() {
if (_plugin.videoWidth > 0) {
// This fires a little too soon.
setTimeout(completion, 200);
}
else {
setTimeout(verifyStream, 500);
}
};
setTimeout(verifyStream, 500);
}
else {
// TODO Investigate whether there is a good way to detect
// when the audio is ready. Does it even matter?
completion();
}
}
_plugin.setStream(stream);
};
};
// Stops and cleans up after the plugin object load timeout.
var injectObject = function injectObject (mimeType, isVisible, params, completion) {
var callbackId = 'TBPlugin_loaded_' + generatePluginUuid();
params.onload = callbackId;
params.userAgent = window.navigator.userAgent.toLowerCase();
scope[callbackId] = function() {
clearObjectLoadTimeout(callbackId);
o.setAttribute('id', 'tb_plugin_' + o.uuid);
o.removeAttribute('tb_callbackId');
pluginRefCounted.uuid = o.uuid;
pluginRefCounted.id = o.id;
pluginRefCounted.onReady(function(err) {
if (err) {
OT.error('Error while starting up plugin ' + o.uuid + ': ' + err);
return;
}
debug('Plugin ' + o.id + ' is loaded');
if (completion && OT.$.isFunction(completion)) {
completion.call(TBPlugin, null, pluginRefCounted);
}
});
};
var tmpContainer = document.createElement('div'),
objBits = [],
extraAttributes = ['width="0" height="0"'],
pluginRefCounted,
o;
if (isVisible !== true) {
extraAttributes.push('visibility="hidden"');
}
objBits.push('');
tmpContainer.innerHTML = objBits.join('');
_document.body.appendChild(tmpContainer);
function firstElementChild(element) {
if(element.firstElementChild) {
return element.firstElementChild;
}
for(var i = 0, len = element.childNodes.length; i < len; ++i) {
if(element.childNodes[i].nodeType === 1) {
return element.childNodes[i];
}
}
return null;
}
o = firstElementChild(tmpContainer);
o.setAttribute('tb_callbackId', callbackId);
pluginRefCounted = new PluginObject(o);
_document.body.appendChild(o);
_document.body.removeChild(tmpContainer);
objectTimeouts[callbackId] = setTimeout(function() {
clearObjectLoadTimeout(callbackId);
completion.call(TBPlugin, 'The object with the mimeType of ' +
mimeType + ' timed out while loading.');
_document.body.removeChild(o);
}, 3000);
return pluginRefCounted;
};
// Creates the Media Capture controller. This exposes selectSources and is
// used in the private API.
//
// Only one Media Capture controller can exist at once, calling this method
// more than once will raise an exception.
//
var createMediaCaptureController = function createMediaCaptureController (completion) {
if (mediaCaptureObject) {
throw new Error('TBPlugin.createMediaCaptureController called multiple times!');
}
mediaCaptureObject = injectObject(pluginInfo.mimeType, false, {windowless: false}, completion);
mediaCaptureObject.selectSources = function() {
return this._.selectSources.apply(this._, arguments);
};
return mediaCaptureObject;
};
// Create an instance of the publisher/subscriber/peerconnection object.
// Many of these can exist at once, but the +id+ of each must be unique
// within a single instance of scope (window or window-like thing).
//
var createPeerController = function createPeerController (completion) {
var o = injectObject(pluginInfo.mimeType, true, {windowless: true}, function(err, plugin) {
if (err) {
completion.call(TBPlugin, err);
return;
}
plugins[plugin.id] = plugin;
completion.call(TBPlugin, null, plugin);
});
return o;
};
// tb_require('./header.js')
// tb_require('./shims.js')
// tb_require('./plugin_object.js')
/* jshint globalstrict: true, strict: false, undef: true, unused: true,
trailing: true, browser: true, smarttabs:true */
/* global OT:true, debug:true */
/* exported VideoContainer */
var VideoContainer = function VideoContainer (plugin, stream) {
this.domElement = plugin._;
this.parentElement = plugin._.parentNode;
plugin.addRef(this);
this.appendTo = function (parentDomElement) {
if (parentDomElement && plugin._.parentNode !== parentDomElement) {
debug('VideoContainer appendTo', parentDomElement);
parentDomElement.appendChild(plugin._);
this.parentElement = parentDomElement;
}
};
this.show = function (completion) {
debug('VideoContainer show');
plugin._.removeAttribute('width');
plugin._.removeAttribute('height');
plugin.setStream(stream, completion);
OT.$.show(plugin._);
};
this.setWidth = function (width) {
debug('VideoContainer setWidth to ' + width);
plugin._.setAttribute('width', width);
};
this.setHeight = function (height) {
debug('VideoContainer setHeight to ' + height);
plugin._.setAttribute('height', height);
};
this.setVolume = function (value) {
// TODO
debug('VideoContainer setVolume not implemented: called with ' + value);
};
this.getVolume = function () {
// TODO
debug('VideoContainer getVolume not implemented');
return 0.5;
};
this.getImgData = function () {
return plugin._.getImgData('image/png');
};
this.getVideoWidth = function () {
return plugin._.videoWidth;
};
this.getVideoHeight = function () {
return plugin._.videoHeight;
};
this.destroy = function () {
plugin._.setStream(null);
plugin.removeRef(this);
};
};
// tb_require('./header.js')
// tb_require('./shims.js')
// tb_require('./plugin_object.js')
/* jshint globalstrict: true, strict: false, undef: true, unused: true,
trailing: true, browser: true, smarttabs:true */
/* global OT:true, TBPlugin:true, pluginInfo:true, ActiveXObject:true,
injectObject:true, curryCallAsync:true */
/* exported AutoUpdater:true */
var AutoUpdater;
(function() {
var autoUpdaterController,
updaterMimeType, // <- cached version, use getInstallerMimeType instead
installedVersion = -1; // <- cached version, use getInstallerMimeType instead
var versionGreaterThan = function versionGreaterThan (version1,version2) {
if (version1 === version2) return false;
var v1 = version1.split('.'),
v2 = version2.split('.');
v1 = parseFloat(parseInt(v1.shift(), 10) + '.' +
v1.map(function(vcomp) { return parseInt(vcomp, 10); }).join(''));
v2 = parseFloat(parseInt(v2.shift(), 10) + '.' +
v2.map(function(vcomp) { return parseInt(vcomp, 10); }).join(''));
return v1 > v2;
};
// Work out the full mimeType (including the currently installed version)
// of the installer.
var findMimeTypeAndVersion = function findMimeTypeAndVersion () {
if (updaterMimeType !== void 0) {
return updaterMimeType;
}
var activeXControlId = 'TokBox.otiePluginInstaller',
unversionedMimeType = 'application/x-otieplugininstaller',
plugin = navigator.plugins[activeXControlId];
installedVersion = -1;
if (plugin) {
// Look through the supported mime-types for the version
// There should only be one mime-type in our use case, and
// if there's more than one they should all have the same
// version.
var numMimeTypes = plugin.length,
extractVersion = new RegExp(unversionedMimeType.replace('-', '\\-') +
',version=([0-9]+)', 'i'),
mimeType,
bits;
for (var i=0; i' + errorMsg + (helpMsg ? ' ' : '') + '
'; OT.$.addClass(container, classNames || 'OT_subscriber_error'); if(container.querySelector('p').offsetHeight > container.offsetHeight) { container.querySelector('span').style.display = 'none'; } }; }; })(window); // Web OT Helpers !(function(window) { var NativeRTCPeerConnection = (window.webkitRTCPeerConnection || window.mozRTCPeerConnection); if (navigator.webkitGetUserMedia) { /*global webkitMediaStream, webkitRTCPeerConnection*/ // Stub for getVideoTracks for Chrome < 26 if (!webkitMediaStream.prototype.getVideoTracks) { webkitMediaStream.prototype.getVideoTracks = function() { return this.videoTracks; }; } // Stubs for getAudioTracks for Chrome < 26 if (!webkitMediaStream.prototype.getAudioTracks) { webkitMediaStream.prototype.getAudioTracks = function() { return this.audioTracks; }; } if (!webkitRTCPeerConnection.prototype.getLocalStreams) { webkitRTCPeerConnection.prototype.getLocalStreams = function() { return this.localStreams; }; } if (!webkitRTCPeerConnection.prototype.getRemoteStreams) { webkitRTCPeerConnection.prototype.getRemoteStreams = function() { return this.remoteStreams; }; } } else if (navigator.mozGetUserMedia) { // Firefox < 23 doesn't support get Video/Audio tracks, we'll just stub them out for now. /* global MediaStream */ if (!MediaStream.prototype.getVideoTracks) { MediaStream.prototype.getVideoTracks = function() { return []; }; } if (!MediaStream.prototype.getAudioTracks) { MediaStream.prototype.getAudioTracks = function() { return []; }; } // This won't work as mozRTCPeerConnection is a weird internal Firefox // object (a wrapped native object I think). // if (!window.mozRTCPeerConnection.prototype.getLocalStreams) { // window.mozRTCPeerConnection.prototype.getLocalStreams = function() { // return this.localStreams; // }; // } // This won't work as mozRTCPeerConnection is a weird internal Firefox // object (a wrapped native object I think). // if (!window.mozRTCPeerConnection.prototype.getRemoteStreams) { // window.mozRTCPeerConnection.prototype.getRemoteStreams = function() { // return this.remoteStreams; // }; // } } // The setEnabled method on MediaStreamTracks is a TBPlugin // construct. In this particular instance it's easier to bring // all the good browsers down to IE's level than bootstrap it up. if (typeof window.MediaStreamTrack !== 'undefined') { if (!window.MediaStreamTrack.prototype.setEnabled) { window.MediaStreamTrack.prototype.setEnabled = function (enabled) { this.enabled = OT.$.castToBoolean(enabled); }; } } OT.$.createPeerConnection = function (config, options, publishersWebRtcStream, completion) { if (TBPlugin.isInstalled()) { TBPlugin.initPeerConnection(config, options, publishersWebRtcStream, completion); } else { var pc; try { pc = new NativeRTCPeerConnection(config, options); } catch(e) { completion(e.message); return; } completion(null, pc); } }; // Returns a String representing the supported WebRTC crypto scheme. The possible // values are SDES_SRTP, DTLS_SRTP, and NONE; // // Broadly: // * Firefox only supports DTLS // * Older versions of Chrome (<= 24) only support SDES // * Newer versions of Chrome (>= 25) support DTLS and SDES // OT.$.supportedCryptoScheme = function() { if (!OT.$.hasCapabilities('webrtc')) return 'NONE'; var chromeVersion = window.navigator.userAgent.toLowerCase().match(/chrome\/([0-9\.]+)/i); return chromeVersion && parseFloat(chromeVersion[1], 10) < 25 ? 'SDES_SRTP' : 'DTLS_SRTP'; }; })(window); // Web OT Helpers !(function(window) { /* jshint globalstrict: true, strict: false, undef: true, unused: true, trailing: true, browser: true, smarttabs:true */ /* global TBPlugin, OT */ /// // Capabilities // // Support functions to query browser/client Media capabilities. // // Indicates whether this client supports the getUserMedia // API. // OT.$.registerCapability('getUserMedia', function() { return !!(navigator.webkitGetUserMedia || navigator.mozGetUserMedia || TBPlugin.isInstalled()); }); // TODO Remove all PeerConnection stuff, that belongs to the messaging layer not the Media layer. // Indicates whether this client supports the PeerConnection // API. // // Chrome Issues: // * The explicit prototype.addStream check is because webkitRTCPeerConnection was // partially implemented, but not functional, in Chrome 22. // // Firefox Issues: // * No real support before Firefox 19 // * Firefox 19 has issues with generating Offers. // * Firefox 20 doesn't interoperate with Chrome. // OT.$.registerCapability('PeerConnection', function() { var browser = OT.$.browserVersion(); if (navigator.webkitGetUserMedia) { return typeof(window.webkitRTCPeerConnection) === 'function' && !!window.webkitRTCPeerConnection.prototype.addStream; } else if (navigator.mozGetUserMedia) { if (typeof(window.mozRTCPeerConnection) === 'function' && browser.version > 20.0) { try { new window.mozRTCPeerConnection(); return true; } catch (err) { return false; } } } else { return TBPlugin.isInstalled(); } }); // Indicates whether this client supports WebRTC // // This is defined as: getUserMedia + PeerConnection + exceeds min browser version // OT.$.registerCapability('webrtc', function() { var browser = OT.$.browserVersion(), minimumVersions = OT.properties.minimumVersion || {}, minimumVersion = minimumVersions[browser.browser.toLowerCase()]; if(minimumVersion && minimumVersion > browser.version) { OT.debug('Support for', browser.browser, 'is disabled because we require', minimumVersion, 'but this is', browser.version); return false; } return OT.$.hasCapabilities('getUserMedia', 'PeerConnection'); }); // TODO Remove all transport stuff, that belongs to the messaging layer not the Media layer. // Indicates if the browser supports bundle // // Broadly: // * Firefox doesn't support bundle // * Chrome support bundle // * OT Plugin supports bundle // OT.$.registerCapability('bundle', function() { return OT.$.hasCapabilities('webrtc') && (OT.$.browser() === 'Chrome' || TBPlugin.isInstalled()); }); // Indicates if the browser supports rtcp mux // // Broadly: // * Older versions of Firefox (<= 25) don't support rtcp mux // * Older versions of Firefox (>= 26) support rtcp mux (not tested yet) // * Chrome support rtcp mux // * OT Plugin supports rtcp mux // OT.$.registerCapability('RTCPMux', function() { return OT.$.hasCapabilities('webrtc') && (OT.$.browser() === 'Chrome' || TBPlugin.isInstalled()); }); // Indicates whether this browser supports the getMediaDevices (getSources) API. // OT.$.registerCapability('getMediaDevices', function() { return OT.$.isFunction(window.MediaStreamTrack) && OT.$.isFunction(window.MediaStreamTrack.getSources); }); })(window); // Web OT Helpers !(function() { var nativeGetUserMedia, vendorToW3CErrors, gumNamesToMessages, mapVendorErrorName, parseErrorEvent, areInvalidConstraints; // Handy cross-browser getUserMedia shim. Inspired by some code from Adam Barth nativeGetUserMedia = (function() { if (navigator.getUserMedia) { return OT.$.bind(navigator.getUserMedia, navigator); } else if (navigator.mozGetUserMedia) { return OT.$.bind(navigator.mozGetUserMedia, navigator); } else if (navigator.webkitGetUserMedia) { return OT.$.bind(navigator.webkitGetUserMedia, navigator); } else if (TBPlugin.isInstalled()) { return OT.$.bind(TBPlugin.getUserMedia, TBPlugin); } })(); // Mozilla error strings and the equivalent W3C names. NOT_SUPPORTED_ERROR does not // exist in the spec right now, so we'll include Mozilla's error description. // Chrome TrackStartError is triggered when the camera is already used by another app (Windows) vendorToW3CErrors = { PERMISSION_DENIED: 'PermissionDeniedError', NOT_SUPPORTED_ERROR: 'NotSupportedError', MANDATORY_UNSATISFIED_ERROR: ' ConstraintNotSatisfiedError', NO_DEVICES_FOUND: 'NoDevicesFoundError', HARDWARE_UNAVAILABLE: 'HardwareUnavailableError', TrackStartError: 'HardwareUnavailableError' }; gumNamesToMessages = { PermissionDeniedError: 'End-user denied permission to hardware devices', PermissionDismissedError: 'End-user dismissed permission to hardware devices', NotSupportedError: 'A constraint specified is not supported by the browser.', ConstraintNotSatisfiedError: 'It\'s not possible to satisfy one or more constraints ' + 'passed into the getUserMedia function', OverconstrainedError: 'Due to changes in the environment, one or more mandatory ' + 'constraints can no longer be satisfied.', NoDevicesFoundError: 'No voice or video input devices are available on this machine.', HardwareUnavailableError: 'The selected voice or video devices are unavailable. Verify ' + 'that the chosen devices are not in use by another application.' }; // Map vendor error strings to names and messages if possible mapVendorErrorName = function mapVendorErrorName(vendorErrorName, vendorErrors) { var errorName, errorMessage; if(vendorErrors.hasOwnProperty(vendorErrorName)) { errorName = vendorErrors[vendorErrorName]; } else { // This doesn't map to a known error from the Media Capture spec, it's // probably a custom vendor error message. errorName = vendorErrorName; } if(gumNamesToMessages.hasOwnProperty(errorName)) { errorMessage = gumNamesToMessages[errorName]; } else { errorMessage = 'Unknown Error while getting user media'; } return { name: errorName, message: errorMessage }; }; // Parse and normalise a getUserMedia error event from Chrome or Mozilla // @ref http://dev.w3.org/2011/webrtc/editor/getusermedia.html#idl-def-NavigatorUserMediaError parseErrorEvent = function parseErrorObject(event) { var error; if (OT.$.isObject(event) && event.name) { error = mapVendorErrorName(event.name, vendorToW3CErrors); error.constraintName = event.constraintName; } else if (typeof event === 'string') { error = mapVendorErrorName(event, vendorToW3CErrors); } else { error = { message: 'Unknown Error type while getting media' }; } return error; }; // Validates a Hash of getUserMedia constraints. Currently we only // check to see if there is at least one non-false constraint. areInvalidConstraints = function(constraints) { if (!constraints || !OT.$.isObject(constraints)) return true; for (var key in constraints) { if(!constraints.hasOwnProperty(key)) { continue; } if (constraints[key]) return false; } return true; }; // A wrapper for the builtin navigator.getUserMedia. In addition to the usual // getUserMedia behaviour, this helper method also accepts a accessDialogOpened // and accessDialogClosed callback. // // @memberof OT.$ // @private // // @param {Object} constraints // A dictionary of constraints to pass to getUserMedia. See // http://dev.w3.org/2011/webrtc/editor/getusermedia.html#idl-def-MediaStreamConstraints // in the Media Capture and Streams spec for more info. // // @param {function} success // Called when getUserMedia completes successfully. The callback will be passed a WebRTC // Stream object. // // @param {function} failure // Called when getUserMedia fails to access a user stream. It will be passed an object // with a code property representing the error that occurred. // // @param {function} accessDialogOpened // Called when the access allow/deny dialog is opened. // // @param {function} accessDialogClosed // Called when the access allow/deny dialog is closed. // // @param {function} accessDenied // Called when access is denied to the camera/mic. This will be either because // the user has clicked deny or because a particular origin is permanently denied. // OT.$.getUserMedia = function(constraints, success, failure, accessDialogOpened, accessDialogClosed, accessDenied, customGetUserMedia) { var getUserMedia = nativeGetUserMedia; if(OT.$.isFunction(customGetUserMedia)) { getUserMedia = customGetUserMedia; } // All constraints are false, we don't allow this. This may be valid later // depending on how/if we integrate data channels. if (areInvalidConstraints(constraints)) { OT.error('Couldn\'t get UserMedia: All constraints were false'); // Using a ugly dummy-code for now. failure.call(null, { name: 'NO_VALID_CONSTRAINTS', message: 'Video and Audio was disabled, you need to enabled at least one' }); return; } var triggerOpenedTimer = null, displayedPermissionDialog = false, finaliseAccessDialog = function() { if (triggerOpenedTimer) { clearTimeout(triggerOpenedTimer); } if (displayedPermissionDialog && accessDialogClosed) accessDialogClosed(); }, triggerOpened = function() { triggerOpenedTimer = null; displayedPermissionDialog = true; if (accessDialogOpened) accessDialogOpened(); }, onStream = function(stream) { finaliseAccessDialog(); success.call(null, stream); }, onError = function(event) { finaliseAccessDialog(); var error = parseErrorEvent(event); // The error name 'PERMISSION_DENIED' is from an earlier version of the spec if (error.name === 'PermissionDeniedError' || error.name === 'PermissionDismissedError') { accessDenied.call(null, error); } else { failure.call(null, error); } }; try { getUserMedia(constraints, onStream, onError); } catch (e) { OT.error('Couldn\'t get UserMedia: ' + e.toString()); onError(); return; } // The 'remember me' functionality of WebRTC only functions over HTTPS, if // we aren't on HTTPS then we should definitely be displaying the access // dialog. // // If we are on HTTPS, we'll wait 500ms to see if we get a stream // immediately. If we do then the user had clicked 'remember me'. Otherwise // we assume that the accessAllowed dialog is visible. // // @todo benchmark and see if 500ms is a reasonable number. It seems like // we should know a lot quicker. // if (location.protocol.indexOf('https') === -1) { // Execute after, this gives the client a chance to bind to the // accessDialogOpened event. triggerOpenedTimer = setTimeout(triggerOpened, 100); } else { // wait a second and then trigger accessDialogOpened triggerOpenedTimer = setTimeout(triggerOpened, 500); } }; })(); // Web OT Helpers !(function(window) { /* jshint globalstrict: true, strict: false, undef: true, unused: true, trailing: true, browser: true, smarttabs:true */ /* global OT */ /// // Device Helpers // // Support functions to enumerating and guerying device info // var chromeToW3CDeviceKinds = { audio: 'audioInput', video: 'videoInput' }; OT.$.shouldAskForDevices = function(callback) { var MST = window.MediaStreamTrack; if(MST != null && OT.$.isFunction(MST.getSources)) { window.MediaStreamTrack.getSources(function(sources) { var hasAudio = sources.some(function(src) { return src.kind === 'audio'; }); var hasVideo = sources.some(function(src) { return src.kind === 'video'; }); callback.call(null, { video: hasVideo, audio: hasAudio }); }); } else { // This environment can't enumerate devices anyway, so we'll memorise this result. OT.$.shouldAskForDevices = function(callback) { setTimeout(OT.$.bind(callback, null, { video: true, audio: true })); }; OT.$.shouldAskForDevices(callback); } }; OT.$.getMediaDevices = function(callback) { if(OT.$.hasCapabilities('getMediaDevices')) { window.MediaStreamTrack.getSources(function(sources) { var filteredSources = OT.$.filter(sources, function(source) { return chromeToW3CDeviceKinds[source.kind] != null; }); callback(void 0, OT.$.map(filteredSources, function(source) { return { deviceId: source.id, label: source.label, kind: chromeToW3CDeviceKinds[source.kind] }; })); }); } else { callback(new Error('This browser does not support getMediaDevices APIs')); } }; })(window); (function(window) { var VideoOrientationTransforms = { 0: 'rotate(0deg)', 270: 'rotate(90deg)', 90: 'rotate(-90deg)', 180: 'rotate(180deg)' }; OT.VideoOrientation = { ROTATED_NORMAL: 0, ROTATED_LEFT: 270, ROTATED_RIGHT: 90, ROTATED_UPSIDE_DOWN: 180 }; var DefaultAudioVolume = 50; var DEGREE_TO_RADIANS = Math.PI * 2 / 360; // // // var _videoElement = new OT.VideoElement({ // fallbackText: 'blah' // }, errorHandler); // // _videoElement.bindToStream(webRtcStream, completion); // => VideoElement // _videoElement.appendTo(DOMElement) // => VideoElement // // _videoElement.domElement // => DomNode // // _videoElement.imgData // => PNG Data string // // _videoElement.orientation = OT.VideoOrientation.ROTATED_LEFT; // // _videoElement.unbindStream(); // _videoElement.destroy() // => Completely cleans up and // removes the video element // // OT.VideoElement = function(/* optional */ options/*, optional errorHandler*/) { var _options = OT.$.defaults( options && !OT.$.isFunction(options) ? options : {}, { fallbackText: 'Sorry, Web RTC is not available in your browser' }), errorHandler = OT.$.isFunction(arguments[arguments.length-1]) ? arguments[arguments.length-1] : void 0, orientationHandler = OT.$.bind(function(orientation) { this.trigger('orientationChanged', orientation); }, this), _videoElement = TBPlugin.isInstalled() ? new PluginVideoElement(_options, errorHandler, orientationHandler) : new NativeDOMVideoElement(_options, errorHandler, orientationHandler), _streamBound = false, _stream, _preInitialisedVolue; OT.$.eventing(this); // Public Properties OT.$.defineProperties(this, { domElement: { get: function() { return _videoElement.domElement(); } }, videoWidth: { get: function() { return _videoElement['video' + (this.isRotated() ? 'Height' : 'Width')](); } }, videoHeight: { get: function() { return _videoElement['video' + (this.isRotated() ? 'Width' : 'Height')](); } }, aspectRatio: { get: function() { return (this.videoWidth() + 0.0) / this.videoHeight(); } }, isRotated: { get: function() { return _videoElement.isRotated(); } }, orientation: { get: function() { return _videoElement.orientation(); }, set: function(orientation) { _videoElement.orientation(orientation); } }, audioChannelType: { get: function() { return _videoElement.audioChannelType(); }, set: function(type) { _videoElement.audioChannelType(type); } } }); // Public Methods this.imgData = function() { return _videoElement.imgData(); }; this.appendTo = function(parentDomElement) { _videoElement.appendTo(parentDomElement); return this; }; this.bindToStream = function(webRtcStream, completion) { _streamBound = false; _stream = webRtcStream; _videoElement.bindToStream(webRtcStream, OT.$.bind(function(err) { if (err) { completion(err); return; } _streamBound = true; if (_preInitialisedVolue) { this.setAudioVolume(_preInitialisedVolue); _preInitialisedVolue = null; } completion(null); }, this)); return this; }; this.unbindStream = function() { if (!_stream) return this; _stream = null; _videoElement.unbindStream(); return this; }; this.setAudioVolume = function (value) { if (_streamBound) _videoElement.setAudioVolume( OT.$.roundFloat(value / 100, 2) ); else _preInitialisedVolue = value; return this; }; this.getAudioVolume = function () { if (_streamBound) return parseInt(_videoElement.getAudioVolume() * 100, 10); else return _preInitialisedVolue || 50; }; this.whenTimeIncrements = function (callback, context) { _videoElement.whenTimeIncrements(callback, context); return this; }; this.destroy = function () { // unbind all events so they don't fire after the object is dead this.off(); _videoElement.destroy(); return void 0; }; }; var PluginVideoElement = function PluginVideoElement (options, errorHandler, orientationChangedHandler) { var _videoProxy, _parentDomElement; canBeOrientatedMixin(this, function() { return _videoProxy.domElement; }, orientationChangedHandler); /// Public methods this.domElement = function() { return _videoProxy ? _videoProxy.domElement : void 0; }; this.videoWidth = function() { return _videoProxy ? _videoProxy.getVideoWidth() : void 0; }; this.videoHeight = function() { return _videoProxy ? _videoProxy.getVideoHeight() : void 0; }; this.imgData = function() { return _videoProxy ? _videoProxy.getImgData() : null; }; // Append the Video DOM element to a parent node this.appendTo = function(parentDomElement) { _parentDomElement = parentDomElement; return this; }; // Bind a stream to the video element. this.bindToStream = function(webRtcStream, completion) { if (!_parentDomElement) { completion('The VideoElement must attached to a DOM node before a stream can be bound'); return; } _videoProxy = webRtcStream._.render(); _videoProxy.appendTo(_parentDomElement); _videoProxy.show(completion); return this; }; // Unbind the currently bound stream from the video element. this.unbindStream = function() { // TODO: some way to tell TBPlugin to release that stream and controller if (_videoProxy) { _videoProxy.destroy(); _parentDomElement = null; _videoProxy = null; } return this; }; this.setAudioVolume = function(value) { if (_videoProxy) _videoProxy.setVolume(value); }; this.getAudioVolume = function() { // Return the actual volume of the DOM element if (_videoProxy) return _videoProxy.getVolume(); return DefaultAudioVolume; }; // see https://wiki.mozilla.org/WebAPI/AudioChannels // The audioChannelType is not currently supported in the plugin. this.audioChannelType = function(/* type */) { return 'unknown'; }; this.whenTimeIncrements = function(callback, context) { // exists for compatibility with NativeVideoElement OT.$.callAsync(OT.$.bind(callback, context)); }; this.destroy = function() { this.unbindStream(); return void 0; }; }; var NativeDOMVideoElement = function NativeDOMVideoElement (options, errorHandler, orientationChangedHandler) { var _domElement, _videoElementMovedWarning = false; /// Private API var _onVideoError = OT.$.bind(function(event) { var reason = 'There was an unexpected problem with the Video Stream: ' + videoElementErrorCodeToStr(event.target.error.code); errorHandler(reason, this, 'VideoElement'); }, this), // The video element pauses itself when it's reparented, this is // unfortunate. This function plays the video again and is triggered // on the pause event. _playVideoOnPause = function() { if(!_videoElementMovedWarning) { OT.warn('Video element paused, auto-resuming. If you intended to do this, ' + 'use publishVideo(false) or subscribeToVideo(false) instead.'); _videoElementMovedWarning = true; } _domElement.play(); }; _domElement = createNativeVideoElement(options.fallbackText, options.attributes); _domElement.addEventListener('pause', _playVideoOnPause); canBeOrientatedMixin(this, function() { return _domElement; }, orientationChangedHandler); /// Public methods this.domElement = function() { return _domElement; }; this.videoWidth = function() { return _domElement.videoWidth; }; this.videoHeight = function() { return _domElement.videoHeight; }; this.imgData = function() { var canvas = OT.$.createElement('canvas', { width: _domElement.videoWidth, height: _domElement.videoHeight, style: { display: 'none' } }); document.body.appendChild(canvas); try { canvas.getContext('2d').drawImage(_domElement, 0, 0, canvas.width, canvas.height); } catch(err) { OT.warn('Cannot get image data yet'); return null; } var imgData = canvas.toDataURL('image/png'); OT.$.removeElement(canvas); return OT.$.trim(imgData.replace('data:image/png;base64,', '')); }; // Append the Video DOM element to a parent node this.appendTo = function(parentDomElement) { parentDomElement.appendChild(_domElement); return this; }; // Bind a stream to the video element. this.bindToStream = function(webRtcStream, completion) { bindStreamToNativeVideoElement(_domElement, webRtcStream, function(err) { if (err) { completion(err); return; } _domElement.addEventListener('error', _onVideoError, false); completion(null); }); return this; }; // Unbind the currently bound stream from the video element. this.unbindStream = function() { if (_domElement) { unbindNativeStream(_domElement); } return this; }; this.setAudioVolume = function(value) { if (_domElement) _domElement.volume = value; }; this.getAudioVolume = function() { // Return the actual volume of the DOM element if (_domElement) return _domElement.volume; return DefaultAudioVolume; }; // see https://wiki.mozilla.org/WebAPI/AudioChannels // The audioChannelType is currently only available in Firefox. This property returns // "unknown" in other browser. The related HTML tag attribute is "mozaudiochannel" this.audioChannelType = function(type) { if (type !== void 0) { _domElement.mozAudioChannelType = type; } if ('mozAudioChannelType' in _domElement) { return _domElement.mozAudioChannelType; } else { return 'unknown'; } }; this.whenTimeIncrements = function(callback, context) { if(_domElement) { var lastTime, handler; handler = OT.$.bind(function() { if(!lastTime || lastTime >= _domElement.currentTime) { lastTime = _domElement.currentTime; } else { _domElement.removeEventListener('timeupdate', handler, false); callback.call(context, this); } }, this); _domElement.addEventListener('timeupdate', handler, false); } }; this.destroy = function() { this.unbindStream(); if (_domElement) { // Unbind this first, otherwise it will trigger when the // video element is removed from the DOM. _domElement.removeEventListener('pause', _playVideoOnPause); OT.$.removeElement(_domElement); _domElement = null; } return void 0; }; }; /// Private Helper functions // A mixin to create the orientation API implementation on +self+ // +getDomElementCallback+ is a function that the mixin will call when it wants to // get the native Dom element for +self+. // // +initialOrientation+ sets the initial orientation (shockingly), it's currently unused // so the initial value is actually undefined. // var canBeOrientatedMixin = function canBeOrientatedMixin (self, getDomElementCallback, orientationChangedHandler, initialOrientation) { var _orientation = initialOrientation; OT.$.defineProperties(self, { isRotated: { get: function() { return this.orientation() && (this.orientation().videoOrientation === 270 || this.orientation().videoOrientation === 90); } }, orientation: { get: function() { return _orientation; }, set: function(orientation) { _orientation = orientation; var transform = VideoOrientationTransforms[orientation.videoOrientation] || VideoOrientationTransforms.ROTATED_NORMAL; switch(OT.$.browser()) { case 'Chrome': case 'Safari': getDomElementCallback().style.webkitTransform = transform; break; case 'IE': if (OT.$.browserVersion().version >= 9) { getDomElementCallback().style.msTransform = transform; } else { // So this basically defines matrix that represents a rotation // of a single vector in a 2d basis. // // R = [cos(Theta) -sin(Theta)] // [sin(Theta) cos(Theta)] // // Where Theta is the number of radians to rotate by // // Then to rotate the vector v: // v' = Rv // // We then use IE8 Matrix filter property, which takes // a 2x2 rotation matrix, to rotate our DOM element. // var radians = orientation.videoOrientation * DEGREE_TO_RADIANS, element = getDomElementCallback(), costheta = Math.cos(radians), sintheta = Math.sin(radians); // element.filters.item(0).M11 = costheta; // element.filters.item(0).M12 = -sintheta; // element.filters.item(0).M21 = sintheta; // element.filters.item(0).M22 = costheta; element.style.filter = 'progid:DXImageTransform.Microsoft.Matrix(' + 'M11='+costheta+',' + 'M12='+(-sintheta)+',' + 'M21='+sintheta+',' + 'M22='+costheta+',SizingMethod=\'auto expand\')'; } break; default: // The standard version, just Firefox, Opera, and IE > 9 getDomElementCallback().style.transform = transform; } orientationChangedHandler(_orientation); } }, // see https://wiki.mozilla.org/WebAPI/AudioChannels // The audioChannelType is currently only available in Firefox. This property returns // "unknown" in other browser. The related HTML tag attribute is "mozaudiochannel" audioChannelType: { get: function() { if ('mozAudioChannelType' in this.domElement) { return this.domElement.mozAudioChannelType; } else { return 'unknown'; } }, set: function(type) { if ('mozAudioChannelType' in this.domElement) { this.domElement.mozAudioChannelType = type; } } } }); }; function createNativeVideoElement(fallbackText, attributes) { var videoElement = document.createElement('video'); videoElement.setAttribute('autoplay', ''); videoElement.innerHTML = fallbackText; if (attributes) { if (attributes.muted === true) { delete attributes.muted; videoElement.muted = 'true'; } for (var key in attributes) { if(!attributes.hasOwnProperty(key)) { continue; } videoElement.setAttribute(key, attributes[key]); } } return videoElement; } // See http://www.w3.org/TR/2010/WD-html5-20101019/video.html#error-codes var _videoErrorCodes = {}; // Checking for window.MediaError for IE compatibility, just so we don't throw // exceptions when the script is included if (window.MediaError) { _videoErrorCodes[window.MediaError.MEDIA_ERR_ABORTED] = 'The fetching process for the media ' + 'resource was aborted by the user agent at the user\'s request.'; _videoErrorCodes[window.MediaError.MEDIA_ERR_NETWORK] = 'A network error of some description ' + 'caused the user agent to stop fetching the media resource, after the resource was ' + 'established to be usable.'; _videoErrorCodes[window.MediaError.MEDIA_ERR_DECODE] = 'An error of some description ' + 'occurred while decoding the media resource, after the resource was established to be ' + ' usable.'; _videoErrorCodes[window.MediaError.MEDIA_ERR_SRC_NOT_SUPPORTED] = 'The media resource ' + 'indicated by the src attribute was not suitable.'; } function videoElementErrorCodeToStr(errorCode) { return _videoErrorCodes[parseInt(errorCode, 10)] || 'An unknown error occurred.'; } function bindStreamToNativeVideoElement(videoElement, webRtcStream, completion) { var cleanup, onLoad, onError, onStoppedLoading, timeout; // Note: onloadedmetadata doesn't fire in Chrome for audio only crbug.com/110938 // After version 36 it will fire if the video track is disabled. var browser = OT.$.browserVersion(), needsDisabledAudioProtection = browser.browser === 'Chrome' && browser.version < 36; if (navigator.mozGetUserMedia || !(needsDisabledAudioProtection && (webRtcStream.getVideoTracks().length > 0 && webRtcStream.getVideoTracks()[0].enabled))) { cleanup = function cleanup () { clearTimeout(timeout); videoElement.removeEventListener('loadedmetadata', onLoad, false); videoElement.removeEventListener('error', onError, false); webRtcStream.onended = null; }; onLoad = function onLoad () { cleanup(); completion(null); }; onError = function onError (event) { cleanup(); unbindNativeStream(videoElement); completion('There was an unexpected problem with the Video Stream: ' + videoElementErrorCodeToStr(event.target.error.code)); }; onStoppedLoading = function onStoppedLoading () { // The stream ended before we fully bound it. Maybe the other end called // stop on it or something else went wrong. cleanup(); unbindNativeStream(videoElement); completion('Stream ended while trying to bind it to a video element.'); }; // Timeout if it takes too long timeout = setTimeout(OT.$.bind(function() { if (videoElement.currentTime === 0) { cleanup(); completion('The video stream failed to connect. Please notify the site ' + 'owner if this continues to happen.'); } else if (webRtcStream.ended === true) { // The ended event should have fired by here, but support for it isn't // always so awesome. onStoppedLoading(); } else { OT.warn('Never got the loadedmetadata event but currentTime > 0'); onLoad(null); } }, this), 30000); videoElement.addEventListener('loadedmetadata', onLoad, false); videoElement.addEventListener('error', onError, false); webRtcStream.onended = onStoppedLoading; } else { OT.$.callAsync(completion, null); } // The official spec way is 'srcObject', we are slowly converging there. if (videoElement.srcObject !== void 0) { videoElement.srcObject = webRtcStream; } else if (videoElement.mozSrcObject !== void 0) { videoElement.mozSrcObject = webRtcStream; } else { videoElement.src = window.URL.createObjectURL(webRtcStream); } videoElement.play(); } function unbindNativeStream(videoElement) { if (videoElement.srcObject !== void 0) { videoElement.srcObject = null; } else if (videoElement.mozSrcObject !== void 0) { videoElement.mozSrcObject = null; } else { window.URL.revokeObjectURL(videoElement.src); } } })(window); // tb_require('../helpers/helpers.js') !(function() { /* jshint globalstrict: true, strict: false, undef: true, unused: true, trailing: true, browser: true, smarttabs:true */ /* global OT */ var currentGuidStorage, currentGuid; var isInvalidStorage = function isInvalidStorage (storageInterface) { return !(OT.$.isFunction(storageInterface.get) && OT.$.isFunction(storageInterface.set)); }; var getClientGuid = function getClientGuid (completion) { if (currentGuid) { completion(null, currentGuid); return; } // It's the first time that getClientGuid has been called // in this page lifetime. Attempt to load any existing Guid // from the storage currentGuidStorage.get(completion); }; OT.overrideGuidStorage = function (storageInterface) { if (isInvalidStorage(storageInterface)) { throw new Error('The storageInterface argument does not seem to be valid, ' + 'it must implement get and set methods'); } if (currentGuidStorage === storageInterface) { return; } currentGuidStorage = storageInterface; // If a client Guid has already been assigned to this client then // let the new storage know about it so that it's in sync. if (currentGuid) { currentGuidStorage.set(currentGuid, function(error) { if (error) { OT.error('Failed to send initial Guid value (' + currentGuid + ') to the newly assigned Guid Storage. The error was: ' + error); // @todo error } }); } }; if (!OT._) OT._ = {}; OT._.getClientGuid = function (completion) { getClientGuid(function(error, guid) { if (error) { completion(error); return; } if (!guid) { // Nothing came back, this client is entirely new. // generate a new Guid and persist it guid = OT.$.uuid(); currentGuidStorage.set(guid, function(error) { if (error) { completion(error); return; } currentGuid = guid; }); } else if (!currentGuid) { currentGuid = guid; } completion(null, currentGuid); }); }; // Implement our default storage mechanism, which sets/gets a cookie // called 'opentok_client_id' OT.overrideGuidStorage({ get: function(completion) { completion(null, OT.$.getCookie('opentok_client_id')); }, set: function(guid, completion) { OT.$.setCookie('opentok_client_id', guid); completion(null); } }); })(window); !(function(window) { // Singleton interval var logQueue = [], queueRunning = false; OT.Analytics = function() { var endPoint = OT.properties.loggingURL + '/logging/ClientEvent', endPointQos = OT.properties.loggingURL + '/logging/ClientQos', reportedErrors = {}, // Map of camel-cased keys to underscored camelCasedKeys, browser = OT.$.browserVersion(), send = function(data, isQos, callback) { OT.$.post((isQos ? endPointQos : endPoint) + '?_=' + OT.$.uuid.v4(), { body: data, xdomainrequest: (browser.browser === 'IE' & browser.version < 10), headers: { 'Content-Type': 'application/x-www-form-urlencoded' } }, callback); }, throttledPost = function() { // Throttle logs so that they only happen 1 at a time if (!queueRunning && logQueue.length > 0) { queueRunning = true; var curr = logQueue[0]; // Remove the current item and send the next log var processNextItem = function() { logQueue.shift(); queueRunning = false; throttledPost(); }; if (curr) { send(curr.data, curr.isQos, function(err) { if(err) { OT.debug('Failed to send ClientEvent, moving on to the next item.'); // There was an error, move onto the next item } else { curr.onComplete(); } setTimeout(processNextItem, 50); }); } } }, post = function(data, onComplete, isQos) { logQueue.push({ data: data, onComplete: onComplete, isQos: isQos }); throttledPost(); }, shouldThrottleError = function(code, type, partnerId) { if (!partnerId) return false; var errKey = [partnerId, type, code].join('_'), //msgLimit = DynamicConfig.get('exceptionLogging', 'messageLimitPerPartner', partnerId); msgLimit = 100; if (msgLimit === null || msgLimit === undefined) return false; return (reportedErrors[errKey] || 0) <= msgLimit; }; camelCasedKeys = { payloadType: 'payload_type', partnerId: 'partner_id', streamId: 'stream_id', sessionId: 'session_id', connectionId: 'connection_id', widgetType: 'widget_type', widgetId: 'widget_id', avgAudioBitrate: 'avg_audio_bitrate', avgVideoBitrate: 'avg_video_bitrate', localCandidateType: 'local_candidate_type', remoteCandidateType: 'remote_candidate_type', transportType: 'transport_type' }; // Log an error via ClientEvents. // // @param [String] code // @param [String] type // @param [String] message // @param [Hash] details additional error details // // @param [Hash] options the options to log the client event with. // @option options [String] action The name of the Event that we are logging. E.g. // 'TokShowLoaded'. Required. // @option options [String] variation Usually used for Split A/B testing, when you // have multiple variations of the +_action+. // @option options [String] payloadType A text description of the payload. Required. // @option options [String] payload The payload. Required. // @option options [String] sessionId The active OpenTok session, if there is one // @option options [String] connectionId The active OpenTok connectionId, if there is one // @option options [String] partnerId // @option options [String] guid ... // @option options [String] widgetId ... // @option options [String] streamId ... // @option options [String] section ... // @option options [String] build ... // // Reports will be throttled to X reports (see exceptionLogging.messageLimitPerPartner // from the dynamic config for X) of each error type for each partner. Reports can be // disabled/enabled globally or on a per partner basis (per partner settings // take precedence) using exceptionLogging.enabled. // this.logError = function(code, type, message, details, options) { if (!options) options = {}; var partnerId = options.partnerId; if (OT.Config.get('exceptionLogging', 'enabled', partnerId) !== true) { return; } if (shouldThrottleError(code, type, partnerId)) { //OT.log('ClientEvents.error has throttled an error of type ' + type + '.' + // code + ' for partner ' + (partnerId || 'No Partner Id')); return; } var errKey = [partnerId, type, code].join('_'), payload = this.escapePayload(OT.$.extend(details || {}, { message: payload, userAgent: OT.$.userAgent() })); reportedErrors[errKey] = typeof(reportedErrors[errKey]) !== 'undefined' ? reportedErrors[errKey] + 1 : 1; return this.logEvent(OT.$.extend(options, { action: type + '.' + code, payloadType: payload[0], payload: payload[1] })); }; // Log a client event to the analytics backend. // // @example Logs a client event called 'foo' // OT.ClientEvents.log({ // action: 'foo', // payload_type: 'foo's payload', // payload: 'bar', // session_id: sessionId, // connection_id: connectionId // }) // // @param [Hash] options the options to log the client event with. // @option options [String] action The name of the Event that we are logging. // E.g. 'TokShowLoaded'. Required. // @option options [String] variation Usually used for Split A/B testing, when // you have multiple variations of the +_action+. // @option options [String] payloadType A text description of the payload. Required. // @option options [String] payload The payload. Required. // @option options [String] session_id The active OpenTok session, if there is one // @option options [String] connection_id The active OpenTok connectionId, if there is one // @option options [String] partner_id // @option options [String] guid ... // @option options [String] widget_id ... // @option options [String] stream_id ... // @option options [String] section ... // @option options [String] build ... // this.logEvent = function(options) { var partnerId = options.partnerId; if (!options) options = {}; OT._.getClientGuid(function(error, guid) { if (error) { // @todo return; } // Set a bunch of defaults var data = OT.$.extend({ 'variation' : '', 'guid' : guid, 'widget_id' : '', 'session_id': '', 'connection_id': '', 'stream_id' : '', 'partner_id' : partnerId, 'source' : window.location.href, 'section' : '', 'build' : '' }, options), onComplete = function(){ // OT.log('logged: ' + '{action: ' + data['action'] + ', variation: ' + data['variation'] // + ', payload_type: ' + data['payload_type'] + ', payload: ' + data['payload'] + '}'); }; // We camel-case our names, but the ClientEvents backend wants them // underscored... for (var key in camelCasedKeys) { if (camelCasedKeys.hasOwnProperty(key) && data[key]) { data[camelCasedKeys[key]] = data[key]; delete data[key]; } } post(data, onComplete, false); }); }; // Log a client QOS to the analytics backend. // this.logQOS = function(options) { var partnerId = options.partnerId; if (!options) options = {}; OT._.getClientGuid(function(error, guid) { if (error) { // @todo return; } // Set a bunch of defaults var data = OT.$.extend({ 'guid' : guid, 'widget_id' : '', 'session_id': '', 'connection_id': '', 'stream_id' : '', 'partner_id' : partnerId, 'source' : window.location.href, 'build' : '', 'duration' : 0 //in milliseconds }, options), onComplete = function(){ // OT.log('logged: ' + '{action: ' + data['action'] + ', variation: ' + data['variation'] // + ', payload_type: ' + data['payload_type'] + ', payload: ' + data['payload'] + '}'); }; // We camel-case our names, but the ClientEvents backend wants them // underscored... for (var key in camelCasedKeys) { if (camelCasedKeys.hasOwnProperty(key)) { if(data[key]) { data[camelCasedKeys[key]] = data[key]; } delete data[key]; } } post(data, onComplete, true); }); }; // Converts +payload+ to two pipe seperated strings. Doesn't currently handle // edgecases, e.g. escaping '\\|' will break stuff. // // *Note:* It strip any keys that have null values. this.escapePayload = function(payload) { var escapedPayload = [], escapedPayloadDesc = []; for (var key in payload) { if (payload.hasOwnProperty(key) && payload[key] !== null && payload[key] !== undefined) { escapedPayload.push( payload[key] ? payload[key].toString().replace('|', '\\|') : '' ); escapedPayloadDesc.push( key.toString().replace('|', '\\|') ); } } return [ escapedPayloadDesc.join('|'), escapedPayload.join('|') ]; }; }; })(window); !(function() { OT.$.registerCapability('audioOutputLevelStat', function() { return OT.$.browserVersion().browser === 'Chrome'; }); OT.$.registerCapability('webAudioCapableRemoteStream', function() { return OT.$.browserVersion().browser === 'Firefox'; }); OT.$.registerCapability('getStatsWithSingleParameter', function() { return OT.$.browserVersion().browser === 'Chrome'; }); OT.$.registerCapability('webAudio', function() { return 'AudioContext' in window; }); })(); !(function(window) { // This is not obvious, so to prevent end-user frustration we'll let them know // explicitly rather than failing with a bunch of permission errors. We don't // handle this using an OT Exception as it's really only a development thing. if (location.protocol === 'file:') { /*global alert*/ alert('You cannot test a page using WebRTC through the file system due to browser ' + 'permissions. You must run it over a web server.'); } if (!window.OT) window.OT = {}; if (!window.URL && window.webkitURL) { window.URL = window.webkitURL; } var _analytics = new OT.Analytics(); var // Global parameters used by upgradeSystemRequirements _intervalId, _lastHash = document.location.hash; /** * The first step in using the OpenTok API is to call theOT.initSession()
* method. Other methods of the OT object check for system requirements and set up error logging.
*
* @class OT
*/
/**
* * Initializes and returns the local session object for a specified session ID. *
*
* You connect to an OpenTok session using the connect() method
* of the Session object returned by the OT.initSession() method.
* Note that calling OT.initSession() does not initiate communications
* with the cloud. It simply initializes the Session object that you can use to
* connect (and to perform other operations once connected).
*
* For an example, see Session.connect(). *
* * @method OT.initSession * @memberof OT * @param {String} apiKey Your OpenTok API key (see the * OpenTok dashboard). * @param {String} sessionId The session ID identifying the OpenTok session. For more * information, see Session creation. * @returns {Session} The session object through which all further interactions with * the session will occur. */ OT.initSession = function(apiKey, sessionId) { if(sessionId == null) { sessionId = apiKey; apiKey = null; } var session = OT.sessions.get(sessionId); if (!session) { session = new OT.Session(apiKey, sessionId); OT.sessions.add(session); } return session; }; /** *
* Initializes and returns a Publisher object. You can then pass this Publisher
* object to Session.publish() to publish a stream to a session.
*
* Note: If you intend to reuse a Publisher object created using
* OT.initPublisher() to publish to different sessions sequentially,
* call either Session.disconnect() or Session.unpublish().
* Do not call both. Then call the preventDefault() method of the
* streamDestroyed or sessionDisconnected event object to prevent the
* Publisher object from being removed from the page.
*
id attribute of the
* existing DOM element used to determine the location of the Publisher video in the HTML DOM. See
* the insertMode property of the properties parameter. If you do not
* specify a targetElement, the application appends a new DOM element to the HTML
* body.
*
*
* The application throws an error if an element with an ID set to the
* targetElement value does not exist in the HTML DOM.
*
OT.initPublisher() fails with an
* error (error code 1500, "Unable to Publish") passed to the completion handler function.
* If the publisher specifies a frame rate, the actual frame rate of the video stream
* is set as the frameRate property of the Stream object, though the actual frame rate
* will vary based on changing network and system conditions. If the developer does not specify a
* frame rate, this property is undefined.
*
* For sessions that use the OpenTok Media Router (sessions with * the media mode * set to routed, lowering the frame rate or lowering the resolution reduces * the maximum bandwidth the stream can use. However, in sessions with the media mode set to * relayed, lowering the frame rate or resolution may not reduce the stream's bandwidth. *
*
* You can also restrict the frame rate of a Subscriber's video stream. To restrict the frame rate
* a Subscriber, call the restrictFrameRate() method of the subscriber, passing in
* true.
* (See Subscriber.restrictFrameRate().)
*
height and width properties to set the dimensions
* of the publisher video; do not set the height and width of the DOM element
* (using CSS).
* targetElement parameter. This string can
* have the following values:
* "replace" The Publisher object replaces contents of the
* targetElement. This is the default."after" The Publisher object is a new element inserted after
* the targetElement in the HTML DOM. (Both the Publisher and targetElement have the
* same parent element.)"before" The Publisher object is a new element inserted before
* the targetElement in the HTML DOM. (Both the Publisher and targetElement have the same
* parent element.)"append" The Publisher object is a new element added as a child
* of the targetElement. If there are other child elements, the Publisher is appended as
* the last child element of the targetElement.true
* (the video image is mirrored). This property does not affect the display
* on other subscribers' web pages.
* true). This setting applies when you pass
* the Publisher object in a call to the Session.publish() method.
* true). This setting applies when you pass
* the Publisher object in a call to the Session.publish() method.
* "widthxheight", where the width and height are represented in
* pixels. Valid values are "1280x720", "640x480", and
* "320x240". The published video will only use the desired resolution if the
* client configuration supports it.
*
* The requested resolution of a video stream is set as the videoDimensions.width and
* videoDimensions.height properties of the Stream object.
*
* The default resolution for a stream (if you do not specify a resolution) is 640x480 pixels. * If the client system cannot support the resolution you requested, the the stream will use the * next largest setting supported. *
** For sessions that use the OpenTok Media Router (sessions with the * media mode * set to routed, lowering the frame rate or lowering the resolution reduces the maximum bandwidth * the stream can use. However, in sessions that have the media mode set to relayed, lowering the * frame rate or resolution may not reduce the stream's bandwidth. *
*style object includes
* the following properties:
* audioLevelDisplayMode (String) — How to display the audio level
* indicator. Possible values are: "auto" (the indicator is displayed when the
* video is disabled), "off" (the indicator is not displayed), and
* "on" (the indicator is always displayed).backgroundImageURI (String) — A URI for an image to display as
* the background image when a video is not displayed. (A video may not be displayed if
* you call publishVideo(false) on the Publisher object). You can pass an http
* or https URI to a PNG, JPEG, or non-animated GIF file location. You can also use the
* data URI scheme (instead of http or https) and pass in base-64-encrypted
* PNG data, such as that obtained from the
* Publisher.getImgData() method. For example,
* you could set the property to "data:VBORw0KGgoAA...", where the portion of the
* string after "data:" is the result of a call to
* Publisher.getImgData(). If the URL or the image data is invalid, the property
* is ignored (the attempt to set the image fails silently).
*
* Note that in Internet Explorer 8 (using the OpenTok Plugin for Internet Explorer),
* you cannot set the backgroundImageURI style to a string larger than 32 kB.
* This is due to an IE 8 limitation on the size of URI strings. Due to this limitation,
* you cannot set the backgroundImageURI style to a string obtained with the
* getImgData() method.
*
buttonDisplayMode (String) — How to display the microphone controls
* Possible values are: "auto" (controls are displayed when the stream is first
* displayed and when the user mouses over the display), "off" (controls are not
* displayed), and "on" (controls are always displayed).nameDisplayMode (String) Whether to display the stream name.
* Possible values are: "auto" (the name is displayed when the stream is first
* displayed and when the user mouses over the display), "off" (the name is not
* displayed), and "on" (the name is always displayed).OT.initPublisher() fails with an
* error (error code 1500, "Unable to Publish") passed to the completion handler function.
* height and width properties to set the dimensions
* of the publisher video; do not set the height and width of the DOM element
* (using CSS).
* error. On success, the error object is set to null. On
* failure, the error object has two properties: code (an integer) and
* message (a string), which identify the cause of the failure. The method succeeds
* when the user grants access to the camera and microphone. The method fails if the user denies
* access to the camera and microphone. The completionHandler function is called
* before the Publisher dispatches an accessAllowed (success) event or an
* accessDenied (failure) event.
*
* The following code adds a completionHandler when calling the
* OT.initPublisher() method:
*
* var publisher = OT.initPublisher('publisher', null, function (error) {
* if (error) {
* console.log(error);
* } else {
* console.log("Publisher initialized.");
* }
* });
*
*
* @returns {Publisher} The Publisher object.
* @see for audio input
* devices or "videoInput" for video input devices.
*
* The deviceId property is a unique ID for the device. You can pass
* the deviceId in as the audioSource or videoSource
* property of the the options parameter of the
* OT.initPublisher() method.
*
* The label property identifies the device. The label
* property is set to an empty string if the user has not previously granted access to
* a camera and microphone. In HTTP, the user must have granted access to a camera and
* microphone in the current page (for example, in response to a call to
* OT.initPublisher()). In HTTPS, the user must have previously granted access
* to the camera and microphone in the current page or in a page previously loaded from the
* domain.
*
*
* @see OT.initPublisher()
* @method OT.getDevices
* @memberof OT
*/
OT.getDevices = function(callback) {
OT.$.getMediaDevices(callback);
};
/**
* Checks if the system supports OpenTok for WebRTC.
* @return {Number} Whether the system supports OpenTok for WebRTC (1) or not (0).
* @see OT.upgradeSystemRequirements()
* @method OT.checkSystemRequirements
* @memberof OT
*/
OT.checkSystemRequirements = function() {
OT.debug('OT.checkSystemRequirements()');
// Try native support first, then TBPlugin...
var systemRequirementsMet = OT.$.hasCapabilities('websockets', 'webrtc') ||
TBPlugin.isInstalled();
systemRequirementsMet = systemRequirementsMet ?
this.HAS_REQUIREMENTS : this.NOT_HAS_REQUIREMENTS;
OT.checkSystemRequirements = function() {
OT.debug('OT.checkSystemRequirements()');
return systemRequirementsMet;
};
if(systemRequirementsMet === this.NOT_HAS_REQUIREMENTS) {
_analytics.logEvent({
action: 'checkSystemRequirements',
variation: 'notHasRequirements',
'payload_type': 'userAgent',
'partner_id': OT.APIKEY,
payload: OT.$.userAgent()
});
}
return systemRequirementsMet;
};
/**
* Displays information about system requirments for OpenTok for WebRTC. This
* information is displayed in an iframe element that fills the browser window.
*
* Note: this information is displayed automatically when you call the
* OT.initSession() or the OT.initPublisher() method
* if the client does not support OpenTok for WebRTC.
*
* Registers a method as an event listener for a specific event. *
* *
* The OT object dispatches one type of event an exception event. The
* following code adds an event listener for the exception event:
*
* OT.addEventListener("exception", exceptionHandler);
*
* function exceptionHandler(event) {
* alert("exception event. \n code == " + event.code + "\n message == " + event.message);
* }
*
*
* * If a handler is not registered for an event, the event is ignored locally. If the event * listener function does not exist, the event is ignored locally. *
*
* Throws an exception if the listener name is invalid.
*
* Removes an event listener for a specific event. *
* *
* Throws an exception if the listener name is invalid.
*
* The OT object dispatches one type of event an exception event. The following
* code adds an event
* listener for the exception event:
*
* OT.on("exception", function (event) {
* // This is the event handler.
* });
*
*
* You can also pass in a third context parameter (which is optional) to define the
* value of
* this in the handler method:
* OT.on("exception",
* function (event) {
* // This is the event handler.
* }),
* session
* );
*
*
* * If you do not add a handler for an event, the event is ignored locally. *
* * @param {String} type The string identifying the type of event. * @param {Function} handler The handler function to process the event. This function takes the event * object as a parameter. * @param {Object} context (Optional) Defines the value ofthis in the event handler
* function.
*
* @memberof OT
* @method on
* @see off()
* @see once()
* @see Events
*/
/**
* Adds an event handler function for an event. Once the handler is called, the specified handler
* method is
* removed as a handler for this event. (When you use the OT.on() method to add an event
* handler, the handler
* is not removed when it is called.) The OT.once() method is the equivilent of
* calling the OT.on()
* method and calling OT.off() the first time the handler is invoked.
*
*
* The following code adds a one-time event handler for the exception event:
*
* OT.once("exception", function (event) {
* console.log(event);
* }
*
*
* You can also pass in a third context parameter (which is optional) to define the
* value of
* this in the handler method:
* OT.once("exception",
* function (event) {
* // This is the event handler.
* },
* session
* );
*
*
* * The method also supports an alternate syntax, in which the first parameter is an object that is a * hash map of * event names and handler functions and the second parameter (optional) is the context for this in * each handler: *
*
* OT.once(
* {exeption: function (event) {
* // This is the event handler.
* }
* },
* session
* );
*
*
* @param {String} type The string identifying the type of event. You can specify multiple event
* names in this string,
* separating them with a space. The event handler will process the first occurence of the events.
* After the first event,
* the handler is removed (for all specified events).
* @param {Function} handler The handler function to process the event. This function takes the event
* object as a parameter.
* @param {Object} context (Optional) Defines the value of this in the event handler
* function.
*
* @memberof OT
* @method once
* @see on()
* @see once()
* @see Events
*/
/**
* Removes an event handler.
*
* Pass in an event name and a handler method, the handler is removed for that event:
* *OT.off("exceptionEvent", exceptionEventHandler);
*
* If you pass in an event name and no handler method, all handlers are removed for that * events:
* *OT.off("exceptionEvent");
*
* * The method also supports an alternate syntax, in which the first parameter is an object that is a * hash map of * event names and handler functions and the second parameter (optional) is the context for matching * handlers: *
*
* OT.off(
* {
* exceptionEvent: exceptionEventHandler
* },
* this
* );
*
*
* @param {String} type (Optional) The string identifying the type of event. You can use a space to
* specify multiple events, as in "eventName1 eventName2 eventName3". If you pass in no
* type value (or other arguments), all event handlers are removed for the object.
* @param {Function} handler (Optional) The event handler function to remove. If you pass in no
* handler, all event handlers are removed for the specified event type.
* @param {Object} context (Optional) If you specify a context, the event handler is
* removed for all specified events and handlers that use the specified context.
*
* @memberof OT
* @method off
* @see on()
* @see once()
* @see Events
*/
/**
* Dispatched by the OT class when the app encounters an exception.
* Note that you set up an event handler for the exception event by calling the
* OT.on() method.
*
* @name exception
* @event
* @borrows ExceptionEvent#message as this.message
* @memberof OT
* @see ExceptionEvent
*/
if (!window.OT) window.OT = OT;
if (!window.TB) window.TB = OT;
})(window);
!(function() {
OT.Collection = function(idField) {
var _models = [],
_byId = {},
_idField = idField || 'id';
OT.$.eventing(this, true);
var modelProperty = function(model, property) {
if(OT.$.isFunction(model[property])) {
return model[property]();
} else {
return model[property];
}
};
var onModelUpdate = OT.$.bind(function onModelUpdate (event) {
this.trigger('update', event);
this.trigger('update:'+event.target.id, event);
}, this),
onModelDestroy = OT.$.bind(function onModelDestroyed (event) {
this.remove(event.target, event.reason);
}, this);
this.reset = function() {
// Stop listening on the models, they are no longer our problem
OT.$.forEach(_models, function(model) {
model.off('updated', onModelUpdate, this);
model.off('destroyed', onModelDestroy, this);
}, this);
_models = [];
_byId = {};
};
this.destroy = function(reason) {
OT.$.forEach(_models, function(model) {
if(model && typeof model.destroy === 'function') {
model.destroy(reason, true);
}
});
this.reset();
this.off();
};
this.get = function(id) { return id && _byId[id] !== void 0 ? _models[_byId[id]] : void 0; };
this.has = function(id) { return id && _byId[id] !== void 0; };
this.toString = function() { return _models.toString(); };
// Return only models filtered by either a dict of properties
// or a filter function.
//
// @example Return all publishers with a streamId of 1
// OT.publishers.where({streamId: 1})
//
// @example The same thing but filtering using a filter function
// OT.publishers.where(function(publisher) {
// return publisher.stream.id === 4;
// });
//
// @example The same thing but filtering using a filter function
// executed with a specific this
// OT.publishers.where(function(publisher) {
// return publisher.stream.id === 4;
// }, self);
//
this.where = function(attrsOrFilterFn, context) {
if (OT.$.isFunction(attrsOrFilterFn)) return OT.$.filter(_models, attrsOrFilterFn, context);
return OT.$.filter(_models, function(model) {
for (var key in attrsOrFilterFn) {
if(!attrsOrFilterFn.hasOwnProperty(key)) {
continue;
}
if (modelProperty(model, key) !== attrsOrFilterFn[key]) return false;
}
return true;
});
};
// Similar to where in behaviour, except that it only returns
// the first match.
this.find = function(attrsOrFilterFn, context) {
var filterFn;
if (OT.$.isFunction(attrsOrFilterFn)) {
filterFn = attrsOrFilterFn;
}
else {
filterFn = function(model) {
for (var key in attrsOrFilterFn) {
if(!attrsOrFilterFn.hasOwnProperty(key)) {
continue;
}
if (modelProperty(model, key) !== attrsOrFilterFn[key]) return false;
}
return true;
};
}
filterFn = OT.$.bind(filterFn, context);
for (var i=0; i<_models.length; ++i) {
if (filterFn(_models[i]) === true) return _models[i];
}
return null;
};
this.add = function(model) {
var id = modelProperty(model, _idField);
if (this.has(id)) {
OT.warn('Model ' + id + ' is already in the collection', _models);
return this;
}
_byId[id] = _models.push(model) - 1;
model.on('updated', onModelUpdate, this);
model.on('destroyed', onModelDestroy, this);
this.trigger('add', model);
this.trigger('add:'+id, model);
return this;
};
this.remove = function(model, reason) {
var id = modelProperty(model, _idField);
_models.splice(_byId[id], 1);
// Shuffle everyone down one
for (var i=_byId[id]; i<_models.length; ++i) {
_byId[_models[i][_idField]] = i;
}
delete _byId[id];
model.off('updated', onModelUpdate, this);
model.off('destroyed', onModelDestroy, this);
this.trigger('remove', model, reason);
this.trigger('remove:'+id, model, reason);
return this;
};
// Used by session connecto fire add events after adding listeners
this._triggerAddEvents = function() {
var models = this.where.apply(this, arguments);
OT.$.forEach(models, function(model) {
this.trigger('add', model);
this.trigger('add:' + modelProperty(model, _idField), model);
}, this);
};
this.length = function() {
return _models.length;
};
};
}(this));
!(function() {
/**
* The Event object defines the basic OpenTok event object that is passed to
* event listeners. Other OpenTok event classes implement the properties and methods of
* the Event object.
*
* For example, the Stream object dispatches a streamPropertyChanged event when
* the stream's properties are updated. You add a callback for an event using the
* on() method of the Stream object:
* stream.on("streamPropertyChanged", function (event) {
* alert("Properties changed for stream " + event.target.streamId);
* });
*
* @class Event
* @property {Boolean} cancelable Whether the event has a default behavior that is cancelable
* (true) or not (false). You can cancel the default behavior by
* calling the preventDefault() method of the Event object in the callback
* function. (See preventDefault().)
*
* @property {Object} target The object that dispatched the event.
*
* @property {String} type The type of event.
*/
OT.Event = OT.$.eventing.Event();
/**
* Prevents the default behavior associated with the event from taking place.
*
* To see whether an event has a default behavior, check the cancelable property
* of the event object.
Call the preventDefault() method in the callback function for the event.
The following events have default behaviors:
* *sessionDisconnect See
*
* SessionDisconnectEvent.preventDefault().streamDestroyed See
* StreamEvent.preventDefault().accessDialogOpened See the
* accessDialogOpened event.accessDenied See the
* accessDenied event.preventDefault() (true) or not (false).
* See preventDefault().
* @method #isDefaultPrevented
* @return {Boolean}
* @memberof Event
*/
// Event names lookup
OT.Event.names = {
// Activity Status for cams/mics
ACTIVE: 'active',
INACTIVE: 'inactive',
UNKNOWN: 'unknown',
// Archive types
PER_SESSION: 'perSession',
PER_STREAM: 'perStream',
// OT Events
EXCEPTION: 'exception',
ISSUE_REPORTED: 'issueReported',
// Session Events
SESSION_CONNECTED: 'sessionConnected',
SESSION_DISCONNECTED: 'sessionDisconnected',
STREAM_CREATED: 'streamCreated',
STREAM_DESTROYED: 'streamDestroyed',
CONNECTION_CREATED: 'connectionCreated',
CONNECTION_DESTROYED: 'connectionDestroyed',
SIGNAL: 'signal',
STREAM_PROPERTY_CHANGED: 'streamPropertyChanged',
MICROPHONE_LEVEL_CHANGED: 'microphoneLevelChanged',
// Publisher Events
RESIZE: 'resize',
SETTINGS_BUTTON_CLICK: 'settingsButtonClick',
DEVICE_INACTIVE: 'deviceInactive',
INVALID_DEVICE_NAME: 'invalidDeviceName',
ACCESS_ALLOWED: 'accessAllowed',
ACCESS_DENIED: 'accessDenied',
ACCESS_DIALOG_OPENED: 'accessDialogOpened',
ACCESS_DIALOG_CLOSED: 'accessDialogClosed',
ECHO_CANCELLATION_MODE_CHANGED: 'echoCancellationModeChanged',
PUBLISHER_DESTROYED: 'destroyed',
// Subscriber Events
SUBSCRIBER_DESTROYED: 'destroyed',
// DeviceManager Events
DEVICES_DETECTED: 'devicesDetected',
// DevicePanel Events
DEVICES_SELECTED: 'devicesSelected',
CLOSE_BUTTON_CLICK: 'closeButtonClick',
MICLEVEL : 'microphoneActivityLevel',
MICGAINCHANGED : 'microphoneGainChanged',
// Environment Loader
ENV_LOADED: 'envLoaded',
ENV_UNLOADED: 'envUnloaded',
// Audio activity Events
AUDIO_LEVEL_UPDATED: 'audioLevelUpdated'
};
OT.ExceptionCodes = {
JS_EXCEPTION: 2000,
AUTHENTICATION_ERROR: 1004,
INVALID_SESSION_ID: 1005,
CONNECT_FAILED: 1006,
CONNECT_REJECTED: 1007,
CONNECTION_TIMEOUT: 1008,
NOT_CONNECTED: 1010,
P2P_CONNECTION_FAILED: 1013,
API_RESPONSE_FAILURE: 1014,
UNABLE_TO_PUBLISH: 1500,
UNABLE_TO_SUBSCRIBE: 1501,
UNABLE_TO_FORCE_DISCONNECT: 1520,
UNABLE_TO_FORCE_UNPUBLISH: 1530
};
/**
* The {@link OT} class dispatches exception events when the OpenTok API encounters
* an exception (error). The ExceptionEvent object defines the properties of the event
* object that is dispatched.
*
* Note that you set up a callback for the exception event by calling the
* OT.on() method.
| * code * * | ** title * | *
| * 1004 * * | ** Authentication error * | *
| * 1005 * * | ** Invalid Session ID * | *
| * 1006 * * | ** Connect Failed * | *
| * 1007 * * | ** Connect Rejected * | *
| * 1008 * * | ** Connect Time-out * | *
| * 1009 * * | ** Security Error * | *
| * 1010 * * | ** Not Connected * | *
| * 1011 * * | ** Invalid Parameter * | *
| * 1013 * | ** Connection Failed * | *
| * 1014 * | ** API Response Failure * | *
| * 1500 * | ** Unable to Publish * | *
| * 1520 * | ** Unable to Force Disconnect * | *
| * 1530 * | ** Unable to Force Unpublish * | *
| * 1535 * | ** Force Unpublish on Invalid Stream * | *
| * 2000 * * | ** Internal Error * | *
| * 2010 * * | ** Report Issue Failure * | *
Check the message property for more details about the error.
exception event, this will be an object other than the OT object
* (such as a Session object or a Publisher object).
*
* @property {String} title The error title.
* @augments Event
*/
OT.ExceptionEvent = function (type, message, title, code, component, target) {
OT.Event.call(this, type);
this.message = message;
this.title = title;
this.code = code;
this.component = component;
this.target = target;
};
OT.IssueReportedEvent = function (type, issueId) {
OT.Event.call(this, type);
this.issueId = issueId;
};
// Triggered when the JS dynamic config and the DOM have loaded.
OT.EnvLoadedEvent = function (type) {
OT.Event.call(this, type);
};
/**
* Dispatched by the Session object when a client connects to or disconnects from a {@link Session}.
* For the local client, the Session object dispatches a "sessionConnected" or "sessionDisconnected"
* event, defined by the {@link SessionConnectEvent} and {@link SessionDisconnectEvent} classes.
*
* The following code keeps a running total of the number of connections to a session
* by monitoring the connections property of the sessionConnect,
* connectionCreated and connectionDestroyed events:
var apiKey = ""; // Replace with your API key. See https://dashboard.tokbox.com/projects
* var sessionID = ""; // Replace with your own session ID.
* // See https://dashboard.tokbox.com/projects
* var token = ""; // Replace with a generated token that has been assigned the moderator role.
* // See https://dashboard.tokbox.com/projects
* var connectionCount = 0;
*
* var session = OT.initSession(apiKey, sessionID);
* session.on("connectionCreated", function(event) {
* connectionCount++;
* displayConnectionCount();
* });
* session.on("connectionDestroyed", function(event) {
* connectionCount--;
* displayConnectionCount();
* });
* session.connect(token);
*
* function displayConnectionCount() {
* document.getElementById("connectionCountField").value = connectionCount.toString();
* }
*
* This example assumes that there is an input text field in the HTML DOM
* with the id set to "connectionCountField":
<input type="text" id="connectionCountField" value="0"></input>* * * @property {Connection} connection A Connection objects for the connections that was * created or deleted. * * @property {Array} connections Deprecated. Use the
connection property. A
* connectionCreated or connectionDestroyed event is dispatched
* for each connection created and destroyed in the session.
*
* @property {String} reason For a connectionDestroyed event,
* a description of why the connection ended. This property can have two values:
*
* "clientDisconnected" A client disconnected from the session by calling
* the disconnect() method of the Session object or by closing the browser.
* (See Session.disconnect().)"forceDisconnected" A moderator has disconnected the publisher
* from the session, by calling the forceDisconnect() method of the Session
* object. (See Session.forceDisconnect().)"networkDisconnected" The network connection terminated abruptly
* (for example, the client lost their internet connection).Depending on the context, this description may allow the developer to refine * the course of action they take in response to an event.
* *For a connectionCreated event, this string is undefined.
The following code initializes a session and sets up an event listener for when * a stream published by another client is created:
* *
* session.on("streamCreated", function(event) {
* // streamContainer is a DOM element
* subscriber = session.subscribe(event.stream, targetElement);
* }).connect(token);
*
*
* The following code initializes a session and sets up an event listener for when * other clients' streams end:
* *
* session.on("streamDestroyed", function(event) {
* console.log("Stream " + event.stream.name + " ended. " + event.reason);
* }).connect(token);
*
*
* The following code publishes a stream and adds an event listener for when the streaming * starts
* *
* var publisher = session.publish(targetElement)
* .on("streamCreated", function(event) {
* console.log("Publisher started streaming.");
* );
*
*
* The following code publishes a stream, and leaves the Publisher in the HTML DOM * when the streaming stops:
* *
* var publisher = session.publish(targetElement)
* .on("streamDestroyed", function(event) {
* event.preventDefault();
* console.log("Publisher stopped streaming.");
* );
*
*
* @class StreamEvent
*
* @property {Boolean} cancelable Whether the event has a default behavior that is cancelable
* (true) or not (false). You can cancel the default behavior by calling
* the preventDefault() method of the StreamEvent object in the event listener
* function. The streamDestroyed
* event is cancelable. (See preventDefault().)
*
* @property {String} reason For a streamDestroyed event,
* a description of why the session disconnected. This property can have one of the following
* values:
*
* "clientDisconnected" A client disconnected from the session by calling
* the disconnect() method of the Session object or by closing the browser.
* (See Session.disconnect().)"forceDisconnected" A moderator has disconnected the publisher of the
* stream from the session, by calling the forceDisconnect() method of the Session
* object. (See Session.forceDisconnect().)"forceUnpublished" A moderator has forced the publisher of the stream
* to stop publishing the stream, by calling the forceUnpublish() method of the
* Session object. (See Session.forceUnpublish().)"networkDisconnected" The network connection terminated abruptly (for
* example, the client lost their internet connection).Depending on the context, this description may allow the developer to refine * the course of action they take in response to an event.
* *For a streamCreated event, this string is undefined.
streamCreated event) or deleted (in the case of a
* streamDestroyed event).
*
* @property {Array} streams Deprecated. Use the stream property. A
* streamCreated or streamDestroyed event is dispatched for
* each stream added or destroyed.
*
* @augments Event
*/
var streamEventPluralDeprecationWarningShown = false;
OT.StreamEvent = function (type, stream, reason, cancelable) {
OT.Event.call(this, type, cancelable);
if (OT.$.canDefineProperty) {
Object.defineProperty(this, 'streams', {
get: function() {
if(!streamEventPluralDeprecationWarningShown) {
OT.warn('OT.StreamEvent streams property is deprecated, use stream instead.');
streamEventPluralDeprecationWarningShown = true;
}
return [stream];
}
});
} else {
this.streams = [stream];
}
this.stream = stream;
this.reason = reason;
};
/**
* Prevents the default behavior associated with the event from taking place.
*
* For the streamDestroyed event dispatched by the Session object,
* the default behavior is that all Subscriber objects that are subscribed to the stream are
* unsubscribed and removed from the HTML DOM. Each Subscriber object dispatches a
* destroyed event when the element is removed from the HTML DOM. If you call the
* preventDefault() method in the event listener for the streamDestroyed
* event, the default behavior is prevented and you can clean up Subscriber objects using your
* own code. See
* Session.getSubscribersForStream().
* For the streamDestroyed event dispatched by a Publisher object, the default
* behavior is that the Publisher object is removed from the HTML DOM. The Publisher object
* dispatches a destroyed event when the element is removed from the HTML DOM.
* If you call the preventDefault() method in the event listener for the
* streamDestroyed event, the default behavior is prevented, and you can
* retain the Publisher for reuse or clean it up using your own code.
*
To see whether an event has a default behavior, check the cancelable property of
* the event object.
Call the preventDefault() method in the event listener function for the event.
connect() method of the Session object.
*
* In version 2.2, the completionHandler of the Session.connect() method
* indicates success or failure in connecting to the session.
*
* @class SessionConnectEvent
* @property {Array} connections Deprecated in version 2.2 (and set to an empty array). In
* version 2.2, listen for the connectionCreated event dispatched by the Session
* object. In version 2.2, the Session object dispatches a connectionCreated event
* for each connection (including your own). This includes connections present when you first
* connect to the session.
*
* @property {Array} streams Deprecated in version 2.2 (and set to an empty array). In version
* 2.2, listen for the streamCreated event dispatched by the Session object. In
* version 2.2, the Session object dispatches a streamCreated event for each stream
* other than those published by your client. This includes streams
* present when you first connect to the session.
*
* @see Session.connect()
disconnect() method of the session object.
*
* * The following code initializes a session and sets up an event listener for when a session is * disconnected. *
*var apiKey = ""; // Replace with your API key. See https://dashboard.tokbox.com/projects
* var sessionID = ""; // Replace with your own session ID.
* // See https://dashboard.tokbox.com/projects
* var token = ""; // Replace with a generated token that has been assigned the moderator role.
* // See https://dashboard.tokbox.com/projects
*
* var session = OT.initSession(apiKey, sessionID);
* session.on("sessionDisconnected", function(event) {
* alert("The session disconnected. " + event.reason);
* });
* session.connect(token);
*
*
* @property {String} reason A description of why the session disconnected.
* This property can have two values:
*
* "clientDisconnected" A client disconnected from the session by calling
* the disconnect() method of the Session object or by closing the browser.
* ( See Session.disconnect().)"forceDisconnected" A moderator has disconnected you from the session
* by calling the forceDisconnect() method of the Session object. (See
* Session.forceDisconnect().)"networkDisconnected" The network connection terminated abruptly
* (for example, the client lost their internet connection).For the sessionDisconnectEvent, the default behavior is that all Subscriber
* objects are unsubscribed and removed from the HTML DOM. Each Subscriber object dispatches a
* destroyed event when the element is removed from the HTML DOM. If you call the
* preventDefault() method in the event listener for the sessionDisconnect
* event, the default behavior is prevented, and you can, optionally, clean up Subscriber objects
* using your own code).
*
*
To see whether an event has a default behavior, check the cancelable property of
* the event object.
Call the preventDefault() method in the event listener function for the event.
streamPropertyChanged event in the
* following circumstances:
*
* hasAudio or hasVideo property of the Stream object to
* change. This change results from a call to the publishAudio() or
* publishVideo() methods of the Publish object.videoDimensions property of a stream changes. For more information,
* see Stream.videoDimensions."hasAudio", "hasVideo", or "videoDimensions".
* @property {Object} newValue The new value of the property (after the change).
* @property {Object} oldValue The old value of the property (before the change).
* @property {Stream} stream The Stream object for which a property has changed.
*
* @see Publisher.publishAudio()
* @see Publisher.publishVideo()
* @see Stream.videoDimensions
* @augments Event
*/
OT.StreamPropertyChangedEvent = function (type, stream, changedProperty, oldValue, newValue) {
OT.Event.call(this, type, false);
this.type = type;
this.stream = stream;
this.changedProperty = changedProperty;
this.oldValue = oldValue;
this.newValue = newValue;
};
/**
* Defines event objects for the archiveStarted and archiveStopped events.
* The Session object dispatches these events when an archive recording of the session starts and
* stops.
*
* @property {String} id The archive ID.
* @property {String} name The name of the archive. You can assign an archive a name when you create
* it, using the OpenTok REST API or one of the
* OpenTok server SDKs.
*
* @class ArchiveEvent
* @augments Event
*/
OT.ArchiveEvent = function (type, archive) {
OT.Event.call(this, type, false);
this.type = type;
this.id = archive.id;
this.name = archive.name;
this.status = archive.status;
this.archive = archive;
};
OT.ArchiveUpdatedEvent = function (stream, key, oldValue, newValue) {
OT.Event.call(this, 'updated', false);
this.target = stream;
this.changedProperty = key;
this.oldValue = oldValue;
this.newValue = newValue;
};
/**
* The Session object dispatches a signal event when the client receives a signal from the session.
*
* @class SignalEvent
* @property {String} type The type assigned to the signal (if there is one). Use the type to
* filter signals received (by adding an event handler for signal:type1 or signal:type2, etc.)
* @property {String} data The data string sent with the signal (if there is one).
* @property {Connection} from The Connection corresponding to the client that sent with the signal.
*
* @see Session.signal()
* @see Session events (signal and signal:type)
* @augments Event
*/
OT.SignalEvent = function(type, data, from) {
OT.Event.call(this, type ? 'signal:' + type : OT.Event.names.SIGNAL, false);
this.data = data;
this.from = from;
};
OT.StreamUpdatedEvent = function (stream, key, oldValue, newValue) {
OT.Event.call(this, 'updated', false);
this.target = stream;
this.changedProperty = key;
this.oldValue = oldValue;
this.newValue = newValue;
};
OT.DestroyedEvent = function(type, target, reason) {
OT.Event.call(this, type, false);
this.target = target;
this.reason = reason;
};
/**
* Defines the event object for the videoDisabled and videoEnabled events
* dispatched by the Subscriber.
*
* @class VideoEnabledChangedEvent
*
* @property {Boolean} cancelable Whether the event has a default behavior that is cancelable
* (true) or not (false). You can cancel the default behavior by
* calling the preventDefault() method of the event object in the callback
* function. (See preventDefault().)
*
* @property {String} reason The reason the video was disabled or enabled. This can be set to one of
* the following values:
*
* "publishVideo" — The publisher started or stopped publishing video,
* by calling publishVideo(true) or publishVideo(false)."quality" — The OpenTok Media Router starts or stops sending video
* to the subscriber based on stream quality changes. This feature of the OpenTok Media
* Router has a subscriber drop the video stream when connectivity degrades. (The subscriber
* continues to receive the audio stream, if there is one.)
*
* If connectivity improves to support video again, the Subscriber object dispatches
* a videoEnabled event, and the Subscriber resumes receiving video.
*
* By default, the Subscriber displays a video disabled indicator when a
* videoDisabled event with this reason is dispatched and removes the indicator
* when the videoDisabled event with this reason is dispatched. You can control
* the display of this icon by calling the setStyle() method of the Subscriber,
* setting the videoDisabledDisplayMode property(or you can set the style when
* calling the Session.subscribe() method, setting the style property
* of the properties parameter).
*
* This feature is only available in sessions that use the OpenTok Media Router (sessions with * the media mode * set to routed), not in sessions with the media mode set to relayed. *
"subscribeToVideo" — The subscriber started or stopped subscribing to
* video, by calling subscribeToVideo(true) or subscribeToVideo(false).
* "videoDisabled" or
* "videoEnabled".
*
* @see Subscriber videoDisabled event
* @see Subscriber videoEnabled event
* @augments Event
*/
OT.VideoEnabledChangedEvent = function(type, properties) {
OT.Event.call(this, type, false);
this.reason = properties.reason;
};
OT.VideoDisableWarningEvent = function(type/*, properties*/) {
OT.Event.call(this, type, false);
};
/**
* Dispatched periodically by a Subscriber or Publisher object to indicate the audio
* level. This event is dispatched up to 60 times per second, depending on the browser.
*
* @property {String} audioLevel The audio level, from 0 to 1.0. Adjust this value logarithmically
* for use in adjusting a user interface element, such as a volume meter. Use a moving average
* to smooth the data.
*
* @class AudioLevelUpdatedEvent
* @augments Event
*/
OT.AudioLevelUpdatedEvent = function(audioLevel) {
OT.Event.call(this, OT.Event.names.AUDIO_LEVEL_UPDATED, false);
this.audioLevel = audioLevel;
};
})(window);
/* jshint ignore:start */
// https://code.google.com/p/stringencoding/
// An implementation of http://encoding.spec.whatwg.org/#api
/**
* @license Copyright 2014 Joshua Bell
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Original source: https://github.com/inexorabletash/text-encoding
***/
(function(global) {
'use strict';
var browser = OT.$.browserVersion();
if(browser.browser === 'IE' && browser.version < 10) {
return; // IE 8 doesn't do websockets. No websockets, no encoding.
}
if ( (global.TextEncoder !== void 0) && (global.TextDecoder !== void 0)) {
// defer to the native ones
// @todo is this a good idea?
return;
}
//
// Utilities
//
/**
* @param {number} a The number to test.
* @param {number} min The minimum value in the range, inclusive.
* @param {number} max The maximum value in the range, inclusive.
* @return {boolean} True if a >= min and a <= max.
*/
function inRange(a, min, max) {
return min <= a && a <= max;
}
/**
* @param {number} n The numerator.
* @param {number} d The denominator.
* @return {number} The result of the integer division of n by d.
*/
function div(n, d) {
return Math.floor(n / d);
}
//
// Implementation of Encoding specification
// http://dvcs.w3.org/hg/encoding/raw-file/tip/Overview.html
//
//
// 3. Terminology
//
//
// 4. Encodings
//
/** @const */ var EOF_byte = -1;
/** @const */ var EOF_code_point = -1;
/**
* @constructor
* @param {Uint8Array} bytes Array of bytes that provide the stream.
*/
function ByteInputStream(bytes) {
/** @type {number} */
var pos = 0;
/** @return {number} Get the next byte from the stream. */
this.get = function() {
return (pos >= bytes.length) ? EOF_byte : Number(bytes[pos]);
};
/** @param {number} n Number (positive or negative) by which to
* offset the byte pointer. */
this.offset = function(n) {
pos += n;
if (pos < 0) {
throw new Error('Seeking past start of the buffer');
}
if (pos > bytes.length) {
throw new Error('Seeking past EOF');
}
};
/**
* @param {Array.
* The completionHandler parameter is a function that is called when the call to
* the asynchronous method succeeds or fails. If the asynchronous call fails, the completion
* handler function is passes an error object (defined by the Error class). The code
* and message properties of the error object provide details about the error.
*
* @property {Number} code The error code, defining the error.
*
*
* In the event of an error, the code value of the error parameter can
* have one of the following values:
*
Errors when calling Session.connect():
code |
* Description | *
| 1004 | *Authentication error. Check the error message for details. This error can result if you * in an expired token when trying to connect to a session. It can also occur if you pass * in an invalid token or API key. Make sure that you are generating the token using the * current version of one of the * OpenTok server SDKs. | *
| 1005 | *Invalid Session ID. Make sure you generate the session ID using the current version of * one of the OpenTok server * SDKs. | *
| 1006 | *Connect Failed. Unable to connect to the session. You may want to have the client check * the network connection. | *
Errors when calling Session.forceDisconnect():
* code
* |
* Description | *
| 1010 | *The client is not connected to the OpenTok session. Check that client connects * successfully and has not disconnected before calling forceDisconnect(). | *
| 1520 | *Unable to force disconnect. The client's token does not have the role set to moderator.
* Once the client has connected to the session, the capabilities property of
* the Session object lists the client's capabilities. |
*
Errors when calling Session.forceUnpublish():
code |
* Description | *
| 1010 | *The client is not connected to the OpenTok session. Check that client connects * successfully and has not disconnected before calling forceUnpublish(). | *
| 1530 | *Unable to force unpublish. The client's token does not have the role set to moderator.
* Once the client has connected to the session, the capabilities property of
* the Session object lists the client's capabilities. |
*
| 1535 | *Force Unpublish on an invalid stream. Make sure that the stream has not left the
* session before you call the forceUnpublish() method. |
*
Errors when calling Session.publish():
code |
* Description | *
| 1010 | *The client is not connected to the OpenTok session. Check that the client connects * successfully before trying to publish. And check that the client has not disconnected * before trying to publish. | *
| 1500 | *Unable to Publish. The client's token does not have the role set to to publish or
* moderator. Once the client has connected to the session, the capabilities
* property of the Session object lists the client's capabilities. |
*
| 1601 | *Internal error -- WebRTC publisher error. Try republishing or reconnecting to the * session. | *
Errors when calling Session.signal():
code |
* Description | *
| 400 | *One of the signal properties — data, type, or to — * is invalid. Or the data cannot be parsed as JSON. | *
| 404 | The to connection does not exist. | *
| 413 | The type string exceeds the maximum length (128 bytes), * or the data string exceeds the maximum size (8 kB). | *
| 500 | *The client is not connected to the OpenTok session. Check that the client connects * successfully before trying to signal. And check that the client has not disconnected before * trying to publish. | *
Errors when calling Session.subscribe():
* Errors when calling Session.subscribe():
* |
* |
* code
* |
* Description | *
| 1600 | *Internal error -- WebRTC subscriber error. Try resubscribing to the stream or * reconnecting to the session. | *
Errors when calling TB.initPublisher():
code |
* Description | *
| 1004 | *Authentication error. Check the error message for details. This error can result if you * pass in an expired token when trying to connect to a session. It can also occur if you * pass in an invalid token or API key. Make sure that you are generating the token using * the current version of one of the * OpenTok server SDKs. | *
General errors that can occur when calling any method:
* *code |
* Description | *
| 1011 | *Invalid Parameter. Check that you have passed valid parameter values into the method * call. | *
| 2000 | *Internal Error. Try reconnecting to the OpenTok session and trying the action again. | *
id property of the Connection object for the client).
*
* The Session object has a connection property that is a Connection object.
* It represents the local client's connection. (A client only has a connection once the
* client has successfully called the connect() method of the {@link Session}
* object.)
*
* The Session object dispatches a connectionCreated event when each client
* (including your own) connects to a session (and for clients that are present in the
* session when you connect). The connectionCreated event object has a
* connection property, which is a Connection object corresponding to the client
* the event pertains to.
*
* The Stream object has a connection property that is a Connection object.
* It represents the connection of the client that is publishing the stream.
*
* @class Connection
* @property {String} connectionId The ID of this connection.
* @property {Number} creationTime The timestamp for the creation of the connection. This
* value is calculated in milliseconds.
* You can convert this value to a Date object by calling new Date(creationTime),
* where creationTime
* is the creationTime property of the Connection object.
* @property {String} data A string containing metadata describing the
* connection. When you generate a user token string pass the connection data string to the
* generate_token() method of our
* server-side libraries. You can also generate a token
* and define connection data on the
* Dashboard page.
*/
OT.Connection = function(id, creationTime, data, capabilitiesHash, permissionsHash) {
var destroyedReason;
this.id = this.connectionId = id;
this.creationTime = creationTime ? Number(creationTime) : null;
this.data = data;
this.capabilities = new OT.ConnectionCapabilities(capabilitiesHash);
this.permissions = new OT.Capabilities(permissionsHash);
this.quality = null;
OT.$.eventing(this);
this.destroy = OT.$.bind(function(reason, quiet) {
destroyedReason = reason || 'clientDisconnected';
if (quiet !== true) {
this.dispatchEvent(
new OT.DestroyedEvent(
'destroyed', // This should be OT.Event.names.CONNECTION_DESTROYED, but
// the value of that is currently shared with Session
this,
destroyedReason
)
);
}
}, this);
this.destroyed = function() {
return destroyedReason !== void 0;
};
this.destroyedReason = function() {
return destroyedReason;
};
};
OT.Connection.fromHash = function(hash) {
return new OT.Connection(hash.id,
hash.creationTime,
hash.data,
OT.$.extend(hash.capablities || {}, { supportsWebRTC: true }),
hash.permissions || [] );
};
})(window);
!(function() {
// id: String | mandatory | immutable
// type: String {video/audio/data/...} | mandatory | immutable
// active: Boolean | mandatory | mutable
// orientation: Integer? | optional | mutable
// frameRate: Float | optional | mutable
// height: Integer | optional | mutable
// width: Integer | optional | mutable
OT.StreamChannel = function(options) {
this.id = options.id;
this.type = options.type;
this.active = OT.$.castToBoolean(options.active);
this.orientation = options.orientation || OT.VideoOrientation.ROTATED_NORMAL;
if (options.frameRate) this.frameRate = parseFloat(options.frameRate, 10);
this.width = parseInt(options.width, 10);
this.height = parseInt(options.height, 10);
OT.$.eventing(this, true);
// Returns true if a property was updated.
this.update = function(attributes) {
var videoDimensions = {},
oldVideoDimensions = {};
for (var key in attributes) {
if(!attributes.hasOwnProperty(key)) {
continue;
}
// we shouldn't really read this before we know the key is valid
var oldValue = this[key];
switch(key) {
case 'active':
this.active = OT.$.castToBoolean(attributes[key]);
break;
case 'disableWarning':
this.disableWarning = OT.$.castToBoolean(attributes[key]);
break;
case 'frameRate':
this.frameRate = parseFloat(attributes[key], 10);
break;
case 'width':
case 'height':
this[key] = parseInt(attributes[key], 10);
videoDimensions[key] = this[key];
oldVideoDimensions[key] = oldValue;
break;
case 'orientation':
this[key] = attributes[key];
videoDimensions[key] = this[key];
oldVideoDimensions[key] = oldValue;
break;
default:
OT.warn('Tried to update unknown key ' + key + ' on ' + this.type +
' channel ' + this.id);
return;
}
this.trigger('update', this, key, oldValue, this[key]);
}
if (OT.$.keys(videoDimensions).length) {
// To make things easier for the public API, we broadcast videoDimensions changes,
// which is an aggregate of width, height, and orientation changes.
this.trigger('update', this, 'videoDimensions', oldVideoDimensions, videoDimensions);
}
return true;
};
};
})(window);
!(function() {
var validPropertyNames = ['name', 'archiving'];
/**
* Specifies a stream. A stream is a representation of a published stream in a session. When a
* client calls the Session.publish() method, a new stream is
* created. Properties of the Stream object provide information about the stream.
*
*
When a stream is added to a session, the Session object dispatches a
* streamCreatedEvent. When a stream is destroyed, the Session object dispatches a
* streamDestroyed event. The StreamEvent object, which defines these event objects,
* has a stream property, which is an array of Stream object. For details and a code
* example, see {@link StreamEvent}.
When a connection to a session is made, the Session object dispatches a
* sessionConnected event, defined by the SessionConnectEvent object. The
* SessionConnectEvent object has a streams property, which is an array of Stream
* objects pertaining to the streams in the session at that time. For details and a code example,
* see {@link SessionConnectEvent}.
connection property of the Session object to see if the stream is being published
* by the local web page.
*
* @property {Number} creationTime The timestamp for the creation
* of the stream. This value is calculated in milliseconds. You can convert this value to a
* Date object by calling new Date(creationTime), where creationTime is
* the creationTime property of the Stream object.
*
* @property {Number} frameRate The frame rate of the video stream. This property is only set if the
* publisher of the stream specifies a frame rate when calling the OT.initPublisher()
* method; otherwise, this property is undefined.
*
* @property {Boolean} hasAudio Whether the stream has audio. This property can change if the
* publisher turns on or off audio (by calling
* Publisher.publishAudio()). When this occurs, the
* {@link Session} object dispatches a streamPropertyChanged event (see
* {@link StreamPropertyChangedEvent}.)
*
* @property {Boolean} hasVideo Whether the stream has video. This property can change if the
* publisher turns on or off video (by calling
* Publisher.publishVideo()). When this occurs, the
* {@link Session} object dispatches a streamPropertyChanged event (see
* {@link StreamPropertyChangedEvent}.)
*
* @property {String} name The name of the stream. Publishers can specify a name when publishing
* a stream (using the publish() method of the publisher's Session object).
*
* @property {String} streamId The unique ID of the stream.
*
* @property {Object} videoDimensions This object has two properties: width and
* height. Both are numbers. The width property is the width of the
* encoded stream; the height property is the height of the encoded stream. (These
* are independent of the actual width of Publisher and Subscriber objects corresponding to the
* stream.) This property can change if a stream
* published from an iOS device resizes, based on a change in the device orientation. When this
* occurs, the {@link Session} object dispatches a streamPropertyChanged event (see
* {@link StreamPropertyChangedEvent}.)
*/
OT.Stream = function(id, name, creationTime, connection, session, channel) {
var destroyedReason;
this.id = this.streamId = id;
this.name = name;
this.creationTime = Number(creationTime);
this.connection = connection;
this.channel = channel;
this.publisher = OT.publishers.find({streamId: this.id});
OT.$.eventing(this);
var onChannelUpdate = OT.$.bind(function(channel, key, oldValue, newValue) {
var _key = key;
switch(_key) {
case 'active':
_key = channel.type === 'audio' ? 'hasAudio' : 'hasVideo';
this[_key] = newValue;
break;
case 'disableWarning':
_key = channel.type === 'audio' ? 'audioDisableWarning': 'videoDisableWarning';
this[_key] = newValue;
if (!this[channel.type === 'audio' ? 'hasAudio' : 'hasVideo']) {
return; // Do NOT event in this case.
}
break;
case 'orientation':
case 'width':
case 'height':
this.videoDimensions = {
width: channel.width,
height: channel.height,
orientation: channel.orientation
};
// We dispatch this via the videoDimensions key instead
return;
}
this.dispatchEvent( new OT.StreamUpdatedEvent(this, _key, oldValue, newValue) );
}, this);
var associatedWidget = OT.$.bind(function() {
if(this.publisher) {
return this.publisher;
} else {
return OT.subscribers.find(function(subscriber) {
return subscriber.stream.id === this.id &&
subscriber.session.id === session.id;
});
}
}, this);
// Returns all channels that have a type of +type+.
this.getChannelsOfType = function (type) {
return OT.$.filter(this.channel, function(channel) {
return channel.type === type;
});
};
this.getChannel = function (id) {
for (var i=0; igetStats method to get the audioOutputLevel.
* This implementation expects the single parameter version of the getStats method.
*
* Currently the audioOutputLevel stats is only supported in Chrome.
*
* @param {OT.SubscriberPeerConnection} peerConnection the peer connection to use to get the stats
* @constructor
*/
OT.GetStatsAudioLevelSampler = function(peerConnection) {
if (!OT.$.hasCapabilities('audioOutputLevelStat', 'getStatsWithSingleParameter')) {
throw new Error('The current platform does not provide the required capabilities');
}
var _peerConnection = peerConnection,
_statsProperty = 'audioOutputLevel';
/*
* Acquires the audio level.
*
* @param {function(?number)} done a callback to be called with the acquired value in the
* [0, 1] range when available or null if no value could be acquired
*/
this.sample = function(done) {
_peerConnection.getStatsWithSingleParameter(function(statsReport) {
var results = statsReport.result();
for (var i = 0; i < results.length; i++) {
var result = results[i];
if (result.local) {
var audioOutputLevel = parseFloat(result.local.stat(_statsProperty));
if (!isNaN(audioOutputLevel)) {
// the mex value delivered by getStats for audio levels is 2^15
done(audioOutputLevel / 32768);
return;
}
}
}
done(null);
});
};
};
/*
* An AudioContext based audio level sampler. It returns the maximum value in the
* last 1024 samples.
*
* It is worth noting that the remote MediaStream audio analysis is currently only
* available in FF.
*
* This implementation gracefully handles the case where the MediaStream has not
* been set yet by returning a null value until the stream is set. It is up to the
* call site to decide what to do with this value (most likely ignore it and retry later).
*
* @constructor
* @param {AudioContext} audioContext an audio context instance to get an analyser node
*/
OT.AnalyserAudioLevelSampler = function(audioContext) {
var _sampler = this,
_analyser = null,
_timeDomainData = null;
var _getAnalyser = function(stream) {
var sourceNode = audioContext.createMediaStreamSource(stream);
var analyser = audioContext.createAnalyser();
sourceNode.connect(analyser);
return analyser;
};
this.webOTStream = null;
this.sample = function(done) {
if (!_analyser && _sampler.webOTStream) {
_analyser = _getAnalyser(_sampler.webOTStream);
_timeDomainData = new Uint8Array(_analyser.frequencyBinCount);
}
if (_analyser) {
_analyser.getByteTimeDomainData(_timeDomainData);
// varies from 0 to 255
var max = 0;
for (var idx = 0; idx < _timeDomainData.length; idx++) {
max = Math.max(max, Math.abs(_timeDomainData[idx] - 128));
}
// normalize the collected level to match the range delivered by
// the getStats' audioOutputLevel
done(max / 128);
} else {
done(null);
}
};
};
/*
* Transforms a raw audio level to produce a "smoother" animation when using displaying the
* audio level. This transformer is state-full because it needs to keep the previous average
* value of the signal for filtering.
*
* It applies a low pass filter to get rid of level jumps and apply a log scale.
*
* @constructor
*/
OT.AudioLevelTransformer = function() {
var _averageAudioLevel = null;
/*
*
* @param {number} audioLevel a level in the [0,1] range
* @returns {number} a level in the [0,1] range transformed
*/
this.transform = function(audioLevel) {
if (_averageAudioLevel === null || audioLevel >= _averageAudioLevel) {
_averageAudioLevel = audioLevel;
} else {
// a simple low pass filter with a smoothing of 70
_averageAudioLevel = audioLevel * 0.3 + _averageAudioLevel * 0.7;
}
// 1.5 scaling to map -30-0 dBm range to [0,1]
var logScaled = (Math.log(_averageAudioLevel) / Math.LN10) / 1.5 + 1;
return Math.min(Math.max(logScaled, 0), 1);
};
};
})(window);
!(function() {
/*
* Executes the provided callback thanks to window.setInterval.
*
* @param {function()} callback
* @param {number} frequency how many times per second we want to execute the callback
* @constructor
*/
OT.IntervalRunner = function(callback, frequency) {
var _callback = callback,
_frequency = frequency,
_intervalId = null;
this.start = function() {
_intervalId = window.setInterval(_callback, 1000 / _frequency);
};
this.stop = function() {
window.clearInterval(_intervalId);
_intervalId = null;
};
};
})(window);
!(function(window) {
// Normalise these
var NativeRTCSessionDescription,
NativeRTCIceCandidate;
if (!TBPlugin.isInstalled()) {
// order is very important: 'RTCSessionDescription' defined in Firefox Nighly but useless
NativeRTCSessionDescription = (window.mozRTCSessionDescription ||
window.RTCSessionDescription);
NativeRTCIceCandidate = (window.mozRTCIceCandidate || window.RTCIceCandidate);
}
else {
NativeRTCSessionDescription = TBPlugin.RTCSessionDescription;
NativeRTCIceCandidate = TBPlugin.RTCIceCandidate;
}
// Helper function to forward Ice Candidates via +messageDelegate+
var iceCandidateForwarder = function(messageDelegate) {
return function(event) {
if (event.candidate) {
messageDelegate(OT.Raptor.Actions.CANDIDATE, event.candidate);
} else {
OT.debug('IceCandidateForwarder: No more ICE candidates.');
}
};
};
// Process incoming Ice Candidates from a remote connection (which have been
// forwarded via iceCandidateForwarder). The Ice Candidates cannot be processed
// until a PeerConnection is available. Once a PeerConnection becomes available
// the pending PeerConnections can be processed by calling processPending.
//
// @example
//
// var iceProcessor = new IceCandidateProcessor();
// iceProcessor.process(iceMessage1);
// iceProcessor.process(iceMessage2);
// iceProcessor.process(iceMessage3);
//
// iceProcessor.setPeerConnection(peerConnection);
// iceProcessor.processPending();
//
var IceCandidateProcessor = function() {
var _pendingIceCandidates = [],
_peerConnection = null;
this.setPeerConnection = function(peerConnection) {
_peerConnection = peerConnection;
};
this.process = function(message) {
var iceCandidate = new NativeRTCIceCandidate(message.content);
if (_peerConnection) {
_peerConnection.addIceCandidate(iceCandidate);
} else {
_pendingIceCandidates.push(iceCandidate);
}
};
this.processPending = function() {
while(_pendingIceCandidates.length) {
_peerConnection.addIceCandidate(_pendingIceCandidates.shift());
}
};
};
// Removes all Confort Noise from +sdp+.
//
// See https://jira.tokbox.com/browse/OPENTOK-7176
//
var removeComfortNoise = function removeComfortNoise (sdp) {
// a=rtpmap:setStyle() method of thePublisher object. (See the documentation for
* setStyle() to see the styles that define this object.)
* @return {Object} The object that defines the styles of the Publisher.
* @see setStyle()
* @method #getStyle
* @memberOf Publisher
*/
/**
* Returns an object that has the properties that define the current user interface controls of
* the Subscriber. You can modify the properties of this object and pass the object to the
* setStyle() method of the Subscriber object. (See the documentation for
* setStyle() to see the styles that define this object.)
* @return {Object} The object that defines the styles of the Subscriber.
* @see setStyle()
* @method #getStyle
* @memberOf Subscriber
*/
// If +key+ is falsly then all styles will be returned.
self.getStyle = function(key) {
return _style.get(key);
};
/**
* Sets properties that define the appearance of some user interface controls of the Publisher.
*
* You can either pass one parameter or two parameters to this method.
* *If you pass one parameter, style, it is an object that has the following
* properties:
*
*
audioLevelDisplayMode (String) — How to display the audio level
* indicator. Possible values are: "auto" (the indicator is displayed when the
* video is disabled), "off" (the indicator is not displayed), and
* "on" (the indicator is always displayed).backgroundImageURI (String) — A URI for an image to display as
* the background image when a video is not displayed. (A video may not be displayed if
* you call publishVideo(false) on the Publisher object). You can pass an http
* or https URI to a PNG, JPEG, or non-animated GIF file location. You can also use the
* data URI scheme (instead of http or https) and pass in base-64-encrypted
* PNG data, such as that obtained from the
* Publisher.getImgData() method. For example,
* you could set the property to "data:VBORw0KGgoAA...", where the portion of
* the string after "data:" is the result of a call to
* Publisher.getImgData(). If the URL or the image data is invalid, the
* property is ignored (the attempt to set the image fails silently).
*
* Note that in Internet Explorer 8 (using the OpenTok Plugin for Internet Explorer),
* you cannot set the backgroundImageURI style to a string larger than
* 32 kB. This is due to an IE 8 limitation on the size of URI strings. Due to this
* limitation, you cannot set the backgroundImageURI style to a string obtained
* with the getImgData() method.
*
buttonDisplayMode (String) — How to display the microphone
* controls. Possible values are: "auto" (controls are displayed when the
* stream is first displayed and when the user mouses over the display), "off"
* (controls are not displayed), and "on" (controls are always displayed).nameDisplayMode (String) Whether to display the stream name.
* Possible values are: "auto" (the name is displayed when the stream is first
* displayed and when the user mouses over the display), "off" (the name is not
* displayed), and "on" (the name is always displayed).For example, the following code passes one parameter to the method:
* *myPublisher.setStyle({nameDisplayMode: "off"});
*
* If you pass two parameters, style and value, they are
* key-value pair that define one property of the display style. For example, the following
* code passes two parameter values to the method:
myPublisher.setStyle("nameDisplayMode", "off");
*
* You can set the initial settings when you call the Session.publish()
* or OT.initPublisher() method. Pass a style property as part of the
* properties parameter of the method.
The OT object dispatches an exception event if you pass in an invalid style
* to the method. The code property of the ExceptionEvent object is set to 1011.
style passed in. Pass a value
* for this parameter only if the value of the style parameter is a String.
*
* @see getStyle()
* @return {Publisher} The Publisher object
* @see setStyle()
*
* @see Session.publish()
* @see OT.initPublisher()
* @method #setStyle
* @memberOf Publisher
*/
/**
* Sets properties that define the appearance of some user interface controls of the Subscriber.
*
* You can either pass one parameter or two parameters to this method.
* *If you pass one parameter, style, it is an object that has the following
* properties:
*
*
audioLevelDisplayMode (String) — How to display the audio level
* indicator. Possible values are: "auto" (the indicator is displayed when the
* video is disabled), "off" (the indicator is not displayed), and
* "on" (the indicator is always displayed).backgroundImageURI (String) — A URI for an image to display as
* the background image when a video is not displayed. (A video may not be displayed if
* you call subscribeToVideo(false) on the Publisher object). You can pass an
* http or https URI to a PNG, JPEG, or non-animated GIF file location. You can also use the
* data URI scheme (instead of http or https) and pass in base-64-encrypted
* PNG data, such as that obtained from the
* Subscriber.getImgData() method. For example,
* you could set the property to "data:VBORw0KGgoAA...", where the portion of
* the string after "data:" is the result of a call to
* Publisher.getImgData(). If the URL or the image data is invalid, the
* property is ignored (the attempt to set the image fails silently).
*
* Note that in Internet Explorer 8 (using the OpenTok Plugin for Internet Explorer),
* you cannot set the backgroundImageURI style to a string larger than
* 32 kB. This is due to an IE 8 limitation on the size of URI strings. Due to this
* limitation, you cannot set the backgroundImageURI style to a string obtained
* with the getImgData() method.
*
buttonDisplayMode (String) — How to display the speaker
* controls. Possible values are: "auto" (controls are displayed when the
* stream is first displayed and when the user mouses over the display), "off"
* (controls are not displayed), and "on" (controls are always displayed).nameDisplayMode (String) Whether to display the stream name.
* Possible values are: "auto" (the name is displayed when the stream is first
* displayed and when the user mouses over the display), "off" (the name is not
* displayed), and "on" (the name is always displayed).videoDisabledDisplayMode (String) Whether to display the video
* disabled indicator and video disabled warning icons for a Subscriber. These icons
* indicate that the video has been disabled (or is in risk of being disabled for
* the warning icon) due to poor stream quality. Possible values are: "auto"
* (the icons are automatically when the displayed video is disabled or in risk of being
* disabled due to poor stream quality), "off" (do not display the icons), and
* "on" (display the icons).For example, the following code passes one parameter to the method:
* *mySubscriber.setStyle({nameDisplayMode: "off"});
*
* If you pass two parameters, style and value, they are key-value
* pair that define one property of the display style. For example, the following code passes
* two parameter values to the method:
mySubscriber.setStyle("nameDisplayMode", "off");
*
* You can set the initial settings when you call the Session.subscribe() method.
* Pass a style property as part of the properties parameter of the
* method.
The OT object dispatches an exception event if you pass in an invalid style
* to the method. The code property of the ExceptionEvent object is set to 1011.
style passed in. Pass a value
* for this parameter only if the value of the style parameter is a String.
*
* @returns {Subscriber} The Subscriber object.
*
* @see getStyle()
* @see setStyle()
*
* @see Session.subscribe()
* @method #setStyle
* @memberOf Subscriber
*/
self.setStyle = function(keyOrStyleHash, value, silent) {
if (typeof(keyOrStyleHash) !== 'string') {
_style.setAll(keyOrStyleHash, silent);
} else {
_style.set(keyOrStyleHash, value);
}
return this;
};
};
var Style = function(initalStyles, onStyleChange) {
var _style = {},
_COMPONENT_STYLES,
_validStyleValues,
isValidStyle,
castValue;
_COMPONENT_STYLES = [
'showMicButton',
'showSpeakerButton',
'nameDisplayMode',
'buttonDisplayMode',
'backgroundImageURI',
'bugDisplayMode'
];
_validStyleValues = {
buttonDisplayMode: ['auto', 'mini', 'mini-auto', 'off', 'on'],
nameDisplayMode: ['auto', 'off', 'on'],
bugDisplayMode: ['auto', 'off', 'on'],
audioLevelDisplayMode: ['auto', 'off', 'on'],
showSettingsButton: [true, false],
showMicButton: [true, false],
backgroundImageURI: null,
showControlBar: [true, false],
showArchiveStatus: [true, false],
videoDisabledDisplayMode: ['auto', 'off', 'on']
};
// Validates the style +key+ and also whether +value+ is valid for +key+
isValidStyle = function(key, value) {
return key === 'backgroundImageURI' ||
(_validStyleValues.hasOwnProperty(key) &&
OT.$.arrayIndexOf(_validStyleValues[key], value) !== -1 );
};
castValue = function(value) {
switch(value) {
case 'true':
return true;
case 'false':
return false;
default:
return value;
}
};
// Returns a shallow copy of the styles.
this.getAll = function() {
var style = OT.$.clone(_style);
for (var key in style) {
if(!style.hasOwnProperty(key)) {
continue;
}
if (OT.$.arrayIndexOf(_COMPONENT_STYLES, key) < 0) {
// Strip unnecessary properties out, should this happen on Set?
delete style[key];
}
}
return style;
};
this.get = function(key) {
if (key) {
return _style[key];
}
// We haven't been asked for any specific key, just return the lot
return this.getAll();
};
// *note:* this will not trigger onStyleChange if +silent+ is truthy
this.setAll = function(newStyles, silent) {
var oldValue, newValue;
for (var key in newStyles) {
if(!newStyles.hasOwnProperty(key)) {
continue;
}
newValue = castValue(newStyles[key]);
if (isValidStyle(key, newValue)) {
oldValue = _style[key];
if (newValue !== oldValue) {
_style[key] = newValue;
if (!silent) onStyleChange(key, newValue, oldValue);
}
} else {
OT.warn('Style.setAll::Invalid style property passed ' + key + ' : ' + newValue);
}
}
return this;
};
this.set = function(key, value) {
OT.debug('setStyle: ' + key.toString());
var newValue = castValue(value),
oldValue;
if (!isValidStyle(key, newValue)) {
OT.warn('Style.set::Invalid style property passed ' + key + ' : ' + newValue);
return this;
}
oldValue = _style[key];
if (newValue !== oldValue) {
_style[key] = newValue;
onStyleChange(key, value, oldValue);
}
return this;
};
if (initalStyles) this.setAll(initalStyles, true);
};
})(window);
!(function() {
/*
* A Publishers Microphone.
*
* TODO
* * bind to changes in mute/unmute/volume/etc and respond to them
*/
OT.Microphone = function(webRTCStream, muted) {
var _muted;
OT.$.defineProperties(this, {
muted: {
get: function() {
return _muted;
},
set: function(muted) {
if (_muted === muted) return;
_muted = muted;
var audioTracks = webRTCStream.getAudioTracks();
for (var i=0, num=audioTracks.length; iOT.initPublisher() method
* creates a Publisher object.
*
* The following code instantiates a session, and publishes an audio-video stream * upon connection to the session:
* *
* var apiKey = ""; // Replace with your API key. See https://dashboard.tokbox.com/projects
* var sessionID = ""; // Replace with your own session ID.
* // See https://dashboard.tokbox.com/projects
* var token = ""; // Replace with a generated token that has been assigned the moderator role.
* // See https://dashboard.tokbox.com/projects
*
* var session = OT.initSession(apiKey, sessionID);
* session.on("sessionConnected", function(event) {
* // This example assumes that a DOM element with the ID 'publisherElement' exists
* var publisherProperties = {width: 400, height:300, name:"Bob's stream"};
* publisher = OT.initPublisher('publisherElement', publisherProperties);
* session.publish(publisher);
* });
* session.connect(token);
*
*
* This example creates a Publisher object and adds its video to a DOM element
* with the ID publisherElement by calling the OT.initPublisher()
* method. It then publishes a stream to the session by calling
* the publish() method of the Session object.
accessAllowed event when
* the user grants access. The Publisher object dispatches an accessDenied event
* when the user denies access.
* @property {Element} element The HTML DOM element containing the Publisher.
* @property {String} id The DOM ID of the Publisher.
* @property {Stream} stream The {@link Stream} object corresponding the the stream of
* the Publisher.
* @property {Session} session The {@link Session} to which the Publisher belongs.
*
* @see OT.initPublisher
* @see Session.publish()
*
* @class Publisher
* @augments EventDispatcher
*/
OT.Publisher = function() {
// Check that the client meets the minimum requirements, if they don't the upgrade
// flow will be triggered.
if (!OT.checkSystemRequirements()) {
OT.upgradeSystemRequirements();
return;
}
var _guid = OT.Publisher.nextId(),
_domId,
_container,
_targetElement,
_stream,
_streamId,
_webRTCStream,
_session,
_peerConnections = {},
_loaded = false,
_publishProperties,
_publishStartTime,
_microphone,
_chrome,
_audioLevelMeter,
_analytics = new OT.Analytics(),
_validResolutions,
_validFrameRates = [ 1, 7, 15, 30 ],
_prevStats,
_state,
_iceServers,
_audioLevelCapable = OT.$.hasCapabilities('webAudio'),
_audioLevelSampler;
_validResolutions = {
'320x240': {width: 320, height: 240},
'640x480': {width: 640, height: 480},
'1280x720': {width: 1280, height: 720}
};
_prevStats = {
'timeStamp' : OT.$.now()
};
OT.$.eventing(this);
if(_audioLevelCapable) {
_audioLevelSampler = new OT.AnalyserAudioLevelSampler(new window.AudioContext());
var publisher = this;
var audioLevelRunner = new OT.IntervalRunner(function() {
_audioLevelSampler.sample(function(audioInputLevel) {
OT.$.requestAnimationFrame(function() {
publisher.dispatchEvent(
new OT.AudioLevelUpdatedEvent(audioInputLevel));
});
});
}, 60);
this.on({
'audioLevelUpdated:added': function(count) {
if (count === 1) {
audioLevelRunner.start();
}
},
'audioLevelUpdated:removed': function(count) {
if (count === 0) {
audioLevelRunner.stop();
}
}
});
}
OT.StylableComponent(this, {
showArchiveStatus: true,
nameDisplayMode: 'auto',
buttonDisplayMode: 'auto',
bugDisplayMode: 'auto',
audioLevelDisplayMode: 'auto',
backgroundImageURI: null
});
/// Private Methods
var logAnalyticsEvent = function(action, variation, payloadType, payload) {
_analytics.logEvent({
action: action,
variation: variation,
'payload_type': payloadType,
payload: payload,
'session_id': _session ? _session.sessionId : null,
'connection_id': _session &&
_session.isConnected() ? _session.connection.connectionId : null,
'partner_id': _session ? _session.apiKey : OT.APIKEY,
streamId: _stream ? _stream.id : null,
'widget_id': _guid,
'widget_type': 'Publisher'
});
},
recordQOS = OT.$.bind(function(connection, parsedStats) {
if(!_state.isPublishing()) {
return;
}
var QoSBlob = {
'widget_type': 'Publisher',
'stream_type': 'WebRTC',
sessionId: _session ? _session.sessionId : null,
connectionId: _session && _session.isConnected() ?
_session.connection.connectionId : null,
partnerId: _session ? _session.apiKey : OT.APIKEY,
streamId: _stream ? _stream.id : null,
width: _container ? OT.$.width(_container.domElement) : undefined,
height: _container ? OT.$.height(_container.domElement) : undefined,
widgetId: _guid,
version: OT.properties.version,
'media_server_name': _session ? _session.sessionInfo.messagingServer : null,
p2pFlag: _session ? _session.sessionInfo.p2pEnabled : false,
duration: _publishStartTime ? new Date().getTime() - _publishStartTime.getTime() : 0,
'remote_connection_id': connection.id
};
_analytics.logQOS( OT.$.extend(QoSBlob, parsedStats) );
this.trigger('qos', parsedStats);
}, this),
/// Private Events
stateChangeFailed = function(changeFailed) {
OT.error('Publisher State Change Failed: ', changeFailed.message);
OT.debug(changeFailed);
},
onLoaded = function() {
if (_state.isDestroyed()) {
// The publisher was destroyed before loading finished
return;
}
OT.debug('OT.Publisher.onLoaded');
_state.set('MediaBound');
// If we have a session and we haven't created the stream yet then
// wait until that is complete before hiding the loading spinner
_container.loading(this.session ? !_stream : false);
_loaded = true;
_createChrome.call(this);
this.trigger('initSuccess');
this.trigger('loaded', this);
},
onLoadFailure = function(reason) {
logAnalyticsEvent('publish', 'Failure', 'reason',
'Publisher PeerConnection Error: ' + reason);
_state.set('Failed');
this.trigger('publishComplete', new OT.Error(OT.ExceptionCodes.P2P_CONNECTION_FAILED,
'Publisher PeerConnection Error: ' + reason));
OT.handleJsException('Publisher PeerConnection Error: ' + reason,
OT.ExceptionCodes.P2P_CONNECTION_FAILED, {
session: _session,
target: this
});
},
onStreamAvailable = function(webOTStream) {
OT.debug('OT.Publisher.onStreamAvailable');
_state.set('BindingMedia');
cleanupLocalStream();
_webRTCStream = webOTStream;
_microphone = new OT.Microphone(_webRTCStream, !_publishProperties.publishAudio);
this.publishVideo(_publishProperties.publishVideo &&
_webRTCStream.getVideoTracks().length > 0);
this.accessAllowed = true;
this.dispatchEvent(
new OT.Event(OT.Event.names.ACCESS_ALLOWED, false)
);
var videoContainerOptions = {
muted: true,
error: OT.$.bind(onVideoError, this)
};
_targetElement = _container.bindVideo(_webRTCStream,
videoContainerOptions,
OT.$.bind(function(err) {
if (err) {
onLoadFailure.call(this, err);
return;
}
onLoaded.call(this);
}, this));
if(_audioLevelSampler) {
_audioLevelSampler.webOTStream = webOTStream;
}
},
onStreamAvailableError = function(error) {
OT.error('OT.Publisher.onStreamAvailableError ' + error.name + ': ' + error.message);
_state.set('Failed');
this.trigger('publishComplete', new OT.Error(OT.ExceptionCodes.UNABLE_TO_PUBLISH,
error.message));
if (_container) _container.destroy();
logAnalyticsEvent('publish', 'Failure', 'reason',
'GetUserMedia:Publisher failed to access camera/mic: ' + error.message);
OT.handleJsException('Publisher failed to access camera/mic: ' + error.message,
OT.ExceptionCodes.UNABLE_TO_PUBLISH, {
session: _session,
target: this
});
},
// The user has clicked the 'deny' button the the allow access dialog
// (or it's set to always deny)
onAccessDenied = function(error) {
OT.error('OT.Publisher.onStreamAvailableError Permission Denied');
_state.set('Failed');
this.trigger('publishComplete', new OT.Error(OT.ExceptionCodes.UNABLE_TO_PUBLISH,
'Publisher Access Denied: Permission Denied' +
(error.message ? ': ' + error.message : '')));
logAnalyticsEvent('publish', 'Failure', 'reason',
'GetUserMedia:Publisher Access Denied: Permission Denied');
var browser = OT.$.browserVersion();
var event = new OT.Event(OT.Event.names.ACCESS_DENIED),
defaultAction = function() {
if(!event.isDefaultPrevented()) {
if(browser.browser === 'Chrome') {
if (_container) {
_container.addError('', null, 'OT_publisher-denied-chrome');
}
if(!accessDialogWasOpened) {
OT.Dialogs.AllowDeny.Chrome.previouslyDenied(window.location.hostname);
} else {
OT.Dialogs.AllowDeny.Chrome.deniedNow();
}
} else if(browser.browser === 'Firefox') {
if(_container) {
_container.addError('', 'Click the reload button in the URL bar to change ' +
'camera & mic settings.', 'OT_publisher-denied-firefox');
}
OT.Dialogs.AllowDeny.Firefox.denied().on({
refresh: function() {
window.location.reload();
}
});
}
}
};
this.dispatchEvent(event, defaultAction);
},
accessDialogPrompt,
accessDialogChromeTimeout,
accessDialogFirefoxTimeout,
accessDialogWasOpened = false,
onAccessDialogOpened = function() {
accessDialogWasOpened = true;
logAnalyticsEvent('accessDialog', 'Opened', '', '');
var browser = OT.$.browserVersion();
this.dispatchEvent(
new OT.Event(OT.Event.names.ACCESS_DIALOG_OPENED, true),
function(event) {
if(!event.isDefaultPrevented()) {
if(browser.browser === 'Chrome') {
accessDialogChromeTimeout = setTimeout(function() {
accessDialogChromeTimeout = null;
logAnalyticsEvent('allowDenyHelpers', 'show', 'version', 'Chrome');
accessDialogPrompt = OT.Dialogs.AllowDeny.Chrome.initialPrompt();
accessDialogPrompt.on('closeButtonClicked', function() {
logAnalyticsEvent('allowDenyHelpers', 'dismissed', 'version', 'Chrome');
});
}, 5000);
} else if(browser.browser === 'Firefox') {
accessDialogFirefoxTimeout = setTimeout(function() {
accessDialogFirefoxTimeout = null;
logAnalyticsEvent('allowDenyHelpers', 'show', 'version', 'Firefox');
accessDialogPrompt = OT.Dialogs.AllowDeny.Firefox.maybeDenied();
accessDialogPrompt.on('closeButtonClicked', function() {
logAnalyticsEvent('allowDenyHelpers', 'dismissed', 'version', 'Firefox');
});
}, 7000);
}
} else {
logAnalyticsEvent('allowDenyHelpers', 'developerPrevented', '', '');
}
}
);
},
onAccessDialogClosed = function() {
logAnalyticsEvent('accessDialog', 'Closed', '', '');
if(accessDialogChromeTimeout) {
clearTimeout(accessDialogChromeTimeout);
logAnalyticsEvent('allowDenyHelpers', 'notShown', 'version', 'Chrome');
accessDialogChromeTimeout = null;
}
if(accessDialogFirefoxTimeout) {
clearTimeout(accessDialogFirefoxTimeout);
logAnalyticsEvent('allowDenyHelpers', 'notShown', 'version', 'Firefox');
accessDialogFirefoxTimeout = null;
}
if(accessDialogPrompt) {
accessDialogPrompt.close();
var browser = OT.$.browserVersion();
logAnalyticsEvent('allowDenyHelpers', 'closed', 'version', browser.browser);
accessDialogPrompt = null;
}
this.dispatchEvent(
new OT.Event(OT.Event.names.ACCESS_DIALOG_CLOSED, false)
);
},
onVideoError = function(errorCode, errorReason) {
OT.error('OT.Publisher.onVideoError');
var message = errorReason + (errorCode ? ' (' + errorCode + ')' : '');
logAnalyticsEvent('stream', null, 'reason',
'Publisher while playing stream: ' + message);
_state.set('Failed');
if (_state.isAttemptingToPublish()) {
this.trigger('publishComplete', new OT.Error(OT.ExceptionCodes.UNABLE_TO_PUBLISH,
message));
} else {
this.trigger('error', message);
}
OT.handleJsException('Publisher error playing stream: ' + message,
OT.ExceptionCodes.UNABLE_TO_PUBLISH, {
session: _session,
target: this
});
},
onPeerDisconnected = function(peerConnection) {
OT.debug('OT.Subscriber has been disconnected from the Publisher\'s PeerConnection');
this.cleanupSubscriber(peerConnection.remoteConnection().id);
},
onPeerConnectionFailure = function(code, reason, peerConnection, prefix) {
logAnalyticsEvent('publish', 'Failure', 'reason|hasRelayCandidates',
(prefix ? prefix : '') + [':Publisher PeerConnection with connection ' +
(peerConnection && peerConnection.remoteConnection &&
peerConnection.remoteConnection().id) + ' failed: ' +
reason, peerConnection.hasRelayCandidates()
].join('|'));
OT.handleJsException('Publisher PeerConnection Error: ' + reason,
OT.ExceptionCodes.UNABLE_TO_PUBLISH, {
session: _session,
target: this
});
// We don't call cleanupSubscriber as it also logs a
// disconnected analytics event, which we don't want in this
// instance. The duplication is crufty though and should
// be tidied up.
delete _peerConnections[peerConnection.remoteConnection().id];
},
/// Private Helpers
// Assigns +stream+ to this publisher. The publisher listens
// for a bunch of events on the stream so it can respond to
// changes.
assignStream = OT.$.bind(function(stream) {
this.stream = _stream = stream;
_stream.on('destroyed', this.disconnect, this);
_state.set('Publishing');
_container.loading(!_loaded);
_publishStartTime = new Date();
this.trigger('publishComplete', null, this);
this.dispatchEvent(new OT.StreamEvent('streamCreated', stream, null, false));
logAnalyticsEvent('publish', 'Success', 'streamType:streamId', 'WebRTC:' + _streamId);
}, this),
// Clean up our LocalMediaStream
cleanupLocalStream = function() {
if (_webRTCStream) {
// Stop revokes our access cam and mic access for this instance
// of localMediaStream.
_webRTCStream.stop();
_webRTCStream = null;
}
},
createPeerConnectionForRemote = function(remoteConnection) {
var peerConnection = _peerConnections[remoteConnection.id];
if (!peerConnection) {
var startConnectingTime = OT.$.now();
logAnalyticsEvent('createPeerConnection', 'Attempt', '', '');
// Cleanup our subscriber when they disconnect
remoteConnection.on('destroyed',
OT.$.bind(this.cleanupSubscriber, this, remoteConnection.id));
peerConnection = _peerConnections[remoteConnection.id] = new OT.PublisherPeerConnection(
remoteConnection,
_session,
_streamId,
_webRTCStream
);
peerConnection.on({
connected: function() {
logAnalyticsEvent('createPeerConnection', 'Success', 'pcc|hasRelayCandidates', [
parseInt(OT.$.now() - startConnectingTime, 10),
peerConnection.hasRelayCandidates()
].join('|'));
},
disconnected: onPeerDisconnected,
error: onPeerConnectionFailure,
qos: recordQOS
}, this);
peerConnection.init(_iceServers);
}
return peerConnection;
},
/// Chrome
// If mode is false, then that is the mode. If mode is true then we'll
// definitely display the button, but we'll defer the model to the
// Publishers buttonDisplayMode style property.
chromeButtonMode = function(mode) {
if (mode === false) return 'off';
var defaultMode = this.getStyle('buttonDisplayMode');
// The default model is false, but it's overridden by +mode+ being true
if (defaultMode === false) return 'on';
// defaultMode is either true or auto.
return defaultMode;
},
updateChromeForStyleChange = function(key, value) {
if (!_chrome) return;
switch(key) {
case 'nameDisplayMode':
_chrome.name.setDisplayMode(value);
_chrome.backingBar.setNameMode(value);
break;
case 'showArchiveStatus':
logAnalyticsEvent('showArchiveStatus', 'styleChange', 'mode', value ? 'on': 'off');
_chrome.archive.setShowArchiveStatus(value);
break;
case 'buttonDisplayMode':
_chrome.muteButton.setDisplayMode(value);
_chrome.backingBar.setMuteMode(value);
break;
case 'audioLevelDisplayMode':
_chrome.audioLevel.setDisplayMode(value);
break;
case 'bugDisplayMode':
// bugDisplayMode can't be updated but is used by some partners
case 'backgroundImageURI':
_container.setBackgroundImageURI(value);
}
},
_createChrome = function() {
if(this.getStyle('bugDisplayMode') === 'off') {
logAnalyticsEvent('bugDisplayMode', 'createChrome', 'mode', 'off');
}
if(!this.getStyle('showArchiveStatus')) {
logAnalyticsEvent('showArchiveStatus', 'createChrome', 'mode', 'off');
}
var widgets = {
backingBar: new OT.Chrome.BackingBar({
nameMode: !_publishProperties.name ? 'off' : this.getStyle('nameDisplayMode'),
muteMode: chromeButtonMode.call(this, this.getStyle('buttonDisplayMode'))
}),
name: new OT.Chrome.NamePanel({
name: _publishProperties.name,
mode: this.getStyle('nameDisplayMode'),
bugMode: this.getStyle('bugDisplayMode')
}),
muteButton: new OT.Chrome.MuteButton({
muted: _publishProperties.publishAudio === false,
mode: chromeButtonMode.call(this, this.getStyle('buttonDisplayMode'))
}),
opentokButton: new OT.Chrome.OpenTokButton({
mode: this.getStyle('bugDisplayMode')
}),
archive: new OT.Chrome.Archiving({
show: this.getStyle('showArchiveStatus'),
archiving: false
})
};
if(_audioLevelCapable) {
_audioLevelMeter = new OT.Chrome.AudioLevelMeter({
mode: this.getStyle('audioLevelDisplayMode')
});
var audioLevelTransformer = new OT.AudioLevelTransformer();
this.on('audioLevelUpdated', function(evt) {
_audioLevelMeter.setValue(audioLevelTransformer.transform(evt.audioLevel));
});
widgets.audioLevel = _audioLevelMeter;
}
_chrome = new OT.Chrome({
parent: _container.domElement
}).set(widgets).on({
muted: OT.$.bind(this.publishAudio, this, false),
unmuted: OT.$.bind(this.publishAudio, this, true)
});
},
reset = OT.$.bind(function() {
if (_chrome) {
_chrome.destroy();
_chrome = null;
}
this.disconnect();
_microphone = null;
if (_targetElement) {
_targetElement.destroy();
_targetElement = null;
}
cleanupLocalStream();
if (_container) {
_container.destroy();
_container = null;
}
if (_session) {
this._.unpublishFromSession(_session, 'reset');
}
this.id = _domId = null;
this.stream = _stream = null;
_loaded = false;
this.session = _session = null;
if (!_state.isDestroyed()) _state.set('NotPublishing');
}, this);
this.publish = function(targetElement, properties) {
OT.debug('OT.Publisher: publish');
if ( _state.isAttemptingToPublish() || _state.isPublishing() ) reset();
_state.set('GetUserMedia');
_publishProperties = OT.$.defaults(properties || {}, {
publishAudio : true,
publishVideo : true,
mirror: true
});
if (!_publishProperties.constraints) {
_publishProperties.constraints = OT.$.clone(defaultConstraints);
if(_publishProperties.audioSource === null || _publishProperties.audioSource === false) {
_publishProperties.constraints.audio = false;
_publishProperties.publishAudio = false;
} else {
if(typeof _publishProperties.audioSource === 'object') {
if(_publishProperties.audioSource.deviceId != null) {
_publishProperties.audioSource = _publishProperties.audioSource.deviceId;
} else {
OT.warn('Invalid audioSource passed to Publisher. Expected either a device ID');
}
}
if (_publishProperties.audioSource) {
if (typeof _publishProperties.constraints.audio !== 'object') {
_publishProperties.constraints.audio = {};
}
if (!_publishProperties.constraints.audio.mandatory) {
_publishProperties.constraints.audio.mandatory = {};
}
if (!_publishProperties.constraints.audio.optional) {
_publishProperties.constraints.audio.optional = [];
}
_publishProperties.constraints.audio.mandatory.sourceId =
_publishProperties.audioSource;
}
}
if(_publishProperties.videoSource === null || _publishProperties.videoSource === false) {
_publishProperties.constraints.video = false;
_publishProperties.publishVideo = false;
} else {
if(typeof _publishProperties.videoSource === 'object') {
if(_publishProperties.videoSource.deviceId != null) {
_publishProperties.videoSource = _publishProperties.videoSource.deviceId;
} else {
OT.warn('Invalid videoSource passed to Publisher. Expected either a device ID');
}
}
if (_publishProperties.videoSource) {
if (typeof _publishProperties.constraints.video !== 'object') {
_publishProperties.constraints.video = {};
}
if (!_publishProperties.constraints.video.mandatory) {
_publishProperties.constraints.video.mandatory = {};
}
if (!_publishProperties.constraints.video.optional) {
_publishProperties.constraints.video.optional = [];
}
_publishProperties.constraints.video.mandatory.sourceId =
_publishProperties.videoSource;
}
if (_publishProperties.resolution) {
if (_publishProperties.resolution !== void 0 &&
!_validResolutions.hasOwnProperty(_publishProperties.resolution)) {
OT.warn('Invalid resolution passed to the Publisher. Got: ' +
_publishProperties.resolution + ' expecting one of "' +
OT.$.keys(_validResolutions).join('","') + '"');
} else {
_publishProperties.videoDimensions = _validResolutions[_publishProperties.resolution];
if (typeof _publishProperties.constraints.video !== 'object') {
_publishProperties.constraints.video = {};
}
if (!_publishProperties.constraints.video.mandatory) {
_publishProperties.constraints.video.mandatory = {};
}
if (!_publishProperties.constraints.video.optional) {
_publishProperties.constraints.video.optional = [];
}
_publishProperties.constraints.video.optional =
_publishProperties.constraints.video.optional.concat([
{minWidth: _publishProperties.videoDimensions.width},
{maxWidth: _publishProperties.videoDimensions.width},
{minHeight: _publishProperties.videoDimensions.height},
{maxHeight: _publishProperties.videoDimensions.height}
]);
}
}
if (_publishProperties.frameRate !== void 0 &&
OT.$.arrayIndexOf(_validFrameRates, _publishProperties.frameRate) === -1) {
OT.warn('Invalid frameRate passed to the publisher got: ' +
_publishProperties.frameRate + ' expecting one of ' + _validFrameRates.join(','));
delete _publishProperties.frameRate;
} else if (_publishProperties.frameRate) {
if (typeof _publishProperties.constraints.video !== 'object') {
_publishProperties.constraints.video = {};
}
if (!_publishProperties.constraints.video.mandatory) {
_publishProperties.constraints.video.mandatory = {};
}
if (!_publishProperties.constraints.video.optional) {
_publishProperties.constraints.video.optional = [];
}
_publishProperties.constraints.video.optional =
_publishProperties.constraints.video.optional.concat([
{ minFrameRate: _publishProperties.frameRate },
{ maxFrameRate: _publishProperties.frameRate }
]);
}
}
} else {
OT.warn('You have passed your own constraints not using ours');
}
if (_publishProperties.style) {
this.setStyle(_publishProperties.style, null, true);
}
if (_publishProperties.name) {
_publishProperties.name = _publishProperties.name.toString();
}
_publishProperties.classNames = 'OT_root OT_publisher';
// Defer actually creating the publisher DOM nodes until we know
// the DOM is actually loaded.
OT.onLoad(function() {
_container = new OT.WidgetView(targetElement, _publishProperties);
this.id = _domId = _container.domId();
this.element = _container.domElement;
OT.$.shouldAskForDevices(OT.$.bind(function(devices) {
if(!devices.video) {
OT.warn('Setting video constraint to false, there are no video sources');
_publishProperties.constraints.video = false;
}
if(!devices.audio) {
OT.warn('Setting audio constraint to false, there are no audio sources');
_publishProperties.constraints.audio = false;
}
OT.$.getUserMedia(
_publishProperties.constraints,
OT.$.bind(onStreamAvailable, this),
OT.$.bind(onStreamAvailableError, this),
OT.$.bind(onAccessDialogOpened, this),
OT.$.bind(onAccessDialogClosed, this),
OT.$.bind(onAccessDenied, this)
);
}, this));
}, this);
return this;
};
/**
* Starts publishing audio (if it is currently not being published)
* when the value is true; stops publishing audio
* (if it is currently being published) when the value is false.
*
* @param {Boolean} value Whether to start publishing audio (true)
* or not (false).
*
* @see OT.initPublisher()
* @see Stream.hasAudio
* @see StreamPropertyChangedEvent
* @method #publishAudio
* @memberOf Publisher
*/
this.publishAudio = function(value) {
_publishProperties.publishAudio = value;
if (_microphone) {
_microphone.muted(!value);
}
if (_chrome) {
_chrome.muteButton.muted(!value);
}
if (_session && _stream) {
_stream.setChannelActiveState('audio', value);
}
return this;
};
/**
* Starts publishing video (if it is currently not being published)
* when the value is true; stops publishing video
* (if it is currently being published) when the value is false.
*
* @param {Boolean} value Whether to start publishing video (true)
* or not (false).
*
* @see OT.initPublisher()
* @see Stream.hasVideo
* @see StreamPropertyChangedEvent
* @method #publishVideo
* @memberOf Publisher
*/
this.publishVideo = function(value) {
var oldValue = _publishProperties.publishVideo;
_publishProperties.publishVideo = value;
if (_session && _stream && _publishProperties.publishVideo !== oldValue) {
_stream.setChannelActiveState('video', value);
}
// We currently do this event if the value of publishVideo has not changed
// This is because the state of the video tracks enabled flag may not match
// the value of publishVideo at this point. This will be tidied up shortly.
if (_webRTCStream) {
var videoTracks = _webRTCStream.getVideoTracks();
for (var i=0, num=videoTracks.length; idestroyed event when the DOM
* element is removed.
*
* @method #destroy
* @memberOf Publisher
* @return {Publisher} The Publisher.
*/
this.destroy = function(/* unused */ reason, quiet) {
if (_state.isDestroyed()) return;
_state.set('Destroyed');
reset();
if (quiet !== true) {
this.dispatchEvent(
new OT.DestroyedEvent(
OT.Event.names.PUBLISHER_DESTROYED,
this,
reason
),
OT.$.bind(this.off,this)
);
}
return this;
};
/**
* @methodOf Publisher
* @private
*/
this.disconnect = function() {
// Close the connection to each of our subscribers
for (var fromConnectionId in _peerConnections) {
this.cleanupSubscriber(fromConnectionId);
}
};
this.cleanupSubscriber = function(fromConnectionId) {
var pc = _peerConnections[fromConnectionId];
if (pc) {
pc.destroy();
delete _peerConnections[fromConnectionId];
logAnalyticsEvent('disconnect', 'PeerConnection',
'subscriberConnection', fromConnectionId);
}
};
this.processMessage = function(type, fromConnection, message) {
OT.debug('OT.Publisher.processMessage: Received ' + type + ' from ' + fromConnection.id);
OT.debug(message);
switch (type) {
case 'unsubscribe':
this.cleanupSubscriber(message.content.connection.id);
break;
default:
var peerConnection = createPeerConnectionForRemote.call(this, fromConnection);
peerConnection.processMessage(type, message);
}
};
/**
* Returns the base-64-encoded string of PNG data representing the Publisher video.
*
* You can use the string as the value for a data URL scheme passed to the src parameter of * an image file, as in the following:
* *
* var imgData = publisher.getImgData();
*
* var img = document.createElement("img");
* img.setAttribute("src", "data:image/png;base64," + imgData);
* var imgWin = window.open("about:blank", "Screenshot");
* imgWin.document.write("<body></body>");
* imgWin.document.body.appendChild(img);
*
*
* @method #getImgData
* @memberOf Publisher
* @return {String} The base-64 encoded string. Returns an empty string if there is no video.
*/
this.getImgData = function() {
if (!_loaded) {
OT.error('OT.Publisher.getImgData: Cannot getImgData before the Publisher is publishing.');
return null;
}
return _targetElement.imgData();
};
// API Compatibility layer for Flash Publisher, this could do with some tidyup.
this._ = {
publishToSession: OT.$.bind(function(session) {
// Add session property to Publisher
this.session = _session = session;
var createStream = function() {
var streamWidth,
streamHeight;
// Bail if this.session is gone, it means we were unpublished
// before createStream could finish.
if (!_session) return;
_state.set('PublishingToSession');
var onStreamRegistered = OT.$.bind(function(err, streamId, message) {
if (err) {
// @todo we should respect err.code here and translate it to the local
// client equivalent.
logAnalyticsEvent('publish', 'Failure', 'reason',
'Publish:' + OT.ExceptionCodes.UNABLE_TO_PUBLISH + ':' + err.message);
if (_state.isAttemptingToPublish()) {
this.trigger('publishComplete', new OT.Error(OT.ExceptionCodes.UNABLE_TO_PUBLISH,
err.message));
}
return;
}
this.streamId = _streamId = streamId;
_iceServers = OT.Raptor.parseIceServers(message);
}, this);
// We set the streamWidth and streamHeight to be the minimum of the requested
// resolution and the actual resolution.
if (_publishProperties.videoDimensions) {
streamWidth = Math.min(_publishProperties.videoDimensions.width,
_targetElement.videoWidth() || 640);
streamHeight = Math.min(_publishProperties.videoDimensions.height,
_targetElement.videoHeight() || 480);
} else {
streamWidth = _targetElement.videoWidth() || 640;
streamHeight = _targetElement.videoHeight() || 480;
}
session._.streamCreate(
_publishProperties && _publishProperties.name ? _publishProperties.name : '',
OT.VideoOrientation.ROTATED_NORMAL,
streamWidth,
streamHeight,
_publishProperties.publishAudio,
_publishProperties.publishVideo,
_publishProperties.frameRate,
onStreamRegistered
);
};
if (_loaded) createStream.call(this);
else this.on('initSuccess', createStream, this);
logAnalyticsEvent('publish', 'Attempt', 'streamType', 'WebRTC');
return this;
}, this),
unpublishFromSession: OT.$.bind(function(session, reason) {
if (!_session || session.id !== _session.id) {
OT.warn('The publisher ' + _guid + ' is trying to unpublish from a session ' +
session.id + ' it is not attached to (it is attached to ' +
(_session && _session.id || 'no session') + ')');
return this;
}
if (session.isConnected() && this.stream) {
session._.streamDestroy(this.stream.id);
}
// Disconnect immediately, rather than wait for the WebSocket to
// reply to our destroyStream message.
this.disconnect();
this.session = _session = null;
// We're back to being a stand-alone publisher again.
if (!_state.isDestroyed()) _state.set('MediaBound');
logAnalyticsEvent('unpublish', 'Success', 'sessionId', session.id);
this._.streamDestroyed(reason);
return this;
}, this),
streamDestroyed: OT.$.bind(function(reason) {
if(OT.$.arrayIndexOf(['reset'], reason) < 0) {
var event = new OT.StreamEvent('streamDestroyed', _stream, reason, true);
var defaultAction = OT.$.bind(function() {
if(!event.isDefaultPrevented()) {
this.destroy();
}
}, this);
this.dispatchEvent(event, defaultAction);
}
}, this),
archivingStatus: OT.$.bind(function(status) {
if(_chrome) {
_chrome.archive.setArchiving(status);
}
return status;
}, this),
webRtcStream: function() {
return _webRTCStream;
}
};
this.detectDevices = function() {
OT.warn('Fixme: Haven\'t implemented detectDevices');
};
this.detectMicActivity = function() {
OT.warn('Fixme: Haven\'t implemented detectMicActivity');
};
this.getEchoCancellationMode = function() {
OT.warn('Fixme: Haven\'t implemented getEchoCancellationMode');
return 'fullDuplex';
};
this.setMicrophoneGain = function() {
OT.warn('Fixme: Haven\'t implemented setMicrophoneGain');
};
this.getMicrophoneGain = function() {
OT.warn('Fixme: Haven\'t implemented getMicrophoneGain');
return 0.5;
};
this.setCamera = function() {
OT.warn('Fixme: Haven\'t implemented setCamera');
};
this.setMicrophone = function() {
OT.warn('Fixme: Haven\'t implemented setMicrophone');
};
// Platform methods:
this.guid = function() {
return _guid;
};
this.videoElement = function() {
return _targetElement.domElement();
};
this.setStream = assignStream;
this.isWebRTC = true;
this.isLoading = function() {
return _container && _container.loading();
};
this.videoWidth = function() {
return _targetElement.videoWidth();
};
this.videoHeight = function() {
return _targetElement.videoHeight();
};
// Make read-only: element, guid, _.webRtcStream
this.on('styleValueChanged', updateChromeForStyleChange, this);
_state = new OT.PublishingState(stateChangeFailed);
this.accessAllowed = false;
/**
* Dispatched when the user has clicked the Allow button, granting the
* app access to the camera and microphone. The Publisher object has an
* accessAllowed property which indicates whether the user
* has granted access to the camera and microphone.
* @see Event
* @name accessAllowed
* @event
* @memberof Publisher
*/
/**
* Dispatched when the user has clicked the Deny button, preventing the
* app from having access to the camera and microphone.
*
* The default behavior of this event is to display a user interface element
* in the Publisher object, indicating that that user has denied access to
* the camera and microphone. Call the preventDefault() method
* method of the event object in the event listener to prevent this message
* from being displayed.
* @see Event
* @name accessDenied
* @event
* @memberof Publisher
*/
/**
* Dispatched when the Allow/Deny dialog box is opened. (This is the dialog box in which
* the user can grant the app access to the camera and microphone.)
*
* The default behavior of this event is to display a message in the browser that instructs
* the user how to enable the camera and microphone. Call the preventDefault()
* method of the event object in the event listener to prevent this message from being displayed.
* @see Event
* @name accessDialogOpened
* @event
* @memberof Publisher
*/
/**
* Dispatched when the Allow/Deny box is closed. (This is the dialog box in which the
* user can grant the app access to the camera and microphone.)
* @see Event
* @name accessDialogClosed
* @event
* @memberof Publisher
*/
/**
* Dispatched periodically to indicate the publisher's audio level. The event is dispatched
* up to 60 times per second, depending on the browser. The audioLevel property
* of the event is audio level, from 0 to 1.0. See {@link AudioLevelUpdatedEvent} for more
* information.
*
* The following example adjusts the value of a meter element that shows volume of the * publisher. Note that the audio level is adjusted logarithmically and a moving average * is applied: *
*
* var movingAvg = null;
* publisher.on('audioLevelUpdated', function(event) {
* if (movingAvg === null || movingAvg <= event.audioLevel) {
* movingAvg = event.audioLevel;
* } else {
* movingAvg = 0.7 * movingAvg + 0.3 * event.audioLevel;
* }
*
* // 1.5 scaling to map the -30 - 0 dBm range to [0,1]
* var logLevel = (Math.log(movingAvg) / Math.LN10) / 1.5 + 1;
* logLevel = Math.min(Math.max(logLevel, 0), 1);
* document.getElementById('publisherMeter').value = logLevel;
* });
*
* This example shows the algorithm used by the default audio level indicator displayed
* in an audio-only Publisher.
*
* @name audioLevelUpdated
* @event
* @memberof Publisher
* @see AudioLevelUpdatedEvent
*/
/**
* The publisher has started streaming to the session.
* @name streamCreated
* @event
* @memberof Publisher
* @see StreamEvent
* @see Session.publish()
*/
/**
* The publisher has stopped streaming to the session. The default behavior is that
* the Publisher object is removed from the HTML DOM). The Publisher object dispatches a
* destroyed event when the element is removed from the HTML DOM. If you call the
* preventDefault() method of the event object in the event listener, the default
* behavior is prevented, and you can, optionally, retain the Publisher for reuse or clean it up
* using your own code.
* @name streamDestroyed
* @event
* @memberof Publisher
* @see StreamEvent
*/
/**
* Dispatched when the Publisher element is removed from the HTML DOM. When this event
* is dispatched, you may choose to adjust or remove HTML DOM elements related to the publisher.
* @name destroyed
* @event
* @memberof Publisher
*/
};
// Helper function to generate unique publisher ids
OT.Publisher.nextId = OT.$.uuid;
})(window);
!(function() {
/**
* The Subscriber object is a representation of the local video element that is playing back
* a remote stream. The Subscriber object includes methods that let you disable and enable
* local audio playback for the subscribed stream. The subscribe() method of the
* {@link Session} object returns a Subscriber object.
*
* @property {Element} element The HTML DOM element containing the Subscriber.
* @property {String} id The DOM ID of the Subscriber.
* @property {Stream} stream The stream to which you are subscribing.
*
* @class Subscriber
* @augments EventDispatcher
*/
OT.Subscriber = function(targetElement, options) {
var _widgetId = OT.$.uuid(),
_domId = targetElement || _widgetId,
_container,
_streamContainer,
_chrome,
_audioLevelMeter,
_stream,
_fromConnectionId,
_peerConnection,
_session = options.session,
_subscribeStartTime,
_startConnectingTime,
_properties = OT.$.clone(options),
_analytics = new OT.Analytics(),
_audioVolume = 100,
_state,
_prevStats,
_lastSubscribeToVideoReason,
_audioLevelCapable = OT.$.hasCapabilities('audioOutputLevelStat') ||
OT.$.hasCapabilities('webAudioCapableRemoteStream'),
_audioLevelSampler,
_audioLevelRunner,
_frameRateRestricted = false;
this.id = _domId;
this.widgetId = _widgetId;
this.session = _session;
_prevStats = {
timeStamp: OT.$.now()
};
if (!_session) {
OT.handleJsException('Subscriber must be passed a session option', 2000, {
session: _session,
target: this
});
return;
}
OT.$.eventing(this, false);
if(_audioLevelCapable) {
this.on({
'audioLevelUpdated:added': function(count) {
if (count === 1 && _audioLevelRunner) {
_audioLevelRunner.start();
}
},
'audioLevelUpdated:removed': function(count) {
if (count === 0 && _audioLevelRunner) {
_audioLevelRunner.stop();
}
}
});
}
OT.StylableComponent(this, {
nameDisplayMode: 'auto',
buttonDisplayMode: 'auto',
audioLevelDisplayMode: 'auto',
videoDisabledIndicatorDisplayMode: 'auto',
backgroundImageURI: null,
showArchiveStatus: true,
showMicButton: true,
bugDisplayMode: 'auto'
});
var logAnalyticsEvent = function(action, variation, payloadType, payload) {
/* jshint camelcase:false*/
_analytics.logEvent({
action: action,
variation: variation,
payload_type: payloadType,
payload: payload,
stream_id: _stream ? _stream.id : null,
session_id: _session ? _session.sessionId : null,
connection_id: _session && _session.isConnected() ?
_session.connection.connectionId : null,
partner_id: _session && _session.isConnected() ? _session.sessionInfo.partnerId : null,
widget_id: _widgetId,
widget_type: 'Subscriber'
});
},
recordQOS = OT.$.bind(function(parsedStats) {
if(_state.isSubscribing() && _session && _session.isConnected()) {
/*jshint camelcase:false */
var QoSBlob = {
widget_type: 'Subscriber',
stream_type : 'WebRTC',
width: _container ? OT.$.width(_container.domElement) : undefined,
height: _container ? OT.$.height(_container.domElement) : undefined,
session_id: _session ? _session.sessionId : null,
connectionId: _session ? _session.connection.connectionId : null,
media_server_name: _session ? _session.sessionInfo.messagingServer : null,
p2pFlag: _session ? _session.sessionInfo.p2pEnabled : false,
partner_id: _session ? _session.apiKey : null,
stream_id: _stream.id,
widget_id: _widgetId,
version: OT.properties.version,
duration: parseInt(OT.$.now() - _subscribeStartTime, 10),
remote_connection_id: _stream.connection.connectionId
};
_analytics.logQOS( OT.$.extend(QoSBlob, parsedStats) );
this.trigger('qos', parsedStats);
}
}, this),
stateChangeFailed = function(changeFailed) {
OT.error('Subscriber State Change Failed: ', changeFailed.message);
OT.debug(changeFailed);
},
onLoaded = function() {
if (_state.isSubscribing() || !_streamContainer) return;
OT.debug('OT.Subscriber.onLoaded');
_state.set('Subscribing');
_subscribeStartTime = OT.$.now();
logAnalyticsEvent('createPeerConnection', 'Success', 'pcc|hasRelayCandidates', [
parseInt(_subscribeStartTime - _startConnectingTime, 10),
_peerConnection && _peerConnection.hasRelayCandidates()
].join('|'));
_container.loading(false);
_createChrome.call(this);
if(_frameRateRestricted) {
_stream.setRestrictFrameRate(true);
}
this.trigger('subscribeComplete', null, this);
this.trigger('loaded', this);
logAnalyticsEvent('subscribe', 'Success', 'streamId', _stream.id);
},
onDisconnected = function() {
OT.debug('OT.Subscriber has been disconnected from the Publisher\'s PeerConnection');
if (_state.isAttemptingToSubscribe()) {
// subscribing error
_state.set('Failed');
this.trigger('subscribeComplete', new OT.Error(null, 'ClientDisconnected'));
} else if (_state.isSubscribing()) {
_state.set('Failed');
// we were disconnected after we were already subscribing
// probably do nothing?
}
this.disconnect();
},
onPeerConnectionFailure = OT.$.bind(function(reason, peerConnection, prefix) {
if (_state.isAttemptingToSubscribe()) {
// We weren't subscribing yet so this was a failure in setting
// up the PeerConnection or receiving the initial stream.
logAnalyticsEvent('createPeerConnection', 'Failure', 'reason|hasRelayCandidates', [
'Subscriber PeerConnection Error: ' + reason,
_peerConnection && _peerConnection.hasRelayCandidates()
].join('|'));
_state.set('Failed');
this.trigger('subscribeComplete', new OT.Error(null, reason));
} else if (_state.isSubscribing()) {
// we were disconnected after we were already subscribing
_state.set('Failed');
this.trigger('error', reason);
}
this.disconnect();
logAnalyticsEvent('subscribe', 'Failure', 'reason',
(prefix ? prefix : '') + ':Subscriber PeerConnection Error: ' + reason);
OT.handleJsException('Subscriber PeerConnection Error: ' + reason,
OT.ExceptionCodes.P2P_CONNECTION_FAILED, {
session: _session,
target: this
}
);
_showError.call(this, reason);
}, this),
onRemoteStreamAdded = function(webOTStream) {
OT.debug('OT.Subscriber.onRemoteStreamAdded');
_state.set('BindingRemoteStream');
// Disable the audio/video, if needed
this.subscribeToAudio(_properties.subscribeToAudio);
_lastSubscribeToVideoReason = 'loading';
this.subscribeToVideo(_properties.subscribeToVideo, 'loading');
var videoContainerOptions = {
error: onPeerConnectionFailure,
audioVolume: _audioVolume
};
// This is a workaround for a bug in Chrome where a track disabled on
// the remote end doesn't fire loadedmetadata causing the subscriber to timeout
// https://jira.tokbox.com/browse/OPENTOK-15605
var browser = OT.$.browserVersion(),
tracks,
reenableVideoTrack = false;
if (!_stream.hasVideo && browser.browser === 'Chrome' && browser.version >= 35) {
tracks = webOTStream.getVideoTracks();
if(tracks.length > 0) {
tracks[0].enabled = false;
reenableVideoTrack = tracks[0];
}
}
_streamContainer = _container.bindVideo(webOTStream,
videoContainerOptions,
OT.$.bind(function(err) {
if (err) {
onPeerConnectionFailure(err.message || err, _peerConnection, 'VideoElement');
return;
}
// Continues workaround for https://jira.tokbox.com/browse/OPENTOK-15605
if (reenableVideoTrack != null && _properties.subscribeToVideo) {
reenableVideoTrack.enabled = true;
}
_streamContainer.orientation({
width: _stream.videoDimensions.width,
height: _stream.videoDimensions.height,
videoOrientation: _stream.videoDimensions.orientation
});
onLoaded.call(this, null);
}, this));
if (OT.$.hasCapabilities('webAudioCapableRemoteStream') && _audioLevelSampler) {
_audioLevelSampler.webOTStream = webOTStream;
}
logAnalyticsEvent('createPeerConnection', 'StreamAdded', '', '');
this.trigger('streamAdded', this);
},
onRemoteStreamRemoved = function(webOTStream) {
OT.debug('OT.Subscriber.onStreamRemoved');
if (_streamContainer.stream === webOTStream) {
_streamContainer.destroy();
_streamContainer = null;
}
this.trigger('streamRemoved', this);
},
streamDestroyed = function () {
this.disconnect();
},
streamUpdated = function(event) {
switch(event.changedProperty) {
case 'videoDimensions':
_streamContainer.orientation({
width: event.newValue.width,
height: event.newValue.height,
videoOrientation: event.newValue.orientation
});
break;
case 'videoDisableWarning':
_chrome.videoDisabledIndicator.setWarning(event.newValue);
this.dispatchEvent(new OT.VideoDisableWarningEvent(
event.newValue ? 'videoDisableWarning' : 'videoDisableWarningLifted'
));
break;
case 'hasVideo':
if(_container) {
var audioOnly = !(_stream.hasVideo && _properties.subscribeToVideo);
_container.audioOnly(audioOnly);
_container.showPoster(audioOnly);
}
this.dispatchEvent(new OT.VideoEnabledChangedEvent(
_stream.hasVideo ? 'videoEnabled' : 'videoDisabled', {
reason: 'publishVideo'
}));
break;
case 'hasAudio':
// noop
}
},
/// Chrome
// If mode is false, then that is the mode. If mode is true then we'll
// definitely display the button, but we'll defer the model to the
// Publishers buttonDisplayMode style property.
chromeButtonMode = function(mode) {
if (mode === false) return 'off';
var defaultMode = this.getStyle('buttonDisplayMode');
// The default model is false, but it's overridden by +mode+ being true
if (defaultMode === false) return 'on';
// defaultMode is either true or auto.
return defaultMode;
},
updateChromeForStyleChange = function(key, value/*, oldValue*/) {
if (!_chrome) return;
switch(key) {
case 'nameDisplayMode':
_chrome.name.setDisplayMode(value);
_chrome.backingBar.setNameMode(value);
break;
case 'videoDisabledDisplayMode':
_chrome.videoDisabledIndicator.setDisplayMode(value);
break;
case 'showArchiveStatus':
_chrome.archive.setShowArchiveStatus(value);
break;
case 'buttonDisplayMode':
_chrome.muteButton.setDisplayMode(value);
_chrome.backingBar.setMuteMode(value);
break;
case 'audioLevelDisplayMode':
_chrome.audioLevel.setDisplayMode(value);
break;
case 'bugDisplayMode':
// bugDisplayMode can't be updated but is used by some partners
case 'backgroundImageURI':
_container.setBackgroundImageURI(value);
}
},
_createChrome = function() {
if(this.getStyle('bugDisplayMode') === 'off') {
logAnalyticsEvent('bugDisplayMode', 'createChrome', 'mode', 'off');
}
var widgets = {
backingBar: new OT.Chrome.BackingBar({
nameMode: !_properties.name ? 'off' : this.getStyle('nameDisplayMode'),
muteMode: chromeButtonMode.call(this, this.getStyle('showMuteButton'))
}),
name: new OT.Chrome.NamePanel({
name: _properties.name,
mode: this.getStyle('nameDisplayMode'),
bugMode: this.getStyle('bugDisplayMode')
}),
muteButton: new OT.Chrome.MuteButton({
muted: _properties.muted,
mode: chromeButtonMode.call(this, this.getStyle('showMuteButton'))
}),
opentokButton: new OT.Chrome.OpenTokButton({
mode: this.getStyle('bugDisplayMode')
}),
archive: new OT.Chrome.Archiving({
show: this.getStyle('showArchiveStatus'),
archiving: false
})
};
if(_audioLevelCapable) {
_audioLevelMeter = new OT.Chrome.AudioLevelMeter({
mode: this.getStyle('audioLevelDisplayMode')
});
var audioLevelTransformer = new OT.AudioLevelTransformer();
this.on('audioLevelUpdated', function(evt) {
_audioLevelMeter.setValue(audioLevelTransformer.transform(evt.audioLevel));
});
widgets.audioLevel = _audioLevelMeter;
}
widgets.videoDisabledIndicator = new OT.Chrome.VideoDisabledIndicator({
mode: this.getStyle('videoDisabledDisplayMode')
});
_chrome = new OT.Chrome({
parent: _container.domElement
}).set(widgets).on({
muted: function() {
muteAudio.call(this, true);
},
unmuted: function() {
muteAudio.call(this, false);
}
}, this);
},
_showError = function() {
// Display the error message inside the container, assuming it's
// been created by now.
if (_container) {
_container.addError(
'The stream was unable to connect due to a network error.',
'Make sure your connection isn\'t blocked by a firewall.'
);
}
};
this.subscribe = function(stream) {
OT.debug('OT.Subscriber: subscribe to ' + stream.id);
if (_state.isSubscribing()) {
// @todo error
OT.error('OT.Subscriber.Subscribe: Cannot subscribe, already subscribing.');
return false;
}
_state.set('Init');
if (!stream) {
// @todo error
OT.error('OT.Subscriber: No stream parameter.');
return false;
}
if (_stream) {
// @todo error
OT.error('OT.Subscriber: Already subscribed');
return false;
}
this.stream = _stream = stream;
this.streamId = _stream.id;
_stream.on({
updated: streamUpdated,
destroyed: streamDestroyed
}, this);
_fromConnectionId = stream.connection.id;
_properties.name = _properties.name || _stream.name;
_properties.classNames = 'OT_root OT_subscriber';
if (_properties.style) {
this.setStyle(_properties.style, null, true);
}
if (_properties.audioVolume) {
this.setAudioVolume(_properties.audioVolume);
}
_properties.subscribeToAudio = OT.$.castToBoolean(_properties.subscribeToAudio, true);
_properties.subscribeToVideo = OT.$.castToBoolean(_properties.subscribeToVideo, true);
_container = new OT.WidgetView(targetElement, _properties);
this.id = _domId = _container.domId();
this.element = _container.domElement;
_startConnectingTime = OT.$.now();
if (_stream.connection.id !== _session.connection.id) {
logAnalyticsEvent('createPeerConnection', 'Attempt', '', '');
_state.set('ConnectingToPeer');
_peerConnection = new OT.SubscriberPeerConnection(_stream.connection, _session,
_stream, this, _properties);
_peerConnection.on({
disconnected: onDisconnected,
error: onPeerConnectionFailure,
remoteStreamAdded: onRemoteStreamAdded,
remoteStreamRemoved: onRemoteStreamRemoved,
qos: recordQOS
}, this);
// initialize the peer connection AFTER we've added the event listeners
_peerConnection.init();
if (OT.$.hasCapabilities('audioOutputLevelStat')) {
_audioLevelSampler = new OT.GetStatsAudioLevelSampler(_peerConnection, 'out');
} else if (OT.$.hasCapabilities('webAudioCapableRemoteStream')) {
_audioLevelSampler = new OT.AnalyserAudioLevelSampler(new window.AudioContext());
}
if(_audioLevelSampler) {
var subscriber = this;
// sample with interval to minimise disturbance on animation loop but dispatch the
// event with RAF since the main purpose is animation of a meter
_audioLevelRunner = new OT.IntervalRunner(function() {
_audioLevelSampler.sample(function(audioOutputLevel) {
if (audioOutputLevel !== null) {
OT.$.requestAnimationFrame(function() {
subscriber.dispatchEvent(
new OT.AudioLevelUpdatedEvent(audioOutputLevel));
});
}
});
}, 60);
}
} else {
logAnalyticsEvent('createPeerConnection', 'Attempt', '', '');
var publisher = _session.getPublisherForStream(_stream);
if(!(publisher && publisher._.webRtcStream())) {
this.trigger('subscribeComplete', new OT.Error(null, 'InvalidStreamID'));
return this;
}
// Subscribe to yourself edge-case
onRemoteStreamAdded.call(this, publisher._.webRtcStream());
}
logAnalyticsEvent('subscribe', 'Attempt', 'streamId', _stream.id);
return this;
};
this.destroy = function(reason, quiet) {
if (_state.isDestroyed()) return;
if(reason === 'streamDestroyed') {
if (_state.isAttemptingToSubscribe()) {
// We weren't subscribing yet so the stream was destroyed before we setup
// the PeerConnection or receiving the initial stream.
this.trigger('subscribeComplete', new OT.Error(null, 'InvalidStreamID'));
}
}
_state.set('Destroyed');
if(_audioLevelRunner) {
_audioLevelRunner.stop();
}
this.disconnect();
if (_chrome) {
_chrome.destroy();
_chrome = null;
}
if (_container) {
_container.destroy();
_container = null;
this.element = null;
}
if (_stream && !_stream.destroyed) {
logAnalyticsEvent('unsubscribe', null, 'streamId', _stream.id);
}
this.id = _domId = null;
this.stream = _stream = null;
this.streamId = null;
this.session =_session = null;
_properties = null;
if (quiet !== true) {
this.dispatchEvent(
new OT.DestroyedEvent(
OT.Event.names.SUBSCRIBER_DESTROYED,
this,
reason
),
OT.$.bind(this.off, this)
);
}
return this;
};
this.disconnect = function() {
if (!_state.isDestroyed() && !_state.isFailed()) {
// If we are already in the destroyed state then disconnect
// has been called after (or from within) destroy.
_state.set('NotSubscribing');
}
if (_streamContainer) {
_streamContainer.destroy();
_streamContainer = null;
}
if (_peerConnection) {
_peerConnection.destroy();
_peerConnection = null;
logAnalyticsEvent('disconnect', 'PeerConnection', 'streamId', _stream.id);
}
};
this.processMessage = function(type, fromConnection, message) {
OT.debug('OT.Subscriber.processMessage: Received ' + type + ' message from ' +
fromConnection.id);
OT.debug(message);
if (_fromConnectionId !== fromConnection.id) {
_fromConnectionId = fromConnection.id;
}
if (_peerConnection) {
_peerConnection.processMessage(type, message);
}
};
this.disableVideo = function(active) {
if (!active) {
OT.warn('Due to high packet loss and low bandwidth, video has been disabled');
} else {
if (_lastSubscribeToVideoReason === 'auto') {
OT.info('Video has been re-enabled');
_chrome.videoDisabledIndicator.disableVideo(false);
} else {
OT.info('Video was not re-enabled because it was manually disabled');
return;
}
}
this.subscribeToVideo(active, 'auto');
if(!active) {
_chrome.videoDisabledIndicator.disableVideo(true);
}
logAnalyticsEvent('updateQuality', 'video', active ? 'videoEnabled' : 'videoDisabled', true);
};
/**
* Return the base-64-encoded string of PNG data representing the Subscriber video.
*
*
You can use the string as the value for a data URL scheme passed to the src parameter of * an image file, as in the following:
* *
* var imgData = subscriber.getImgData();
*
* var img = document.createElement("img");
* img.setAttribute("src", "data:image/png;base64," + imgData);
* var imgWin = window.open("about:blank", "Screenshot");
* imgWin.document.write("<body></body>");
* imgWin.document.body.appendChild(img);
*
* @method #getImgData
* @memberOf Subscriber
* @return {String} The base-64 encoded string. Returns an empty string if there is no video.
*/
this.getImgData = function() {
if (!this.isSubscribing()) {
OT.error('OT.Subscriber.getImgData: Cannot getImgData before the Subscriber ' +
'is subscribing.');
return null;
}
return _streamContainer.imgData();
};
/**
* Sets the audio volume, between 0 and 100, of the Subscriber.
*
* You can set the initial volume when you call the Session.subscribe()
* method. Pass a audioVolume property of the properties parameter
* of the method.
mySubscriber.setAudioVolume(50).setStyle(newStyle);* * @see getAudioVolume() * @see Session.subscribe() * @method #setAudioVolume * @memberOf Subscriber */ this.setAudioVolume = function(value) { value = parseInt(value, 10); if (isNaN(value)) { OT.error('OT.Subscriber.setAudioVolume: value should be an integer between 0 and 100'); return this; } _audioVolume = Math.max(0, Math.min(100, value)); if (_audioVolume !== value) { OT.warn('OT.Subscriber.setAudioVolume: value should be an integer between 0 and 100'); } if(_properties.muted && _audioVolume > 0) { _properties.premuteVolume = value; muteAudio.call(this, false); } if (_streamContainer) { _streamContainer.setAudioVolume(_audioVolume); } return this; }; /** * Returns the audio volume, between 0 and 100, of the Subscriber. * *
Generally you use this method in conjunction with the setAudioVolume()
* method.
value is true; stops
* subscribing to audio (if it is currently being subscribed to) when the value
* is false.
* * Note: This method only affects the local playback of audio. It has no impact on the * audio for other connections subscribing to the same stream. If the Publsher is not * publishing audio, enabling the Subscriber audio will have no practical effect. *
* * @param {Boolean} value Whether to start subscribing to audio (true) or not
* (false).
*
* @return {Subscriber} The Subscriber object. This lets you chain method calls, as in the
* following:
*
* mySubscriber.subscribeToAudio(true).subscribeToVideo(false);* * @see subscribeToVideo() * @see Session.subscribe() * @see StreamPropertyChangedEvent * * @method #subscribeToAudio * @memberOf Subscriber */ this.subscribeToAudio = function(pValue) { var value = OT.$.castToBoolean(pValue, true); if (_peerConnection) { _peerConnection.subscribeToAudio(value && !_properties.subscribeMute); if (_session && _stream && value !== _properties.subscribeToAudio) { _stream.setChannelActiveState('audio', value && !_properties.subscribeMute); } } _properties.subscribeToAudio = value; return this; }; var muteAudio = function(_mute) { _chrome.muteButton.muted(_mute); if(_mute === _properties.mute) { return; } if(OT.$.browser() === 'Chrome' || TBPlugin.isInstalled()) { _properties.subscribeMute = _properties.muted = _mute; this.subscribeToAudio(_properties.subscribeToAudio); } else { if(_mute) { _properties.premuteVolume = this.getAudioVolume(); _properties.muted = true; this.setAudioVolume(0); } else if(_properties.premuteVolume || _properties.audioVolume) { _properties.muted = false; this.setAudioVolume(_properties.premuteVolume || _properties.audioVolume); } } _properties.mute = _properties.mute; }; var reasonMap = { auto: 'quality', publishVideo: 'publishVideo', subscribeToVideo: 'subscribeToVideo' }; /** * Toggles video on and off. Starts subscribing to video (if it is available and * currently not being subscribed to) when the
value is true;
* stops subscribing to video (if it is currently being subscribed to) when the
* value is false.
* * Note: This method only affects the local playback of video. It has no impact on * the video for other connections subscribing to the same stream. If the Publsher is not * publishing video, enabling the Subscriber video will have no practical video. *
* * @param {Boolean} value Whether to start subscribing to video (true) or not
* (false).
*
* @return {Subscriber} The Subscriber object. This lets you chain method calls, as in the
* following:
*
* mySubscriber.subscribeToVideo(true).subscribeToAudio(false);* * @see subscribeToAudio() * @see Session.subscribe() * @see StreamPropertyChangedEvent * * @method #subscribeToVideo * @memberOf Subscriber */ this.subscribeToVideo = function(pValue, reason) { var value = OT.$.castToBoolean(pValue, true); if(_container) { var audioOnly = !(value && _stream.hasVideo); _container.audioOnly(audioOnly); _container.showPoster(audioOnly); if(value && _container.video()) { _container.loading(value); _container.video().whenTimeIncrements(function(){ _container.loading(false); }, this); } } if (_chrome && _chrome.videoDisabledIndicator) { _chrome.videoDisabledIndicator.disableVideo(false); } if (_peerConnection) { _peerConnection.subscribeToVideo(value); if (_session && _stream && (value !== _properties.subscribeToVideo || reason !== _lastSubscribeToVideoReason)) { _stream.setChannelActiveState('video', value, reason); } } _properties.subscribeToVideo = value; _lastSubscribeToVideoReason = reason; if (reason !== 'loading') { this.dispatchEvent(new OT.VideoEnabledChangedEvent( value ? 'videoEnabled' : 'videoDisabled', { reason: reasonMap[reason] || 'subscribeToVideo' } )); } return this; }; this.isSubscribing = function() { return _state.isSubscribing(); }; this.isWebRTC = true; this.isLoading = function() { return _container && _container.loading(); }; this.videoWidth = function() { return _streamContainer.videoWidth(); }; this.videoHeight = function() { return _streamContainer.videoHeight(); }; /** * Restricts the frame rate of the Subscriber's video stream, when you pass in *
true. When you pass in false, the frame rate of the video stream
* is not restricted.
* * When the frame rate is restricted, the Subscriber video frame will update once or less per * second. *
* This feature is only available in sessions that use the OpenTok Media Router (sessions with * the media mode * set to routed), not in sessions with the media mode set to relayed. In relayed sessions, * calling this method has no effect. *
* Restricting the subscriber frame rate has the following benefits: *
* Reducing a subscriber's frame rate has no effect on the frame rate of the video in
* other clients.
*
* @param {Boolean} value Whether to restrict the Subscriber's video frame rate
* (true) or not (false).
*
* @return {Subscriber} The Subscriber object. This lets you chain method calls, as in the
* following:
*
*
mySubscriber.restrictFrameRate(false).subscribeToAudio(true);* * @method #restrictFrameRate * @memberOf Subscriber */ this.restrictFrameRate = function(val) { OT.debug('OT.Subscriber.restrictFrameRate(' + val + ')'); logAnalyticsEvent('restrictFrameRate', val.toString(), 'streamId', _stream.id); if (_session.sessionInfo.p2pEnabled) { OT.warn('OT.Subscriber.restrictFrameRate: Cannot restrictFrameRate on a P2P session'); } if (typeof val !== 'boolean') { OT.error('OT.Subscriber.restrictFrameRate: expected a boolean value got a ' + typeof val); } else { _frameRateRestricted = val; _stream.setRestrictFrameRate(val); } return this; }; this.on('styleValueChanged', updateChromeForStyleChange, this); this._ = { archivingStatus: function(status) { if(_chrome) { _chrome.archive.setArchiving(status); } } }; _state = new OT.SubscribingState(stateChangeFailed); /** * Dispatched periodically to indicate the subscriber's audio level. The event is dispatched * up to 60 times per second, depending on the browser. The
audioLevel property
* of the event is audio level, from 0 to 1.0. See {@link AudioLevelUpdatedEvent} for more
* information.
* * The following example adjusts the value of a meter element that shows volume of the * subscriber. Note that the audio level is adjusted logarithmically and a moving average * is applied: *
* var movingAvg = null;
* subscriber.on('audioLevelUpdated', function(event) {
* if (movingAvg === null || movingAvg <= event.audioLevel) {
* movingAvg = event.audioLevel;
* } else {
* movingAvg = 0.7 * movingAvg + 0.3 * event.audioLevel;
* }
*
* // 1.5 scaling to map the -30 - 0 dBm range to [0,1]
* var logLevel = (Math.log(movingAvg) / Math.LN10) / 1.5 + 1;
* logLevel = Math.min(Math.max(logLevel, 0), 1);
* document.getElementById('subscriberMeter').value = logLevel;
* });
*
* This example shows the algorithm used by the default audio level indicator displayed * in an audio-only Subscriber. * * @name audioLevelUpdated * @event * @memberof Subscriber * @see AudioLevelUpdatedEvent */ /** * Dispatched when the video for the subscriber is disabled. *
* The reason property defines the reason the video was disabled. This can be set to
* one of the following values:
*
* *
"publishVideo" — The publisher stopped publishing video by calling
* publishVideo(false)."quality" — The OpenTok Media Router stopped sending video
* to the subscriber based on stream quality changes. This feature of the OpenTok Media
* Router has a subscriber drop the video stream when connectivity degrades. (The subscriber
* continues to receive the audio stream, if there is one.)
*
* Before sending this event, when the Subscriber's stream quality deteriorates to a level
* that is low enough that the video stream is at risk of being disabled, the Subscriber
* dispatches a videoDisableWarning event.
*
* If connectivity improves to support video again, the Subscriber object dispatches
* a videoEnabled event, and the Subscriber resumes receiving video.
*
* By default, the Subscriber displays a video disabled indicator when a
* videoDisabled event with this reason is dispatched and removes the indicator
* when the videoDisabled event with this reason is dispatched. You can control
* the display of this icon by calling the setStyle() method of the Subscriber,
* setting the videoDisabledDisplayMode property(or you can set the style when
* calling the Session.subscribe() method, setting the style property
* of the properties parameter).
*
* This feature is only available in sessions that use the OpenTok Media Router (sessions with * the media mode * set to routed), not in sessions with the media mode set to relayed. *
"subscribeToVideo" — The subscriber started or stopped subscribing to
* video, by calling subscribeToVideo(false).
* videoDisabled event.
*
* By default, the Subscriber displays a video disabled warning indicator when this event
* is dispatched (and the video is disabled). You can control the display of this icon by
* calling the setStyle() method and setting the
* videoDisabledDisplayMode property (or you can set the style when calling
* the Session.subscribe() method and setting the style property
* of the properties parameter).
*
* This feature is only available in sessions that use the OpenTok Media Router (sessions with
* the media mode
* set to routed), not in sessions with the media mode set to relayed.
*
* @see Event
* @see event:videoDisabled
* @see event:videoDisableWarningLifted
* @name videoDisableWarning
* @event
* @memberof Subscriber
*/
/**
* Dispatched when the OpenTok Media Router determines that the stream quality has improved
* to the point at which the video being disabled is not an immediate risk. This event is
* dispatched after the Subscriber object dispatches a videoDisableWarning event.
*
* This feature is only available in sessions that use the OpenTok Media Router (sessions with * the media mode * set to routed), not in sessions with the media mode set to relayed. * * @see Event * @see event:videoDisabled * @see event:videoDisableWarning * @name videoDisableWarningLifted * @event * @memberof Subscriber */ /** * Dispatched when the OpenTok Media Router resumes sending video to the subscriber * after video was previously disabled. *
* The reason property defines the reason the video was enabled. This can be set to
* one of the following values:
*
* *
"publishVideo" — The publisher started publishing video by calling
* publishVideo(true)."quality" — The OpenTok Media Router resumed sending video
* to the subscriber based on stream quality changes. This feature of the OpenTok Media
* Router has a subscriber drop the video stream when connectivity degrades and then resume
* the video stream if the stream quality improves.
* * This feature is only available in sessions that use the OpenTok Media Router (sessions with * the media mode * set to routed), not in sessions with the media mode set to relayed. *
"subscribeToVideo" — The subscriber started or stopped subscribing to
* video, by calling subscribeToVideo(false).
*
* To prevent video from resuming, in the videoEnabled event listener,
* call subscribeToVideo(false) on the Subscriber object.
*
* @see VideoEnabledChangedEvent
* @see event:videoDisabled
* @name videoEnabled
* @event
* @memberof Subscriber
*/
/**
* Dispatched when the Subscriber element is removed from the HTML DOM. When this event is
* dispatched, you may choose to adjust or remove HTML DOM elements related to the subscriber.
* @see Event
* @name destroyed
* @event
* @memberof Subscriber
*/
};
})(window);
!(function() {
var parseErrorFromJSONDocument,
onGetResponseCallback,
onGetErrorCallback;
OT.SessionInfo = function(jsonDocument) {
var sessionJSON = jsonDocument[0];
OT.log('SessionInfo Response:');
OT.log(jsonDocument);
/*jshint camelcase:false*/
this.sessionId = sessionJSON.session_id;
this.partnerId = sessionJSON.partner_id;
this.sessionStatus = sessionJSON.session_status;
this.messagingServer = sessionJSON.messaging_server_url;
this.messagingURL = sessionJSON.messaging_url;
this.symphonyAddress = sessionJSON.symphony_address;
this.p2pEnabled = !!(sessionJSON.properties &&
sessionJSON.properties.p2p &&
sessionJSON.properties.p2p.preference &&
sessionJSON.properties.p2p.preference.value === 'enabled');
};
// Retrieves Session Info for +session+. The SessionInfo object will be passed
// to the +onSuccess+ callback. The +onFailure+ callback will be passed an error
// object and the DOMEvent that relates to the error.
OT.SessionInfo.get = function(session, onSuccess, onFailure) {
var sessionInfoURL = OT.properties.apiURL + '/session/' + session.id + '?extended=true',
browser = OT.$.browserVersion(),
startTime = OT.$.now(),
options,
validateRawSessionInfo = function(sessionInfo) {
session.logEvent('Instrumentation', null, 'gsi', OT.$.now() - startTime);
var error = parseErrorFromJSONDocument(sessionInfo);
if (error === false) {
onGetResponseCallback(session, onSuccess, sessionInfo);
} else {
onGetErrorCallback(session, onFailure, error, JSON.stringify(sessionInfo));
}
};
if(browser.browser === 'IE' && browser.version < 10) {
sessionInfoURL = sessionInfoURL + '&format=json&token=' + encodeURIComponent(session.token) +
'&version=1&cache=' + OT.$.uuid();
options = {
xdomainrequest: true
};
}
else {
options = {
headers: {
'X-TB-TOKEN-AUTH': session.token,
'X-TB-VERSION': 1
}
};
}
session.logEvent('getSessionInfo', 'Attempt', 'api_url', OT.properties.apiURL);
OT.$.getJSON(sessionInfoURL, options, function(error, sessionInfo) {
if(error) {
var responseText = sessionInfo;
onGetErrorCallback(session, onFailure,
new OT.Error(error.target && error.target.status || error.code, error.message ||
'Could not connect to the OpenTok API Server.'), responseText);
} else {
validateRawSessionInfo(sessionInfo);
}
});
};
var messageServerToClientErrorCodes = {};
messageServerToClientErrorCodes['404'] = OT.ExceptionCodes.INVALID_SESSION_ID;
messageServerToClientErrorCodes['409'] = OT.ExceptionCodes.INVALID_SESSION_ID;
messageServerToClientErrorCodes['400'] = OT.ExceptionCodes.INVALID_SESSION_ID;
messageServerToClientErrorCodes['403'] = OT.ExceptionCodes.AUTHENTICATION_ERROR;
// Return the error in +jsonDocument+, if there is one. Otherwise it will return
// false.
parseErrorFromJSONDocument = function(jsonDocument) {
if(OT.$.isArray(jsonDocument)) {
var errors = OT.$.filter(jsonDocument, function(node) {
return node.error != null;
});
var numErrorNodes = errors.length;
if(numErrorNodes === 0) {
return false;
}
var errorCode = errors[0].error.code;
if (messageServerToClientErrorCodes[errorCode.toString()]) {
errorCode = messageServerToClientErrorCodes[errorCode];
}
return {
code: errorCode,
message: errors[0].error.errorMessage && errors[0].error.errorMessage.message
};
} else {
return {
code: null,
message: 'Unknown error: getSessionInfo JSON response was badly formed'
};
}
};
onGetResponseCallback = function(session, onSuccess, rawSessionInfo) {
session.logEvent('getSessionInfo', 'Success', 'api_url', OT.properties.apiURL);
onSuccess( new OT.SessionInfo(rawSessionInfo) );
};
onGetErrorCallback = function(session, onFailure, error, responseText) {
session.logEvent('Connect', 'Failure', 'errorMessage',
'GetSessionInfo:' + (error.code || 'No code') + ':' + error.message + ':' +
(responseText || 'Empty responseText from API server'));
onFailure(error, session);
};
})(window);
!(function() {
/**
* A class defining properties of the capabilities property of a
* Session object. See Session.capabilities.
*
* All Capabilities properties are undefined until you have connected to a session
* and the Session object has dispatched the sessionConnected event.
*
* For more information on token roles, see the
* generate_token()
* method of the OpenTok server-side libraries.
*
* @class Capabilities
*
* @property {Number} forceDisconnect Specifies whether you can call
* the Session.forceDisconnect() method (1) or not (0). To call the
* Session.forceDisconnect() method,
* the user must have a token that is assigned the role of moderator.
* @property {Number} forceUnpublish Specifies whether you can call
* the Session.forceUnpublish() method (1) or not (0). To call the
* Session.forceUnpublish() method, the user must have a token that
* is assigned the role of moderator.
* @property {Number} publish Specifies whether you can publish to the session (1) or not (0).
* The ability to publish is based on a few factors. To publish, the user must have a token that
* is assigned a role that supports publishing. There must be a connected camera and microphone.
* @property {Number} subscribe Specifies whether you can subscribe to streams
* in the session (1) or not (0). Currently, this capability is available for all users on all
* platforms.
*/
OT.Capabilities = function(permissions) {
this.publish = OT.$.arrayIndexOf(permissions, 'publish') !== -1 ? 1 : 0;
this.subscribe = OT.$.arrayIndexOf(permissions, 'subscribe') !== -1 ? 1 : 0;
this.forceUnpublish = OT.$.arrayIndexOf(permissions, 'forceunpublish') !== -1 ? 1 : 0;
this.forceDisconnect = OT.$.arrayIndexOf(permissions, 'forcedisconnect') !== -1 ? 1 : 0;
this.supportsWebRTC = OT.$.hasCapabilities('webrtc') ? 1 : 0;
this.permittedTo = function(action) {
return this.hasOwnProperty(action) && this[action] === 1;
};
};
})(window);
!(function(window) {
/**
* The Session object returned by the OT.initSession() method provides access to
* much of the OpenTok functionality.
*
* @class Session
* @augments EventDispatcher
*
* @property {Capabilities} capabilities A {@link Capabilities} object that includes information
* about the capabilities of the client. All properties of the capabilities object
* are undefined until you have connected to a session and the Session object has dispatched the
* sessionConnected event.
* @property {Connection} connection The {@link Connection} object for this session. The
* connection property is only available once the Session object dispatches the sessionConnected
* event. The Session object asynchronously dispatches a sessionConnected event in response
* to a successful call to the connect() method. See: connect and
* {@link Connection}.
* @property {String} sessionId The session ID for this session. You pass this value into the
* OT.initSession() method when you create the Session object. (Note: a Session
* object is not connected to the OpenTok server until you call the connect() method of the
* object and the object dispatches a connected event. See {@link OT.initSession} and
* {@link connect}).
* For more information on sessions and session IDs, see
* Session creation.
*/
OT.Session = function(apiKey, sessionId) {
OT.$.eventing(this);
// Check that the client meets the minimum requirements, if they don't the upgrade
// flow will be triggered.
if (!OT.checkSystemRequirements()) {
OT.upgradeSystemRequirements();
return;
}
if(sessionId == null) {
sessionId = apiKey;
apiKey = null;
}
this.id = this.sessionId = sessionId;
var _initialConnection = true,
_apiKey = apiKey,
_token,
_sessionId = sessionId,
_socket,
_widgetId = OT.$.uuid(),
_connectionId,
_analytics = new OT.Analytics(),
sessionConnectFailed,
sessionDisconnectedHandler,
connectionCreatedHandler,
connectionDestroyedHandler,
streamCreatedHandler,
streamPropertyModifiedHandler,
streamDestroyedHandler,
archiveCreatedHandler,
archiveDestroyedHandler,
archiveUpdatedHandler,
reset,
disconnectComponents,
destroyPublishers,
destroySubscribers,
connectMessenger,
getSessionInfo,
onSessionInfoResponse,
permittedTo,
dispatchError;
var setState = OT.$.statable(this, [
'disconnected', 'connecting', 'connected', 'disconnecting'
], 'disconnected');
this.connection = null;
this.connections = new OT.Collection();
this.streams = new OT.Collection();
this.archives = new OT.Collection();
//--------------------------------------
// MESSAGE HANDLERS
//--------------------------------------
// The duplication of this and sessionConnectionFailed will go away when
// session and messenger are refactored
sessionConnectFailed = function(reason, code) {
setState('disconnected');
OT.error(reason);
this.trigger('sessionConnectFailed',
new OT.Error(code || OT.ExceptionCodes.CONNECT_FAILED, reason));
OT.handleJsException(reason, code || OT.ExceptionCodes.CONNECT_FAILED, {
session: this
});
};
sessionDisconnectedHandler = function(event) {
var reason = event.reason;
if(reason === 'networkTimedout') {
reason = 'networkDisconnected';
this.logEvent('Connect', 'TimeOutDisconnect', 'reason', event.reason);
} else {
this.logEvent('Connect', 'Disconnected', 'reason', event.reason);
}
var publicEvent = new OT.SessionDisconnectEvent('sessionDisconnected', reason);
reset.call(this);
disconnectComponents.call(this, reason);
var defaultAction = OT.$.bind(function() {
// Publishers handle preventDefault'ing themselves
destroyPublishers.call(this, publicEvent.reason);
// Subscriers don't, destroy 'em if needed
if (!publicEvent.isDefaultPrevented()) destroySubscribers.call(this, publicEvent.reason);
}, this);
this.dispatchEvent(publicEvent, defaultAction);
};
connectionCreatedHandler = function(connection) {
// We don't broadcast events for the symphony connection
if (connection.id.match(/^symphony\./)) return;
this.dispatchEvent(new OT.ConnectionEvent(
OT.Event.names.CONNECTION_CREATED,
connection
));
};
connectionDestroyedHandler = function(connection, reason) {
// We don't broadcast events for the symphony connection
if (connection.id.match(/^symphony\./)) return;
// Don't delete the connection if it's ours. This only happens when
// we're about to receive a session disconnected and session disconnected
// will also clean up our connection.
if (connection.id === _socket.id()) return;
this.dispatchEvent(
new OT.ConnectionEvent(
OT.Event.names.CONNECTION_DESTROYED,
connection,
reason
)
);
};
streamCreatedHandler = function(stream) {
if(stream.connection.id !== this.connection.id) {
this.dispatchEvent(new OT.StreamEvent(
OT.Event.names.STREAM_CREATED,
stream,
null,
false
));
}
};
streamPropertyModifiedHandler = function(event) {
var stream = event.target,
propertyName = event.changedProperty,
newValue = event.newValue;
if (propertyName === 'videoDisableWarning' || propertyName === 'audioDisableWarning') {
return; // These are not public properties, skip top level event for them.
}
if (propertyName === 'orientation') {
propertyName = 'videoDimensions';
newValue = {width: newValue.width, height: newValue.height};
}
this.dispatchEvent(new OT.StreamPropertyChangedEvent(
OT.Event.names.STREAM_PROPERTY_CHANGED,
stream,
propertyName,
event.oldValue,
newValue
));
};
streamDestroyedHandler = function(stream, reason) {
// if the stream is one of ours we delegate handling
// to the publisher itself.
if(stream.connection.id === this.connection.id) {
OT.$.forEach(OT.publishers.where({ streamId: stream.id }), OT.$.bind(function(publisher) {
publisher._.unpublishFromSession(this, reason);
}, this));
return;
}
var event = new OT.StreamEvent('streamDestroyed', stream, reason, true);
var defaultAction = OT.$.bind(function() {
if (!event.isDefaultPrevented()) {
// If we are subscribed to any of the streams we should unsubscribe
OT.$.forEach(OT.subscribers.where({streamId: stream.id}), function(subscriber) {
if (subscriber.session.id === this.id) {
if(subscriber.stream) {
subscriber.destroy('streamDestroyed');
}
}
}, this);
} else {
// @TODO Add a one time warning that this no longer cleans up the publisher
}
}, this);
this.dispatchEvent(event, defaultAction);
};
archiveCreatedHandler = function(archive) {
this.dispatchEvent(new OT.ArchiveEvent('archiveStarted', archive));
};
archiveDestroyedHandler = function(archive) {
this.dispatchEvent(new OT.ArchiveEvent('archiveDestroyed', archive));
};
archiveUpdatedHandler = function(event) {
var archive = event.target,
propertyName = event.changedProperty,
newValue = event.newValue;
if(propertyName === 'status' && newValue === 'stopped') {
this.dispatchEvent(new OT.ArchiveEvent('archiveStopped', archive));
} else {
this.dispatchEvent(new OT.ArchiveEvent('archiveUpdated', archive));
}
};
// Put ourselves into a pristine state
reset = function() {
this.token = _token = null;
setState('disconnected');
this.connection = null;
this.capabilities = new OT.Capabilities([]);
this.connections.destroy();
this.streams.destroy();
this.archives.destroy();
};
disconnectComponents = function(reason) {
OT.$.forEach(OT.publishers.where({session: this}), function(publisher) {
publisher.disconnect(reason);
});
OT.$.forEach(OT.subscribers.where({session: this}), function(subscriber) {
subscriber.disconnect();
});
};
destroyPublishers = function(reason) {
OT.$.forEach(OT.publishers.where({session: this}), function(publisher) {
publisher._.streamDestroyed(reason);
});
};
destroySubscribers = function(reason) {
OT.$.forEach(OT.subscribers.where({session: this}), function(subscriber) {
subscriber.destroy(reason);
});
};
connectMessenger = function() {
OT.debug('OT.Session: connecting to Raptor');
var socketUrl = this.sessionInfo.messagingURL,
symphonyUrl = OT.properties.symphonyAddresss || this.sessionInfo.symphonyAddress;
_socket = new OT.Raptor.Socket(_widgetId, socketUrl, symphonyUrl,
OT.SessionDispatcher(this));
var analyticsPayload = [
socketUrl, OT.$.userAgent(), OT.properties.version,
window.externalHost ? 'yes' : 'no'
];
_socket.connect(_token, this.sessionInfo, OT.$.bind(function(error, sessionState) {
if (error) {
_socket = void 0;
analyticsPayload.splice(0,0,error.message);
this.logEvent('Connect', 'Failure',
'reason|webSocketServerUrl|userAgent|sdkVersion|chromeFrame',
analyticsPayload.map(function(e) { return e.replace('|', '\\|'); }).join('|'));
sessionConnectFailed.call(this, error.message, error.code);
return;
}
OT.debug('OT.Session: Received session state from Raptor', sessionState);
this.connection = this.connections.get(_socket.id());
if(this.connection) {
this.capabilities = this.connection.permissions;
}
setState('connected');
this.logEvent('Connect', 'Success',
'webSocketServerUrl|userAgent|sdkVersion|chromeFrame',
OT.$.map(analyticsPayload, function(e) {
return e.replace('|', '\\|');
}).join('|'), {connectionId: this.connection.id});
// Listen for our own connection's destroyed event so we know when we've been disconnected.
this.connection.on('destroyed', sessionDisconnectedHandler, this);
// Listen for connection updates
this.connections.on({
add: connectionCreatedHandler,
remove: connectionDestroyedHandler
}, this);
// Listen for stream updates
this.streams.on({
add: streamCreatedHandler,
remove: streamDestroyedHandler,
update: streamPropertyModifiedHandler
}, this);
this.archives.on({
add: archiveCreatedHandler,
remove: archiveDestroyedHandler,
update: archiveUpdatedHandler
}, this);
this.dispatchEvent(
new OT.SessionConnectEvent(OT.Event.names.SESSION_CONNECTED), OT.$.bind(function() {
this.connections._triggerAddEvents(); // { id: this.connection.id }
this.streams._triggerAddEvents(); // { id: this.stream.id }
this.archives._triggerAddEvents();
}, this)
);
}, this));
};
getSessionInfo = function() {
if (this.is('connecting')) {
OT.SessionInfo.get(
this,
OT.$.bind(onSessionInfoResponse, this),
OT.$.bind(function(error) {
sessionConnectFailed.call(this, error.message +
(error.code ? ' (' + error.code + ')' : ''), error.code);
}, this)
);
}
};
onSessionInfoResponse = function(sessionInfo) {
if (this.is('connecting')) {
var overrides = OT.properties.sessionInfoOverrides;
this.sessionInfo = sessionInfo;
if (overrides != null && typeof overrides === 'object') {
this.sessionInfo = OT.$.defaults(overrides, this.sessionInfo);
console.log('is', this.sessionInfo);
}
if (this.sessionInfo.partnerId && this.sessionInfo.partnerId !== _apiKey) {
this.apiKey = _apiKey = this.sessionInfo.partnerId;
var reason = 'Authentication Error: The API key does not match the token or session.';
this.logEvent('Connect', 'Failure', 'reason', 'GetSessionInfo:' +
OT.ExceptionCodes.AUTHENTICATION_ERROR + ':' + reason);
sessionConnectFailed.call(this, reason, OT.ExceptionCodes.AUTHENTICATION_ERROR);
} else {
connectMessenger.call(this);
}
}
};
// Check whether we have permissions to perform the action.
permittedTo = OT.$.bind(function(action) {
return this.capabilities.permittedTo(action);
}, this);
dispatchError = OT.$.bind(function(code, message, completionHandler) {
OT.dispatchError(code, message, completionHandler, this);
}, this);
this.logEvent = function(action, variation, payloadType, payload, options) {
/* jshint camelcase:false */
var event = {
action: action,
variation: variation,
payload_type: payloadType,
payload: payload,
session_id: _sessionId,
partner_id: _apiKey,
widget_id: _widgetId,
widget_type: 'Controller'
};
if (this.connection && this.connection.id) _connectionId = event.connection_id =
this.connection.id;
else if (_connectionId) event.connection_id = _connectionId;
if (options) event = OT.$.extend(options, event);
_analytics.logEvent(event);
};
/**
* Connects to an OpenTok session.
*
* Upon a successful connection, the completion handler (the second parameter of the method) is * invoked without an error object passed in. (If there is an error connecting, the completion * handler is invoked with an error object.) Make sure that you have successfully connected to the * session before calling other methods of the Session object. *
*
* The Session object dispatches a connectionCreated event when any client
* (including your own) connects to to the session.
*
* The following code initializes a session and sets up an event listener for when the session * connects: *
*
* var apiKey = ""; // Replace with your API key. See https://dashboard.tokbox.com/projects
* var sessionID = ""; // Replace with your own session ID.
* // See https://dashboard.tokbox.com/projects
* var token = ""; // Replace with a generated token that has been assigned the moderator role.
* // See https://dashboard.tokbox.com/projects
*
* var session = OT.initSession(apiKey, sessionID);
* session.on("sessionConnected", function(sessionConnectEvent) {
* //
* });
* session.connect(token);
*
* *
* In this example, the sessionConnectHandler() function is passed an event
* object of type {@link SessionConnectEvent}.
*
* exception (ExceptionEvent) Dispatched
* by the OT class locally in the event of an error.
*
* connectionCreated (ConnectionEvent)
* Dispatched by the Session object on all clients connected to the session.
*
* sessionConnected (SessionConnectEvent)
* Dispatched locally by the Session object when the connection is established.
*
connect() method succeeds or fails. This function takes one parameter —
* error (see the Error object).
* On success, the completionHandler function is not passed any
* arguments. On error, the function is passed an error object parameter
* (see the Error object). The
* error object has two properties: code (an integer) and
* message (a string), which identify the cause of the failure. The following
* code adds a completionHandler when calling the connect() method:
*
* session.connect(token, function (error) {
* if (error) {
* console.log(error.message);
* } else {
* console.log("Connected to session.");
* }
* });
*
*
* Note that upon connecting to the session, the Session object dispatches a
* sessionConnected event in addition to calling the completionHandler.
* The SessionConnectEvent object, which defines the sessionConnected event,
* includes connections and streams properties, which
* list the connections and streams in the session when you connect.
*
* Calling the disconnect() method ends your connection with the session. In the
* course of terminating your connection, it also ceases publishing any stream(s) you were
* publishing.
*
* Session objects on remote clients dispatch streamDestroyed events for any
* stream you were publishing. The Session object dispatches a sessionDisconnected
* event locally. The Session objects on remote clients dispatch connectionDestroyed
* events, letting other connections know you have left the session. The
* {@link SessionDisconnectEvent} and {@link StreamEvent} objects that define the
* sessionDisconnect and connectionDestroyed events each have a
* reason property. The reason property lets the developer determine
* whether the connection is being terminated voluntarily and whether any streams are being
* destroyed as a byproduct of the underlying connection's voluntary destruction.
*
* If the session is not currently connected, calling this method causes a warning to be logged. * See OT.setLogLevel(). *
* *
* Note: If you intend to reuse a Publisher object created using
* OT.initPublisher() to publish to different sessions sequentially, call either
* Session.disconnect() or Session.unpublish(). Do not call both.
* Then call the preventDefault() method of the streamDestroyed or
* sessionDisconnected event object to prevent the Publisher object from being
* removed from the page. Be sure to call preventDefault() only if the
* connection.connectionId property of the Stream object in the event matches the
* connection.connectionId property of your Session object (to ensure that you are
* preventing the default behavior for your published streams, not for other streams that you
* subscribe to).
*
* sessionDisconnected
* (SessionDisconnectEvent)
* Dispatched locally when the connection is disconnected.
*
* connectionDestroyed (ConnectionEvent)
* Dispatched on other clients, along with the streamDestroyed event (as warranted).
*
* streamDestroyed (StreamEvent)
* Dispatched on other clients if streams are lost as a result of the session disconnecting.
*
publish() method starts publishing an audio-video stream to the session.
* The audio-video stream is captured from a local microphone and webcam. Upon successful
* publishing, the Session objects on all connected clients dispatch the
* streamCreated event.
*
*
*
* You pass a Publisher object as the one parameter of the method. You can initialize a
* Publisher object by calling the OT.initPublisher()
* method. Before calling Session.publish().
*
This method takes an alternate form: publish([targetElement:String,
* properties:Object]):Publisher In this form, you do not pass a Publisher
* object into the function. Instead, you pass in a targetElement (the ID of the
* DOM element that the Publisher will replace) and a properties object that
* defines options for the Publisher (see OT.initPublisher().)
* The method returns a new Publisher object, which starts sending an audio-video stream to the
* session. The remainder of this documentation describes the form that takes a single Publisher
* object as a parameter.
*
*
* A local display of the published stream is created on the web page by replacing * the specified element in the DOM with a streaming video display. The video stream * is automatically mirrored horizontally so that users see themselves and movement * in their stream in a natural way. If the width and height of the display do not match * the 4:3 aspect ratio of the video signal, the video stream is cropped to fit the * display. *
* ** If calling this method creates a new Publisher object and the OpenTok library does not * have access to the camera or microphone, the web page alerts the user to grant access * to the camera and microphone. *
* *
* The OT object dispatches an exception event if the user's role does not
* include permissions required to publish. For example, if the user's role is set to subscriber,
* then they cannot publish. You define a user's role when you create the user token using the
* generate_token() method of the
* OpenTok server-side
* libraries or the Dashboard page.
* You pass the token string as a parameter of the connect() method of the Session
* object. See ExceptionEvent and
* OT.on().
*
* The application throws an error if the session is not connected. *
* *
* exception (ExceptionEvent) Dispatched
* by the OT object. This can occur when user's role does not allow publishing (the
* code property of event object is set to 1500); it can also occur if the c
* onnection fails to connect (the code property of event object is set to 1013).
* WebRTC is a peer-to-peer protocol, and it is possible that connections will fail to connect.
* The most common cause for failure is a firewall that the protocol cannot traverse.
*
* streamCreated (StreamEvent)
* The stream has been published. The Session object dispatches this on all clients
* subscribed to the stream, as well as on the publisher's client.
*
* The following example publishes a video once the session connects: *
*
* var sessionId = ""; // Replace with your own session ID.
* // See https://dashboard.tokbox.com/projects
* var token = ""; // Replace with a generated token that has been assigned the moderator role.
* // See https://dashboard.tokbox.com/projects
* var session = OT.initSession(apiKey, sessionID);
* session.on("sessionConnected", function (event) {
* var publisherOptions = {width: 400, height:300, name:"Bob's stream"};
* // This assumes that there is a DOM element with the ID 'publisher':
* publisher = OT.initPublisher('publisher', publisherOptions);
* session.publish(publisher);
* });
* session.connect(token);
*
*
* @param {Publisher} publisher A Publisher object, which you initialize by calling the
* OT.initPublisher() method.
*
* @param {Function} completionHandler (Optional) A function to be called when the call to the
* publish() method succeeds or fails. This function takes one parameter —
* error. On success, the completionHandler function is not passed any
* arguments. On error, the function is passed an error object parameter
* (see the Error object). The
* error object has two properties: code (an integer) and
* message (a string), which identify the cause of the failure. Calling
* publish() fails if the role assigned to your token is not "publisher" or
* "moderator"; in this case error.code is set to 1500. Calling
* publish() also fails the client fails to connect; in this case
* error.code is set to 1013. The following code adds a
* completionHandler when calling the publish() method:
*
* session.publish(publisher, null, function (error) {
* if (error) {
* console.log(error.message);
* } else {
* console.log("Publishing a stream.");
* }
* });
*
*
* @returns The Publisher object for this stream.
*
* @method #publish
* @memberOf Session
*/
this.publish = function(publisher, properties, completionHandler) {
if(typeof publisher === 'function') {
completionHandler = publisher;
publisher = undefined;
}
if(typeof properties === 'function') {
completionHandler = properties;
properties = undefined;
}
if (this.isNot('connected')) {
/*jshint camelcase:false*/
_analytics.logError(1010, 'OT.exception',
'We need to be connected before you can publish', null, {
action: 'publish',
variation: 'Failure',
payload_type: 'reason',
payload: 'We need to be connected before you can publish',
session_id: _sessionId,
partner_id: _apiKey,
widgetId: _widgetId,
widget_type: 'Controller'
});
if (completionHandler && OT.$.isFunction(completionHandler)) {
dispatchError(OT.ExceptionCodes.NOT_CONNECTED,
'We need to be connected before you can publish', completionHandler);
}
return null;
}
if (!permittedTo('publish')) {
this.logEvent('publish', 'Failure', 'reason',
'This token does not allow publishing. The role must be at least `publisher` ' +
'to enable this functionality');
dispatchError(OT.ExceptionCodes.UNABLE_TO_PUBLISH,
'This token does not allow publishing. The role must be at least `publisher` ' +
'to enable this functionality', completionHandler);
return null;
}
// If the user has passed in an ID of a element then we create a new publisher.
if (!publisher || typeof(publisher)==='string' || OT.$.isElementNode(publisher)) {
// Initiate a new Publisher with the new session credentials
publisher = OT.initPublisher(publisher, properties);
} else if (publisher instanceof OT.Publisher){
// If the publisher already has a session attached to it we can
if ('session' in publisher && publisher.session && 'sessionId' in publisher.session) {
// send a warning message that we can't publish again.
if( publisher.session.sessionId === this.sessionId){
OT.warn('Cannot publish ' + publisher.guid() + ' again to ' +
this.sessionId + '. Please call session.unpublish(publisher) first.');
} else {
OT.warn('Cannot publish ' + publisher.guid() + ' publisher already attached to ' +
publisher.session.sessionId+ '. Please call session.unpublish(publisher) first.');
}
}
} else {
dispatchError(OT.ExceptionCodes.UNABLE_TO_PUBLISH,
'Session.publish :: First parameter passed in is neither a ' +
'string nor an instance of the Publisher',
completionHandler);
return;
}
publisher.once('publishComplete', function(err) {
if (err) {
dispatchError(OT.ExceptionCodes.UNABLE_TO_PUBLISH,
'Session.publish :: ' + err.message,
completionHandler);
return;
}
if (completionHandler && OT.$.isFunction(completionHandler)) {
completionHandler.apply(null, arguments);
}
});
// Add publisher reference to the session
publisher._.publishToSession(this);
// return the embed publisher
return publisher;
};
/**
* Ceases publishing the specified publisher's audio-video stream
* to the session. By default, the local representation of the audio-video stream is
* removed from the web page. Upon successful termination, the Session object on every
* connected web page dispatches
* a streamDestroyed event.
*
*
*
* To prevent the Publisher from being removed from the DOM, add an event listener for the
* streamDestroyed event dispatched by the Publisher object and call the
* preventDefault() method of the event object.
*
* Note: If you intend to reuse a Publisher object created using
* OT.initPublisher() to publish to different sessions sequentially, call
* either Session.disconnect() or Session.unpublish(). Do not call
* both. Then call the preventDefault() method of the streamDestroyed
* or sessionDisconnected event object to prevent the Publisher object from being
* removed from the page. Be sure to call preventDefault() only if the
* connection.connectionId property of the Stream object in the event matches the
* connection.connectionId property of your Session object (to ensure that you are
* preventing the default behavior for your published streams, not for other streams that you
* subscribe to).
*
* streamDestroyed (StreamEvent)
* The stream associated with the Publisher has been destroyed. Dispatched on by the
* Publisher on on the Publisher's browser. Dispatched by the Session object on
* all other connections subscribing to the publisher's stream.
*
* <script>
* var apiKey = ""; // Replace with your API key. See https://dashboard.tokbox.com/projects
* var sessionID = ""; // Replace with your own session ID.
* // See https://dashboard.tokbox.com/projects
* var token = "Replace with the TokBox token string provided to you."
* var session = OT.initSession(apiKey, sessionID);
* session.on("sessionConnected", function sessionConnectHandler(event) {
* // This assumes that there is a DOM element with the ID 'publisher':
* publisher = OT.initPublisher('publisher');
* session.publish(publisher);
* });
* session.connect(token);
* var publisher;
*
* function unpublish() {
* session.unpublish(publisher);
* }
* </script>
*
* <body>
*
* <div id="publisherContainer/>
* <br/>
*
* <a href="javascript:unpublish()">Stop Publishing</a>
*
* </body>
*
*
*
* @see publish()
*
* @see streamDestroyed event
*
* @param {Publisher} publisher The Publisher object to stop streaming.
*
* @method #unpublish
* @memberOf Session
*/
this.unpublish = function(publisher) {
if (!publisher) {
OT.error('OT.Session.unpublish: publisher parameter missing.');
return;
}
// Unpublish the localMedia publisher
publisher._.unpublishFromSession(this, 'unpublished');
};
/**
* Subscribes to a stream that is available to the session. You can get an array of
* available streams from the streams property of the sessionConnected
* and streamCreated events (see
* SessionConnectEvent and
* StreamEvent).
*
* * The subscribed stream is displayed on the local web page by replacing the specified element * in the DOM with a streaming video display. If the width and height of the display do not * match the 4:3 aspect ratio of the video signal, the video stream is cropped to fit * the display. If the stream lacks a video component, a blank screen with an audio indicator * is displayed in place of the video stream. *
* *
* The application throws an error if the session is not connected or if the
* targetElement does not exist in the HTML DOM.
*
* var apiKey = ""; // Replace with your API key. See https://dashboard.tokbox.com/projects
* var sessionID = ""; // Replace with your own session ID.
* // See https://dashboard.tokbox.com/projects
*
* var session = OT.initSession(apiKey, sessionID);
* session.on("streamCreated", function(event) {
* subscriber = session.subscribe(event.stream, targetElement);
* });
* session.connect(token);
*
*
* @param {Stream} stream The Stream object representing the stream to which we are trying to
* subscribe.
*
* @param {Object} targetElement (Optional) The DOM element or the id attribute of
* the existing DOM element used to determine the location of the Subscriber video in the HTML
* DOM. See the insertMode property of the properties parameter. If
* you do not specify a targetElement, the application appends a new DOM element
* to the HTML body.
*
* @param {Object} properties This is an object that contains the following properties:
* audioVolume (Number) The desired audio volume, between 0 and
* 100, when the Subscriber is first opened (default: 50). After you subscribe to the
* stream, you can adjust the volume by calling the
* setAudioVolume() method of the
* Subscriber object. This volume setting affects local playback only; it does not affect
* the stream's volume on other clients.height (Number) The desired height, in pixels, of the
* displayed Subscriber video stream (default: 198). Note: Use the
* height and width properties to set the dimensions
* of the Subscriber video; do not set the height and width of the DOM element
* (using CSS).insertMode (String) Specifies how the Subscriber object will
* be inserted in the HTML DOM. See the targetElement parameter. This
* string can have the following values:
* "replace" The Subscriber object replaces contents of the
* targetElement. This is the default."after" The Subscriber object is a new element inserted
* after the targetElement in the HTML DOM. (Both the Subscriber and targetElement
* have the same parent element.)"before" The Subscriber object is a new element inserted
* before the targetElement in the HTML DOM. (Both the Subsciber and targetElement
* have the same parent element.)"append" The Subscriber object is a new element added as a
* child of the targetElement. If there are other child elements, the Subscriber is
* appended as the last child element of the targetElement.style (Object) An object containing properties that define the initial
* appearance of user interface controls of the Subscriber. The style object
* includes the following properties:
* audioLevelDisplayMode (String) — How to display the audio level
* indicator. Possible values are: "auto" (the indicator is displayed when the
* video is disabled), "off" (the indicator is not displayed), and
* "on" (the indicator is always displayed).backgroundImageURI (String) — A URI for an image to display as
* the background image when a video is not displayed. (A video may not be displayed if
* you call subscribeToVideo(false) on the Subscriber object). You can pass an
* http or https URI to a PNG, JPEG, or non-animated GIF file location. You can also use the
* data URI scheme (instead of http or https) and pass in base-64-encrypted
* PNG data, such as that obtained from the
* Subscriber.getImgData() method. For example,
* you could set the property to "data:VBORw0KGgoAA...", where the portion of
* the string after "data:" is the result of a call to
* Subscriber.getImgData(). If the URL or the image data is invalid, the
* property is ignored (the attempt to set the image fails silently).
*
* Note that in Internet Explorer 8 (using the OpenTok Plugin for Internet Explorer),
* you cannot set the backgroundImageURI style to a string larger than
* 32 kB. This is due to an IE 8 limitation on the size of URI strings. Due to this
* limitation, you cannot set the backgroundImageURI style to a string obtained
* with the getImgData() method.
*
buttonDisplayMode (String) — How to display the speaker controls
* Possible values are: "auto" (controls are displayed when the stream is first
* displayed and when the user mouses over the display), "off" (controls are not
* displayed), and "on" (controls are always displayed).nameDisplayMode (String) Whether to display the stream name.
* Possible values are: "auto" (the name is displayed when the stream is first
* displayed and when the user mouses over the display), "off" (the name is not
* displayed), and "on" (the name is always displayed).videoDisabledDisplayMode (String) Whether to display the video
* disabled indicator and video disabled warning icons for a Subscriber. These icons
* indicate that the video has been disabled (or is in risk of being disabled for
* the warning icon) due to poor stream quality. This style only applies to the Subscriber
* object. Possible values are: "auto" (the icons are automatically when the
* displayed video is disabled or in risk of being disabled due to poor stream quality),
* "off" (do not display the icons), and "on" (display the
* icons). The default setting is "auto"subscribeToAudio (Boolean) Whether to initially subscribe to audio
* (if available) for the stream (default: true).subscribeToVideo (Boolean) Whether to initially subscribe to video
* (if available) for the stream (default: true).width (Number) The desired width, in pixels, of the
* displayed Subscriber video stream (default: 264). Note: Use the
* height and width properties to set the dimensions
* of the Subscriber video; do not set the height and width of the DOM element
* (using CSS).subscribe() method succeeds or fails. This function takes one parameter —
* error. On success, the completionHandler function is not passed any
* arguments. On error, the function is passed an error object, defined by the
* Error class, has two properties: code (an integer) and
* message (a string), which identify the cause of the failure. The following
* code adds a completionHandler when calling the subscribe() method:
*
* session.subscribe(stream, "subscriber", null, function (error) {
* if (error) {
* console.log(error.message);
* } else {
* console.log("Subscribed to stream: " + stream.id);
* }
* });
*
*
* @signature subscribe(stream, targetElement, properties, completionHandler)
* @returns {Subscriber} The Subscriber object for this stream. Stream control functions
* are exposed through the Subscriber object.
* @method #subscribe
* @memberOf Session
*/
this.subscribe = function(stream, targetElement, properties, completionHandler) {
if (!this.connection || !this.connection.connectionId) {
dispatchError(OT.ExceptionCodes.UNABLE_TO_SUBSCRIBE,
'Session.subscribe :: Connection required to subscribe',
completionHandler);
return;
}
if (!stream) {
dispatchError(OT.ExceptionCodes.UNABLE_TO_SUBSCRIBE,
'Session.subscribe :: stream cannot be null',
completionHandler);
return;
}
if (!stream.hasOwnProperty('streamId')) {
dispatchError(OT.ExceptionCodes.UNABLE_TO_SUBSCRIBE,
'Session.subscribe :: invalid stream object',
completionHandler);
return;
}
if(typeof targetElement === 'function') {
completionHandler = targetElement;
targetElement = undefined;
}
if(typeof properties === 'function') {
completionHandler = properties;
properties = undefined;
}
var subscriber = new OT.Subscriber(targetElement, OT.$.extend(properties || {}, {
session: this
}));
subscriber.once('subscribeComplete', function(err) {
if (err) {
dispatchError(OT.ExceptionCodes.UNABLE_TO_SUBSCRIBE,
'Session.subscribe :: ' + err.message,
completionHandler);
return;
}
if (completionHandler && OT.$.isFunction(completionHandler)) {
completionHandler.apply(null, arguments);
}
});
OT.subscribers.add(subscriber);
subscriber.subscribe(stream);
return subscriber;
};
/**
* Stops subscribing to a stream in the session. the display of the audio-video stream is
* removed from the local web page.
*
* * The following code subscribes to other clients' streams. For each stream, the code also * adds an Unsubscribe link. *
*
* var apiKey = ""; // Replace with your API key. See https://dashboard.tokbox.com/projects
* var sessionID = ""; // Replace with your own session ID.
* // See https://dashboard.tokbox.com/projects
* var streams = [];
*
* var session = OT.initSession(apiKey, sessionID);
* session.on("streamCreated", function(event) {
* var stream = event.stream;
* displayStream(stream);
* });
* session.connect(token);
*
* function displayStream(stream) {
* var div = document.createElement('div');
* div.setAttribute('id', 'stream' + stream.streamId);
*
* var subscriber = session.subscribe(stream, div);
* subscribers.push(subscriber);
*
* var aLink = document.createElement('a');
* aLink.setAttribute('href', 'javascript: unsubscribe("' + subscriber.id + '")');
* aLink.innerHTML = "Unsubscribe";
*
* var streamsContainer = document.getElementById('streamsContainer');
* streamsContainer.appendChild(div);
* streamsContainer.appendChild(aLink);
*
* streams = event.streams;
* }
*
* function unsubscribe(subscriberId) {
* console.log("unsubscribe called");
* for (var i = 0; i < subscribers.length; i++) {
* var subscriber = subscribers[i];
* if (subscriber.id == subscriberId) {
* session.unsubscribe(subscriber);
* }
* }
* }
*
*
* @param {Subscriber} subscriber The Subscriber object to unsubcribe.
*
* @see subscribe()
*
* @method #unsubscribe
* @memberOf Session
*/
this.unsubscribe = function(subscriber) {
if (!subscriber) {
var errorMsg = 'OT.Session.unsubscribe: subscriber cannot be null';
OT.error(errorMsg);
throw new Error(errorMsg);
}
if (!subscriber.stream) {
OT.warn('OT.Session.unsubscribe:: tried to unsubscribe a subscriber that had no stream');
return false;
}
OT.debug('OT.Session.unsubscribe: subscriber ' + subscriber.id);
subscriber.destroy();
return true;
};
/**
* Returns an array of local Subscriber objects for a given stream.
*
* @param {Stream} stream The stream for which you want to find subscribers.
*
* @returns {Array} An array of {@link Subscriber} objects for the specified stream.
*
* @see unsubscribe()
* @see Subscriber
* @see StreamEvent
* @method #getSubscribersForStream
* @memberOf Session
*/
this.getSubscribersForStream = function(stream) {
return OT.subscribers.where({streamId: stream.id});
};
/**
* Returns the local Publisher object for a given stream.
*
* @param {Stream} stream The stream for which you want to find the Publisher.
*
* @returns {Publisher} A Publisher object for the specified stream. Returns
* null if there is no local Publisher object
* for the specified stream.
*
* @see forceUnpublish()
* @see Subscriber
* @see StreamEvent
*
* @method #getPublisherForStream
* @memberOf Session
*/
this.getPublisherForStream = function(stream) {
var streamId,
errorMsg;
if (typeof stream === 'string') {
streamId = stream;
} else if (typeof stream === 'object' && stream && stream.hasOwnProperty('id')) {
streamId = stream.id;
} else {
errorMsg = 'Session.getPublisherForStream :: Invalid stream type';
OT.error(errorMsg);
throw new Error(errorMsg);
}
return OT.publishers.where({streamId: streamId})[0];
};
// Private Session API: for internal OT use only
this._ = {
jsepCandidateP2p: function(streamId, subscriberId, candidate) {
return _socket.jsepCandidateP2p(streamId, subscriberId, candidate);
},
jsepCandidate: function(streamId, candidate) {
return _socket.jsepCandidate(streamId, candidate);
},
jsepOffer: function(streamId, offerSdp) {
return _socket.jsepOffer(streamId, offerSdp);
},
jsepOfferP2p: function(streamId, subscriberId, offerSdp) {
return _socket.jsepOfferP2p(streamId, subscriberId, offerSdp);
},
jsepAnswer: function(streamId, answerSdp) {
return _socket.jsepAnswer(streamId, answerSdp);
},
jsepAnswerP2p: function(streamId, subscriberId, answerSdp) {
return _socket.jsepAnswerP2p(streamId, subscriberId, answerSdp);
},
// session.on("signal", function(SignalEvent))
// session.on("signal:{type}", function(SignalEvent))
dispatchSignal: OT.$.bind(function(fromConnection, type, data) {
var event = new OT.SignalEvent(type, data, fromConnection);
event.target = this;
// signal a "signal" event
// NOTE: trigger doesn't support defaultAction, and therefore preventDefault.
this.trigger(OT.Event.names.SIGNAL, event);
// signal an "signal:{type}" event" if there was a custom type
if (type) this.dispatchEvent(event);
}, this),
subscriberCreate: function(stream, subscriber, channelsToSubscribeTo, completion) {
return _socket.subscriberCreate(stream.id, subscriber.widgetId,
channelsToSubscribeTo, completion);
},
subscriberDestroy: function(stream, subscriber) {
return _socket.subscriberDestroy(stream.id, subscriber.widgetId);
},
subscriberUpdate: function(stream, subscriber, attributes) {
return _socket.subscriberUpdate(stream.id, subscriber.widgetId, attributes);
},
subscriberChannelUpdate: function(stream, subscriber, channel, attributes) {
return _socket.subscriberChannelUpdate(stream.id, subscriber.widgetId, channel.id,
attributes);
},
streamCreate: OT.$.bind(function(name, orientation, encodedWidth, encodedHeight,
hasAudio, hasVideo,
frameRate, completion) {
_socket.streamCreate(
name,
orientation,
encodedWidth,
encodedHeight,
hasAudio,
hasVideo,
frameRate,
OT.Config.get('bitrates', 'min', OT.APIKEY),
OT.Config.get('bitrates', 'max', OT.APIKEY),
completion
);
}, this),
streamDestroy: function(streamId) {
_socket.streamDestroy(streamId);
},
streamChannelUpdate: function(stream, channel, attributes) {
_socket.streamChannelUpdate(stream.id, channel.id, attributes);
}
};
/**
* Sends a signal to each client or a specified client in the session. Specify a
* to property of the signal parameter to limit the signal to
* be sent to a specific client; otherwise the signal is sent to each client connected to
* the session.
* * The following example sends a signal of type "foo" with a specified data payload ("hello") * to all clients connected to the session: *
* session.signal({
* type: "foo",
* data: "hello"
* },
* function(error) {
* if (error) {
* console.log("signal error: " + error.message);
* } else {
* console.log("signal sent");
* }
* }
* );
*
*
* Calling this method without specifying a recipient client (by setting the to
* property of the signal parameter) results in multiple signals sent (one to each
* client in the session). For information on charges for signaling, see the
* OpenTok pricing page.
*
* The following example sends a signal of type "foo" with a data payload ("hello") to a * specific client connected to the session: *
* session.signal({
* type: "foo",
* to: recipientConnection; // a Connection object
* data: "hello"
* },
* function(error) {
* if (error) {
* console.log("signal error: " + error.message);
* } else {
* console.log("signal sent");
* }
* }
* );
*
*
* Add an event handler for the signal event to listen for all signals sent in
* the session. Add an event handler for the signal:type event to listen for
* signals of a specified type only (replace type, in signal:type,
* with the type of signal to listen for). The Session object dispatches these events. (See
* events.)
*
* @param {Object} signal An object that contains the following properties defining the signal:
*
data — (String) The data to send. The limit to the length of data
* string is 8kB. Do not set the data string to null or
* undefined.to — (Connection) A Connection
* object corresponding to the client that the message is to be sent to. If you do not
* specify this property, the signal is sent to all clients connected to the session.type — (String) The type of the signal. You can use the type to
* filter signals when setting an event handler for the signal:type event
* (where you replace type with the type string). The maximum length of the
* type string is 128 characters, and it must contain only letters (A-Z and a-z),
* numbers (0-9), '-', '_', and '~'.Each property is optional. If you set none of the properties, you will send a signal * with no data or type to each client connected to the session.
* * @param {Function} completionHandler A function that is called when sending the signal * succeeds or fails. This function takes one parameter —error.
* On success, the completionHandler function is not passed any
* arguments. On error, the function is passed an error object, defined by the
* Error class. The error object has the following
* properties:
*
* code — (Number) An error code, which can be one of the following:
* | 400 | One of the signal properties — data, type, or to — * is invalid. | *
| 404 | The client specified by the to property is not connected to * the session. | *
| 413 | The type string exceeds the maximum length (128 bytes), * or the data string exceeds the maximum size (8 kB). | *
| 500 | You are not connected to the OpenTok session. | *
message — (String) A description of the error.Note that the completionHandler success result (error == null)
* indicates that the options passed into the Session.signal() method are valid
* and the signal was sent. It does not indicate that the signal was successfully
* received by any of the intended recipients.
*
* @method #signal
* @memberOf Session
* @see signal and signal:type events
*/
this.signal = function(options, completion) {
var _options = options,
_completion = completion;
if (OT.$.isFunction(_options)) {
_completion = _options;
_options = null;
}
if (this.isNot('connected')) {
var notConnectedErrorMsg = 'Unable to send signal - you are not connected to the session.';
dispatchError(500, notConnectedErrorMsg, _completion);
return;
}
_socket.signal(_options, _completion);
if (options && options.data && (typeof(options.data) !== 'string')) {
OT.warn('Signaling of anything other than Strings is deprecated. ' +
'Please update the data property to be a string.');
}
this.logEvent('signal', 'send', 'type',
(options && options.data) ? typeof(options.data) : 'null');
};
/**
* Forces a remote connection to leave the session.
*
*
* The forceDisconnect() method is normally used as a moderation tool
* to remove users from an ongoing session.
*
* When a connection is terminated using the forceDisconnect(),
* sessionDisconnected, connectionDestroyed and
* streamDestroyed events are dispatched in the same way as they
* would be if the connection had terminated itself using the disconnect()
* method. However, the reason property of a {@link ConnectionEvent} or
* {@link StreamEvent} object specifies "forceDisconnected" as the reason
* for the destruction of the connection and stream(s).
*
* While you can use the forceDisconnect() method to terminate your own connection,
* calling the disconnect() method is simpler.
*
* The OT object dispatches an exception event if the user's role
* does not include permissions required to force other users to disconnect.
* You define a user's role when you create the user token using the
* generate_token() method using
* OpenTok
* server-side libraries or the
* Dashboard page.
* See ExceptionEvent and OT.on().
*
* The application throws an error if the session is not connected. *
* *
* connectionDestroyed (ConnectionEvent)
* On clients other than which had the connection terminated.
*
* exception (ExceptionEvent)
* The user's role does not allow forcing other user's to disconnect (event.code =
* 1530),
* or the specified stream is not publishing to the session (event.code = 1535).
*
* sessionDisconnected
* (SessionDisconnectEvent)
* On the client which has the connection terminated.
*
* streamDestroyed (StreamEvent)
* If streams are stopped as a result of the connection ending.
*
connectionId property of the Connection object).
*
* @param {Function} completionHandler (Optional) A function to be called when the call to the
* forceDiscononnect() method succeeds or fails. This function takes one parameter
* — error. On success, the completionHandler function is
* not passed any arguments. On error, the function is passed an error object
* parameter. The error object, defined by the Error
* class, has two properties: code (an integer)
* and message (a string), which identify the cause of the failure.
* Calling forceDisconnect() fails if the role assigned to your
* token is not "moderator"; in this case error.code is set to 1520. The following
* code adds a completionHandler when calling the forceDisconnect()
* method:
*
* session.forceDisconnect(connection, function (error) {
* if (error) {
* console.log(error);
* } else {
* console.log("Connection forced to disconnect: " + connection.id);
* }
* });
*
*
* @method #forceDisconnect
* @memberOf Session
*/
this.forceDisconnect = function(connectionOrConnectionId, completionHandler) {
if (this.isNot('connected')) {
var notConnectedErrorMsg = 'Cannot call forceDisconnect(). You are not ' +
'connected to the session.';
dispatchError(OT.ExceptionCodes.NOT_CONNECTED, notConnectedErrorMsg, completionHandler);
return;
}
var notPermittedErrorMsg = 'This token does not allow forceDisconnect. ' +
'The role must be at least `moderator` to enable this functionality';
if (permittedTo('forceDisconnect')) {
var connectionId = typeof connectionOrConnectionId === 'string' ?
connectionOrConnectionId : connectionOrConnectionId.id;
_socket.forceDisconnect(connectionId, function(err) {
if (err) {
dispatchError(OT.ExceptionCodes.UNABLE_TO_FORCE_DISCONNECT,
notPermittedErrorMsg, completionHandler);
} else if (completionHandler && OT.$.isFunction(completionHandler)) {
completionHandler.apply(null, arguments);
}
});
} else {
// if this throws an error the handleJsException won't occur
dispatchError(OT.ExceptionCodes.UNABLE_TO_FORCE_DISCONNECT,
notPermittedErrorMsg, completionHandler);
}
};
/**
* Forces the publisher of the specified stream to stop publishing the stream.
*
*
* Calling this method causes the Session object to dispatch a streamDestroyed
* event on all clients that are subscribed to the stream (including the client that is
* publishing the stream). The reason property of the StreamEvent object is
* set to "forceUnpublished".
*
* The OT object dispatches an exception event if the user's role
* does not include permissions required to force other users to unpublish.
* You define a user's role when you create the user token using the generate_token()
* method using the
* OpenTok
* server-side libraries or the Dashboard
* page.
* You pass the token string as a parameter of the connect() method of the Session
* object. See ExceptionEvent and
* OT.on().
*
* exception (ExceptionEvent)
* The user's role does not allow forcing other users to unpublish.
*
* streamDestroyed (StreamEvent)
* The stream has been unpublished. The Session object dispatches this on all clients
* subscribed to the stream, as well as on the publisher's client.
*
forceUnpublish() method succeeds or fails. This function takes one parameter
* — error. On success, the completionHandler function is
* not passed any arguments. On error, the function is passed an error object
* parameter. The error object, defined by the Error
* class, has two properties: code (an integer)
* and message (a string), which identify the cause of the failure. Calling
* forceUnpublish() fails if the role assigned to your token is not "moderator";
* in this case error.code is set to 1530. The following code adds a
* completionHandler when calling the forceUnpublish() method:
*
* session.forceUnpublish(stream, function (error) {
* if (error) {
* console.log(error);
* } else {
* console.log("Connection forced to disconnect: " + connection.id);
* }
* });
*
*
* @method #forceUnpublish
* @memberOf Session
*/
this.forceUnpublish = function(streamOrStreamId, completionHandler) {
if (this.isNot('connected')) {
var notConnectedErrorMsg = 'Cannot call forceUnpublish(). You are not ' +
'connected to the session.';
dispatchError(OT.ExceptionCodes.NOT_CONNECTED, notConnectedErrorMsg, completionHandler);
return;
}
var notPermittedErrorMsg = 'This token does not allow forceUnpublish. ' +
'The role must be at least `moderator` to enable this functionality';
if (permittedTo('forceUnpublish')) {
var stream = typeof streamOrStreamId === 'string' ?
this.streams.get(streamOrStreamId) : streamOrStreamId;
_socket.forceUnpublish(stream.id, function(err) {
if (err) {
dispatchError(OT.ExceptionCodes.UNABLE_TO_FORCE_UNPUBLISH,
notPermittedErrorMsg, completionHandler);
} else if (completionHandler && OT.$.isFunction(completionHandler)) {
completionHandler.apply(null, arguments);
}
});
} else {
// if this throws an error the handleJsException won't occur
dispatchError(OT.ExceptionCodes.UNABLE_TO_FORCE_UNPUBLISH,
notPermittedErrorMsg, completionHandler);
}
};
this.getStateManager = function() {
OT.warn('Fixme: Have not implemented session.getStateManager');
};
this.isConnected = function() {
return this.is('connected');
};
this.capabilities = new OT.Capabilities([]);
/**
* Dispatched when an archive recording of the session starts.
*
* @name archiveStarted
* @event
* @memberof Session
* @see ArchiveEvent
* @see Archiving overview.
*/
/**
* Dispatched when an archive recording of the session stops.
*
* @name archiveStopped
* @event
* @memberof Session
* @see ArchiveEvent
* @see Archiving overview.
*/
/**
* A new client (including your own) has connected to the session.
* @name connectionCreated
* @event
* @memberof Session
* @see ConnectionEvent
* @see OT.initSession()
*/
/**
* A client, other than your own, has disconnected from the session.
* @name connectionDestroyed
* @event
* @memberof Session
* @see ConnectionEvent
*/
/**
* The page has connected to an OpenTok session. This event is dispatched asynchronously
* in response to a successful call to the connect() method of a Session
* object. Before calling the connect() method, initialize the session by
* calling the OT.initSession() method. For a code example and more details,
* see Session.connect().
* @name sessionConnected
* @event
* @memberof Session
* @see SessionConnectEvent
* @see Session.connect()
* @see OT.initSession()
*/
/**
* The client has disconnected from the session. This event may be dispatched asynchronously
* in response to a successful call to the disconnect() method of the Session object.
* The event may also be disptached if a session connection is lost inadvertantly, as in the case
* of a lost network connection.
*
* The default behavior is that all Subscriber objects are unsubscribed and removed from the
* HTML DOM. Each Subscriber object dispatches a destroyed event when the element is
* removed from the HTML DOM. If you call the preventDefault() method in the event
* listener for the sessionDisconnect event, the default behavior is prevented, and
* you can, optionally, clean up Subscriber objects using your own code.
*
* @name sessionDisconnected
* @event
* @memberof Session
* @see Session.disconnect()
* @see Session.forceDisconnect()
* @see SessionDisconnectEvent
*/
/**
* A new stream, published by another client, has been created on this session. For streams
* published by your own client, the Publisher object dispatches a streamCreated
* event. For a code example and more details, see {@link StreamEvent}.
* @name streamCreated
* @event
* @memberof Session
* @see StreamEvent
* @see Session.publish()
*/
/**
* A stream from another client has stopped publishing to the session.
*
* The default behavior is that all Subscriber objects that are subscribed to the stream are
* unsubscribed and removed from the HTML DOM. Each Subscriber object dispatches a
* destroyed event when the element is removed from the HTML DOM. If you call the
* preventDefault() method in the event listener for the
* streamDestroyed event, the default behavior is prevented and you can clean up
* Subscriber objects using your own code. See
* Session.getSubscribersForStream().
*
* For streams published by your own client, the Publisher object dispatches a
* streamDestroyed event.
*
* For a code example and more details, see {@link StreamEvent}.
* @name streamDestroyed
* @event
* @memberof Session
* @see StreamEvent
*/
/**
* A stream has started or stopped publishing audio or video (see
* Publisher.publishAudio() and
* Publisher.publishVideo()); or the
* videoDimensions property of the Stream
* object has changed (see Stream.videoDimensions).
*
* Note that a subscriber's video can be disabled or enabled for reasons other than the
* publisher disabling or enabling it. A Subscriber object dispatches videoDisabled
* and videoEnabled events in all conditions that cause the subscriber's stream
* to be disabled or enabled.
*
* @name streamPropertyChanged
* @event
* @memberof Session
* @see StreamPropertyChangedEvent
* @see Publisher.publishAudio()
* @see Publisher.publishVideo()
* @see Stream.hasAudio
* @see Stream.hasVideo
* @see Stream.videoDimensions
* @see Subscriber videoDisabled event
* @see Subscriber videoEnabled event
*/
/**
* A signal was received from the session. The SignalEvent
* class defines this event object. It includes the following properties:
*
data — (String) The data string sent with the signal (if there
* is one).from — (Connection) The Connection
* corresponding to the client that sent with the signal.type — (String) The type assigned to the signal (if there is
* one).
* You can register to receive all signals sent in the session, by adding an event handler
* for the signal event. For example, the following code adds an event handler
* to process all signals sent in the session:
*
* session.on("signal", function(event) {
* console.log("Signal sent from connection: " + event.from.id);
* console.log("Signal data: " + event.data);
* });
*
* You can register for signals of a specfied type by adding an event handler for the
* signal:type event (replacing type with the actual type string
* to filter on).
*
* @name signal
* @event
* @memberof Session
* @see Session.signal()
* @see SignalEvent
* @see signal:type event
*/
/**
* A signal of the specified type was received from the session. The
* SignalEvent class defines this event object.
* It includes the following properties:
*
data — (String) The data string sent with the signal.from — (Connection) The Connection
* corresponding to the client that sent with the signal.type — (String) The type assigned to the signal (if there is one).
*
* You can register for signals of a specfied type by adding an event handler for the
* signal:type event (replacing type with the actual type string
* to filter on). For example, the following code adds an event handler for signals of
* type "foo":
*
* session.on("signal:foo", function(event) {
* console.log("foo signal sent from connection " + event.from.id);
* console.log("Signal data: " + event.data);
* });
*
*
* You can register to receive all signals sent in the session, by adding an event
* handler for the signal event.
*
* @name signal:type
* @event
* @memberof Session
* @see Session.signal()
* @see SignalEvent
* @see signal event
*/
};
})(window);
(function() {
var txt = function(text) {
return document.createTextNode(text);
};
var el = function(attr, children, tagName) {
var el = OT.$.createElement(tagName || 'div', attr, children);
el.on = OT.$.bind(OT.$.on, OT.$, el);
return el;
};
function DevicePickerController(opts) {
var destroyExistingPublisher,
publisher,
devicesById;
this.change = OT.$.bind(function() {
destroyExistingPublisher();
var settings;
this.pickedDevice = devicesById[opts.selectTag.value];
if(!this.pickedDevice) {
console.log('No device for', opts.mode, opts.selectTag.value);
return;
}
settings = {
insertMode: 'append',
name: this.pickedDevice.label,
audioSource: null,
videoSource: null,
width: 220,
height: 165
};
settings[opts.mode] = this.pickedDevice.deviceId;
console.log('initPublisher', opts.previewTag, settings);
var pub = OT.initPublisher(opts.previewTag, settings);
pub.on({
accessDialogOpened: function(event) {
event.preventDefault();
},
accessDialogClosed: function() {
},
accessAllowed: function() {
},
accessDenied: function(event) {
event.preventDefault();
}
});
publisher = pub;
}, this);
this.cleanup = destroyExistingPublisher = function() {
if(publisher) {
publisher.destroy();
publisher = void 0;
}
};
var disableSelector = function (opt, str) {
opt.innerHTML = '';
opt.appendChild(el({}, txt(str), 'option'));
opt.setAttribute('disabled', '');
};
var addDevice = function (device) {
devicesById[device.deviceId] = device;
return el({ value: device.deviceId }, txt(device.label), 'option');
};
this.setDeviceList = OT.$.bind(function (devices) {
opts.selectTag.innerHTML = '';
devicesById = {};
if(devices.length > 0) {
devices.map(addDevice).map(OT.$.bind(opts.selectTag.appendChild, opts.selectTag));
opts.selectTag.removeAttribute('disabled');
} else {
disableSelector(opts.selectTag, 'No devices');
}
this.change();
}, this);
this.setLoading = function() {
disableSelector(opts.selectTag, 'Loading...');
};
OT.$.on(opts.selectTag, 'change', this.change);
}
OT.HardwareSetup = function(targetElement, options, callback) {
var camera,
microphone,
setupDOM,
setState;
setState = OT.$.statable(this, ['getDevices', 'chooseDevices', 'destroyed'], 'getDevices');
this.audioSource = function() {
return microphone && microphone.pickedDevice;
};
this.videoSource = function() {
return camera && camera.pickedDevice;
};
this.destroy = OT.$.bind(function() {
if(this.is('destroyed')) {
return;
}
if(camera) {
camera.cleanup();
}
if(microphone) {
microphone.cleanup();
}
if(this.is('chooseDevices')) {
targetElement.parentNode.removeChild(targetElement);
}
setState('destroyed');
}, this);
if(targetElement == null) {
callback(new Error('You must provide a targetElement'));
return;
}
if(!OT.$.hasCapabilities('getMediaDevices')) {
callback(new Error('This browser does not support getMediaDevices APIs'));
return;
}
var camSelector,
camPreview,
micSelector,
micPreview,
container;
camSelector = el({ style: 'width: 100%' }, '', 'select');
camPreview = el({
style: 'background-color: #000; margin-left: 100px; width: 220px; height: 165px;'
}, ''),
micSelector = el({ style: 'width: 100%' }, '', 'select'),
micPreview = el({
style: 'background-color: #000; margin-left: 100px; width: 220px; height: 165px;'
}, '');
container = el({
id: 'OT_' + OT.$.uuid(),
style: 'border: 1px solid #000; padding: 10px; width: 320px;'
}, [
el({ style: 'padding: 0 0 10px; overflow: auto; text-align: right; ' }, [
el({ style: 'float: left; width: 90px; padding-right: 10px; line-height: 160%;' },
'Camera'),
el({ style: 'margin-left: 100px; ' }, camSelector),
camPreview
]),
el({ style: 'overflow: auto; text-align: right;' }, [
el({ style: 'float: left; width: 90px; padding-right: 10px; line-height: 160%;' },
'Microphone'),
el({ style: 'margin-left: 100px; ' }, micSelector),
micPreview
])
]);
camera = new DevicePickerController({
selectTag: camSelector,
previewTag: camPreview,
mode: 'videoSource'
});
microphone = new DevicePickerController({
selectTag: micSelector,
previewTag: micPreview,
mode: 'audioSource'
});
camera.setLoading();
microphone.setLoading();
OT.getDevices(OT.$.bind(function(error, devices) {
if (error) {
callback(error);
return;
}
if(this.is('destroyed')) {
return; // They destroyed us before we got the devices, bail.
}
setupDOM();
camera.setDeviceList(devices.filter(function(device) {
return device.kind === 'videoinput';
}));
microphone.setDeviceList(devices.filter(function(device) {
return device.kind === 'audioinput';
}));
setState('chooseDevices');
}, this));
setupDOM = function() {
var insertMode = options.insertMode;
if(!(insertMode == null || insertMode === 'replace')) {
if(insertMode === 'append') {
targetElement.appendChild(container);
targetElement = container;
} else if(insertMode === 'before') {
targetElement.parentNode.insertBefore(container, targetElement);
targetElement = container;
} else if(insertMode === 'after') {
targetElement.parentNode.insertBefore(container, targetElement.nextSibling);
targetElement = container;
}
} else {
OT.$.emptyElement(targetElement);
if(targetElement.getAttribute('id') == null) {
targetElement.setAttribute('id', container.getAttribute('id'));
}
for(var key in container.style) {
targetElement.style[key] = container.style[key];
}
while(container.childNodes.length > 0) {
targetElement.appendChild(container.firstChild);
}
}
};
};
OT.initHardwareSetup = function(targetElement, options, callback) {
return new OT.HardwareSetup(targetElement, options, callback);
};
})();
!(function() {
var style = document.createElement('link');
style.type = 'text/css';
style.media = 'screen';
style.rel = 'stylesheet';
style.href = OT.properties.cssURL;
var head = document.head || document.getElementsByTagName('head')[0];
head.appendChild(style);
})(window);
!(function(){
/*global define*/
// Register as a named AMD module, since TokBox could be concatenated with other
// files that may use define, but not via a proper concatenation script that
// understands anonymous AMD modules. A named AMD is safest and most robust
// way to register. Uppercase TB is used because AMD module names are
// derived from file names, and OpenTok is normally delivered in an uppercase
// file name.
if (typeof define === 'function' && define.amd) {
define( 'TB', [], function () { return TB; } );
}
})(window);