-
-
Notifications
You must be signed in to change notification settings - Fork 4k
/
a-assets.js
278 lines (248 loc) · 8.59 KB
/
a-assets.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
var ANode = require('./a-node');
var bind = require('../utils/bind');
var debug = require('../utils/debug');
var registerElement = require('./a-register-element').registerElement;
var THREE = require('../lib/three');
var fileLoader = new THREE.FileLoader();
var warn = debug('core:a-assets:warn');
/**
* Asset management system. Handles blocking on asset loading.
*/
module.exports = registerElement('a-assets', {
prototype: Object.create(ANode.prototype, {
createdCallback: {
value: function () {
this.isAssets = true;
this.fileLoader = fileLoader;
this.timeout = null;
}
},
attachedCallback: {
value: function () {
var self = this;
var i;
var loaded = [];
var mediaEl;
var mediaEls;
var imgEl;
var imgEls;
var timeout;
if (!this.parentNode.isScene) {
throw new Error('<a-assets> must be a child of a <a-scene>.');
}
// Wait for <img>s.
imgEls = this.querySelectorAll('img');
for (i = 0; i < imgEls.length; i++) {
imgEl = fixUpMediaElement(imgEls[i]);
loaded.push(new Promise(function (resolve, reject) {
// Set in cache because we won't be needing to call three.js loader if we have.
// a loaded media element.
THREE.Cache.files[imgEls[i].getAttribute('src')] = imgEl;
imgEl.onload = resolve;
imgEl.onerror = reject;
}));
}
// Wait for <audio>s and <video>s.
mediaEls = this.querySelectorAll('audio, video');
for (i = 0; i < mediaEls.length; i++) {
mediaEl = fixUpMediaElement(mediaEls[i]);
if (!mediaEl.src && !mediaEl.srcObject) {
warn('Audio/video asset has neither `src` nor `srcObject` attributes.');
}
loaded.push(mediaElementLoaded(mediaEl));
}
// Trigger loaded for scene to start rendering.
Promise.all(loaded).then(bind(this.load, this));
// Timeout to start loading anyways.
timeout = parseInt(this.getAttribute('timeout'), 10) || 3000;
this.timeout = setTimeout(function () {
if (self.hasLoaded) { return; }
warn('Asset loading timed out in ', timeout, 'ms');
self.emit('timeout');
self.load();
}, timeout);
}
},
detachedCallback: {
value: function () {
if (this.timeout) { clearTimeout(this.timeout); }
}
},
load: {
value: function () {
ANode.prototype.load.call(this, null, function waitOnFilter (el) {
return el.isAssetItem && el.hasAttribute('src');
});
}
}
})
});
/**
* Preload using XHRLoader for any type of asset.
*/
registerElement('a-asset-item', {
prototype: Object.create(ANode.prototype, {
createdCallback: {
value: function () {
this.data = null;
this.isAssetItem = true;
}
},
attachedCallback: {
value: function () {
var self = this;
var src = this.getAttribute('src');
fileLoader.setResponseType(
this.getAttribute('response-type') || inferResponseType(src));
fileLoader.load(src, function handleOnLoad (response) {
self.data = response;
/*
Workaround for a Chrome bug. If another XHR is sent to the same url before the
previous one closes, the second request never finishes.
setTimeout finishes the first request and lets the logic triggered by load open
subsequent requests.
setTimeout can be removed once the fix for the bug below ships:
https://bugs.chromium.org/p/chromium/issues/detail?id=633696&q=component%3ABlink%3ENetwork%3EXHR%20&colspec=ID%20Pri%20M%20Stars%20ReleaseBlock%20Component%20Status%20Owner%20Summary%20OS%20Modified
*/
setTimeout(function load () { ANode.prototype.load.call(self); });
}, function handleOnProgress (xhr) {
self.emit('progress', {
loadedBytes: xhr.loaded,
totalBytes: xhr.total,
xhr: xhr
});
}, function handleOnError (xhr) {
self.emit('error', {xhr: xhr});
});
}
}
})
});
/**
* Create a Promise that resolves once the media element has finished buffering.
*
* @param {Element} el - HTMLMediaElement.
* @returns {Promise}
*/
function mediaElementLoaded (el) {
if (!el.hasAttribute('autoplay') && el.getAttribute('preload') !== 'auto') {
return;
}
// If media specifies autoplay or preload, wait until media is completely buffered.
return new Promise(function (resolve, reject) {
if (el.readyState === 4) { return resolve(); } // Already loaded.
if (el.error) { return reject(); } // Error.
el.addEventListener('loadeddata', checkProgress, false);
el.addEventListener('progress', checkProgress, false);
el.addEventListener('error', reject, false);
function checkProgress () {
// Add up the seconds buffered.
var secondsBuffered = 0;
for (var i = 0; i < el.buffered.length; i++) {
secondsBuffered += el.buffered.end(i) - el.buffered.start(i);
}
// Compare seconds buffered to media duration.
if (secondsBuffered >= el.duration) {
// Set in cache because we won't be needing to call three.js loader if we have.
// a loaded media element.
// Store video elements only. three.js loader is used for audio elements.
// See assetParse too.
if (el.tagName === 'VIDEO') {
THREE.Cache.files[el.getAttribute('src')] = el;
}
resolve();
}
}
});
}
/**
* Automatically add attributes to media elements where convenient.
* crossorigin, playsinline.
*/
function fixUpMediaElement (mediaEl) {
// Cross-origin.
var newMediaEl = setCrossOrigin(mediaEl);
// Plays inline for mobile.
if (newMediaEl.tagName && newMediaEl.tagName.toLowerCase() === 'video') {
newMediaEl.setAttribute('playsinline', '');
newMediaEl.setAttribute('webkit-playsinline', '');
}
if (newMediaEl !== mediaEl) {
mediaEl.parentNode.appendChild(newMediaEl);
mediaEl.parentNode.removeChild(mediaEl);
}
return newMediaEl;
}
/**
* Automatically set `crossorigin` if not defined on the media element.
* If it is not defined, we must create and re-append a new media element <img> and
* have the browser re-request it with `crossorigin` set.
*
* @param {Element} Media element (e.g., <img>, <audio>, <video>).
* @returns {Element} Media element to be used to listen to for loaded events.
*/
function setCrossOrigin (mediaEl) {
var newMediaEl;
var src;
// Already has crossorigin set.
if (mediaEl.hasAttribute('crossorigin')) { return mediaEl; }
src = mediaEl.getAttribute('src');
if (src !== null) {
// Does not have protocol.
if (src.indexOf('://') === -1) { return mediaEl; }
// Determine if cross origin is actually needed.
if (extractDomain(src) === window.location.host) { return mediaEl; }
}
warn('Cross-origin element (e.g., <img>) was requested without `crossorigin` set. ' +
'A-Frame will re-request the asset with `crossorigin` attribute set. ' +
'Please set `crossorigin` on the element (e.g., <img crossorigin="anonymous">)', src);
mediaEl.crossOrigin = 'anonymous';
newMediaEl = mediaEl.cloneNode(true);
return newMediaEl;
}
/**
* Extract domain out of URL.
*
* @param {string} url
* @returns {string}
*/
function extractDomain (url) {
// Find and remove protocol (e.g., http, ftp, etc.) to get domain.
var domain = url.indexOf('://') > -1 ? url.split('/')[2] : url.split('/')[0];
// Find and remove port number.
return domain.substring(0, domain.indexOf(':'));
}
/**
* Infer response-type attribute from src.
* Default is text (default XMLHttpRequest.responseType)
* and arraybuffer for .glb files.
*
* @param {string} src
* @returns {string}
*/
function inferResponseType (src) {
var fileName = getFileNameFromURL(src);
var dotLastIndex = fileName.lastIndexOf('.');
if (dotLastIndex >= 0) {
var extension = fileName.slice(dotLastIndex, src.search(/\?|#|$/));
if (extension === '.glb') {
return 'arraybuffer';
}
}
return 'text';
}
module.exports.inferResponseType = inferResponseType;
/**
* Extract filename from URL
*
* @param {string} url
* @returns {string}
*/
function getFileNameFromURL (url) {
var parser = document.createElement('a');
parser.href = url;
var query = parser.search.replace(/^\?/, '');
var filePath = url.replace(query, '').replace('?', '');
return filePath.substring(filePath.lastIndexOf('/') + 1);
}
module.exports.getFileNameFromURL = getFileNameFromURL;