forked from red5pro/streaming-html5
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
228 lines (208 loc) · 7.76 KB
/
Copy pathindex.js
File metadata and controls
228 lines (208 loc) · 7.76 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
(function(window, document, red5pro, PublisherBase /* see: src/static/script/main.js */) {
'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 {}
})();
var targetPublisher;
var targetView;
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 addressField = document.getElementById('address-field');
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,
app: 'live'
};
function displayServerAddress (serverAddress) {
addressField.innerText = 'Origin Address: ' + serverAddress;
}
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) {
//
}
}
function onUnpublishFail (message) {
console.error('[Red5ProPublisher] Unpublish Error :: ' + message);
}
function onUnpublishSuccess () {
console.log('[Red5ProPublisher] Unpublish Complete.');
}
function requestOrigin (configuration) {
var host = configuration.host;
var app = configuration.app;
var streamName = configuration.stream1;
var port = serverSettings.httpport;
var portURI = (port.length > 0 ? ':' + port : '');
var baseUrl = isSecure ? protocol + '://' + host : protocol + '://' + host + portURI;
var apiVersion = configuration.streamManagerAPI || '2.0';
var url = baseUrl + '/streammanager/api/' + apiVersion + '/event/' + app + '/' + streamName + '?action=broadcast';
return new Promise(function (resolve, reject) {
fetch(url)
.then(function (res) {
if (res.headers.get("content-type") &&
res.headers.get("content-type").toLowerCase().indexOf("application/json") >= 0) {
return res.json();
}
else {
throw new TypeError('Could not properly parse response.');
}
})
.then(function (json) {
resolve(json.serverAddress);
})
.catch(function (error) {
var jsonError = typeof error === 'string' ? error : JSON.stringify(error, null, 2)
console.error('[PublisherStreamManagerTest] :: Error - Could not request Origin IP from Stream Manager. ' + jsonError)
reject(error)
});
});
}
function getUserMediaConfiguration () {
return {
audio: configuration.useAudio ? configuration.userMedia.audio : false,
video: configuration.useVideo ? configuration.userMedia.video : false,
frameRate: configuration.frameRate
};
}
function determinePublisher () {
var config = Object.assign({},
configuration,
defaultConfiguration,
getUserMediaConfiguration());
var rtcConfig = Object.assign({}, config, {
protocol: getSocketLocationFromProtocol().protocol,
port: getSocketLocationFromProtocol().port,
streamName: config.stream1,
streamType: 'webrtc'
});
var rtmpConfig = Object.assign({}, config, {
protocol: 'rtmp',
port: serverSettings.rtmpport,
streamName: config.stream1,
width: config.cameraWidth,
height: config.cameraHeight,
swf: '../../lib/red5pro/red5pro-publisher.swf',
swfobjectURL: '../../lib/swfobject/swfobject.js',
productInstallURL: '../../lib/swfobject/playerProductInstall.swf'
});
var publishOrder = config.publisherFailoverOrder
.split(',')
.map(function (item) {
return item.trim()
});
return PublisherBase.determinePublisher({
rtc: rtcConfig,
rtmp: rtmpConfig
}, publishOrder);
}
function preview (publisher, requiresGUM) {
var elementId = 'red5pro-publisher-video';
var gUM = getUserMediaConfiguration();
return PublisherBase.preview(publisher, elementId, requiresGUM ? gUM : undefined);
}
function publish (publisher, view, streamName) {
streamTitle.innerText = streamName;
targetPublisher = publisher;
targetView = view;
return new Promise(function (resolve, reject) {
PublisherBase.publish(publisher, streamName)
.then(function () {
onPublishSuccess(publisher);
})
.catch(function (error) {
reject(error);
})
});
}
function unpublish () {
return new Promise(function (resolve, reject) {
var view = targetView;
var publisher = targetPublisher;
PublisherBase.unpublish(publisher, view)
.then(function () {
onUnpublishSuccess();
resolve();
})
.catch(function (error) {
var jsonError = typeof error === 'string' ? error : JSON.stringify(error, 2, null);
onUnpublishFail('Unmount Error ' + jsonError);
reject(error);
});
});
}
// Kick off.
requestOrigin(configuration)
.then(function (serverAddress) {
displayServerAddress(serverAddress);
configuration.host = serverAddress;
return determinePublisher();
})
.then(function (payload) {
var requiresPreview = payload.requiresPreview;
var publisher = payload.publisher;
publisher.on('*', onPublisherEvent);
return preview(publisher, requiresPreview);
})
.then(function (payload) {
var publisher = payload.publisher;
var view = payload.view;
return publish(publisher, view, configuration.stream1);
})
.catch(function (error) {
var jsonError = typeof error === 'string' ? error : JSON.stringify(error, null, 2);
console.error('[Red5ProPublisher] :: Error in access of Origin IP: ' + jsonError);
updateStatusFromEvent({
type: red5pro.PublisherEventTypes.CONNECT_FAILURE
});
onPublishFail(jsonError);
});
window.addEventListener('beforeunload', function() {
function clearRefs () {
if (targetPublisher) {
targetPublisher.off('*', onPublisherEvent);
}
targetView = targetPublisher = undefined;
}
unpublish().then(clearRefs).catch(clearRefs);
window.untrackBitrate();
});
})(this, document, window.red5prosdk, new window.R5ProBase.Publisher());