Skip to content
Snippets Groups Projects
Unverified Commit da53bb74 authored by Dominick D'Aniello's avatar Dominick D'Aniello Committed by GitHub
Browse files

Merge pull request #525 from mozilla/feature/multiple-video-fix

Don't use TextureCache for videos
parents 4655c70a c170dd7a
No related branches found
No related tags found
No related merge requests found
...@@ -161,6 +161,16 @@ function createImageTexture(url) { ...@@ -161,6 +161,16 @@ function createImageTexture(url) {
}); });
} }
function disposeTexture(texture) {
if (texture.image instanceof HTMLVideoElement) {
const video = texture.image;
video.pause();
video.src = "";
video.load();
}
texture.dispose();
}
class TextureCache { class TextureCache {
cache = new Map(); cache = new Map();
...@@ -193,13 +203,7 @@ class TextureCache { ...@@ -193,13 +203,7 @@ class TextureCache {
// console.log("release", src, cacheItem.count); // console.log("release", src, cacheItem.count);
if (cacheItem.count <= 0) { if (cacheItem.count <= 0) {
// Unload the video element to prevent it from continuing to play in the background // Unload the video element to prevent it from continuing to play in the background
if (cacheItem.texture.image instanceof HTMLVideoElement) { disposeTexture(cacheItem.texture);
const video = cacheItem.texture.image;
video.pause();
video.src = "";
video.load();
}
cacheItem.texture.dispose();
this.cache.delete(src); this.cache.delete(src);
} }
} }
...@@ -253,8 +257,12 @@ AFRAME.registerComponent("media-video", { ...@@ -253,8 +257,12 @@ AFRAME.registerComponent("media-video", {
}, },
remove() { remove() {
if (this.data.src) { if (this.mesh && this.mesh.material) {
textureCache.release(this.data.src); disposeTexture(this.mesh.material.map);
}
if (this.video) {
this.video.removeEventListener("pause", this.onPauseStateChange);
this.video.removeEventListener("play", this.onPauseStateChange);
} }
}, },
...@@ -265,24 +273,19 @@ AFRAME.registerComponent("media-video", { ...@@ -265,24 +273,19 @@ AFRAME.registerComponent("media-video", {
async updateTexture(src) { async updateTexture(src) {
let texture; let texture;
try { try {
if (textureCache.has(src)) { texture = await createVideoTexture(src);
texture = textureCache.retain(src);
} else {
texture = await createVideoTexture(src);
texture.audioSource = this.el.sceneEl.audioListener.context.createMediaElementSource(texture.image);
this.video = texture.image;
this.video.addEventListener("pause", this.onPauseStateChange); // No way to cancel promises, so if src has changed while we were creating the texture just throw it away.
this.video.addEventListener("play", this.onPauseStateChange); if (this.data.src !== src) {
disposeTexture(texture);
return;
}
textureCache.set(src, texture); texture.audioSource = this.el.sceneEl.audioListener.context.createMediaElementSource(texture.image);
this.video = texture.image;
// No way to cancel promises, so if src has changed while we were creating the texture just throw it away. this.video.addEventListener("pause", this.onPauseStateChange);
if (this.data.src !== src) { this.video.addEventListener("play", this.onPauseStateChange);
textureCache.release(src);
return;
}
}
const sound = new THREE.PositionalAudio(this.el.sceneEl.audioListener); const sound = new THREE.PositionalAudio(this.el.sceneEl.audioListener);
sound.setNodeSource(texture.audioSource); sound.setNodeSource(texture.audioSource);
...@@ -325,12 +328,10 @@ AFRAME.registerComponent("media-video", { ...@@ -325,12 +328,10 @@ AFRAME.registerComponent("media-video", {
if (!src || src === oldData.src) return; if (!src || src === oldData.src) return;
if (this.mesh && this.mesh.map) { this.remove();
if (this.mesh && this.mesh.material) {
this.mesh.material.map = null; this.mesh.material.map = null;
this.mesh.material.needsUpdate = true; this.mesh.material.needsUpdate = true;
if (this.mesh.map !== errorTexture) {
textureCache.release(oldData.src);
}
} }
this.updateTexture(src); this.updateTexture(src);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment