From e0bc5e67f0be025aa54fbac3d25065b4b8c00f40 Mon Sep 17 00:00:00 2001 From: root Date: Mon, 26 Nov 2012 12:06:35 -0500 Subject: www/ folder --- www/static/js/tokbox.js | 181 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 181 insertions(+) create mode 100755 www/static/js/tokbox.js (limited to 'www/static/js/tokbox.js') diff --git a/www/static/js/tokbox.js b/www/static/js/tokbox.js new file mode 100755 index 0000000..69155ce --- /dev/null +++ b/www/static/js/tokbox.js @@ -0,0 +1,181 @@ + +function Toggler (div, on, off) + { + var state = false + function activate () + { + $(div).addClass("on").html("ON") + on () + } + function deactivate () + { + $(div).removeClass("on").html("off") + off () + } + function toggle () + { + state = ! state + if (state) + activate () + else + deactivate () + } + function destroy () + { + $(div).unbind("click") + } + $(div).bind("click", toggle) + } + +var Tokbox = + { + height: 150, + width: null, + token_url: "/cgi-bin/tokbox_room.cgi", + sessionid: null, + token: null, + togglers: [], + + session: null, + publisher: null, + subscribers: [], + + subscribeToStreams: function (streams) + { + for (var i = 0; i < streams.length; i++) + { + var stream = streams[i] + if (stream.connection.connectionId != Tokbox.session.connection.connectionId) + { + var parentDiv = document.getElementById("tokbox-subscribers") + var stubDiv = document.createElement("div") + stubDiv.id = "opentok_subscriber_"+stream.connection.connectionId + parentDiv.appendChild(stubDiv) + + var subscriberProps = {width: Tokbox.width, height: Tokbox.height, audioEnabled: true} + var subscriber = Tokbox.session.subscribe(stream, stubDiv.id, subscriberProps) + Tokbox.subscribers.push(subscriber) + } + } + }, + sessionConnectedHandler: function (event) + { + Tokbox.height = $("#tokbox-embed").height() + Tokbox.width = Math.floor( Tokbox.height / 1.618 ) + $("#tokbox-loading").hide() + + Tokbox.subscribeToStreams(event.streams) + + var parentDiv = document.getElementById("tokbox-publisher") + var stubDiv = document.createElement("div") + stubDiv.id = "opentok_publisher" + parentDiv.appendChild(stubDiv) + + var publisherProps = {width: Tokbox.width, height: Tokbox.height, microphoneEnabled: false} + Tokbox.publisher = Tokbox.session.publish(stubDiv.id, publisherProps) + $("#tokbox-loading").hide() + $("#tokbox-settings").fadeIn(1000) + }, + streamCreatedHandler: function (event) + { + Tokbox.subscribeToStreams(event.streams) + }, + tokenCallback: function (raw) + { + var lines = API.parse("/tokbox", raw) + if (! lines) + return d.error("API ERROR") + for (i in lines) + { + pair = lines[i].split("\t") + if (pair[0] === "ERROR") + return d.error(pair[1]) + else if (pair[0] === "SESSION") + Tokbox.sessionid = d.trim(pair[1]) + else if (pair[0] === "TOKEN") + Tokbox.token = d.trim(pair[1]) + } + if (Tokbox.sessionid && Tokbox.token) + Tokbox.activate() + }, + activate: function () + { + Tokbox.session = TB.initSession(Tokbox.sessionid) + Tokbox.session.addEventListener("sessionConnected", Tokbox.sessionConnectedHandler) + Tokbox.session.addEventListener("streamCreated", Tokbox.streamCreatedHandler) + Tokbox.session.connect(626221, Tokbox.token) + }, + microphoneOn: function () + { + Tokbox.publisher.publishAudio(true) + d.warn(">>>> MICROPHONE ON") + }, + microphoneOff: function () + { + Tokbox.publisher.publishAudio(false) + d.warn(">>>> MICROPHONE OFF") + }, + mute: function () + { + for (var i = 0; i < Tokbox.subscribers.length; i++) + { + try + { + Tokbox.subscribers[i].subscribeToAudio(false) + d.warn("MUTED "+i) + } + catch (err) + { + d.warn("UNMUTE ERROR "+i+" "+ err.description) + } + } + d.warn(">>>> MUTE ALL") + }, + unmute: function () + { + for (var i = 0; i < Tokbox.subscribers.length; i++) + { + try + { + Tokbox.subscribers[i].subscribeToAudio(true) + d.warn("UNMUTED "+i) + } + catch (err) + { + d.warn("UNMUTE ERROR "+i+" "+ err.description) + } + } + d.warn(">>>> UNMUTE ALL") + }, + load: function () + { + $("#tokbox-embed").show() + $("#tokbox-settings").hide() + $("#tokbox-loading").show() + $(window).trigger("resize") + $.get(Tokbox.token_url, {room:Room.name}).success(Tokbox.tokenCallback) + Tokbox.togglers.push( new Toggler ("#tokbox-microphone", Tokbox.microphoneOn, Tokbox.microphoneOff) ) + Tokbox.togglers.push( new Toggler ("#tokbox-mute-all", Tokbox.mute, Tokbox.unmute) ) + }, + unload: function () + { + $("#tokbox-embed").hide() + $(window).trigger("resize") + if (Tokbox.session) + { + if (Tokbox.publisher) + Tokbox.session.unpublish(Tokbox.publisher) + Tokbox.session.disconnect() + } + Tokbox.publisher = null + Tokbox.session = null + $("#tokbox-publisher").html("") + $("#tokbox-subscriber").html("") + for (t in Tokbox.togglers) + Tokbox.togglers[i].destroy () + Tokbox.togglers = [] + }, + init: function () + { + } + } -- cgit v1.2.3-70-g09d2