-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathguest.js
137 lines (121 loc) · 4.23 KB
/
guest.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
/* global analytics */
/* eslint-disable object-shorthand */
(function () {
/**
* Options for adding OpenTok publisher and subscriber video elements
*/
const insertOptions = {
width: '100%',
height: '100%',
showControls: false,
};
/**
* Get our OpenTok API Key, Session ID, and Token from the JSON embedded
* in the HTML.
*/
const getCredentials = function () {
const el = document.getElementById('credentials');
const credentials = JSON.parse(el.getAttribute('data'));
el.remove();
return credentials;
};
/**
* Create an OpenTok publisher object
*/
const initPublisher = function () {
const properties = Object.assign({ name: 'Guest', insertMode: 'after' }, insertOptions);
return OT.initPublisher('hostDivider', properties);
};
/**
* Subscribe to a stream
*/
const subscribe = function (session, stream) {
const name = stream.name;
const insertMode = name === 'Host' ? 'before' : 'after';
const properties = Object.assign({ name: name, insertMode: insertMode }, insertOptions);
if (name === 'EC') return;
session.subscribe(stream, 'hostDivider', properties, function (error) {
if (error) {
console.log(error);
}
});
};
const switchToViewerMode = function () {
window.location.href = `viewer${window.location.search}`;
};
/**
* Toggle publishing audio/video to allow host to mute
* their video (publishVideo) or audio (publishAudio)
* @param {Object} publisher The OpenTok publisher object
* @param {Object} el The DOM element of the control whose id corresponds to the action
*/
const toggleMedia = function (publisher, el) {
const enabled = el.classList.contains('disabled');
el.classList.toggle('disabled');
publisher[el.id](enabled);
};
const addPublisherControls = function (publisher) {
const publisherContainer = document.getElementById(publisher.element.id);
const el = document.createElement('div');
const controls = [
'<div class="publisher-controls-container">',
'<div id="publishVideo" class="control video-control"></div>',
'<div id="publishAudio" class="control audio-control"></div>',
'</div>',
].join('\n');
el.innerHTML = controls;
publisherContainer.appendChild(el.firstChild);
};
/**
* Start publishing our audio and video to the session. Also, start
* subscribing to other streams as they are published.
* @param {Object} session The OpenTok session
* @param {Object} publisher The OpenTok publisher object
*/
const publishAndSubscribe = function (session, publisher) {
let streams = 1;
session.publish(publisher);
addPublisherControls(publisher);
session.on('streamCreated', function (event) {
subscribe(session, event.stream);
streams++;
if (streams > 3) {
document.getElementById('videoContainer').classList.add('wrap');
}
});
session.on('streamDestroyed', function (event) {
subscribe(session, event.stream);
streams--;
if (streams < 4) {
document.getElementById('videoContainer').classList.remove('wrap');
}
});
document.getElementById('publishVideo').addEventListener('click', function () {
toggleMedia(publisher, this);
});
document.getElementById('publishAudio').addEventListener('click', function () {
toggleMedia(publisher, this);
});
document.getElementById('back-viewer').addEventListener('click', switchToViewerMode);
};
const init = function () {
const credentials = getCredentials();
const props = { connectionEventsSuppressed: true };
const session = OT.initSession(credentials.apiKey, credentials.sessionId, props);
const publisher = initPublisher();
session.connect(credentials.token, function (error) {
if (error) {
console.log(error);
analytics.init(session);
analytics.log('initialize', 'variationAttempt');
analytics.log('initialize', 'variationError');
} else {
publishAndSubscribe(session, publisher);
analytics.init(session);
analytics.log('initialize', 'variationAttempt');
analytics.log('initialize', 'variationSuccess');
}
});
};
document.addEventListener('DOMContentLoaded', init);
})();