forked from red5pro/streaming-html5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
208 lines (189 loc) · 6.4 KB
/
index.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
(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 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',
bandwidth: {
video: 2500
}
};
var userMedia = {
video: {
width: {
min: 640,
ideal: 1280,
max: 1920
},
height: {
min: 480,
ideal: 720,
max: 1080
},
frameRate: {
min: 25,
ideal: 40,
max: 60
}
}
};
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) {
updateStatusFromEvent(window.red5prosdk.PublisherEventTypes.PUBLISH_FAIL);
statisticsField.innerText = message;
statisticsField.style.color = '#ff0000';
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 getUserMediaConfiguration () {
return Object.assign({}, {
audio: configuration.useAudio ? configuration.userMedia.audio : false,
video: configuration.useVideo ? configuration.userMedia.video : false,
frameRate: configuration.frameRate
}, configuration.useVideo ? userMedia : {});
}
function determinePublisher () {
var config = Object.assign({},
configuration,
getUserMediaConfiguration(),
defaultConfiguration);
var rtcConfig = Object.assign({}, config, {
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.
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 publishing - ' + jsonError);
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());