-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathindex.js
More file actions
241 lines (216 loc) · 7.3 KB
/
Copy pathindex.js
File metadata and controls
241 lines (216 loc) · 7.3 KB
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
(function(window, document, red5prosdk) {
'use strict';
var serverSettings = (function() {
var settings = sessionStorage.getItem('r5proServerSettings');
try {
return JSON.parse(settings);
}
catch (e) {
console.error('Could not read server settings from sessionstorage: ' + e.message);
}
return {};
})();
var configuration = (function () {
var conf = sessionStorage.getItem('r5proTestBed');
try {
return JSON.parse(conf);
}
catch (e) {
console.error('Could not read testbed configuration from sessionstorage: ' + e.message);
}
return {}
})();
red5prosdk.setLogLevel(configuration.verboseLogging ? red5prosdk.LOG_LEVELS.TRACE : red5prosdk.LOG_LEVELS.WARN);
var targetPublisher;
var updateStatusFromEvent = window.red5proHandlePublisherEvent; // defined in src/template/partial/status-field-publisher.hbs
var streamTitle = document.getElementById('stream-title');
var statisticsField = document.getElementById('statistics-field');
var cameraSelect = document.getElementById('camera-select');
var publishButton = document.getElementById('publish-button');
var protocol = serverSettings.protocol;
var isSecure = protocol == 'https';
function getSocketLocationFromProtocol () {
return !isSecure
? {protocol: 'ws', port: serverSettings.wsport}
: {protocol: 'wss', port: serverSettings.wssport};
}
var defaultConfiguration = {
protocol: getSocketLocationFromProtocol().protocol,
port: getSocketLocationFromProtocol().port,
streamMode: configuration.recordBroadcast ? 'record' : 'live'
};
var mediaConstraints = {
audio: configuration.useAudio ? configuration.mediaConstraints.audio : false,
video: configuration.useVideo ? configuration.mediaConstraints.video : false,
};
function onBitrateUpdate (bitrate, packetsSent) {
statisticsField.innerText = 'Bitrate: ' + Math.floor(bitrate) + '. Packets Sent: ' + packetsSent + '.';
}
function onPublisherEvent (event) {
console.log('[Red5ProPublisher] ' + event.type + '.');
updateStatusFromEvent(event);
}
function onPublishFail (message) {
console.error('[Red5ProPublisher] Publish Error :: ' + message);
}
function onPublishSuccess (publisher) {
console.log('[Red5ProPublisher] Publish Complete.');
try {
window.trackBitrate(publisher.getPeerConnection(), onBitrateUpdate);
}
catch (e) {
// no tracking for you!
}
}
function onUnpublishFail (message) {
console.error('[Red5ProPublisher] Unpublish Error :: ' + message);
}
function onUnpublishSuccess () {
console.log('[Red5ProPublisher] Unpublish Complete.');
}
function getAuthenticationParams () {
var auth = configuration.authentication;
return auth && auth.enabled
? {
connectionParams: {
username: auth.username,
password: auth.password
}
}
: {};
}
function getUserMediaConfiguration () {
return Object.assign({}, {
mediaConstraints: mediaConstraints
});
}
var SELECT_DEFAULT = 'Select a camera...';
function onCameraSelect (selection) {
if (!configuration.useVideo) {
return;
}
var validSelection = selection && selection !== 'undefined' && selection !== SELECT_DEFAULT;
publishButton.disabled = !validSelection;
}
function onPublishRequest () {
var selection = cameraSelect.value;
if (mediaConstraints.video && typeof mediaConstraints.video !== 'boolean') {
mediaConstraints.video.deviceId = { exact: selection }
}
else {
mediaConstraints.video = {
deviceId: { exact: selection }
};
}
// Kick off.
unpublish()
.then(startPublishSession)
.catch(function (error) {
console.error('[Red5ProPublisher] :: Error in unpublishing - ' + error);
startPublishSession();
});
}
function waitForSelect () {
navigator.mediaDevices.enumerateDevices()
.then(function (devices) {
var videoCameras = devices.filter(function (item) {
return item.kind === 'videoinput';
})
var cameras = [{
label: SELECT_DEFAULT
}].concat(videoCameras);
var options = cameras.map(function (camera, index) {
return '<option value="' + camera.deviceId + '">' + (camera.label || 'camera ' + index) + '</option>';
});
cameraSelect.innerHTML = options.join(' ');
cameraSelect.addEventListener('change', function () {
onCameraSelect(cameraSelect.value);
});
publishButton.addEventListener('click', function() {
onPublishRequest();
});
})
.catch(function (error) {
console.error('Could not access camera devices: ' + error);
});
}
function startPublishSession () {
// Kick off.
determinePublisher()
.then(function (publisherImpl) {
streamTitle.innerText = configuration.stream1;
targetPublisher = publisherImpl;
targetPublisher.on('*', onPublisherEvent);
return targetPublisher.publish();
})
.then(function () {
onPublishSuccess(targetPublisher);
})
.catch(function (error) {
var jsonError = typeof error === 'string' ? error : JSON.stringify(error, null, 2);
console.error('[Red5ProPublisher] :: Error in publishing - ' + jsonError);
onPublishFail(jsonError);
});
return true;
}
function determinePublisher () {
var config = Object.assign({},
configuration,
defaultConfiguration,
getAuthenticationParams(),
getUserMediaConfiguration());
var rtcConfig = Object.assign({}, config, {
protocol: getSocketLocationFromProtocol().protocol,
port: getSocketLocationFromProtocol().port,
streamName: config.stream1
});
return new red5prosdk.RTCPublisher().init(rtcConfig);
}
function unpublish () {
return new Promise(function (resolve, reject) {
if (targetPublisher) {
var stream = targetPublisher.getMediaStream();
if (stream) {
var i;
var track;
var tracks = stream.getTracks();
for (i = 0; i < tracks.length; i++) {
track = tracks[i];
track.stop();
}
}
document.getElementById('red5pro-publisher').srcObject = null;
targetPublisher.unpublish()
.then(function () {
onUnpublishSuccess();
resolve();
})
.catch(function (error) {
var jsonError = typeof error === 'string' ? error : JSON.stringify(error, 2, null);
onUnpublishFail('Unmount Error ' + jsonError);
reject(error);
});
}
else {
resolve();
}
});
}
// Kick off.
waitForSelect();
var shuttingDown = false;
function shutdown() {
if (shuttingDown) return;
shuttingDown = true;
function clearRefs () {
if (targetPublisher) {
targetPublisher.off('*', onPublisherEvent);
}
targetPublisher = undefined;
}
unpublish().then(clearRefs).catch(clearRefs);
window.untrackBitrate();
}
window.addEventListener('pagehide', shutdown);
window.addEventListener('beforeunload', shutdown);
})(this, document, window.red5prosdk);