Skip to content

Commit

Permalink
Fix React Native createObjectURL polyfill incompatibility (shaka-proj…
Browse files Browse the repository at this point in the history
…ect#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 shaka-project#1842
  • Loading branch information
nitro404 authored and Kevin Scroggins committed Apr 15, 2019
1 parent 80bf991 commit 9a9ffd8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
12 changes: 11 additions & 1 deletion lib/media/media_source_engine.js
Expand Up @@ -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.
Expand All @@ -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;
};
Expand Down
8 changes: 5 additions & 3 deletions test/media/media_source_engine_unit.js
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -176,7 +178,7 @@ describe('MediaSourceEngine', function() {
});

afterAll(function() {
window.URL.createObjectURL = originalCreateObjectURL;
shaka.media.MediaSourceEngine.createObjectURL = originalCreateObjectURL;
window.MediaSource = originalMediaSource;
});

Expand Down

0 comments on commit 9a9ffd8

Please sign in to comment.