From 9a9ffd8fe793b34ae353a7f9befe312f0842d107 Mon Sep 17 00:00:00 2001 From: Kevin Scroggins Date: Tue, 19 Mar 2019 23:07:13 -0400 Subject: [PATCH] Fix React Native createObjectURL polyfill incompatibility (#1845) React Native introduces its own polyfill for window.URL.createObjectURL which is not compatible with Shaka. Encapsulating a reference to the original function inside of the MediaSourceEngine circumvents this issue. Closes #1842 --- lib/media/media_source_engine.js | 12 +++++++++++- test/media/media_source_engine_unit.js | 8 +++++--- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/lib/media/media_source_engine.js b/lib/media/media_source_engine.js index e6e51dcd0c..e75c7a217a 100644 --- a/lib/media/media_source_engine.js +++ b/lib/media/media_source_engine.js @@ -85,6 +85,16 @@ shaka.media.MediaSourceEngine = function(video) { }; +/** + * Internal reference to window.URL.createObjectURL function to avoid + * compatibility issues with other libraries and frameworks such as React + * Native. For use in unit tests only, not meant for external use. + * + * @type {function(?):string} + */ +shaka.media.MediaSourceEngine.createObjectURL = window.URL.createObjectURL; + + /** * Create a MediaSource object, attach it to the video element, and return it. * Resolves the given promise when the MediaSource is ready. @@ -99,7 +109,7 @@ shaka.media.MediaSourceEngine.prototype.createMediaSource = function(p) { // Set up MediaSource on the video element. this.eventManager_.listenOnce(mediaSource, 'sourceopen', p.resolve); - this.video_.src = window.URL.createObjectURL(mediaSource); + this.video_.src = shaka.media.MediaSourceEngine.createObjectURL(mediaSource); return mediaSource; }; diff --git a/test/media/media_source_engine_unit.js b/test/media/media_source_engine_unit.js index 6a720f6a21..b819f57550 100644 --- a/test/media/media_source_engine_unit.js +++ b/test/media/media_source_engine_unit.js @@ -150,7 +150,8 @@ describe('MediaSourceEngine', function() { }); describe('constructor', function() { - const originalCreateObjectURL = window.URL.createObjectURL; + const originalCreateObjectURL = + shaka.media.MediaSourceEngine.createObjectURL; const originalMediaSource = window.MediaSource; /** @type {jasmine.Spy} */ let createObjectURLSpy; @@ -165,7 +166,8 @@ describe('MediaSourceEngine', function() { createObjectURLSpy = jasmine.createSpy('createObjectURL'); createObjectURLSpy.and.returnValue('blob:foo'); - window.URL.createObjectURL = Util.spyFunc(createObjectURLSpy); + shaka.media.MediaSourceEngine.createObjectURL = + Util.spyFunc(createObjectURLSpy); let mediaSourceSpy = jasmine.createSpy('MediaSource').and.callFake(() => { return mockMediaSource; @@ -176,7 +178,7 @@ describe('MediaSourceEngine', function() { }); afterAll(function() { - window.URL.createObjectURL = originalCreateObjectURL; + shaka.media.MediaSourceEngine.createObjectURL = originalCreateObjectURL; window.MediaSource = originalMediaSource; });