From ca73cac96eeb2bcd3562cfa37054a07c09d3a115 Mon Sep 17 00:00:00 2001 From: Brandon Casey <2381475+brandonocasey@users.noreply.github.com> Date: Thu, 18 Jun 2020 16:59:33 -0400 Subject: [PATCH] fix: Use revokeObjectURL dispose for created MSE blob urls (#849) --- src/videojs-http-streaming.js | 9 ++++++- test/videojs-http-streaming.test.js | 40 +++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/src/videojs-http-streaming.js b/src/videojs-http-streaming.js index 82cb1929..f1406b62 100644 --- a/src/videojs-http-streaming.js +++ b/src/videojs-http-streaming.js @@ -746,7 +746,9 @@ class VhsHandler extends Component { return; } - this.tech_.src(window.URL.createObjectURL(this.masterPlaylistController_.mediaSource)); + this.mediaSourceUrl_ = window.URL.createObjectURL(this.masterPlaylistController_.mediaSource); + + this.tech_.src(this.mediaSourceUrl_); } /** @@ -852,6 +854,11 @@ class VhsHandler extends Component { delete this.tech_.hls; } + if (this.mediaSourceUrl_ && window.URL.revokeObjectURL) { + window.URL.revokeObjectURL(this.mediaSourceUrl_); + this.mediaSourceUrl_ = null; + } + super.dispose(); } diff --git a/test/videojs-http-streaming.test.js b/test/videojs-http-streaming.test.js index 4d57e2f0..a2cc24c6 100644 --- a/test/videojs-http-streaming.test.js +++ b/test/videojs-http-streaming.test.js @@ -123,6 +123,46 @@ QUnit.module('VHS', { } }); +QUnit.test('mse urls are created and revoked', function(assert) { + const old = { + createObjectURL: window.URL.createObjectURL, + revokeObjectURL: window.URL.revokeObjectURL + }; + const ids = []; + + window.URL.createObjectURL = (...args) => { + const id = old.createObjectURL.apply(window.URL, args); + + ids.push(id); + return id; + }; + + window.URL.revokeObjectURL = (...args) => { + const index = ids.indexOf(args[0]); + + if (index !== -1) { + ids.splice(index, 1); + } + return old.revokeObjectURL.apply(window.URL, args); + }; + + this.player.src({ + src: 'manifest/playlist.m3u8', + type: 'application/vnd.apple.mpegurl' + }); + + this.clock.tick(1); + + assert.ok(ids.length > 0, 'object urls created'); + + this.player.dispose(); + + assert.equal(ids.length, 0, 'all object urls removed'); + + window.URL.createObjectURL = old.createObjectURL; + window.URL.revokeObjectURL = old.revokeObjectURL; +}); + QUnit.test('version is exported', function(assert) { this.player.src({ src: 'manifest/playlist.m3u8',