-
Notifications
You must be signed in to change notification settings - Fork 14
/
student.js
214 lines (192 loc) · 6.35 KB
/
student.js
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/* global OT, chrome */
window.addEventListener('load', function studentController () {
var session
var publisherCamera
var publisherScreen
var id = window.location.hash.slice(1)
function _msg (m) {
$('#message').text(m)
}
function toggleSourceControl (value) {
if (value === 'camera') {
$('#select-camera').show()
$('#camera-view').show()
$('#select-screen').hide()
$('#screen-view').hide()
} else if (value === 'screen') {
$('#select-camera').hide()
$('#camera-view').hide()
$('#select-screen').show()
$('#screen-view').show()
}
}
toggleSourceControl($('input[type=radio][name=videoType]').val())
$('input[type=radio][name=videoType]').change(function () {
toggleSourceControl(this.value)
})
function installChromeExtension () {
var extUrl = 'https://chrome.google.com/webstore/detail/fbjkpogjjhklbffmfooofjgablhmcnhn'
if (chrome && chrome.webstore) {
chrome.webstore.install(extUrl, function () {
$('#chrome-ext-install').hide()
$('#share-screen').removeClass('invisible')
_msg('Chrome screenshare extension installed')
}, function (err) {
console.log(err)
_msg('Please install the screen sharing extension and refresh the page.')
})
}
}
function checkScreenShareSupport (callback) {
OT.checkScreenSharingCapability(function (res) {
var screenshareEnabled = false
if (!res.supported || res.extensionRegistered === false) {
_msg('Screensharing is not supported')
} else if (res.extensionRequired === 'chrome' && res.extensionInstalled === false) {
console.log('Chrome Screenshare required')
$('#chrome-ext-install').show()
} else {
console.log('Screenshare available')
$('#chrome-ext-install').hide()
$('#share-screen').removeClass('invisible')
screenshareEnabled = true
}
// Trigger callback
callback(screenshareEnabled, res)
})
}
var createPublisherScreenshare = function () {
var opts = {
audioSource: null,
insertMode: 'append',
publishAudio: false,
videoSource: 'screen',
width: '100%',
height: '100%',
name: $('#camera-name').val()
}
_msg('Setting up screenshare...')
publisherScreen = OT.initPublisher('screen-view', opts, function (err) {
if (err) {
console.log(err)
_msg('Error getting access to screen share.')
return
}
_msg('Screen sharing started.')
$('#share-screen').attr('disabled', 'disabled').hide()
$('#publish').removeAttr('disabled').show()
})
$('input[type=radio][name=videoType]').attr('disabled', 'disabled')
publisherScreen.on('mediaStopped', function () {
_msg('Screen sharing stopped')
})
}
$('#share-screen').on('click', createPublisherScreenshare)
$('#chrome-ext-install').on('click', function (evt) {
installChromeExtension()
})
function launchSession (data) {
session = OT.initSession(data.apiKey, data.sessionId)
session.on('streamCreated', function (event) {
console.log('streamCreated', event)
if (event.stream.connection.data === id) {
session.subscribe(event.stream, 'other-sources', {
insertMode: 'append',
width: '200px',
height: '150px'
})
}
})
session.connect(data.token, function (error) {
if (error) {
_msg('Error connecting to OpenTok session')
_msg('Error')
console.log(error)
return
}
console.log('Connected to session', data.sessionId)
_msg('Connected to OpenTok')
$('.start-camera').removeAttr('disabled')
$('#start-camera').on('click', function (evt) {
publisherCamera = OT.initPublisher('camera-view', {
audioSource: null,
videoSource: $('#camera-list').val(),
height: '100%',
width: '100%',
insertMode: 'append',
name: $('#camera-name').val()
}, function (err) {
if (err) {
_msg('Error getting feed for camera 1')
console.log(err)
return
}
$('input[type=radio][name=videoType]').attr('disabled', 'disabled')
$('#publish').removeAttr('disabled').show()
$('.camera').attr('disabled', 'disabled')
$('#start-camera').hide()
})
})
$('#publish').on('click', function (evt) {
if (publisherCamera != null) {
session.publish(publisherCamera, function (err) {
if (err) {
_msg('Unable to publish camera 1')
console.log(err)
return
}
$('#publish').attr('disabled', 'disabled').hide()
console.log('Published camera')
_msg('Live')
})
}
if (publisherScreen != null) {
session.publish(publisherScreen, function (err) {
if (err) {
_msg('Unable to publish screen')
console.log(err)
return
}
$('#publish').attr('disabled', 'disabled').hide()
console.log('Published camera')
_msg('Live')
})
}
})
})
}
OT.getDevices(function (err, devices) {
if (err) {
_msg('Error getting list of media devices')
console.log(err)
return
}
console.log('MediaDevices', devices)
if (devices.length < 1) {
_msg('No media devices available')
console.error('No media devices available')
return
}
var videoDevices = devices.filter(function (d) {
return d.kind === 'videoInput'
}).map(function (d, i) {
var deviceLabel = d.label.replace(/_/g, ' ').split(' (')[0] || 'Camera ' + (i + 1)
return '<option value="' + d.deviceId + '">' + deviceLabel + '</option>'
})
$('#camera-list').append(videoDevices.join(''))
$.get('/token?id=' + id, function (data) {
console.log('Token data', data)
window.location.hash = data.id
$('#share-url').val(window.location.href)
$('.navbar-brand').attr('href', window.location.href)
OT.registerScreenSharingExtension('chrome', 'fbjkpogjjhklbffmfooofjgablhmcnhn', 2)
checkScreenShareSupport(function () {
launchSession(data)
})
}, 'json')
.fail(function (err) {
_msg('Error getting token')
console.log(err)
})
})
})