/
RepresentationController.js
331 lines (273 loc) · 12.8 KB
/
RepresentationController.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/**
* 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 '../../streaming/constants/Constants';
import DashJSError from '../../streaming/vo/DashJSError';
import FactoryMaker from '../../core/FactoryMaker';
function RepresentationController(config) {
config = config || {};
const eventBus = config.eventBus;
const events = config.events;
const errors = config.errors;
const indexHandler = config.indexHandler;
const abrController = config.abrController;
const dashMetrics = config.dashMetrics;
const playbackController = config.playbackController;
const timelineConverter = config.timelineConverter;
const type = config.type;
const streamId = config.streamId;
const dashConstants = config.dashConstants;
let instance,
realAdaptation,
updating,
voAvailableRepresentations,
currentVoRepresentation;
function setup() {
resetInitialSettings();
eventBus.on(events.QUALITY_CHANGE_REQUESTED, onQualityChanged, instance);
eventBus.on(events.REPRESENTATION_UPDATE_COMPLETED, onRepresentationUpdated, instance);
eventBus.on(events.WALLCLOCK_TIME_UPDATED, onWallclockTimeUpdated, instance);
eventBus.on(events.MANIFEST_VALIDITY_CHANGED, onManifestValidityChanged, instance);
}
function checkConfig() {
if (!abrController || !dashMetrics || !playbackController || !timelineConverter) {
throw new Error(Constants.MISSING_CONFIG_ERROR);
}
}
function getData() {
return realAdaptation;
}
function isUpdating() {
return updating;
}
function getCurrentRepresentation() {
return currentVoRepresentation;
}
function resetInitialSettings() {
realAdaptation = null;
updating = true;
voAvailableRepresentations = [];
}
function reset() {
eventBus.off(events.QUALITY_CHANGE_REQUESTED, onQualityChanged, instance);
eventBus.off(events.REPRESENTATION_UPDATE_COMPLETED, onRepresentationUpdated, instance);
eventBus.off(events.WALLCLOCK_TIME_UPDATED, onWallclockTimeUpdated, instance);
eventBus.off(events.MANIFEST_VALIDITY_CHANGED, onManifestValidityChanged, instance);
resetInitialSettings();
}
function getType() {
return type;
}
function getStreamId() {
return streamId;
}
function updateData(newRealAdaptation, availableRepresentations, type, quality) {
checkConfig();
startDataUpdate();
voAvailableRepresentations = availableRepresentations;
currentVoRepresentation = getRepresentationForQuality(quality);
// In case segmentDuration is not set (ex. SegmentTimeline), request a segment to determine the segmentDuration
if (currentVoRepresentation && isNaN(currentVoRepresentation.segmentDuration)) {
indexHandler.getSegmentRequestForTime(null, currentVoRepresentation, 0);
}
realAdaptation = newRealAdaptation;
if (type !== Constants.VIDEO && type !== Constants.AUDIO && type !== Constants.FRAGMENTED_TEXT) {
endDataUpdate();
return;
}
updateAvailabilityWindow(playbackController.getIsDynamic(), true);
}
function addRepresentationSwitch() {
checkConfig();
const now = new Date();
const currentRepresentation = getCurrentRepresentation();
const currentVideoTimeMs = playbackController.getTime() * 1000;
if (currentRepresentation) {
dashMetrics.addRepresentationSwitch(currentRepresentation.adaptation.type, now, currentVideoTimeMs, currentRepresentation.id);
}
}
function getRepresentationForQuality(quality) {
return quality === null || quality === undefined || quality >= voAvailableRepresentations.length ? null : voAvailableRepresentations[quality];
}
function getQualityForRepresentation(voRepresentation) {
return voAvailableRepresentations.indexOf(voRepresentation);
}
function isAllRepresentationsUpdated() {
for (let i = 0, ln = voAvailableRepresentations.length; i < ln; i++) {
let segmentInfoType = voAvailableRepresentations[i].segmentInfoType;
if (voAvailableRepresentations[i].segmentAvailabilityRange === null || !voAvailableRepresentations[i].hasInitialization() ||
((segmentInfoType === dashConstants.SEGMENT_BASE || segmentInfoType === dashConstants.BASE_URL) && !voAvailableRepresentations[i].segments)
) {
return false;
}
}
return true;
}
function setExpectedLiveEdge(liveEdge) {
timelineConverter.setExpectedLiveEdge(liveEdge);
dashMetrics.updateManifestUpdateInfo({presentationStartTime: liveEdge});
}
function updateRepresentation(representation, isDynamic) {
// In case segmentDuration is not set (ex. SegmentTimeline), request a segment to determine the segmentDuration
if (isNaN(representation.segmentDuration)) {
indexHandler.getSegmentRequestForTime(null, representation, 0);
}
representation.segmentAvailabilityRange = timelineConverter.calcSegmentAvailabilityRange(representation, isDynamic);
if ((representation.segmentAvailabilityRange.end < representation.segmentAvailabilityRange.start) && !representation.useCalculatedLiveEdgeTime) {
let error = new DashJSError(errors.SEGMENTS_UNAVAILABLE_ERROR_CODE, errors.SEGMENTS_UNAVAILABLE_ERROR_MESSAGE, {availabilityDelay: representation.segmentAvailabilityRange.start - representation.segmentAvailabilityRange.end});
endDataUpdate(error);
return;
}
if (isDynamic) {
setExpectedLiveEdge(representation.segmentAvailabilityRange.end);
}
}
function updateAvailabilityWindow(isDynamic, notifyUpdate) {
checkConfig();
for (let i = 0, ln = voAvailableRepresentations.length; i < ln; i++) {
updateRepresentation(voAvailableRepresentations[i], isDynamic);
if (notifyUpdate) {
eventBus.trigger(events.REPRESENTATION_UPDATE_STARTED, { sender: instance, representation: voAvailableRepresentations[i]});
}
}
}
function resetAvailabilityWindow() {
voAvailableRepresentations.forEach(rep => {
rep.segmentAvailabilityRange = null;
});
}
function startDataUpdate() {
updating = true;
eventBus.trigger(events.DATA_UPDATE_STARTED, { sender: instance });
}
function endDataUpdate(error) {
updating = false;
let eventArg = {sender: instance, data: realAdaptation, currentRepresentation: currentVoRepresentation};
if (error) {
eventArg.error = error;
}
eventBus.trigger(events.DATA_UPDATE_COMPLETED, eventArg);
}
function postponeUpdate(postponeTimePeriod) {
let delay = postponeTimePeriod;
let update = function () {
if (isUpdating()) return;
startDataUpdate();
// clear the segmentAvailabilityRange for all reps.
// this ensures all are updated before the live edge search starts
resetAvailabilityWindow();
updateAvailabilityWindow(playbackController.getIsDynamic(), true);
};
eventBus.trigger(events.AST_IN_FUTURE, { delay: delay });
setTimeout(update, delay);
}
function onRepresentationUpdated(e) {
if (e.sender.getType() !== getType() || e.sender.getStreamInfo().id !== streamId || !isUpdating()) return;
if (e.error) {
endDataUpdate(e.error);
return;
}
let streamInfo = e.sender.getStreamInfo();
let r = e.representation;
let manifestUpdateInfo = dashMetrics.getCurrentManifestUpdate();
let alreadyAdded = false;
let postponeTimePeriod = 0;
let repInfo,
err,
repSwitch;
if (r.adaptation.period.mpd.manifest.type === dashConstants.DYNAMIC && !r.adaptation.period.mpd.manifest.ignorePostponeTimePeriod) {
let segmentAvailabilityTimePeriod = r.segmentAvailabilityRange.end - r.segmentAvailabilityRange.start;
// We must put things to sleep unless till e.g. the startTime calculation in ScheduleController.onLiveEdgeSearchCompleted fall after the segmentAvailabilityRange.start
let liveDelay = playbackController.computeLiveDelay(currentVoRepresentation.segmentDuration, streamInfo.manifestInfo.DVRWindowSize);
postponeTimePeriod = (liveDelay - segmentAvailabilityTimePeriod) * 1000;
}
if (postponeTimePeriod > 0) {
postponeUpdate(postponeTimePeriod);
err = new DashJSError(errors.SEGMENTS_UPDATE_FAILED_ERROR_CODE, errors.SEGMENTS_UPDATE_FAILED_ERROR_MESSAGE);
endDataUpdate(err);
return;
}
if (manifestUpdateInfo) {
for (let i = 0; i < manifestUpdateInfo.representationInfo.length; i++) {
repInfo = manifestUpdateInfo.representationInfo[i];
if (repInfo.index === r.index && repInfo.mediaType === getType()) {
alreadyAdded = true;
break;
}
}
if (!alreadyAdded) {
dashMetrics.addManifestUpdateRepresentationInfo(r, getType());
}
}
if (isAllRepresentationsUpdated()) {
abrController.setPlaybackQuality(getType(), streamInfo, getQualityForRepresentation(currentVoRepresentation));
dashMetrics.updateManifestUpdateInfo({latency: currentVoRepresentation.segmentAvailabilityRange.end - playbackController.getTime()});
repSwitch = dashMetrics.getCurrentRepresentationSwitch(getCurrentRepresentation().adaptation.type);
if (!repSwitch) {
addRepresentationSwitch();
}
endDataUpdate();
}
}
function onWallclockTimeUpdated(e) {
if (e.isDynamic) {
updateAvailabilityWindow(e.isDynamic);
}
}
function onQualityChanged(e) {
if (e.mediaType !== getType() || streamId !== e.streamInfo.id) return;
currentVoRepresentation = getRepresentationForQuality(e.newQuality);
addRepresentationSwitch();
}
function onManifestValidityChanged(e) {
if (e.newDuration) {
const representation = getCurrentRepresentation();
if (representation && representation.adaptation.period) {
const period = representation.adaptation.period;
period.duration = e.newDuration;
}
}
}
instance = {
getData: getData,
isUpdating: isUpdating,
updateData: updateData,
updateRepresentation: updateRepresentation,
getCurrentRepresentation: getCurrentRepresentation,
getRepresentationForQuality: getRepresentationForQuality,
getType: getType,
getStreamId: getStreamId,
reset: reset
};
setup();
return instance;
}
RepresentationController.__dashjs_factory_name = 'RepresentationController';
export default FactoryMaker.getClassFactory(RepresentationController);