/
ManifestUpdater.js
312 lines (264 loc) · 11 KB
/
ManifestUpdater.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
/**
* 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 EventBus from '../core/EventBus.js';
import Events from '../core/events/Events.js';
import MediaPlayerEvents from '../streaming/MediaPlayerEvents.js';
import FactoryMaker from '../core/FactoryMaker.js';
import Debug from '../core/Debug.js';
import Errors from '../core/errors/Errors.js';
import DashConstants from '../dash/constants/DashConstants.js';
import URLUtils from './utils/URLUtils.js';
import LocationSelector from './utils/LocationSelector.js';
function ManifestUpdater() {
const context = this.context;
const eventBus = EventBus(context).getInstance();
const urlUtils = URLUtils(context).getInstance();
let instance,
logger,
refreshDelay,
refreshTimer,
isPaused,
isStopped,
isUpdating,
manifestLoader,
manifestModel,
locationSelector,
adapter,
errHandler,
contentSteeringController,
settings;
function setup() {
logger = Debug(context).getInstance().getLogger(instance);
locationSelector = LocationSelector(context).create();
}
function setConfig(config) {
if (!config) return;
if (config.manifestModel) {
manifestModel = config.manifestModel;
}
if (config.adapter) {
adapter = config.adapter;
}
if (config.manifestLoader) {
manifestLoader = config.manifestLoader;
}
if (config.errHandler) {
errHandler = config.errHandler;
}
if (config.locationSelector) {
locationSelector = config.locationSelector;
}
if (config.settings) {
settings = config.settings;
}
if (config.contentSteeringController) {
contentSteeringController = config.contentSteeringController;
}
}
function initialize() {
resetInitialSettings();
eventBus.on(Events.STREAMS_COMPOSED, onStreamsComposed, this);
eventBus.on(MediaPlayerEvents.PLAYBACK_STARTED, onPlaybackStarted, this);
eventBus.on(MediaPlayerEvents.PLAYBACK_PAUSED, onPlaybackPaused, this);
eventBus.on(Events.INTERNAL_MANIFEST_LOADED, onManifestLoaded, this);
}
function setManifest(manifest) {
update(manifest);
}
function resetInitialSettings() {
refreshDelay = NaN;
isUpdating = false;
isPaused = true;
isStopped = false;
stopManifestRefreshTimer();
}
function reset() {
eventBus.off(MediaPlayerEvents.PLAYBACK_STARTED, onPlaybackStarted, this);
eventBus.off(MediaPlayerEvents.PLAYBACK_PAUSED, onPlaybackPaused, this);
eventBus.off(Events.STREAMS_COMPOSED, onStreamsComposed, this);
eventBus.off(Events.INTERNAL_MANIFEST_LOADED, onManifestLoaded, this);
resetInitialSettings();
}
function stopManifestRefreshTimer() {
if (refreshTimer !== null) {
clearTimeout(refreshTimer);
refreshTimer = null;
}
}
function startManifestRefreshTimer(delay) {
stopManifestRefreshTimer();
if (isStopped) {
return;
}
if (isNaN(delay) && !isNaN(refreshDelay)) {
delay = refreshDelay * 1000;
}
if (!isNaN(delay)) {
logger.debug('Refresh manifest in ' + delay + ' milliseconds.');
refreshTimer = setTimeout(onRefreshTimer, delay);
}
}
function refreshManifest(ignorePatch = false) {
isUpdating = true;
const manifest = manifestModel.getValue();
// default to the original url in the manifest
let url = manifest.url;
// Check for PatchLocation and Location alternatives
let serviceLocation = null;
const availablePatchLocations = adapter.getPatchLocation(manifest);
const patchLocation = locationSelector.select(availablePatchLocations);
let queryParams = null;
if (patchLocation && !ignorePatch) {
url = patchLocation.url;
serviceLocation = patchLocation.serviceLocation;
queryParams = patchLocation.queryParams;
} else {
const availableMpdLocations = _getAvailableMpdLocations(manifest);
const mpdLocation = locationSelector.select(availableMpdLocations);
if (mpdLocation) {
url = mpdLocation.url;
serviceLocation = mpdLocation.serviceLocation;
queryParams = mpdLocation.queryParams;
}
}
// if one of the alternatives was relative, convert to absolute
if (urlUtils.isRelative(url)) {
url = urlUtils.resolve(url, manifest.url);
}
manifestLoader.load(url, serviceLocation, queryParams);
}
function _getAvailableMpdLocations(manifest) {
const manifestLocations = adapter.getLocation(manifest);
const synthesizedElements = contentSteeringController.getSynthesizedLocationElements(manifestLocations);
return manifestLocations.concat(synthesizedElements);
}
function update(manifest) {
if (!manifest) {
// successful update with no content implies existing manifest remains valid
manifest = manifestModel.getValue();
// override load time to avoid invalid latency tracking and ensure update cadence
manifest.loadedTime = new Date();
} else if (adapter.getIsPatch(manifest)) {
// with patches the in-memory manifest is our base
let patch = manifest;
manifest = manifestModel.getValue();
// check for patch validity
let isPatchValid = adapter.isPatchValid(manifest, patch);
let patchSuccessful = isPatchValid;
if (isPatchValid) {
// grab publish time before update
let publishTime = adapter.getPublishTime(manifest);
// apply validated patch to manifest
adapter.applyPatchToManifest(manifest, patch);
// get the updated publish time
let updatedPublishTime = adapter.getPublishTime(manifest);
// ensure the patch properly updated the in-memory publish time
patchSuccessful = publishTime.getTime() !== updatedPublishTime.getTime();
}
// if the patch failed to apply, force a full manifest refresh
if (!patchSuccessful) {
logger.debug('Patch provided is invalid, performing full manifest refresh');
refreshManifest(true);
return;
}
// override load time to avoid invalid latency tracking and ensure update cadence
manifest.loadedTime = new Date();
}
// See DASH-IF IOP v4.3 section 4.6.4 "Transition Phase between Live and On-Demand"
// Stop manifest update, ignore static manifest and signal end of dynamic stream to detect end of stream
if (manifestModel.getValue() && manifestModel.getValue().type === DashConstants.DYNAMIC && manifest.type === DashConstants.STATIC) {
eventBus.trigger(Events.DYNAMIC_TO_STATIC);
isUpdating = false;
isStopped = true;
return;
}
manifestModel.setValue(manifest);
const date = new Date();
const latencyOfLastUpdate = (date.getTime() - manifest.loadedTime.getTime()) / 1000;
refreshDelay = adapter.getManifestUpdatePeriod(manifest, latencyOfLastUpdate);
// setTimeout uses a 32 bit number to store the delay. Any number greater than it
// will cause event associated with setTimeout to trigger immediately
if (refreshDelay * 1000 > 0x7FFFFFFF) {
refreshDelay = 0x7FFFFFFF / 1000;
}
eventBus.trigger(Events.MANIFEST_UPDATED, { manifest: manifest });
logger.info('Manifest has been refreshed at ' + date + '[' + date.getTime() / 1000 + '] ');
if (!isPaused) {
startManifestRefreshTimer();
}
}
function onRefreshTimer() {
if (isPaused) {
return;
}
if (isUpdating) {
startManifestRefreshTimer(settings.get().streaming.manifestUpdateRetryInterval);
return;
}
refreshManifest();
}
function onManifestLoaded(e) {
if (!e.error) {
update(e.manifest);
} else if (e.error.code === Errors.MANIFEST_LOADER_PARSING_FAILURE_ERROR_CODE) {
errHandler.error(e.error);
}
}
function onPlaybackStarted(/*e*/) {
isPaused = false;
startManifestRefreshTimer();
}
function onPlaybackPaused(/*e*/) {
isPaused = !settings.get().streaming.scheduling.scheduleWhilePaused;
if (isPaused) {
stopManifestRefreshTimer();
}
}
function onStreamsComposed(/*e*/) {
// When streams are ready we can consider manifest update completed. Resolve the update promise.
isUpdating = false;
}
function getIsUpdating() {
return isUpdating;
}
instance = {
initialize,
setManifest,
refreshManifest,
getIsUpdating,
setConfig,
reset
};
setup();
return instance;
}
ManifestUpdater.__dashjs_factory_name = 'ManifestUpdater';
export default FactoryMaker.getClassFactory(ManifestUpdater);