/
TextController.js
313 lines (276 loc) · 11.2 KB
/
TextController.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/**
* The copyright in this software is being made available under the BSD License,
* included below. This software may be subject to other third party and contributor
* rights, including patent rights, and no such rights are granted under this license.
*
* Copyright (c) 2013, Dash Industry Forum.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation and/or
* other materials provided with the distribution.
* * Neither the name of Dash Industry Forum nor the names of its
* contributors may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
import Constants from '../constants/Constants';
import FactoryMaker from '../../core/FactoryMaker';
import TextSourceBuffer from './TextSourceBuffer';
import TextTracks from './TextTracks';
import VTTParser from '../utils/VTTParser';
import TTMLParser from '../utils/TTMLParser';
import EventBus from '../../core/EventBus';
import Events from '../../core/events/Events';
function TextController() {
let context = this.context;
let instance;
let textSourceBuffer;
let errHandler,
dashManifestModel,
manifestModel,
mediaController,
videoModel,
streamController,
textTracks,
vttParser,
ttmlParser,
eventBus,
defaultLanguage,
lastEnabledIndex,
textDefaultEnabled, // this is used for default settings (each time a file is loaded, we check value of this settings )
allTracksAreDisabled, // this is used for one session (when a file has been loaded, we use this settings to enable/disable text)
forceTextStreaming;
function setup() {
defaultLanguage = '';
lastEnabledIndex = -1;
textDefaultEnabled = true;
forceTextStreaming = false;
textTracks = TextTracks(context).getInstance();
vttParser = VTTParser(context).getInstance();
ttmlParser = TTMLParser(context).getInstance();
textSourceBuffer = TextSourceBuffer(context).getInstance();
eventBus = EventBus(context).getInstance();
textTracks.initialize();
eventBus.on(Events.TEXT_TRACKS_QUEUE_INITIALIZED, onTextTracksAdded, instance);
resetInitialSettings();
}
function setConfig(config) {
if (!config) {
return;
}
if (config.errHandler) {
errHandler = config.errHandler;
}
if (config.dashManifestModel) {
dashManifestModel = config.dashManifestModel;
}
if (config.manifestModel) {
manifestModel = config.manifestModel;
}
if (config.mediaController) {
mediaController = config.mediaController;
}
if (config.videoModel) {
videoModel = config.videoModel;
}
if (config.streamController) {
streamController = config.streamController;
}
if (config.textTracks) {
textTracks = config.textTracks;
}
if (config.vttParser) {
vttParser = config.vttParser;
}
if (config.ttmlParser) {
ttmlParser = config.ttmlParser;
}
// create config for source buffer
textSourceBuffer.setConfig({
errHandler: errHandler,
dashManifestModel: dashManifestModel,
manifestModel: manifestModel,
mediaController: mediaController,
videoModel: videoModel,
streamController: streamController,
textTracks: textTracks,
vttParser: vttParser,
ttmlParser: ttmlParser
});
}
function getTextSourceBuffer() {
return textSourceBuffer;
}
function getAllTracksAreDisabled() {
return allTracksAreDisabled;
}
function addEmbeddedTrack(mediaInfo) {
textSourceBuffer.addEmbeddedTrack(mediaInfo);
}
function setTextDefaultLanguage(lang) {
if (typeof lang !== 'string') {
return;
}
defaultLanguage = lang;
}
function getTextDefaultLanguage() {
return defaultLanguage;
}
function onTextTracksAdded(e) {
let tracks = e.tracks;
let index = e.index;
tracks.some((item, idx) => {
if (item.lang === defaultLanguage) {
this.setTextTrack(idx);
index = idx;
return true;
}
});
if (!textDefaultEnabled) {
// disable text at startup
this.setTextTrack(-1);
}
lastEnabledIndex = index;
eventBus.trigger(Events.TEXT_TRACKS_ADDED, {
enabled: isTextEnabled(),
index: index,
tracks: tracks
});
}
function setTextDefaultEnabled(enable) {
if (typeof enable !== 'boolean') {
return;
}
textDefaultEnabled = enable;
}
function getTextDefaultEnabled() {
return textDefaultEnabled;
}
function enableText(enable) {
if (typeof enable !== 'boolean') {
return;
}
if (isTextEnabled() !== enable) {
// change track selection
if (enable) {
// apply last enabled tractk
this.setTextTrack(lastEnabledIndex);
}
if (!enable) {
// keep last index and disable text track
lastEnabledIndex = this.getCurrentTrackIdx();
this.setTextTrack(-1);
}
}
}
function isTextEnabled() {
let enabled = true;
if (allTracksAreDisabled && !forceTextStreaming) {
enabled = false;
}
return enabled;
}
// when set to true NextFragmentRequestRule will allow schedule of chunks even if tracks are all disabled. Allowing streaming to hidden track for external players to work with.
function enableForcedTextStreaming(enable) {
if (typeof enable !== 'boolean') {
return;
}
forceTextStreaming = enable;
}
function setTextTrack(idx) {
//For external time text file, the only action needed to change a track is marking the track mode to showing.
// Fragmented text tracks need the additional step of calling TextController.setTextTrack();
let config = textSourceBuffer.getConfig();
let fragmentModel = config.fragmentModel;
let fragmentedTracks = config.fragmentedTracks;
let mediaInfosArr,
streamProcessor;
let oldTrackIdx = textTracks.getCurrentTrackIdx();
if (oldTrackIdx !== idx) {
textTracks.setModeForTrackIdx(oldTrackIdx, Constants.TEXT_HIDDEN);
textTracks.setCurrentTrackIdx(idx);
textTracks.setModeForTrackIdx(idx, Constants.TEXT_SHOWING);
let currentTrackInfo = textTracks.getCurrentTrackInfo();
if (currentTrackInfo && currentTrackInfo.isFragmented && !currentTrackInfo.isEmbedded) {
for (let i = 0; i < fragmentedTracks.length; i++) {
let mediaInfo = fragmentedTracks[i];
if (currentTrackInfo.lang === mediaInfo.lang && currentTrackInfo.index === mediaInfo.index &&
(currentTrackInfo.label ? currentTrackInfo.label === mediaInfo.id : true)) {
let currentFragTrack = mediaController.getCurrentTrackFor(Constants.FRAGMENTED_TEXT, streamController.getActiveStreamInfo());
if (mediaInfo !== currentFragTrack) {
fragmentModel.abortRequests();
fragmentModel.removeExecutedRequestsBeforeTime();
textSourceBuffer.remove();
textTracks.deleteCuesFromTrackIdx(oldTrackIdx);
mediaController.setTrack(mediaInfo);
textSourceBuffer.setCurrentFragmentedTrackIdx(i);
}
}
}
} else if (currentTrackInfo && !currentTrackInfo.isFragmented) {
const streamProcessors = streamController.getActiveStreamProcessors();
for (let i = 0; i < streamProcessors.length; i++) {
if (streamProcessors[i].getType() === Constants.TEXT) {
streamProcessor = streamProcessors[i];
mediaInfosArr = streamProcessor.getMediaInfoArr();
break;
}
}
if (streamProcessor && mediaInfosArr) {
for (let i = 0; i < mediaInfosArr.length; i++) {
if (mediaInfosArr[i].index === currentTrackInfo.index && mediaInfosArr[i].lang === currentTrackInfo.lang) {
streamProcessor.selectMediaInfo(mediaInfosArr[i]);
break;
}
}
}
}
}
allTracksAreDisabled = idx === -1 ? true : false;
}
function getCurrentTrackIdx() {
return textTracks.getCurrentTrackIdx();
}
function resetInitialSettings() {
allTracksAreDisabled = false;
}
function reset() {
resetInitialSettings();
textSourceBuffer.resetEmbedded();
}
instance = {
setConfig: setConfig,
getTextSourceBuffer: getTextSourceBuffer,
getAllTracksAreDisabled: getAllTracksAreDisabled,
addEmbeddedTrack: addEmbeddedTrack,
getTextDefaultLanguage: getTextDefaultLanguage,
setTextDefaultLanguage: setTextDefaultLanguage,
setTextDefaultEnabled: setTextDefaultEnabled,
getTextDefaultEnabled: getTextDefaultEnabled,
enableText: enableText,
isTextEnabled: isTextEnabled,
setTextTrack: setTextTrack,
getCurrentTrackIdx: getCurrentTrackIdx,
enableForcedTextStreaming: enableForcedTextStreaming,
reset: reset
};
setup();
return instance;
}
TextController.__dashjs_factory_name = 'TextController';
export default FactoryMaker.getSingletonFactory(TextController);