summaryrefslogtreecommitdiff
path: root/www/static/js/tokbox.js
diff options
context:
space:
mode:
authorroot <root@lalalizard.com>2012-11-26 12:06:35 -0500
committerroot <root@lalalizard.com>2012-11-26 12:06:35 -0500
commite0bc5e67f0be025aa54fbac3d25065b4b8c00f40 (patch)
treeb6da444ba40190fa2a8aece52daedad82c65aab3 /www/static/js/tokbox.js
parentef51b60c6481254d88c5fc3c34f4127b7f881a58 (diff)
www/ folder
Diffstat (limited to 'www/static/js/tokbox.js')
-rwxr-xr-xwww/static/js/tokbox.js181
1 files changed, 181 insertions, 0 deletions
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 ()
+ {
+ }
+ }