summaryrefslogtreecommitdiff
path: root/frontend/static/js/tokbox.js
blob: 69155cef1ae818646b840710e33ac03c64150b0f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
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 ()
		{
		}
	}