-
Notifications
You must be signed in to change notification settings - Fork 0
/
SourceController.js
272 lines (247 loc) · 7.68 KB
/
SourceController.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
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
import { utils } from '@ceeblue/webrtc-client';
import videojs from 'video.js';
const {EventEmitter, Connect, NetAddress} = utils;
// This is not exported because we access it with the plugin SourceController from :
// SourceController.SourceType
const SourceType = {
HLS: 'hls',
LLHLS: 'llhls',
DASH: 'dash',
WEBRTC: 'webrtc'
};
/**
* Does the Videojs player source selection.
* This class controls the order of the sources and the source selection.
* It switches automatically to the next source if the current one fails.
*/
export class SourceController extends EventEmitter {
/**
* Event triggered when the source changes.
*
* @param {string|null} source the source to play or null if no more source is available
*/
onSourceChanged(source) {}
/**
* Is the controller started?
*
* @return {boolean} true if the controller has been started
*/
get started() {
return this._sourceIndex > 0;
}
/**
* Set the auto mode
*
* @param {boolean} value true to automatically switch to the next source if the current one fails
*/
set auto(value) {
this._auto = value;
}
/**
* Set the current source dynamically
*
* @param {string} source the source type to play
*/
set source(source) {
for (let i = 0; i < this._sources.length; i++) {
if (this._sources[i].sourceType === source) {
this._sourceIndex = i;
this._reset();
this._tryNextSource();
return;
}
}
throw new Error('Unknown source ' + source);
}
/**
* The list of possible source types
*/
static get SourceType() {
return SourceType;
}
/**
* SourceController constructor
*
* @param {VideojsPlayer} player Videojs player
* @param {Connect.Params} connectParams The Ceeblue connection parameters
* @param {Array<SourceType|SourceObject>} sources an array of sources to try in order
*/
constructor(player, connectParams, sources) {
super();
this._sourceIndex = 0;
this._player = player;
this._auto = true;
this._sources = [];
if (!sources || !sources.length) {
throw new Error('SourceController sources must not be empty');
}
// Initiate the list of Source Objects
for (let source of sources) {
source = SourceController.sourceToObject(source, connectParams);
if (source) {
this._sources.push(source);
}
}
}
/**
* Start the source controller, try the first source.
*
* @param {string?} sourceType the name of the selected source to start with, if null the first source is played
*/
start(sourceType) {
if (this.started) {
videojs.log.error('SourceController already started');
return;
}
this.source = sourceType || this._sources[0].sourceType;
}
/**
* Stop the source controller, reset the player.
*/
stop() {
if (!this.started) {
return;
}
this._sourceIndex = 0;
this._reset();
}
/**
* Convert a source type to a videojs source object compatible with Ceeblue Cloud
*
* @param {SourceType|SourceObject} source SourceType or Source Object
* @param {Connect.Params} connectParams The Ceeblue connection parameters
* @return {Object} a videojs source object for videojs or null if the parameters are invalid
*/
static sourceToObject(source, connectParams) {
if (typeof source !== 'string') {
// Already a source object, set the sourceType and check the src property
if (source.src === undefined) {
videojs.log.error('SourceObject must have src');
return null;
}
// Parse the MIME type to get the SourceType, by default it's WEBRTC
source.sourceType = SourceType.WEBRTC;
if (source.type !== undefined) {
switch (source.type) {
case 'application/vnd.apple.mpegurl':
source.sourceType = source.src.includes('/hls/') ? SourceType.HLS : SourceType.LLHLS;
break;
case 'application/dash+xml':
source.sourceType = SourceType.DASH;
break;
default:
videojs.log.warn('Unknown source type ' + source.type + ' using the MIME type instead');
source.sourceType = source.type;
break;
}
}
return source;
}
if (connectParams.endPoint === undefined || connectParams.streamName === undefined) {
videojs.log.error('ConnectParams must have endPoint and streamName');
return null;
}
connectParams.endPoint = connectParams.endPoint;
let host = new NetAddress(connectParams.endPoint);
const domain = host.domain;
const result = {sourceType: source};
host = domain + ((host.port) ? (':' + host.port) : '');
// Construct the source object from the source type and the connection parameters
switch (source) {
case SourceType.HLS:
result.src = `https://${host}/hls/${connectParams.streamName}/index.m3u8`;
result.type = 'application/vnd.apple.mpegurl';
break;
case SourceType.LLHLS:
result.src = `https://${host}/cmaf/${connectParams.streamName}/index.m3u8`;
result.type = 'application/vnd.apple.mpegurl';
break;
case SourceType.DASH:
result.src = `https://${host}/cmaf/${connectParams.streamName}/index.mpd`;
result.type = 'application/dash+xml';
break;
case SourceType.WEBRTC:
const protocol = connectParams.endPoint.startsWith('https://') ? 'https' : 'wss';
result.src = Connect.buildURL(Connect.Type.WEBRTC, connectParams, protocol).toString();
result.iceserver = connectParams.iceServer;
if (!result.iceserver) {
// Use the default ICE servers structure
result.iceserver = {
urls: ['turn:' + domain + ':3478?transport=tcp', 'turn:' + domain + ':3478'],
username: 'csc_demo',
credential: 'UtrAFClFFO'
};
}
break;
default:
videojs.log.error('Unknown source type ' + source);
return null;
}
// add token as query parameter
if (source !== SourceType.WEBRTC && connectParams.accessToken) {
result.src += '?id=' + connectParams.accessToken;
}
return result;
}
/**
* Reset the player
*/
_reset() {
this._player.off('error');
this._player.off('loadedmetadata');
this._player.off('ended');
// important before starting a new source!
this._player.pause();
this._player.reset();
}
/**
* Try the next source or ends if no more source is available.
*/
_tryNextSource() {
// Show error when all sources have been tried without success
if (this._sourceIndex >= this._sources.length) {
// end of sources
this._sourceIndex = 0;
this.onSourceChanged(null);
return;
}
const newSource = this._sources[this._sourceIndex++];
videojs.log('Trying source', newSource);
this._player.on('error', (_, error) => {
if (!error) {
error = this._player.error() && this._player.error().message;
}
videojs.log('Player error : ', error);
this._stopSource();
});
this._player.on('loadedmetadata', () => {
videojs.log('loadedmetadata');
this._player.qualityButton();
this.onSourceChanged(newSource.sourceType);
});
this._player.on('ended', (e) => {
videojs.log('Player ended');
this._stopSource();
});
this._player.src(newSource);
}
/**
* Stop the current source in the next time tick, then try next source.
*/
_stopSource() {
if (this._stopSourceTimeout) {
// already stopping
return;
}
this._stopSourceTimeout = setTimeout(() => {
this._reset();
if (this._auto) {
this._tryNextSource();
} else {
this._sourceIndex = 0;
this.onSourceChanged(null);
}
this._stopSourceTimeout = null;
}, 0);
}
}